Skip to content

Conversation

RustanLeino
Copy link
Collaborator

This PR aims to help stabilize verification by filling in matching patterns for the quantifiers introduced by automatic induction to represent the induction hypothesis. It also suppresses the generation of the induction hypothesis if no such matching patterns are found.

Full description

This PR computes matching patterns for the quantification that's about to be used with automatic induction. If there are no matching patterns, the induction hypothesis is not added. Tooltips or warnings show the patterns or announce the lack thereof.

The PR no longer uses arrow-typed variables as induction variables. (If a user really wants them, an {:induction ...} attribute can be given manually.)

Treat this more like other parameters when computing induction variables.

With this PR, ternary expressions (that is, _ ==#[_] _ and _ !=#[_] _) are considered as candidate trigger expressions. In addition, a codatatype == (which is defined by Dafny as a greatest predicate) is considered as a focal predicate for extreme predicates.

The PR also fixes a crash in trigger selection when the candidate expression has a lambda expression.

Finally, the "selected trigger" tooltip is extended to also show the t := e binding for any bound variable added as part of a quantifier rewrite.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Copy link
Member

@cpitclaudel cpitclaudel left a comment

Choose a reason for hiding this comment

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

Here is a first pass with mostly cosmetic details. I'm currently making my way through the core computation.

(ToNatRight([]) * BASE() + First(xs1)) * BASE() + First(xs);
{ reveal ToNatRight(); }
(0 * BASE() + First(xs1)) * BASE() + First(xs);
}
Copy link
Member

Choose a reason for hiding this comment

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

Why isn't the right proof here to rewrite xs as [xs[0], xs[1]]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see how that rewrite helps any. It's nice to have a name for the expression [xs[1]]. But I removed the assertion assert DropFirst(xs1) == [];, which wasn't needed.

@@ -0,0 +1,5 @@
Fill in matching patterns for the quantifiers introduced by automatic induction to represent the induction hypothesis. Suppress the generation of the induction hypothesis if no such matching patterns are found. Enhance tooltips accordingly. This feature is added to make stabilize verification, but by sometimes not generating induction hypotheses, some automatic proofs may no longer go through.
Copy link
Member

Choose a reason for hiding this comment

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

Typo: "is added to make stabilize verification"

Shouldn't we include an example or two?

let-expressions.dfy(8,8): Info: Selected triggers: {s[_t#0], s[i]}
let-expressions.dfy(9,8): Info: Selected triggers: {s[_t#0], s[i]}
let-expressions.dfy(8,8): Info: Selected triggers: {s[_t#0], s[i]} where _t#0 := i + 1
let-expressions.dfy(9,8): Info: Selected triggers: {s[_t#0], s[i]} where _t#0 := i + 1
Copy link
Member

Choose a reason for hiding this comment

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

These temporary variable names appeared when we started supporting automatic rewriting of matching loops, but the trigger that we add is (almost?) always the same for this new variable as it was for the original variable that it looped with (after all, it was precisely because the new term caused a loop with the trigger of the first one that we created a new variable).

Given this, shouldn't we just hide that redundant trigger and print only {s[i]}, rather that showing the temporary variable with the additional where clause?

Copy link
Member

Choose a reason for hiding this comment

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

We used to print something like

loop-detection-messages--unit-tests.dfy(12,9): Warning: Selected triggers: {f(i)} (may loop with "f(i + 1)")
 /!\ Suppressing loops would leave this expression without triggers.

Once these loops were eliminated by rewriting we returned to normal messages, but I'm not sure that this was a purposeful choice (6904c90). Perhaps we should print something like "potential matching loop with … eliminated by rewriting"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It seems I've been confused about the role of these names, and indeed about their worth as well. For now, I just back out my changes that had added printing of where t#0 := ... tooltips. Separately from this PR, we should rethink whether or not we really want to do these rewrites.

inductionVariables.Add(new IdentifierExpr(n.Tok, n));
}
}

if (inductionVariables.Count != 0) {
List<List<Expression>> triggers = null;
if (lemma != null) {
triggers = ComputeInductionTriggers(inductionVariables, body, lemma.EnclosingClass.EnclosingModuleDefinition);
Copy link
Member

Choose a reason for hiding this comment

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

I find the logic here a bit hard to follow: we have ComputeInductionTriggers and ComputeAndReportInductionTriggers and ReportInductionTriggers, and two sets of calls for each of these methods. Is the duplication necessary? Can we add a comment why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added a comment. (The reason is to get the tooltips to come out in a nice order.)


Reporter.Message(MessageSource.Rewriter, warningLevel, null, tok,
$"Could not find a trigger for the induction hypothesis. Without a trigger, this may cause brittle verification. " +
$"Change or remove the {{:induction}} attribute to generate a different induction hypothesis, or add {{:nowarn}} to silence this warning. " +
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to make it produce a quantifier without triggers (and thus recover the previous behavior?)

Copy link
Member

@cpitclaudel cpitclaudel left a comment

Choose a reason for hiding this comment

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

OK, I finished reading through. It looks good to me! Beyond stylistic concerns that I left in individual comments, my main worry is the impact that this will have on customers: how many proofs will be broken, and how good a transition path are we giving them? Is there an easy way to recover the previous behavior (e.g. what if we made :induction true do that, though with a warning?

(When we started generating triggers automatically we did so under a flag at first.)

- Mention --manual-lemma-induction
- Mention {:inductionTrigger}
@cpitclaudel cpitclaudel force-pushed the triggers-for-auto-induction branch from 6b37d78 to cf8c9ec Compare October 24, 2024 09:15
@cpitclaudel cpitclaudel force-pushed the triggers-for-auto-induction branch from cf8c9ec to ffe54af Compare October 24, 2024 09:18
@cpitclaudel
Copy link
Member

I think this is ready for review. We can do the forall statement generation in a separate PR. I tried to split things into relevant commits, so reviewing them one by one might be best.

Also: I'm not sure how to regenerate the .doo files: running the recommended make command creates errors:

$ make -C Source/DafnyStandardLibraries update-binary
make: Entering directory 'Source/DafnyStandardLibraries'
dotnet run --project ../Dafny --no-build --roll-forward LatestMajor -- build -t:lib --hidden-no-verify=false  src/Std/dfyconfig.toml --output:build/DafnyStandardLibraries.doo
[…]
src/Std/Arithmetic/LittleEndianNat.dfy[ParametricConversion](113,30): Error: member 'First' does not exist in top-level module declaration '_default'
    |
113 |     ensures ToNatRight(xs) == First(xs)
    |                               ^^^^^

# Conflicts:
#	Source/DafnyCore/Verifier/BoogieGenerator.ExpressionTranslator.cs
#	Source/DafnyCore/Verifier/Statements/BoogieGenerator.TrForallStmt.cs
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-cs.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-go.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-java.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-js.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-notarget.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-py.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries.doo
#	Source/IntegrationTests/TestFiles/LitTests/LitTest/dafny0/CoinductiveProofs.dfy.expect
* By using _inductionTrigger for generated triggers, the Dafny machinery for cloning things into refinement modules works correctly
* Tooltips only show things not already in the program text
@RustanLeino
Copy link
Collaborator Author

@cpitclaudel Thanks for your help. I think everything has been addressed now.

@RustanLeino RustanLeino enabled auto-merge (squash) October 31, 2024 20:16
RustanLeino and others added 6 commits October 31, 2024 14:29
# Conflicts:
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-cs.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-go.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-java.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-js.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-notarget.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries-py.doo
#	Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries.doo
cpitclaudel
cpitclaudel previously approved these changes Nov 7, 2024
Copy link
Member

@cpitclaudel cpitclaudel left a comment

Choose a reason for hiding this comment

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

The follow-up changes look good, thanks. I merged with main.

@RustanLeino RustanLeino merged commit e77b51e into dafny-lang:master Nov 8, 2024
22 checks passed
@RustanLeino RustanLeino deleted the triggers-for-auto-induction branch November 12, 2024 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants