2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[Type]{Type - public interface}
8 -- re-exports from TypeRep:
9 Type, PredType, ThetaType,
12 superKind, superBoxity, -- KX and BX respectively
13 liftedBoxity, unliftedBoxity, -- :: BX
15 typeCon, -- :: BX -> KX
16 liftedTypeKind, unliftedTypeKind, openTypeKind, -- :: KX
17 mkArrowKind, mkArrowKinds, -- :: KX -> KX -> KX
18 isTypeKind, isAnyTypeKind,
21 usageKindCon, -- :: KX
22 usageTypeKind, -- :: KX
23 usOnceTyCon, usManyTyCon, -- :: $
24 usOnce, usMany, -- :: $
26 -- exports from this module:
27 hasMoreBoxityInfo, defaultKind,
29 mkTyVarTy, mkTyVarTys, getTyVar, getTyVar_maybe, isTyVarTy,
31 mkAppTy, mkAppTys, splitAppTy, splitAppTys, splitAppTy_maybe,
33 mkFunTy, mkFunTys, splitFunTy, splitFunTy_maybe, splitFunTys,
34 funResultTy, funArgTy, zipFunTys, isFunTy,
36 mkTyConApp, mkTyConTy,
37 tyConAppTyCon, tyConAppArgs,
38 splitTyConApp_maybe, splitTyConApp,
44 mkForAllTy, mkForAllTys, splitForAllTy_maybe, splitForAllTys,
45 applyTy, applyTys, isForAllTy, dropForAlls,
48 SourceType(..), sourceTypeRep, mkPredTy, mkPredTys,
54 isUnLiftedType, isUnboxedTupleType, isAlgType, isStrictType, isPrimitiveType,
57 tyVarsOfType, tyVarsOfTypes, tyVarsOfPred, tyVarsOfTheta,
58 typeKind, addFreeTyVars,
60 -- Tidying up for printing
62 tidyOpenType, tidyOpenTypes,
63 tidyTyVarBndr, tidyFreeTyVars,
64 tidyOpenTyVar, tidyOpenTyVars,
65 tidyTopType, tidyPred,
68 eqType, eqKind, eqUsage,
75 #include "HsVersions.h"
77 -- We import the representation and primitive functions from TypeRep.
78 -- Many things are reexported, but not the representation!
84 import {-# SOURCE #-} PprType( pprType ) -- Only called in debug messages
85 import {-# SOURCE #-} Subst ( substTyWith )
88 import Var ( TyVar, tyVarKind, tyVarName, setTyVarName )
92 import Name ( NamedThing(..), mkLocalName, tidyOccName )
93 import Class ( classTyCon )
94 import TyCon ( TyCon, isRecursiveTyCon, isPrimTyCon,
95 isUnboxedTupleTyCon, isUnLiftedTyCon,
96 isFunTyCon, isNewTyCon, newTyConRep,
97 isAlgTyCon, isSynTyCon, tyConArity,
98 tyConKind, getSynTyConDefn,
103 import CmdLineOpts ( opt_DictsStrict )
104 import SrcLoc ( noSrcLoc )
105 import PrimRep ( PrimRep(..) )
106 import Unique ( Uniquable(..) )
107 import Util ( mapAccumL, seqList, lengthIs )
109 import UniqSet ( sizeUniqSet ) -- Should come via VarSet
110 import Maybe ( isJust )
114 %************************************************************************
116 \subsection{Stuff to do with kinds.}
118 %************************************************************************
121 hasMoreBoxityInfo :: Kind -> Kind -> Bool
122 -- (k1 `hasMoreBoxityInfo` k2) checks that k1 <: k2
123 hasMoreBoxityInfo k1 k2
124 | k2 `eqKind` openTypeKind = isAnyTypeKind k1
125 | otherwise = k1 `eqKind` k2
128 isAnyTypeKind :: Kind -> Bool
129 -- True of kind * and *# and ?
130 isAnyTypeKind (TyConApp tc _) = tc == typeCon || tc == openKindCon
131 isAnyTypeKind (NoteTy _ k) = isAnyTypeKind k
132 isAnyTypeKind other = False
134 isTypeKind :: Kind -> Bool
135 -- True of kind * and *#
136 isTypeKind (TyConApp tc _) = tc == typeCon
137 isTypeKind (NoteTy _ k) = isTypeKind k
138 isTypeKind other = False
140 defaultKind :: Kind -> Kind
141 -- Used when generalising: default kind '?' to '*'
142 defaultKind kind | kind `eqKind` openTypeKind = liftedTypeKind
147 %************************************************************************
149 \subsection{Constructor-specific functions}
151 %************************************************************************
154 ---------------------------------------------------------------------
158 mkTyVarTy :: TyVar -> Type
161 mkTyVarTys :: [TyVar] -> [Type]
162 mkTyVarTys = map mkTyVarTy -- a common use of mkTyVarTy
164 getTyVar :: String -> Type -> TyVar
165 getTyVar msg (TyVarTy tv) = tv
166 getTyVar msg (SourceTy p) = getTyVar msg (sourceTypeRep p)
167 getTyVar msg (NoteTy _ t) = getTyVar msg t
168 getTyVar msg other = panic ("getTyVar: " ++ msg)
170 getTyVar_maybe :: Type -> Maybe TyVar
171 getTyVar_maybe (TyVarTy tv) = Just tv
172 getTyVar_maybe (NoteTy _ t) = getTyVar_maybe t
173 getTyVar_maybe (SourceTy p) = getTyVar_maybe (sourceTypeRep p)
174 getTyVar_maybe other = Nothing
176 isTyVarTy :: Type -> Bool
177 isTyVarTy (TyVarTy tv) = True
178 isTyVarTy (NoteTy _ ty) = isTyVarTy ty
179 isTyVarTy (SourceTy p) = isTyVarTy (sourceTypeRep p)
180 isTyVarTy other = False
184 ---------------------------------------------------------------------
187 We need to be pretty careful with AppTy to make sure we obey the
188 invariant that a TyConApp is always visibly so. mkAppTy maintains the
192 mkAppTy orig_ty1 orig_ty2
193 = ASSERT( not (isSourceTy orig_ty1) ) -- Source types are of kind *
196 mk_app (NoteTy _ ty1) = mk_app ty1
197 mk_app (TyConApp tc tys) = mkTyConApp tc (tys ++ [orig_ty2])
198 mk_app ty1 = AppTy orig_ty1 orig_ty2
200 mkAppTys :: Type -> [Type] -> Type
201 mkAppTys orig_ty1 [] = orig_ty1
202 -- This check for an empty list of type arguments
203 -- avoids the needless loss of a type synonym constructor.
204 -- For example: mkAppTys Rational []
205 -- returns to (Ratio Integer), which has needlessly lost
206 -- the Rational part.
207 mkAppTys orig_ty1 orig_tys2
208 = ASSERT( not (isSourceTy orig_ty1) ) -- Source types are of kind *
211 mk_app (NoteTy _ ty1) = mk_app ty1
212 mk_app (TyConApp tc tys) = mkTyConApp tc (tys ++ orig_tys2)
213 mk_app ty1 = foldl AppTy orig_ty1 orig_tys2
215 splitAppTy_maybe :: Type -> Maybe (Type, Type)
216 splitAppTy_maybe (FunTy ty1 ty2) = Just (TyConApp funTyCon [ty1], ty2)
217 splitAppTy_maybe (AppTy ty1 ty2) = Just (ty1, ty2)
218 splitAppTy_maybe (NoteTy _ ty) = splitAppTy_maybe ty
219 splitAppTy_maybe (SourceTy p) = splitAppTy_maybe (sourceTypeRep p)
220 splitAppTy_maybe (TyConApp tc []) = Nothing
221 splitAppTy_maybe (TyConApp tc tys) = split tys []
223 split [ty2] acc = Just (TyConApp tc (reverse acc), ty2)
224 split (ty:tys) acc = split tys (ty:acc)
226 splitAppTy_maybe other = Nothing
228 splitAppTy :: Type -> (Type, Type)
229 splitAppTy ty = case splitAppTy_maybe ty of
231 Nothing -> panic "splitAppTy"
233 splitAppTys :: Type -> (Type, [Type])
234 splitAppTys ty = split ty ty []
236 split orig_ty (AppTy ty arg) args = split ty ty (arg:args)
237 split orig_ty (NoteTy _ ty) args = split orig_ty ty args
238 split orig_ty (SourceTy p) args = split orig_ty (sourceTypeRep p) args
239 split orig_ty (FunTy ty1 ty2) args = ASSERT( null args )
240 (TyConApp funTyCon [], [ty1,ty2])
241 split orig_ty (TyConApp tc tc_args) args = (TyConApp tc [], tc_args ++ args)
242 split orig_ty ty args = (orig_ty, args)
246 ---------------------------------------------------------------------
251 mkFunTy :: Type -> Type -> Type
252 mkFunTy arg res = FunTy arg res
254 mkFunTys :: [Type] -> Type -> Type
255 mkFunTys tys ty = foldr FunTy ty tys
257 isFunTy :: Type -> Bool
258 isFunTy ty = isJust (splitFunTy_maybe ty)
260 splitFunTy :: Type -> (Type, Type)
261 splitFunTy (FunTy arg res) = (arg, res)
262 splitFunTy (NoteTy _ ty) = splitFunTy ty
263 splitFunTy (SourceTy p) = splitFunTy (sourceTypeRep p)
265 splitFunTy_maybe :: Type -> Maybe (Type, Type)
266 splitFunTy_maybe (FunTy arg res) = Just (arg, res)
267 splitFunTy_maybe (NoteTy _ ty) = splitFunTy_maybe ty
268 splitFunTy_maybe (SourceTy p) = splitFunTy_maybe (sourceTypeRep p)
269 splitFunTy_maybe other = Nothing
271 splitFunTys :: Type -> ([Type], Type)
272 splitFunTys ty = split [] ty ty
274 split args orig_ty (FunTy arg res) = split (arg:args) res res
275 split args orig_ty (NoteTy _ ty) = split args orig_ty ty
276 split args orig_ty (SourceTy p) = split args orig_ty (sourceTypeRep p)
277 split args orig_ty ty = (reverse args, orig_ty)
279 zipFunTys :: Outputable a => [a] -> Type -> ([(a,Type)], Type)
280 zipFunTys orig_xs orig_ty = split [] orig_xs orig_ty orig_ty
282 split acc [] nty ty = (reverse acc, nty)
283 split acc (x:xs) nty (FunTy arg res) = split ((x,arg):acc) xs res res
284 split acc xs nty (NoteTy _ ty) = split acc xs nty ty
285 split acc xs nty (SourceTy p) = split acc xs nty (sourceTypeRep p)
286 split acc (x:xs) nty ty = pprPanic "zipFunTys" (ppr orig_xs <+> pprType orig_ty)
288 funResultTy :: Type -> Type
289 funResultTy (FunTy arg res) = res
290 funResultTy (NoteTy _ ty) = funResultTy ty
291 funResultTy (SourceTy p) = funResultTy (sourceTypeRep p)
292 funResultTy ty = pprPanic "funResultTy" (pprType ty)
294 funArgTy :: Type -> Type
295 funArgTy (FunTy arg res) = arg
296 funArgTy (NoteTy _ ty) = funArgTy ty
297 funArgTy (SourceTy p) = funArgTy (sourceTypeRep p)
298 funArgTy ty = pprPanic "funArgTy" (pprType ty)
302 ---------------------------------------------------------------------
305 @mkTyConApp@ is a key function, because it builds a TyConApp, FunTy or SourceTy,
309 mkTyConApp :: TyCon -> [Type] -> Type
310 -- Assumes TyCon is not a SynTyCon; use mkSynTy instead for those
312 | isFunTyCon tycon, [ty1,ty2] <- tys
315 | isNewTyCon tycon, -- A saturated newtype application;
316 not (isRecursiveTyCon tycon), -- Not recursive (we don't use SourceTypes for them)
317 tys `lengthIs` tyConArity tycon -- use the SourceType form
318 = SourceTy (NType tycon tys)
321 = ASSERT(not (isSynTyCon tycon))
324 mkTyConTy :: TyCon -> Type
325 mkTyConTy tycon = ASSERT( not (isSynTyCon tycon) )
328 -- splitTyConApp "looks through" synonyms, because they don't
329 -- mean a distinct type, but all other type-constructor applications
330 -- including functions are returned as Just ..
332 tyConAppTyCon :: Type -> TyCon
333 tyConAppTyCon ty = fst (splitTyConApp ty)
335 tyConAppArgs :: Type -> [Type]
336 tyConAppArgs ty = snd (splitTyConApp ty)
338 splitTyConApp :: Type -> (TyCon, [Type])
339 splitTyConApp ty = case splitTyConApp_maybe ty of
341 Nothing -> pprPanic "splitTyConApp" (pprType ty)
343 splitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
344 splitTyConApp_maybe (TyConApp tc tys) = Just (tc, tys)
345 splitTyConApp_maybe (FunTy arg res) = Just (funTyCon, [arg,res])
346 splitTyConApp_maybe (NoteTy _ ty) = splitTyConApp_maybe ty
347 splitTyConApp_maybe (SourceTy p) = splitTyConApp_maybe (sourceTypeRep p)
348 splitTyConApp_maybe other = Nothing
352 ---------------------------------------------------------------------
358 | n_args == arity -- Exactly saturated
360 | n_args > arity -- Over-saturated
361 = case splitAt arity tys of { (as,bs) -> mkAppTys (mk_syn as) bs }
362 -- Its important to use mkAppTys, rather than (foldl AppTy),
363 -- because (mk_syn as) might well return a partially-applied
364 -- type constructor; indeed, usually will!
365 | otherwise -- Un-saturated
367 -- For the un-saturated case we build TyConApp directly
368 -- (mkTyConApp ASSERTs that the tc isn't a SynTyCon).
369 -- Here we are relying on checkValidType to find
370 -- the error. What we can't do is use mkSynTy with
371 -- too few arg tys, because that is utterly bogus.
374 mk_syn tys = NoteTy (SynNote (TyConApp tycon tys))
375 (substTyWith tyvars tys body)
377 (tyvars, body) = ASSERT( isSynTyCon tycon ) getSynTyConDefn tycon
378 arity = tyConArity tycon
382 Notes on type synonyms
383 ~~~~~~~~~~~~~~~~~~~~~~
384 The various "split" functions (splitFunTy, splitRhoTy, splitForAllTy) try
385 to return type synonyms whereever possible. Thus
390 splitFunTys (a -> Foo a) = ([a], Foo a)
393 The reason is that we then get better (shorter) type signatures in
394 interfaces. Notably this plays a role in tcTySigs in TcBinds.lhs.
399 repType looks through
403 (d) usage annotations
404 (e) [recursive] newtypes
405 It's useful in the back end.
407 Remember, non-recursive newtypes get expanded as part of the SourceTy case,
408 but recursive ones are represented by TyConApps and have to be expanded
412 repType :: Type -> Type
413 repType (ForAllTy _ ty) = repType ty
414 repType (NoteTy _ ty) = repType ty
415 repType (SourceTy p) = repType (sourceTypeRep p)
416 repType (TyConApp tc tys) | isNewTyCon tc && tys `lengthIs` tyConArity tc
417 = repType (newTypeRep tc tys)
421 typePrimRep :: Type -> PrimRep
422 typePrimRep ty = case repType ty of
423 TyConApp tc _ -> tyConPrimRep tc
425 AppTy _ _ -> PtrRep -- ??
431 ---------------------------------------------------------------------
436 mkForAllTy :: TyVar -> Type -> Type
438 = mkForAllTys [tyvar] ty
440 mkForAllTys :: [TyVar] -> Type -> Type
441 mkForAllTys tyvars ty = foldr ForAllTy ty tyvars
443 isForAllTy :: Type -> Bool
444 isForAllTy (NoteTy _ ty) = isForAllTy ty
445 isForAllTy (ForAllTy _ _) = True
446 isForAllTy other_ty = False
448 splitForAllTy_maybe :: Type -> Maybe (TyVar, Type)
449 splitForAllTy_maybe ty = splitFAT_m ty
451 splitFAT_m (NoteTy _ ty) = splitFAT_m ty
452 splitFAT_m (SourceTy p) = splitFAT_m (sourceTypeRep p)
453 splitFAT_m (ForAllTy tyvar ty) = Just(tyvar, ty)
454 splitFAT_m _ = Nothing
456 splitForAllTys :: Type -> ([TyVar], Type)
457 splitForAllTys ty = split ty ty []
459 split orig_ty (ForAllTy tv ty) tvs = split ty ty (tv:tvs)
460 split orig_ty (NoteTy _ ty) tvs = split orig_ty ty tvs
461 split orig_ty (SourceTy p) tvs = split orig_ty (sourceTypeRep p) tvs
462 split orig_ty t tvs = (reverse tvs, orig_ty)
464 dropForAlls :: Type -> Type
465 dropForAlls ty = snd (splitForAllTys ty)
468 -- (mkPiType now in CoreUtils)
470 Applying a for-all to its arguments. Lift usage annotation as required.
473 applyTy :: Type -> Type -> Type
474 applyTy (SourceTy p) arg = applyTy (sourceTypeRep p) arg
475 applyTy (NoteTy _ fun) arg = applyTy fun arg
476 applyTy (ForAllTy tv ty) arg = substTyWith [tv] [arg] ty
477 applyTy other arg = panic "applyTy"
479 applyTys :: Type -> [Type] -> Type
480 applyTys fun_ty arg_tys
481 = substTyWith tvs arg_tys ty
483 (mu, tvs, ty) = split fun_ty arg_tys
485 split fun_ty [] = (Nothing, [], fun_ty)
486 split (NoteTy _ fun_ty) args = split fun_ty args
487 split (SourceTy p) args = split (sourceTypeRep p) args
488 split (ForAllTy tv fun_ty) (arg:args) = case split fun_ty args of
489 (mu, tvs, ty) -> (mu, tv:tvs, ty)
490 split other_ty args = panic "applyTys"
494 %************************************************************************
496 \subsection{Source types}
498 %************************************************************************
500 A "source type" is a type that is a separate type as far as the type checker is
501 concerned, but which has low-level representation as far as the back end is concerned.
503 Source types are always lifted.
505 The key function is sourceTypeRep which gives the representation of a source type:
508 mkPredTy :: PredType -> Type
509 mkPredTy pred = SourceTy pred
511 mkPredTys :: ThetaType -> [Type]
512 mkPredTys preds = map SourceTy preds
514 sourceTypeRep :: SourceType -> Type
515 -- Convert a predicate to its "representation type";
516 -- the type of evidence for that predicate, which is actually passed at runtime
517 sourceTypeRep (IParam _ ty) = ty
518 sourceTypeRep (ClassP clas tys) = mkTyConApp (classTyCon clas) tys
519 -- Note the mkTyConApp; the classTyCon might be a newtype!
520 sourceTypeRep (NType tc tys) = newTypeRep tc tys
521 -- ToDo: Consider caching this substitution in a NType
523 isSourceTy :: Type -> Bool
524 isSourceTy (NoteTy _ ty) = isSourceTy ty
525 isSourceTy (SourceTy sty) = True
529 splitNewType_maybe :: Type -> Maybe Type
530 -- Newtypes that are recursive are reprsented by TyConApp, just
531 -- as they always were. Occasionally we want to find their representation type.
532 -- NB: remember that in this module, non-recursive newtypes are transparent
534 splitNewType_maybe ty
535 = case splitTyConApp_maybe ty of
536 Just (tc,tys) | isNewTyCon tc -> ASSERT( tys `lengthIs` tyConArity tc )
537 -- The assert should hold because repType should
538 -- only be applied to *types* (of kind *)
539 Just (newTypeRep tc tys)
542 -- A local helper function (not exported)
543 newTypeRep new_tycon tys = case newTyConRep new_tycon of
544 (tvs, rep_ty) -> substTyWith tvs tys rep_ty
548 %************************************************************************
550 \subsection{Kinds and free variables}
552 %************************************************************************
554 ---------------------------------------------------------------------
555 Finding the kind of a type
556 ~~~~~~~~~~~~~~~~~~~~~~~~~~
558 typeKind :: Type -> Kind
560 typeKind (TyVarTy tyvar) = tyVarKind tyvar
561 typeKind (TyConApp tycon tys) = foldr (\_ k -> funResultTy k) (tyConKind tycon) tys
562 typeKind (NoteTy _ ty) = typeKind ty
563 typeKind (SourceTy _) = liftedTypeKind -- Predicates are always
564 -- represented by lifted types
565 typeKind (AppTy fun arg) = funResultTy (typeKind fun)
567 typeKind (FunTy arg res) = fix_up (typeKind res)
569 fix_up (TyConApp tycon _) | tycon == typeCon
570 || tycon == openKindCon = liftedTypeKind
571 fix_up (NoteTy _ kind) = fix_up kind
573 -- The basic story is
574 -- typeKind (FunTy arg res) = typeKind res
575 -- But a function is lifted regardless of its result type
576 -- Hence the strange fix-up.
577 -- Note that 'res', being the result of a FunTy, can't have
578 -- a strange kind like (*->*).
580 typeKind (ForAllTy tv ty) = typeKind ty
584 ---------------------------------------------------------------------
585 Free variables of a type
586 ~~~~~~~~~~~~~~~~~~~~~~~~
588 tyVarsOfType :: Type -> TyVarSet
589 tyVarsOfType (TyVarTy tv) = unitVarSet tv
590 tyVarsOfType (TyConApp tycon tys) = tyVarsOfTypes tys
591 tyVarsOfType (NoteTy (FTVNote tvs) ty2) = tvs
592 tyVarsOfType (NoteTy (SynNote ty1) ty2) = tyVarsOfType ty1
593 tyVarsOfType (SourceTy sty) = tyVarsOfSourceType sty
594 tyVarsOfType (FunTy arg res) = tyVarsOfType arg `unionVarSet` tyVarsOfType res
595 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionVarSet` tyVarsOfType arg
596 tyVarsOfType (ForAllTy tyvar ty) = tyVarsOfType ty `minusVarSet` unitVarSet tyvar
598 tyVarsOfTypes :: [Type] -> TyVarSet
599 tyVarsOfTypes tys = foldr (unionVarSet.tyVarsOfType) emptyVarSet tys
601 tyVarsOfPred :: PredType -> TyVarSet
602 tyVarsOfPred = tyVarsOfSourceType -- Just a subtype
604 tyVarsOfSourceType :: SourceType -> TyVarSet
605 tyVarsOfSourceType (IParam _ ty) = tyVarsOfType ty
606 tyVarsOfSourceType (ClassP _ tys) = tyVarsOfTypes tys
607 tyVarsOfSourceType (NType _ tys) = tyVarsOfTypes tys
609 tyVarsOfTheta :: ThetaType -> TyVarSet
610 tyVarsOfTheta = foldr (unionVarSet . tyVarsOfSourceType) emptyVarSet
612 -- Add a Note with the free tyvars to the top of the type
613 addFreeTyVars :: Type -> Type
614 addFreeTyVars ty@(NoteTy (FTVNote _) _) = ty
615 addFreeTyVars ty = NoteTy (FTVNote (tyVarsOfType ty)) ty
620 %************************************************************************
622 \subsection{TidyType}
624 %************************************************************************
626 tidyTy tidies up a type for printing in an error message, or in
629 It doesn't change the uniques at all, just the print names.
632 tidyTyVarBndr :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
633 tidyTyVarBndr (tidy_env, subst) tyvar
634 = case tidyOccName tidy_env (getOccName name) of
635 (tidy', occ') -> -- New occname reqd
636 ((tidy', subst'), tyvar')
638 subst' = extendVarEnv subst tyvar tyvar'
639 tyvar' = setTyVarName tyvar name'
640 name' = mkLocalName (getUnique name) occ' noSrcLoc
641 -- Note: make a *user* tyvar, so it printes nicely
642 -- Could extract src loc, but no need.
644 name = tyVarName tyvar
646 tidyFreeTyVars :: TidyEnv -> TyVarSet -> TidyEnv
647 -- Add the free tyvars to the env in tidy form,
648 -- so that we can tidy the type they are free in
649 tidyFreeTyVars env tyvars = fst (tidyOpenTyVars env (varSetElems tyvars))
651 tidyOpenTyVars :: TidyEnv -> [TyVar] -> (TidyEnv, [TyVar])
652 tidyOpenTyVars env tyvars = mapAccumL tidyOpenTyVar env tyvars
654 tidyOpenTyVar :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
655 -- Treat a new tyvar as a binder, and give it a fresh tidy name
656 tidyOpenTyVar env@(tidy_env, subst) tyvar
657 = case lookupVarEnv subst tyvar of
658 Just tyvar' -> (env, tyvar') -- Already substituted
659 Nothing -> tidyTyVarBndr env tyvar -- Treat it as a binder
661 tidyType :: TidyEnv -> Type -> Type
662 tidyType env@(tidy_env, subst) ty
665 go (TyVarTy tv) = case lookupVarEnv subst tv of
666 Nothing -> TyVarTy tv
667 Just tv' -> TyVarTy tv'
668 go (TyConApp tycon tys) = let args = map go tys
669 in args `seqList` TyConApp tycon args
670 go (NoteTy note ty) = (NoteTy $! (go_note note)) $! (go ty)
671 go (SourceTy sty) = SourceTy (tidySourceType env sty)
672 go (AppTy fun arg) = (AppTy $! (go fun)) $! (go arg)
673 go (FunTy fun arg) = (FunTy $! (go fun)) $! (go arg)
674 go (ForAllTy tv ty) = ForAllTy tvp $! (tidyType envp ty)
676 (envp, tvp) = tidyTyVarBndr env tv
678 go_note (SynNote ty) = SynNote $! (go ty)
679 go_note note@(FTVNote ftvs) = note -- No need to tidy the free tyvars
681 tidyTypes env tys = map (tidyType env) tys
683 tidyPred :: TidyEnv -> SourceType -> SourceType
684 tidyPred = tidySourceType
686 tidySourceType :: TidyEnv -> SourceType -> SourceType
687 tidySourceType env (IParam n ty) = IParam n (tidyType env ty)
688 tidySourceType env (ClassP clas tys) = ClassP clas (tidyTypes env tys)
689 tidySourceType env (NType tc tys) = NType tc (tidyTypes env tys)
693 @tidyOpenType@ grabs the free type variables, tidies them
694 and then uses @tidyType@ to work over the type itself
697 tidyOpenType :: TidyEnv -> Type -> (TidyEnv, Type)
699 = (env', tidyType env' ty)
701 env' = tidyFreeTyVars env (tyVarsOfType ty)
703 tidyOpenTypes :: TidyEnv -> [Type] -> (TidyEnv, [Type])
704 tidyOpenTypes env tys = mapAccumL tidyOpenType env tys
706 tidyTopType :: Type -> Type
707 tidyTopType ty = tidyType emptyTidyEnv ty
712 %************************************************************************
714 \subsection{Liftedness}
716 %************************************************************************
719 isUnLiftedType :: Type -> Bool
720 -- isUnLiftedType returns True for forall'd unlifted types:
721 -- x :: forall a. Int#
722 -- I found bindings like these were getting floated to the top level.
723 -- They are pretty bogus types, mind you. It would be better never to
726 isUnLiftedType (ForAllTy tv ty) = isUnLiftedType ty
727 isUnLiftedType (NoteTy _ ty) = isUnLiftedType ty
728 isUnLiftedType (TyConApp tc _) = isUnLiftedTyCon tc
729 isUnLiftedType (SourceTy _) = False -- All source types are lifted
730 isUnLiftedType other = False
732 isUnboxedTupleType :: Type -> Bool
733 isUnboxedTupleType ty = case splitTyConApp_maybe ty of
734 Just (tc, ty_args) -> isUnboxedTupleTyCon tc
737 -- Should only be applied to *types*; hence the assert
738 isAlgType :: Type -> Bool
739 isAlgType ty = case splitTyConApp_maybe ty of
740 Just (tc, ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
745 @isStrictType@ computes whether an argument (or let RHS) should
746 be computed strictly or lazily, based only on its type.
747 Works just like isUnLiftedType, except that it has a special case
748 for dictionaries. Since it takes account of ClassP, you might think
749 this function should be in TcType, but isStrictType is used by DataCon,
750 which is below TcType in the hierarchy, so it's convenient to put it here.
753 isStrictType (ForAllTy tv ty) = isStrictType ty
754 isStrictType (NoteTy _ ty) = isStrictType ty
755 isStrictType (TyConApp tc _) = isUnLiftedTyCon tc
756 isStrictType (SourceTy (ClassP clas _)) = opt_DictsStrict && not (isNewTyCon (classTyCon clas))
757 -- We may be strict in dictionary types, but only if it
758 -- has more than one component.
759 -- [Being strict in a single-component dictionary risks
760 -- poking the dictionary component, which is wrong.]
761 isStrictType other = False
765 isPrimitiveType :: Type -> Bool
766 -- Returns types that are opaque to Haskell.
767 -- Most of these are unlifted, but now that we interact with .NET, we
768 -- may have primtive (foreign-imported) types that are lifted
769 isPrimitiveType ty = case splitTyConApp_maybe ty of
770 Just (tc, ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
776 %************************************************************************
778 \subsection{Sequencing on types
780 %************************************************************************
783 seqType :: Type -> ()
784 seqType (TyVarTy tv) = tv `seq` ()
785 seqType (AppTy t1 t2) = seqType t1 `seq` seqType t2
786 seqType (FunTy t1 t2) = seqType t1 `seq` seqType t2
787 seqType (NoteTy note t2) = seqNote note `seq` seqType t2
788 seqType (SourceTy p) = seqPred p
789 seqType (TyConApp tc tys) = tc `seq` seqTypes tys
790 seqType (ForAllTy tv ty) = tv `seq` seqType ty
792 seqTypes :: [Type] -> ()
794 seqTypes (ty:tys) = seqType ty `seq` seqTypes tys
796 seqNote :: TyNote -> ()
797 seqNote (SynNote ty) = seqType ty
798 seqNote (FTVNote set) = sizeUniqSet set `seq` ()
800 seqPred :: SourceType -> ()
801 seqPred (ClassP c tys) = c `seq` seqTypes tys
802 seqPred (NType tc tys) = tc `seq` seqTypes tys
803 seqPred (IParam n ty) = n `seq` seqType ty
807 %************************************************************************
809 \subsection{Equality on types}
811 %************************************************************************
813 Comparison; don't use instances so that we know where it happens.
814 Look through newtypes but not usage types.
816 Note that eqType can respond 'False' for partial applications of newtypes.
818 newtype Parser m a = MkParser (Foogle m a)
821 Monad (Parser m) `eqType` Monad (Foogle m)
823 Well, yes, but eqType won't see that they are the same.
824 I don't think this is harmful, but it's soemthing to watch out for.
827 eqType t1 t2 = eq_ty emptyVarEnv t1 t2
828 eqKind = eqType -- No worries about looking
829 eqUsage = eqType -- through source types for these two
831 -- Look through Notes
832 eq_ty env (NoteTy _ t1) t2 = eq_ty env t1 t2
833 eq_ty env t1 (NoteTy _ t2) = eq_ty env t1 t2
835 -- Look through SourceTy. This is where the looping danger comes from
836 eq_ty env (SourceTy sty1) t2 = eq_ty env (sourceTypeRep sty1) t2
837 eq_ty env t1 (SourceTy sty2) = eq_ty env t1 (sourceTypeRep sty2)
839 -- The rest is plain sailing
840 eq_ty env (TyVarTy tv1) (TyVarTy tv2) = case lookupVarEnv env tv1 of
841 Just tv1a -> tv1a == tv2
842 Nothing -> tv1 == tv2
843 eq_ty env (ForAllTy tv1 t1) (ForAllTy tv2 t2)
844 | tv1 == tv2 = eq_ty (delVarEnv env tv1) t1 t2
845 | otherwise = eq_ty (extendVarEnv env tv1 tv2) t1 t2
846 eq_ty env (AppTy s1 t1) (AppTy s2 t2) = (eq_ty env s1 s2) && (eq_ty env t1 t2)
847 eq_ty env (FunTy s1 t1) (FunTy s2 t2) = (eq_ty env s1 s2) && (eq_ty env t1 t2)
848 eq_ty env (TyConApp tc1 tys1) (TyConApp tc2 tys2) = (tc1 == tc2) && (eq_tys env tys1 tys2)
849 eq_ty env t1 t2 = False
851 eq_tys env [] [] = True
852 eq_tys env (t1:tys1) (t2:tys2) = (eq_ty env t1 t2) && (eq_tys env tys1 tys2)
853 eq_tys env tys1 tys2 = False