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,
25 Fixity(..), FixityDirection(..),
26 defaultFixity, maxPrecedence,
27 arrowFixity, negateFixity, negatePrecedence,
30 IPName(..), ipNameName, mapIPName,
34 RecFlag(..), isRec, isNonRec, boolToRecFlag,
36 TopLevelFlag(..), isTopLevel, isNotTopLevel,
40 TupCon(..), tupParens, tupleParens,
42 OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc,
43 isDeadOcc, isLoopBreaker,
45 InsideLam, insideLam, notInsideLam,
46 OneBranch, oneBranch, notOneBranch,
50 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
53 Activation(..), isActive, isNeverActive, isAlwaysActive,
55 SuccessFlag(..), succeeded, failed, successIf
58 #include "HsVersions.h"
60 import FastString( FastString )
64 %************************************************************************
66 \subsection[Unused]{Unused}
68 %************************************************************************
70 Used as a placeholder in types.
76 unused = error "Unused is used!"
80 %************************************************************************
82 \subsection[Arity]{Arity}
84 %************************************************************************
91 %************************************************************************
93 \subsection[Version]{Module and identifier version numbers}
95 %************************************************************************
100 bogusVersion :: Version -- Shouldn't look at these
101 bogusVersion = error "bogusVersion"
103 bumpVersion :: Version -> Version
106 initialVersion :: Version
110 %************************************************************************
114 %************************************************************************
118 type DeprecTxt = FastString -- reason/explanation for deprecation
121 %************************************************************************
123 \subsection{Implicit parameter identity}
125 %************************************************************************
127 The @IPName@ type is here because it is used in TypeRep (i.e. very
128 early in the hierarchy), but also in HsSyn.
132 = Dupable name -- ?x: you can freely duplicate this implicit parameter
133 | Linear name -- %x: you must use the splitting function to duplicate it
134 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
135 -- (used in HscTypes.OrigIParamCache)
138 ipNameName :: IPName name -> name
139 ipNameName (Dupable n) = n
140 ipNameName (Linear n) = n
142 mapIPName :: (a->b) -> IPName a -> IPName b
143 mapIPName f (Dupable n) = Dupable (f n)
144 mapIPName f (Linear n) = Linear (f n)
146 instance Outputable name => Outputable (IPName name) where
147 ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
148 ppr (Linear n) = char '%' <> ppr n -- Splittable implicit parameters
152 %************************************************************************
154 \subsection[Fixity]{Fixity info}
156 %************************************************************************
159 ------------------------
160 data Fixity = Fixity Int FixityDirection
162 instance Outputable Fixity where
163 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
165 instance Eq Fixity where -- Used to determine if two fixities conflict
166 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
168 ------------------------
169 data FixityDirection = InfixL | InfixR | InfixN
172 instance Outputable FixityDirection where
173 ppr InfixL = ptext SLIT("infixl")
174 ppr InfixR = ptext SLIT("infixr")
175 ppr InfixN = ptext SLIT("infix")
177 ------------------------
178 maxPrecedence = (9::Int)
179 defaultFixity = Fixity maxPrecedence InfixL
181 negateFixity :: Fixity
182 negateFixity = Fixity negatePrecedence InfixL -- Precedence of unary negate is wired in as infixl 6!
184 arrowFixity :: Fixity -- Fixity of '->' in types
185 arrowFixity = Fixity 0 InfixR
187 negatePrecedence :: Int
196 @(compareFixity op1 op2)@ tells which way to arrange appication, or
197 whether there's an error.
200 compareFixity :: Fixity -> Fixity
201 -> (Bool, -- Error please
202 Bool) -- Associate to the right: a op1 (b op2 c)
203 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
204 = case prec1 `compare` prec2 of
207 EQ -> case (dir1, dir2) of
208 (InfixR, InfixR) -> right
209 (InfixL, InfixL) -> left
212 right = (False, True)
213 left = (False, False)
214 error_please = (True, False)
218 %************************************************************************
220 \subsection[NewType/DataType]{NewType/DataType flag}
222 %************************************************************************
226 = NewType -- "newtype Blah ..."
227 | DataType -- "data Blah ..."
228 deriving( Eq ) -- Needed because Demand derives Eq
230 instance Outputable NewOrData where
231 ppr NewType = ptext SLIT("newtype")
232 ppr DataType = ptext SLIT("data")
236 %************************************************************************
238 \subsection[Top-level/local]{Top-level/not-top level flag}
240 %************************************************************************
247 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
249 isNotTopLevel NotTopLevel = True
250 isNotTopLevel TopLevel = False
252 isTopLevel TopLevel = True
253 isTopLevel NotTopLevel = False
255 instance Outputable TopLevelFlag where
256 ppr TopLevel = ptext SLIT("<TopLevel>")
257 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
261 %************************************************************************
263 \subsection[Top-level/local]{Top-level/not-top level flag}
265 %************************************************************************
273 isBoxed :: Boxity -> Bool
275 isBoxed Unboxed = False
279 %************************************************************************
281 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
283 %************************************************************************
286 data RecFlag = Recursive
290 isRec :: RecFlag -> Bool
291 isRec Recursive = True
292 isRec NonRecursive = False
294 isNonRec :: RecFlag -> Bool
295 isNonRec Recursive = False
296 isNonRec NonRecursive = True
298 boolToRecFlag :: Bool -> RecFlag
299 boolToRecFlag True = Recursive
300 boolToRecFlag False = NonRecursive
302 instance Outputable RecFlag where
303 ppr Recursive = ptext SLIT("Recursive")
304 ppr NonRecursive = ptext SLIT("NonRecursive")
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 tupParens :: TupCon -> SDoc -> SDoc
320 tupParens (TupCon b _) p = tupleParens b p
322 tupleParens :: Boxity -> SDoc -> SDoc
323 tupleParens Boxed p = parens p
324 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
327 %************************************************************************
329 \subsection[Generic]{Generic flag}
331 %************************************************************************
333 This is the "Embedding-Projection pair" datatype, it contains
334 two pieces of code (normally either RenamedExpr's or Id's)
335 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
336 represents functions of type
345 T and Tring are arbitrary, but typically T is the 'main' type while
346 Tring is the 'representation' type. (This just helps us remember
347 whether to use 'from' or 'to'.
350 data EP a = EP { fromEP :: a, -- :: T -> Tring
351 toEP :: a } -- :: Tring -> T
354 Embedding-projection pairs are used in several places:
356 First of all, each type constructor has an EP associated with it, the
357 code in EP converts (datatype T) from T to Tring and back again.
359 Secondly, when we are filling in Generic methods (in the typechecker,
360 tcMethodBinds), we are constructing bimaps by induction on the structure
361 of the type of the method signature.
364 %************************************************************************
366 \subsection{Occurrence information}
368 %************************************************************************
370 This data type is used exclusively by the simplifier, but it appears in a
371 SubstResult, which is currently defined in VarEnv, which is pretty near
372 the base of the module hierarchy. So it seemed simpler to put the
373 defn of OccInfo here, safely at the bottom
379 | IAmDead -- Marks unused variables. Sometimes useful for
380 -- lambda and case-bound variables.
386 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
387 -- in a group of recursive definitions
389 seqOccInfo :: OccInfo -> ()
390 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
393 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
394 -- Substituting a redex for this occurrence is
395 -- dangerous because it might duplicate work.
399 type OneBranch = Bool -- True <=> Occurs in only one case branch
400 -- so no code-duplication issue to worry about
404 isLoopBreaker :: OccInfo -> Bool
405 isLoopBreaker IAmALoopBreaker = True
406 isLoopBreaker other = False
408 isDeadOcc :: OccInfo -> Bool
409 isDeadOcc IAmDead = True
410 isDeadOcc other = False
412 isOneOcc (OneOcc _ _) = True
413 isOneOcc other = False
415 isFragileOcc :: OccInfo -> Bool
416 isFragileOcc (OneOcc _ _) = True
417 isFragileOcc other = False
421 instance Outputable OccInfo where
422 -- only used for debugging; never parsed. KSW 1999-07
423 ppr NoOccInfo = empty
424 ppr IAmALoopBreaker = ptext SLIT("_Kx")
425 ppr IAmDead = ptext SLIT("_Kd")
426 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
427 | one_branch = ptext SLIT("_Ks")
428 | otherwise = ptext SLIT("_Ks*")
430 instance Show OccInfo where
431 showsPrec p occ = showsPrecSDoc p (ppr occ)
434 %************************************************************************
436 \subsection{Strictness indication}
438 %************************************************************************
440 The strictness annotations on types in data type declarations
441 e.g. data T = MkT !Int !(Bool,Bool)
444 data StrictnessMark -- Used in interface decls only
450 isMarkedUnboxed MarkedUnboxed = True
451 isMarkedUnboxed other = False
453 isMarkedStrict NotMarkedStrict = False
454 isMarkedStrict other = True -- All others are strict
456 instance Outputable StrictnessMark where
457 ppr MarkedStrict = ptext SLIT("!")
458 ppr MarkedUnboxed = ptext SLIT("!!")
459 ppr NotMarkedStrict = ptext SLIT("_")
463 %************************************************************************
465 \subsection{Success flag}
467 %************************************************************************
470 data SuccessFlag = Succeeded | Failed
472 successIf :: Bool -> SuccessFlag
473 successIf True = Succeeded
474 successIf False = Failed
476 succeeded, failed :: SuccessFlag -> Bool
477 succeeded Succeeded = True
478 succeeded Failed = False
480 failed Succeeded = False
485 %************************************************************************
487 \subsection{Activation}
489 %************************************************************************
491 When a rule or inlining is active
494 type CompilerPhase = Int -- Compilation phase
495 -- Phases decrease towards zero
496 -- Zero is the last phase
498 data Activation = NeverActive
500 | ActiveBefore CompilerPhase -- Active only *before* this phase
501 | ActiveAfter CompilerPhase -- Active in this phase and later
502 deriving( Eq ) -- Eq used in comparing rules in HsDecls
504 instance Outputable Activation where
505 ppr AlwaysActive = empty -- The default
506 ppr (ActiveBefore n) = brackets (char '~' <> int n)
507 ppr (ActiveAfter n) = brackets (int n)
508 ppr NeverActive = ptext SLIT("NEVER")
510 isActive :: CompilerPhase -> Activation -> Bool
511 isActive p NeverActive = False
512 isActive p AlwaysActive = True
513 isActive p (ActiveAfter n) = p <= n
514 isActive p (ActiveBefore n) = p > n
516 isNeverActive, isAlwaysActive :: Activation -> Bool
517 isNeverActive NeverActive = True
518 isNeverActive act = False
520 isAlwaysActive AlwaysActive = True
521 isAlwaysActive other = False