-
Notifications
You must be signed in to change notification settings - Fork 25
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
"Validatable" trait for classes using the Valid()/Repr idiom #22
Open
robin-aws
wants to merge
2
commits into
dafny-lang:master
Choose a base branch
from
robin-aws:frames
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
|
||
// RUN: %dafny /compile:0 "%s" > "%t" | ||
// RUN: %diff "%s.expect" "%t" | ||
|
||
include "../src/Frames.dfy" | ||
|
||
module ValidatableExample { | ||
|
||
import opened Frames | ||
|
||
// Example loosely based on Chapters 8 and 9 of "Using Dafny, an Automatic Program Verifier": | ||
// http://leino.science/papers/krml221.pdf | ||
|
||
class Cell extends Validatable { | ||
// public variables | ||
var Data: int; | ||
|
||
function Valid(): bool | ||
reads this, Repr | ||
{ | ||
Repr == {this} | ||
} | ||
|
||
constructor() | ||
ensures Valid() && fresh(Repr - {this}) | ||
ensures Data == 0 | ||
{ | ||
Data := 0; | ||
Repr := {this}; | ||
} | ||
|
||
method Inc() | ||
requires Valid() | ||
modifies Repr | ||
ensures ValidAndDisjoint() | ||
ensures Data == old(Data) + 1 | ||
{ | ||
Data := Data + 1; | ||
} | ||
} | ||
|
||
class Counter extends Validatable { | ||
// public variables | ||
ghost var Value: int; | ||
// private variables | ||
var incs: Cell; | ||
var decs: Cell; | ||
|
||
function Valid(): bool | ||
reads this, Repr | ||
{ | ||
&& this in Repr | ||
&& ValidComponent(incs) | ||
&& ValidComponent(decs) | ||
&& incs.Repr !! decs.Repr | ||
&& Value == incs.Data - decs.Data | ||
} | ||
|
||
constructor() | ||
ensures Valid() && fresh(Repr - {this} - incs.Repr - decs.Repr) | ||
ensures Value == 0 | ||
{ | ||
incs := new Cell(); | ||
decs := new Cell(); | ||
Value := 0; | ||
new; | ||
Repr := {this} + incs.Repr + decs.Repr; | ||
} | ||
|
||
method GetValue() returns (x: int) | ||
requires Valid() | ||
ensures x == Value | ||
{ | ||
x := incs.Data - decs.Data; | ||
} | ||
|
||
method Inc() | ||
requires Valid() | ||
modifies Repr | ||
ensures ValidAndDisjoint() | ||
ensures Value == old(Value) + 1 | ||
{ | ||
incs.Inc(); | ||
Value := Value + 1; | ||
Repr := {this} + incs.Repr + decs.Repr; | ||
} | ||
|
||
method Dec() | ||
requires Valid() | ||
modifies Repr | ||
ensures ValidAndDisjoint() | ||
ensures Value == old(Value) - 1 | ||
{ | ||
decs.Inc(); | ||
Value := Value - 1; | ||
Repr := {this} + incs.Repr + decs.Repr; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
Dafny program verifier finished with 14 verified, 0 errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// RUN: %dafny /compile:0 "%s" > "%t" | ||
// RUN: %diff "%s.expect" "%t" | ||
|
||
/******************************************************************************* | ||
* Copyright by the contributors to the Dafny Project | ||
* SPDX-License-Identifier: MIT | ||
*******************************************************************************/ | ||
|
||
module Frames { | ||
|
||
// A trait for objects with a Valid() predicate. Necessary in order to | ||
// generalize some proofs, but also useful for reducing the boilerplate | ||
// that most such objects need to include. | ||
trait {:termination false} Validatable { | ||
// Ghost state tracking the common set of objects most | ||
// methods need to read. | ||
ghost var Repr: set<object> | ||
|
||
predicate Valid() | ||
reads this, Repr | ||
ensures Valid() ==> this in Repr | ||
|
||
// Convenience predicate for when your object's validity depends on one | ||
// or more other objects. | ||
predicate ValidComponent(component: Validatable) | ||
reads this, Repr | ||
{ | ||
&& component in Repr | ||
&& component.Repr <= Repr | ||
&& this !in component.Repr | ||
&& component.Valid() | ||
} | ||
|
||
// Convenience predicate, since you often want to assert that | ||
// new objects in Repr are fresh as well in most postconditions. | ||
twostate predicate ValidAndDisjoint() | ||
reads this, Repr | ||
{ | ||
Valid() && fresh(Repr - old(Repr)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
Dafny program verifier finished with 3 verified, 0 errors |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that mean you're turning off the termination checker for this trait? Termination is an important correctness property, and I wouldn't feel comfortable using a standard library that adds such axioms without clear documentation on why it does so, why it's really needed, and why it's safe.
I haven't thought it through, but did you try to re-enable the termination checker and to prove termination using a decreasing
Repr
set?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I wrote this so long ago I got used to seeing that. :) It's not turning off termination checking for this trait nor the classes inheriting it. It's only necessary in order to allow any class to extend this trait at all. This appears to be because allowing a class to extend a trait from a different module introduces unsoundness in the termination checking somehow - I'm only going by the error message here as this attribute doesn't appear to be documented.
This is still a good reason to block on doing better though. I know Rustan's talked about this unsoundness not being hard to fix so we'll at least document it better if not fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rustan has kindly provided LOTS of detail on why we have
{:termination false}
: dafny-lang/dafny#1588 :)