summaryrefslogtreecommitdiff
path: root/Chalice/examples/refinement/List.chalice
blob: c3d8175c8bb868a877d7c22714a1e0df26a117bb (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/** 
Interesting issues:
  * using functions in refinement to write getters
  * using functions to write coupling invariant
  * refining to a recursive implementation
  * restricting refinement (List1 must have at least one item)
  * refining a method with an output variable  

Do we shadows the abstract variables in the later concrete programs?

How do we handle generic sequence in List2
*/

class List0 {
  var rep: seq<int>;

  method init() 
    requires acc(this.*);
    ensures acc(rep);
  {
    rep := [0];
  }

  function length(): int
    requires acc(rep);
  {
    |rep|
  }

  method get(i: int) returns (v: int)
    requires acc(rep);
    requires 0 <= i && i < length();
    ensures acc(rep);
  {
    v := rep[i];
  }

  method pick() returns (v: int) 
    requires acc(rep);
    ensures acc(rep);
  {
    var i: int;
    assume 0 <= i && i < length();
    v := rep[i];
  }
}

class List1 {
  ghost var rep: seq<int>;

  var data: int;
  var l: seq<List1>;

  function inv(): bool
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l);
  {
    /** valid */ |l| >= 0 &&  
    (forall i in [0..|l|] :: l[i] != null && l[i].l == l[i+1..]) &&
    /** coupling */ |l| + 1 == |rep| && 
    (forall i in [0..|l|] :: l[i].data == rep[i+1]) &&
    data == rep[0]
  }

  method init()
    requires acc(this.*);
    ensures acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && inv();
  {
    rep := [0];
    data := 0;
    l := nil<List1>;    
  }

  function length(): int
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && inv();
  {
    |l| + 1
  }

  method checkLength() 
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && inv();
  {
    assert |l| + 1 == |rep|;
  }

  method get(i: int) returns (v: int)
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && inv();
    requires 0 <= i && i < length();
    ensures acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && inv();
  {
    if (i == 0) {
      v := data;
    } else {
      v := l[i-1].data;
    }
    assert v == rep[i];
  }
}

class List2 {
  ghost var rep: seq<int>;
  ghost var l: seq<List2>;

  var data: int;
  var next: List2;
  var size: int;

  function inv(): bool
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && acc(size) && acc(next) && acc(l[*].size) && acc(l[*].next);
  {
    /** valid */ |l| >= 0 &&  
    (forall i in [0..|l|] :: l[i] != null && l[i].l == l[i+1..]) &&
    /** coupling */ |l| + 1 == |rep| && 
    (forall i in [0..|l|] :: l[i].data == rep[i+1]) &&
    data == rep[0] &&
    /** coupling */ size == |l| + 1 &&
    (next == null ==> |l| == 0) &&
    (next != null ==> |l| > 0 && next == l[0] && l[|l|-1].next == null) &&
    (forall i in [0..|l|] :: l[i].size == size - i - 1) && 
    (forall i in [0..|l|-1] :: l[i].next == l[i+1])    
  }

  method init()
    requires acc(this.*);
    ensures acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l)  && acc(size) && acc(next) && acc(l[*].size) && acc(l[*].next) && inv();
  {
    rep := [0];
    data := 0;
    l := nil<List2>;    
    next := null;
    size := 1;
  }

  function length(): int
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && acc(size) && acc(next) && acc(l[*].size) && acc(l[*].next) && inv();
  {
    size
  }

  method checkLength() 
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && acc(size) && acc(next) && acc(l[*].size) && acc(l[*].next) && inv();
  {
    assert size == |l| + 1;
  }

  method get(i: int) returns (v: int)
    requires acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && acc(size) && acc(next) && acc(l[*].size) && acc(l[*].next) && inv();
    requires 0 <= i && i < length();
    ensures acc(rep) && acc(l) && acc(data) && acc(l[*].data) && acc(l[*].l) && acc(size) && acc(next) && acc(l[*].size) && acc(l[*].next) && inv();
  {
    if (i == 0) {
      v := data;
    } else {
      call w := next.get(i-1);
      v := w;
      assert v == l[i-1].data;
    }
  }
}