> ## Documentation Index
> Fetch the complete documentation index at: https://companyname-a7d5b98e-v2-pagination.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Basic values and literals in Fift

export const Aside = ({type = "note", title = "", icon = "", iconType = "regular", children}) => {
  const asideVariants = ["note", "tip", "caution", "danger"];
  const asideComponents = {
    note: {
      outerStyle: "border-sky-500/20 bg-sky-50/50 dark:border-sky-500/30 dark:bg-sky-500/10",
      innerStyle: "text-sky-900 dark:text-sky-200",
      calloutType: "note",
      icon: <svg width="14" height="14" viewBox="0 0 14 14" fill="currentColor" xmlns="http://www.w3.org/2000/svg" className="w-4 h-4 text-sky-500" aria-label="Note">
          <path fill-rule="evenodd" clip-rule="evenodd" d="M7 1.3C10.14 1.3 12.7 3.86 12.7 7C12.7 10.14 10.14 12.7 7 12.7C5.48908 12.6974 4.0408 12.096 2.97241 11.0276C1.90403 9.9592 1.30264 8.51092 1.3 7C1.3 3.86 3.86 1.3 7 1.3ZM7 0C3.14 0 0 3.14 0 7C0 10.86 3.14 14 7 14C10.86 14 14 10.86 14 7C14 3.14 10.86 0 7 0ZM8 3H6V8H8V3ZM8 9H6V11H8V9Z"></path>
        </svg>
    },
    tip: {
      outerStyle: "border-emerald-500/20 bg-emerald-50/50 dark:border-emerald-500/30 dark:bg-emerald-500/10",
      innerStyle: "text-emerald-900 dark:text-emerald-200",
      calloutType: "tip",
      icon: <svg width="11" height="14" viewBox="0 0 11 14" fill="currentColor" xmlns="http://www.w3.org/2000/svg" className="text-emerald-600 dark:text-emerald-400/80 w-3.5 h-auto" aria-label="Tip">
          <path d="M3.12794 12.4232C3.12794 12.5954 3.1776 12.7634 3.27244 12.907L3.74114 13.6095C3.88471 13.8248 4.21067 14 4.46964 14H6.15606C6.41415 14 6.74017 13.825 6.88373 13.6095L7.3508 12.9073C7.43114 12.7859 7.49705 12.569 7.49705 12.4232L7.50055 11.3513H3.12521L3.12794 12.4232ZM5.31288 0C2.52414 0.00875889 0.5 2.26889 0.5 4.78826C0.5 6.00188 0.949566 7.10829 1.69119 7.95492C2.14321 8.47011 2.84901 9.54727 3.11919 10.4557C3.12005 10.4625 3.12175 10.4698 3.12261 10.4771H7.50342C7.50427 10.4698 7.50598 10.463 7.50684 10.4557C7.77688 9.54727 8.48281 8.47011 8.93484 7.95492C9.67728 7.13181 10.1258 6.02703 10.1258 4.78826C10.1258 2.15486 7.9709 0.000106649 5.31288 0ZM7.94902 7.11267C7.52078 7.60079 6.99082 8.37878 6.6077 9.18794H4.02051C3.63739 8.37878 3.10743 7.60079 2.67947 7.11294C2.11997 6.47551 1.8126 5.63599 1.8126 4.78826C1.8126 3.09829 3.12794 1.31944 5.28827 1.3126C7.2435 1.3126 8.81315 2.88226 8.81315 4.78826C8.81315 5.63599 8.50688 6.47551 7.94902 7.11267ZM4.87534 2.18767C3.66939 2.18767 2.68767 3.16939 2.68767 4.37534C2.68767 4.61719 2.88336 4.81288 3.12521 4.81288C3.36705 4.81288 3.56274 4.61599 3.56274 4.37534C3.56274 3.6515 4.1515 3.06274 4.87534 3.06274C5.11719 3.06274 5.31288 2.86727 5.31288 2.62548C5.31288 2.38369 5.11599 2.18767 4.87534 2.18767Z"></path>
        </svg>
    },
    caution: {
      outerStyle: "border-amber-500/20 bg-amber-50/50 dark:border-amber-500/30 dark:bg-amber-500/10",
      innerStyle: "text-amber-900 dark:text-amber-200",
      calloutType: "warning",
      icon: <svg className="flex-none w-5 h-5 text-amber-400 dark:text-amber-300/80" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" aria-label="Warning">
          <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
        </svg>
    },
    danger: {
      outerStyle: "border-red-500/20 bg-red-50/50 dark:border-red-500/30 dark:bg-red-500/10",
      innerStyle: "text-red-900 dark:text-red-200",
      calloutType: "danger",
      icon: <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor" className="text-red-600 dark:text-red-400/80 w-4 h-4" aria-label="Danger">
          <path d="M17.1 292c-12.9-22.3-12.9-49.7 0-72L105.4 67.1c12.9-22.3 36.6-36 62.4-36l176.6 0c25.7 0 49.5 13.7 62.4 36L494.9 220c12.9 22.3 12.9 49.7 0 72L406.6 444.9c-12.9 22.3-36.6 36-62.4 36l-176.6 0c-25.7 0-49.5-13.7-62.4-36L17.1 292zm41.6-48c-4.3 7.4-4.3 16.6 0 24l88.3 152.9c4.3 7.4 12.2 12 20.8 12l176.6 0c8.6 0 16.5-4.6 20.8-12L453.4 268c4.3-7.4 4.3-16.6 0-24L365.1 91.1c-4.3-7.4-12.2-12-20.8-12l-176.6 0c-8.6 0-16.5 4.6-20.8 12L58.6 244zM256 128c13.3 0 24 10.7 24 24l0 112c0 13.3-10.7 24-24 24s-24-10.7-24-24l0-112c0-13.3 10.7-24 24-24zM224 352a32 32 0 1 1 64 0 32 32 0 1 1 -64 0z"></path>
        </svg>
    }
  };
  let variant = type;
  let gotInvalidVariant = false;
  if (!asideVariants.includes(type)) {
    gotInvalidVariant = true;
    variant = "danger";
  }
  const iconVariants = ["regular", "solid", "light", "thin", "sharp-solid", "duotone", "brands"];
  if (!iconVariants.includes(iconType)) {
    iconType = "regular";
  }
  return <>
      <div className={`callout my-4 px-5 py-4 overflow-hidden rounded-2xl flex gap-3 border ${asideComponents[variant].outerStyle}`} data-callout-type={asideComponents[variant].calloutType}>
        <div className="mt-0.5 w-4" data-component-part="callout-icon">
          {}
          {icon === "" ? asideComponents[variant].icon : <Icon icon={icon} iconType={iconType} size={14} />}
        </div>
        <div className={`text-sm prose min-w-0 w-full ${asideComponents[variant].innerStyle}`} data-component-part="callout-content">
          {gotInvalidVariant ? <p>
              <span className="font-bold">
                Invalid <code>type</code> passed!
              </span>
              <br />
              <span className="font-bold">Received: </span>
              {type}
              <br />
              <span className="font-bold">Expected one of: </span>
              {asideVariants.join(", ")}
            </p> : <>
              {title && <p className="font-bold">{title}</p>}
              {children}
            </>}
        </div>
      </div>
    </>;
};

<Aside type="note">
  The official smart contract language of TON Blockchain is [Tolk](/tolk/overview). Fift is now a **legacy** language, with its compiler no longer maintained.
</Aside>

## Integers

Fift recognizes integers in decimal, binary, and hexadecimal formats. Binary literals are prefixed by `0b`, hexadecimal literals are prefixed by `0x`, and decimal literals do not require a prefix.

Examples:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
0b1101     // Binary literal, denoting integer 13
13         // Decimal literal
0xd        // Hexadecimal literal, denoting integer 13
```

An integer literal may be prefixed by a minus sign `-` to change its sign; the minus sign is accepted both before and after the `0x` and `0b` prefixes.

Examples:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
-0b1101     // Binary literal, denoting integer -13
0b-1101     // Binary literal, denoting integer -13
-13         // Decimal literal
-0xd        // Hexadecimal literal, denoting integer -13
0x-d        // Hexadecimal literal, denoting integer -13
```

Integer literals are pushed into the Fift stack in the order of their appearance. For example, executing:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
1 0xa 3 -0b10
```

produces the stack:

```text theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
1 10 3 -2     // Rightmost number is the top of the stack
```

In other words, `1` is pushed first, followed by `0xa` (i.e., `10`), followed by `3`, and finally `-0b10` (i.e., `-2`), leaving `-0b10` at the top of the stack.

Fift has special syntax for decimal and common fractions. If a string consists of two valid integer literals separated by a slash `/`, then Fift interprets it as a fractional literal and represents it by two `Integer`s `p` and `q` in the stack, the numerator `p` and the denominator `q`.

For instance, `-17/12` pushes `-17` and `12` into the Fift stack, being thus equivalent to executing `-17 12`; and `-0x11/0b1100` does the same thing. Decimal, binary, and hexadecimal fractions, such as `2.39` or `-0x11.ef`, are also represented by two integers `p` and `q`, where `q` is a suitable power of the base (10, 2, or 16, respectively). For instance, `2.39` is equivalent to `239 100`, and `-0x11.ef` is equivalent to `-0x11ef 0x100`.

Such a representation of fractions is useful for defining *"rational arithmetic operations"*. For example, the following code defines two words `R-` and `R*` that carry out rational subtraction and multiplication, respectively. Word `R-` takes the stack `a b c d`, representing the fractions `a/b` and `c/d`, and produces the stack `(a*d-b*c) b*d`, representing the arithmetical subtraction `a/b - c/d = (a*d-b*c) / (b*d)`. Similarly, word `R*` takes the stack `a b c d`, representing the fractions `a/b` and `c/d`, and produces the stack `a*c b*d`, representing the arithmetical multiplication `a/b * c/d = (a*c) / (b*d)`.

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
// a b c d ---> (a*d-b*c) b*d
{ -rot over * 2swap tuck * rot - -rot * } : R-
// a b c d ---> a*c b*d
{ rot * -rot * swap } : R*
```

So that, executing `1.7 2/3 R- 5/2 R*` will produce the stack `155 60`, corresponding to the arithmetical operation `(1.7 - 2/3) * 5/2 = 31/30 * 5/2 = 155/60`.

For advanced details on integers, see the [Integers section](/languages/fift/whitepaper#2-8-integer-and-fractional-constants%2C-or-literals) in the Fift whitepaper.

## Strings

A string is any sequence of UTF-8 characters enclosed by double quotes `"`. Single line strings can be introduced by the character `"`, which scans the remainder of the line until the next `"` character. Single line strings do not support new line characters.

When Fift recognizes a string, it pushes it into the Fift stack as a value of type `String`. For instance, `"Hello, world!"` pushes the corresponding `String` into the stack:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
"Hello, world!"
// String "Hello, world!" is at the top of the stack
```

For multi-line strings, Fift has the word `$<<"""` which scans all the next lines until it finds triple quote characters in a new line `"""`, which finalize the multi-line string. For instance,

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
$<<"""
Hello, world!
This is a multi-line string
    Another line...
"""
// The following multi-line string is at the top of the stack:
// "Hello, world!
// This is a multi-line string
//     Another line...
// "
```

<Aside>
  For multiline strings, words `$<<"""` and `"""` must be by themselves in new lines alone. For instance,

  ```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
  $<<""" Hello, world!
  This is a multi-line string
      Another line...
  """
  ```

  does not work because `$<<"""` is not in a line alone. Also,

  ```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
  $<<"""
  Hello, world!
  This is a multi-line string
      Another line... """
  ```

  does not work because `"""` is not in a line alone.
</Aside>

## Booleans

As described in the [Types page](/languages/fift/types), Fift does not have a separate type for booleans. Instead, booleans are emulated using `Integer`s, where `-1` represents truth, and `0` falsehood. Comparison primitives normally return `-1` to indicate success and `0` otherwise.

Fift has the constants `true` and `false` to push these special integers into the stack:

| Word    | Description                 |
| :------ | :-------------------------- |
| `true`  | pushes `-1` into the stack. |
| `false` | pushes `0` into the stack.  |

Certain operations, like conditional execution of blocks and loops with exit conditions, extend the notion of "true" to any non-zero integer. However, if the intention is to work with bitwise operations over booleans, any non-zero integer must first be transformed into `-1`. Fift has the word `0<>` that transforms any non-zero integer into `-1`, and leaves `0` unchanged.

For instance:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
5 0<>     // Produces -1 at the top of the stack
-2 0<>    // Produces -1 at the top of the stack
0 0<>     // Produces 0 at the top of the stack
```

## Null

Fift has the two words `null` and `null?` to work with values of type `Null`. The word `null` pushes to the stack the `null` value, while the word `null?` consumes the value at the top of the stack and checks if such value is `null` or not. If the value is `null`, word `null?` pushes `-1` to the stack, `0` otherwise.

For instance:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
// push null, then check if null.
// The stack contains -1 at the top.
null null?
// push 5, then check if null.
// The stack contains 0 at the top.
5 null?
```

## Boxes

A `Box` is a location or container that can be used to store exactly one value of any Fift-supported type. They are usually used to define variables and mutable data structures, like mutable [tuples](#tuples).

Fift provides the words `hole` and `box` to create boxes. Word `hole` pushes an empty box to the top of the stack, i.e., an empty box is a box containing the [`null`](#null) value. Word `box` consumes the value `v` at the top of the stack and pushes a box containing `v`.

For instance:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
hole       // Pushes an empty box
5 box      // Pushes a box containing 5
"foo" box  // Pushes a box containing "foo"
```

The value currently stored in a `Box` may be fetched by means of word `@` (pronounced "fetch"), and modified by means of word `!` (pronounced "store"). Word `@` consumes the box at the top of the stack and pushes the value contained in the box. Word `!` requires a stack of the form `x p`, where `x` is the value to store and `p` is the box in which `x` is going to be stored. `!` consumes both `x` and `p` and stores `x` in `p`. Word `!` does not push anything into the stack.

For instance:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
// Create a constant A storing an empty box
hole constant A
// Fetch the contents of box in A
A @   // Pushes null into the stack
// Store 5 into the box in A
5 A !
// Fetch the contents of box in A
A @   // Pushes 5 into the stack
// Store "foo" into the box in A
"foo" A !
// Fetch the contents of box in A
A @   // Pushes "foo" into the stack
```

As the example suggests, the value stored in a box does not need to be of the same type as the box contents mutate.

For advanced details about boxes, see the [named and unnamed variables section](/languages/fift/whitepaper#2-14-named-and-unnamed-variables) in the Fift whitepaper and the variables section in the [words, constants, and variables page](/languages/fift/variables#variables).

## Tuples

Tuples are ordered collections of arbitrary values of any type. When a `Tuple` consists of values `[ x1 ... xn ]` (in that order), the number `n` is called the length of the tuple. Tuples of length two are called *pairs*; tuples of length three are *triples*. A tuple with no elements is called the *empty tuple*.

To construct tuples of known length, Fift provides the word `tuple`. The word `tuple` requires that the tuple's data be laid out in the stack as follows `x1 x2 ..... xn n`, where `x1`, `x2`, `...`, `xn` are the `n` values in the tuple, and `n` is the length of the tuple, located at the top of the stack. For example, to construct tuple `[ 1 2 5 7 ]` consisting of `4` values, execute `1 2 5 7 4 tuple`, which will push the integers `1`, `2`, `5`, `7` into the stack, then it will push integer `4`, which is the tuple's length, and finally, Fift will execute the word `tuple`, which will consume the numbers `1`, `2`, `5`, `7`, `4` from the stack and push the tuple `[ 1 2 5 7 ]` at the top of the stack.

To build pairs and triples it is possible to use the words `pair` and `triple`, respectively. For example, to build pair `[ x1 x2 ]`, execute `x1 x2 pair`, which is equivalent to executing `x1 x2 2 tuple`. To build triple `[ x1 x2 x3 ]`, execute `x1 x2 x3 triple`, which is equivalent to executing `x1 x2 x3 3 tuple`.

To append a value `x` at the end of tuple `t`, Fift provides the word `,`. The word `,` requires that `t` and `x` be laid out in the stack as `t x`, where `x` is at the top of the stack. The word then consumes `t` and `x` from the stack, appends `x` to the end of `t` and pushes the resulting tuple to the end of the stack. For example, suppose the stack contains `[ 1 2 ] 3`, where `[ 1 2 ]` is a tuple, and `3` is the element to add located at the top of the stack; executing `,` will consume `[ 1 2 ]` and `3` from the stack and push the tuple `[ 1 2 3]` at the top of the stack.

Use word `|` to push the empty tuple at the top of the stack. Using words `|` and `,` in combination, allows the construction of tuples in stages, starting from the empty tuple. For instance, the following produces tuple `[ 2 3 9 ]` at the top of the stack:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
| 2 , 3 , 9 ,
```

first `|` pushes the empty tuple at the top of the stack; `2` then pushes `2` at the top of the stack, producing the stack `[ ] 2`, where `[ ]` denotes the empty tuple. Next, word `,` consumes `[ ]` and `2`, and pushes the tuple `[ 2 ]` at the top of the stack. Then, `3` pushes `3` at the top of the stack, producing the stack `[ 2 ] 3`, which then gets consumed by word `,` to produce tuple `[ 2 3 ]` at the top of the stack. Finally, `9` pushes `9` to produce stack `[ 2 3 ] 9`, which then gets consumed by word `,` to produce `[ 2 3 9 ]` at the top of the stack.

The components of a `Tuple` are not necessarily of the same type, and the components can also be a `Tuple`. For instance:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
1 2 "one" triple 4 5 "two" triple pair
```

produces the tuple at the top of the stack:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
[ [ 1 2 "one" ] [ 4 5 "two" ] ]
```

which is a tuple with two tuples as components, and each tuple component contains two integers and one string.

Once a `Tuple` has been constructed, it is possible to extract any of its components, or completely unpack the `Tuple` into the stack. For this, Fift provides words `[]` and `explode`, respectively.

The word `[]` extracts a specific index from a tuple. Word `[]` requires that the stack has the form `t i`, where `t` is the tuple and `i` is the index to extract. When word `[]` executes, it consumes the elements `t i` from the stack and pushes to the top of the stack the `i`-th element in `t`. Indexes start counting from 0, so that if a tuple has `n` components, index `i` must satisfy `0 <= i < n`.

For instance, the following defines a 3x3 matrix of integers `Matrix`, and then, accesses entry at row `1` and column `2`:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
// The first line defines constant Matrix,
// containing the nested tuples:
// [
//    [ 1 2 3 ]
//    [ 4 5 6 ]
//    [ 7 8 9 ]
// ]
1 2 3 triple 4 5 6 triple 7 8 9 triple triple constant Matrix
Matrix 1 [] 2 []
```

produces `6` at the top of the stack. Line `Matrix 1 [] 2 []` executes as follows. First, constant `Matrix` pushes the entire matrix at the top of the stack. Next, `1` pushes `1` to the top of the stack, so the the stack now looks like `Matrix 1`. Next, word `[]` consumes `Matrix` and `1` from the stack and pushes `[ 4 5 6 ]` to the top of the stack, i.e., the row at index `1`. Finally, `2` pushes `2` to the stack, and `[]` consumes `[ 4 5 6 ]` and `2`, and pushes `6` to the top of the stack, i.e., the element at index `2`. The notation `Matrix 1 [] 2 []` is very reminiscent of the notation `Matrix[1][2]` usually found in programming languages.

The word `explode` extracts all the components of a tuple and pushes them into the stack. Word `explode` requires that the stack has the form `[x1 x2 ... xn]`, i.e., a tuple of `n` elements at the top of the stack. When `explode` executes, it consumes the tuple at the top of the stack and produces the stack `x1 x2 ... xn n`, where the number of elements `n` is at the top of the stack.

For instance, the following defines a constant `A` containing the tuple `[ 7 8 9 ]`, and the second line decomposes the tuple:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
// The first line defines constant A
// containing tuple [ 7 8 9 ]
7 8 9 triple constant A
A explode
```

produces `7 8 9 3` in the stack, where `3`, the number at the top of the stack, is the number of elements in the original tuple.

`Tuples` are immutable: it is not possible to change a component of a `Tuple`. If the intention is to create something like an array, where the components can be freely updated, a `Tuple` of `Box`es needs to be created.

Fift has the word `allot` that creates a tuple of a specified number of [boxes](#boxes). The word `allot` takes the integer `n` at the top of the stack and creates a tuple of `n` empty boxes, i.e., each box contains `null`. Later, it is possible to mutate the boxes using the word `!`.

For instance, the following defines a constant `A` containing a tuple of `10` empty boxes. Later, it sets the 0-th box to contain the integer `10`, and 2-th box to contain string `"foo"`.

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
// Create a tuple of 10 empty boxes, name it A
10 allot constant A
// Set 0-th box to contain 10
10 A 0 [] !
// Set 2-th box to contain "foo"
"foo" A 2 [] !
```

The code `10 A 0 [] !` pushes `10` into the stack. Then `A 0 []` pushes the 0-th box into the stack. Finally, `!` consumes both the box and `10` and updates the contents of the box to `10`. The code `"foo" A 2 [] !` has a similar explanation.

For advanced details about tuple, see the [Tuples section](/languages/fift/whitepaper#2-15-tuples-and-arrays) in the Fift whitepaper.

## Lists

Lisp-style lists can also be represented in Fift. A list `(1 2 3)` can be represented by nesting 2-tuples `[ 1 [ 2 [ 3 null ] ] ]`, where [`null`](#null) represents the empty list. Fift provides several words to create and deconstruct lists.

The word `cons` takes a stack of the form `h l`, where `h` is a value and `l` is a list, i.e., a nested tuple; consumes both `h` and `l`, and produces the tuple `[ h l ]`.

For instance, the following code produces the list `(1 2 3)`:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
1 2 3 null cons cons cons
```

First, `1 2 3 null` produces the stack `1 2 3 null`, where `null` is at the top of the stack. Then, the first `cons` produces the stack `1 2 [ 3 null ]`, where tuple `[ 3 null ]` is at the top of the stack. Next, the second `cons` produces the stack `1 [ 2 [ 3 null ] ]`. Finally, the third `cons` produces the stack `[ 1 [ 2 [ 3 null ] ] ]` containing a single tuple at the top, which is the list `(1 2 3)`.

It is so common to build fixed-length lists like in the above example, that Fift provides a special word for building a list when the elements are known. The word `list` takes a stack of the form `x1 ... xn n`, where `n` is the number of elements in the list, and consumes `x1`, `...`, `xn` and `n` from the stack to produce the list `(x1 .... xn)` at the top of the stack.

For instance, the following produces the same list as the previous example:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
1 2 3 3 list
```

<Aside>
  The list constructed with the code:

  ```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
  1 2 3 3 list
  ```

  is different from the tuple constructed with the code:

  ```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
  1 2 3 3 tuple
  ```

  The list is really the nested tuple `[ 1 [ 2 [ 3 null ] ] ]`, while the tuple is `[ 1 2 3 ]`.
</Aside>

To deconstruct a list, use the word `uncons`. The word `uncons` consumes the list at the top of the stack and pushes the list's head followed by the list's tail. For instance, the following code defines a constant `A` containing list `(5 6 7)` and deconstruct the list by calling `uncons` several times:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
// Create the list (5 6 7), name it A
5 6 7 3 list constant A
// Deconstruct the list by repeatedly calling uncons
A uncons uncons uncons
```

The first `uncons` produces the stack `5 [ 6 [ 7 null ] ]`. The second `uncons` the stack `5 6 [ 7 null ]`. Finally, the third `uncons` produces the stack `5 6 7 null`. Since there is a `null` at the top of the stack, `drop` removes the `null` to produce the stack `5 6 7`.

For advanced details about lists, see the [Lists section](/languages/fift/whitepaper#2-16-lists) in the Fift whitepaper.

## Atoms

An `Atom` is an entity uniquely identified by its name. `Atom`s can be used to represent identifiers, labels, operation names, tags, and stack markers. Fift offers several words to create and manipulate `Atom`s.

The word <code>\`name</code> pushes into the stack the atom identified by `name`. For instance, the following builds the [list](#lists) `(+ 2 (* 3 4))`, which is the Lisp-style representation of arithmetical expression `2 + (3 * 4)`:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
`+ 2 `* 3 4 3 list 3 list
```

First <code>\`+ 2 \`\* 3 4 3</code> produces the stack `+ 2 * 3 4 3`, where `+` and `*` are atoms, i.e., identifiers. Then, `list` produces the stack `+ 2 (* 3 4)`, where list `(* 3 4)` is at the top of the stack. Next, `3` produces the stack `+ 2 (* 3 4) 3`. Finally, `list` produces the stack `(+ 2 (* 3 4))`, containing the final list at the top of the stack.

It is possible to compare atoms using the word `eq?`. The word `eq?` consumes the two top-most elements in a stack of the form `u v` where `u` and `v` are the atoms to compare, and pushes to the stack `-1` if the atoms are equal, `0` otherwise.

For instance,

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
`+ `+ eq?   // pushes -1 to the stack
`+ `- eq?   // pushes 0 to the stack
```

For advanced details about atoms, see the [Atoms section](/languages/fift/whitepaper#2-17-atoms) in the Fift whitepaper.

## Slices

A `Slice` is a read-only view of a portion of a `Cell`. Fift uses the syntax `b{<BINARY_DATA>}` and `x{<HEX_DATA>}` for defining slices.

`b{<BINARY_DATA>}` pushes a slice into the stack that contains no references and up to 1023 data bits specified in `<BINARY_DATA>`, which must be a string consisting only of the characters `0` and `1`.

`x{<HEX_DATA>}` pushes a slice into the stack that contains no references and up to 1023 data bits specified in `<HEX_DATA>`. More precisely, each hex digit from `<HEX_DATA>` is transformed into four binary digits in the usual fashion. After that, if the last character of `<HEX_DATA>` is an underscore `_`, then all trailing binary zeroes and the binary digit immediately preceding them are removed from the resulting binary string.

An empty `Slice` can be pushed into the stack by `b{}` or `x{}`.

For example, `b{00011101}` and `x{1d}` both push the same `Slice` consisting of eight data bits and no references. Similarly, `b{111010}` and `x{EA_}` push the same `Slice` consisting of six data bits. For `x{EA_}` the binary representation of `EA` is `11101010`, but since `EA_` has a final underscore, the trailing zeroes in the binary representation are removed together with the binary digit immediately preceding them, i.e., the final `10`, producing `111010`.

To construct a `Slice` with some `Cell` references, Fift provides the word `|_`, which consumes the top-most two elements in a stack of the form `s s'`, where `s` and `s'` are slices. It crates a new slice `s''` obtained from `s` by appending a new reference to a cell containing `s'`; then, pushes `s''` into the stack.

For instance, the following code creates a cell containing the slice `b{001}`, and attaches the cell as a reference to the slice `b{101}`:

```fift theme={"theme":{"light":"github-light-default","dark":"dark-plus"},"languages":{"custom":["/resources/grammars/tolk.tmLanguage.json","/resources/grammars/tlb.tmLanguage.json","/resources/grammars/fift.tmLanguage.json","/resources/grammars/tasm.tmLanguage.json","/resources/grammars/func.tmLanguage.json"]}}
b{101} b{001} |_
```

First, `b{101} b{001}` pushes the two slices in that order. Next, `|_` consumes the two slices. Then, `|_` copies `b{101}` into a new slice `s` and creates a cell `c` containing `b{001}`. Finally, `|_` attaches `c` as a reference to `s` and pushes `s` into the stack.

For advanced details about slices, see the [Slices section](/languages/fift/whitepaper#5-1-slice-literals) in the Fift whitepaper. For a list of words for manipulating slices and reading their contents, see the list of common slice words page.
