-
Notifications
You must be signed in to change notification settings - Fork 10
/
ModelVerification.fs
66 lines (56 loc) · 2.01 KB
/
ModelVerification.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
module ModelVerification
open Microsoft.Z3
open System.Collections.Generic
open GlobalOptions
open Util
open BooleanValuation
open Problem
open Model
let verifySat (p:Problem) (m:Model) =
let mutable finalRes = true
let z3 = new Context();
let z3model = new Dictionary<Expr,Expr>()
let g = p.goal.Translate(z3)
let vgoal = g.Translate(z3)
for i in 1..p.var2expr.Length - 1 do
if (p.var2expr.[i] <> null &&
p.var2expr.[i].IsConst) then
if (p.var2expr.[i].IsBool) then
let e = ((if ((m.getValueBool i) = True) then z3.MkTrue() else z3.MkFalse()) :> Expr)
z3model.Add(p.var2expr.[i].Translate(z3), e.Translate(z3))
else
let mutable mv = (m.getValueBV i)
if not mv.isConcreteValue then
mv <- mv.Minimum
let e = mv.toZ3Expr z3
z3model.Add(p.var2expr.[i].Translate(z3), e.Translate(z3))
let vars : Expr[] = Array.create z3model.Count null
let values : Expr[] = Array.create z3model.Count null
let mutable i = 0
for kv in z3model do
vars.[i] <- kv.Key
values.[i] <- kv.Value
i <- i + 1
if SHOWMODEL then
for kv in z3model do
printfn "%s := %s" (kv.Key.ToString()) (kv.Value.ToString())
for i in 0 .. int(vgoal.Size) - 1 do
let orig = vgoal.Formulas.[i]
let subst = orig.Substitute(vars, values)
let simple = subst.Simplify()
if (simple.IsTrue) then
if DBG then
printfn "OK: %s -> %s -> %s" (orig.ToString()) (subst.ToString()) (simple.ToString())
else
printfn "ERROR: %s -> %s -> %s" (orig.ToString()) (subst.ToString()) (simple.ToString())
finalRes <- false
finalRes
let Verify ( p : Problem ) ( m : Model ) =
if not VERIFYMODEL then
true
else
if not (verifySat p m) then
false
else
polite <| (lazy "Ok, model verified.\n")
true