-
Notifications
You must be signed in to change notification settings - Fork 49
/
zebra.scm
executable file
·326 lines (312 loc) · 10.1 KB
/
zebra.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env chicken-scheme
;; [[file:~/prg/scm/aima/aima.org::*6.7][6\.7:2]]
(use debug
matchable)
(import-for-syntax matchable)
(use define-record-and-printer
srfi-1
srfi-95
list-utils
alist-lib)
(include "~/prg/scm/aima-chicken/aima-csp-core.scm")
(define (neighbors-from-constraints constraints)
(let ((neighbors (make-hash-table)))
(for-each (match-lambda ((x . y)
(hash-table-update!/default
neighbors
x
(lambda (Y) (cons y Y))
'())))
(hash-table-keys constraints))
neighbors))
(define (set-domains! domains variables domain)
(for-each (lambda (variable) (hash-table-set! domains variable domain))
variables))
(define-syntax xor
(lambda (expression rename compare)
(match expression
((_ x y)
(let ((%or (rename 'or))
(%and (rename 'and))
(%not (rename 'not)))
`(,%and (,%or ,x ,y)
(,%not (,%and ,x ,y))))))))
(define (set-pairwise-constraints! constraints X Y relation)
R(for-each
(lambda (x)
(for-each
(lambda (y)
;; Or do we want to merge these with some binary
;; operation?
(hash-table-set! constraints (cons x y) relation))
(delete x Y)))
X))
(define (set-bidirectional-constraint! constraints x y constraint-x constraint-y)
(hash-table-update!/default constraints
(cons x y)
(lambda (constraints-x)
(lambda (x y) (and (constraint-x x y)
(constraints-x x y))))
(lambda (x y) #t))
(hash-table-update!/default constraints
(cons y x)
(lambda (constraints-y)
(lambda (y x) (and (constraint-y y x)
(constraints-y y x))))
(lambda (x y) #t)))
(define (set-pairwise-bidirectional-constraints! constraints X Y constraint-x constraint-y)
(for-each
(lambda (x y)
(set-bidirectional-constraint! constraints x y constraint-x constraint-y))
X
Y))
(define (set-alldiff-constraints! constraints variables)
(set-pairwise-constraints! constraints variables variables neq?))
(let ((domains (make-hash-table))
(constraints (make-hash-table)))
(set-domains! domains
'(n1)
'(norwegian))
(set-domains! domains
'(n2 n3 n4 n5)
'(english spanish japanese ukrainian))
(set-domains! domains
'(p1 p2 p3 p4 p5)
'(dog zebra snail fox horse))
(set-domains! domains
'(s1 s2 s3 s4 s5)
'(smarties snickers milky-way kit-kat hershey))
(set-domains! domains
'(c1 c2 c3 c4 c5)
'(blue yellow ivory red green))
(set-domains! domains
'(d1 d2 d4 d5)
'(water coffee orange-juice tea))
(set-domains! domains
'(d3)
'(milk))
(set-alldiff-constraints! constraints '(n1 n2 n3 n4 n5))
(set-alldiff-constraints! constraints '(d1 d2 d3 d4 d5))
(set-alldiff-constraints! constraints '(c1 c2 c3 c4 c5))
(set-alldiff-constraints! constraints '(s1 s2 s3 s4 s5))
(set-alldiff-constraints! constraints '(p1 p2 p3 p4 p5))
(set-pairwise-bidirectional-constraints!
constraints
'(n1 n2 n3 n4 n5)
'(c1 c2 c3 c4 c5)
(lambda (n c)
(xor (and (eq? n 'english)
(eq? c 'red))
(and (neq? n 'english)
(neq? c 'red))))
(lambda (c n)
(xor (and (eq? n 'english)
(eq? c 'red))
(and (neq? n 'english)
(neq? c 'red)))))
(set-pairwise-bidirectional-constraints!
constraints
'(n1 n2 n3 n4 n5)
'(p1 p2 p3 p4 p5)
(lambda (n p)
(xor (and (eq? n 'spanish)
(eq? p 'dog))
(and (neq? n 'spanish)
(neq? p 'dog))))
(lambda (p n)
(xor (and (eq? n 'spanish)
(eq? p 'dog))
(and (neq? n 'spanish)
(neq? p 'dog)))))
(set-pairwise-bidirectional-constraints!
constraints
'(c1 c1 c1 c2 c2 c3 c3 c4 c4 c5 c5 c5)
'(c3 c4 c5 c4 c5 c1 c5 c1 c2 c1 c2 c3)
(lambda (c1 c2)
(not (or (and (eq? c1 'ivory)
(eq? c2 'green))
(and (eq? c2 'ivory)
(eq? c1 'green)))))
(lambda (c2 c1)
(not (or (and (eq? c1 'ivory)
(eq? c2 'green))
(and (eq? c2 'ivory)
(eq? c1 'green))))))
(set-pairwise-bidirectional-constraints!
constraints
'(c1 c2 c3 c4)
'(c2 c3 c4 c5)
(lambda (left right)
(if (and (or (eq? right 'ivory)
(eq? left 'ivory))
(or (eq? right 'green)
(eq? left 'green)))
(and (eq? left 'ivory)
(eq? right 'green))
#t))
(lambda (right left)
(if (and (or (eq? right 'ivory)
(eq? left 'ivory))
(or (eq? right 'green)
(eq? left 'green)))
(and (eq? left 'ivory)
(eq? right 'green))
#t)))
;; (set-pairwise-bidirectional-constraints!
;; constraints
;; '(s1 s2 s2 s3 s3 s4 s4 s5)
;; '(p2 p1 p3 p2 p4 p3 p5 p4)
;; (lambda (s p)
;; (not (and (eq? s 'hershey)
;; (neq? p 'fox))))
;; (lambda (p s)
;; (not (and (eq? s 'hershey)
;; (neq? p 'fox)))))
(set-pairwise-bidirectional-constraints!
constraints
'(s1 s1 s1 s1 s2 s2 s2 s3 s3 s3 s4 s4 s4 s5 s5 s5 s5)
'(p1 p3 p4 p5 p2 p4 p5 p3 p1 p5 p4 p1 p2 p5 p1 p2 p3)
(lambda (s p)
(not (or (and (eq? s 'hershey)
(eq? p 'fox))
(and (eq? s 'kit-kat)
(eq? p 'horse)))))
(lambda (p s)
(not (or (and (eq? s 'hershey)
(eq? p 'fox))
(and (eq? s 'kit-kat)
(eq? p 'horse))))))
(set-pairwise-bidirectional-constraints!
constraints
'(s1 s2 s3 s4 s5)
'(c1 c2 c3 c4 c5)
(lambda (s c)
(xor (and (eq? s 'kit-kat)
(eq? c 'yellow))
(and (neq? s 'kit-kat)
(neq? c 'yellow))))
(lambda (c s)
(xor (and (eq? s 'kit-kat)
(eq? c 'yellow))
(and (neq? s 'kit-kat)
(neq? c 'yellow)))))
;; (set-pairwise-bidirectional-constraints!
;; constraints
;; '(n1 n2 n2 n3 n3 n4 n4 n5)
;; '(c2 c1 c3 c2 c4 c3 c5 c4)
;; (lambda (n c)
;; (not (and (eq? n 'norwegian)
;; (neq? c 'blue))))
;; (lambda (c n)
;; (not (and (eq? c 'blue)
;; (neq? n 'norwegian)))))
(set-pairwise-bidirectional-constraints!
constraints
'(n1 n1 n1 n1 n2 n2 n2 n3 n3 n3 n4 n4 n4 n5 n5 n5 n5)
'(c1 c3 c4 c5 c2 c4 c5 c3 c1 c5 c4 c1 c2 c5 c1 c2 c3)
(lambda (n c)
(not (and (eq? n 'norwegian)
(eq? c 'blue))))
(lambda (c n)
(not (and (eq? c 'blue)
(eq? n 'norwegian)))))
(set-pairwise-bidirectional-constraints!
constraints
'(s1 s2 s3 s4 s5)
'(p1 p2 p3 p4 p5)
(lambda (s p)
(xor (and (eq? s 'smarties)
(eq? p 'snail))
(and (neq? s 'smarties)
(neq? p 'snail))))
(lambda (p s)
(xor (and (eq? s 'smarties)
(eq? p 'snail))
(and (neq? s 'smarties)
(neq? p 'snail)))))
(set-pairwise-bidirectional-constraints!
constraints
'(s1 s2 s3 s4 s5)
'(d1 d2 d3 d4 d5)
(lambda (s d)
(xor (and (eq? s 'snickers)
(eq? d 'orange-juice))
(and (neq? s 'snickers)
(neq? d 'orange-juice))))
(lambda (d s)
(xor (and (eq? s 'snickers)
(eq? d 'orange-juice))
(and (neq? s 'snickers)
(neq? d 'orange-juice)))))
(set-pairwise-bidirectional-constraints!
constraints
'(n1 n2 n3 n4 n5)
'(d1 d2 d3 d4 d5)
(lambda (n d)
(xor (and (eq? n 'ukrainian)
(eq? d 'tea))
(and (neq? n 'ukrainian)
(neq? d 'tea))))
(lambda (d n)
(xor (and (eq? n 'ukrainian)
(eq? d 'tea))
(and (neq? n 'ukrainian)
(neq? d 'tea)))))
(set-pairwise-bidirectional-constraints!
constraints
'(n1 n2 n3 n4 n5)
'(s1 s2 s3 s4 s5)
(lambda (n s)
(xor (and (eq? n 'japanese)
(eq? s 'milky-way))
(and (neq? n 'japanese)
(neq? s 'milky-way))))
(lambda (s n)
(xor (and (eq? n 'japanese)
(eq? s 'milky-way))
(and (neq? n 'japanese)
(neq? s 'milky-way)))))
;; (set-pairwise-bidirectional-constraints!
;; constraints
;; '(s1 s1 s1 s2 s2 s3 s3 s4 s4)
;; '(p3 p4 p5 p4 p5 p1 p5 p1 p2)
;; (lambda (s p)
;; (not (and (eq? s 'hershey)
;; (eq? p 'fox))))
;; (lambda (p s)
;; (not (and (eq? s 'hershey)
;; (eq? p 'fox)))))
;; (set-pairwise-bidirectional-constraints!
;; constraints
;; '(s1 s2 s2 s3 s3 s4 s4 s5)
;; '(p2 p1 p3 p2 p4 p3 p5 p4)
;; (lambda (s p)
;; (not (and (eq? s 'kit-kat)
;; (neq? p 'horse))))
;; (lambda (p s)
;; (not (and (eq? p 'horse)
;; (neq? s 'kit-kat)))))
(set-pairwise-bidirectional-constraints!
constraints
'(d1 d2 d3 d4 d5)
'(c1 c2 c3 c4 c5)
(lambda (d c)
(xor (and (eq? d 'coffee)
(eq? c 'green))
(and (neq? d 'coffee)
(neq? c 'green))))
(lambda (c d)
(xor (and (eq? d 'coffee)
(eq? c 'green))
(and (neq? d 'coffee)
(neq? c 'green)))))
(let* ((neighbors (neighbors-from-constraints constraints))
(csp (make-csp domains constraints neighbors))
(result (backtracking-search csp)))
;; (debug (hash-table->alist neighbors))
;; (debug (hash-table->alist constraints))
(unless (failure? result)
(debug (sort (hash-table->alist (backtracking-search csp))
string<?
(compose symbol->string car))))))
;; 6\.7:2 ends here