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,
23 Fixity(..), FixityDirection(..),
24 defaultFixity, maxPrecedence, negateFixity, negatePrecedence,
28 RecFlag(..), isRec, isNonRec,
30 TopLevelFlag(..), isTopLevel, isNotTopLevel,
32 Boxity(..), isBoxed, tupleParens,
34 OccInfo(..), seqOccInfo, isFragileOcc, isDeadOcc, isLoopBreaker,
36 InsideLam, insideLam, notInsideLam,
37 OneBranch, oneBranch, notOneBranch,
44 #include "HsVersions.h"
49 %************************************************************************
51 \subsection[Unused]{Unused}
53 %************************************************************************
55 Used as a placeholder in types.
61 unused = error "Unused is used!"
65 %************************************************************************
67 \subsection[Arity]{Arity}
69 %************************************************************************
76 %************************************************************************
78 \subsection[Version]{Module and identifier version numbers}
80 %************************************************************************
85 bogusVersion :: Version -- Shouldn't look at these
86 bogusVersion = error "bogusVersion"
88 bumpVersion :: Bool -> Version -> Version
89 -- Bump if the predicate (typically equality between old and new) is false
90 bumpVersion False v = v+1
91 bumpVersion True v = v
93 initialVersion :: Version
98 %************************************************************************
100 \subsection[Fixity]{Fixity info}
102 %************************************************************************
105 data Fixity = Fixity Int FixityDirection
106 data FixityDirection = InfixL | InfixR | InfixN
109 instance Outputable Fixity where
110 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
112 instance Outputable FixityDirection where
113 ppr InfixL = ptext SLIT("infixl")
114 ppr InfixR = ptext SLIT("infixr")
115 ppr InfixN = ptext SLIT("infix")
117 instance Eq Fixity where -- Used to determine if two fixities conflict
118 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
120 maxPrecedence = (9::Int)
121 defaultFixity = Fixity maxPrecedence InfixL
123 negateFixity :: Fixity
124 negateFixity = Fixity negatePrecedence InfixL -- Precedence of unary negate is wired in as infixl 6!
126 negatePrecedence :: Int
131 %************************************************************************
133 \subsection[NewType/DataType]{NewType/DataType flag}
135 %************************************************************************
139 = NewType -- "newtype Blah ..."
140 | DataType -- "data Blah ..."
141 deriving( Eq ) -- Needed because Demand derives Eq
144 %************************************************************************
146 \subsection[Top-level/local]{Top-level/not-top level flag}
148 %************************************************************************
155 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
157 isNotTopLevel NotTopLevel = True
158 isNotTopLevel TopLevel = False
160 isTopLevel TopLevel = True
161 isTopLevel NotTopLevel = False
164 %************************************************************************
166 \subsection[Top-level/local]{Top-level/not-top level flag}
168 %************************************************************************
176 isBoxed :: Boxity -> Bool
178 isBoxed Unboxed = False
180 tupleParens :: Boxity -> SDoc -> SDoc
181 tupleParens Boxed p = parens p
182 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
186 %************************************************************************
188 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
190 %************************************************************************
193 data RecFlag = Recursive
196 isRec :: RecFlag -> Bool
197 isRec Recursive = True
198 isRec NonRecursive = False
200 isNonRec :: RecFlag -> Bool
201 isNonRec Recursive = False
202 isNonRec NonRecursive = True
205 %************************************************************************
207 \subsection[Interactive/Batch]{Interactive/Batch flag}
209 %************************************************************************
212 data GhciMode = Batch
216 %************************************************************************
218 \subsection[Generic]{Generic flag}
220 %************************************************************************
222 This is the "Embedding-Projection pair" datatype, it contains
223 two pieces of code (normally either RenamedHsExpr's or Id's)
224 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
225 represents functions of type
234 T and Tring are arbitrary, but typically T is the 'main' type while
235 Tring is the 'representation' type. (This just helps us remember
236 whether to use 'from' or 'to'.
239 data EP a = EP { fromEP :: a, -- :: T -> Tring
240 toEP :: a } -- :: Tring -> T
243 Embedding-projection pairs are used in several places:
245 First of all, each type constructor has an EP associated with it, the
246 code in EP converts (datatype T) from T to Tring and back again.
248 Secondly, when we are filling in Generic methods (in the typechecker,
249 tcMethodBinds), we are constructing bimaps by induction on the structure
250 of the type of the method signature.
253 %************************************************************************
255 \subsection{Occurrence information}
257 %************************************************************************
259 This data type is used exclusively by the simplifier, but it appears in a
260 SubstResult, which is currently defined in VarEnv, which is pretty near
261 the base of the module hierarchy. So it seemed simpler to put the
262 defn of OccInfo here, safely at the bottom
268 | IAmDead -- Marks unused variables. Sometimes useful for
269 -- lambda and case-bound variables.
275 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
276 -- in a group of recursive definitions
278 seqOccInfo :: OccInfo -> ()
279 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
282 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
283 -- Substituting a redex for this occurrence is
284 -- dangerous because it might duplicate work.
288 type OneBranch = Bool -- True <=> Occurs in only one case branch
289 -- so no code-duplication issue to worry about
293 isLoopBreaker :: OccInfo -> Bool
294 isLoopBreaker IAmALoopBreaker = True
295 isLoopBreaker other = False
297 isDeadOcc :: OccInfo -> Bool
298 isDeadOcc IAmDead = True
299 isDeadOcc other = False
301 isFragileOcc :: OccInfo -> Bool
302 isFragileOcc (OneOcc _ _) = True
303 isFragileOcc other = False
307 instance Outputable OccInfo where
308 -- only used for debugging; never parsed. KSW 1999-07
309 ppr NoOccInfo = empty
310 ppr IAmALoopBreaker = ptext SLIT("_Kx")
311 ppr IAmDead = ptext SLIT("_Kd")
312 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
313 | one_branch = ptext SLIT("_Ks")
314 | otherwise = ptext SLIT("_Ks*")
316 instance Show OccInfo where
317 showsPrec p occ = showsPrecSDoc p (ppr occ)