-
Notifications
You must be signed in to change notification settings - Fork 1
/
amb.scm
187 lines (158 loc) · 4.52 KB
/
amb.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
;; This software is Copyright (c) Jeronimo C. Pellegrini, 2010-2016.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to:
;; The Free Software Foundation, Inc.,
;; 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
;; amb.scm -- implements the AMB operator
;;
;; SYNOPSIS:
;;; amb
;;;
;; (amb-reset)
;; :: resets the amb tree: this will make amb forget everything.
;;
;; (amb x1 x2 ...)
;; :: add "x1 x2 ..." to the set of answers
;;
;; (amb)
;; :: selects one of the answers
;;
;; (require p)
;; :: selects one of the answers x such that (p x) is #true
;;
;; (amb-list lst)
;; :: same as (amb x1 x2 ...), where lst is (list x1 x2 ...)
;;
;; (amb-all v)
;; :: shows all values that may still be returned by amb in the future
;;
;; if amb has no more solutions to show, it will signal an error
;;; amb+
;;;
;; (amb+-reset)
;; :: resets the amb tree: this will make amb+ forget everything.
;;
;; (amb+ x1 x2 ...)
;; :: add "x1 x2 ..." to the set of answers
;;
;; (amb+)
;; :: selects one of the answers
;;
;; (require+ p)
;; :: selects one of the answers x such that (p x) is #true
;;
;; (amb+-delete-one)
;; :: deletes one element from the amb+ set.
;;
;; (amb+-add k)
;; :: adds k to the amb set
;;
;; (amb+-list lst)
;; :: same as (amb+ x1 x2 ...), where lst is (list x1 x2 ...)
;;
;; (amb+-fail)
;; :: synonym to (amb+)
;;
;; if amb+ has no more solutions to show, it will return #f
(begin
;; called when (amb) is called and there is no more answers available.
(define fail
(lambda ()
(error "Amb tree exhausted")))
(define amb-saved-fail fail)
;; resets the amb tree: this will make amb forget everything.
(define amb-reset
(lambda ()
(set! fail amb-saved-fail)))
(define-syntax amb
(syntax-rules ()
((amb) (fail))
((amb expression) expression)
((amb expression ...)
(let ((saved-fail fail))
(call-with-current-continuation
(lambda (k-success)
(call-with-current-continuation
(lambda (k-failure)
(set! fail (lambda ()
(set! fail saved-fail)
(k-failure 'boo)))
(k-success expression)))
...
(saved-fail)))))))
(define amb-save
(lambda ()
(let ((amb-saved-fail-bak amb-saved-fail)
(amb-fail-bak fail))
(amb-reset)
(list amb-saved-fail-bak amb-fail-bak))))
(define amb-restore
(lambda (lst)
(set! fail (car lst))
(set! amb-saved-fail (cadr lst))))
;; gets the first value from amb that satisfies p.
(define (require p)
(if (not p) (amb)))
;; shows all values available.
(define-syntax amb-all
(syntax-rules ()
((_ e)
(let ((saved-fail fail)
(results '()))
(if (call-with-current-continuation
(lambda (k)
(set! fail (lambda () (k #f)))
(let ((v e))
(set! results (cons v results))
(k #t))))
(fail))
(set! fail saved-fail)
(reverse results)))))
(define now
(lambda ()
(call-with-current-continuation
(lambda (k) (k k)))))
(define amb+-set '())
(define (amb+-reset)
(set! amb+-set '()))
(define (amb+-delete-one)
(set! amb+-set (cdr amb+-set)))
(define (amb+-add k)
(set! amb+-set (cons k amb+-set)))
(define (amb+-fail)
(if (null? amb+-set)
#f;(error "amb+: no more choices!")
(let ((back (car amb+-set)))
(amb+-delete-one)
(back back))))
(define (amb+ . args)
(let ((choices args))
(let ((cc (now)))
(cond ((null? choices) (amb+-fail))
((pair? choices)
(let ((choice (car choices)))
(set! choices (cdr choices))
(amb+-add cc)
choice))
(else (error "amb choices must be a list"))))))
(define (amb+-list lst) (apply amb+ lst))
(define (require+ p)
(if (not p) (amb+)))
(define amb-list
(lambda (lst)
(let loop ((lst lst))
(if (null? lst)
(amb)
(amb (car lst) (loop (cdr lst)))))))
) ;; begin