From 20793b09248b7773fac1dd5fb4f32da7f3c7115c Mon Sep 17 00:00:00 2001 From: Sander Date: Wed, 11 Dec 2019 09:34:08 +0000 Subject: [PATCH] Improve ReasonLIGO documentation. --- .../docs/language-basics/boolean-if-else.md | 12 ++++---- .../docs/language-basics/functions.md | 18 +++++++++++- .../docs/language-basics/maps-records.md | 28 +++++++++---------- .../docs/language-basics/math-numbers-tez.md | 15 +++++++--- .../language-basics/sets-lists-touples.md | 26 ++++++++--------- .../src/variables-and-constants/add.religo | 4 +++ .../src/variables-and-constants/const.religo | 1 + gitlab-pages/docs/language-basics/strings.md | 18 ++++++++---- gitlab-pages/docs/language-basics/types.md | 6 ++-- .../unit-option-pattern-matching.md | 8 +++--- .../variables-and-constants.md | 8 ++++-- 11 files changed, 91 insertions(+), 53 deletions(-) create mode 100644 gitlab-pages/docs/language-basics/src/variables-and-constants/add.religo create mode 100644 gitlab-pages/docs/language-basics/src/variables-and-constants/const.religo diff --git a/gitlab-pages/docs/language-basics/boolean-if-else.md b/gitlab-pages/docs/language-basics/boolean-if-else.md index 7dfabba45..379c8ea04 100644 --- a/gitlab-pages/docs/language-basics/boolean-if-else.md +++ b/gitlab-pages/docs/language-basics/boolean-if-else.md @@ -21,7 +21,7 @@ let a: bool = true let b: bool = false ``` - + ```reasonligo let a: bool = true; let b: bool = false; @@ -50,7 +50,7 @@ let b: string = "Alice" // true let c: bool = (a = b) ``` - + ```reasonligo let a: string = "Alice"; let b: string = "Alice"; @@ -86,7 +86,7 @@ let g: bool = (a >= b) let h: bool = (a =/= b) ``` - + ```reasonligo let a: int = 5; let b: int = 4; @@ -118,7 +118,7 @@ let b: tez = 10mutez // false let c: bool = (a = b) ``` - + ```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 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 > ``` \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/functions.md b/gitlab-pages/docs/language-basics/functions.md index 189d5a3e1..5488d507f 100644 --- a/gitlab-pages/docs/language-basics/functions.md +++ b/gitlab-pages/docs/language-basics/functions.md @@ -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. + + + +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. + @@ -99,7 +115,7 @@ const a: int = increment(1); let increment : (int -> int) = fun (i: int) -> i + 1 ``` - + ```reasonligo let increment: (int => int) = (i: int) => i + 1; ``` diff --git a/gitlab-pages/docs/language-basics/maps-records.md b/gitlab-pages/docs/language-basics/maps-records.md index 60e6d4bd9..38403511e 100644 --- a/gitlab-pages/docs/language-basics/maps-records.md +++ b/gitlab-pages/docs/language-basics/maps-records.md @@ -22,7 +22,7 @@ type ledger is map(address, tez); type ledger = (address, tez) map ``` - + ```reasonligo type ledger = map(address, tez); ``` @@ -57,19 +57,19 @@ let ledger: ledger = Map.literal > > `("": address)` means that we type-cast a string into an address. - + ```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, `(, )`. > > `("": address)` means that we type-cast a string into an address. -``` + ### 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 let balance: option(tez) = @@ -112,7 +112,7 @@ const balance: tez = get_force(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) let balance: tez = Map.find ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) ledger ``` - + ```reasonligo let balance: tez = @@ -146,7 +146,7 @@ let iter_op (m : ledger) : unit = in Map.iter assert_eq m ``` - + ```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 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 let fold_op = (m: ledger): ledger => { let aggregate = (ignore: address, j: (tez, tez)) => j[0] + j[1]; @@ -243,7 +243,7 @@ type user = { } ``` - + ```reasonligo type user = { id: nat, @@ -275,7 +275,7 @@ let user: user = { } ``` - + ```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 let is_admin: bool = user.is_admin; ``` diff --git a/gitlab-pages/docs/language-basics/math-numbers-tez.md b/gitlab-pages/docs/language-basics/math-numbers-tez.md index 8ad4ecc96..02be976ed 100644 --- a/gitlab-pages/docs/language-basics/math-numbers-tez.md +++ b/gitlab-pages/docs/language-basics/math-numbers-tez.md @@ -60,7 +60,7 @@ let g: int = 1_000_000 >let g: int = 1_000_000; >``` - + ```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 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 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 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 +let a: int = int(1n); +let b: nat = abs(1); +``` + \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/sets-lists-touples.md b/gitlab-pages/docs/language-basics/sets-lists-touples.md index ca8638234..9549f69ac 100644 --- a/gitlab-pages/docs/language-basics/sets-lists-touples.md +++ b/gitlab-pages/docs/language-basics/sets-lists-touples.md @@ -31,7 +31,7 @@ let my_set: int_set = Set.add 3 (Set.add 2 (Set.add 1 (Set.empty: int set))) ``` - + ```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 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 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 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 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 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 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 let larger_list: int_list = [4, ...my_list]; -/* Reasonligo doesn't have a List.cons */ +/* ReasonLIGO doesn't have a List.cons */ ``` @@ -226,7 +226,7 @@ let incremented_list: int_list = List.map increment larger_list ``` - + ```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 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 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 let first_name: string = full_name[1]; ``` diff --git a/gitlab-pages/docs/language-basics/src/variables-and-constants/add.religo b/gitlab-pages/docs/language-basics/src/variables-and-constants/add.religo new file mode 100644 index 000000000..0cabb6def --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/variables-and-constants/add.religo @@ -0,0 +1,4 @@ +let add = (a: int, b: int): int => { + let c: int = a + b; + c; +}; \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/variables-and-constants/const.religo b/gitlab-pages/docs/language-basics/src/variables-and-constants/const.religo new file mode 100644 index 000000000..3c3b108e5 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/variables-and-constants/const.religo @@ -0,0 +1 @@ +let age : int = 25; \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/strings.md b/gitlab-pages/docs/language-basics/strings.md index 16efeba0d..76435b62c 100644 --- a/gitlab-pages/docs/language-basics/strings.md +++ b/gitlab-pages/docs/language-basics/strings.md @@ -16,15 +16,19 @@ const a: string = "Hello Alice"; ``` let a: string = "Hello Alice" ``` + +``` +let a: string = "Hello Alice"; +``` ## Concatenating strings -Strings can be concatenated using the `^` operator. - +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, "!"); ``` +Strings can be concatenated using the `^` operator. + ```cameligo let name: string = "Alice" let greeting: string = "Hello" let full_greeting: string = greeting ^ " " ^ name ``` - + +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 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 let name: string = "Alice"; let length: nat = String.size(name); diff --git a/gitlab-pages/docs/language-basics/types.md b/gitlab-pages/docs/language-basics/types.md index 32289c2a3..03762940c 100644 --- a/gitlab-pages/docs/language-basics/types.md +++ b/gitlab-pages/docs/language-basics/types.md @@ -27,7 +27,7 @@ type animal_breed = string let dog_breed: animal_breed = "Saluki" ``` - + ```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}, ), ]); diff --git a/gitlab-pages/docs/language-basics/unit-option-pattern-matching.md b/gitlab-pages/docs/language-basics/unit-option-pattern-matching.md index 567890145..c533f3476 100644 --- a/gitlab-pages/docs/language-basics/unit-option-pattern-matching.md +++ b/gitlab-pages/docs/language-basics/unit-option-pattern-matching.md @@ -24,7 +24,7 @@ const n: unit = Unit; let n: unit = () ``` - + ```reasonligo let n: unit = (); ``` @@ -62,7 +62,7 @@ let u: user = Admin 1000n let g: user = Guest () ``` - + ```reasonligo type id = nat; type user = @@ -101,7 +101,7 @@ let p1: dinner = None let p2: dinner = Some "Hamburgers" ``` - + ```reasonligo type dinner = option(string); @@ -138,7 +138,7 @@ let is_hungry (d: dinner) : bool = | Some s -> false ``` - + ```reasonligo type dinner = option(string); let is_hungry = (d: dinner): bool => diff --git a/gitlab-pages/docs/language-basics/variables-and-constants.md b/gitlab-pages/docs/language-basics/variables-and-constants.md index 2c29f18c6..f2d388abd 100644 --- a/gitlab-pages/docs/language-basics/variables-and-constants.md +++ b/gitlab-pages/docs/language-basics/variables-and-constants.md @@ -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 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 ``` - + -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.