Skip to content

Commit

Permalink
Match matching matches other matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
robrix committed Oct 17, 2020
1 parent d8626a0 commit bb43930
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/Facet/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,19 @@ case' s cs = case getFirst (foldMap (\ (p, f) -> First $ f <$> match s
_ -> error "non-exhaustive patterns in lambda"

match :: Value a -> Pattern (Value a) b -> Maybe (Pattern (Value a) (Value a))
match s = \case
Wildcard -> Just Wildcard
Var _ -> Just (Var s)
Tuple [pl, pr]
| Prd l r <- s -> do
l' <- match l pl
r' <- match r pr
Just $ Tuple [l', r']
Tuple _ -> Nothing
Con n ps
| VCon n' fs <- s -> do
guard (tm n == tm n')
-- NB: we’re assuming they’re the same length because they’ve passed elaboration.
Con n' <$> sequenceA (zipWith match (toList fs) ps)
| otherwise -> Nothing
match = curry $ \case
(_, Wildcard) -> Just Wildcard
(s, Var _) -> Just (Var s)
(Prd l r, Tuple [pl, pr]) -> do
l' <- match l pl
r' <- match r pr
Just $ Tuple [l', r']
(_, Tuple _) -> Nothing
(VCon n' fs, Con n ps) -> do
guard (tm n == tm n')
-- NB: we’re assuming they’re the same length because they’ve passed elaboration.
Con n' <$> sequenceA (zipWith match (toList fs) ps)
(_, Con _ _) -> Nothing


elim :: HasCallStack => Value a -> Elim (Value a) -> Value a
Expand Down

0 comments on commit bb43930

Please sign in to comment.