-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.scm
99 lines (95 loc) · 3.86 KB
/
test.scm
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
(define (unify-match p1 p2 frame)
(cond ((eq? frame 'failed) 'failed)
((equal? p1 p2) frame)
((var? p1) (extend-if-possible-frame p1 p2 frame))
((var? p2) (extend-if-possible-layer p1 p2 frame)) ; {\em ; ***}
((and (pair? p1) (pair? p2))
(unify-match (cdr p1)
(cdr p2)
(unify-match (car p1)
(car p2)
frame)))
(else 'failed)))
(define (extend-if-possible-frame var val frame)
(let ((binding (binding-in-frame var frame))
(bindlayer (binding-up-frame var frame)))
(cond (binding
(unify-match
(binding-value binding) val frame))
(bindlayer
)
((var? val) ; {\em ; ***}
(let ((binding (binding-in-frame val frame)))
(if binding
(unify-match
var (binding-value binding) frame)
(extend var val frame))))
((depends-on? val var frame) ; {\em ; ***}
'failed)
(else (extend var val frame)))))
(define (extend-if-possible-layer var val frame)
(let ((binding (binding-in-frame var frame)))
(cond (binding
(unify-match
(binding-value binding) val frame))
((var? val) ; {\em ; ***}
(let ((binding (binding-in-frame val frame)))
(if binding
(unify-match
var (binding-value binding) frame)
(extend var val frame))))
((depends-on? val var frame) ; {\em ; ***}
'failed)
(else (extend var val frame)))))
(define (depends-on-frame? exp var frame)
(define (tree-walk e n)
(cond ((and (var? e) (= n 0))
(if (equal? var e)
true
(let ((b (binding-in-frame e frame))
(by (binding-up-frame e frame)))
(cond (and b by)
(or (tree-walk (binding-value b) 1)
(tree-walk (car by) 0)))
(b
(tree-walk (binding-value b) 1))
(by
(tree-walk (car by) 0))
(else false))))
((var? e)
(let ((binding-layer (binding-down-layer e frame)))
(cond ((binding-layer)
(tree-walk (car binding-layer) 1))
(else false))))
((pair? e)
(or (tree-walk (car e) n)
(tree-walk (cdr e) n)))
(else false)))
(tree-walk exp 0))
(define (depends-on-layer? exp var frame)
(define (tree-walk e n)
(cond
((and (var? e) (= n 1))
(if (equal? var e)
true
(let ((binding-layer (binding-down-layer e frame)))
(cond ((binding-layer)
(tree-walk (car binding-layer) 1))
(else false))))
))
((var? e)
(let ((b (binding-in-frame e frame))
(by (binding-up-frame e frame)))
(cond (and b by)
(or (tree-walk (binding-value b) 1)
(tree-walk (car by) 0)))
(b
(tree-walk (binding-value b) 1))
(by
(tree-walk (car by) 0))
(else false))
((pair? e)
(or (tree-walk (car e) n)
(tree-walk (cdr e) n)))
(else false)))
(tree-walk exp 1))