From 0e7b79026d14f066c9d90a6f385e95f14d584a82 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Wed, 1 Jan 2020 12:30:55 -0800 Subject: [PATCH 1/4] Add docs page for loops --- gitlab-pages/docs/language-basics/loops.md | 87 ++++++++++++++++++++++ gitlab-pages/website/sidebars.json | 1 + 2 files changed, 88 insertions(+) create mode 100644 gitlab-pages/docs/language-basics/loops.md diff --git a/gitlab-pages/docs/language-basics/loops.md b/gitlab-pages/docs/language-basics/loops.md new file mode 100644 index 000000000..6ac382fe5 --- /dev/null +++ b/gitlab-pages/docs/language-basics/loops.md @@ -0,0 +1,87 @@ +--- +id: loops +title: Loops +--- + + + +## While Loop + + + + + +The PascaLIGO while loop should look familiar to users of imperative languages. +While loops are of the form `while `, and evaluate +their associated block until the condition evaluates to false. + +> ⚠️ The current PascaLIGO while loop has semantics that have diverged from other LIGO syntaxes. The goal of LIGO is that the various syntaxes express the same semantics, so this will be corrected in future versions. For details on how loops will likely work after refactoring, see the CameLIGO tab of this example. + +```pascaligo +function while_sum (var n : nat) : nat is block { + var i : nat := 0n ; + var r : nat := 0n ; + while i < n block { + i := i + 1n; + r := r + i; + } +} with r +``` + + + +`Loop.fold_while` is a fold operation that takes an initial value of a certain type +and then iterates on it until a condition is reached. The auxillary function +that does the fold returns either boolean true or boolean false to indicate +whether the fold should continue or not. The initial value must match the input +parameter of the auxillary function, and the auxillary should return type `(bool * input)`. + +```cameligo +let aux (i: int) : bool * int = + if i < 100 then continue (i + 1) else stop i + +let counter_simple (n: int) : int = + Loop.fold_while aux_simple n +``` + + + +## For Loop + + + + +To iterate over a range of integers you use a loop of the form `for to `. + +```pascaligo +function for_sum (var n : nat) : int is block { + var acc : int := 0 ; + for i := 1 to int(n) + begin + acc := acc + i; + end +} with acc +``` + + + + + + +PascaLIGO for loops can also iterate through the contents of a collection. This is +done with a loop of the form `for in `. + +```pascaligo +function for_collection_list (var nee : unit) : (int * string) is block { + var acc : int := 0; + var st : string := "to"; + var mylist : list(int) := list 1; 1; 1 end; + for x in list mylist + begin + acc := acc + x; + st := st ^ "to"; + end +} with (acc, st) +``` + + diff --git a/gitlab-pages/website/sidebars.json b/gitlab-pages/website/sidebars.json index 13ce03e4e..4c5017e56 100644 --- a/gitlab-pages/website/sidebars.json +++ b/gitlab-pages/website/sidebars.json @@ -8,6 +8,7 @@ "language-basics/strings", "language-basics/functions", "language-basics/boolean-if-else", + "language-basics/loops", "language-basics/unit-option-pattern-matching", "language-basics/maps-records", "language-basics/sets-lists-touples" From 6b469ac869d20db0fa7d9237d44f827b1d66cdef Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Wed, 1 Jan 2020 12:30:55 -0800 Subject: [PATCH 2/4] Add docs page for loops --- gitlab-pages/docs/language-basics/loops.md | 87 ++++++++++++++++++++++ gitlab-pages/website/sidebars.json | 1 + 2 files changed, 88 insertions(+) create mode 100644 gitlab-pages/docs/language-basics/loops.md diff --git a/gitlab-pages/docs/language-basics/loops.md b/gitlab-pages/docs/language-basics/loops.md new file mode 100644 index 000000000..6ac382fe5 --- /dev/null +++ b/gitlab-pages/docs/language-basics/loops.md @@ -0,0 +1,87 @@ +--- +id: loops +title: Loops +--- + + + +## While Loop + + + + + +The PascaLIGO while loop should look familiar to users of imperative languages. +While loops are of the form `while `, and evaluate +their associated block until the condition evaluates to false. + +> ⚠️ The current PascaLIGO while loop has semantics that have diverged from other LIGO syntaxes. The goal of LIGO is that the various syntaxes express the same semantics, so this will be corrected in future versions. For details on how loops will likely work after refactoring, see the CameLIGO tab of this example. + +```pascaligo +function while_sum (var n : nat) : nat is block { + var i : nat := 0n ; + var r : nat := 0n ; + while i < n block { + i := i + 1n; + r := r + i; + } +} with r +``` + + + +`Loop.fold_while` is a fold operation that takes an initial value of a certain type +and then iterates on it until a condition is reached. The auxillary function +that does the fold returns either boolean true or boolean false to indicate +whether the fold should continue or not. The initial value must match the input +parameter of the auxillary function, and the auxillary should return type `(bool * input)`. + +```cameligo +let aux (i: int) : bool * int = + if i < 100 then continue (i + 1) else stop i + +let counter_simple (n: int) : int = + Loop.fold_while aux_simple n +``` + + + +## For Loop + + + + +To iterate over a range of integers you use a loop of the form `for to `. + +```pascaligo +function for_sum (var n : nat) : int is block { + var acc : int := 0 ; + for i := 1 to int(n) + begin + acc := acc + i; + end +} with acc +``` + + + + + + +PascaLIGO for loops can also iterate through the contents of a collection. This is +done with a loop of the form `for in `. + +```pascaligo +function for_collection_list (var nee : unit) : (int * string) is block { + var acc : int := 0; + var st : string := "to"; + var mylist : list(int) := list 1; 1; 1 end; + for x in list mylist + begin + acc := acc + x; + st := st ^ "to"; + end +} with (acc, st) +``` + + diff --git a/gitlab-pages/website/sidebars.json b/gitlab-pages/website/sidebars.json index f684e1fb4..00fe28d25 100644 --- a/gitlab-pages/website/sidebars.json +++ b/gitlab-pages/website/sidebars.json @@ -8,6 +8,7 @@ "language-basics/strings", "language-basics/functions", "language-basics/boolean-if-else", + "language-basics/loops", "language-basics/unit-option-pattern-matching", "language-basics/maps-records", "language-basics/sets-lists-touples" From e25c5bac3542393e22ded87be345073ecc1fafd1 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Thu, 2 Jan 2020 20:38:45 +0100 Subject: [PATCH 3/4] adding loops.md to the list of tested file and fixed "loops.md" --- gitlab-pages/docs/language-basics/loops.md | 2 +- src/test/md_file_tests.ml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/gitlab-pages/docs/language-basics/loops.md b/gitlab-pages/docs/language-basics/loops.md index 6ac382fe5..eee32142e 100644 --- a/gitlab-pages/docs/language-basics/loops.md +++ b/gitlab-pages/docs/language-basics/loops.md @@ -41,7 +41,7 @@ let aux (i: int) : bool * int = if i < 100 then continue (i + 1) else stop i let counter_simple (n: int) : int = - Loop.fold_while aux_simple n + Loop.fold_while aux n ``` diff --git a/src/test/md_file_tests.ml b/src/test/md_file_tests.ml index c2e275a3d..eb89fa4a5 100644 --- a/src/test/md_file_tests.ml +++ b/src/test/md_file_tests.ml @@ -92,6 +92,7 @@ let md_files = [ "/gitlab-pages/docs/language-basics/sets-lists-touples.md"; "/gitlab-pages/docs/language-basics/operators.md"; "/gitlab-pages/docs/language-basics/unit-option-pattern-matching.md"; + "/gitlab-pages/docs/language-basics/loops.md"; "/gitlab-pages/docs/contributors/big-picture/back-end.md"; "/gitlab-pages/docs/contributors/big-picture/vendors.md"; "/gitlab-pages/docs/contributors/big-picture/front-end.md"; From fcd33026bb5ccf6a7d9b1a73845cbc2bc44d3fbd Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Thu, 2 Jan 2020 12:26:02 -0800 Subject: [PATCH 4/4] Fix loops page with ReasonLIGO --- gitlab-pages/docs/language-basics/loops.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gitlab-pages/docs/language-basics/loops.md b/gitlab-pages/docs/language-basics/loops.md index 964bf35ba..9fd26d024 100644 --- a/gitlab-pages/docs/language-basics/loops.md +++ b/gitlab-pages/docs/language-basics/loops.md @@ -64,7 +64,8 @@ let aux = (i: int): (bool, int) => stop(i); }; -let counter_simple = (n: int): int => Loop.fold_while(aux, n);``` +let counter_simple = (n: int): int => Loop.fold_while(aux, n); +```