2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[Type]{Type - public interface}
8 -- re-exports from TypeRep:
12 superKind, superBoxity, -- :: SuperKind
14 boxedKind, -- :: Kind :: BX
15 anyBoxKind, -- :: Kind :: BX
16 typeCon, -- :: KindCon :: BX -> KX
17 anyBoxCon, -- :: KindCon :: BX
19 boxedTypeKind, unboxedTypeKind, openTypeKind, -- Kind :: superKind
21 mkArrowKind, mkArrowKinds, -- mentioned below: hasMoreBoxityInfo,
25 -- exports from this module:
28 mkTyVarTy, mkTyVarTys, getTyVar, getTyVar_maybe, isTyVarTy,
30 mkAppTy, mkAppTys, splitAppTy, splitAppTys, splitAppTy_maybe,
32 mkFunTy, mkFunTys, splitFunTy, splitFunTy_maybe, splitFunTys, splitFunTysN,
33 funResultTy, funArgTy, zipFunTys,
35 mkTyConApp, mkTyConTy, splitTyConApp_maybe,
36 splitAlgTyConApp_maybe, splitAlgTyConApp,
37 mkDictTy, mkDictTys, mkPredTy, splitPredTy_maybe, splitDictTy_maybe, isDictTy,
39 mkSynTy, isSynTy, deNoteType,
41 repType, splitRepFunTys, splitNewType_maybe, typePrimRep,
43 UsageAnn(..), mkUsgTy, isUsgTy{- dont use -}, isNotUsgTy, splitUsgTy, unUsgTy, tyUsg,
44 mkUsForAllTy, mkUsForAllTys, splitUsForAllTys, substUsTy,
46 mkForAllTy, mkForAllTys, splitForAllTy_maybe, splitForAllTys,
47 applyTy, applyTys, mkPiType, hoistForAllTys,
49 TauType, RhoType, SigmaType, PredType(..), ThetaType,
50 ClassPred, ClassContext, mkClassPred,
51 getClassTys_maybe, ipName_maybe, classesToPreds, classesOfPreds,
52 isTauTy, mkRhoTy, splitRhoTy,
53 mkSigmaTy, isSigmaTy, splitSigmaTy,
56 isUnLiftedType, isUnboxedType, isUnboxedTupleType, isAlgType, isDataType, isNewType,
59 tyVarsOfType, tyVarsOfTypes, tyVarsOfPred, tyVarsOfTheta,
60 namesOfType, typeKind, addFreeTyVars,
62 -- Tidying up for printing
64 tidyOpenType, tidyOpenTypes,
65 tidyTyVar, tidyTyVars,
73 #include "HsVersions.h"
75 -- We import the representation and primitive functions from TypeRep.
76 -- Many things are reexported, but not the representation!
82 import {-# SOURCE #-} DataCon( DataCon, dataConRepType )
83 import {-# SOURCE #-} PprType( pprType, pprPred ) -- Only called in debug messages
84 import {-# SOURCE #-} Subst ( mkTyVarSubst, substTy )
87 import Var ( TyVar, Var, UVar,
88 tyVarKind, tyVarName, setTyVarName, isId, idType,
93 import Name ( Name, NamedThing(..), mkLocalName, tidyOccName
96 import Class ( classTyCon, Class )
98 isUnboxedTupleTyCon, isUnLiftedTyCon,
99 isFunTyCon, isDataTyCon, isNewTyCon, newTyConRep,
100 isAlgTyCon, isSynTyCon, tyConArity,
101 tyConKind, tyConDataCons, getSynTyConDefn,
102 tyConPrimRep, tyConClass_maybe
106 import SrcLoc ( noSrcLoc )
107 import Maybes ( maybeToBool )
108 import PrimRep ( PrimRep(..), isFollowableRep )
109 import Unique ( Uniquable(..) )
110 import Util ( mapAccumL, seqList )
112 import UniqSet ( sizeUniqSet ) -- Should come via VarSet
116 %************************************************************************
118 \subsection{Stuff to do with kinds.}
120 %************************************************************************
123 hasMoreBoxityInfo :: Kind -> Kind -> Bool
124 hasMoreBoxityInfo k1 k2
125 | k2 == openTypeKind = ASSERT( is_type_kind k1) True
126 | otherwise = k1 == k2
128 -- Returns true for things of form (Type x)
129 is_type_kind k = case splitTyConApp_maybe k of
130 Just (tc,[_]) -> tc == typeCon
135 %************************************************************************
137 \subsection{Constructor-specific functions}
139 %************************************************************************
142 ---------------------------------------------------------------------
146 mkTyVarTy :: TyVar -> Type
149 mkTyVarTys :: [TyVar] -> [Type]
150 mkTyVarTys = map mkTyVarTy -- a common use of mkTyVarTy
152 getTyVar :: String -> Type -> TyVar
153 getTyVar msg (TyVarTy tv) = tv
154 getTyVar msg (NoteTy _ t) = getTyVar msg t
155 getTyVar msg other = panic ("getTyVar: " ++ msg)
157 getTyVar_maybe :: Type -> Maybe TyVar
158 getTyVar_maybe (TyVarTy tv) = Just tv
159 getTyVar_maybe (NoteTy _ t) = getTyVar_maybe t
160 getTyVar_maybe other = Nothing
162 isTyVarTy :: Type -> Bool
163 isTyVarTy (TyVarTy tv) = True
164 isTyVarTy (NoteTy _ ty) = isTyVarTy ty
165 isTyVarTy other = False
169 ---------------------------------------------------------------------
172 We need to be pretty careful with AppTy to make sure we obey the
173 invariant that a TyConApp is always visibly so. mkAppTy maintains the
177 mkAppTy orig_ty1 orig_ty2 = ASSERT2( isNotUsgTy orig_ty1 && isNotUsgTy orig_ty2, pprType orig_ty1 <+> text "to" <+> pprType orig_ty2 )
180 mk_app (NoteTy _ ty1) = mk_app ty1
181 mk_app (TyConApp tc tys) = mkTyConApp tc (tys ++ [orig_ty2])
182 mk_app ty1 = AppTy orig_ty1 orig_ty2
184 mkAppTys :: Type -> [Type] -> Type
185 mkAppTys orig_ty1 [] = orig_ty1
186 -- This check for an empty list of type arguments
187 -- avoids the needless of a type synonym constructor.
188 -- For example: mkAppTys Rational []
189 -- returns to (Ratio Integer), which has needlessly lost
190 -- the Rational part.
191 mkAppTys orig_ty1 orig_tys2 = ASSERT2( isNotUsgTy orig_ty1, pprType orig_ty1 )
194 mk_app (NoteTy _ ty1) = mk_app ty1
195 mk_app (TyConApp tc tys) = mkTyConApp tc (tys ++ orig_tys2)
196 mk_app ty1 = ASSERT2( all isNotUsgTy orig_tys2, pprType orig_ty1 <+> text "to" <+> hsep (map pprType orig_tys2) )
197 foldl AppTy orig_ty1 orig_tys2
199 splitAppTy_maybe :: Type -> Maybe (Type, Type)
200 splitAppTy_maybe (FunTy ty1 ty2) = Just (TyConApp funTyCon [ty1], ty2)
201 splitAppTy_maybe (AppTy ty1 ty2) = Just (ty1, ty2)
202 splitAppTy_maybe (NoteTy _ ty) = splitAppTy_maybe ty
203 splitAppTy_maybe (TyConApp tc []) = Nothing
204 splitAppTy_maybe (TyConApp tc tys) = split tys []
206 split [ty2] acc = Just (TyConApp tc (reverse acc), ty2)
207 split (ty:tys) acc = split tys (ty:acc)
209 splitAppTy_maybe other = Nothing
211 splitAppTy :: Type -> (Type, Type)
212 splitAppTy ty = case splitAppTy_maybe ty of
214 Nothing -> panic "splitAppTy"
216 splitAppTys :: Type -> (Type, [Type])
217 splitAppTys ty = split ty ty []
219 split orig_ty (AppTy ty arg) args = split ty ty (arg:args)
220 split orig_ty (NoteTy _ ty) args = split orig_ty ty args
221 split orig_ty (FunTy ty1 ty2) args = ASSERT( null args )
222 (TyConApp funTyCon [], [ty1,ty2])
223 split orig_ty (TyConApp tc tc_args) args = (TyConApp tc [], tc_args ++ args)
224 split orig_ty ty args = (orig_ty, args)
228 ---------------------------------------------------------------------
233 mkFunTy :: Type -> Type -> Type
234 mkFunTy arg res = FunTy arg res
236 mkFunTys :: [Type] -> Type -> Type
237 mkFunTys tys ty = foldr FunTy ty tys
239 splitFunTy :: Type -> (Type, Type)
240 splitFunTy (FunTy arg res) = (arg, res)
241 splitFunTy (NoteTy _ ty) = splitFunTy ty
243 splitFunTy_maybe :: Type -> Maybe (Type, Type)
244 splitFunTy_maybe (FunTy arg res) = Just (arg, res)
245 splitFunTy_maybe (NoteTy (IPNote _) ty) = Nothing
246 splitFunTy_maybe (NoteTy _ ty) = splitFunTy_maybe ty
247 splitFunTy_maybe other = Nothing
249 splitFunTys :: Type -> ([Type], Type)
250 splitFunTys ty = split [] ty ty
252 split args orig_ty (FunTy arg res) = split (arg:args) res res
253 split args orig_ty (NoteTy (IPNote _) ty)
254 = (reverse args, orig_ty)
255 split args orig_ty (NoteTy _ ty) = split args orig_ty ty
256 split args orig_ty ty = (reverse args, orig_ty)
258 splitFunTysN :: String -> Int -> Type -> ([Type], Type)
259 splitFunTysN msg orig_n orig_ty = split orig_n [] orig_ty orig_ty
261 split 0 args syn_ty ty = (reverse args, syn_ty)
262 split n args syn_ty (FunTy arg res) = split (n-1) (arg:args) res res
263 split n args syn_ty (NoteTy _ ty) = split n args syn_ty ty
264 split n args syn_ty ty = pprPanic ("splitFunTysN: " ++ msg) (int orig_n <+> pprType orig_ty)
266 zipFunTys :: Outputable a => [a] -> Type -> ([(a,Type)], Type)
267 zipFunTys orig_xs orig_ty = split [] orig_xs orig_ty orig_ty
269 split acc [] nty ty = (reverse acc, nty)
270 split acc (x:xs) nty (FunTy arg res) = split ((x,arg):acc) xs res res
271 split acc xs nty (NoteTy _ ty) = split acc xs nty ty
272 split acc (x:xs) nty ty = pprPanic "zipFunTys" (ppr orig_xs <+> pprType orig_ty)
274 funResultTy :: Type -> Type
275 funResultTy (FunTy arg res) = res
276 funResultTy (NoteTy _ ty) = funResultTy ty
277 funResultTy ty = pprPanic "funResultTy" (pprType ty)
279 funArgTy :: Type -> Type
280 funArgTy (FunTy arg res) = arg
281 funArgTy (NoteTy _ ty) = funArgTy ty
282 funArgTy ty = pprPanic "funArgTy" (pprType ty)
286 ---------------------------------------------------------------------
291 mkTyConApp :: TyCon -> [Type] -> Type
293 | isFunTyCon tycon && length tys == 2
295 (ty1:ty2:_) -> FunTy ty1 ty2
298 = ASSERT(not (isSynTyCon tycon))
301 mkTyConTy :: TyCon -> Type
302 mkTyConTy tycon = ASSERT( not (isSynTyCon tycon) )
305 -- splitTyConApp "looks through" synonyms, because they don't
306 -- mean a distinct type, but all other type-constructor applications
307 -- including functions are returned as Just ..
309 splitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
310 splitTyConApp_maybe (TyConApp tc tys) = Just (tc, tys)
311 splitTyConApp_maybe (FunTy arg res) = Just (funTyCon, [arg,res])
312 splitTyConApp_maybe (NoteTy (IPNote _) ty) = Nothing
313 splitTyConApp_maybe (NoteTy _ ty) = splitTyConApp_maybe ty
314 splitTyConApp_maybe other = Nothing
316 -- splitAlgTyConApp_maybe looks for
317 -- *saturated* applications of *algebraic* data types
318 -- "Algebraic" => newtype, data type, or dictionary (not function types)
319 -- We return the constructors too.
321 splitAlgTyConApp_maybe :: Type -> Maybe (TyCon, [Type], [DataCon])
322 splitAlgTyConApp_maybe (TyConApp tc tys)
324 tyConArity tc == length tys = Just (tc, tys, tyConDataCons tc)
325 splitAlgTyConApp_maybe (NoteTy (IPNote _) ty)
327 splitAlgTyConApp_maybe (NoteTy _ ty) = splitAlgTyConApp_maybe ty
328 splitAlgTyConApp_maybe other = Nothing
330 splitAlgTyConApp :: Type -> (TyCon, [Type], [DataCon])
331 -- Here the "algebraic" property is an *assertion*
332 splitAlgTyConApp (TyConApp tc tys) = ASSERT( isAlgTyCon tc && tyConArity tc == length tys )
333 (tc, tys, tyConDataCons tc)
334 splitAlgTyConApp (NoteTy _ ty) = splitAlgTyConApp ty
337 "Dictionary" types are just ordinary data types, but you can
338 tell from the type constructor whether it's a dictionary or not.
341 mkDictTy :: Class -> [Type] -> Type
342 mkDictTy clas tys = TyConApp (classTyCon clas) tys
344 mkDictTys :: ClassContext -> [Type]
345 mkDictTys cxt = [mkDictTy cls tys | (cls,tys) <- cxt]
347 mkPredTy :: PredType -> Type
348 mkPredTy (Class clas tys) = TyConApp (classTyCon clas) tys
349 mkPredTy (IParam n ty) = NoteTy (IPNote n) ty
351 splitPredTy_maybe :: Type -> Maybe PredType
352 splitPredTy_maybe (TyConApp tc tys)
353 | maybeToBool maybe_class
354 && tyConArity tc == length tys = Just (Class clas tys)
356 maybe_class = tyConClass_maybe tc
357 Just clas = maybe_class
359 splitPredTy_maybe (NoteTy (IPNote n) ty)
361 splitPredTy_maybe (NoteTy _ ty) = splitPredTy_maybe ty
362 splitPredTy_maybe other = Nothing
364 splitDictTy_maybe :: Type -> Maybe (Class, [Type])
366 = case splitPredTy_maybe ty of
367 Just p -> getClassTys_maybe p
370 isDictTy :: Type -> Bool
371 -- This version is slightly more efficient than (maybeToBool . splitDictTy)
372 isDictTy (TyConApp tc tys)
373 | maybeToBool (tyConClass_maybe tc)
374 && tyConArity tc == length tys
376 isDictTy (NoteTy _ ty) = isDictTy ty
377 isDictTy other = False
380 ---------------------------------------------------------------------
385 mkSynTy syn_tycon tys
386 = ASSERT( isSynTyCon syn_tycon )
387 ASSERT( isNotUsgTy body )
388 ASSERT( length tyvars == length tys )
389 NoteTy (SynNote (TyConApp syn_tycon tys))
390 (substTy (mkTyVarSubst tyvars tys) body)
392 (tyvars, body) = getSynTyConDefn syn_tycon
394 isSynTy (NoteTy (SynNote _) _) = True
395 isSynTy other = False
397 deNoteType :: Type -> Type
398 -- Sorry for the cute name
399 deNoteType ty@(TyVarTy tyvar) = ty
400 deNoteType (TyConApp tycon tys) = TyConApp tycon (map deNoteType tys)
401 deNoteType (NoteTy _ ty) = deNoteType ty
402 deNoteType (AppTy fun arg) = AppTy (deNoteType fun) (deNoteType arg)
403 deNoteType (FunTy fun arg) = FunTy (deNoteType fun) (deNoteType arg)
404 deNoteType (ForAllTy tv ty) = ForAllTy tv (deNoteType ty)
407 Notes on type synonyms
408 ~~~~~~~~~~~~~~~~~~~~~~
409 The various "split" functions (splitFunTy, splitRhoTy, splitForAllTy) try
410 to return type synonyms whereever possible. Thus
415 splitFunTys (a -> Foo a) = ([a], Foo a)
418 The reason is that we then get better (shorter) type signatures in
419 interfaces. Notably this plays a role in tcTySigs in TcBinds.lhs.
425 repType looks through
429 It's useful in the back end where we're not
430 interested in newtypes anymore.
433 repType :: Type -> Type
434 repType (ForAllTy _ ty) = repType ty
435 repType (NoteTy _ ty) = repType ty
436 repType ty = case splitNewType_maybe ty of
437 Just ty' -> repType ty' -- Still re-apply repType in case of for-all
440 splitRepFunTys :: Type -> ([Type], Type)
441 -- Like splitFunTys, but looks through newtypes and for-alls
442 splitRepFunTys ty = split [] (repType ty)
444 split args (FunTy arg res) = split (arg:args) (repType res)
445 split args ty = (reverse args, ty)
447 typePrimRep :: Type -> PrimRep
448 typePrimRep ty = case repType ty of
449 TyConApp tc _ -> tyConPrimRep tc
451 AppTy _ _ -> PtrRep -- ??
454 splitNewType_maybe :: Type -> Maybe Type
455 -- Find the representation of a newtype, if it is one
456 -- Looks through multiple levels of newtype, but does not look through for-alls
457 splitNewType_maybe (NoteTy (IPNote _) ty)
459 splitNewType_maybe (NoteTy _ ty) = splitNewType_maybe ty
460 splitNewType_maybe (TyConApp tc tys) = case newTyConRep tc of
461 Just rep_ty -> ASSERT( length tys == tyConArity tc )
462 -- The assert should hold because repType should
463 -- only be applied to *types* (of kind *)
464 Just (applyTys rep_ty tys)
466 splitNewType_maybe other = Nothing
471 ---------------------------------------------------------------------
475 NB: Invariant: if present, usage note is at the very top of the type.
476 This should be carefully preserved.
478 In some parts of the compiler, comments use the _Once Upon a
479 Polymorphic Type_ (POPL'99) usage of "rho = generalised
480 usage-annotated type; sigma = usage-annotated type; tau =
481 usage-annotated type except on top"; unfortunately this conflicts with
482 the rho/tau/theta/sigma usage in the rest of the compiler. (KSW
486 mkUsgTy :: UsageAnn -> Type -> Type
488 mkUsgTy UsMany ty = ASSERT2( isNotUsgTy ty, pprType ty )
491 mkUsgTy usg ty = ASSERT2( isNotUsgTy ty, pprType ty )
492 NoteTy (UsgNote usg) ty
494 -- The isUsgTy function is utterly useless if UsManys are omitted.
495 -- Be warned! KSW 1999-04.
496 isUsgTy :: Type -> Bool
500 isUsgTy (NoteTy (UsgForAll _) ty) = isUsgTy ty
501 isUsgTy (NoteTy (UsgNote _) _ ) = True
502 isUsgTy other = False
505 -- The isNotUsgTy function may return a false True if UsManys are omitted;
506 -- in other words, A SSERT( isNotUsgTy ty ) may be useful but
507 -- A SSERT( not (isNotUsg ty) ) is asking for trouble. KSW 1999-04.
508 isNotUsgTy :: Type -> Bool
509 isNotUsgTy (NoteTy (UsgForAll _) _) = False
510 isNotUsgTy (NoteTy (UsgNote _) _) = False
511 isNotUsgTy other = True
513 -- splitUsgTy_maybe is not exported, since it is meaningless if
514 -- UsManys are omitted. It is used in several places in this module,
515 -- however. KSW 1999-04.
516 splitUsgTy_maybe :: Type -> Maybe (UsageAnn,Type)
517 splitUsgTy_maybe (NoteTy (UsgNote usg) ty2) = ASSERT( isNotUsgTy ty2 )
519 splitUsgTy_maybe ty@(NoteTy (UsgForAll _) _) = pprPanic "splitUsgTy_maybe:" $ pprType ty
520 splitUsgTy_maybe ty = Nothing
522 splitUsgTy :: Type -> (UsageAnn,Type)
523 splitUsgTy ty = case splitUsgTy_maybe ty of
529 pprPanic "splitUsgTy: no usage annot:" $ pprType ty
532 tyUsg :: Type -> UsageAnn
533 tyUsg = fst . splitUsgTy
535 unUsgTy :: Type -> Type
536 -- strip outer usage annotation if present
537 unUsgTy ty = case splitUsgTy_maybe ty of
538 Just (_,ty1) -> ASSERT2( isNotUsgTy ty1, pprType ty )
542 mkUsForAllTy :: UVar -> Type -> Type
543 mkUsForAllTy uv ty = NoteTy (UsgForAll uv) ty
545 mkUsForAllTys :: [UVar] -> Type -> Type
546 mkUsForAllTys uvs ty = foldr (NoteTy . UsgForAll) ty uvs
548 splitUsForAllTys :: Type -> ([UVar],Type)
549 splitUsForAllTys ty = split ty []
550 where split (NoteTy (UsgForAll u) ty) uvs = split ty (u:uvs)
551 split other_ty uvs = (reverse uvs, other_ty)
553 substUsTy :: VarEnv UsageAnn -> Type -> Type
554 -- assumes range is fresh uvars, so no conflicts
555 substUsTy ve (NoteTy note@(UsgNote (UsVar u))
556 ty ) = NoteTy (case lookupVarEnv ve u of
557 Just ua -> UsgNote ua
560 substUsTy ve (NoteTy note@(UsgNote _) ty ) = NoteTy note (substUsTy ve ty)
561 substUsTy ve (NoteTy note@(UsgForAll _) ty ) = NoteTy note (substUsTy ve ty)
562 substUsTy ve (NoteTy (SynNote ty1) ty2) = NoteTy (SynNote (substUsTy ve ty1))
564 substUsTy ve (NoteTy note@(FTVNote _) ty ) = NoteTy note (substUsTy ve ty)
565 substUsTy ve ty@(TyVarTy _ ) = ty
566 substUsTy ve (AppTy ty1 ty2) = AppTy (substUsTy ve ty1)
568 substUsTy ve (FunTy ty1 ty2) = FunTy (substUsTy ve ty1)
570 substUsTy ve (TyConApp tyc tys) = TyConApp tyc (map (substUsTy ve) tys)
571 substUsTy ve (ForAllTy yv ty ) = ForAllTy yv (substUsTy ve ty)
575 ---------------------------------------------------------------------
579 We need to be clever here with usage annotations; they need to be
580 lifted or lowered through the forall as appropriate.
583 mkForAllTy :: TyVar -> Type -> Type
584 mkForAllTy tyvar ty = case splitUsgTy_maybe ty of
585 Just (usg,ty') -> NoteTy (UsgNote usg)
587 Nothing -> ForAllTy tyvar ty
589 mkForAllTys :: [TyVar] -> Type -> Type
590 mkForAllTys tyvars ty = case splitUsgTy_maybe ty of
591 Just (usg,ty') -> NoteTy (UsgNote usg)
592 (foldr ForAllTy ty' tyvars)
593 Nothing -> foldr ForAllTy ty tyvars
595 splitForAllTy_maybe :: Type -> Maybe (TyVar, Type)
596 splitForAllTy_maybe ty = case splitUsgTy_maybe ty of
597 Just (usg,ty') -> do (tyvar,ty'') <- splitFAT_m ty'
598 return (tyvar, NoteTy (UsgNote usg) ty'')
599 Nothing -> splitFAT_m ty
601 splitFAT_m (NoteTy (IPNote _) ty) = Nothing
602 splitFAT_m (NoteTy _ ty) = splitFAT_m ty
603 splitFAT_m (ForAllTy tyvar ty) = Just(tyvar, ty)
604 splitFAT_m _ = Nothing
606 splitForAllTys :: Type -> ([TyVar], Type)
607 splitForAllTys ty = case splitUsgTy_maybe ty of
608 Just (usg,ty') -> let (tvs,ty'') = split ty' ty' []
609 in (tvs, NoteTy (UsgNote usg) ty'')
610 Nothing -> split ty ty []
612 split orig_ty (ForAllTy tv ty) tvs = split ty ty (tv:tvs)
613 split orig_ty (NoteTy (IPNote _) ty) tvs = (reverse tvs, orig_ty)
614 split orig_ty (NoteTy _ ty) tvs = split orig_ty ty tvs
615 split orig_ty t tvs = (reverse tvs, orig_ty)
618 @mkPiType@ makes a (->) type or a forall type, depending on whether
619 it is given a type variable or a term variable.
622 mkPiType :: Var -> Type -> Type -- The more polymorphic version doesn't work...
623 mkPiType v ty | isId v = mkFunTy (idType v) ty
624 | otherwise = mkForAllTy v ty
627 Applying a for-all to its arguments
630 applyTy :: Type -> Type -> Type
631 applyTy (NoteTy note@(UsgNote _) fun) arg = NoteTy note (applyTy fun arg)
632 applyTy (NoteTy note@(UsgForAll _) fun) arg = NoteTy note (applyTy fun arg)
633 applyTy (NoteTy _ fun) arg = applyTy fun arg
634 applyTy (ForAllTy tv ty) arg = ASSERT( isNotUsgTy arg )
635 substTy (mkTyVarSubst [tv] [arg]) ty
636 applyTy other arg = panic "applyTy"
638 applyTys :: Type -> [Type] -> Type
639 applyTys fun_ty arg_tys
640 = substTy (mkTyVarSubst tvs arg_tys) ty
642 (tvs, ty) = split fun_ty arg_tys
644 split fun_ty [] = ([], fun_ty)
645 split (NoteTy note@(UsgNote _) fun_ty)
646 args = case split fun_ty args of
647 (tvs, ty) -> (tvs, NoteTy note ty)
648 split (NoteTy note@(UsgForAll _) fun_ty)
649 args = case split fun_ty args of
650 (tvs, ty) -> (tvs, NoteTy note ty)
651 split (NoteTy _ fun_ty) args = split fun_ty args
652 split (ForAllTy tv fun_ty) (arg:args) = ASSERT2( isNotUsgTy arg, vcat (map pprType arg_tys) $$
653 text "in application of" <+> pprType fun_ty)
654 case split fun_ty args of
655 (tvs, ty) -> (tv:tvs, ty)
656 split other_ty args = panic "applyTys"
659 Note that we allow applications to be of usage-annotated- types, as an
660 extension: we handle them by lifting the annotation outside. The
661 argument, however, must still be unannotated.
664 hoistForAllTys :: Type -> Type
665 -- Move all the foralls to the top
666 -- e.g. T -> forall a. a ==> forall a. T -> a
668 = case hoist ty of { (tvs, body) -> mkForAllTys tvs body }
670 hoist :: Type -> ([TyVar], Type)
671 hoist ty = case splitFunTys ty of { (args, res) ->
672 case splitForAllTys res of {
673 ([], body) -> ([], ty) ;
674 (tvs1, body1) -> case hoist body1 of { (tvs2,body2) ->
675 (tvs1 ++ tvs2, mkFunTys args body2)
680 %************************************************************************
682 \subsection{Stuff to do with the source-language types}
684 PredType and ThetaType are used in types for expressions and bindings.
685 ClassPred and ClassContext are used in class and instance declarations.
687 %************************************************************************
692 data PredType = Class Class [Type]
694 type ThetaType = [PredType]
695 type ClassPred = (Class, [Type])
696 type ClassContext = [ClassPred]
697 type SigmaType = Type
701 instance Outputable PredType where
706 mkClassPred clas tys = Class clas tys
708 getClassTys_maybe :: PredType -> Maybe ClassPred
709 getClassTys_maybe (Class clas tys) = Just (clas, tys)
710 getClassTys_maybe _ = Nothing
712 ipName_maybe :: PredType -> Maybe Name
713 ipName_maybe (IParam n _) = Just n
714 ipName_maybe _ = Nothing
716 classesToPreds cts = map (uncurry Class) cts
718 classesOfPreds theta = concatMap cvt theta
719 where cvt (Class clas tys) = [(clas, tys)]
720 cvt (IParam _ _ ) = []
723 @isTauTy@ tests for nested for-alls.
726 isTauTy :: Type -> Bool
727 isTauTy (TyVarTy v) = True
728 isTauTy (TyConApp _ tys) = all isTauTy tys
729 isTauTy (AppTy a b) = isTauTy a && isTauTy b
730 isTauTy (FunTy a b) = isTauTy a && isTauTy b
731 isTauTy (NoteTy (IPNote _) ty) = False
732 isTauTy (NoteTy _ ty) = isTauTy ty
733 isTauTy other = False
737 mkRhoTy :: [PredType] -> Type -> Type
738 mkRhoTy theta ty = foldr (\p r -> FunTy (mkPredTy p) r) ty theta
740 splitRhoTy :: Type -> ([PredType], Type)
741 splitRhoTy ty = split ty ty []
743 split orig_ty (FunTy arg res) ts = case splitPredTy_maybe arg of
744 Just p -> split res res (p:ts)
745 Nothing -> (reverse ts, orig_ty)
746 split orig_ty (NoteTy (IPNote _) ty) ts = (reverse ts, orig_ty)
747 split orig_ty (NoteTy _ ty) ts = split orig_ty ty ts
748 split orig_ty ty ts = (reverse ts, orig_ty)
754 mkSigmaTy tyvars theta tau = mkForAllTys tyvars (mkRhoTy theta tau)
756 isSigmaTy :: Type -> Bool
757 isSigmaTy (FunTy a b) = isPredTy a
758 where isPredTy (NoteTy (IPNote _) _) = True
759 -- JRL could be a dict ty, but that would be polymorphic,
760 -- and thus there would have been an outer ForAllTy
762 isSigmaTy (NoteTy (IPNote _) _) = False
763 isSigmaTy (NoteTy _ ty) = isSigmaTy ty
764 isSigmaTy (ForAllTy tyvar ty) = True
767 splitSigmaTy :: Type -> ([TyVar], [PredType], Type)
771 (tyvars,rho) = splitForAllTys ty
772 (theta,tau) = splitRhoTy rho
776 %************************************************************************
778 \subsection{Kinds and free variables}
780 %************************************************************************
782 ---------------------------------------------------------------------
783 Finding the kind of a type
784 ~~~~~~~~~~~~~~~~~~~~~~~~~~
786 typeKind :: Type -> Kind
788 typeKind (TyVarTy tyvar) = tyVarKind tyvar
789 typeKind (TyConApp tycon tys) = foldr (\_ k -> funResultTy k) (tyConKind tycon) tys
790 typeKind (NoteTy _ ty) = typeKind ty
791 typeKind (AppTy fun arg) = funResultTy (typeKind fun)
793 typeKind (FunTy arg res) = boxedTypeKind -- A function is boxed regardless of its result type
794 -- No functions at the type level, hence we don't need
795 -- to say (typeKind res).
797 typeKind (ForAllTy tv ty) = typeKind ty
801 ---------------------------------------------------------------------
802 Free variables of a type
803 ~~~~~~~~~~~~~~~~~~~~~~~~
805 tyVarsOfType :: Type -> TyVarSet
807 tyVarsOfType (TyVarTy tv) = unitVarSet tv
808 tyVarsOfType (TyConApp tycon tys) = tyVarsOfTypes tys
809 tyVarsOfType (NoteTy (FTVNote tvs) ty2) = tvs
810 tyVarsOfType (NoteTy (SynNote ty1) ty2) = tyVarsOfType ty1
811 tyVarsOfType (NoteTy (UsgNote _) ty) = tyVarsOfType ty
812 tyVarsOfType (NoteTy (UsgForAll _) ty) = tyVarsOfType ty
813 tyVarsOfType (NoteTy (IPNote _) ty) = tyVarsOfType ty
814 tyVarsOfType (FunTy arg res) = tyVarsOfType arg `unionVarSet` tyVarsOfType res
815 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionVarSet` tyVarsOfType arg
816 tyVarsOfType (ForAllTy tyvar ty) = tyVarsOfType ty `minusVarSet` unitVarSet tyvar
818 tyVarsOfTypes :: [Type] -> TyVarSet
819 tyVarsOfTypes tys = foldr (unionVarSet.tyVarsOfType) emptyVarSet tys
821 tyVarsOfPred :: PredType -> TyVarSet
822 tyVarsOfPred (Class clas tys) = tyVarsOfTypes tys
823 tyVarsOfPred (IParam n ty) = tyVarsOfType ty
825 tyVarsOfTheta :: ThetaType -> TyVarSet
826 tyVarsOfTheta = foldr (unionVarSet . tyVarsOfPred) emptyVarSet
828 -- Add a Note with the free tyvars to the top of the type
829 -- (but under a usage if there is one)
830 addFreeTyVars :: Type -> Type
831 addFreeTyVars (NoteTy note@(UsgNote _) ty) = NoteTy note (addFreeTyVars ty)
832 addFreeTyVars (NoteTy note@(UsgForAll _) ty) = NoteTy note (addFreeTyVars ty)
833 addFreeTyVars ty@(NoteTy (FTVNote _) _) = ty
834 addFreeTyVars ty = NoteTy (FTVNote (tyVarsOfType ty)) ty
836 -- Find the free names of a type, including the type constructors and classes it mentions
837 namesOfType :: Type -> NameSet
838 namesOfType (TyVarTy tv) = unitNameSet (getName tv)
839 namesOfType (TyConApp tycon tys) = unitNameSet (getName tycon) `unionNameSets`
841 namesOfType (NoteTy (SynNote ty1) ty2) = namesOfType ty1
842 namesOfType (NoteTy other_note ty2) = namesOfType ty2
843 namesOfType (FunTy arg res) = namesOfType arg `unionNameSets` namesOfType res
844 namesOfType (AppTy fun arg) = namesOfType fun `unionNameSets` namesOfType arg
845 namesOfType (ForAllTy tyvar ty) = namesOfType ty `minusNameSet` unitNameSet (getName tyvar)
847 namesOfTypes tys = foldr (unionNameSets . namesOfType) emptyNameSet tys
851 %************************************************************************
853 \subsection{TidyType}
855 %************************************************************************
857 tidyTy tidies up a type for printing in an error message, or in
860 It doesn't change the uniques at all, just the print names.
863 tidyTyVar :: TidyEnv -> TyVar -> (TidyEnv, TyVar)
864 tidyTyVar env@(tidy_env, subst) tyvar
865 = case lookupVarEnv subst tyvar of
867 Just tyvar' -> -- Already substituted
870 Nothing -> -- Make a new nice name for it
872 case tidyOccName tidy_env (getOccName name) of
873 (tidy', occ') -> -- New occname reqd
874 ((tidy', subst'), tyvar')
876 subst' = extendVarEnv subst tyvar tyvar'
877 tyvar' = setTyVarName tyvar name'
878 name' = mkLocalName (getUnique name) occ' noSrcLoc
879 -- Note: make a *user* tyvar, so it printes nicely
880 -- Could extract src loc, but no need.
882 name = tyVarName tyvar
884 tidyTyVars env tyvars = mapAccumL tidyTyVar env tyvars
886 tidyType :: TidyEnv -> Type -> Type
887 tidyType env@(tidy_env, subst) ty
890 go (TyVarTy tv) = case lookupVarEnv subst tv of
891 Nothing -> TyVarTy tv
892 Just tv' -> TyVarTy tv'
893 go (TyConApp tycon tys) = let args = map go tys
894 in args `seqList` TyConApp tycon args
895 go (NoteTy note ty) = (NoteTy SAPPLY (go_note note)) SAPPLY (go ty)
896 go (AppTy fun arg) = (AppTy SAPPLY (go fun)) SAPPLY (go arg)
897 go (FunTy fun arg) = (FunTy SAPPLY (go fun)) SAPPLY (go arg)
898 go (ForAllTy tv ty) = ForAllTy tvp SAPPLY (tidyType envp ty)
900 (envp, tvp) = tidyTyVar env tv
902 go_note (SynNote ty) = SynNote SAPPLY (go ty)
903 go_note note@(FTVNote ftvs) = note -- No need to tidy the free tyvars
904 go_note note@(UsgNote _) = note -- Usage annotation is already tidy
905 go_note note@(UsgForAll _) = note -- Uvar binder is already tidy
906 go_note (IPNote n) = IPNote (tidyIPName n)
908 tidyTypes env tys = map (tidyType env) tys
912 @tidyOpenType@ grabs the free type variables, tidies them
913 and then uses @tidyType@ to work over the type itself
916 tidyOpenType :: TidyEnv -> Type -> (TidyEnv, Type)
918 = (env', tidyType env' ty)
920 env' = foldl go env (varSetElems (tyVarsOfType ty))
921 go env tyvar = fst (tidyTyVar env tyvar)
923 tidyOpenTypes :: TidyEnv -> [Type] -> (TidyEnv, [Type])
924 tidyOpenTypes env tys = mapAccumL tidyOpenType env tys
926 tidyTopType :: Type -> Type
927 tidyTopType ty = tidyType emptyTidyEnv ty
931 tidyIPName :: Name -> Name
933 = mkLocalName (getUnique name) (getOccName name) noSrcLoc
937 %************************************************************************
939 \subsection{Boxedness and liftedness}
941 %************************************************************************
944 isUnboxedType :: Type -> Bool
945 isUnboxedType ty = not (isFollowableRep (typePrimRep ty))
947 isUnLiftedType :: Type -> Bool
948 -- isUnLiftedType returns True for forall'd unlifted types:
949 -- x :: forall a. Int#
950 -- I found bindings like these were getting floated to the top level.
951 -- They are pretty bogus types, mind you. It would be better never to
954 isUnLiftedType (ForAllTy tv ty) = isUnLiftedType ty
955 isUnLiftedType (NoteTy _ ty) = isUnLiftedType ty
956 isUnLiftedType (TyConApp tc _) = isUnLiftedTyCon tc
957 isUnLiftedType other = False
959 isUnboxedTupleType :: Type -> Bool
960 isUnboxedTupleType ty = case splitTyConApp_maybe ty of
961 Just (tc, ty_args) -> isUnboxedTupleTyCon tc
964 -- Should only be applied to *types*; hence the assert
965 isAlgType :: Type -> Bool
966 isAlgType ty = case splitTyConApp_maybe ty of
967 Just (tc, ty_args) -> ASSERT( length ty_args == tyConArity tc )
971 -- Should only be applied to *types*; hence the assert
972 isDataType :: Type -> Bool
973 isDataType ty = case splitTyConApp_maybe ty of
974 Just (tc, ty_args) -> ASSERT( length ty_args == tyConArity tc )
978 isNewType :: Type -> Bool
979 isNewType ty = case splitTyConApp_maybe ty of
980 Just (tc, ty_args) -> ASSERT( length ty_args == tyConArity tc )
986 %************************************************************************
988 \subsection{Sequencing on types
990 %************************************************************************
993 seqType :: Type -> ()
994 seqType (TyVarTy tv) = tv `seq` ()
995 seqType (AppTy t1 t2) = seqType t1 `seq` seqType t2
996 seqType (FunTy t1 t2) = seqType t1 `seq` seqType t2
997 seqType (NoteTy note t2) = seqNote note `seq` seqType t2
998 seqType (TyConApp tc tys) = tc `seq` seqTypes tys
999 seqType (ForAllTy tv ty) = tv `seq` seqType ty
1001 seqTypes :: [Type] -> ()
1003 seqTypes (ty:tys) = seqType ty `seq` seqTypes tys
1005 seqNote :: TyNote -> ()
1006 seqNote (SynNote ty) = seqType ty
1007 seqNote (FTVNote set) = sizeUniqSet set `seq` ()
1008 seqNote (UsgNote usg) = usg `seq` ()
1009 seqNote (IPNote nm) = nm `seq` ()