Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Support for opaque function handles #4203

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Source/DafnyCore/Verifier/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5764,6 +5764,7 @@ public string FunctionHandle(Function f) {
functionHandles[f] = name;
var args = new List<Bpl.Expr>();
var vars = MkTyParamBinders(GetTypeParams(f), out args);
var argsRequires = new List<Bpl.Expr>(args); // Requires don't have reveal parameters
var formals = MkTyParamFormals(GetTypeParams(f), false, true);
var tyargs = new List<Bpl.Expr>();
foreach (var fm in f.Formals) {
Expand All @@ -5773,9 +5774,15 @@ public string FunctionHandle(Function f) {
if (f.IsFuelAware()) {
vars.Add(BplBoundVar("$ly", predef.LayerType, out var ly));
args.Add(ly);
argsRequires.Add(ly);
formals.Add(BplFormalVar("$fuel", predef.LayerType, true));
AddFuelSuccSynonymAxiom(f, true);
}
if (f.IsOpaque) {
vars.Add(BplBoundVar("$reveal", Boogie.Type.Bool, out var reveal));
args.Add(reveal);
formals.Add(BplFormalVar("$reveal", Boogie.Type.Bool, true));
}

Func<List<Bpl.Expr>, List<Bpl.Expr>> SnocSelf = x => x;
Func<List<Bpl.Expr>, List<Bpl.Expr>> SnocPrevH = x => x;
Expand Down Expand Up @@ -5817,6 +5824,7 @@ public string FunctionHandle(Function f) {
lhs_args.Add(fe);
var be = UnboxIfBoxed(fe, fm.Type);
rhs_args.Add(be);

rhs_dict[fm] = new BoogieWrapper(be, fm.Type);
// args and its [Box]args
var arg = BplBoundVar(fm_name, TrType(fm.Type), func_vars);
Expand Down Expand Up @@ -5845,7 +5853,7 @@ public string FunctionHandle(Function f) {
}

{
// Requires(Ty.., F#Handle( Ty1, ..., TyN, Layer, self), Heap, arg1, ..., argN)
// Requires(Ty.., F#Handle( Ty1, ..., TyN, Layer, reveal, self), Heap, arg1, ..., argN)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my initial experiments, this should make it easier to allow all functions to have a reveal parameter. The fact that requires didn't have this parameter was an awkward asymmetry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, we add the reveal parameter to the handle version of the function, not its requires. Since requirements and postconditions are always revealed, there is no need for a reveal parameter in .reveal

// = F#Requires(Ty1, .., TyN, Layer, Heap, self, [Unbox] arg1, .., [Unbox] argN)

var fhandle = FunctionCall(f.tok, name, predef.HandleType, SnocSelf(SnocPrevH(args)));
Expand All @@ -5855,7 +5863,7 @@ public string FunctionHandle(Function f) {
// In case this is the /requires/ or /reads/ function, then there is no precondition
rhs = Bpl.Expr.True;
} else {
var args_h = f.ReadsHeap ? Snoc(SnocPrevH(args), h) : args;
var args_h = f.ReadsHeap ? Snoc(SnocPrevH(argsRequires), h) : argsRequires;
rhs = FunctionCall(f.tok, RequiresName(f), Bpl.Type.Bool, Concat(SnocSelf(args_h), rhs_args));
}

Expand Down
16 changes: 16 additions & 0 deletions Test/git-issues/git-issue-4202.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %baredafny verify %args "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

opaque function F(x: int): char
{ 'D' }

function InitArray<D>(f: int -> D): (a: D)
{
f(44)
}

method Main() {
reveal F();
var c := InitArray(F);
MikaelMayer marked this conversation as resolved.
Show resolved Hide resolved
assert c == 'D';
}
2 changes: 2 additions & 0 deletions Test/git-issues/git-issue-4202.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Dafny program verifier finished with 1 verified, 0 errors
1 change: 1 addition & 0 deletions docs/dev/news/4202.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support for opaque function handles