-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterp_capture.rkt
More file actions
232 lines (190 loc) · 6.91 KB
/
interp_capture.rkt
File metadata and controls
232 lines (190 loc) · 6.91 KB
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
#lang racket
; capture avoiding substituion in racket
(require racket/trace)
(define (freevars e)
(match e
[(? symbol? x) (set x)]
[(? number? y) (set)]
[`(lambda (,x) ,eb) (set-remove (freevars eb) x)]
[`(,ef ,ea) (set-union (freevars ef) (freevars ea))]
)
)
(define (cas e0 x e1)
; (displayln (~a "e0: " e0 "\nx: " x "\ne1: " e1 "\n"))
(match e0
[(? symbol? y) #:when (eq? y x) e1]
[(? symbol? y) y]
[`(lambda (,y) ,eb) #:when (eq? y x) e0]
[`(lambda (,y) ,eb) #:when (not (set-member? (freevars e1) y)) `(lambda (,y) ,(cas eb x e1))]
[`(,ef ,ea) `(,(cas ef x e1) ,(cas ea x e1))]
[(? number? y) y]
)
)
; (cas '((lambda (y) x) z) 'x 'y)
; (cas '((lambda (y) x) z) 'x 'y)
; (trace cas)
; (cas 'x 5)
; ; (cas `(lambda (x) x) 'x 5)
; ; (cas `(lambda (x) x) `(`(lambda (y) y) 'y) 5)
; ; (cas `(`(lambda (x) x) `(lambda (y) y)) 'y 5)
; (cas (cas `(lambda (x) x) 'x 0) 'x '(lambda (x) x))
(define (atom? exp)
(match exp
[`(lambda (,_) ,_) #t]
[(? symbol? y) #t]
[(? number? y) #t]
[_ #f]
)
)
(define (value? exp)
(match exp
[`(lambda (,_) ,_) #t]
[(? number? y) #t]
[_ #f]
)
)
(define (reduce exp)
(match exp
[(? symbol? y) y]
[`(lambda (,x) ,eb) exp]
[(? number? y) y]
[`((lambda (,y) ,eb) ,args) #:when (value? args) (cas eb y args)]
[`((lambda (,y) ,eb) ,args) `((lambda (,y) ,eb) ,(reduce args))]
[`(,ef ,ea) `(,(reduce ef) ,ea)]
)
)
; (trace reduce)
; (cas `(((lambda (x) ((lambda (y) x) y)) y)))
; (reduce `((lambda (x) x)(lambda (y) y)))
; (reduce (reduce `((lambda (x) x)(lambda (y) y))))
(define (all-beta-reductions exp)
(let loop ([exp exp] [acc (set)] )
;(displayln (~a "acc: " acc "\nexp: " exp "\n"))
(let ([nRedex (reduce exp)])
(cond
[(equal? nRedex exp) acc]
[else (loop nRedex (set-add acc nRedex))]
)
)
)
)
(define (gatherCBV exp)
(let loop ([trace (list exp)])
(if (value? (car trace))
(reverse trace)
(loop (cons (reduce (car trace)) trace))
)
)
)
; (define (reduce exp)
; (match exp
; [(? symbol? y) y]
; [`(lambda (,x) ,eb) exp]
; [(? number? y) y]
; [`((lambda (,y) ,eb) ,args) #:when (value? args) (cas eb y args)]
; [`((lambda (,y) ,eb) ,args) `((lambda (,y) ,eb) ,(reduce args))]
; [`(,ef ,ea) `(,(reduce ef) ,ea)]
; )
; )
(define (reduce-cbn exp)
; (displayln (~a "Exp: " exp "\n"))
(match exp
[(? symbol? y) y]
; [(? number? y) y]
[`((lambda (,y) ,eb) ,args) (reduce-cbn (cas eb y args))]
[`((lambda (,y) ,eb) ,args) #:when (value? args) exp]
[`(lambda (,y) ,eb) `(lambda (,y) ,(reduce-cbn eb))]
[`((lambda (,x) ,eb) ((lambda (,y) ,ex) ,args)) (cas eb x args)]
[`(,ef ,ea) `(,(reduce-cbn ef) ,(reduce-cbn ea))]
; [`((,ef ,ea) (,e3 ,e4)) exp]
)
)
;cbn
; ((lambda (x) (lambda (y) x)) ((lambda (z) z) w))
; (lambda (y) ((lambda (z) z) w))
; (lambda (y) w)
(define (reduce-cbn-v2 exp)
(match exp
[(? symbol? y) y]
[`(lambda (,x) ,eb)
(if (atom? eb)
exp
`(lambda (,x) ,(reduce-cbn-v2 eb))
)
]
[`((lambda (,y) ,eb) ,args)
(cas eb y args)]
[`(,ef ,ea) `(,(reduce-cbn-v2 ef) ,ea)]
)
)
(define (gatherCBN exp)
(let loop ([exp exp] )
(let ([nRedex (reduce-cbn-v2 exp)])
(displayln (~a "exp: " exp "\nnRedex: "nRedex "\n---"))
(cond
[(equal? nRedex exp) exp]
[else (loop nRedex)]
)
)
)
)
; (define (gatherCBN exp)
; (let loop ([trace (list exp)])
; (displayln (~a "Car of Trace: " (car trace) "\n"))
; (cond
; [(value? (car trace)) (reverse trace)]
; [(and ((equal? (car trace) exp) (value? (car trace)))) (reverse trace)]
; [else (loop (cons (reduce-cbn (car trace)) trace))]
; )
; ; (if (value? (car trace))
; ; (reverse trace)
; ; (loop (cons (reduce-cbn (car trace)) trace))
; ; )
; )
; )
; (require racket/trace)
(trace gatherCBN)
(trace gatherCBV)
(trace cas)
(trace reduce)
(trace reduce-cbn)
(trace reduce-cbn-v2)
(trace all-beta-reductions)
; (pretty-print (gatherCBN `((lambda (a) (a (lambda (b) (a b)))) 5)))
(displayln (~a "\n\n"))
(pretty-print (gatherCBN `((lambda (a) ((lambda (b) (a b)) a)) 5)))
; (λ (a) (a (λ (b) (a b)))) vs (λ (a) ((λ (b) (a b)) a))
(λ (a) (a (λ (b) (a b))))
CBN: (λ (a) (a (λ (b) (a b))))
-> (λ (a) (a (λ (b) (a b)))) ; Under lambda, CBN stops here
APP: ((λ (a) (a (λ (b) (a b)))) (lambda (x) x))
->𝛽 (lambda (x) x) (lambda (b) ((lambda (x) x) b))
->𝛽 (lambda (x) x) (lambda (b) (b))
(λ (a) ((λ (b) (a b)) a))
CBN: (λ (a) ((λ (b) (a b)) a))
-> (λ (a) ((λ (b) (a b)) a)) ; Under lambda, CBN stops here
APP: ((lambda (a) ((lambda (b) (a b)) a)) (lambda (x) x))
->𝛽 ((lambda (b) ((lambda (x) x) b)) (lambda (x) x))
->𝛽 (lambda (b) ((lambda (x) x) (lambda (x) x)))
->𝛽 (lamdba (b) (lambda (x) x))
; (pretty-print (gatherCBN `((lambda (x) (lambda (y) (x y))) ((lambda (a) (a a)) (lambda (w) w)))))
; (pretty-print (gatherCBN `((lambda (y) (((lambda (a) (a a)) (lambda (w) w)) y)))))
; I'm not really getting the match form in gather, why not just loop with a reduced value at the front of the trace when you have not yet reached a value? What does this case do: [`((lambda (,y) ,eb) ,args) (loop (cons args trace))] ?
; In reduce-cbn: you are not quite doing CBN evaluation due to the last case. What happens when you have ((e1 e2) (e3 e4)) where e1 is a lambda?
; come up with a set of all possibilities, which are right -> traces that can be CBV, CBN, etc
; (trace all-beta-reductions)
; (all-beta-reductions `((lambda (x) x)(lambda (y) y)))
; (pretty-print (reduce-cbn `((lambda (x) x) (((lambda (a) (lambda (b) (a b))) ((lambda (y) y) w)) (lambda (z) z)))))
; (pretty-print (all-beta-reductions-cbn `((lambda (x) x) (((lambda (a) (lambda (b) (a b))) (lambda (y) y)) (lambda (z) z)))))
; (pretty-print (gatherCBN `(((lambda (a) (lambda (b) (a b))) (lambda (y) y)) (lambda (z) z))))
; (pretty-print (all-beta-reductions-cbn `((lambda (x) (lambda (y) x)) ((lambda (z) z) w))))
; (pretty-print (gatherCBN `((lambda (x) (x x)) (lambda (y) (y y)))))
; (cas `(lambda (b) (a b)) 'a `(lambda (y) y))
; (beta-all-reductions `((lambda (x) x) (lambda (y) y) (lambda (z) z)))
; (beta-all-reductions `((lambda (x) x) (((lambda (a) (lambda (b) (a b))) (lambda (y) y)) (lambda (z) z))))
; (all-beta-reductions `((lambda (x) x) (((lambda (a) (lambda (b) (a b))) (lambda (y) y)) (lambda (z) z))))
; (reduce `(lambda (y) y))
;cbn
; ((lambda (x) (lambda (y) x)) ((lambda (z) z) w))
; (lambda (y) ((lambda (z) z) w))
; (lambda (y) w)