Skip to content

Commit

Permalink
another bug fix to have the monomorphizer descend into the constructo…
Browse files Browse the repository at this point in the history
…rs of monomorphic datatype declarations (#301)
  • Loading branch information
shazqadeer authored Oct 15, 2020
1 parent a7c6c3b commit a52e113
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Source/Core/Monomorphization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,16 @@ public override Expr VisitExpr(Expr node)
{
return exprMonomorphizationVisitor.VisitExpr(node);
}


public override Declaration VisitTypeCtorDecl(TypeCtorDecl node)
{
if (node is DatatypeTypeCtorDecl datatypeTypeCtorDecl)
{
datatypeTypeCtorDecl.Constructors.Iter(constructor => VisitFunction(constructor));
}
return base.VisitTypeCtorDecl(node);
}

private class TypeInstantiationComparer : IEqualityComparer<List<Type>>
{
public bool Equals(List<Type> l1, List<Type> l2)
Expand Down
52 changes: 51 additions & 1 deletion Test/monomorphize/vector.bpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function {:inline} Len<T>(v: Vec T): int
{
len#Vec(v)
}

procedure test0()
{
var s: Vec int;
Expand Down Expand Up @@ -112,7 +113,7 @@ ensures (forall i, j: int :: 0 <= i && i <= j && j < Len(s') ==> Nth(s', i) <= N

procedure sorted_insert(s: Vec int, x: int) returns (s': Vec int)
requires (forall i, j: int :: 0 <= i && i <= j && j < Len(s) ==> Nth(s, i) <= Nth(s, j));
ensures (forall i, j: int :: 0 <= i && i <= j && j < Len(s) ==> Nth(s, i) <= Nth(s, j));
ensures (forall i, j: int :: 0 <= i && i <= j && j < Len(s') ==> Nth(s', i) <= Nth(s', j));
{
var pos: int;
var val: int;
Expand All @@ -138,3 +139,52 @@ ensures (forall i, j: int :: 0 <= i && i <= j && j < Len(s) ==> Nth(s, i) <= Nth
}
s' := Append(s', val);
}

type {:datatype} Value;
function {:constructor} Integer(i: int): Value;
function {:constructor} Vector(v: Vec Value): Value;

procedure test3(val: Value) returns (val': Value)
requires is#Vector(val) && Len(v#Vector(val)) == 1 && Nth(v#Vector(val), 0) == Integer(0);
ensures val == val';
{
var s: Vec Value;

s := Empty();
s := Append(s, Integer(0));
val' := Vector(s);
}

function has_zero(val: Value): (bool)
{
if (is#Integer(val))
then val == Integer(0)
else (exists i: int :: 0 <= i && i < Len(v#Vector(val)) && has_zero(Nth(v#Vector(val), i)))
}

procedure traverse(val: Value) returns (b: bool)
ensures b == has_zero(val);
{
var s: Vec Value;
var i: int;

b := false;
if (is#Integer(val)) {
b := val == Integer(0);
return;
}
s := v#Vector(val);
i := 0;
while (i < Len(s))
invariant !b;
invariant 0 <= i;
invariant (forall j: int :: 0 <= j && j < i ==> !has_zero(Nth(s, j)));
{
call b := traverse(Nth(s, i));
if (b) {
return;
}
assert !has_zero(Nth(s, i));
i := i + 1;
}
}
2 changes: 1 addition & 1 deletion Test/monomorphize/vector.bpl.expect
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

Boogie program verifier finished with 8 verified, 0 errors
Boogie program verifier finished with 10 verified, 0 errors

0 comments on commit a52e113

Please sign in to comment.