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

"Validatable" trait for classes using the Valid()/Repr idiom #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
99 changes: 99 additions & 0 deletions examples/Frames.dfy
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;
}
}
}
2 changes: 2 additions & 0 deletions examples/Frames.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Dafny program verifier finished with 14 verified, 0 errors
42 changes: 42 additions & 0 deletions src/Frames.dfy
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 {
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

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 :)

// 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))
}
}
}
2 changes: 2 additions & 0 deletions src/Frames.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Dafny program verifier finished with 3 verified, 0 errors