Skip to content

Commit 18e41ca

Browse files
committed
updated monday
1 parent 65b90be commit 18e41ca

File tree

3 files changed

+109
-161
lines changed

3 files changed

+109
-161
lines changed

redex-doc/redex/scribblings/long-tut/code/mon-aft.rkt

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,17 @@ subst (if time, otherwise it's provide)
2222
;; -----------------------------------------------------------------------------
2323
(require redex)
2424

25-
(define-language Lambda0
26-
(e ::=
27-
x
28-
(lambda (x ...) e)
29-
(e e ...))
30-
(x ::= variable-not-otherwise-mentioned))
31-
32-
(define-language Lambda-bad
33-
(e ::=
34-
x
35-
(side-condition (lambda (x_ ...) e) (term (unique-vars (x_ ...))))
36-
(e e ...))
37-
(x ::= variable-not-otherwise-mentioned))
38-
3925
(define-language Lambda
4026
(e ::=
4127
x
42-
(lambda (x_!_ ...) e)
43-
(e e ...))
28+
(lambda (x) e)
29+
(e e))
4430
(x ::= variable-not-otherwise-mentioned))
4531

4632
(define e1 (term y))
4733
(define e2 (term (lambda (y) y)))
48-
(define e3 (term (lambda (x y) y)))
49-
(define e4 (term (,e2 e3)))
34+
(define e3 (term (lambda (x) (lambda (y) y))))
35+
(define e4 (term (,e2 ,e3)))
5036

5137
(define in-Lambda? (redex-match? Lambda e))
5238

@@ -56,8 +42,8 @@ subst (if time, otherwise it's provide)
5642
(test-equal (in-Lambda? e3) #true)
5743
(test-equal (in-Lambda? e4) #true))
5844

59-
(define eb1 (term (lambda (x x) y)))
60-
(define eb2 (term (lambda (x y) 3)))
45+
(define eb1 (term (lambda (y) (lambda () y))))
46+
(define eb2 (term (lambda (x) (lambda (y) 3))))
6147

6248
(module+ test
6349
(test-equal (in-Lambda? eb1) #false)
@@ -95,19 +81,18 @@ subst (if time, otherwise it's provide)
9581
(module+ test
9682
(test-equal (term (fv x)) (term (x)))
9783
(test-equal (term (fv (lambda (x) x))) (term ()))
98-
(test-equal (term (fv (lambda (x) (y z x)))) (term (y z))))
84+
(test-equal (term (fv (lambda (x) ((y z) x)))) (term (y z))))
9985

10086
(define-metafunction Lambda
101-
fv : any -> (x ...)
87+
fv : e -> (x ...)
10288
[(fv x) (x)]
103-
[(fv (lambda (x ...) any_body))
104-
(subtract (x_e ...) x ...)
105-
(where (x_e ...) (fv any_body))]
106-
[(fv (any_f any_a ...))
107-
(x_f ... x_a ... ...)
108-
(where (x_f ...) (fv any_f))
109-
(where ((x_a ...) ...) ((fv any_a) ...))]
110-
[(fv any) ()])
89+
[(fv (lambda (x) e_body))
90+
(subtract (x_e ...) x)
91+
(where (x_e ...) (fv e_body))]
92+
[(fv (e_f e_a))
93+
(x_f ... x_a ...)
94+
(where (x_f ...) (fv e_f))
95+
(where (x_a ...) (fv e_a))])
11196

11297
;; -----------------------------------------------------------------------------
11398
;; (subtract (x ...) x_1 ...) removes x_1 ... from (x ...)
@@ -137,65 +122,57 @@ subst (if time, otherwise it's provide)
137122
;; (sd e) computes the static distance version of e
138123

139124
(define-extended-language SD Lambda
140-
(e ::= .... (K n n) (lambda n e))
125+
(e ::= .... (K n) (lambda e) n)
141126
(n ::= natural))
142127

143-
(define sd1 (term (K 0 1)))
144-
(define sd2 (term 1))
128+
(define sd1 (term (K 0)))
145129

146130
(define SD? (redex-match? SD e))
147131

148132
(module+ test
149133
(test-equal (SD? sd1) #true))
150134

151135
(define-metafunction SD
152-
sd : any -> any
153-
[(sd any_1) (sd/a any_1 ())])
136+
sd : e -> e
137+
[(sd e) (sd/a e ())])
154138

155139
(module+ test
156140
(test-equal (term (sd/a x ())) (term x))
157-
(test-equal (term (sd/a x ((y) (z) (x)))) (term (K 2 0)))
141+
(test-equal (term (sd/a x (y z x))) (term (K 2)))
158142
(test-equal (term (sd/a ((lambda (x) x) (lambda (y) y)) ()))
159-
(term ((lambda 1 (K 0 0)) (lambda 1 (K 0 0)))))
143+
(term ((lambda (K 0)) (lambda (K 0)))))
160144
(test-equal (term (sd/a (lambda (x) (x (lambda (y) y))) ()))
161-
(term (lambda 1 ((K 0 0) (lambda 1 (K 0 0))))))
162-
(test-equal (term (sd/a (lambda (z x) (x (lambda (y) z))) ()))
163-
(term (lambda 2 ((K 0 1) (lambda 1 (K 1 0)))))))
145+
(term (lambda ((K 0) (lambda (K 0))))))
146+
(test-equal (term (sd/a (lambda (z) (lambda (x) (x (lambda (y) z)))) ()))
147+
(term (lambda (lambda ((K 0) (lambda (K 2))))))))
164148

165149
(define-metafunction SD
166-
sd/a : any ((x ...) ...) -> any
167-
[(sd/a x ((x_1 ...) ... (x_0 ... x x_2 ...) (x_3 ...) ...))
150+
sd/a : e (x ...) -> e
151+
[(sd/a x (x_1 ... x x_2 ...))
168152
;; bound variable
169-
(K n_rib n_pos)
170-
(where n_rib ,(length (term ((x_1 ...) ...))))
171-
(where n_pos ,(length (term (x_0 ...))))
172-
(where #false (in x (x_1 ... ...)))]
173-
[(sd/a (lambda (x ...) any_1) (any_rest ...))
174-
(lambda n (sd/a any_1 ((x ...) any_rest ...)))
175-
(where n ,(length (term (x ...))))]
176-
[(sd/a (any_fun any_arg ...) (any_rib ...))
177-
((sd/a any_fun (any_rib ...)) (sd/a any_arg (any_rib ...)) ...)]
178-
[(sd/a any_1 any)
179-
;; free variable, constant, etc
153+
(K n)
154+
(where n ,(length (term (x_1 ...))))
155+
(where #false (in x (x_1 ...)))]
156+
[(sd/a (lambda (x) e) (x_rest ...))
157+
(lambda (sd/a e (x x_rest ...)))
158+
(where n ,(length (term (x_rest ...))))]
159+
[(sd/a (e_fun e_arg) (x_rib ...))
160+
((sd/a e_fun (x_rib ...)) (sd/a e_arg (x_rib ...)))]
161+
[(sd/a any_1 (x ...))
162+
;; free variable or constant
180163
any_1])
181164

182165
;; -----------------------------------------------------------------------------
183166
;; (=α e_1 e_2) determines whether e_1 and e_2 are α equivalent
184167

185-
(define-extended-language Lambda/n Lambda
186-
(e ::= .... n)
187-
(n ::= natural))
188-
189-
(define in-Lambda/n? (redex-match? Lambda/n e))
190-
191168
(module+ test
192169
(test-equal (term (=α (lambda (x) x) (lambda (y) y))) #true)
193-
(test-equal (term (=α (lambda (x) (x 1)) (lambda (y) (y 1)))) #true)
170+
(test-equal (term (=α (lambda (x) (x z)) (lambda (y) (y z)))) #true)
194171
(test-equal (term (=α (lambda (x) x) (lambda (y) z))) #false))
195172

196173
(define-metafunction SD
197-
=α : any any -> boolean
198-
[(=α any_1 any_2) ,(equal? (term (sd any_1)) (term (sd any_2)))])
174+
=α : e e -> boolean
175+
[(=α e_1 e_2) ,(equal? (term (sd e_1)) (term (sd e_2)))])
199176

200177
(define (=α/racket x y) (term (=α ,x ,y)))
201178

@@ -206,38 +183,36 @@ subst (if time, otherwise it's provide)
206183
(test-equal (term (subst ([1 x][2 y]) x)) 1)
207184
(test-equal (term (subst ([1 x][2 y]) y)) 2)
208185
(test-equal (term (subst ([1 x][2 y]) z)) (term z))
209-
(test-equal (term (subst ([1 x][2 y]) (lambda (z w) (x y))))
210-
(term (lambda (z w) (1 2))))
211-
(test-equal (term (subst ([1 x][2 y]) (lambda (z w) (lambda (x) (x y)))))
212-
(term (lambda (z w) (lambda (x) (x 2))))
186+
(test-equal (term (subst ([1 x][2 y]) (lambda (z) (lambda (w) (x y)))))
187+
(term (lambda (z) (lambda (w) (1 2)))))
188+
(test-equal (term (subst ([1 x][2 y]) (lambda (z) (lambda (w) (lambda (x) (x y))))))
189+
(term (lambda (z) (lambda (w) (lambda (x) (x 2)))))
213190
#:equiv =α/racket)
214191
(test-equal (term (subst ((2 x)) ((lambda (x) (1 x)) x)))
215192
(term ((lambda (x) (1 x)) 2))
216193
#:equiv =α/racket))
217-
218-
219194

220195
(define-metafunction Lambda
221196
subst : ((any x) ...) any -> any
222197
[(subst [(any_1 x_1) ... (any_x x) (any_2 x_2) ...] x) any_x]
223198
[(subst [(any_1 x_1) ... ] x) x]
224-
[(subst [(any_1 x_1) ... ] (lambda (x ...) any_body))
225-
(lambda (x_new ...)
199+
[(subst [(any_1 x_1) ... ] (lambda (x) any_body))
200+
(lambda (x_new)
226201
(subst ((any_1 x_1) ...)
227-
(subst-raw ((x_new x) ...) any_body)))
228-
(where (x_new ...) ,(variables-not-in (term any_body) (term (x ...)))) ]
202+
(subst-raw (x_new x) any_body)))
203+
(where x_new ,(variable-not-in (term any_body) (term x)))]
229204
[(subst [(any_1 x_1) ... ] (any ...)) ((subst [(any_1 x_1) ... ] any) ...)]
230205
[(subst [(any_1 x_1) ... ] any_*) any_*])
231206

232207
(define-metafunction Lambda
233-
subst-raw : ((x x) ...) any -> any
234-
[(subst-raw ((x_n1 x_o1) ... (x_new x) (x_n2 x_o2) ...) x) x_new]
235-
[(subst-raw ((x_n1 x_o1) ... ) x) x]
236-
[(subst-raw ((x_n1 x_o1) ... ) (lambda (x ...) any))
237-
(lambda (x ...) (subst-raw ((x_n1 x_o1) ... ) any))]
238-
[(subst-raw [(any_1 x_1) ... ] (any ...))
239-
((subst-raw [(any_1 x_1) ... ] any) ...)]
240-
[(subst-raw [(any_1 x_1) ... ] any_*) any_*])
208+
subst-raw : (x x) any -> any
209+
[(subst-raw (x_new x_) x_) x_new]
210+
[(subst-raw (x_new x_) x) x]
211+
[(subst-raw (x_new x_) (lambda (x) any))
212+
(lambda (x) (subst-raw (x_new x_) any))]
213+
[(subst-raw (x_new x_) (any ...))
214+
((subst-raw (x_new x_) any) ...)]
215+
[(subst-raw (x_new x_) any_*) any_*])
241216

242217
;; -----------------------------------------------------------------------------
243218
(module+ test

0 commit comments

Comments
 (0)