This repository was archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlibrary.lisp
255 lines (211 loc) · 6.97 KB
/
library.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
;;; library.lisp
(cl:in-package #:coalton-user)
(coalton-toplevel
;; Defined in early-types.lisp
#+ignore
(define-type Unit
Unit)
#+ignore
(define-type Void)
(define-type coalton:Boolean
coalton:True
coalton:False)
(define-type (Maybe t)
Nothing
(Just t))
(define-type (Either a b)
(Left a)
(Right b))
(define-type (Liszt t)
Knil
(Kons t (Liszt t)))
(define-type (Binary-Tree s t)
(Leaf s)
(Branch t (Binary-Tree s t) (Binary-Tree s t))))
(cl:defmacro Liszt (cl:&rest list-elements)
(cl:reduce (cl:lambda (x acc)
`(Kons ,x ,acc))
list-elements
:from-end cl:t
:initial-value 'Knil))
(cl:defmacro coalton:if (expr then else)
`(match ,expr
(True ,then)
(False ,else)))
(cl:defmacro coalton:cond ((clause-a then-a) cl:&rest clauses)
(cl:if (cl:not (cl:endp clauses))
`(if ,clause-a
,then-a
(cond ,@clauses))
(cl:progn
(cl:assert (cl:eq 'else clause-a) () "COALTON:COND must have an ELSE clause.")
then-a)))
(cl:declaim (cl:inline lisp-boolean-to-coalton-boolean))
(cl:defun lisp-boolean-to-coalton-boolean (x)
(cl:if x True False))
;;; Erroring
(coalton-toplevel
(declare error (fn String -> t))
(define (error str)
(lisp t (cl:error "~A" str))))
;;; Combinators
(coalton-toplevel
(define (ignore x) Unit)
(define (identity x) x)
(define (constantly x) (fn (y) x))
(define (flip f) (fn (x y) (f y x)))
(define (compose f g) (fn (x) (f (g x))))
(define (curry f) (fn (x) (fn (y) (f x y)))))
;;; Boolean
(coalton-toplevel
(define (not x) (if x False True))
(define (binary-and x y) (if x y False))
(define (binary-or x y) (if x True y))
(define (complement f) (compose not f)))
(cl:defmacro and (x cl:&rest xs)
(cl:reduce (cl:lambda (x acc)
`(binary-and ,x ,acc))
(cl:cons x xs)
:from-end cl:t))
(cl:defmacro or (x cl:&rest xs)
(cl:reduce (cl:lambda (x acc)
`(binary-or ,x ,acc))
(cl:cons x xs)
:from-end cl:t))
;;; Strings
(coalton-toplevel
(declare concat (fn String String -> String))
(define (concat a b) (lisp String
(cl:concatenate 'cl:string a b)))
(declare string-length (fn String -> Integer))
(define (string-length s) (lisp Integer (cl:length s))))
;;; Comparators and Predicates
(cl:macrolet ((define-comparators (cl:&rest names)
`(coalton-toplevel
,@(cl:loop
:for op :in names
:for clop := (cl:intern (cl:symbol-name op) :cl)
:append (cl:list
`(declare ,op (fn Integer Integer -> Boolean))
`(define (,op x y) (lisp Boolean
(lisp-boolean-to-coalton-boolean
(,clop x y))))))))
(define-predicates (cl:&rest names)
`(coalton-toplevel
,@(cl:loop
:for op :in names
:for clop := (cl:intern (cl:symbol-name op) :cl)
:append (cl:list
`(declare ,op (fn Integer -> Boolean))
`(define (,op x) (lisp Boolean
(lisp-boolean-to-coalton-boolean
(,clop x)))))))))
(define-comparators = /= > < >= <=)
(define-predicates evenp oddp plusp minusp zerop))
;;; Arithmetic
(coalton-toplevel
(declare + (fn Integer Integer -> Integer))
(define (+ x y) (lisp Integer (cl:+ x y)))
(define (1+ x) (+ 1 x))
(declare - (fn Integer Integer -> Integer))
(define (- x y) (lisp Integer (cl:- x y)))
(define (1- x) (- x 1))
(define (~ x) (- 0 x)) ; ML-ism
(declare * (fn Integer Integer -> Integer))
(define (* x y) (lisp Integer (cl:* x y)))
(define (double n) (* n 2))
(declare / (fn Integer Integer -> Integer))
(define (/ x y) (lisp Integer (cl:values (cl:floor x y))))
(define (half n) (/ n 2))
(define (safe-/ x y) (if (zerop y)
Nothing
(Just (/ x y)))))
;;; Mutable Cells
(coalton-toplevel
(define-type (Ref t)
(Ref t))
(declare mutate-cell (fn (Ref t) t -> Unit))
(define (mutate-cell r v)
(lisp Unit
(cl:progn
(cl:setf (cl:svref (cl:slot-value r 'coalton-impl::value) 0) v)
Unit))))
(coalton-toplevel
(define (gcd u v)
(cond ((= u v) u)
((zerop u) v)
((zerop v) u)
((evenp u) (if (evenp v)
(double (gcd (half u) (half v)))
(gcd (half u) v)))
(else (cond ((evenp v) (gcd u (half v)))
((> u v) (gcd (half (- u v)) v))
(else (gcd (half (- v u)) u)))))))
;;; Random examples
(coalton-toplevel
(define (fact1 n)
(letrec ((%fact (fn (n answer)
(if (zerop n)
answer
(%fact (1- n) (* n answer))))))
(%fact n 1)))
(define (car x)
(match x
(Knil (error "Can't take CAR of KNIL"))
((Kons a b) a)))
(define (cdr x)
(match x
(Knil Knil)
((Kons a b) b)))
;; (define (length l) (fold (fn (x acc) (1+ acc)) 0 l))
(define (length l)
(letrec ((%length
(fn (l n)
(match l
(Knil n)
((Kons a b) (%length b (1+ n)))))))
(%length l 0)))
(define (map f x)
(match x
(Knil Knil)
((Kons x xs) (Kons (f x) (map f xs)))))
(define (mapper f) (fn x (map f x)))
(define (fold f init l)
(match l
(Knil init)
((Kons x xs) (fold f (f x init) xs))))
(define (tabulate f n)
(letrec ((%tabulate
(fn (n l)
(if (zerop n)
l
(%tabulate (1- n)
(Kons (f (1- n)) l))))))
(%tabulate n Knil)))
(define (range a b)
(cond
((>= a b) Knil)
(else
(tabulate ((curry +) a) (- b a))))))
(coalton-toplevel
(define (reverse l) (fold Kons Knil l))
(define (sum l) (fold + 0 l))
(define (product l) (fold * 1 l))
(define (keep-if f l) ; AKA filter
(fold (fn (x acc)
(if (f x)
(Kons x acc)
acc))
Knil
l))
(define (remove-if f l) (keep-if (complement f) l))
(define (replicate x n) (tabulate (constantly x) n))
(define (iota n) (tabulate identity n))
(define (range a b) (tabulate ((curry +) a) (- b a)))
(define (fact2 n) (product (range 1 (1+ n)))))
;; Grab Bag
(coalton-toplevel
(declare integer-name (fn Integer -> String))
(define (integer-name n)
(lisp String
(cl:format cl:nil "~R" n))))