2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[SaLib]{Basic datatypes, functions for the strictness analyser}
6 See also: the ``library'' for the ``back end'' (@SaBackLib@).
16 AbsValEnv{-abstract-}, StrictEnv, AbsenceEnv,
18 nullAbsValEnv, addOneToAbsValEnv, growAbsValEnvList,
23 #include "HsVersions.h"
27 import IdInfo ( StrictnessInfo(..) )
28 import Demand ( Demand )
32 %************************************************************************
34 \subsection[AbsVal-datatype]{@AbsVal@: abstract values (and @AbsValEnv@)}
36 %************************************************************************
38 @AnalysisKind@ tells what kind of analysis is being done.
42 = StrAnal -- We're doing strictness analysis
43 | AbsAnal -- We're doing absence analysis
47 @AbsVal@ is the data type of HNF abstract values.
51 = AbsTop -- AbsTop is the completely uninformative
54 | AbsBot -- An expression whose abstract value is
55 -- AbsBot is sure to fail to terminate.
56 -- AbsBot represents the abstract
57 -- *function* bottom too.
59 | AbsProd [AbsVal] -- (Lifted) product of abstract values
60 -- "Lifted" means that AbsBot is *different* from
61 -- AbsProd [AbsBot, ..., AbsBot]
63 | AbsFun -- An abstract function, with the given:
64 Type -- Type of the *argument* to the function
65 (AbsVal -> AbsVal) -- The function
67 | AbsApproxFun -- This is used to represent a coarse
68 [Demand] -- approximation to a function value. It's an
69 AbsVal -- abstract function which is strict in its
70 -- arguments if the Demand so indicates.
71 -- INVARIANT: the [Demand] is non-empty
73 -- AbsApproxFun has to take a *list* of demands, no just one,
74 -- because function spaces are now lifted. Hence, (f bot top)
75 -- might be bot, but the partial application (f bot) is a *function*,
78 mkAbsApproxFun :: Demand -> AbsVal -> AbsVal
79 mkAbsApproxFun d (AbsApproxFun ds val) = AbsApproxFun (d:ds) val
80 mkAbsApproxFun d val = AbsApproxFun [d] val
82 instance Outputable AbsVal where
83 ppr AbsTop = ptext SLIT("AbsTop")
84 ppr AbsBot = ptext SLIT("AbsBot")
85 ppr (AbsProd prod) = hsep [ptext SLIT("AbsProd"), ppr prod]
86 ppr (AbsFun bndr_ty body) = ptext SLIT("AbsFun")
87 ppr (AbsApproxFun demands val)
88 = ptext SLIT("AbsApprox") <+> brackets (interpp'SP demands) <+> ppr val
93 An @AbsValEnv@ maps @Ids@ to @AbsVals@. Any unbound @Ids@ are
94 implicitly bound to @AbsTop@, the completely uninformative,
95 pessimistic value---see @absEval@ of a @Var@.
98 newtype AbsValEnv = AbsValEnv (IdEnv AbsVal)
100 type StrictEnv = AbsValEnv -- Environment for strictness analysis
101 type AbsenceEnv = AbsValEnv -- Environment for absence analysis
103 nullAbsValEnv -- this is the one and only way to create AbsValEnvs
104 = AbsValEnv emptyVarEnv
106 addOneToAbsValEnv (AbsValEnv idenv) y z = AbsValEnv (extendVarEnv idenv y z)
107 growAbsValEnvList (AbsValEnv idenv) ys = AbsValEnv (extendVarEnvList idenv ys)
109 lookupAbsValEnv (AbsValEnv idenv) y
110 = lookupVarEnv idenv y
114 absValFromStrictness :: AnalysisKind -> StrictnessInfo -> AbsVal
116 absValFromStrictness anal NoStrictnessInfo = AbsTop
117 absValFromStrictness anal (StrictnessInfo args_info bot_result)
118 = case args_info of -- Check the invariant that the arg list on
119 [] -> res -- AbsApproxFun is non-empty
120 _ -> AbsApproxFun args_info res
122 res | not bot_result = AbsTop
123 | otherwise = case anal of
129 #endif /* OLD_STRICTNESS */