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
78 emptyDmdEnv = emptyVarEnv
79 topDmdType = DmdType emptyDmdEnv [] TopRes
80 botDmdType = DmdType emptyDmdEnv [] BotRes
82 isBotRes :: DmdResult -> Bool
83 isBotRes BotRes = True
84 isBotRes other = False
86 returnsCPR :: DmdResult -> Bool
87 returnsCPR RetCPR = True
88 returnsCPR other = False
90 mkDmdType :: DmdEnv -> [Demand] -> DmdResult -> DmdType
91 mkDmdType fv ds res = DmdType fv ds res
93 mkTopDmdType :: [Demand] -> DmdResult -> DmdType
94 mkTopDmdType ds res = DmdType emptyDmdEnv ds res
96 dmdTypeDepth :: DmdType -> Arity
97 dmdTypeDepth (DmdType _ ds _) = length ds
99 dmdTypeRes :: DmdType -> DmdResult
100 dmdTypeRes (DmdType _ _ res_ty) = res_ty
104 %************************************************************************
106 \subsection{Strictness signature
108 %************************************************************************
110 In a let-bound Id we record its strictness info.
111 In principle, this strictness info is a demand transformer, mapping
112 a demand on the Id into a DmdType, which gives
113 a) the free vars of the Id's value
114 b) the Id's arguments
115 c) an indication of the result of applying
116 the Id to its arguments
118 However, in fact we store in the Id an extremely emascuated demand transfomer,
121 (Nevertheless we dignify StrictSig as a distinct type.)
123 This DmdType gives the demands unleashed by the Id when it is applied
124 to as many arguments as are given in by the arg demands in the DmdType.
126 For example, the demand transformer described by the DmdType
127 DmdType {x -> U(LL)} [V,A] Top
128 says that when the function is applied to two arguments, it
129 unleashes demand U(LL) on the free var x, V on the first arg,
132 If this same function is applied to one arg, all we can say is
133 that it uses x with U*(LL), and its arg with demand L.
136 newtype StrictSig = StrictSig DmdType
139 instance Outputable StrictSig where
140 ppr (StrictSig ty) = ppr ty
142 instance Show StrictSig where
143 show (StrictSig ty) = showSDoc (ppr ty)
145 mkStrictSig :: Id -> Arity -> DmdType -> StrictSig
146 mkStrictSig id arity dmd_ty
147 = WARN( arity /= dmdTypeDepth dmd_ty, ppr id <+> (ppr arity $$ ppr dmd_ty) )
150 splitStrictSig :: StrictSig -> ([Demand], DmdResult)
151 splitStrictSig (StrictSig (DmdType _ dmds res)) = (dmds, res)
153 strictSigResInfo :: StrictSig -> DmdResult
154 strictSigResInfo (StrictSig (DmdType _ _ res)) = res
156 topSig = StrictSig topDmdType
157 botSig = StrictSig botDmdType
159 -- appIsBottom returns true if an application to n args would diverge
160 appIsBottom (StrictSig (DmdType _ ds BotRes)) n = n >= length ds
161 appIsBottom _ _ = False
163 isBottomingSig (StrictSig (DmdType _ _ BotRes)) = True
164 isBottomingSig _ = False
166 pprIfaceStrictSig :: StrictSig -> SDoc
167 -- Used for printing top-level strictness pragmas in interface files
168 pprIfaceStrictSig (StrictSig (DmdType _ dmds res))
169 = hcat (map ppr dmds) <> ppr res
173 %************************************************************************
177 %************************************************************************
181 = Lazy -- L; used for unlifted types too, so that
184 | Call Demand -- C(d)
186 | Seq Keepity -- S/U(ds)
192 -- Equality needed for fixpoints in DmdAnal
194 data Deferredness = Now | Defer
197 data Keepity = Keep | Drop
200 topDmd, lazyDmd, seqDmd :: Demand
201 topDmd = Lazy -- The most uninformative demand
203 seqDmd = Seq Keep Now [] -- Polymorphic seq demand
206 isStrictDmd :: Demand -> Bool
207 isStrictDmd Bot = True
208 isStrictDmd Err = True
209 isStrictDmd (Seq _ Now _) = True
210 isStrictDmd Eval = True
211 isStrictDmd (Call _) = True
212 isStrictDmd other = False
214 instance Outputable Demand where
220 ppr (Call d) = char 'C' <> parens (ppr d)
221 ppr (Seq k l []) = ppr k <> ppr l
222 ppr (Seq k l ds) = ppr k <> ppr l <> parens (hcat (map ppr ds))
224 instance Outputable Deferredness where
228 instance Outputable Keepity where