-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ss
126 lines (113 loc) · 2.99 KB
/
test.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
(load "µkanren.ss")
;;; This file is merely a running set of test cases where I work out
;;; what the current implementation can and can't do. This should guide
;;; me as I modify the kanren-du-jour to something more powerful.
;;; Enlightening me in the process!
(define (nullo l)
(== l '()))
(define (conso a d p)
(== p `(,a . ,d)))
(define (caro p a)
(fresh (d)
(conso a d p)))
(define (cdro p d)
(fresh (a)
(conso a d p)))
(define (listo l)
(conde
[(nullo l)]
[(fresh (d)
(cdro l d)
(listo d))]))
(define (lolo l)
(conde
[(nullo l)]
[(fresh (a d)
(conso a d l)
(listo a)
(lolo d))]))
(define (singletono l)
(fresh (d)
(cdro l d)
(nullo d)))
(define (loso l)
(conde
[(nullo l)]
[(fresh ( a d)
(conso a d l)
(singletono a)
(loso d))]))
(define (appendo l1 l2 out)
(conde
[(nullo l1) (== l2 out)]
[(fresh (a d rest)
(conso a d l1)
(appendo d l2 rest)
(conso a rest out))]))
(define (membero x l)
(conde
[(fresh (a)
(caro l a)
(== a x))]
[(fresh (d)
(cdro l d)
(membero x d))]))
(module ((trun* with-printed-goals)
(trun with-printed-goals))
(define-syntax (with-printed-goals stx)
(syntax-case stx ()
[(_ (g ...) expr ...)
#'(begin
(begin (printf "-> ~a\n" 'g) ...)
expr ...)]))
(define-syntax (trun* stx)
(syntax-case stx ()
[(trun* q g0 g ...)
#'(with-printed-goals (g0 g ...)
(prun* q g0 g ...))]
[(trun* (x ...) g0 g ...)
#'(with-printed-goals (g0 g ...)
(prun* (x ...) g0 g ...))]))
(define-syntax (trun stx)
(syntax-case stx ()
[(trun n q g0 g ...)
#'(with-printed-goals (g0 g ...)
(prun n q g0 g ...))])))
(trun* q (nullo '()))
(trun* q (nullo '(a)))
(trun* l (conso 'the '(quick brown fox) l))
(trun* a (caro '(the quick brown fox) a))
(trun* d (cdro '(the quick brown fox) d))
(trun* q (listo '()))
(trun* q (listo '(foo)))
(trun* q (listo #t))
(trun* q (singletono '(x)))
(trun* q (singletono '()))
(trun* q (loso '()))
(trun* q (loso '((x) (y))))
(trun* q (loso '(() ())))
(trun* q (lolo '()))
(trun* q (lolo '(() () ())))
(trun* q (lolo '(a b c)))
(trun* q (listo '(foo bar baz quux)))
(trun* l (appendo '(the quick brown fox)
'(jumped over the lazy dog)
l))
#;(trun* l1 (appendo l1 ; Currently does not terminate
'(and eggs)
'(hot tea and eggs)))
(trun* l2 (appendo '(hot tea)
l2
'(hot tea and eggs)))
(trun* q (membero 'x '(x)))
(trun* q (membero 'x '(x y)))
(trun* q (membero 'x '(y x)))
(trun* q (membero 'x '()))
(trun* q (membero 'x '(foo bar)))
(trun* x (membero x '(foo bar)))
(trun* y (membero 'foo `(,y bar)))
(trun* y (membero y '()))
(trun* y (membero y '(foo bar baz)))
(trun* y (membero 'foo `(foo ,y baz)))
(trun* (x y) (membero x `(foo ,y baz)))
(trun 5 l (membero 'tofu l))