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

Implement a proof-by-contradiction attribute #5001

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions Source/DafnyCore/ProofDependencyWarnings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ private static bool ShouldWarnVacuous(DafnyOptions options, string verboseName,
lit == false) {
return false;
}

if (poDep.ProofObligation is AssertStatementDescription { IsIntentionalContradiction: true }) {
return false;
}
}

// Ensures clauses are often proven vacuously during well-formedness checks.
Expand Down
4 changes: 2 additions & 2 deletions Source/DafnyCore/Verifier/BoogieGenerator.TrStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,14 @@ private void TrAssertStmt(PredicateStmt stmt, BoogieStmtListBuilder builder, Lis
var splits = TrSplitExpr(stmt.Expr, etran, true, out var splitHappened);
if (!splitHappened) {
var tok = enclosingToken == null ? GetToken(stmt.Expr) : new NestedToken(enclosingToken, GetToken(stmt.Expr));
var desc = new PODesc.AssertStatement(stmt.Expr, errorMessage, successMessage);
var desc = new PODesc.AssertStatementDescription(assertStmt, errorMessage, successMessage);
(proofBuilder ?? b).Add(Assert(tok, etran.TrExpr(stmt.Expr), desc, stmt.Tok,
etran.TrAttributes(stmt.Attributes, null)));
} else {
foreach (var split in splits) {
if (split.IsChecked) {
var tok = enclosingToken == null ? split.E.tok : new NestedToken(enclosingToken, split.Tok);
var desc = new PODesc.AssertStatement(stmt.Expr, errorMessage, successMessage);
var desc = new PODesc.AssertStatementDescription(assertStmt, errorMessage, successMessage);
(proofBuilder ?? b).Add(AssertNS(ToDafnyToken(flags.ReportRanges, tok), split.E, desc, stmt.Tok,
etran.TrAttributes(stmt.Attributes, null))); // attributes go on every split
}
Expand Down
16 changes: 11 additions & 5 deletions Source/DafnyCore/Verifier/ProofObligationDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public PreconditionSatisfied([CanBeNull] string customErrMsg, [CanBeNull] string
}
}

public class AssertStatement : ProofObligationDescriptionCustomMessages {
public class AssertStatementDescription : ProofObligationDescriptionCustomMessages {
public override string DefaultSuccessDescription =>
"assertion always holds";

Expand All @@ -341,14 +341,20 @@ public class AssertStatement : ProofObligationDescriptionCustomMessages {
public override string ShortDescription => "assert statement";

public override Expression GetAssertedExpr(DafnyOptions options) {
return predicate;
return AssertStatement.Expr;
}

private Expression predicate;
public AssertStmt AssertStatement { get; }

public AssertStatement(Expression predicate, [CanBeNull] string customErrMsg, [CanBeNull] string customSuccessMsg)
// We provide a way to mark an assertion as an intentional element of a
// proof by contradiction with the `{:contradiction}` attribute. Dafny
// skips warning about such assertions being proved due to contradictory
// assumptions.
public bool IsIntentionalContradiction => Attributes.Contains(AssertStatement.Attributes, "contradiction");

public AssertStatementDescription(AssertStmt assertStmt, [CanBeNull] string customErrMsg, [CanBeNull] string customSuccessMsg)
: base(customErrMsg, customSuccessMsg) {
this.predicate = predicate;
this.AssertStatement = assertStmt;
}

public override bool IsImplicit => false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %verify --warn-contradictory-assumptions "%s" > "%t"
// DIFF: "%s.expect" "%t"

type CodeUnit
type CodeUnitSeq = seq<CodeUnit>
type MinimalWellFormedCodeUnitSeq = s: CodeUnitSeq
| IsMinimalWellFormedCodeUnitSubsequence(s)
witness *

function IsMinimalWellFormedCodeUnitSubsequence(s: CodeUnitSeq): (b: bool)
ensures b ==>
&& |s| > 0
&& forall i | 0 < i < |s| :: !IsMinimalWellFormedCodeUnitSubsequence(s[..i])
decreases |s|

/**
* If minimal well-formed code unique subsequences `m1` and `m2` are prefixes of `s`, then they are equal.
*/
lemma LemmaUniquePrefixMinimalWellFormedCodeUnitSeq(
s: CodeUnitSeq, m1: MinimalWellFormedCodeUnitSeq, m2: MinimalWellFormedCodeUnitSeq
)
decreases |s|, |m1|, |m2|
requires m1 <= s
requires m2 <= s
ensures m1 == m2
{
// Handle only the |m1| <= |m2| case explicitly
if |m1| > |m2| {
LemmaUniquePrefixMinimalWellFormedCodeUnitSeq(s, m2, m1);
} else {
assert m1 <= m2;
assert m1 == m2 by {
var m2' := m2[..|m1|];
if m1 < m2 {
assert {:contradiction} m1 == m2';
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Dafny program verifier finished with 2 verified, 0 errors
Loading