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 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 #ifndef OLD_STRICTNESS
23 AbsValEnv{-abstract-}, StrictEnv, AbsenceEnv,
25 nullAbsValEnv, addOneToAbsValEnv, growAbsValEnvList,
30 #include "HsVersions.h"
34 import IdInfo ( StrictnessInfo(..) )
35 import Demand ( Demand )
39 %************************************************************************
41 \subsection[AbsVal-datatype]{@AbsVal@: abstract values (and @AbsValEnv@)}
43 %************************************************************************
45 @AnalysisKind@ tells what kind of analysis is being done.
49 = StrAnal -- We're doing strictness analysis
50 | AbsAnal -- We're doing absence analysis
54 @AbsVal@ is the data type of HNF abstract values.
58 = AbsTop -- AbsTop is the completely uninformative
61 | AbsBot -- An expression whose abstract value is
62 -- AbsBot is sure to fail to terminate.
63 -- AbsBot represents the abstract
64 -- *function* bottom too.
66 | AbsProd [AbsVal] -- (Lifted) product of abstract values
67 -- "Lifted" means that AbsBot is *different* from
68 -- AbsProd [AbsBot, ..., AbsBot]
70 | AbsFun -- An abstract function, with the given:
71 Type -- Type of the *argument* to the function
72 (AbsVal -> AbsVal) -- The function
74 | AbsApproxFun -- This is used to represent a coarse
75 [Demand] -- approximation to a function value. It's an
76 AbsVal -- abstract function which is strict in its
77 -- arguments if the Demand so indicates.
78 -- INVARIANT: the [Demand] is non-empty
80 -- AbsApproxFun has to take a *list* of demands, no just one,
81 -- because function spaces are now lifted. Hence, (f bot top)
82 -- might be bot, but the partial application (f bot) is a *function*,
85 mkAbsApproxFun :: Demand -> AbsVal -> AbsVal
86 mkAbsApproxFun d (AbsApproxFun ds val) = AbsApproxFun (d:ds) val
87 mkAbsApproxFun d val = AbsApproxFun [d] val
89 instance Outputable AbsVal where
90 ppr AbsTop = ptext SLIT("AbsTop")
91 ppr AbsBot = ptext SLIT("AbsBot")
92 ppr (AbsProd prod) = hsep [ptext SLIT("AbsProd"), ppr prod]
93 ppr (AbsFun bndr_ty body) = ptext SLIT("AbsFun")
94 ppr (AbsApproxFun demands val)
95 = ptext SLIT("AbsApprox") <+> brackets (interpp'SP demands) <+> ppr val
100 An @AbsValEnv@ maps @Ids@ to @AbsVals@. Any unbound @Ids@ are
101 implicitly bound to @AbsTop@, the completely uninformative,
102 pessimistic value---see @absEval@ of a @Var@.
105 newtype AbsValEnv = AbsValEnv (IdEnv AbsVal)
107 type StrictEnv = AbsValEnv -- Environment for strictness analysis
108 type AbsenceEnv = AbsValEnv -- Environment for absence analysis
110 nullAbsValEnv -- this is the one and only way to create AbsValEnvs
111 = AbsValEnv emptyVarEnv
113 addOneToAbsValEnv (AbsValEnv idenv) y z = AbsValEnv (extendVarEnv idenv y z)
114 growAbsValEnvList (AbsValEnv idenv) ys = AbsValEnv (extendVarEnvList idenv ys)
116 lookupAbsValEnv (AbsValEnv idenv) y
117 = lookupVarEnv idenv y
121 absValFromStrictness :: AnalysisKind -> StrictnessInfo -> AbsVal
123 absValFromStrictness anal NoStrictnessInfo = AbsTop
124 absValFromStrictness anal (StrictnessInfo args_info bot_result)
125 = case args_info of -- Check the invariant that the arg list on
126 [] -> res -- AbsApproxFun is non-empty
127 _ -> AbsApproxFun args_info res
129 res | not bot_result = AbsTop
130 | otherwise = case anal of
136 #endif /* OLD_STRICTNESS */