2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Demand]{@Demand@: the amount of demand on a value}
8 Demand(..), Keepity(..), Deferredness(..), topDmd,
9 StrictSig(..), topSig, botSig, mkStrictSig,
10 DmdType(..), topDmdType, mkDmdType, mkTopDmdType,
12 DmdResult(..), isBotRes
15 #include "HsVersions.h"
17 import BasicTypes ( Arity )
19 import VarEnv ( VarEnv, emptyVarEnv )
20 import UniqFM ( ufmToList )
21 import qualified Demand
26 %************************************************************************
28 \subsection{Strictness signatures
30 %************************************************************************
33 data StrictSig = StrictSig Arity DmdType
35 -- Equality needed when comparing strictness
36 -- signatures for fixpoint finding
38 topSig = StrictSig 0 topDmdType
39 botSig = StrictSig 0 botDmdType
41 mkStrictSig :: Id -> Arity -> DmdType -> StrictSig
42 mkStrictSig id arity ty
43 = WARN( arity /= dmdTypeDepth ty, ppr id <+> (ppr arity $$ ppr ty) )
46 instance Outputable StrictSig where
47 ppr (StrictSig arity ty) = ppr ty
51 %************************************************************************
53 \subsection{Demand types}
55 %************************************************************************
58 data DmdType = DmdType
59 DmdEnv -- Demand on explicitly-mentioned
61 [Demand] -- Demand on arguments
62 DmdResult -- Nature of result
64 -- IMPORTANT INVARIANT
65 -- The default demand on free variables not in the DmdEnv is:
66 -- DmdResult = BotRes <=> Bot
67 -- DmdResult = TopRes/ResCPR <=> Abs
69 type DmdEnv = VarEnv Demand
71 data DmdResult = TopRes -- Nothing known
72 | RetCPR -- Returns a constructed product
73 | BotRes -- Diverges or errors
76 -- Equality needed for fixpoints in DmdAnal
77 instance Eq DmdType where
78 (==) (DmdType fv1 ds1 res1)
79 (DmdType fv2 ds2 res2) = ufmToList fv1 == ufmToList fv2
80 && ds1 == ds2 && res1 == res2
82 instance Outputable DmdType where
83 ppr (DmdType fv ds res)
84 = hsep [text "DmdType",
85 hcat (map ppr ds) <> ppr res,
86 braces (fsep (map pp_elt (ufmToList fv)))]
88 pp_elt (uniq, dmd) = ppr uniq <> text "->" <> ppr dmd
90 instance Outputable DmdResult where
95 emptyDmdEnv = emptyVarEnv
96 topDmdType = DmdType emptyDmdEnv [] TopRes
97 botDmdType = DmdType emptyDmdEnv [] BotRes
99 isBotRes :: DmdResult -> Bool
100 isBotRes BotRes = True
101 isBotRes other = False
103 mkDmdType :: DmdEnv -> [Demand] -> DmdResult -> DmdType
104 mkDmdType fv ds res = DmdType fv ds res
106 mkTopDmdType :: [Demand] -> DmdResult -> DmdType
107 mkTopDmdType ds res = DmdType emptyDmdEnv ds res
109 dmdTypeDepth :: DmdType -> Arity
110 dmdTypeDepth (DmdType _ ds _) = length ds
114 %************************************************************************
118 %************************************************************************
122 = Lazy -- L; used for unlifted types too, so that
125 | Call Demand -- C(d)
127 | Seq Keepity -- S/U(ds)
133 -- Equality needed for fixpoints in DmdAnal
135 data Deferredness = Now | Defer
138 data Keepity = Keep | Drop
141 topDmd :: Demand -- The most uninformative demand
144 instance Outputable Demand where
150 ppr (Call d) = char 'C' <> parens (ppr d)
151 ppr (Seq k l ds) = ppr k <> ppr l <> parens (hcat (map ppr ds))
153 instance Outputable Deferredness where
157 instance Outputable Keepity where