-
Notifications
You must be signed in to change notification settings - Fork 17
/
01-playthings.ss
executable file
·392 lines (339 loc) · 9.79 KB
/
01-playthings.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
;
; Chapter 1 of The Reasoned Schemer:
; Playthings
;
; Code examples assembled by Peteris Krumins (peter@catonmat.net).
; His blog is at http://www.catonmat.net -- good coders code, great reuse.
;
; Get yourself this wonderful book at Amazon: http://bit.ly/89tulL
;
;
; You'll have to get Oleg Kiselyov's implementation of this logic programming
; system to run the examples in this file. The implementation is here:
; http://sourceforge.net/projects/kanren/
;
(load "mk.scm")
(load "mkextraforms.scm")
(define U fail)
(define S succeed)
; ---------------------------------------------------------------------------
; Expression (run* (q) g ...) has the value '() if goals `g ...` fail.
; Goal U (#u) fails.
;
(run* (q) U) ; '()
; If the variable q is fresh, (== #t q) succeeds associating #t with q.
; == is called the unify operator.
; Variable q is fresh here.
;
(run* (q)
(== #t q)) ; '(#t)
; Goal U fails.
;
(run* (q)
U
(== #t q)) ; '()
; Goals S and (== #s q) succeed, therefore q gets associated with #t.
;
(run* (q)
S
(== #t q)) ; '(#t)
; S and (== 'corn q) succeeds, therefore 'corn gets associated
; with the fresh variable q.
;
(run* (q)
S
(== 'corn q)) ; '(corn)
; U fails, therefore the value of (run* ...) is '()
;
(run* (q)
U
(== 'corn q)) ; '()
; S succeeds and (== #f q) associates #f with q.
; (run* ...) returns a nonempty list if its goals succeed.
;
(run* (q)
S
(== #f q)) ; '(#f)
; (== #f x) fails because x is #t and #f is not equal to #t
;
(run* (q)
(let ((x #t))
(== #f x))) ; '()
; (== #f x) succeeds because x is #f and #f is equal to #f
;
(run* (q)
(let ((x #f))
(== #f x))) ; '(._0)
; (fresh (x ...) g ...) introduces fresh variables `x ...` and succeeds
; if goals `g ...` succeed.
;
(run* (q)
(fresh (x)
(== #t x)
(== #t q))) ; '(#t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The law of fresh: ;
; ;
; If x is fresh, then (== v x) succeeds and associates x with v. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; (== #t x) is the same as (== x #t)
;
(run* (q)
(fresh (x)
(== x #t)
(== #t q))) ; '(#t)
; (== #t q) is the same as (== q #t)
;
(run* (q)
(fresh (x)
(== x #t)
(== q #t))) ; '(#t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The law of ==: ;
; ;
; (== v w) is the same as (== w v). ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; q stays fresh after running, gets reified.
;
(run* (q)
S) ; '(._0)
; x in (run* (x) ...) stays fresh, gets reified.
; Only the (fresh (x) ...)'s x gets associated with #t (different scope).
;
(run* (x)
(let ((x #f))
(fresh (x)
(== #t x)))) ; '(._0)
; Fresh variables (x y) get associated with r. They get reified.
;
(run* (r)
(fresh (x y)
(== (cons x (cons y '())) r))) ; ((._0 ._1))
; Same as previous, only x now is t and y is u.
;
(run* (s)
(fresh (t u)
(== (cons t (cons u '())) s))) ; ((._0 ._1))
; (y x y) get reified.
;
(run* (r)
(fresh (x)
(let ((y x))
(fresh (x)
(== (cons y (cons x (cons y '()))) r)))))
; ==> '((_.o _.1 _.0))
; (x y x) get reified. Reifying r's value reifies the fresh variables
; in order in which they appear in the list.
;
(run* (r)
(fresh (x)
(let ((y x))
(fresh (x)
(== (cons x (cons y (cons x '()))) r)))))
; ==> '((_.o _.1 _.0))
; The first goal (== #f q) succeeds, associating #f with q.
; #t can't then be associated with q in the next goal (== #t q), since
; q is no longer fresh.
;
(run* (q)
(== #f q)
(== #t q)) ; '()
; Succeeds because in the second goal #f is already associated with q.
;
(run* (q)
(== #f q)
(== #f q)) ; '(#f)
; x and q are the same.
;
(run* (q)
(let ((x q))
(== #t x))) ; '(#t)
; r stays fresh. We say x and r co-refer or share.
;
(run* (r)
(fresh (x)
(== x r))) ; '(._0)
; q gets x's association and x got associated with #t before.
;
(run* (q)
(fresh (x)
(== #t x)
(== x q))) ; '(#t)
; x and q co-refer, then x gets associated with #t that makes q associated
; with #t.
(run* (q)
(fresh (x)
(== x q)
(== #t x))) ; '(#t)
; x and q are different variables
;
(run* (q)
(fresh (x)
(== (eq? x q) q))) ; '(#f)
; x and q are different variables
;
(run* (q)
(let ((x q))
(fresh (q)
(== (eq? x q) x)))) ; '(#f)
; Remember cond from The Little Schemer?
;
(cond
(#f #t)
(else #f)) ; #f
; Remember cond from The Little Schemer?
;
(cond
(#f S)
(else U)) ; fails
; conde is the default control mechanism of Prolog.
; e stands for "every line".
;
(run* (x)
(conde
((== 'olive x) S)
((== 'oil x) S)
(else U))) ; '(olive oil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The law of conde: ;
; ;
; To get more values from conde, pretend that the successful conde line has ;
; failed, refreshing all variables that got an association from that line. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; run1 produces at most one value.
;
(run 1 (x)
(conde
((== 'olive x) S)
((== 'oil x) S)
(else U))) ; '(olive)
; (S S) leaves x fresh as x was refreshed on the previous line.
;
(run* (x)
(conde
((== 'virgin x) U)
((== 'olive x) S)
(S S)
((== 'oil x) S)
(else U))) ; '(olive ._0 oil)
; Had we run (run* (x) ...), we'd have gotten '(extra olive oil).
;
(run 2 (x)
(conde
((== 'extra x) S)
((== 'virgin x) U)
((== 'olive x) S)
((== 'oil x) S)
(else U))) ; '(extra olive)
; We already knew that.
;
(run* (r)
(fresh (x y)
(== 'split x)
(== 'pea y)
(== (cons x (cons y '())) r))) ; '((split pea))
; Didn't know this, but you'll have to figure it out.
;
(run* (r)
(fresh (x y)
(conde
((== 'split x) (== 'pea y))
((== 'navy x) (== 'bean y))
(else U))
(== (cons x (cons y '())) r))) ; '((split pea) (navy bean))
; This is interesting.
;
(run* (r)
(fresh (x y)
(conde
((== 'split x) (== 'pea y))
((== 'navy x) (== 'bean y))
(else U))
(== (cons x (cons y (cons 'soup '()))) r)))
; ==> '((split pea soup) (navy bean soup))
; A tea cup
;
(define teacupo
(lambda (x)
(conde
((== 'tea x) S)
((== 'cup x) S)
(else U))))
; Let's test out the tea cup
;
(run* (x)
(teacupo x)) ; '(tea cup)
; This is difficult.
;
(run* (r)
(fresh (x y)
(conde
((teacupo x) (== #t y) S)
((== #f x) (== #t y))
(else U))
(== (cons x (cons y '())) r)))
; ==> '((tea #t) (cup #t) (#f #t))
; Food for thought.
;
(run* (r)
(fresh (x y z)
(conde
((== y x) (fresh (x) (== z x)))
((fresh (x) (== y x)) (== z x))
(else U))
(== (cons y (cons z '())) r)))
; ==> '((._0 ._1) (._0 ._1))
; Shows that the two occurrences of ._0 in the previous example represent
; different variables.
;
(run* (r)
(fresh (x y z)
(conde
((== y x) (fresh (x) (== z x)))
((fresh (x) (== y x)) (== z x))
(else U))
(== #f x)
(== (cons y (cons z '())) r)))
; ==> '((#f ._0) (._0 #f))
; I am unsure about this. Since the first line of let associates
; #t with q, the second line can't associate #f with q anymore. Not sure
; how it associated and succeeded.
;
(run* (q)
(let ((a (== #t q))
(b (== #f q)))
b))
; ==> '(#f)
; Also unsure about this for the same reason.
;
(run* (q)
(let ((a (== #t q))
(b (fresh (x)
(== x q)
(== #f x)))
(c (conde
((== #t q) S)
(else (== #f q)))))
b))
; ==> '(#f)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;.
; ;
; This space reserved for ;
; JAM STAINS! ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'
;
; Go get yourself this wonderful book and have fun with logic programming!
;
; Shortened URL to the book at Amazon.com: http://bit.ly/89tulL
;
; Sincerely,
; Peteris Krumins
; http://www.catonmat.net
;