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>, ()>;

/// Logic program state
///
/// The `State` structure represents a state of the search. A logic program consists of goals,
/// which when applied to states, produce streams of states. Each state is a solution to a
/// (part of) logic program. The `State` can be cloned and each clone can be modified independently
/// of each other; the data structures within `State` are clone-on-write.
///
/// A state has four separate data storages that are clone-on-write:
///    1. The current substitution of LTerms
///    2. The constraint store
///    3. The domain store
///    4. User data
#[derive(Debug, Clone)]
pub struct State<U: UserState> {
    /// The substitution map
    smap: Rc<SMap>,

    /// The constraint store
    cstore: Rc<ConstraintStore<U>>,

    /// The domain store
    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,
        }
    }

    /// Return a reference to the substition map of the 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)
    }

    /// Returns the state with replaced substitution map
    pub fn with_smap(self, smap: SMap) -> State<U> {
        State {
            smap: Rc::new(smap),
            ..self
        }
    }

    /// Get a cloned reference to the substitution map of the state
    pub fn get_smap(&self) -> Rc<SMap> {
        Rc::clone(&self.smap)
    }

    /// Return a reference to the constraint store of the state
    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)
    }

    /// Returns the state with replaced with a new constraint store. The old store is dropped.
    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)
    }

    /// Return a reference to the domain store of the state
    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
        }
    }

    /// Get a cloned reference to the domain store fo the state
    pub fn get_dstore(&self) -> Rc<HashMap<Rc<LTerm>, Rc<FiniteDomain>>> {
        Rc::clone(&self.dstore)
    }

    /// Return the state with a new constraint
    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),
        }
    }

    /// Adds a new domain constraint for a variable `x`; or if the term is a value, then
    /// checks that the value is within the domain. If new domain constraint is added for a
    /// variable, it is updated to the domain store.
    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(()),
        }
    }

    /// Updates domain constraint of a variable `x`.
    ///
    /// If variable does not have an existing domain, then it is given the `domain`.
    ///
    /// If the variable `x` is already constrained, then the resulting constraint is such that it
    /// fulfills both the old and the new constraint; i.e. it is an intersection of the domains.
    /// If the domains are disjoint, the constraint fails and `None` is returned.
    ///
    /// Note: if domains are resolved into singletons, then they are converted into value
    ///       kind LTerms.
    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(()), /* disjoint domains */
            },
            None => self.resolve_storable_domain(x, domain),
        }
    }

    /// Stores a new `domain` for a variable `x` by updating the corresponding domain information
    /// of the state. Any existing domain information is replaced with the new.
    ///
    /// If the domain is a singleton, i.e. a single value, it is converted into a constant value
    /// instead, by creating a new constant from the singleton value and extending the
    /// substitution to map from the variable `x` to the newly created constant.
    fn resolve_storable_domain(mut self, x: &Rc<LTerm>, domain: Rc<FiniteDomain>) -> SResult<U> {
        assert!(x.is_var());
        match domain.singleton_value() {
            Some(n) => {
                // Extend substitution from `x` to the singleton value `n`
                self.smap_to_mut()
                    .extend(Rc::clone(x), Rc::new(LTerm::from(n)));

                // Remove domain information from store
                let _ = self.dstore_to_mut().remove(x);

                // The substitution has been modified, re-run constraints.
                self.run_constraints()
            }
            None => {
                // Extend or update domain store with the given `domain`
                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(()),
        }
    }

    // Removes domain `exclude` from the domain of all variables in list `x`.
    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)
    }

    /// Runs all constraints from the constraint store on the current state. If any of the
    /// constraints fail, `None` is returned. Otherwise the state is returned with an updated
    /// constraint store.
    pub fn run_constraints(mut self) -> SResult<U> {
        let mut constraints = self.cstore.iter().cloned().collect::<Vec<Constraint<U>>>();

        // Each constraint is first removed from the store and then run against the state.
        // If the constraint does not want to be removed from the store, it adds itself
        // back when it is run.
        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, /* Constraint has removed itself. */
            };
        }

        Ok(self)
    }

    /// Processes extension for disequality constraints.
    fn process_extension_diseq(self, _extension: &SMap) -> SResult<U> {
        self.run_constraints()
    }

    /// Processes extension for finite domain constraints.
    ///
    /// For each new substitution we need to add a new domain constraint. If variable `x`
    /// is substituted with term `v`, then `x` and `v` must have domains with non-zero
    /// intersection. If variable `x` has a domain, then the domain is assigned to the
    /// term `v` as well.
    ///
    /// If all substitutions are successful domain constraints, a state with updated
    /// domain- and constraint-stores is returned.
    ///
    /// If the resulting intersection domain is non-zero, the
    /// substitution is not possible, the constraint fails and `None` is returned.
    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 => {
                    // No domain information found in store for `x`.
                }
            }
        }
        Ok(self)
    }

    fn process_extension_user(self, extension: &SMap) -> SResult<U> {
        UserState::process_extension(self, extension)
    }

    /// Processes the extension to substitution
    ///
    /// The extension to substitution consists of all substitutions added in a single
    /// unification. It consists of the substitutions had to be added in order to unify
    /// the two terms.
    fn process_extension(self, extension: SMap) -> SResult<U> {
        self.process_extension_diseq(&extension)?
            .process_extension_fd(&extension)?
            .process_extension_user(&extension)
    }

    /// Verifies that all variables constrained by domain constraints have domains
    /// associated with them.
    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> {
        // Extension will contain all substitutions added in the recursive unification of the terms
        let mut extension = SMap::new();
        if unify_rec(&mut self.smap, &mut extension, u, v) {
            self.process_extension(extension)
        } else {
            Err(())
        }
    }

    /// Add disequality constraint
    pub fn disunify(self, u: &Rc<LTerm>, v: &Rc<LTerm>) -> SResult<U> {
        // Disunification is implemented in terms of unification
        let mut extension = SMap::new();
        let mut state = self.clone();
        if unify_rec(&mut state.smap, &mut extension, u, v) {
            if extension.is_empty() {
                // Unification succeeded without extending the current substitution, therefore
                // disequality constraint fails.
                Err(())
            } else {
                // Unification succeeded with extended substitution map. Instead of adding the
                // substitutions to the state, we add corresponding constraint to disequality
                // constraint store, against which later unifications will be verified.
                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);
    }
}