-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path06-the-fun-never-ends.scm
183 lines (151 loc) · 2.64 KB
/
06-the-fun-never-ends.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
(load "mk.scm")
(load "mkextraforms.scm")
(load "05-double-your-fun.scm")
(define S succeed)
(define U fail)
(define anyo
(lambda (g)
(conde
(g S)
(else
(anyo g)))))
(define nevero (anyo U))
;; It has no value:
; (run 1 (q)
; nevero
; (== #t q))
(run 1 (q)
U
nevero) ;; ()
(define alwayso (anyo S))
(run 1 (q)
alwayso
(== #t q)) ;; (#t)
; It has no value.
;; (run* (q)
;; alwayso
;; (== #t q))
(run 5 (q)
alwayso
(== #t q))
;; (#t #t #t #t #t)
(run 5 (q)
(== #t q)
alwayso)
;; (#t #t #t #t #t)
(define salo
(lambda (g)
(conde
(S S)
(else g))))
(run 1 (q)
(salo alwayso)
(== #t q)) ;; (#t)
(run 1 (q)
(salo nevero)
(== #t q)) ;; (#t)
;; It has no value.
; (run* (q)
; (salo nevero)
; (== #t q))
;; It has no value.
; (run 1 (q)
; (salo nevero)
; U
; (== #t q))
;; It has no value.
; (run 1 (q)
; alwayso
; U
; (== #t q))
;; It has no value.
; (run 1 (q)
; (conde
; ((== #f q) alwayso)
; (else (anyo (== #t q))))
; (== #t q))
(run 1 (q)
(condi
((== #f q) alwayso)
(else (== #t q)))
(== #t q))
;; it has no value since the
;; second condi line is out of values.
; (run 2 (q)
; (condi
; ((== #f q) alwayso)
; (else (== #t q)))
; (== #t q))
(run 5 (q)
(condi
((== #f q) alwayso)
(else (anyo (== #t q))))
(== #t q)) ;; (#t #t #t #t #t)
;; condi behaves like conde, except that
;; interleaves the values!
(run 5 (r)
(condi
((teacupo r) S)
((== #f r) S)
(else U))) ;; ('tea #f 'cup)
(run 5 (q)
(condi
((== #f q) alwayso)
((== #t q) alwayso)
(else U))
(== #t q)) ; (#t #t #t #t #t)
; if you use conde, then there's no value.
; (run 5 (q)
; (condi
; (alwayso S)
; (else nevero))
; (== #t q))
;; It has no value
; (run 1 (q)
; (all
; (conde
; ((== #f q) S)
; (else (== #t q)))
; alwayso)
; (== #t q)) ; It has no value.
; alli allows to 'switch' conde behavior.
(run 1 (q)
(alli
(conde
((== #f q) S)
(else (== #t q)))
alwayso)
(== #t q)) ; (#t)
(run 5 (q)
(alli
(conde
((== #f q) S)
(else (== #t q)))
alwayso)
(== #t q)) ; (#t #t #t #t #t)
(run 5 (q)
(alli
(conde
(S (== #t q))
(else (== #f q)))
alwayso)
(== #t q)) ; (#t #t #t #t #t)
(run 5 (q)
(all
(conde
(S S)
(else nevero))
alwayso)
(== #t q)) ; '(#t #t #t #t #t)
; if we replace all for alli, then
; it has no value because we enter
; in the nevero.
;; NOTE: understanding `all`.
(run* (r)
(fresh (a b)
(all
(teacupo a)
(conde
((== b 'blue) S)
(else (== b 'red))))
(== r (list a b))))