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

feat: Proof refactoring suggestions #5812

Merged

Conversation

fabiomadge
Copy link
Collaborator

Description

This PR adds suggestions for proof refactoring in two scenarios. It suggests moving single-use assertions into by-proofs and hiding functions that are unused for the proof of a method.

How has this been tested?

New tests

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

Copy link
Member

@keyboardDrummer keyboardDrummer left a comment

Choose a reason for hiding this comment

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

I think warnings that are turned off by default are anti-patterns. Turning them off by default implies that we do not recommend using this warning.

I think in this case the opposite is true: we do recommend using this warning! The only thing is that it drags down verification to do the analysis that enables it. I think it would be better to have a single option --analyse-proofs that turns on TrackVerificationCoverage and automatically all the warnings/suggestion that are associated with it.

We could then still have options that allow turning off some of the warning associated with --analyse-proofs, but I would be OK with not having those.

Source/DafnyCore/DafnyConsolePrinter.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
var factProvider = "";
var factConsumer = "";
var recomendation = "";
var completeInformation = true;
Copy link
Member

Choose a reason for hiding this comment

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

Can you say more about this completeInformation variable? What's an example program in which it is false?

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's when the dependency isn't of a type that we know how to handle.

Copy link
Member

Choose a reason for hiding this comment

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

Do you have an example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FunctionDefinitionDependency? No point in moving that.

Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
private static Dictionary<ProofDependency, HashSet<DafnyConsolePrinter.AssertCmdPartialCopy>>
ComputeAssertionsUsedByFact(string scopeName, IReadOnlyList<DafnyConsolePrinter.VerificationRunResultPartialCopy> vcResults) {
var assertionsUsedByFact = manager.GetPotentialDependenciesForDefinition(scopeName)
.Where(dep => dep is not EnsuresDependency)
Copy link
Member

Choose a reason for hiding this comment

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

What's the .Where for? Could you add a clarifying comment? If a fact is used to prove an ensures clause and an assertion (could be make sure we have a test for this?), then we should not recommend moving that fact to a by block right?

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's the other way around, proving something using on ensures doesn't make much sense, so we exclude it.

Copy link
Member

Choose a reason for hiding this comment

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

Is it a performance optimization then? Wouldn't assertionsProvenUsingFact[ensuresDependency].Any() always be false?

Also, do you do the same optimization on line 222?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Isn't this convenient... dafny-lang/blog#37 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Can you elaborate?

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's an example of an ensures dependency being the singular dependency. Didn't you ask for that?

Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
private static Dictionary<ProofDependency, HashSet<DafnyConsolePrinter.AssertCmdPartialCopy>>
ComputeAssertionsUsedByFact(string scopeName, IReadOnlyList<DafnyConsolePrinter.VerificationRunResultPartialCopy> vcResults) {
var assertionsUsedByFact = manager.GetPotentialDependenciesForDefinition(scopeName)
.Where(dep => dep is not EnsuresDependency)
Copy link
Member

Choose a reason for hiding this comment

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

Is it a performance optimization then? Wouldn't assertionsProvenUsingFact[ensuresDependency].Any() always be false?

Also, do you do the same optimization on line 222?

continue;
}

assertionsProvenUsingFact.TryAdd(factDependency, new HashSet<AssertCmdPartialCopy>());
Copy link
Member

Choose a reason for hiding this comment

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

Didn't you already fill this dictionary with values? Why do you sometimes need to add another value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but not with the function definition axioms

Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
Source/DafnyCore/ProofDependencyWarnings.cs Outdated Show resolved Hide resolved
var factProvider = "";
var factConsumer = "";
var recomendation = "";
var completeInformation = true;
Copy link
Member

Choose a reason for hiding this comment

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

Do you have an example?


bool IsNotSelfReferential(AssertCmdPartialCopy assert) =>
!manager.ProofDependenciesById.TryGetValue(assert.Id, out var assertDependency)
|| !(factDependency == assertDependency || factDependency is CallRequiresDependency req && req.call == assertDependency);
Copy link
Member

Choose a reason for hiding this comment

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

factDependency == assertDependency when does that occur? Is this for things like a loop invariant?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't count the assertion itself when considering what was needed to proof it.

Copy link
Member

Choose a reason for hiding this comment

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

I guess that this is not about a case where an expression is both an assumptions and assertion, as is the case with invariants, but this is about an assertion always being used in the VC in which it occurs.

Source/DafnyCore/ProofDependencyWarnings.cs Show resolved Hide resolved
Source/DafnyCore/Options/CommonOptionBag.cs Outdated Show resolved Hide resolved
@keyboardDrummer keyboardDrummer enabled auto-merge (squash) October 18, 2024 09:16
IsHidden = true
};
public static readonly Option<bool> AnalyzeProofs = new("--analyze-proofs", @"
Uses data from to prover to suggest ways to refine the proof:
Copy link
Member

@keyboardDrummer keyboardDrummer Oct 18, 2024

Choose a reason for hiding this comment

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

Maybe indicate that this only works for proofs that verify? Use data from verified proofs

I find refine ambiguous. I would go for to provide additional warnings and suggestions, or to warn about potential specification bugs and suggest changes to improve verification performance

Our documentation uses verifier, solver and prover. I'd use the term verifier (referring to the whole verification pipeline) or SMT solver in externally facing documentation.

Warning if any assertions are proved

I don't think that's valid English. I would use an imperative sentence Warn if ..

@keyboardDrummer
Copy link
Member

Approved and set to auto-merge. I had one remaining comment about documentation. If you want to address that we can do it in a follow-up.

@keyboardDrummer keyboardDrummer merged commit 64f5715 into dafny-lang:master Oct 18, 2024
22 checks passed
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