[project @ 2000-03-23 17:45:17 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   | EnumType    -- Enumeration; all constructors are nullary
125   deriving( Eq )        -- Needed because Demand derives Eq
126 \end{code}
127
128 %************************************************************************
129 %*                                                                      *
130 \subsection[Top-level/local]{Top-level/not-top level flag}
131 %*                                                                      *
132 %************************************************************************
133
134 \begin{code}
135 data TopLevelFlag
136   = TopLevel
137   | NotTopLevel
138
139 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
140
141 isNotTopLevel NotTopLevel = True
142 isNotTopLevel TopLevel    = False
143
144 isTopLevel TopLevel     = True
145 isTopLevel NotTopLevel  = False
146 \end{code}
147
148 %************************************************************************
149 %*                                                                      *
150 \subsection[Recursive/Non-Recursive]{Recursive/Non-Recursive flag}
151 %*                                                                      *
152 %************************************************************************
153
154 \begin{code} 
155 data RecFlag = Recursive 
156              | NonRecursive
157
158 isRec :: RecFlag -> Bool
159 isRec Recursive    = True
160 isRec NonRecursive = False
161
162 isNonRec :: RecFlag -> Bool
163 isNonRec Recursive    = False
164 isNonRec NonRecursive = True
165 \end{code}
166
167
168 %************************************************************************
169 %*                                                                      *
170 \subsection{Occurrence information}
171 %*                                                                      *
172 %************************************************************************
173
174 This data type is used exclusively by the simplifier, but it appears in a
175 SubstResult, which is currently defined in VarEnv, which is pretty near
176 the base of the module hierarchy.  So it seemed simpler to put the
177 defn of OccInfo here, safely at the bottom
178
179 \begin{code}
180 data OccInfo 
181   = NoOccInfo
182
183   | IAmDead             -- Marks unused variables.  Sometimes useful for
184                         -- lambda and case-bound variables.
185
186   | OneOcc InsideLam
187
188            OneBranch
189
190   | IAmALoopBreaker     -- Used by the occurrence analyser to mark loop-breakers
191                         -- in a group of recursive definitions
192
193 seqOccInfo :: OccInfo -> ()
194 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
195 seqOccInfo occ                  = ()
196
197 type InsideLam = Bool   -- True <=> Occurs inside a non-linear lambda
198                         -- Substituting a redex for this occurrence is
199                         -- dangerous because it might duplicate work.
200 insideLam    = True
201 notInsideLam = False
202
203 type OneBranch = Bool   -- True <=> Occurs in only one case branch
204                         --      so no code-duplication issue to worry about
205 oneBranch    = True
206 notOneBranch = False
207
208 isFragileOccInfo :: OccInfo -> Bool
209 isFragileOccInfo (OneOcc _ _) = True
210 isFragileOccInfo other        = False
211 \end{code}
212
213 \begin{code}
214 instance Outputable OccInfo where
215   -- only used for debugging; never parsed.  KSW 1999-07
216   ppr NoOccInfo                                   = empty
217   ppr IAmALoopBreaker                             = ptext SLIT("_Kx")
218   ppr IAmDead                                     = ptext SLIT("_Kd")
219   ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
220                                      | one_branch = ptext SLIT("_Ks")
221                                      | otherwise  = ptext SLIT("_Ks*")
222
223 instance Show OccInfo where
224   showsPrec p occ = showsPrecSDoc p (ppr occ)
225 \end{code}
226