Use TyVars in PatSyns
[ghc.git] / compiler / typecheck / TcMType.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 Monadic type operations
7
8 This module contains monadic operations over types that contain
9 mutable type variables
10 -}
11
12 {-# LANGUAGE CPP, TupleSections, MultiWayIf #-}
13
14 module TcMType (
15 TcTyVar, TcKind, TcType, TcTauType, TcThetaType, TcTyVarSet,
16
17 --------------------------------
18 -- Creating new mutable type variables
19 newFlexiTyVar,
20 newFlexiTyVarTy, -- Kind -> TcM TcType
21 newFlexiTyVarTys, -- Int -> Kind -> TcM [TcType]
22 newOpenFlexiTyVarTy, newOpenTypeKind,
23 newMetaKindVar, newMetaKindVars, newMetaTyVarTyAtLevel,
24 cloneMetaTyVar,
25 newFmvTyVar, newFskTyVar,
26
27 readMetaTyVar, writeMetaTyVar, writeMetaTyVarRef,
28 newMetaDetails, isFilledMetaTyVar, isUnfilledMetaTyVar,
29
30 --------------------------------
31 -- Expected types
32 ExpType(..), ExpSigmaType, ExpRhoType,
33 mkCheckExpType,
34 newInferExpType, newInferExpTypeInst, newInferExpTypeNoInst,
35 readExpType, readExpType_maybe,
36 expTypeToType, checkingExpType_maybe, checkingExpType,
37 tauifyExpType, inferResultToType,
38
39 --------------------------------
40 -- Creating fresh type variables for pm checking
41 genInstSkolTyVarsX,
42
43 --------------------------------
44 -- Creating new evidence variables
45 newEvVar, newEvVars, newDict,
46 newWanted, newWanteds, cloneWanted, cloneWC,
47 emitWanted, emitWantedEq, emitWantedEvVar, emitWantedEvVars,
48 newTcEvBinds, addTcEvBind,
49
50 newCoercionHole, fillCoercionHole, isFilledCoercionHole,
51 unpackCoercionHole, unpackCoercionHole_maybe,
52 checkCoercionHole,
53
54 --------------------------------
55 -- Instantiation
56 newMetaTyVars, newMetaTyVarX,
57 newMetaSigTyVars, newMetaSigTyVarX,
58 newSigTyVar, newWildCardX,
59 tcInstType,
60 tcInstSkolTyVars, tcInstSuperSkolTyVarsX,
61 tcInstSigTyVars,
62 tcSkolDFunType, tcSuperSkolTyVars,
63
64 instSkolTyCoVars, freshenTyVarBndrs, freshenCoVarBndrsX,
65
66 --------------------------------
67 -- Zonking and tidying
68 zonkTidyTcType, zonkTidyOrigin,
69 mkTypeErrorThing, mkTypeErrorThingArgs,
70 tidyEvVar, tidyCt, tidySkolemInfo,
71 skolemiseUnboundMetaTyVar, skolemiseRuntimeUnk,
72 zonkTcTyVar, zonkTcTyVars, zonkTcTyVarToTyVar,
73 zonkTyCoVarsAndFV, zonkTcTypeAndFV,
74 zonkTyCoVarsAndFVList,
75 zonkTcTypeAndSplitDepVars, zonkTcTypesAndSplitDepVars,
76 zonkQuantifiedTyVar,
77 quantifyTyVars, quantifyZonkedTyVars,
78 zonkTcTyCoVarBndr, zonkTcTyVarBinder,
79 zonkTcType, zonkTcTypes, zonkCo,
80 zonkTyCoVarKind, zonkTcTypeMapper,
81
82 zonkEvVar, zonkWC, zonkSimples, zonkId, zonkCt, zonkSkolemInfo,
83
84 tcGetGlobalTyCoVars
85 ) where
86
87 #include "HsVersions.h"
88
89 -- friends:
90 import TyCoRep
91 import TcType
92 import Type
93 import Kind
94 import Coercion
95 import Class
96 import Var
97
98 -- others:
99 import TcRnMonad -- TcType, amongst others
100 import TcEvidence
101 import Id
102 import Name
103 import VarSet
104 import TysWiredIn
105 import TysPrim
106 import VarEnv
107 import PrelNames
108 import Util
109 import Outputable
110 import FastString
111 import SrcLoc
112 import Bag
113 import Pair
114 import UniqFM
115 import qualified GHC.LanguageExtensions as LangExt
116
117 import Control.Monad
118 import Maybes
119 import Data.List ( mapAccumL )
120 import Control.Arrow ( second )
121
122 {-
123 ************************************************************************
124 * *
125 Kind variables
126 * *
127 ************************************************************************
128 -}
129
130 mkKindName :: Unique -> Name
131 mkKindName unique = mkSystemName unique kind_var_occ
132
133 kind_var_occ :: OccName -- Just one for all MetaKindVars
134 -- They may be jiggled by tidying
135 kind_var_occ = mkOccName tvName "k"
136
137 newMetaKindVar :: TcM TcKind
138 newMetaKindVar = do { uniq <- newUnique
139 ; details <- newMetaDetails TauTv
140 ; let kv = mkTcTyVar (mkKindName uniq) liftedTypeKind details
141 ; return (mkTyVarTy kv) }
142
143 newMetaKindVars :: Int -> TcM [TcKind]
144 newMetaKindVars n = mapM (\ _ -> newMetaKindVar) (nOfThem n ())
145
146 {-
147 ************************************************************************
148 * *
149 Evidence variables; range over constraints we can abstract over
150 * *
151 ************************************************************************
152 -}
153
154 newEvVars :: TcThetaType -> TcM [EvVar]
155 newEvVars theta = mapM newEvVar theta
156
157 --------------
158
159 newEvVar :: TcPredType -> TcRnIf gbl lcl EvVar
160 -- Creates new *rigid* variables for predicates
161 newEvVar ty = do { name <- newSysName (predTypeOccName ty)
162 ; return (mkLocalIdOrCoVar name ty) }
163
164 newWanted :: CtOrigin -> Maybe TypeOrKind -> PredType -> TcM CtEvidence
165 -- Deals with both equality and non-equality predicates
166 newWanted orig t_or_k pty
167 = do loc <- getCtLocM orig t_or_k
168 d <- if isEqPred pty then HoleDest <$> newCoercionHole
169 else EvVarDest <$> newEvVar pty
170 return $ CtWanted { ctev_dest = d
171 , ctev_pred = pty
172 , ctev_nosh = WDeriv
173 , ctev_loc = loc }
174
175 newWanteds :: CtOrigin -> ThetaType -> TcM [CtEvidence]
176 newWanteds orig = mapM (newWanted orig Nothing)
177
178 cloneWanted :: Ct -> TcM CtEvidence
179 cloneWanted ct
180 = newWanted (ctEvOrigin ev) Nothing (ctEvPred ev)
181 where
182 ev = ctEvidence ct
183
184 cloneWC :: WantedConstraints -> TcM WantedConstraints
185 cloneWC wc@(WC { wc_simple = simples, wc_impl = implics })
186 = do { simples' <- mapBagM clone_one simples
187 ; implics' <- mapBagM clone_implic implics
188 ; return (wc { wc_simple = simples', wc_impl = implics' }) }
189 where
190 clone_one ct = do { ev <- cloneWanted ct; return (mkNonCanonical ev) }
191
192 clone_implic implic@(Implic { ic_wanted = inner_wanted })
193 = do { inner_wanted' <- cloneWC inner_wanted
194 ; return (implic { ic_wanted = inner_wanted' }) }
195
196 -- | Emits a new Wanted. Deals with both equalities and non-equalities.
197 emitWanted :: CtOrigin -> TcPredType -> TcM EvTerm
198 emitWanted origin pty
199 = do { ev <- newWanted origin Nothing pty
200 ; emitSimple $ mkNonCanonical ev
201 ; return $ ctEvTerm ev }
202
203 -- | Emits a new equality constraint
204 emitWantedEq :: CtOrigin -> TypeOrKind -> Role -> TcType -> TcType -> TcM Coercion
205 emitWantedEq origin t_or_k role ty1 ty2
206 = do { hole <- newCoercionHole
207 ; loc <- getCtLocM origin (Just t_or_k)
208 ; emitSimple $ mkNonCanonical $
209 CtWanted { ctev_pred = pty, ctev_dest = HoleDest hole
210 , ctev_nosh = WDeriv, ctev_loc = loc }
211 ; return (mkHoleCo hole role ty1 ty2) }
212 where
213 pty = mkPrimEqPredRole role ty1 ty2
214
215 -- | Creates a new EvVar and immediately emits it as a Wanted.
216 -- No equality predicates here.
217 emitWantedEvVar :: CtOrigin -> TcPredType -> TcM EvVar
218 emitWantedEvVar origin ty
219 = do { new_cv <- newEvVar ty
220 ; loc <- getCtLocM origin Nothing
221 ; let ctev = CtWanted { ctev_dest = EvVarDest new_cv
222 , ctev_pred = ty
223 , ctev_nosh = WDeriv
224 , ctev_loc = loc }
225 ; emitSimple $ mkNonCanonical ctev
226 ; return new_cv }
227
228 emitWantedEvVars :: CtOrigin -> [TcPredType] -> TcM [EvVar]
229 emitWantedEvVars orig = mapM (emitWantedEvVar orig)
230
231 newDict :: Class -> [TcType] -> TcM DictId
232 newDict cls tys
233 = do { name <- newSysName (mkDictOcc (getOccName cls))
234 ; return (mkLocalId name (mkClassPred cls tys)) }
235
236 predTypeOccName :: PredType -> OccName
237 predTypeOccName ty = case classifyPredType ty of
238 ClassPred cls _ -> mkDictOcc (getOccName cls)
239 EqPred _ _ _ -> mkVarOccFS (fsLit "cobox")
240 IrredPred _ -> mkVarOccFS (fsLit "irred")
241
242 {-
243 ************************************************************************
244 * *
245 Coercion holes
246 * *
247 ************************************************************************
248 -}
249
250 newCoercionHole :: TcM CoercionHole
251 newCoercionHole
252 = do { u <- newUnique
253 ; traceTc "New coercion hole:" (ppr u)
254 ; ref <- newMutVar Nothing
255 ; return $ CoercionHole u ref }
256
257 -- | Put a value in a coercion hole
258 fillCoercionHole :: CoercionHole -> Coercion -> TcM ()
259 fillCoercionHole (CoercionHole u ref) co
260 = do {
261 #ifdef DEBUG
262 ; cts <- readTcRef ref
263 ; whenIsJust cts $ \old_co ->
264 pprPanic "Filling a filled coercion hole" (ppr u $$ ppr co $$ ppr old_co)
265 #endif
266 ; traceTc "Filling coercion hole" (ppr u <+> text ":=" <+> ppr co)
267 ; writeTcRef ref (Just co) }
268
269 -- | Is a coercion hole filled in?
270 isFilledCoercionHole :: CoercionHole -> TcM Bool
271 isFilledCoercionHole (CoercionHole _ ref) = isJust <$> readTcRef ref
272
273 -- | Retrieve the contents of a coercion hole. Panics if the hole
274 -- is unfilled
275 unpackCoercionHole :: CoercionHole -> TcM Coercion
276 unpackCoercionHole hole
277 = do { contents <- unpackCoercionHole_maybe hole
278 ; case contents of
279 Just co -> return co
280 Nothing -> pprPanic "Unfilled coercion hole" (ppr hole) }
281
282 -- | Retrieve the contents of a coercion hole, if it is filled
283 unpackCoercionHole_maybe :: CoercionHole -> TcM (Maybe Coercion)
284 unpackCoercionHole_maybe (CoercionHole _ ref) = readTcRef ref
285
286 -- | Check that a coercion is appropriate for filling a hole. (The hole
287 -- itself is needed only for printing. NB: This must be /lazy/ in the coercion,
288 -- as it's used in TcHsSyn in the presence of knots.
289 -- Always returns the checked coercion, but this return value is necessary
290 -- so that the input coercion is forced only when the output is forced.
291 checkCoercionHole :: Coercion -> CoercionHole -> Role -> Type -> Type -> TcM Coercion
292 checkCoercionHole co h r t1 t2
293 -- co is already zonked, but t1 and t2 might not be
294 | debugIsOn
295 = do { t1 <- zonkTcType t1
296 ; t2 <- zonkTcType t2
297 ; let (Pair _t1 _t2, _role) = coercionKindRole co
298 ; return $
299 ASSERT2( t1 `eqType` _t1 && t2 `eqType` _t2 && r == _role
300 , (text "Bad coercion hole" <+>
301 ppr h <> colon <+> vcat [ ppr _t1, ppr _t2, ppr _role
302 , ppr co, ppr t1, ppr t2
303 , ppr r ]) )
304 co }
305 | otherwise
306 = return co
307
308 {-
309 ************************************************************************
310 *
311 Expected types
312 *
313 ************************************************************************
314
315 Note [ExpType]
316 ~~~~~~~~~~~~~~
317
318 An ExpType is used as the "expected type" when type-checking an expression.
319 An ExpType can hold a "hole" that can be filled in by the type-checker.
320 This allows us to have one tcExpr that works in both checking mode and
321 synthesis mode (that is, bidirectional type-checking). Previously, this
322 was achieved by using ordinary unification variables, but we don't need
323 or want that generality. (For example, #11397 was caused by doing the
324 wrong thing with unification variables.) Instead, we observe that these
325 holes should
326
327 1. never be nested
328 2. never appear as the type of a variable
329 3. be used linearly (never be duplicated)
330
331 By defining ExpType, separately from Type, we can achieve goals 1 and 2
332 statically.
333
334 See also [wiki:Typechecking]
335
336 Note [TcLevel of ExpType]
337 ~~~~~~~~~~~~~~~~~~~~~~~~~
338 Consider
339
340 data G a where
341 MkG :: G Bool
342
343 foo MkG = True
344
345 This is a classic untouchable-variable / ambiguous GADT return type
346 scenario. But, with ExpTypes, we'll be inferring the type of the RHS.
347 And, because there is only one branch of the case, we won't trigger
348 Note [Case branches must never infer a non-tau type] of TcMatches.
349 We thus must track a TcLevel in an Inferring ExpType. If we try to
350 fill the ExpType and find that the TcLevels don't work out, we
351 fill the ExpType with a tau-tv at the low TcLevel, hopefully to
352 be worked out later by some means. This is triggered in
353 test gadt/gadt-escape1.
354
355 -}
356
357 -- actual data definition is in TcType
358
359 -- | Make an 'ExpType' suitable for inferring a type of kind * or #.
360 newInferExpTypeNoInst :: TcM ExpSigmaType
361 newInferExpTypeNoInst = newInferExpType False
362
363 newInferExpTypeInst :: TcM ExpRhoType
364 newInferExpTypeInst = newInferExpType True
365
366 newInferExpType :: Bool -> TcM ExpType
367 newInferExpType inst
368 = do { u <- newUnique
369 ; tclvl <- getTcLevel
370 ; traceTc "newOpenInferExpType" (ppr u <+> ppr inst <+> ppr tclvl)
371 ; ref <- newMutVar Nothing
372 ; return (Infer (IR { ir_uniq = u, ir_lvl = tclvl
373 , ir_ref = ref, ir_inst = inst })) }
374
375 -- | Extract a type out of an ExpType, if one exists. But one should always
376 -- exist. Unless you're quite sure you know what you're doing.
377 readExpType_maybe :: ExpType -> TcM (Maybe TcType)
378 readExpType_maybe (Check ty) = return (Just ty)
379 readExpType_maybe (Infer (IR { ir_ref = ref})) = readMutVar ref
380
381 -- | Extract a type out of an ExpType. Otherwise, panics.
382 readExpType :: ExpType -> TcM TcType
383 readExpType exp_ty
384 = do { mb_ty <- readExpType_maybe exp_ty
385 ; case mb_ty of
386 Just ty -> return ty
387 Nothing -> pprPanic "Unknown expected type" (ppr exp_ty) }
388
389 -- | Returns the expected type when in checking mode.
390 checkingExpType_maybe :: ExpType -> Maybe TcType
391 checkingExpType_maybe (Check ty) = Just ty
392 checkingExpType_maybe _ = Nothing
393
394 -- | Returns the expected type when in checking mode. Panics if in inference
395 -- mode.
396 checkingExpType :: String -> ExpType -> TcType
397 checkingExpType _ (Check ty) = ty
398 checkingExpType err et = pprPanic "checkingExpType" (text err $$ ppr et)
399
400 tauifyExpType :: ExpType -> TcM ExpType
401 -- ^ Turn a (Infer hole) type into a (Check alpha),
402 -- where alpha is a fresh unification variable
403 tauifyExpType (Check ty) = return (Check ty) -- No-op for (Check ty)
404 tauifyExpType (Infer inf_res) = do { ty <- inferResultToType inf_res
405 ; return (Check ty) }
406
407 -- | Extracts the expected type if there is one, or generates a new
408 -- TauTv if there isn't.
409 expTypeToType :: ExpType -> TcM TcType
410 expTypeToType (Check ty) = return ty
411 expTypeToType (Infer inf_res) = inferResultToType inf_res
412
413 inferResultToType :: InferResult -> TcM Type
414 inferResultToType (IR { ir_uniq = u, ir_lvl = tc_lvl
415 , ir_ref = ref })
416 = do { rr <- newMetaTyVarTyAtLevel tc_lvl runtimeRepTy
417 ; tau <- newMetaTyVarTyAtLevel tc_lvl (tYPE rr)
418 -- See Note [TcLevel of ExpType]
419 ; writeMutVar ref (Just tau)
420 ; traceTc "Forcing ExpType to be monomorphic:"
421 (ppr u <+> text ":=" <+> ppr tau)
422 ; return tau }
423
424
425 {- *********************************************************************
426 * *
427 SkolemTvs (immutable)
428 * *
429 ********************************************************************* -}
430
431 tcInstType :: ([TyVar] -> TcM (TCvSubst, [TcTyVar]))
432 -- ^ How to instantiate the type variables
433 -> Id -- ^ Type to instantiate
434 -> TcM ([(Name, TcTyVar)], TcThetaType, TcType) -- ^ Result
435 -- (type vars, preds (incl equalities), rho)
436 tcInstType inst_tyvars id
437 = case tcSplitForAllTys (idType id) of
438 ([], rho) -> let -- There may be overloading despite no type variables;
439 -- (?x :: Int) => Int -> Int
440 (theta, tau) = tcSplitPhiTy rho
441 in
442 return ([], theta, tau)
443
444 (tyvars, rho) -> do { (subst, tyvars') <- inst_tyvars tyvars
445 ; let (theta, tau) = tcSplitPhiTy (substTyAddInScope subst rho)
446 tv_prs = map tyVarName tyvars `zip` tyvars'
447 ; return (tv_prs, theta, tau) }
448
449 tcSkolDFunType :: DFunId -> TcM ([TcTyVar], TcThetaType, TcType)
450 -- Instantiate a type signature with skolem constants.
451 -- We could give them fresh names, but no need to do so
452 tcSkolDFunType dfun
453 = do { (tv_prs, theta, tau) <- tcInstType tcInstSuperSkolTyVars dfun
454 ; return (map snd tv_prs, theta, tau) }
455
456 tcSuperSkolTyVars :: [TyVar] -> (TCvSubst, [TcTyVar])
457 -- Make skolem constants, but do *not* give them new names, as above
458 -- Moreover, make them "super skolems"; see comments with superSkolemTv
459 -- see Note [Kind substitution when instantiating]
460 -- Precondition: tyvars should be ordered by scoping
461 tcSuperSkolTyVars = mapAccumL tcSuperSkolTyVar emptyTCvSubst
462
463 tcSuperSkolTyVar :: TCvSubst -> TyVar -> (TCvSubst, TcTyVar)
464 tcSuperSkolTyVar subst tv
465 = (extendTvSubstWithClone subst tv new_tv, new_tv)
466 where
467 kind = substTyUnchecked subst (tyVarKind tv)
468 new_tv = mkTcTyVar (tyVarName tv) kind superSkolemTv
469
470 -- | Given a list of @['TyVar']@, skolemize the type variables,
471 -- returning a substitution mapping the original tyvars to the
472 -- skolems, and the list of newly bound skolems. See also
473 -- tcInstSkolTyVars' for a precondition. The resulting
474 -- skolems are non-overlappable; see Note [Overlap and deriving]
475 -- for an example where this matters.
476 tcInstSkolTyVars :: [TyVar] -> TcM (TCvSubst, [TcTyVar])
477 tcInstSkolTyVars = tcInstSkolTyVars' False emptyTCvSubst
478
479 tcInstSuperSkolTyVars :: [TyVar] -> TcM (TCvSubst, [TcTyVar])
480 tcInstSuperSkolTyVars = tcInstSuperSkolTyVarsX emptyTCvSubst
481
482 tcInstSuperSkolTyVarsX :: TCvSubst -> [TyVar] -> TcM (TCvSubst, [TcTyVar])
483 tcInstSuperSkolTyVarsX subst = tcInstSkolTyVars' True subst
484
485 tcInstSkolTyVars' :: Bool -> TCvSubst -> [TyVar] -> TcM (TCvSubst, [TcTyVar])
486 -- Precondition: tyvars should be ordered (kind vars first)
487 -- see Note [Kind substitution when instantiating]
488 -- Get the location from the monad; this is a complete freshening operation
489 tcInstSkolTyVars' overlappable subst tvs
490 = do { loc <- getSrcSpanM
491 ; lvl <- getTcLevel
492 ; instSkolTyCoVarsX (mkTcSkolTyVar lvl loc overlappable) subst tvs }
493
494 mkTcSkolTyVar :: TcLevel -> SrcSpan -> Bool -> TcTyVarMaker
495 mkTcSkolTyVar lvl loc overlappable
496 = \ uniq old_name kind -> mkTcTyVar (mkInternalName uniq (getOccName old_name) loc)
497 kind details
498 where
499 details = SkolemTv (pushTcLevel lvl) overlappable
500 -- NB: skolems bump the level
501
502 tcInstSigTyVars :: SrcSpan -> [TyVar]
503 -> TcM (TCvSubst, [TcTyVar])
504 tcInstSigTyVars loc tvs
505 = do { lvl <- getTcLevel
506 ; instSkolTyCoVars (mkTcSkolTyVar lvl loc False) tvs }
507
508 ------------------
509 freshenTyVarBndrs :: [TyVar] -> TcRnIf gbl lcl (TCvSubst, [TyVar])
510 -- ^ Give fresh uniques to a bunch of TyVars, but they stay
511 -- as TyVars, rather than becoming TcTyVars
512 -- Used in FamInst.newFamInst, and Inst.newClsInst
513 freshenTyVarBndrs = instSkolTyCoVars mk_tv
514 where
515 mk_tv uniq old_name kind = mkTyVar (setNameUnique old_name uniq) kind
516
517 freshenCoVarBndrsX :: TCvSubst -> [CoVar] -> TcRnIf gbl lcl (TCvSubst, [CoVar])
518 -- ^ Give fresh uniques to a bunch of CoVars
519 -- Used in FamInst.newFamInst
520 freshenCoVarBndrsX subst = instSkolTyCoVarsX mk_cv subst
521 where
522 mk_cv uniq old_name kind = mkCoVar (setNameUnique old_name uniq) kind
523
524 ------------------
525 type TcTyVarMaker = Unique -> Name -> Kind -> TyCoVar
526 instSkolTyCoVars :: TcTyVarMaker -> [TyVar] -> TcRnIf gbl lcl (TCvSubst, [TyCoVar])
527 instSkolTyCoVars mk_tcv = instSkolTyCoVarsX mk_tcv emptyTCvSubst
528
529 instSkolTyCoVarsX :: TcTyVarMaker
530 -> TCvSubst -> [TyCoVar] -> TcRnIf gbl lcl (TCvSubst, [TyCoVar])
531 instSkolTyCoVarsX mk_tcv = mapAccumLM (instSkolTyCoVarX mk_tcv)
532
533 instSkolTyCoVarX :: TcTyVarMaker
534 -> TCvSubst -> TyCoVar -> TcRnIf gbl lcl (TCvSubst, TyCoVar)
535 instSkolTyCoVarX mk_tcv subst tycovar
536 = do { uniq <- newUnique -- using a new unique is critical. See
537 -- Note [Skolems in zonkSyntaxExpr] in TcHsSyn
538 ; let new_tcv = mk_tcv uniq old_name kind
539 subst1 | isTyVar new_tcv
540 = extendTvSubstWithClone subst tycovar new_tcv
541 | otherwise
542 = extendCvSubstWithClone subst tycovar new_tcv
543 ; return (subst1, new_tcv) }
544 where
545 old_name = tyVarName tycovar
546 kind = substTyUnchecked subst (tyVarKind tycovar)
547
548 newFskTyVar :: TcType -> TcM TcTyVar
549 newFskTyVar fam_ty
550 = do { uniq <- newUnique
551 ; let name = mkSysTvName uniq (fsLit "fsk")
552 ; return (mkTcTyVar name (typeKind fam_ty) (FlatSkol fam_ty)) }
553 {-
554 Note [Kind substitution when instantiating]
555 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
556 When we instantiate a bunch of kind and type variables, first we
557 expect them to be topologically sorted.
558 Then we have to instantiate the kind variables, build a substitution
559 from old variables to the new variables, then instantiate the type
560 variables substituting the original kind.
561
562 Exemple: If we want to instantiate
563 [(k1 :: *), (k2 :: *), (a :: k1 -> k2), (b :: k1)]
564 we want
565 [(?k1 :: *), (?k2 :: *), (?a :: ?k1 -> ?k2), (?b :: ?k1)]
566 instead of the buggous
567 [(?k1 :: *), (?k2 :: *), (?a :: k1 -> k2), (?b :: k1)]
568
569
570 ************************************************************************
571 * *
572 MetaTvs (meta type variables; mutable)
573 * *
574 ************************************************************************
575 -}
576
577 mkMetaTyVarName :: Unique -> FastString -> Name
578 -- Makes a /System/ Name, which is eagerly eliminated by
579 -- the unifier; see TcUnify.nicer_to_update_tv1, and
580 -- TcCanonical.canEqTyVarTyVar (nicer_to_update_tv2)
581 mkMetaTyVarName uniq str = mkSysTvName uniq str
582
583 newSigTyVar :: Name -> Kind -> TcM TcTyVar
584 newSigTyVar name kind
585 = do { details <- newMetaDetails SigTv
586 ; return (mkTcTyVar name kind details) }
587
588 newFmvTyVar :: TcType -> TcM TcTyVar
589 -- Very like newMetaTyVar, except sets mtv_tclvl to one less
590 -- so that the fmv is untouchable.
591 newFmvTyVar fam_ty
592 = do { uniq <- newUnique
593 ; ref <- newMutVar Flexi
594 ; cur_lvl <- getTcLevel
595 ; let details = MetaTv { mtv_info = FlatMetaTv
596 , mtv_ref = ref
597 , mtv_tclvl = fmvTcLevel cur_lvl }
598 name = mkMetaTyVarName uniq (fsLit "s")
599 ; return (mkTcTyVar name (typeKind fam_ty) details) }
600
601 newMetaDetails :: MetaInfo -> TcM TcTyVarDetails
602 newMetaDetails info
603 = do { ref <- newMutVar Flexi
604 ; tclvl <- getTcLevel
605 ; return (MetaTv { mtv_info = info
606 , mtv_ref = ref
607 , mtv_tclvl = tclvl }) }
608
609 cloneMetaTyVar :: TcTyVar -> TcM TcTyVar
610 cloneMetaTyVar tv
611 = ASSERT( isTcTyVar tv )
612 do { uniq <- newUnique
613 ; ref <- newMutVar Flexi
614 ; let name' = setNameUnique (tyVarName tv) uniq
615 details' = case tcTyVarDetails tv of
616 details@(MetaTv {}) -> details { mtv_ref = ref }
617 _ -> pprPanic "cloneMetaTyVar" (ppr tv)
618 ; return (mkTcTyVar name' (tyVarKind tv) details') }
619
620 -- Works for both type and kind variables
621 readMetaTyVar :: TyVar -> TcM MetaDetails
622 readMetaTyVar tyvar = ASSERT2( isMetaTyVar tyvar, ppr tyvar )
623 readMutVar (metaTyVarRef tyvar)
624
625 isFilledMetaTyVar :: TyVar -> TcM Bool
626 -- True of a filled-in (Indirect) meta type variable
627 isFilledMetaTyVar tv
628 | MetaTv { mtv_ref = ref } <- tcTyVarDetails tv
629 = do { details <- readMutVar ref
630 ; return (isIndirect details) }
631 | otherwise = return False
632
633 isUnfilledMetaTyVar :: TyVar -> TcM Bool
634 -- True of a un-filled-in (Flexi) meta type variable
635 isUnfilledMetaTyVar tv
636 | MetaTv { mtv_ref = ref } <- tcTyVarDetails tv
637 = do { details <- readMutVar ref
638 ; return (isFlexi details) }
639 | otherwise = return False
640
641 --------------------
642 -- Works with both type and kind variables
643 writeMetaTyVar :: TcTyVar -> TcType -> TcM ()
644 -- Write into a currently-empty MetaTyVar
645
646 writeMetaTyVar tyvar ty
647 | not debugIsOn
648 = writeMetaTyVarRef tyvar (metaTyVarRef tyvar) ty
649
650 -- Everything from here on only happens if DEBUG is on
651 | not (isTcTyVar tyvar)
652 = WARN( True, text "Writing to non-tc tyvar" <+> ppr tyvar )
653 return ()
654
655 | MetaTv { mtv_ref = ref } <- tcTyVarDetails tyvar
656 = writeMetaTyVarRef tyvar ref ty
657
658 | otherwise
659 = WARN( True, text "Writing to non-meta tyvar" <+> ppr tyvar )
660 return ()
661
662 --------------------
663 writeMetaTyVarRef :: TcTyVar -> TcRef MetaDetails -> TcType -> TcM ()
664 -- Here the tyvar is for error checking only;
665 -- the ref cell must be for the same tyvar
666 writeMetaTyVarRef tyvar ref ty
667 | not debugIsOn
668 = do { traceTc "writeMetaTyVar" (ppr tyvar <+> dcolon <+> ppr (tyVarKind tyvar)
669 <+> text ":=" <+> ppr ty)
670 ; writeTcRef ref (Indirect ty) }
671
672 -- Everything from here on only happens if DEBUG is on
673 | otherwise
674 = do { meta_details <- readMutVar ref;
675 -- Zonk kinds to allow the error check to work
676 ; zonked_tv_kind <- zonkTcType tv_kind
677 ; zonked_ty_kind <- zonkTcType ty_kind
678 ; let kind_check_ok = isPredTy tv_kind -- Don't check kinds for updates
679 -- to coercion variables
680 || tcEqKind zonked_ty_kind zonked_tv_kind
681
682 kind_msg = hang (text "Ill-kinded update to meta tyvar")
683 2 ( ppr tyvar <+> text "::" <+> (ppr tv_kind $$ ppr zonked_tv_kind)
684 <+> text ":="
685 <+> ppr ty <+> text "::" <+> (ppr ty_kind $$ ppr zonked_ty_kind) )
686
687 ; traceTc "writeMetaTyVar" (ppr tyvar <+> text ":=" <+> ppr ty)
688
689 -- Check for double updates
690 ; MASSERT2( isFlexi meta_details, double_upd_msg meta_details )
691
692 -- Check for level OK
693 -- See Note [Level check when unifying]
694 ; MASSERT2( level_check_ok, level_check_msg )
695
696 -- Check Kinds ok
697 ; MASSERT2( kind_check_ok, kind_msg )
698
699 -- Do the write
700 ; writeMutVar ref (Indirect ty) }
701 where
702 tv_kind = tyVarKind tyvar
703 ty_kind = typeKind ty
704
705 tv_lvl = tcTyVarLevel tyvar
706 ty_lvl = tcTypeLevel ty
707
708 level_check_ok = isFmvTyVar tyvar
709 || not (ty_lvl `strictlyDeeperThan` tv_lvl)
710 level_check_msg = ppr ty_lvl $$ ppr tv_lvl $$ ppr tyvar $$ ppr ty
711
712 double_upd_msg details = hang (text "Double update of meta tyvar")
713 2 (ppr tyvar $$ ppr details)
714
715
716 {- Note [Level check when unifying]
717 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
718 When unifying
719 alpha:lvl := ty
720 we expect that the TcLevel of 'ty' will be <= lvl.
721 However, during unflatting we do
722 fuv:l := ty:(l+1)
723 which is usually wrong; hence the check isFmmvTyVar in level_check_ok.
724 See Note [TcLevel assignment] in TcType.
725 -}
726
727 {-
728 % Generating fresh variables for pattern match check
729 -}
730
731 -- UNINSTANTIATED VERSION OF tcInstSkolTyCoVars
732 genInstSkolTyVarsX :: SrcSpan -> TCvSubst -> [TyVar]
733 -> TcRnIf gbl lcl (TCvSubst, [TcTyVar])
734 -- Precondition: tyvars should be scoping-ordered
735 -- see Note [Kind substitution when instantiating]
736 -- Get the location from the monad; this is a complete freshening operation
737 genInstSkolTyVarsX loc subst tvs
738 = instSkolTyCoVarsX (mkTcSkolTyVar topTcLevel loc False) subst tvs
739
740 {-
741 ************************************************************************
742 * *
743 MetaTvs: TauTvs
744 * *
745 ************************************************************************
746
747 Note [Never need to instantiate coercion variables]
748 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
749 With coercion variables sloshing around in types, it might seem that we
750 sometimes need to instantiate coercion variables. This would be problematic,
751 because coercion variables inhabit unboxed equality (~#), and the constraint
752 solver thinks in terms only of boxed equality (~). The solution is that
753 we never need to instantiate coercion variables in the first place.
754
755 The tyvars that we need to instantiate come from the types of functions,
756 data constructors, and patterns. These will never be quantified over
757 coercion variables, except for the special case of the promoted Eq#. But,
758 that can't ever appear in user code, so we're safe!
759 -}
760
761 newAnonMetaTyVar :: MetaInfo -> Kind -> TcM TcTyVar
762 -- Make a new meta tyvar out of thin air
763 newAnonMetaTyVar meta_info kind
764 = do { uniq <- newUnique
765 ; let name = mkMetaTyVarName uniq s
766 s = case meta_info of
767 TauTv -> fsLit "t"
768 FlatMetaTv -> fsLit "fmv"
769 SigTv -> fsLit "a"
770 ; details <- newMetaDetails meta_info
771 ; return (mkTcTyVar name kind details) }
772
773 newFlexiTyVar :: Kind -> TcM TcTyVar
774 newFlexiTyVar kind = newAnonMetaTyVar TauTv kind
775
776 newFlexiTyVarTy :: Kind -> TcM TcType
777 newFlexiTyVarTy kind = do
778 tc_tyvar <- newFlexiTyVar kind
779 return (mkTyVarTy tc_tyvar)
780
781 newFlexiTyVarTys :: Int -> Kind -> TcM [TcType]
782 newFlexiTyVarTys n kind = mapM newFlexiTyVarTy (nOfThem n kind)
783
784 newOpenTypeKind :: TcM TcKind
785 newOpenTypeKind
786 = do { rr <- newFlexiTyVarTy runtimeRepTy
787 ; return (tYPE rr) }
788
789 -- | Create a tyvar that can be a lifted or unlifted type.
790 -- Returns alpha :: TYPE kappa, where both alpha and kappa are fresh
791 newOpenFlexiTyVarTy :: TcM TcType
792 newOpenFlexiTyVarTy
793 = do { kind <- newOpenTypeKind
794 ; newFlexiTyVarTy kind }
795
796 newMetaSigTyVars :: [TyVar] -> TcM (TCvSubst, [TcTyVar])
797 newMetaSigTyVars = mapAccumLM newMetaSigTyVarX emptyTCvSubst
798
799 newMetaTyVars :: [TyVar] -> TcM (TCvSubst, [TcTyVar])
800 -- Instantiate with META type variables
801 -- Note that this works for a sequence of kind, type, and coercion variables
802 -- variables. Eg [ (k:*), (a:k->k) ]
803 -- Gives [ (k7:*), (a8:k7->k7) ]
804 newMetaTyVars = mapAccumLM newMetaTyVarX emptyTCvSubst
805 -- emptyTCvSubst has an empty in-scope set, but that's fine here
806 -- Since the tyvars are freshly made, they cannot possibly be
807 -- captured by any existing for-alls.
808
809 newMetaTyVarX :: TCvSubst -> TyVar -> TcM (TCvSubst, TcTyVar)
810 -- Make a new unification variable tyvar whose Name and Kind come from
811 -- an existing TyVar. We substitute kind variables in the kind.
812 newMetaTyVarX subst tyvar = new_meta_tv_x TauTv subst tyvar
813
814 newMetaSigTyVarX :: TCvSubst -> TyVar -> TcM (TCvSubst, TcTyVar)
815 -- Just like newMetaTyVarX, but make a SigTv
816 newMetaSigTyVarX subst tyvar = new_meta_tv_x SigTv subst tyvar
817
818 newWildCardX :: TCvSubst -> TyVar -> TcM (TCvSubst, TcTyVar)
819 newWildCardX subst tv
820 = do { new_tv <- newAnonMetaTyVar TauTv (substTy subst (tyVarKind tv))
821 ; return (extendTvSubstWithClone subst tv new_tv, new_tv) }
822
823 new_meta_tv_x :: MetaInfo -> TCvSubst -> TyVar -> TcM (TCvSubst, TcTyVar)
824 new_meta_tv_x info subst tv
825 = do { uniq <- newUnique
826 ; details <- newMetaDetails info
827 ; let name = mkSystemName uniq (getOccName tv)
828 -- See Note [Name of an instantiated type variable]
829 kind = substTyUnchecked subst (tyVarKind tv)
830 -- Unchecked because we call newMetaTyVarX from
831 -- tcInstBinderX, which is called from tc_infer_args
832 -- which does not yet take enough trouble to ensure
833 -- the in-scope set is right; e.g. Trac #12785 trips
834 -- if we use substTy here
835 new_tv = mkTcTyVar name kind details
836 subst1 = extendTvSubstWithClone subst tv new_tv
837 ; return (subst1, new_tv) }
838
839 newMetaTyVarTyAtLevel :: TcLevel -> TcKind -> TcM TcType
840 newMetaTyVarTyAtLevel tc_lvl kind
841 = do { uniq <- newUnique
842 ; ref <- newMutVar Flexi
843 ; let name = mkMetaTyVarName uniq (fsLit "p")
844 details = MetaTv { mtv_info = TauTv
845 , mtv_ref = ref
846 , mtv_tclvl = tc_lvl }
847 ; return (mkTyVarTy (mkTcTyVar name kind details)) }
848
849 {- Note [Name of an instantiated type variable]
850 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
851 At the moment we give a unification variable a System Name, which
852 influences the way it is tidied; see TypeRep.tidyTyVarBndr.
853
854 ************************************************************************
855 * *
856 Quantification
857 * *
858 ************************************************************************
859
860 Note [quantifyTyVars]
861 ~~~~~~~~~~~~~~~~~~~~~
862 quantifyTyVars is given the free vars of a type that we
863 are about to wrap in a forall.
864
865 It takes these free type/kind variables (partitioned into dependent and
866 non-dependent variables) and
867 1. Zonks them and remove globals and covars
868 2. Extends kvs1 with free kind vars in the kinds of tvs (removing globals)
869 3. Calls zonkQuantifiedTyVar on each
870
871 Step (2) is often unimportant, because the kind variable is often
872 also free in the type. Eg
873 Typeable k (a::k)
874 has free vars {k,a}. But the type (see Trac #7916)
875 (f::k->*) (a::k)
876 has free vars {f,a}, but we must add 'k' as well! Hence step (3).
877
878 * This function distinguishes between dependent and non-dependent
879 variables only to keep correct defaulting behavior with -XNoPolyKinds.
880 With -XPolyKinds, it treats both classes of variables identically.
881
882 * quantifyTyVars never quantifies over
883 - a coercion variable
884 - a runtime-rep variable
885
886 Note [quantifyTyVars determinism]
887 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
888 The results of quantifyTyVars are wrapped in a forall and can end up in the
889 interface file. One such example is inferred type signatures. They also affect
890 the results of optimizations, for example worker-wrapper. This means that to
891 get deterministic builds quantifyTyVars needs to be deterministic.
892
893 To achieve this TcDepVars is backed by deterministic sets which allows them
894 to be later converted to a list in a deterministic order.
895
896 For more information about deterministic sets see
897 Note [Deterministic UniqFM] in UniqDFM.
898 -}
899
900 quantifyTyVars, quantifyZonkedTyVars
901 :: TcTyCoVarSet -- global tvs
902 -> TcDepVars -- See Note [Dependent type variables] in TcType
903 -> TcM [TcTyVar]
904 -- See Note [quantifyTyVars]
905 -- Can be given a mixture of TcTyVars and TyVars, in the case of
906 -- associated type declarations. Also accepts covars, but *never* returns any.
907
908 -- The zonked variant assumes everything is already zonked.
909
910 quantifyTyVars gbl_tvs (DV { dv_kvs = dep_tkvs, dv_tvs = nondep_tkvs })
911 = do { dep_tkvs <- zonkTyCoVarsAndFVDSet dep_tkvs
912 ; nondep_tkvs <- zonkTyCoVarsAndFVDSet nondep_tkvs
913 ; gbl_tvs <- zonkTyCoVarsAndFV gbl_tvs
914 ; quantifyZonkedTyVars gbl_tvs (DV { dv_kvs = dep_tkvs, dv_tvs = nondep_tkvs }) }
915
916 quantifyZonkedTyVars gbl_tvs dvs@(DV{ dv_kvs = dep_tkvs, dv_tvs = nondep_tkvs })
917 = do { traceTc "quantifyZonkedTyVars" (vcat [ppr dvs, ppr gbl_tvs])
918 ; let all_cvs = filterVarSet isCoVar $ dVarSetToVarSet dep_tkvs
919 dep_kvs = dVarSetElemsWellScoped $
920 dep_tkvs `dVarSetMinusVarSet` gbl_tvs
921 `dVarSetMinusVarSet` closeOverKinds all_cvs
922 -- dVarSetElemsWellScoped: put the kind variables into
923 -- well-scoped order.
924 -- E.g. [k, (a::k)] not the other way roud
925 -- closeOverKinds all_cvs: do not quantify over coercion
926 -- variables, or any any tvs that a covar depends on
927
928 nondep_tvs = dVarSetElems $
929 (nondep_tkvs `minusDVarSet` dep_tkvs)
930 `dVarSetMinusVarSet` gbl_tvs
931 -- See Note [Dependent type variables] in TcType
932 -- The `minus` dep_tkvs removes any kind-level vars
933 -- e.g. T k (a::k) Since k appear in a kind it'll
934 -- be in dv_kvs, and is dependent. So remove it from
935 -- dv_tvs which will also contain k
936 -- No worry about dependent covars here;
937 -- they are all in dep_tkvs
938 -- No worry about scoping, because these are all
939 -- type variables
940 -- NB kinds of tvs are zonked by zonkTyCoVarsAndFV
941
942 -- In the non-PolyKinds case, default the kind variables
943 -- to *, and zonk the tyvars as usual. Notice that this
944 -- may make quantifyTyVars return a shorter list
945 -- than it was passed, but that's ok
946 ; poly_kinds <- xoptM LangExt.PolyKinds
947 ; dep_kvs' <- mapMaybeM (zonk_quant (not poly_kinds)) dep_kvs
948 ; nondep_tvs' <- mapMaybeM (zonk_quant False) nondep_tvs
949 -- Because of the order, any kind variables
950 -- mentioned in the kinds of the nondep_tvs'
951 -- now refer to the dep_kvs'
952
953 ; traceTc "quantifyTyVars"
954 (vcat [ text "globals:" <+> ppr gbl_tvs
955 , text "nondep:" <+> ppr nondep_tvs
956 , text "dep:" <+> ppr dep_kvs
957 , text "dep_kvs'" <+> ppr dep_kvs'
958 , text "nondep_tvs'" <+> ppr nondep_tvs' ])
959
960 ; return (dep_kvs' ++ nondep_tvs') }
961 where
962 zonk_quant default_kind tkv
963 | isTcTyVar tkv = zonkQuantifiedTyVar default_kind tkv
964 | otherwise = return $ Just tkv
965 -- For associated types, we have the class variables
966 -- in scope, and they are TyVars not TcTyVars
967
968 zonkQuantifiedTyVar :: Bool -- True <=> this is a kind var and -XNoPolyKinds
969 -- False <=> not a kind var or -XPolyKinds
970 -> TcTyVar
971 -> TcM (Maybe TcTyVar)
972 -- The quantified type variables often include meta type variables
973 -- we want to freeze them into ordinary type variables, and
974 -- default their kind (e.g. from TYPE v to TYPE Lifted)
975 -- The meta tyvar is updated to point to the new skolem TyVar. Now any
976 -- bound occurrences of the original type variable will get zonked to
977 -- the immutable version.
978 --
979 -- We leave skolem TyVars alone; they are immutable.
980 --
981 -- This function is called on both kind and type variables,
982 -- but kind variables *only* if PolyKinds is on.
983 --
984 -- This returns a tyvar if it should be quantified over;
985 -- otherwise, it returns Nothing. The latter case happens for
986 -- * Kind variables, with -XNoPolyKinds: don't quantify over these
987 -- * RuntimeRep variables: we never quantify over these
988
989 zonkQuantifiedTyVar default_kind tv
990 = case tcTyVarDetails tv of
991 SkolemTv {} -> do { kind <- zonkTcType (tyVarKind tv)
992 ; return $ Just (setTyVarKind tv kind) }
993 -- It might be a skolem type variable,
994 -- for example from a user type signature
995
996 MetaTv { mtv_ref = ref }
997 -> do { when debugIsOn (check_empty ref)
998 ; zonk_meta_tv tv }
999
1000 _other -> pprPanic "zonkQuantifiedTyVar" (ppr tv) -- FlatSkol, RuntimeUnk
1001
1002 where
1003 zonk_meta_tv :: TcTyVar -> TcM (Maybe TcTyVar)
1004 zonk_meta_tv tv
1005 | isRuntimeRepVar tv -- Never quantify over a RuntimeRep var
1006 = do { writeMetaTyVar tv ptrRepLiftedTy
1007 ; return Nothing }
1008
1009 | default_kind -- -XNoPolyKinds and this is a kind var
1010 = do { _ <- default_kind_var tv
1011 ; return Nothing }
1012
1013 | otherwise
1014 = do { tv' <- skolemiseUnboundMetaTyVar tv
1015 ; return (Just tv') }
1016
1017 default_kind_var :: TyVar -> TcM Type
1018 -- defaultKindVar is used exclusively with -XNoPolyKinds
1019 -- See Note [Defaulting with -XNoPolyKinds]
1020 -- It takes an (unconstrained) meta tyvar and defaults it.
1021 -- Works only on vars of type *; for other kinds, it issues an error.
1022 default_kind_var kv
1023 | isStarKind (tyVarKind kv)
1024 = do { writeMetaTyVar kv liftedTypeKind
1025 ; return liftedTypeKind }
1026 | otherwise
1027 = do { addErr (vcat [ text "Cannot default kind variable" <+> quotes (ppr kv')
1028 , text "of kind:" <+> ppr (tyVarKind kv')
1029 , text "Perhaps enable PolyKinds or add a kind signature" ])
1030 ; return (mkTyVarTy kv) }
1031 where
1032 (_, kv') = tidyOpenTyCoVar emptyTidyEnv kv
1033
1034 check_empty ref -- [Sept 04] Check for non-empty.
1035 = when debugIsOn $ -- See note [Silly Type Synonym]
1036 do { cts <- readMutVar ref
1037 ; case cts of
1038 Flexi -> return ()
1039 Indirect ty -> WARN( True, ppr tv $$ ppr ty )
1040 return () }
1041
1042 skolemiseRuntimeUnk :: TcTyVar -> TcM TyVar
1043 skolemiseRuntimeUnk tv
1044 = skolemise_tv tv RuntimeUnk
1045
1046 skolemiseUnboundMetaTyVar :: TcTyVar -> TcM TyVar
1047 skolemiseUnboundMetaTyVar tv
1048 = skolemise_tv tv (SkolemTv (metaTyVarTcLevel tv) False)
1049
1050 skolemise_tv :: TcTyVar -> TcTyVarDetails -> TcM TyVar
1051 -- We have a Meta tyvar with a ref-cell inside it
1052 -- Skolemise it, so that
1053 -- we are totally out of Meta-tyvar-land
1054 -- We create a skolem TyVar, not a regular TyVar
1055 -- See Note [Zonking to Skolem]
1056 skolemise_tv tv details
1057 = ASSERT2( isMetaTyVar tv, ppr tv )
1058 do { span <- getSrcSpanM -- Get the location from "here"
1059 -- ie where we are generalising
1060 ; kind <- zonkTcType (tyVarKind tv)
1061 ; let uniq = getUnique tv
1062 -- NB: Use same Unique as original tyvar. This is
1063 -- important for TcHsType.splitTelescopeTvs to work properly
1064
1065 tv_name = getOccName tv
1066 final_name = mkInternalName uniq tv_name span
1067 final_tv = mkTcTyVar final_name kind details
1068
1069 ; traceTc "Skolemising" (ppr tv <+> text ":=" <+> ppr final_tv)
1070 ; writeMetaTyVar tv (mkTyVarTy final_tv)
1071 ; return final_tv }
1072
1073 {- Note [Defaulting with -XNoPolyKinds]
1074 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1075 Consider
1076
1077 data Compose f g a = Mk (f (g a))
1078
1079 We infer
1080
1081 Compose :: forall k1 k2. (k2 -> *) -> (k1 -> k2) -> k1 -> *
1082 Mk :: forall k1 k2 (f :: k2 -> *) (g :: k1 -> k2) (a :: k1).
1083 f (g a) -> Compose k1 k2 f g a
1084
1085 Now, in another module, we have -XNoPolyKinds -XDataKinds in effect.
1086 What does 'Mk mean? Pre GHC-8.0 with -XNoPolyKinds,
1087 we just defaulted all kind variables to *. But that's no good here,
1088 because the kind variables in 'Mk aren't of kind *, so defaulting to *
1089 is ill-kinded.
1090
1091 After some debate on #11334, we decided to issue an error in this case.
1092 The code is in defaultKindVar.
1093
1094 Note [What is a meta variable?]
1095 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1096 A "meta type-variable", also know as a "unification variable" is a placeholder
1097 introduced by the typechecker for an as-yet-unknown monotype.
1098
1099 For example, when we see a call `reverse (f xs)`, we know that we calling
1100 reverse :: forall a. [a] -> [a]
1101 So we know that the argument `f xs` must be a "list of something". But what is
1102 the "something"? We don't know until we explore the `f xs` a bit more. So we set
1103 out what we do know at the call of `reverse` by instantiate its type with a fresh
1104 meta tyvar, `alpha` say. So now the type of the argument `f xs`, and of the
1105 result, is `[alpha]`. The unification variable `alpha` stands for the
1106 as-yet-unknown type of the elements of the list.
1107
1108 As type inference progresses we may learn more about `alpha`. For example, suppose
1109 `f` has the type
1110 f :: forall b. b -> [Maybe b]
1111 Then we instantiate `f`'s type with another fresh unification variable, say
1112 `beta`; and equate `f`'s result type with reverse's argument type, thus
1113 `[alpha] ~ [Maybe beta]`.
1114
1115 Now we can solve this equality to learn that `alpha ~ Maybe beta`, so we've
1116 refined our knowledge about `alpha`. And so on.
1117
1118 If you found this Note useful, you may also want to have a look at
1119 Section 5 of "Practical type inference for higher rank types" (Peyton Jones,
1120 Vytiniotis, Weirich and Shields. J. Functional Programming. 2011).
1121
1122 Note [What is zonking?]
1123 ~~~~~~~~~~~~~~~~~~~~~~~
1124 GHC relies heavily on mutability in the typechecker for efficient operation.
1125 For this reason, throughout much of the type checking process meta type
1126 variables (the MetaTv constructor of TcTyVarDetails) are represented by mutable
1127 variables (known as TcRefs).
1128
1129 Zonking is the process of ripping out these mutable variables and replacing them
1130 with a real TcType. This involves traversing the entire type expression, but the
1131 interesting part of replacing the mutable variables occurs in zonkTyVarOcc.
1132
1133 There are two ways to zonk a Type:
1134
1135 * zonkTcTypeToType, which is intended to be used at the end of type-checking
1136 for the final zonk. It has to deal with unfilled metavars, either by filling
1137 it with a value like Any or failing (determined by the UnboundTyVarZonker
1138 used).
1139
1140 * zonkTcType, which will happily ignore unfilled metavars. This is the
1141 appropriate function to use while in the middle of type-checking.
1142
1143 Note [Zonking to Skolem]
1144 ~~~~~~~~~~~~~~~~~~~~~~~~
1145 We used to zonk quantified type variables to regular TyVars. However, this
1146 leads to problems. Consider this program from the regression test suite:
1147
1148 eval :: Int -> String -> String -> String
1149 eval 0 root actual = evalRHS 0 root actual
1150
1151 evalRHS :: Int -> a
1152 evalRHS 0 root actual = eval 0 root actual
1153
1154 It leads to the deferral of an equality (wrapped in an implication constraint)
1155
1156 forall a. () => ((String -> String -> String) ~ a)
1157
1158 which is propagated up to the toplevel (see TcSimplify.tcSimplifyInferCheck).
1159 In the meantime `a' is zonked and quantified to form `evalRHS's signature.
1160 This has the *side effect* of also zonking the `a' in the deferred equality
1161 (which at this point is being handed around wrapped in an implication
1162 constraint).
1163
1164 Finally, the equality (with the zonked `a') will be handed back to the
1165 simplifier by TcRnDriver.tcRnSrcDecls calling TcSimplify.tcSimplifyTop.
1166 If we zonk `a' with a regular type variable, we will have this regular type
1167 variable now floating around in the simplifier, which in many places assumes to
1168 only see proper TcTyVars.
1169
1170 We can avoid this problem by zonking with a skolem. The skolem is rigid
1171 (which we require for a quantified variable), but is still a TcTyVar that the
1172 simplifier knows how to deal with.
1173
1174 Note [Silly Type Synonyms]
1175 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1176 Consider this:
1177 type C u a = u -- Note 'a' unused
1178
1179 foo :: (forall a. C u a -> C u a) -> u
1180 foo x = ...
1181
1182 bar :: Num u => u
1183 bar = foo (\t -> t + t)
1184
1185 * From the (\t -> t+t) we get type {Num d} => d -> d
1186 where d is fresh.
1187
1188 * Now unify with type of foo's arg, and we get:
1189 {Num (C d a)} => C d a -> C d a
1190 where a is fresh.
1191
1192 * Now abstract over the 'a', but float out the Num (C d a) constraint
1193 because it does not 'really' mention a. (see exactTyVarsOfType)
1194 The arg to foo becomes
1195 \/\a -> \t -> t+t
1196
1197 * So we get a dict binding for Num (C d a), which is zonked to give
1198 a = ()
1199 [Note Sept 04: now that we are zonking quantified type variables
1200 on construction, the 'a' will be frozen as a regular tyvar on
1201 quantification, so the floated dict will still have type (C d a).
1202 Which renders this whole note moot; happily!]
1203
1204 * Then the \/\a abstraction has a zonked 'a' in it.
1205
1206 All very silly. I think its harmless to ignore the problem. We'll end up with
1207 a \/\a in the final result but all the occurrences of a will be zonked to ()
1208
1209 ************************************************************************
1210 * *
1211 Zonking types
1212 * *
1213 ************************************************************************
1214
1215 -}
1216
1217 -- | @tcGetGlobalTyCoVars@ returns a fully-zonked set of *scoped* tyvars free in
1218 -- the environment. To improve subsequent calls to the same function it writes
1219 -- the zonked set back into the environment. Note that this returns all
1220 -- variables free in anything (term-level or type-level) in scope. We thus
1221 -- don't have to worry about clashes with things that are not in scope, because
1222 -- if they are reachable, then they'll be returned here.
1223 tcGetGlobalTyCoVars :: TcM TcTyVarSet
1224 tcGetGlobalTyCoVars
1225 = do { (TcLclEnv {tcl_tyvars = gtv_var}) <- getLclEnv
1226 ; gbl_tvs <- readMutVar gtv_var
1227 ; gbl_tvs' <- zonkTyCoVarsAndFV gbl_tvs
1228 ; writeMutVar gtv_var gbl_tvs'
1229 ; return gbl_tvs' }
1230
1231 -- | Zonk a type without using the smart constructors; the result type
1232 -- is available for inspection within the type-checking knot.
1233 zonkTcTypeInKnot :: TcType -> TcM TcType
1234 zonkTcTypeInKnot = mapType (zonkTcTypeMapper { tcm_smart = False }) ()
1235
1236 zonkTcTypeAndFV :: TcType -> TcM DTyCoVarSet
1237 -- Zonk a type and take its free variables
1238 -- With kind polymorphism it can be essential to zonk *first*
1239 -- so that we find the right set of free variables. Eg
1240 -- forall k1. forall (a:k2). a
1241 -- where k2:=k1 is in the substitution. We don't want
1242 -- k2 to look free in this type!
1243 -- NB: This might be called from within the knot, so don't use
1244 -- smart constructors. See Note [Zonking within the knot] in TcHsType
1245 zonkTcTypeAndFV ty
1246 = tyCoVarsOfTypeDSet <$> zonkTcTypeInKnot ty
1247
1248 -- | Zonk a type and call 'splitDepVarsOfType' on it.
1249 -- Works within the knot.
1250 zonkTcTypeAndSplitDepVars :: TcType -> TcM TcDepVars
1251 zonkTcTypeAndSplitDepVars ty
1252 = splitDepVarsOfType <$> zonkTcTypeInKnot ty
1253
1254 zonkTcTypesAndSplitDepVars :: [TcType] -> TcM TcDepVars
1255 zonkTcTypesAndSplitDepVars tys
1256 = splitDepVarsOfTypes <$> mapM zonkTcTypeInKnot tys
1257
1258 zonkTyCoVar :: TyCoVar -> TcM TcType
1259 -- Works on TyVars and TcTyVars
1260 zonkTyCoVar tv | isTcTyVar tv = zonkTcTyVar tv
1261 | isTyVar tv = mkTyVarTy <$> zonkTyCoVarKind tv
1262 | otherwise = ASSERT2( isCoVar tv, ppr tv )
1263 mkCoercionTy . mkCoVarCo <$> zonkTyCoVarKind tv
1264 -- Hackily, when typechecking type and class decls
1265 -- we have TyVars in scopeadded (only) in
1266 -- TcHsType.tcTyClTyVars, but it seems
1267 -- painful to make them into TcTyVars there
1268
1269 zonkTyCoVarsAndFV :: TyCoVarSet -> TcM TyCoVarSet
1270 zonkTyCoVarsAndFV tycovars =
1271 tyCoVarsOfTypes <$> mapM zonkTyCoVar (nonDetEltsUFM tycovars)
1272 -- It's OK to use nonDetEltsUFM here because we immediately forget about
1273 -- the ordering by turning it into a nondeterministic set and the order
1274 -- of zonking doesn't matter for determinism.
1275
1276 -- Takes a list of TyCoVars, zonks them and returns a
1277 -- deterministically ordered list of their free variables.
1278 zonkTyCoVarsAndFVList :: [TyCoVar] -> TcM [TyCoVar]
1279 zonkTyCoVarsAndFVList tycovars =
1280 tyCoVarsOfTypesList <$> mapM zonkTyCoVar tycovars
1281
1282 -- Takes a deterministic set of TyCoVars, zonks them and returns a
1283 -- deterministic set of their free variables.
1284 -- See Note [quantifyTyVars determinism].
1285 zonkTyCoVarsAndFVDSet :: DTyCoVarSet -> TcM DTyCoVarSet
1286 zonkTyCoVarsAndFVDSet tycovars =
1287 tyCoVarsOfTypesDSet <$> mapM zonkTyCoVar (dVarSetElems tycovars)
1288
1289 zonkTcTyVars :: [TcTyVar] -> TcM [TcType]
1290 zonkTcTyVars tyvars = mapM zonkTcTyVar tyvars
1291
1292 ----------------- Types
1293 zonkTyCoVarKind :: TyCoVar -> TcM TyCoVar
1294 zonkTyCoVarKind tv = do { kind' <- zonkTcType (tyVarKind tv)
1295 ; return (setTyVarKind tv kind') }
1296
1297 zonkTcTypes :: [TcType] -> TcM [TcType]
1298 zonkTcTypes tys = mapM zonkTcType tys
1299
1300 {-
1301 ************************************************************************
1302 * *
1303 Zonking constraints
1304 * *
1305 ************************************************************************
1306 -}
1307
1308 zonkImplication :: Implication -> TcM Implication
1309 zonkImplication implic@(Implic { ic_skols = skols
1310 , ic_given = given
1311 , ic_wanted = wanted
1312 , ic_info = info })
1313 = do { skols' <- mapM zonkTcTyCoVarBndr skols -- Need to zonk their kinds!
1314 -- as Trac #7230 showed
1315 ; given' <- mapM zonkEvVar given
1316 ; info' <- zonkSkolemInfo info
1317 ; wanted' <- zonkWCRec wanted
1318 ; return (implic { ic_skols = skols'
1319 , ic_given = given'
1320 , ic_wanted = wanted'
1321 , ic_info = info' }) }
1322
1323 zonkEvVar :: EvVar -> TcM EvVar
1324 zonkEvVar var = do { ty' <- zonkTcType (varType var)
1325 ; return (setVarType var ty') }
1326
1327
1328 zonkWC :: WantedConstraints -> TcM WantedConstraints
1329 zonkWC wc = zonkWCRec wc
1330
1331 zonkWCRec :: WantedConstraints -> TcM WantedConstraints
1332 zonkWCRec (WC { wc_simple = simple, wc_impl = implic, wc_insol = insol })
1333 = do { simple' <- zonkSimples simple
1334 ; implic' <- mapBagM zonkImplication implic
1335 ; insol' <- zonkSimples insol
1336 ; return (WC { wc_simple = simple', wc_impl = implic', wc_insol = insol' }) }
1337
1338 zonkSimples :: Cts -> TcM Cts
1339 zonkSimples cts = do { cts' <- mapBagM zonkCt' cts
1340 ; traceTc "zonkSimples done:" (ppr cts')
1341 ; return cts' }
1342
1343 zonkCt' :: Ct -> TcM Ct
1344 zonkCt' ct = zonkCt ct
1345
1346 zonkCt :: Ct -> TcM Ct
1347 zonkCt ct@(CHoleCan { cc_ev = ev })
1348 = do { ev' <- zonkCtEvidence ev
1349 ; return $ ct { cc_ev = ev' } }
1350 zonkCt ct
1351 = do { fl' <- zonkCtEvidence (cc_ev ct)
1352 ; return (mkNonCanonical fl') }
1353
1354 zonkCtEvidence :: CtEvidence -> TcM CtEvidence
1355 zonkCtEvidence ctev@(CtGiven { ctev_pred = pred })
1356 = do { pred' <- zonkTcType pred
1357 ; return (ctev { ctev_pred = pred'}) }
1358 zonkCtEvidence ctev@(CtWanted { ctev_pred = pred, ctev_dest = dest })
1359 = do { pred' <- zonkTcType pred
1360 ; let dest' = case dest of
1361 EvVarDest ev -> EvVarDest $ setVarType ev pred'
1362 -- necessary in simplifyInfer
1363 HoleDest h -> HoleDest h
1364 ; return (ctev { ctev_pred = pred', ctev_dest = dest' }) }
1365 zonkCtEvidence ctev@(CtDerived { ctev_pred = pred })
1366 = do { pred' <- zonkTcType pred
1367 ; return (ctev { ctev_pred = pred' }) }
1368
1369 zonkSkolemInfo :: SkolemInfo -> TcM SkolemInfo
1370 zonkSkolemInfo (SigSkol cx ty) = do { ty' <- zonkTcType ty
1371 ; return (SigSkol cx ty') }
1372 zonkSkolemInfo (InferSkol ntys) = do { ntys' <- mapM do_one ntys
1373 ; return (InferSkol ntys') }
1374 where
1375 do_one (n, ty) = do { ty' <- zonkTcType ty; return (n, ty') }
1376 zonkSkolemInfo skol_info = return skol_info
1377
1378 {-
1379 %************************************************************************
1380 %* *
1381 \subsection{Zonking -- the main work-horses: zonkTcType, zonkTcTyVar}
1382 * *
1383 * For internal use only! *
1384 * *
1385 ************************************************************************
1386
1387 -}
1388
1389 -- zonkId is used *during* typechecking just to zonk the Id's type
1390 zonkId :: TcId -> TcM TcId
1391 zonkId id
1392 = do { ty' <- zonkTcType (idType id)
1393 ; return (Id.setIdType id ty') }
1394
1395 -- | A suitable TyCoMapper for zonking a type inside the knot, and
1396 -- before all metavars are filled in.
1397 zonkTcTypeMapper :: TyCoMapper () TcM
1398 zonkTcTypeMapper = TyCoMapper
1399 { tcm_smart = True
1400 , tcm_tyvar = const zonkTcTyVar
1401 , tcm_covar = const (\cv -> mkCoVarCo <$> zonkTyCoVarKind cv)
1402 , tcm_hole = hole
1403 , tcm_tybinder = \_env tv _vis -> ((), ) <$> zonkTcTyCoVarBndr tv }
1404 where
1405 hole :: () -> CoercionHole -> Role -> Type -> Type
1406 -> TcM Coercion
1407 hole _ h r t1 t2
1408 = do { contents <- unpackCoercionHole_maybe h
1409 ; case contents of
1410 Just co -> do { co <- zonkCo co
1411 ; checkCoercionHole co h r t1 t2 }
1412 Nothing -> do { t1 <- zonkTcType t1
1413 ; t2 <- zonkTcType t2
1414 ; return $ mkHoleCo h r t1 t2 } }
1415
1416
1417 -- For unbound, mutable tyvars, zonkType uses the function given to it
1418 -- For tyvars bound at a for-all, zonkType zonks them to an immutable
1419 -- type variable and zonks the kind too
1420 zonkTcType :: TcType -> TcM TcType
1421 zonkTcType = mapType zonkTcTypeMapper ()
1422
1423 -- | "Zonk" a coercion -- really, just zonk any types in the coercion
1424 zonkCo :: Coercion -> TcM Coercion
1425 zonkCo = mapCoercion zonkTcTypeMapper ()
1426
1427 zonkTcTyCoVarBndr :: TcTyCoVar -> TcM TcTyCoVar
1428 -- A tyvar binder is never a unification variable (MetaTv),
1429 -- rather it is always a skolems. BUT it may have a kind
1430 -- that has not yet been zonked, and may include kind
1431 -- unification variables.
1432 zonkTcTyCoVarBndr tyvar
1433 -- can't use isCoVar, because it looks at a TyCon. Argh.
1434 = ASSERT2( isImmutableTyVar tyvar || (not $ isTyVar tyvar), pprTyVar tyvar )
1435 updateTyVarKindM zonkTcType tyvar
1436
1437 zonkTcTyVarBinder :: TyVarBndr TcTyVar vis -> TcM (TyVarBndr TcTyVar vis)
1438 zonkTcTyVarBinder (TvBndr tv vis)
1439 = do { tv' <- zonkTcTyCoVarBndr tv
1440 ; return (TvBndr tv' vis) }
1441
1442 zonkTcTyVar :: TcTyVar -> TcM TcType
1443 -- Simply look through all Flexis
1444 zonkTcTyVar tv
1445 | isTcTyVar tv
1446 = case tcTyVarDetails tv of
1447 SkolemTv {} -> zonk_kind_and_return
1448 RuntimeUnk {} -> zonk_kind_and_return
1449 FlatSkol ty -> zonkTcType ty
1450 MetaTv { mtv_ref = ref }
1451 -> do { cts <- readMutVar ref
1452 ; case cts of
1453 Flexi -> zonk_kind_and_return
1454 Indirect ty -> zonkTcType ty }
1455
1456 | otherwise -- coercion variable
1457 = zonk_kind_and_return
1458 where
1459 zonk_kind_and_return = do { z_tv <- zonkTyCoVarKind tv
1460 ; return (mkTyVarTy z_tv) }
1461
1462 -- Variant that assumes that any result of zonking is still a TyVar.
1463 -- Should be used only on skolems and SigTvs
1464 zonkTcTyVarToTyVar :: TcTyVar -> TcM TcTyVar
1465 zonkTcTyVarToTyVar tv
1466 = do { ty <- zonkTcTyVar tv
1467 ; return (tcGetTyVar "zonkTcTyVarToVar" ty) }
1468
1469 {-
1470 %************************************************************************
1471 %* *
1472 Tidying
1473 * *
1474 ************************************************************************
1475 -}
1476
1477 zonkTidyTcType :: TidyEnv -> TcType -> TcM (TidyEnv, TcType)
1478 zonkTidyTcType env ty = do { ty' <- zonkTcType ty
1479 ; return (tidyOpenType env ty') }
1480
1481 -- | Make an 'ErrorThing' storing a type.
1482 mkTypeErrorThing :: TcType -> ErrorThing
1483 mkTypeErrorThing ty = ErrorThing ty (Just $ length $ snd $ repSplitAppTys ty)
1484 zonkTidyTcType
1485 -- NB: Use *rep*splitAppTys, else we get #11313
1486
1487 -- | Make an 'ErrorThing' storing a type, with some extra args known about
1488 mkTypeErrorThingArgs :: TcType -> Int -> ErrorThing
1489 mkTypeErrorThingArgs ty num_args
1490 = ErrorThing ty (Just $ (length $ snd $ repSplitAppTys ty) + num_args)
1491 zonkTidyTcType
1492
1493 zonkTidyOrigin :: TidyEnv -> CtOrigin -> TcM (TidyEnv, CtOrigin)
1494 zonkTidyOrigin env (GivenOrigin skol_info)
1495 = do { skol_info1 <- zonkSkolemInfo skol_info
1496 ; let skol_info2 = tidySkolemInfo env skol_info1
1497 ; return (env, GivenOrigin skol_info2) }
1498 zonkTidyOrigin env orig@(TypeEqOrigin { uo_actual = act
1499 , uo_expected = exp
1500 , uo_thing = m_thing })
1501 = do { (env1, act') <- zonkTidyTcType env act
1502 ; (env2, exp') <- zonkTidyTcType env1 exp
1503 ; (env3, m_thing') <- zonkTidyErrorThing env2 m_thing
1504 ; return ( env3, orig { uo_actual = act'
1505 , uo_expected = exp'
1506 , uo_thing = m_thing' }) }
1507 zonkTidyOrigin env (KindEqOrigin ty1 m_ty2 orig t_or_k)
1508 = do { (env1, ty1') <- zonkTidyTcType env ty1
1509 ; (env2, m_ty2') <- case m_ty2 of
1510 Just ty2 -> second Just <$> zonkTidyTcType env1 ty2
1511 Nothing -> return (env1, Nothing)
1512 ; (env3, orig') <- zonkTidyOrigin env2 orig
1513 ; return (env3, KindEqOrigin ty1' m_ty2' orig' t_or_k) }
1514 zonkTidyOrigin env (FunDepOrigin1 p1 l1 p2 l2)
1515 = do { (env1, p1') <- zonkTidyTcType env p1
1516 ; (env2, p2') <- zonkTidyTcType env1 p2
1517 ; return (env2, FunDepOrigin1 p1' l1 p2' l2) }
1518 zonkTidyOrigin env (FunDepOrigin2 p1 o1 p2 l2)
1519 = do { (env1, p1') <- zonkTidyTcType env p1
1520 ; (env2, p2') <- zonkTidyTcType env1 p2
1521 ; (env3, o1') <- zonkTidyOrigin env2 o1
1522 ; return (env3, FunDepOrigin2 p1' o1' p2' l2) }
1523 zonkTidyOrigin env orig = return (env, orig)
1524
1525 zonkTidyErrorThing :: TidyEnv -> Maybe ErrorThing
1526 -> TcM (TidyEnv, Maybe ErrorThing)
1527 zonkTidyErrorThing env (Just (ErrorThing thing n_args zonker))
1528 = do { (env', thing') <- zonker env thing
1529 ; return (env', Just $ ErrorThing thing' n_args zonker) }
1530 zonkTidyErrorThing env Nothing
1531 = return (env, Nothing)
1532
1533 ----------------
1534 tidyCt :: TidyEnv -> Ct -> Ct
1535 -- Used only in error reporting
1536 -- Also converts it to non-canonical
1537 tidyCt env ct
1538 = case ct of
1539 CHoleCan { cc_ev = ev }
1540 -> ct { cc_ev = tidy_ev env ev }
1541 _ -> mkNonCanonical (tidy_ev env (ctEvidence ct))
1542 where
1543 tidy_ev :: TidyEnv -> CtEvidence -> CtEvidence
1544 -- NB: we do not tidy the ctev_evar field because we don't
1545 -- show it in error messages
1546 tidy_ev env ctev@(CtGiven { ctev_pred = pred })
1547 = ctev { ctev_pred = tidyType env pred }
1548 tidy_ev env ctev@(CtWanted { ctev_pred = pred })
1549 = ctev { ctev_pred = tidyType env pred }
1550 tidy_ev env ctev@(CtDerived { ctev_pred = pred })
1551 = ctev { ctev_pred = tidyType env pred }
1552
1553 ----------------
1554 tidyEvVar :: TidyEnv -> EvVar -> EvVar
1555 tidyEvVar env var = setVarType var (tidyType env (varType var))
1556
1557 ----------------
1558 tidySkolemInfo :: TidyEnv -> SkolemInfo -> SkolemInfo
1559 tidySkolemInfo env (DerivSkol ty) = DerivSkol (tidyType env ty)
1560 tidySkolemInfo env (SigSkol cx ty) = SigSkol cx (tidyType env ty)
1561 tidySkolemInfo env (InferSkol ids) = InferSkol (mapSnd (tidyType env) ids)
1562 tidySkolemInfo env (UnifyForAllSkol ty) = UnifyForAllSkol (tidyType env ty)
1563 tidySkolemInfo _ info = info