forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optional.lisp
159 lines (135 loc) · 3.88 KB
/
optional.lisp
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
(coalton-library/utils:defstdlib-package #:coalton-library/optional
(:use
#:coalton
#:coalton-library/builtin
#:coalton-library/classes)
(:local-nicknames
(#:cell #:coalton-library/cell)
(#:iter #:coalton-library/iterator))
(:export
#:from-some
#:some?
#:none?))
(in-package #:coalton-library/optional)
(named-readtables:in-readtable coalton:coalton)
#+coalton-release
(cl:declaim #.coalton-impl/settings:*coalton-optimize-library*)
(coalton-toplevel
;;
;; Optional
;;
(declare from-some (String -> (Optional :a) -> :a))
(define (from-some str opt)
"Get the value of OPT, erroring with the provided string if it is None."
(match opt
((Some x) x)
((None) (error str))))
(declare some? ((Optional :a) -> Boolean))
(define (some? x)
"Is X Some?"
(match x
((Some _) True)
((None) False)))
(declare none? ((Optional :a) -> Boolean))
(define (none? x)
"Is X None?"
(match x
((None) True)
((Some _) False)))
;;
;; Instances
;;
(define-instance (Eq :a => Eq (Optional :a))
(define (== x y)
(match (Tuple x y)
((Tuple (Some x) (Some y)) (== x y))
((Tuple (None) (None)) True)
(_ False))))
(define-instance (Ord :a => Ord (Optional :a))
(define (<=> x y)
(match (Tuple x y)
((Tuple (Some x) (Some y))
(<=> x y))
((Tuple (Some _) (None))
GT)
((Tuple (None) (Some _))
LT)
((Tuple (None) (None))
Eq))))
(define-instance (Num :a => Num (Optional :a))
(define (+ a b) (liftA2 + a b))
(define (- a b) (liftA2 - a b))
(define (* a b) (liftA2 * a b))
(define (fromInt x) (pure (fromInt x))))
(define-instance (Semigroup :a => Semigroup (Optional :a))
(define (<> a b)
(match (Tuple a b)
((Tuple (Some a) (Some b)) (Some (<> a b)))
(_ None))))
(define-instance (Monoid :a => Monoid (Optional :a))
(define mempty (Some mempty)))
(define-instance (Functor Optional)
(define (map f x)
(match x
((Some a) (Some (f a)))
((None) None))))
(define-instance (Applicative Optional)
(define (pure x)
(Some x))
(define (liftA2 f x y)
(match (Tuple x y)
((Tuple (Some x) (Some y))
(Some (f x y)))
(_ None))))
(define-instance (Monad Optional)
(define (>>= x f)
(match x
((Some x) (f x))
((None) None))))
(define-instance (MonadFail Optional)
(define (fail _)
None))
(define-instance (Alternative Optional)
(define empty None)
(define (alt x y)
(match x
((Some _) x)
(_ y))))
(define-instance (Unwrappable Optional)
(define (unwrap-or-else succeed fail opt)
(match opt
((Some elt) (succeed elt))
((None) (fail)))))
(define-instance (iter:IntoIterator (Optional :a) :a)
(define (iter:into-iter opt)
(match opt
((Some x)
(let cell = (cell:new True))
(iter:with-size
(fn ()
(unless (cell:read cell)
(return None))
(cell:write! cell False)
opt)
1))
((None)
iter:empty))))
(define-instance (iter:FromIterator :container :elt => iter:FromIterator (Optional :container) (Optional :elt))
(define (iter:collect! iter)
(let error = (cell:new False))
(let out =
(iter:collect!
(iter:map-while! (fn (x)
(match x
((Some _) x)
((None)
(cell:write! error True)
None)))
iter)))
(if (cell:read error)
None
(Some out))))
(define-instance (Default (Optional :a))
(define (default) None)))
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/OPTIONAL")