/
option.glu
166 lines (144 loc) · 3.58 KB
/
option.glu
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//@NO-IMPLICIT-PRELUDE
//! A type that can represent the abscence of a value
let { error } = import! std.prim
let { Semigroup } = import! std.semigroup
let { Monoid } = import! std.monoid
let { Eq, Ord, Ordering } = import! std.cmp
let { Functor } = import! std.functor
let { Monad } = import! std.monad
let { Traversable } = import! std.traversable
let { Applicative } = import! std.applicative
let { Alternative } = import! std.alternative
let { Show } = import! std.show
let { Bool } = import! std.bool
let { Option } = import! std.types
let string @ { ? } = import! std.string
let { Foldable } = import! std.foldable
let { (<>) } = import! std.semigroup
let unwrap opt : Option a -> a =
match opt with
| Some x -> x
| None -> error "Option was None"
let former =
let semigroup : Semigroup (Option a) = {
append = \l r ->
match l with
| Some x -> Some x
| None -> r,
}
let monoid : Monoid (Option a) = {
semigroup = semigroup,
empty = None,
}
{ semigroup, monoid }
let latter =
let semigroup : Semigroup (Option a) = {
append = \l r ->
match r with
| Some x -> Some x
| None -> l,
}
let monoid : Monoid (Option a) = {
semigroup = semigroup,
empty = None,
}
{ semigroup, monoid }
let semigroup a : Semigroup a -> Semigroup (Option a) = {
append = \l r ->
match (l, r) with
| (Some x, Some y) -> Some (a.append x y)
| (Some _, None) -> l
| (None, Some _) -> r
| (None, None) -> None,
}
let monoid a : Semigroup a -> Monoid (Option a) = {
semigroup = semigroup a,
empty = None,
}
let eq ?a : [Eq a] -> Eq (Option a) = {
(==) = \l r ->
match (l, r) with
| (Some l_val, Some r_val) -> a.(==) l_val r_val
| (None, None) -> True
| _ -> False,
}
let ord ?a : [Ord a] -> Ord (Option a) =
{
eq = eq,
compare = \l r ->
match (l, r) with
| (Some l_val, Some r_val) -> a.compare l_val r_val
| (None, Some _) -> LT
| (Some _, None) -> GT
| (None, None) -> EQ,
}
let functor : Functor Option = {
map = \f x ->
match x with
| Some y -> Some (f y)
| None -> None,
}
let applicative : Applicative Option = {
functor = functor,
apply = \f x ->
match (f, x) with
| (Some g, Some y) -> Some (g y)
| _ -> None,
wrap = \x -> Some x,
}
let alternative : Alternative Option = {
applicative = applicative,
or = \x y ->
match x with
| Some _ -> x
| None -> y,
empty = None,
}
let monad : Monad Option = {
applicative = applicative,
flat_map = \f m ->
match m with
| Some x -> f x
| None -> None,
}
let show ?d : [Show a] -> Show (Option a) =
let show o =
match o with
| Some x -> "Some (" <> d.show x <> ")"
| None -> "None"
{ show }
let foldable : Foldable Option = {
foldr = \f z o ->
match o with
| None -> z
| Some x -> f x z,
foldl = \f z o ->
match o with
| None -> z
| Some x -> f z x,
}
let traversable : Traversable Option = {
functor = functor,
foldable = foldable,
traverse = \app f o ->
match o with
| None -> app.wrap None
| Some x -> app.functor.map Some (f x),
}
{
Option,
unwrap,
semigroup,
monoid,
former,
latter,
eq,
ord,
functor,
applicative,
alternative,
monad,
show,
foldable,
traversable,
}