2017-01-29 13:02:04 +01:00
|
|
|
|
2017-01-30 22:58:38 +01:00
|
|
|
from random import randint
|
|
|
|
|
|
2017-01-31 20:52:16 +01:00
|
|
|
def f(x):
|
|
|
|
|
#@ ensures result > x
|
|
|
|
|
return x+1
|
|
|
|
|
|
2017-01-31 20:19:55 +01:00
|
|
|
def swap(a, i, j):
|
|
|
|
|
#@ requires 0 <= i < len(a) and 0 <= j < len(a)
|
|
|
|
|
t = a[i]
|
|
|
|
|
a[i] = a[j]
|
|
|
|
|
a[j] = t
|
2017-01-30 22:53:30 +01:00
|
|
|
|
2017-02-02 21:52:15 +01:00
|
|
|
def t10():
|
|
|
|
|
s = 0
|
2019-07-06 12:41:28 +02:00
|
|
|
for i in range(0, 11):
|
2017-02-02 21:52:15 +01:00
|
|
|
#@ invariant 2 * s == i * (i-1)
|
|
|
|
|
s = s + i
|
|
|
|
|
#@ assert s == 55
|
2017-01-31 22:56:00 +01:00
|
|
|
|
2017-01-31 14:35:51 +01:00
|
|
|
i = randint(0, 10)
|
|
|
|
|
#@ assert 0 <= i <= 10
|
|
|
|
|
|
2017-01-29 15:52:12 +01:00
|
|
|
a = 0
|
|
|
|
|
b = 1
|
|
|
|
|
while b < 100:
|
2017-01-30 19:12:37 +01:00
|
|
|
#@ invariant b >= a >= 0
|
|
|
|
|
#@ invariant b >= 1
|
|
|
|
|
#@ variant 200 - b - a
|
2017-01-29 15:52:12 +01:00
|
|
|
print(a)
|
|
|
|
|
b = a+b
|
2017-01-30 19:12:37 +01:00
|
|
|
#@ assert b >= 1
|
2017-01-29 15:52:12 +01:00
|
|
|
a = b-a
|
2017-01-30 13:50:18 +01:00
|
|
|
|
|
|
|
|
# lists
|
2017-01-30 09:08:52 +01:00
|
|
|
l = range(0, 10)
|
2017-01-30 19:12:37 +01:00
|
|
|
#@ assert forall i. 0 <= i < 10 -> l[i] >= 0
|
2017-01-30 09:52:35 +01:00
|
|
|
l[2] = 42
|
2017-01-30 19:12:37 +01:00
|
|
|
#@ assert l[2] == 42
|
2017-01-30 09:52:35 +01:00
|
|
|
i = 0
|
|
|
|
|
while i < 10:
|
2017-01-30 19:12:37 +01:00
|
|
|
#@ invariant 0 <= i
|
|
|
|
|
#@ invariant forall j. 0 <= j < i -> l[j] == 0
|
|
|
|
|
#@ variant 10 - i
|
2017-01-30 09:52:35 +01:00
|
|
|
l[i] = 0
|
|
|
|
|
i = i+1
|
2017-01-29 13:02:04 +01:00
|
|
|
|
2017-01-30 21:27:16 +01:00
|
|
|
sum = 0
|
2019-07-06 14:54:56 +02:00
|
|
|
#@ label L
|
2017-01-30 22:53:30 +01:00
|
|
|
for x in [42]*3:
|
2017-01-30 21:27:16 +01:00
|
|
|
#@ invariant sum >= 0
|
2017-01-30 19:12:37 +01:00
|
|
|
print(x)
|
2017-01-30 21:27:16 +01:00
|
|
|
#@ assert x >= 0
|
|
|
|
|
sum = sum+x
|
2017-01-30 19:12:37 +01:00
|
|
|
|
2017-01-30 22:53:30 +01:00
|
|
|
foo = [1,2,3]
|
|
|
|
|
#@ assert len(foo)==3
|
|
|
|
|
#@ assert foo[1]==2
|
|
|
|
|
|
2019-07-06 14:54:56 +02:00
|
|
|
#@ assert at(sum, L) == 0
|
|
|
|
|
|
2017-01-30 22:53:30 +01:00
|
|
|
|
2017-01-30 21:27:16 +01:00
|
|
|
# Python's division is neither Euclidean division, nor computer division
|
|
|
|
|
#@ assert 4 // 3 == 1 and 4 % 3 == 1
|
|
|
|
|
#@ assert -4 // 3 == -2 and -4 % 3 == 2
|
|
|
|
|
#@ assert 4 // -3 == -2 and 4 % -3 == -2
|
|
|
|
|
#@ assert -4 // -3 == 1 and -4 % -3 == -1
|
2017-01-30 13:50:18 +01:00
|
|
|
|
2017-01-29 13:02:04 +01:00
|
|
|
# Local Variables:
|
2017-01-30 19:12:37 +01:00
|
|
|
# compile-command: "make -C ../.. && why3 prove -P alt-ergo test.py"
|
2017-01-29 13:02:04 +01:00
|
|
|
# End:
|
2017-01-30 21:27:16 +01:00
|
|
|
|