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,
23 Fixity(..), FixityDirection(..),
24 defaultFixity, maxPrecedence,
25 negateFixity, funTyFixity,
28 IPName(..), ipNameName, mapIPName,
30 RecFlag(..), isRec, isNonRec, boolToRecFlag,
32 TopLevelFlag(..), isTopLevel, isNotTopLevel,
38 TupCon(..), tupleParens,
40 OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc,
41 isDeadOcc, isLoopBreaker, isNoOcc,
43 InsideLam, insideLam, notInsideLam,
44 OneBranch, oneBranch, notOneBranch,
49 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
52 Activation(..), isActive, isNeverActive, isAlwaysActive,
53 InlineSpec(..), defaultInlineSpec, alwaysInlineSpec, neverInlineSpec,
55 SuccessFlag(..), succeeded, failed, successIf
58 #include "HsVersions.h"
60 import FastString( FastString )
64 %************************************************************************
66 \subsection[Arity]{Arity}
68 %************************************************************************
75 %************************************************************************
77 \subsection[Version]{Module and identifier version numbers}
79 %************************************************************************
84 bumpVersion :: Version -> Version
87 initialVersion :: Version
91 %************************************************************************
95 %************************************************************************
99 type DeprecTxt = FastString -- reason/explanation for deprecation
102 %************************************************************************
104 \subsection{Implicit parameter identity}
106 %************************************************************************
108 The @IPName@ type is here because it is used in TypeRep (i.e. very
109 early in the hierarchy), but also in HsSyn.
113 = Dupable name -- ?x: you can freely duplicate this implicit parameter
114 | Linear name -- %x: you must use the splitting function to duplicate it
115 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
116 -- (used in HscTypes.OrigIParamCache)
119 ipNameName :: IPName name -> name
120 ipNameName (Dupable n) = n
121 ipNameName (Linear n) = n
123 mapIPName :: (a->b) -> IPName a -> IPName b
124 mapIPName f (Dupable n) = Dupable (f n)
125 mapIPName f (Linear n) = Linear (f n)
127 instance Outputable name => Outputable (IPName name) where
128 ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
129 ppr (Linear n) = char '%' <> ppr n -- Splittable implicit parameters
133 %************************************************************************
135 \subsection[Fixity]{Fixity info}
137 %************************************************************************
140 ------------------------
141 data Fixity = Fixity Int FixityDirection
143 instance Outputable Fixity where
144 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
146 instance Eq Fixity where -- Used to determine if two fixities conflict
147 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
149 ------------------------
150 data FixityDirection = InfixL | InfixR | InfixN
153 instance Outputable FixityDirection where
154 ppr InfixL = ptext SLIT("infixl")
155 ppr InfixR = ptext SLIT("infixr")
156 ppr InfixN = ptext SLIT("infix")
158 ------------------------
159 maxPrecedence = (9::Int)
160 defaultFixity = Fixity maxPrecedence InfixL
162 negateFixity, funTyFixity :: Fixity
164 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
165 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
173 @(compareFixity op1 op2)@ tells which way to arrange appication, or
174 whether there's an error.
177 compareFixity :: Fixity -> Fixity
178 -> (Bool, -- Error please
179 Bool) -- Associate to the right: a op1 (b op2 c)
180 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
181 = case prec1 `compare` prec2 of
184 EQ -> case (dir1, dir2) of
185 (InfixR, InfixR) -> right
186 (InfixL, InfixL) -> left
189 right = (False, True)
190 left = (False, False)
191 error_please = (True, False)
195 %************************************************************************
197 \subsection[Top-level/local]{Top-level/not-top level flag}
199 %************************************************************************
206 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
208 isNotTopLevel NotTopLevel = True
209 isNotTopLevel TopLevel = False
211 isTopLevel TopLevel = True
212 isTopLevel NotTopLevel = False
214 instance Outputable TopLevelFlag where
215 ppr TopLevel = ptext SLIT("<TopLevel>")
216 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
220 %************************************************************************
222 Top-level/not-top level flag
224 %************************************************************************
232 isBoxed :: Boxity -> Bool
234 isBoxed Unboxed = False
238 %************************************************************************
240 Recursive/Non-Recursive flag
242 %************************************************************************
245 data RecFlag = Recursive
249 isRec :: RecFlag -> Bool
250 isRec Recursive = True
251 isRec NonRecursive = False
253 isNonRec :: RecFlag -> Bool
254 isNonRec Recursive = False
255 isNonRec NonRecursive = True
257 boolToRecFlag :: Bool -> RecFlag
258 boolToRecFlag True = Recursive
259 boolToRecFlag False = NonRecursive
261 instance Outputable RecFlag where
262 ppr Recursive = ptext SLIT("Recursive")
263 ppr NonRecursive = ptext SLIT("NonRecursive")
266 %************************************************************************
268 Instance overlap flag
270 %************************************************************************
274 = NoOverlap -- This instance must not overlap another
276 | OverlapOk -- Silently ignore this instance if you find a
277 -- more specific one that matches the constraint
278 -- you are trying to resolve
280 -- Example: constraint (Foo [Int])
281 -- instances (Foo [Int])
282 -- (Foo [a]) OverlapOk
283 -- Since the second instance has the OverlapOk flag,
284 -- the first instance will be chosen (otherwise
285 -- its ambiguous which to choose)
287 | Incoherent -- Like OverlapOk, but also ignore this instance
288 -- if it doesn't match the constraint you are
289 -- trying to resolve, but could match if the type variables
290 -- in the constraint were instantiated
292 -- Example: constraint (Foo [b])
293 -- instances (Foo [Int]) Incoherent
295 -- Without the Incoherent flag, we'd complain that
296 -- instantiating 'b' would change which instance
299 instance Outputable OverlapFlag where
300 ppr NoOverlap = empty
301 ppr OverlapOk = ptext SLIT("[overlap ok]")
302 ppr Incoherent = ptext SLIT("[incoherent]")
306 %************************************************************************
310 %************************************************************************
313 data TupCon = TupCon Boxity Arity
315 instance Eq TupCon where
316 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
318 tupleParens :: Boxity -> SDoc -> SDoc
319 tupleParens Boxed p = parens p
320 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
323 %************************************************************************
325 \subsection[Generic]{Generic flag}
327 %************************************************************************
329 This is the "Embedding-Projection pair" datatype, it contains
330 two pieces of code (normally either RenamedExpr's or Id's)
331 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
332 represents functions of type
341 T and Tring are arbitrary, but typically T is the 'main' type while
342 Tring is the 'representation' type. (This just helps us remember
343 whether to use 'from' or 'to'.
346 data EP a = EP { fromEP :: a, -- :: T -> Tring
347 toEP :: a } -- :: Tring -> T
350 Embedding-projection pairs are used in several places:
352 First of all, each type constructor has an EP associated with it, the
353 code in EP converts (datatype T) from T to Tring and back again.
355 Secondly, when we are filling in Generic methods (in the typechecker,
356 tcMethodBinds), we are constructing bimaps by induction on the structure
357 of the type of the method signature.
360 %************************************************************************
362 \subsection{Occurrence information}
364 %************************************************************************
366 This data type is used exclusively by the simplifier, but it appears in a
367 SubstResult, which is currently defined in VarEnv, which is pretty near
368 the base of the module hierarchy. So it seemed simpler to put the
369 defn of OccInfo here, safely at the bottom
375 | IAmDead -- Marks unused variables. Sometimes useful for
376 -- lambda and case-bound variables.
382 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
383 -- in a group of recursive definitions
385 isNoOcc :: OccInfo -> Bool
386 isNoOcc NoOccInfo = True
387 isNoOcc other = False
389 seqOccInfo :: OccInfo -> ()
390 seqOccInfo occ = occ `seq` ()
393 type InterestingCxt = Bool -- True <=> Function: is applied
394 -- Data value: scrutinised by a case with
395 -- at least one non-DEFAULT branch
398 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
399 -- Substituting a redex for this occurrence is
400 -- dangerous because it might duplicate work.
405 type OneBranch = Bool -- True <=> Occurs in only one case branch
406 -- so no code-duplication issue to worry about
410 isLoopBreaker :: OccInfo -> Bool
411 isLoopBreaker IAmALoopBreaker = True
412 isLoopBreaker other = False
414 isDeadOcc :: OccInfo -> Bool
415 isDeadOcc IAmDead = True
416 isDeadOcc other = False
418 isOneOcc (OneOcc _ _ _) = True
419 isOneOcc other = False
421 isFragileOcc :: OccInfo -> Bool
422 isFragileOcc (OneOcc _ _ _) = True
423 isFragileOcc other = False
427 instance Outputable OccInfo where
428 -- only used for debugging; never parsed. KSW 1999-07
429 ppr NoOccInfo = empty
430 ppr IAmALoopBreaker = ptext SLIT("LoopBreaker")
431 ppr IAmDead = ptext SLIT("Dead")
432 ppr (OneOcc inside_lam one_branch int_cxt)
433 = ptext SLIT("Once") <> pp_lam <> pp_br <> pp_args
435 pp_lam | inside_lam = char 'L'
437 pp_br | one_branch = empty
438 | otherwise = char '*'
439 pp_args | int_cxt = char '!'
442 instance Show OccInfo where
443 showsPrec p occ = showsPrecSDoc p (ppr occ)
446 %************************************************************************
448 \subsection{Strictness indication}
450 %************************************************************************
452 The strictness annotations on types in data type declarations
453 e.g. data T = MkT !Int !(Bool,Bool)
456 data StrictnessMark -- Used in interface decls only
462 isMarkedUnboxed MarkedUnboxed = True
463 isMarkedUnboxed other = False
465 isMarkedStrict NotMarkedStrict = False
466 isMarkedStrict other = True -- All others are strict
468 instance Outputable StrictnessMark where
469 ppr MarkedStrict = ptext SLIT("!")
470 ppr MarkedUnboxed = ptext SLIT("!!")
471 ppr NotMarkedStrict = ptext SLIT("_")
475 %************************************************************************
477 \subsection{Success flag}
479 %************************************************************************
482 data SuccessFlag = Succeeded | Failed
484 successIf :: Bool -> SuccessFlag
485 successIf True = Succeeded
486 successIf False = Failed
488 succeeded, failed :: SuccessFlag -> Bool
489 succeeded Succeeded = True
490 succeeded Failed = False
492 failed Succeeded = False
497 %************************************************************************
499 \subsection{Activation}
501 %************************************************************************
503 When a rule or inlining is active
506 type CompilerPhase = Int -- Compilation phase
507 -- Phases decrease towards zero
508 -- Zero is the last phase
510 data Activation = NeverActive
512 | ActiveBefore CompilerPhase -- Active only *before* this phase
513 | ActiveAfter CompilerPhase -- Active in this phase and later
514 deriving( Eq ) -- Eq used in comparing rules in HsDecls
518 Activation -- Says during which phases inlining is allowed
519 Bool -- True <=> make the RHS look small, so that when inlining
520 -- is enabled, it will definitely actually happen
523 defaultInlineSpec = Inline AlwaysActive False -- Inlining is OK, but not forced
524 alwaysInlineSpec = Inline AlwaysActive True -- INLINE always
525 neverInlineSpec = Inline NeverActive False -- NOINLINE
527 instance Outputable Activation where
528 ppr AlwaysActive = empty -- The default
529 ppr (ActiveBefore n) = brackets (char '~' <> int n)
530 ppr (ActiveAfter n) = brackets (int n)
531 ppr NeverActive = ptext SLIT("NEVER")
533 instance Outputable InlineSpec where
534 ppr (Inline act True) = ptext SLIT("INLINE") <> ppr act
535 ppr (Inline act False) = ptext SLIT("NOINLINE") <> ppr act
537 isActive :: CompilerPhase -> Activation -> Bool
538 isActive p NeverActive = False
539 isActive p AlwaysActive = True
540 isActive p (ActiveAfter n) = p <= n
541 isActive p (ActiveBefore n) = p > n
543 isNeverActive, isAlwaysActive :: Activation -> Bool
544 isNeverActive NeverActive = True
545 isNeverActive act = False
547 isAlwaysActive AlwaysActive = True
548 isAlwaysActive other = False