14c9893befceea90ba6f5dccda01e7823f58afc1
[ghc-hetmet.git] / ghc / compiler / basicTypes / BasicTypes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
3 %
4 \section[BasicTypes]{Miscellanous types}
5
6 This module defines a miscellaneously collection of very simple
7 types that
8
9 \begin{itemize}
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
13 \end{itemize}
14
15 \begin{code}
16 module BasicTypes(
17         Version, bumpVersion, initialVersion, bogusVersion,
18
19         Arity, 
20
21         Unused, unused,
22
23         Fixity(..), FixityDirection(..),
24         defaultFixity, maxPrecedence, negateFixity, negatePrecedence,
25
26         NewOrData(..), 
27
28         RecFlag(..), isRec, isNonRec,
29
30         TopLevelFlag(..), isTopLevel, isNotTopLevel,
31
32         Boxity(..), isBoxed, tupleParens,
33
34         OccInfo(..), seqOccInfo, isFragileOccInfo, isLoopBreaker,
35
36         InsideLam, insideLam, notInsideLam,
37         OneBranch, oneBranch, notOneBranch
38
39    ) where
40
41 #include "HsVersions.h"
42
43 import Outputable
44 \end{code}
45
46 %************************************************************************
47 %*                                                                      *
48 \subsection[Unused]{Unused}
49 %*                                                                      *
50 %************************************************************************
51
52 Used as a placeholder in types.
53
54 \begin{code}
55 type Unused = ()
56
57 unused :: Unused
58 unused = error "Unused is used!"
59 \end{code}
60
61
62 %************************************************************************
63 %*                                                                      *
64 \subsection[Arity]{Arity}
65 %*                                                                      *
66 %************************************************************************
67
68 \begin{code}
69 type Arity = Int
70 \end{code}
71
72
73 %************************************************************************
74 %*                                                                      *
75 \subsection[Version]{Module and identifier version numbers}
76 %*                                                                      *
77 %************************************************************************
78
79 \begin{code}
80 type Version = Int
81
82 bogusVersion :: Version -- Shouldn't look at these
83 bogusVersion = error "bogusVersion"
84
85 bumpVersion :: Version -> Version 
86 bumpVersion v = v+1
87
88 initialVersion :: Version
89 initialVersion = 1
90 \end{code}
91
92
93 %************************************************************************
94 %*                                                                      *
95 \subsection[Fixity]{Fixity info}
96 %*                                                                      *
97 %************************************************************************
98
99 \begin{code}
100 data Fixity = Fixity Int FixityDirection
101 data FixityDirection = InfixL | InfixR | InfixN 
102                      deriving(Eq)
103
104 instance Outputable Fixity where
105     ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
106
107 instance Outputable FixityDirection where
108     ppr InfixL = ptext SLIT("infixl")
109     ppr InfixR = ptext SLIT("infixr")
110     ppr InfixN = ptext SLIT("infix")
111
112 instance Eq Fixity where                -- Used to determine if two fixities conflict
113   (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
114
115 maxPrecedence = (9::Int)
116 defaultFixity = Fixity maxPrecedence InfixL
117
118 negateFixity :: Fixity
119 negateFixity     = Fixity negatePrecedence InfixL       -- Precedence of unary negate is wired in as infixl 6!
120
121 negatePrecedence :: Int
122 negatePrecedence = 6
123 \end{code}
124
125
126 %************************************************************************
127 %*                                                                      *
128 \subsection[NewType/DataType]{NewType/DataType flag}
129 %*                                                                      *
130 %************************************************************************
131
132 \begin{code}
133 data NewOrData
134   = NewType     -- "newtype Blah ..."
135   | DataType    -- "data Blah ..."
136   deriving( Eq )        -- Needed because Demand derives Eq
137 \end{code}
138
139 %************************************************************************
140 %*                                                                      *
141 \subsection[Top-level/local]{Top-level/not-top level flag}
142 %*                                                                      *
143 %************************************************************************
144
145 \begin{code}
146 data TopLevelFlag
147   = TopLevel
148   | NotTopLevel
149
150 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
151
152 isNotTopLevel NotTopLevel = True
153 isNotTopLevel TopLevel    = False
154
155 isTopLevel TopLevel     = True
156 isTopLevel NotTopLevel  = False
157 \end{code}
158
159 %************************************************************************
160 %*                                                                      *
161 \subsection[Top-level/local]{Top-level/not-top level flag}
162 %*                                                                      *
163 %************************************************************************
164
165 \begin{code}
166 data Boxity
167   = Boxed
168   | Unboxed
169   deriving( Eq )
170
171 isBoxed :: Boxity -> Bool
172 isBoxed Boxed   = True
173 isBoxed Unboxed = False
174
175 tupleParens :: Boxity -> SDoc -> SDoc
176 tupleParens Boxed   p = parens p
177 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
178 \end{code}
179
180
181 %************************************************************************
182 %*                                                                      *
183 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
184 %*                                                                      *
185 %************************************************************************
186
187 \begin{code} 
188 data RecFlag = Recursive 
189              | NonRecursive
190
191 isRec :: RecFlag -> Bool
192 isRec Recursive    = True
193 isRec NonRecursive = False
194
195 isNonRec :: RecFlag -> Bool
196 isNonRec Recursive    = False
197 isNonRec NonRecursive = True
198 \end{code}
199
200
201 %************************************************************************
202 %*                                                                      *
203 \subsection{Occurrence information}
204 %*                                                                      *
205 %************************************************************************
206
207 This data type is used exclusively by the simplifier, but it appears in a
208 SubstResult, which is currently defined in VarEnv, which is pretty near
209 the base of the module hierarchy.  So it seemed simpler to put the
210 defn of OccInfo here, safely at the bottom
211
212 \begin{code}
213 data OccInfo 
214   = NoOccInfo
215
216   | IAmDead             -- Marks unused variables.  Sometimes useful for
217                         -- lambda and case-bound variables.
218
219   | OneOcc InsideLam
220
221            OneBranch
222
223   | IAmALoopBreaker     -- Used by the occurrence analyser to mark loop-breakers
224                         -- in a group of recursive definitions
225
226 seqOccInfo :: OccInfo -> ()
227 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
228 seqOccInfo occ                  = ()
229
230 type InsideLam = Bool   -- True <=> Occurs inside a non-linear lambda
231                         -- Substituting a redex for this occurrence is
232                         -- dangerous because it might duplicate work.
233 insideLam    = True
234 notInsideLam = False
235
236 type OneBranch = Bool   -- True <=> Occurs in only one case branch
237                         --      so no code-duplication issue to worry about
238 oneBranch    = True
239 notOneBranch = False
240
241 isLoopBreaker :: OccInfo -> Bool
242 isLoopBreaker IAmALoopBreaker = True
243 isLoopBreaker other           = False
244
245 isFragileOccInfo :: OccInfo -> Bool
246 isFragileOccInfo (OneOcc _ _) = True
247 isFragileOccInfo other        = False
248 \end{code}
249
250 \begin{code}
251 instance Outputable OccInfo where
252   -- only used for debugging; never parsed.  KSW 1999-07
253   ppr NoOccInfo                                   = empty
254   ppr IAmALoopBreaker                             = ptext SLIT("_Kx")
255   ppr IAmDead                                     = ptext SLIT("_Kd")
256   ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
257                                      | one_branch = ptext SLIT("_Ks")
258                                      | otherwise  = ptext SLIT("_Ks*")
259
260 instance Show OccInfo where
261   showsPrec p occ = showsPrecSDoc p (ppr occ)
262 \end{code}
263