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
300 instance Outputable OverlapFlag where
301 ppr NoOverlap = empty
302 ppr OverlapOk = ptext SLIT("[overlap ok]")
303 ppr Incoherent = ptext SLIT("[incoherent]")
307 %************************************************************************
311 %************************************************************************
314 data TupCon = TupCon Boxity Arity
316 instance Eq TupCon where
317 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
319 tupleParens :: Boxity -> SDoc -> SDoc
320 tupleParens Boxed p = parens p
321 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
324 %************************************************************************
326 \subsection[Generic]{Generic flag}
328 %************************************************************************
330 This is the "Embedding-Projection pair" datatype, it contains
331 two pieces of code (normally either RenamedExpr's or Id's)
332 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
333 represents functions of type
342 T and Tring are arbitrary, but typically T is the 'main' type while
343 Tring is the 'representation' type. (This just helps us remember
344 whether to use 'from' or 'to'.
347 data EP a = EP { fromEP :: a, -- :: T -> Tring
348 toEP :: a } -- :: Tring -> T
351 Embedding-projection pairs are used in several places:
353 First of all, each type constructor has an EP associated with it, the
354 code in EP converts (datatype T) from T to Tring and back again.
356 Secondly, when we are filling in Generic methods (in the typechecker,
357 tcMethodBinds), we are constructing bimaps by induction on the structure
358 of the type of the method signature.
361 %************************************************************************
363 \subsection{Occurrence information}
365 %************************************************************************
367 This data type is used exclusively by the simplifier, but it appears in a
368 SubstResult, which is currently defined in VarEnv, which is pretty near
369 the base of the module hierarchy. So it seemed simpler to put the
370 defn of OccInfo here, safely at the bottom
376 | IAmDead -- Marks unused variables. Sometimes useful for
377 -- lambda and case-bound variables.
383 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
384 -- in a group of recursive definitions
386 isNoOcc :: OccInfo -> Bool
387 isNoOcc NoOccInfo = True
388 isNoOcc other = False
390 seqOccInfo :: OccInfo -> ()
391 seqOccInfo occ = occ `seq` ()
394 type InterestingCxt = Bool -- True <=> Function: is applied
395 -- Data value: scrutinised by a case with
396 -- at least one non-DEFAULT branch
399 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
400 -- Substituting a redex for this occurrence is
401 -- dangerous because it might duplicate work.
406 type OneBranch = Bool -- True <=> Occurs in only one case branch
407 -- so no code-duplication issue to worry about
411 isLoopBreaker :: OccInfo -> Bool
412 isLoopBreaker IAmALoopBreaker = True
413 isLoopBreaker other = False
415 isDeadOcc :: OccInfo -> Bool
416 isDeadOcc IAmDead = True
417 isDeadOcc other = False
419 isOneOcc (OneOcc _ _ _) = True
420 isOneOcc other = False
422 isFragileOcc :: OccInfo -> Bool
423 isFragileOcc (OneOcc _ _ _) = True
424 isFragileOcc other = False
428 instance Outputable OccInfo where
429 -- only used for debugging; never parsed. KSW 1999-07
430 ppr NoOccInfo = empty
431 ppr IAmALoopBreaker = ptext SLIT("LoopBreaker")
432 ppr IAmDead = ptext SLIT("Dead")
433 ppr (OneOcc inside_lam one_branch int_cxt)
434 = ptext SLIT("Once") <> pp_lam <> pp_br <> pp_args
436 pp_lam | inside_lam = char 'L'
438 pp_br | one_branch = empty
439 | otherwise = char '*'
440 pp_args | int_cxt = char '!'
443 instance Show OccInfo where
444 showsPrec p occ = showsPrecSDoc p (ppr occ)
447 %************************************************************************
449 \subsection{Strictness indication}
451 %************************************************************************
453 The strictness annotations on types in data type declarations
454 e.g. data T = MkT !Int !(Bool,Bool)
457 data StrictnessMark -- Used in interface decls only
463 isMarkedUnboxed MarkedUnboxed = True
464 isMarkedUnboxed other = False
466 isMarkedStrict NotMarkedStrict = False
467 isMarkedStrict other = True -- All others are strict
469 instance Outputable StrictnessMark where
470 ppr MarkedStrict = ptext SLIT("!")
471 ppr MarkedUnboxed = ptext SLIT("!!")
472 ppr NotMarkedStrict = ptext SLIT("_")
476 %************************************************************************
478 \subsection{Success flag}
480 %************************************************************************
483 data SuccessFlag = Succeeded | Failed
485 successIf :: Bool -> SuccessFlag
486 successIf True = Succeeded
487 successIf False = Failed
489 succeeded, failed :: SuccessFlag -> Bool
490 succeeded Succeeded = True
491 succeeded Failed = False
493 failed Succeeded = False
498 %************************************************************************
500 \subsection{Activation}
502 %************************************************************************
504 When a rule or inlining is active
507 type CompilerPhase = Int -- Compilation phase
508 -- Phases decrease towards zero
509 -- Zero is the last phase
511 data Activation = NeverActive
513 | ActiveBefore CompilerPhase -- Active only *before* this phase
514 | ActiveAfter CompilerPhase -- Active in this phase and later
515 deriving( Eq ) -- Eq used in comparing rules in HsDecls
519 Activation -- Says during which phases inlining is allowed
520 Bool -- True <=> make the RHS look small, so that when inlining
521 -- is enabled, it will definitely actually happen
524 defaultInlineSpec = Inline AlwaysActive False -- Inlining is OK, but not forced
525 alwaysInlineSpec = Inline AlwaysActive True -- INLINE always
526 neverInlineSpec = Inline NeverActive False -- NOINLINE
528 instance Outputable Activation where
529 ppr AlwaysActive = empty -- The default
530 ppr (ActiveBefore n) = brackets (char '~' <> int n)
531 ppr (ActiveAfter n) = brackets (int n)
532 ppr NeverActive = ptext SLIT("NEVER")
534 instance Outputable InlineSpec where
535 ppr (Inline act True) = ptext SLIT("INLINE") <> ppr act
536 ppr (Inline act False) = ptext SLIT("NOINLINE") <> ppr act
538 isActive :: CompilerPhase -> Activation -> Bool
539 isActive p NeverActive = False
540 isActive p AlwaysActive = True
541 isActive p (ActiveAfter n) = p <= n
542 isActive p (ActiveBefore n) = p > n
544 isNeverActive, isAlwaysActive :: Activation -> Bool
545 isNeverActive NeverActive = True
546 isNeverActive act = False
548 isAlwaysActive AlwaysActive = True
549 isAlwaysActive other = False