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,
28 IPName(..), ipNameName, mapIPName,
32 RecFlag(..), isRec, isNonRec, boolToRecFlag,
34 TopLevelFlag(..), isTopLevel, isNotTopLevel,
38 TupCon(..), tupleParens,
40 OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc,
41 isDeadOcc, isLoopBreaker,
43 InsideLam, insideLam, notInsideLam,
44 OneBranch, oneBranch, notOneBranch,
48 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
51 Activation(..), isActive, isNeverActive, isAlwaysActive,
53 SuccessFlag(..), succeeded, failed, successIf
56 #include "HsVersions.h"
58 import FastString( FastString )
62 %************************************************************************
64 \subsection[Arity]{Arity}
66 %************************************************************************
73 %************************************************************************
75 \subsection[Version]{Module and identifier version numbers}
77 %************************************************************************
82 bumpVersion :: Version -> Version
85 initialVersion :: Version
89 %************************************************************************
93 %************************************************************************
97 type DeprecTxt = FastString -- reason/explanation for deprecation
100 %************************************************************************
102 \subsection{Implicit parameter identity}
104 %************************************************************************
106 The @IPName@ type is here because it is used in TypeRep (i.e. very
107 early in the hierarchy), but also in HsSyn.
111 = Dupable name -- ?x: you can freely duplicate this implicit parameter
112 | Linear name -- %x: you must use the splitting function to duplicate it
113 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
114 -- (used in HscTypes.OrigIParamCache)
117 ipNameName :: IPName name -> name
118 ipNameName (Dupable n) = n
119 ipNameName (Linear n) = n
121 mapIPName :: (a->b) -> IPName a -> IPName b
122 mapIPName f (Dupable n) = Dupable (f n)
123 mapIPName f (Linear n) = Linear (f n)
125 instance Outputable name => Outputable (IPName name) where
126 ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
127 ppr (Linear n) = char '%' <> ppr n -- Splittable implicit parameters
131 %************************************************************************
133 \subsection[Fixity]{Fixity info}
135 %************************************************************************
138 ------------------------
139 data Fixity = Fixity Int FixityDirection
141 instance Outputable Fixity where
142 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
144 instance Eq Fixity where -- Used to determine if two fixities conflict
145 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
147 ------------------------
148 data FixityDirection = InfixL | InfixR | InfixN
151 instance Outputable FixityDirection where
152 ppr InfixL = ptext SLIT("infixl")
153 ppr InfixR = ptext SLIT("infixr")
154 ppr InfixN = ptext SLIT("infix")
156 ------------------------
157 maxPrecedence = (9::Int)
158 defaultFixity = Fixity maxPrecedence InfixL
160 negateFixity :: Fixity
161 negateFixity = Fixity negatePrecedence InfixL -- Precedence of unary negate is wired in as infixl 6!
163 negatePrecedence :: Int
172 @(compareFixity op1 op2)@ tells which way to arrange appication, or
173 whether there's an error.
176 compareFixity :: Fixity -> Fixity
177 -> (Bool, -- Error please
178 Bool) -- Associate to the right: a op1 (b op2 c)
179 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
180 = case prec1 `compare` prec2 of
183 EQ -> case (dir1, dir2) of
184 (InfixR, InfixR) -> right
185 (InfixL, InfixL) -> left
188 right = (False, True)
189 left = (False, False)
190 error_please = (True, False)
194 %************************************************************************
196 \subsection[NewType/DataType]{NewType/DataType flag}
198 %************************************************************************
202 = NewType -- "newtype Blah ..."
203 | DataType -- "data Blah ..."
204 deriving( Eq ) -- Needed because Demand derives Eq
206 instance Outputable NewOrData where
207 ppr NewType = ptext SLIT("newtype")
208 ppr DataType = ptext SLIT("data")
212 %************************************************************************
214 \subsection[Top-level/local]{Top-level/not-top level flag}
216 %************************************************************************
223 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
225 isNotTopLevel NotTopLevel = True
226 isNotTopLevel TopLevel = False
228 isTopLevel TopLevel = True
229 isTopLevel NotTopLevel = False
231 instance Outputable TopLevelFlag where
232 ppr TopLevel = ptext SLIT("<TopLevel>")
233 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
237 %************************************************************************
239 \subsection[Top-level/local]{Top-level/not-top level flag}
241 %************************************************************************
249 isBoxed :: Boxity -> Bool
251 isBoxed Unboxed = False
255 %************************************************************************
257 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
259 %************************************************************************
262 data RecFlag = Recursive
266 isRec :: RecFlag -> Bool
267 isRec Recursive = True
268 isRec NonRecursive = False
270 isNonRec :: RecFlag -> Bool
271 isNonRec Recursive = False
272 isNonRec NonRecursive = True
274 boolToRecFlag :: Bool -> RecFlag
275 boolToRecFlag True = Recursive
276 boolToRecFlag False = NonRecursive
278 instance Outputable RecFlag where
279 ppr Recursive = ptext SLIT("Recursive")
280 ppr NonRecursive = ptext SLIT("NonRecursive")
283 %************************************************************************
287 %************************************************************************
290 data TupCon = TupCon Boxity Arity
292 instance Eq TupCon where
293 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
295 tupleParens :: Boxity -> SDoc -> SDoc
296 tupleParens Boxed p = parens p
297 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
300 %************************************************************************
302 \subsection[Generic]{Generic flag}
304 %************************************************************************
306 This is the "Embedding-Projection pair" datatype, it contains
307 two pieces of code (normally either RenamedExpr's or Id's)
308 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
309 represents functions of type
318 T and Tring are arbitrary, but typically T is the 'main' type while
319 Tring is the 'representation' type. (This just helps us remember
320 whether to use 'from' or 'to'.
323 data EP a = EP { fromEP :: a, -- :: T -> Tring
324 toEP :: a } -- :: Tring -> T
327 Embedding-projection pairs are used in several places:
329 First of all, each type constructor has an EP associated with it, the
330 code in EP converts (datatype T) from T to Tring and back again.
332 Secondly, when we are filling in Generic methods (in the typechecker,
333 tcMethodBinds), we are constructing bimaps by induction on the structure
334 of the type of the method signature.
337 %************************************************************************
339 \subsection{Occurrence information}
341 %************************************************************************
343 This data type is used exclusively by the simplifier, but it appears in a
344 SubstResult, which is currently defined in VarEnv, which is pretty near
345 the base of the module hierarchy. So it seemed simpler to put the
346 defn of OccInfo here, safely at the bottom
352 | IAmDead -- Marks unused variables. Sometimes useful for
353 -- lambda and case-bound variables.
359 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
360 -- in a group of recursive definitions
362 seqOccInfo :: OccInfo -> ()
363 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
366 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
367 -- Substituting a redex for this occurrence is
368 -- dangerous because it might duplicate work.
372 type OneBranch = Bool -- True <=> Occurs in only one case branch
373 -- so no code-duplication issue to worry about
377 isLoopBreaker :: OccInfo -> Bool
378 isLoopBreaker IAmALoopBreaker = True
379 isLoopBreaker other = False
381 isDeadOcc :: OccInfo -> Bool
382 isDeadOcc IAmDead = True
383 isDeadOcc other = False
385 isOneOcc (OneOcc _ _) = True
386 isOneOcc other = False
388 isFragileOcc :: OccInfo -> Bool
389 isFragileOcc (OneOcc _ _) = True
390 isFragileOcc other = False
394 instance Outputable OccInfo where
395 -- only used for debugging; never parsed. KSW 1999-07
396 ppr NoOccInfo = empty
397 ppr IAmALoopBreaker = ptext SLIT("_Kx")
398 ppr IAmDead = ptext SLIT("_Kd")
399 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
400 | one_branch = ptext SLIT("_Ks")
401 | otherwise = ptext SLIT("_Ks*")
403 instance Show OccInfo where
404 showsPrec p occ = showsPrecSDoc p (ppr occ)
407 %************************************************************************
409 \subsection{Strictness indication}
411 %************************************************************************
413 The strictness annotations on types in data type declarations
414 e.g. data T = MkT !Int !(Bool,Bool)
417 data StrictnessMark -- Used in interface decls only
423 isMarkedUnboxed MarkedUnboxed = True
424 isMarkedUnboxed other = False
426 isMarkedStrict NotMarkedStrict = False
427 isMarkedStrict other = True -- All others are strict
429 instance Outputable StrictnessMark where
430 ppr MarkedStrict = ptext SLIT("!")
431 ppr MarkedUnboxed = ptext SLIT("!!")
432 ppr NotMarkedStrict = ptext SLIT("_")
436 %************************************************************************
438 \subsection{Success flag}
440 %************************************************************************
443 data SuccessFlag = Succeeded | Failed
445 successIf :: Bool -> SuccessFlag
446 successIf True = Succeeded
447 successIf False = Failed
449 succeeded, failed :: SuccessFlag -> Bool
450 succeeded Succeeded = True
451 succeeded Failed = False
453 failed Succeeded = False
458 %************************************************************************
460 \subsection{Activation}
462 %************************************************************************
464 When a rule or inlining is active
467 type CompilerPhase = Int -- Compilation phase
468 -- Phases decrease towards zero
469 -- Zero is the last phase
471 data Activation = NeverActive
473 | ActiveBefore CompilerPhase -- Active only *before* this phase
474 | ActiveAfter CompilerPhase -- Active in this phase and later
475 deriving( Eq ) -- Eq used in comparing rules in HsDecls
477 instance Outputable Activation where
478 ppr AlwaysActive = empty -- The default
479 ppr (ActiveBefore n) = brackets (char '~' <> int n)
480 ppr (ActiveAfter n) = brackets (int n)
481 ppr NeverActive = ptext SLIT("NEVER")
483 isActive :: CompilerPhase -> Activation -> Bool
484 isActive p NeverActive = False
485 isActive p AlwaysActive = True
486 isActive p (ActiveAfter n) = p <= n
487 isActive p (ActiveBefore n) = p > n
489 isNeverActive, isAlwaysActive :: Activation -> Bool
490 isNeverActive NeverActive = True
491 isNeverActive act = False
493 isAlwaysActive AlwaysActive = True
494 isAlwaysActive other = False