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,
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 :: Fixity
159 negateFixity = Fixity negatePrecedence InfixL -- Precedence of unary negate is wired in as infixl 6!
161 negatePrecedence :: Int
170 @(compareFixity op1 op2)@ tells which way to arrange appication, or
171 whether there's an error.
174 compareFixity :: Fixity -> Fixity
175 -> (Bool, -- Error please
176 Bool) -- Associate to the right: a op1 (b op2 c)
177 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
178 = case prec1 `compare` prec2 of
181 EQ -> case (dir1, dir2) of
182 (InfixR, InfixR) -> right
183 (InfixL, InfixL) -> left
186 right = (False, True)
187 left = (False, False)
188 error_please = (True, False)
192 %************************************************************************
194 \subsection[Top-level/local]{Top-level/not-top level flag}
196 %************************************************************************
203 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
205 isNotTopLevel NotTopLevel = True
206 isNotTopLevel TopLevel = False
208 isTopLevel TopLevel = True
209 isTopLevel NotTopLevel = False
211 instance Outputable TopLevelFlag where
212 ppr TopLevel = ptext SLIT("<TopLevel>")
213 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
217 %************************************************************************
219 \subsection[Top-level/local]{Top-level/not-top level flag}
221 %************************************************************************
229 isBoxed :: Boxity -> Bool
231 isBoxed Unboxed = False
235 %************************************************************************
237 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
239 %************************************************************************
242 data RecFlag = Recursive
246 isRec :: RecFlag -> Bool
247 isRec Recursive = True
248 isRec NonRecursive = False
250 isNonRec :: RecFlag -> Bool
251 isNonRec Recursive = False
252 isNonRec NonRecursive = True
254 boolToRecFlag :: Bool -> RecFlag
255 boolToRecFlag True = Recursive
256 boolToRecFlag False = NonRecursive
258 instance Outputable RecFlag where
259 ppr Recursive = ptext SLIT("Recursive")
260 ppr NonRecursive = ptext SLIT("NonRecursive")
263 %************************************************************************
267 %************************************************************************
270 data TupCon = TupCon Boxity Arity
272 instance Eq TupCon where
273 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
275 tupleParens :: Boxity -> SDoc -> SDoc
276 tupleParens Boxed p = parens p
277 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
280 %************************************************************************
282 \subsection[Generic]{Generic flag}
284 %************************************************************************
286 This is the "Embedding-Projection pair" datatype, it contains
287 two pieces of code (normally either RenamedExpr's or Id's)
288 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
289 represents functions of type
298 T and Tring are arbitrary, but typically T is the 'main' type while
299 Tring is the 'representation' type. (This just helps us remember
300 whether to use 'from' or 'to'.
303 data EP a = EP { fromEP :: a, -- :: T -> Tring
304 toEP :: a } -- :: Tring -> T
307 Embedding-projection pairs are used in several places:
309 First of all, each type constructor has an EP associated with it, the
310 code in EP converts (datatype T) from T to Tring and back again.
312 Secondly, when we are filling in Generic methods (in the typechecker,
313 tcMethodBinds), we are constructing bimaps by induction on the structure
314 of the type of the method signature.
317 %************************************************************************
319 \subsection{Occurrence information}
321 %************************************************************************
323 This data type is used exclusively by the simplifier, but it appears in a
324 SubstResult, which is currently defined in VarEnv, which is pretty near
325 the base of the module hierarchy. So it seemed simpler to put the
326 defn of OccInfo here, safely at the bottom
332 | IAmDead -- Marks unused variables. Sometimes useful for
333 -- lambda and case-bound variables.
339 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
340 -- in a group of recursive definitions
342 seqOccInfo :: OccInfo -> ()
343 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
346 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
347 -- Substituting a redex for this occurrence is
348 -- dangerous because it might duplicate work.
352 type OneBranch = Bool -- True <=> Occurs in only one case branch
353 -- so no code-duplication issue to worry about
357 isLoopBreaker :: OccInfo -> Bool
358 isLoopBreaker IAmALoopBreaker = True
359 isLoopBreaker other = False
361 isDeadOcc :: OccInfo -> Bool
362 isDeadOcc IAmDead = True
363 isDeadOcc other = False
365 isOneOcc (OneOcc _ _) = True
366 isOneOcc other = False
368 isFragileOcc :: OccInfo -> Bool
369 isFragileOcc (OneOcc _ _) = True
370 isFragileOcc other = False
374 instance Outputable OccInfo where
375 -- only used for debugging; never parsed. KSW 1999-07
376 ppr NoOccInfo = empty
377 ppr IAmALoopBreaker = ptext SLIT("_Kx")
378 ppr IAmDead = ptext SLIT("_Kd")
379 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
380 | one_branch = ptext SLIT("_Ks")
381 | otherwise = ptext SLIT("_Ks*")
383 instance Show OccInfo where
384 showsPrec p occ = showsPrecSDoc p (ppr occ)
387 %************************************************************************
389 \subsection{Strictness indication}
391 %************************************************************************
393 The strictness annotations on types in data type declarations
394 e.g. data T = MkT !Int !(Bool,Bool)
397 data StrictnessMark -- Used in interface decls only
403 isMarkedUnboxed MarkedUnboxed = True
404 isMarkedUnboxed other = False
406 isMarkedStrict NotMarkedStrict = False
407 isMarkedStrict other = True -- All others are strict
409 instance Outputable StrictnessMark where
410 ppr MarkedStrict = ptext SLIT("!")
411 ppr MarkedUnboxed = ptext SLIT("!!")
412 ppr NotMarkedStrict = ptext SLIT("_")
416 %************************************************************************
418 \subsection{Success flag}
420 %************************************************************************
423 data SuccessFlag = Succeeded | Failed
425 successIf :: Bool -> SuccessFlag
426 successIf True = Succeeded
427 successIf False = Failed
429 succeeded, failed :: SuccessFlag -> Bool
430 succeeded Succeeded = True
431 succeeded Failed = False
433 failed Succeeded = False
438 %************************************************************************
440 \subsection{Activation}
442 %************************************************************************
444 When a rule or inlining is active
447 type CompilerPhase = Int -- Compilation phase
448 -- Phases decrease towards zero
449 -- Zero is the last phase
451 data Activation = NeverActive
453 | ActiveBefore CompilerPhase -- Active only *before* this phase
454 | ActiveAfter CompilerPhase -- Active in this phase and later
455 deriving( Eq ) -- Eq used in comparing rules in HsDecls
457 instance Outputable Activation where
458 ppr AlwaysActive = empty -- The default
459 ppr (ActiveBefore n) = brackets (char '~' <> int n)
460 ppr (ActiveAfter n) = brackets (int n)
461 ppr NeverActive = ptext SLIT("NEVER")
463 isActive :: CompilerPhase -> Activation -> Bool
464 isActive p NeverActive = False
465 isActive p AlwaysActive = True
466 isActive p (ActiveAfter n) = p <= n
467 isActive p (ActiveBefore n) = p > n
469 isNeverActive, isAlwaysActive :: Activation -> Bool
470 isNeverActive NeverActive = True
471 isNeverActive act = False
473 isAlwaysActive AlwaysActive = True
474 isAlwaysActive other = False