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 TyThing(..), isTyClThing,
14 superKind, superBoxity, -- KX and BX respectively
15 liftedBoxity, unliftedBoxity, -- :: BX
17 typeCon, -- :: BX -> KX
18 liftedTypeKind, unliftedTypeKind, openTypeKind, -- :: KX
19 mkArrowKind, mkArrowKinds, -- :: KX -> KX -> KX
20 isTypeKind, isAnyTypeKind,
23 usageKindCon, -- :: KX
24 usageTypeKind, -- :: KX
25 usOnceTyCon, usManyTyCon, -- :: $
26 usOnce, usMany, -- :: $
28 -- exports from this module:
29 hasMoreBoxityInfo, defaultKind,
31 mkTyVarTy, mkTyVarTys, getTyVar, getTyVar_maybe, isTyVarTy,
33 mkAppTy, mkAppTys, splitAppTy, splitAppTys, splitAppTy_maybe,
35 mkFunTy, mkFunTys, splitFunTy, splitFunTy_maybe, splitFunTys,
36 funResultTy, funArgTy, zipFunTys, isFunTy,
38 mkGenTyConApp, mkTyConApp, mkTyConTy,
39 tyConAppTyCon, tyConAppArgs,
40 splitTyConApp_maybe, splitTyConApp,
46 mkForAllTy, mkForAllTys, splitForAllTy_maybe, splitForAllTys,
47 applyTy, applyTys, isForAllTy, dropForAlls,
50 SourceType(..), sourceTypeRep, mkPredTy, mkPredTys,
56 isUnLiftedType, isUnboxedTupleType, isAlgType, isStrictType, isPrimitiveType,
59 tyVarsOfType, tyVarsOfTypes, tyVarsOfPred, tyVarsOfTheta,
60 typeKind, addFreeTyVars,
62 -- Tidying up for printing
64 tidyOpenType, tidyOpenTypes,
65 tidyTyVarBndr, tidyFreeTyVars,
66 tidyOpenTyVar, tidyOpenTyVars,
67 tidyTopType, tidyPred,
70 eqType, eqKind, eqUsage,
77 #include "HsVersions.h"
79 -- We import the representation and primitive functions from TypeRep.
80 -- Many things are reexported, but not the representation!
86 import {-# SOURCE #-} PprType( pprType ) -- Only called in debug messages
87 import {-# SOURCE #-} Subst ( substTyWith )
90 import Var ( Id, TyVar, tyVarKind, tyVarName, setTyVarName )
94 import Name ( NamedThing(..), mkInternalName, tidyOccName )
95 import Class ( Class, classTyCon )
96 import TyCon ( TyCon, isRecursiveTyCon, isPrimTyCon,
97 isUnboxedTupleTyCon, isUnLiftedTyCon,
98 isFunTyCon, isNewTyCon, newTyConRep,
99 isAlgTyCon, isSynTyCon, tyConArity,
100 tyConKind, getSynTyConDefn,
105 import CmdLineOpts ( opt_DictsStrict )
106 import SrcLoc ( noSrcLoc )
107 import PrimRep ( PrimRep(..) )
108 import Unique ( Uniquable(..) )
109 import Util ( mapAccumL, seqList, lengthIs )
111 import UniqSet ( sizeUniqSet ) -- Should come via VarSet
112 import Maybe ( isJust )
116 %************************************************************************
120 %************************************************************************
123 data TyThing = AnId Id
127 isTyClThing :: TyThing -> Bool
128 isTyClThing (ATyCon _) = True
129 isTyClThing (AClass _) = True
130 isTyClThing (AnId _) = False
132 instance NamedThing TyThing where
133 getName (AnId id) = getName id
134 getName (ATyCon tc) = getName tc
135 getName (AClass cl) = getName cl
139 %************************************************************************
141 \subsection{Stuff to do with kinds.}
143 %************************************************************************
146 hasMoreBoxityInfo :: Kind -> Kind -> Bool
147 -- (k1 `hasMoreBoxityInfo` k2) checks that k1 <: k2
148 hasMoreBoxityInfo k1 k2
149 | k2 `eqKind` openTypeKind = isAnyTypeKind k1
150 | otherwise = k1 `eqKind` k2
153 isAnyTypeKind :: Kind -> Bool
154 -- True of kind * and *# and ?
155 isAnyTypeKind (TyConApp tc _) = tc == typeCon || tc == openKindCon
156 isAnyTypeKind (NoteTy _ k) = isAnyTypeKind k
157 isAnyTypeKind other = False
159 isTypeKind :: Kind -> Bool
160 -- True of kind * and *#
161 isTypeKind (TyConApp tc _) = tc == typeCon
162 isTypeKind (NoteTy _ k) = isTypeKind k
163 isTypeKind other = False
165 defaultKind :: Kind -> Kind
166 -- Used when generalising: default kind '?' to '*'
167 defaultKind kind | kind `eqKind` openTypeKind = liftedTypeKind
172 %************************************************************************
174 \subsection{Constructor-specific functions}
176 %************************************************************************
179 ---------------------------------------------------------------------
183 mkTyVarTy :: TyVar -> Type
186 mkTyVarTys :: [TyVar] -> [Type]
187 mkTyVarTys = map mkTyVarTy -- a common use of mkTyVarTy
189 getTyVar :: String -> Type -> TyVar
190 getTyVar msg (TyVarTy tv) = tv
191 getTyVar msg (SourceTy p) = getTyVar msg (sourceTypeRep p)
192 getTyVar msg (NoteTy _ t) = getTyVar msg t
193 getTyVar msg other = panic ("getTyVar: " ++ msg)
195 getTyVar_maybe :: Type -> Maybe TyVar
196 getTyVar_maybe (TyVarTy tv) = Just tv
197 getTyVar_maybe (NoteTy _ t) = getTyVar_maybe t
198 getTyVar_maybe (SourceTy p) = getTyVar_maybe (sourceTypeRep p)
199 getTyVar_maybe other = Nothing
201 isTyVarTy :: Type -> Bool
202 isTyVarTy (TyVarTy tv) = True
203 isTyVarTy (NoteTy _ ty) = isTyVarTy ty
204 isTyVarTy (SourceTy p) = isTyVarTy (sourceTypeRep p)
205 isTyVarTy other = False
209 ---------------------------------------------------------------------
212 We need to be pretty careful with AppTy to make sure we obey the
213 invariant that a TyConApp is always visibly so. mkAppTy maintains the
217 mkAppTy orig_ty1 orig_ty2
218 = ASSERT( not (isSourceTy orig_ty1) ) -- Source types are of kind *
221 mk_app (NoteTy _ ty1) = mk_app ty1
222 mk_app (TyConApp tc tys) = mkGenTyConApp tc (tys ++ [orig_ty2])
223 mk_app ty1 = AppTy orig_ty1 orig_ty2
224 -- We call mkGenTyConApp because the TyConApp could be an
225 -- under-saturated type synonym. GHC allows that; e.g.
226 -- type Foo k = k a -> k a
228 -- foo :: Foo Id -> Foo Id
230 -- Here Id is partially applied in the type sig for Foo,
231 -- but once the type synonyms are expanded all is well
233 mkAppTys :: Type -> [Type] -> Type
234 mkAppTys orig_ty1 [] = orig_ty1
235 -- This check for an empty list of type arguments
236 -- avoids the needless loss of a type synonym constructor.
237 -- For example: mkAppTys Rational []
238 -- returns to (Ratio Integer), which has needlessly lost
239 -- the Rational part.
240 mkAppTys orig_ty1 orig_tys2
241 = ASSERT( not (isSourceTy orig_ty1) ) -- Source types are of kind *
244 mk_app (NoteTy _ ty1) = mk_app ty1
245 mk_app (TyConApp tc tys) = mkTyConApp tc (tys ++ orig_tys2)
246 mk_app ty1 = foldl AppTy orig_ty1 orig_tys2
248 splitAppTy_maybe :: Type -> Maybe (Type, Type)
249 splitAppTy_maybe (FunTy ty1 ty2) = Just (TyConApp funTyCon [ty1], ty2)
250 splitAppTy_maybe (AppTy ty1 ty2) = Just (ty1, ty2)
251 splitAppTy_maybe (NoteTy _ ty) = splitAppTy_maybe ty
252 splitAppTy_maybe (SourceTy p) = splitAppTy_maybe (sourceTypeRep p)
253 splitAppTy_maybe (TyConApp tc []) = Nothing
254 splitAppTy_maybe (TyConApp tc tys) = split tys []
256 split [ty2] acc = Just (TyConApp tc (reverse acc), ty2)
257 split (ty:tys) acc = split tys (ty:acc)
259 splitAppTy_maybe other = Nothing
261 splitAppTy :: Type -> (Type, Type)
262 splitAppTy ty = case splitAppTy_maybe ty of
264 Nothing -> panic "splitAppTy"
266 splitAppTys :: Type -> (Type, [Type])
267 splitAppTys ty = split ty ty []
269 split orig_ty (AppTy ty arg) args = split ty ty (arg:args)
270 split orig_ty (NoteTy _ ty) args = split orig_ty ty args
271 split orig_ty (SourceTy p) args = split orig_ty (sourceTypeRep p) args
272 split orig_ty (FunTy ty1 ty2) args = ASSERT( null args )
273 (TyConApp funTyCon [], [ty1,ty2])
274 split orig_ty (TyConApp tc tc_args) args = (TyConApp tc [], tc_args ++ args)
275 split orig_ty ty args = (orig_ty, args)
279 ---------------------------------------------------------------------
284 mkFunTy :: Type -> Type -> Type
285 mkFunTy arg res = FunTy arg res
287 mkFunTys :: [Type] -> Type -> Type
288 mkFunTys tys ty = foldr FunTy ty tys
290 isFunTy :: Type -> Bool
291 isFunTy ty = isJust (splitFunTy_maybe ty)
293 splitFunTy :: Type -> (Type, Type)
294 splitFunTy (FunTy arg res) = (arg, res)
295 splitFunTy (NoteTy _ ty) = splitFunTy ty
296 splitFunTy (SourceTy p) = splitFunTy (sourceTypeRep p)
298 splitFunTy_maybe :: Type -> Maybe (Type, Type)
299 splitFunTy_maybe (FunTy arg res) = Just (arg, res)
300 splitFunTy_maybe (NoteTy _ ty) = splitFunTy_maybe ty
301 splitFunTy_maybe (SourceTy p) = splitFunTy_maybe (sourceTypeRep p)
302 splitFunTy_maybe other = Nothing
304 splitFunTys :: Type -> ([Type], Type)
305 splitFunTys ty = split [] ty ty
307 split args orig_ty (FunTy arg res) = split (arg:args) res res
308 split args orig_ty (NoteTy _ ty) = split args orig_ty ty
309 split args orig_ty (SourceTy p) = split args orig_ty (sourceTypeRep p)
310 split args orig_ty ty = (reverse args, orig_ty)
312 zipFunTys :: Outputable a => [a] -> Type -> ([(a,Type)], Type)
313 zipFunTys orig_xs orig_ty = split [] orig_xs orig_ty orig_ty
315 split acc [] nty ty = (reverse acc, nty)
316 split acc (x:xs) nty (FunTy arg res) = split ((x,arg):acc) xs res res
317 split acc xs nty (NoteTy _ ty) = split acc xs nty ty
318 split acc xs nty (SourceTy p) = split acc xs nty (sourceTypeRep p)
319 split acc (x:xs) nty ty = pprPanic "zipFunTys" (ppr orig_xs <+> pprType orig_ty)
321 funResultTy :: Type -> Type
322 funResultTy (FunTy arg res) = res
323 funResultTy (NoteTy _ ty) = funResultTy ty
324 funResultTy (SourceTy p) = funResultTy (sourceTypeRep p)
325 funResultTy ty = pprPanic "funResultTy" (pprType ty)
327 funArgTy :: Type -> Type
328 funArgTy (FunTy arg res) = arg
329 funArgTy (NoteTy _ ty) = funArgTy ty
330 funArgTy (SourceTy p) = funArgTy (sourceTypeRep p)
331 funArgTy ty = pprPanic "funArgTy" (pprType ty)
335 ---------------------------------------------------------------------
338 @mkTyConApp@ is a key function, because it builds a TyConApp, FunTy or SourceTy,
342 mkGenTyConApp :: TyCon -> [Type] -> Type
344 | isSynTyCon tc = mkSynTy tc tys
345 | otherwise = mkTyConApp tc tys
347 mkTyConApp :: TyCon -> [Type] -> Type
348 -- Assumes TyCon is not a SynTyCon; use mkSynTy instead for those
350 | isFunTyCon tycon, [ty1,ty2] <- tys
353 | isNewTyCon tycon, -- A saturated newtype application;
354 not (isRecursiveTyCon tycon), -- Not recursive (we don't use SourceTypes for them)
355 tys `lengthIs` tyConArity tycon -- use the SourceType form
356 = SourceTy (NType tycon tys)
359 = ASSERT(not (isSynTyCon tycon))
362 mkTyConTy :: TyCon -> Type
363 mkTyConTy tycon = ASSERT( not (isSynTyCon tycon) )
366 -- splitTyConApp "looks through" synonyms, because they don't
367 -- mean a distinct type, but all other type-constructor applications
368 -- including functions are returned as Just ..
370 tyConAppTyCon :: Type -> TyCon
371 tyConAppTyCon ty = fst (splitTyConApp ty)
373 tyConAppArgs :: Type -> [Type]
374 tyConAppArgs ty = snd (splitTyConApp ty)
376 splitTyConApp :: Type -> (TyCon, [Type])
377 splitTyConApp ty = case splitTyConApp_maybe ty of
379 Nothing -> pprPanic "splitTyConApp" (pprType ty)
381 splitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
382 splitTyConApp_maybe (TyConApp tc tys) = Just (tc, tys)
383 splitTyConApp_maybe (FunTy arg res) = Just (funTyCon, [arg,res])
384 splitTyConApp_maybe (NoteTy _ ty) = splitTyConApp_maybe ty
385 splitTyConApp_maybe (SourceTy p) = splitTyConApp_maybe (sourceTypeRep p)
386 splitTyConApp_maybe other = Nothing
390 ---------------------------------------------------------------------
396 | n_args == arity -- Exactly saturated
398 | n_args > arity -- Over-saturated
399 = case splitAt arity tys of { (as,bs) -> mkAppTys (mk_syn as) bs }
400 -- Its important to use mkAppTys, rather than (foldl AppTy),
401 -- because (mk_syn as) might well return a partially-applied
402 -- type constructor; indeed, usually will!
403 | otherwise -- Un-saturated
405 -- For the un-saturated case we build TyConApp directly
406 -- (mkTyConApp ASSERTs that the tc isn't a SynTyCon).
407 -- Here we are relying on checkValidType to find
408 -- the error. What we can't do is use mkSynTy with
409 -- too few arg tys, because that is utterly bogus.
412 mk_syn tys = NoteTy (SynNote (TyConApp tycon tys))
413 (substTyWith tyvars tys body)
415 (tyvars, body) = ASSERT( isSynTyCon tycon ) getSynTyConDefn tycon
416 arity = tyConArity tycon
420 Notes on type synonyms
421 ~~~~~~~~~~~~~~~~~~~~~~
422 The various "split" functions (splitFunTy, splitRhoTy, splitForAllTy) try
423 to return type synonyms whereever possible. Thus
428 splitFunTys (a -> Foo a) = ([a], Foo a)
431 The reason is that we then get better (shorter) type signatures in
432 interfaces. Notably this plays a role in tcTySigs in TcBinds.lhs.
437 repType looks through
441 (d) usage annotations
442 (e) [recursive] newtypes
443 It's useful in the back end.
445 Remember, non-recursive newtypes get expanded as part of the SourceTy case,
446 but recursive ones are represented by TyConApps and have to be expanded
450 repType :: Type -> Type
451 repType (ForAllTy _ ty) = repType ty
452 repType (NoteTy _ ty) = repType ty
453 repType (SourceTy p) = repType (sourceTypeRep p)
454 repType (TyConApp tc tys) | isNewTyCon tc && tys `lengthIs` tyConArity tc
455 = repType (newTypeRep tc tys)
459 typePrimRep :: Type -> PrimRep
460 typePrimRep ty = case repType ty of
461 TyConApp tc _ -> tyConPrimRep tc
463 AppTy _ _ -> PtrRep -- ??
469 ---------------------------------------------------------------------
474 mkForAllTy :: TyVar -> Type -> Type
476 = mkForAllTys [tyvar] ty
478 mkForAllTys :: [TyVar] -> Type -> Type
479 mkForAllTys tyvars ty = foldr ForAllTy ty tyvars
481 isForAllTy :: Type -> Bool
482 isForAllTy (NoteTy _ ty) = isForAllTy ty
483 isForAllTy (ForAllTy _ _) = True
484 isForAllTy other_ty = False
486 splitForAllTy_maybe :: Type -> Maybe (TyVar, Type)
487 splitForAllTy_maybe ty = splitFAT_m ty
489 splitFAT_m (NoteTy _ ty) = splitFAT_m ty
490 splitFAT_m (SourceTy p) = splitFAT_m (sourceTypeRep p)
491 splitFAT_m (ForAllTy tyvar ty) = Just(tyvar, ty)
492 splitFAT_m _ = Nothing
494 splitForAllTys :: Type -> ([TyVar], Type)
495 splitForAllTys ty = split ty ty []
497 split orig_ty (ForAllTy tv ty) tvs = split ty ty (tv:tvs)
498 split orig_ty (NoteTy _ ty) tvs = split orig_ty ty tvs
499 split orig_ty (SourceTy p) tvs = split orig_ty (sourceTypeRep p) tvs
500 split orig_ty t tvs = (reverse tvs, orig_ty)
502 dropForAlls :: Type -> Type
503 dropForAlls ty = snd (splitForAllTys ty)
506 -- (mkPiType now in CoreUtils)
508 Applying a for-all to its arguments. Lift usage annotation as required.
511 applyTy :: Type -> Type -> Type
512 applyTy (SourceTy p) arg = applyTy (sourceTypeRep p) arg
513 applyTy (NoteTy _ fun) arg = applyTy fun arg
514 applyTy (ForAllTy tv ty) arg = substTyWith [tv] [arg] ty
515 applyTy other arg = panic "applyTy"
517 applyTys :: Type -> [Type] -> Type
518 applyTys orig_fun_ty arg_tys
519 = substTyWith tvs arg_tys ty
521 (tvs, ty) = split orig_fun_ty arg_tys
523 split fun_ty [] = ([], fun_ty)
524 split (NoteTy _ fun_ty) args = split fun_ty args
525 split (SourceTy p) args = split (sourceTypeRep p) args
526 split (ForAllTy tv fun_ty) (arg:args) = case split fun_ty args of
527 (tvs, ty) -> (tv:tvs, ty)
528 split other_ty args = panic "applyTys"
529 -- No show instance for Type yet
533 %************************************************************************
535 \subsection{Source types}
537 %************************************************************************
539 A "source type" is a type that is a separate type as far as the type checker is
540 concerned, but which has low-level representation as far as the back end is concerned.
542 Source types are always lifted.
544 The key function is sourceTypeRep which gives the representation of a source type:
547 mkPredTy :: PredType -> Type
548 mkPredTy pred = SourceTy pred
550 mkPredTys :: ThetaType -> [Type]
551 mkPredTys preds = map SourceTy preds
553 sourceTypeRep :: SourceType -> Type
554 -- Convert a predicate to its "representation type";
555 -- the type of evidence for that predicate, which is actually passed at runtime
556 sourceTypeRep (IParam _ ty) = ty
557 sourceTypeRep (ClassP clas tys) = mkTyConApp (classTyCon clas) tys
558 -- Note the mkTyConApp; the classTyCon might be a newtype!
559 sourceTypeRep (NType tc tys) = newTypeRep tc tys
560 -- ToDo: Consider caching this substitution in a NType
562 isSourceTy :: Type -> Bool
563 isSourceTy (NoteTy _ ty) = isSourceTy ty
564 isSourceTy (SourceTy sty) = True
568 splitNewType_maybe :: Type -> Maybe Type
569 -- Newtypes that are recursive are reprsented by TyConApp, just
570 -- as they always were. Occasionally we want to find their representation type.
571 -- NB: remember that in this module, non-recursive newtypes are transparent
573 splitNewType_maybe ty
574 = case splitTyConApp_maybe ty of
575 Just (tc,tys) | isNewTyCon tc -> ASSERT( tys `lengthIs` tyConArity tc )
576 -- The assert should hold because repType should
577 -- only be applied to *types* (of kind *)
578 Just (newTypeRep tc tys)
581 -- A local helper function (not exported)
582 newTypeRep new_tycon tys = case newTyConRep new_tycon of
583 (tvs, rep_ty) -> substTyWith tvs tys rep_ty
587 %************************************************************************
589 \subsection{Kinds and free variables}
591 %************************************************************************
593 ---------------------------------------------------------------------
594 Finding the kind of a type
595 ~~~~~~~~~~~~~~~~~~~~~~~~~~
597 typeKind :: Type -> Kind
599 typeKind (TyVarTy tyvar) = tyVarKind tyvar
600 typeKind (TyConApp tycon tys) = foldr (\_ k -> funResultTy k) (tyConKind tycon) tys
601 typeKind (NoteTy _ ty) = typeKind ty
602 typeKind (SourceTy _) = liftedTypeKind -- Predicates are always
603 -- represented by lifted types
604 typeKind (AppTy fun arg) = funResultTy (typeKind fun)
606 typeKind (FunTy arg res) = fix_up (typeKind res)
608 fix_up (TyConApp tycon _) | tycon == typeCon
609 || tycon == openKindCon = liftedTypeKind
610 fix_up (NoteTy _ kind) = fix_up kind
612 -- The basic story is
613 -- typeKind (FunTy arg res) = typeKind res
614 -- But a function is lifted regardless of its result type
615 -- Hence the strange fix-up.
616 -- Note that 'res', being the result of a FunTy, can't have
617 -- a strange kind like (*->*).
619 typeKind (ForAllTy tv ty) = typeKind ty
623 ---------------------------------------------------------------------
624 Free variables of a type
625 ~~~~~~~~~~~~~~~~~~~~~~~~
627 tyVarsOfType :: Type -> TyVarSet
628 tyVarsOfType (TyVarTy tv) = unitVarSet tv
629 tyVarsOfType (TyConApp tycon tys) = tyVarsOfTypes tys
630 tyVarsOfType (NoteTy (FTVNote tvs) ty2) = tvs
631 tyVarsOfType (NoteTy (SynNote ty1) ty2) = tyVarsOfType ty2 -- See note [Syn] below
632 tyVarsOfType (SourceTy sty) = tyVarsOfSourceType sty
633 tyVarsOfType (FunTy arg res) = tyVarsOfType arg `unionVarSet` tyVarsOfType res
634 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionVarSet` tyVarsOfType arg
635 tyVarsOfType (ForAllTy tyvar ty) = tyVarsOfType ty `minusVarSet` unitVarSet tyvar
640 -- What are the free tyvars of (T x)? Empty, of course!
641 -- Here's the example that Ralf Laemmel showed me:
642 -- foo :: (forall a. C u a -> C u a) -> u
643 -- mappend :: Monoid u => u -> u -> u
645 -- bar :: Monoid u => u
646 -- bar = foo (\t -> t `mappend` t)
647 -- We have to generalise at the arg to f, and we don't
648 -- want to capture the constraint (Monad (C u a)) because
649 -- it appears to mention a. Pretty silly, but it was useful to him.
652 tyVarsOfTypes :: [Type] -> TyVarSet
653 tyVarsOfTypes tys = foldr (unionVarSet.tyVarsOfType) emptyVarSet tys
655 tyVarsOfPred :: PredType -> TyVarSet
656 tyVarsOfPred = tyVarsOfSourceType -- Just a subtype
658 tyVarsOfSourceType :: SourceType -> TyVarSet
659 tyVarsOfSourceType (IParam _ ty) = tyVarsOfType ty
660 tyVarsOfSourceType (ClassP _ tys) = tyVarsOfTypes tys
661 tyVarsOfSourceType (NType _ tys) = tyVarsOfTypes tys
663 tyVarsOfTheta :: ThetaType -> TyVarSet
664 tyVarsOfTheta = foldr (unionVarSet . tyVarsOfSourceType) emptyVarSet
666 -- Add a Note with the free tyvars to the top of the type
667 addFreeTyVars :: Type -> Type
668 addFreeTyVars ty@(NoteTy (FTVNote _) _) = ty
669 addFreeTyVars ty = NoteTy (FTVNote (tyVarsOfType ty)) ty
672 %************************************************************************
674 \subsection{TidyType}
676 %************************************************************************
678 tidyTy tidies up a type for printing in an error message, or in
681 It doesn't change the uniques at all, just the print names.
684 tidyTyVarBndr :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
685 tidyTyVarBndr (tidy_env, subst) tyvar
686 = case tidyOccName tidy_env (getOccName name) of
687 (tidy', occ') -> -- New occname reqd
688 ((tidy', subst'), tyvar')
690 subst' = extendVarEnv subst tyvar tyvar'
691 tyvar' = setTyVarName tyvar name'
692 name' = mkInternalName (getUnique name) occ' noSrcLoc
693 -- Note: make a *user* tyvar, so it printes nicely
694 -- Could extract src loc, but no need.
696 name = tyVarName tyvar
698 tidyFreeTyVars :: TidyEnv -> TyVarSet -> TidyEnv
699 -- Add the free tyvars to the env in tidy form,
700 -- so that we can tidy the type they are free in
701 tidyFreeTyVars env tyvars = fst (tidyOpenTyVars env (varSetElems tyvars))
703 tidyOpenTyVars :: TidyEnv -> [TyVar] -> (TidyEnv, [TyVar])
704 tidyOpenTyVars env tyvars = mapAccumL tidyOpenTyVar env tyvars
706 tidyOpenTyVar :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
707 -- Treat a new tyvar as a binder, and give it a fresh tidy name
708 tidyOpenTyVar env@(tidy_env, subst) tyvar
709 = case lookupVarEnv subst tyvar of
710 Just tyvar' -> (env, tyvar') -- Already substituted
711 Nothing -> tidyTyVarBndr env tyvar -- Treat it as a binder
713 tidyType :: TidyEnv -> Type -> Type
714 tidyType env@(tidy_env, subst) ty
717 go (TyVarTy tv) = case lookupVarEnv subst tv of
718 Nothing -> TyVarTy tv
719 Just tv' -> TyVarTy tv'
720 go (TyConApp tycon tys) = let args = map go tys
721 in args `seqList` TyConApp tycon args
722 go (NoteTy note ty) = (NoteTy $! (go_note note)) $! (go ty)
723 go (SourceTy sty) = SourceTy (tidySourceType env sty)
724 go (AppTy fun arg) = (AppTy $! (go fun)) $! (go arg)
725 go (FunTy fun arg) = (FunTy $! (go fun)) $! (go arg)
726 go (ForAllTy tv ty) = ForAllTy tvp $! (tidyType envp ty)
728 (envp, tvp) = tidyTyVarBndr env tv
730 go_note (SynNote ty) = SynNote $! (go ty)
731 go_note note@(FTVNote ftvs) = note -- No need to tidy the free tyvars
733 tidyTypes env tys = map (tidyType env) tys
735 tidyPred :: TidyEnv -> SourceType -> SourceType
736 tidyPred = tidySourceType
738 tidySourceType :: TidyEnv -> SourceType -> SourceType
739 tidySourceType env (IParam n ty) = IParam n (tidyType env ty)
740 tidySourceType env (ClassP clas tys) = ClassP clas (tidyTypes env tys)
741 tidySourceType env (NType tc tys) = NType tc (tidyTypes env tys)
745 @tidyOpenType@ grabs the free type variables, tidies them
746 and then uses @tidyType@ to work over the type itself
749 tidyOpenType :: TidyEnv -> Type -> (TidyEnv, Type)
751 = (env', tidyType env' ty)
753 env' = tidyFreeTyVars env (tyVarsOfType ty)
755 tidyOpenTypes :: TidyEnv -> [Type] -> (TidyEnv, [Type])
756 tidyOpenTypes env tys = mapAccumL tidyOpenType env tys
758 tidyTopType :: Type -> Type
759 tidyTopType ty = tidyType emptyTidyEnv ty
764 %************************************************************************
766 \subsection{Liftedness}
768 %************************************************************************
771 isUnLiftedType :: Type -> Bool
772 -- isUnLiftedType returns True for forall'd unlifted types:
773 -- x :: forall a. Int#
774 -- I found bindings like these were getting floated to the top level.
775 -- They are pretty bogus types, mind you. It would be better never to
778 isUnLiftedType (ForAllTy tv ty) = isUnLiftedType ty
779 isUnLiftedType (NoteTy _ ty) = isUnLiftedType ty
780 isUnLiftedType (TyConApp tc _) = isUnLiftedTyCon tc
781 isUnLiftedType (SourceTy _) = False -- All source types are lifted
782 isUnLiftedType other = False
784 isUnboxedTupleType :: Type -> Bool
785 isUnboxedTupleType ty = case splitTyConApp_maybe ty of
786 Just (tc, ty_args) -> isUnboxedTupleTyCon tc
789 -- Should only be applied to *types*; hence the assert
790 isAlgType :: Type -> Bool
791 isAlgType ty = case splitTyConApp_maybe ty of
792 Just (tc, ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
797 @isStrictType@ computes whether an argument (or let RHS) should
798 be computed strictly or lazily, based only on its type.
799 Works just like isUnLiftedType, except that it has a special case
800 for dictionaries. Since it takes account of ClassP, you might think
801 this function should be in TcType, but isStrictType is used by DataCon,
802 which is below TcType in the hierarchy, so it's convenient to put it here.
805 isStrictType (ForAllTy tv ty) = isStrictType ty
806 isStrictType (NoteTy _ ty) = isStrictType ty
807 isStrictType (TyConApp tc _) = isUnLiftedTyCon tc
808 isStrictType (SourceTy (ClassP clas _)) = opt_DictsStrict && not (isNewTyCon (classTyCon clas))
809 -- We may be strict in dictionary types, but only if it
810 -- has more than one component.
811 -- [Being strict in a single-component dictionary risks
812 -- poking the dictionary component, which is wrong.]
813 isStrictType other = False
817 isPrimitiveType :: Type -> Bool
818 -- Returns types that are opaque to Haskell.
819 -- Most of these are unlifted, but now that we interact with .NET, we
820 -- may have primtive (foreign-imported) types that are lifted
821 isPrimitiveType ty = case splitTyConApp_maybe ty of
822 Just (tc, ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
828 %************************************************************************
830 \subsection{Sequencing on types
832 %************************************************************************
835 seqType :: Type -> ()
836 seqType (TyVarTy tv) = tv `seq` ()
837 seqType (AppTy t1 t2) = seqType t1 `seq` seqType t2
838 seqType (FunTy t1 t2) = seqType t1 `seq` seqType t2
839 seqType (NoteTy note t2) = seqNote note `seq` seqType t2
840 seqType (SourceTy p) = seqPred p
841 seqType (TyConApp tc tys) = tc `seq` seqTypes tys
842 seqType (ForAllTy tv ty) = tv `seq` seqType ty
844 seqTypes :: [Type] -> ()
846 seqTypes (ty:tys) = seqType ty `seq` seqTypes tys
848 seqNote :: TyNote -> ()
849 seqNote (SynNote ty) = seqType ty
850 seqNote (FTVNote set) = sizeUniqSet set `seq` ()
852 seqPred :: SourceType -> ()
853 seqPred (ClassP c tys) = c `seq` seqTypes tys
854 seqPred (NType tc tys) = tc `seq` seqTypes tys
855 seqPred (IParam n ty) = n `seq` seqType ty
859 %************************************************************************
861 \subsection{Equality on types}
863 %************************************************************************
865 Comparison; don't use instances so that we know where it happens.
866 Look through newtypes but not usage types.
868 Note that eqType can respond 'False' for partial applications of newtypes.
870 newtype Parser m a = MkParser (Foogle m a)
873 Monad (Parser m) `eqType` Monad (Foogle m)
875 Well, yes, but eqType won't see that they are the same.
876 I don't think this is harmful, but it's soemthing to watch out for.
879 eqType t1 t2 = eq_ty emptyVarEnv t1 t2
880 eqKind = eqType -- No worries about looking
881 eqUsage = eqType -- through source types for these two
883 -- Look through Notes
884 eq_ty env (NoteTy _ t1) t2 = eq_ty env t1 t2
885 eq_ty env t1 (NoteTy _ t2) = eq_ty env t1 t2
887 -- Look through SourceTy. This is where the looping danger comes from
888 eq_ty env (SourceTy sty1) t2 = eq_ty env (sourceTypeRep sty1) t2
889 eq_ty env t1 (SourceTy sty2) = eq_ty env t1 (sourceTypeRep sty2)
891 -- The rest is plain sailing
892 eq_ty env (TyVarTy tv1) (TyVarTy tv2) = case lookupVarEnv env tv1 of
893 Just tv1a -> tv1a == tv2
894 Nothing -> tv1 == tv2
895 eq_ty env (ForAllTy tv1 t1) (ForAllTy tv2 t2)
896 | tv1 == tv2 = eq_ty (delVarEnv env tv1) t1 t2
897 | otherwise = eq_ty (extendVarEnv env tv1 tv2) t1 t2
898 eq_ty env (AppTy s1 t1) (AppTy s2 t2) = (eq_ty env s1 s2) && (eq_ty env t1 t2)
899 eq_ty env (FunTy s1 t1) (FunTy s2 t2) = (eq_ty env s1 s2) && (eq_ty env t1 t2)
900 eq_ty env (TyConApp tc1 tys1) (TyConApp tc2 tys2) = (tc1 == tc2) && (eq_tys env tys1 tys2)
901 eq_ty env t1 t2 = False
903 eq_tys env [] [] = True
904 eq_tys env (t1:tys1) (t2:tys2) = (eq_ty env t1 t2) && (eq_tys env tys1 tys2)
905 eq_tys env tys1 tys2 = False