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

  /** Interesting issues */  

  /* We can expose representation here */
  method magic1() returns (c: Cell) 
    requires acc(x)
    ensures acc(c.n) && x == old(x);
  {
    var c [acc(c.n)];
  }

  /* This should prevent us from exposing representation */
  method magic2() returns (c: Cell)
    requires acc(x);
    ensures acc(x) && x == old(x) && acc(c.n);
  {
    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;
  
  transforms init() 
  {
    replaces * by {this.y := 0; this.z := 0;}
  }

  transforms inc()
  {    
    replaces * by {this.y := this.y + 1;}
  }

  transforms dec() 
  {
    replaces * by {this.z := this.z + 1;}
  }       

  /** This violates abstraction of x -- we must hold all permissions to x to update it */
  method magic3() 
    requires acc(y);    
  {
    y := y + 1;
  }

  /** This does also -- but it also prohibits us from reading part of the state across refinement */
  method magic4() returns (i)
    requires acc(y);
  {
    i := y;
  }
}

class Cell {var n: int}
/*
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;
  
  transforms init() 
  {
    replaces * by {
      this.a := new Cell {n := 0};
      this.b := new Cell {n := 0};
    }
  }

  transforms inc() 
  {
    replaces * by {this.a.n := this.a.n + 1;}
  }

  transforms dec()
  {
    replaces * by {this.b.n := this.b.n + 1;}
  }

  transforms magic1() returns (c: Cell)
  {
    replaces * by {c := this.a;}
  }

  transforms magic2() returns (c: Cell)
  {
    replaces * by {c := this.a;}
  }

  transforms magic3()
  {
    replaces * by {this.a.n := this.a.n + 1;}
  }

  transforms magic4() returns (i)
  {
    replaces * by {i := this.a.n;}
  }
}
*/