-
Notifications
You must be signed in to change notification settings - Fork 2
/
Maybe.fm
56 lines (44 loc) · 1.05 KB
/
Maybe.fm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Maybe.fm
// ========
//
// Optional values. A value of type `Maybe(A)` can either be
// a value of type `A`, or nothing at all. Maybe captures
// the idea of "nullable" values, and functions that can
// fail to produce a result.
import Bool
// Definition
// ----------
T Maybe{A}
| none
| some(value : A)
// Operations
// ----------
maybe(A; B; b : B, f : A -> B, mb : Maybe(A)) : B
case mb
| none => b
| some => f(mb.value)
is_some(A; mb : Maybe(A)) : Bool
case mb
| none => false
| some => true
is_none(A; mb : Maybe(A)) : Bool
case mb
| none => true
| some => false
with_default(A; x : A, mb : Maybe(A)) : A
maybe(__ x, (x) => x, mb)
map_maybe(A; B; f : A -> B, mb : Maybe(A)) : Maybe(B)
case mb
| none => none(_)
| some => some(_ f(mb.value))
apply_maybe(A; B; mf: Maybe(A -> B), mb : Maybe(A))
: Maybe(B)
case mf
| none => none(_)
| some => map_maybe(A; B; mf.value, mb)
pure_maybe(A;): A -> Maybe(A)
some(A;)
bind_maybe(A; B; f: A -> Maybe(B), mb: Maybe(A)) : Maybe(B)
case mb
| none => none(_)
| some => f(mb.value)