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

feat: wildcards #1049

Merged
merged 15 commits into from
Jun 6, 2023
Merged

feat: wildcards #1049

merged 15 commits into from
Jun 6, 2023

Conversation

brprice
Copy link
Contributor

@brprice brprice commented May 30, 2023

No description provided.

@brprice
Copy link
Contributor Author

brprice commented May 30, 2023

Status: ready for review/merge

Comment on lines 160 to 167
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)
Copy link
Contributor Author

@brprice brprice May 30, 2023

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.

Comment on lines 830 to 847
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 "_")
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 is somewhat odd to render the wildcard match as a text "_" node...
Any better ideas?

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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

Comment on lines 744 to 832
addCaseBranch :: ActionM m => QualifiedText -> ExprZ -> m ExprZ
addCaseBranch rawCon ze = case target ze of
Copy link
Contributor Author

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

@brprice brprice force-pushed the brprice/wildcards branch 2 times, most recently from ba4cc75 to bc1b6de Compare May 30, 2023 15:31
@brprice brprice force-pushed the brprice/wildcards branch from bc1b6de to fd25c52 Compare June 1, 2023 16:44
@brprice brprice requested a review from georgefst June 1, 2023 16:44
@brprice brprice force-pushed the brprice/wildcards branch from fd25c52 to 69d75cc Compare June 1, 2023 16:48
brprice added 13 commits June 1, 2023 18:00
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]>
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]>
brprice added 2 commits June 1, 2023 20:07
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]>
@brprice brprice force-pushed the brprice/wildcards branch from 03ffeca to 2037646 Compare June 1, 2023 19:09
@dhess dhess added this pull request to the merge queue Jun 6, 2023
Merged via the queue into main with commit a99aa5e Jun 6, 2023
@dhess dhess deleted the brprice/wildcards branch June 6, 2023 10:21
@georgefst georgefst mentioned this pull request Aug 8, 2023
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.

3 participants