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: Use reveal_ constant as function argument in override axiom #5111

Merged
merged 16 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions Source/DafnyCore/Resolver/ModuleResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,9 @@ void InheritedTraitMembers(TopLevelDeclWithMembers cl) {
} else if (member.IsGhost != traitMember.IsGhost) {
reporter.Error(MessageSource.Resolver, member.tok, "overridden {0} '{1}' in '{2}' has different ghost/compiled status than in trait '{3}'",
traitMember.WhatKind, traitMember.Name, cl.Name, trait.Name);
} else if (!member.IsOpaque && traitMember.IsOpaque) {
reporter.Error(MessageSource.Resolver, member.tok, "overridden {0} '{1}' in '{2}' must be 'opaque' since the member is 'opaque' in trait '{3}'",
traitMember.WhatKind, traitMember.Name, cl.Name, trait.Name);
} else {
// Copy trait member's extern attribute onto class member if class does not provide one
if (!Attributes.Contains(member.Attributes, "extern") && Attributes.Contains(traitMember.Attributes, "extern")) {
Expand Down
8 changes: 8 additions & 0 deletions Source/DafnyCore/Verifier/BoogieGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,14 @@ private void CreateRevealableConstant(Function f) {
sink.AddTopLevelDeclaration(revealTrigger);
Bpl.Expr revealTrigger_expr = new Bpl.IdentifierExpr(f.tok, revealTrigger);
this.functionReveals[f] = revealTrigger_expr;

// If this is an override, generate:
// axiom reveal_FunctionA ==> reveal_FunctionParent;
if (f.OverriddenFunction is { IsOpaque: true }) {
var revealParent = GetRevealConstant(f.OverriddenFunction);
var implication = BplImp(revealTrigger_expr, revealParent);
AddOtherDefinition(revealTrigger, new Axiom(f.tok, implication));
}
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// RUN: %testDafnyForEachResolver --expect-exit-code=4 "%s" -- --general-traits=datatype

module True {
trait A {
ghost predicate Valid()
}

trait B extends A {
ghost opaque predicate Valid() { true }
}

method TestByRequires(b: B)
requires b.Valid()
{
var a: A := b;
assert a.Valid();
}

method TestByReveal(b: B) {
var a: A := b;
assert a.Valid() by {
reveal b.Valid();
}
}
}

module X {
trait A {
ghost predicate Valid(w: bool)
}

trait B extends A {
ghost opaque predicate Valid(x: bool) { x }
}

method TestByRequires(b: B, y: bool)
requires b.Valid(y)
{
var a: A := b;
assert a.Valid(y);
}

method TestByReveal(b: B, y: bool) {
var a: A := b;
assert a.Valid(y) by { // error: assertion violation
reveal b.Valid();
}
}
}

module Parameters {
trait A {
ghost predicate P<T>(t: T)
}

trait B extends A {
ghost opaque predicate P<T>(t: T) { t == t }
}


method TestByRequires(b: B)
requires b.P(5)
{
var a: A := b;
assert a.P(5);
}

method TestByRequires'(b: B)
requires b.P(6)
{
var a: A := b;
assert a.P(5); // error: assertion violation
}

method TestByRequires''(b: B)
requires b.P(true)
{
var a: A := b;
assert a.P(5); // error: assertion violation
}


method TestByReveal(b: B) {
var a: A := b;
assert a.P(5) by {
reveal b.P();
}
}

method TestByReveal'(b: B) {
var a: A := b;
assert a.P(true) by {
reveal b.P();
}
}
}

module StayOpaque {
trait A {
opaque predicate Valid()
}

trait B extends A {
opaque predicate Valid() { true }
}

trait C extends A {
opaque predicate Valid() { true }
}

method TestByRequires(b: B)
requires b.Valid()
{
var a: A := b;
assert a.Valid();
}

method TestByReveal(b: B) {
var a: A := b;
assert a.Valid() by {
reveal b.Valid();
}
}

method TestIndependence0(b: B, c: C) {
var a: A := b;
assert b.Valid() by { // error: revealing for C should not affect B
reveal c.Valid();
}
}

method TestIndependence1(b: B, c: C) {
var a: A := b;
assert a.Valid() by { // fine: revealing for C also reveals for all A's
reveal c.Valid();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
git-issue-5017a.dfy(45,18): Error: assertion might not hold
git-issue-5017a.dfy(72,14): Error: assertion might not hold
git-issue-5017a.dfy(79,14): Error: assertion might not hold
git-issue-5017a.dfy(127,18): Error: assertion might not hold

Dafny program verifier finished with 14 verified, 4 errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// RUN: %testDafnyForEachResolver --expect-exit-code=2 "%s" -- --general-traits=datatype

module Opaque {
trait A {
predicate Valid()
}

trait B {
opaque predicate Valid() { true }
}

trait C {
opaque predicate Valid()
}


trait K0 extends A {
predicate Valid()
}
trait K1 extends A {
opaque predicate Valid()
}
trait K2 extends A {
predicate Valid() { true }
}
trait K3 extends A {
opaque predicate Valid() { true }
}

trait L0 extends B {
predicate Valid() { true } // error: B.Valid already has a body
}
trait L1 extends B {
opaque predicate Valid() { true } // error: B.Valid already has a body
}

trait M0 extends C {
predicate Valid() // error: must be opaque (since it is in C)
}
trait M1 extends C {
opaque predicate Valid()
}
trait M2 extends C {
predicate Valid() { true } // error: must be opaque (since it is in C)
}
trait M3 extends C {
opaque predicate Valid() { true }
}


datatype W0 extends A = W0 {
predicate Valid() { true }
}
class W1 extends A {
predicate Valid() { true }
}

datatype X0 extends A = X0 {
opaque predicate Valid() { true }
}
class X1 extends A {
opaque predicate Valid() { true }
}

datatype Y0 extends C = Y0 {
predicate Valid() { true } // error: must be opaque (since it is in C)
}
class Y1 extends C {
predicate Valid() { true } // error: must be opaque (since it is in C)
}

datatype Z0 extends C = Z0 {
opaque predicate Valid() { true }
}
class Z1 extends C {
opaque predicate Valid() { true }
}
}

module StayOpaque {
trait A {
opaque predicate Valid()
}

datatype B extends A = B {
opaque predicate Valid() { true }
}

class C extends A {
opaque predicate Valid() { true }
}

method TestA(a: A) {
reveal a.Valid(); // error: nothing to reveal
}

method TestB(b: B) {
reveal b.Valid();
}

method TestC(c: C) {
reveal c.Valid();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
git-issue-5017b.dfy(31,14): Error: fully defined predicate 'Valid' is inherited from trait 'B' and is not allowed to be re-declared
git-issue-5017b.dfy(34,21): Error: fully defined predicate 'Valid' is inherited from trait 'B' and is not allowed to be re-declared
git-issue-5017b.dfy(34,21): Error: static lemma 'reveal_Valid' is inherited from trait 'B' and is not allowed to be re-declared
git-issue-5017b.dfy(38,14): Error: overridden predicate 'Valid' in 'M0' must be 'opaque' since the member is 'opaque' in trait 'C'
git-issue-5017b.dfy(44,14): Error: overridden predicate 'Valid' in 'M2' must be 'opaque' since the member is 'opaque' in trait 'C'
git-issue-5017b.dfy(66,14): Error: overridden predicate 'Valid' in 'Y0' must be 'opaque' since the member is 'opaque' in trait 'C'
git-issue-5017b.dfy(69,14): Error: overridden predicate 'Valid' in 'Y1' must be 'opaque' since the member is 'opaque' in trait 'C'
git-issue-5017b.dfy(94,13): Error: member 'reveal_Valid' does not exist in trait 'A'
Copy link
Member

Choose a reason for hiding this comment

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

I worried a bit about this issue. It seems that, to resolver it, one would need to implement this member, but the resolution is to actually. make Valid opaque in A. Could you detect that particular case and emit a more useful error message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Excellent idea. I improved many messages. However, it became more difficult for some of them. The problem is that, early on during resolution, we change reveal X(); expressions into reveal_X(); lemma calls. By the time that these attempted lemma calls go through the resolver, the name looked up is reveal_X. To improve the error message, we then have to detect that we're in a reveal-statement context and that the thing we're trying to look up has a name that starts with "reveal_" (which is not actually guaranteed to be one of the rewritten calls, since this could have been a user-defined name in the first place), and then remove the "reveal_" prefix, try to find what the name X might have referred to, and finally reconstruct the reason why no reveal_X lemma was created in the first place. It would have been much better not to have done the reveal_X-lemma surgery so early in the pipeline. Then, the symbol we'd be looking at during resolution would be X. (Btw, I've seen this mistake many times--a change is made too early in the resolution pipeline, because it seems as if that's a quick way to get a new feature into the language, but then this bites later on.)

Anyhow, I feel okay with the error messages I did improve. At some point in the future, I expect that we'll revisit reveal statements to expand their role. That would be a good time to handle reveal in a way that's less hacky than today's reveal_-lemmas.

git-issue-5017b.dfy(94,18): Error: expression has no reveal lemma
9 resolution/type errors detected in git-issue-5017b.dfy
Loading
Loading