Hoare logic for classroom demonstration
int
and bool
- a
to z
). Any type of assignment is possible after declaration onlyA
to Z
int
and bool
pre
stands for precondition. post
for post condition and inv
for loop invariant@
stands for universal quantifier and #
stands for existensial quantifierInstall z3 solver
make
./a.out < tests/copy.txt
pre true;
A[0] = 1;
post A[0]==1;
pre {
int n;
n>0;
}
n=5;
A[3] = n;
post A[3] == 5;
pre {
int x;
int y;
(x>0) && (y>0)&& (x>=y);
}
int r;
r = x;
inv (x>0) && (y>0)&& (x>=y) && (r>=0) && (r<=x);
while(r>=y){
r = r - y;
}
post (x>0) && (y>0)&& (x>=y) && (r>=0) && (r<y) && (r>=0) && (r<=x);
pre {
int x;
(x>0);
}
if (x%2 == 0){
x = x+1;
}
else {
x = x+2;
}
post x%2 == 1;
pre true;
A[0] = 1;
post A[0]==2;
pre {
int i;
i<0;
}
post i>0;
Check tests
for more examples.