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(..),
9 topDmd, lazyDmd, seqDmd, evalDmd, isStrictDmd,
11 DmdType(..), topDmdType, mkDmdType, mkTopDmdType,
12 dmdTypeDepth, dmdTypeRes,
14 DmdResult(..), isBotRes, returnsCPR,
16 StrictSig(..), mkStrictSig, topSig, botSig,
17 splitStrictSig, strictSigResInfo,
18 pprIfaceStrictSig, appIsBottom, isBottomingSig
21 #include "HsVersions.h"
23 import BasicTypes ( Arity )
25 import VarEnv ( VarEnv, emptyVarEnv )
26 import UniqFM ( ufmToList )
27 import qualified Demand
32 %************************************************************************
34 \subsection{Demand types}
36 %************************************************************************
39 data DmdType = DmdType
40 DmdEnv -- Demand on explicitly-mentioned
42 [Demand] -- Demand on arguments
43 DmdResult -- Nature of result
45 -- IMPORTANT INVARIANT
46 -- The default demand on free variables not in the DmdEnv is:
47 -- DmdResult = BotRes <=> Bot
48 -- DmdResult = TopRes/ResCPR <=> Abs
50 type DmdEnv = VarEnv Demand
52 data DmdResult = TopRes -- Nothing known
53 | RetCPR -- Returns a constructed product
54 | BotRes -- Diverges or errors
56 -- Equality for fixpoints
57 -- Show needed for Show in Lex.Token (sigh)
59 -- Equality needed for fixpoints in DmdAnal
60 instance Eq DmdType where
61 (==) (DmdType fv1 ds1 res1)
62 (DmdType fv2 ds2 res2) = ufmToList fv1 == ufmToList fv2
63 && ds1 == ds2 && res1 == res2
65 instance Outputable DmdType where
66 ppr (DmdType fv ds res)
67 = hsep [text "DmdType",
68 hcat (map ppr ds) <> ppr res,
69 braces (fsep (map pp_elt (ufmToList fv)))]
71 pp_elt (uniq, dmd) = ppr uniq <> text "->" <> ppr dmd
73 instance Outputable DmdResult where
74 ppr TopRes = empty -- Keep these distinct from Demand letters
75 ppr RetCPR = char 'm' -- so that we can print strictness sigs as
76 ppr BotRes = char 'b' -- dddr
79 emptyDmdEnv = emptyVarEnv
80 topDmdType = DmdType emptyDmdEnv [] TopRes
81 botDmdType = DmdType emptyDmdEnv [] BotRes
83 isBotRes :: DmdResult -> Bool
84 isBotRes BotRes = True
85 isBotRes other = False
87 returnsCPR :: DmdResult -> Bool
88 returnsCPR RetCPR = True
89 returnsCPR other = False
91 mkDmdType :: DmdEnv -> [Demand] -> DmdResult -> DmdType
92 mkDmdType fv ds res = DmdType fv ds res
94 mkTopDmdType :: [Demand] -> DmdResult -> DmdType
95 mkTopDmdType ds res = DmdType emptyDmdEnv ds res
97 dmdTypeDepth :: DmdType -> Arity
98 dmdTypeDepth (DmdType _ ds _) = length ds
100 dmdTypeRes :: DmdType -> DmdResult
101 dmdTypeRes (DmdType _ _ res_ty) = res_ty
105 %************************************************************************
107 \subsection{Strictness signature
109 %************************************************************************
111 In a let-bound Id we record its strictness info.
112 In principle, this strictness info is a demand transformer, mapping
113 a demand on the Id into a DmdType, which gives
114 a) the free vars of the Id's value
115 b) the Id's arguments
116 c) an indication of the result of applying
117 the Id to its arguments
119 However, in fact we store in the Id an extremely emascuated demand transfomer,
122 (Nevertheless we dignify StrictSig as a distinct type.)
124 This DmdType gives the demands unleashed by the Id when it is applied
125 to as many arguments as are given in by the arg demands in the DmdType.
127 For example, the demand transformer described by the DmdType
128 DmdType {x -> U(LL)} [V,A] Top
129 says that when the function is applied to two arguments, it
130 unleashes demand U(LL) on the free var x, V on the first arg,
133 If this same function is applied to one arg, all we can say is
134 that it uses x with U*(LL), and its arg with demand L.
137 newtype StrictSig = StrictSig DmdType
140 instance Outputable StrictSig where
141 ppr (StrictSig ty) = ppr ty
143 instance Show StrictSig where
144 show (StrictSig ty) = showSDoc (ppr ty)
146 mkStrictSig :: Id -> Arity -> DmdType -> StrictSig
147 mkStrictSig id arity dmd_ty
148 = WARN( arity /= dmdTypeDepth dmd_ty, ppr id <+> (ppr arity $$ ppr dmd_ty) )
151 splitStrictSig :: StrictSig -> ([Demand], DmdResult)
152 splitStrictSig (StrictSig (DmdType _ dmds res)) = (dmds, res)
154 strictSigResInfo :: StrictSig -> DmdResult
155 strictSigResInfo (StrictSig (DmdType _ _ res)) = res
157 topSig = StrictSig topDmdType
158 botSig = StrictSig botDmdType
160 -- appIsBottom returns true if an application to n args would diverge
161 appIsBottom (StrictSig (DmdType _ ds BotRes)) n = n >= length ds
162 appIsBottom _ _ = False
164 isBottomingSig (StrictSig (DmdType _ _ BotRes)) = True
165 isBottomingSig _ = False
167 pprIfaceStrictSig :: StrictSig -> SDoc
168 -- Used for printing top-level strictness pragmas in interface files
169 pprIfaceStrictSig (StrictSig (DmdType _ dmds res))
170 = hcat (map ppr dmds) <> ppr res
174 %************************************************************************
178 %************************************************************************
182 = Lazy -- L; used for unlifted types too, so that
185 | Call Demand -- C(d)
187 | Seq Keepity -- S/U(ds)
193 -- Equality needed for fixpoints in DmdAnal
195 data Deferredness = Now | Defer
198 data Keepity = Keep | Drop
201 topDmd, lazyDmd, seqDmd :: Demand
202 topDmd = Lazy -- The most uninformative demand
204 seqDmd = Seq Keep Now [] -- Polymorphic seq demand
207 isStrictDmd :: Demand -> Bool
208 isStrictDmd Bot = True
209 isStrictDmd Err = True
210 isStrictDmd (Seq _ Now _) = True
211 isStrictDmd Eval = True
212 isStrictDmd (Call _) = True
213 isStrictDmd other = False
215 instance Outputable Demand where
221 ppr (Call d) = char 'C' <> parens (ppr d)
222 ppr (Seq k l []) = ppr k <> ppr l
223 ppr (Seq k l ds) = ppr k <> ppr l <> parens (hcat (map ppr ds))
225 instance Outputable Deferredness where
229 instance Outputable Keepity where