-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
261 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
method encode_cyclic(s: seq<int>) returns (res: seq<int>) | ||
// post-conditions-start | ||
ensures |s| == |res| | ||
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 0 ==> res[i] == s[i + 1]) | ||
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 1 ==> res[i] == s[i + 1]) | ||
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 2 ==> res[i] == s[i - 2]) | ||
ensures forall i :: |s| - |s| % 3 <= i < |s| ==> (res[i] == s[i]) | ||
// post-conditions-end | ||
{ | ||
// impl-start | ||
res := s; | ||
var i := 0; | ||
while i + 2 < |s| | ||
// invariants-start | ||
invariant 0 <= i <= |s| | ||
invariant |s| == |res| | ||
invariant i % 3 == 0 | ||
invariant forall j :: i <= j < |s| ==> (res[j] == s[j]) | ||
invariant forall j :: 0 <= j < i ==> (j % 3 == 0 ==> res[j] == s[j + 1]) | ||
invariant forall j :: 0 <= j < i ==> (j % 3 == 1 ==> res[j] == s[j + 1]) | ||
invariant forall j :: 0 <= j < i ==> (j % 3 == 2 ==> res[j] == s[j - 2]) | ||
// invariants-end | ||
{ | ||
res := res[i := s[i + 1]]; | ||
res := res[i + 1 := s[i + 2]]; | ||
res := res[i + 2 := s[i]]; | ||
i := i + 3; | ||
} | ||
// impl-end | ||
} | ||
|
||
method decode_cyclic(s: seq<int>) returns (res: seq<int>) | ||
// post-conditions-start | ||
ensures |s| == |res| | ||
ensures forall i :: |s| - |s| % 3 <= i < |s| ==> (res[i] == s[i]) | ||
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 0 ==> res[i] == s[i + 2]) | ||
ensures forall i :: 0 <= i < |s| - |s| % 3 ==> (i % 3 == 1 ==> res[i] == s[i - 1]) | ||
// post-conditions-end | ||
{ | ||
// impl-start | ||
var intermediate := encode_cyclic(s); | ||
res := encode_cyclic(intermediate); | ||
// impl-end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function CalcBal(s: seq<int>, i: int, j: int) : int | ||
requires 0 <= i <= j <= |s| | ||
{ | ||
if i == j then 0 | ||
else (if s[j - 1] == 0 then 1 else -1) + CalcBal(s, i, j - 1) | ||
} | ||
|
||
method correct_bracketing(s: seq<int>) returns (b: bool) | ||
// pre-conditions-start | ||
requires forall i :: 0 <= i < |s| ==> s[i] == 0 || s[i] == 1 | ||
// pre-conditions-end | ||
// post-conditions-start | ||
ensures (forall i :: 0 <= i <= |s| ==> CalcBal(s, 0, i) >= 0) ==> b | ||
ensures b ==> (forall i :: 0 <= i <= |s| ==> CalcBal(s, 0, i) >= 0) | ||
// post-conditions-end | ||
{ | ||
// impl-start | ||
var i := 0; | ||
var depth := 0; | ||
b := true; | ||
while i < |s| | ||
// invariants-start | ||
invariant 0 <= i <= |s| | ||
invariant depth == CalcBal(s, 0, i) | ||
invariant (forall j :: 0 <= j <= i ==> CalcBal(s, 0, j) >= 0) ==> b | ||
invariant b ==> (forall j :: 0 <= j <= i ==> CalcBal(s, 0, j) >= 0) | ||
// invariants-end | ||
{ | ||
if s[i] == 0 { | ||
depth := depth + 1; | ||
} else { | ||
depth := depth - 1; | ||
} | ||
if depth < 0 { | ||
b := false; | ||
} | ||
i := i + 1; | ||
} | ||
// impl-end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
datatype Option<T> = None | Some(T) | ||
|
||
|
||
function getVal(mx : Option<int>) : int | ||
requires mx != None | ||
{ | ||
match mx { | ||
case Some(n) => n | ||
} | ||
} | ||
|
||
method rolling_max(s: seq<int>) returns (res: Option<int>) | ||
// post-conditions-start | ||
ensures res == None <==> |s| < 2 | ||
ensures res != None ==> exists x :: 0 <= x < |s| && s[x] == getVal(res) | ||
ensures forall x, y :: 0 <= x < y < |s| ==> s[x] <= getVal(res) || s[y] <= getVal(res) | ||
// post-conditions-end | ||
{ | ||
// impl-start | ||
if |s| < 2 { | ||
return None; | ||
} | ||
|
||
res := None; | ||
var mx := s[0]; | ||
var i := 1; | ||
|
||
while i < |s| | ||
// invariants-start | ||
invariant 0 <= i <= |s| | ||
invariant i > 1 ==> res != None | ||
invariant exists x :: 0 <= x < |s| && s[x] == mx | ||
invariant res != None ==> exists x :: 0 <= x < |s| && s[x] == getVal(res) | ||
invariant i == 1 ==> res == None | ||
invariant i > 1 ==> getVal(res) <= mx | ||
invariant forall x :: 0 <= x < i ==> s[x] == mx || (res != None && s[x] <= getVal(res)) | ||
invariant i > 1 ==> forall x, y :: 0 <= x < y < i ==> s[x] <= getVal(res) || s[y] <= getVal(res) | ||
// invariants-end | ||
{ | ||
if s[i] > mx { | ||
res := Some(mx); | ||
mx := s[i]; | ||
} else { | ||
match res { | ||
case None => res := Some(s[i]); | ||
case Some(n1) => | ||
{ | ||
if (s[i] > n1) { | ||
res := Some(s[i]); | ||
} | ||
} | ||
} | ||
} | ||
i := i + 1; | ||
} | ||
// impl-end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
function CalcBal(s: seq<int>, i: int, j: int, acc: int) : int | ||
requires 0 <= i <= j <= |s| | ||
{ | ||
if i == j then acc | ||
else (if s[j - 1] == 0 then 1 else -1) + CalcBal(s, i, j - 1, acc) | ||
} | ||
|
||
method checkFixed(s1: seq<int>, s2: seq<int>) returns (b: bool) | ||
// pre-conditions-start | ||
requires forall i :: 0 <= i < |s1| ==> s1[i] == 0 || s1[i] == 1 | ||
requires forall i :: 0 <= i < |s2| ==> s2[i] == 0 || s2[i] == 1 | ||
// pre-conditions-end | ||
// post-conditions-start | ||
ensures b ==> forall i :: 0 <= i <= |s1| ==> CalcBal(s1, 0, i, 0) >= 0 | ||
ensures b ==> forall i :: 0 <= i <= |s2| ==> CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0) >= 0 | ||
ensures !b ==> (exists i :: 0 <= i <= |s1| && CalcBal(s1, 0, i, 0) < 0) || (exists i :: 0 <= i <= |s2| && CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0) < 0) | ||
// post-conditions-end | ||
{ | ||
// impl-start | ||
var bal := 0; | ||
var i := 0; | ||
while i < |s1| | ||
// invariants-start | ||
invariant 0 <= i <= |s1| | ||
invariant bal == CalcBal(s1, 0, i, 0) | ||
invariant forall j :: 0 <= j < i ==> CalcBal(s1, 0, j, 0) >= 0 | ||
// invariants-end | ||
{ | ||
if s1[i] == 0 { | ||
bal := bal + 1; | ||
} else { | ||
bal := bal - 1; | ||
} | ||
if bal < 0 { | ||
// assert-start | ||
assert CalcBal(s1, 0, i + 1, 0) < 0; | ||
// assert-end | ||
return false; | ||
} | ||
i := i + 1; | ||
} | ||
|
||
i := 0; | ||
while i < |s2| | ||
// invariants-start | ||
invariant 0 <= i <= |s2| | ||
invariant bal == CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0) | ||
invariant forall j :: 0 <= j < i ==> CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, j, 0) >= 0 | ||
// invariants-end | ||
{ | ||
if s2[i] == 0 { | ||
bal := bal + 1; | ||
} else { | ||
bal := bal - 1; | ||
} | ||
if bal < 0 { | ||
// assert-start | ||
assert CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i + 1, 0) < 0; | ||
assert exists i :: 0 <= i <= |s2| && CalcBal(s1, 0, |s1|, 0) + CalcBal(s2, 0, i, 0) < 0; | ||
// assert-end | ||
return false; | ||
} | ||
i := i + 1; | ||
} | ||
return true; | ||
// impl-end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
method is_nested(s: seq<int>) returns (res: bool) | ||
// post-conditions-start | ||
ensures res == exists x, y, z, w :: 0 <= x < y < z < w < |s| && s[x] == 0 && s[y] == 0 && s[z] == 1 && s[w] == 1 | ||
// post-conditions-end | ||
{ | ||
// impl-start | ||
var bal := 0; | ||
var i := 0; | ||
|
||
while i < |s| && bal < 2 | ||
// invariants-start | ||
invariant 0 <= i <= |s| | ||
invariant (bal >= 1) == (exists x :: 0 <= x < i && s[x] == 0) | ||
invariant (bal >= 2) == (exists x, y :: 0 <= x < y < i && s[x] == 0 && s[y] == 0) | ||
invariant !(exists x, y, z :: 0 <= x < y < z < i && s[x] == 0 && s[y] == 0 && s[z] == 1) | ||
// invariants-end | ||
{ | ||
if s[i] == 0 { | ||
bal := bal + 1; | ||
} | ||
i := i + 1; | ||
} | ||
|
||
if bal < 2 { | ||
return false; | ||
} | ||
|
||
while i < |s| && bal > 0 | ||
// invariants-start | ||
invariant 0 <= i <= |s| | ||
invariant 0 <= bal <= 2 | ||
invariant exists x, y :: 0 <= x && x < y && y < i && s[x] == 0 && s[y] == 0 | ||
invariant (bal <= 1) == (exists x, y, z :: 0 <= x < y < z < i && s[x] == 0 && s[y] == 0 && s[z] == 1) | ||
invariant (bal == 0) == (exists x, y, z, w :: 0 <= x < y < z < w < i && s[x] == 0 && s[y] == 0 && s[z] == 1 && s[w] == 1) | ||
// invariants-end | ||
{ | ||
if s[i] == 1 { | ||
bal := bal - 1; | ||
} | ||
i := i + 1; | ||
} | ||
|
||
if bal > 0 { | ||
return false; | ||
} | ||
|
||
return true; | ||
// impl-end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters