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
148 %************************************************************************
150 \subsection[Top-level/local]{Top-level/not-top level flag}
152 %************************************************************************
159 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
161 isNotTopLevel NotTopLevel = True
162 isNotTopLevel TopLevel = False
164 isTopLevel TopLevel = True
165 isTopLevel NotTopLevel = False
168 %************************************************************************
170 \subsection[Top-level/local]{Top-level/not-top level flag}
172 %************************************************************************
180 isBoxed :: Boxity -> Bool
182 isBoxed Unboxed = False
184 tupleParens :: Boxity -> SDoc -> SDoc
185 tupleParens Boxed p = parens p
186 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
190 %************************************************************************
192 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
194 %************************************************************************
197 data RecFlag = Recursive
200 isRec :: RecFlag -> Bool
201 isRec Recursive = True
202 isRec NonRecursive = False
204 isNonRec :: RecFlag -> Bool
205 isNonRec Recursive = False
206 isNonRec NonRecursive = True
209 %************************************************************************
211 \subsection[Generic]{Generic flag}
213 %************************************************************************
215 This is the "Embedding-Projection pair" datatype, it contains
216 two pieces of code (normally either RenamedHsExpr's or Id's)
217 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
218 represents functions of type
227 T and Tring are arbitrary, but typically T is the 'main' type while
228 Tring is the 'representation' type. (This just helps us remember
229 whether to use 'from' or 'to'.
232 data EP a = EP { fromEP :: a, -- :: T -> Tring
233 toEP :: a } -- :: Tring -> T
236 Embedding-projection pairs are used in several places:
238 First of all, each type constructor has an EP associated with it, the
239 code in EP converts (datatype T) from T to Tring and back again.
241 Secondly, when we are filling in Generic methods (in the typechecker,
242 tcMethodBinds), we are constructing bimaps by induction on the structure
243 of the type of the method signature.
246 %************************************************************************
248 \subsection{Occurrence information}
250 %************************************************************************
252 This data type is used exclusively by the simplifier, but it appears in a
253 SubstResult, which is currently defined in VarEnv, which is pretty near
254 the base of the module hierarchy. So it seemed simpler to put the
255 defn of OccInfo here, safely at the bottom
261 | IAmDead -- Marks unused variables. Sometimes useful for
262 -- lambda and case-bound variables.
268 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
269 -- in a group of recursive definitions
271 seqOccInfo :: OccInfo -> ()
272 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
275 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
276 -- Substituting a redex for this occurrence is
277 -- dangerous because it might duplicate work.
281 type OneBranch = Bool -- True <=> Occurs in only one case branch
282 -- so no code-duplication issue to worry about
286 isLoopBreaker :: OccInfo -> Bool
287 isLoopBreaker IAmALoopBreaker = True
288 isLoopBreaker other = False
290 isDeadOcc :: OccInfo -> Bool
291 isDeadOcc IAmDead = True
292 isDeadOcc other = False
294 isFragileOcc :: OccInfo -> Bool
295 isFragileOcc (OneOcc _ _) = True
296 isFragileOcc other = False
300 instance Outputable OccInfo where
301 -- only used for debugging; never parsed. KSW 1999-07
302 ppr NoOccInfo = empty
303 ppr IAmALoopBreaker = ptext SLIT("_Kx")
304 ppr IAmDead = ptext SLIT("_Kd")
305 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
306 | one_branch = ptext SLIT("_Ks")
307 | otherwise = ptext SLIT("_Ks*")
309 instance Show OccInfo where
310 showsPrec p occ = showsPrecSDoc p (ppr occ)
313 %************************************************************************
315 \subsection{Strictness indication}
317 %************************************************************************
319 The strictness annotations on types in data type declarations
320 e.g. data T = MkT !Int !(Bool,Bool)
324 = MarkedUserStrict -- "!" in a source decl
325 | MarkedStrict -- "!" in an interface decl: strict but not unboxed
326 | MarkedUnboxed -- "!!" in an interface decl: unboxed
327 | NotMarkedStrict -- No annotation at all
330 isMarkedUnboxed MarkedUnboxed = True
331 isMarkedUnboxed other = False
333 isMarkedStrict NotMarkedStrict = False
334 isMarkedStrict other = True -- All others are strict
336 instance Outputable StrictnessMark where
337 ppr MarkedUserStrict = ptext SLIT("!u")
338 ppr MarkedStrict = ptext SLIT("!")
339 ppr MarkedUnboxed = ptext SLIT("! !")
340 ppr NotMarkedStrict = empty
344 %************************************************************************
346 \subsection{Activation}
348 %************************************************************************
350 When a rule or inlining is active
353 type CompilerPhase = Int -- Compilation phase
354 -- Phases decrease towards zero
355 -- Zero is the last phase
357 pprPhase :: CompilerPhase -> SDoc
358 pprPhase n = brackets (int n)
360 data Activation = NeverActive
362 | ActiveAfter CompilerPhase -- Active in this phase and later
363 deriving( Eq ) -- Eq used in comparing rules in HsDecls
365 instance Outputable Activation where
366 ppr AlwaysActive = empty -- The default
367 ppr (ActiveAfter n) = pprPhase n
368 ppr NeverActive = ptext SLIT("NEVER")
370 isActive :: CompilerPhase -> Activation -> Bool
371 isActive p NeverActive = False
372 isActive p AlwaysActive = True
373 isActive p (ActiveAfter n) = p <= n
375 isNeverActive, isAlwaysActive :: Activation -> Bool
376 isNeverActive NeverActive = True
377 isNeverActive act = False
379 isAlwaysActive AlwaysActive = True
380 isAlwaysActive other = False