Skip to content

Commit c031b4c

Browse files
committed
ES: transformations as-pat
1 parent 800c17e commit c031b4c

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

grin/src/Transformations/ExtendedSyntax/Names.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ mapNameDefExpM :: Monad m => (Name -> m Name) -> Exp -> m Exp
126126
mapNameDefExpM f = \case
127127
Def name args body -> Def <$> f name <*> mapM f args <*> pure body
128128
EBind leftExp (VarPat var) rightExp -> do EBind leftExp <$> (VarPat <$> f var) <*> pure rightExp
129-
EBind leftExp (AsPat var val) rightExp -> EBind leftExp <$> (AsPat <$> f var <*> mapNamesValM f val) <*> pure rightExp
129+
EBind leftExp (AsPat tag args var) rightExp -> EBind leftExp <$> (AsPat tag <$> mapM f args <*> f var) <*> pure rightExp
130130
Alt cpat n body -> Alt <$> mapNamesCPatM f cpat <*> f n <*> pure body
131131
exp -> pure exp
132132

grin/src/Transformations/ExtendedSyntax/Optimising/CopyPropagation.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ copyPropagation = flip evalState mempty . hyloM rmBlocks builder where
6666
pure $ project $ EBind (SReturn valWithOrigVars) bpat rightExp
6767

6868
-- left unit law + eliminate redundant rebinds
69-
EBind (SReturn (Var valVar)) (AsPat patVar asPat) rightExp
69+
EBind (SReturn (Var valVar)) (AsPat patTag patArgs patVar) rightExp
7070
| origVar <- getAlias valVar aliases
7171
, origVal <- getOrigVal origVar origVals
72-
, ConstTagNode patTag patArgs <- asPat
7372
, ConstTagNode valTag valArgs <- origVal
7473
, patTag == valTag -> do
7574
let aliases' = aliases <> (Map.fromList $ zip (patVar:patArgs) (origVar:valArgs))
@@ -79,10 +78,9 @@ copyPropagation = flip evalState mempty . hyloM rmBlocks builder where
7978

8079
-- add the lhs value as an original value
8180
-- and eliminate redudant rebinds
82-
EBind (SReturn val) (AsPat patVar asPat) rightExp
81+
EBind (SReturn val) (AsPat patTag patArgs patVar) rightExp
8382
| isn't _Lit val
8483
, valWithOrigVars <- substNamesVal aliases val
85-
, ConstTagNode patTag patArgs <- asPat
8684
, ConstTagNode valTag valArgs <- valWithOrigVars
8785
, patTag == valTag -> do
8886
let origVals' = Map.insert patVar valWithOrigVars origVals
@@ -91,10 +89,10 @@ copyPropagation = flip evalState mempty . hyloM rmBlocks builder where
9189
put newEnv
9290
pure $ project $ EBind (SReturn val) (VarPat patVar) rightExp
9391

94-
-- simplify as-pattern matching against the same basic value it binds
95-
EBind (SReturn retVal) (AsPat var patVal) rightExp
96-
| isBasicValue retVal
97-
, retVal == patVal -> do
92+
-- simplify as-pattern matching against the same node value it binds
93+
EBind (SReturn retVal@(ConstTagNode retTag retArgs)) (AsPat patTag patArgs var) rightExp
94+
| retTag == patTag
95+
, retArgs == patArgs -> do
9896
pure $ project $ EBind (SReturn retVal) (VarPat var) rightExp
9997

10098
_ -> pure $ project exp'

grin/src/Transformations/ExtendedSyntax/Util.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ mapNamesVal f = \case
7272

7373
mapNamesBPat :: (Name -> Name) -> BPat -> BPat
7474
mapNamesBPat f = \case
75-
VarPat v -> VarPat (f v)
76-
AsPat var val -> AsPat (f var) (mapNamesVal f val)
75+
VarPat v -> VarPat (f v)
76+
AsPat tag vars v -> AsPat tag (map f vars) (f v)
7777

7878
-- TODO: replace at use sites with
7979
-- mapValVal :: (Val -> Val) -> Val -> Val
@@ -130,7 +130,8 @@ cPatToVal = \case
130130
DefaultPat -> Unit
131131

132132
cPatToAsPat :: Name -> CPat -> BPat
133-
cPatToAsPat name cPat = AsPat name (cPatToVal cPat)
133+
cPatToAsPat name (NodePat tag args) = AsPat tag args name
134+
cPatToAsPat _ cPat = error $ "cPatToAsPat: cannot convert to as-pattern: " ++ show (PP cPat)
134135

135136
-- monadic recursion schemes
136137
-- see: https://jtobin.io/monadic-recursion-schemes

0 commit comments

Comments
 (0)