-
Notifications
You must be signed in to change notification settings - Fork 1
/
std.tl
328 lines (262 loc) · 7.23 KB
/
std.tl
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
; Unprefixing
(tl-define define tl-define)
(define #t tl-#t)
(define #f tl-#f)
(define error tl-error)
(define lambda tl-lambda)
(define macro tl-macro)
(define apply tl-apply)
(define set! tl-set!)
(define display tl-display)
(define if tl-if)
(define + tl-+)
(define - tl--)
(define * tl-*)
(define / tl-/)
(define % tl-%)
(define = tl-=)
(define < tl-<)
(define nand tl-nand)
; TODO: replace these with clever usages of formal specifiers
(define car tl-car)
(define cdr tl-cdr)
(define cons tl-cons)
(define type tl-type)
(define null? tl-null?)
;(define apply tl-apply)
(define call-with-current-continuation tl-call-with-current-continuation)
(define call/cc call-with-current-continuation)
(define read tl-read)
; Parser init
(tl-prefix "'" quote)
(define quote
(macro (lit) environ lit))
(tl-prefix "~" tl-negate)
(define tl-negate (lambda (x) (- 0 x)))
; Continuation compatibility
(define tl-eval-in
(lambda (env ex)
(call/cc (lambda (ret) (tl-eval-in& env ex ret)))))
(define current-continuation
(macro () env (tl-eval-in env '((lambda () (call/cc (lambda (cc) cc)))))))
; Essential combinatorics
(define map
(lambda (f l)
(if (null? l)
'()
(cons (f (car l)) (map f (cdr l))))))
(define filter
(lambda (f l)
(if (null? l)
'()
(if (f (car l))
(cons (car l) (filter f (cdr l)))
(filter f (cdr l))))))
(define for-each
(lambda (f l)
(if (null? l)
#t
(begin
(f (car l))
(for-each f (cdr l))))))
(define foldr
(lambda (f v l)
(if (null? l)
v
(foldr f (f v (car l)) (cdr l)))))
(define foldl
(lambda (f v l)
(if (null? l)
v
(f (car l) (foldl f v (cdr l))))))
; Mathematics and Logic
(define not
(lambda (a)
(nand a a)))
(define and
(lambda (a b)
(not (nand a b))))
(define or
(lambda (a b)
(nand (not a) (not b))))
(define <=
(lambda (a b)
(or
(< a b)
(= a b))))
(define >
(lambda (a b)
(not (<= a b))))
(define >=
(lambda (a b)
(or
(> a b)
(= a b))))
(define max
(lambda (a b)
(if (> a b)
a
b)))
(define min
(lambda (a b)
(if (< a b)
a
b)))
; Core language
(define pair?
(lambda (v)
(= (type v) 'pair)))
(define procedure?
(lambda (v)
(or (= (type v) 'func) (= (type v) 'macro))))
(define append
(lambda (pref suff)
(if (null? pref) suff
(if (null? (cdr pref))
(cons (car pref) suff)
(cons (car pref) (append (cdr pref) suff))))))
(define reverse
(lambda (l)
(define res '())
(for-each (lambda (e) (set! res (cons e res))) l)
res))
(tl-prefix "," unquote)
(tl-prefix "@" unquote-splicing)
(tl-prefix "`" quasiquote)
(define tl-quasiquote
(lambda (l env)
(if (null? l)
'()
(if (pair? (car l))
(if (= (caar l) 'unquote)
(cons (tl-eval-in env (cadar l)) (tl-quasiquote (cdr l) env))
(if (= (caar l) 'unquote-splicing)
(append (tl-eval-in env (cadar l)) (tl-quasiquote (cdr l) env))
(cons (tl-quasiquote (car l) env) (tl-quasiquote (cdr l) env))))
(cons (car l) (tl-quasiquote (cdr l) env))))))
(define quasiquote
(macro (ex) environ
(tl-quasiquote ex environ)))
; Silly aliases
(define eval
(macro (expr) environ
(tl-eval-in& environ expr (lambda (ex) (tl-eval-in environ ex)))))
(define tl-permute-inner
(lambda (choices f pfx)
(if (null? choices)
(f pfx)
(for-each
(lambda (e) (tl-permute-inner (cdr choices) f (cons e pfx)))
(car choices)))))
(define tl-permute (lambda (choices f) (tl-permute-inner choices f '())))
(define tl-path-get
(lambda (l path)
(cond
((null? path) l)
((= (car path) 'a) (tl-path-get (car l) (cdr path)))
((= (car path) 'd) (tl-path-get (cdr l) (cdr path)))
((= (car path) 'i) (tl-path-get l (cdr path))))))
(define tl-make-path
(lambda (var path)
(cond
((null? path) var)
((= (car path) 'a) (cons 'car (cons (tl-make-path var (cdr path)) '())))
((= (car path) 'd) (cons 'cdr (cons (tl-make-path var (cdr path)) '())))
((= (car path) 'i) (tl-make-path var (cdr path))))))
(define tl-make-cxr
(lambda (perm)
(display 'tl-make-cxr perm)
(let* (
(conc (apply tl-concat (filter (lambda (x) (not (= x 'i))) perm)))
(sym (tl-concat 'c conc 'r))
)
(eval `(set! ,sym (lambda (l) ,(tl-make-path 'l perm)))))))
(define tl-if-defined
(macro (var iftrue iffalse) env
(if (= (tl-eval-in env `(tl-rescue (lambda () ,var #t))) #t)
(tl-eval-in env iftrue)
(tl-eval-in env iffalse))))
; Language essentials
(define list
(lambda l l))
(define begin
(macro body environ
(tl-eval-in environ `((lambda () @body)))))
(define cond
(macro clauses environ
(if (tl-eval-in environ (caar clauses))
(tl-eval-in environ (cons 'begin (cdar clauses)))
(tl-eval-in environ (cons 'cond (cdr clauses))))))
(define else #t)
(define let
(macro (bindings . body) environ
(tl-eval-in environ `((lambda ,(map car bindings) @body) @(map cadr bindings)))))
(define let*
(macro (bindings . body) environ
(tl-eval-in environ (if (null? bindings)
`(begin @body)
`(let (,(car bindings)) (let* ,(cdr bindings) @body))))))
; Need these four for bootstrapping
(define caar (lambda (l) (car (car l))))
(define cdar (lambda (l) (cdr (car l))))
(define cadr (lambda (l) (car (cdr l))))
(define cadar (lambda (l) (car (cdr (car l)))))
(tl-if-defined no-cxr
#t
(tl-permute '((a d) (a d) (a d i) (a d i)) tl-make-cxr))
; List manipulation
(define length
(lambda (l)
(foldr + 0 (map (lambda (x) 1) l))))
(define item
(lambda (l i)
(cond
((< i 0) '())
((= i 0) (car l))
((null? l) '())
(else (item (cdr l) (- i 1))))))
; Useful reducers
(define any
(lambda (f l)
(foldr or #f (map f l))))
(define all
(lambda (f l)
(foldr and #t (map f l))))
(define id
(lambda (x) x))
; Associative list manipulation
(define assocp
(lambda (pred a)
(cond
((null? a) #f)
((pred (caar a)) (car a))
(else (assocp pred (cdr a))))))
(define del-assocp
(lambda (pred a)
(cond
((null? a) '())
((pred (caar a)) (del-assocp pred (cdr a)))
(else (cons (car a) (del-assocp pred (cdr a)))))))
(define assoc
(lambda (k a)
(assocp (lambda (x) (= k x)) a)))
(define del-assoc
(lambda (k a)
(del-assocp (lambda (x) (= k x)) a)))
; loop/recur
(define tl-loop-co-sym '#tl-continue)
(define tl-loop-br-sym '#tl-break)
(define loop
(macro (bindings . body) env
(tl-eval-in env `(call/cc (lambda (,tl-loop-br-sym)
(apply (lambda ,(cons tl-loop-co-sym (map car bindings))
@body
)
(call/cc (lambda (co) (cons co (quote ,(map cadr bindings)))))))))))
(define recur
(macro args env
(tl-eval-in env
`((eval ,tl-loop-co-sym) (list (eval ,tl-loop-co-sym) @args)))))
(define break
(macro (value) env
(tl-eval-in env `((eval ,tl-loop-br-sym) ,value))))