-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Juvix/Compiler/Core/Transformation/DetectConstantSideConditions.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Juvix.Compiler.Core.Transformation.DetectConstantSideConditions | ||
( detectConstantSideConditions, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Core.Extra | ||
import Juvix.Compiler.Core.Transformation.Base | ||
|
||
detectConstantSideConditions :: Module -> Module | ||
detectConstantSideConditions md = mapAllNodes (umap go) md | ||
where | ||
boolSym = lookupConstructorInfo md (BuiltinTag TagTrue) ^. constructorInductive | ||
|
||
go :: Node -> Node | ||
go node = case node of | ||
NMatch m -> NMatch (over matchBranches (concatMap convertMatchBranch) m) | ||
_ -> node | ||
|
||
convertMatchBranch :: MatchBranch -> [MatchBranch] | ||
convertMatchBranch br@MatchBranch {..} = | ||
case _matchBranchRhs of | ||
MatchBranchRhsExpression {} -> | ||
[br] | ||
MatchBranchRhsIfs ifs -> | ||
case ifs1 of | ||
[] -> | ||
case nonEmpty ifs0 of | ||
Nothing -> [] | ||
Just ifs0' -> [set matchBranchRhs (MatchBranchRhsIfs ifs0') br] | ||
SideIfBranch {..} : ifs1' -> | ||
let ifsBody = mkIfs boolSym (map (\(SideIfBranch i c b) -> (i, c, b)) ifs0) _sideIfBranchBody | ||
in set matchBranchRhs (MatchBranchRhsExpression ifsBody) br | ||
: | ||
-- All branches after the first true branch are redundant | ||
-- and can be removed. We leave one of the redundant | ||
-- branches to make redundant pattern detection work in this | ||
-- case. | ||
case ifs1' of | ||
[] -> [] | ||
if1 : _ -> [set matchBranchRhs (MatchBranchRhsExpression (if1 ^. sideIfBranchBody)) br] | ||
where | ||
ifs' = filter (not . isFalseConstr . (^. sideIfBranchCondition)) (toList ifs) | ||
(ifs0, ifs1) = span (not . isTrueConstr . (^. sideIfBranchCondition)) ifs' |