Skip to content

Commit cd2108e

Browse files
committed
finish uncopying monday afternoon
1 parent 8a23b02 commit cd2108e

File tree

4 files changed

+24
-150
lines changed

4 files changed

+24
-150
lines changed

redex-doc/redex/scribblings/long-tut/code/common.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
;; (sd e) computes the static distance version of e
9191
(define-extended-language SD Lambda
92-
(e ::= .... (K n))
92+
(e ::= .... (K n) (lambda e))
9393
(n ::= natural))
9494

9595
(define SD? (redex-match? SD e))

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

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ subst (if time, otherwise it's provide)
139139

140140
;; SD metafunction
141141
(define-metafunction SD
142-
sd : any -> any
143-
[(sd any) (sd/a any ())])
142+
sd : e -> e
143+
[(sd e) (sd/a e ())])
144144

145145
(module+ test
146146
(test-equal (term (sd/a x ())) (term x))
@@ -153,19 +153,18 @@ subst (if time, otherwise it's provide)
153153
(term (lambda (lambda ((K 0) (lambda (K 2))))))))
154154

155155
(define-metafunction SD
156-
sd/a : any (x ...) -> any
156+
sd/a : e (x ...) -> any
157157
[(sd/a x (x_1 ... x x_2 ...))
158158
;; bound variable
159159
(K n)
160160
(where n ,(length (term (x_1 ...))))
161161
(where #false (in x (x_1 ...)))]
162-
[(sd/a (lambda (x) any_body) (x_rest ...))
163-
(lambda (sd/a any_body (x x_rest ...)))
162+
[(sd/a (lambda (x) e_body) (x_rest ...))
163+
(lambda (sd/a e_body (x x_rest ...)))
164164
(where n ,(length (term (x_rest ...))))]
165-
[(sd/a (any_fun any_arg) (x_rib ...))
166-
((sd/a any_fun (x_rib ...)) (sd/a any_arg (x_rib ...)))]
165+
[(sd/a (e_fun e_arg) (x ...))
166+
((sd/a e_fun (x ...)) (sd/a e_arg (x ...)))]
167167
[(sd/a any_1 (x ...))
168-
;; free variable or constant, etc
169168
any_1])
170169

171170
;; -----------------------------------------------------------------------------
@@ -177,52 +176,10 @@ subst (if time, otherwise it's provide)
177176
(test-equal (term (=α (lambda (x) x) (lambda (y) z))) #false))
178177

179178
(define-metafunction SD
180-
=α : any any -> boolean
179+
=α : e e -> boolean
181180
[(=α e_1 e_2) ,(equal? (term (sd e_1)) (term (sd e_2)))])
182181

183182
(define (=α/racket x y) (term (=α ,x ,y)))
184183

185-
;; -----------------------------------------------------------------------------
186-
;; (subst ([e x] ...) e_*) substitutes e ... for x ... in e_* (hygienically)
187-
188-
(module+ test
189-
(test-equal (term (subst ([1 x][2 y]) x)) 1)
190-
(test-equal (term (subst ([1 x][2 y]) y)) 2)
191-
(test-equal (term (subst ([1 x][2 y]) z)) (term z))
192-
(test-equal (term (subst ([1 x][2 y]) (lambda (z) (lambda (w) (x y)))))
193-
(term (lambda (z) (lambda (w) (1 2)))))
194-
(test-equal (term (subst ([1 x][2 y]) (lambda (z) (lambda (w) (lambda (x) (x y))))))
195-
(term (lambda (z) (lambda (w) (lambda (x) (x 2)))))
196-
#:equiv =α/racket)
197-
(test-equal (term (subst ((2 x)) ((lambda (x) (1 x)) x)))
198-
(term ((lambda (x) (1 x)) 2))
199-
#:equiv =α/racket)
200-
(test-equal (term (subst (((lambda (x) y) x)) (lambda (y) x)))
201-
(term (lambda (y1) (lambda (x) y)))
202-
#:equiv =α/racket))
203-
204-
(define-metafunction Lambda
205-
subst : ((any x) ...) any -> any
206-
[(subst [(any_1 x_1) ... (any_x x) (any_2 x_2) ...] x) any_x]
207-
[(subst [(any_1 x_1) ... ] x) x]
208-
[(subst [(any_1 x_1) ... ] (lambda (x) any_body))
209-
(lambda (x_new)
210-
(subst ((any_1 x_1) ...)
211-
(subst-raw (x_new x) any_body)))
212-
(where x_new ,(variable-not-in (term (any_1 ... any_body)) (term x)))]
213-
[(subst [(any_1 x_1) ... ] (any ...)) ((subst [(any_1 x_1) ... ] any) ...)]
214-
[(subst [(any_1 x_1) ... ] any_*) any_*])
215-
216-
(define-metafunction Lambda
217-
subst-raw : (x x) any -> any
218-
[(subst-raw (x_new x_) x_) x_new]
219-
[(subst-raw (x_new x_) x) x]
220-
[(subst-raw (x_new x_) (lambda (x) any))
221-
(lambda (x) (subst-raw (x_new x_) any))]
222-
[(subst-raw (x_new x_) (any ...))
223-
((subst-raw (x_new x_) any) ...)]
224-
[(subst-raw (x_new x_) any_*) any_*])
225-
226-
;; -----------------------------------------------------------------------------
227184
(module+ test
228185
(test-results))

redex-doc/redex/scribblings/long-tut/mon-aft.scrbl

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ accumulator-functions in Redex.
156156

157157
Now α equivalence is straightforward:
158158

159-
160159
@code-from-file["code/mon-aft.rkt"
161160
#rx"determines whether e_1 and e_2 are α equivalent"
162161
#:exp-count 3]
@@ -165,6 +164,7 @@ Now α equivalence is straightforward:
165164
@; -----------------------------------------------------------------------------
166165
@section{Extending a Language: @racket[any]}
167166

167+
168168
Suppose we wish to extend @racket[Lambda] with @racket[if] and Booleans,
169169
like this:
170170
@;%
@@ -178,7 +178,7 @@ like this:
178178
(if e e e)))
179179
))
180180
@;%
181-
Guess what? @racket[(term (fv (lambda (x y) (if x y false))))] doesn't
181+
Guess what? @racket[(term (sd (lambda (x y) (if x y false))))] doesn't
182182
work because @racket[false] and @racket[if] are not covered.
183183

184184
We want metafunctions that are as generic as possible for computing such
@@ -187,45 +187,11 @@ equivalences.
187187

188188
Redex contracts come with @racket[any] and Redex patterns really are over
189189
Racket's S-expressions. This definition now works for extensions that don't
190-
add binders:
191-
@;%
192-
@(begin
193-
#reader scribble/comment-reader
194-
(racketblock
195-
(module+ test
196-
(test-equal (SD? sd1) #true))
197-
198-
(define-metafunction SD
199-
sd : e -> e
200-
[(sd e) (sd/a e ())])
201-
202-
(module+ test
203-
(test-equal (term (sd/a x ())) (term x))
204-
(test-equal (term (sd/a x (y z x))) (term (K 2)))
205-
(test-equal (term (sd/a ((lambda (x) x) (lambda (y) y)) ()))
206-
(term ((lambda (K 0)) (lambda (K 0)))))
207-
(test-equal (term (sd/a (lambda (x) (x (lambda (y) y))) ()))
208-
(term (lambda ((K 0) (lambda (K 0))))))
209-
(test-equal (term (sd/a (lambda (z) (lambda (x) (x (lambda (y) z)))) ()))
210-
(term (lambda (lambda ((K 0) (lambda (K 2))))))))
211-
212-
(define-metafunction SD
213-
sd/a : e (x ...) -> e
214-
[(sd/a x (x_1 ... x x_2 ...))
215-
;; bound variable
216-
(K n)
217-
(where n ,(length (term (x_1 ...))))
218-
(where #false (in x (x_1 ...)))]
219-
[(sd/a (lambda (x) e) (x_rest ...))
220-
(lambda (sd/a e (x x_rest ...)))
221-
(where n ,(length (term (x_rest ...))))]
222-
[(sd/a (e_fun e_arg) (x_rib ...))
223-
((sd/a e_fun (x_rib ...)) (sd/a e_arg (x_rib ...)))]
224-
[(sd/a any_1 (x ...))
225-
;; free variable or constant
226-
any_1])
227-
))
228-
@;%
190+
add binders:
191+
192+
@code-from-file["code/common.rkt"
193+
#rx"define-extended-language SD Lambda"
194+
#:exp-count 6]
229195

230196
@; -----------------------------------------------------------------------------
231197
@section{Substitution}
@@ -234,49 +200,6 @@ The last thing we need is substitution, because it @emph{is} the syntactic
234200
equivalent of function application. We define it with @emph{any} having
235201
future extensions in mind.
236202

237-
@;%
238-
@(begin
239-
#reader scribble/comment-reader
240-
(racketblock
241-
;; (subst ([e x] ...) e_*) substitutes e ... for x ... in e_* (hygienically)
242-
243-
(module+ test
244-
(test-equal (term (subst ([1 x][2 y]) x)) 1)
245-
(test-equal (term (subst ([1 x][2 y]) y)) 2)
246-
(test-equal (term (subst ([1 x][2 y]) z)) (term z))
247-
(test-equal (term (subst ([1 x][2 y]) (lambda (z) (lambda (w) (x y)))))
248-
(term (lambda (z) (lambda (w) (1 2)))))
249-
(test-equal (term (subst ([1 x][2 y]) (lambda (z) (lambda (w) (lambda (x) (x y))))))
250-
(term (lambda (z) (lambda (w) (lambda (x) (x 2)))))
251-
#:equiv =α/racket)
252-
(test-equal (term (subst ((2 x)) ((lambda (x) (1 x)) x)))
253-
(term ((lambda (x) (1 x)) 2))
254-
#:equiv =α/racket))
255-
256-
(define-metafunction Lambda
257-
subst : ((any x) ...) any -> any
258-
[(subst [(any_1 x_1) ... (any_x x) (any_2 x_2) ...] x) any_x]
259-
[(subst [(any_1 x_1) ... ] x) x]
260-
[(subst [(any_1 x_1) ... ] (lambda (x) any_body))
261-
(lambda (x_new)
262-
(subst ((any_1 x_1) ...)
263-
(subst-raw (x_new x) any_body)))
264-
(where x_new ,(variable-not-in (term any_body) (term x)))]
265-
[(subst [(any_1 x_1) ... ] (any ...)) ((subst [(any_1 x_1) ... ] any) ...)]
266-
[(subst [(any_1 x_1) ... ] any_*) any_*])
267-
268-
(define-metafunction Lambda
269-
subst-raw : (x x) any -> any
270-
[(subst-raw (x_new x_) x_) x_new]
271-
[(subst-raw (x_new x_) x) x]
272-
[(subst-raw (x_new x_) (lambda (x) any))
273-
(lambda (x) (subst-raw (x_new x_) any))]
274-
[(subst-raw (x_new x_) (any ...))
275-
((subst-raw (x_new x_) any) ...)]
276-
[(subst-raw (x_new x_) any_*) any_*])
277-
278-
))
279-
@;%
280-
281-
282-
}
203+
@code-from-file["code/common.rkt"
204+
#rx"substitutes e [.][.][.] for x [.][.][.] in e_[*]"
205+
#:exp-count 3]

redex-doc/redex/scribblings/long-tut/shared.rkt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
#lang at-exp racket/base
2-
(module+ test (require rackunit))
3-
4-
#; #;
5-
(this is to facilitate
6-
some testing code
7-
that appears below)
8-
(it helps test
9-
codeblock-from-file)
102

113
(provide
124
goals ;; bulletize goals
@@ -26,7 +18,7 @@
2618
"exercise/ex.rkt"
2719
(for-label racket/base redex/reduction-semantics)
2820
(for-syntax racket/base racket/match racket/list racket/port
29-
syntax/parse syntax/strip-context)
21+
syntax/parse syntax/strip-context compiler/cm-accomplice)
3022
scribble/manual
3123
scribble/core
3224
scribble/example
@@ -125,14 +117,16 @@ to the top of your file:
125117
(define (get-code file rx:start number-of-expressions number-of-lines-to-skip extra-code doing-eval? stx)
126118
(define src (syntax-source stx))
127119
(define-values (src-dir _1 _2) (split-path src))
120+
(define file-with-code-to-show (build-path src-dir file))
121+
(register-external-file file-with-code-to-show)
128122
(define-values (in out) (make-pipe))
129123
(define-values (start-pos start-line end-line no-prompt?s)
130124
(get-start-and-end-lines file rx:start
131125
number-of-expressions
132126
stx
133127
src-dir))
134128
(set! start-line (+ start-line number-of-lines-to-skip))
135-
(call-with-input-file (build-path src-dir file)
129+
(call-with-input-file file-with-code-to-show
136130
(λ (port)
137131
(for/list ([l (in-lines port)]
138132
[i (in-range end-line)]
@@ -148,7 +142,7 @@ to the top of your file:
148142
(for/list ([no-prompt? (in-list (append no-prompt?s
149143
(make-list (length extra-code)
150144
#f)))])
151-
(define e (replace-context stx (read-syntax (build-path src-dir file) in)))
145+
(define e (replace-context stx (read-syntax file-with-code-to-show in)))
152146
(if no-prompt?
153147
`(eval:no-prompt ,e)
154148
e))]

0 commit comments

Comments
 (0)