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
use crate::lterm::LTerm;
use crate::lvalue::LValue;
use crate::user::UserState;
use std::collections::HashMap;
use std::fmt::Debug;
use std::rc::Rc;
mod substitution;
pub use substitution::SMap;
mod unification;
use unification::unify_rec;
pub mod constraint;
pub use constraint::{
BaseConstraint, Constraint, DiseqFdConstraint, DisequalityConstraint, DistinctFdConstraint,
FiniteDomain, LessThanOrEqualFdConstraint, MinusFdConstraint, PlusFdConstraint,
PlusZConstraint, TimesFdConstraint, TimesZConstraint, TreeConstraint, UserConstraint,
};
use constraint::store::ConstraintStore;
mod reification;
pub use reification::reify;
pub type SResult<U> = Result<State<U>, ()>;
#[derive(Debug, Clone)]
pub struct State<U: UserState> {
smap: Rc<SMap>,
cstore: Rc<ConstraintStore<U>>,
dstore: Rc<HashMap<Rc<LTerm>, Rc<FiniteDomain>>>,
pub user_state: U,
}
impl<U: UserState> State<U> {
pub fn new(user_state: U) -> State<U> {
State {
smap: Rc::new(SMap::new()),
cstore: Rc::new(ConstraintStore::new()),
dstore: Rc::new(HashMap::new()),
user_state,
}
}
pub fn smap_ref(&self) -> &SMap {
self.smap.as_ref()
}
pub fn smap_to_mut(&mut self) -> &mut SMap {
Rc::make_mut(&mut self.smap)
}
pub fn with_smap(self, smap: SMap) -> State<U> {
State {
smap: Rc::new(smap),
..self
}
}
pub fn get_smap(&self) -> Rc<SMap> {
Rc::clone(&self.smap)
}
pub fn cstore_ref(&self) -> &ConstraintStore<U> {
self.cstore.as_ref()
}
pub fn cstore_to_mut(&mut self) -> &mut ConstraintStore<U> {
Rc::make_mut(&mut self.cstore)
}
pub fn with_cstore(self, cstore: ConstraintStore<U>) -> State<U> {
State {
cstore: Rc::new(cstore),
..self
}
}
pub fn get_cstore(&self) -> Rc<ConstraintStore<U>> {
Rc::clone(&self.cstore)
}
pub fn dstore_ref(&self) -> &HashMap<Rc<LTerm>, Rc<FiniteDomain>> {
self.dstore.as_ref()
}
pub fn dstore_to_mut(&mut self) -> &mut HashMap<Rc<LTerm>, Rc<FiniteDomain>> {
Rc::make_mut(&mut self.dstore)
}
pub fn with_dstore(self, dstore: HashMap<Rc<LTerm>, Rc<FiniteDomain>>) -> State<U> {
State {
dstore: Rc::new(dstore),
..self
}
}
pub fn get_dstore(&self) -> Rc<HashMap<Rc<LTerm>, Rc<FiniteDomain>>> {
Rc::clone(&self.dstore)
}
pub fn with_constraint<T: Into<Constraint<U>>>(mut self, constraint: T) -> State<U> {
self.cstore_to_mut().push_and_normalize(constraint.into());
self
}
pub fn take_constraint(
mut self,
constraint: &Constraint<U>,
) -> (State<U>, Option<Constraint<U>>) {
match self.cstore_to_mut().take(constraint) {
Some(constraint) => (self, Some(constraint)),
None => (self, None),
}
}
pub fn process_domain(self, x: &Rc<LTerm>, domain: Rc<FiniteDomain>) -> SResult<U> {
match x.as_ref() {
LTerm::Var(_, _) => self.update_var_domain(x, domain),
LTerm::Val(LValue::Number(v)) if domain.contains(*v) => Ok(self),
_ => Err(()),
}
}
fn update_var_domain(self, x: &Rc<LTerm>, domain: Rc<FiniteDomain>) -> SResult<U> {
assert!(x.is_var());
match self.dstore.get(x) {
Some(old_domain) => match old_domain.intersect(domain.as_ref()) {
Some(intersection) => self.resolve_storable_domain(x, Rc::new(intersection)),
None => Err(()),
},
None => self.resolve_storable_domain(x, domain),
}
}
fn resolve_storable_domain(mut self, x: &Rc<LTerm>, domain: Rc<FiniteDomain>) -> SResult<U> {
assert!(x.is_var());
match domain.singleton_value() {
Some(n) => {
self.smap_to_mut()
.extend(Rc::clone(x), Rc::new(LTerm::from(n)));
let _ = self.dstore_to_mut().remove(x);
self.run_constraints()
}
None => {
let _ = self.dstore_to_mut().insert(Rc::clone(x), domain);
Ok(self)
}
}
}
pub fn remove_domain(mut self, x: &Rc<LTerm>) -> SResult<U> {
match self.dstore_to_mut().remove(x) {
Some(_) => Ok(self),
None => Err(()),
}
}
pub fn exclude_from_domain(mut self, x: &Rc<LTerm>, exclude: Rc<FiniteDomain>) -> SResult<U> {
assert!(x.is_list());
let dstore = self.get_dstore();
for y in x.as_ref() {
match dstore.get(y) {
Some(domain) => {
match self.process_domain(&y, Rc::new(domain.diff(exclude.as_ref()).ok_or(())?))
{
Ok(state) => self = state,
Err(error) => return Err(error),
}
}
None => (),
}
}
Ok(self)
}
pub fn run_constraints(mut self) -> SResult<U> {
let mut constraints = self.cstore.iter().cloned().collect::<Vec<Constraint<U>>>();
for constraint in constraints.drain(..) {
self = match self.take_constraint(&constraint) {
(unconstrained_state, Some(constraint)) => {
match constraint.run(unconstrained_state) {
Ok(constrained_state) => constrained_state,
Err(error) => return Err(error),
}
}
(constrained_state, None) => constrained_state,
};
}
Ok(self)
}
fn process_extension_diseq(self, _extension: &SMap) -> SResult<U> {
self.run_constraints()
}
fn process_extension_fd(mut self, extension: &SMap) -> SResult<U> {
let dstore = self.get_dstore();
for (x, v) in extension.iter() {
match dstore.get(x) {
Some(domain) => {
self = self
.process_domain(v, domain.clone())?
.remove_domain(x)?
.run_constraints()?
}
None => {
}
}
}
Ok(self)
}
fn process_extension_user(self, extension: &SMap) -> SResult<U> {
UserState::process_extension(self, extension)
}
fn process_extension(self, extension: SMap) -> SResult<U> {
self.process_extension_diseq(&extension)?
.process_extension_fd(&extension)?
.process_extension_user(&extension)
}
pub fn verify_all_bound(&self) {
for constraint in self.cstore_ref().iter().filter(|c| c.is_finite_domain()) {
for u in &constraint.operands() {
let uwalk = self.smap_ref().walk(u);
if uwalk.is_var() && !self.dstore_ref().contains_key(uwalk) {
panic!(
"Error: Variable {:?} not bound to any domain. {:?}",
u, self
);
}
}
}
}
pub fn unify(mut self, u: &Rc<LTerm>, v: &Rc<LTerm>) -> SResult<U> {
let mut extension = SMap::new();
if unify_rec(&mut self.smap, &mut extension, u, v) {
self.process_extension(extension)
} else {
Err(())
}
}
pub fn disunify(self, u: &Rc<LTerm>, v: &Rc<LTerm>) -> SResult<U> {
let mut extension = SMap::new();
let mut state = self.clone();
if unify_rec(&mut state.smap, &mut extension, u, v) {
if extension.is_empty() {
Err(())
} else {
let c = Rc::new(DisequalityConstraint::from(extension));
Ok(self.with_constraint(c))
}
} else {
Ok(self)
}
}
pub fn reify(&mut self) {
let cstore = self.get_cstore();
for c in cstore.iter() {
c.reify(self);
}
U::reify(self);
}
}