-
Notifications
You must be signed in to change notification settings - Fork 5
/
lazier.scm
328 lines (286 loc) · 9.79 KB
/
lazier.scm
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
;;
;; Lazier, a "compiler" from lambda calculus to Lazy K.
;; Copyright 2002 Ben Rudiak-Gould. Distributed under the GPL.
;;
;; Usage examples:
;;
;; > (lazy-def '(myprog input) '(cdr (cdr input))) ; drops first two bytes of input
;; > (lazy-def 'myprog '(o cdr cdr)) ; equivalent definition
;;
;; > (laze 'myprog)
;; ((s ((s i) (k (k i)))) (k (k i)))
;;
;; > (print-as-cc (laze 'myprog))
;; S(SI(K(KI)))(K(KI))
;;
;; > (print-as-unlambda (laze 'myprog))
;; ``s``si`k`ki`k`ki
;;
;; > (print-as-iota (laze 'myprog))
;; ***i*i*i*ii***i*i*i*ii*ii**i*i*ii**i*i*ii*ii**i*i*ii**i*i*ii*ii
;;
;; > (print-as-jot (laze 'myprog))
;; 111111100011111110001111111110000011110011110011111111100000111100111100
;; 11111111100000
;;
;; > (lazy-def '(f x y z) '(x (y z))) ; \
;; > (lazy-def 'f '(lambda (x y z) '(x (y z)))) ; | equivalent
;; > (lazy-def 'f '(lambda (x) (lambda (y) ...))) ; /
;;
;; > (laze '(f arg1 arg2))
;; ((s (k arg1)) arg2)
;;
;; > (print-as-unlambda (laze '(f arg1 arg2)))
;; ``s`k[arg1][arg2]
;;
; lazy-def.
(define lazy-defs '())
(define (lazy-def name body)
(set! lazy-defs
(cons (if (pair? name)
(cons (car name)
(curry-lambda (cdr name) (curry-exp body)) )
(cons name (curry-exp body)) )
lazy-defs )))
(define (lazy-def-lookup name)
(assv name lazy-defs) )
; Currying.
(define (curry-exp expr)
(cond ((not (pair? expr)) expr)
((eq? (car expr) 'lambda)
(curry-lambda (cadr expr) (curry-exp (caddr expr))) )
(else
(curry-app (map curry-exp expr)) )))
(define (curry-lambda vars body)
(if (null? vars)
body
`(lambda (,(car vars)) ,(curry-lambda (cdr vars) body)) ))
(define (curry-app lst)
(let iter ((sofar (car lst))
(togo (cdr lst)) )
(if (null? togo)
sofar
(iter (list sofar (car togo)) (cdr togo)) )))
; Macro expansion.
(define (expr-dispatch expr leaf appl lamb)
(if (pair? expr)
(if (eq? (car expr) 'lambda)
(lamb (caadr expr) (caddr expr))
(appl (car expr) (cadr expr)) )
(leaf expr) ))
(define (expand-macros expr)
(let helper ((expr expr) (exclude '()) (stack '()))
(expr-dispatch expr
(lambda (leaf)
(cond ((memv leaf exclude) leaf)
((memv leaf stack)
(display "Recursion within lazy-defs detected: ")
(display (cons leaf stack))
(newline)
(error) )
(else
(let ((def (lazy-def-lookup leaf)))
(if def
(helper (cdr def) exclude (cons leaf stack))
leaf )))))
(lambda (f g)
(list (helper f exclude stack) (helper g exclude stack)) )
(lambda (var body)
`(lambda (,var) ,(helper body (cons var exclude) stack)) ))))
; Replace ((lambda (var) body) value) with body[value/var] if:
;
; - value is a symbol, or
; - var appears only once in body and value contains no
; more than one free variable which is not in body.
;
; I'm not sure if the first of these is ever needed -- it may
; always be handled by the other optimizations -- but it's easy
; to check for.
(define (apply-lambdas expr)
(let ((top-level-free-vars (free-vars expr)))
(let self ((expr expr))
(expr-dispatch expr
(lambda (leaf) leaf)
(lambda (f g)
(let ((f: (self f))
(g: (self g)) )
(expr-dispatch f:
(lambda (leaf) (list f: g:))
(lambda (f:: g::) (list f: g:))
(lambda (var body)
(if (or (not (pair? g:))
(and (<= (count-occurrences var body) 1)
(not (more-than-one-additional
(free-vars g:)
(append top-level-free-vars (free-vars f:)) ))))
(var-subst var g: body)
(list f: g:) )))))
(lambda (var body)
`(lambda (,var) ,(self body)) )))))
(define (add-prime var)
(string->symbol (string-append (symbol->string var) ":")) )
(define (var-subst var value template)
(if (eqv? var value)
template
(let loop ((template template))
(expr-dispatch template
(lambda (leaf)
(if (eqv? var leaf) value leaf) )
(lambda (f g)
(list (loop f) (loop g)) )
(lambda (v body)
(if (eqv? var v)
template
(do ((template-vars (free-vars template))
(value-vars (free-vars value))
(v: v (add-prime v:)) )
((and (not (memv v: template-vars))
(not (memv v: value-vars)) )
`(lambda (,v:)
,(loop (var-subst v v: body)) )))))))))
(define (more-than-one-additional a b)
(let loop ((a a) (last-sym (cons #f #f)))
(cond ((null? a) #f)
((memv (car a) b)
(loop (cdr a) last-sym) )
((or (pair? last-sym) ; no last symbol
(eqv? last-sym (car a)) )
(loop (cdr a) (car a)) )
(else #t) )))
(define (free-vars expr)
(let loop ((expr expr) (bound '()))
(expr-dispatch expr
(lambda (leaf)
(if (memv leaf bound)
'()
(list leaf) ))
(lambda (f g)
(append (loop f bound) (loop g bound)) )
(lambda (var body)
(loop body (cons var bound)) ))))
(define (contains-free-variable param template)
(expr-dispatch template
(lambda (leaf)
(eqv? param leaf) )
(lambda (f g)
(or (contains-free-variable param f)
(contains-free-variable param g) ))
(lambda (var body)
(and (not (eqv? param var))
(contains-free-variable param body) ))))
(define (count-occurrences param template)
(expr-dispatch template
(lambda (leaf)
(if (eqv? param leaf) 1 0) )
(lambda (f g)
(+ (count-occurrences param f) (count-occurrences param g)) )
(lambda (var body)
(if (eqv? var param)
0
(count-occurrences param body) ))))
; Abstraction elimination.
(define (unabstract-lambda var body)
(if (contains-free-variable var body)
(expr-dispatch body
(lambda (leaf) 'i)
(lambda (f g)
(if (and (eqv? var g) (not (contains-free-variable var f)))
f
`((s ,(unabstract-lambda var f)) ,(unabstract-lambda var g)) ))
(lambda (v b)
(unabstract-lambda var (unabstract body)) ))
(list 'k body) ))
(define (unabstract code)
(expr-dispatch code
(lambda (leaf) leaf)
(lambda (f g)
(list (unabstract f) (unabstract g)) )
(lambda (var body)
(unabstract-lambda var (unabstract body)) )))
; Reduces expressions involving the S, K, I combinators where this
; results in a shorter expression. Usually results in only a small
; benefit.
(define (apply-ski expr)
(if (not (pair? expr))
expr
(let ((lhs (apply-ski (car expr)))
(rhs (cadr expr)) )
(cond ((eq? lhs 'i) ; Ix -> x
(apply-ski rhs) )
((and (pair? lhs) ; Kxy -> x
(eq? 'k (car lhs)) )
(cadr lhs) )
((and (pair? lhs) ; Sxyz -> xz(yz) when x or y is K_
(pair? (car lhs))
(eq? 's (caar lhs)) )
(let ((z rhs)
(y (cadr lhs))
(x (cadar lhs)) )
(if (or (and (pair? x) (eq? (car x) 'k))
(and (pair? y) (eq? (car y) 'k)) )
(apply-ski `((,x ,z) (,y ,z)))
(list lhs (apply-ski rhs)) )))
(else
(list lhs (apply-ski rhs)) )))))
; This converts expressions of the form ((x z) (y z)) to (s x y z).
; If z is just a symbol, then this change makes no difference to
; Unlambda output, always reduces the size of CC output (I think),
; and can either increase or reduce the side of Iota and Jot output.
; Currently the change is made only when z is not just a symbol.
;
; Like apply-ski, this gives only a small benefit in most cases.
(define (unapply-s expr)
(expr-dispatch expr
(lambda (leaf) leaf)
(lambda (f g)
(let ((f: (unapply-s f))
(g: (unapply-s g)) )
(if (and (pair? f:)
(pair? g:)
(pair? (cadr f:))
(equal? (cadr f:) (cadr g:)) )
`(((s ,(car f:)) ,(car g:)) ,(cadr f:))
(list f: g:) )))
(lambda (var body)
`(lambda (,var) ,(unapply-s body)) )))
; Putting it all together.
(define (laze code)
(unapply-s (apply-ski (unabstract (apply-lambdas (expand-macros (curry-exp code)))))) )
; Printing it out.
(define (print-as-cc lazified-code)
(let self ((code lazified-code))
(expr-dispatch code
(lambda (leaf)
(if (memq leaf '(i k s))
(display (char-upcase (string-ref (symbol->string leaf) 0)))
(begin
(display "[")
(display leaf)
(display "]") )))
(lambda (f g)
(self f)
(if (pair? g) (display "(") '())
(self g)
(if (pair? g) (display ")") '()) )
(lambda (var body)
(error "Can't print lambdas as CC!") )))
(newline) )
(define (print-as-generic aply k s i)
(lambda (lazified-code)
(let self ((code lazified-code))
(expr-dispatch code
(lambda (leaf)
(cond ((eq? leaf 'i) (display i))
((eq? leaf 'k) (display k))
((eq? leaf 's) (display s))
(else (display "[") (display leaf) (display "]")) ))
(lambda (f g)
(display aply)
(self f)
(self g) )
(lambda (var body)
(error "Can't print lambdas as Lazy code!") )))
(newline) ))
(define print-as-unlambda (print-as-generic "`" "k" "s" "i"))
(define print-as-iota (print-as-generic "*" "*i*i*ii" "*i*i*i*ii" "*ii"))
(define print-as-jot (print-as-generic "1" "11100" "11111000" "11111111100000"))