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 HsPragmas ( GenPragmas, ClassOpPragmas )
18 import HsTypes ( HsType )
19 import CoreSyn ( CoreExpr )
20 import PprCore () -- Instances for Outputable
23 import Id ( DictVar, Id, GenId )
24 import Name ( OccName, NamedThing(..) )
25 import BasicTypes ( RecFlag(..) )
28 import SrcLoc ( SrcLoc )
29 import Type ( GenType )
30 import TyVar ( GenTyVar )
33 %************************************************************************
35 \subsection{Bindings: @HsBinds@}
37 %************************************************************************
39 The following syntax may produce new syntax which is not part of the input,
40 and which is instead a translation of the input to the typechecker.
41 Syntax translations are marked TRANSLATION in comments. New empty
42 productions are useful in development but may not appear in the final
45 Collections of bindings, created by dependency analysis and translation:
48 data HsBinds flexi id pat -- binders and bindees
51 | ThenBinds (HsBinds flexi id pat)
52 (HsBinds flexi id pat)
54 | MonoBind (MonoBinds flexi id pat)
55 [Sig id] -- Empty on typechecker output
60 nullBinds :: HsBinds flexi id pat -> Bool
62 nullBinds EmptyBinds = True
63 nullBinds (ThenBinds b1 b2) = nullBinds b1 && nullBinds b2
64 nullBinds (MonoBind b _ _) = nullMonoBinds b
68 instance (Outputable pat, NamedThing id, Outputable id) =>
69 Outputable (HsBinds flexi id pat) where
70 ppr binds = ppr_binds binds
72 ppr_binds EmptyBinds = empty
73 ppr_binds (ThenBinds binds1 binds2)
74 = ($$) (ppr_binds binds1) (ppr_binds binds2)
75 ppr_binds (MonoBind bind sigs is_rec)
76 = vcat [ifNotPprForUser (ptext rec_str),
81 rec_str = case is_rec of
82 Recursive -> SLIT("{- rec -}")
83 NonRecursive -> SLIT("{- nonrec -}")
86 %************************************************************************
88 \subsection{Bindings: @MonoBinds@}
90 %************************************************************************
92 Global bindings (where clauses)
95 data MonoBinds flexi id pat
98 | AndMonoBinds (MonoBinds flexi id pat)
99 (MonoBinds flexi id pat)
102 (GRHSsAndBinds flexi id pat)
106 Bool -- True => infix declaration
107 [Match flexi id pat] -- must have at least one Match
110 | VarMonoBind id -- TRANSLATION
111 (HsExpr flexi id pat)
113 | CoreMonoBind id -- TRANSLATION
114 CoreExpr -- No zonking; this is a final CoreExpr with Ids and Types!
116 | AbsBinds -- Binds abstraction; TRANSLATION
117 [GenTyVar flexi] -- Type variables
119 [([GenTyVar flexi], id, id)] -- (type variables, polymorphic, momonmorphic) triples
120 (MonoBinds flexi id pat) -- The "business end"
122 -- Creates bindings for *new* (polymorphic, overloaded) locals
123 -- in terms of *old* (monomorphic, non-overloaded) ones.
125 -- See section 9 of static semantics paper for more details.
126 -- (You can get a PhD for explaining the True Meaning
127 -- of this last construct.)
139 f1p = /\ tvs -> \ [d1,d2] -> letrec DBINDS and BIND
142 gp = ...same again, with gm instead of fm
144 This is a pretty bad translation, because it duplicates all the bindings.
145 So the desugarer tries to do a better job:
147 fp = /\ [a,b] -> \ [d1,d2] -> case tp [a,b] [d1,d2] of
151 p = /\ [a,b] -> \ [d1,d2] -> letrec DBINDS and BIND
155 nullMonoBinds :: MonoBinds flexi id pat -> Bool
157 nullMonoBinds EmptyMonoBinds = True
158 nullMonoBinds (AndMonoBinds bs1 bs2) = nullMonoBinds bs1 && nullMonoBinds bs2
159 nullMonoBinds other_monobind = False
161 andMonoBinds :: [MonoBinds flexi id pat] -> MonoBinds flexi id pat
162 andMonoBinds binds = foldr AndMonoBinds EmptyMonoBinds binds
166 instance (NamedThing id, Outputable id, Outputable pat) =>
167 Outputable (MonoBinds flexi id pat) where
168 ppr mbind = ppr_monobind mbind
171 ppr_monobind EmptyMonoBinds = empty
172 ppr_monobind (AndMonoBinds binds1 binds2)
173 = ($$) (ppr_monobind binds1) (ppr_monobind binds2)
175 ppr_monobind (PatMonoBind pat grhss_n_binds locn)
176 = sep [ppr pat, nest 4 (pprGRHSsAndBinds False grhss_n_binds)]
178 ppr_monobind (FunMonoBind fun inf matches locn)
179 = pprMatches (False, ppr fun) matches
180 -- ToDo: print infix if appropriate
182 ppr_monobind (VarMonoBind name expr)
183 = sep [ppr name <+> equals, nest 4 (pprExpr expr)]
185 ppr_monobind (CoreMonoBind name expr)
186 = sep [ppr name <+> equals, nest 4 (ppr expr)]
188 ppr_monobind (AbsBinds tyvars dictvars exports val_binds)
189 = ($$) (sep [ptext SLIT("AbsBinds"),
190 brackets (interpp'SP tyvars),
191 brackets (interpp'SP dictvars),
192 brackets (interpp'SP exports)])
193 (nest 4 (ppr val_binds))
196 %************************************************************************
198 \subsection{@Sig@: type signatures and value-modifying user pragmas}
200 %************************************************************************
202 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
203 ``specialise this function to these four types...'') in with type
204 signatures. Then all the machinery to move them into place, etc.,
209 = Sig name -- a bog-std type signature
213 | ClassOpSig name -- Selector name
214 (Maybe name) -- Default-method name (if any)
218 | SpecSig name -- specialise a function or datatype ...
219 (HsType name) -- ... to these types
220 (Maybe name) -- ... maybe using this as the code for it
223 | InlineSig name -- INLINE f
227 name -- Associate the "name"d function with
228 FAST_STRING -- the compiler-builtin unfolding (known
229 SrcLoc -- by the String name)
233 instance (NamedThing name, Outputable name) => Outputable (Sig name) where
234 ppr sig = ppr_sig sig
237 ppr_sig (Sig var ty _)
238 = sep [ppr var <+> ptext SLIT("::"),
241 ppr_sig (ClassOpSig var _ ty _)
242 = sep [ppr (getOccName var) <+> ptext SLIT("::"),
245 ppr_sig (SpecSig var ty using _)
246 = sep [ hsep [text "{-# SPECIALIZE", ppr var, ptext SLIT("::")],
247 nest 4 (hsep [ppr ty, pp_using using, text "#-}"])
250 pp_using Nothing = empty
251 pp_using (Just me) = hsep [char '=', ppr me]
253 ppr_sig (InlineSig var _)
254 = hsep [text "{-# INLINE", ppr var, text "#-}"]
256 ppr_sig (MagicUnfoldingSig var str _)
257 = hsep [text "{-# MAGIC_UNFOLDING", ppr var, ptext str, text "#-}"]