Skip to content

Commit

Permalink
Check all files contain an // impl-start comment (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet authored Sep 4, 2024
1 parent eea53ff commit 1f5bf94
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-dafny-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ jobs:
uses: cachix/install-nix-action@v20
- name: Test that all dafny files are named correctly
run: nix run .#dafny-namecheck
- name: Test that all dafny files contain an impl
run: nix run .#check-contains-impl
- name: Run Dafny on all files
run: nix run .#dafny-check
2 changes: 2 additions & 0 deletions .github/workflows/test-dafny-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
uses: tj-actions/changed-files@v45
- name: Test that all dafny files are named correctly
run: nix run .#dafny-namecheck
- name: Test that all dafny files contain an impl
run: nix run .#check-contains-impl

- name: Run Dafny on new files
env:
Expand Down
4 changes: 4 additions & 0 deletions 023-strlen.dfy
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function strlen(s: string) : (len: int)
// post-conditions-start
ensures len == |s|
// post-conditions-end
{
// impl-start
|s|
// impl-end
}
14 changes: 12 additions & 2 deletions 025-factorize.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,40 @@ function prod(s: seq<int>) : int {
}

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 2 <= i && 2 * cur <= i * cur; // assert-line
}
taken := taken * temp;
i := i + 1;
Expand All @@ -36,5 +45,6 @@ method factorize(n: nat) returns (factors: seq<nat>)
factors := factors + [cur];
taken := taken * cur;
}
assert taken == n;
assert taken == n; // assert-line
// impl-end
}
17 changes: 17 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
done
'';

check-contains-impl = pkgs.writeShellScriptBin "check-contains-impl" ''
DIR=''${1:-.}
for file in "$DIR"/*.dfy; do
if [[ -e $file ]]; then
filename=$(basename "$file")
if ! ${pkgs.gnugrep}/bin/grep -q "// impl-start" "$file"; then
echo "File $file does not contain an impl, please recheck if it was marked"
exit 1
fi
fi
done
echo "All dafny files contain an impl."
'';

dafny-check-new = pkgs.writeShellScriptBin "dafny-check" ''
file_count=0
Expand Down

0 comments on commit 1f5bf94

Please sign in to comment.