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,
42 #include "HsVersions.h"
47 %************************************************************************
49 \subsection[Unused]{Unused}
51 %************************************************************************
53 Used as a placeholder in types.
59 unused = error "Unused is used!"
63 %************************************************************************
65 \subsection[Arity]{Arity}
67 %************************************************************************
74 %************************************************************************
76 \subsection[Version]{Module and identifier version numbers}
78 %************************************************************************
83 bogusVersion :: Version -- Shouldn't look at these
84 bogusVersion = error "bogusVersion"
86 bumpVersion :: Version -> Version
89 initialVersion :: Version
94 %************************************************************************
96 \subsection[Fixity]{Fixity info}
98 %************************************************************************
101 data Fixity = Fixity Int FixityDirection
102 data FixityDirection = InfixL | InfixR | InfixN
105 instance Outputable Fixity where
106 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
108 instance Outputable FixityDirection where
109 ppr InfixL = ptext SLIT("infixl")
110 ppr InfixR = ptext SLIT("infixr")
111 ppr InfixN = ptext SLIT("infix")
113 instance Eq Fixity where -- Used to determine if two fixities conflict
114 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
116 maxPrecedence = (9::Int)
117 defaultFixity = Fixity maxPrecedence InfixL
119 negateFixity :: Fixity
120 negateFixity = Fixity negatePrecedence InfixL -- Precedence of unary negate is wired in as infixl 6!
122 negatePrecedence :: Int
127 %************************************************************************
129 \subsection[NewType/DataType]{NewType/DataType flag}
131 %************************************************************************
135 = NewType -- "newtype Blah ..."
136 | DataType -- "data Blah ..."
137 deriving( Eq ) -- Needed because Demand derives Eq
140 %************************************************************************
142 \subsection[Top-level/local]{Top-level/not-top level flag}
144 %************************************************************************
151 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
153 isNotTopLevel NotTopLevel = True
154 isNotTopLevel TopLevel = False
156 isTopLevel TopLevel = True
157 isTopLevel NotTopLevel = False
160 %************************************************************************
162 \subsection[Top-level/local]{Top-level/not-top level flag}
164 %************************************************************************
172 isBoxed :: Boxity -> Bool
174 isBoxed Unboxed = False
176 tupleParens :: Boxity -> SDoc -> SDoc
177 tupleParens Boxed p = parens p
178 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
182 %************************************************************************
184 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
186 %************************************************************************
189 data RecFlag = Recursive
192 isRec :: RecFlag -> Bool
193 isRec Recursive = True
194 isRec NonRecursive = False
196 isNonRec :: RecFlag -> Bool
197 isNonRec Recursive = False
198 isNonRec NonRecursive = True
201 %************************************************************************
203 \subsection[Generic]{Generic flag}
205 %************************************************************************
207 This is the "Embedding-Projection pair" datatype, it contains
208 two pieces of code (normally either RenamedHsExpr's or Id's)
209 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
210 represents functions of type
219 T and Tring are arbitrary, but typically T is the 'main' type while
220 Tring is the 'representation' type. (This just helps us remember
221 whether to use 'from' or 'to'.
224 data EP a = EP { fromEP :: a, -- :: T -> Tring
225 toEP :: a } -- :: Tring -> T
228 Embedding-projection pairs are used in several places:
230 First of all, each type constructor has an EP associated with it, the
231 code in EP converts (datatype T) from T to Tring and back again.
233 Secondly, when we are filling in Generic methods (in the typechecker,
234 tcMethodBinds), we are constructing bimaps by induction on the structure
235 of the type of the method signature.
238 %************************************************************************
240 \subsection{Occurrence information}
242 %************************************************************************
244 This data type is used exclusively by the simplifier, but it appears in a
245 SubstResult, which is currently defined in VarEnv, which is pretty near
246 the base of the module hierarchy. So it seemed simpler to put the
247 defn of OccInfo here, safely at the bottom
253 | IAmDead -- Marks unused variables. Sometimes useful for
254 -- lambda and case-bound variables.
260 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
261 -- in a group of recursive definitions
263 seqOccInfo :: OccInfo -> ()
264 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
267 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
268 -- Substituting a redex for this occurrence is
269 -- dangerous because it might duplicate work.
273 type OneBranch = Bool -- True <=> Occurs in only one case branch
274 -- so no code-duplication issue to worry about
278 isLoopBreaker :: OccInfo -> Bool
279 isLoopBreaker IAmALoopBreaker = True
280 isLoopBreaker other = False
282 isDeadOcc :: OccInfo -> Bool
283 isDeadOcc IAmDead = True
284 isDeadOcc other = False
286 isFragileOcc :: OccInfo -> Bool
287 isFragileOcc (OneOcc _ _) = True
288 isFragileOcc other = False
292 instance Outputable OccInfo where
293 -- only used for debugging; never parsed. KSW 1999-07
294 ppr NoOccInfo = empty
295 ppr IAmALoopBreaker = ptext SLIT("_Kx")
296 ppr IAmDead = ptext SLIT("_Kd")
297 ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
298 | one_branch = ptext SLIT("_Ks")
299 | otherwise = ptext SLIT("_Ks*")
301 instance Show OccInfo where
302 showsPrec p occ = showsPrecSDoc p (ppr occ)