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"
17 import HsMatches ( pprMatches, pprGRHSsAndBinds,
18 Match, GRHSsAndBinds )
19 import HsPat ( collectPatBinders, InPat )
20 import HsPragmas ( GenPragmas, ClassOpPragmas )
21 import HsTypes ( PolyType )
24 import Id ( DictVar(..), Id(..), GenId )
27 import SrcLoc ( SrcLoc{-instances-} )
28 --import TyVar ( GenTyVar{-instances-} )
31 %************************************************************************
33 \subsection{Bindings: @HsBinds@}
35 %************************************************************************
37 The following syntax may produce new syntax which is not part of the input,
38 and which is instead a translation of the input to the typechecker.
39 Syntax translations are marked TRANSLATION in comments. New empty
40 productions are useful in development but may not appear in the final
43 Collections of bindings, created by dependency analysis and translation:
46 data HsBinds tyvar uvar id pat -- binders and bindees
49 | ThenBinds (HsBinds tyvar uvar id pat)
50 (HsBinds tyvar uvar id pat)
52 | SingleBind (Bind tyvar uvar id pat)
54 | BindWith -- Bind with a type signature.
55 -- These appear only on typechecker input
56 -- (PolyType [in Sigs] can't appear on output)
57 (Bind tyvar uvar id pat)
60 | AbsBinds -- Binds abstraction; TRANSLATION
63 [(id, id)] -- (old, new) pairs
64 [(id, HsExpr tyvar uvar id pat)] -- local dictionaries
65 (Bind tyvar uvar id pat) -- "the business end"
67 -- Creates bindings for *new* (polymorphic, overloaded) locals
68 -- in terms of *old* (monomorphic, non-overloaded) ones.
70 -- See section 9 of static semantics paper for more details.
71 -- (You can get a PhD for explaining the True Meaning
72 -- of this last construct.)
76 nullBinds :: HsBinds tyvar uvar id pat -> Bool
78 nullBinds EmptyBinds = True
79 nullBinds (ThenBinds b1 b2) = nullBinds b1 && nullBinds b2
80 nullBinds (SingleBind b) = nullBind b
81 nullBinds (BindWith b _) = nullBind b
82 nullBinds (AbsBinds _ _ _ ds b) = null ds && nullBind b
86 instance (Outputable pat, NamedThing id, Outputable id,
87 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
88 Outputable (HsBinds tyvar uvar id pat) where
90 ppr sty EmptyBinds = ppNil
91 ppr sty (ThenBinds binds1 binds2)
92 = ppAbove (ppr sty binds1) (ppr sty binds2)
93 ppr sty (SingleBind bind) = ppr sty bind
94 ppr sty (BindWith bind sigs)
95 = ppAbove (if null sigs
97 else ppAboves (map (ppr sty) sigs))
99 ppr sty (AbsBinds tyvars dictvars local_pairs dict_binds val_binds)
100 = ppAbove (ppSep [ppPStr SLIT("AbsBinds"),
101 ppBesides[ppLbrack, interpp'SP sty tyvars, ppRbrack],
102 ppBesides[ppLbrack, interpp'SP sty dictvars, ppRbrack],
103 ppBesides[ppLbrack, interpp'SP sty local_pairs, ppRbrack]])
104 (ppNest 4 (ppAbove (ppAboves (map (ppr sty) dict_binds)) (ppr sty val_binds)))
107 %************************************************************************
109 \subsection{@Sig@: type signatures and value-modifying user pragmas}
111 %************************************************************************
113 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
114 ``specialise this function to these four types...'') in with type
115 signatures. Then all the machinery to move them into place, etc.,
120 = Sig name -- a bog-std type signature
122 (GenPragmas name) -- only interface ones have pragmas
125 | ClassOpSig name -- class-op sigs have different pragmas
127 (ClassOpPragmas name) -- only interface ones have pragmas
130 | SpecSig name -- specialise a function or datatype ...
131 (PolyType name) -- ... to these types
132 (Maybe name) -- ... maybe using this as the code for it
135 | InlineSig name -- INLINE f
138 -- ToDo: strictly speaking, could omit based on -DOMIT_DEFORESTER
139 | DeforestSig name -- Deforest using this function definition
143 name -- Associate the "name"d function with
144 FAST_STRING -- the compiler-builtin unfolding (known
145 SrcLoc -- by the String name)
149 instance (NamedThing name, Outputable name) => Outputable (Sig name) where
150 ppr sty (Sig var ty pragmas _)
151 = ppHang (ppCat [pprNonOp sty var, ppPStr SLIT("::")])
152 4 (ppHang (ppr sty ty)
153 4 (ifnotPprForUser sty (ppr sty pragmas)))
155 ppr sty (ClassOpSig var ty pragmas _)
156 = ppHang (ppCat [pprNonOp sty var, ppPStr SLIT("::")])
157 4 (ppHang (ppr sty ty)
158 4 (ifnotPprForUser sty (ppr sty pragmas)))
160 ppr sty (DeforestSig var _)
161 = ppHang (ppCat [ppStr "{-# DEFOREST", pprNonOp sty var])
164 ppr sty (SpecSig var ty using _)
165 = ppHang (ppCat [ppPStr SLIT("{-# SPECIALIZE"), pprNonOp sty var, ppPStr SLIT("::")])
166 4 (ppCat [ppr sty ty, pp_using using, ppPStr SLIT("#-}")])
168 pp_using Nothing = ppNil
169 pp_using (Just me) = ppCat [ppChar '=', ppr sty me]
171 ppr sty (InlineSig var _)
172 = ppCat [ppPStr SLIT("{-# INLINE"), pprNonOp sty var, ppPStr SLIT("#-}")]
174 ppr sty (MagicUnfoldingSig var str _)
175 = ppCat [ppPStr SLIT("{-# MAGIC_UNFOLDING"), pprNonOp sty var, ppPStr str, ppPStr SLIT("#-}")]
178 %************************************************************************
180 \subsection{Binding: @Bind@}
182 %************************************************************************
185 data Bind tyvar uvar id pat -- binders and bindees
186 = EmptyBind -- because it's convenient when parsing signatures
187 | NonRecBind (MonoBinds tyvar uvar id pat)
188 | RecBind (MonoBinds tyvar uvar id pat)
192 nullBind :: Bind tyvar uvar id pat -> Bool
194 nullBind EmptyBind = True
195 nullBind (NonRecBind bs) = nullMonoBinds bs
196 nullBind (RecBind bs) = nullMonoBinds bs
200 bindIsRecursive :: Bind tyvar uvar id pat -> Bool
202 bindIsRecursive EmptyBind = False
203 bindIsRecursive (NonRecBind _) = False
204 bindIsRecursive (RecBind _) = True
208 instance (NamedThing id, Outputable id, Outputable pat,
209 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
210 Outputable (Bind tyvar uvar id pat) where
211 ppr sty EmptyBind = ppNil
212 ppr sty (NonRecBind binds)
213 = ppAbove (ifnotPprForUser sty (ppStr "{- nonrec -}"))
215 ppr sty (RecBind binds)
216 = ppAbove (ifnotPprForUser sty (ppStr "{- rec -}"))
220 %************************************************************************
222 \subsection{Bindings: @MonoBinds@}
224 %************************************************************************
226 Global bindings (where clauses)
229 data MonoBinds tyvar uvar id pat
231 | AndMonoBinds (MonoBinds tyvar uvar id pat)
232 (MonoBinds tyvar uvar id pat)
234 (GRHSsAndBinds tyvar uvar id pat)
237 [Match tyvar uvar id pat] -- must have at least one Match
239 | VarMonoBind id -- TRANSLATION
240 (HsExpr tyvar uvar id pat)
244 nullMonoBinds :: MonoBinds tyvar uvar id pat -> Bool
246 nullMonoBinds EmptyMonoBinds = True
247 nullMonoBinds (AndMonoBinds bs1 bs2) = nullMonoBinds bs1 && nullMonoBinds bs2
248 nullMonoBinds other_monobind = False
252 instance (NamedThing id, Outputable id, Outputable pat,
253 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar) =>
254 Outputable (MonoBinds tyvar uvar id pat) where
255 ppr sty EmptyMonoBinds = ppNil
256 ppr sty (AndMonoBinds binds1 binds2)
257 = ppAbove (ppr sty binds1) (ppr sty binds2)
259 ppr sty (PatMonoBind pat grhss_n_binds locn)
260 = ppHang (ppr sty pat) 4 (pprGRHSsAndBinds sty False grhss_n_binds)
262 ppr sty (FunMonoBind fun matches locn)
263 = pprMatches sty (False, pprNonOp sty fun) matches
265 ppr sty (VarMonoBind name expr)
266 = ppHang (ppCat [pprNonOp sty name, ppEquals]) 4 (ppr sty expr)
269 %************************************************************************
271 \subsection{Collecting binders from @HsBinds@}
273 %************************************************************************
275 Get all the binders in some @MonoBinds@, IN THE ORDER OF
276 APPEARANCE; e.g., in:
284 it should return @[x, y, f, a, b]@ (remember, order important).
287 collectTopLevelBinders :: HsBinds tyvar uvar name (InPat name) -> [name]
288 collectTopLevelBinders EmptyBinds = []
289 collectTopLevelBinders (SingleBind b) = collectBinders b
290 collectTopLevelBinders (BindWith b _) = collectBinders b
291 collectTopLevelBinders (ThenBinds b1 b2)
292 = collectTopLevelBinders b1 ++ collectTopLevelBinders b2
294 collectBinders :: Bind tyvar uvar name (InPat name) -> [name]
295 collectBinders EmptyBind = []
296 collectBinders (NonRecBind monobinds) = collectMonoBinders monobinds
297 collectBinders (RecBind monobinds) = collectMonoBinders monobinds
299 collectMonoBinders :: MonoBinds tyvar uvar name (InPat name) -> [name]
300 collectMonoBinders EmptyMonoBinds = []
301 collectMonoBinders (PatMonoBind pat grhss_w_binds _) = collectPatBinders pat
302 collectMonoBinders (FunMonoBind f matches _) = [f]
303 collectMonoBinders (VarMonoBind v expr) = error "collectMonoBinders"
304 collectMonoBinders (AndMonoBinds bs1 bs2)
305 = collectMonoBinders bs1 ++ collectMonoBinders bs2
307 -- We'd like the binders -- and where they came from --
308 -- so we can make new ones with equally-useful origin info.
310 collectMonoBindersAndLocs
311 :: MonoBinds tyvar uvar name (InPat name) -> [(name, SrcLoc)]
313 collectMonoBindersAndLocs EmptyMonoBinds = []
315 collectMonoBindersAndLocs (AndMonoBinds bs1 bs2)
316 = collectMonoBindersAndLocs bs1 ++ collectMonoBindersAndLocs bs2
318 collectMonoBindersAndLocs (PatMonoBind pat grhss_w_binds locn)
319 = collectPatBinders pat `zip` repeat locn
321 collectMonoBindersAndLocs (FunMonoBind f matches locn) = [(f, locn)]
324 collectMonoBindersAndLocs (VarMonoBind v expr)
325 = trace "collectMonoBindersAndLocs:VarMonoBind" []
326 -- ToDo: this is dubious, i.e., wrong, but harmless?