Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
141 lines (116 loc) · 2.91 KB
/
Copy pathexample.py
File metadata and controls
141 lines (116 loc) · 2.91 KB
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
#!/usr/bin/env python
from pyrefine import *
def gcd2(x, y):
define_('int -> int -> int',
'x > 0 and y > 0',
'ret > 0')
t = c_('int', 0)
while x != y:
loop_('x > 0 and y > 0', 'x + y')
if x > y:
y, x = x, y
assert y > x
y = y - x
assert x == y
return y
def simple(x, y):
define_('int -> int -> int',
'x > 0 and y > 0',
'ret == x + y')
a = c_('int', x)
res = c_('int', 0)
res = a + y
return res
def simple_if(x, y):
define_('int -> int -> int',
'x != 0',
'ret > 0')
a = c_('int', 0)
a = x
if x < 0:
a = -x
return a
def simple_if2(x, y):
define_('int -> int -> int',
'y >= 0 and x >= 0',
'ret > 0')
a = c_('int', 1)
if y > x:
a = y
b = c_('int', y)
return a + b
def simple_if3(x, y):
define_('int -> int -> bool',
'y >= 0',
'ret == True')
if x > y:
x = y
return x <= y
def simple_if_else(x, y):
define_('int -> int -> int',
'True',
'ret > 0')
a = c_('int', 0)
if x > y:
a = x - y
else:
a = y - x + 1
return a
def simple_while(x):
define_('int -> int',
'x > 0',
'ret > 0')
s = c_('int', x)
x = x - 1
while x != 0:
loop_('x >= 0 and s > 0', 'x')
s = s + x
x = x - 1
return s
def simple_while2(x):
define_('int -> int',
'x >= 0',
'ret >= 0')
s = c_('int', 0)
i = c_('int', 0)
while i != x:
loop_('0 <= s and x >= 0 and 0 <= i <= x ', 'x - i')
s = s + i
i = i + 1
return s
def gcd(x, y):
define_('int -> int -> int',
'x > 0 and y > 0',
'ret > 0')
while x != y:
loop_('x > 0 and y > 0', 'x + y')
if x < y:
y = y - x
else:
x = x - y
return y
def proof():
define_('int', 'True', 'True')
implies = define_('bool -> bool -> bool', 'True',
'ret == (a >> b)',
lambda a, b: b if a else True)
# (a /\ b) -> a
prop1 = define_('bool -> bool -> bool', 'True', 'ret',
lambda a, b: implies((a and b), a))
# a -> ((a -> b) -> b)
prop2 = define_('bool -> bool -> bool', 'True', 'ret',
lambda a, b: implies(a, implies(implies(a, b), b)))
# a \/ ~a
excluded_middle = define_('bool -> bool', 'True', 'ret',
lambda a: a or not a)
# ~(a /\ b) <-> ~a \/ ~b
de_morgan = define_('bool -> bool -> bool', 'True', 'ret',
lambda a, b: not (a and b) == (not a or not b))
assert implies(False, False)
assert implies(False, True)
assert not implies(True, False)
assert implies(True, True)
if __name__ == '__main__':
print(gcd(8, 12))
proof()
print("Done")