summaryrefslogtreecommitdiff
path: root/Chalice/tests/regressions/workitem-10221.chalice
blob: 2a8ae723164a41832c30a06555405a8b10c0fd7e (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
// In this example, additional unfold/fold pairs make the verification of the last three methods fail.

class Node {
  var next : Node;
  var val : int;

  predicate list {
    acc(next) && acc(val) && (next!=null ==> next.list)
  }
  
  function vals() : seq<int>
    requires list
  {
    unfolding list in (next == null ? [val] : [val] ++ next.vals())
  }

  function reverse_vals() : seq<int>
    requires list
  {
    unfolding list in (next == null ? [val] : next.reverse_vals() ++ [val])
  }

  method reverse_in_place() returns (r:Node)
    requires list;
    ensures true;
  {
    var l : Node := this;
    r := null;

    var rev : seq<int> := this.reverse_vals();
    
    while (l != null) 
      invariant l!=null ==> l.list;
      invariant r!=null ==> r.list;
      invariant rev == (l==null ? nil<int> : l.reverse_vals()) ++ (r==null ? nil<int> : r.vals());
    { 
      var y: Node;
//      if (r != null) {
//        unfold r.list; fold r.list;
//      }
      unfold l.list;
//      if (l.next != null) {
//        unfold l.next.list; fold l.next.list;
//      }
    
      y := l.next;
      l.next := r;
      r := l;
      fold r.list;
      l := y;
    }
    assert r.vals() == rev; // should be the post-condition
  }
  

   method reverse_in_place_01() returns (r:Node)
    requires list;
    ensures true;
  {
    var l : Node := this;
    r := null;

    var rev : seq<int> := this.reverse_vals();
    
    while (l != null) 
      invariant l!=null ==> l.list;
      invariant r!=null ==> r.list;
      invariant rev == (l==null ? nil<int> : l.reverse_vals()) ++ (r==null ? nil<int> : r.vals());
    { 
      var y: Node;
//      if (r != null) {
//        unfold r.list; fold r.list;
//      }
      unfold l.list;
      if (l.next != null) {
        unfold l.next.list; fold l.next.list;
      }
    
      y := l.next;
      l.next := r;
      r := l;
      fold r.list;
      l := y;
    }
    assert r.vals() == rev; // should be the post-condition
  }
  
   

   method reverse_in_place_10() returns (r:Node)
    requires list;
    ensures true;
  {
    var l : Node := this;
    r := null;

    var rev : seq<int> := this.reverse_vals();
    
    while (l != null) 
      invariant l!=null ==> l.list;
      invariant r!=null ==> r.list;
      invariant rev == (l==null ? nil<int> : l.reverse_vals()) ++ (r==null ? nil<int> : r.vals());
    { 
      var y: Node;
      if (r != null) {
        unfold r.list; fold r.list;
      }
      unfold l.list;
//      if (l.next != null) {
//        unfold l.next.list; fold l.next.list;
//      }
    
      y := l.next;
      l.next := r;
      r := l;
      fold r.list;
      l := y;
    }
    assert r.vals() == rev; // should be the post-condition
  }
  
   


   method reverse_in_place_11() returns (r:Node)
    requires list;
    ensures true;
  {
    var l : Node := this;
    r := null;

    var rev : seq<int> := this.reverse_vals();
    
    while (l != null) 
      invariant l!=null ==> l.list;
      invariant r!=null ==> r.list;
      invariant rev == (l==null ? nil<int> : l.reverse_vals()) ++ (r==null ? nil<int> : r.vals());
    { 
      var y: Node;
      if (r != null) {
        unfold r.list; fold r.list;
      }
      unfold l.list;
      if (l.next != null) {
        unfold l.next.list; fold l.next.list;
      }
    
      y := l.next;
      l.next := r;
      r := l;
      fold r.list;
      l := y;
    }
    assert r.vals() == rev; // should be the post-condition
  }
  
   
}