[project @ 2000-04-20 12:50:18 by simonpj]
[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,
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         OccInfo(..), seqOccInfo, isFragileOccInfo,
33         InsideLam, insideLam, notInsideLam,
34         OneBranch, oneBranch, notOneBranch
35
36    ) where
37
38 #include "HsVersions.h"
39
40 import Outputable
41 \end{code}
42
43 %************************************************************************
44 %*                                                                      *
45 \subsection[Unused]{Unused}
46 %*                                                                      *
47 %************************************************************************
48
49 Used as a placeholder in types.
50
51 \begin{code}
52 type Unused = ()
53
54 unused :: Unused
55 unused = error "Unused is used!"
56 \end{code}
57
58
59 %************************************************************************
60 %*                                                                      *
61 \subsection[Arity]{Arity}
62 %*                                                                      *
63 %************************************************************************
64
65 \begin{code}
66 type Arity = Int
67 \end{code}
68
69
70 %************************************************************************
71 %*                                                                      *
72 \subsection[Version]{Module and identifier version numbers}
73 %*                                                                      *
74 %************************************************************************
75
76 \begin{code}
77 type Version = Int
78 \end{code}
79
80
81 %************************************************************************
82 %*                                                                      *
83 \subsection[Fixity]{Fixity info}
84 %*                                                                      *
85 %************************************************************************
86
87 \begin{code}
88 data Fixity = Fixity Int FixityDirection
89 data FixityDirection = InfixL | InfixR | InfixN 
90                      deriving(Eq)
91
92 instance Outputable Fixity where
93     ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
94
95 instance Outputable FixityDirection where
96     ppr InfixL = ptext SLIT("infixl")
97     ppr InfixR = ptext SLIT("infixr")
98     ppr InfixN = ptext SLIT("infix")
99
100 instance Eq Fixity where                -- Used to determine if two fixities conflict
101   (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
102
103 maxPrecedence = (9::Int)
104 defaultFixity = Fixity maxPrecedence InfixL
105
106 negateFixity :: Fixity
107 negateFixity     = Fixity negatePrecedence InfixL       -- Precedence of unary negate is wired in as infixl 6!
108
109 negatePrecedence :: Int
110 negatePrecedence = 6
111 \end{code}
112
113
114 %************************************************************************
115 %*                                                                      *
116 \subsection[NewType/DataType]{NewType/DataType flag}
117 %*                                                                      *
118 %************************************************************************
119
120 \begin{code}
121 data NewOrData
122   = NewType     -- "newtype Blah ..."
123   | DataType    -- "data Blah ..."
124   deriving( Eq )        -- Needed because Demand derives Eq
125 \end{code}
126
127 %************************************************************************
128 %*                                                                      *
129 \subsection[Top-level/local]{Top-level/not-top level flag}
130 %*                                                                      *
131 %************************************************************************
132
133 \begin{code}
134 data TopLevelFlag
135   = TopLevel
136   | NotTopLevel
137
138 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
139
140 isNotTopLevel NotTopLevel = True
141 isNotTopLevel TopLevel    = False
142
143 isTopLevel TopLevel     = True
144 isTopLevel NotTopLevel  = False
145 \end{code}
146
147 %************************************************************************
148 %*                                                                      *
149 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
150 %*                                                                      *
151 %************************************************************************
152
153 \begin{code} 
154 data RecFlag = Recursive 
155              | NonRecursive
156
157 isRec :: RecFlag -> Bool
158 isRec Recursive    = True
159 isRec NonRecursive = False
160
161 isNonRec :: RecFlag -> Bool
162 isNonRec Recursive    = False
163 isNonRec NonRecursive = True
164 \end{code}
165
166
167 %************************************************************************
168 %*                                                                      *
169 \subsection{Occurrence information}
170 %*                                                                      *
171 %************************************************************************
172
173 This data type is used exclusively by the simplifier, but it appears in a
174 SubstResult, which is currently defined in VarEnv, which is pretty near
175 the base of the module hierarchy.  So it seemed simpler to put the
176 defn of OccInfo here, safely at the bottom
177
178 \begin{code}
179 data OccInfo 
180   = NoOccInfo
181
182   | IAmDead             -- Marks unused variables.  Sometimes useful for
183                         -- lambda and case-bound variables.
184
185   | OneOcc InsideLam
186
187            OneBranch
188
189   | IAmALoopBreaker     -- Used by the occurrence analyser to mark loop-breakers
190                         -- in a group of recursive definitions
191
192 seqOccInfo :: OccInfo -> ()
193 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
194 seqOccInfo occ                  = ()
195
196 type InsideLam = Bool   -- True <=> Occurs inside a non-linear lambda
197                         -- Substituting a redex for this occurrence is
198                         -- dangerous because it might duplicate work.
199 insideLam    = True
200 notInsideLam = False
201
202 type OneBranch = Bool   -- True <=> Occurs in only one case branch
203                         --      so no code-duplication issue to worry about
204 oneBranch    = True
205 notOneBranch = False
206
207 isFragileOccInfo :: OccInfo -> Bool
208 isFragileOccInfo (OneOcc _ _) = True
209 isFragileOccInfo other        = False
210 \end{code}
211
212 \begin{code}
213 instance Outputable OccInfo where
214   -- only used for debugging; never parsed.  KSW 1999-07
215   ppr NoOccInfo                                   = empty
216   ppr IAmALoopBreaker                             = ptext SLIT("_Kx")
217   ppr IAmDead                                     = ptext SLIT("_Kd")
218   ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
219                                      | one_branch = ptext SLIT("_Ks")
220                                      | otherwise  = ptext SLIT("_Ks*")
221
222 instance Show OccInfo where
223   showsPrec p occ = showsPrecSDoc p (ppr occ)
224 \end{code}
225