2019-11-08 03:19:27 +04:00
|
|
|
---
|
|
|
|
id: unit-option-pattern-matching
|
|
|
|
title: Unit, Option, Pattern matching
|
|
|
|
---
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
import Syntax from '@theme/Syntax';
|
|
|
|
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Optionals are a pervasive programing pattern in OCaml. Since Michelson
|
|
|
|
and LIGO are both inspired by OCaml, *optional types* are available in
|
|
|
|
LIGO as well. Similarly, OCaml features a *unit* type, and LIGO
|
|
|
|
features it as well. Both the option type and the unit types are
|
|
|
|
instances of a more general kind of types: *variant types* (sometimes
|
|
|
|
called *sum types*).
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-10 22:07:20 +04:00
|
|
|
## The unit Type
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The `unit` type in Michelson or LIGO is a predefined type that
|
|
|
|
contains only one value that carries no information. It is used when
|
|
|
|
no relevant information is required or produced. Here is how it used.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
2020-02-05 19:28:40 +04:00
|
|
|
|
|
|
|
In PascaLIGO, the unique value of the `unit` type is `Unit`.
|
|
|
|
```pascaligo group=a
|
2020-02-10 22:07:20 +04:00
|
|
|
const n : unit = Unit // Note the capital letter
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
2020-02-05 19:28:40 +04:00
|
|
|
|
|
|
|
In CameLIGO, the unique value of the `unit` type is `()`, following
|
|
|
|
the OCaml convention.
|
|
|
|
```cameligo group=a
|
|
|
|
let n : unit = ()
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
2020-02-05 19:28:40 +04:00
|
|
|
|
|
|
|
In ReasonLIGO, the unique value of the `unit` type is `()`, following
|
|
|
|
the OCaml convention.
|
|
|
|
```reasonligo group=a
|
|
|
|
let n : unit = ();
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
## Variant types
|
|
|
|
|
|
|
|
A variant type is a user-defined or a built-in type (in case of
|
|
|
|
options) that defines a type by cases, so a value of a variant type is
|
|
|
|
either this, or that or... The simplest variant type is equivalent to
|
|
|
|
the enumerated types found in Java, C++, JavaScript etc.
|
|
|
|
|
|
|
|
Here is how we define a coin as being either head or tail (and nothing
|
|
|
|
else):
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```pascaligo group=b
|
|
|
|
type coin is Head | Tail
|
2020-02-25 21:07:53 +04:00
|
|
|
const head : coin = Head
|
|
|
|
const tail : coin = Tail
|
2020-02-05 19:28:40 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```cameligo group=b
|
|
|
|
type coin = Head | Tail
|
|
|
|
let head : coin = Head
|
|
|
|
let tail : coin = Tail
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```reasonligo group=b
|
2020-02-25 21:07:53 +04:00
|
|
|
type coin = Head | Tail;
|
2020-02-05 19:28:40 +04:00
|
|
|
let head : coin = Head;
|
|
|
|
let tail : coin = Tail;
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The names `Head` and `Tail` in the definition of the type `coin` are
|
2020-02-25 21:07:53 +04:00
|
|
|
called *data constructors*, or *variants*. In this particular, they
|
|
|
|
carry no information beyond their names, so they are called *constant
|
|
|
|
constructors*.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
In general, it is interesting for variants to carry some information,
|
|
|
|
and thus go beyond enumerated types. In the following, we show how to
|
|
|
|
define different kinds of users of a system.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```pascaligo group=c
|
2019-11-08 03:19:27 +04:00
|
|
|
type id is nat
|
2020-02-05 19:28:40 +04:00
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
type user is
|
2020-02-05 19:28:40 +04:00
|
|
|
Admin of id
|
2019-11-08 03:19:27 +04:00
|
|
|
| Manager of id
|
2020-02-05 19:28:40 +04:00
|
|
|
| Guest
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
const u : user = Admin (1000n)
|
2020-02-25 21:07:53 +04:00
|
|
|
const g : user = Guest
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```cameligo group=c
|
2019-11-08 03:19:27 +04:00
|
|
|
type id = nat
|
2020-02-05 19:28:40 +04:00
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
type user =
|
2020-02-05 19:28:40 +04:00
|
|
|
Admin of id
|
2019-11-08 03:19:27 +04:00
|
|
|
| Manager of id
|
2020-02-05 19:28:40 +04:00
|
|
|
| Guest
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
let u : user = Admin 1000n
|
|
|
|
let g : user = Guest
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```reasonligo group=c
|
2019-12-10 17:47:31 +04:00
|
|
|
type id = nat;
|
2020-02-05 19:28:40 +04:00
|
|
|
|
2019-12-10 17:47:31 +04:00
|
|
|
type user =
|
2020-02-05 19:28:40 +04:00
|
|
|
| Admin (id)
|
|
|
|
| Manager (id)
|
|
|
|
| Guest;
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
let u : user = Admin (1000n);
|
|
|
|
let g : user = Guest;
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
In LIGO, a constant constructor is equivalent to the same constructor
|
|
|
|
taking an argument of type `unit`, so, for example, `Guest` is the
|
|
|
|
same value as `Guest (unit)`.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
## Optional values
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The `option` type is a predefined variant type that is used to express
|
|
|
|
whether there is a value of some type or none. This is especially
|
|
|
|
useful when calling a *partial function*, that is, a function that is
|
|
|
|
not defined for some inputs. In that case, the value of the `option`
|
|
|
|
type would be `None`, otherwise `Some (v)`, where `v` is some
|
|
|
|
meaningful value *of any type*. An example in arithmetic is the
|
|
|
|
division operation:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```pascaligo group=d
|
|
|
|
function div (const a : nat; const b : nat) : option (nat) is
|
|
|
|
if b = 0n then (None: option (nat)) else Some (a/b)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```cameligo group=d
|
|
|
|
let div (a, b : nat * nat) : nat option =
|
|
|
|
if b = 0n then (None: nat option) else Some (a/b)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```reasonligo group=d
|
|
|
|
let div = ((a, b) : (nat, nat)) : option (nat) =>
|
|
|
|
if (b == 0n) { (None: option (nat)); } else { Some (a/b); };
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
## Pattern matching
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
*Pattern matching* is similiar to the `switch` construct in
|
|
|
|
Javascript, and can be used to route the program's control flow based
|
|
|
|
on the value of a variant. Consider for example the definition of a
|
2020-02-10 22:07:20 +04:00
|
|
|
function `flip` that flips a coin.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```pascaligo group=e
|
|
|
|
type coin is Head | Tail
|
|
|
|
|
|
|
|
function flip (const c : coin) : coin is
|
|
|
|
case c of
|
2020-02-25 21:07:53 +04:00
|
|
|
Head -> Tail
|
|
|
|
| Tail -> Head
|
2020-02-05 19:28:40 +04:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
You can call the function `flip` by using the LIGO compiler like so:
|
|
|
|
```shell
|
|
|
|
ligo run-function
|
|
|
|
gitlab-pages/docs/language-basics/src/unit-option-pattern-matching/flip.ligo
|
2020-02-25 21:07:53 +04:00
|
|
|
flip "Head"
|
2020-02-05 19:28:40 +04:00
|
|
|
# Outputs: Tail(Unit)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```cameligo group=e
|
|
|
|
type coin = Head | Tail
|
|
|
|
|
|
|
|
let flip (c : coin) : coin =
|
|
|
|
match c with
|
|
|
|
Head -> Tail
|
|
|
|
| Tail -> Head
|
|
|
|
```
|
|
|
|
|
|
|
|
You can call the function `flip` by using the LIGO compiler like so:
|
|
|
|
```shell
|
|
|
|
ligo run-function
|
|
|
|
gitlab-pages/docs/language-basics/src/unit-option-pattern-matching/flip.mligo
|
|
|
|
flip Head
|
|
|
|
# Outputs: Tail(Unit)
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```reasonligo group=e
|
|
|
|
type coin = | Head | Tail;
|
|
|
|
|
|
|
|
let flip = (c : coin) : coin =>
|
|
|
|
switch (c) {
|
|
|
|
| Head => Tail
|
|
|
|
| Tail => Head
|
2019-12-10 17:47:31 +04:00
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can call the function `flip` by using the LIGO compiler like so:
|
|
|
|
```shell
|
|
|
|
ligo run-function
|
|
|
|
gitlab-pages/docs/language-basics/src/unit-option-pattern-matching/flip.religo
|
|
|
|
flip Head
|
|
|
|
# Outputs: Tail(Unit)
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|