-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.lisp
152 lines (112 loc) · 3.79 KB
/
test.lisp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(in-package #:cl-microkanren)
(setq prove:*default-test-function* #'equalp)
(subtest "walk"
(subtest "nested references"
;; walk should get the actual value of the lvar if it exists
(is (walk (lvar 0)
(ext-s (lvar 0) (lvar 1) (ext-s (lvar 1) 7 nil)))
7)
(is (walk (lvar 0)
(ext-s (lvar 0) (lvar 1)
(ext-s (lvar 1) (lvar 2)
(ext-s (lvar 2) (lvar 3) nil))))
(lvar 3))
(is (walk (lvar 0)
(ext-s (lvar 0) (lvar 1)
(ext-s (lvar 1) 5
(ext-s (lvar 2) (lvar 3) nil))))
5))
(subtest "should treat nil as values"
(is (walk '() (ext-s (lvar 0) '() nil))
'())
(is (walk (lvar 1) (ext-s (lvar 0) (lvar 1)
(ext-s (lvar 1) '() nil)))
'())
(is (walk (lvar 1) nil)
'())
(is (walk (lvar 0) (ext-s (lvar 0) '() nil))
'()))
(subtest "things that are not lvars should be returned"
(is (walk 5 (ext-s (lvar 0) 5 nil))
5)
(is (walk '(123) (ext-s (lvar 0) 5 nil))
'(123))
(is (walk 5 nil)
5)))
(subtest "unify"
(subtest "unify should return false instead of nil when false"
(is (unify 1 2 empty-state)
'false)
(is (unify 1 2 nil)
'false)
(is (unify 1 1 empty-state)
empty-state)
(is (unify #(0) 6 '((#(0) . 5)))
'false)
(is (unify 1 1 '())
'())))
(subtest "=="
(subtest "should return empty list for no match"
(is (call/empty-state (call/fresh (lambda (b) (conj (== b 5) (== b 6)))))
'())
(is (call/empty-state (call/fresh (lambda (b) (conj (== b '())
(== b '())))))
'((((#(0))) . 1)))
(UNIFY #(0) '(#(1) #(2)) '((#(2) 1 2) (#(1))))
(UNIFY '(#(3) . #(5)) '(1 2) '((#(1) #(3) . #(4))))
;; this run:
(run 1 (q) (fresh (x)
(== `(a c ,x r n) q)
(== x '(1 2 3))))
(UNIFY (A C #(1) R N) #(0) NIL)
(WALK (A C #(1) R N) NIL)
(EXT-S #(0) (A C #(1) R N) NIL)
(UNIFY #(1) (1 2 3) ((#(0) A C #(1) R N)))
(WALK #(1) ((#(0) A C #(1) R N)))
(WALK (1 2 3) ((#(0) A C #(1) R N)))
(EXT-S #(1) (1 2 3) ((#(0) A C #(1) R N)))
;; this run:
(run 1 (q) (fresh (x y)
(== `(a c ,x r n) q)
(== `(m j a ,y) x)
(== y '(1 2 3))))
(== (A C #(1) R N) #(0))
(UNIFY (A C #(1) R N) #(0) NIL)
(== (M J A #(2)) #(1))
(UNIFY (M J A #(2)) #(1) ((#(0) A C #(1) R N)))
;; this run:
(is (unify '(A B #(1)) '(A B #(0)) NIL)
'((#(1) . #(0))))
;; the value retuned is _.0
;; it is because #(1) is pointing to #(0)
;; #(0) is pointning to nothing
;; which means it is unbound
(run 1 (q) (fresh (x)
(== `(a b ,x) `(a b ,q))))
(== (A B #(1)) (A B #(0)))
(UNIFY (A B #(1)) (A B #(0)) NIL)
(WALK (A B #(1)) NIL)
(WALK (A B #(0)) NIL)
;; both U & V are pairs in this scenario
(UNIFY A A NIL) ;; returns NIL == empty-state
(UNIFY (B #(1)) (B #(0)) NIL) ;; returns nil
;; because u and v ar pairs, we walk again with the car
(UNIFY B B NIL)
;; ... and the cdr
(UNIFY (#(1)) (#(0)) NIL)
;; which are pairs...
(UNIFY #(1) #(0) NIL)
;; they are both lvars but not equal
(LVAR=? #(1) #(0))
;; so extend the state, pointing #(1) to #(0)
(EXT-S #(1) #(0) NIL)
;; then check the CDR
(UNIFY NIL NIL ((#(1) . #(0))))
;; this returns the state unchanged
;; because (eqv? nil nil) == T
;; this run
(run 1 (q) (fresh (x y)
(== q x)
(== `(a b ,x) `(a b c)) ))
(print "end")))
(finalize)