-
Notifications
You must be signed in to change notification settings - Fork 126
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
Numeric Constraint Guards #1380
Conversation
I tried these two examples:
The first one type checked correctly, although I got a panic:
Is that because the evaluator is not yet implemented? The second one also works correctly from the point of view of this extension, but it reveals some additional facts we need to |
@Riib11 I don't think we need the
This is all I changed:
|
The first example doesn't work because you didn't quantify |
Awesome! I had added that keyword to deal with an issue getting happy to accept my grammar and couldn't find a better way, but looks like you found one. |
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.
I think we are almost there, just fix up the error
stuff
Update to master
* Annotate each property with a location * Disallow property guards with index declarations (maybe this could be made to work, but be conservative for now) * Require signatures on all members of a recursive group with prop guards
This PR implements a new feature: numeric constraint guards.
Addresses #701 and (partially) #1369
Numeric constraint guards are like guards (e.g. Haskell) except that the guarding condition is a numeric constraint that is treated as locally assumed (for the sake of type-checking) in the branch it guards.
Without this feature,
f
would have to be written verbosely and awkwardly like so asf'
:For more complicated constraints, this second kind of implementation becomes more and more cumbersome.
Examples
The following function
inits
computes the concatenation of all initial sequences of a given sequence, using a tail-recursive style.// TODO: more cumbersome example
// TODO: practical everyday examples
Implementation
The significance of this feature is that the guarding numeric constraints are introduced as assumptions locally to the guarded expression.
Immediately after parsing (and the NoPat pass), each guarded expression is expanded into an auxiliary top-level declaration that has the same type signature as the declaration the constraint guards appeared in, but with the guarding constraints appended to its signature's constraint. The original guarded expression is replaced with an appropriate reference to this auxiliary definition.
During typechecking, the guarding constraints of each case are assumed locally before typechecking the guarded expression.
During evaluation, the constraint guards are folded over until a constraint evaluates to
True
(viaevalProp
), in which case only that constraints's guarded expression is evaluated as the result.TODO
prime
) and conjunctions. SeeCryptol.TypeCheck.Infer.checkSigB
for the implementation. Issues a warning if cannot prove exhaustive.Exclusivity checking for a declaration's set of guarding constraints