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@).
10 -- If DEBUG is off, omit all exports
11 module SaAbsInt () where
17 AbsValEnv{-abstract-}, StrictEnv, AbsenceEnv,
19 nullAbsValEnv, addOneToAbsValEnv, growAbsValEnvList,
25 #include "HsVersions.h"
29 import IdInfo ( StrictnessInfo(..) )
30 import Demand ( Demand )
34 %************************************************************************
36 \subsection[AbsVal-datatype]{@AbsVal@: abstract values (and @AbsValEnv@)}
38 %************************************************************************
40 @AnalysisKind@ tells what kind of analysis is being done.
44 = StrAnal -- We're doing strictness analysis
45 | AbsAnal -- We're doing absence analysis
49 @AbsVal@ is the data type of HNF abstract values.
53 = AbsTop -- AbsTop is the completely uninformative
56 | AbsBot -- An expression whose abstract value is
57 -- AbsBot is sure to fail to terminate.
58 -- AbsBot represents the abstract
59 -- *function* bottom too.
61 | AbsProd [AbsVal] -- (Lifted) product of abstract values
62 -- "Lifted" means that AbsBot is *different* from
63 -- AbsProd [AbsBot, ..., AbsBot]
65 | AbsFun -- An abstract function, with the given:
66 Type -- Type of the *argument* to the function
67 (AbsVal -> AbsVal) -- The function
69 | AbsApproxFun -- This is used to represent a coarse
70 [Demand] -- approximation to a function value. It's an
71 AbsVal -- abstract function which is strict in its
72 -- arguments if the Demand so indicates.
73 -- INVARIANT: the [Demand] is non-empty
75 -- AbsApproxFun has to take a *list* of demands, no just one,
76 -- because function spaces are now lifted. Hence, (f bot top)
77 -- might be bot, but the partial application (f bot) is a *function*,
80 mkAbsApproxFun :: Demand -> AbsVal -> AbsVal
81 mkAbsApproxFun d (AbsApproxFun ds val) = AbsApproxFun (d:ds) val
82 mkAbsApproxFun d val = AbsApproxFun [d] val
84 instance Outputable AbsVal where
85 ppr AbsTop = ptext SLIT("AbsTop")
86 ppr AbsBot = ptext SLIT("AbsBot")
87 ppr (AbsProd prod) = hsep [ptext SLIT("AbsProd"), ppr prod]
88 ppr (AbsFun bndr_ty body) = ptext SLIT("AbsFun")
89 ppr (AbsApproxFun demands val)
90 = ptext SLIT("AbsApprox") <+> brackets (interpp'SP demands) <+> ppr val
95 An @AbsValEnv@ maps @Ids@ to @AbsVals@. Any unbound @Ids@ are
96 implicitly bound to @AbsTop@, the completely uninformative,
97 pessimistic value---see @absEval@ of a @Var@.
100 newtype AbsValEnv = AbsValEnv (IdEnv AbsVal)
102 type StrictEnv = AbsValEnv -- Environment for strictness analysis
103 type AbsenceEnv = AbsValEnv -- Environment for absence analysis
105 nullAbsValEnv -- this is the one and only way to create AbsValEnvs
106 = AbsValEnv emptyVarEnv
108 addOneToAbsValEnv (AbsValEnv idenv) y z = AbsValEnv (extendVarEnv idenv y z)
109 growAbsValEnvList (AbsValEnv idenv) ys = AbsValEnv (extendVarEnvList idenv ys)
111 lookupAbsValEnv (AbsValEnv idenv) y
112 = lookupVarEnv idenv y
116 absValFromStrictness :: AnalysisKind -> StrictnessInfo -> AbsVal
118 absValFromStrictness anal NoStrictnessInfo = AbsTop
119 absValFromStrictness anal (StrictnessInfo args_info bot_result)
120 = case args_info of -- Check the invariant that the arg list on
121 [] -> res -- AbsApproxFun is non-empty
122 _ -> AbsApproxFun args_info res
124 res | not bot_result = AbsTop
125 | otherwise = case anal of