forked from qobi/R6RS-AD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nondeterministic-constraints.ss
169 lines (147 loc) · 4.81 KB
/
nondeterministic-constraints.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
#!r6rs
(library
(nondeterministic-constraints)
(export set-nondeterministic-strategy! create-domain-variable
binding nondeterministic-solution assert-nondeterministic-constraint!)
(import (rnrs) (QobiScheme) (nondeterministic-scheme))
(define *strategy* #f)
(define-record-type domain-variable
(fields (mutable domain) (mutable demons)))
(define (set-nondeterministic-strategy! strategy) (set! *strategy* strategy))
(define (domain-variable-domain-local-set! domain-variable domain)
(let ((domain (domain-variable-domain domain-variable)))
(upon-fail (domain-variable-domain-set! domain-variable domain)))
(domain-variable-domain-set! domain-variable domain))
(define (domain-variable-demons-local-set! domain-variable demons)
(let ((demons (domain-variable-demons domain-variable)))
(upon-fail (domain-variable-demons-set! domain-variable demons)))
(domain-variable-demons-set! domain-variable demons))
(define (create-domain-variable domain)
(let ((domain (remove-duplicates domain)))
(when (null? domain) (fail))
(make-domain-variable domain '())))
(define (restrict-domain! d domain)
(when (null? domain) (fail))
(when (< (length domain) (length (domain-variable-domain d)))
(domain-variable-domain-local-set! d domain)
(for-each (lambda (demon) (demon)) (domain-variable-demons d))))
(define (bound? d) (null? (rest (domain-variable-domain d))))
(define (binding d) (first (domain-variable-domain d)))
(define (nondeterministic-solution ds)
(let loop ((ds ds) (xs '()))
(if (null? ds)
(reverse xs)
(let ((x (a-member-of (domain-variable-domain (first ds)))))
(restrict-domain! (first ds) (list x))
(loop (rest ds) (cons x xs))))))
(define (some-element predicate d)
(some (lambda (x) (predicate x)) (domain-variable-domain d)))
(define (one-element predicate d)
(one (lambda (x) (predicate x)) (domain-variable-domain d)))
(define (the-element predicate d)
(list (find-if (lambda (x) (predicate x)) (domain-variable-domain d))))
(define (the-elements predicate d)
(remove-if-not (lambda (x) (predicate x)) (domain-variable-domain d)))
(define (attach-demon! demon d)
(domain-variable-demons-local-set! d (cons demon (domain-variable-demons d)))
(demon))
(define (assert-constraint-efd! constraint ds)
(for-each
(lambda (d)
(attach-demon! (lambda ()
(when (every bound? ds)
(unless (apply constraint (map binding ds)) (fail))))
d))
ds))
(define (assert-constraint-fc! constraint ds)
(for-each
(lambda (d)
(attach-demon!
(lambda ()
(when (one (lambda (d) (not (bound? d))) ds)
(let* ((i (position-if (lambda (d) (not (bound? d))) ds))
(d (list-ref ds i)))
(unless (some-element
(lambda (x)
(apply
constraint
(map-indexed (lambda (d j) (if (= j i) x (binding d))) ds)))
d)
(fail)))))
d))
ds))
(define (assert-constraint-vp! constraint ds)
(for-each
(lambda (d)
(attach-demon!
(lambda ()
(when (one (lambda (d) (not (bound? d))) ds)
(let* ((i (position-if (lambda (d) (not (bound? d))) ds))
(d (list-ref ds i)))
(when (one-element
(lambda (x)
(apply
constraint
(map-indexed (lambda (d j) (if (= j i) x (binding d))) ds)))
d)
(restrict-domain!
d
(the-element
(lambda (x)
(apply constraint
(map-indexed (lambda (d j) (if (= j i) x (binding d))) ds)))
d))))))
d))
ds))
(define (assert-constraint-gfc! constraint ds)
(for-each
(lambda (d)
(attach-demon!
(lambda ()
(when (one (lambda (d) (not (bound? d))) ds)
(let* ((i (position-if (lambda (d) (not (bound? d))) ds))
(d (list-ref ds i)))
(restrict-domain!
d
(the-elements
(lambda (x)
(apply constraint
(map-indexed (lambda (d j) (if (= j i) x (binding d))) ds)))
d)))))
d))
ds))
(define (assert-constraint-ac! constraint ds)
(for-each
(lambda (d)
(attach-demon!
(lambda ()
(for-each-indexed
(lambda (d i)
(restrict-domain!
d
(the-elements
(lambda (x)
(let loop ((ds ds) (xs '()) (j 0))
(if (null? ds)
(apply constraint (reverse xs))
(if (= j i)
(loop (rest ds) (cons x xs) (+ j 1))
(some-element
(lambda (x) (loop (rest ds) (cons x xs) (+ j 1)))
(first ds))))))
d)))
ds))
d))
ds))
(define (assert-nondeterministic-constraint! constraint . ds)
(case *strategy*
((efd) (assert-constraint-efd! constraint ds))
((fc) (assert-constraint-efd! constraint ds)
(assert-constraint-fc! constraint ds))
((vp) (assert-constraint-efd! constraint ds)
(assert-constraint-fc! constraint ds)
(assert-constraint-vp! constraint ds))
((gfc) (assert-constraint-efd! constraint ds)
(assert-constraint-gfc! constraint ds))
((ac) (assert-constraint-ac! constraint ds))
(else (error #f "Unrecognized strategy")))))