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
//! Types used for representing Minion models in Rust.

use std::collections::HashMap;

pub type VarName = String;
pub type Tuple = (Constant, Constant);
pub type TwoVars = (Var, Var);

/// A Minion model.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Model {
    pub named_variables: SymbolTable,
    pub constraints: Vec<Constraint>,
}

impl Model {
    /// Creates an empty Minion model.
    pub fn new() -> Model {
        Model {
            named_variables: SymbolTable::new(),
            constraints: Vec::new(),
        }
    }
}

impl Default for Model {
    fn default() -> Self {
        Self::new()
    }
}

/// All supported Minion constraints.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Constraint {
    Difference(TwoVars, Var),
    Div(TwoVars, Var),
    DivUndefZero(TwoVars, Var),
    Modulo(TwoVars, Var),
    ModuloUndefZero(TwoVars, Var),
    Pow(TwoVars, Var),
    Product(TwoVars, Var),
    WeightedSumGeq(Vec<Constant>, Vec<Var>, Var),
    WeightedSumLeq(Vec<Constant>, Vec<Var>, Var),
    CheckAssign(Box<Constraint>),
    CheckGsa(Box<Constraint>),
    ForwardChecking(Box<Constraint>),
    Reify(Box<Constraint>, Var),
    ReifyImply(Box<Constraint>, Var),
    ReifyImplyQuick(Box<Constraint>, Var),
    WatchedAnd(Vec<Constraint>),
    WatchedOr(Vec<Constraint>),
    GacAllDiff(Vec<Var>),
    AllDiff(Vec<Var>),
    AllDiffMatrix(Vec<Var>, Constant),
    WatchSumGeq(Vec<Var>, Constant),
    WatchSumLeq(Vec<Var>, Constant),
    OccurrenceGeq(Vec<Var>, Constant, Constant),
    OccurrenceLeq(Vec<Var>, Constant, Constant),
    Occurrence(Vec<Var>, Constant, Var),
    LitSumGeq(Vec<Var>, Vec<Constant>, Constant),
    Gcc(Vec<Var>, Vec<Constant>, Vec<Var>),
    GccWeak(Vec<Var>, Vec<Constant>, Vec<Var>),
    LexLeqRv(Vec<Var>, Vec<Var>),
    LexLeq(Vec<Var>, Vec<Var>),
    LexLess(Vec<Var>, Vec<Var>),
    LexLeqQuick(Vec<Var>, Vec<Var>),
    LexLessQuick(Vec<Var>, Vec<Var>),
    WatchVecNeq(Vec<Var>, Vec<Var>),
    WatchVecExistsLess(Vec<Var>, Vec<Var>),
    Hamming(Vec<Var>, Vec<Var>, Constant),
    NotHamming(Vec<Var>, Vec<Var>, Constant),
    FrameUpdate(Vec<Var>, Vec<Var>, Vec<Var>, Vec<Var>, Constant),
    //HaggisGac(Vec<Var>,Vec<
    //HaggisGacStable
    //ShortStr2
    //ShortcTupleStr2
    NegativeTable(Vec<Var>, Vec<Tuple>),
    Table(Vec<Var>, Vec<Tuple>),
    GacSchema(Vec<Var>, Vec<Tuple>),
    LightTable(Vec<Var>, Vec<Tuple>),
    Mddc(Vec<Var>, Vec<Tuple>),
    NegativeMddc(Vec<Var>, Vec<Tuple>),
    Str2Plus(Vec<Var>, Var),
    Max(Vec<Var>, Var),
    Min(Vec<Var>, Var),
    NvalueGeq(Vec<Var>, Var),
    NvalueLeq(Vec<Var>, Var),
    SumLeq(Vec<Var>, Var),
    SumGeq(Vec<Var>, Var),
    Element(Vec<Var>, Var, Var),
    ElementOne(Vec<Var>, Var, Var),
    ElementUndefZero(Vec<Var>, Var, Var),
    WatchElement(Vec<Var>, Var, Var),
    WatchElementOne(Vec<Var>, Var, Var),
    WatchElementOneUndefZero(Vec<Var>, Var, Var),
    WatchElementUndefZero(Vec<Var>, Var, Var),
    WLiteral(Var, Constant),
    WNotLiteral(Var, Constant),
    WInIntervalSet(Var, Vec<Constant>),
    WInRange(Var, Vec<Constant>),
    WInset(Var, Vec<Constant>),
    WNotInRange(Var, Vec<Constant>),
    WNotInset(Var, Vec<Constant>),
    Abs(Var, Var),
    DisEq(Var, Var),
    Eq(Var, Var),
    MinusEq(Var, Var),
    GacEq(Var, Var),
    WatchLess(Var, Var),
    WatchNeq(Var, Var),
    Ineq(Var, Var, Constant),
}

/// Representation of a Minion Variable.
///
/// A variable can either be a named variable, or an anomynous "constant as a variable".
///
/// The latter is not stored in the symbol table, or counted in Minions internal list of all
/// variables, but is used to allow the use of a constant in the place of a variable in a
/// constraint.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Var {
    NameRef(VarName),
    ConstantAsVar(i32),
}

/// Representation of a Minion constant.
#[non_exhaustive]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Constant {
    Bool(bool),
    Integer(i32),
}

/// Representation of variable domains.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum VarDomain {
    Bound(i32, i32),
    Discrete(i32, i32),
    SparseBound(i32, i32),
    Bool,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
/// Stores all named variables in a Minion model alongside their domains.
///
/// Named variables referenced in [constraints](Constraint) must be in the symbol table for the
/// model to be valid. In the future, this will raise some sort of type error.
pub struct SymbolTable {
    table: HashMap<VarName, VarDomain>,

    // for now doubles both as Minion's SearchOrder and print order
    var_order: Vec<VarName>,
}

impl SymbolTable {
    fn new() -> SymbolTable {
        SymbolTable {
            table: HashMap::new(),
            var_order: Vec::new(),
        }
    }

    /// Creates a new variable and adds it to the symbol table.
    ///
    /// # Returns
    ///
    /// If a variable already exists with the given name, `None` is returned.
    pub fn add_var(&mut self, name: VarName, vartype: VarDomain) -> Option<()> {
        if self.table.contains_key(&name) {
            return None;
        }

        self.table.insert(name.clone(), vartype);
        self.var_order.push(name);

        Some(())
    }

    /// Gets the domain of a named variable.
    ///
    /// # Returns
    ///
    /// `None` if no variable is known by that name.
    pub fn get_vartype(&self, name: VarName) -> Option<VarDomain> {
        self.table.get(&name).cloned()
    }

    /// Gets the canonical ordering of variables.
    pub fn get_variable_order(&self) -> Vec<VarName> {
        self.var_order.clone()
    }

    pub fn contains(&self, name: VarName) -> bool {
        self.table.contains_key(&name)
    }
}