@@ -52,14 +52,26 @@ arityRaising (te, exp) = runVarM te (apoM builder ([], exp))
52
52
53
53
-- Set of stores in the function body.
54
54
collectStores :: Exp -> [(Name , Val )]
55
- collectStores = para $ \ case
55
+ collectStores e = followVariables $ flip para e $ \ case
56
56
SBlockF (_, body) -> body
57
57
AltF _ (_, body) -> body
58
58
ECaseF _ alts -> mconcat $ map snd alts
59
- EBindF (SStore node, _) (Var v) (_, rhs) -> [(v,node)] <> rhs
59
+ EBindF (SStore node@ (ConstTagNode _ _), _) (Var v) (_, rhs) -> [(v,node)] <> rhs
60
+ EBindF (SStore var@ (Var _), _) (Var v) (_, rhs) -> [(v,var)] <> rhs
61
+ EBindF (SReturn node, _) (Var v) (_, rhs) -> [(v,node)] <> rhs
60
62
EBindF (_, lhs) _ (_, rhs) -> lhs <> rhs
61
63
_ -> mempty
62
64
65
+ -- Follow variables in substiotions based on stores.
66
+ followVariables :: [(Name , Val )] -> [(Name , Val )]
67
+ followVariables susbsts = Map. toList $ go susbstMap where
68
+ susbstMap = Map. fromList susbsts
69
+ go m =
70
+ let m' = flip Map. map m $ \ case
71
+ (Var v) | Just val <- Map. lookup v susbstMap -> val
72
+ rest -> rest
73
+ in if m == m' then m else go m'
74
+
63
75
-- The substituition that contains a Node or a list of new invariant parameters
64
76
builder :: ([(Name , Either Val [Name ])], Exp ) -> VarM (ExpF (Either Exp ([(Name , Either Val [Name ])], Exp )))
65
77
builder (substs0, exp0) = case exp0 of
0 commit comments