-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSat.scala
322 lines (286 loc) · 11.6 KB
/
Sat.scala
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
import leon.lang._
import leon.lang.synthesis._
object Sat {
sealed abstract class Formula
case class And(f1: Formula, f2: Formula) extends Formula
case class Or(f1: Formula, f2: Formula) extends Formula
case class Not(f: Formula) extends Formula
case class Var(i: Int) extends Formula
//vars are numbered from 2 to n+1, and Not(Var(n)) is represented as -n. 1 is true and -1 is false
sealed abstract class VarList
case class VarCons(head: Int, tail: VarList) extends VarList
case class VarNil() extends VarList
case class VarLit(value: Boolean) extends VarList
sealed abstract class ClauseList
case class ClauseCons(head: VarList, tail: ClauseList) extends ClauseList
case class ClauseNil() extends ClauseList
case class ClauseLit(value: Boolean) extends ClauseList
def eval(formula: Formula, trueVars: Set[Int]): Boolean = formula match {
case Var(n) => if(n == 1) true else if(n == -1) false else trueVars.contains(n)
case Not(f) => !eval(f, trueVars)
case And(f1, f2) => eval(f1, trueVars) && eval(f2, trueVars)
case Or(f1, f2) => eval(f1, trueVars) || eval(f2, trueVars)
}
//buggy version of eval
def evalWrong(formula: Formula, trueVars: Set[Int]): Boolean = formula match {
case Var(n) => trueVars.contains(n) //bug
case Not(f) => !eval(f, trueVars)
case And(f1, f2) => eval(f1, trueVars) && eval(f2, trueVars)
case Or(f1, f2) => eval(f1, trueVars) || eval(f2, trueVars)
}
def evalCnf(clauses: ClauseList, trueVars: Set[Int]): Boolean = clauses match {
case ClauseCons(cl, cls) => evalClauseCnf(cl, trueVars) && evalCnf(cls, trueVars)
case ClauseNil() => true
case ClauseLit(b) => b
}
def evalDnf(clauses: ClauseList, trueVars: Set[Int]): Boolean = clauses match {
case ClauseCons(cl, cls) => evalClauseDnf(cl, trueVars) || evalDnf(cls, trueVars)
case ClauseNil() => false
case ClauseLit(b) => b
}
//buggy version of evalCnf/Dnf
def evalCnfWrong(clauses: ClauseList, trueVars: Set[Int]): Boolean = clauses match {
case ClauseCons(cl, cls) => evalClauseCnf(cl, trueVars) && evalCnf(cls, trueVars)
case ClauseNil() => false //bug
case ClauseLit(b) => b
}
def evalDnfWrong(clauses: ClauseList, trueVars: Set[Int]): Boolean = clauses match {
case ClauseCons(cl, cls) => evalClauseDnf(cl, trueVars) || evalDnf(cls, trueVars)
case ClauseNil() => true //bug
case ClauseLit(b) => b
}
def evalClauseCnf(clause: VarList, trueVars: Set[Int]): Boolean = clause match {
case VarCons(v, vs) => (if(v < 0) trueVars.contains(-v) else trueVars.contains(v)) || evalClauseCnf(vs, trueVars)
if(v == 1) true
else if(v == -1) evalClauseCnf(vs, trueVars)
else if(v < -1) !trueVars.contains(-v) || evalClauseCnf(vs, trueVars)
else if(v > 1) trueVars.contains(v) || evalClauseCnf(vs, trueVars)
else false
case VarNil() => false
case VarLit(b) => b
}
def evalClauseDnf(clause: VarList, trueVars: Set[Int]): Boolean = clause match {
case VarCons(v, vs) => {
if(v == 1) evalClauseDnf(vs, trueVars)
else if(v == -1) false
else if(v < -1) !trueVars.contains(-v) && evalClauseDnf(vs, trueVars)
else if(v > 1) trueVars.contains(v) && evalClauseDnf(vs, trueVars)
else false
}
case VarNil() => true
case VarLit(b) => b
}
//buggy version of evalClauses
def evalClauseCnfWrong(clause: VarList, trueVars: Set[Int]): Boolean = clause match {
case VarCons(v, vs) => (if(v < 0) trueVars.contains(-v) else trueVars.contains(v)) || evalClauseCnf(vs, trueVars)
if(v == 1) true
else if(v == -1) evalClauseCnf(vs, trueVars)
else if(v < -1) trueVars.contains(-v) || evalClauseCnf(vs, trueVars) //bug
else if(v > 1) trueVars.contains(v) || evalClauseCnf(vs, trueVars)
else false
case VarNil() => false
case VarLit(b) => b
}
def evalClauseDnfWrong(clause: VarList, trueVars: Set[Int]): Boolean = clause match {
case VarCons(v, vs) => {
if(v == 1) evalClauseDnf(vs, trueVars)
else if(v == -1) false
else if(v < -1) trueVars.contains(-v) && evalClauseDnf(vs, trueVars) //bug
else if(v > 1) trueVars.contains(v) && evalClauseDnf(vs, trueVars)
else false
}
case VarNil() => true
case VarLit(b) => b
}
def concatClauses(cll1: ClauseList, cll2: ClauseList): ClauseList = cll1 match {
case ClauseCons(cl, tail) => ClauseCons(cl, concatClauses(tail, cll2))
case ClauseNil() => cll2
case ClauseLit(b) => ClauseCons(VarLit(b), cll2)
}
def concatVars(l1: VarList, l2: VarList): VarList = l1 match {
case VarCons(v, vs) => VarCons(v, concatVars(vs, l2))
case VarNil() => l2
case VarLit(b) => if(b) VarCons(1, l2) else VarCons(-1, l2)
}
def distributeClause(cl: VarList, cll: ClauseList): ClauseList = cll match {
case ClauseCons(cl2, cl2s) => ClauseCons(concatVars(cl, cl2), distributeClause(cl, cl2s))
case ClauseNil() => ClauseNil()
case ClauseLit(b) => if(b) ClauseCons(VarCons(1, cl), ClauseNil()) else ClauseCons(VarCons(-1, cl), ClauseNil())
}
def distribute(cll1: ClauseList, cll2: ClauseList): ClauseList = cll1 match {
case ClauseCons(cl, cls) => concatClauses(distributeClause(cl, cll2), distribute(cls, cll2))
case ClauseNil() => cll2
case ClauseLit(b) => distributeClause(VarLit(b), cll2)
}
def negateClauses(cll: ClauseList): ClauseList = cll match {
case ClauseCons(cl, cls) => ClauseCons(negateVars(cl), negateClauses(cls))
case ClauseNil() => ClauseNil()
case ClauseLit(b) => ClauseLit(!b)
}
def negateVars(lst: VarList): VarList = lst match {
case VarCons(v, vs) => VarCons(-v, negateVars(vs))
case VarNil() => VarNil()
case VarLit(b) => VarLit(!b)
}
def cnfNaive(formula: Formula): ClauseList = formula match {
case And(f1, f2) => {
val cnf1 = cnfNaive(f1)
val cnf2 = cnfNaive(f2)
concatClauses(cnf1, cnf2)
}
case Or(f1, f2) => {
val cnf1 = cnfNaive(f1)
val cnf2 = cnfNaive(f2)
distribute(cnf1, cnf2)
}
case Not(And(f1, f2)) => cnfNaive(Or(Not(f1), Not(f2)))
case Not(Or(f1, f2)) => cnfNaive(And(Not(f1), Not(f2)))
case Not(Not(f)) => cnfNaive(f)
case Not(Var(n)) => ClauseCons(VarCons(-n, VarNil()), ClauseNil())
case Var(n) => ClauseCons(VarCons(n, VarNil()), ClauseNil())
}
def dnfNaive(formula: Formula): ClauseList = formula match {
case And(f1, f2) => {
val dnf1 = dnfNaive(f1)
val dnf2 = dnfNaive(f2)
distribute(dnf1, dnf2)
}
case Or(f1, f2) => {
val dnf1 = dnfNaive(f1)
val dnf2 = dnfNaive(f2)
concatClauses(dnf1, dnf2)
}
case Not(And(f1, f2)) => dnfNaive(Or(Not(f1), Not(f2)))
case Not(Or(f1, f2)) => dnfNaive(And(Not(f1), Not(f2)))
case Not(Not(f)) => dnfNaive(f)
case Not(Var(n)) => ClauseCons(VarCons(-n, VarNil()), ClauseNil())
case Var(n) => ClauseCons(VarCons(n, VarNil()), ClauseNil())
}
def vars(formula: Formula): Set[Int] = formula match {
case Var(n) => Set(n)
case Not(f) => vars(f)
case And(f1, f2) => vars(f1) ++ vars(f2)
case Or(f1, f2) => vars(f1) ++ vars(f2)
}
def isContradictory(clause: VarList, vars: Set[Int]): Boolean = clause match {
case VarCons(v, vs) => vars.contains(-v) || vars.contains(-1) || isContradictory(vs, vars ++ Set(v))
case VarNil() => false
case VarLit(b) => !b
}
def isSatDnf(clauses: ClauseList): Boolean = clauses match {
case ClauseCons(cl, cls) => !isContradictory(cl, Set.empty) || isSatDnf(cls)
case ClauseNil() => false
case ClauseLit(b) => b
}
def simplify(formula: ClauseList): ClauseList = formula match {
case ClauseNil() => ClauseNil()
case ClauseCons(cl, cls) => simplify(cl) match {
case VarNil() => ClauseLit(false)
case VarLit(b) => if(!b) ClauseLit(false) else ClauseCons(VarLit(b), simplify(cls))
case vs => ClauseCons(vs, simplify(cls))
}
case ClauseLit(b) => ClauseLit(b)
}
def simplify(vars: VarList): VarList = vars match {
case VarNil() => VarLit(false)
case VarLit(b) => VarLit(b)
case VarCons(1, vs) => VarLit(true)
case VarCons(-1, vs) => simplify(vs)
case VarCons(v, vs) => VarCons(v, simplify(vs))
}
//for substitute we assume we are dealing with a cnf formula
def substitute(formula: ClauseList, variable: Int, value: Boolean): ClauseList = formula match {
case ClauseNil() => ClauseNil()
case ClauseCons(cl, cls) => ClauseCons(substitute(cl, variable, value), substitute(cls, variable, value))
case ClauseLit(b) => ClauseLit(b)
}
def substitute(vars: VarList, variable: Int, value: Boolean): VarList = vars match {
case VarNil() => VarNil()
case VarLit(b) => VarLit(b)
case VarCons(v, vs) =>
if (v == variable && value) VarLit(true)
else if(v == variable && !value) VarCons(-1, substitute(vs, variable, value))
else if(v == -variable && value) VarCons(-1, substitute(vs, variable, value))
else if(v == -variable && !value) VarLit(true)
else VarCons(v, substitute(vs, variable, value))
}
def choose(formula: ClauseList): Int = formula match {
case ClauseCons(varList, cls) => varList match {
case VarCons(head, vs) => head
case VarNil() => 0
case VarLit(b) => 0
}
case ClauseNil() => 0
case ClauseLit(b) => 0
}
def dpll(formula: ClauseList): Boolean = formula match {
case ClauseNil() => true
case ClauseLit(b) => b
case _ => {
val chosenVar = choose(formula)
val lhs = dpll(simplify(substitute(formula, chosenVar, true)))
val rhs = dpll(simplify(substitute(formula, chosenVar, false)))
lhs || rhs
}
}
def property1(formula: Formula, trueVars: Set[Int]): Boolean = {
val dnfFormula = dnfNaive(formula)
eval(formula, trueVars) == evalDnf(dnfFormula, trueVars)
} holds
def property2(formula: Formula, trueVars: Set[Int]): Boolean = {
val cnfFormula = cnfNaive(formula)
eval(formula, trueVars) == evalCnf(cnfFormula, trueVars)
} holds
def propertyWrong1(formula: Formula, trueVars: Set[Int]): Boolean = {
val dnfFormula = dnfNaive(formula)
isSatDnf(dnfFormula)
} holds
def property3(formula: Formula, trueVars: Set[Int]): Boolean = {
val dnfFormula = dnfNaive(formula)
if(!isSatDnf(dnfFormula)) eval(formula, trueVars) else true
} holds
def property4(formula: Formula): Boolean = {
val cnfFormula = cnfNaive(formula)
val dnfFormula = dnfNaive(formula)
isSatDnf(dnfFormula) == dpll(cnfFormula)
}
def main(args: Array[String]) {
val f1 = And(Var(1), Or(Var(1), Not(Var(2)), Var(3)), Var(2), Not(Var(3)))
val dnff1 = clauses2list(dnfNaive(f1))
val vars1 = vars(f1)
//vars.foreach(v => {
//})
println(f1 + " translated in dnf as:\n\t" + dnff1.mkString("\n\t"))
}
//some non-leon functions to test the program with scala
object False {
def apply(): Formula = And(Var(1), Not(Var(1)))
}
object True {
def apply(): Formula = Or(Var(1), Not(Var(1)))
}
object Or {
def apply(fs: Formula*): Formula = fs match {
case Seq() => False()
case Seq(f) => f
case fs => fs.reduceLeft((f1, f2) => Or(f1, f2))
}
}
object And {
def apply(fs: Formula*): Formula = fs match {
case Seq() => True()
case Seq(f) => f
case fs => fs.reduceLeft((f1, f2) => And(f1, f2))
}
}
def clause2list(cl: VarList): List[Int] = cl match {
case VarCons(v, vs) => v :: clause2list(vs)
case VarNil() => Nil
case VarLit(b) => if(b) List(1) else List(-1)
}
def clauses2list(cll: ClauseList): List[List[Int]] = cll match {
case ClauseCons(cl, cls) => clause2list(cl) :: clauses2list(cls)
case ClauseNil() => Nil
case ClauseLit(b) => if(b) List(List(1)) else List(List(-1))
}
}