Skip to content

Commit

Permalink
Change files to ensure correct codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet committed Oct 2, 2024
1 parent ea4d330 commit f3a0a97
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 62 deletions.
49 changes: 0 additions & 49 deletions 025-factorize.dfy
Original file line number Diff line number Diff line change
@@ -1,50 +1 @@
function prod(s: seq<int>) : int {
if |s| == 0 then 1 else prod(s[..|s| - 1]) * s[|s| - 1]
}

method factorize(n: nat) returns (factors: seq<nat>)
// pre-conditions-start
requires n > 0
// pre-conditions-end
// post-conditions-start
ensures prod(factors) == n
// post-conditions-end
{
// impl-start
factors := [];
ghost var taken := 1;
var cur := n;
var i := 2;
while i * i <= cur
// invariants-start
invariant prod(factors) == taken
invariant taken * cur == n
invariant cur >= 1
// invariants-end
{
ghost var pre := cur;
ghost var temp := 1;
while cur % i == 0
// invariants-start
invariant cur >= 1
invariant temp * cur == pre
invariant prod(factors) == taken * temp
// invariants-end
decreases cur - 1
{
factors := factors + [i];

cur := cur / i;
temp := temp * i;
assert 2 <= i && 2 * cur <= i * cur; // assert-line
}
taken := taken * temp;
i := i + 1;
}
if cur > 1 {
factors := factors + [cur];
taken := taken * cur;
}
assert taken == n; // assert-line
// impl-end
}
14 changes: 7 additions & 7 deletions 037-sort_even.dfy
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
method sorted_even(a: seq<int>) returns (sorted_even: seq<int>)
method sorted_even(a: seq<int>) returns (sorted: seq<int>)
// pre-conditions-start
requires |a| > 0
// pre-conditions-end
// post-conditions-start
ensures |sorted_even| == |a|
ensures forall i, j :: 0 <= i < j && 2 * i < |sorted_even| && 2 * j < |sorted_even| ==>
sorted_even[2 * i] <= sorted_even[2 * j]
ensures forall i :: 0 <= i < |a| && i % 2 == 1 ==> sorted_even[i] == a[i]
ensures multiset(a) == multiset(sorted_even)
ensures |sorted| == |a|
ensures forall i, j :: 0 <= i < j && 2 * i < |sorted| && 2 * j < |sorted| ==>
sorted[2 * i] <= sorted[2 * j]
ensures forall i :: 0 <= i < |a| && i % 2 == 1 ==> sorted[i] == a[i]
ensures multiset(a) == multiset(sorted)
// post-conditions-end
{
// impl-start
Expand All @@ -24,7 +24,7 @@ method sorted_even(a: seq<int>) returns (sorted_even: seq<int>)
i := i + 1;
}

sorted_even := SortSeqPred(a, p);
sorted := SortSeqPred(a, p);
// impl-end
}

Expand Down
2 changes: 1 addition & 1 deletion 073-smallest_change.dfy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
method smallest_change(s: seq<int>) returns (c: int)
// post-conditions-start
ensures c == |set i {:trigger s[i]} | 0 <= i < |s| / 2 && s[i] != s[|s| - 1 - i]|
ensures c == |set i | 0 <= i < |s| / 2 && s[i] != s[|s| - 1 - i]|
// post-conditions-end
{
// impl-start
Expand Down
8 changes: 5 additions & 3 deletions 087-get_row.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
method get_row(lst: seq<seq<int>>, x: int) returns (pos: seq<(int, int)>)
type SortSeqState = seq<(int, int)>

method get_row(lst: seq<seq<int>>, x: int) returns (pos: SortSeqState)
// post-conditions-start
ensures forall i :: 0 <= i < |pos| ==> (
var (a, b) := pos[i];
Expand Down Expand Up @@ -50,7 +52,7 @@ method get_row(lst: seq<seq<int>>, x: int) returns (pos: seq<(int, int)>)
assert forall i, j :: 0 <= i < |lst| && 0 <= j < |lst[i]| && lst[i][j] == x ==> (i, j) in multiset(pos);
}
// assert-end

// assert-start
assert forall i :: 0 <= i < |pos| ==> (
var (a, b) := pos[i]; 0 <= a < |lst| && 0 <= b < |lst[a]| && lst[a][b] == x
Expand All @@ -71,7 +73,7 @@ function less_eq(a: (int, int), b: (int, int)): bool {
(x == u && y == v) || less(a, b)
}

method SortSeq(s: seq<(int, int)>) returns (sorted: seq<(int, int)>)
method SortSeq(s: SortSeqState) returns (sorted: SortSeqState)
// post-conditions-start
ensures forall i, j :: 0 <= i < j < |sorted| ==> less_eq(sorted[i], sorted[j])
ensures |sorted| == |s|
Expand Down
4 changes: 3 additions & 1 deletion 095-check_dict_case.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ predicate IsUpperCase(s: string)
forall i :: 0 <= i < |s| ==> 'A' <= s[i] <= 'Z'
}

method CheckDictCase(dict: map<string, string>) returns (result: bool)
type DictCase = map<string, string>

method CheckDictCase(dict: DictCase) returns (result: bool)
// post-conditions-start
ensures dict == map[] ==> !result
ensures result ==> (forall k :: k in dict ==> IsLowerCase(k)) || (forall k :: k in dict ==> IsUpperCase(k))
Expand Down
2 changes: 1 addition & 1 deletion 105-by_length.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ method SortReverseAndName(arr: seq<int>) returns (result: seq<string>)
// post-conditions-start
ensures |result| <= |arr|
ensures forall i :: 0 <= i < |result| ==>
result[i] in {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
result[i] in ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
// post-conditions-end
{
// impl-start
Expand Down

0 comments on commit f3a0a97

Please sign in to comment.