2 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
4 \section[BasicTypes]{Miscellanous types}
6 This module defines a miscellaneously collection of very simple
10 \item have no other obvious home
11 \item don't depend on any other complicated types
12 \item are used in more than one "part" of the compiler
17 Version, bumpVersion, initialVersion, bogusVersion,
23 Fixity(..), FixityDirection(..),
24 defaultFixity, maxPrecedence, negateFixity, negatePrecedence,
28 RecFlag(..), isRec, isNonRec,
30 TopLevelFlag(..), isTopLevel, isNotTopLevel,
32 Boxity(..), isBoxed, tupleParens,
34 OccInfo(..), seqOccInfo, isFragileOcc, isDeadOcc, isLoopBreaker,
36 InsideLam, insideLam, notInsideLam,
37 OneBranch, oneBranch, notOneBranch,
41 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
43 CompilerPhase, pprPhase,
44 Activation(..), isActive, isNeverActive, isAlwaysActive
47 #include "HsVersions.h"
52 %************************************************************************
54 \subsection[Unused]{Unused}
56 %************************************************************************
58 Used as a placeholder in types.
64 unused = error "Unused is used!"
68 %************************************************************************
70 \subsection[Arity]{Arity}
72 %************************************************************************
79 %************************************************************************
81 \subsection[Version]{Module and identifier version numbers}
83 %************************************************************************
88 bogusVersion :: Version -- Shouldn't look at these
89 bogusVersion = error "bogusVersion"
91 bumpVersion :: Bool -> Version -> Version
92 -- Bump if the predicate (typically equality between old and new) is false
93 bumpVersion False v = v+1
94 bumpVersion True v = v
96 initialVersion :: Version
101 %************************************************************************
103 \subsection[Fixity]{Fixity info}
105 %************************************************************************
108 data Fixity = Fixity Int FixityDirection
109 data FixityDirection = InfixL | InfixR | InfixN
112 instance Outputable Fixity where
113 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
115 instance Outputable FixityDirection where
116 ppr InfixL = ptext SLIT("infixl")
117 ppr InfixR = ptext SLIT("infixr")
118 ppr InfixN = ptext SLIT("infix")
120 instance Eq Fixity where -- Used to determine if two fixities conflict
121 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
123 maxPrecedence = (9::Int)
124 defaultFixity = Fixity maxPrecedence InfixL
126 negateFixity :: Fixity
127 negateFixity = Fixity negatePrecedence InfixL -- Precedence of unary negate is wired in as infixl 6!
129 negatePrecedence :: Int
134 %************************************************************************
136 \subsection[NewType/DataType]{NewType/DataType flag}
138 %************************************************************************
142 = NewType -- "newtype Blah ..."
143 | DataType -- "data Blah ..."
144 deriving( Eq ) -- Needed because Demand derives Eq
147 %************************************************************************
149 \subsection[Top-level/local]{Top-level/not-top level flag}
151 %************************************************************************
158 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
160 isNotTopLevel NotTopLevel = True
161 isNotTopLevel TopLevel = False
163 isTopLevel TopLevel = True
164 isTopLevel NotTopLevel = False
167 %************************************************************************
169 \subsection[Top-level/local]{Top-level/not-top level flag}
171 %************************************************************************
179 isBoxed :: Boxity -> Bool
181 isBoxed Unboxed = False
183 tupleParens :: Boxity -> SDoc -> SDoc
184 tupleParens Boxed p = parens p
185 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
189 %************************************************************************
191 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
193 %************************************************************************
196 data RecFlag = Recursive
199 isRec :: RecFlag -> Bool
200 isRec Recursive = True
201 isRec NonRecursive = False
203 isNonRec :: RecFlag -> Bool
204 isNonRec Recursive = False
205 isNonRec NonRecursive = True
208 %************************************************************************
210 \subsection[Generic]{Generic flag}
212 %************************************************************************
214 This is the "Embedding-Projection pair" datatype, it contains
215 two pieces of code (normally either RenamedHsExpr's or Id's)
216 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
217 represents functions of type
226 T and Tring are arbitrary, but typically T is the 'main' type while
227 Tring is the 'representation' type. (This just helps us remember
228 whether to use 'from' or 'to'.
231 data EP a = EP { fromEP :: a, -- :: T -> Tring
232 toEP :: a } -- :: Tring -> T
235 Embedding-projection pairs are used in several places:
237 First of all, each type constructor has an EP associated with it, the
238 code in EP converts (datatype T) from T to Tring and back again.
240 Secondly, when we are filling in Generic methods (in the typechecker,
241 tcMethodBinds), we are constructing bimaps by induction on the structure
242 of the type of the method signature.
245 %************************************************************************
247 \subsection{Occurrence information}
249 %************************************************************************
251 This data type is used exclusively by the simplifier, but it appears in a
252 SubstResult, which is currently defined in VarEnv, which is pretty near
253 the base of the module hierarchy. So it seemed simpler to put the
254 defn of OccInfo here, safely at the bottom
260 | IAmDead -- Marks unused variables. Sometimes useful for
261 -- lambda and case-bound variables.
267 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
268 -- in a group of recursive definitions
270 seqOccInfo :: OccInfo -> ()
271 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
274 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
275 -- Substituting a redex for this occurrence is
276 -- dangerous because it might duplicate work.
280 type OneBranch = Bool -- True <=> Occurs in only one case branch
281 -- so no code-duplication issue to worry about
285 isLoopBreaker :: OccInfo -> Bool
286 isLoopBreaker IAmALoopBreaker = True
287 isLoopBreaker other = False
289 isDeadOcc :: OccInfo -> Bool
290 isDeadOcc IAmDead = True
291 isDeadOcc other = False
293 isFragileOcc :: OccInfo -> Bool
294 isFragileOcc (OneOcc _ _) = True
295 isFragileOcc other = False
299 instance Outputable OccInfo where
300 -- only used for debugging; never parsed. KSW 1999-07
301 ppr NoOccInfo = empty
302 ppr IAmALoopBreaker = ptext SLIT("_Kx")
303 ppr IAmDead = ptext SLIT("_Kd")
304 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
305 | one_branch = ptext SLIT("_Ks")
306 | otherwise = ptext SLIT("_Ks*")
308 instance Show OccInfo where
309 showsPrec p occ = showsPrecSDoc p (ppr occ)
312 %************************************************************************
314 \subsection{Strictness indication}
316 %************************************************************************
318 The strictness annotations on types in data type declarations
319 e.g. data T = MkT !Int !(Bool,Bool)
323 = MarkedUserStrict -- "!" in a source decl
324 | MarkedStrict -- "!" in an interface decl: strict but not unboxed
325 | MarkedUnboxed -- "!!" in an interface decl: unboxed
326 | NotMarkedStrict -- No annotation at all
329 isMarkedUnboxed MarkedUnboxed = True
330 isMarkedUnboxed other = False
332 isMarkedStrict NotMarkedStrict = False
333 isMarkedStrict other = True -- All others are strict
335 instance Outputable StrictnessMark where
336 ppr MarkedUserStrict = ptext SLIT("!u")
337 ppr MarkedStrict = ptext SLIT("!")
338 ppr MarkedUnboxed = ptext SLIT("! !")
339 ppr NotMarkedStrict = empty
343 %************************************************************************
345 \subsection{Activation}
347 %************************************************************************
349 When a rule or inlining is active
352 type CompilerPhase = Int -- Compilation phase
353 -- Phases decrease towards zero
354 -- Zero is the last phase
356 pprPhase :: CompilerPhase -> SDoc
357 pprPhase n = brackets (int n)
359 data Activation = NeverActive
361 | ActiveAfter CompilerPhase -- Active in this phase and later
362 deriving( Eq ) -- Eq used in comparing rules in HsDecls
364 instance Outputable Activation where
365 ppr AlwaysActive = empty -- The default
366 ppr (ActiveAfter n) = pprPhase n
367 ppr NeverActive = ptext SLIT("NEVER")
369 isActive :: CompilerPhase -> Activation -> Bool
370 isActive p NeverActive = False
371 isActive p AlwaysActive = True
372 isActive p (ActiveAfter n) = p <= n
374 isNeverActive, isAlwaysActive :: Activation -> Bool
375 isNeverActive NeverActive = True
376 isNeverActive act = False
378 isAlwaysActive AlwaysActive = True
379 isAlwaysActive other = False