Skip to content

Commit

Permalink
work on some more
Browse files Browse the repository at this point in the history
  • Loading branch information
TimWhiting committed Jul 10, 2024
1 parent 781e60d commit 3cd2901
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/Core/MatchMerge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mergeBranches :: [Branch] -> Unique ([Branch], Bool)
-- No branches, no changes
mergeBranches [] = return ([], False)
-- Skip branches with complex guards (in the future we can optimize to merge guards)
mergeBranches (b@(Branch [pat@PatCon{patConPatterns=ps}] guard):bs) | not (all isTrueGuard guard) =
mergeBranches (b@(Branch [pat@PatCon{patConPatterns=ps}] guard):bs) | not (all isTrueGuard guard) = do
mergeBranches bs >>= (\(bs', v) -> return (b:bs', v))
-- Branch with constructor pattern, try to merge it with the rest
mergeBranches branches@(b@(Branch [pat@PatCon{patConPatterns=ps}] _): rst) =
Expand Down Expand Up @@ -294,11 +294,11 @@ getReplaceMap discriminatingVars template p'
-- introduce a new variable using the template's name, and map the other name to the template
rp' = (tn2, Var tn1 InfoNone):rp in
case pat' of
Nothing -> -- Differs
-- Doesn't discriminate, but do need to propagate
Nothing -> --
-- Differs but doesn't discriminate
if tn1 `notElem` discriminatingVars then (Nothing, rp')
-- Introduce just a wild?
else (Just [PatWild], rp')

else error "MatchMerge: getReplaceMap. If a pattern discriminates than it should not be part of the template"
Just _ -> -- Use the new pattern
(pat', rp')
(PatWild, PatWild) -> (Nothing, [])
Expand Down
21 changes: 20 additions & 1 deletion test/cgen/match-merge.kk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ fun reflow(e: list<int>)
Cons(a, Nil) -> a.show.println
Nil -> "Nothing".println

fun no-guard(e: list<int>)
match e
Cons(a, b) | a == 0 -> b.println
Cons(b, c) | b == 1 -> c.println
Nil -> e.println

fun named-subpatterns(e: list<list<int>>)
match e
Cons(n0 as Nil, Nil) -> n0.println
Cons(n1 as Cons(0, Nil), Nil) -> n1.println

fun main()
try {
implicit-error-fallthrough([]) // Nothing
Expand All @@ -24,4 +35,12 @@ fun main()
reflow([]) // Nothing
reflow([3,2]) // 2
reflow([2,1,2]) // 4
reflow([1]) // 1
reflow([1]) // 1

no-guard([0, 1]) // [1]
no-guard([1, 2]) // [2]
no-guard([]) // []

named-subpatterns([[]]) // []
named-subpatterns([[0]]) // [0]

0 comments on commit 3cd2901

Please sign in to comment.