#type zero;;
()
# ();; - : unit = ()
# type one = Unit;; - : type one = Unit
True | False
# type two = Left | Right - : type two = Left | Right
# type weekday = Monday | Tuesday | Wednesday | Thursday | Friday ;; - : type weekday = Monday | Tuesday | Wednesday | Thursday | Friday
5
as its cardinality is 5# let next_day = function |Monday -> Tuesday |Tuesday ->Wednesday |Wednesday -> Thursday |Thursday -> Friday |Friday -> Monday;; - : val next_day : weekday -> weekday = <fun>
# next_day Monday;; - : weekday = Tuesday
('a,'b)
or more generally tuples ('a, 'b , 'c)
-: (1,'a') : int * char -: (True, Monday) : bool * weekday
bool * weekday
= 2 * 7 = 14
# type 'a option = None | Some of 'a;;
a+1
where a is the number of inhabitants in type 'a
# type ('a, 'b)sum = Left of 'a | Right of 'b;;
a+b
'a -> 'b
have?
where 'a -> 'b
via f
and 'b -> 'a
via g
These two data types 'a
and 'b
are essentilly the same
is equivalent to
# let f = (function | Left () -> true | Right () -> false) # let g = (function | true -> Left () | false -> Right ())
Algebra | Proposition | Type | Constructor | Destructor (pattern) |
---|---|---|---|---|
empty |
None | language-dependent | ||
unit |
() |
() |
||
'a * 'b |
(a, b) |
match (a, b) with ... |
||
('a, 'b)sum |
Left a , Right b |
match ... with Left a -> ... | Right b -> ... |
||
'a->'b |
fun x -> ... |
function application |