Get rid of TcMType.newWantedEvVar(s)
[ghc.git] / compiler / typecheck / Inst.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 The @Inst@ type: dictionaries or method instances
7 -}
8
9 {-# LANGUAGE CPP #-}
10
11 module Inst (
12 deeplySkolemise,
13 deeplyInstantiate, instCall, instStupidTheta,
14 emitWanted, emitWanteds,
15
16 newOverloadedLit, mkOverLit,
17
18 newClsInst,
19 tcGetInsts, tcGetInstEnvs, getOverlapFlag,
20 tcExtendLocalInstEnv, instCallConstraints, newMethodFromName,
21 tcSyntaxName,
22
23 -- Simple functions over evidence variables
24 tyVarsOfWC, tyVarsOfBag,
25 tyVarsOfCt, tyVarsOfCts,
26 ) where
27
28 #include "HsVersions.h"
29
30 import {-# SOURCE #-} TcExpr( tcPolyExpr, tcSyntaxOp )
31 import {-# SOURCE #-} TcUnify( unifyType )
32
33 import FastString
34 import HsSyn
35 import TcHsSyn
36 import TcRnMonad
37 import TcEnv
38 import TcEvidence
39 import InstEnv
40 import FunDeps
41 import TcMType
42 import Type
43 import Coercion ( Role(..) )
44 import TcType
45 import HscTypes
46 import Class( Class )
47 import MkId( mkDictFunId )
48 import Id
49 import Name
50 import Var ( EvVar )
51 import VarEnv
52 import VarSet
53 import PrelNames
54 import SrcLoc
55 import DynFlags
56 import Bag
57 import Util
58 import Outputable
59 import Control.Monad( unless )
60 import Data.Maybe( isJust )
61
62 {-
63 ************************************************************************
64 * *
65 Emitting constraints
66 * *
67 ************************************************************************
68 -}
69
70 emitWanteds :: CtOrigin -> TcThetaType -> TcM [EvVar]
71 emitWanteds origin theta = mapM (emitWanted origin) theta
72
73 emitWanted :: CtOrigin -> TcPredType -> TcM EvVar
74 emitWanted origin pred
75 = do { loc <- getCtLoc origin
76 ; ev <- newEvVar pred
77 ; emitFlat $ mkNonCanonical $
78 CtWanted { ctev_pred = pred, ctev_evar = ev, ctev_loc = loc }
79 ; return ev }
80
81 newMethodFromName :: CtOrigin -> Name -> TcRhoType -> TcM (HsExpr TcId)
82 -- Used when Name is the wired-in name for a wired-in class method,
83 -- so the caller knows its type for sure, which should be of form
84 -- forall a. C a => <blah>
85 -- newMethodFromName is supposed to instantiate just the outer
86 -- type variable and constraint
87
88 newMethodFromName origin name inst_ty
89 = do { id <- tcLookupId name
90 -- Use tcLookupId not tcLookupGlobalId; the method is almost
91 -- always a class op, but with -XRebindableSyntax GHC is
92 -- meant to find whatever thing is in scope, and that may
93 -- be an ordinary function.
94
95 ; let (tvs, theta, _caller_knows_this) = tcSplitSigmaTy (idType id)
96 (the_tv:rest) = tvs
97 subst = zipOpenTvSubst [the_tv] [inst_ty]
98
99 ; wrap <- ASSERT( null rest && isSingleton theta )
100 instCall origin [inst_ty] (substTheta subst theta)
101 ; return (mkHsWrap wrap (HsVar id)) }
102
103 {-
104 ************************************************************************
105 * *
106 Deep instantiation and skolemisation
107 * *
108 ************************************************************************
109
110 Note [Deep skolemisation]
111 ~~~~~~~~~~~~~~~~~~~~~~~~~
112 deeplySkolemise decomposes and skolemises a type, returning a type
113 with all its arrows visible (ie not buried under foralls)
114
115 Examples:
116
117 deeplySkolemise (Int -> forall a. Ord a => blah)
118 = ( wp, [a], [d:Ord a], Int -> blah )
119 where wp = \x:Int. /\a. \(d:Ord a). <hole> x
120
121 deeplySkolemise (forall a. Ord a => Maybe a -> forall b. Eq b => blah)
122 = ( wp, [a,b], [d1:Ord a,d2:Eq b], Maybe a -> blah )
123 where wp = /\a.\(d1:Ord a).\(x:Maybe a)./\b.\(d2:Ord b). <hole> x
124
125 In general,
126 if deeplySkolemise ty = (wrap, tvs, evs, rho)
127 and e :: rho
128 then wrap e :: ty
129 and 'wrap' binds tvs, evs
130
131 ToDo: this eta-abstraction plays fast and loose with termination,
132 because it can introduce extra lambdas. Maybe add a `seq` to
133 fix this
134 -}
135
136 deeplySkolemise
137 :: TcSigmaType
138 -> TcM (HsWrapper, [TyVar], [EvVar], TcRhoType)
139
140 deeplySkolemise ty
141 | Just (arg_tys, tvs, theta, ty') <- tcDeepSplitSigmaTy_maybe ty
142 = do { ids1 <- newSysLocalIds (fsLit "dk") arg_tys
143 ; (subst, tvs1) <- tcInstSkolTyVars tvs
144 ; ev_vars1 <- newEvVars (substTheta subst theta)
145 ; (wrap, tvs2, ev_vars2, rho) <- deeplySkolemise (substTy subst ty')
146 ; return ( mkWpLams ids1
147 <.> mkWpTyLams tvs1
148 <.> mkWpLams ev_vars1
149 <.> wrap
150 <.> mkWpEvVarApps ids1
151 , tvs1 ++ tvs2
152 , ev_vars1 ++ ev_vars2
153 , mkFunTys arg_tys rho ) }
154
155 | otherwise
156 = return (idHsWrapper, [], [], ty)
157
158 deeplyInstantiate :: CtOrigin -> TcSigmaType -> TcM (HsWrapper, TcRhoType)
159 -- Int -> forall a. a -> a ==> (\x:Int. [] x alpha) :: Int -> alpha
160 -- In general if
161 -- if deeplyInstantiate ty = (wrap, rho)
162 -- and e :: ty
163 -- then wrap e :: rho
164
165 deeplyInstantiate orig ty
166 | Just (arg_tys, tvs, theta, rho) <- tcDeepSplitSigmaTy_maybe ty
167 = do { (subst, tvs') <- tcInstTyVars tvs
168 ; ids1 <- newSysLocalIds (fsLit "di") (substTys subst arg_tys)
169 ; let theta' = substTheta subst theta
170 ; wrap1 <- instCall orig (mkTyVarTys tvs') theta'
171 ; traceTc "Instantiating (deeply)" (vcat [ text "origin" <+> pprCtOrigin orig
172 , text "type" <+> ppr ty
173 , text "with" <+> ppr tvs'
174 , text "args:" <+> ppr ids1
175 , text "theta:" <+> ppr theta' ])
176 ; (wrap2, rho2) <- deeplyInstantiate orig (substTy subst rho)
177 ; return (mkWpLams ids1
178 <.> wrap2
179 <.> wrap1
180 <.> mkWpEvVarApps ids1,
181 mkFunTys arg_tys rho2) }
182
183 | otherwise = return (idHsWrapper, ty)
184
185 {-
186 ************************************************************************
187 * *
188 Instantiating a call
189 * *
190 ************************************************************************
191 -}
192
193 ----------------
194 instCall :: CtOrigin -> [TcType] -> TcThetaType -> TcM HsWrapper
195 -- Instantiate the constraints of a call
196 -- (instCall o tys theta)
197 -- (a) Makes fresh dictionaries as necessary for the constraints (theta)
198 -- (b) Throws these dictionaries into the LIE
199 -- (c) Returns an HsWrapper ([.] tys dicts)
200
201 instCall orig tys theta
202 = do { dict_app <- instCallConstraints orig theta
203 ; return (dict_app <.> mkWpTyApps tys) }
204
205 ----------------
206 instCallConstraints :: CtOrigin -> TcThetaType -> TcM HsWrapper
207 -- Instantiates the TcTheta, puts all constraints thereby generated
208 -- into the LIE, and returns a HsWrapper to enclose the call site.
209
210 instCallConstraints orig preds
211 | null preds
212 = return idHsWrapper
213 | otherwise
214 = do { evs <- mapM go preds
215 ; traceTc "instCallConstraints" (ppr evs)
216 ; return (mkWpEvApps evs) }
217 where
218 go pred
219 | Just (Nominal, ty1, ty2) <- getEqPredTys_maybe pred -- Try short-cut
220 = do { co <- unifyType ty1 ty2
221 ; return (EvCoercion co) }
222 | otherwise
223 = do { ev_var <- emitWanted orig pred
224 ; return (EvId ev_var) }
225
226 ----------------
227 instStupidTheta :: CtOrigin -> TcThetaType -> TcM ()
228 -- Similar to instCall, but only emit the constraints in the LIE
229 -- Used exclusively for the 'stupid theta' of a data constructor
230 instStupidTheta orig theta
231 = do { _co <- instCallConstraints orig theta -- Discard the coercion
232 ; return () }
233
234 {-
235 ************************************************************************
236 * *
237 Literals
238 * *
239 ************************************************************************
240
241 In newOverloadedLit we convert directly to an Int or Integer if we
242 know that's what we want. This may save some time, by not
243 temporarily generating overloaded literals, but it won't catch all
244 cases (the rest are caught in lookupInst).
245 -}
246
247 newOverloadedLit :: CtOrigin
248 -> HsOverLit Name
249 -> TcRhoType
250 -> TcM (HsOverLit TcId)
251 newOverloadedLit orig lit res_ty
252 = do dflags <- getDynFlags
253 newOverloadedLit' dflags orig lit res_ty
254
255 newOverloadedLit' :: DynFlags
256 -> CtOrigin
257 -> HsOverLit Name
258 -> TcRhoType
259 -> TcM (HsOverLit TcId)
260 newOverloadedLit' dflags orig
261 lit@(OverLit { ol_val = val, ol_rebindable = rebindable
262 , ol_witness = meth_name }) res_ty
263
264 | not rebindable
265 , Just expr <- shortCutLit dflags val res_ty
266 -- Do not generate a LitInst for rebindable syntax.
267 -- Reason: If we do, tcSimplify will call lookupInst, which
268 -- will call tcSyntaxName, which does unification,
269 -- which tcSimplify doesn't like
270 = return (lit { ol_witness = expr, ol_type = res_ty
271 , ol_rebindable = rebindable })
272
273 | otherwise
274 = do { hs_lit <- mkOverLit val
275 ; let lit_ty = hsLitType hs_lit
276 ; fi' <- tcSyntaxOp orig meth_name (mkFunTy lit_ty res_ty)
277 -- Overloaded literals must have liftedTypeKind, because
278 -- we're instantiating an overloaded function here,
279 -- whereas res_ty might be openTypeKind. This was a bug in 6.2.2
280 -- However this'll be picked up by tcSyntaxOp if necessary
281 ; let witness = HsApp (noLoc fi') (noLoc (HsLit hs_lit))
282 ; return (lit { ol_witness = witness, ol_type = res_ty
283 , ol_rebindable = rebindable }) }
284
285 ------------
286 mkOverLit :: OverLitVal -> TcM HsLit
287 mkOverLit (HsIntegral src i)
288 = do { integer_ty <- tcMetaTy integerTyConName
289 ; return (HsInteger src i integer_ty) }
290
291 mkOverLit (HsFractional r)
292 = do { rat_ty <- tcMetaTy rationalTyConName
293 ; return (HsRat r rat_ty) }
294
295 mkOverLit (HsIsString src s) = return (HsString src s)
296
297 {-
298 ************************************************************************
299 * *
300 Re-mappable syntax
301
302 Used only for arrow syntax -- find a way to nuke this
303 * *
304 ************************************************************************
305
306 Suppose we are doing the -XRebindableSyntax thing, and we encounter
307 a do-expression. We have to find (>>) in the current environment, which is
308 done by the rename. Then we have to check that it has the same type as
309 Control.Monad.(>>). Or, more precisely, a compatible type. One 'customer' had
310 this:
311
312 (>>) :: HB m n mn => m a -> n b -> mn b
313
314 So the idea is to generate a local binding for (>>), thus:
315
316 let then72 :: forall a b. m a -> m b -> m b
317 then72 = ...something involving the user's (>>)...
318 in
319 ...the do-expression...
320
321 Now the do-expression can proceed using then72, which has exactly
322 the expected type.
323
324 In fact tcSyntaxName just generates the RHS for then72, because we only
325 want an actual binding in the do-expression case. For literals, we can
326 just use the expression inline.
327 -}
328
329 tcSyntaxName :: CtOrigin
330 -> TcType -- Type to instantiate it at
331 -> (Name, HsExpr Name) -- (Standard name, user name)
332 -> TcM (Name, HsExpr TcId) -- (Standard name, suitable expression)
333 -- USED ONLY FOR CmdTop (sigh) ***
334 -- See Note [CmdSyntaxTable] in HsExpr
335
336 tcSyntaxName orig ty (std_nm, HsVar user_nm)
337 | std_nm == user_nm
338 = do rhs <- newMethodFromName orig std_nm ty
339 return (std_nm, rhs)
340
341 tcSyntaxName orig ty (std_nm, user_nm_expr) = do
342 std_id <- tcLookupId std_nm
343 let
344 -- C.f. newMethodAtLoc
345 ([tv], _, tau) = tcSplitSigmaTy (idType std_id)
346 sigma1 = substTyWith [tv] [ty] tau
347 -- Actually, the "tau-type" might be a sigma-type in the
348 -- case of locally-polymorphic methods.
349
350 addErrCtxtM (syntaxNameCtxt user_nm_expr orig sigma1) $ do
351
352 -- Check that the user-supplied thing has the
353 -- same type as the standard one.
354 -- Tiresome jiggling because tcCheckSigma takes a located expression
355 span <- getSrcSpanM
356 expr <- tcPolyExpr (L span user_nm_expr) sigma1
357 return (std_nm, unLoc expr)
358
359 syntaxNameCtxt :: HsExpr Name -> CtOrigin -> Type -> TidyEnv
360 -> TcRn (TidyEnv, SDoc)
361 syntaxNameCtxt name orig ty tidy_env
362 = do { inst_loc <- getCtLoc orig
363 ; let msg = vcat [ ptext (sLit "When checking that") <+> quotes (ppr name)
364 <+> ptext (sLit "(needed by a syntactic construct)")
365 , nest 2 (ptext (sLit "has the required type:")
366 <+> ppr (tidyType tidy_env ty))
367 , nest 2 (pprArisingAt inst_loc) ]
368 ; return (tidy_env, msg) }
369
370 {-
371 ************************************************************************
372 * *
373 Instances
374 * *
375 ************************************************************************
376 -}
377
378 getOverlapFlag :: Maybe OverlapMode -> TcM OverlapFlag
379 getOverlapFlag overlap_mode
380 = do { dflags <- getDynFlags
381 ; let overlap_ok = xopt Opt_OverlappingInstances dflags
382 incoherent_ok = xopt Opt_IncoherentInstances dflags
383 use x = OverlapFlag { isSafeOverlap = safeLanguageOn dflags
384 , overlapMode = x }
385 default_oflag | incoherent_ok = use Incoherent
386 | overlap_ok = use Overlaps
387 | otherwise = use NoOverlap
388
389 final_oflag = setOverlapModeMaybe default_oflag overlap_mode
390 ; return final_oflag }
391
392 tcGetInsts :: TcM [ClsInst]
393 -- Gets the local class instances.
394 tcGetInsts = fmap tcg_insts getGblEnv
395
396 newClsInst :: Maybe OverlapMode -> Name -> [TyVar] -> ThetaType
397 -> Class -> [Type] -> TcM ClsInst
398 newClsInst overlap_mode dfun_name tvs theta clas tys
399 = do { (subst, tvs') <- freshenTyVarBndrs tvs
400 -- Be sure to freshen those type variables,
401 -- so they are sure not to appear in any lookup
402 ; let tys' = substTys subst tys
403 theta' = substTheta subst theta
404 dfun = mkDictFunId dfun_name tvs' theta' clas tys'
405 -- Substituting in the DFun type just makes sure that
406 -- we are using TyVars rather than TcTyVars
407 -- Not sure if this is really the right place to do so,
408 -- but it'll do fine
409 ; oflag <- getOverlapFlag overlap_mode
410 ; return (mkLocalInstance dfun oflag tvs' clas tys') }
411
412 tcExtendLocalInstEnv :: [ClsInst] -> TcM a -> TcM a
413 -- Add new locally-defined instances
414 tcExtendLocalInstEnv dfuns thing_inside
415 = do { traceDFuns dfuns
416 ; env <- getGblEnv
417 ; (inst_env', cls_insts') <- foldlM addLocalInst
418 (tcg_inst_env env, tcg_insts env)
419 dfuns
420 ; let env' = env { tcg_insts = cls_insts'
421 , tcg_inst_env = inst_env' }
422 ; setGblEnv env' thing_inside }
423
424 addLocalInst :: (InstEnv, [ClsInst]) -> ClsInst -> TcM (InstEnv, [ClsInst])
425 -- Check that the proposed new instance is OK,
426 -- and then add it to the home inst env
427 -- If overwrite_inst, then we can overwrite a direct match
428 addLocalInst (home_ie, my_insts) ispec
429 = do {
430 -- Instantiate the dfun type so that we extend the instance
431 -- envt with completely fresh template variables
432 -- This is important because the template variables must
433 -- not overlap with anything in the things being looked up
434 -- (since we do unification).
435 --
436 -- We use tcInstSkolType because we don't want to allocate fresh
437 -- *meta* type variables.
438 --
439 -- We use UnkSkol --- and *not* InstSkol or PatSkol --- because
440 -- these variables must be bindable by tcUnifyTys. See
441 -- the call to tcUnifyTys in InstEnv, and the special
442 -- treatment that instanceBindFun gives to isOverlappableTyVar
443 -- This is absurdly delicate.
444
445 -- Load imported instances, so that we report
446 -- duplicates correctly
447
448 -- 'matches' are existing instance declarations that are less
449 -- specific than the new one
450 -- 'dups' are those 'matches' that are equal to the new one
451 ; isGHCi <- getIsGHCi
452 ; eps <- getEps
453 ; tcg_env <- getGblEnv
454 ; let (home_ie', my_insts')
455 | isGHCi = ( deleteFromInstEnv home_ie ispec
456 , filterOut (identicalInstHead ispec) my_insts)
457 | otherwise = (home_ie, my_insts)
458 -- If there is a home-package duplicate instance,
459 -- silently delete it
460
461 (_tvs, cls, tys) = instanceHead ispec
462 -- If we're compiling sig-of and there's an external duplicate
463 -- instance, silently ignore it (that's the instance we're
464 -- implementing!) NB: we still count local duplicate instances
465 -- as errors.
466 -- See Note [Signature files and type class instances]
467 global_ie
468 | isJust (tcg_sig_of tcg_env) = emptyInstEnv
469 | otherwise = eps_inst_env eps
470 inst_envs = InstEnvs { ie_global = global_ie
471 , ie_local = home_ie'
472 , ie_visible = tcg_visible_orphan_mods tcg_env }
473 (matches, _, _) = lookupInstEnv inst_envs cls tys
474 dups = filter (identicalInstHead ispec) (map fst matches)
475
476 -- Check functional dependencies
477 ; case checkFunDeps inst_envs ispec of
478 Just specs -> funDepErr ispec specs
479 Nothing -> return ()
480
481 -- Check for duplicate instance decls.
482 ; unless (null dups) $
483 dupInstErr ispec (head dups)
484
485 ; return (extendInstEnv home_ie' ispec, ispec:my_insts') }
486
487 {-
488 Note [Signature files and type class instances]
489 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
490 Instances in signature files do not have an effect when compiling:
491 when you compile a signature against an implementation, you will
492 see the instances WHETHER OR NOT the instance is declared in
493 the file (this is because the signatures go in the EPS and we
494 can't filter them out easily.) This is also why we cannot
495 place the instance in the hi file: it would show up as a duplicate,
496 and we don't have instance reexports anyway.
497
498 However, you might find them useful when typechecking against
499 a signature: the instance is a way of indicating to GHC that
500 some instance exists, in case downstream code uses it.
501
502 Implementing this is a little tricky. Consider the following
503 situation (sigof03):
504
505 module A where
506 instance C T where ...
507
508 module ASig where
509 instance C T
510
511 When compiling ASig, A.hi is loaded, which brings its instances
512 into the EPS. When we process the instance declaration in ASig,
513 we should ignore it for the purpose of doing a duplicate check,
514 since it's not actually a duplicate. But don't skip the check
515 entirely, we still want this to fail (tcfail221):
516
517 module ASig where
518 instance C T
519 instance C T
520
521 Note that in some situations, the interface containing the type
522 class instances may not have been loaded yet at all. The usual
523 situation when A imports another module which provides the
524 instances (sigof02m):
525
526 module A(module B) where
527 import B
528
529 See also Note [Signature lazy interface loading]. We can't
530 rely on this, however, since sometimes we'll have spurious
531 type class instances in the EPS, see #9422 (sigof02dm)
532
533 ************************************************************************
534 * *
535 Errors and tracing
536 * *
537 ************************************************************************
538 -}
539
540 traceDFuns :: [ClsInst] -> TcRn ()
541 traceDFuns ispecs
542 = traceTc "Adding instances:" (vcat (map pp ispecs))
543 where
544 pp ispec = hang (ppr (instanceDFunId ispec) <+> colon)
545 2 (ppr ispec)
546 -- Print the dfun name itself too
547
548 funDepErr :: ClsInst -> [ClsInst] -> TcRn ()
549 funDepErr ispec ispecs
550 = addClsInstsErr (ptext (sLit "Functional dependencies conflict between instance declarations:"))
551 (ispec : ispecs)
552
553 dupInstErr :: ClsInst -> ClsInst -> TcRn ()
554 dupInstErr ispec dup_ispec
555 = addClsInstsErr (ptext (sLit "Duplicate instance declarations:"))
556 [ispec, dup_ispec]
557
558 addClsInstsErr :: SDoc -> [ClsInst] -> TcRn ()
559 addClsInstsErr herald ispecs
560 = setSrcSpan (getSrcSpan (head sorted)) $
561 addErr (hang herald 2 (pprInstances sorted))
562 where
563 sorted = sortWith getSrcLoc ispecs
564 -- The sortWith just arranges that instances are dislayed in order
565 -- of source location, which reduced wobbling in error messages,
566 -- and is better for users
567
568 {-
569 ************************************************************************
570 * *
571 Simple functions over evidence variables
572 * *
573 ************************************************************************
574 -}
575
576 ---------------- Getting free tyvars -------------------------
577 tyVarsOfCt :: Ct -> TcTyVarSet
578 tyVarsOfCt (CTyEqCan { cc_tyvar = tv, cc_rhs = xi }) = extendVarSet (tyVarsOfType xi) tv
579 tyVarsOfCt (CFunEqCan { cc_tyargs = tys, cc_fsk = fsk }) = extendVarSet (tyVarsOfTypes tys) fsk
580 tyVarsOfCt (CDictCan { cc_tyargs = tys }) = tyVarsOfTypes tys
581 tyVarsOfCt (CIrredEvCan { cc_ev = ev }) = tyVarsOfType (ctEvPred ev)
582 tyVarsOfCt (CHoleCan { cc_ev = ev }) = tyVarsOfType (ctEvPred ev)
583 tyVarsOfCt (CNonCanonical { cc_ev = ev }) = tyVarsOfType (ctEvPred ev)
584
585 tyVarsOfCts :: Cts -> TcTyVarSet
586 tyVarsOfCts = foldrBag (unionVarSet . tyVarsOfCt) emptyVarSet
587
588 tyVarsOfWC :: WantedConstraints -> TyVarSet
589 -- Only called on *zonked* things, hence no need to worry about flatten-skolems
590 tyVarsOfWC (WC { wc_flat = flat, wc_impl = implic, wc_insol = insol })
591 = tyVarsOfCts flat `unionVarSet`
592 tyVarsOfBag tyVarsOfImplic implic `unionVarSet`
593 tyVarsOfCts insol
594
595 tyVarsOfImplic :: Implication -> TyVarSet
596 -- Only called on *zonked* things, hence no need to worry about flatten-skolems
597 tyVarsOfImplic (Implic { ic_skols = skols
598 , ic_given = givens, ic_wanted = wanted })
599 = (tyVarsOfWC wanted `unionVarSet` tyVarsOfTypes (map evVarPred givens))
600 `delVarSetList` skols
601
602 tyVarsOfBag :: (a -> TyVarSet) -> Bag a -> TyVarSet
603 tyVarsOfBag tvs_of = foldrBag (unionVarSet . tvs_of) emptyVarSet