Improve ReasonLIGO documentation.

This commit is contained in:
Sander 2019-12-11 09:34:08 +00:00
parent 21a6749a81
commit 20793b0924
11 changed files with 91 additions and 53 deletions

View File

@ -21,7 +21,7 @@ let a: bool = true
let b: bool = false
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let a: bool = true;
let b: bool = false;
@ -50,7 +50,7 @@ let b: string = "Alice"
// true
let c: bool = (a = b)
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let a: string = "Alice";
let b: string = "Alice";
@ -86,7 +86,7 @@ let g: bool = (a >= b)
let h: bool = (a =/= b)
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let a: int = 5;
let b: int = 4;
@ -118,7 +118,7 @@ let b: tez = 10mutez
// false
let c: bool = (a = b)
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let a: tez = 5mutez;
let b: tez = 10mutez;
@ -176,7 +176,7 @@ let min_age: nat = 16n
let is_adult (age: nat) : bool =
if (age > min_age) then true else false
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let min_age: nat = 16n;
@ -199,7 +199,7 @@ let is_adult = (age: nat): bool =>
> You can run the function above with
> ```
> ligo run-function -s cameligo src/if-else.mligo is_adult 21n
> ligo run-function -s reasonligo src/if-else.religo is_adult 21n
> ```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -70,6 +70,22 @@ let add (a: int) (b: int) : int = a + b
The function body is a series of expressions, which are evaluated to give the return
value.
<!--ReasonLIGO-->
Functions in ReasonLIGO are defined using the `let` keyword, like value bindings.
The difference is that after the value name a list of function parameters is provided,
along with a return type.
Here's how you define a basic function that accepts two `ints` and returns an `int` as well:
```reasonligo
let add (a: int, b: int) : int = a + b;
```
The function body is a series of expressions, which are evaluated to give the return
value.
<!--END_DOCUSAURUS_CODE_TABS-->
<!--DOCUSAURUS_CODE_TABS-->
@ -99,7 +115,7 @@ const a: int = increment(1);
let increment : (int -> int) = fun (i: int) -> i + 1
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let increment: (int => int) = (i: int) => i + 1;
```

View File

@ -22,7 +22,7 @@ type ledger is map(address, tez);
type ledger = (address, tez) map
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type ledger = map(address, tez);
```
@ -57,19 +57,19 @@ let ledger: ledger = Map.literal
>
> `("<string value>": address)` means that we type-cast a string into an address.
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let ledger: ledger =
Map.literal([
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address, 1000(mutez)),
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address, 2000(mutez)),
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address, 1000mutez),
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address, 2000mutez),
]);
```
> Map.literal constructs the map from a list of key-value pair tuples, `(<key>, <value>)`.
>
> `("<string value>": address)` means that we type-cast a string into an address.
```
<!--END_DOCUSAURUS_CODE_TABS-->
### Accessing map values by key
@ -88,7 +88,7 @@ const balance: option(tez) = ledger[("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": add
let balance: tez option = Map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) ledger
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let balance: option(tez) =
@ -112,7 +112,7 @@ const balance: tez = get_force(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)
let balance: tez = Map.find ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) ledger
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let balance: tez =
@ -146,7 +146,7 @@ let iter_op (m : ledger) : unit =
in Map.iter assert_eq m
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let iter_op = (m: ledger): unit => {
let assert_eq = (i: address, j: tez) => assert(j > 100);
@ -173,7 +173,7 @@ let map_op (m : ledger) : ledger =
in Map.map increment m
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let map_op = (m: ledger): ledger => {
let increment = (ignore: address, j: tez) => j + 1;
@ -207,7 +207,7 @@ let fold_op (m : ledger) : ledger =
in Map.fold aggregate m 10
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let fold_op = (m: ledger): ledger => {
let aggregate = (ignore: address, j: (tez, tez)) => j[0] + j[1];
@ -243,7 +243,7 @@ type user = {
}
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type user = {
id: nat,
@ -275,7 +275,7 @@ let user: user = {
}
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let user: user = {
id: 1n,
@ -301,7 +301,7 @@ const is_admin: bool = user.is_admin;
let is_admin: bool = user.is_admin
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let is_admin: bool = user.is_admin;
```

View File

@ -60,7 +60,7 @@ let g: int = 1_000_000
>let g: int = 1_000_000;
>```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
// int + int produces int
@ -71,6 +71,7 @@ let b: int = 5n + 10;
let c: tez = 5mutez + 10mutez;
// you can't add tez + int or tez + nat, this won't compile
// let d: tez = 5mutez + 10n;
// two nats produce a nat
let e: nat = 5n + 10n;
// nat + int produces an int, this won't compile
// let f: nat = 5n + 10;
@ -112,7 +113,7 @@ let b: int = 5n - 2n
let d: tez = 5mutez - 1mt
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let a: int = 5 - 10;
// substraction of two nats, yields an int
@ -147,7 +148,7 @@ let b: nat = 5n * 5n
let c: tez = 5n * 5mutez
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let a: int = 5 * 5;
let b: nat = 5n * 5n;
@ -179,7 +180,7 @@ let b: nat = 10n / 3n
let c: nat = 10mutez / 3mutez
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let a: int = 10 / 3;
let b: nat = 10n / 3n;
@ -199,4 +200,10 @@ const a: int = int(1n);
const b: nat = abs(1);
```
<!--ReasonLIGO-->
```reasonligo
let a: int = int(1n);
let b: nat = abs(1);
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -31,7 +31,7 @@ let my_set: int_set =
Set.add 3 (Set.add 2 (Set.add 1 (Set.empty: int set)))
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type int_set = set(int);
let my_set: int_set =
@ -52,7 +52,7 @@ const my_set_2: int_set = set_empty;
```cameligo
let my_set: int_set = (Set.empty: int set)
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let my_set: int_set = (Set.empty: set(int));
```
@ -72,7 +72,7 @@ const contains_three_fn: bool = set_mem(3, my_set);
```cameligo
let contains_three: bool = Set.mem 3 my_set
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let contains_three: bool = Set.mem(3, my_set);
```
@ -92,7 +92,7 @@ const set_size: nat = size(my_set);
let set_size: nat = Set.size my_set
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let set_size: nat = Set.size(my_set);
```
@ -115,7 +115,7 @@ let larger_set: int_set = Set.add 4 my_set
let smaller_set: int_set = Set.remove 3 my_set
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let larger_set: int_set = Set.add(4, my_set);
@ -140,7 +140,7 @@ let sum (result: int) (i: int) : int = result + i
let sum_of_a_set: int = Set.fold sum my_set 0
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let sum = (result: int, i: int): int => result + i;
let sum_of_a_set: int = Set.fold(sum, my_set, 0);
@ -172,7 +172,7 @@ type int_list = int list
let my_list: int_list = [1; 2; 3]
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type int_list = list(int);
let my_list: int_list = [1, 2, 3];
@ -196,10 +196,10 @@ let larger_list: int_list = 4 :: my_list
(* CameLIGO doesn't have a List.cons *)
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let larger_list: int_list = [4, ...my_list];
/* Reasonligo doesn't have a List.cons */
/* ReasonLIGO doesn't have a List.cons */
```
<!--END_DOCUSAURUS_CODE_TABS-->
@ -226,7 +226,7 @@ let incremented_list: int_list = List.map increment larger_list
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let increment = (i: int): int => i + 1;
@ -254,7 +254,7 @@ let sum (result: int) (i: int) : int = result + i
let sum_of_a_list: int = List.fold sum my_list 0
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let sum = (result: int, i: int): int => result + i;
@ -299,7 +299,7 @@ type full_name = string * string
let full_name: full_name = ("Alice", "Johnson")
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type full_name = (string, string);
/* The parenthesis here are optional */
@ -329,7 +329,7 @@ const first_name: string = full_name.1;
let first_name: string = full_name.1
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let first_name: string = full_name[1];
```

View File

@ -0,0 +1,4 @@
let add = (a: int, b: int): int => {
let c: int = a + b;
c;
};

View File

@ -0,0 +1 @@
let age : int = 25;

View File

@ -16,15 +16,19 @@ const a: string = "Hello Alice";
```
let a: string = "Hello Alice"
```
<!--ReasonLIGO-->
```
let a: string = "Hello Alice";
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Concatenating strings
Strings can be concatenated using the `^` operator.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
Strings can be concatenated using the `^` operator.
```pascaligo
const name: string = "Alice";
const greeting: string = "Hello";
@ -34,12 +38,16 @@ const full_greeting: string = greeting ^ " " ^ name;
const full_greeting_exclamation: string = string_concat(full_greeting, "!");
```
<!--Cameligo-->
Strings can be concatenated using the `^` operator.
```cameligo
let name: string = "Alice"
let greeting: string = "Hello"
let full_greeting: string = greeting ^ " " ^ name
```
<!--Reasonligo-->
<!--ReasonLIGO-->
Strings can be concatenated using the `++` operator.
```reasonligo
let name: string = "Alice";
let greeting: string = "Hello";
@ -64,7 +72,7 @@ const slice: string = string_slice(0n, 1n, name);
let name: string = "Alice"
let slice: string = String.slice 0n 1n name
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let name: string = "Alice";
let slice: string = String.slice(0n, 1n, name);
@ -89,7 +97,7 @@ const length: nat = size(name);
let name: string = "Alice"
let length: nat = String.size name
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let name: string = "Alice";
let length: nat = String.size(name);

View File

@ -27,7 +27,7 @@ type animal_breed = string
let dog_breed: animal_breed = "Saluki"
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type animal_breed = string;
@ -64,7 +64,7 @@ type account_balances = map(address, tez);
let ledger: account_balances =
Map.literal([
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address, 10(mutez)),
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address, 10mutez),
]);
```
@ -141,7 +141,7 @@ let ledger: account_balances =
Map.literal([
(
"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address,
{balance: 10(mutez), number_of_transactions: 5n},
{balance: 10mutez, number_of_transactions: 5n},
),
]);

View File

@ -24,7 +24,7 @@ const n: unit = Unit;
let n: unit = ()
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let n: unit = ();
```
@ -62,7 +62,7 @@ let u: user = Admin 1000n
let g: user = Guest ()
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type id = nat;
type user =
@ -101,7 +101,7 @@ let p1: dinner = None
let p2: dinner = Some "Hamburgers"
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type dinner = option(string);
@ -138,7 +138,7 @@ let is_hungry (d: dinner) : bool =
| Some s -> false
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
type dinner = option(string);
let is_hungry = (d: dinner): bool =>

View File

@ -5,6 +5,8 @@ title: Constants & Variables
The next building block after types are constants and variables.
pleh.
## Constants
Constants are immutable by design, which means their values can't be reassigned.
@ -32,7 +34,7 @@ ligo evaluate-value -s cameligo gitlab-pages/docs/language-basics/src/variables-
# Outputs: 25
```
<!--Reasonligo-->
<!--ReasonLIGO-->
```reasonligo
let age: int = 25;
```
@ -98,9 +100,9 @@ You can run the `add` function defined above using the LIGO compiler like this:
ligo run-function -s cameligo gitlab-pages/docs/language-basics/src/variables-and-constants/add.mligo add '(1,1)'
# Outputs: 2
```
<!--Reasonligo-->
<!--ReasonLIGO-->
As expected from a functional language, Reasonligo uses value-binding
As expected from a functional language, ReasonLIGO uses value-binding
for variables rather than assignment. Variables are changed by replacement,
with a new value being bound in place of the old one.