summaryrefslogtreecommitdiff
path: root/Chalice/tests/refinements/Counter.chalice
blob: d1efae767bd7bc728f540a6f0b250b153cc2aa27 (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
class Counter0 {
  var x: int;
  
  method init() 
    requires acc(x); 
    ensures acc(x) && x == 0;
  {
    x := 0;  
  }

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

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

  method magic() returns (c: Cell)
    requires acc(x);
    ensures acc(x) && acc(c.n) && x == old(x);
  {
    var c [acc(c.n)]
  }
}

class Counter1 refines Counter0 {
  var y: int;
  var z: int;  
  replaces x by acc(y) && acc(z) && x == y - z && y >= 0 && z >= 0;
  
  refines init() 
  {
    this.y := 0; 
    this.z := 0;
  }

  refines inc()
  {    
    this.y := this.y + 1;
  }

  refines dec() 
  {
    this.z := this.z + 1;
  }      

  refines magic() returns (c: Cell)
  {
    c := new Cell;
  } 
}

class Cell {var n: int}

/** TODO: 
Two-step data refinement doesn't work for the following reason:
the spec of Counter1 uses the abstract field x which disappears at the concrete method body level.
I'm not sure what a good solution to this problem...
*/

class Counter2 refines Counter0 {
  var a: Cell;
  var b: Cell;
  replaces x by acc(a) && acc(b) && acc(a.n) && acc(b.n) && x == a.n - b.n;
  
  refines init() 
  {    
    this.a := new Cell;
    this.b := new Cell;
    this.a.n := 0;
    this.b.n := 0;
  }

  refines inc() 
  {
    this.a.n := this.a.n + 1;
  }

  refines dec()
  {
    var i := this.b.n + 1;
    this.b := new Cell;
    this.b.n := i;
  }

  refines magic() returns (c: Cell)
  {
    c := a;
  }
}

class Client {
  method main() 
  {
    var c := new Counter0;
    call c.init();
    call c.inc();
    call c.inc();
    call c.dec();
    call d := c.magic();
    d.n := 100;
    assert c.x == 1;
  }
}