2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
5 \section[BasicTypes]{Miscellanous types}
7 This module defines a miscellaneously collection of very simple
11 \item have no other obvious home
12 \item don't depend on any other complicated types
13 \item are used in more than one "part" of the compiler
18 Version, bumpVersion, initialVersion,
24 Fixity(..), FixityDirection(..),
25 defaultFixity, maxPrecedence,
26 negateFixity, funTyFixity,
29 IPName(..), ipNameName, mapIPName,
31 RecFlag(..), isRec, isNonRec, boolToRecFlag,
33 TopLevelFlag(..), isTopLevel, isNotTopLevel,
39 TupCon(..), tupleParens,
41 OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc,
42 isDeadOcc, isLoopBreaker, isNonRuleLoopBreaker, isNoOcc,
44 InsideLam, insideLam, notInsideLam,
45 OneBranch, oneBranch, notOneBranch,
50 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
53 Activation(..), isActive, isNeverActive, isAlwaysActive,
54 InlineSpec(..), defaultInlineSpec, alwaysInlineSpec, neverInlineSpec,
56 SuccessFlag(..), succeeded, failed, successIf
63 %************************************************************************
65 \subsection[Arity]{Arity}
67 %************************************************************************
74 %************************************************************************
76 \subsection[Version]{Module and identifier version numbers}
78 %************************************************************************
83 bumpVersion :: Version -> Version
86 initialVersion :: Version
90 %************************************************************************
94 %************************************************************************
98 type DeprecTxt = FastString -- reason/explanation for deprecation
101 %************************************************************************
103 \subsection{Implicit parameter identity}
105 %************************************************************************
107 The @IPName@ type is here because it is used in TypeRep (i.e. very
108 early in the hierarchy), but also in HsSyn.
111 newtype IPName name = IPName name -- ?x
112 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
113 -- (used in HscTypes.OrigIParamCache)
115 ipNameName :: IPName name -> name
116 ipNameName (IPName n) = n
118 mapIPName :: (a->b) -> IPName a -> IPName b
119 mapIPName f (IPName n) = IPName (f n)
121 instance Outputable name => Outputable (IPName name) where
122 ppr (IPName n) = char '?' <> ppr n -- Ordinary implicit parameters
126 %************************************************************************
128 \subsection[Fixity]{Fixity info}
130 %************************************************************************
133 ------------------------
134 data Fixity = Fixity Int FixityDirection
136 instance Outputable Fixity where
137 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
139 instance Eq Fixity where -- Used to determine if two fixities conflict
140 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
142 ------------------------
143 data FixityDirection = InfixL | InfixR | InfixN
146 instance Outputable FixityDirection where
147 ppr InfixL = ptext (sLit "infixl")
148 ppr InfixR = ptext (sLit "infixr")
149 ppr InfixN = ptext (sLit "infix")
151 ------------------------
154 defaultFixity :: Fixity
155 defaultFixity = Fixity maxPrecedence InfixL
157 negateFixity, funTyFixity :: Fixity
159 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
160 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
168 @(compareFixity op1 op2)@ tells which way to arrange appication, or
169 whether there's an error.
172 compareFixity :: Fixity -> Fixity
173 -> (Bool, -- Error please
174 Bool) -- Associate to the right: a op1 (b op2 c)
175 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
176 = case prec1 `compare` prec2 of
179 EQ -> case (dir1, dir2) of
180 (InfixR, InfixR) -> right
181 (InfixL, InfixL) -> left
184 right = (False, True)
185 left = (False, False)
186 error_please = (True, False)
190 %************************************************************************
192 \subsection[Top-level/local]{Top-level/not-top level flag}
194 %************************************************************************
201 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
203 isNotTopLevel NotTopLevel = True
204 isNotTopLevel TopLevel = False
206 isTopLevel TopLevel = True
207 isTopLevel NotTopLevel = False
209 instance Outputable TopLevelFlag where
210 ppr TopLevel = ptext (sLit "<TopLevel>")
211 ppr NotTopLevel = ptext (sLit "<NotTopLevel>")
215 %************************************************************************
217 Top-level/not-top level flag
219 %************************************************************************
227 isBoxed :: Boxity -> Bool
229 isBoxed Unboxed = False
233 %************************************************************************
235 Recursive/Non-Recursive flag
237 %************************************************************************
240 data RecFlag = Recursive
244 isRec :: RecFlag -> Bool
245 isRec Recursive = True
246 isRec NonRecursive = False
248 isNonRec :: RecFlag -> Bool
249 isNonRec Recursive = False
250 isNonRec NonRecursive = True
252 boolToRecFlag :: Bool -> RecFlag
253 boolToRecFlag True = Recursive
254 boolToRecFlag False = NonRecursive
256 instance Outputable RecFlag where
257 ppr Recursive = ptext (sLit "Recursive")
258 ppr NonRecursive = ptext (sLit "NonRecursive")
261 %************************************************************************
263 Instance overlap flag
265 %************************************************************************
269 = NoOverlap -- This instance must not overlap another
271 | OverlapOk -- Silently ignore this instance if you find a
272 -- more specific one that matches the constraint
273 -- you are trying to resolve
275 -- Example: constraint (Foo [Int])
276 -- instances (Foo [Int])
277 -- (Foo [a]) OverlapOk
278 -- Since the second instance has the OverlapOk flag,
279 -- the first instance will be chosen (otherwise
280 -- its ambiguous which to choose)
282 | Incoherent -- Like OverlapOk, but also ignore this instance
283 -- if it doesn't match the constraint you are
284 -- trying to resolve, but could match if the type variables
285 -- in the constraint were instantiated
287 -- Example: constraint (Foo [b])
288 -- instances (Foo [Int]) Incoherent
290 -- Without the Incoherent flag, we'd complain that
291 -- instantiating 'b' would change which instance
295 instance Outputable OverlapFlag where
296 ppr NoOverlap = empty
297 ppr OverlapOk = ptext (sLit "[overlap ok]")
298 ppr Incoherent = ptext (sLit "[incoherent]")
302 %************************************************************************
306 %************************************************************************
309 data TupCon = TupCon Boxity Arity
311 instance Eq TupCon where
312 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
314 tupleParens :: Boxity -> SDoc -> SDoc
315 tupleParens Boxed p = parens p
316 tupleParens Unboxed p = ptext (sLit "(#") <+> p <+> ptext (sLit "#)")
319 %************************************************************************
321 \subsection[Generic]{Generic flag}
323 %************************************************************************
325 This is the "Embedding-Projection pair" datatype, it contains
326 two pieces of code (normally either RenamedExpr's or Id's)
327 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
328 represents functions of type
337 T and Tring are arbitrary, but typically T is the 'main' type while
338 Tring is the 'representation' type. (This just helps us remember
339 whether to use 'from' or 'to'.
342 data EP a = EP { fromEP :: a, -- :: T -> Tring
343 toEP :: a } -- :: Tring -> T
346 Embedding-projection pairs are used in several places:
348 First of all, each type constructor has an EP associated with it, the
349 code in EP converts (datatype T) from T to Tring and back again.
351 Secondly, when we are filling in Generic methods (in the typechecker,
352 tcMethodBinds), we are constructing bimaps by induction on the structure
353 of the type of the method signature.
356 %************************************************************************
358 \subsection{Occurrence information}
360 %************************************************************************
362 This data type is used exclusively by the simplifier, but it appears in a
363 SubstResult, which is currently defined in VarEnv, which is pretty near
364 the base of the module hierarchy. So it seemed simpler to put the
365 defn of OccInfo here, safely at the bottom
369 = NoOccInfo -- Many occurrences, or unknown
371 | IAmDead -- Marks unused variables. Sometimes useful for
372 -- lambda and case-bound variables.
374 | OneOcc -- Occurs exactly once, not inside a rule
379 | IAmALoopBreaker -- Note [LoopBreaker OccInfo]
380 !RulesOnly -- True <=> This is a weak or rules-only loop breaker
381 -- See OccurAnal Note [Weak loop breakers]
383 type RulesOnly = Bool
386 Note [LoopBreaker OccInfo]
387 ~~~~~~~~~~~~~~~~~~~~~~~~~~
388 An OccInfo of (IAmLoopBreaker False) is used by the occurrence
389 analyser in two ways:
390 (a) to mark loop-breakers in a group of recursive
391 definitions (hence the name)
392 (b) to mark binders that must not be inlined in this phase
393 (perhaps it has a NOINLINE pragma)
394 Things with (IAmLoopBreaker False) do not get an unfolding
395 pinned on to them, so they are completely opaque.
397 See OccurAnal Note [Weak loop breakers] for (IAmLoopBreaker True).
401 isNoOcc :: OccInfo -> Bool
402 isNoOcc NoOccInfo = True
405 seqOccInfo :: OccInfo -> ()
406 seqOccInfo occ = occ `seq` ()
409 type InterestingCxt = Bool -- True <=> Function: is applied
410 -- Data value: scrutinised by a case with
411 -- at least one non-DEFAULT branch
414 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
415 -- Substituting a redex for this occurrence is
416 -- dangerous because it might duplicate work.
417 insideLam, notInsideLam :: InsideLam
422 type OneBranch = Bool -- True <=> Occurs in only one case branch
423 -- so no code-duplication issue to worry about
424 oneBranch, notOneBranch :: OneBranch
428 isLoopBreaker :: OccInfo -> Bool
429 isLoopBreaker (IAmALoopBreaker _) = True
430 isLoopBreaker _ = False
432 isNonRuleLoopBreaker :: OccInfo -> Bool
433 isNonRuleLoopBreaker (IAmALoopBreaker False) = True -- Loop-breaker that breaks a non-rule cycle
434 isNonRuleLoopBreaker _ = False
436 isDeadOcc :: OccInfo -> Bool
437 isDeadOcc IAmDead = True
440 isOneOcc :: OccInfo -> Bool
441 isOneOcc (OneOcc _ _ _) = True
444 isFragileOcc :: OccInfo -> Bool
445 isFragileOcc (OneOcc _ _ _) = True
446 isFragileOcc _ = False
450 instance Outputable OccInfo where
451 -- only used for debugging; never parsed. KSW 1999-07
452 ppr NoOccInfo = empty
453 ppr (IAmALoopBreaker ro) = ptext (sLit "LoopBreaker") <> if ro then char '!' else empty
454 ppr IAmDead = ptext (sLit "Dead")
455 ppr (OneOcc inside_lam one_branch int_cxt)
456 = ptext (sLit "Once") <> pp_lam <> pp_br <> pp_args
458 pp_lam | inside_lam = char 'L'
460 pp_br | one_branch = empty
461 | otherwise = char '*'
462 pp_args | int_cxt = char '!'
465 instance Show OccInfo where
466 showsPrec p occ = showsPrecSDoc p (ppr occ)
469 %************************************************************************
471 \subsection{Strictness indication}
473 %************************************************************************
475 The strictness annotations on types in data type declarations
476 e.g. data T = MkT !Int !(Bool,Bool)
479 data StrictnessMark -- Used in interface decls only
485 isMarkedUnboxed :: StrictnessMark -> Bool
486 isMarkedUnboxed MarkedUnboxed = True
487 isMarkedUnboxed _ = False
489 isMarkedStrict :: StrictnessMark -> Bool
490 isMarkedStrict NotMarkedStrict = False
491 isMarkedStrict _ = True -- All others are strict
493 instance Outputable StrictnessMark where
494 ppr MarkedStrict = ptext (sLit "!")
495 ppr MarkedUnboxed = ptext (sLit "!!")
496 ppr NotMarkedStrict = ptext (sLit "_")
500 %************************************************************************
502 \subsection{Success flag}
504 %************************************************************************
507 data SuccessFlag = Succeeded | Failed
509 instance Outputable SuccessFlag where
510 ppr Succeeded = ptext (sLit "Succeeded")
511 ppr Failed = ptext (sLit "Failed")
513 successIf :: Bool -> SuccessFlag
514 successIf True = Succeeded
515 successIf False = Failed
517 succeeded, failed :: SuccessFlag -> Bool
518 succeeded Succeeded = True
519 succeeded Failed = False
521 failed Succeeded = False
526 %************************************************************************
528 \subsection{Activation}
530 %************************************************************************
532 When a rule or inlining is active
535 type CompilerPhase = Int -- Compilation phase
536 -- Phases decrease towards zero
537 -- Zero is the last phase
539 data Activation = NeverActive
541 | ActiveBefore CompilerPhase -- Active only *before* this phase
542 | ActiveAfter CompilerPhase -- Active in this phase and later
543 deriving( Eq ) -- Eq used in comparing rules in HsDecls
547 Activation -- Says during which phases inlining is allowed
548 Bool -- True <=> make the RHS look small, so that when inlining
549 -- is enabled, it will definitely actually happen
552 defaultInlineSpec, alwaysInlineSpec, neverInlineSpec :: InlineSpec
554 defaultInlineSpec = Inline AlwaysActive False -- Inlining is OK, but not forced
555 alwaysInlineSpec = Inline AlwaysActive True -- INLINE always
556 neverInlineSpec = Inline NeverActive False -- NOINLINE
558 instance Outputable Activation where
559 ppr NeverActive = ptext (sLit "NEVER")
560 ppr AlwaysActive = ptext (sLit "ALWAYS")
561 ppr (ActiveBefore n) = brackets (char '~' <> int n)
562 ppr (ActiveAfter n) = brackets (int n)
564 instance Outputable InlineSpec where
565 ppr (Inline act is_inline)
566 | is_inline = ptext (sLit "INLINE")
568 AlwaysActive -> empty
570 | otherwise = ptext (sLit "NOINLINE")
575 isActive :: CompilerPhase -> Activation -> Bool
576 isActive _ NeverActive = False
577 isActive _ AlwaysActive = True
578 isActive p (ActiveAfter n) = p <= n
579 isActive p (ActiveBefore n) = p > n
581 isNeverActive, isAlwaysActive :: Activation -> Bool
582 isNeverActive NeverActive = True
583 isNeverActive _ = False
585 isAlwaysActive AlwaysActive = True
586 isAlwaysActive _ = False