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

Add Validatable trait #329

Closed
wants to merge 9 commits into from
Closed

Add Validatable trait #329

wants to merge 9 commits into from

Conversation

robin-aws
Copy link
Contributor

Closes #319

See issue for motivation.

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

@lavaleri
Copy link
Contributor

AWS CodeBuild CI Report

  • CodeBuild project: DafnyESDK
  • Commit ID: 26d30ec
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@lavaleri
Copy link
Contributor

AWS CodeBuild CI Report

  • CodeBuild project: DafnyESDK
  • Commit ID: d476453
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@lavaleri
Copy link
Contributor

AWS CodeBuild CI Report

  • CodeBuild project: DafnyESDK
  • Commit ID: 20e91cd
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@robin-aws robin-aws linked an issue May 23, 2020 that may be closed by this pull request
Comment on lines +39 to +42
// TODO-RS: Better name?
twostate predicate ValidAndFresh()
reads this, Repr
ensures ValidAndFresh() ==> Valid() && fresh(Repr - old(Repr))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a benefit to writing ValidAndFresh() as opposed to Valid() && Fresh()? The former doesn't seem much more convenient, whereas the latter is easier to name (except perhaps for confusion between fresh() and Fresh()).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's questionable whether this is worth the trouble. I definitely DON'T think just "Fresh" is the right word anyway, so I'd rather come up with better or just drop this predicate. I'm more just observing this is a very common pattern.

Copy link
Contributor

@RustanLeino RustanLeino left a comment

Choose a reason for hiding this comment

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

This is great, since it gives us a central place to talk about the standard dynamic-frames idiom. Two general questions I have are:

  • All members of trait Validatable are ghost. Yet, I imagine that there will be some code generated (in C# and Java) that says a class implements this empty interface. Does that have any negative consequences? (If so, we could think of some compiler directive that would say to ignore traits with only ghost members.)

  • I'm thinking there may be a better name than Validable. Given the ValidComponent member, should the trait perhaps have a name like Composite?

Comment on lines +13 to +14
// TODO-RS: The style guide actually implies this should be "repr",
// but the convention is well-established at this point.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should update the style guide. I suggest it be updated to say that "public" fields (or, at least, public ghost fields) be capitalized, where "public" means "intended to be used and understood by clients".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should that also align with the set of symbols you export then?


// Convenience predicate for when your object's validity depends on one
// or more other objects.
predicate ValidComponent(component: Validatable)
Copy link
Contributor

Choose a reason for hiding this comment

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

Having convenience predicates like this is fantastic!

Comment on lines +25 to +29
ensures ValidComponent(component) ==> (
&& component in Repr
&& component.Repr <= Repr
&& this !in component.Repr
&& component.Valid())
Copy link
Contributor

Choose a reason for hiding this comment

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

This postcondition is not necessary, since it just restates the body and functions are transparent (non-opaque) in Dafny (unless you limit visibility with an export clause, but you're not).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was convinced this was necessary for validation for some reason, perhaps it was confounded with whatever the real fix was. Will try removing.

// TODO-RS: Better name?
twostate predicate ValidAndFresh()
reads this, Repr
ensures ValidAndFresh() ==> Valid() && fresh(Repr - old(Repr))
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto. This postcondition is not necessary.

@robin-aws
Copy link
Contributor Author

  • All members of trait Validatable are ghost. Yet, I imagine that there will be some code generated (in C# and Java) that says a class implements this empty interface. Does that have any negative consequences? (If so, we could think of some compiler directive that would say to ignore traits with only ghost members.)

None that I can see. Empty "marker" interfaces are definitely already a thing (e.g. Java's RandomAccess interface). This may fall into the general bucket of hiding compiled components that aren't explicitly externed from target language code: #244 That is, I'll be even less worried if the compiled name is __DAFNY_INTERNAL__Validatable.

  • I'm thinking there may be a better name than Validable. Given the ValidComponent member, should the trait perhaps have a name like Composite?

Agreed, although I think Composite is too narrow - that implies a validatable object has to either contain others or be contained by others, and there are examples in here like SeqReader that are completely self-contained.

It's also worth noting that as defined, these kind of have to be "mutably validatable" because it forces this in Repr, whereas some classes like CacheEntryEncrypt have a Valid() method that does not read the heap at all. I tried adding the reads clause to classes like that and it became a rathole to keep everything validating so I avoided it. I also experimented with supporting empty Repr sets but didn't get too far, and I'm not sure if that's worthwhile. Finally, if we add support for traits on datatypes, I'm not sure whether this trait would ever apply and if we care.

If this is codifying the dynamic-frames idiom, should we call it Framed? :)

@robin-aws
Copy link
Contributor Author

If this is codifying the dynamic-frames idiom, should we call it Framed? :)

On second thought that conflicts pretty badly with message body frames. :)

@robin-aws
Copy link
Contributor Author

Note that having this in a public Dafny standard library might help guide new users to use the idioms: dafny-lang/dafny#292

@robin-aws
Copy link
Contributor Author

I've cut a standard library PR for this instead now, so it makes more sense to use that version if it gets in: dafny-lang/libraries#22

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.

Define a common Validatable trait
5 participants