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,
36 TupCon(..), tupleParens,
38 OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc,
39 isDeadOcc, isLoopBreaker,
41 InsideLam, insideLam, notInsideLam,
42 OneBranch, oneBranch, notOneBranch,
46 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
49 Activation(..), isActive, isNeverActive, isAlwaysActive,
51 SuccessFlag(..), succeeded, failed, successIf
54 #include "HsVersions.h"
56 import FastString( FastString )
60 %************************************************************************
62 \subsection[Arity]{Arity}
64 %************************************************************************
71 %************************************************************************
73 \subsection[Version]{Module and identifier version numbers}
75 %************************************************************************
80 bumpVersion :: Version -> Version
83 initialVersion :: Version
87 %************************************************************************
91 %************************************************************************
95 type DeprecTxt = FastString -- reason/explanation for deprecation
98 %************************************************************************
100 \subsection{Implicit parameter identity}
102 %************************************************************************
104 The @IPName@ type is here because it is used in TypeRep (i.e. very
105 early in the hierarchy), but also in HsSyn.
109 = Dupable name -- ?x: you can freely duplicate this implicit parameter
110 | Linear name -- %x: you must use the splitting function to duplicate it
111 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
112 -- (used in HscTypes.OrigIParamCache)
115 ipNameName :: IPName name -> name
116 ipNameName (Dupable n) = n
117 ipNameName (Linear n) = n
119 mapIPName :: (a->b) -> IPName a -> IPName b
120 mapIPName f (Dupable n) = Dupable (f n)
121 mapIPName f (Linear n) = Linear (f n)
123 instance Outputable name => Outputable (IPName name) where
124 ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
125 ppr (Linear n) = char '%' <> ppr n -- Splittable implicit parameters
129 %************************************************************************
131 \subsection[Fixity]{Fixity info}
133 %************************************************************************
136 ------------------------
137 data Fixity = Fixity Int FixityDirection
139 instance Outputable Fixity where
140 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
142 instance Eq Fixity where -- Used to determine if two fixities conflict
143 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
145 ------------------------
146 data FixityDirection = InfixL | InfixR | InfixN
149 instance Outputable FixityDirection where
150 ppr InfixL = ptext SLIT("infixl")
151 ppr InfixR = ptext SLIT("infixr")
152 ppr InfixN = ptext SLIT("infix")
154 ------------------------
155 maxPrecedence = (9::Int)
156 defaultFixity = Fixity maxPrecedence InfixL
158 negateFixity, funTyFixity :: Fixity
160 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
161 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
169 @(compareFixity op1 op2)@ tells which way to arrange appication, or
170 whether there's an error.
173 compareFixity :: Fixity -> Fixity
174 -> (Bool, -- Error please
175 Bool) -- Associate to the right: a op1 (b op2 c)
176 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
177 = case prec1 `compare` prec2 of
180 EQ -> case (dir1, dir2) of
181 (InfixR, InfixR) -> right
182 (InfixL, InfixL) -> left
185 right = (False, True)
186 left = (False, False)
187 error_please = (True, False)
191 %************************************************************************
193 \subsection[Top-level/local]{Top-level/not-top level flag}
195 %************************************************************************
202 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
204 isNotTopLevel NotTopLevel = True
205 isNotTopLevel TopLevel = False
207 isTopLevel TopLevel = True
208 isTopLevel NotTopLevel = False
210 instance Outputable TopLevelFlag where
211 ppr TopLevel = ptext SLIT("<TopLevel>")
212 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
216 %************************************************************************
218 \subsection[Top-level/local]{Top-level/not-top level flag}
220 %************************************************************************
228 isBoxed :: Boxity -> Bool
230 isBoxed Unboxed = False
234 %************************************************************************
236 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
238 %************************************************************************
241 data RecFlag = Recursive
245 isRec :: RecFlag -> Bool
246 isRec Recursive = True
247 isRec NonRecursive = False
249 isNonRec :: RecFlag -> Bool
250 isNonRec Recursive = False
251 isNonRec NonRecursive = True
253 boolToRecFlag :: Bool -> RecFlag
254 boolToRecFlag True = Recursive
255 boolToRecFlag False = NonRecursive
257 instance Outputable RecFlag where
258 ppr Recursive = ptext SLIT("Recursive")
259 ppr NonRecursive = ptext SLIT("NonRecursive")
262 %************************************************************************
266 %************************************************************************
269 data TupCon = TupCon Boxity Arity
271 instance Eq TupCon where
272 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
274 tupleParens :: Boxity -> SDoc -> SDoc
275 tupleParens Boxed p = parens p
276 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
279 %************************************************************************
281 \subsection[Generic]{Generic flag}
283 %************************************************************************
285 This is the "Embedding-Projection pair" datatype, it contains
286 two pieces of code (normally either RenamedExpr's or Id's)
287 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
288 represents functions of type
297 T and Tring are arbitrary, but typically T is the 'main' type while
298 Tring is the 'representation' type. (This just helps us remember
299 whether to use 'from' or 'to'.
302 data EP a = EP { fromEP :: a, -- :: T -> Tring
303 toEP :: a } -- :: Tring -> T
306 Embedding-projection pairs are used in several places:
308 First of all, each type constructor has an EP associated with it, the
309 code in EP converts (datatype T) from T to Tring and back again.
311 Secondly, when we are filling in Generic methods (in the typechecker,
312 tcMethodBinds), we are constructing bimaps by induction on the structure
313 of the type of the method signature.
316 %************************************************************************
318 \subsection{Occurrence information}
320 %************************************************************************
322 This data type is used exclusively by the simplifier, but it appears in a
323 SubstResult, which is currently defined in VarEnv, which is pretty near
324 the base of the module hierarchy. So it seemed simpler to put the
325 defn of OccInfo here, safely at the bottom
331 | IAmDead -- Marks unused variables. Sometimes useful for
332 -- lambda and case-bound variables.
338 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
339 -- in a group of recursive definitions
341 seqOccInfo :: OccInfo -> ()
342 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
345 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
346 -- Substituting a redex for this occurrence is
347 -- dangerous because it might duplicate work.
351 type OneBranch = Bool -- True <=> Occurs in only one case branch
352 -- so no code-duplication issue to worry about
356 isLoopBreaker :: OccInfo -> Bool
357 isLoopBreaker IAmALoopBreaker = True
358 isLoopBreaker other = False
360 isDeadOcc :: OccInfo -> Bool
361 isDeadOcc IAmDead = True
362 isDeadOcc other = False
364 isOneOcc (OneOcc _ _) = True
365 isOneOcc other = False
367 isFragileOcc :: OccInfo -> Bool
368 isFragileOcc (OneOcc _ _) = True
369 isFragileOcc other = False
373 instance Outputable OccInfo where
374 -- only used for debugging; never parsed. KSW 1999-07
375 ppr NoOccInfo = empty
376 ppr IAmALoopBreaker = ptext SLIT("LoopBreaker")
377 ppr IAmDead = ptext SLIT("Dead")
378 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("OnceInLam")
379 | one_branch = ptext SLIT("Once")
380 | otherwise = ptext SLIT("OnceEachBranch")
382 instance Show OccInfo where
383 showsPrec p occ = showsPrecSDoc p (ppr occ)
386 %************************************************************************
388 \subsection{Strictness indication}
390 %************************************************************************
392 The strictness annotations on types in data type declarations
393 e.g. data T = MkT !Int !(Bool,Bool)
396 data StrictnessMark -- Used in interface decls only
402 isMarkedUnboxed MarkedUnboxed = True
403 isMarkedUnboxed other = False
405 isMarkedStrict NotMarkedStrict = False
406 isMarkedStrict other = True -- All others are strict
408 instance Outputable StrictnessMark where
409 ppr MarkedStrict = ptext SLIT("!")
410 ppr MarkedUnboxed = ptext SLIT("!!")
411 ppr NotMarkedStrict = ptext SLIT("_")
415 %************************************************************************
417 \subsection{Success flag}
419 %************************************************************************
422 data SuccessFlag = Succeeded | Failed
424 successIf :: Bool -> SuccessFlag
425 successIf True = Succeeded
426 successIf False = Failed
428 succeeded, failed :: SuccessFlag -> Bool
429 succeeded Succeeded = True
430 succeeded Failed = False
432 failed Succeeded = False
437 %************************************************************************
439 \subsection{Activation}
441 %************************************************************************
443 When a rule or inlining is active
446 type CompilerPhase = Int -- Compilation phase
447 -- Phases decrease towards zero
448 -- Zero is the last phase
450 data Activation = NeverActive
452 | ActiveBefore CompilerPhase -- Active only *before* this phase
453 | ActiveAfter CompilerPhase -- Active in this phase and later
454 deriving( Eq ) -- Eq used in comparing rules in HsDecls
456 instance Outputable Activation where
457 ppr AlwaysActive = empty -- The default
458 ppr (ActiveBefore n) = brackets (char '~' <> int n)
459 ppr (ActiveAfter n) = brackets (int n)
460 ppr NeverActive = ptext SLIT("NEVER")
462 isActive :: CompilerPhase -> Activation -> Bool
463 isActive p NeverActive = False
464 isActive p AlwaysActive = True
465 isActive p (ActiveAfter n) = p <= n
466 isActive p (ActiveBefore n) = p > n
468 isNeverActive, isAlwaysActive :: Activation -> Bool
469 isNeverActive NeverActive = True
470 isNeverActive act = False
472 isAlwaysActive AlwaysActive = True
473 isAlwaysActive other = False