forked from barghouthi/cs704
-
Notifications
You must be signed in to change notification settings - Fork 0
/
z3tut.py
129 lines (96 loc) · 1.86 KB
/
z3tut.py
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
from z3 import *
## look at z3.py
## Satisfiability
x = Int('x')
y = Int('y')
s = Solver()
s.add(x + y > 0)
print s.check()
print s.model()
print s.model()[x]
print s.model()[y]
s.reset()
# Unsatisfiable example
s.add(And(x + y > x, y < 0))
print s.check()
##############################
# Checking implication A => B
# VALIDITY
A = x > 0
B = x > -10
s.add(A)
s.add(Not(B))
if s.check() == unsat:
print "VALID"
else:
print "NOT VALID"
def isValid(phi):
s = Solver()
s.add (Not(phi))
if s.check() == sat:
print s.model()
return False
else:
return True
#############################
# Bools
p = Bool('p')
q = Bool('q')
r = Bool('r')
s = Solver()
s.add(And(Or(p,q), Implies(q,r)))
print s.check()
m = s.model()
print m
# negate the model
phi = True
s.add(p != m[p])
s.add(q != m[q])
s.add(r != m[r])
print s.check()
m = s.model()
print m
s.add(p != m[p])
s.add(q != m[q])
s.add(r != m[r])
print s.check()
############################
# checking Hoare triples
# {x > 0} x <- x + 1 {x > 1}
x = Int('x')
x1 = Int('x1')
pre = x > 0
trans = x1 == x + 1
post = x1 > 1
print isValid(Implies(And(pre, trans), post))
# {x > 0} x <- x + y {x > 1}
y = Int('y')
y1 = Int('y1')
pre = x > 0
trans = And(x1 == x + y, y1 == y)
post = x1 > 1
print isValid(Implies(And(pre, trans), post))
# {LOCK == 0 /\ new != old}
# while (new != old)
# old = new;
# if (*) {
# LOCK = 0;
# new++;
# }else{
# LOCK = 1
#{LOCK != 0}
lock = Int('lock')
lockp = Int('lockp')
old = Int('old')
oldp = Int('oldp')
new = Int('new')
newp = Int('newp')
pre = lock == 0
post = lock != 0
trans = And(oldp == new,
Or(And(lockp == 0, newp == new + 1),
And(lock == 1, newp == new)))
loop = new != old
inv = (old != new) == (lock == 0)
invp = (oldp != newp) == (lockp == 0)
print isValid(Implies(And(inv, loop, trans), invp))