-
Notifications
You must be signed in to change notification settings - Fork 51
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
67 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/Juvix/Compiler/Core/Transformation/Optimize/CasePermutation.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,65 @@ | ||
module Juvix.Compiler.Core.Transformation.Optimize.CasePermutation (casePermutation) where | ||
|
||
import Data.HashMap.Strict qualified as HashMap | ||
import Data.HashSet qualified as HashSet | ||
import Juvix.Compiler.Core.Extra | ||
import Juvix.Compiler.Core.Transformation.Base | ||
|
||
isConstructorTree :: Case -> Node -> Bool | ||
isConstructorTree c node = case run $ runFail $ go mempty node of | ||
Just ctrsMap -> | ||
all (checkOne ctrsMap) tags && checkDefault ctrsMap (c ^. caseDefault) | ||
Nothing -> False | ||
where | ||
tags = map (^. caseBranchTag) (c ^. caseBranches) | ||
|
||
checkOne :: HashMap Tag Int -> Tag -> Bool | ||
checkOne ctrsMap tag = case HashMap.lookup tag ctrsMap of | ||
Just 1 -> True | ||
Nothing -> True | ||
_ -> {- isImmediate -} False | ||
|
||
checkDefault :: HashMap Tag Int -> Maybe Node -> Bool | ||
checkDefault ctrsMap = \case | ||
Just {} -> | ||
-- or isImmediate | ||
sum (HashMap.filterWithKey (\k _ -> not (HashSet.member k tags')) ctrsMap) <= 1 | ||
where | ||
tags' = HashSet.fromList tags | ||
Nothing -> True | ||
|
||
go :: (Member Fail r) => HashMap Tag Int -> Node -> Sem r (HashMap Tag Int) | ||
go ctrs = \case | ||
NCtr Constr {..} -> return $ HashMap.alter (Just . maybe 1 (+ 1)) _constrTag ctrs | ||
NCase Case {..} -> foldM go ctrs (map (^. caseBranchBody) _caseBranches) | ||
_ -> fail | ||
|
||
convertNode :: Node -> Node | ||
convertNode = dmap go | ||
where | ||
go :: Node -> Node | ||
go node = case node of | ||
NCase c@Case {..} -> case _caseValue of | ||
NCase c' | ||
| isConstructorTree c _caseValue -> | ||
NCase | ||
c' | ||
{ _caseBranches = map permuteBranch (c' ^. caseBranches), | ||
_caseDefault = fmap (mkBody c) (c' ^. caseDefault) | ||
} | ||
where | ||
permuteBranch :: CaseBranch -> CaseBranch | ||
permuteBranch br@CaseBranch {..} = | ||
case shift _caseBranchBindersNum (NCase c {_caseValue = mkBottom'}) of | ||
NCase cs -> | ||
over caseBranchBody (mkBody cs) br | ||
_ -> impossible | ||
|
||
mkBody :: Case -> Node -> Node | ||
mkBody cs n = NCase cs {_caseValue = n} | ||
_ -> | ||
node | ||
_ -> node | ||
|
||
casePermutation :: InfoTable -> InfoTable | ||
casePermutation = mapAllNodes convertNode |
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