summaryrefslogtreecommitdiff
path: root/Chalice/examples/refinement/Counter.chalice
blob: 310a9d78d2f56941a493003ac51567538130b5c9 (plain)
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
class Cell {var n: int;}

class Counter0 {
  var x: int;
  
  method init() 
    requires acc(this.*);
    ensures acc(x);
  {
    x := 0;  
  }

  method inc()
    requires acc(x);
    ensures acc(x);
  {
    x := x + 1;
  } 

  method dec()
    requires acc(x);
    ensures acc(x);
  {    
    x := x - 1;  
  }

  method magic1() returns (c: Cell) 
    requires acc(x)
    ensures acc(c.n);
  {
    c := new Cell;
  }

  method magic2() returns (c: Cell)
    requires acc(x);
    ensures acc(x) && acc(c.n);
  {
    c := new Cell;
  }
}

class Counter1 {
  ghost var x: int;
  var y: int;
  var z: int;  
  // replaces x by acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0;
  
  method init()
    requires acc(this.*);
    ensures acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0;
  {
    x := 0;
    y := 0;
    z := 0;    
  }

  method inc()
    requires acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0;
    ensures acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0;
  {
    x := x + 1;
    y := y + 1;
  }

  method dec() 
    requires acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0;
    ensures acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0;
  {
    x := x - 1;
    z := z + 1;
  }       
}

class Counter2 {
  ghost var x: int;
  ghost var y: int;
  ghost var z: int;
  var a: Cell;
  var b: Cell;
  // replace y || z by acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
  
  method init() 
    requires acc(this.*);
    ensures acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
  {
    x := 0;
    y := 0;
    z := 0;
    a := new Cell;
    b := new Cell;
    a.n := 0;
    b.n := 0;
  }

  method inc() 
    requires acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
    ensures acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
  {
    x := x + 1;
    y := y + 1;
    a.n := a.n + 1;
  }

  method dec()
    requires acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
    ensures acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
  {
    x := x - 1;
    z := z + 1;
    b.n := b.n + 1;
  }

  method magic1() returns (c: Cell)
    requires acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
    ensures acc(c.n);
  {
    c := a;
  }

  method magic2() returns (c: Cell)
    requires acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
    ensures acc(x) && acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0 && acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n && acc(c.n);
  {
    c := a;
  }
}