2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[HsBinds]{Abstract syntax: top-level bindings and signatures}
6 Datatype for: @Binds@, @Bind@, @Sig@, @MonoBinds@.
9 #include "HsVersions.h"
13 import AbsUniType ( pprUniType, TyVar, UniType
14 IF_ATTACK_PRAGMAS(COMMA cmpTyVar)
15 IF_ATTACK_PRAGMAS(COMMA cmpUniType)
17 import HsExpr ( Expr )
18 import HsMatches ( pprMatches, pprGRHSsAndBinds, Match, GRHSsAndBinds )
19 import HsPat ( ProtoNamePat(..), RenamedPat(..),
21 IF_ATTACK_PRAGMAS(COMMA typeOfPat)
23 import HsPragmas ( GenPragmas, ClassOpPragmas )
24 import HsTypes ( PolyType )
25 import Id ( Id, DictVar(..) )
26 import IdInfo ( UnfoldingGuidance )
31 import ProtoName ( ProtoName(..) ) -- .. for pragmas only
32 import SrcLoc ( SrcLoc )
33 import Unique ( Unique )
37 %************************************************************************
39 \subsection[AbsSyn-Binds]{Bindings: @Binds@}
41 %************************************************************************
43 The following syntax may produce new syntax which is not part of the input,
44 and which is instead a translation of the input to the typechecker.
45 Syntax translations are marked TRANSLATION in comments. New empty
46 productions are useful in development but may not appear in the final
49 Collections of bindings, created by dependency analysis and translation:
52 data Binds bdee pat -- binders and bindees
55 | ThenBinds (Binds bdee pat)
58 | SingleBind (Bind bdee pat)
60 | BindWith -- Bind with a type signature.
61 -- These appear only on typechecker input
62 -- (PolyType [in Sigs] can't appear on output)
63 (Bind bdee pat) -- really ProtoNameBind, but...
64 -- (see "really" comment below)
67 | AbsBinds -- Binds abstraction; TRANSLATION
70 [(Id, Id)] -- (old, new) pairs
71 [(Inst, Expr bdee pat)] -- local dictionaries
72 (Bind bdee pat) -- "the business end"
74 -- Creates bindings for *new* (polymorphic, overloaded) locals
75 -- in terms of *old* (monomorphic, non-overloaded) ones.
77 -- See section 9 of static semantics paper for more details.
78 -- (You can get a PhD for explaining the True Meaning
79 -- of this last construct.)
82 The corresponding unparameterised synonyms:
85 type ProtoNameBinds = Binds ProtoName ProtoNamePat
86 type RenamedBinds = Binds Name RenamedPat
87 type TypecheckedBinds = Binds Id TypecheckedPat
91 nullBinds :: Binds bdee pat -> Bool
92 nullBinds EmptyBinds = True
93 nullBinds (ThenBinds b1 b2) = (nullBinds b1) && (nullBinds b2)
94 nullBinds (SingleBind b) = nullBind b
95 nullBinds (BindWith b _) = nullBind b
96 nullBinds (AbsBinds _ _ _ ds b) = (null ds) && (nullBind b)
99 ToDo: make this recursiveness checking also require that
100 there be something there, i.e., not null ?
103 bindsAreRecursive :: TypecheckedBinds -> Bool
105 bindsAreRecursive EmptyBinds = False
106 bindsAreRecursive (ThenBinds b1 b2)
107 = (bindsAreRecursive b1) || (bindsAreRecursive b2)
108 bindsAreRecursive (SingleBind b) = bindIsRecursive b
109 bindsAreRecursive (BindWith b _) = bindIsRecursive b
110 bindsAreRecursive (AbsBinds _ _ _ ds b)
111 = (bindsAreRecursive d) || (bindIsRecursive b)
116 instance (NamedThing bdee, Outputable bdee,
117 NamedThing pat, Outputable pat) =>
118 Outputable (Binds bdee pat) where
120 ppr sty EmptyBinds = ppNil
121 ppr sty (ThenBinds binds1 binds2)
122 = ppAbove (ppr sty binds1) (ppr sty binds2)
123 ppr sty (SingleBind bind) = ppr sty bind
124 ppr sty (BindWith bind sigs)
125 = ppAbove (if null sigs then ppNil else ppr sty sigs) (ppr sty bind)
126 ppr sty (AbsBinds tyvars dictvars local_pairs dict_binds val_binds)
127 = ppAbove (ppSep [ppPStr SLIT("AbsBinds"),
128 ppBesides[ppLbrack, interpp'SP sty tyvars, ppRbrack],
129 ppBesides[ppLbrack, interpp'SP sty dictvars, ppRbrack],
130 ppBesides[ppLbrack, interpp'SP sty local_pairs, ppRbrack]])
131 (ppNest 4 (ppAbove (ppAboves (map (ppr sty) dict_binds)) (ppr sty val_binds)))
134 %************************************************************************
136 \subsection[AbsSyn-Sig]{@Sig@: type signatures and value-modifying user pragmas}
138 %************************************************************************
140 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
141 ``specialise this function to these four types...'') in with type
142 signatures. Then all the machinery to move them into place, etc.,
147 = Sig name -- a bog-std type signature
149 (GenPragmas name) -- only interface ones have pragmas
152 | ClassOpSig name -- class-op sigs have different pragmas
154 (ClassOpPragmas name) -- only interface ones have pragmas
157 | SpecSig name -- specialise a function or datatype ...
158 (PolyType name) -- ... to these types
159 (Maybe name) -- ... maybe using this as the code for it
162 | InlineSig name -- INLINE f [howto]
163 UnfoldingGuidance -- "howto": how gung-ho we are about inlining
166 -- ToDo: strictly speaking, could omit based on -DOMIT_DEFORESTER
167 | DeforestSig name -- Deforest using this function definition
171 name -- Associate the "name"d function with
172 FAST_STRING -- the compiler-builtin unfolding (known
173 SrcLoc -- by the String name)
175 type ProtoNameSig = Sig ProtoName
176 type RenamedSig = Sig Name
178 type ProtoNameClassOpSig = Sig ProtoName
179 type RenamedClassOpSig = Sig Name
183 instance (Outputable name) => Outputable (Sig name) where
184 ppr sty (Sig var ty pragmas _)
185 = ppHang (ppCat [ppr sty var, ppPStr SLIT("::")])
186 4 (ppAbove (ppr sty ty)
187 (ifnotPprForUser sty (ppr sty pragmas)))
189 ppr sty (ClassOpSig var ty pragmas _)
190 = ppHang (ppCat [ppr sty var, ppPStr SLIT("::")])
191 4 (ppAbove (ppr sty ty)
192 (ifnotPprForUser sty (ppr sty pragmas)))
194 ppr sty (DeforestSig var _)
195 = ppHang (ppCat [ppStr "{-# DEFOREST", ppr sty var])
198 ppr sty (SpecSig var ty using _)
199 = ppHang (ppCat [ppPStr SLIT("{-# SPECIALIZE"), ppr sty var, ppPStr SLIT("::")])
200 4 (ppCat [ppr sty ty, pp_using using, ppPStr SLIT("#-}")])
202 pp_using Nothing = ppNil
203 pp_using (Just me) = ppCat [ppChar '=', ppr sty me]
205 ppr sty (InlineSig var _ _)
206 = ppHang (ppCat [ppPStr SLIT("{-# INLINE"), ppr sty var])
207 4 (ppCat [ppPStr SLIT("<enthusiasm not done yet>"), ppPStr SLIT("#-}")])
209 ppr sty (MagicUnfoldingSig var str _)
210 = ppCat [ppPStr SLIT("{-# MAGIC_UNFOLDING"), ppr sty var, ppPStr str, ppPStr SLIT("#-}")]
213 %************************************************************************
215 \subsection[AbsSyn-Bind]{Binding: @Bind@}
217 %************************************************************************
220 data Bind bdee pat -- binders and bindees
221 = EmptyBind -- because it's convenient when parsing signatures
222 | NonRecBind (MonoBinds bdee pat)
223 | RecBind (MonoBinds bdee pat)
226 The corresponding unparameterised synonyms:
229 type ProtoNameBind = Bind ProtoName ProtoNamePat
230 type RenamedBind = Bind Name RenamedPat
231 type TypecheckedBind = Bind Id TypecheckedPat
235 nullBind :: Bind bdee pat -> Bool
236 nullBind EmptyBind = True
237 nullBind (NonRecBind bs) = nullMonoBinds bs
238 nullBind (RecBind bs) = nullMonoBinds bs
242 bindIsRecursive :: TypecheckedBind -> Bool
243 bindIsRecursive EmptyBind = False
244 bindIsRecursive (NonRecBind _) = False
245 bindIsRecursive (RecBind _) = True
249 instance (NamedThing bdee, Outputable bdee,
250 NamedThing pat, Outputable pat) =>
251 Outputable (Bind bdee pat) where
252 ppr sty EmptyBind = ppNil
253 ppr sty (NonRecBind binds)
254 = ppAbove (ifnotPprForUser sty (ppStr "{- nonrec -}"))
256 ppr sty (RecBind binds)
257 = ppAbove (ifnotPprForUser sty (ppStr "{- rec -}"))
261 %************************************************************************
263 \subsection[AbsSyn-MonoBinds]{Bindings: @MonoBinds@}
265 %************************************************************************
267 Global bindings (where clauses)
270 data MonoBinds bdee pat -- binders and bindees
271 = EmptyMonoBinds -- TRANSLATION
272 | AndMonoBinds (MonoBinds bdee pat)
275 (GRHSsAndBinds bdee pat)
277 | VarMonoBind Id -- TRANSLATION
280 [Match bdee pat] -- must have at least one Match
284 The corresponding unparameterised synonyms:
286 type ProtoNameMonoBinds = MonoBinds ProtoName ProtoNamePat
287 type RenamedMonoBinds = MonoBinds Name RenamedPat
288 type TypecheckedMonoBinds = MonoBinds Id TypecheckedPat
292 nullMonoBinds :: MonoBinds bdee pat -> Bool
293 nullMonoBinds EmptyMonoBinds = True
294 nullMonoBinds (AndMonoBinds bs1 bs2) = (nullMonoBinds bs1) && (nullMonoBinds bs2)
295 nullMonoBinds other_monobind = False
299 instance (NamedThing bdee, Outputable bdee,
300 NamedThing pat, Outputable pat) =>
301 Outputable (MonoBinds bdee pat) where
302 ppr sty EmptyMonoBinds = ppNil
303 ppr sty (AndMonoBinds binds1 binds2)
304 = ppAbove (ppr sty binds1) (ppr sty binds2)
306 ppr sty (PatMonoBind pat grhss_n_binds locn)
308 ifPprShowAll sty (ppr sty locn),
309 (if (hasType pat) then
310 ppHang (ppCat [ppr sty pat, ppStr "::"]) 4 (pprUniType sty (getType pat))
314 (ppHang (ppr sty pat) 4 (pprGRHSsAndBinds sty False grhss_n_binds)) ]
316 ppr sty (FunMonoBind fun matches locn)
318 ifPprShowAll sty (ppr sty locn),
319 if (hasType fun) then
320 ppHang (ppCat [pprNonOp sty fun, ppStr "::"]) 4
321 (pprUniType sty (getType fun))
324 pprMatches sty (False, pprNonOp sty fun) matches
327 ppr sty (VarMonoBind name expr)
328 = ppHang (ppCat [pprNonOp sty name, ppEquals]) 4 (ppr sty expr)