forked from zhangxiaojie/magic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
interp-call-by-name.ss
136 lines (113 loc) · 4 KB
/
interp-call-by-name.ss
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
;; call-by-name interpreter (reducer)
(load "pmatch.scm")
(load "encoding.scm")
(define occur-free?
(lambda (y exp)
(pmatch exp
[(lambda (,x) ,e) (and (not (eq? y x)) (occur-free? y e))]
[(,rator ,rand) (or (occur-free? y rator) (occur-free? y rand))]
[,exp (eq? y exp)])))
(define value?
(lambda (exp)
(pmatch exp
[,x (guard (symbol? x)) #t]
[(lambda (,x) ,e) #t]
[(,rator ,rand) #f])))
(define gensym
(let ((n -1))
(lambda (x)
(set! n (+ 1 n))
(string->symbol
(string-append (symbol->string x) "." (number->string n))))))
(define subst
(lambda (x y exp)
(pmatch exp
[,u (guard (symbol? u)) (if (eq? u x) y u)]
[(lambda (,u) ,e)
(cond
[(eq? u x) exp]
[(and (occur-free? u y) (occur-free? x e))
(let* ([u* (gensym u)]
[e* (subst u u* e)])
`(lambda (,u*) ,(subst x y e*)))]
[else
`(lambda (,u) ,(subst x y e))])]
[(,e1 ,e2) `(,(subst x y e1) ,(subst x y e2))]
[,exp exp])))
(define redex-of car)
(define ctx-of cdr)
(define find-redexes
(lambda (exp)
(letrec ([find
(lambda (exp C)
(pmatch exp
[(lambda (,x) ,e)
(find e (lambda (v) (C `(lambda (,x) ,v))))]
[((lambda (,x) ,e1) ,e2)
(append `((,exp . ,C))
(find e1 (lambda (v) (C `((lambda (,x) ,v) ,e2))))
(find e2 (lambda (v) (C `((lambda (,x) ,e1) ,v)))))]
[(,e1 ,e2)
(append (find e1 (lambda (v) (C `(,v ,e2))))
(find e2 (lambda (v) (C `(,e1 ,v)))))]
[,exp '()]))])
(find exp (lambda (v) v)))))
;; do one beta-reduction if the operand is a lambda, otherwise output it
;; verbatically.
(define beta
(lambda (redex)
(pmatch redex
[((lambda (,x) ,e1) ,e2) (subst x e2 e1)]
[,other other])))
;; deterministic reducer
(define reducer
(lambda (exp)
(let ([redexes (find-redexes exp)])
(cond
[(null? redexes) exp]
[else
(let ([first (car redexes)])
(reducer ((ctx-of first) (beta (redex-of first)))))]))))
;;; random reducer
(define random-reducer
(lambda (exp tick)
(cond
[(zero? tick) exp]
[else
(let ([redexes (find-redexes exp)])
(cond
[(null? redexes) exp]
[else
(let* ([pick (list-ref redexes (random (length redexes)))]
[new-exp ((ctx-of pick) (beta (redex-of pick)))])
(random-reducer new-exp (sub1 tick)))]))])))
;;; tests
(reducer `(,!-n ,lthree))
; => (lambda (f) (lambda (x) (f (f (f (f (f (f x))))))))
(reducer (random-reducer `(,!-n ,lthree) 300))
; => (lambda (f) (lambda (x.42) (f (f (f (f (f (f x.42))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (reducer '((lambda (y) z) ((lambda (x) (x x)) (lambda (x) (x x)))))
;; (nd-reducer '((lambda (y) z) ((lambda (x) (x x)) (lambda (x) (x x)))) 3)
;; (map reducer (nd-reducer `(,! ,ltwo) 1))
; non-deterministic reducer
(define nd-reducer
(lambda (exp tick)
(letrec ([reduce1
(lambda (redexes tick)
(cond
[(null? redexes) '()]
[(zero? tick) (map (lambda (x) ((ctx-of x) (redex-of x))) redexes)]
[else
(let ([pick (list-ref redexes (random (length redexes)))])
(cond
[(value? pick)
(reduce1 redexes tick)]
[else
(let ([pick* ((ctx-of pick) (beta (redex-of pick)))]
[new-redexes (append (remq pick redexes)
(find-redexes pick*) `((,pick* . ,(lambda (v) v))))])
(if (null? new-redexes)
'haha
(reduce1 new-redexes (sub1 tick))))]))]))])
(reduce1 (find-redexes exp) tick))))