2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[HsBinds]{Abstract syntax: top-level bindings and signatures}
6 Datatype for: @HsBinds@, @Bind@, @Sig@, @MonoBinds@.
11 #include "HsVersions.h"
13 import {-# SOURCE #-} HsExpr ( pprExpr, HsExpr )
14 import {-# SOURCE #-} HsMatches ( pprMatches, Match, pprGRHSsAndBinds, GRHSsAndBinds )
17 import HsTypes ( HsType )
18 import CoreSyn ( CoreExpr )
19 import PprCore () -- Instances for Outputable
22 import Id ( Id, GenId )
23 import Name ( OccName, NamedThing(..) )
24 import BasicTypes ( RecFlag(..) )
27 import SrcLoc ( SrcLoc )
28 import Type ( GenType )
29 import TyVar ( GenTyVar )
32 %************************************************************************
34 \subsection{Bindings: @HsBinds@}
36 %************************************************************************
38 The following syntax may produce new syntax which is not part of the input,
39 and which is instead a translation of the input to the typechecker.
40 Syntax translations are marked TRANSLATION in comments. New empty
41 productions are useful in development but may not appear in the final
44 Collections of bindings, created by dependency analysis and translation:
47 data HsBinds flexi id pat -- binders and bindees
50 | ThenBinds (HsBinds flexi id pat)
51 (HsBinds flexi id pat)
53 | MonoBind (MonoBinds flexi id pat)
54 [Sig id] -- Empty on typechecker output
59 nullBinds :: HsBinds flexi id pat -> Bool
61 nullBinds EmptyBinds = True
62 nullBinds (ThenBinds b1 b2) = nullBinds b1 && nullBinds b2
63 nullBinds (MonoBind b _ _) = nullMonoBinds b
67 instance (Outputable pat, NamedThing id, Outputable id) =>
68 Outputable (HsBinds flexi id pat) where
69 ppr binds = ppr_binds binds
71 ppr_binds EmptyBinds = empty
72 ppr_binds (ThenBinds binds1 binds2)
73 = ($$) (ppr_binds binds1) (ppr_binds binds2)
74 ppr_binds (MonoBind bind sigs is_rec)
75 = vcat [ifNotPprForUser (ptext rec_str),
80 rec_str = case is_rec of
81 Recursive -> SLIT("{- rec -}")
82 NonRecursive -> SLIT("{- nonrec -}")
85 %************************************************************************
87 \subsection{Bindings: @MonoBinds@}
89 %************************************************************************
91 Global bindings (where clauses)
94 data MonoBinds flexi id pat
97 | AndMonoBinds (MonoBinds flexi id pat)
98 (MonoBinds flexi id pat)
101 (GRHSsAndBinds flexi id pat)
105 Bool -- True => infix declaration
106 [Match flexi id pat] -- must have at least one Match
109 | VarMonoBind id -- TRANSLATION
110 (HsExpr flexi id pat)
112 | CoreMonoBind id -- TRANSLATION
113 CoreExpr -- No zonking; this is a final CoreExpr with Ids and Types!
115 | AbsBinds -- Binds abstraction; TRANSLATION
116 [GenTyVar flexi] -- Type variables
118 [([GenTyVar flexi], id, id)] -- (type variables, polymorphic, momonmorphic) triples
119 (MonoBinds flexi id pat) -- The "business end"
121 -- Creates bindings for *new* (polymorphic, overloaded) locals
122 -- in terms of *old* (monomorphic, non-overloaded) ones.
124 -- See section 9 of static semantics paper for more details.
125 -- (You can get a PhD for explaining the True Meaning
126 -- of this last construct.)
138 f1p = /\ tvs -> \ [d1,d2] -> letrec DBINDS and BIND
141 gp = ...same again, with gm instead of fm
143 This is a pretty bad translation, because it duplicates all the bindings.
144 So the desugarer tries to do a better job:
146 fp = /\ [a,b] -> \ [d1,d2] -> case tp [a,b] [d1,d2] of
150 p = /\ [a,b] -> \ [d1,d2] -> letrec DBINDS and BIND
154 nullMonoBinds :: MonoBinds flexi id pat -> Bool
156 nullMonoBinds EmptyMonoBinds = True
157 nullMonoBinds (AndMonoBinds bs1 bs2) = nullMonoBinds bs1 && nullMonoBinds bs2
158 nullMonoBinds other_monobind = False
160 andMonoBinds :: [MonoBinds flexi id pat] -> MonoBinds flexi id pat
161 andMonoBinds binds = foldr AndMonoBinds EmptyMonoBinds binds
165 instance (NamedThing id, Outputable id, Outputable pat) =>
166 Outputable (MonoBinds flexi id pat) where
167 ppr mbind = ppr_monobind mbind
170 ppr_monobind EmptyMonoBinds = empty
171 ppr_monobind (AndMonoBinds binds1 binds2)
172 = ($$) (ppr_monobind binds1) (ppr_monobind binds2)
174 ppr_monobind (PatMonoBind pat grhss_n_binds locn)
175 = sep [ppr pat, nest 4 (pprGRHSsAndBinds False grhss_n_binds)]
177 ppr_monobind (FunMonoBind fun inf matches locn)
178 = pprMatches (False, ppr fun) matches
179 -- ToDo: print infix if appropriate
181 ppr_monobind (VarMonoBind name expr)
182 = sep [ppr name <+> equals, nest 4 (pprExpr expr)]
184 ppr_monobind (CoreMonoBind name expr)
185 = sep [ppr name <+> equals, nest 4 (ppr expr)]
187 ppr_monobind (AbsBinds tyvars dictvars exports val_binds)
188 = ($$) (sep [ptext SLIT("AbsBinds"),
189 brackets (interpp'SP tyvars),
190 brackets (interpp'SP dictvars),
191 brackets (interpp'SP exports)])
192 (nest 4 (ppr val_binds))
195 %************************************************************************
197 \subsection{@Sig@: type signatures and value-modifying user pragmas}
199 %************************************************************************
201 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
202 ``specialise this function to these four types...'') in with type
203 signatures. Then all the machinery to move them into place, etc.,
208 = Sig name -- a bog-std type signature
212 | ClassOpSig name -- Selector name
213 (Maybe name) -- Default-method name (if any)
217 | SpecSig name -- specialise a function or datatype ...
218 (HsType name) -- ... to these types
219 (Maybe name) -- ... maybe using this as the code for it
222 | InlineSig name -- INLINE f
225 | SpecInstSig (HsType name) -- (Class tys); should be a specialisation of the
226 -- current instance decl
231 sigsForMe :: (name -> Bool) -> [Sig name] -> [Sig name]
233 = filter sig_for_me sigs
235 sig_for_me (Sig n _ _) = f n
236 sig_for_me (ClassOpSig n _ _ _) = f n
237 sig_for_me (SpecSig n _ _ _) = f n
238 sig_for_me (InlineSig n _) = f n
239 sig_for_me (SpecInstSig _ _) = False
243 instance (NamedThing name, Outputable name) => Outputable (Sig name) where
244 ppr sig = ppr_sig sig
247 ppr_sig (Sig var ty _)
248 = sep [ppr var <+> ptext SLIT("::"),
251 ppr_sig (ClassOpSig var _ ty _)
252 = sep [ppr (getOccName var) <+> ptext SLIT("::"),
255 ppr_sig (SpecSig var ty using _)
256 = sep [ hsep [text "{-# SPECIALIZE", ppr var, ptext SLIT("::")],
257 nest 4 (hsep [ppr ty, pp_using using, text "#-}"])
260 pp_using Nothing = empty
261 pp_using (Just me) = hsep [char '=', ppr me]
263 ppr_sig (InlineSig var _)
264 = hsep [text "{-# INLINE", ppr var, text "#-}"]
266 ppr_sig (SpecInstSig ty _)
267 = hsep [text "{-# SPECIALIZE instance", ppr ty, text "#-}"]