Reinventing Dafny from scratch: Keywords function, method, compiled, ghost, predicate, lemma #1564
Replies: 5 comments 37 replies
-
I think we should incorporate discussion about other places where Also, I think there should be a default modifier for methods, for example I would also like to say that as part of this discussion that there should be net fewer keywords as a result of the proposal. It is difficult to keep track of the various modifiers and a matrix explaining all of them is not intuitive and hard to develop fluency in. |
Beta Was this translation helpful? Give feedback.
-
I'm still personally mulling over how we might ideally encode all the different tasty flavours of Dafny procedures (if I may risk using one of the remaining terms we HAVEN'T used as a keyword yet :). But fundamentally, I'm starting to lean towards the principle that everything should be compiled by default, and I suspect a decent number of current Dafny users are not going to like that (including @leino!), because it will make "pure" proofs in Dafny read a little less well and require a bit more typing. But here's my reasoning, which I'll propose as a Dafny language design tenant:
I'm hearing from lots of newer users of Dafny that it's just plain surprising that @MikaelMayer I had some thoughts around your particular proposed keywords, but I'll pause that for now as the wrench I'm throwing here probably forces a reset of our thoughts there anyway. :) |
Beta Was this translation helpful? Give feedback.
-
My newest suggestion, to move the discussion since I still would like to go and accept #1555 : In case functions are now compiled by default, and we encourage the use of "definition" instead of "ghost function", we would also change the following notation
to
and emit a warning on We do not no need to add the "compiled" keyword there, since it seems acceptable for most of us that methods are compiled. |
Beta Was this translation helpful? Give feedback.
-
Thanks, everyone, for the good discussion. Based on it, and thinking it over for quite some time, my conclusion is the following (which is mostly what is already suggested above). Let's start with a review of some concepts in Dafny. Expressions and statementsDafny distinguishes between expressions and statements. This is a frequent debate in programming languages, but for a verified language like Dafny, I think it makes good sense to keep this distinction. For example, it is desirable for specifications to be expressions. (If someone disagrees, then a debate on this topic is best done separately from the issue being discussed here.) Under this distinction, the body of a "function" is an expression and the body of a "method" is a list of statements. So that expressions can be used in verification, they need to be deterministic. This means that an expression Compiled and not compiledDafny also provides a simple mechanism to omit certain declarations from an executable program. This mechanism is what is called "ghost". This mechanism would be useful in any programming language, and it is especially important in a verified language. For example, Dafny includes specifications, which are always ghost, and this means there is never any run-time overhead for specifications. Also, having ghost contexts in the language allows the language to support proof authoring, which in general may not be computable. Finally, having the notion of ghost expressions in the language lets ghost contexts use things that aren't easily computable or aren't computable in a finite amount of time, like Dafny's "least predicates", which in general are not computable, and two-state expressions, which would require storing arbitrary amounts of data if they were to be present at run time. Declarations and keywordsGiven these two distinctions--functions vs methods, and compiled vs not compiled--here is a table that shows Dafny's current keywords:
Notes: A Here's what the same table would look like for Dafny 4, where 🆕 marks the changes from the current Dafny.
MigrationAs mentioned in the discussion above, it would be nice to provide some migration tools. Along those lines, but not necessarily as the only help for migration, we could (for a limited time, starting now and ending near the release of Dafny 4) offer a command-line option
and would generate an error upon encountering a plain keyword Function by methodThere is one more construct, which has been mentioned in the discussion above and has caused some confusion. It is the function FunctionSignatureAndSpecification {
Expr
} by method {
StmtList
} The whole point of this construct is to allow a function--which, remember, has the value of an expression, is deterministic, and does not make any allocations or changes in the heap--to be implemented by a list of (possibly nondeterministic, possibly allocating, possibly mutating) statements. The verification conditions associated with the function-by-method construct are such that any nondeterminism and heap use are unobservable to the caller of the function. This means that compiled code can use the So, then, should the syntax of function-by-method include a ghost function FunctionSignatureAndSpecification {
Expr
} by method {
StmtList
} I think this would be confusing, because the initial function FunctionSignatureAndSpecification { Expr } part as all being part of the function's specification. The by method { StmtList } part is of concern only to the implementation, so the caller need not worry about it. Note that, under this view, the Dafny 4 keyword A previous proposalIn light of this more updated discussion and grander vision, I think we should cancel #1555. |
Beta Was this translation helpful? Give feedback.
-
I very much like @RustanLeino's proposal above. Would it make sense, on top of that, to allow |
Beta Was this translation helpful? Give feedback.
-
Let's launch this discussion: Some notes first
Mismatch with existing function affordances: Every new single Dafny user is being told that a "function" is a "mathematical function", that it is not compiled.
This is an unexpected behavior. It differs with every other programming language, where a function, whether a lambda, a top-level function like in JavaScript or PHP, are always compiled and accessible from procedures (global code or class methods)
Two languages that ressemble: There is a huge overlap between the "ghost language" (i.e. mathematical specification) and the "compiled language" (i.e. program expressions), and we know that if a function definition matches both languages, it can furthermore be embodied and become a "function method", for which PR #1555 suggests a new appropriate set of keywords "compiled function". It seems that the "ghost language" includes the language as well, except for lemmas where the state cannot be modified.
Given all this information, let's take a step back. Although "compiled function" is appropriate for future versions of Dafny 3, here is the current array of keywords.
Current state of the art
We also acknowledge the useful feature of the almost "syntactic sugar:" below, where
F_method
is invoked at compile-time instead of F is every compiled context.If we were to reinvent Dafny keywords from scratch, I would strongly suggest that the keywords "ghost" and "compiled" are present to enforce some behaviors, that "compiled" is the default for function, methods and predicates, with the special exception that functions and predicates can be inferred as ghost only.
But in any case, here is a suggestion of the keywords we might use, in bold for things that are a bit new. One idea is to keep the duality between compiled and ghost.
Proposal
We could also have "procedure" instead of "method", but that's very minor.
Also, the language server should always suggest the simplest keyword, so if I prefix "function" with "ghost", it will suggest "expression" or "definition", but if I again prefix it with "compiled", it will suggest "function".
Now, the
function .... by method
would simply becomeexpression ... by method
,definition ... by method
orghost function ... by method
or evendefinition .... by compiled algorithm
..., whatever feels best (again, I would advise for the LS to suggest smallest keywords)Beta Was this translation helpful? Give feedback.
All reactions