summaryrefslogtreecommitdiff
path: root/Chalice/refinements/Counter.chalice
blob: e0c0c7df1ad9bf004c158f29b003b5101a3eb124 (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
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;  
  }
}

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;
  }       
}

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 Counter1 {
  var a: Cell;
  var b: Cell;
  replaces y, z by acc(a) && acc(b) && acc(a.n) && acc(b.n) && y == a.n && z == b.n;
  
  refines init() 
  {    
    this.a := new Cell {n := 0};
    this.b := new Cell {n := 0};    
  }

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

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