-
Notifications
You must be signed in to change notification settings - Fork 10
/
State.fs
217 lines (186 loc) · 7.88 KB
/
State.fs
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
module State
open GlobalOptions
open Learning
open Util
open Literal
open Clause
open Trail
open Database
open WatchManager
open Z3Check
open BooleanValuation
open BitVectorValuation
open BoundsValuation
open VariableOrder
open Stats
type Answer =
| Sat
| Unsat
| Unknown
type State (maxVar:int,
tr:Ref<Trail>,
tBoolValuation:Ref<BooleanValuation>,
tRLEValuation:Ref<BitVectorValuation>,
tBndsValutation:Ref<BoundsValuation>,
db:Ref<Database>) =
member val trail = tr
member val bVal = tBoolValuation
member val bvVal = tRLEValuation
member val bounds = tBndsValutation
member val database = db
member val numeralDB = (!db).Numerals
member val variableDB = (!db).Variables
member val clauseDB = (!db).Clauses
member val theoryDB = (!db).Theory
member val watchManager = (!db).Watches
member val private inTempMode = false with get, set
member val private answer = Unknown with get, set
member val private conflict : Ref<Clause> Option = None with get, set
member val private oldFlags = None with get, set
member val boundsEnabled = true with get, set
// AZ: Only to be used in sandbox
member r.clearConflict () =
r.conflict <- None
member this.printConflict (conflict:Ref<Clause>) =
printf " | >C< "
this.printClause conflict
member this.printClause (clause:Ref<Clause>)=
(!this.trail).printClauseVerbose this.database clause
// if DBG then printfn "--------------------------------------------------------------------------------"
// if DBG then (!s.trail).forcePrint("", this.partAssignment, this.bounds)
// AZ: The types mismatch, I know, but this is only for debugging purposes
member this.printCube (cube:Ref<Clause>)=
(!this.trail).printCubeVerbose this.database cube
// if DBG then printfn "--------------------------------------------------------------------------------"
// if DBG then (!s.trail).forcePrint("", this.partAssignment, this.bounds)
member this.Push (e:TrailElement) =
let c = (!this.trail).Push this.bVal this.bvVal this.bounds this.database (!db).Statistics e
match c with
| Some(cc) -> this.SetConflict (Some cc) //xTheory conflict, detected during a push attempt.
| None -> ()
member this.Pop =
(!this.trail).Pop this.bVal this.bvVal this.bounds this.database
member this.SetConflict (co:Ref<Clause> option) =
match co with
| Some(cc) -> checkExplanation this.trail this.database this.bvVal this.bounds !cc false false true
| None -> ()
this.conflict <- co
member this.ClearConflict =
this.conflict <- None
member this.GetConflictClause =
assert(this.conflict.IsSome)
this.conflict.Value
member this.IsComplete =
let highest_var = (!this.variableDB).highestVarInUse
let bool_vars_assigned = (!this.bVal).assignedVars
let bv_vars_assigned = (!this.bvVal).assignedVars
(highest_var = bool_vars_assigned + bv_vars_assigned)
member this.IsConflicted =
match this.conflict with
| Some(_) -> true
| None -> false
member this.IsSearch = (not this.IsSolved) && this.conflict.IsNone
member this.MarkSAT = this.answer <- Sat
member this.MarkUNSAT = this.answer <- Unsat
member this.MarkUNKNOWN = this.answer <- Unknown
member this.IsSolved = this.answer = Sat || this.answer = Unsat
member this.IsSatisfiable = this.answer = Sat
member this.IsUnsatisfiable = this.answer = Unsat
member this.Learn (c:Clause) =
assert(not this.IsConflicted)
let bVal = this.bVal
if getSize(c) > 1 then
dbg <| (lazy sprintf "* New clause: %s" (clauseToString c))
(!this.database).addAndWatchClause bVal c |> ignore
else if getSize(c) = 1 && (!bVal).getValueB (c.[1]) = Undefined then
(!this.clauseDB).addUnit c.[1]
this.Push (Imp (ref c, c.[1]))
assert(not this.IsConflicted)
else if (getSize(c) = 1 && (!bVal).getValueB (c.[1]) = False) then
//this.conflict <- Some (ref c)
assert(false)
else
assert(c.[0] > 0)
printfn "Warning attempted to add a redundant clause"
member this.defineTseitinVariable (lits: Literal list) =
let cls = newClauseFromList lits
match (!this.clauseDB).getTseitinDefinition cls with
| Some t -> t
| None ->
let tseitinVar = getTseitinVar this.database this.bVal this.bvVal this.bounds
(!this.clauseDB).registerTseitin cls tseitinVar
let tsVarImpliesGenLits = newClauseFromList ((Negate tseitinVar) :: lits)
this.Learn tsVarImpliesGenLits
for l in lits do
this.Learn (newClauseFromList(Negate l :: tseitinVar :: []))
if DBG then printfn "Defining %d <--> %s" tseitinVar (clauseToString (newClauseFromList lits))
tseitinVar
member r.enterTempMode (s:State)=
//Some of the sanbox data structures might be out of date.
//The database is shared so that is fine,
//But bVal, bvVal, bounds
assert (not r.inTempMode)
r.inTempMode <- true
let sortsRef = (ref (!s.variableDB).sorts)
(!r.bVal).enterTempMode s.bVal
(!r.bvVal).enterTempMode s.bvVal sortsRef
(!r.bounds).enterTempMode s.bounds sortsRef
(!r.database).enterTempMode (s.database)
(!r.watchManager).enterTempMode (!s.variableDB).highestVarInUse
member r.leaveTempMode() =
assert (r.inTempMode)
r.inTempMode <- false
(!r.bVal).leaveTempMode
(!r.bvVal).leaveTempMode
(!r.bounds).leaveTempMode
(!r.database).leaveTempMode()
(!r.watchManager).leaveTempMode ((!r.variableDB).highestVarInUse)
member r.isInTempMode = r.inTempMode
member r.trailElemToString (t:TrailElement) =
match t with
| BAssgnmnt (v,_,_) -> let bnds = (!r.bounds).get v
v.ToString() + ":bv \in " + (bnds.ToString())
| MAssgnmnt (v,_,_) -> let pattern = (!r.bvVal).getValue v
v.ToString() + ":bv = " + (pattern.ToString())
| BoolDecision l
| Imp (_,l) when (!r.theoryDB).isDefined (lit2var l) ->
(if isNegated l then "not" else "")
+ ((!r.theoryDB).getThRelation (lit2var l)).ToString r.numeralDB
| _ -> ""
member r.ShowFancyConflict (cnflct:Clause, ?annotation:string) =
if SHOW_CONFLICTS then
assert (cnflct.Length > 0)
assert (cnflct.Length = cnflct.[0] + 1)
let nDB = (!r.database).Numerals
let tDB = (!r.database).Theory
printf "Conflict #%d: " !(!(!r.database).Statistics).conflicts
match annotation with
| None -> ()
| Some(a) -> printf " %s" a
printfn ""
let mutable first = true
for i in 1 .. cnflct.Length - 1 do
let l = cnflct.[i]
let v = lit2var l
if not ((!tDB).isDefined v) then
if first then
printf " "
first <- false
else
printf " || "
printf "%d" l
if not first then printfn " || "
first <- true
for i in 1 .. cnflct.Length - 1 do
let l = cnflct.[i]
let v = (lit2var l)
if (!tDB).isDefined v then
if first then
first <- false
printf " "
else
printf " || "
if l < 0 then
printf "not "
printfn "%s" (((!tDB).getThRelation v).ToString(nDB, false))
printfn ""