-
Notifications
You must be signed in to change notification settings - Fork 29
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
Hackeython: ADTs in JML #3427
base: main
Are you sure you want to change the base?
Hackeython: ADTs in JML #3427
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #3427 +/- ##
============================================
- Coverage 37.77% 37.75% -0.03%
+ Complexity 17031 17030 -1
============================================
Files 2076 2076
Lines 126950 127017 +67
Branches 21381 21388 +7
============================================
- Hits 47952 47951 -1
- Misses 73092 73159 +67
- Partials 5906 5907 +1 ☔ View full report in Codecov by Sentry. |
One of the primary todos might be to prepare a proposal for JMLref. |
OpenJML used "," where Daniel @Drodt proposed "|". Small detail. I think in the face of enums, "," would fit better into the Java biotope. |
I just had a look at the axioms created for ADTs. There seems to be an unexpected "not free in" condition.
|
Discussion at the HackKeYthon:
|
Thanks!
Can you elaborate what you mean by free here?
What are the issues when not pattern matching?
What intuition? How do you plan to merge?
In what way? Which selectors?
Any progress on that front? Did you consider the more type class oriented approach or is that too dissimilar to Java? |
After implementing switch expressions into the parser for key files, we discussed how it should be represented as a term in key and the semantics of a switch expression switch expressions as a termWe propose to add the terms
Adding the case term is necessary so switch can have arbitrarily many cases. Default cases stating a catch all value and no default cases can be used to close a cascade of cases. switch semanticsInstead of giving the semantics of the newly introduced terms we give the semantics of an example switch statement (I also upload the discussion from the whiteboard so nothing is lost) The expression
should evaluate to Here @mattulbrich proposed to use the Hilbert |
Free here means that different ground terms denote different semantic values. Like a language with concatenation the free monoid on the alphabet (words are equal only if the same sequence of letters. Lists in this sense are free (wrt. to their constrcutors) while finite sets are not ({1,2} being the same as {2,1}) (wrt to insert and empty).
In the example of a list: Calling
The similarity with enums would obviously lead to a comma as separator between constructors. 😛
Selectors = Destructors
See elsewhere ... |
Another important observation is the following: In order to avoid name clashes, constructors need to be prefixed with the type. I.e. for
one needs to write
Now |
Thanks, @philipppk! A few more explaining words: When translating such switch-case JML expressions into JavaDL, we can use existing infrastructure or use new infrastructure. Using the Hilbert-Epsilon operator is nice because it reuses existing structures. However, during normalisation the structure of the argument of the operator may be modified such that specialised taclet or pretty printing may no longer apply. And the epsilon-presentation is not exactly user-friendly. When using a specialised JavaDL syntax with new constructors (cases, nodefault, default, switch), normalisation cannot destroy the structure of expression, yet we need many new rules to deal with the situation. At any rate: "if" should/could be retrofitted as a special case of switch case. |
I would like to to tell David Cok from openjml about these advances and would like to mention that syntax-wise the languages are compatible. This would mean that we could perhaps support both |
Followup in next KeY meeting: We could introduce a switch operator per term with the appropriate signature thus avoiding a vari-arity symbol. |
Actually, it turns out that this can be generally implemented into KeY generally w/o touch a single line of Java code. The example switch.key (pls remove the file extension .txt) shows how a new function symbol Those are not part of KeY at the moment, so I added a new type binding (whose intuition is any→any) with a constructor Since KeY does not support parameterised types, it is all a bit ugly regarding the typing, but still sound. |
I started with the JML implementation part.
|
Short floor discussion:
|
Just a quick thought: Why do we invent our own ADT stuff when there are established tools for functional verification? Would it not be funny to have something like "functional Java" (which is obviously F*/Haskell with Java-style Syntax)? (Or do we have reached this point with allowing more stuff in model methods?) |
9d9603b
to
6c83a9a
Compare
That is indeed the idea of switch expressions, model methods and ADT declarations. ... |
Intended Change
Based on #3420. Changes the JML pareser to accept definitions of ADTs. Datatypes have a list of constructors separated by
|
, and a body of functions.Currently, ADTs can only be defined on class-level, not inside methods or outside of classes.
There also is no semantics for these ADTs. KeY simply ignores them. Ideally, we want to use the defined types and functions in specification and translate them to KeY ADTs.
We also want to use the new Java switch expressions to allow pattern matching on ADTs, i.e,:
Open questions are:
l1 + l2
forl1.append(l2)
?List
class/interface?OpenJML has a similar concept of ADTs and pattern matching. For demonstration, there is a list example.
Type of pull request
Ensuring quality
Additional information and contact(s)
The contributions within this pull request are licensed under GPLv2 (only) for inclusion in KeY.