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
|
module XFD.FD where
import List (nubBy)
import qualified Integer as I (abs)
infixl 7 *#
infixl 6 +#, -#
infix 4 =#, /=#, <#, <=#, >#, >=#
infixr 3 /\
infixr 2 \/
domainWPrefix :: String -> Int -> Int -> [FDExpr]
domainWPrefix prf lo up = genFDVars (nameList prefix 1) lo up
where
prefix :: String
prefix = prf ++ (num lo) ++ "_" ++ (num up) ++ "_"
num :: Int -> String
num i = if i < 0 then "-" ++ (show (- i)) else show i
nameList :: String -> Int -> [String]
nameList pr i = (pr ++ (show i)) : nameList pr (i+1)
genFDVars :: [String] -> Int -> Int -> [FDExpr]
genFDVars (n:ns) l u = FDVar n l u _ : genFDVars ns l u
genFDVars [] _ _ = []
domain :: Int -> Int -> [FDExpr]
domain = domainWPrefix "fdv_"
fd :: Int -> FDExpr
fd x = FDInt x
notC :: FDConstr -> FDConstr
notC c = FDNot c
(+#) :: FDExpr -> FDExpr -> FDExpr
x +# y = FDBinExp Plus x y
(-#) :: FDExpr -> FDExpr -> FDExpr
x -# y = FDBinExp Minus x y
(*#) :: FDExpr -> FDExpr -> FDExpr
x *# y = FDBinExp Times x y
(=#) :: FDExpr -> FDExpr -> FDConstr
x =# y = FDRelCon Equ x y
(/=#) :: FDExpr -> FDExpr -> FDConstr
x /=# y = FDRelCon Neq x y
(<#) :: FDExpr -> FDExpr -> FDConstr
x <# y = FDRelCon Lt x y
(<=#) :: FDExpr -> FDExpr -> FDConstr
x <=# y = FDRelCon Leq x y
(>#) :: FDExpr -> FDExpr -> FDConstr
x ># y = FDRelCon Gt x y
(>=#) :: FDExpr -> FDExpr -> FDConstr
x >=# y = FDRelCon Geq x y
true :: FDConstr
true = FDTrue
false :: FDConstr
false = FDFalse
(/\) :: FDConstr -> FDConstr -> FDConstr
c1 /\ c2 = FDAnd c1 c2
(\/) :: FDConstr -> FDConstr -> FDConstr
c1 \/ c2 = FDOr c1 c2
andC :: [FDConstr] -> FDConstr
andC = foldr (/\) true
orC :: [FDConstr] -> FDConstr
orC = foldr (\/) false
allC :: (a -> FDConstr) -> [a] -> FDConstr
allC c = andC . map c
allDifferent :: [FDExpr] -> FDConstr
allDifferent vs = FDAllDiff vs
sum :: [FDExpr] -> FDRel -> FDExpr -> FDConstr
sum vs rel v = FDSum vs rel v
scalarProduct :: [FDExpr] -> [FDExpr] -> FDRel -> FDExpr -> FDConstr
scalarProduct cs vs rel v = FDScalar cs vs rel v
count :: FDExpr -> [FDExpr] -> FDRel -> FDExpr -> FDConstr
count v vs rel c = FDCount v vs rel c
abs :: FDExpr -> FDExpr
abs e = FDAbs e
data FDExpr = FDVar String Int Int Int
| FDInt Int
| FDBinExp FDOp FDExpr FDExpr
| FDAbs FDExpr
data FDOp = Plus | Minus | Times
data FDRel = Equ | Neq | Lt | Leq | Gt | Geq
data FDConstr = FDTrue
| FDFalse
| FDRelCon FDRel FDExpr FDExpr
| FDAnd FDConstr FDConstr
| FDOr FDConstr FDConstr
| FDNot FDConstr
| FDAllDiff [FDExpr]
| FDSum [FDExpr] FDRel FDExpr
| FDScalar [FDExpr] [FDExpr] FDRel FDExpr
| FDCount FDExpr [FDExpr] FDRel FDExpr
getFDVarName :: FDExpr -> String
getFDVarName var = case var of
FDVar n _ _ _ -> n
_ -> error "non FDVar has no name"
getFDVal :: FDExpr -> Int
getFDVal var = case var of
FDVar _ _ _ v -> v
FDInt i -> i
FDBinExp fdop fde1 fde2 -> (arithOp fdop) (valOf fde1) (valOf fde2)
FDAbs e -> getFDVal e
where
valOf e = case e of
FDInt i -> i
FDBinExp op e1 e2 -> (arithOp op) (valOf e1) (valOf e2)
_ -> error $ "FD variable or value expected but FD expression found:\n"++
show e
arithOp Plus = (+)
arithOp Minus = (-)
arithOp Times = (*)
allFDVars :: FDConstr -> [FDExpr]
allFDVars = (nubBy fdVarEq) . allFDVars'
allFDVars' :: FDConstr -> [FDExpr]
allFDVars' FDTrue = []
allFDVars' FDFalse = []
allFDVars' (FDRelCon _ fde1 fde2) = allEFDVars fde1 ++ allEFDVars fde2
allFDVars' (FDAnd c1 c2) = allFDVars' c1 ++ allFDVars' c2
allFDVars' (FDOr c1 c2) = allFDVars' c1 ++ allFDVars' c2
allFDVars' (FDNot c) = allFDVars' c
allFDVars' (FDAllDiff fdvars) = filterVars fdvars
allFDVars' (FDSum fdvars _ fdv) = filterVars (fdvars ++ [fdv])
allFDVars' (FDScalar cs fdvars _ fdv) = filterVars (cs ++ fdvars ++ [fdv])
allFDVars' (FDCount fdv fdvars _ c) = filterVars (fdvars ++ [fdv,c])
filterFDVars :: [FDExpr] -> [FDExpr]
filterFDVars = (nubBy fdVarEq) . filterVars
fdVarEq :: FDExpr -> FDExpr -> Bool
fdVarEq v1 v2 = case (v1, v2) of
((FDVar n1 _ _ _), (FDVar n2 _ _ _)) -> n1 == n2
_ -> False
filterVars :: [FDExpr] -> [FDExpr]
filterVars = concatMap allEFDVars
allEFDVars :: FDExpr -> [FDExpr]
allEFDVars e = case e of
FDVar _ _ _ _ -> [e]
FDInt _ -> []
FDBinExp _ e1 e2 -> allEFDVars e1 ++ allEFDVars e2
FDAbs ex -> allEFDVars ex
|