Skip to content

Commit e38be6c

Browse files
committed
ES: conversion as-pat
1 parent c031b4c commit e38be6c

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

grin/src/Transformations/ExtendedSyntax/Conversion.hs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,18 @@ instance Convertible CPat New.CPat where
110110
DefaultPat -> New.DefaultPat
111111
TagPat _ -> error "convert: Tag patterns are not supported in the new syntax."
112112

113+
oldNodeToNewNode :: Tag -> [Val] -> New.Val
114+
oldNodeToNewNode tag vals
115+
| any (isn't _Var) vals = error $ "ConstTagNode " ++ show (PP $ ConstTagNode tag vals) ++ " has a non-variable argument."
116+
| otherwise = New.ConstTagNode (convert tag) (map (convert . view _Var) vals)
117+
118+
oldNodeToAsPat :: Tag -> [Val] -> Name -> New.BPat
119+
oldNodeToAsPat tag args name
120+
| New.ConstTagNode newTag newArgs <- oldNodeToNewNode tag args
121+
= New.AsPat newTag newArgs (convert name)
122+
113123
instance Convertible Val New.Val where
114-
convert n@(ConstTagNode t vals)
115-
| any (isn't _Var) [] = error $ "ConstTagNode " ++ show (PP n) ++ " has a non-variable argument."
116-
| otherwise = New.ConstTagNode (convert t) (map (convert . view _Var) vals)
124+
convert (ConstTagNode t vals) = oldNodeToNewNode t vals
117125
convert v@(VarTagNode _ _) = error $ "Cannot transform VarTagNode to new syntax: " ++ show (PP v)
118126
convert v@(ValTag _) = error $ "Cannot transform ValTag to new syntax: " ++ show (PP v)
119127
convert Unit = New.Unit
@@ -132,33 +140,32 @@ instance Convertible Exp New.Exp where
132140
of Binding Pattern Simplification to a more concise form.
133141
134142
v.0 <- pure <value>
135-
<non-var pat> <- pure v.0
143+
<node pat> <- pure v.0
136144
<rhs2>
137145
138-
<non-var pat> @ v.0 <- pure <value>
146+
<node pat> @ v.0 <- pure <value>
139147
<rhs2>
140148
-}
141149
(EBind lhs1 (Var var) rhs1)
142-
| EBind (SReturn (Var var')) pat rhs2 <- rhs1
143-
, isn't _Var pat
150+
| EBind (SReturn (Var var')) (ConstTagNode tag args) rhs2 <- rhs1
144151
, var == var'
145-
-> pure $ New.EBindF lhs1 (New.AsPat (convert var) (convert pat)) rhs2
152+
-> pure $ New.EBindF lhs1 (oldNodeToAsPat tag args var) rhs2
146153
{- NOTE: In this case, v.0 has been defined earlier in the program.
147154
This is a more general case that covers the one before as well.
148155
149156
v.0 <- pure <value>
150157
<...>
151-
<non-var pat> <- pure v.0
158+
<node pat> <- pure v.0
152159
<rhs>
153160
154161
v.0 <- pure <value>
155162
<...>
156-
<non-var pat> @ a.0 <- pure v.0
163+
<node pat> @ a.0 <- pure v.0
157164
<rhs>
158165
-}
159-
(EBind lhs pat rhs) | isn't _Var pat -> do
166+
(EBind lhs (ConstTagNode tag args) rhs) -> do
160167
asPatName <- deriveNewName "a"
161-
pure $ New.EBindF lhs (New.AsPat (convert asPatName) (convert pat)) rhs
168+
pure $ New.EBindF lhs (oldNodeToAsPat tag args asPatName) rhs
162169
(EBind lhs (Var var) rhs)
163170
-> pure $ New.EBindF lhs (New.VarPat $ convert var) rhs
164171
(ECase scrut alts)
@@ -262,9 +269,9 @@ instance Convertible New.Exp Exp where
262269
convert (New.Program exts defs) = Program (map convert exts) (map convert defs)
263270
convert (New.Def name args body) = Def (convert name) (map convert args) (convert body)
264271
convert e@(New.EBind lhs pat rhs)
265-
| (New.VarPat v) <- pat = EBind (convert lhs) (Var $ convert v) (convert rhs)
266-
| (New.AsPat v pat') <- pat -- condition
267-
, rhs' <- EBind (SReturn (Var $ convert v)) (convert pat') (convert rhs) -- helper
272+
| (New.VarPat v) <- pat = EBind (convert lhs) (Var $ convert v) (convert rhs)
273+
| (New.AsPat tag args v) <- pat -- condition
274+
, rhs' <- EBind (SReturn (Var $ convert v)) (ConstTagNode (convert tag) (map (Var . convert) args)) (convert rhs) -- helper
268275
= EBind (convert lhs) (Var $ convert v) rhs'
269276
convert e@(New.ECase scrut alts) = ECase (Var $ convert scrut) (map convert alts)
270277
convert (New.SApp f vars) = SApp (convert f) $ map (Var . convert) vars

0 commit comments

Comments
 (0)