-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: wildcards #1049
feat: wildcards #1049
Conversation
Status: ready for review/merge |
primer/src/Primer/Core/DSL.hs
Outdated
case_ :: MonadFresh ID m => m Expr -> [m CaseBranch] -> m Expr | ||
case_ e brs = Case <$> meta <*> e <*> sequence brs <*> pure CaseExhaustive | ||
|
||
-- | A non-exhaustive case | ||
caseFB_ :: MonadFresh ID m => m Expr -> [m CaseBranch] -> m Expr -> m Expr | ||
caseFB_ e brs fb = Case <$> meta <*> e <*> sequence brs <*> (CaseFallback <$> fb) |
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.
We leave case_
alone to avoid churn.
primer/src/Primer/API.hs
Outdated
viewFallback = case fb of | ||
CaseExhaustive -> Nothing | ||
CaseFallback rhs -> | ||
let | ||
-- these IDs will not clash with any others in the tree, | ||
-- since node IDs in the input expression are unique, | ||
-- and don't contain non-numerical characters | ||
boxId = nodeId <> "Pwild" | ||
patternRootId = boxId <> "B" | ||
in | ||
Just $ | ||
Tree | ||
{ nodeId = boxId | ||
, body = | ||
BoxBody . RecordPair Flavor.Pattern $ | ||
( Tree | ||
{ nodeId = patternRootId | ||
, body = TextBody $ RecordPair Flavor.PatternWildcard (Name Nothing "_") |
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.
It is somewhat odd to render the wildcard match as a text "_" node...
Any better ideas?
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.
We are currently thinking that we should render this as a pattern box, but the contents being a NoBody
, with a new flavor of PatternWildcard
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.
We also considered not rending the pattern box at all in this case and going straight into the output. However, we realized that while this might be the nicest solution for the wildcard pattern, it wouldn't mesh well with deep patterns (once we can support them) that have nested wildcard patterns.
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.
We now render it as its own flavor with no body
primer/src/Primer/Action.hs
Outdated
addCaseBranch :: ActionM m => QualifiedText -> ExprZ -> m ExprZ | ||
addCaseBranch rawCon ze = case target ze of |
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.
In general, all our "add/delete case branches" actions (both high and low level) are (will) be offered/accepted on the case
node itself. This feels a bit odd but is I think the best we can do for now, since we don't have the ability to select one branch -- we can only select things in branches or binders in patterns. A different choice would be to give "add" at the root of the wildcard RHS and "delete" at the root of each non-wildcard branch's rhs
ba4cc75
to
bc1b6de
Compare
bc1b6de
to
fd25c52
Compare
fd25c52
to
69d75cc
Compare
This is setup for adding fallback branches / wildcard matches, which will add another constructor to the `CaseFallback'` type (and remove `CaseFallbackUseParams`). BREAKING CHANGE: this changes the AST of primer programs, which affects programs stored in a database and the strongly-typed API. Signed-off-by: Ben Price <[email protected]>
We make three changes: - `transformCaseBranches` now modifies the new (currently trivial) fallback/wildcard branches - define a new `transformNamedCaseBranches` that only modifies the non-fallback branches - define a new `transformNamedCaseBranch` that modifies only one particular branch Signed-off-by: Ben Price <[email protected]>
Signed-off-by: Ben Price <[email protected]>
This means that a pattern match can explicitly match on a subset of the constructors of a type, and give a final "catch-all" branch to match any other constructor. This does not give any extra expressivity, since it is possible to exhaustively list every branch, duplicating the output (since all ADTs are finitely branching). However, this is setup to enable pattern matching on primitives in future work, since primitives are infinitely (or impractically large, but finitely) branching. Note that we expose a new DSL combinator `caseFB_` instead of modifying the existing `case_` to avoid churn and the noise of having `CaseExhaustive` in many places. BREAKING CHANGE: this changes the AST of core primer terms, affecting programs stored in a database and the strongly-typed API. This also adds some new flavors to the OpenAPI. Signed-off-by: Ben Price <[email protected]>
BREAKING CHANGE: this adds a new action, which may show up in `Prog`'s logs, which are visible over the strongly-typed API and in saved databases. Signed-off-by: Ben Price <[email protected]>
BREAKING CHANGE: this adds a new action, which may show up in `Prog`'s logs, which are visible over the strongly-typed API and in saved databases. Signed-off-by: Ben Price <[email protected]>
BREAKING CHANGE: this expands the OpenAPI Signed-off-by: Ben Price <[email protected]>
Consider a pattern match which has only a falllback branch: `case e of _ -> t`; before this commit, evaluating this would force `e` to WHNF before reducing the `case`; after this commit this will reduce to `t` even if `e` diverges. Note that a case with at least one explict match must still force `e`, so one can recover the old behaviour by writing `case e of C x y z -> t ; _ -> t`, duplicating the `t`; however, this is only possible for non-empty types. We make this design decision so that when a future extension enables deep pattern matching the semantics are more obvious and consistent (since a wildcard is then the same as a pattern variable which happens not to be referenced). Signed-off-by: Ben Price <[email protected]>
As setup for enabling moving to a fallback branch, we introduce a notion of a `BranchMove`. This is currently just the `ValConName` of the branch to move to, but later will have another option to move to the fallback branch. BREAKING CHANGE: this changes actions, which are stored in programs logs. These are visible via the strongly-typed api and in saved databases. Signed-off-by: Ben Price <[email protected]>
BREAKING CHANGE: this changes actions, which are stored in programs logs. These are visible via the strongly-typed api and in saved databases. Signed-off-by: Ben Price <[email protected]>
As setup for enabling pattern matching on primitives, we introduce a notion of a `Pattern`. This is currently just a `ValConName` (since all pattern matches are specified up to alpha equivalence by their head constructor) , but later will have another option to be a `PrimCon` naming branches on a primitive type. BREAKING CHANGE: this changes actions, which are stored in programs logs. These are visible via the strongly-typed api and in saved databases. Signed-off-by: Ben Price <[email protected]>
BREAKING CHANGE: this expands the AST of primer programs, visible in stored databases and via the strongly-typed API; it also adds a new flavor to the OpenAPI. Signed-off-by: Ben Price <[email protected]>
BREAKING CHANGE: this changes actions, which are stored in programs logs. These are visible via the strongly-typed api and in saved databases. Signed-off-by: Ben Price <[email protected]>
69d75cc
to
03ffeca
Compare
Note that there is no way in our API to say "this action requires an argument, and the argument can be any Int except 3 or 7". Thus when adding a branch to a pattern match of primitive type, we cannot request a frontend to give a branch that does not currently exist. We will give an error if an attempt to add a currently-existing branch is made. It would be possible to augment the API's notion of `FreeInt` and `FreeChar` to include a finite list of illegal entries, but we leave this as a possibility for future work. BREAKING CHANGE: this expands the OpenAPI Signed-off-by: Ben Price <[email protected]>
Previously we emitted it as a TextBody with contents "_". This is a rendering decision that should be left to a frontend. BREAKING CHANGE: this changes the OpenAPI Signed-off-by: Ben Price <[email protected]>
03ffeca
to
2037646
Compare
No description provided.