[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / compiler / abstractSyn / AbsSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
3 %
4 \section[AbsSyntax]{Abstract syntax definition}
5
6 This module glues together the pieces of the Haskell abstract syntax,
7 which is declared in the various \tr{Hs*} modules.  This module,
8 therefore, is almost nothing but re-exporting.
9
10 The abstract syntax, used in the front end of the compiler, follows
11 that of a paper on the static semantics of Haskell by Simon Peyton
12 Jones and Phil Wadler.
13
14 The abstract syntax is parameterised with respect to variables
15 (abbrev: \tr{name}) and patterns (abbrev: \tr{pat}); here is a typical
16 example:
17 \begin{pseudocode}
18 type ProtoNameExpr   = Expr ProtoName   ProtoNamePat
19 type TypecheckedExpr = Expr Id          TypecheckedPat
20 \end{pseudocode}
21 Some parts of the syntax are unparameterised, because there is no
22 need for them to be.
23
24 \begin{code}
25 #include "HsVersions.h"
26
27 module AbsSyn (
28         -- the mostly-parameterised data types
29         ArithSeqInfo(..),
30         Bind(..),
31         Binds(..),
32         ClassDecl(..),
33         ClassPragmas,   -- abstract
34         ConDecl(..),
35         DefaultDecl(..),
36         Expr(..),
37         FixityDecl(..),
38         GRHSsAndBinds(..),
39         GRHS(..),
40         IE(..),
41         ImportedInterface(..),
42         IfaceImportDecl(..),
43         InPat(..),
44         InstDecl(..),
45         InstancePragmas,    -- abstract
46         Interface(..),
47         Literal(..),
48         Match(..),
49         Module(..),
50         MonoBinds(..),
51         MonoType(..),
52         PolyType(..),
53         Qual(..),
54         Renaming(..),
55         Sig(..),
56         GenPragmas,         -- abstract
57         ClassOpPragmas,     -- abstract
58         TyDecl(..),
59         DataPragmas,        -- abstract
60         TypePragmas,        -- abstract
61         TypecheckedPat(..),
62         SpecialisedInstanceSig(..), -- a user pragma
63         DataTypeSig(..),
64
65         Context(..),    -- synonyms
66         ClassAssertion(..),
67
68         -- synonyms for the (unparameterised) typechecker input
69         ProtoNameArithSeqInfo(..),
70         ProtoNameBind(..),
71         ProtoNameBinds(..),
72         ProtoNameClassDecl(..),
73         ProtoNameClassPragmas(..),
74         ProtoNameConDecl(..),
75         ProtoNameContext(..),
76         ProtoNameDefaultDecl(..),
77         ProtoNameExpr(..),
78         ProtoNameFixityDecl(..),
79         ProtoNameGRHSsAndBinds(..),
80         ProtoNameGRHS(..),
81         ProtoNameImportedInterface(..),
82         ProtoNameInstDecl(..),
83         ProtoNameInstancePragmas(..),
84         ProtoNameInterface(..),
85         ProtoNameMatch(..),
86         ProtoNameModule(..),
87         ProtoNameMonoBinds(..),
88         ProtoNameMonoType(..),
89         ProtoNamePat(..),
90         ProtoNamePolyType(..),
91         ProtoNameQual(..),
92         ProtoNameSig(..),
93         ProtoNameClassOpSig(..),
94         ProtoNameGenPragmas(..),
95         ProtoNameClassOpPragmas(..),
96         ProtoNameTyDecl(..),
97         ProtoNameDataPragmas(..),
98         ProtoNameSpecialisedInstanceSig(..),
99         ProtoNameDataTypeSig(..),
100
101         RenamedArithSeqInfo(..),
102         RenamedBind(..),
103         RenamedBinds(..),
104         RenamedClassDecl(..),
105         RenamedClassPragmas(..),
106         RenamedConDecl(..),
107         RenamedContext(..),
108         RenamedDefaultDecl(..),
109         RenamedExpr(..),
110         RenamedFixityDecl(..),
111         RenamedGRHSsAndBinds(..),
112         RenamedGRHS(..),
113         RenamedImportedInterface(..),
114         RenamedInstDecl(..),
115         RenamedInstancePragmas(..),
116         RenamedInterface(..),
117         RenamedMatch(..),
118         RenamedModule(..),
119         RenamedMonoBinds(..),
120         RenamedMonoType(..),
121         RenamedPat(..),
122         RenamedPolyType(..),
123         RenamedQual(..),
124         RenamedSig(..),
125         RenamedClassOpSig(..),
126         RenamedGenPragmas(..),
127         RenamedClassOpPragmas(..),
128         RenamedTyDecl(..),
129         RenamedDataPragmas(..),
130         RenamedSpecialisedInstanceSig(..),
131         RenamedDataTypeSig(..),
132
133         -- synonyms for the (unparameterised) typechecker output
134         TypecheckedArithSeqInfo(..),
135         TypecheckedBind(..),
136         TypecheckedBinds(..),
137         TypecheckedExpr(..),
138         TypecheckedGRHSsAndBinds(..),
139         TypecheckedGRHS(..),
140         TypecheckedMatch(..),
141         TypecheckedMonoBinds(..),
142         TypecheckedModule(..),
143         TypecheckedQual(..),
144
145         -- little help functions (AbsSynFuns)
146         collectTopLevelBinders,
147         collectBinders, collectTypedBinders,
148         collectMonoBinders,
149         collectMonoBindersAndLocs,
150         collectQualBinders,
151         collectPatBinders,
152         collectTypedPatBinders,
153         extractMonoTyNames,
154         cmpInstanceTypes, getNonPrelOuterTyCon,
155         getIEStrings, getRawIEStrings, ImExportListInfo(..),
156 --OLD:  getMentionedVars,
157         mkDictApp,
158         mkDictLam,
159         mkTyApp,
160         mkTyLam,
161         nullBinds,
162         nullMonoBinds,
163         isLitPat, patsAreAllLits, isConPat, patsAreAllCons,
164         irrefutablePat,
165 #ifdef DPH
166         patsAreAllProcessor,
167 #endif
168         unfailablePat, unfailablePats,
169         pprContext,
170         typeOfPat,
171         negLiteral,
172
173         eqConDecls, eqMonoType, cmpPolyType,
174
175         -- imported things so we get a closed interface
176         Outputable(..), NamedThing(..),
177         ExportFlag, SrcLoc,
178         Pretty(..), PprStyle, PrettyRep,
179
180         OptIdInfo(..), -- I hate the instance virus!
181         IdInfo, SpecEnv, StrictnessInfo, UpdateInfo, ArityInfo,
182         DemandInfo, Demand, ArgUsageInfo, ArgUsage, DeforestInfo,
183         FBTypeInfo, FBType, FBConsum, FBProd,
184
185         Name(..),       -- NB: goes out *WITH* constructors
186         Id, DictVar(..), Inst, ProtoName, TyVar, UniType, TauType(..),
187         Maybe, PreludeNameFun(..), Unique,
188         FullName, ShortName, Arity(..), TyCon, Class, ClassOp,
189         UnfoldingGuidance, BinderInfo, BasicLit, PrimOp, PrimKind,
190         IdEnv(..), UniqFM, FiniteMap,
191         CoreExpr, CoreAtom, UnfoldingCoreAtom, UnfoldingCoreExpr,
192         UnfoldingPrimOp, UfCostCentre, Bag
193         IF_ATTACK_PRAGMAS(COMMA cmpClass COMMA cmpTyCon COMMA cmpTyVar)
194         IF_ATTACK_PRAGMAS(COMMA cmpUniType COMMA pprPrimOp)
195 #ifndef __GLASGOW_HASKELL__
196         ,TAG_
197 #endif
198 #ifdef DPH
199         ,ParQuals(..), ProtoNameParQuals(..),
200         RenamedParQuals(..), TypecheckedParQuals(..),
201         collectParQualBinders
202 #endif {- Data Parallel Haskell -}
203      ) where
204
205
206 import AbsSynFuns       -- help functions
207
208 import HsBinds          -- the main stuff to export
209 import HsCore
210 import HsDecls
211 import HsExpr
212 import HsImpExp
213 import HsLit
214 import HsMatches
215 import HsPat
216 import HsPragmas
217 import HsTypes
218
219 import AbsPrel          ( PrimKind, PrimOp
220                           IF_ATTACK_PRAGMAS(COMMA tagOf_PrimOp)
221                           IF_ATTACK_PRAGMAS(COMMA pprPrimOp)
222                         )
223 import AbsUniType       ( TyVar, TyCon, Arity(..), Class, ClassOp, TauType(..)
224                           IF_ATTACK_PRAGMAS(COMMA cmpTyVar)
225                           IF_ATTACK_PRAGMAS(COMMA cmpClass)
226                           IF_ATTACK_PRAGMAS(COMMA cmpTyCon)
227                           IF_ATTACK_PRAGMAS(COMMA cmpUniType)
228                         )
229 import BasicLit         ( BasicLit )
230 import FiniteMap        ( FiniteMap )
231 import Id               ( Id, DictVar(..), DataCon(..) )
232 import IdInfo
233 import Inst             ( Inst )
234 import Maybes           ( Maybe )
235 import Name
236 import NameTypes        ( ShortName, FullName ) -- .. for pragmas only
237 import Outputable
238 import Pretty
239 import ProtoName        ( ProtoName(..) ) -- .. for pragmas only
240 import SrcLoc           ( SrcLoc )
241 import Unique           ( Unique )
242 import Util
243 \end{code}
244
245 All we actually declare here is the top-level structure for a module.
246 \begin{code}
247 data Module name pat
248   = Module
249         FAST_STRING             -- module name
250         [IE]                    -- export list
251         [ImportedInterface name pat]
252                                 -- We snaffle interesting stuff out of the
253                                 -- imported interfaces early on, adding that
254                                 -- info to TyDecls/etc; so this list is
255                                 -- often empty, downstream.
256         [FixityDecl name]
257         [TyDecl name]
258         [DataTypeSig name]      -- user pragmas that modify TyDecls;
259                                 -- (much like "Sigs" modify value "Binds")
260         [ClassDecl name pat]
261         [InstDecl  name pat]
262         [SpecialisedInstanceSig name] -- user pragmas that modify InstDecls
263         [DefaultDecl name]
264         (Binds name pat)        -- the main stuff!
265         [Sig name]              -- "Sigs" are folded into the "Binds"
266                                 -- pretty early on, so this list is
267                                 -- often either empty or just the
268                                 -- interface signatures.
269         SrcLoc
270 \end{code}
271
272 \begin{code}
273 type ProtoNameModule    = Module ProtoName ProtoNamePat
274 type RenamedModule      = Module Name      RenamedPat
275 type TypecheckedModule  = Module Id        TypecheckedPat
276 \end{code}
277
278 \begin{code}
279 instance (NamedThing name, Outputable name, NamedThing pat, Outputable pat) =>
280         Outputable (Module name pat) where
281
282     ppr sty (Module name exports imports fixities
283                       typedecls typesigs classdecls instdecls instsigs
284                       defdecls binds sigs src_loc)
285       = ppAboves [
286             ifPprShowAll sty (ppr sty src_loc),
287             if (null exports)
288             then (ppCat [ppPStr SLIT("module"), ppPStr name, ppPStr SLIT("where")])
289             else (ppAboves [
290                     ppCat [ppPStr SLIT("module"), ppPStr name, ppLparen],
291                     ppNest 8 (interpp'SP sty exports),
292                     ppNest 4 (ppPStr SLIT(") where"))
293                   ]),
294             ppr sty imports,    ppr sty fixities,
295             ppr sty typedecls,  ppr sty typesigs,
296             ppr sty classdecls,
297             ppr sty instdecls,  ppr sty instsigs,
298             ppr sty defdecls,
299             ppr sty binds,      ppr sty sigs
300         ]
301 \end{code}