1 module CLasH.Normalize.NormalizeTypes where
4 import qualified Control.Monad.Trans.Writer as Writer
5 import qualified Data.Monoid as Monoid
8 import qualified CoreSyn
11 import CLasH.Translator.TranslatorTypes
13 -- Wrap a writer around a TranslatorSession, to run a single transformation
14 -- over a single expression and track if the expression was changed.
15 type TransformMonad = Writer.WriterT Monoid.Any TranslatorSession
17 -- | In what context does a core expression occur?
18 data CoreContext = AppFirst -- ^ The expression is the first
19 -- argument of an application (i.e.,
21 | AppSecond -- ^ The expression is the second
22 -- argument of an application
23 -- (i.e., something is applied to it)
24 | LetBinding [CoreSyn.CoreBndr]
25 -- ^ The expression is bound in a
26 -- (recursive or non-recursive) let
28 | LetBody [CoreSyn.CoreBndr]
29 -- ^ The expression is the body of a
31 | LambdaBody CoreSyn.CoreBndr
32 -- ^ The expression is the body of a
34 | CaseAlt CoreSyn.CoreBndr
35 -- ^ The expression is the body of a
37 | Other -- ^ Another context
39 -- | Transforms a CoreExpr and keeps track if it has changed.
40 type Transform = [CoreContext] -> CoreSyn.CoreExpr -> TransformMonad CoreSyn.CoreExpr
42 -- Predicates for each of the context types
43 is_appfirst_ctx, is_appsecond_ctx, is_letbinding_ctx, is_letbody_ctx, is_lambdabody_ctx
44 :: CoreContext -> Bool
46 is_appfirst_ctx AppFirst = True
47 is_appfirst_ctx _ = False
49 is_appsecond_ctx AppSecond = True
50 is_appsecond_ctx _ = False
52 is_letbinding_ctx (LetBinding _) = True
53 is_letbinding_ctx _ = False
55 is_letbody_ctx (LetBody _) = True
56 is_letbody_ctx _ = False
58 is_lambdabody_ctx (LambdaBody _) = True
59 is_lambdabody_ctx _ = False