1 {-# LANGUAGE TemplateHaskell #-}
2 module NormalizeTypes where
6 import qualified Control.Monad.Trans.Writer as Writer
7 import qualified Control.Monad.Trans.State as State
8 import qualified Data.Monoid as Monoid
9 import qualified Data.Accessor.Template
11 import qualified Data.Map as Map
16 import qualified UniqSupply
17 import qualified VarSet
18 import Outputable ( Outputable, showSDoc, ppr )
24 data TransformState = TransformState {
25 tsUniqSupply_ :: UniqSupply.UniqSupply
26 , tsBindings_ :: Map.Map CoreBndr CoreExpr
27 , tsNormalized_ :: VarSet.VarSet -- ^ The binders that have been normalized
30 $( Data.Accessor.Template.deriveAccessors ''TransformState )
32 -- A session of multiple transformations over multiple expressions
33 type TransformSession = (State.State TransformState)
34 -- Wrap a writer around a TransformSession, to run a single transformation
35 -- over a single expression and track if the expression was changed.
36 type TransformMonad = Writer.WriterT Monoid.Any TransformSession
38 -- | Transforms a CoreExpr and keeps track if it has changed.
39 type Transform = CoreExpr -> TransformMonad CoreExpr
41 -- Finds the value of a global binding, if available
42 getGlobalBind :: CoreBndr -> TransformSession (Maybe CoreExpr)
43 getGlobalBind bndr = do
44 bindings <- getA tsBindings
45 return $ Map.lookup bndr bindings
47 -- Adds a new global binding with the given value
48 addGlobalBind :: CoreBndr -> CoreExpr -> TransformSession ()
49 addGlobalBind bndr expr = modA tsBindings (Map.insert bndr expr)
51 -- Returns a list of all global binders
52 getGlobalBinders :: TransformSession [CoreBndr]
54 bindings <- getA tsBindings
55 return $ Map.keys bindings