2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1998
6 Type - public interface
10 -- re-exports from TypeRep
11 TyThing(..), Type, PredType(..), ThetaType,
15 Kind, SimpleKind, KindVar,
16 kindFunResult, splitKindFunTys, splitKindFunTysN,
18 liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
19 argTypeKindTyCon, ubxTupleKindTyCon,
21 liftedTypeKind, unliftedTypeKind, openTypeKind,
22 argTypeKind, ubxTupleKind,
24 tySuperKind, coSuperKind,
26 isLiftedTypeKind, isUnliftedTypeKind, isOpenTypeKind,
27 isUbxTupleKind, isArgTypeKind, isKind, isTySuperKind,
28 isCoSuperKind, isSuperKind, isCoercionKind, isEqPred,
29 mkArrowKind, mkArrowKinds,
31 isSubArgTypeKind, isSubOpenTypeKind, isSubKind, defaultKind, eqKind,
34 -- Re-exports from TyCon
37 mkTyVarTy, mkTyVarTys, getTyVar, getTyVar_maybe, isTyVarTy,
39 mkAppTy, mkAppTys, splitAppTy, splitAppTys,
40 splitAppTy_maybe, repSplitAppTy_maybe,
42 mkFunTy, mkFunTys, splitFunTy, splitFunTy_maybe,
43 splitFunTys, splitFunTysN,
44 funResultTy, funArgTy, zipFunTys, isFunTy,
46 mkTyConApp, mkTyConTy,
47 tyConAppTyCon, tyConAppArgs,
48 splitTyConApp_maybe, splitTyConApp,
49 splitNewTyConApp_maybe, splitNewTyConApp,
51 repType, repType', typePrimRep, coreView, tcView, kindView,
53 mkForAllTy, mkForAllTys, splitForAllTy_maybe, splitForAllTys,
54 applyTy, applyTys, isForAllTy, dropForAlls,
57 predTypeRep, mkPredTy, mkPredTys,
58 tyConOrigHead, pprSourceTyCon,
61 splitRecNewType_maybe, newTyConInstRhs,
64 isUnLiftedType, isUnboxedTupleType, isAlgType, isPrimitiveType,
65 isStrictType, isStrictPred,
68 tyVarsOfType, tyVarsOfTypes, tyVarsOfPred, tyVarsOfTheta,
69 typeKind, addFreeTyVars,
71 -- Tidying up for printing
73 tidyOpenType, tidyOpenTypes,
74 tidyTyVarBndr, tidyFreeTyVars,
75 tidyOpenTyVar, tidyOpenTyVars,
76 tidyTopType, tidyPred,
80 coreEqType, tcEqType, tcEqTypes, tcCmpType, tcCmpTypes,
81 tcEqPred, tcCmpPred, tcEqTypeX,
87 TvSubstEnv, emptyTvSubstEnv, -- Representation widely visible
88 TvSubst(..), emptyTvSubst, -- Representation visible to a few friends
89 mkTvSubst, mkOpenTvSubst, zipOpenTvSubst, zipTopTvSubst, mkTopTvSubst, notElemTvSubst,
90 getTvSubstEnv, setTvSubstEnv, getTvInScope, extendTvInScope,
91 extendTvSubst, extendTvSubstList, isInScope, composeTvSubst, zipTyEnv,
93 -- Performing substitution on types
94 substTy, substTys, substTyWith, substTheta,
95 substPred, substTyVar, substTyVars, substTyVarBndr, deShadowTy, lookupTyVar,
98 pprType, pprParendType, pprTypeApp, pprTyThingCategory, pprForAll,
99 pprPred, pprTheta, pprThetaArrow, pprClassPred, pprKind, pprParendKind
102 #include "HsVersions.h"
104 -- We import the representation and primitive functions from TypeRep.
105 -- Many things are reexported, but not the representation!
125 import Data.Maybe ( isJust )
129 %************************************************************************
133 %************************************************************************
135 In Core, we "look through" non-recursive newtypes and PredTypes.
138 {-# INLINE coreView #-}
139 coreView :: Type -> Maybe Type
140 -- Strips off the *top layer only* of a type to give
141 -- its underlying representation type.
142 -- Returns Nothing if there is nothing to look through.
144 -- In the case of newtypes, it returns
145 -- *either* a vanilla TyConApp (recursive newtype, or non-saturated)
146 -- *or* the newtype representation (otherwise), meaning the
147 -- type written in the RHS of the newtype decl,
148 -- which may itself be a newtype
150 -- Example: newtype R = MkR S
152 -- newtype T = MkT (T -> T)
153 -- expandNewTcApp on R gives Just S
155 -- on T gives Nothing (no expansion)
157 -- By being non-recursive and inlined, this case analysis gets efficiently
158 -- joined onto the case analysis that the caller is already doing
159 coreView (NoteTy _ ty) = Just ty
161 | isEqPred p = Nothing
162 | otherwise = Just (predTypeRep p)
163 coreView (TyConApp tc tys) | Just (tenv, rhs, tys') <- coreExpandTyCon_maybe tc tys
164 = Just (mkAppTys (substTy (mkTopTvSubst tenv) rhs) tys')
165 -- Its important to use mkAppTys, rather than (foldl AppTy),
166 -- because the function part might well return a
167 -- partially-applied type constructor; indeed, usually will!
168 coreView ty = Nothing
172 -----------------------------------------------
173 {-# INLINE tcView #-}
174 tcView :: Type -> Maybe Type
175 -- Same, but for the type checker, which just looks through synonyms
176 tcView (NoteTy _ ty) = Just ty
177 tcView (TyConApp tc tys) | Just (tenv, rhs, tys') <- tcExpandTyCon_maybe tc tys
178 = Just (mkAppTys (substTy (mkTopTvSubst tenv) rhs) tys')
181 -----------------------------------------------
182 {-# INLINE kindView #-}
183 kindView :: Kind -> Maybe Kind
184 -- C.f. coreView, tcView
185 -- For the moment, we don't even handle synonyms in kinds
186 kindView (NoteTy _ k) = Just k
187 kindView other = Nothing
191 %************************************************************************
193 \subsection{Constructor-specific functions}
195 %************************************************************************
198 ---------------------------------------------------------------------
202 mkTyVarTy :: TyVar -> Type
205 mkTyVarTys :: [TyVar] -> [Type]
206 mkTyVarTys = map mkTyVarTy -- a common use of mkTyVarTy
208 getTyVar :: String -> Type -> TyVar
209 getTyVar msg ty = case getTyVar_maybe ty of
211 Nothing -> panic ("getTyVar: " ++ msg)
213 isTyVarTy :: Type -> Bool
214 isTyVarTy ty = isJust (getTyVar_maybe ty)
216 getTyVar_maybe :: Type -> Maybe TyVar
217 getTyVar_maybe ty | Just ty' <- coreView ty = getTyVar_maybe ty'
218 getTyVar_maybe (TyVarTy tv) = Just tv
219 getTyVar_maybe other = Nothing
224 ---------------------------------------------------------------------
227 We need to be pretty careful with AppTy to make sure we obey the
228 invariant that a TyConApp is always visibly so. mkAppTy maintains the
232 mkAppTy orig_ty1 orig_ty2
235 mk_app (NoteTy _ ty1) = mk_app ty1
236 mk_app (TyConApp tc tys) = mkTyConApp tc (tys ++ [orig_ty2])
237 mk_app ty1 = AppTy orig_ty1 orig_ty2
238 -- Note that the TyConApp could be an
239 -- under-saturated type synonym. GHC allows that; e.g.
240 -- type Foo k = k a -> k a
242 -- foo :: Foo Id -> Foo Id
244 -- Here Id is partially applied in the type sig for Foo,
245 -- but once the type synonyms are expanded all is well
247 mkAppTys :: Type -> [Type] -> Type
248 mkAppTys orig_ty1 [] = orig_ty1
249 -- This check for an empty list of type arguments
250 -- avoids the needless loss of a type synonym constructor.
251 -- For example: mkAppTys Rational []
252 -- returns to (Ratio Integer), which has needlessly lost
253 -- the Rational part.
254 mkAppTys orig_ty1 orig_tys2
257 mk_app (NoteTy _ ty1) = mk_app ty1
258 mk_app (TyConApp tc tys) = mkTyConApp tc (tys ++ orig_tys2)
259 -- mkTyConApp: see notes with mkAppTy
260 mk_app ty1 = foldl AppTy orig_ty1 orig_tys2
263 splitAppTy_maybe :: Type -> Maybe (Type, Type)
264 splitAppTy_maybe ty | Just ty' <- coreView ty
265 = splitAppTy_maybe ty'
266 splitAppTy_maybe ty = repSplitAppTy_maybe ty
269 repSplitAppTy_maybe :: Type -> Maybe (Type,Type)
270 -- Does the AppTy split, but assumes that any view stuff is already done
271 repSplitAppTy_maybe (FunTy ty1 ty2) = Just (TyConApp funTyCon [ty1], ty2)
272 repSplitAppTy_maybe (AppTy ty1 ty2) = Just (ty1, ty2)
273 repSplitAppTy_maybe (TyConApp tc tys) = case snocView tys of
274 Just (tys', ty') -> Just (TyConApp tc tys', ty')
276 repSplitAppTy_maybe other = Nothing
278 splitAppTy :: Type -> (Type, Type)
279 splitAppTy ty = case splitAppTy_maybe ty of
281 Nothing -> panic "splitAppTy"
284 splitAppTys :: Type -> (Type, [Type])
285 splitAppTys ty = split ty ty []
287 split orig_ty ty args | Just ty' <- coreView ty = split orig_ty ty' args
288 split orig_ty (AppTy ty arg) args = split ty ty (arg:args)
289 split orig_ty (TyConApp tc tc_args) args = (TyConApp tc [], tc_args ++ args)
290 split orig_ty (FunTy ty1 ty2) args = ASSERT( null args )
291 (TyConApp funTyCon [], [ty1,ty2])
292 split orig_ty ty args = (orig_ty, args)
297 ---------------------------------------------------------------------
302 mkFunTy :: Type -> Type -> Type
303 mkFunTy (PredTy (EqPred ty1 ty2)) res = mkForAllTy (mkWildCoVar (PredTy (EqPred ty1 ty2))) res
304 mkFunTy arg res = FunTy arg res
306 mkFunTys :: [Type] -> Type -> Type
307 mkFunTys tys ty = foldr mkFunTy ty tys
309 isFunTy :: Type -> Bool
310 isFunTy ty = isJust (splitFunTy_maybe ty)
312 splitFunTy :: Type -> (Type, Type)
313 splitFunTy ty | Just ty' <- coreView ty = splitFunTy ty'
314 splitFunTy (FunTy arg res) = (arg, res)
315 splitFunTy other = pprPanic "splitFunTy" (ppr other)
317 splitFunTy_maybe :: Type -> Maybe (Type, Type)
318 splitFunTy_maybe ty | Just ty' <- coreView ty = splitFunTy_maybe ty'
319 splitFunTy_maybe (FunTy arg res) = Just (arg, res)
320 splitFunTy_maybe other = Nothing
322 splitFunTys :: Type -> ([Type], Type)
323 splitFunTys ty = split [] ty ty
325 split args orig_ty ty | Just ty' <- coreView ty = split args orig_ty ty'
326 split args orig_ty (FunTy arg res) = split (arg:args) res res
327 split args orig_ty ty = (reverse args, orig_ty)
329 splitFunTysN :: Int -> Type -> ([Type], Type)
330 -- Split off exactly n arg tys
331 splitFunTysN 0 ty = ([], ty)
332 splitFunTysN n ty = case splitFunTy ty of { (arg, res) ->
333 case splitFunTysN (n-1) res of { (args, res) ->
336 zipFunTys :: Outputable a => [a] -> Type -> ([(a,Type)], Type)
337 zipFunTys orig_xs orig_ty = split [] orig_xs orig_ty orig_ty
339 split acc [] nty ty = (reverse acc, nty)
341 | Just ty' <- coreView ty = split acc xs nty ty'
342 split acc (x:xs) nty (FunTy arg res) = split ((x,arg):acc) xs res res
343 split acc (x:xs) nty ty = pprPanic "zipFunTys" (ppr orig_xs <+> ppr orig_ty)
345 funResultTy :: Type -> Type
346 funResultTy ty | Just ty' <- coreView ty = funResultTy ty'
347 funResultTy (FunTy arg res) = res
348 funResultTy ty = pprPanic "funResultTy" (ppr ty)
350 funArgTy :: Type -> Type
351 funArgTy ty | Just ty' <- coreView ty = funArgTy ty'
352 funArgTy (FunTy arg res) = arg
353 funArgTy ty = pprPanic "funArgTy" (ppr ty)
357 ---------------------------------------------------------------------
360 @mkTyConApp@ is a key function, because it builds a TyConApp, FunTy or PredTy,
364 mkTyConApp :: TyCon -> [Type] -> Type
366 | isFunTyCon tycon, [ty1,ty2] <- tys
372 mkTyConTy :: TyCon -> Type
373 mkTyConTy tycon = mkTyConApp tycon []
375 -- splitTyConApp "looks through" synonyms, because they don't
376 -- mean a distinct type, but all other type-constructor applications
377 -- including functions are returned as Just ..
379 tyConAppTyCon :: Type -> TyCon
380 tyConAppTyCon ty = fst (splitTyConApp ty)
382 tyConAppArgs :: Type -> [Type]
383 tyConAppArgs ty = snd (splitTyConApp ty)
385 splitTyConApp :: Type -> (TyCon, [Type])
386 splitTyConApp ty = case splitTyConApp_maybe ty of
388 Nothing -> pprPanic "splitTyConApp" (ppr ty)
390 splitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
391 splitTyConApp_maybe ty | Just ty' <- coreView ty = splitTyConApp_maybe ty'
392 splitTyConApp_maybe (TyConApp tc tys) = Just (tc, tys)
393 splitTyConApp_maybe (FunTy arg res) = Just (funTyCon, [arg,res])
394 splitTyConApp_maybe other = Nothing
396 -- Sometimes we do NOT want to look throught a newtype. When case matching
397 -- on a newtype we want a convenient way to access the arguments of a newty
398 -- constructor so as to properly form a coercion.
399 splitNewTyConApp :: Type -> (TyCon, [Type])
400 splitNewTyConApp ty = case splitNewTyConApp_maybe ty of
402 Nothing -> pprPanic "splitNewTyConApp" (ppr ty)
403 splitNewTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
404 splitNewTyConApp_maybe ty | Just ty' <- tcView ty = splitNewTyConApp_maybe ty'
405 splitNewTyConApp_maybe (TyConApp tc tys) = Just (tc, tys)
406 splitNewTyConApp_maybe (FunTy arg res) = Just (funTyCon, [arg,res])
407 splitNewTyConApp_maybe other = Nothing
409 -- get instantiated newtype rhs, the arguments had better saturate
411 newTyConInstRhs :: TyCon -> [Type] -> Type
412 newTyConInstRhs tycon tys =
413 let (tvs, ty) = newTyConRhs tycon in substTyWith tvs tys ty
417 ---------------------------------------------------------------------
421 Notes on type synonyms
422 ~~~~~~~~~~~~~~~~~~~~~~
423 The various "split" functions (splitFunTy, splitRhoTy, splitForAllTy) try
424 to return type synonyms whereever possible. Thus
429 splitFunTys (a -> Foo a) = ([a], Foo a)
432 The reason is that we then get better (shorter) type signatures in
433 interfaces. Notably this plays a role in tcTySigs in TcBinds.lhs.
438 repType looks through
442 (d) usage annotations
443 (e) all newtypes, including recursive ones, but not newtype families
444 It's useful in the back end.
447 repType :: Type -> Type
448 -- Only applied to types of kind *; hence tycons are saturated
449 repType ty | Just ty' <- coreView ty = repType ty'
450 repType (ForAllTy _ ty) = repType ty
451 repType (TyConApp tc tys)
452 | isClosedNewTyCon tc = -- Recursive newtypes are opaque to coreView
453 -- but we must expand them here. Sure to
454 -- be saturated because repType is only applied
455 -- to types of kind *
456 ASSERT( {- isRecursiveTyCon tc && -} tys `lengthIs` tyConArity tc )
457 repType (new_type_rep tc tys)
460 -- repType' aims to be a more thorough version of repType
461 -- For now it simply looks through the TyConApp args too
462 repType' ty -- | pprTrace "repType'" (ppr ty $$ ppr (go1 ty)) False = undefined
466 go (TyConApp tc tys) = mkTyConApp tc (map repType' tys)
470 -- new_type_rep doesn't ask any questions:
471 -- it just expands newtype, whether recursive or not
472 new_type_rep new_tycon tys = ASSERT( tys `lengthIs` tyConArity new_tycon )
473 case newTyConRep new_tycon of
474 (tvs, rep_ty) -> substTyWith tvs tys rep_ty
476 -- ToDo: this could be moved to the code generator, using splitTyConApp instead
477 -- of inspecting the type directly.
478 typePrimRep :: Type -> PrimRep
479 typePrimRep ty = case repType ty of
480 TyConApp tc _ -> tyConPrimRep tc
482 AppTy _ _ -> PtrRep -- See note below
484 other -> pprPanic "typePrimRep" (ppr ty)
485 -- Types of the form 'f a' must be of kind *, not *#, so
486 -- we are guaranteed that they are represented by pointers.
487 -- The reason is that f must have kind *->*, not *->*#, because
488 -- (we claim) there is no way to constrain f's kind any other
494 ---------------------------------------------------------------------
499 mkForAllTy :: TyVar -> Type -> Type
501 = mkForAllTys [tyvar] ty
503 mkForAllTys :: [TyVar] -> Type -> Type
504 mkForAllTys tyvars ty = foldr ForAllTy ty tyvars
506 isForAllTy :: Type -> Bool
507 isForAllTy (NoteTy _ ty) = isForAllTy ty
508 isForAllTy (ForAllTy _ _) = True
509 isForAllTy other_ty = False
511 splitForAllTy_maybe :: Type -> Maybe (TyVar, Type)
512 splitForAllTy_maybe ty = splitFAT_m ty
514 splitFAT_m ty | Just ty' <- coreView ty = splitFAT_m ty'
515 splitFAT_m (ForAllTy tyvar ty) = Just(tyvar, ty)
516 splitFAT_m _ = Nothing
518 splitForAllTys :: Type -> ([TyVar], Type)
519 splitForAllTys ty = split ty ty []
521 split orig_ty ty tvs | Just ty' <- coreView ty = split orig_ty ty' tvs
522 split orig_ty (ForAllTy tv ty) tvs = split ty ty (tv:tvs)
523 split orig_ty t tvs = (reverse tvs, orig_ty)
525 dropForAlls :: Type -> Type
526 dropForAlls ty = snd (splitForAllTys ty)
529 -- (mkPiType now in CoreUtils)
533 Instantiate a for-all type with one or more type arguments.
534 Used when we have a polymorphic function applied to type args:
536 Then we use (applyTys type-of-f [t1,t2]) to compute the type of
540 applyTy :: Type -> Type -> Type
541 applyTy ty arg | Just ty' <- coreView ty = applyTy ty' arg
542 applyTy (ForAllTy tv ty) arg = substTyWith [tv] [arg] ty
543 applyTy other arg = panic "applyTy"
545 applyTys :: Type -> [Type] -> Type
546 -- This function is interesting because
547 -- a) the function may have more for-alls than there are args
548 -- b) less obviously, it may have fewer for-alls
549 -- For case (b) think of
550 -- applyTys (forall a.a) [forall b.b, Int]
551 -- This really can happen, via dressing up polymorphic types with newtype
552 -- clothing. Here's an example:
553 -- newtype R = R (forall a. a->a)
554 -- foo = case undefined :: R of
557 applyTys orig_fun_ty [] = orig_fun_ty
558 applyTys orig_fun_ty arg_tys
559 | n_tvs == n_args -- The vastly common case
560 = substTyWith tvs arg_tys rho_ty
561 | n_tvs > n_args -- Too many for-alls
562 = substTyWith (take n_args tvs) arg_tys
563 (mkForAllTys (drop n_args tvs) rho_ty)
564 | otherwise -- Too many type args
565 = ASSERT2( n_tvs > 0, ppr orig_fun_ty ) -- Zero case gives infnite loop!
566 applyTys (substTyWith tvs (take n_tvs arg_tys) rho_ty)
569 (tvs, rho_ty) = splitForAllTys orig_fun_ty
571 n_args = length arg_tys
575 %************************************************************************
577 \subsection{Source types}
579 %************************************************************************
581 A "source type" is a type that is a separate type as far as the type checker is
582 concerned, but which has low-level representation as far as the back end is concerned.
584 Source types are always lifted.
586 The key function is predTypeRep which gives the representation of a source type:
589 mkPredTy :: PredType -> Type
590 mkPredTy pred = PredTy pred
592 mkPredTys :: ThetaType -> [Type]
593 mkPredTys preds = map PredTy preds
595 predTypeRep :: PredType -> Type
596 -- Convert a PredType to its "representation type";
597 -- the post-type-checking type used by all the Core passes of GHC.
598 -- Unwraps only the outermost level; for example, the result might
599 -- be a newtype application
600 predTypeRep (IParam _ ty) = ty
601 predTypeRep (ClassP clas tys) = mkTyConApp (classTyCon clas) tys
602 -- Result might be a newtype application, but the consumer will
603 -- look through that too if necessary
604 predTypeRep (EqPred ty1 ty2) = pprPanic "predTypeRep" (ppr (EqPred ty1 ty2))
606 -- The original head is the tycon and its variables for a vanilla tycon and it
607 -- is the family tycon and its type indexes for a family instance.
608 tyConOrigHead :: TyCon -> (TyCon, [Type])
609 tyConOrigHead tycon = case tyConFamInst_maybe tycon of
610 Nothing -> (tycon, mkTyVarTys (tyConTyVars tycon))
611 Just famInst -> famInst
613 -- Pretty prints a tycon, using the family instance in case of a
614 -- representation tycon. For example
615 -- e.g. data T [a] = ...
616 -- In that case we want to print `T [a]', where T is the family TyCon
618 | Just (repTyCon, tys) <- tyConFamInst_maybe tycon
619 = ppr $ repTyCon `TyConApp` tys -- can't be FunTyCon
625 %************************************************************************
629 %************************************************************************
632 splitRecNewType_maybe :: Type -> Maybe Type
633 -- Sometimes we want to look through a recursive newtype, and that's what happens here
634 -- It only strips *one layer* off, so the caller will usually call itself recursively
635 -- Only applied to types of kind *, hence the newtype is always saturated
636 splitRecNewType_maybe ty | Just ty' <- coreView ty = splitRecNewType_maybe ty'
637 splitRecNewType_maybe (TyConApp tc tys)
638 | isClosedNewTyCon tc
639 = ASSERT( tys `lengthIs` tyConArity tc ) -- splitRecNewType_maybe only be applied
640 -- to *types* (of kind *)
641 ASSERT( isRecursiveTyCon tc ) -- Guaranteed by coreView
642 case newTyConRhs tc of
643 (tvs, rep_ty) -> ASSERT( length tvs == length tys )
644 Just (substTyWith tvs tys rep_ty)
646 splitRecNewType_maybe other = Nothing
653 %************************************************************************
655 \subsection{Kinds and free variables}
657 %************************************************************************
659 ---------------------------------------------------------------------
660 Finding the kind of a type
661 ~~~~~~~~~~~~~~~~~~~~~~~~~~
663 typeKind :: Type -> Kind
664 typeKind (TyConApp tycon tys) = ASSERT( not (isCoercionTyCon tycon) )
665 -- We should be looking for the coercion kind,
667 foldr (\_ k -> kindFunResult k) (tyConKind tycon) tys
668 typeKind (NoteTy _ ty) = typeKind ty
669 typeKind (PredTy pred) = predKind pred
670 typeKind (AppTy fun arg) = kindFunResult (typeKind fun)
671 typeKind (ForAllTy tv ty) = typeKind ty
672 typeKind (TyVarTy tyvar) = tyVarKind tyvar
673 typeKind (FunTy arg res)
674 -- Hack alert. The kind of (Int -> Int#) is liftedTypeKind (*),
675 -- not unliftedTypKind (#)
676 -- The only things that can be after a function arrow are
677 -- (a) types (of kind openTypeKind or its sub-kinds)
678 -- (b) kinds (of super-kind TY) (e.g. * -> (* -> *))
679 | isTySuperKind k = k
680 | otherwise = ASSERT( isSubOpenTypeKind k) liftedTypeKind
684 predKind :: PredType -> Kind
685 predKind (EqPred {}) = coSuperKind -- A coercion kind!
686 predKind (ClassP {}) = liftedTypeKind -- Class and implicitPredicates are
687 predKind (IParam {}) = liftedTypeKind -- always represented by lifted types
691 ---------------------------------------------------------------------
692 Free variables of a type
693 ~~~~~~~~~~~~~~~~~~~~~~~~
695 tyVarsOfType :: Type -> TyVarSet
696 -- NB: for type synonyms tyVarsOfType does *not* expand the synonym
697 tyVarsOfType (TyVarTy tv) = unitVarSet tv
698 tyVarsOfType (TyConApp tycon tys) = tyVarsOfTypes tys
699 tyVarsOfType (NoteTy (FTVNote tvs) ty2) = tvs
700 tyVarsOfType (PredTy sty) = tyVarsOfPred sty
701 tyVarsOfType (FunTy arg res) = tyVarsOfType arg `unionVarSet` tyVarsOfType res
702 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionVarSet` tyVarsOfType arg
703 tyVarsOfType (ForAllTy tyvar ty) = delVarSet (tyVarsOfType ty) tyvar
705 tyVarsOfTypes :: [Type] -> TyVarSet
706 tyVarsOfTypes tys = foldr (unionVarSet.tyVarsOfType) emptyVarSet tys
708 tyVarsOfPred :: PredType -> TyVarSet
709 tyVarsOfPred (IParam _ ty) = tyVarsOfType ty
710 tyVarsOfPred (ClassP _ tys) = tyVarsOfTypes tys
711 tyVarsOfPred (EqPred ty1 ty2) = tyVarsOfType ty1 `unionVarSet` tyVarsOfType ty2
713 tyVarsOfTheta :: ThetaType -> TyVarSet
714 tyVarsOfTheta = foldr (unionVarSet . tyVarsOfPred) emptyVarSet
716 -- Add a Note with the free tyvars to the top of the type
717 addFreeTyVars :: Type -> Type
718 addFreeTyVars ty@(NoteTy (FTVNote _) _) = ty
719 addFreeTyVars ty = NoteTy (FTVNote (tyVarsOfType ty)) ty
723 %************************************************************************
725 \subsection{TidyType}
727 %************************************************************************
729 tidyTy tidies up a type for printing in an error message, or in
732 It doesn't change the uniques at all, just the print names.
735 tidyTyVarBndr :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
736 tidyTyVarBndr env@(tidy_env, subst) tyvar
737 = case tidyOccName tidy_env (getOccName name) of
738 (tidy', occ') -> ((tidy', subst'), tyvar'')
740 subst' = extendVarEnv subst tyvar tyvar''
741 tyvar' = setTyVarName tyvar name'
742 name' = tidyNameOcc name occ'
743 -- Don't forget to tidy the kind for coercions!
744 tyvar'' | isCoVar tyvar = setTyVarKind tyvar' kind'
746 kind' = tidyType env (tyVarKind tyvar)
748 name = tyVarName tyvar
750 tidyFreeTyVars :: TidyEnv -> TyVarSet -> TidyEnv
751 -- Add the free tyvars to the env in tidy form,
752 -- so that we can tidy the type they are free in
753 tidyFreeTyVars env tyvars = fst (tidyOpenTyVars env (varSetElems tyvars))
755 tidyOpenTyVars :: TidyEnv -> [TyVar] -> (TidyEnv, [TyVar])
756 tidyOpenTyVars env tyvars = mapAccumL tidyOpenTyVar env tyvars
758 tidyOpenTyVar :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
759 -- Treat a new tyvar as a binder, and give it a fresh tidy name
760 tidyOpenTyVar env@(tidy_env, subst) tyvar
761 = case lookupVarEnv subst tyvar of
762 Just tyvar' -> (env, tyvar') -- Already substituted
763 Nothing -> tidyTyVarBndr env tyvar -- Treat it as a binder
765 tidyType :: TidyEnv -> Type -> Type
766 tidyType env@(tidy_env, subst) ty
769 go (TyVarTy tv) = case lookupVarEnv subst tv of
770 Nothing -> TyVarTy tv
771 Just tv' -> TyVarTy tv'
772 go (TyConApp tycon tys) = let args = map go tys
773 in args `seqList` TyConApp tycon args
774 go (NoteTy note ty) = (NoteTy $! (go_note note)) $! (go ty)
775 go (PredTy sty) = PredTy (tidyPred env sty)
776 go (AppTy fun arg) = (AppTy $! (go fun)) $! (go arg)
777 go (FunTy fun arg) = (FunTy $! (go fun)) $! (go arg)
778 go (ForAllTy tv ty) = ForAllTy tvp $! (tidyType envp ty)
780 (envp, tvp) = tidyTyVarBndr env tv
782 go_note note@(FTVNote ftvs) = note -- No need to tidy the free tyvars
784 tidyTypes env tys = map (tidyType env) tys
786 tidyPred :: TidyEnv -> PredType -> PredType
787 tidyPred env (IParam n ty) = IParam n (tidyType env ty)
788 tidyPred env (ClassP clas tys) = ClassP clas (tidyTypes env tys)
789 tidyPred env (EqPred ty1 ty2) = EqPred (tidyType env ty1) (tidyType env ty2)
793 @tidyOpenType@ grabs the free type variables, tidies them
794 and then uses @tidyType@ to work over the type itself
797 tidyOpenType :: TidyEnv -> Type -> (TidyEnv, Type)
799 = (env', tidyType env' ty)
801 env' = tidyFreeTyVars env (tyVarsOfType ty)
803 tidyOpenTypes :: TidyEnv -> [Type] -> (TidyEnv, [Type])
804 tidyOpenTypes env tys = mapAccumL tidyOpenType env tys
806 tidyTopType :: Type -> Type
807 tidyTopType ty = tidyType emptyTidyEnv ty
812 tidyKind :: TidyEnv -> Kind -> (TidyEnv, Kind)
813 tidyKind env k = tidyOpenType env k
818 %************************************************************************
820 \subsection{Liftedness}
822 %************************************************************************
825 isUnLiftedType :: Type -> Bool
826 -- isUnLiftedType returns True for forall'd unlifted types:
827 -- x :: forall a. Int#
828 -- I found bindings like these were getting floated to the top level.
829 -- They are pretty bogus types, mind you. It would be better never to
832 isUnLiftedType ty | Just ty' <- coreView ty = isUnLiftedType ty'
833 isUnLiftedType (ForAllTy tv ty) = isUnLiftedType ty
834 isUnLiftedType (TyConApp tc _) = isUnLiftedTyCon tc
835 isUnLiftedType other = False
837 isUnboxedTupleType :: Type -> Bool
838 isUnboxedTupleType ty = case splitTyConApp_maybe ty of
839 Just (tc, ty_args) -> isUnboxedTupleTyCon tc
842 -- Should only be applied to *types*; hence the assert
843 isAlgType :: Type -> Bool
844 isAlgType ty = case splitTyConApp_maybe ty of
845 Just (tc, ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
850 @isStrictType@ computes whether an argument (or let RHS) should
851 be computed strictly or lazily, based only on its type.
852 Works just like isUnLiftedType, except that it has a special case
853 for dictionaries. Since it takes account of ClassP, you might think
854 this function should be in TcType, but isStrictType is used by DataCon,
855 which is below TcType in the hierarchy, so it's convenient to put it here.
858 isStrictType (PredTy pred) = isStrictPred pred
859 isStrictType ty | Just ty' <- coreView ty = isStrictType ty'
860 isStrictType (ForAllTy tv ty) = isStrictType ty
861 isStrictType (TyConApp tc _) = isUnLiftedTyCon tc
862 isStrictType other = False
864 isStrictPred (ClassP clas _) = opt_DictsStrict && not (isNewTyCon (classTyCon clas))
865 isStrictPred other = False
866 -- We may be strict in dictionary types, but only if it
867 -- has more than one component.
868 -- [Being strict in a single-component dictionary risks
869 -- poking the dictionary component, which is wrong.]
873 isPrimitiveType :: Type -> Bool
874 -- Returns types that are opaque to Haskell.
875 -- Most of these are unlifted, but now that we interact with .NET, we
876 -- may have primtive (foreign-imported) types that are lifted
877 isPrimitiveType ty = case splitTyConApp_maybe ty of
878 Just (tc, ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
884 %************************************************************************
886 \subsection{Sequencing on types
888 %************************************************************************
891 seqType :: Type -> ()
892 seqType (TyVarTy tv) = tv `seq` ()
893 seqType (AppTy t1 t2) = seqType t1 `seq` seqType t2
894 seqType (FunTy t1 t2) = seqType t1 `seq` seqType t2
895 seqType (NoteTy note t2) = seqNote note `seq` seqType t2
896 seqType (PredTy p) = seqPred p
897 seqType (TyConApp tc tys) = tc `seq` seqTypes tys
898 seqType (ForAllTy tv ty) = tv `seq` seqType ty
900 seqTypes :: [Type] -> ()
902 seqTypes (ty:tys) = seqType ty `seq` seqTypes tys
904 seqNote :: TyNote -> ()
905 seqNote (FTVNote set) = sizeUniqSet set `seq` ()
907 seqPred :: PredType -> ()
908 seqPred (ClassP c tys) = c `seq` seqTypes tys
909 seqPred (IParam n ty) = n `seq` seqType ty
910 seqPred (EqPred ty1 ty2) = seqType ty1 `seq` seqType ty2
914 %************************************************************************
916 Equality for Core types
917 (We don't use instances so that we know where it happens)
919 %************************************************************************
921 Note that eqType works right even for partial applications of newtypes.
922 See Note [Newtype eta] in TyCon.lhs
925 coreEqType :: Type -> Type -> Bool
929 rn_env = mkRnEnv2 (mkInScopeSet (tyVarsOfType t1 `unionVarSet` tyVarsOfType t2))
931 eq env (TyVarTy tv1) (TyVarTy tv2) = rnOccL env tv1 == rnOccR env tv2
932 eq env (ForAllTy tv1 t1) (ForAllTy tv2 t2) = eq (rnBndr2 env tv1 tv2) t1 t2
933 eq env (AppTy s1 t1) (AppTy s2 t2) = eq env s1 s2 && eq env t1 t2
934 eq env (FunTy s1 t1) (FunTy s2 t2) = eq env s1 s2 && eq env t1 t2
935 eq env (TyConApp tc1 tys1) (TyConApp tc2 tys2)
936 | tc1 == tc2, all2 (eq env) tys1 tys2 = True
937 -- The lengths should be equal because
938 -- the two types have the same kind
939 -- NB: if the type constructors differ that does not
940 -- necessarily mean that the types aren't equal
941 -- (synonyms, newtypes)
942 -- Even if the type constructors are the same, but the arguments
943 -- differ, the two types could be the same (e.g. if the arg is just
944 -- ignored in the RHS). In both these cases we fall through to an
945 -- attempt to expand one side or the other.
947 -- Now deal with newtypes, synonyms, pred-tys
948 eq env t1 t2 | Just t1' <- coreView t1 = eq env t1' t2
949 | Just t2' <- coreView t2 = eq env t1 t2'
951 -- Fall through case; not equal!
956 %************************************************************************
958 Comparision for source types
959 (We don't use instances so that we know where it happens)
961 %************************************************************************
965 do *not* look through newtypes, PredTypes
968 tcEqType :: Type -> Type -> Bool
969 tcEqType t1 t2 = isEqual $ cmpType t1 t2
971 tcEqTypes :: [Type] -> [Type] -> Bool
972 tcEqTypes tys1 tys2 = isEqual $ cmpTypes tys1 tys2
974 tcCmpType :: Type -> Type -> Ordering
975 tcCmpType t1 t2 = cmpType t1 t2
977 tcCmpTypes :: [Type] -> [Type] -> Ordering
978 tcCmpTypes tys1 tys2 = cmpTypes tys1 tys2
980 tcEqPred :: PredType -> PredType -> Bool
981 tcEqPred p1 p2 = isEqual $ cmpPred p1 p2
983 tcCmpPred :: PredType -> PredType -> Ordering
984 tcCmpPred p1 p2 = cmpPred p1 p2
986 tcEqTypeX :: RnEnv2 -> Type -> Type -> Bool
987 tcEqTypeX env t1 t2 = isEqual $ cmpTypeX env t1 t2
990 Now here comes the real worker
993 cmpType :: Type -> Type -> Ordering
994 cmpType t1 t2 = cmpTypeX rn_env t1 t2
996 rn_env = mkRnEnv2 (mkInScopeSet (tyVarsOfType t1 `unionVarSet` tyVarsOfType t2))
998 cmpTypes :: [Type] -> [Type] -> Ordering
999 cmpTypes ts1 ts2 = cmpTypesX rn_env ts1 ts2
1001 rn_env = mkRnEnv2 (mkInScopeSet (tyVarsOfTypes ts1 `unionVarSet` tyVarsOfTypes ts2))
1003 cmpPred :: PredType -> PredType -> Ordering
1004 cmpPred p1 p2 = cmpPredX rn_env p1 p2
1006 rn_env = mkRnEnv2 (mkInScopeSet (tyVarsOfPred p1 `unionVarSet` tyVarsOfPred p2))
1008 cmpTypeX :: RnEnv2 -> Type -> Type -> Ordering -- Main workhorse
1009 cmpTypeX env t1 t2 | Just t1' <- tcView t1 = cmpTypeX env t1' t2
1010 | Just t2' <- tcView t2 = cmpTypeX env t1 t2'
1012 cmpTypeX env (TyVarTy tv1) (TyVarTy tv2) = rnOccL env tv1 `compare` rnOccR env tv2
1013 cmpTypeX env (ForAllTy tv1 t1) (ForAllTy tv2 t2) = cmpTypeX (rnBndr2 env tv1 tv2) t1 t2
1014 cmpTypeX env (AppTy s1 t1) (AppTy s2 t2) = cmpTypeX env s1 s2 `thenCmp` cmpTypeX env t1 t2
1015 cmpTypeX env (FunTy s1 t1) (FunTy s2 t2) = cmpTypeX env s1 s2 `thenCmp` cmpTypeX env t1 t2
1016 cmpTypeX env (PredTy p1) (PredTy p2) = cmpPredX env p1 p2
1017 cmpTypeX env (TyConApp tc1 tys1) (TyConApp tc2 tys2) = (tc1 `compare` tc2) `thenCmp` cmpTypesX env tys1 tys2
1018 cmpTypeX env t1 (NoteTy _ t2) = cmpTypeX env t1 t2
1020 -- Deal with the rest: TyVarTy < AppTy < FunTy < TyConApp < ForAllTy < PredTy
1021 cmpTypeX env (AppTy _ _) (TyVarTy _) = GT
1023 cmpTypeX env (FunTy _ _) (TyVarTy _) = GT
1024 cmpTypeX env (FunTy _ _) (AppTy _ _) = GT
1026 cmpTypeX env (TyConApp _ _) (TyVarTy _) = GT
1027 cmpTypeX env (TyConApp _ _) (AppTy _ _) = GT
1028 cmpTypeX env (TyConApp _ _) (FunTy _ _) = GT
1030 cmpTypeX env (ForAllTy _ _) (TyVarTy _) = GT
1031 cmpTypeX env (ForAllTy _ _) (AppTy _ _) = GT
1032 cmpTypeX env (ForAllTy _ _) (FunTy _ _) = GT
1033 cmpTypeX env (ForAllTy _ _) (TyConApp _ _) = GT
1035 cmpTypeX env (PredTy _) t2 = GT
1037 cmpTypeX env _ _ = LT
1040 cmpTypesX :: RnEnv2 -> [Type] -> [Type] -> Ordering
1041 cmpTypesX env [] [] = EQ
1042 cmpTypesX env (t1:tys1) (t2:tys2) = cmpTypeX env t1 t2 `thenCmp` cmpTypesX env tys1 tys2
1043 cmpTypesX env [] tys = LT
1044 cmpTypesX env ty [] = GT
1047 cmpPredX :: RnEnv2 -> PredType -> PredType -> Ordering
1048 cmpPredX env (IParam n1 ty1) (IParam n2 ty2) = (n1 `compare` n2) `thenCmp` cmpTypeX env ty1 ty2
1049 -- Compare names only for implicit parameters
1050 -- This comparison is used exclusively (I believe)
1051 -- for the Avails finite map built in TcSimplify
1052 -- If the types differ we keep them distinct so that we see
1053 -- a distinct pair to run improvement on
1054 cmpPredX env (ClassP c1 tys1) (ClassP c2 tys2) = (c1 `compare` c2) `thenCmp` (cmpTypesX env tys1 tys2)
1055 cmpPredX env (EqPred ty1 ty2) (EqPred ty1' ty2') = (cmpTypeX env ty1 ty1') `thenCmp` (cmpTypeX env ty2 ty2')
1057 -- Constructor order: IParam < ClassP < EqPred
1058 cmpPredX env (IParam {}) _ = LT
1059 cmpPredX env (ClassP {}) (IParam {}) = GT
1060 cmpPredX env (ClassP {}) (EqPred {}) = LT
1061 cmpPredX env (EqPred {}) _ = GT
1064 PredTypes are used as a FM key in TcSimplify,
1065 so we take the easy path and make them an instance of Ord
1068 instance Eq PredType where { (==) = tcEqPred }
1069 instance Ord PredType where { compare = tcCmpPred }
1073 %************************************************************************
1077 %************************************************************************
1081 = TvSubst InScopeSet -- The in-scope type variables
1082 TvSubstEnv -- The substitution itself
1083 -- See Note [Apply Once]
1084 -- and Note [Extending the TvSubstEnv]
1086 {- ----------------------------------------------------------
1090 We use TvSubsts to instantiate things, and we might instantiate
1094 So the substition might go [a->b, b->a]. A similar situation arises in Core
1095 when we find a beta redex like
1096 (/\ a /\ b -> e) b a
1097 Then we also end up with a substition that permutes type variables. Other
1098 variations happen to; for example [a -> (a, b)].
1100 ***************************************************
1101 *** So a TvSubst must be applied precisely once ***
1102 ***************************************************
1104 A TvSubst is not idempotent, but, unlike the non-idempotent substitution
1105 we use during unifications, it must not be repeatedly applied.
1107 Note [Extending the TvSubst]
1108 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1109 The following invariant should hold of a TvSubst
1111 The in-scope set is needed *only* to
1112 guide the generation of fresh uniques
1114 In particular, the *kind* of the type variables in
1115 the in-scope set is not relevant
1117 This invariant allows a short-cut when the TvSubstEnv is empty:
1118 if the TvSubstEnv is empty --- i.e. (isEmptyTvSubt subst) holds ---
1119 then (substTy subst ty) does nothing.
1121 For example, consider:
1122 (/\a. /\b:(a~Int). ...b..) Int
1123 We substitute Int for 'a'. The Unique of 'b' does not change, but
1124 nevertheless we add 'b' to the TvSubstEnv, because b's type does change
1126 This invariant has several crucial consequences:
1128 * In substTyVarBndr, we need extend the TvSubstEnv
1129 - if the unique has changed
1130 - or if the kind has changed
1132 * In substTyVar, we do not need to consult the in-scope set;
1133 the TvSubstEnv is enough
1135 * In substTy, substTheta, we can short-circuit when the TvSubstEnv is empty
1138 -------------------------------------------------------------- -}
1141 type TvSubstEnv = TyVarEnv Type
1142 -- A TvSubstEnv is used both inside a TvSubst (with the apply-once
1143 -- invariant discussed in Note [Apply Once]), and also independently
1144 -- in the middle of matching, and unification (see Types.Unify)
1145 -- So you have to look at the context to know if it's idempotent or
1146 -- apply-once or whatever
1147 emptyTvSubstEnv :: TvSubstEnv
1148 emptyTvSubstEnv = emptyVarEnv
1150 composeTvSubst :: InScopeSet -> TvSubstEnv -> TvSubstEnv -> TvSubstEnv
1151 -- (compose env1 env2)(x) is env1(env2(x)); i.e. apply env2 then env1
1152 -- It assumes that both are idempotent
1153 -- Typically, env1 is the refinement to a base substitution env2
1154 composeTvSubst in_scope env1 env2
1155 = env1 `plusVarEnv` mapVarEnv (substTy subst1) env2
1156 -- First apply env1 to the range of env2
1157 -- Then combine the two, making sure that env1 loses if
1158 -- both bind the same variable; that's why env1 is the
1159 -- *left* argument to plusVarEnv, because the right arg wins
1161 subst1 = TvSubst in_scope env1
1163 emptyTvSubst = TvSubst emptyInScopeSet emptyVarEnv
1165 isEmptyTvSubst :: TvSubst -> Bool
1166 -- See Note [Extending the TvSubstEnv]
1167 isEmptyTvSubst (TvSubst _ env) = isEmptyVarEnv env
1169 mkTvSubst :: InScopeSet -> TvSubstEnv -> TvSubst
1172 getTvSubstEnv :: TvSubst -> TvSubstEnv
1173 getTvSubstEnv (TvSubst _ env) = env
1175 getTvInScope :: TvSubst -> InScopeSet
1176 getTvInScope (TvSubst in_scope _) = in_scope
1178 isInScope :: Var -> TvSubst -> Bool
1179 isInScope v (TvSubst in_scope _) = v `elemInScopeSet` in_scope
1181 notElemTvSubst :: TyVar -> TvSubst -> Bool
1182 notElemTvSubst tv (TvSubst _ env) = not (tv `elemVarEnv` env)
1184 setTvSubstEnv :: TvSubst -> TvSubstEnv -> TvSubst
1185 setTvSubstEnv (TvSubst in_scope _) env = TvSubst in_scope env
1187 extendTvInScope :: TvSubst -> [Var] -> TvSubst
1188 extendTvInScope (TvSubst in_scope env) vars = TvSubst (extendInScopeSetList in_scope vars) env
1190 extendTvSubst :: TvSubst -> TyVar -> Type -> TvSubst
1191 extendTvSubst (TvSubst in_scope env) tv ty = TvSubst in_scope (extendVarEnv env tv ty)
1193 extendTvSubstList :: TvSubst -> [TyVar] -> [Type] -> TvSubst
1194 extendTvSubstList (TvSubst in_scope env) tvs tys
1195 = TvSubst in_scope (extendVarEnvList env (tvs `zip` tys))
1197 -- mkOpenTvSubst and zipOpenTvSubst generate the in-scope set from
1198 -- the types given; but it's just a thunk so with a bit of luck
1199 -- it'll never be evaluated
1201 mkOpenTvSubst :: TvSubstEnv -> TvSubst
1202 mkOpenTvSubst env = TvSubst (mkInScopeSet (tyVarsOfTypes (varEnvElts env))) env
1204 zipOpenTvSubst :: [TyVar] -> [Type] -> TvSubst
1205 zipOpenTvSubst tyvars tys
1207 | length tyvars /= length tys
1208 = pprTrace "zipOpenTvSubst" (ppr tyvars $$ ppr tys) emptyTvSubst
1211 = TvSubst (mkInScopeSet (tyVarsOfTypes tys)) (zipTyEnv tyvars tys)
1213 -- mkTopTvSubst is called when doing top-level substitutions.
1214 -- Here we expect that the free vars of the range of the
1215 -- substitution will be empty.
1216 mkTopTvSubst :: [(TyVar, Type)] -> TvSubst
1217 mkTopTvSubst prs = TvSubst emptyInScopeSet (mkVarEnv prs)
1219 zipTopTvSubst :: [TyVar] -> [Type] -> TvSubst
1220 zipTopTvSubst tyvars tys
1222 | length tyvars /= length tys
1223 = pprTrace "zipOpenTvSubst" (ppr tyvars $$ ppr tys) emptyTvSubst
1226 = TvSubst emptyInScopeSet (zipTyEnv tyvars tys)
1228 zipTyEnv :: [TyVar] -> [Type] -> TvSubstEnv
1231 | length tyvars /= length tys
1232 = pprTrace "mkTopTvSubst" (ppr tyvars $$ ppr tys) emptyVarEnv
1235 = zip_ty_env tyvars tys emptyVarEnv
1237 -- Later substitutions in the list over-ride earlier ones,
1238 -- but there should be no loops
1239 zip_ty_env [] [] env = env
1240 zip_ty_env (tv:tvs) (ty:tys) env = zip_ty_env tvs tys (extendVarEnv env tv ty)
1241 -- There used to be a special case for when
1243 -- (a not-uncommon case) in which case the substitution was dropped.
1244 -- But the type-tidier changes the print-name of a type variable without
1245 -- changing the unique, and that led to a bug. Why? Pre-tidying, we had
1246 -- a type {Foo t}, where Foo is a one-method class. So Foo is really a newtype.
1247 -- And it happened that t was the type variable of the class. Post-tiding,
1248 -- it got turned into {Foo t2}. The ext-core printer expanded this using
1249 -- sourceTypeRep, but that said "Oh, t == t2" because they have the same unique,
1250 -- and so generated a rep type mentioning t not t2.
1252 -- Simplest fix is to nuke the "optimisation"
1253 zip_ty_env tvs tys env = pprTrace "Var/Type length mismatch: " (ppr tvs $$ ppr tys) env
1254 -- zip_ty_env _ _ env = env
1256 instance Outputable TvSubst where
1257 ppr (TvSubst ins env)
1258 = brackets $ sep[ ptext SLIT("TvSubst"),
1259 nest 2 (ptext SLIT("In scope:") <+> ppr ins),
1260 nest 2 (ptext SLIT("Env:") <+> ppr env) ]
1263 %************************************************************************
1265 Performing type substitutions
1267 %************************************************************************
1270 substTyWith :: [TyVar] -> [Type] -> Type -> Type
1271 substTyWith tvs tys = ASSERT( length tvs == length tys )
1272 substTy (zipOpenTvSubst tvs tys)
1274 substTy :: TvSubst -> Type -> Type
1275 substTy subst ty | isEmptyTvSubst subst = ty
1276 | otherwise = subst_ty subst ty
1278 substTys :: TvSubst -> [Type] -> [Type]
1279 substTys subst tys | isEmptyTvSubst subst = tys
1280 | otherwise = map (subst_ty subst) tys
1282 substTheta :: TvSubst -> ThetaType -> ThetaType
1283 substTheta subst theta
1284 | isEmptyTvSubst subst = theta
1285 | otherwise = map (substPred subst) theta
1287 substPred :: TvSubst -> PredType -> PredType
1288 substPred subst (IParam n ty) = IParam n (subst_ty subst ty)
1289 substPred subst (ClassP clas tys) = ClassP clas (map (subst_ty subst) tys)
1290 substPred subst (EqPred ty1 ty2) = EqPred (subst_ty subst ty1) (subst_ty subst ty2)
1292 deShadowTy :: TyVarSet -> Type -> Type -- Remove any nested binders mentioning tvs
1294 = subst_ty (mkTvSubst in_scope emptyTvSubstEnv) ty
1296 in_scope = mkInScopeSet tvs
1298 subst_ty :: TvSubst -> Type -> Type
1299 -- subst_ty is the main workhorse for type substitution
1301 -- Note that the in_scope set is poked only if we hit a forall
1302 -- so it may often never be fully computed
1306 go (TyVarTy tv) = substTyVar subst tv
1307 go (TyConApp tc tys) = let args = map go tys
1308 in args `seqList` TyConApp tc args
1310 go (PredTy p) = PredTy $! (substPred subst p)
1312 go (NoteTy (FTVNote _) ty2) = go ty2 -- Discard the free tyvar note
1314 go (FunTy arg res) = (FunTy $! (go arg)) $! (go res)
1315 go (AppTy fun arg) = mkAppTy (go fun) $! (go arg)
1316 -- The mkAppTy smart constructor is important
1317 -- we might be replacing (a Int), represented with App
1318 -- by [Int], represented with TyConApp
1319 go (ForAllTy tv ty) = case substTyVarBndr subst tv of
1320 (subst', tv') -> ForAllTy tv' $! (subst_ty subst' ty)
1322 substTyVar :: TvSubst -> TyVar -> Type
1323 substTyVar subst@(TvSubst in_scope env) tv
1324 = case lookupTyVar subst tv of {
1325 Nothing -> TyVarTy tv;
1326 Just ty -> ty -- See Note [Apply Once]
1329 substTyVars :: TvSubst -> [TyVar] -> [Type]
1330 substTyVars subst tvs = map (substTyVar subst) tvs
1332 lookupTyVar :: TvSubst -> TyVar -> Maybe Type
1333 -- See Note [Extending the TvSubst]
1334 lookupTyVar (TvSubst in_scope env) tv = lookupVarEnv env tv
1336 substTyVarBndr :: TvSubst -> TyVar -> (TvSubst, TyVar)
1337 substTyVarBndr subst@(TvSubst in_scope env) old_var
1338 = (TvSubst (in_scope `extendInScopeSet` new_var) new_env, new_var)
1340 is_co_var = isCoVar old_var
1342 new_env | no_change = delVarEnv env old_var
1343 | otherwise = extendVarEnv env old_var (TyVarTy new_var)
1345 no_change = new_var == old_var && not is_co_var
1346 -- no_change means that the new_var is identical in
1347 -- all respects to the old_var (same unique, same kind)
1348 -- See Note [Extending the TvSubst]
1350 -- In that case we don't need to extend the substitution
1351 -- to map old to new. But instead we must zap any
1352 -- current substitution for the variable. For example:
1353 -- (\x.e) with id_subst = [x |-> e']
1354 -- Here we must simply zap the substitution for x
1356 new_var = uniqAway in_scope subst_old_var
1357 -- The uniqAway part makes sure the new variable is not already in scope
1359 subst_old_var -- subst_old_var is old_var with the substitution applied to its kind
1360 -- It's only worth doing the substitution for coercions,
1361 -- becuase only they can have free type variables
1362 | is_co_var = setTyVarKind old_var (substTy subst (tyVarKind old_var))
1363 | otherwise = old_var
1366 ----------------------------------------------------
1371 There's a little subtyping at the kind level:
1380 where * [LiftedTypeKind] means boxed type
1381 # [UnliftedTypeKind] means unboxed type
1382 (#) [UbxTupleKind] means unboxed tuple
1383 ?? [ArgTypeKind] is the lub of *,#
1384 ? [OpenTypeKind] means any type at all
1388 error :: forall a:?. String -> a
1389 (->) :: ?? -> ? -> *
1390 (\(x::t) -> ...) Here t::?? (i.e. not unboxed tuple)
1393 type KindVar = TyVar -- invariant: KindVar will always be a
1394 -- TcTyVar with details MetaTv TauTv ...
1395 -- kind var constructors and functions are in TcType
1397 type SimpleKind = Kind
1402 During kind inference, a kind variable unifies only with
1404 sk ::= * | sk1 -> sk2
1406 data T a = MkT a (T Int#)
1407 fails. We give T the kind (k -> *), and the kind variable k won't unify
1408 with # (the kind of Int#).
1412 When creating a fresh internal type variable, we give it a kind to express
1413 constraints on it. E.g. in (\x->e) we make up a fresh type variable for x,
1416 During unification we only bind an internal type variable to a type
1417 whose kind is lower in the sub-kind hierarchy than the kind of the tyvar.
1419 When unifying two internal type variables, we collect their kind constraints by
1420 finding the GLB of the two. Since the partial order is a tree, they only
1421 have a glb if one is a sub-kind of the other. In that case, we bind the
1422 less-informative one to the more informative one. Neat, eh?
1429 %************************************************************************
1431 Functions over Kinds
1433 %************************************************************************
1436 kindFunResult :: Kind -> Kind
1437 kindFunResult k = funResultTy k
1439 splitKindFunTys :: Kind -> ([Kind],Kind)
1440 splitKindFunTys k = splitFunTys k
1442 splitKindFunTysN :: Int -> Kind -> ([Kind],Kind)
1443 splitKindFunTysN k = splitFunTysN k
1445 isUbxTupleKind, isOpenTypeKind, isArgTypeKind, isUnliftedTypeKind :: Kind -> Bool
1447 isOpenTypeKindCon tc = tyConUnique tc == openTypeKindTyConKey
1449 isOpenTypeKind (TyConApp tc _) = isOpenTypeKindCon tc
1450 isOpenTypeKind other = False
1452 isUbxTupleKindCon tc = tyConUnique tc == ubxTupleKindTyConKey
1454 isUbxTupleKind (TyConApp tc _) = isUbxTupleKindCon tc
1455 isUbxTupleKind other = False
1457 isArgTypeKindCon tc = tyConUnique tc == argTypeKindTyConKey
1459 isArgTypeKind (TyConApp tc _) = isArgTypeKindCon tc
1460 isArgTypeKind other = False
1462 isUnliftedTypeKindCon tc = tyConUnique tc == unliftedTypeKindTyConKey
1464 isUnliftedTypeKind (TyConApp tc _) = isUnliftedTypeKindCon tc
1465 isUnliftedTypeKind other = False
1467 isSubOpenTypeKind :: Kind -> Bool
1468 -- True of any sub-kind of OpenTypeKind (i.e. anything except arrow)
1469 isSubOpenTypeKind (FunTy k1 k2) = ASSERT2 ( isKind k1, text "isSubOpenTypeKind" <+> ppr k1 <+> text "::" <+> ppr (typeKind k1) )
1470 ASSERT2 ( isKind k2, text "isSubOpenTypeKind" <+> ppr k2 <+> text "::" <+> ppr (typeKind k2) )
1472 isSubOpenTypeKind (TyConApp kc []) = ASSERT( isKind (TyConApp kc []) ) True
1473 isSubOpenTypeKind other = ASSERT( isKind other ) False
1474 -- This is a conservative answer
1475 -- It matters in the call to isSubKind in
1476 -- checkExpectedKind.
1478 isSubArgTypeKindCon kc
1479 | isUnliftedTypeKindCon kc = True
1480 | isLiftedTypeKindCon kc = True
1481 | isArgTypeKindCon kc = True
1484 isSubArgTypeKind :: Kind -> Bool
1485 -- True of any sub-kind of ArgTypeKind
1486 isSubArgTypeKind (TyConApp kc []) = isSubArgTypeKindCon kc
1487 isSubArgTypeKind other = False
1489 isSuperKind :: Type -> Bool
1490 isSuperKind (TyConApp (skc) []) = isSuperKindTyCon skc
1491 isSuperKind other = False
1493 isKind :: Kind -> Bool
1494 isKind k = isSuperKind (typeKind k)
1498 isSubKind :: Kind -> Kind -> Bool
1499 -- (k1 `isSubKind` k2) checks that k1 <: k2
1500 isSubKind (TyConApp kc1 []) (TyConApp kc2 []) = kc1 `isSubKindCon` kc2
1501 isSubKind (FunTy a1 r1) (FunTy a2 r2) = (a2 `isSubKind` a1) && (r1 `isSubKind` r2)
1502 isSubKind (PredTy (EqPred ty1 ty2)) (PredTy (EqPred ty1' ty2'))
1503 = ty1 `tcEqType` ty1' && ty2 `tcEqType` ty2'
1504 isSubKind k1 k2 = False
1506 eqKind :: Kind -> Kind -> Bool
1509 isSubKindCon :: TyCon -> TyCon -> Bool
1510 -- (kc1 `isSubKindCon` kc2) checks that kc1 <: kc2
1511 isSubKindCon kc1 kc2
1512 | isLiftedTypeKindCon kc1 && isLiftedTypeKindCon kc2 = True
1513 | isUnliftedTypeKindCon kc1 && isUnliftedTypeKindCon kc2 = True
1514 | isUbxTupleKindCon kc1 && isUbxTupleKindCon kc2 = True
1515 | isOpenTypeKindCon kc2 = True
1516 -- we already know kc1 is not a fun, its a TyCon
1517 | isArgTypeKindCon kc2 && isSubArgTypeKindCon kc1 = True
1520 defaultKind :: Kind -> Kind
1521 -- Used when generalising: default kind '?' and '??' to '*'
1523 -- When we generalise, we make generic type variables whose kind is
1524 -- simple (* or *->* etc). So generic type variables (other than
1525 -- built-in constants like 'error') always have simple kinds. This is important;
1528 -- We want f to get type
1529 -- f :: forall (a::*). a -> Bool
1531 -- f :: forall (a::??). a -> Bool
1532 -- because that would allow a call like (f 3#) as well as (f True),
1533 --and the calling conventions differ. This defaulting is done in TcMType.zonkTcTyVarBndr.
1535 | isSubOpenTypeKind k = liftedTypeKind
1536 | isSubArgTypeKind k = liftedTypeKind
1539 isEqPred :: PredType -> Bool
1540 isEqPred (EqPred _ _) = True
1541 isEqPred other = False