# Builtins

`A B`

### ` abs(t : T) `

Returns the absolute value of input `t`

.

When `t`

is of type:

The function may be considered as a non-failing conversion function from `int`

to `nat`

.

#### Parameter

#### Returns

`R`

`t`

### ` add(s : set<T>, e : T) `

`s`

augmented with element `e`

.If `e`

is already present in `s`

, it returns a copy of `s`

.#### Parameters

`e`

:

`T`

#### Returns

### ` address_to_contract<T>(a : address) `

Converts address `a`

to a `contract<T>`

value to be used as parameter of `make_operation`

.

`T`

is:

`unit`

for an account address (`tz1...`

)- the type of the
*parameter*section for a contract address

`get_entrypoint`

builtin.#### Parameter

#### Returns

`option<contract<T>>`

`none`

when the address or the path to entrypoint is invalid`some`

of a`contract<T>`

value

### ` apply_lambda(f : lambda<A * T, R>, x : A) `

Executes partial application of a lambda value `f`

to an argument value `x`

.

For example:

`const plus_x = lambda<nat>((a : nat * nat) -> a[0] + a[1]);`

const plus_three = apply_lambda(plus_x, 3);

const r = exec_lambda(plus_three, 1);

/* r is equal to 4 */

#### Parameters

`f`

:

`lambda<A*T,R>`

`x`

:

`A`

#### Returns

### ` blake2b(b : bytes) `

`C D`

### ` call_view<T>(a : address, id : string, arg : X) `

Calls a contract's view and returns an `option<T>`

of the result.

For example, consider the following view declaration in contract `A`

:

`view affine(x : rational) : rational {`

return (2 * x + 3)

}

Call this function from contract `B`

with:

`const r ?= call_view<rational>(addr_A, "affine", -3/2) : "AFFINE_ERR"`

#### Parameters

`arg`

:

`X`

#### Returns

`none`

if a problem occured (view not found, or view execution failed)`some(v)`

,`v`

being the value returned by the view

### ` ceil(r : rational) `

#### Parameter

#### Returns

### ` check_signature(k : key, s : signature, b : bytes) `

`s`

is obtained by signing sequence of bytes `b`

with account public key `k`

.#### Parameters

#### Returns

### ` concat `

Concatenates two values of type `string`

, `bytes`

, or `list<T>`

, or a list of values of type `string`

or `bytes`

.

### String

Concatenates two `string`

values.

For example:

`const m = concat("Hello ", "Archetype");`

/* m is "Hello Archetype" */

It is equivalent to the `+`

operator.

### Bytes

Concatenates two `bytes`

values.

For example:

`const m = concat(0x48656c6c6f, 0x417263686574797065);`

/* m is 0x48656c6c6f417263686574797065 */

### List

Concatenates two `lists`

of any element of any type.

It is equivalent to the following code:

`var res : list<T> = l;`

for e in reverse(L) do

res.prepend(e)

done;

L := res

For example:

`const l = concat([1; 2; 3], [4; 5]);`

/* l is [1; 2; 3; 4; 5] */

### List of values

Concatenates values of a list, of type `string`

or `bytes`

For example:

`const m = concat([ "Hello "; "Archetype "; "world!" ]);`

/* m is "Hello Archetype world!"*/

#### Returns

### ` contains(c : C, e : T) `

`e`

is contained in `c`

or not.#### Parameters

#### Returns

### ` contract_to_address(c : contract<T>) `

#### Parameter

#### Returns

### ` create_contract(path, delegator, amount, storage) `

Creates an operation for contract creation. This operations is then added to the `operations`

builtin list of generated operations.

For example, in order to create contract from source `./tests/simple.tz`

for `0`

as natural initial storage:

`const storage_init : nat = 0;`

const op_addr : (operation * address) =

create_contract("./tests/simple.tz", none, 0tz, storage_init);

operations := [op_addr[0]]

It is also possible to import the contract with:

`import simple from "./tests/simple.tz"`

and deploy it with:

`const storage_init : nat = 0;`

const op_addr : (operation * address) =

create_contract(simple, none, 0tz, storage_init);

operations := [op_addr[0]]

##### info

The import of archetype contract (from `.arl`

file) will be available in next releases.

#### Parameters

`path`

:

`string literal`

`del`

:

`option<address>`

`storage`

:

`any`

#### Returns

`operation * address`

- Operation for contract creation
- Address of created contract

### ` create_ticket(s : T, n : nat) `

`E F`

### ` exec_lambda(f : lambda<T, R>, x : T) `

Applies a lambda value `f`

to argument `x`

.

For example:

`const plus_three = lambda((x : nat) -> x + 3);`

const r = exec_lambda(plus_three, 1);

/* r is equal to 4 */

#### Parameters

`x`

:

`T`

#### Returns

### ` floor(r : rational) `

#### Parameter

#### Returns

### ` fold (i : or<L, R>, f : L -> or<L, R>) `

`fold`

applies the *inlined* function `f`

to the `left`

of initial value `i`

:

- if it returns a
`left`

value, it applies`f`

to the result of`f(i)`

, and iteratively applies f on as long as it returns a`left`

value. - if it returns a
`right`

value, iteration stops, and the`right`

value is returned.

Hence the application may be represented as:
`fold(i, f) = right(f(...(f(i))))`

For example, the following expression *reverses* the list `l`

typed `list<nat>`

:

`var empty : list<nat> = [];`

var p : or<list<nat> * list<nat>, list<nat>> = left<list<nat>> ((empty, l));

res := fold (p, x ->

match x[1] with

| hd::tail -> left<list<nat>> ((prepend(x[0], hd), tail))

| [] -> right<list<nat> * list<nat>>(x[0])

end

)

##### note

Folded function is inlined and cannot be a `lambda`

variable.

#### Parameters

`f`

:

`function`

*Inlined*folding function that:

- takes a parameter of type
`L`

- returns a values of type
`or<L, R>`

#### Returns

`R`

`G H`

### ` get_entrypoint<T>(s, a : address) `

Returns an `option`

of contract's entrypoint (typed `contract<T>`

) to the entrypoint `s`

with argument `T`

at contract address `a`

.

For example, consider the following contract at address `a`

:

`archetype example`

entry exec(n : nat) { /* ... */ }

The `exec`

entrypoint may be retrieved with:

`const e = get_entrypoint("%exec", a) ? the : fail("EXEC_NOT_FOUND")`

The entrypoint may then be used as an argument of `make_operation`

.

#### Parameters

`s`

:

`string literal`

`%`

#### Returns

`option<contract<T>>`

`contract`

value### ` greedy_and(a : bool, b : bool) `

`a`

and `b`

, meaning that `b`

is evaluated even if `a`

is false.#### Parameters

#### Returns

### ` greedy_or(a : bool, b : bool) `

`a`

and `b`

, meaning that `b`

is evaluated even if `a`

is true.#### Parameters

#### Returns

`I J`

### ` int_to_date(i : int) `

`i`

to a date, where `i`

is considered as a tiemstamp value, that is the number of seconds since `1970-01-01`

.#### Parameter

#### Returns

### ` int_to_nat(i : int) `

#### Parameter

#### Returns

`option<nat>`

`some(n)`

when`i`

is positive`none`

otherwise

### ` is_none(o : option<T>) `

Returns `false`

if an optional value `o`

is `some`

, `true`

otherwise.

It is equivalent to the following expression:

`o ? false : true`

or equivalently:

`match o with`

| some(v) -> false

| none -> true

end

#### Parameter

#### Returns

### ` is_some(o : option<T>) `

Returns `true`

if an optional value `o`

is `some`

, `false`

otherwise.

It is equivalent to the following expression:

`o ? true : false`

or equivalently:

`match o with`

| some(v) -> true

| none -> false

end

#### Parameter

#### Returns

### ` join_tickets(t1 : ticket<T>, t2 : ticket<T>) `

#### Parameters

#### Returns

`option<ticket<T>>`

### ` keccak(b : bytes) `

`K L`

### ` key_hash_to_contract(pkh : key_hash) `

#### Parameter

#### Returns

`contract<unit>`

### ` key_to_address(k : key) `

#### Parameter

#### Returns

### ` key_to_key_hash(k : key) `

#### Parameter

#### Returns

### ` left<(L,)? T>(x : L) `

Constructs a *left* value typed `or<L, R>`

.

The left type `L`

may be omitted as it can be inferred, but the right type `R`

is mandatory.

For example:

`const o = left<string>(2)`

`o`

is then typed `option<nat, string>`

. It is equivalent to:

`const o = left<nat, string>(2)`

#### Parameter

`x`

:

`L`

#### Returns

### ` length(o : T) `

#### Parameter

#### Returns

`M N`

### ` make_asset(k : asset_key<A>, v : asset_value<A>) `

Creates an asset from identifier and data (key and value).

It is typically convenient when an asset data is provided as an entry point parameter.

Consider for example the following asset declaration:

`asset vehicle {`

vin : string;

nbrepairs : nat = 0;

dateofrepair : date = now;

}

The `add_vehicle`

entry point below provides a vehicle data as a record:

`entry add_vehicle(k : asset_key<vehicle>, d : asset_value<vehicle>) {`

vehicle.put(make_asset(k, d))

}

#### Parameters

#### Returns

`A`

`k`

and data `v`

.### ` make_big_map<K, V>(m) `

*literal*or variable.

This is useful to solve the type amiguity of literals, like for example the empty big_map

`[]`

.#### Parameter

#### Returns

Fails with

does not fail

### ` make_map<K, V>(m) `

*literal*or variable.

This is useful to solve the type amiguity of literals, like for example the empty map

`[]`

.#### Parameter

#### Returns

Fails with

does not fail

### ` make_list<T>(l) `

*literal*or variable.

This is useful to solve the type amiguity of literals, like for example the empty list

`[]`

.#### Parameter

#### Returns

Fails with

does not fail

### ` make_operation(a : tez, c : contract<T>, arg : T) `

#### Parameters

`arg`

:

`T`

#### Returns

### ` make_set<T>(s) `

*literal*or variable.

This is useful to solve the type amiguity of literals, like for example the empty set

`[]`

.#### Parameter

#### Returns

Fails with

does not fail

### ` map `

Maps a value of type `list<T>`

, `map<K, V>`

, or `option<T>`

.

### List

Applies an inlined function of type `T -> T`

on each element of a list of elements typed `T`

and returns a new list.

For example:

`const l = map([ 1; 2 ; 3 ], x -> 2 * x + 1);`

/* l is [ 3; 5; 7 ] */

### Map

Applies an inlined function of type `(K * V) -> V`

on each value of a map with key of type `K`

and value of type `V`

; it returns a new map.

For example:

`const m = map([ ("a", 5); ("b", 6); ("c", 7) ], x -> 2 * x[1] + 1)`

/* m is [ ("a", 11); ("b", 13); ("c", 15) ] */

Note that the `x`

intput of the inlined mapping function is a *pair* (key and value) of the map; hence `x[0]`

is the key and `x[1]`

is the value.

### Option

Applies an inlined function of type `T -> T`

to the `some`

value of an `option<T>`

value, or returns `none`

.

For example:

`const o1 = map(some(2), x -> 2 * x + 1);`

/* o1 is some(5) */

const o2 = map(none, x -> 2 * x + 1);

/* o2 is none */

Note that the ` ? :`

syntax is also available to fold an option. The above example is then equivalent to:

`const o1 = some(2) ? 2 * the + 1 : none;`

/* o1 is some(5) */

#### Parameters

`f`

:

`function`

*Inlined*mapping function.

#### Returns

`-`

### ` max(a : T, b : T) `

`a`

and `b`

#### Parameters

#### Returns

### ` min(a : T, b : T) `

`a`

and `b`

#### Parameters

#### Returns

### ` mutez_to_nat(v : tez) `

Converts a `tez`

value in mutez (millionth of tez) to a `nat`

value.

For example:

`const n = mutez_to_nat(5tz);`

/* n is 5000000 */

#### Parameter

#### Returns

### ` nat_to_string(n : nat) `

#### Parameter

#### Returns

### ` none<T> `

`none`

value for type `option<T>`

.#### Returns

### ` nth(l : list<T>, n : nat) `

Returns an option of the `n`

th element of list `l`

.

##### warning

Implementation is in O(n) complexity (Michelson lists are linked).

#### Parameters

#### Returns

`O P`

### ` open_chest(k : chest_key, c : chest, t : nat) `

`c`

with key `k`

under time `t`

.#### Parameters

#### Returns

`or<bytes, bool>`

`left(v)`

,`v`

being the value in the chest`right(true)`

when chest key`k`

does not open the chest`right(false)`

when chest key`k`

opens the chest but`t`

parameter is not the value used to lock the chest

### ` pack(o : T) `

#### Parameter

#### Returns

### ` pairing_check(l : list<bls12_381_g1 * bls12_381_g2>) `

#### Parameter

`l`

:

`list<bls12_381_g1 * bls12_381_g2>`

#### Returns

### ` prepend(l : list<T>, e : T) `

`e`

at the beginning of list `l`

.#### Parameters

#### Returns

### ` put(m : map<K, V>, k : K, v : V) `

`v`

with key `k`

in map (`map`

, `big_map`

or `iterable_map`

)`m`

.#### Parameters

#### Returns

`Q R`

### ` read_ticket(t : ticket<T>) `

#### Parameter

#### Returns

`address * T * nat`

### ` remove(c : C, i : T) `

`c`

without item `i`

.#### Parameters

`i`

:

`T`

#### Returns

### ` reverse(l : list<T>) `

`l`

with elements in reverse order.#### Parameter

#### Returns

### ` right<T(, R)>(x : R) `

Constructs a *right* value typed `or<L, R>`

.

The left type `L`

is mandatory, while the right type `R`

may be omited as it can be inferred.

For example:

`const o = right<nat>("abc")`

`o`

is then typed `option<nat, string>`

. It is equivalent to:

`const o = right<nat, string>("abc")`

#### Parameter

`x`

:

`R`

#### Returns

`S T`

### ` sapling_empty_state(k : key_hash) `

#### Parameter

#### Returns

### ` sapling_verify_update(s, t) `

#### Parameters

#### Returns

`option<bytes * int * sapling_state(n)>`

### ` set_delegate(opkh : option<key_hash>) `

#### Parameter

`opkh`

:

`none`

remove delegation of current contract`some(pkh)`

update delegation of current contract with address behind`pkh`

#### Returns

### ` sha256(b : bytes) `

#### Parameter

#### Returns

### ` sha3(b : bytes) `

### ` sha512(b : bytes) `

#### Parameter

#### Returns

### ` slice(s : T, o : nat, l : nat) `

Extracts a section of a string or bytes `s`

and returns it as a new string or bytes.

The section starts at offset postion `o`

is has a length of `l`

.

For example:

`const s = "Hello Archetype World!";`

const e = slice(s, 6, 9) ? the : ""; /* e is "Archetype" */

const b = 0x48656c6c6f2041726368657479706520576f726c64;

const c = slice(b, 12, 18); /* c is some(0x417263686574797065) */

const f = slice(b, 30, 40); /* f is none */

#### Parameters

#### Returns

`none`

if a problem occured (offset + length out of bounds)`some(v)`

,`v`

being the value of sub sequence

### ` some(v : T) `

`some`

value for type `option<T>`

.#### Parameter

#### Returns

### ` split_ticket(t : ticket<T>, n1 : nat, n2 : nat) `

#### Parameters

#### Returns

`option<ticket<T> * ticket<T>>`

`n1`

and `n2`

values.### ` sub_mutez(a : tez, b : tez) `

`a`

to `b`

and returns an option of tez value.#### Parameters

#### Returns

### ` sub_nat(a : nat, b : nat) `

`a`

to `b`

and returns an option of nat value.#### Parameters

#### Returns

`option<nat>`

`some`

of`a - b`

when`a >= b`

`none`

otherwise

`T U`

### ` unpack<T>(b : bytes) `

Deserializes a value of type `bytes`

into the corresponding value of type `option<T>`

.

For example, suppose `p`

is a bytes value:

`const v ?= unpack<nat>(p) : "UNPACK_FAILED";`

/* v is typed nat */

#### Parameter

#### Returns

### ` update(m : map<K, V>, k : K, v : option<T>) `

`v`

from associated to key `k`

in map `m`

: `none`

removes key`k`

`some(v)`

, adds key`k`

with value`v`

#### Parameters

`k`

:

`K`

#### Returns

### ` update(s : set<T>, e : T, b : bool) `

`e`

in set `s`

: - removes
`e`

when`b`

is`false`

- adds
`e`

when`b`

is`true`

#### Parameters

`e`

:

`T`