From 518c743172a9da4e30a14774703b1250c945f9c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20=C5=A0ima?= Date: Wed, 29 Jan 2020 16:15:03 +0000 Subject: [PATCH] LIGO-258 Docs updates --- .../docs/advanced/entrypoints-contracts.md | 2 +- .../docs/language-basics/boolean-if-else.md | 15 +--------- .../docs/language-basics/math-numbers-tez.md | 12 ++++++++ .../src/boolean-if-else/if-else.ligo | 4 +++ .../src/math-numbers-tez/isnat.ligo | 1 + .../language-basics/src/types/annotation.ligo | 4 +++ gitlab-pages/docs/language-basics/types.md | 19 +++++++++++- gitlab-pages/website/package-lock.json | 30 ++++--------------- 8 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 gitlab-pages/docs/language-basics/src/boolean-if-else/if-else.ligo create mode 100644 gitlab-pages/docs/language-basics/src/math-numbers-tez/isnat.ligo create mode 100644 gitlab-pages/docs/language-basics/src/types/annotation.ligo diff --git a/gitlab-pages/docs/advanced/entrypoints-contracts.md b/gitlab-pages/docs/advanced/entrypoints-contracts.md index 752e553f3..dff0a535a 100644 --- a/gitlab-pages/docs/advanced/entrypoints-contracts.md +++ b/gitlab-pages/docs/advanced/entrypoints-contracts.md @@ -220,7 +220,7 @@ In our case, we have a `counter.ligo` contract that accepts a parameter of type -```pascaligo +```pascaligo skip // counter.ligo type action is | Increment of int diff --git a/gitlab-pages/docs/language-basics/boolean-if-else.md b/gitlab-pages/docs/language-basics/boolean-if-else.md index d57e6fa99..07cb40d04 100644 --- a/gitlab-pages/docs/language-basics/boolean-if-else.md +++ b/gitlab-pages/docs/language-basics/boolean-if-else.md @@ -139,21 +139,8 @@ Conditional logic is an important part of every real world program. ```pascaligo group=e const min_age: nat = 16n; -(* - This function is really obnoxious, but it showcases - how the if statement and it's syntax can be used. - - Normally, you'd use `with (age > min_age)` instead. -*) function is_adult(const age: nat): bool is - block { - var is_adult: bool := False; - if (age > min_age) then begin - is_adult := True; - end else begin - is_adult := False; - end - } with is_adult + if (age > min_age) then True else False ``` > You can run the function above with diff --git a/gitlab-pages/docs/language-basics/math-numbers-tez.md b/gitlab-pages/docs/language-basics/math-numbers-tez.md index 1609679e8..91a4b923a 100644 --- a/gitlab-pages/docs/language-basics/math-numbers-tez.md +++ b/gitlab-pages/docs/language-basics/math-numbers-tez.md @@ -200,6 +200,18 @@ const a: int = int(1n); const b: nat = abs(1); ``` + + +## Check if a value is a `nat` + +You can check if a value is a `nat`, by using a syntax specific built-in function, which accepts an `int` and returns an `option(nat)`, more specifically `Some(nat)` if the provided integer is a natural number, and `None` otherwise: + + + +```pascaligo +const its_a_nat: option(nat) = is_nat(1) +``` + ```reasonligo group=e let a: int = int(1n); diff --git a/gitlab-pages/docs/language-basics/src/boolean-if-else/if-else.ligo b/gitlab-pages/docs/language-basics/src/boolean-if-else/if-else.ligo new file mode 100644 index 000000000..cc2b84529 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/boolean-if-else/if-else.ligo @@ -0,0 +1,4 @@ +const min_age: nat = 16n; + +function is_adult(const age: nat): bool is + if (age > min_age) then True else False \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/math-numbers-tez/isnat.ligo b/gitlab-pages/docs/language-basics/src/math-numbers-tez/isnat.ligo new file mode 100644 index 000000000..5a89add1a --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/math-numbers-tez/isnat.ligo @@ -0,0 +1 @@ +const its_a_nat: option(nat) = is_nat(1) \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/types/annotation.ligo b/gitlab-pages/docs/language-basics/src/types/annotation.ligo new file mode 100644 index 000000000..9373e08ec --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/types/annotation.ligo @@ -0,0 +1,4 @@ +type int_map is map(int, int); +function get_first(const int_map: int_map): option(int) is int_map[1] +// empty map needs a type annotation +const first: option(int) = get_first(((map end) : int_map )); \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/types.md b/gitlab-pages/docs/language-basics/types.md index a2d456473..b19004a2b 100644 --- a/gitlab-pages/docs/language-basics/types.md +++ b/gitlab-pages/docs/language-basics/types.md @@ -3,7 +3,7 @@ id: types title: Types --- -LIGO is strongly and statically typed. This means that the compiler checks your program at compilation time and makes sure there won't be any type related runtime errors. LIGO types are built on top of Michelson's type system. +LIGO is strongly and statically typed. This means that the compiler checks your program at compilation time and makes sure there won't be any type related runtime errors. LIGO types are built on top of Michelson's type system. ## Built-in types @@ -36,6 +36,8 @@ let dog_breed: animal_breed = "Saluki"; +> Types in LIGO are `structural`, which means that `animalBreed`/`animal_breed` and `string` are interchangable and are considered equal. + ## Simple types @@ -146,3 +148,18 @@ let ledger: account_balances = ``` + +## Annotations + +In certain cases, type of an expression cannot be properly determined. This can be circumvented by annotating an expression with it's desired type, here's an example: + + + +```pascaligo +type int_map is map(int, int); +function get_first(const int_map: int_map): option(int) is int_map[1] +// empty map needs a type annotation +const first: option(int) = get_first(((map end) : int_map )); +``` + + \ No newline at end of file diff --git a/gitlab-pages/website/package-lock.json b/gitlab-pages/website/package-lock.json index aedc47552..27e5e7983 100644 --- a/gitlab-pages/website/package-lock.json +++ b/gitlab-pages/website/package-lock.json @@ -5624,15 +5624,6 @@ "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==", "dev": true }, - "klaw-sync": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/klaw-sync/-/klaw-sync-6.0.0.tgz", - "integrity": "sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==", - "dev": true, - "requires": { - "graceful-fs": "^4.1.11" - } - }, "lazy-cache": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/lazy-cache/-/lazy-cache-2.0.2.tgz", @@ -7412,15 +7403,6 @@ "integrity": "sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw=", "dev": true }, - "preprocess": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/preprocess/-/preprocess-3.1.0.tgz", - "integrity": "sha1-pE5c3Vu7WlTwrSiaru2AmV19k4o=", - "dev": true, - "requires": { - "xregexp": "3.1.0" - } - }, "prismjs": { "version": "1.17.1", "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.17.1.tgz", @@ -7750,6 +7732,12 @@ "picomatch": "^2.0.4" } }, + "reason-highlightjs": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/reason-highlightjs/-/reason-highlightjs-0.2.1.tgz", + "integrity": "sha512-DWWPtfeQjwKgHj2OOieEIAB544uAVjwOAIAg2Yu09CobdUe41Yah0Z67GEvmVtpYCGG/+3CZvDRM1hMVr1zN3A==", + "dev": true + }, "rechoir": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", @@ -9433,12 +9421,6 @@ "integrity": "sha512-Eux0i2QdDYKbdbA6AM6xE4m6ZTZr4G4xF9kahI2ukSEMCzwce2eX9WlTI5J3s+NU7hpasFsr8hWIONae7LluAQ==", "dev": true }, - "xregexp": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/xregexp/-/xregexp-3.1.0.tgz", - "integrity": "sha1-FNhGHgvdOCJL/uUDmgiY/EL80zY=", - "dev": true - }, "xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",