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@.
9 #include "HsVersions.h"
16 IMPORT_DELOOPER(HsLoop) ( pprMatches, pprGRHSsAndBinds,
19 import HsPragmas ( GenPragmas, ClassOpPragmas )
20 import HsTypes ( HsType )
21 import CoreSyn ( SYN_IE(CoreExpr) )
24 import Id ( SYN_IE(DictVar), SYN_IE(Id), GenId )
25 import Name ( getOccName, OccName, NamedThing(..) )
26 import Outputable ( interpp'SP, ifnotPprForUser, pprQuote,
27 Outputable(..){-instance * (,)-}
29 import PprCore --( GenCoreExpr {- instance Outputable -} )
30 import PprType ( GenTyVar {- instance Outputable -} )
33 import SrcLoc ( SrcLoc{-instances-} )
34 import TyVar ( GenTyVar{-instances-} )
35 import Unique ( Unique {- instance Eq -} )
38 %************************************************************************
40 \subsection{Bindings: @HsBinds@}
42 %************************************************************************
44 The following syntax may produce new syntax which is not part of the input,
45 and which is instead a translation of the input to the typechecker.
46 Syntax translations are marked TRANSLATION in comments. New empty
47 productions are useful in development but may not appear in the final
50 Collections of bindings, created by dependency analysis and translation:
53 data HsBinds tyvar uvar id pat -- binders and bindees
56 | ThenBinds (HsBinds tyvar uvar id pat)
57 (HsBinds tyvar uvar id pat)
59 | MonoBind (MonoBinds tyvar uvar id pat)
60 [Sig id] -- Empty on typechecker output
69 nullBinds :: HsBinds tyvar uvar id pat -> Bool
71 nullBinds EmptyBinds = True
72 nullBinds (ThenBinds b1 b2) = nullBinds b1 && nullBinds b2
73 nullBinds (MonoBind b _ _) = nullMonoBinds b
77 instance (Outputable pat, NamedThing id, Outputable id,
78 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
79 Outputable (HsBinds tyvar uvar id pat) where
81 ppr sty binds = pprQuote sty (\ sty -> ppr_binds sty binds)
83 ppr_binds sty EmptyBinds = empty
84 ppr_binds sty (ThenBinds binds1 binds2)
85 = ($$) (ppr_binds sty binds1) (ppr_binds sty binds2)
86 ppr_binds sty (MonoBind bind sigs is_rec)
88 ifnotPprForUser sty (ptext rec_str),
91 else vcat (map (ppr sty) sigs),
95 rec_str | is_rec = SLIT("{- rec -}")
96 | otherwise = SLIT("{- nonrec -}")
99 %************************************************************************
101 \subsection{Bindings: @MonoBinds@}
103 %************************************************************************
105 Global bindings (where clauses)
108 data MonoBinds tyvar uvar id pat
111 | AndMonoBinds (MonoBinds tyvar uvar id pat)
112 (MonoBinds tyvar uvar id pat)
115 (GRHSsAndBinds tyvar uvar id pat)
119 Bool -- True => infix declaration
120 [Match tyvar uvar id pat] -- must have at least one Match
123 | VarMonoBind id -- TRANSLATION
124 (HsExpr tyvar uvar id pat)
126 | CoreMonoBind id -- TRANSLATION
127 CoreExpr -- No zonking; this is a final CoreExpr with Ids and Types!
129 | AbsBinds -- Binds abstraction; TRANSLATION
130 [tyvar] -- Type variables
132 [([tyvar], id, id)] -- (type variables, polymorphic, momonmorphic) triples
133 (MonoBinds tyvar uvar id pat) -- The "business end"
135 -- Creates bindings for *new* (polymorphic, overloaded) locals
136 -- in terms of *old* (monomorphic, non-overloaded) ones.
138 -- See section 9 of static semantics paper for more details.
139 -- (You can get a PhD for explaining the True Meaning
140 -- of this last construct.)
152 f1p = /\ tvs -> \ [d1,d2] -> letrec DBINDS and BIND
155 gp = ...same again, with gm instead of fm
157 This is a pretty bad translation, because it duplicates all the bindings.
158 So the desugarer tries to do a better job:
160 fp = /\ [a,b] -> \ [d1,d2] -> case tp [a,b] [d1,d2] of
164 p = /\ [a,b] -> \ [d1,d2] -> letrec DBINDS and BIND
168 nullMonoBinds :: MonoBinds tyvar uvar id pat -> Bool
170 nullMonoBinds EmptyMonoBinds = True
171 nullMonoBinds (AndMonoBinds bs1 bs2) = nullMonoBinds bs1 && nullMonoBinds bs2
172 nullMonoBinds other_monobind = False
174 andMonoBinds :: [MonoBinds tyvar uvar id pat] -> MonoBinds tyvar uvar id pat
175 andMonoBinds binds = foldr AndMonoBinds EmptyMonoBinds binds
179 instance (NamedThing id, Outputable id, Outputable pat,
180 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
181 Outputable (MonoBinds tyvar uvar id pat) where
182 ppr sty mbind = pprQuote sty (\ sty -> ppr_monobind sty mbind)
185 ppr_monobind sty EmptyMonoBinds = empty
186 ppr_monobind sty (AndMonoBinds binds1 binds2)
187 = ($$) (ppr_monobind sty binds1) (ppr_monobind sty binds2)
189 ppr_monobind sty (PatMonoBind pat grhss_n_binds locn)
190 = hang (ppr sty pat) 4 (pprGRHSsAndBinds sty False grhss_n_binds)
192 ppr_monobind sty (FunMonoBind fun inf matches locn)
193 = pprMatches sty (False, ppr sty fun) matches
194 -- ToDo: print infix if appropriate
196 ppr_monobind sty (VarMonoBind name expr)
197 = hang (hsep [ppr sty name, equals]) 4 (pprExpr sty expr)
199 ppr_monobind sty (CoreMonoBind name expr)
200 = hang (hsep [ppr sty name, equals]) 4 (ppr sty expr)
202 ppr_monobind sty (AbsBinds tyvars dictvars exports val_binds)
203 = ($$) (sep [ptext SLIT("AbsBinds"),
204 brackets (interpp'SP sty tyvars),
205 brackets (interpp'SP sty dictvars),
206 brackets (interpp'SP sty exports)])
207 (nest 4 (ppr sty val_binds))
210 %************************************************************************
212 \subsection{@Sig@: type signatures and value-modifying user pragmas}
214 %************************************************************************
216 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
217 ``specialise this function to these four types...'') in with type
218 signatures. Then all the machinery to move them into place, etc.,
223 = Sig name -- a bog-std type signature
227 | ClassOpSig name -- Selector name
228 name -- Default-method name
232 | SpecSig name -- specialise a function or datatype ...
233 (HsType name) -- ... to these types
234 (Maybe name) -- ... maybe using this as the code for it
237 | InlineSig name -- INLINE f
240 | DeforestSig name -- Deforest using this function definition
244 name -- Associate the "name"d function with
245 FAST_STRING -- the compiler-builtin unfolding (known
246 SrcLoc -- by the String name)
250 instance (NamedThing name, Outputable name) => Outputable (Sig name) where
251 ppr sty sig = pprQuote sty (\ sty -> ppr_sig sty sig)
254 ppr_sig sty (Sig var ty _)
255 = hang (hsep [ppr sty var, ptext SLIT("::")])
258 ppr_sig sty (ClassOpSig var _ ty _)
259 = hang (hsep [ppr sty (getOccName var), ptext SLIT("::")])
262 ppr_sig sty (DeforestSig var _)
263 = hang (hsep [text "{-# DEFOREST", ppr sty var])
266 ppr_sig sty (SpecSig var ty using _)
267 = hang (hsep [text "{-# SPECIALIZE", ppr sty var, ptext SLIT("::")])
268 4 (hsep [ppr sty ty, pp_using using, text "#-}"])
271 pp_using Nothing = empty
272 pp_using (Just me) = hsep [char '=', ppr sty me]
274 ppr_sig sty (InlineSig var _)
276 = hsep [text "{-# INLINE", ppr sty var, text "#-}"]
278 ppr_sig sty (MagicUnfoldingSig var str _)
279 = hsep [text "{-# MAGIC_UNFOLDING", ppr sty var, ptext str, text "#-}"]