Merge branch 'more-api-docs-syntax-headers' into 'dev'

Add remaining API docs

See merge request ligolang/ligo!503
This commit is contained in:
Sander 2020-03-17 15:38:41 +00:00
commit 78faa138da
11 changed files with 1056 additions and 447 deletions

View File

@ -13,7 +13,7 @@ A lazily deserialized map that's intended to store large amounts of data.
The gast costs of deserialized maps are higher than standard maps as data is lazily deserialized.
<SyntaxTitle syntax="pascaligo">
type big_map (key, value)
type big_map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type ('key, 'value) big_map
@ -45,6 +45,7 @@ type register = (address, move) big_map
</Syntax>
<Syntax syntax="reasonligo">
The type of a big map from values of type `key` to
values of type `value` is `big_map (key, value)`.
@ -56,7 +57,7 @@ type register = big_map (address, move);
</Syntax>
<SyntaxTitle syntax="pascaligo">
function empty : big_map (key, value)
function empty : big_map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val empty : ('key, 'value) big_map
@ -97,7 +98,7 @@ let empty : register = Big_map.empty
<SyntaxTitle syntax="pascaligo">
function literal : list (key * value) -> big_map (key, value)
function literal : list ('key * 'value) -> big_map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val literal : ('key * 'value) list -> ('key, 'value) big_map
@ -149,7 +150,7 @@ let moves : register =
</Syntax>
<SyntaxTitle syntax="pascaligo">
function find_opt : key -> big_map (key, value) -> option value
function find_opt : 'key -> big_map ('key, 'value) -> option 'value
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val find_opt : 'key -> ('key, 'value) big_map -> 'value option
@ -197,7 +198,7 @@ let my_balance : option (move) =
</Syntax>
<SyntaxTitle syntax="pascaligo">
function update : key -> option value -> big_map (key, value) -> big_map (key, value)
function update : 'key -> option 'value -> big_map ('key, 'value) -> big_map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val update: 'key -> 'value option -> ('key, 'value) big_map -> ('key, 'value) big_map
@ -262,7 +263,7 @@ let updated_map : register =
</Syntax>
<SyntaxTitle syntax="pascaligo">
function add : key -> value -> big_map (key, value) -> big_map (key, value)
function add : 'key -> 'value -> big_map ('key, 'value) -> big_map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val add : 'key -> 'value -> ('key, 'value) big_map -> ('key, 'value) big_map
@ -298,13 +299,13 @@ let add = (m: register): register =>
<SyntaxTitle syntax="pascaligo">
function remove: key -> big_map (key, value) -> big_map (key, value)
function remove: 'key -> big_map ('key, 'value) -> big_map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val remove: 'key -> ('key, 'value) big_map -> ('key, 'value) big_map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let remove: (key, big_map ('key, 'value)) => big_map ('key, 'value)
let remove: ('key, big_map ('key, 'value)) => big_map ('key, 'value)
</SyntaxTitle>
<Syntax syntax="pascaligo">

View File

@ -0,0 +1,69 @@
---
id: bitwise-reference
title: Bitwise
description: Operations on bytes
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
<SyntaxTitle syntax="pascaligo">
function and : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val and : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let and: (nat, nat) -> nat
</SyntaxTitle>
A bitwise `and` operation.
<SyntaxTitle syntax="pascaligo">
function or : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val or : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let or: (nat, nat) -> nat
</SyntaxTitle>
A bitwise `or` operation.
<SyntaxTitle syntax="pascaligo">
function xor : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val xor : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let xor: (nat, nat) -> nat
</SyntaxTitle>
A bitwise `xor` operation.
<SyntaxTitle syntax="pascaligo">
function shift_left : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val shift_left : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let shift_left: (nat, nat) -> nat
</SyntaxTitle>
A bitwise shift left operation.
<SyntaxTitle syntax="pascaligo">
function shift_right : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val shift_right : nat -> nat -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let shift_right: (nat, nat) -> nat
</SyntaxTitle>
A bitwise shift right operation.

View File

@ -1,21 +1,43 @@
---
id: bytes-reference
title: Bytes — Manipulate bytes data
title: Bytes
description: Operations on bytes
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
## Bytes.concat(b1: bytes, b2: bytes) : bytes
<SyntaxTitle syntax="pascaligo">
type bytes
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type bytes
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type bytes
</SyntaxTitle>
<SyntaxTitle syntax="pascaligo">
function concat : bytes -> bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val concat : bytes -> bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let concat: (bytes, bytes) => bytes
</SyntaxTitle>
Concatenate together two `bytes` arguments and return the result.
<Syntax syntax="pascaligo">
```pascaligo
function concat_op (const s : bytes) : bytes is
begin skip end with bytes_concat(s , 0x7070)
function concat_op (const s : bytes) : bytes is Bytes.concat(s , 0x7070)
```
> Note that `bytes_concat` is *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
@ -33,41 +55,58 @@ let concat_op = (s: bytes): bytes => Bytes.concat(s, 0x7070);
</Syntax>
## Bytes.slice(pos1: nat, pos2: nat, data: bytes) : bytes
<SyntaxTitle syntax="pascaligo">
function sub : nat -> nat -> bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val sub : nat -> nat -> bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let sub : (nat, nat, bytes) => bytes
</SyntaxTitle>
Extract the bytes between `pos1` and `pos2`. **Positions are zero indexed and
inclusive**. For example if you gave the input "ff7a7aff" to the following:
<Syntax syntax="pascaligo">
```pascaligo
function slice_op (const s : bytes) : bytes is
begin skip end with bytes_slice(1n , 2n , s)
function slice_op (const s : bytes) : bytes is Bytes.sub(1n , 2n , s)
```
> Note that `bytes_slice` is *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
```cameligo
let slice_op (s : bytes) : bytes =
Bytes.slice 1n 2n s
let slice_op (s : bytes) : bytes = Bytes.sub 1n 2n s
```
> Note that `Bytes.slice` is *deprecated*.
</Syntax>
<Syntax syntax="reasonligo">
```
let slice_op = (s: bytes): bytes => Bytes.slice(1n, 2n, s);
let slice_op = (s: bytes): bytes => Bytes.sub(1n, 2n, s);
```
> Note that `Bytes.slice` is *deprecated*.
</Syntax>
It would return "7a7a" rather than "ff7a" or "ff" or "7a".
## Bytes.pack(data: a') : bytes
<SyntaxTitle syntax="pascaligo">
function pack : 'a -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val pack : 'a -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let pack : 'a => bytes
</SyntaxTitle>
Converts Michelson data structures to a binary format for serialization.
@ -105,10 +144,19 @@ let id_string = (p: string) : option(string) => {
</Syntax>
## Bytes.unpack(packed: bytes) : a'
<SyntaxTitle syntax="pascaligo">
function unpack : bytes -> option 'a
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val unpack : bytes -> 'a option
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let unpack: bytes => option('a)
</SyntaxTitle>
Reverses the result of using `unpack` on data, going from Michelson's binary
serialization format to the `option` type annotated on the call.
Reverses the result of using `pack` on data.
As the conversion might fail an option type is returned.
> ⚠️ `PACK` and `UNPACK` are features of Michelson that are intended to be used by people that really know what they're doing. There are several failure cases (such as `UNPACK`ing a lambda from an untrusted source), most of which are beyond the scope of this document. Don't use these functions without doing your homework first.
@ -143,3 +191,12 @@ let id_string = (p: string) : option(string) => {
</Syntax>
<SyntaxTitle syntax="pascaligo">
function length : bytes -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val length : bytes -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let length: bytes => nat
</SyntaxTitle>

View File

@ -1,11 +1,58 @@
---
id: crypto-reference
title: Crypto — Cryptographic functions
title: Crypto
description: Cryptographic operations
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
## Crypto.blake2b(data: bytes): bytes
<SyntaxTitle syntax="pascaligo">
type key
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type key
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type key
</SyntaxTitle>
A public cryptographic key.
<SyntaxTitle syntax="pascaligo">
type key_hash
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type key_hash
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type key_hash
</SyntaxTitle>
The hash of a public cryptographic key.
<SyntaxTitle syntax="pascaligo">
type signature
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type signature
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type signature
</SyntaxTitle>
A cryptographic signature.
<SyntaxTitle syntax="pascaligo">
function blake2b : bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val blake2b : bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let blake2b: bytes => bytes
</SyntaxTitle>
Runs the [blake2b hash algorithm](https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2)
over the given `bytes` data and returns a `bytes` representing the hash.
@ -15,9 +62,11 @@ over the given `bytes` data and returns a `bytes` representing the hash.
<Syntax syntax="pascaligo">
```pascaligo
function hasherman_blake (const s: bytes) : bytes is blake2b(s)
function hasherman_blake (const s: bytes) : bytes is Crypto.blake2b(s)
```
> Note that `blake2b` is *deprecated*. Please use `Crypto.blake2b`.
</Syntax>
<Syntax syntax="cameligo">
@ -25,6 +74,8 @@ function hasherman_blake (const s: bytes) : bytes is blake2b(s)
let hasherman_blake (s: bytes) : bytes = Crypto.blake2b s
```
</Syntax>
<Syntax syntax="reasonligo">
@ -34,8 +85,15 @@ let hasherman_blake = (s: bytes) => Crypto.blake2b(s);
</Syntax>
## Crypto.sha256(data: bytes) : bytes
<SyntaxTitle syntax="pascaligo">
function sha256 : bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val sha256 : bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let sha256: bytes => bytes
</SyntaxTitle>
Runs the [sha256 hash algorithm](https://en.wikipedia.org/wiki/SHA-2) over the given
`bytes` data and returns a `bytes` representing the hash.
@ -45,10 +103,11 @@ Runs the [sha256 hash algorithm](https://en.wikipedia.org/wiki/SHA-2) over the g
<Syntax syntax="pascaligo">
```pascaligo
function hasherman (const s : bytes) : bytes is
begin skip end with sha_256(s)
function hasherman (const s : bytes) : bytes is Crypto.sha256(s)
```
> Note that `sha_256` is *deprecated*. Please use `Crypto.sha256`.
</Syntax>
<Syntax syntax="cameligo">
@ -66,8 +125,15 @@ let hasherman = (s: bytes): bytes => Crypto.sha256(s);
</Syntax>
## Crypto.sha512(data: bytes) : bytes
<SyntaxTitle syntax="pascaligo">
function sha512 : bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val sha512 : bytes -> bytes
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let sha512: bytes => bytes
</SyntaxTitle>
Runs the [sha512 hash algorithm](https://en.wikipedia.org/wiki/SHA-2) over the given
`bytes` data and returns a `bytes` representing the hash.
@ -77,9 +143,11 @@ Runs the [sha512 hash algorithm](https://en.wikipedia.org/wiki/SHA-2) over the g
<Syntax syntax="pascaligo">
```pascaligo
function hasherman512 (const s: bytes) : bytes is sha_512(s)
function hasherman512 (const s: bytes) : bytes is Crypto.sha512(s)
```
> Note that `sha_512` is *deprecated*. Please use `Crypto.sha512`.
</Syntax>
<Syntax syntax="cameligo">
@ -96,8 +164,15 @@ let hasherman512 = (s: bytes) => Crypto.sha512(s);
</Syntax>
## Crypto.hash_key(k: key) : key_hash
<SyntaxTitle syntax="pascaligo">
function hash_key : key -> key_hash
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val hash_key : key -> key_hash
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let hash_key: key => key_hash
</SyntaxTitle>
Hashes a key for easy comparison and storage.
@ -108,11 +183,13 @@ Hashes a key for easy comparison and storage.
```pascaligo
function check_hash_key (const kh1 : key_hash; const k2 : key) : bool * key_hash is block {
var ret : bool := False ;
var kh2 : key_hash := crypto_hash_key(k2) ;
var kh2 : key_hash := Crypto.hash_key(k2) ;
if kh1 = kh2 then ret := True else skip;
} with (ret, kh2)
```
> Note that `hash_key` is *deprecated*. Please use `Crypto.hash_key`.
</Syntax>
<Syntax syntax="cameligo">
@ -141,8 +218,15 @@ let check_hash_key = ((kh1, k2): (key_hash, key)) : (bool, key_hash) => {
</Syntax>
## Crypto.check(pk: key, signed: signature, data: bytes) : bool
<SyntaxTitle syntax="pascaligo">
function check : key -> signature -> bytes -> bool
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val check : key -> signature -> bytes -> bool
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let check: (key, signature, bytes) => bool
</SyntaxTitle>
Check that a message has been signed by a particular key.
@ -157,9 +241,11 @@ function check_signature
(const pk: key;
const signed: signature;
const msg: bytes) : bool
is crypto_check(pk, signed, msg)
is Crypto.check(pk, signed, msg)
```
> Note that `crypto_check` is *deprecated*. Please use `Crypto.check`.
</Syntax>
<Syntax syntax="cameligo">

View File

@ -1,11 +1,96 @@
---
id: current-reference
title: Tezos - Things relating to the current execution context
title: Tezos
description: General operations for Tezos
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
# Tezos.balance
<SyntaxTitle syntax="pascaligo">
type timestamp
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type timestamp
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type timestamp
</SyntaxTitle>
A date in the real world.
<SyntaxTitle syntax="pascaligo">
type mutez
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type mutez
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type mutez
</SyntaxTitle>
A specific type for tokens.
<SyntaxTitle syntax="pascaligo">
type address
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type address
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type address
</SyntaxTitle>
An untyped address which can refer to a smart contract or account.
<SyntaxTitle syntax="pascaligo">
type contract('parameter)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type 'parameter contract
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type contract('parameter)
</SyntaxTitle>
A typed contract.
Use `unit` as `parameter` to indicate an implicit account.
<SyntaxTitle syntax="pascaligo">
type operation
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type operation
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type operation
</SyntaxTitle>
An operation emitted by the contract
<SyntaxTitle syntax="pascaligo">
type chain_id
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type chain_id
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type chain_id
</SyntaxTitle>
The identifier of a chain, used to indicate test or main chains.
<SyntaxTitle syntax="pascaligo">
function balance : mutez
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val balance : mutez
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let balance: mutez
</SyntaxTitle>
Get the balance for the contract.
@ -42,7 +127,15 @@ let main = ((p,s) : (unit, tez)) =>
</Syntax>
## Tezos.now
<SyntaxTitle syntax="pascaligo">
function now : timestamp
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val now : timestamp
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let now: timestamp
</SyntaxTitle>
Returns the current time as a [unix timestamp](https://en.wikipedia.org/wiki/Unix_time).
@ -64,7 +157,7 @@ const some_date: timestamp = ("2000-01-01T10:10:10Z" : timestamp);
const one_day_later: timestamp = some_date + one_day;
```
> Note that `now` is *deprecated*.
> Note that `now` is *deprecated*. Please use `Tezos.now`.
</Syntax>
<Syntax syntax="cameligo">
@ -106,7 +199,7 @@ const one_day: int = 86_400;
const in_24_hrs: timestamp = today - one_day;
```
> Note that `now` is *deprecated*.
> Note that `now` is *deprecated*. Please use `Tezos.now`.
</Syntax>
<Syntax syntax="cameligo">
@ -145,7 +238,7 @@ for numbers
const not_tommorow: bool = (Tezos.now = in_24_hrs)
```
> Note that `now` is *deprecated*.
> Note that `now` is *deprecated*. Please use `Tezos.now`.
</Syntax>
<Syntax syntax="cameligo">
@ -169,7 +262,15 @@ let not_tomorrow: bool = (Tezos.now == in_24_hrs);
## Amount
<SyntaxTitle syntax="pascaligo">
function amount : mutez
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val amount : mutez
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let amount: mutez
</SyntaxTitle>
Get the amount of tez provided by the sender to complete this
transaction.
@ -207,7 +308,15 @@ let threshold = (p : unit) : int =>
</Syntax>
## Sender
<SyntaxTitle syntax="pascaligo">
function sender : address
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val sender : address
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let sender: address
</SyntaxTitle>
Get the address that initiated the current transaction.
@ -219,7 +328,7 @@ Get the address that initiated the current transaction.
function main (const p : unit) : address is Tezos.sender
```
> Note that `sender` is *deprecated*.
> Note that `sender` is *deprecated*. Please use `Tezos.sender`.
</Syntax>
<Syntax syntax="cameligo">
@ -243,7 +352,15 @@ let main = (p : unit) : address => Tezos.sender;
## Address
<SyntaxTitle syntax="pascaligo">
function address : contract 'a -> address
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val address : 'a contract -> address
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let address: contract('a) => address
</SyntaxTitle>
Get the address associated with a value of type `contract`.
@ -257,7 +374,7 @@ function main (const p : key_hash) : address is block {
} with Tezos.address(c)
```
> Note that `implicit_account` and `address` are *deprecated*.
> Note that `implicit_account` and `address` are *deprecated*. Please use `Tezos.implicit_account` and `Tezos.address` instead.
</Syntax>
<Syntax syntax="cameligo">
@ -287,7 +404,15 @@ let main = (p : key_hash) : address => {
</Syntax>
## Self Address
<SyntaxTitle syntax="pascaligo">
function self_address : address
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val self_address : address
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let self_address: address
</SyntaxTitle>
Get the address of the currently running contract.
@ -299,7 +424,7 @@ Get the address of the currently running contract.
function main (const p : unit) : address is Tezos.self_address
```
> Note that `self_address` is *deprecated*.
> Note that `self_address` is *deprecated*. Please use `Tezos.self_address`.
</Syntax>
<Syntax syntax="cameligo">
@ -320,8 +445,15 @@ let main = (p : unit) : address => Tezos.self_address;
> Note that `Current.self_address` is *deprecated*.
</Syntax>
## Self
<SyntaxTitle syntax="pascaligo">
function self : string -> contract 'a
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val self : string -> 'a contract
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let self: string => contract('a)
</SyntaxTitle>
Typecast the currently running contract with an entrypoint annotation.
If your are using entrypoints: use "%bar" for constructor Bar
@ -353,13 +485,21 @@ let main = (p: unit) : contract(unit) =>
</Syntax>
## Implicit Account
<SyntaxTitle syntax="pascaligo">
function implicit_account : key_hash -> contract 'a
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val implicit_account : key_hash -> 'a contract
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let implicit_account: key_hash => contract('a)
</SyntaxTitle>
Get the default contract associated with an on-chain key-pair. This
contract does not execute code, instead it exists to receive tokens on
behalf of a key's owner.
See also: http://tezos.gitlab.io/user/glossary.html#implicit-account
<Syntax syntax="pascaligo">
@ -368,7 +508,7 @@ function main (const kh: key_hash) : contract (unit) is
Tezos.implicit_account (kh)
```
> Note that `implicit_account` is *deprecated*.
> Note that `implicit_account` is *deprecated*. Please use `Tezos.implicit_account`.
</Syntax>
<Syntax syntax="cameligo">
@ -392,7 +532,15 @@ let main = (kh : key_hash): contract (unit) =>
</Syntax>
## Source
<SyntaxTitle syntax="pascaligo">
function source : address
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val source : address
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let source: address
</SyntaxTitle>
Get the _originator_ (address) of the current transaction. That is, if
a chain of transactions led to the current execution get the address
@ -426,7 +574,7 @@ current transaction.
function main (const p: unit) : address is Tezos.source
```
> Note that `source` is *deprecated*.
> Note that `source` is *deprecated*. Please use `Tezos.source`.
</Syntax>
<Syntax syntax="cameligo">
@ -449,7 +597,15 @@ let main = (p : unit) : address => Tezos.source;
</Syntax>
## Failwith
<SyntaxTitle syntax="pascaligo">
function failwith : string -> unit
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
function failwith : string -> unit
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
function failwith : string -> unit
</SyntaxTitle>
Cause the contract to fail with an error message.
@ -485,3 +641,125 @@ let main = ((p,s) : (int, unit)) =>
</Syntax>
<SyntaxTitle syntax="pascaligo">
function chain_id : chain_id
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val chain_id : chain_id
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let chain_id: chain_id
</SyntaxTitle>
Get the identifier of the chain to distinguish between main and test chains.
This is mainly intended to avoid replay attacks between the chains, and can currently
only be used together with `Bytes.pack` and `Bytes.unpack`.
<Syntax syntax="pascaligo">
```pascaligo
type storage is bytes
function main (const ignore : unit; const storage: storage) :
(list(operation) * storage) is block {
const packed : bytes = Bytes.pack (Tezos.chain_id);
if (storage =/= packed) then {
failwith("wrong chain")
} else
skip;
} with ((nil: list(operation)), packed)
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo
type storage = bytes
let main ((ignore, storage): (unit * storage)) =
let packed = Bytes.pack Tezos.chain_id in
if (storage <> packed) then
(failwith "wrong chain" : (operation list * storage))
else
(([]: operation list), (packed: storage))
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo
type storage = bytes;
let main = ((ignore, storage): (unit, storage)) => {
let packed = Bytes.pack(Tezos.chain_id);
if (storage != packed) {
(failwith("wrong chain"): (list(operation), storage));
} else {
([]: list(operation), packed);
}
};
```
</Syntax>
<SyntaxTitle syntax="pascaligo">
function transaction : 'parameter -> mutez -> contract('parameter) -> operation
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val transaction : 'parameter -> mutez -> 'parameter contract -> operation
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let transaction: 'parameter -> mutez -> contract('parameter) -> operation
</SyntaxTitle>
Create a transaction to a contract or account.
To indicate an account, use `unit` as `parameter`.
<SyntaxTitle syntax="pascaligo">
function set_delegate : option(key_hash) -> operation
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val set_delegate : key_hash option -> operation
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let set_delegate: option(key_hash) => operation
</SyntaxTitle>
Create a delegation.
See also: http://tezos.gitlab.io/user/glossary.html?highlight=delegate#delegate
<SyntaxTitle syntax="pascaligo">
function get_contract_opt : address -> option(contract('parameter))
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val get_contract_opt : address -> 'parameter contract option
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let get_contract_opt : address => option(contract('parameter))
</SyntaxTitle>
Get a contract from an address.
When no contract is found or the contract doesn't match the type,
`None` is returned.
<SyntaxTitle syntax="pascaligo">
function get_entrypoint_opt : string -> address -> option(contract('parameter))
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
function get_entrypoint_opt : string -> address -> 'parameter contract option
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
function get_entrypoint_opt: (string, address) => option(contract('parameter))
</SyntaxTitle>
Get a contract from an address and entrypoint.
Entrypoints are written in the form of: `%entrypoint`.
When no contract is found or the contract doesn't match the type,
`None` is returned.

View File

@ -1,96 +1,62 @@
---
id: list-reference
title: Lists — Linear Collections
title: List
description: List operations
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
Lists are linear collections of elements of the same type. Linear
means that, in order to reach an element in a list, we must visit all
the elements before (sequential access). Elements can be repeated, as
only their order in the collection matters. The first element is
called the *head*, and the sub-list after the head is called the
*tail*. For those familiar with algorithmic data structure, you can
think of a list a *stack*, where the top is written on the left.
<SyntaxTitle syntax="pascaligo">
type list ('t)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type 't list
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type list('t)
</SyntaxTitle>
# Defining Lists
A sequence of elements of the same type.
<SyntaxTitle syntax="pascaligo">
function length : nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val length : nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let length: nat
</SyntaxTitle>
<Syntax syntax="pascaligo">
Get the number of elements in a list.
```pascaligo group=lists
const empty_list : list (int) = nil // Or list []
const my_list : list (int) = list [1; 2; 2] // The head is 1
```
<SyntaxTitle syntax="pascaligo">
function size : nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val size : nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let size: nat
</SyntaxTitle>
</Syntax>
<Syntax syntax="cameligo">
Get the number of elements in a list.
```cameligo group=lists
let empty_list : int list = []
let my_list : int list = [1; 2; 2] // The head is 1
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=lists
let empty_list : list (int) = [];
let my_list : list (int) = [1, 2, 2]; // The head is 1
```
</Syntax>
# Adding to Lists
Lists can be augmented by adding an element before the head (or, in
terms of stack, by *pushing an element on top*).
<Syntax syntax="pascaligo">
```pascaligo group=lists
const larger_list : list (int) = 5 # my_list // [5;1;2;2]
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=lists
let larger_list : int list = 5 :: my_list // [5;1;2;2]
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=lists
let larger_list : list (int) = [5, ...my_list]; // [5,1,2,2]
```
</Syntax>
# Functional Iteration over Lists
A *functional iterator* is a function that traverses a data structure
and calls in turn a given function over the elements of that structure
to compute some value. Another approach is possible in PascaLIGO:
*loops* (see the relevant section).
There are three kinds of functional iterations over LIGO lists: the
*iterated operation*, the *map operation* (not to be confused with the
*map data structure*) and the *fold operation*.
## Iterated Operation over Lists
The first, the *iterated operation*, is an iteration over the list
with a unit return value. It is useful to enforce certain invariants
on the element of a list, or fail.
Synonym for `List.length`.
<SyntaxTitle syntax="pascaligo">
function iter : ('a -> unit) -> list('a) -> unit
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val iter : ('a -> unit) -> 'a list -> unit
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let iter: (('a => unit), list('a)) => unit
</SyntaxTitle>
Iterate over items in a list.
<Syntax syntax="pascaligo">
@ -104,6 +70,8 @@ function iter_op (const l : list (int)) : unit is
> Note that `list_iter` is *deprecated*.
Alternatively it's also possible to use [loops](../language-basics/loops.md).
</Syntax>
<Syntax syntax="cameligo">
@ -126,17 +94,23 @@ let iter_op = (l : list (int)) : unit => {
</Syntax>
## Mapped Operation over Lists
We may want to change all the elements of a given list by applying to
them a function. This is called a *map operation*, not to be confused
with the map data structure.
<SyntaxTitle syntax="pascaligo">
function map : ('a -> 'b) -> list('a) -> list('b)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val map : ('a -> 'b) -> 'a list -> 'b list
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let map: (('a => 'b), list('a)) => list('b)
</SyntaxTitle>
Apply a function to items of a list to create a new list.
<Syntax syntax="pascaligo">
```pascaligo group=lists
const larger_list: list(int) = list [1; 2; 3]
function increment (const i : int): int is i + 1
// Creates a new list with all elements incremented by 1
@ -149,6 +123,8 @@ const plus_one : list (int) = List.map (increment, larger_list)
<Syntax syntax="cameligo">
```cameligo group=lists
let larger_list: int list = [1; 2; 3]
let increment (i : int) : int = i + 1
// Creates a new list with all elements incremented by 1
@ -159,6 +135,8 @@ let plus_one : int list = List.map increment larger_list
<Syntax syntax="reasonligo">
```reasonligo group=lists
let larger_list: list(int) = [1, 2, 3];
let increment = (i : int) : int => i + 1;
// Creates a new list with all elements incremented by 1
@ -167,22 +145,25 @@ let plus_one : list (int) = List.map (increment, larger_list);
</Syntax>
<SyntaxTitle syntax="pascaligo">
function fold : (('accumulator -> 'item -> 'accumulator) -> list('item) -> 'accumulator) -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val fold : ('accumulator -> 'item -> 'accumulator) -> 'item list -> 'accumulator -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let fold: ((('accumulator, 'item) => 'accumulator), list('item), 'accumulator) => 'accumulator
</SyntaxTitle>
## Folded Operation over Lists
A *folded operation* is the most general of iterations. The folded
function takes two arguments: an *accumulator* and the structure
*element* at hand, with which it then produces a new accumulator. This
enables having a partial result that becomes complete when the
traversal of the data structure is over.
[Fold over items in a list](../language-basics/sets-lists-tuples#folded-operation-over-lists);
<Syntax syntax="pascaligo">
```pascaligo group=lists
const my_list: list(int) = list [1; 2; 3]
function sum (const acc : int; const i : int): int is acc + i
const sum_of_elements : int = List.fold (sum, my_list, 0)
```
@ -192,7 +173,10 @@ const sum_of_elements : int = List.fold (sum, my_list, 0)
<Syntax syntax="cameligo">
```cameligo group=lists
let my_list : int list = [1; 2; 3]
let sum (acc, i : int * int) : int = acc + i
let sum_of_elements : int = List.fold sum my_list 0
```
@ -200,40 +184,11 @@ let sum_of_elements : int = List.fold sum my_list 0
<Syntax syntax="reasonligo">
```reasonligo group=lists
let my_list : list(int) = [1, 2, 3];
let sum = ((result, i): (int, int)): int => result + i;
let sum_of_elements : int = List.fold (sum, my_list, 0);
```
</Syntax>
# List Length
Get the number of elements in a list.
<Syntax syntax="pascaligo">
```pascaligo
function size_of (const l : list (int)) : nat is List.length (l)
```
> Note that `size` is *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
```cameligo
let size_of (l : int list) : nat = List.length l
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo
let size_of = (l : list (int)) : nat => List.length (l);
```
</Syntax>

View File

@ -1,22 +1,28 @@
---
id: map-reference
title: Maps
title: Map
description: Map operations
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
*Maps* are a data structure which associate values of the same type to
values of the same type. The former are called *key* and the latter
*values*. Together they make up a *binding*. An additional requirement
is that the type of the keys must be *comparable*, in the Michelson
sense.
# Declaring a Map
<SyntaxTitle syntax="pascaligo">
type map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type ('key, 'value) map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type map ('key, 'value)
</SyntaxTitle>
<Syntax syntax="pascaligo">
The type of a map from values of type `key` to
values of type `value` is `map (key, value)`.
```pascaligo group=maps
type move is int * int
type register is map (address, move)
@ -25,6 +31,9 @@ type register is map (address, move)
</Syntax>
<Syntax syntax="cameligo">
The type of a map from values of type `key` to values
of type `value` is `(key, value) map`.
```cameligo group=maps
type move = int * int
type register = (address, move) map
@ -33,6 +42,9 @@ type register = (address, move) map
</Syntax>
<Syntax syntax="reasonligo">
The type of a map from values of type `key` to
values of type `value` is `map (key, value)`.
```reasonligo group=maps
type move = (int, int);
type register = map (address, move);
@ -40,13 +52,26 @@ type register = map (address, move);
</Syntax>
<SyntaxTitle syntax="pascaligo">
function empty : map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val empty : ('key, 'value) map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let empty: map('key, 'value)
</SyntaxTitle>
# Creating an Empty Map
Create an empty map.
<Syntax syntax="pascaligo">
```pascaligo group=maps
const empty : register = Map.empty
```
Or
```pascaligo group=maps
const empty : register = map []
```
@ -68,16 +93,34 @@ let empty : register = Map.empty
</Syntax>
# Creating a Non-empty Map
<SyntaxTitle syntax="pascaligo">
function literal : list ('key * 'value) -> map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val literal : ('key * 'value) list -> ('key, 'value) map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let literal: list(('key, 'value)) => map('key, 'value)
</SyntaxTitle>
Create a non-empty map.
<Syntax syntax="pascaligo">
```pascaligo group=maps
const moves : register =
Map.literal (list [
(("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2));
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))]);
```
Alternative way of creating an empty map:
```pascaligo group=maps
const moves_alternative : register =
map [
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) -> (1,2);
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (0,3)]
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (0,3)];
```
</Syntax>
@ -103,14 +146,32 @@ let moves : register =
</Syntax>
# Accessing Map Bindings
<SyntaxTitle syntax="pascaligo">
function find_opt : 'key -> map ('key, 'value) -> option 'value
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val find_opt : 'key -> ('key, 'value) map -> 'value option
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let find_opt : ('key, map ('key, 'value)) => option ('value)
</SyntaxTitle>
Retrieve a (option) value from a map with the given key. Returns `None` if the
key is missing and the value otherwise.
<Syntax syntax="pascaligo">
```pascaligo group=maps
const my_balance : option (move) =
moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)]
Map.find_opt (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), moves)
```
Alternatively:
```pascaligo group=maps
const my_balance_alternative : option (move) =
moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)];
```
</Syntax>
@ -126,67 +187,40 @@ let my_balance : move option =
```reasonligo group=maps
let my_balance : option (move) =
Map.find_opt (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), moves);
Map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address, moves);
```
</Syntax>
Notice how the value we read is an optional value: this is to force
the reader to account for a missing key in the map. This requires
*pattern matching*.
<SyntaxTitle syntax="pascaligo">
function update : 'key -> option 'value -> map ('key, 'value) -> map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val update: 'key -> 'value option -> ('key, 'value) map -> ('key, 'value) map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let update: ('key, option('value), map('key, 'value)) => map ('key, 'value)
</SyntaxTitle>
Note: when `None` is used as a value, the key and associated value is removed
from the map.
<Syntax syntax="pascaligo">
```pascaligo group=maps
function force_access (const key : address; const moves : register) : move is
case moves[key] of
Some (move) -> move
| None -> (failwith ("No move.") : move)
end
const updated_map : register = Map.update(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some (4,9), moves);
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=maps
let force_access (key, moves : address * register) : move =
match Map.find_opt key moves with
Some move -> move
| None -> (failwith "No move." : move)
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=maps
let force_access = ((key, moves) : (address, register)) : move => {
switch (Map.find_opt (key, moves)) {
| Some (move) => move
| None => failwith ("No move.") : move
}
};
```
</Syntax>
# Updating a Map
Given a map, we may want to add a new binding, remove one, or modify
one by changing the value associated to an already existing key. All
those operations are called *updates*.
<Syntax syntax="pascaligo">
Alternatively:
```pascaligo group=maps
function assign (var m : register) : register is
function update (var m : register) : register is
block {
m [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)] := (4,9)
m [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)] := (4,9);
} with m
```
If multiple bindings need to be updated, PascaLIGO offers a *patch
@ -206,14 +240,40 @@ function assignments (var m : register) : register is
<Syntax syntax="cameligo">
```cameligo group=maps
let assign (m : register) : register =
let updated_map : register =
Map.update
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4,9)) m
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4,9)) moves
```
Notice the optional value `Some (4,9)` instead of `(4,9)`. If we had
use `None` instead, that would have meant that the binding is removed.
As a particular case, we can only add a key and its associated value.
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=maps
let updated_map : register =
Map.update
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some ((4,9)), moves);
```
</Syntax>
<SyntaxTitle syntax="pascaligo">
function add : 'key -> 'value -> map ('key, 'value) -> map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val add : 'key -> 'value -> ('key, 'value) map -> ('key, 'value) map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let add: ('key, 'value, map('key, 'value)) => map('key, 'value)
</SyntaxTitle>
<Syntax syntax="pascaligo">
```pascaligo group=maps
const added_item : register = Map.add (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (4, 9), moves)
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=maps
let add (m : register) : register =
@ -224,17 +284,6 @@ let add (m : register) : register =
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=maps
let assign = (m : register) : register =>
Map.update
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), Some ((4,9)), m);
```
Notice the optional value `Some (4,9)` instead of `(4,9)`. If we had
use `None` instead, that would have meant that the binding is removed.
As a particular case, we can only add a key and its associated value.
```reasonligo group=maps
let add = (m: register): register =>
Map.add
@ -244,57 +293,63 @@ let add = (m : register) : register =>
</Syntax>
To remove a binding from a map, we need its key.
<SyntaxTitle syntax="pascaligo">
function remove : 'key -> map ('key, 'value) -> map ('key, 'value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val remove : 'key -> ('key, 'value) map -> ('key, 'value) map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let remove: (key, map('key, 'value)) => map('key, 'value)
</SyntaxTitle>
<Syntax syntax="pascaligo">
```pascaligo group=maps
function delete (const key : address; var moves : register) : register is
const updated_map : register =
Map.remove (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), moves)
```
Alternatively, the instruction `remove key from map m` removes the key
`key` from the map `m`.
```pascaligo group=maps
function rem (var m : register) : register is
block {
remove key from map moves
} with moves
remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) from map moves
} with m
const updated_map : register = rem (moves)
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=maps
let delete (key, moves : address * register) : register =
Map.remove key moves
let updated_map : register =
Map.remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) moves
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=maps
let delete = ((key, moves) : (address, register)) : register =>
Map.remove (key, moves);
let updated_map : register =
Map.remove (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), moves)
```
</Syntax>
# Functional Iteration over Maps
A *functional iterator* is a function that traverses a data structure
and calls in turn a given function over the elements of that structure
to compute some value. Another approach is possible in PascaLIGO:
*loops* (see the relevant section).
There are three kinds of functional iterations over LIGO maps: the
*iterated operation*, the *map operation* (not to be confused with the
*map data structure*) and the *fold operation*.
## Iterated Operation over Maps
The first, the *iterated operation*, is an iteration over the map with
no return value: its only use is to produce side-effects. This can be
useful if for example you would like to check that each value inside
of a map is within a certain range, and fail with an error otherwise.
<SyntaxTitle syntax="pascaligo">
function iter : ((key, value) -> unit) -> map (key, value) -> unit
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val iter : (('key * 'value) -> unit) -> ('key, 'value) map -> unit
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let iter: ((('key, 'value)) => unit, map('key, 'value)) => unit
</SyntaxTitle>
<Syntax syntax="pascaligo">
@ -330,14 +385,15 @@ let iter_op = (m : register) : unit => {
</Syntax>
## Map Operations over Maps
We may want to change all the bindings of a map by applying to them a
function. This is called a *map operation*, not to be confused with
the map data structure. The predefined functional iterator
implementing the map operation over maps is called `Map.map`.
<SyntaxTitle syntax="pascaligo">
function map : (('key, 'value) -> ('mapped_key, 'mapped_item)) -> map ('key, 'value) -> map ('mapped_key, 'mapped_value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val map : (('key * 'value) -> ('mapped_key * 'mapped_item)) -> (key, value) map -> (mapped_key, mapped_value) map
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let map: ((('key, 'value)) => ('mapped_key, 'mapped_item), map(key, value)) => map(mapped_key, mapped_value)
</SyntaxTitle>
<Syntax syntax="pascaligo">
@ -374,14 +430,15 @@ let map_op = (m : register) : register => {
</Syntax>
## Folded Operations over Maps
A *folded operation* is the most general of iterations. The folded
function takes two arguments: an *accumulator* and the structure
*element* at hand, with which it then produces a new accumulator. This
enables having a partial result that becomes complete when the
traversal of the data structure is over.
<SyntaxTitle syntax="pascaligo">
function fold : (('accumulator -> ('key, 'value) -> 'accumulator) -> map ('key, 'value) -> 'accumulator) -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val fold : ('accumulator -> ('key * 'value) -> 'accumulator) -> ('key, 'value) map -> 'accumulator -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let fold: ((('accumulator, ('key, 'value)) => 'accumulator), map('key, 'value), 'accumulator) => 'accumulator
</SyntaxTitle>
<Syntax syntax="pascaligo">
@ -417,3 +474,28 @@ let fold_op = (m : register) : int => {
</Syntax>
<SyntaxTitle syntax="pascaligo">
function size : map ('key, 'value) -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val size : ('key, 'value) map -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let size: map('key, 'value) => nat
</SyntaxTitle>
Returns the number of items in the map.
<SyntaxTitle syntax="pascaligo">
function mem : key -> map (key, value) -> bool
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val mem : 'key -> ('key, 'value) map => bool
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let mem : ('key, map('key, 'value)) => bool
</SyntaxTitle>
Checks if a key exists in the map.

View File

@ -1,20 +1,45 @@
---
id: set-reference
title: Sets — Unordered unique collection of a type
title: Set
description: Set operations
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
Sets are unordered collections of values of the same type, like lists
are ordered collections. Like the mathematical sets and lists, sets
can be empty and, if not, elements of sets in LIGO are *unique*,
whereas they can be repeated in a *list*.
Sets are unordered collections of unique values of the same type.
# Empty Sets
<SyntaxTitle syntax="pascaligo">
type set ('value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type 'value set
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type set('value)
</SyntaxTitle>
<SyntaxTitle syntax="pascaligo">
function empty : set('value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val empty : 'value set
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let empty: set('value)
</SyntaxTitle>
Create an empty set.
<Syntax syntax="pascaligo">
```pascaligo group=sets
const my_set : set (int) = Set.empty
```
Alternative syntax:
```pascaligo group=sets
const my_set : set (int) = set []
```
@ -35,12 +60,26 @@ let my_set : set (int) = Set.empty;
</Syntax>
<SyntaxTitle syntax="pascaligo">
function literal : list('value) -> set('value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val literal : 'value list -> 'value set
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let literal: list('value) => set('value)
</SyntaxTitle>
# Non-empty Sets
Create a non-empty set.
<Syntax syntax="pascaligo">
```pascaligo group=sets
const my_set : set (int) = Set.literal (list [3; 2; 2; 1])
```
Or use the following syntax sugar:
```pascaligo group=sets
const my_set : set (int) = set [3; 2; 2; 1]
```
@ -50,7 +89,7 @@ const my_set : set (int) = set [3; 2; 2; 1]
```cameligo group=sets
let my_set : int set =
Set.add 3 (Set.add 2 (Set.add 2 (Set.add 1 (Set.empty : int set))))
Set.literal [3; 2; 2; 1]
```
</Syntax>
@ -58,19 +97,33 @@ let my_set : int set =
```reasonligo group=sets
let my_set : set (int) =
Set.add (3, Set.add (2, Set.add (2, Set.add (1, Set.empty : set (int)))));
Set.literal ([3, 2, 2, 1]);
```
</Syntax>
<SyntaxTitle syntax="pascaligo">
function mem : 'value -> set('value) -> 'bool
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val mem : 'value -> 'value set -> bool
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let mem: ('value, set('value)) => bool
</SyntaxTitle>
# Set Membership
Checks if a value exists in the set.
<Syntax syntax="pascaligo">
```pascaligo group=sets
const contains_3 : bool = my_set contains 3
const contains_3 : bool = Set.mem(3, my_set)
```
Or:
```pascaligo group=sets
const contains_3_alt : bool = my_set contains 3
```
</Syntax>
@ -89,12 +142,17 @@ let contains_3 : bool = Set.mem (3, my_set);
</Syntax>
<SyntaxTitle syntax="pascaligo">
function cardinal : set('value) -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val cardinal : 'value set -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let cardinal: set('value) => nat
</SyntaxTitle>
# Cardinal of Sets
The predefined function `Set.size` returns the number of
elements in a given set as follows.
Number of elements in a set.
<Syntax syntax="pascaligo">
@ -102,7 +160,7 @@ elements in a given set as follows.
const cardinal : nat = Set.size (my_set)
```
> Note that `size` is *deprecated*.
> Note that `size` is *deprecated*. Please use `Set.size`
</Syntax>
<Syntax syntax="cameligo">
@ -120,72 +178,41 @@ let cardinal : nat = Set.size (my_set);
</Syntax>
<SyntaxTitle syntax="pascaligo">
function add : 'value -> set('value) -> set('value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val add : 'value -> 'value set -> 'value set
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let add: ('value, set('value)) => set('value)
</SyntaxTitle>
# Updating Sets
Add a value to a set.
There are two ways to update a set, that is to add or remove from it.
<SyntaxTitle syntax="pascaligo">
function remove : 'value -> set('value) -> set('value)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val remove : 'value -> 'value set -> 'value set
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let remove: ('value, set('value)) => set('value)
</SyntaxTitle>
Remove a value from a set.
<Syntax syntax="pascaligo">
<SyntaxTitle syntax="pascaligo">
function iter : ('a -> unit) -> set('a) -> unit
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val iter : ('a -> unit) -> 'a set -> unit
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let iter: (('a => unit), set('a)) => unit
</SyntaxTitle>
In PascaLIGO, either we create a new set from the given one, or we
modify it in-place. First, let us consider the former way:
```pascaligo group=sets
const larger_set : set (int) = Set.add (4, my_set)
const smaller_set : set (int) = Set.remove (3, my_set)
```
> Note that `set_add` and `set_remove` are *deprecated*.
If we are in a block, we can use an instruction to modify the set
bound to a given variable. This is called a *patch*. It is only
possible to add elements by means of a patch, not remove any: it is
the union of two sets.
```pascaligo group=sets
function update (var s : set (int)) : set (int) is block {
patch s with set [4; 7]
} with s
const new_set : set (int) = update (my_set)
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=sets
let larger_set : int set = Set.add 4 my_set
let smaller_set : int set = Set.remove 3 my_set
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=sets
let larger_set : set (int) = Set.add (4, my_set);
let smaller_set : set (int) = Set.remove (3, my_set);
```
</Syntax>
# Functional Iteration over Sets
A *functional iterator* is a function that traverses a data structure
and calls in turn a given function over the elements of that structure
to compute some value. Another approach is possible in PascaLIGO:
*loops* (see the relevant section).
There are three kinds of functional iterations over LIGO maps: the
*iterated operation*, the *mapped operation* (not to be confused with
the *map data structure*) and the *folded operation*.
## Iterated Operation
The first, the *iterated operation*, is an iteration over the map with
no return value: its only use is to produce side-effects. This can be
useful if for example you would like to check that each value inside
of a map is within a certain range, and fail with an error otherwise.
Iterate over values in a set.
<Syntax syntax="pascaligo">
@ -221,15 +248,17 @@ let iter_op = (s : set (int)) : unit => {
</Syntax>
<SyntaxTitle syntax="pascaligo">
function fold : (('accumulator -> 'item -> 'accumulator) -> set ('item) -> 'accumulator) -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val fold : ('accumulator -> 'item -> 'accumulator) -> 'set list -> 'accumulator -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let fold: ((('accumulator, 'item) => 'accumulator), set('item), 'accumulator) => 'accumulator
</SyntaxTitle>
## Folded Operation
A *folded operation* is the most general of iterations. The folded
function takes two arguments: an *accumulator* and the structure
*element* at hand, with which it then produces a new accumulator. This
enables having a partial result that becomes complete when the
traversal of the data structure is over.
[Fold over values in a set](../language-basics/sets-lists-tuples#folded-operation)
<Syntax syntax="pascaligo">
@ -241,17 +270,6 @@ const sum_of_elements : int = Set.fold (sum, my_set, 0)
> Note that `set_fold` is *deprecated*.
It is possible to use a *loop* over a set as well.
```pascaligo group=sets
function loop (const s : set (int)) : int is block {
var sum : int := 0;
for element in set s block {
sum := sum + element
}
} with sum
```
</Syntax>
<Syntax syntax="cameligo">
@ -269,4 +287,3 @@ let sum_of_elements : int = Set.fold (sum, my_set, 0);
```
</Syntax>

View File

@ -1,45 +1,77 @@
---
id: string-reference
title: String — Manipulate string data
title: String
description: Operations for strings.
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
## String.size(s: string) : nat
<SyntaxTitle syntax="pascaligo">
type string
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
type string
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
type string
</SyntaxTitle>
Get the size of a string. [Michelson only supports ASCII strings](http://tezos.gitlab.io/whitedoc/michelson.html#constants)
A sequence of characters.
<SyntaxTitle syntax="pascaligo">
function length : string -> nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val length : string -> nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let length: string => nat
</SyntaxTitle>
Get the size of a string.
[Michelson only supports ASCII strings](http://tezos.gitlab.io/whitedoc/michelson.html#constants)
so for now you can assume that each character takes one byte of storage.
<Syntax syntax="pascaligo">
```pascaligo
function string_size (const s: string) : nat is size(s)
function string_size (const s: string) : nat is String.length(s)
```
> Note that `size` and `String.size` are *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
```cameligo
let size_op (s: string) : nat = String.size s
let size_op (s: string) : nat = String.length s
```
> Note that `String.size` is *deprecated*.
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo
let size_op = (s: string): nat => String.size(s);
let size_op = (s: string): nat => String.length(s);
```
> Note that `String.size` is *deprecated*.
</Syntax>
## String.length(s: string) : nat
Alias for `String.size`.
## String.slice(pos1: nat, pos2: nat, s: string) : string
<SyntaxTitle syntax="pascaligo">
function sub : nat -> nat -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val sub : nat -> nat -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let sub: (nat, nat, string) => string
</SyntaxTitle>
Get the substring of `s` between `pos1` inclusive and `pos2` inclusive. For example
the string "tata" given to the function below would return "at".
@ -48,31 +80,41 @@ the string "tata" given to the function below would return "at".
<Syntax syntax="pascaligo">
```pascaligo
function slice_op (const s : string) : string is string_slice(1n , 2n , s)
function slice_op (const s : string) : string is String.sub(1n , 2n , s)
```
> Note that `string_slice` is *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
```cameligo
let slice_op (s: string) : string = String.slice 1n 2n s
let slice_op (s: string) : string = String.sub 1n 2n s
```
> Note that `String.slice` is *deprecated*.
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo
let slice_op = (s: string): string => String.slice(1n, 2n, s);
let slice_op = (s: string): string => String.sub(1n, 2n, s);
```
> Note that `String.slice` is *deprecated*.
</Syntax>
## String.sub(pos1: nat, pos2: nat, s: string) : string
Alias for `String.slice`.
## String.concat(s1: string, s2: string) : string
<SyntaxTitle syntax="pascaligo">
function concat : string -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val concat : string -> string -> string
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let concat: (string, string) => string
</SyntaxTitle>
Concatenate two strings and return the result.
@ -81,21 +123,40 @@ Concatenate two strings and return the result.
<Syntax syntax="pascaligo">
```pascaligo
function concat_op (const s : string) : string is s ^ "toto"
function concat_op (const s : string) : string is String.concat(s, "toto")
```
Alternatively:
```pascaligo
function concat_op_alt (const s : string) : string is s ^ "toto"
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo
let concat_syntax (s: string) = s ^ "test_literal"
let concat_syntax (s: string) = String.concat s "test_literal"
```
Alternatively:
```cameligo
let concat_syntax_alt (s: string) = s ^ "test_literal"
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo
let concat_syntax = (s: string) => s ++ "test_literal";
let concat_syntax = (s: string) => String.concat(s, "test_literal");
```
Alternatively:
```reasonligo
let concat_syntax_alt = (s: string) => s ++ "test_literal";
```
</Syntax>

View File

@ -21,17 +21,20 @@
"advanced/first-contract",
"advanced/michelson-and-ligo"
],
"API & Reference": [
"Reference": [
"api/cli-commands",
"api/cheat-sheet",
"api/cheat-sheet"
],
"API":[
"reference/big-map-reference",
"reference/bitwise-reference",
"reference/bytes-reference",
"reference/crypto-reference",
"reference/current-reference",
"reference/list-reference",
"reference/map-reference",
"reference/set-reference",
"reference/string-reference"
"reference/string-reference",
"reference/current-reference"
]
},
"contributors-docs": {

View File

@ -151,8 +151,8 @@ module Simplify = struct
(* String module *)
| "String.length" -> ok C_SIZE
| "String.size" -> ok C_SIZE
| "String.slice" -> ok C_SLICE
| "String.size" -> ok C_SIZE (* Deprecated *)
| "String.slice" -> ok C_SLICE (* Deprecated *)
| "String.sub" -> ok C_SLICE
| "String.concat" -> ok C_CONCAT