2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[HsBinds]{Abstract syntax: top-level bindings and signatures}
7 Datatype for: @BindGroup@, @Bind@, @Sig@, @Bind@.
10 {-# OPTIONS -fno-warn-incomplete-patterns #-}
11 -- The above warning supression flag is a temporary kludge.
12 -- While working on this module you are encouraged to remove it and fix
13 -- any warnings in the module. See
14 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 {-# LANGUAGE DeriveDataTypeable #-}
20 import {-# SOURCE #-} HsExpr ( pprExpr, LHsExpr,
21 MatchGroup, pprFunBind,
23 import {-# SOURCE #-} HsPat ( LPat )
41 import Data.IORef( IORef )
42 import Data.Data hiding ( Fixity )
45 %************************************************************************
47 \subsection{Bindings: @BindGroup@}
49 %************************************************************************
51 Global bindings (where clauses)
54 -- During renaming, we need bindings where the left-hand sides
55 -- have been renamed but the the right-hand sides have not.
56 -- the ...LR datatypes are parametrized by two id types,
57 -- one for the left and one for the right.
58 -- Other than during renaming, these will be the same.
60 type HsLocalBinds id = HsLocalBindsLR id id
62 data HsLocalBindsLR idL idR -- Bindings in a 'let' expression
63 -- or a 'where' clause
64 = HsValBinds (HsValBindsLR idL idR)
65 | HsIPBinds (HsIPBinds idR)
67 deriving (Data, Typeable)
69 type HsValBinds id = HsValBindsLR id id
71 data HsValBindsLR idL idR -- Value bindings (not implicit parameters)
72 = ValBindsIn -- Before renaming
73 (LHsBindsLR idL idR) [LSig idR] -- Not dependency analysed
74 -- Recursive by default
76 | ValBindsOut -- After renaming
77 [(RecFlag, LHsBinds idL)] -- Dependency analysed, later bindings
78 -- in the list may depend on earlier
81 deriving (Data, Typeable)
83 type LHsBinds id = Bag (LHsBind id)
84 type LHsBind id = Located (HsBind id)
85 type HsBind id = HsBindLR id id
87 type LHsBindLR idL idR = Located (HsBindLR idL idR)
88 type LHsBindsLR idL idR = Bag (LHsBindLR idL idR)
91 = -- | FunBind is used for both functions @f x = e@
92 -- and variables @f = \x -> e@
94 -- Reason 1: Special case for type inference: see 'TcBinds.tcMonoBinds'.
96 -- Reason 2: Instance decls can only have FunBinds, which is convenient.
97 -- If you change this, you'll need to change e.g. rnMethodBinds
99 -- But note that the form @f :: a->a = ...@
100 -- parses as a pattern binding, just like
101 -- @(f :: a -> a) = ... @
104 fun_id :: Located idL,
106 fun_infix :: Bool, -- ^ True => infix declaration
108 fun_matches :: MatchGroup idR, -- ^ The payload
110 fun_co_fn :: HsWrapper, -- ^ Coercion from the type of the MatchGroup to the type of
113 -- f :: Int -> forall a. a -> a
116 -- Then the MatchGroup will have type (Int -> a' -> a')
117 -- (with a free type variable a'). The coercion will take
118 -- a CoreExpr of this type and convert it to a CoreExpr of
119 -- type Int -> forall a'. a' -> a'
120 -- Notice that the coercion captures the free a'.
122 bind_fvs :: NameSet, -- ^ After the renamer, this contains a superset of the
123 -- Names of the other binders in this binding group that
124 -- are free in the RHS of the defn
125 -- Before renaming, and after typechecking,
126 -- the field is unused; it's just an error thunk
128 fun_tick :: Maybe (Int,[Id]) -- ^ This is the (optional) module-local tick number.
131 | PatBind { -- The pattern is never a simple variable;
132 -- That case is done by FunBind
134 pat_rhs :: GRHSs idR,
135 pat_rhs_ty :: PostTcType, -- Type of the GRHSs
136 bind_fvs :: NameSet -- Same as for FunBind
139 | VarBind { -- Dictionary binding and suchlike
140 var_id :: idL, -- All VarBinds are introduced by the type checker
141 var_rhs :: LHsExpr idR, -- Located only for consistency
142 var_inline :: Bool -- True <=> inline this binding regardless
143 -- (used for implication constraints only)
146 | AbsBinds { -- Binds abstraction; TRANSLATION
148 abs_ev_vars :: [EvVar], -- Includes equality constraints
150 -- AbsBinds only gets used when idL = idR after renaming,
151 -- but these need to be idL's for the collect... code in HsUtil to have
153 abs_exports :: [([TyVar], idL, idL, TcSpecPrags)], -- (tvs, poly_id, mono_id, prags)
155 abs_ev_binds :: TcEvBinds, -- Evidence bindings
156 abs_binds :: LHsBinds idL -- Typechecked user bindings
158 deriving (Data, Typeable)
159 -- Consider (AbsBinds tvs ds [(ftvs, poly_f, mono_f) binds]
161 -- Creates bindings for (polymorphic, overloaded) poly_f
162 -- in terms of monomorphic, non-overloaded mono_f
165 -- 1. 'binds' binds mono_f
166 -- 2. ftvs is a subset of tvs
167 -- 3. ftvs includes all tyvars free in ds
169 -- See section 9 of static semantics paper for more details.
170 -- (You can get a PhD for explaining the True Meaning
171 -- of this last construct.)
173 placeHolderNames :: NameSet
174 -- Used for the NameSet in FunBind and PatBind prior to the renamer
175 placeHolderNames = panic "placeHolderNames"
178 instance (OutputableBndr idL, OutputableBndr idR) => Outputable (HsLocalBindsLR idL idR) where
179 ppr (HsValBinds bs) = ppr bs
180 ppr (HsIPBinds bs) = ppr bs
181 ppr EmptyLocalBinds = empty
183 instance (OutputableBndr idL, OutputableBndr idR) => Outputable (HsValBindsLR idL idR) where
184 ppr (ValBindsIn binds sigs)
185 = pprValBindsForUser binds sigs
187 ppr (ValBindsOut sccs sigs)
188 = getPprStyle $ \ sty ->
189 if debugStyle sty then -- Print with sccs showing
190 vcat (map ppr sigs) $$ vcat (map ppr_scc sccs)
192 pprValBindsForUser (unionManyBags (map snd sccs)) sigs
194 ppr_scc (rec_flag, binds) = pp_rec rec_flag <+> pprLHsBinds binds
195 pp_rec Recursive = ptext (sLit "rec")
196 pp_rec NonRecursive = ptext (sLit "nonrec")
198 -- *not* pprLHsBinds because we don't want braces; 'let' and
199 -- 'where' include a list of HsBindGroups and we don't want
200 -- several groups of bindings each with braces around.
201 -- Sort by location before printing
202 pprValBindsForUser :: (OutputableBndr idL, OutputableBndr idR, OutputableBndr id2)
203 => LHsBindsLR idL idR -> [LSig id2] -> SDoc
204 pprValBindsForUser binds sigs
205 = pprDeeperList vcat (map snd (sort_by_loc decls))
208 decls :: [(SrcSpan, SDoc)]
209 decls = [(loc, ppr sig) | L loc sig <- sigs] ++
210 [(loc, ppr bind) | L loc bind <- bagToList binds]
212 sort_by_loc decls = sortLe (\(l1,_) (l2,_) -> l1 <= l2) decls
214 pprLHsBinds :: (OutputableBndr idL, OutputableBndr idR) => LHsBindsLR idL idR -> SDoc
216 | isEmptyLHsBinds binds = empty
217 | otherwise = lbrace <+> pprDeeperList vcat (map ppr (bagToList binds)) <+> rbrace
220 emptyLocalBinds :: HsLocalBindsLR a b
221 emptyLocalBinds = EmptyLocalBinds
223 isEmptyLocalBinds :: HsLocalBindsLR a b -> Bool
224 isEmptyLocalBinds (HsValBinds ds) = isEmptyValBinds ds
225 isEmptyLocalBinds (HsIPBinds ds) = isEmptyIPBinds ds
226 isEmptyLocalBinds EmptyLocalBinds = True
228 isEmptyValBinds :: HsValBindsLR a b -> Bool
229 isEmptyValBinds (ValBindsIn ds sigs) = isEmptyLHsBinds ds && null sigs
230 isEmptyValBinds (ValBindsOut ds sigs) = null ds && null sigs
232 emptyValBindsIn, emptyValBindsOut :: HsValBindsLR a b
233 emptyValBindsIn = ValBindsIn emptyBag []
234 emptyValBindsOut = ValBindsOut [] []
236 emptyLHsBinds :: LHsBindsLR idL idR
237 emptyLHsBinds = emptyBag
239 isEmptyLHsBinds :: LHsBindsLR idL idR -> Bool
240 isEmptyLHsBinds = isEmptyBag
243 plusHsValBinds :: HsValBinds a -> HsValBinds a -> HsValBinds a
244 plusHsValBinds (ValBindsIn ds1 sigs1) (ValBindsIn ds2 sigs2)
245 = ValBindsIn (ds1 `unionBags` ds2) (sigs1 ++ sigs2)
246 plusHsValBinds (ValBindsOut ds1 sigs1) (ValBindsOut ds2 sigs2)
247 = ValBindsOut (ds1 ++ ds2) (sigs1 ++ sigs2)
249 getTypeSigNames :: HsValBinds a -> NameSet
250 -- Get the names that have a user type sig
251 getTypeSigNames (ValBindsIn {})
252 = panic "getTypeSigNames"
253 getTypeSigNames (ValBindsOut _ sigs)
254 = mkNameSet [unLoc n | L _ (TypeSig n _) <- sigs]
266 f1p = /\ tvs -> \ [d1,d2] -> letrec DBINDS and BIND
269 gp = ...same again, with gm instead of fm
271 This is a pretty bad translation, because it duplicates all the bindings.
272 So the desugarer tries to do a better job:
274 fp = /\ [a,b] -> \ [d1,d2] -> case tp [a,b] [d1,d2] of
278 tp = /\ [a,b] -> \ [d1,d2] -> letrec DBINDS and BIND
282 instance (OutputableBndr idL, OutputableBndr idR) => Outputable (HsBindLR idL idR) where
283 ppr mbind = ppr_monobind mbind
285 ppr_monobind :: (OutputableBndr idL, OutputableBndr idR) => HsBindLR idL idR -> SDoc
287 ppr_monobind (PatBind { pat_lhs = pat, pat_rhs = grhss })
288 = pprPatBind pat grhss
289 ppr_monobind (VarBind { var_id = var, var_rhs = rhs })
290 = sep [pprBndr CaseBind var, nest 2 $ equals <+> pprExpr (unLoc rhs)]
291 ppr_monobind (FunBind { fun_id = fun, fun_infix = inf,
293 fun_matches = matches,
295 = pprTicks empty (case tick of
297 Just t -> text "-- tick id = " <> ppr t)
298 $$ pprFunBind (unLoc fun) inf matches
299 $$ ifPprDebug (ppr wrap)
301 ppr_monobind (AbsBinds { abs_tvs = tyvars, abs_ev_vars = dictvars
302 , abs_exports = exports, abs_binds = val_binds
303 , abs_ev_binds = ev_binds })
304 = sep [ptext (sLit "AbsBinds"),
305 brackets (interpp'SP tyvars),
306 brackets (interpp'SP dictvars),
307 brackets (sep (punctuate comma (map ppr_exp exports)))]
309 nest 2 ( vcat [pprBndr LetBind x | (_,x,_,_) <- exports]
310 -- Print type signatures
311 $$ pprLHsBinds val_binds )
313 ifPprDebug (ppr ev_binds)
315 ppr_exp (tvs, gbl, lcl, prags)
316 = vcat [ppr gbl <+> ptext (sLit "<=") <+> ppr tvs <+> ppr lcl,
317 nest 2 (pprTcSpecPrags gbl prags)]
322 pprTicks :: SDoc -> SDoc -> SDoc
323 -- Print stuff about ticks only when -dppr-debug is on, to avoid
324 -- them appearing in error messages (from the desugarer); see Trac # 3263
325 pprTicks pp_no_debug pp_when_debug
326 = getPprStyle (\ sty -> if debugStyle sty then pp_when_debug
330 %************************************************************************
332 Implicit parameter bindings
334 %************************************************************************
340 TcEvBinds -- Only in typechecker output; binds
341 -- uses of the implicit parameters
342 deriving (Data, Typeable)
344 isEmptyIPBinds :: HsIPBinds id -> Bool
345 isEmptyIPBinds (IPBinds is ds) = null is && isEmptyTcEvBinds ds
347 type LIPBind id = Located (IPBind id)
349 -- | Implicit parameter bindings.
354 deriving (Data, Typeable)
356 instance (OutputableBndr id) => Outputable (HsIPBinds id) where
357 ppr (IPBinds bs ds) = pprDeeperList vcat (map ppr bs)
358 $$ ifPprDebug (ppr ds)
360 instance (OutputableBndr id) => Outputable (IPBind id) where
361 ppr (IPBind id rhs) = pprBndr LetBind id <+> equals <+> pprExpr (unLoc rhs)
365 %************************************************************************
367 \subsection{Coercion functions}
369 %************************************************************************
372 -- A HsWrapper is an expression with a hole in it
373 -- We need coercions to have concrete form so that we can zonk them
376 = WpHole -- The identity coercion
378 | WpCompose HsWrapper HsWrapper
379 -- (wrap1 `WpCompse` wrap2)[e] = wrap1[ wrap2[ e ]]
381 -- Hence (\a. []) `WpCompose` (\b. []) = (\a b. [])
382 -- But ([] a) `WpCompose` ([] b) = ([] b a)
384 | WpCast Coercion -- A cast: [] `cast` co
385 -- Guaranteed not the identity coercion
387 -- Evidence abstraction and application
388 -- (both dictionaries and coercions)
389 | WpEvLam EvVar -- \d. [] the 'd' is an evidence variable
390 | WpEvApp EvTerm -- [] d the 'd' is evidence for a constraint
392 -- Type abstraction and application
393 | WpTyLam TyVar -- \a. [] the 'a' is a type variable (not coercion var)
394 | WpTyApp Type -- [] t the 't' is a type (not coercion)
397 | WpLet TcEvBinds -- Non-empty (or possibly non-empty) evidence bindings,
398 -- so that the identity coercion is always exactly WpHole
399 deriving (Data, Typeable)
403 = TcEvBinds -- Mutable evidence bindings
404 EvBindsVar -- Mutable because they are updated "later"
405 -- when an implication constraint is solved
407 | EvBinds -- Immutable after zonking
412 data EvBindsVar = EvBindsVar (IORef EvBindMap) Unique
413 -- The Unique is only for debug printing
416 type EvBindMap = VarEnv EvBind
418 emptyEvBindMap :: EvBindMap
419 emptyEvBindMap = emptyVarEnv
421 extendEvBinds :: EvBindMap -> EvVar -> EvTerm -> EvBindMap
422 extendEvBinds bs v t = extendVarEnv bs v (EvBind v t)
424 lookupEvBind :: EvBindMap -> EvVar -> Maybe EvBind
425 lookupEvBind = lookupVarEnv
427 evBindMapBinds :: EvBindMap -> Bag EvBind
428 evBindMapBinds = foldVarEnv consBag emptyBag
431 instance Data TcEvBinds where
432 -- Placeholder; we can't travers into TcEvBinds
433 toConstr _ = abstractConstr "TcEvBinds"
434 gunfold _ _ = error "gunfold"
435 dataTypeOf _ = mkNoRepType "TcEvBinds"
437 -- All evidence is bound by EvBinds; no side effects
438 data EvBind = EvBind EvVar EvTerm
441 = EvId EvId -- Term-level variable-to-variable bindings
442 -- (no coercion variables! they come via EvCoercion)
444 | EvCoercion Coercion -- Coercion bindings
446 | EvCast EvVar Coercion -- d |> co
448 | EvDFunApp DFunId -- Dictionary instance application
451 | EvSuperClass DictId Int -- n'th superclass. Used for both equalities and
452 -- dictionaries, even though the former have no
453 -- selector Id. We count up from _0_
455 deriving( Data, Typeable)
457 evVarTerm :: EvVar -> EvTerm
458 evVarTerm v | isCoVar v = EvCoercion (mkCoVarCoercion v)
462 Note [EvBinds/EvTerm]
463 ~~~~~~~~~~~~~~~~~~~~~
464 How evidence is created and updated. Bindings for dictionaries,
465 and coercions and implicit parameters are carried around in TcEvBinds
466 which during constraint generation and simplification is always of the
467 form (TcEvBinds ref). After constraint simplification is finished it
468 will be transformed to t an (EvBinds ev_bag).
470 Evidence for coercions *SHOULD* be filled in using the TcEvBinds
471 However, all EvVars that correspond to *wanted* coercion terms in
472 an EvBind must be mutable variables so that they can be readily
473 inlined (by zonking) after constraint simplification is finished.
475 Conclusion: a new wanted coercion variable should be made mutable.
476 [Notice though that evidence variables that bind coercion terms
477 from super classes will be "given" and hence rigid]
481 emptyTcEvBinds :: TcEvBinds
482 emptyTcEvBinds = EvBinds emptyBag
484 isEmptyTcEvBinds :: TcEvBinds -> Bool
485 isEmptyTcEvBinds (EvBinds b) = isEmptyBag b
486 isEmptyTcEvBinds (TcEvBinds {}) = panic "isEmptyTcEvBinds"
488 (<.>) :: HsWrapper -> HsWrapper -> HsWrapper
491 c1 <.> c2 = c1 `WpCompose` c2
493 mkWpTyApps :: [Type] -> HsWrapper
494 mkWpTyApps tys = mk_co_app_fn WpTyApp tys
496 mkWpEvApps :: [EvTerm] -> HsWrapper
497 mkWpEvApps args = mk_co_app_fn WpEvApp args
499 mkWpEvVarApps :: [EvVar] -> HsWrapper
500 mkWpEvVarApps vs = mkWpEvApps (map evVarTerm vs)
502 mkWpTyLams :: [TyVar] -> HsWrapper
503 mkWpTyLams ids = mk_co_lam_fn WpTyLam ids
505 mkWpLams :: [Var] -> HsWrapper
506 mkWpLams ids = mk_co_lam_fn WpEvLam ids
508 mkWpLet :: TcEvBinds -> HsWrapper
509 -- This no-op is a quite a common case
510 mkWpLet (EvBinds b) | isEmptyBag b = WpHole
511 mkWpLet ev_binds = WpLet ev_binds
513 mk_co_lam_fn :: (a -> HsWrapper) -> [a] -> HsWrapper
514 mk_co_lam_fn f as = foldr (\x wrap -> f x `WpCompose` wrap) WpHole as
516 mk_co_app_fn :: (a -> HsWrapper) -> [a] -> HsWrapper
517 -- For applications, the *first* argument must
518 -- come *last* in the composition sequence
519 mk_co_app_fn f as = foldr (\x wrap -> wrap `WpCompose` f x) WpHole as
521 idHsWrapper :: HsWrapper
524 isIdHsWrapper :: HsWrapper -> Bool
525 isIdHsWrapper WpHole = True
526 isIdHsWrapper _ = False
532 instance Outputable HsWrapper where
533 ppr co_fn = pprHsWrapper (ptext (sLit "<>")) co_fn
535 pprHsWrapper :: SDoc -> HsWrapper -> SDoc
536 -- In debug mode, print the wrapper
537 -- otherwise just print what's inside
538 pprHsWrapper doc wrap
539 = getPprStyle (\ s -> if debugStyle s then (help (add_parens doc) wrap False) else doc)
541 help :: (Bool -> SDoc) -> HsWrapper -> Bool -> SDoc
542 -- True <=> appears in function application position
543 -- False <=> appears as body of let or lambda
545 help it (WpCompose f1 f2) = help (help it f2) f1
546 help it (WpCast co) = add_parens $ sep [it False, nest 2 (ptext (sLit "|>")
547 <+> pprParendType co)]
548 help it (WpEvApp id) = no_parens $ sep [it True, nest 2 (ppr id)]
549 help it (WpTyApp ty) = no_parens $ sep [it True, ptext (sLit "@") <+> pprParendType ty]
550 help it (WpEvLam id) = add_parens $ sep [ ptext (sLit "\\") <> pp_bndr id, it False]
551 help it (WpTyLam tv) = add_parens $ sep [ptext (sLit "/\\") <> pp_bndr tv, it False]
552 help it (WpLet binds) = add_parens $ sep [ptext (sLit "let") <+> braces (ppr binds), it False]
554 pp_bndr v = pprBndr LambdaBind v <> dot
556 add_parens, no_parens :: SDoc -> Bool -> SDoc
557 add_parens d True = parens d
558 add_parens d False = d
561 instance Outputable TcEvBinds where
562 ppr (TcEvBinds v) = ppr v
563 ppr (EvBinds bs) = ptext (sLit "EvBinds") <> braces (ppr bs)
565 instance Outputable EvBindsVar where
566 ppr (EvBindsVar _ u) = ptext (sLit "EvBindsVar") <> angleBrackets (ppr u)
568 instance Outputable EvBind where
569 ppr (EvBind v e) = ppr v <+> equals <+> ppr e
571 instance Outputable EvTerm where
573 ppr (EvCast v co) = ppr v <+> (ptext (sLit "`cast`")) <+> pprParendType co
574 ppr (EvCoercion co) = ppr co
575 ppr (EvSuperClass d n) = ptext (sLit "sc") <> parens (ppr (d,n))
576 ppr (EvDFunApp df tys ts) = ppr df <+> sep [ char '@' <> ppr tys
580 %************************************************************************
582 \subsection{@Sig@: type signatures and value-modifying user pragmas}
584 %************************************************************************
586 It is convenient to lump ``value-modifying'' user-pragmas (e.g.,
587 ``specialise this function to these four types...'') in with type
588 signatures. Then all the machinery to move them into place, etc.,
592 type LSig name = Located (Sig name)
594 data Sig name -- Signatures and pragmas
595 = -- An ordinary type signature
596 -- f :: Num a => a -> a
597 TypeSig (Located name) (LHsType name)
599 -- A type signature in generated code, notably the code
600 -- generated for record selectors. We simply record
601 -- the desired Id itself, replete with its name, type
602 -- and IdDetails. Otherwise it's just like a type
603 -- signature: there should be an accompanying binding
606 -- An ordinary fixity declaration
608 | FixSig (FixitySig name)
612 | InlineSig (Located name) -- Function name
613 InlinePragma -- Never defaultInlinePragma
615 -- A specialisation pragma
616 -- {-# SPECIALISE f :: Int -> Int #-}
617 | SpecSig (Located name) -- Specialise a function or datatype ...
618 (LHsType name) -- ... to these types
619 InlinePragma -- The pragma on SPECIALISE_INLINE form
620 -- If it's just defaultInlinePragma, then we said
621 -- SPECIALISE, not SPECIALISE_INLINE
623 -- A specialisation pragma for instance declarations only
624 -- {-# SPECIALISE instance Eq [Int] #-}
625 | SpecInstSig (LHsType name) -- (Class tys); should be a specialisation of the
626 -- current instance decl
627 deriving (Data, Typeable)
630 type LFixitySig name = Located (FixitySig name)
631 data FixitySig name = FixitySig (Located name) Fixity
632 deriving (Data, Typeable)
634 -- TsSpecPrags conveys pragmas from the type checker to the desugarer
636 = IsDefaultMethod -- Super-specialised: a default method should
637 -- be macro-expanded at every call site
638 | SpecPrags [Located TcSpecPrag]
639 deriving (Data, Typeable)
643 HsWrapper -- An wrapper, that specialises the polymorphic function
644 InlinePragma -- Inlining spec for the specialised function
645 deriving (Data, Typeable)
647 noSpecPrags :: TcSpecPrags
648 noSpecPrags = SpecPrags []
650 hasSpecPrags :: TcSpecPrags -> Bool
651 hasSpecPrags (SpecPrags ps) = not (null ps)
652 hasSpecPrags IsDefaultMethod = False
654 isDefaultMethod :: TcSpecPrags -> Bool
655 isDefaultMethod IsDefaultMethod = True
656 isDefaultMethod (SpecPrags {}) = False
661 okBindSig :: Sig a -> Bool
664 okHsBootSig :: Sig a -> Bool
665 okHsBootSig (TypeSig _ _) = True
666 okHsBootSig (FixSig _) = True
667 okHsBootSig _ = False
669 okClsDclSig :: Sig a -> Bool
670 okClsDclSig (SpecInstSig _) = False
671 okClsDclSig _ = True -- All others OK
673 okInstDclSig :: Sig a -> Bool
674 okInstDclSig (TypeSig _ _) = False
675 okInstDclSig (FixSig _) = False
676 okInstDclSig _ = True
678 sigForThisGroup :: NameSet -> LSig Name -> Bool
679 sigForThisGroup ns sig
680 = case sigName sig of
682 Just n -> n `elemNameSet` ns
684 sigName :: LSig name -> Maybe name
685 sigName (L _ sig) = sigNameNoLoc sig
687 sigNameNoLoc :: Sig name -> Maybe name
688 sigNameNoLoc (TypeSig n _) = Just (unLoc n)
689 sigNameNoLoc (SpecSig n _ _) = Just (unLoc n)
690 sigNameNoLoc (InlineSig n _) = Just (unLoc n)
691 sigNameNoLoc (FixSig (FixitySig n _)) = Just (unLoc n)
692 sigNameNoLoc _ = Nothing
694 isFixityLSig :: LSig name -> Bool
695 isFixityLSig (L _ (FixSig {})) = True
696 isFixityLSig _ = False
698 isVanillaLSig :: LSig name -> Bool -- User type signatures
699 -- A badly-named function, but it's part of the GHCi (used
700 -- by Haddock) so I don't want to change it gratuitously.
701 isVanillaLSig (L _(TypeSig {})) = True
702 isVanillaLSig _ = False
704 isTypeLSig :: LSig name -> Bool -- Type signatures
705 isTypeLSig (L _(TypeSig {})) = True
706 isTypeLSig (L _(IdSig {})) = True
709 isSpecLSig :: LSig name -> Bool
710 isSpecLSig (L _(SpecSig {})) = True
713 isSpecInstLSig :: LSig name -> Bool
714 isSpecInstLSig (L _ (SpecInstSig {})) = True
715 isSpecInstLSig _ = False
717 isPragLSig :: LSig name -> Bool
718 -- Identifies pragmas
719 isPragLSig (L _ (SpecSig {})) = True
720 isPragLSig (L _ (InlineSig {})) = True
723 isInlineLSig :: LSig name -> Bool
724 -- Identifies inline pragmas
725 isInlineLSig (L _ (InlineSig {})) = True
726 isInlineLSig _ = False
728 hsSigDoc :: Sig name -> SDoc
729 hsSigDoc (TypeSig {}) = ptext (sLit "type signature")
730 hsSigDoc (IdSig {}) = ptext (sLit "id signature")
731 hsSigDoc (SpecSig {}) = ptext (sLit "SPECIALISE pragma")
732 hsSigDoc (InlineSig {}) = ptext (sLit "INLINE pragma")
733 hsSigDoc (SpecInstSig {}) = ptext (sLit "SPECIALISE instance pragma")
734 hsSigDoc (FixSig {}) = ptext (sLit "fixity declaration")
737 Signature equality is used when checking for duplicate signatures
740 eqHsSig :: Eq a => LSig a -> LSig a -> Bool
741 eqHsSig (L _ (FixSig (FixitySig n1 _))) (L _ (FixSig (FixitySig n2 _))) = unLoc n1 == unLoc n2
742 eqHsSig (L _ (IdSig n1)) (L _ (IdSig n2)) = n1 == n2
743 eqHsSig (L _ (TypeSig n1 _)) (L _ (TypeSig n2 _)) = unLoc n1 == unLoc n2
744 eqHsSig (L _ (InlineSig n1 _)) (L _ (InlineSig n2 _)) = unLoc n1 == unLoc n2
745 -- For specialisations, we don't have equality over
746 -- HsType, so it's not convenient to spot duplicate
747 -- specialisations here. Check for this later, when we're in Type land
748 eqHsSig _other1 _other2 = False
752 instance (OutputableBndr name) => Outputable (Sig name) where
753 ppr sig = ppr_sig sig
755 ppr_sig :: OutputableBndr name => Sig name -> SDoc
756 ppr_sig (TypeSig var ty) = pprVarSig (unLoc var) (ppr ty)
757 ppr_sig (IdSig id) = pprVarSig id (ppr (varType id))
758 ppr_sig (FixSig fix_sig) = ppr fix_sig
759 ppr_sig (SpecSig var ty inl) = pragBrackets (pprSpec var (ppr ty) inl)
760 ppr_sig (InlineSig var inl) = pragBrackets (ppr inl <+> ppr var)
761 ppr_sig (SpecInstSig ty) = pragBrackets (ptext (sLit "SPECIALIZE instance") <+> ppr ty)
763 instance Outputable name => Outputable (FixitySig name) where
764 ppr (FixitySig name fixity) = sep [ppr fixity, ppr name]
766 pragBrackets :: SDoc -> SDoc
767 pragBrackets doc = ptext (sLit "{-#") <+> doc <+> ptext (sLit "#-}")
769 pprVarSig :: (Outputable id) => id -> SDoc -> SDoc
770 pprVarSig var pp_ty = sep [ppr var <+> dcolon, nest 2 pp_ty]
772 pprSpec :: (Outputable id) => id -> SDoc -> InlinePragma -> SDoc
773 pprSpec var pp_ty inl = ptext (sLit "SPECIALIZE") <+> pp_inl <+> pprVarSig var pp_ty
775 pp_inl | isDefaultInlinePragma inl = empty
776 | otherwise = ppr inl
778 pprTcSpecPrags :: Outputable id => id -> TcSpecPrags -> SDoc
779 pprTcSpecPrags _ IsDefaultMethod = ptext (sLit "<default method>")
780 pprTcSpecPrags gbl (SpecPrags ps) = vcat (map (pprSpecPrag gbl) ps)
782 pprSpecPrag :: Outputable id => id -> Located TcSpecPrag -> SDoc
783 pprSpecPrag var (L _ (SpecPrag _expr inl)) = pprSpec var (ptext (sLit "<type>")) inl
785 instance Outputable TcSpecPrag where
786 ppr (SpecPrag _ p) = ptext (sLit "SpecPrag") <+> ppr p