2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsBinds]{Abstract syntax: top-level bindings and signatures}
6 Datatype for: @BindGroup@, @Bind@, @Sig@, @Bind@.
11 #include "HsVersions.h"
13 import {-# SOURCE #-} HsExpr ( HsExpr, pprExpr, LHsExpr,
14 MatchGroup, pprFunBind,
16 import {-# SOURCE #-} HsPat ( LPat )
18 import HsTypes ( LHsType, PostTcType )
20 import NameSet ( NameSet, elemNameSet )
21 import BasicTypes ( IPName, RecFlag(..), Activation(..), Fixity )
23 import SrcLoc ( Located(..), SrcSpan, unLoc )
24 import Util ( sortLe )
25 import Var ( TyVar, DictId, Id )
26 import Bag ( Bag, emptyBag, isEmptyBag, bagToList, unionBags, unionManyBags )
29 %************************************************************************
31 \subsection{Bindings: @BindGroup@}
33 %************************************************************************
35 Global bindings (where clauses)
38 data HsLocalBinds id -- Bindings in a 'let' expression
39 -- or a 'where' clause
40 = HsValBinds (HsValBinds id)
41 | HsIPBinds (HsIPBinds id)
44 data HsValBinds id -- Value bindings (not implicit parameters)
45 = ValBindsIn -- Before typechecking
46 (LHsBinds id) [LSig id] -- Not dependency analysed
47 -- Recursive by default
49 | ValBindsOut -- After renaming
50 [(RecFlag, LHsBinds id)] -- Dependency analysed
53 type LHsBinds id = Bag (LHsBind id)
54 type DictBinds id = LHsBinds id -- Used for dictionary or method bindings
55 type LHsBind id = Located (HsBind id)
58 = FunBind (Located id)
59 -- Used for both functions f x = e
60 -- and variables f = \x -> e
61 -- Reason: the Match stuff lets us have an optional
62 -- result type sig f :: a->a = ...mentions a...
64 -- This also means that instance decls can only have
65 -- FunBinds, so if you change this, you'll need to
66 -- change e.g. rnMethodBinds
67 Bool -- True => infix declaration
69 NameSet -- After the renamer, this contains a superset of the
70 -- Names of the other binders in this binding group that
71 -- are free in the RHS of the defn
72 -- Before renaming, and after typechecking,
73 -- the field is unused; it's just an error thunk
75 | PatBind (LPat id) -- The pattern is never a simple variable;
76 -- That case is done by FunBind
78 PostTcType -- Type of the GRHSs
79 NameSet -- Same as for FunBind
81 | VarBind id (Located (HsExpr id)) -- Dictionary binding and suchlike
82 -- All VarBinds are introduced by the type checker
83 -- Located only for consistency
85 | AbsBinds -- Binds abstraction; TRANSLATION
86 [TyVar] -- Type variables
88 [([TyVar], id, id, [Prag])] -- (tvs, poly_id, mono_id, prags)
89 (LHsBinds id) -- The dictionary bindings and typechecked user bindings
90 -- mixed up together; you can tell the dict bindings because
91 -- they are all VarBinds
93 -- Consider (AbsBinds tvs ds [(ftvs, poly_f, mono_f) binds]
95 -- Creates bindings for (polymorphic, overloaded) poly_f
96 -- in terms of monomorphic, non-overloaded mono_f
99 -- 1. 'binds' binds mono_f
100 -- 2. ftvs is a subset of tvs
101 -- 3. ftvs includes all tyvars free in ds
103 -- See section 9 of static semantics paper for more details.
104 -- (You can get a PhD for explaining the True Meaning
105 -- of this last construct.)
107 placeHolderNames :: NameSet
108 -- Used for the NameSet in FunBind and PatBind prior to the renamer
109 placeHolderNames = panic "placeHolderNames"
112 instance OutputableBndr id => Outputable (HsLocalBinds id) where
113 ppr (HsValBinds bs) = ppr bs
114 ppr (HsIPBinds bs) = ppr bs
115 ppr EmptyLocalBinds = empty
117 instance OutputableBndr id => Outputable (HsValBinds id) where
118 ppr (ValBindsIn binds sigs)
119 = pprValBindsForUser binds sigs
121 ppr (ValBindsOut sccs sigs)
122 = getPprStyle $ \ sty ->
123 if debugStyle sty then -- Print with sccs showing
124 vcat (map ppr sigs) $$ vcat (map ppr_scc sccs)
126 pprValBindsForUser (unionManyBags (map snd sccs)) sigs
128 ppr_scc (rec_flag, binds) = pp_rec rec_flag <+> pprLHsBinds binds
129 pp_rec Recursive = ptext SLIT("rec")
130 pp_rec NonRecursive = ptext SLIT("nonrec")
132 -- *not* pprLHsBinds because we don't want braces; 'let' and
133 -- 'where' include a list of HsBindGroups and we don't want
134 -- several groups of bindings each with braces around.
135 -- Sort by location before printing
136 pprValBindsForUser binds sigs
137 = vcat (map snd (sort_by_loc decls))
140 decls :: [(SrcSpan, SDoc)]
141 decls = [(loc, ppr sig) | L loc sig <- sigs] ++
142 [(loc, ppr bind) | L loc bind <- bagToList binds]
144 sort_by_loc decls = sortLe (\(l1,_) (l2,_) -> l1 <= l2) decls
146 pprLHsBinds :: OutputableBndr id => LHsBinds id -> SDoc
148 | isEmptyLHsBinds binds = empty
149 | otherwise = lbrace <+> vcat (map ppr (bagToList binds)) <+> rbrace
152 emptyLocalBinds :: HsLocalBinds a
153 emptyLocalBinds = EmptyLocalBinds
155 isEmptyLocalBinds :: HsLocalBinds a -> Bool
156 isEmptyLocalBinds (HsValBinds ds) = isEmptyValBinds ds
157 isEmptyLocalBinds (HsIPBinds ds) = isEmptyIPBinds ds
158 isEmptyLocalBinds EmptyLocalBinds = True
160 isEmptyValBinds :: HsValBinds a -> Bool
161 isEmptyValBinds (ValBindsIn ds sigs) = isEmptyLHsBinds ds && null sigs
162 isEmptyValBinds (ValBindsOut ds sigs) = null ds && null sigs
164 emptyValBindsIn, emptyValBindsOut :: HsValBinds a
165 emptyValBindsIn = ValBindsIn emptyBag []
166 emptyValBindsOut = ValBindsOut [] []
168 emptyLHsBinds :: LHsBinds id
169 emptyLHsBinds = emptyBag
171 isEmptyLHsBinds :: LHsBinds id -> Bool
172 isEmptyLHsBinds = isEmptyBag
175 plusHsValBinds :: HsValBinds a -> HsValBinds a -> HsValBinds a
176 plusHsValBinds (ValBindsIn ds1 sigs1) (ValBindsIn ds2 sigs2)
177 = ValBindsIn (ds1 `unionBags` ds2) (sigs1 ++ sigs2)
178 plusHsValBinds (ValBindsOut ds1 sigs1) (ValBindsOut ds2 sigs2)
179 = ValBindsOut (ds1 ++ ds2) (sigs1 ++ sigs2)
191 f1p = /\ tvs -> \ [d1,d2] -> letrec DBINDS and BIND
194 gp = ...same again, with gm instead of fm
196 This is a pretty bad translation, because it duplicates all the bindings.
197 So the desugarer tries to do a better job:
199 fp = /\ [a,b] -> \ [d1,d2] -> case tp [a,b] [d1,d2] of
203 tp = /\ [a,b] -> \ [d1,d2] -> letrec DBINDS and BIND
207 instance OutputableBndr id => Outputable (HsBind id) where
208 ppr mbind = ppr_monobind mbind
210 ppr_monobind :: OutputableBndr id => HsBind id -> SDoc
212 ppr_monobind (PatBind pat grhss _ _) = pprPatBind pat grhss
213 ppr_monobind (VarBind var rhs) = ppr var <+> equals <+> pprExpr (unLoc rhs)
214 ppr_monobind (FunBind fun inf matches _) = pprFunBind (unLoc fun) matches
215 -- ToDo: print infix if appropriate
217 ppr_monobind (AbsBinds tyvars dictvars exports val_binds)
218 = sep [ptext SLIT("AbsBinds"),
219 brackets (interpp'SP tyvars),
220 brackets (interpp'SP dictvars),
221 brackets (sep (punctuate comma (map ppr_exp exports)))]
223 nest 2 ( vcat [pprBndr LetBind x | (_,x,_,_) <- exports]
224 -- Print type signatures
225 $$ pprLHsBinds val_binds )
227 ppr_exp (tvs, gbl, lcl, prags)
228 = vcat [ppr gbl <+> ptext SLIT("<=") <+> ppr tvs <+> ppr lcl,
229 nest 2 (vcat (map (pprPrag gbl) prags))]
232 %************************************************************************
234 Implicit parameter bindings
236 %************************************************************************
242 (DictBinds id) -- Only in typechecker output; binds
243 -- uses of the implicit parameters
245 isEmptyIPBinds :: HsIPBinds id -> Bool
246 isEmptyIPBinds (IPBinds is ds) = null is && isEmptyBag ds
248 type LIPBind id = Located (IPBind id)
250 -- | Implicit parameter bindings.
256 instance (OutputableBndr id) => Outputable (HsIPBinds id) where
257 ppr (IPBinds bs ds) = vcat (map ppr bs)
260 instance (OutputableBndr id) => Outputable (IPBind id) where
261 ppr (IPBind id rhs) = pprBndr LetBind id <+> equals <+> pprExpr (unLoc rhs)
265 %************************************************************************
267 \subsection{@Sig@: type signatures and value-modifying user pragmas}
269 %************************************************************************
271 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
272 ``specialise this function to these four types...'') in with type
273 signatures. Then all the machinery to move them into place, etc.,
277 type LSig name = Located (Sig name)
280 = Sig (Located name) -- a bog-std type signature
283 | SpecSig (Located name) -- specialise a function or datatype ...
284 (LHsType name) -- ... to these types
286 | InlineSig Bool -- True <=> INLINE f, False <=> NOINLINE f
287 (Located name) -- Function name
288 Activation -- When inlining is *active*
290 | SpecInstSig (LHsType name) -- (Class tys); should be a specialisation of the
291 -- current instance decl
293 | FixSig (FixitySig name) -- Fixity declaration
295 type LFixitySig name = Located (FixitySig name)
296 data FixitySig name = FixitySig (Located name) Fixity
298 -- A Prag conveys pragmas from the type checker to the desugarer
301 Bool -- True <=> INLINE, False <=> NOINLINE
305 (HsExpr Id) -- An expression, of the given specialised type, which
306 PostTcType -- specialises the polymorphic function
307 [Id] -- Dicts mentioned free in the expression
309 isInlinePrag (InlinePrag _ _) = True
310 isInlinePrag prag = False
312 isSpecPrag (SpecPrag _ _ _) = True
313 isSpecPrag prag = False
317 okBindSig :: NameSet -> LSig Name -> Bool
318 okBindSig ns sig = sigForThisGroup ns sig
320 okHsBootSig :: LSig Name -> Bool
321 okHsBootSig (L _ (Sig _ _)) = True
322 okHsBootSig (L _ (FixSig _)) = True
323 okHsBootSig sig = False
325 okClsDclSig :: LSig Name -> Bool
326 okClsDclSig (L _ (SpecInstSig _)) = False
327 okClsDclSig sig = True -- All others OK
329 okInstDclSig :: NameSet -> LSig Name -> Bool
330 okInstDclSig ns lsig@(L _ sig) = ok ns sig
332 ok ns (Sig _ _) = False
333 ok ns (FixSig _) = False
334 ok ns (SpecInstSig _) = True
335 ok ns sig = sigForThisGroup ns lsig
337 sigForThisGroup :: NameSet -> LSig Name -> Bool
338 sigForThisGroup ns sig
339 = case sigName sig of
341 Just n -> n `elemNameSet` ns
343 sigName :: LSig name -> Maybe name
344 sigName (L _ sig) = f sig
346 f (Sig n _) = Just (unLoc n)
347 f (SpecSig n _) = Just (unLoc n)
348 f (InlineSig _ n _) = Just (unLoc n)
349 f (FixSig (FixitySig n _)) = Just (unLoc n)
352 isFixityLSig :: LSig name -> Bool
353 isFixityLSig (L _ (FixSig _)) = True
354 isFixityLSig _ = False
356 isVanillaLSig :: LSig name -> Bool
357 isVanillaLSig (L _(Sig name _)) = True
358 isVanillaLSig sig = False
360 isSpecLSig :: LSig name -> Bool
361 isSpecLSig (L _(SpecSig name _)) = True
362 isSpecLSig sig = False
364 isSpecInstLSig (L _ (SpecInstSig _)) = True
365 isSpecInstLSig sig = False
367 isPragLSig :: LSig name -> Bool
368 -- Identifies pragmas
369 isPragLSig (L _ (SpecSig _ _)) = True
370 isPragLSig (L _ (InlineSig _ _ _)) = True
371 isPragLSig other = False
373 hsSigDoc (Sig _ _) = ptext SLIT("type signature")
374 hsSigDoc (SpecSig _ _) = ptext SLIT("SPECIALISE pragma")
375 hsSigDoc (InlineSig True _ _) = ptext SLIT("INLINE pragma")
376 hsSigDoc (InlineSig False _ _) = ptext SLIT("NOINLINE pragma")
377 hsSigDoc (SpecInstSig _) = ptext SLIT("SPECIALISE instance pragma")
378 hsSigDoc (FixSig (FixitySig _ _)) = ptext SLIT("fixity declaration")
381 Signature equality is used when checking for duplicate signatures
384 eqHsSig :: LSig Name -> LSig Name -> Bool
385 eqHsSig (L _ (FixSig (FixitySig n1 _))) (L _ (FixSig (FixitySig n2 _))) = unLoc n1 == unLoc n2
386 eqHsSig (L _ (Sig n1 _)) (L _ (Sig n2 _)) = unLoc n1 == unLoc n2
387 eqHsSig (L _ (InlineSig b1 n1 _)) (L _ (InlineSig b2 n2 _)) = b1 == b2 && unLoc n1 == unLoc n2
388 -- For specialisations, we don't have equality over
389 -- HsType, so it's not convenient to spot duplicate
390 -- specialisations here. Check for this later, when we're in Type land
391 eqHsSig _other1 _other2 = False
395 instance (OutputableBndr name) => Outputable (Sig name) where
396 ppr sig = ppr_sig sig
398 ppr_sig :: OutputableBndr name => Sig name -> SDoc
399 ppr_sig (Sig var ty) = pprVarSig (unLoc var) ty
400 ppr_sig (FixSig fix_sig) = ppr fix_sig
401 ppr_sig (SpecSig var ty) = pragBrackets (pprSpec var ty)
402 ppr_sig (InlineSig inl var phase) = pragBrackets (pprInline var inl phase)
403 ppr_sig (SpecInstSig ty) = pragBrackets (ptext SLIT("SPECIALIZE instance") <+> ppr ty)
405 instance Outputable name => Outputable (FixitySig name) where
406 ppr (FixitySig name fixity) = sep [ppr fixity, ppr name]
408 pragBrackets :: SDoc -> SDoc
409 pragBrackets doc = ptext SLIT("{-#") <+> doc <+> ptext SLIT("#-}")
411 pprInline :: Outputable id => id -> Bool -> Activation -> SDoc
412 pprInline var True phase = hsep [ptext SLIT("INLINE"), ppr phase, ppr var]
413 pprInline var False phase = hsep [ptext SLIT("NOINLINE"), ppr phase, ppr var]
415 pprVarSig :: (Outputable id, Outputable ty) => id -> ty -> SDoc
416 pprVarSig var ty = sep [ppr var <+> dcolon, nest 2 (ppr ty)]
418 pprSpec :: (Outputable id, Outputable ty) => id -> ty -> SDoc
419 pprSpec var ty = sep [ptext SLIT("SPECIALIZE") <+> pprVarSig var ty]
421 pprPrag :: Outputable id => id -> Prag -> SDoc
422 pprPrag var (InlinePrag inl act) = pprInline var inl act
423 pprPrag var (SpecPrag expr ty _) = pprSpec var ty