2 #include "HsVersions.h"
5 GenType(..), SYN_IE(Type), SYN_IE(TauType),
7 getTyVar, getTyVar_maybe, isTyVarTy,
8 mkAppTy, mkAppTys, splitAppTy, splitAppTys,
10 splitFunTy, splitFunTyExpandingDicts, splitFunTyExpandingDictsAndPeeking,
11 getFunTy_maybe, getFunTyExpandingDicts_maybe,
12 mkTyConTy, getTyCon_maybe, applyTyCon,
14 mkForAllTy, mkForAllTys, getForAllTy_maybe, getForAllTyExpandingDicts_maybe,
15 splitForAllTy, splitForAllTyExpandingDicts,
16 mkForAllUsageTy, getForAllUsageTy,
17 applyTy, specialiseTy,
19 expandTy, -- only let out for debugging (ToDo: rm?)
21 isPrimType, isUnboxedType, typePrimRep,
23 SYN_IE(RhoType), SYN_IE(SigmaType), SYN_IE(ThetaType),
25 mkRhoTy, splitRhoTy, mkTheta, isDictTy,
26 mkSigmaTy, splitSigmaTy,
28 maybeAppTyCon, getAppTyCon,
29 maybeAppDataTyCon, getAppDataTyCon, getAppSpecDataTyCon,
30 maybeAppDataTyConExpandingDicts, maybeAppSpecDataTyConExpandingDicts,
31 getAppDataTyConExpandingDicts, getAppSpecDataTyConExpandingDicts,
34 matchTy, matchTys, eqTy, eqSimpleTy, eqSimpleTheta,
36 instantiateTy, instantiateTauTy, instantiateUsage,
41 tyVarsOfType, tyVarsOfTypes, namesOfType, typeKind,
46 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
47 IMPORT_DELOOPER(IdLoop) -- for paranoia checking
48 IMPORT_DELOOPER(TyLoop)
49 --IMPORT_DELOOPER(PrelLoop) -- for paranoia checking
51 import {-# SOURCE #-} Id ( Id, dataConArgTys )
52 import {-# SOURCE #-} TysPrim ( voidTy )
53 import {-# SOURCE #-} TysWiredIn ( tupleTyCon )
57 import Class ( classDictArgTys, GenClass{-instances-}, SYN_IE(Class) )
58 import Kind ( mkBoxedTypeKind, resultKind, notArrowKind, Kind )
59 import TyCon ( mkFunTyCon, isFunTyCon, isEnumerationTyCon, isTupleTyCon, maybeTyConSingleCon,
60 isPrimTyCon, isAlgTyCon, isDataTyCon, isSynTyCon, maybeNewTyCon, isNewTyCon,
61 tyConKind, tyConDataCons, getSynTyConDefn, TyCon )
62 import TyVar ( tyVarKind, GenTyVar{-instances-}, SYN_IE(GenTyVarSet),
63 emptyTyVarSet, unionTyVarSets, minusTyVarSet,
64 unitTyVarSet, nullTyVarEnv, lookupTyVarEnv, delFromTyVarEnv,
65 addOneToTyVarEnv, SYN_IE(TyVarEnv), SYN_IE(TyVar) )
66 import Usage ( usageOmega, GenUsage, SYN_IE(Usage), SYN_IE(UVar), SYN_IE(UVarEnv),
67 nullUVarEnv, addOneToUVarEnv, lookupUVarEnv, eqUVar,
70 import Name ( NamedThing(..),
71 NameSet(..), unionNameSets, emptyNameSet, unitNameSet, minusNameSet
75 import Maybes ( maybeToBool, assocMaybe )
76 import PrimRep ( PrimRep(..) )
77 import Unique -- quite a few *Keys
78 import Util ( thenCmp, zipEqual, assoc,
79 panic, panic#, assertPanic, pprPanic,
88 -- PprType --(pprType )
96 type Type = GenType TyVar UVar -- Used after typechecker
98 data GenType tyvar uvar -- Parameterised over type and usage variables
105 | TyConTy -- Constants of a specified kind
106 TyCon -- Must *not* be a SynTyCon
107 (GenUsage uvar) -- Usage gives uvar of the full application,
108 -- iff the full application is of kind Type
109 -- c.f. the Usage field in TyVars
111 | SynTy -- Synonyms must be saturated, and contain their expansion
112 TyCon -- Must be a SynTyCon
114 (GenType tyvar uvar) -- Expansion!
118 (GenType tyvar uvar) -- TypeKind
121 uvar -- Quantify over this
122 [uvar] -- Bounds; the quantified var must be
123 -- less than or equal to all these
126 -- Two special cases that save a *lot* of administrative
129 | FunTy -- BoxedTypeKind
130 (GenType tyvar uvar) -- Both args are of TypeKind
136 (GenType tyvar uvar) -- Arg has kind TypeKind
143 type ThetaType = [(Class, Type)]
144 type SigmaType = Type
148 Notes on type synonyms
149 ~~~~~~~~~~~~~~~~~~~~~~
150 The various "split" functions (splitFunTy, splitRhoTy, splitForAllTy) try
151 to return type synonyms whereever possible. Thus
156 splitFunTys (a -> Foo a) = ([a], Foo a)
159 The reason is that we then get better (shorter) type signatures in
160 interfaces. Notably this plays a role in tcTySigs in TcBinds.lhs.
165 Removes just the top level of any abbreviations.
168 expandTy :: Type -> Type -- Restricted to Type due to Dict expansion
170 expandTy (FunTy t1 t2 u) = AppTy (AppTy (TyConTy mkFunTyCon u) t1) t2
171 expandTy (SynTy _ _ t) = expandTy t
172 expandTy (DictTy clas ty u)
173 = case all_arg_tys of
175 [] -> voidTy -- Empty dictionary represented by Void
177 [arg_ty] -> expandTy arg_ty -- just the <whatever> itself
179 -- The extra expandTy is to make sure that
180 -- the result isn't still a dict, which it might be
181 -- if the original guy was a dict with one superdict and
184 other -> ASSERT(not (null all_arg_tys))
185 foldl AppTy (TyConTy (tupleTyCon (length all_arg_tys)) u) all_arg_tys
188 -- Note: length of all_arg_tys can be 0 if the class is
189 -- CCallable, CReturnable (and anything else
190 -- *really weird* that the user writes).
192 all_arg_tys = classDictArgTys clas ty
197 Simple construction and analysis functions
198 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200 mkTyVarTy :: t -> GenType t u
201 mkTyVarTys :: [t] -> [GenType t y]
203 mkTyVarTys = map mkTyVarTy -- a common use of mkTyVarTy
205 getTyVar :: String -> GenType t u -> t
206 getTyVar msg (TyVarTy tv) = tv
207 getTyVar msg (SynTy _ _ t) = getTyVar msg t
208 getTyVar msg other = panic ("getTyVar: " ++ msg)
210 getTyVar_maybe :: GenType t u -> Maybe t
211 getTyVar_maybe (TyVarTy tv) = Just tv
212 getTyVar_maybe (SynTy _ _ t) = getTyVar_maybe t
213 getTyVar_maybe other = Nothing
215 isTyVarTy :: GenType t u -> Bool
216 isTyVarTy (TyVarTy tv) = True
217 isTyVarTy (SynTy _ _ t) = isTyVarTy t
218 isTyVarTy other = False
224 mkAppTys :: GenType t u -> [GenType t u] -> GenType t u
225 mkAppTys t ts = foldl AppTy t ts
227 splitAppTy :: GenType t u -> (GenType t u, GenType t u)
228 splitAppTy (AppTy t arg) = (t,arg)
229 splitAppTy (SynTy _ _ t) = splitAppTy t
230 splitAppTy other = panic "splitAppTy"
232 splitAppTys :: GenType t u -> (GenType t u, [GenType t u])
233 splitAppTys t = go t []
235 go (AppTy t arg) ts = go t (arg:ts)
236 go (FunTy fun arg u) ts = (TyConTy mkFunTyCon u, fun:arg:ts)
237 go (SynTy _ _ t) ts = go t ts
242 -- NB mkFunTy, mkFunTys puts in Omega usages, for now at least
243 mkFunTy arg res = FunTy arg res usageOmega
245 mkFunTys :: [GenType t u] -> GenType t u -> GenType t u
246 mkFunTys ts t = foldr (\ f a -> FunTy f a usageOmega) t ts
248 -- getFunTy_maybe and splitFunTy *must* have the general type given, which
249 -- means they *can't* do the DictTy jiggery-pokery that
250 -- *is* sometimes required. Hence we also have the ExpandingDicts variants
251 -- The relationship between these
252 -- two functions is like that between eqTy and eqSimpleTy.
253 -- ToDo: NUKE when we do dicts via newtype
255 getFunTy_maybe :: GenType t u -> Maybe (GenType t u, GenType t u)
259 -- See notes on type synonyms above
260 go syn_t (FunTy arg result _) = Just (arg,result)
261 go syn_t (AppTy (AppTy (TyConTy tycon _) arg) res)
262 | isFunTyCon tycon = Just (arg, res)
263 go syn_t (SynTy _ _ t) = go syn_t t
264 go syn_t other = Nothing
266 getFunTyExpandingDicts_maybe :: Bool -- True <=> peek inside newtype applicatons
268 -> Maybe (Type, Type)
270 getFunTyExpandingDicts_maybe peek (FunTy arg result _) = Just (arg,result)
271 getFunTyExpandingDicts_maybe peek
272 (AppTy (AppTy (TyConTy tycon _) arg) res) | isFunTyCon tycon = Just (arg, res)
273 getFunTyExpandingDicts_maybe peek (SynTy _ _ t) = getFunTyExpandingDicts_maybe peek t
274 getFunTyExpandingDicts_maybe peek ty@(DictTy _ _ _) = getFunTyExpandingDicts_maybe peek (expandTy ty)
276 getFunTyExpandingDicts_maybe True (ForAllTy _ ty) = getFunTyExpandingDicts_maybe True ty
277 -- Ignore for-alls when peeking. See note with defn of getFunTyExpandingDictsAndPeeking
280 {- This is a truly disgusting bit of code.
281 It's used by the code generator to look at the rep of a newtype.
282 The code gen will have thrown away coercions involving that newtype, so
283 this is the other side of the coin.
284 Gruesome in the extreme.
287 getFunTyExpandingDicts_maybe peek other
288 | not peek = Nothing -- that was easy
290 = case (maybeAppTyCon other) of
292 | isNewTyCon tc && not (null data_cons)
293 -> getFunTyExpandingDicts_maybe peek inside_ty
295 data_cons = tyConDataCons tc
296 [the_con] = data_cons
297 [inside_ty] = dataConArgTys the_con arg_tys
302 splitFunTy :: GenType t u -> ([GenType t u], GenType t u)
303 splitFunTyExpandingDicts :: Type -> ([Type], Type)
304 splitFunTyExpandingDictsAndPeeking :: Type -> ([Type], Type)
306 splitFunTy t = split_fun_ty getFunTy_maybe t
307 splitFunTyExpandingDicts t = split_fun_ty (getFunTyExpandingDicts_maybe False) t
308 splitFunTyExpandingDictsAndPeeking t = split_fun_ty (getFunTyExpandingDicts_maybe True) t
309 -- This "peeking" stuff is used only by the code generator.
310 -- It's interested in the representation type of things, ignoring:
311 -- newtype Why??? Nuked SLPJ May 97. We may not know the
312 -- rep of an abstractly imported newtype
314 -- expanding dictionary reps
315 -- synonyms, of course
317 split_fun_ty get t = go t []
319 go t ts = case (get t) of
320 Just (arg,res) -> go res (arg:ts)
321 Nothing -> (reverse ts, t)
325 -- NB applyTyCon puts in usageOmega, for now at least
327 = ASSERT(not (isSynTyCon tycon))
328 TyConTy tycon usageOmega
330 applyTyCon :: TyCon -> [GenType t u] -> GenType t u
332 = ASSERT (not (isSynTyCon tycon))
333 --(if (not (isSynTyCon tycon)) then \x->x else pprTrace "applyTyCon:" (pprTyCon PprDebug tycon)) $
334 foldl AppTy (TyConTy tycon usageOmega) tys
336 getTyCon_maybe :: GenType t u -> Maybe TyCon
338 getTyCon_maybe (TyConTy tycon _) = Just tycon
339 getTyCon_maybe (SynTy _ _ t) = getTyCon_maybe t
340 getTyCon_maybe other_ty = Nothing
344 specialiseTy :: Type -- The type of the Id of which the SpecId
345 -- is a specialised version
346 -> [Maybe Type] -- The types at which it is specialised
347 -> Int -- Number of leading dictionary args to ignore
350 specialiseTy main_ty maybe_tys dicts_to_ignore
351 = --false:ASSERT(isTauTy tau) TauType??
352 mkSigmaTy remaining_tyvars
353 (instantiateThetaTy inst_env remaining_theta)
354 (instantiateTauTy inst_env tau)
356 (tyvars, theta, tau) = splitSigmaTy main_ty -- A prefix of, but usually all,
357 -- the theta is discarded!
358 remaining_theta = drop dicts_to_ignore theta
359 tyvars_and_maybe_tys = tyvars `zip` maybe_tys
360 remaining_tyvars = [tyvar | (tyvar, Nothing) <- tyvars_and_maybe_tys]
361 inst_env = [(tyvar,ty) | (tyvar, Just ty) <- tyvars_and_maybe_tys]
365 mkSynTy syn_tycon tys
366 = ASSERT(isSynTyCon syn_tycon)
367 SynTy syn_tycon tys (instantiateTauTy (zipEqual "mkSynTy" tyvars tys) body)
369 (tyvars, body) = getSynTyConDefn syn_tycon
375 isTauTy :: GenType t u -> Bool
376 isTauTy (TyVarTy v) = True
377 isTauTy (TyConTy _ _) = True
378 isTauTy (AppTy a b) = isTauTy a && isTauTy b
379 isTauTy (FunTy a b _) = isTauTy a && isTauTy b
380 isTauTy (SynTy _ _ ty) = isTauTy ty
381 isTauTy other = False
386 NB mkRhoTy and mkDictTy put in usageOmega, for now at least
389 mkDictTy :: Class -> GenType t u -> GenType t u
390 mkDictTy clas ty = DictTy clas ty usageOmega
392 mkRhoTy :: [(Class, GenType t u)] -> GenType t u -> GenType t u
394 foldr (\(c,t) r -> FunTy (DictTy c t usageOmega) r usageOmega) ty theta
396 splitRhoTy :: GenType t u -> ([(Class,GenType t u)], GenType t u)
400 -- See notes on type synonyms above
401 go syn_t (FunTy (DictTy c t _) r _) ts = go r r ((c,t):ts)
402 go syn_t (AppTy (AppTy (TyConTy tycon _) (DictTy c t _)) r) ts
405 go syn_t (SynTy _ _ t) ts = go syn_t t ts
406 go syn_t t ts = (reverse ts, syn_t)
409 mkTheta :: [Type] -> ThetaType
410 -- recover a ThetaType from the types of some dictionaries
414 cvt (DictTy clas ty _) = (clas, ty)
415 cvt other = panic "Type.mkTheta" -- pprPanic "mkTheta:" (pprType PprDebug other)
417 isDictTy (DictTy _ _ _) = True
418 isDictTy (SynTy _ _ t) = isDictTy t
426 mkForAllTy = ForAllTy
428 mkForAllTys :: [t] -> GenType t u -> GenType t u
429 mkForAllTys tyvars ty = foldr ForAllTy ty tyvars
431 getForAllTy_maybe :: GenType t u -> Maybe (t,GenType t u)
432 getForAllTy_maybe (SynTy _ _ t) = getForAllTy_maybe t
433 getForAllTy_maybe (ForAllTy tyvar t) = Just(tyvar,t)
434 getForAllTy_maybe _ = Nothing
436 getForAllTyExpandingDicts_maybe :: Type -> Maybe (TyVar, Type)
437 getForAllTyExpandingDicts_maybe (SynTy _ _ t) = getForAllTyExpandingDicts_maybe t
438 getForAllTyExpandingDicts_maybe (ForAllTy tyvar t) = Just(tyvar,t)
439 getForAllTyExpandingDicts_maybe ty@(DictTy _ _ _) = getForAllTyExpandingDicts_maybe (expandTy ty)
440 getForAllTyExpandingDicts_maybe _ = Nothing
442 splitForAllTy :: GenType t u -> ([t], GenType t u)
443 splitForAllTy t = go t t []
445 -- See notes on type synonyms above
446 go syn_t (ForAllTy tv t) tvs = go t t (tv:tvs)
447 go syn_t (SynTy _ _ t) tvs = go syn_t t tvs
448 go syn_t t tvs = (reverse tvs, syn_t)
450 splitForAllTyExpandingDicts :: Type -> ([TyVar], Type)
451 splitForAllTyExpandingDicts ty
454 go tvs ty = case getForAllTyExpandingDicts_maybe ty of
455 Just (tv, ty') -> go (tv:tvs) ty'
456 Nothing -> (reverse tvs, ty)
460 mkForAllUsageTy :: u -> [u] -> GenType t u -> GenType t u
461 mkForAllUsageTy = ForAllUsageTy
463 getForAllUsageTy :: GenType t u -> Maybe (u,[u],GenType t u)
464 getForAllUsageTy (ForAllUsageTy uvar bounds t) = Just(uvar,bounds,t)
465 getForAllUsageTy (SynTy _ _ t) = getForAllUsageTy t
466 getForAllUsageTy _ = Nothing
469 Applied tycons (includes FunTyCons)
470 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
473 :: GenType tyvar uvar
474 -> Maybe (TyCon, -- the type constructor
475 [GenType tyvar uvar]) -- types to which it is applied
478 = case (getTyCon_maybe app_ty) of
480 Just tycon -> Just (tycon, arg_tys)
482 (app_ty, arg_tys) = splitAppTys ty
486 :: GenType tyvar uvar
487 -> (TyCon, -- the type constructor
488 [GenType tyvar uvar]) -- types to which it is applied
491 = case maybeAppTyCon ty of
494 Nothing -> panic "Type.getAppTyCon" -- (ppr PprShowAll ty)
498 Applied data tycons (give back constrs)
499 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
500 Nota Bene: all these functions suceed for @newtype@ applications too!
504 :: GenType (GenTyVar any) uvar
505 -> Maybe (TyCon, -- the type constructor
506 [GenType (GenTyVar any) uvar], -- types to which it is applied
507 [Id]) -- its family of data-constructors
508 maybeAppDataTyConExpandingDicts, maybeAppSpecDataTyConExpandingDicts
509 :: Type -> Maybe (TyCon, [Type], [Id])
511 maybeAppDataTyCon ty = maybe_app_data_tycon (\x->x) ty
512 maybeAppDataTyConExpandingDicts ty = maybe_app_data_tycon expandTy ty
513 maybeAppSpecDataTyConExpandingDicts ty = maybe_app_data_tycon expandTy ty
516 maybe_app_data_tycon expand ty
518 expanded_ty = expand ty
519 (app_ty, arg_tys) = splitAppTys expanded_ty
521 case (getTyCon_maybe app_ty) of
522 Just tycon | isAlgTyCon tycon && -- NB "Alg"; succeeds for newtype too
523 notArrowKind (typeKind expanded_ty)
524 -- Must be saturated for ty to be a data type
525 -> Just (tycon, arg_tys, tyConDataCons tycon)
529 getAppDataTyCon, getAppSpecDataTyCon
530 :: GenType (GenTyVar any) uvar
531 -> (TyCon, -- the type constructor
532 [GenType (GenTyVar any) uvar], -- types to which it is applied
533 [Id]) -- its family of data-constructors
534 getAppDataTyConExpandingDicts, getAppSpecDataTyConExpandingDicts
535 :: Type -> (TyCon, [Type], [Id])
537 getAppDataTyCon ty = get_app_data_tycon maybeAppDataTyCon ty
538 getAppDataTyConExpandingDicts ty = --pprTrace "getAppDataTyConEx...:" (pprType PprDebug ty) $
539 get_app_data_tycon maybeAppDataTyConExpandingDicts ty
541 -- these should work like the UniTyFuns.getUniDataSpecTyCon* things of old (ToDo)
542 getAppSpecDataTyCon = getAppDataTyCon
543 getAppSpecDataTyConExpandingDicts = getAppDataTyConExpandingDicts
545 get_app_data_tycon maybe ty
549 Nothing -> panic "Type.getAppDataTyCon"-- (pprGenType PprShowAll ty)
553 maybeBoxedPrimType :: Type -> Maybe (Id, Type)
555 maybeBoxedPrimType ty
556 = case (maybeAppDataTyCon ty) of -- Data type,
557 Just (tycon, tys_applied, [data_con]) | isDataTyCon tycon -- with exactly one constructor
558 -> case (dataConArgTys data_con tys_applied) of
559 [data_con_arg_ty] -- Applied to exactly one type,
560 | isPrimType data_con_arg_ty -- which is primitive
561 -> Just (data_con, data_con_arg_ty)
562 other_cases -> Nothing
563 other_cases -> Nothing
567 splitSigmaTy :: GenType t u -> ([t], [(Class,GenType t u)], GenType t u)
571 (tyvars,rho) = splitForAllTy ty
572 (theta,tau) = splitRhoTy rho
574 mkSigmaTy tyvars theta tau = mkForAllTys tyvars (mkRhoTy theta tau)
578 Finding the kind of a type
579 ~~~~~~~~~~~~~~~~~~~~~~~~~~
581 typeKind :: GenType (GenTyVar any) u -> Kind
583 typeKind (TyVarTy tyvar) = tyVarKind tyvar
584 typeKind (TyConTy tycon usage) = tyConKind tycon
585 typeKind (SynTy _ _ ty) = typeKind ty
586 typeKind (FunTy fun arg _) = mkBoxedTypeKind
587 typeKind (DictTy clas arg _) = mkBoxedTypeKind
588 typeKind (AppTy fun arg) = resultKind (typeKind fun)
589 typeKind (ForAllTy _ _) = mkBoxedTypeKind
590 typeKind (ForAllUsageTy _ _ _) = mkBoxedTypeKind
594 Free variables of a type
595 ~~~~~~~~~~~~~~~~~~~~~~~~
597 tyVarsOfType :: GenType (GenTyVar flexi) uvar -> GenTyVarSet flexi
599 tyVarsOfType (TyVarTy tv) = unitTyVarSet tv
600 tyVarsOfType (TyConTy tycon usage) = emptyTyVarSet
601 tyVarsOfType (SynTy _ tys ty) = tyVarsOfTypes tys
602 tyVarsOfType (FunTy arg res _) = tyVarsOfType arg `unionTyVarSets` tyVarsOfType res
603 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionTyVarSets` tyVarsOfType arg
604 tyVarsOfType (DictTy clas ty _) = tyVarsOfType ty
605 tyVarsOfType (ForAllTy tyvar ty) = tyVarsOfType ty `minusTyVarSet` unitTyVarSet tyvar
606 tyVarsOfType (ForAllUsageTy _ _ ty) = tyVarsOfType ty
608 tyVarsOfTypes :: [GenType (GenTyVar flexi) uvar] -> GenTyVarSet flexi
609 tyVarsOfTypes tys = foldr (unionTyVarSets.tyVarsOfType) emptyTyVarSet tys
611 -- Find the free names of a type, including the type constructors and classes it mentions
612 namesOfType :: GenType (GenTyVar flexi) uvar -> NameSet
613 namesOfType (TyVarTy tv) = unitNameSet (getName tv)
614 namesOfType (TyConTy tycon usage) = unitNameSet (getName tycon)
615 namesOfType (SynTy tycon tys ty) = unitNameSet (getName tycon) `unionNameSets`
617 namesOfType (FunTy arg res _) = namesOfType arg `unionNameSets` namesOfType res
618 namesOfType (AppTy fun arg) = namesOfType fun `unionNameSets` namesOfType arg
619 namesOfType (DictTy clas ty _) = unitNameSet (getName clas) `unionNameSets`
621 namesOfType (ForAllTy tyvar ty) = namesOfType ty `minusNameSet` unitNameSet (getName tyvar)
622 namesOfType (ForAllUsageTy _ _ ty) = panic "forall usage"
629 -- applyTy :: GenType (GenTyVar flexi) uvar
630 -- -> GenType (GenTyVar flexi) uvar
631 -- -> GenType (GenTyVar flexi) uvar
633 applyTy :: Type -> Type -> Type
635 applyTy (SynTy _ _ fun) arg = applyTy fun arg
636 applyTy (ForAllTy tv ty) arg = instantiateTy [(tv,arg)] ty
637 applyTy ty@(DictTy _ _ _) arg = applyTy (expandTy ty) arg
638 applyTy other arg = panic "applyTy"
642 instantiateTy :: [(GenTyVar flexi, GenType (GenTyVar flexi) uvar)]
643 -> GenType (GenTyVar flexi) uvar
644 -> GenType (GenTyVar flexi) uvar
646 instantiateTauTy :: Eq tv =>
647 [(tv, GenType tv' u)]
651 applyTypeEnvToTy :: TyVarEnv Type -> SigmaType -> SigmaType
653 -- instantiateTauTy works only (a) on types with no ForAlls,
654 -- and when (b) all the type variables are being instantiated
655 -- In return it is more polymorphic than instantiateTy
657 instant_help ty lookup_tv deflt_tv choose_tycon
658 if_usage if_forall bound_forall_tv_BAD deflt_forall_tv
661 go (TyVarTy tv) = case (lookup_tv tv) of
662 Nothing -> deflt_tv tv
664 go ty@(TyConTy tycon usage) = choose_tycon ty tycon usage
665 go (SynTy tycon tys ty) = SynTy tycon (map go tys) (go ty)
666 go (FunTy arg res usage) = FunTy (go arg) (go res) usage
667 go (AppTy fun arg) = AppTy (go fun) (go arg)
668 go (DictTy clas ty usage) = DictTy clas (go ty) usage
669 go (ForAllUsageTy uvar bds ty) = if_usage $
670 ForAllUsageTy uvar bds (go ty)
671 go (ForAllTy tv ty) = if_forall $
672 (if (bound_forall_tv_BAD && maybeToBool (lookup_tv tv)) then
673 trace "instantiateTy: unexpected forall hit"
675 \x->x) ForAllTy (deflt_forall_tv tv) (go ty)
677 instantiateTy [] ty = ty
679 instantiateTy tenv ty
680 = instant_help ty lookup_tv deflt_tv choose_tycon
681 if_usage if_forall bound_forall_tv_BAD deflt_forall_tv
683 lookup_tv tv = case [ty | (tv',ty) <- tenv, tv == tv'] of
686 _ -> panic "instantiateTy:lookup_tv"
688 deflt_tv tv = TyVarTy tv
689 choose_tycon ty _ _ = ty
692 bound_forall_tv_BAD = True
693 deflt_forall_tv tv = tv
695 instantiateTauTy tenv ty
696 = instant_help ty lookup_tv deflt_tv choose_tycon
697 if_usage if_forall bound_forall_tv_BAD deflt_forall_tv
699 lookup_tv tv = case [ty | (tv',ty) <- tenv, tv == tv'] of
702 _ -> panic "instantiateTauTy:lookup_tv"
704 deflt_tv tv = panic "instantiateTauTy"
705 choose_tycon _ tycon usage = TyConTy tycon usage
706 if_usage ty = panic "instantiateTauTy:ForAllUsageTy"
707 if_forall ty = panic "instantiateTauTy:ForAllTy"
708 bound_forall_tv_BAD = panic "instantiateTauTy:bound_forall_tv"
709 deflt_forall_tv tv = panic "instantiateTauTy:deflt_forall_tv"
711 instantiateThetaTy tenv theta
712 = [(clas,instantiateTauTy tenv ty) | (clas,ty) <- theta]
714 -- applyTypeEnv applies a type environment to a type.
715 -- It can handle shadowing; for example:
716 -- f = /\ t1 t2 -> \ d ->
717 -- letrec f' = /\ t1 -> \x -> ...(f' t1 x')...
719 -- Here, when we clone t1 to t1', say, we'll come across shadowing
720 -- when applying the clone environment to the type of f'.
722 -- As a sanity check, we should also check that name capture
723 -- doesn't occur, but that means keeping track of the free variables of the
724 -- range of the TyVarEnv, which I don't do just yet.
726 -- We don't use instant_help because we need to carry in the environment
728 applyTypeEnvToTy tenv ty
731 go tenv ty@(TyVarTy tv) = case (lookupTyVarEnv tenv tv) of
734 go tenv ty@(TyConTy tycon usage) = ty
735 go tenv (SynTy tycon tys ty) = SynTy tycon (map (go tenv) tys) (go tenv ty)
736 go tenv (FunTy arg res usage) = FunTy (go tenv arg) (go tenv res) usage
737 go tenv (AppTy fun arg) = AppTy (go tenv fun) (go tenv arg)
738 go tenv (DictTy clas ty usage) = DictTy clas (go tenv ty) usage
739 go tenv (ForAllUsageTy uvar bds ty) = ForAllUsageTy uvar bds (go tenv ty)
740 go tenv (ForAllTy tv ty) = ForAllTy tv (go tenv' ty)
742 tenv' = case lookupTyVarEnv tenv tv of
744 Just _ -> delFromTyVarEnv tenv tv
749 :: Ord3 u => [(u, GenType t u')] -> GenType t u -> GenType t u'
751 instantiateUsage = panic "instantiateUsage: not implemented"
755 At present there are no unboxed non-primitive types, so
756 isUnboxedType is the same as isPrimType.
758 We're a bit cavalier about finding out whether something is
759 primitive/unboxed or not. Rather than deal with the type
760 arguemnts we just zoom into the function part of the type.
761 That is, given (T a) we just recurse into the "T" part,
765 isPrimType, isUnboxedType :: Type -> Bool
767 isPrimType (AppTy ty _) = isPrimType ty
768 isPrimType (SynTy _ _ ty) = isPrimType ty
769 isPrimType (TyConTy tycon _) = case maybeNewTyCon tycon of
770 Just (tyvars, ty) -> isPrimType ty
771 Nothing -> isPrimTyCon tycon
775 isUnboxedType = isPrimType
778 This is *not* right: it is a placeholder (ToDo 96/03 WDP):
780 typePrimRep :: Type -> PrimRep
782 typePrimRep (SynTy _ _ ty) = typePrimRep ty
783 typePrimRep (AppTy ty _) = typePrimRep ty
784 typePrimRep (TyConTy tc _)
785 | isPrimTyCon tc = case (assocMaybe tc_primrep_list (uniqueOf tc)) of
787 Nothing -> panic "Type.typePrimRep" -- pprPanic "typePrimRep:" (pprTyCon PprDebug tc)
789 | otherwise = case maybeNewTyCon tc of
790 Just (tyvars, ty) | isPrimType ty -> typePrimRep ty
791 _ -> PtrRep -- Default
793 typePrimRep _ = PtrRep -- the "default"
796 = [(addrPrimTyConKey, AddrRep)
797 ,(arrayPrimTyConKey, ArrayRep)
798 ,(byteArrayPrimTyConKey, ByteArrayRep)
799 ,(charPrimTyConKey, CharRep)
800 ,(doublePrimTyConKey, DoubleRep)
801 ,(floatPrimTyConKey, FloatRep)
802 ,(foreignObjPrimTyConKey, ForeignObjRep)
803 ,(intPrimTyConKey, IntRep)
804 ,(mutableArrayPrimTyConKey, ArrayRep)
805 ,(mutableByteArrayPrimTyConKey, ByteArrayRep)
806 ,(stablePtrPrimTyConKey, StablePtrRep)
807 ,(statePrimTyConKey, VoidRep)
808 ,(synchVarPrimTyConKey, PtrRep)
809 ,(voidTyConKey, PtrRep) -- Not VoidRep! That's just for Void#
810 -- The type Void is represented by a pointer to
812 ,(wordPrimTyConKey, WordRep)
816 %************************************************************************
818 \subsection{Matching on types}
820 %************************************************************************
822 Matching is a {\em unidirectional} process, matching a type against a
823 template (which is just a type with type variables in it). The
824 matcher assumes that there are no repeated type variables in the
825 template, so that it simply returns a mapping of type variables to
826 types. It also fails on nested foralls.
828 @matchTys@ matches corresponding elements of a list of templates and
832 matchTy :: GenType t1 u1 -- Template
833 -> GenType t2 u2 -- Proposed instance of template
834 -> Maybe [(t1,GenType t2 u2)] -- Matching substitution
837 matchTys :: [GenType t1 u1] -- Templates
838 -> [GenType t2 u2] -- Proposed instance of template
839 -> Maybe ([(t1,GenType t2 u2)],-- Matching substitution
840 [GenType t2 u2]) -- Left over instance types
842 matchTy ty1 ty2 = match ty1 ty2 (\s -> Just s) []
843 matchTys tys1 tys2 = go [] tys1 tys2
845 go s [] tys2 = Just (s,tys2)
846 go s (ty1:tys1) [] = trace "matchTys" Nothing
847 go s (ty1:tys1) (ty2:tys2) = match ty1 ty2 (\s' -> go s' tys1 tys2) s
850 @match@ is the main function.
853 match :: GenType t1 u1 -> GenType t2 u2 -- Current match pair
854 -> ([(t1, GenType t2 u2)] -> Maybe result) -- Continuation
855 -> [(t1, GenType t2 u2)] -- Current substitution
858 match (TyVarTy v) ty k = \s -> k ((v,ty) : s)
859 match (FunTy fun1 arg1 _) (FunTy fun2 arg2 _) k = match fun1 fun2 (match arg1 arg2 k)
860 match (AppTy fun1 arg1) (AppTy fun2 arg2) k = match fun1 fun2 (match arg1 arg2 k)
861 match (TyConTy con1 _) (TyConTy con2 _) k | con1 == con2 = k
862 match (DictTy clas1 ty1 _) (DictTy clas2 ty2 _) k | clas1 == clas2 = match ty1 ty2 k
863 match (SynTy _ _ ty1) ty2 k = match ty1 ty2 k
864 match ty1 (SynTy _ _ ty2) k = match ty1 ty2 k
866 -- With type synonyms, we have to be careful for the exact
867 -- same reasons as in the unifier. Please see the
868 -- considerable commentary there before changing anything
872 match _ _ _ = \s -> Nothing
875 %************************************************************************
877 \subsection{Equality on types}
879 %************************************************************************
881 The functions eqSimpleTy and eqSimpleTheta are polymorphic in the types t
882 and u, but ONLY WORK FOR SIMPLE TYPES (ie. they panic if they see
883 dictionaries or polymorphic types). The function eqTy has a more
884 specific type, but does the `right thing' for all types.
887 eqSimpleTheta :: (Eq t,Eq u) =>
888 [(Class,GenType t u)] -> [(Class,GenType t u)] -> Bool
890 eqSimpleTheta [] [] = True
891 eqSimpleTheta ((c1,t1):th1) ((c2,t2):th2) =
892 c1==c2 && t1 `eqSimpleTy` t2 && th1 `eqSimpleTheta` th2
893 eqSimpleTheta other1 other2 = False
897 eqSimpleTy :: (Eq t,Eq u) => GenType t u -> GenType t u -> Bool
899 (TyVarTy tv1) `eqSimpleTy` (TyVarTy tv2) =
901 (AppTy f1 a1) `eqSimpleTy` (AppTy f2 a2) =
902 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2
903 (TyConTy tc1 u1) `eqSimpleTy` (TyConTy tc2 u2) =
904 tc1 == tc2 --ToDo: later: && u1 == u2
906 (FunTy f1 a1 u1) `eqSimpleTy` (FunTy f2 a2 u2) =
907 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2 && u1 == u2
908 (FunTy f1 a1 u1) `eqSimpleTy` t2 =
909 -- Expand t1 just in case t2 matches that version
910 (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) `eqSimpleTy` t2
911 t1 `eqSimpleTy` (FunTy f2 a2 u2) =
912 -- Expand t2 just in case t1 matches that version
913 t1 `eqSimpleTy` (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
915 (SynTy tc1 ts1 t1) `eqSimpleTy` (SynTy tc2 ts2 t2) =
916 (tc1 == tc2 && and (zipWith eqSimpleTy ts1 ts2) && length ts1 == length ts2)
917 || t1 `eqSimpleTy` t2
918 (SynTy _ _ t1) `eqSimpleTy` t2 =
919 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
920 t1 `eqSimpleTy` (SynTy _ _ t2) =
921 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
923 (DictTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got DictTy"
924 _ `eqSimpleTy` (DictTy _ _ _) = panic "eqSimpleTy: got DictTy"
926 (ForAllTy _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllTy"
927 _ `eqSimpleTy` (ForAllTy _ _) = panic "eqSimpleTy: got ForAllTy"
929 (ForAllUsageTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllUsageTy"
930 _ `eqSimpleTy` (ForAllUsageTy _ _ _) = panic "eqSimpleTy: got ForAllUsageTy"
932 _ `eqSimpleTy` _ = False
935 Types are ordered so we can sort on types in the renamer etc. DNT: Since
936 this class is also used in CoreLint and other such places, we DO expand out
937 Fun/Syn/Dict types (if necessary).
940 eqTy :: Type -> Type -> Bool
943 eq nullTyVarEnv nullUVarEnv t1 t2
945 eq tve uve (TyVarTy tv1) (TyVarTy tv2) =
947 case (lookupTyVarEnv tve tv1) of
950 eq tve uve (AppTy f1 a1) (AppTy f2 a2) =
951 eq tve uve f1 f2 && eq tve uve a1 a2
952 eq tve uve (TyConTy tc1 u1) (TyConTy tc2 u2) =
953 tc1 == tc2 -- ToDo: LATER: && eqUsage uve u1 u2
955 eq tve uve (FunTy f1 a1 u1) (FunTy f2 a2 u2) =
956 eq tve uve f1 f2 && eq tve uve a1 a2 && eqUsage uve u1 u2
957 eq tve uve (FunTy f1 a1 u1) t2 =
958 -- Expand t1 just in case t2 matches that version
959 eq tve uve (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) t2
960 eq tve uve t1 (FunTy f2 a2 u2) =
961 -- Expand t2 just in case t1 matches that version
962 eq tve uve t1 (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
964 eq tve uve (DictTy c1 t1 u1) (DictTy c2 t2 u2)
966 = eq tve uve t1 t2 && eqUsage uve u1 u2
967 -- NB we use a guard for c1==c2 so that if they aren't equal we
968 -- fall through into expanding the type. Why? Because brain-dead
969 -- people might write
970 -- class Foo a => Baz a where {}
971 -- and that means that a Foo dictionary and a Baz dictionary are identical
972 -- Sigh. Let's hope we don't spend too much time in here!
974 eq tve uve t1@(DictTy _ _ _) t2 =
975 eq tve uve (expandTy t1) t2 -- Expand the dictionary and try again
976 eq tve uve t1 t2@(DictTy _ _ _) =
977 eq tve uve t1 (expandTy t2) -- Expand the dictionary and try again
979 eq tve uve (SynTy tc1 ts1 t1) (SynTy tc2 ts2 t2) =
980 (tc1 == tc2 && and (zipWith (eq tve uve) ts1 ts2) && length ts1 == length ts2)
982 eq tve uve (SynTy _ _ t1) t2 =
983 eq tve uve t1 t2 -- Expand the abbrevation and try again
984 eq tve uve t1 (SynTy _ _ t2) =
985 eq tve uve t1 t2 -- Expand the abbrevation and try again
987 eq tve uve (ForAllTy tv1 t1) (ForAllTy tv2 t2) =
988 eq (addOneToTyVarEnv tve tv1 tv2) uve t1 t2
989 eq tve uve (ForAllUsageTy u1 b1 t1) (ForAllUsageTy u2 b2 t2) =
990 eqBounds uve b1 b2 && eq tve (addOneToUVarEnv uve u1 u2) t1 t2
994 eqBounds uve [] [] = True
995 eqBounds uve (u1:b1) (u2:b2) = eqUVar uve u1 u2 && eqBounds uve b1 b2
996 eqBounds uve _ _ = False
1000 showTypeCategory :: Type -> Char
1002 {C,I,F,D} char, int, float, double
1004 S other single-constructor type
1005 {c,i,f,d} unboxed ditto
1007 s *unpacked" single-cons...
1013 + dictionary, unless it's a ...
1016 M other (multi-constructor) data-con type
1018 - reserved for others to mark as "uninteresting"
1024 case getTyCon_maybe ty of
1025 Nothing -> if maybeToBool (getFunTy_maybe ty)
1030 let utc = uniqueOf tycon in
1031 if utc == charDataConKey then 'C'
1032 else if utc == intDataConKey then 'I'
1033 else if utc == floatDataConKey then 'F'
1034 else if utc == doubleDataConKey then 'D'
1035 else if utc == integerDataConKey then 'J'
1036 else if utc == charPrimTyConKey then 'c'
1037 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
1038 || utc == addrPrimTyConKey) then 'i'
1039 else if utc == floatPrimTyConKey then 'f'
1040 else if utc == doublePrimTyConKey then 'd'
1041 else if isPrimTyCon tycon {- array, we hope -} then 'A'
1042 else if isEnumerationTyCon tycon then 'E'
1043 else if isTupleTyCon tycon then 'T'
1044 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
1045 else if utc == listTyConKey then 'L'
1046 else 'M' -- oh, well...