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 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
17 IMPORT_DELOOPER(HsLoop) ( pprMatches, pprGRHSsAndBinds,
21 import {-# SOURCE #-} HsMatches ( pprMatches, Match, pprGRHSsAndBinds, GRHSsAndBinds )
22 import {-# SOURCE #-} HsExpr ( pprExpr, HsExpr )
25 import HsPragmas ( GenPragmas, ClassOpPragmas )
26 import HsTypes ( HsType )
27 import CoreSyn ( SYN_IE(CoreExpr) )
30 import Id ( SYN_IE(DictVar), SYN_IE(Id), GenId )
31 import Name ( OccName, NamedThing(..) )
32 import Outputable ( interpp'SP, ifnotPprForUser, pprQuote,
33 Outputable(..){-instance * (,)-}
35 import PprCore --( GenCoreExpr {- instance Outputable -} )
36 import PprType ( GenTyVar {- instance Outputable -} )
39 import SrcLoc ( SrcLoc{-instances-} )
40 import TyVar ( GenTyVar{-instances-} )
41 import Unique ( Unique {- instance Eq -} )
44 %************************************************************************
46 \subsection{Bindings: @HsBinds@}
48 %************************************************************************
50 The following syntax may produce new syntax which is not part of the input,
51 and which is instead a translation of the input to the typechecker.
52 Syntax translations are marked TRANSLATION in comments. New empty
53 productions are useful in development but may not appear in the final
56 Collections of bindings, created by dependency analysis and translation:
59 data HsBinds tyvar uvar id pat -- binders and bindees
62 | ThenBinds (HsBinds tyvar uvar id pat)
63 (HsBinds tyvar uvar id pat)
65 | MonoBind (MonoBinds tyvar uvar id pat)
66 [Sig id] -- Empty on typechecker output
75 nullBinds :: HsBinds tyvar uvar id pat -> Bool
77 nullBinds EmptyBinds = True
78 nullBinds (ThenBinds b1 b2) = nullBinds b1 && nullBinds b2
79 nullBinds (MonoBind b _ _) = nullMonoBinds b
83 instance (Outputable pat, NamedThing id, Outputable id,
84 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
85 Outputable (HsBinds tyvar uvar id pat) where
87 ppr sty binds = pprQuote sty (\ sty -> ppr_binds sty binds)
89 ppr_binds sty EmptyBinds = empty
90 ppr_binds sty (ThenBinds binds1 binds2)
91 = ($$) (ppr_binds sty binds1) (ppr_binds sty binds2)
92 ppr_binds sty (MonoBind bind sigs is_rec)
94 ifnotPprForUser sty (ptext rec_str),
97 else vcat (map (ppr sty) sigs),
101 rec_str | is_rec = SLIT("{- rec -}")
102 | otherwise = SLIT("{- nonrec -}")
105 %************************************************************************
107 \subsection{Bindings: @MonoBinds@}
109 %************************************************************************
111 Global bindings (where clauses)
114 data MonoBinds tyvar uvar id pat
117 | AndMonoBinds (MonoBinds tyvar uvar id pat)
118 (MonoBinds tyvar uvar id pat)
121 (GRHSsAndBinds tyvar uvar id pat)
125 Bool -- True => infix declaration
126 [Match tyvar uvar id pat] -- must have at least one Match
129 | VarMonoBind id -- TRANSLATION
130 (HsExpr tyvar uvar id pat)
132 | CoreMonoBind id -- TRANSLATION
133 CoreExpr -- No zonking; this is a final CoreExpr with Ids and Types!
135 | AbsBinds -- Binds abstraction; TRANSLATION
136 [tyvar] -- Type variables
138 [([tyvar], id, id)] -- (type variables, polymorphic, momonmorphic) triples
139 (MonoBinds tyvar uvar id pat) -- The "business end"
141 -- Creates bindings for *new* (polymorphic, overloaded) locals
142 -- in terms of *old* (monomorphic, non-overloaded) ones.
144 -- See section 9 of static semantics paper for more details.
145 -- (You can get a PhD for explaining the True Meaning
146 -- of this last construct.)
158 f1p = /\ tvs -> \ [d1,d2] -> letrec DBINDS and BIND
161 gp = ...same again, with gm instead of fm
163 This is a pretty bad translation, because it duplicates all the bindings.
164 So the desugarer tries to do a better job:
166 fp = /\ [a,b] -> \ [d1,d2] -> case tp [a,b] [d1,d2] of
170 p = /\ [a,b] -> \ [d1,d2] -> letrec DBINDS and BIND
174 nullMonoBinds :: MonoBinds tyvar uvar id pat -> Bool
176 nullMonoBinds EmptyMonoBinds = True
177 nullMonoBinds (AndMonoBinds bs1 bs2) = nullMonoBinds bs1 && nullMonoBinds bs2
178 nullMonoBinds other_monobind = False
180 andMonoBinds :: [MonoBinds tyvar uvar id pat] -> MonoBinds tyvar uvar id pat
181 andMonoBinds binds = foldr AndMonoBinds EmptyMonoBinds binds
185 instance (NamedThing id, Outputable id, Outputable pat,
186 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
187 Outputable (MonoBinds tyvar uvar id pat) where
188 ppr sty mbind = pprQuote sty (\ sty -> ppr_monobind sty mbind)
191 ppr_monobind sty EmptyMonoBinds = empty
192 ppr_monobind sty (AndMonoBinds binds1 binds2)
193 = ($$) (ppr_monobind sty binds1) (ppr_monobind sty binds2)
195 ppr_monobind sty (PatMonoBind pat grhss_n_binds locn)
196 = hang (ppr sty pat) 4 (pprGRHSsAndBinds sty False grhss_n_binds)
198 ppr_monobind sty (FunMonoBind fun inf matches locn)
199 = pprMatches sty (False, ppr sty fun) matches
200 -- ToDo: print infix if appropriate
202 ppr_monobind sty (VarMonoBind name expr)
203 = hang (hsep [ppr sty name, equals]) 4 (pprExpr sty expr)
205 ppr_monobind sty (CoreMonoBind name expr)
206 = hang (hsep [ppr sty name, equals]) 4 (ppr sty expr)
208 ppr_monobind sty (AbsBinds tyvars dictvars exports val_binds)
209 = ($$) (sep [ptext SLIT("AbsBinds"),
210 brackets (interpp'SP sty tyvars),
211 brackets (interpp'SP sty dictvars),
212 brackets (interpp'SP sty exports)])
213 (nest 4 (ppr sty val_binds))
216 %************************************************************************
218 \subsection{@Sig@: type signatures and value-modifying user pragmas}
220 %************************************************************************
222 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
223 ``specialise this function to these four types...'') in with type
224 signatures. Then all the machinery to move them into place, etc.,
229 = Sig name -- a bog-std type signature
233 | ClassOpSig name -- Selector name
234 name -- Default-method name
238 | SpecSig name -- specialise a function or datatype ...
239 (HsType name) -- ... to these types
240 (Maybe name) -- ... maybe using this as the code for it
243 | InlineSig name -- INLINE f
246 | DeforestSig name -- Deforest using this function definition
250 name -- Associate the "name"d function with
251 FAST_STRING -- the compiler-builtin unfolding (known
252 SrcLoc -- by the String name)
256 instance (NamedThing name, Outputable name) => Outputable (Sig name) where
257 ppr sty sig = pprQuote sty (\ sty -> ppr_sig sty sig)
260 ppr_sig sty (Sig var ty _)
261 = hang (hsep [ppr sty var, ptext SLIT("::")])
264 ppr_sig sty (ClassOpSig var _ ty _)
265 = hang (hsep [ppr sty (getOccName var), ptext SLIT("::")])
268 ppr_sig sty (DeforestSig var _)
269 = hang (hsep [text "{-# DEFOREST", ppr sty var])
272 ppr_sig sty (SpecSig var ty using _)
273 = hang (hsep [text "{-# SPECIALIZE", ppr sty var, ptext SLIT("::")])
274 4 (hsep [ppr sty ty, pp_using using, text "#-}"])
277 pp_using Nothing = empty
278 pp_using (Just me) = hsep [char '=', ppr sty me]
280 ppr_sig sty (InlineSig var _)
282 = hsep [text "{-# INLINE", ppr sty var, text "#-}"]
284 ppr_sig sty (MagicUnfoldingSig var str _)
285 = hsep [text "{-# MAGIC_UNFOLDING", ppr sty var, ptext str, text "#-}"]