-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path02-old-toys.ss
executable file
·348 lines (293 loc) · 8.57 KB
/
02-old-toys.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
;
; Chapter 2 of The Reasoned Schemer:
; Teaching Old Toys New Tricks
;
; Code examples assembled by Peteris Krumins ([email protected]).
; 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")
; ---------------------------------------------------------------------------
; This is from The Little Schemer
;
(let ((x (lambda (a) a))
(y 'c))
(x y)) ; 'c
; x and y stay fresh and get reified
;
(run* (r)
(fresh (y x)
(== (cons x (cons y '())) r))) ; '((._0 ._1))
; Same but with different syntax for constructing x y list of freshes
;
(run* (r)
(fresh (y x)
(== `(,x ,y) r))) ; '((._0 ._1))
; x is v and y is w, v and w are fresh
;
(run* (r)
(fresh (v w)
(== (let
((x v)
(y w))
(cons x (cons y '()))) r))) ; '((._0 ._1))
; This is from The Little Schemer
;
(car '(grape raisin pear)) ; 'grape
(car '(a c o r n)) ; 'a
; caro is car in relational programming. It succeeds if it can (cons a d) to
; produce p.
;
(define caro
(lambda (p a)
(fresh (d)
(== (cons a d) p))))
; Let's try out caro.
;
(run* (r)
(caro '(a c o r n) r)) ; '(a) because 'a is car of '(a c o r n)
(run* (r)
(caro '(a c o r n) 'a)
(== #t r)); ; '(#t) because caro succeeds
; caro associates r with x, then assigns 'pear to x, making r equal to 'pear
;
(run* (r)
(fresh (x y)
(caro (cons r (cons y '())) x)
(== 'pear x))) ; '(pear)
; This is from The Little Schemer
;
(cons
(car '(grape raisin pear))
(car '((a) (b) (c)))) ; '(grape a)
(run* (r)
(fresh (x y)
(caro '(grape raisin pear) x)
(caro '((a) (b) (c)) y)
(== (cons x y) r))) ; '((grape a))
; This is from The Little Schemer
;
(cdr '(grape raisin pear)) ; '(raisin pear)
(car (cdr '(a c o r n))) ; 'c
; cdro is cdr in relational programming. It succeeds if it can (cons a d) to
; produce p.
;
(define cdro
(lambda (p d)
(fresh (a)
(== (cons a d) p))))
; Let's try out cdro. The process of transforming (car (cdr l)) into
; (cdro l v) and (caro v r) is called unnesting.
;
(run* (r)
(fresh (v)
(cdro '(a c o r n) v)
(caro v r))) ; '(c)
; This is from The Little Schemer
;
(cons
(cdr '(grape raisin pear))
(car '((a) (b) (c)))) ; '((grape raisin) a)
; Same with run
;
(run* (r)
(fresh (x y)
(cdro '(grape raisin pear) x)
(caro '((a) (b) (c)) y)
(== (cons x y) r))) ; '(((grape raisin) a))
; This succeeds because '(c o r n) is cdr of '(a c o r n)
;
(run* (q)
(cdro '(a c o r n) '(c o r n))
(== #t q)) ; '(#t)
; cdr of '(c o r n) is '(o r n) and that needs to match `(,x r n). That can
; only happen if x is 'o.
;
(run* (x)
(cdro '(c o r n) `(,x r n))) ; '(o)
; cdr of l is '(c o r n), so l must be '(? c o r n).
; car of l is x is ?
; 'a gets assigned to x, making ?='a and l '(a c o r n)
;
(run* (l)
(fresh (x)
(cdro l '(c o r n))
(caro l x)
(== 'a x))) ; '(a c o r n)
; conso!
;
(define conso
(lambda (a d p)
(== (cons a d) p)))
; conso is magnificent
;
(run* (l)
(conso '(a b c) '(d e) l)) ; '((a b c d e))
; x is 'd because we need to find something that prepended to '(a b c) would
; give '(d a b c). The only possibility is that x is 'd.
;
(run* (x)
(conso x '(a b c) '(d a b c))) ; '(d)
; `(,y a ,z c) becomes r, so y is 'e, z is 'd and x is 'c.
; Therefore r becomes '(e a d c)
;
(run* (r)
(fresh (x y z)
(== `(e a d ,x) r)
(conso y `(a ,z c) r))) ; '((e a d c))
; What value can we associate with x so that `(,x a ,x c) is `(d a ,x c)?
; Clearly it's 'd.
;
(run* (x)
(conso x `(a ,x c) `(d a ,x c))) ; '((d))
; Same but different question
;
;
(run* (l)
(fresh (x)
(== `(d a ,x c) l)
(conso x `(a ,x c) l))) ; '((d a d c))
; Same but different question
;
;
(run* (l)
(fresh (x)
(conso x `(a ,x c) l)
(== `(d a ,x c) l))) ; '((d a d c))
; Great puzzle
;
(run* (l)
(fresh (d x y w s)
(conso w '(a n s) s)
(cdro l s)
(caro l x)
(== 'b x)
(cdro l d)
(caro d y)
(== 'e y))) ; '((b e a n s))
; This is from The Little Schemer
;
(null? '(grape raisin pear)) ; #f
(null? '()) ; #t
; nullo!
;
(define nullo
(lambda (l)
(== '() l)))
; Examples of nullo
;
(run* (q)
(nullo '(grape raisin pear))
(== #t q)) ; '() because nullo fails
(run* (q)
(nullo '())
(== #t q)) ; '(#t) because nullo succeeds
; nullo succeeds and associates '() with q
;
(run* (q)
(nullo q)) ; '(()) because nullo succeeds
; This is from The Little Schemer
;
(eq? 'pear 'plum) ; #f
(eq? 'plum 'plum) ; #t
; eqo!
;
(define eqo
(lambda (x y)
(== x y)))
; Examples of eqo
;
(run* (q)
(eqo 'pear 'plum)
(== #t q)) ; '() because eqo fails
(run* (q)
(eqo 'plum 'plum)
(== #t q)) ; '(#t) because eqo succeeds
; This is NOT from The Little Schemer
;
(cons 'split 'pea) ; `(split . pea)
`(split pea) ; `(split . pea)
(pair? `(split . pea)) ; #t
(pair? `((split) . pea)) ; #t
(pair? `()) ; #f
(pair? `pair) ; #f
(pair? `(pear)) ; #t because it's `(pear . ())
(car `(pear)) ; 'pear
(cdr `(pear)) ; '()
(cons `(split) 'pea) ; `((split) . pea)
; Now back to The Reasoned Schemer
;
(run* (r)
(fresh (x y)
(== (cons x (cons y 'salad)) r))) ; `(._0 ._1 . salad)
; pairo!
;
(define pairo
(lambda (p)
(fresh (a d)
(conso a d p))))
; Examples of pairo
;
(run* (q)
(pairo (cons q q))
(== #t q)) ; '(#t) because `(q . q) is a pair
(run* (q)
(pairo '())
(== #t q)) ; '() because '() is not a pair
(run* (q)
(pairo 'pair)
(== #t q)) ; '()
; pairo finds that any two variables make up a pair but since they
; stay fresh, run* reifies them.
;
(run* (x)
(pairo x)) ; '((._0 ._1))
; pairo finds that anything in place of r makes a pair in (cons r 'pear)
;
(run* (r)
(pairo (cons r 'pear))) ; '(._0)
; caro, cdro, pairo can be defined using conso
;
(define caro
(lambda (p a)
(fresh (d)
(conso a d p))))
(define cdro
(lambda (p d)
(fresh (a)
(conso a d p))))
; (define pairo ...) aleady did above
; Test caro and cdro
;
(run* (l)
(fresh (d x y w s)
(conso w '(a n s) s)
(cdro l s)
(caro l x)
(== 'b x)
(cdro l d)
(caro d y)
(== 'e y))) ; '((b e a n s))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; This space reserved for ;
; ;
; "Conso the Mangificento" ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; 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
;