项目作者: shraiysh

项目描述 :
Hoare logic for classroom demonstration
高级语言: C++
项目地址: git://github.com/shraiysh/hoare-logic.git
创建时间: 2019-09-23T14:25:46Z
项目社区:https://github.com/shraiysh/hoare-logic

开源协议:GNU Affero General Public License v3.0

下载


Hoare Logic

Sample Code

  • There can be only 26 variables(int and bool - a to z). Any type of assignment is possible after declaration only
  • There are 26 integer arrays - A to Z
  • Two dataypes int and bool
  • pre stands for precondition. post for post condition and inv for loop invariant
  • @ stands for universal quantifier and # stands for existensial quantifier
  • Datatypes of free variables need to be mentioned. Arrays are already of int type

Installation

Install z3 solver

  1. make
  2. ./a.out < tests/copy.txt

Valid Hoare Triples

  1. pre true;
  2. A[0] = 1;
  3. post A[0]==1;

  1. pre {
  2. int n;
  3. n>0;
  4. }
  5. n=5;
  6. A[3] = n;
  7. post A[3] == 5;

  1. pre {
  2. int x;
  3. int y;
  4. (x>0) && (y>0)&& (x>=y);
  5. }
  6. int r;
  7. r = x;
  8. inv (x>0) && (y>0)&& (x>=y) && (r>=0) && (r<=x);
  9. while(r>=y){
  10. r = r - y;
  11. }
  12. post (x>0) && (y>0)&& (x>=y) && (r>=0) && (r<y) && (r>=0) && (r<=x);

  1. pre {
  2. int x;
  3. (x>0);
  4. }
  5. if (x%2 == 0){
  6. x = x+1;
  7. }
  8. else {
  9. x = x+2;
  10. }
  11. post x%2 == 1;

Invalid Hoare triples

  1. pre true;
  2. A[0] = 1;
  3. post A[0]==2;

  1. pre {
  2. int i;
  3. i<0;
  4. }
  5. post i>0;

Check tests for more examples.

Output

  • Proves the correctness of hoare-triple. Provides counter-example if fails.

Reference