2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[HsDecls]{Abstract syntax: global declarations}
6 Definitions for: @FixityDecl@, @TyDecl@ and @ConDecl@, @ClassDecl@,
7 @InstDecl@, @DefaultDecl@.
10 #include "HsVersions.h"
17 import HsBinds ( HsBinds, MonoBinds, Sig, nullMonoBinds )
18 import HsPragmas ( DataPragmas, ClassPragmas,
19 InstancePragmas, ClassOpPragmas
23 import SpecEnv ( SpecEnv )
24 import HsCore ( UfExpr )
25 import HsBasic ( Fixity )
28 import Name ( pprSym, pprNonSym, getOccName, OccName )
29 import Outputable ( interppSP, interpp'SP,
30 Outputable(..){-instance * []-}
33 import SrcLoc ( SrcLoc )
34 import PprStyle ( PprStyle(..) )
38 %************************************************************************
40 \subsection[HsDecl]{Declarations}
42 %************************************************************************
45 data HsDecl tyvar uvar name pat
47 | ClD (ClassDecl tyvar uvar name pat)
48 | InstD (InstDecl tyvar uvar name pat)
49 | DefD (DefaultDecl name)
50 | ValD (HsBinds tyvar uvar name pat)
51 | SigD (IfaceSig name)
55 hsDeclName (TyD (TyData _ name _ _ _ _ _)) = name
56 hsDeclName (TyD (TyNew _ name _ _ _ _ _)) = name
57 hsDeclName (TyD (TySynonym name _ _ _)) = name
58 hsDeclName (ClD (ClassDecl _ name _ _ _ _ _)) = name
59 hsDeclName (SigD (IfaceSig name _ _ _)) = name
60 -- Others don't make sense
64 instance (NamedThing name, Outputable name, Outputable pat,
65 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
66 => Outputable (HsDecl tyvar uvar name pat) where
68 ppr sty (TyD td) = ppr sty td
69 ppr sty (ClD cd) = ppr sty cd
70 ppr sty (SigD sig) = ppr sty sig
71 ppr sty (ValD binds) = ppr sty binds
72 ppr sty (DefD def) = ppr sty def
73 ppr sty (InstD inst) = ppr sty inst
77 %************************************************************************
79 \subsection[FixityDecl]{A fixity declaration}
81 %************************************************************************
84 data FixityDecl name = FixityDecl name Fixity SrcLoc
86 instance Outputable name => Outputable (FixityDecl name) where
87 ppr sty (FixityDecl name fixity loc) = ppSep [ppr sty fixity, ppr sty name]
91 %************************************************************************
93 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
95 %************************************************************************
99 = TyData (Context name) -- context
100 name -- type constructor
101 [HsTyVar name] -- type variables
102 [ConDecl name] -- data constructors (empty if abstract)
103 (Maybe [name]) -- derivings; Nothing => not specified
104 -- (i.e., derive default); Just [] => derive
105 -- *nothing*; Just <list> => as you would
110 | TyNew (Context name) -- context
111 name -- type constructor
112 [HsTyVar name] -- type variables
113 (ConDecl name) -- data constructor
114 (Maybe [name]) -- derivings; as above
118 | TySynonym name -- type constructor
119 [HsTyVar name] -- type variables
120 (HsType name) -- synonym expansion
126 instance (NamedThing name, Outputable name)
127 => Outputable (TyDecl name) where
129 ppr sty (TySynonym tycon tyvars mono_ty src_loc)
130 = ppHang (pp_decl_head sty SLIT("type") ppNil tycon tyvars)
133 ppr sty (TyData context tycon tyvars condecls derivings pragmas src_loc)
135 (pp_decl_head sty SLIT("data") (pp_context_and_arrow sty context) tycon tyvars)
136 (pp_condecls sty condecls)
139 ppr sty (TyNew context tycon tyvars condecl derivings pragmas src_loc)
141 (pp_decl_head sty SLIT("newtype") (pp_context_and_arrow sty context) tycon tyvars)
145 pp_decl_head sty str pp_context tycon tyvars
146 = ppCat [ppPStr str, pp_context, ppr sty (getOccName tycon),
147 interppSP sty tyvars, ppPStr SLIT("=")]
149 pp_condecls sty [] = ppNil -- Curious!
150 pp_condecls sty (c:cs)
151 = ppSep (ppr sty c : map (\ c -> ppBeside (ppStr "| ") (ppr sty c)) cs)
153 pp_tydecl sty pp_head pp_decl_rhs derivings
154 = ppHang pp_head 4 (ppSep [
156 case (derivings, sty) of
158 (_,PprInterface) -> ppNil -- No derivings in interfaces
159 (Just ds,_) -> ppCat [ppPStr SLIT("deriving"), ppParens (interpp'SP sty ds)]
162 pp_context_and_arrow :: Outputable name => PprStyle -> Context name -> Pretty
163 pp_context_and_arrow sty [] = ppNil
164 pp_context_and_arrow sty theta = ppCat [pprContext sty theta, ppPStr SLIT("=>")]
167 A type for recording what types a datatype should be specialised to.
168 It's called a ``Sig'' because it's sort of like a ``type signature''
169 for an datatype declaration.
172 data SpecDataSig name
173 = SpecDataSig name -- tycon to specialise
177 instance (NamedThing name, Outputable name)
178 => Outputable (SpecDataSig name) where
180 ppr sty (SpecDataSig tycon ty _)
181 = ppCat [ppStr "{-# SPECIALIZE data", ppr sty ty, ppStr "#-}"]
184 %************************************************************************
186 \subsection[ConDecl]{A data-constructor declaration}
188 %************************************************************************
192 = ConDecl name -- prefix-style con decl
196 | ConOpDecl (BangType name) -- infix-style con decl
202 [([name], BangType name)] -- list of "fields"
205 | NewConDecl name -- newtype con decl
210 = Banged (HsType name) -- HsType: to allow Haskell extensions
211 | Unbanged (HsType name) -- (MonoType only needed for straight Haskell)
215 instance (NamedThing name, Outputable name) => Outputable (ConDecl name) where
217 ppr sty (ConDecl con tys _)
218 = ppCat [ppr sty (getOccName con), ppInterleave ppNil (map (ppr_bang sty) tys)]
220 -- We print ConOpDecls in prefix form in interface files
221 ppr PprInterface (ConOpDecl ty1 op ty2 _)
222 = ppCat [ppr PprInterface (getOccName op), ppr_bang PprInterface ty1, ppr_bang PprInterface ty2]
223 ppr sty (ConOpDecl ty1 op ty2 _)
224 = ppCat [ppr_bang sty ty1, ppr sty (getOccName op), ppr_bang sty ty2]
226 ppr sty (NewConDecl con ty _)
227 = ppCat [ppr sty (getOccName con), pprParendHsType sty ty]
228 ppr sty (RecConDecl con fields _)
229 = ppCat [ppr sty (getOccName con),
230 ppCurlies (ppInterleave pp'SP (map pp_field fields))
233 pp_field (ns, ty) = ppCat [ppCat (map (ppr sty . getOccName) ns),
234 ppPStr SLIT("::"), ppr_bang sty ty]
236 ppr_bang sty (Banged ty) = ppBeside (ppStr "! ") (pprParendHsType sty ty)
237 -- The extra space helps the lexical analyser that lexes
238 -- interface files; it doesn't make the rigid operator/identifier
239 -- distinction, so "!a" is a valid identifier so far as it is concerned
240 ppr_bang sty (Unbanged ty) = pprParendHsType sty ty
243 %************************************************************************
245 \subsection[ClassDecl]{A class declaration}
247 %************************************************************************
250 data ClassDecl tyvar uvar name pat
251 = ClassDecl (Context name) -- context...
252 name -- name of the class
253 (HsTyVar name) -- the class type variable
254 [Sig name] -- methods' signatures
255 (MonoBinds tyvar uvar name pat) -- default methods
261 instance (NamedThing name, Outputable name, Outputable pat,
262 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
263 => Outputable (ClassDecl tyvar uvar name pat) where
265 ppr sty (ClassDecl context clas tyvar sigs methods pragmas src_loc)
266 | null sigs -- No "where" part
269 | iface_style -- All on one line (for now at least)
270 = ppCat [top_matter, ppStr "where",
271 ppCurlies (ppInterleave (ppPStr SLIT("; ")) pp_sigs)]
273 | otherwise -- Laid out
274 = ppSep [ppCat [top_matter, ppStr "where {"],
275 ppNest 4 ((ppIntersperse ppSemi pp_sigs `ppAbove` pp_methods)
276 `ppBeside` ppStr "}")]
278 top_matter = ppCat [ppStr "class", pp_context_and_arrow sty context,
279 ppr sty (getOccName clas), ppr sty tyvar]
280 pp_sigs = map (ppr sty) sigs
281 pp_methods = ppr sty methods
282 iface_style = case sty of {PprInterface -> True; other -> False}
285 %************************************************************************
287 \subsection[InstDecl]{An instance declaration (also, @SpecInstSig@)}
289 %************************************************************************
292 data InstDecl tyvar uvar name pat
293 = InstDecl (HsType name) -- Context => Class Instance-type
294 -- Using a polytype means that the renamer conveniently
295 -- figures out the quantified type variables for us.
297 (MonoBinds tyvar uvar name pat)
299 [Sig name] -- User-supplied pragmatic info
301 (Maybe name) -- Name for the dictionary function
307 instance (NamedThing name, Outputable name, Outputable pat,
308 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
309 => Outputable (InstDecl tyvar uvar name pat) where
311 ppr sty (InstDecl inst_ty binds uprags dfun_name src_loc)
312 | case sty of { PprInterface -> True; other -> False} ||
313 nullMonoBinds binds && null uprags
314 = ppCat [ppStr "instance", ppr sty inst_ty]
317 = ppAboves [ppCat [ppStr "instance", ppr sty inst_ty, ppStr "where"],
318 ppNest 4 (ppr sty uprags),
319 ppNest 4 (ppr sty binds) ]
322 A type for recording what instances the user wants to specialise;
323 called a ``Sig'' because it's sort of like a ``type signature'' for an
326 data SpecInstSig name
327 = SpecInstSig name -- class
328 (HsType name) -- type to specialise to
331 instance (NamedThing name, Outputable name)
332 => Outputable (SpecInstSig name) where
334 ppr sty (SpecInstSig clas ty _)
335 = ppCat [ppStr "{-# SPECIALIZE instance", ppr sty clas, ppr sty ty, ppStr "#-}"]
338 %************************************************************************
340 \subsection[DefaultDecl]{A @default@ declaration}
342 %************************************************************************
344 There can only be one default declaration per module, but it is hard
345 for the parser to check that; we pass them all through in the abstract
346 syntax, and that restriction must be checked in the front end.
349 data DefaultDecl name
350 = DefaultDecl [HsType name]
353 instance (NamedThing name, Outputable name)
354 => Outputable (DefaultDecl name) where
356 ppr sty (DefaultDecl tys src_loc)
357 = ppBeside (ppPStr SLIT("default ")) (ppParens (interpp'SP sty tys))
360 %************************************************************************
362 \subsection{Signatures in interface files}
364 %************************************************************************
373 instance (NamedThing name, Outputable name) => Outputable (IfaceSig name) where
374 ppr sty (IfaceSig var ty _ _)
375 = ppHang (ppCat [ppr sty (getOccName var), ppPStr SLIT("::")])
380 | HsStrictness (StrictnessInfo name)
381 | HsUnfold (UfExpr name)
382 | HsUpdate UpdateInfo
383 | HsDeforest DeforestInfo
384 | HsArgUsage ArgUsageInfo
385 | HsFBType FBTypeInfo
386 -- ToDo: specialisations