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
17 {-# LANGUAGE DeriveDataTypeable #-}
20 Version, bumpVersion, initialVersion,
28 Fixity(..), FixityDirection(..),
29 defaultFixity, maxPrecedence,
30 negateFixity, funTyFixity,
33 IPName(..), ipNameName, mapIPName,
35 RecFlag(..), isRec, isNonRec, boolToRecFlag,
39 TopLevelFlag(..), isTopLevel, isNotTopLevel,
45 TupCon(..), tupleParens,
47 OccInfo(..), seqOccInfo, zapFragileOcc, isOneOcc,
48 isDeadOcc, isLoopBreaker, isNonRuleLoopBreaker, isNoOcc,
51 InsideLam, insideLam, notInsideLam,
52 OneBranch, oneBranch, notOneBranch,
57 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
60 Activation(..), isActive, isNeverActive, isAlwaysActive, isEarlyActive,
61 RuleMatchInfo(..), isConLike, isFunLike,
62 InlinePragma(..), defaultInlinePragma, alwaysInlinePragma, neverInlinePragma, dfunInlinePragma,
63 isDefaultInlinePragma, isInlinePragma, inlinePragmaSat,
64 inlinePragmaActivation, inlinePragmaRuleMatchInfo,
65 setInlinePragmaActivation, setInlinePragmaRuleMatchInfo,
67 SuccessFlag(..), succeeded, failed, successIf
73 import Data.Data hiding (Fixity)
76 %************************************************************************
78 \subsection[Arity]{Arity}
80 %************************************************************************
86 %************************************************************************
88 \subsection[FunctionOrData]{FunctionOrData}
90 %************************************************************************
93 data FunctionOrData = IsFunction | IsData
94 deriving (Eq, Ord, Data, Typeable)
96 instance Outputable FunctionOrData where
97 ppr IsFunction = text "(function)"
98 ppr IsData = text "(data)"
102 %************************************************************************
104 \subsection[Version]{Module and identifier version numbers}
106 %************************************************************************
111 bumpVersion :: Version -> Version
114 initialVersion :: Version
118 %************************************************************************
122 %************************************************************************
126 -- reason/explanation from a WARNING or DEPRECATED pragma
127 data WarningTxt = WarningTxt [FastString]
128 | DeprecatedTxt [FastString]
129 deriving (Eq, Data, Typeable)
131 instance Outputable WarningTxt where
132 ppr (WarningTxt ws) = doubleQuotes (vcat (map ftext ws))
133 ppr (DeprecatedTxt ds) = text "Deprecated:" <+>
134 doubleQuotes (vcat (map ftext ds))
137 %************************************************************************
139 \subsection{Implicit parameter identity}
141 %************************************************************************
143 The @IPName@ type is here because it is used in TypeRep (i.e. very
144 early in the hierarchy), but also in HsSyn.
147 newtype IPName name = IPName name -- ?x
148 deriving( Eq, Ord, Data, Typeable )
149 -- Ord is used in the IP name cache finite map
150 -- (used in HscTypes.OrigIParamCache)
152 ipNameName :: IPName name -> name
153 ipNameName (IPName n) = n
155 mapIPName :: (a->b) -> IPName a -> IPName b
156 mapIPName f (IPName n) = IPName (f n)
158 instance Outputable name => Outputable (IPName name) where
159 ppr (IPName n) = char '?' <> ppr n -- Ordinary implicit parameters
162 %************************************************************************
166 %************************************************************************
169 type RuleName = FastString
172 %************************************************************************
174 \subsection[Fixity]{Fixity info}
176 %************************************************************************
179 ------------------------
180 data Fixity = Fixity Int FixityDirection
181 deriving (Data, Typeable)
183 instance Outputable Fixity where
184 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
186 instance Eq Fixity where -- Used to determine if two fixities conflict
187 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
189 ------------------------
190 data FixityDirection = InfixL | InfixR | InfixN
191 deriving (Eq, Data, Typeable)
193 instance Outputable FixityDirection where
194 ppr InfixL = ptext (sLit "infixl")
195 ppr InfixR = ptext (sLit "infixr")
196 ppr InfixN = ptext (sLit "infix")
198 ------------------------
201 defaultFixity :: Fixity
202 defaultFixity = Fixity maxPrecedence InfixL
204 negateFixity, funTyFixity :: Fixity
206 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
207 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
215 @(compareFixity op1 op2)@ tells which way to arrange appication, or
216 whether there's an error.
219 compareFixity :: Fixity -> Fixity
220 -> (Bool, -- Error please
221 Bool) -- Associate to the right: a op1 (b op2 c)
222 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
223 = case prec1 `compare` prec2 of
226 EQ -> case (dir1, dir2) of
227 (InfixR, InfixR) -> right
228 (InfixL, InfixL) -> left
231 right = (False, True)
232 left = (False, False)
233 error_please = (True, False)
237 %************************************************************************
239 \subsection[Top-level/local]{Top-level/not-top level flag}
241 %************************************************************************
248 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
250 isNotTopLevel NotTopLevel = True
251 isNotTopLevel TopLevel = False
253 isTopLevel TopLevel = True
254 isTopLevel NotTopLevel = False
256 instance Outputable TopLevelFlag where
257 ppr TopLevel = ptext (sLit "<TopLevel>")
258 ppr NotTopLevel = ptext (sLit "<NotTopLevel>")
262 %************************************************************************
264 Top-level/not-top level flag
266 %************************************************************************
272 deriving( Eq, Data, Typeable )
274 isBoxed :: Boxity -> Bool
276 isBoxed Unboxed = False
280 %************************************************************************
282 Recursive/Non-Recursive flag
284 %************************************************************************
287 data RecFlag = Recursive
289 deriving( Eq, Data, Typeable )
291 isRec :: RecFlag -> Bool
292 isRec Recursive = True
293 isRec NonRecursive = False
295 isNonRec :: RecFlag -> Bool
296 isNonRec Recursive = False
297 isNonRec NonRecursive = True
299 boolToRecFlag :: Bool -> RecFlag
300 boolToRecFlag True = Recursive
301 boolToRecFlag False = NonRecursive
303 instance Outputable RecFlag where
304 ppr Recursive = ptext (sLit "Recursive")
305 ppr NonRecursive = ptext (sLit "NonRecursive")
308 %************************************************************************
310 Instance overlap flag
312 %************************************************************************
316 = NoOverlap -- This instance must not overlap another
318 | OverlapOk -- Silently ignore this instance if you find a
319 -- more specific one that matches the constraint
320 -- you are trying to resolve
322 -- Example: constraint (Foo [Int])
323 -- instances (Foo [Int])
324 -- (Foo [a]) OverlapOk
325 -- Since the second instance has the OverlapOk flag,
326 -- the first instance will be chosen (otherwise
327 -- its ambiguous which to choose)
329 | Incoherent -- Like OverlapOk, but also ignore this instance
330 -- if it doesn't match the constraint you are
331 -- trying to resolve, but could match if the type variables
332 -- in the constraint were instantiated
334 -- Example: constraint (Foo [b])
335 -- instances (Foo [Int]) Incoherent
337 -- Without the Incoherent flag, we'd complain that
338 -- instantiating 'b' would change which instance
342 instance Outputable OverlapFlag where
343 ppr NoOverlap = empty
344 ppr OverlapOk = ptext (sLit "[overlap ok]")
345 ppr Incoherent = ptext (sLit "[incoherent]")
349 %************************************************************************
353 %************************************************************************
356 data TupCon = TupCon Boxity Arity
358 instance Eq TupCon where
359 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
361 tupleParens :: Boxity -> SDoc -> SDoc
362 tupleParens Boxed p = parens p
363 tupleParens Unboxed p = ptext (sLit "(#") <+> p <+> ptext (sLit "#)")
366 %************************************************************************
368 \subsection[Generic]{Generic flag}
370 %************************************************************************
372 This is the "Embedding-Projection pair" datatype, it contains
373 two pieces of code (normally either RenamedExpr's or Id's)
374 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
375 represents functions of type
384 T and Tring are arbitrary, but typically T is the 'main' type while
385 Tring is the 'representation' type. (This just helps us remember
386 whether to use 'from' or 'to'.
389 data EP a = EP { fromEP :: a, -- :: T -> Tring
390 toEP :: a } -- :: Tring -> T
393 Embedding-projection pairs are used in several places:
395 First of all, each type constructor has an EP associated with it, the
396 code in EP converts (datatype T) from T to Tring and back again.
398 Secondly, when we are filling in Generic methods (in the typechecker,
399 tcMethodBinds), we are constructing bimaps by induction on the structure
400 of the type of the method signature.
403 %************************************************************************
405 \subsection{Occurrence information}
407 %************************************************************************
409 This data type is used exclusively by the simplifier, but it appears in a
410 SubstResult, which is currently defined in VarEnv, which is pretty near
411 the base of the module hierarchy. So it seemed simpler to put the
412 defn of OccInfo here, safely at the bottom
415 -- | Identifier occurrence information
417 = NoOccInfo -- ^ There are many occurrences, or unknown occurences
419 | IAmDead -- ^ Marks unused variables. Sometimes useful for
420 -- lambda and case-bound variables.
425 !InterestingCxt -- ^ Occurs exactly once, not inside a rule
427 -- | This identifier breaks a loop of mutually recursive functions. The field
428 -- marks whether it is only a loop breaker due to a reference in a rule
429 | IAmALoopBreaker -- Note [LoopBreaker OccInfo]
430 !RulesOnly -- True <=> This is a weak or rules-only loop breaker
431 -- See OccurAnal Note [Weak loop breakers]
433 type RulesOnly = Bool
436 Note [LoopBreaker OccInfo]
437 ~~~~~~~~~~~~~~~~~~~~~~~~~~
438 An OccInfo of (IAmLoopBreaker False) is used by the occurrence
439 analyser in two ways:
440 (a) to mark loop-breakers in a group of recursive
441 definitions (hence the name)
442 (b) to mark binders that must not be inlined in this phase
443 (perhaps it has a NOINLINE pragma)
444 Things with (IAmLoopBreaker False) do not get an unfolding
445 pinned on to them, so they are completely opaque.
447 See OccurAnal Note [Weak loop breakers] for (IAmLoopBreaker True).
451 isNoOcc :: OccInfo -> Bool
452 isNoOcc NoOccInfo = True
455 seqOccInfo :: OccInfo -> ()
456 seqOccInfo occ = occ `seq` ()
459 type InterestingCxt = Bool -- True <=> Function: is applied
460 -- Data value: scrutinised by a case with
461 -- at least one non-DEFAULT branch
464 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
465 -- Substituting a redex for this occurrence is
466 -- dangerous because it might duplicate work.
467 insideLam, notInsideLam :: InsideLam
472 type OneBranch = Bool -- True <=> Occurs in only one case branch
473 -- so no code-duplication issue to worry about
474 oneBranch, notOneBranch :: OneBranch
478 isLoopBreaker :: OccInfo -> Bool
479 isLoopBreaker (IAmALoopBreaker _) = True
480 isLoopBreaker _ = False
482 isNonRuleLoopBreaker :: OccInfo -> Bool
483 isNonRuleLoopBreaker (IAmALoopBreaker False) = True -- Loop-breaker that breaks a non-rule cycle
484 isNonRuleLoopBreaker _ = False
486 nonRuleLoopBreaker :: OccInfo
487 nonRuleLoopBreaker = IAmALoopBreaker False
489 isDeadOcc :: OccInfo -> Bool
490 isDeadOcc IAmDead = True
493 isOneOcc :: OccInfo -> Bool
494 isOneOcc (OneOcc {}) = True
497 zapFragileOcc :: OccInfo -> OccInfo
498 zapFragileOcc (OneOcc {}) = NoOccInfo
499 zapFragileOcc occ = occ
503 instance Outputable OccInfo where
504 -- only used for debugging; never parsed. KSW 1999-07
505 ppr NoOccInfo = empty
506 ppr (IAmALoopBreaker ro) = ptext (sLit "LoopBreaker") <> if ro then char '!' else empty
507 ppr IAmDead = ptext (sLit "Dead")
508 ppr (OneOcc inside_lam one_branch int_cxt)
509 = ptext (sLit "Once") <> pp_lam <> pp_br <> pp_args
511 pp_lam | inside_lam = char 'L'
513 pp_br | one_branch = empty
514 | otherwise = char '*'
515 pp_args | int_cxt = char '!'
518 instance Show OccInfo where
519 showsPrec p occ = showsPrecSDoc p (ppr occ)
522 %************************************************************************
524 \subsection{Strictness indication}
526 %************************************************************************
528 The strictness annotations on types in data type declarations
529 e.g. data T = MkT !Int !(Bool,Bool)
532 data StrictnessMark -- Used in interface decls only
538 isMarkedUnboxed :: StrictnessMark -> Bool
539 isMarkedUnboxed MarkedUnboxed = True
540 isMarkedUnboxed _ = False
542 isMarkedStrict :: StrictnessMark -> Bool
543 isMarkedStrict NotMarkedStrict = False
544 isMarkedStrict _ = True -- All others are strict
546 instance Outputable StrictnessMark where
547 ppr MarkedStrict = ptext (sLit "!")
548 ppr MarkedUnboxed = ptext (sLit "!!")
549 ppr NotMarkedStrict = ptext (sLit "_")
553 %************************************************************************
555 \subsection{Success flag}
557 %************************************************************************
560 data SuccessFlag = Succeeded | Failed
562 instance Outputable SuccessFlag where
563 ppr Succeeded = ptext (sLit "Succeeded")
564 ppr Failed = ptext (sLit "Failed")
566 successIf :: Bool -> SuccessFlag
567 successIf True = Succeeded
568 successIf False = Failed
570 succeeded, failed :: SuccessFlag -> Bool
571 succeeded Succeeded = True
572 succeeded Failed = False
574 failed Succeeded = False
579 %************************************************************************
581 \subsection{Activation}
583 %************************************************************************
585 When a rule or inlining is active
588 type CompilerPhase = Int -- Compilation phase
589 -- Phases decrease towards zero
590 -- Zero is the last phase
592 data Activation = NeverActive
594 | ActiveBefore CompilerPhase -- Active only *before* this phase
595 | ActiveAfter CompilerPhase -- Active in this phase and later
596 deriving( Eq, Data, Typeable ) -- Eq used in comparing rules in HsDecls
598 data RuleMatchInfo = ConLike -- See Note [CONLIKE pragma]
600 deriving( Eq, Data, Typeable )
602 data InlinePragma -- Note [InlinePragma]
604 { inl_inline :: Bool -- True <=> INLINE,
605 -- False <=> no pragma at all, or NOINLINE
606 , inl_sat :: Maybe Arity -- Just n <=> Inline only when applied to n
607 -- explicit (non-type, non-dictionary) args
608 , inl_act :: Activation -- Says during which phases inlining is allowed
609 , inl_rule :: RuleMatchInfo -- Should the function be treated like a constructor?
610 } deriving( Eq, Data, Typeable )
615 This data type mirrors what you can write in an INLINE or NOINLINE pragma in
618 If you write nothing at all, you get defaultInlinePragma:
620 inl_act = AlwaysActive
623 It's not possible to get that combination by *writing* something, so
624 if an Id has defaultInlinePragma it means the user didn't specify anything.
626 If inl_inline = True, then the Id should have an InlineRule unfolding.
628 Note [CONLIKE pragma]
629 ~~~~~~~~~~~~~~~~~~~~~
630 The ConLike constructor of a RuleMatchInfo is aimed at the following.
632 {-# RULE "r/cons" forall a as. r (a:as) = f (a+1) #-}
633 g b bs = let x = b:bs in ..x...x...(r x)...
634 Now, the rule applies to the (r x) term, because GHC "looks through"
635 the definition of 'x' to see that it is (b:bs).
638 {-# RULE "r/f" forall v. r (f v) = f (v+1) #-}
639 g v = let x = f v in ..x...x...(r x)...
640 Normally the (r x) would *not* match the rule, because GHC would be
641 scared about duplicating the redex (f v), so it does not "look
642 through" the bindings.
644 However the CONLIKE modifier says to treat 'f' like a constructor in
645 this situation, and "look through" the unfolding for x. So (r x)
646 fires, yielding (f (v+1)).
648 This is all controlled with a user-visible pragma:
649 {-# NOINLINE CONLIKE [1] f #-}
651 The main effects of CONLIKE are:
653 - The occurrence analyser (OccAnal) and simplifier (Simplify) treat
654 CONLIKE thing like constructors, by ANF-ing them
656 - New function coreUtils.exprIsExpandable is like exprIsCheap, but
657 additionally spots applications of CONLIKE functions
659 - A CoreUnfolding has a field that caches exprIsExpandable
661 - The rule matcher consults this field. See
662 Note [Expanding variables] in Rules.lhs.
665 isConLike :: RuleMatchInfo -> Bool
666 isConLike ConLike = True
669 isFunLike :: RuleMatchInfo -> Bool
670 isFunLike FunLike = True
673 defaultInlinePragma, alwaysInlinePragma, neverInlinePragma, dfunInlinePragma
675 defaultInlinePragma = InlinePragma { inl_act = AlwaysActive
678 , inl_sat = Nothing }
680 alwaysInlinePragma = defaultInlinePragma { inl_inline = True }
681 neverInlinePragma = defaultInlinePragma { inl_act = NeverActive }
683 -- A DFun has an always-active inline activation so that
684 -- exprIsConApp_maybe can "see" its unfolding
685 -- (However, its actual Unfolding is a DFunUnfolding, which is
686 -- never inlined other than via exprIsConApp_maybe.)
687 dfunInlinePragma = defaultInlinePragma { inl_act = AlwaysActive
688 , inl_rule = ConLike }
690 isDefaultInlinePragma :: InlinePragma -> Bool
691 isDefaultInlinePragma (InlinePragma { inl_act = activation
692 , inl_rule = match_info
693 , inl_inline = inline })
694 = not inline && isAlwaysActive activation && isFunLike match_info
696 isInlinePragma :: InlinePragma -> Bool
697 isInlinePragma prag = inl_inline prag
699 inlinePragmaSat :: InlinePragma -> Maybe Arity
700 inlinePragmaSat = inl_sat
702 inlinePragmaActivation :: InlinePragma -> Activation
703 inlinePragmaActivation (InlinePragma { inl_act = activation }) = activation
705 inlinePragmaRuleMatchInfo :: InlinePragma -> RuleMatchInfo
706 inlinePragmaRuleMatchInfo (InlinePragma { inl_rule = info }) = info
708 setInlinePragmaActivation :: InlinePragma -> Activation -> InlinePragma
709 setInlinePragmaActivation prag activation = prag { inl_act = activation }
711 setInlinePragmaRuleMatchInfo :: InlinePragma -> RuleMatchInfo -> InlinePragma
712 setInlinePragmaRuleMatchInfo prag info = prag { inl_rule = info }
714 instance Outputable Activation where
715 ppr AlwaysActive = brackets (ptext (sLit "ALWAYS"))
716 ppr NeverActive = brackets (ptext (sLit "NEVER"))
717 ppr (ActiveBefore n) = brackets (char '~' <> int n)
718 ppr (ActiveAfter n) = brackets (int n)
720 instance Outputable RuleMatchInfo where
721 ppr ConLike = ptext (sLit "CONLIKE")
722 ppr FunLike = ptext (sLit "FUNLIKE")
724 instance Outputable InlinePragma where
725 ppr (InlinePragma { inl_inline = inline, inl_act = activation
726 , inl_rule = info, inl_sat = mb_arity })
727 = pp_inl_act (inline, activation) <+> pp_sat <+> pp_info
729 pp_inl_act (False, AlwaysActive) = empty -- defaultInlinePragma
730 pp_inl_act (False, NeverActive) = ptext (sLit "NOINLINE")
731 pp_inl_act (False, act) = ptext (sLit "NOINLINE") <> ppr act
732 pp_inl_act (True, AlwaysActive) = ptext (sLit "INLINE")
733 pp_inl_act (True, act) = ptext (sLit "INLINE") <> ppr act
735 pp_sat | Just ar <- mb_arity = parens (ptext (sLit "sat-args=") <> int ar)
737 pp_info | isFunLike info = empty
738 | otherwise = ppr info
740 isActive :: CompilerPhase -> Activation -> Bool
741 isActive _ NeverActive = False
742 isActive _ AlwaysActive = True
743 isActive p (ActiveAfter n) = p <= n
744 isActive p (ActiveBefore n) = p > n
746 isNeverActive, isAlwaysActive, isEarlyActive :: Activation -> Bool
747 isNeverActive NeverActive = True
748 isNeverActive _ = False
750 isAlwaysActive AlwaysActive = True
751 isAlwaysActive _ = False
753 isEarlyActive AlwaysActive = True
754 isEarlyActive (ActiveBefore {}) = True
755 isEarlyActive _ = False