@@ -22,31 +22,17 @@ subst (if time, otherwise it's provide)
22
22
;; -----------------------------------------------------------------------------
23
23
(require redex)
24
24
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
-
39
25
(define-language Lambda
40
26
(e ::=
41
27
x
42
- (lambda (x_!_ ... ) e)
43
- (e e ... ))
28
+ (lambda (x ) e)
29
+ (e e))
44
30
(x ::= variable-not-otherwise-mentioned))
45
31
46
32
(define e1 (term y))
47
33
(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)))
50
36
51
37
(define in-Lambda? (redex-match? Lambda e))
52
38
@@ -56,8 +42,8 @@ subst (if time, otherwise it's provide)
56
42
(test-equal (in-Lambda? e3) #true )
57
43
(test-equal (in-Lambda? e4) #true ))
58
44
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 ) )))
61
47
62
48
(module+ test
63
49
(test-equal (in-Lambda? eb1) #false )
@@ -95,19 +81,18 @@ subst (if time, otherwise it's provide)
95
81
(module+ test
96
82
(test-equal (term (fv x)) (term (x)))
97
83
(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))))
99
85
100
86
(define-metafunction Lambda
101
- fv : any -> (x ... )
87
+ fv : e -> (x ... )
102
88
[(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))])
111
96
112
97
;; -----------------------------------------------------------------------------
113
98
;; (subtract (x ...) x_1 ...) removes x_1 ... from (x ...)
@@ -137,65 +122,57 @@ subst (if time, otherwise it's provide)
137
122
;; (sd e) computes the static distance version of e
138
123
139
124
(define-extended-language SD Lambda
140
- (e ::= .... (K n n ) (lambda n e) )
125
+ (e ::= .... (K n) (lambda e) n )
141
126
(n ::= natural))
142
127
143
- (define sd1 (term (K 0 1 )))
144
- (define sd2 (term 1 ))
128
+ (define sd1 (term (K 0 )))
145
129
146
130
(define SD? (redex-match? SD e))
147
131
148
132
(module+ test
149
133
(test-equal (SD? sd1) #true ))
150
134
151
135
(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 ())])
154
138
155
139
(module+ test
156
140
(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 )))
158
142
(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 )))))
160
144
(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 ) )))))))
164
148
165
149
(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 ... ))
168
152
;; 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
180
163
any_1])
181
164
182
165
;; -----------------------------------------------------------------------------
183
166
;; (=α e_1 e_2) determines whether e_1 and e_2 are α equivalent
184
167
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
-
191
168
(module+ test
192
169
(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 )
194
171
(test-equal (term (=α (lambda (x) x) (lambda (y) z))) #false ))
195
172
196
173
(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 )))])
199
176
200
177
(define (=α/racket x y) (term (=α ,x ,y)))
201
178
@@ -206,38 +183,36 @@ subst (if time, otherwise it's provide)
206
183
(test-equal (term (subst ([1 x][2 y]) x)) 1 )
207
184
(test-equal (term (subst ([1 x][2 y]) y)) 2 )
208
185
(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 ) ))))
213
190
#:equiv =α/racket)
214
191
(test-equal (term (subst ((2 x)) ((lambda (x) (1 x)) x)))
215
192
(term ((lambda (x) (1 x)) 2 ))
216
193
#:equiv =α/racket))
217
-
218
-
219
194
220
195
(define-metafunction Lambda
221
196
subst : ((any x) ... ) any -> any
222
197
[(subst [(any_1 x_1) ... (any_x x) (any_2 x_2) ... ] x) any_x]
223
198
[(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)
226
201
(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 )))]
229
204
[(subst [(any_1 x_1) ... ] (any ... )) ((subst [(any_1 x_1) ... ] any ) ... )]
230
205
[(subst [(any_1 x_1) ... ] any_*) any_*])
231
206
232
207
(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_*])
241
216
242
217
;; -----------------------------------------------------------------------------
243
218
(module+ test
0 commit comments