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@).
12 AbsValEnv{-abstract-}, StrictEnv, AbsenceEnv,
14 nullAbsValEnv, addOneToAbsValEnv, growAbsValEnvList,
19 #include "HsVersions.h"
23 import IdInfo ( StrictnessInfo(..) )
24 import Demand ( Demand )
28 %************************************************************************
30 \subsection[AbsVal-datatype]{@AbsVal@: abstract values (and @AbsValEnv@)}
32 %************************************************************************
34 @AnalysisKind@ tells what kind of analysis is being done.
38 = StrAnal -- We're doing strictness analysis
39 | AbsAnal -- We're doing absence analysis
43 @AbsVal@ is the data type of HNF abstract values.
47 = AbsTop -- AbsTop is the completely uninformative
50 | AbsBot -- An expression whose abstract value is
51 -- AbsBot is sure to fail to terminate.
52 -- AbsBot represents the abstract
53 -- *function* bottom too.
55 | AbsProd [AbsVal] -- (Lifted) product of abstract values
56 -- "Lifted" means that AbsBot is *different* from
57 -- AbsProd [AbsBot, ..., AbsBot]
59 | AbsFun -- An abstract function, with the given:
60 Type -- Type of the *argument* to the function
61 (AbsVal -> AbsVal) -- The function
63 | AbsApproxFun -- This is used to represent a coarse
64 [Demand] -- approximation to a function value. It's an
65 AbsVal -- abstract function which is strict in its
66 -- arguments if the Demand so indicates.
67 -- INVARIANT: the [Demand] is non-empty
69 -- AbsApproxFun has to take a *list* of demands, no just one,
70 -- because function spaces are now lifted. Hence, (f bot top)
71 -- might be bot, but the partial application (f bot) is a *function*,
74 mkAbsApproxFun :: Demand -> AbsVal -> AbsVal
75 mkAbsApproxFun d (AbsApproxFun ds val) = AbsApproxFun (d:ds) val
76 mkAbsApproxFun d val = AbsApproxFun [d] val
78 instance Outputable AbsVal where
79 ppr AbsTop = ptext SLIT("AbsTop")
80 ppr AbsBot = ptext SLIT("AbsBot")
81 ppr (AbsProd prod) = hsep [ptext SLIT("AbsProd"), ppr prod]
82 ppr (AbsFun bndr_ty body) = ptext SLIT("AbsFun")
83 ppr (AbsApproxFun demands val)
84 = ptext SLIT("AbsApprox") <+> brackets (interpp'SP demands) <+> ppr val
89 An @AbsValEnv@ maps @Ids@ to @AbsVals@. Any unbound @Ids@ are
90 implicitly bound to @AbsTop@, the completely uninformative,
91 pessimistic value---see @absEval@ of a @Var@.
94 newtype AbsValEnv = AbsValEnv (IdEnv AbsVal)
96 type StrictEnv = AbsValEnv -- Environment for strictness analysis
97 type AbsenceEnv = AbsValEnv -- Environment for absence analysis
99 nullAbsValEnv -- this is the one and only way to create AbsValEnvs
100 = AbsValEnv emptyVarEnv
102 addOneToAbsValEnv (AbsValEnv idenv) y z = AbsValEnv (extendVarEnv idenv y z)
103 growAbsValEnvList (AbsValEnv idenv) ys = AbsValEnv (extendVarEnvList idenv ys)
105 lookupAbsValEnv (AbsValEnv idenv) y
106 = lookupVarEnv idenv y
110 absValFromStrictness :: AnalysisKind -> StrictnessInfo -> AbsVal
112 absValFromStrictness anal NoStrictnessInfo = AbsTop
113 absValFromStrictness anal (StrictnessInfo args_info bot_result)
114 = case args_info of -- Check the invariant that the arg list on
115 [] -> res -- AbsApproxFun is non-empty
116 _ -> AbsApproxFun args_info res
118 res | not bot_result = AbsTop
119 | otherwise = case anal of