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 type Type = GenType TyVar UVar -- Used after typechecker
90 data GenType tyvar uvar -- Parameterised over type and usage variables
97 | TyConTy -- Constants of a specified kind
98 TyCon -- Must *not* be a SynTyCon
99 (GenUsage uvar) -- Usage gives uvar of the full application,
100 -- iff the full application is of kind Type
101 -- c.f. the Usage field in TyVars
103 | SynTy -- Synonyms must be saturated, and contain their expansion
104 TyCon -- Must be a SynTyCon
106 (GenType tyvar uvar) -- Expansion!
110 (GenType tyvar uvar) -- TypeKind
113 uvar -- Quantify over this
114 [uvar] -- Bounds; the quantified var must be
115 -- less than or equal to all these
118 -- Two special cases that save a *lot* of administrative
121 | FunTy -- BoxedTypeKind
122 (GenType tyvar uvar) -- Both args are of TypeKind
128 (GenType tyvar uvar) -- Arg has kind TypeKind
135 type ThetaType = [(Class, Type)]
136 type SigmaType = Type
140 Notes on type synonyms
141 ~~~~~~~~~~~~~~~~~~~~~~
142 The various "split" functions (splitFunTy, splitRhoTy, splitForAllTy) try
143 to return type synonyms whereever possible. Thus
148 splitFunTys (a -> Foo a) = ([a], Foo a)
151 The reason is that we then get better (shorter) type signatures in
152 interfaces. Notably this plays a role in tcTySigs in TcBinds.lhs.
155 Simple construction and analysis functions
156 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158 mkTyVarTy :: t -> GenType t u
159 mkTyVarTys :: [t] -> [GenType t y]
161 mkTyVarTys = map mkTyVarTy -- a common use of mkTyVarTy
163 getTyVar :: String -> GenType t u -> t
164 getTyVar msg (TyVarTy tv) = tv
165 getTyVar msg (SynTy _ _ t) = getTyVar msg t
166 getTyVar msg other = panic ("getTyVar: " ++ msg)
168 getTyVar_maybe :: GenType t u -> Maybe t
169 getTyVar_maybe (TyVarTy tv) = Just tv
170 getTyVar_maybe (SynTy _ _ t) = getTyVar_maybe t
171 getTyVar_maybe other = Nothing
173 isTyVarTy :: GenType t u -> Bool
174 isTyVarTy (TyVarTy tv) = True
175 isTyVarTy (SynTy _ _ t) = isTyVarTy t
176 isTyVarTy other = False
182 mkAppTys :: GenType t u -> [GenType t u] -> GenType t u
183 mkAppTys t ts = foldl AppTy t ts
185 splitAppTy :: GenType t u -> (GenType t u, GenType t u)
186 splitAppTy (AppTy t arg) = (t,arg)
187 splitAppTy (SynTy _ _ t) = splitAppTy t
188 splitAppTy other = panic "splitAppTy"
190 splitAppTys :: GenType t u -> (GenType t u, [GenType t u])
191 splitAppTys t = go t []
193 go (AppTy t arg) ts = go t (arg:ts)
194 go (FunTy fun arg u) ts = (TyConTy mkFunTyCon u, fun:arg:ts)
195 go (SynTy _ _ t) ts = go t ts
200 -- NB mkFunTy, mkFunTys puts in Omega usages, for now at least
201 mkFunTy arg res = FunTy arg res usageOmega
203 mkFunTys :: [GenType t u] -> GenType t u -> GenType t u
204 mkFunTys ts t = foldr (\ f a -> FunTy f a usageOmega) t ts
206 -- getFunTy_maybe and splitFunTy *must* have the general type given, which
207 -- means they *can't* do the DictTy jiggery-pokery that
208 -- *is* sometimes required. Hence we also have the ExpandingDicts variants
209 -- The relationship between these
210 -- two functions is like that between eqTy and eqSimpleTy.
211 -- ToDo: NUKE when we do dicts via newtype
213 getFunTy_maybe :: GenType t u -> Maybe (GenType t u, GenType t u)
217 -- See notes on type synonyms above
218 go syn_t (FunTy arg result _) = Just (arg,result)
219 go syn_t (AppTy (AppTy (TyConTy tycon _) arg) res)
220 | isFunTyCon tycon = Just (arg, res)
221 go syn_t (SynTy _ _ t) = go syn_t t
222 go syn_t other = Nothing
224 getFunTyExpandingDicts_maybe :: Bool -- True <=> peek inside newtype applicatons
226 -> Maybe (Type, Type)
228 getFunTyExpandingDicts_maybe peek (FunTy arg result _) = Just (arg,result)
229 getFunTyExpandingDicts_maybe peek
230 (AppTy (AppTy (TyConTy tycon _) arg) res) | isFunTyCon tycon = Just (arg, res)
231 getFunTyExpandingDicts_maybe peek (SynTy _ _ t) = getFunTyExpandingDicts_maybe peek t
232 getFunTyExpandingDicts_maybe peek ty@(DictTy _ _ _) = getFunTyExpandingDicts_maybe peek (expandTy ty)
234 getFunTyExpandingDicts_maybe True (ForAllTy _ ty) = getFunTyExpandingDicts_maybe True ty
235 -- Ignore for-alls when peeking. See note with defn of getFunTyExpandingDictsAndPeeking
238 {- This is a truly disgusting bit of code.
239 It's used by the code generator to look at the rep of a newtype.
240 The code gen will have thrown away coercions involving that newtype, so
241 this is the other side of the coin.
242 Gruesome in the extreme.
245 getFunTyExpandingDicts_maybe peek other
246 | not peek = Nothing -- that was easy
248 = case (maybeAppTyCon other) of
250 | isNewTyCon tc && not (null data_cons)
251 -> getFunTyExpandingDicts_maybe peek inside_ty
253 data_cons = tyConDataCons tc
254 [the_con] = data_cons
255 [inside_ty] = dataConArgTys the_con arg_tys
260 splitFunTy :: GenType t u -> ([GenType t u], GenType t u)
261 splitFunTyExpandingDicts :: Type -> ([Type], Type)
262 splitFunTyExpandingDictsAndPeeking :: Type -> ([Type], Type)
264 splitFunTy t = split_fun_ty getFunTy_maybe t
265 splitFunTyExpandingDicts t = split_fun_ty (getFunTyExpandingDicts_maybe False) t
266 splitFunTyExpandingDictsAndPeeking t = split_fun_ty (getFunTyExpandingDicts_maybe True) t
267 -- This "peeking" stuff is used only by the code generator.
268 -- It's interested in the representation type of things, ignoring:
269 -- newtype Why??? Nuked SLPJ May 97. We may not know the
270 -- rep of an abstractly imported newtype
272 -- expanding dictionary reps
273 -- synonyms, of course
275 split_fun_ty get t = go t []
277 go t ts = case (get t) of
278 Just (arg,res) -> go res (arg:ts)
279 Nothing -> (reverse ts, t)
283 -- NB applyTyCon puts in usageOmega, for now at least
285 = ASSERT(not (isSynTyCon tycon))
286 TyConTy tycon usageOmega
288 applyTyCon :: TyCon -> [GenType t u] -> GenType t u
290 = ASSERT (not (isSynTyCon tycon))
291 --(if (not (isSynTyCon tycon)) then \x->x else pprTrace "applyTyCon:" (pprTyCon PprDebug tycon)) $
292 foldl AppTy (TyConTy tycon usageOmega) tys
294 getTyCon_maybe :: GenType t u -> Maybe TyCon
296 getTyCon_maybe (TyConTy tycon _) = Just tycon
297 getTyCon_maybe (SynTy _ _ t) = getTyCon_maybe t
298 getTyCon_maybe other_ty = Nothing
302 specialiseTy :: Type -- The type of the Id of which the SpecId
303 -- is a specialised version
304 -> [Maybe Type] -- The types at which it is specialised
305 -> Int -- Number of leading dictionary args to ignore
308 specialiseTy main_ty maybe_tys dicts_to_ignore
309 = --false:ASSERT(isTauTy tau) TauType??
310 mkSigmaTy remaining_tyvars
311 (instantiateThetaTy inst_env remaining_theta)
312 (instantiateTauTy inst_env tau)
314 (tyvars, theta, tau) = splitSigmaTy main_ty -- A prefix of, but usually all,
315 -- the theta is discarded!
316 remaining_theta = drop dicts_to_ignore theta
317 tyvars_and_maybe_tys = tyvars `zip` maybe_tys
318 remaining_tyvars = [tyvar | (tyvar, Nothing) <- tyvars_and_maybe_tys]
319 inst_env = [(tyvar,ty) | (tyvar, Just ty) <- tyvars_and_maybe_tys]
323 mkSynTy syn_tycon tys
324 = ASSERT(isSynTyCon syn_tycon)
325 SynTy syn_tycon tys (instantiateTauTy (zipEqual "mkSynTy" tyvars tys) body)
327 (tyvars, body) = getSynTyConDefn syn_tycon
333 isTauTy :: GenType t u -> Bool
334 isTauTy (TyVarTy v) = True
335 isTauTy (TyConTy _ _) = True
336 isTauTy (AppTy a b) = isTauTy a && isTauTy b
337 isTauTy (FunTy a b _) = isTauTy a && isTauTy b
338 isTauTy (SynTy _ _ ty) = isTauTy ty
339 isTauTy other = False
344 NB mkRhoTy and mkDictTy put in usageOmega, for now at least
347 mkDictTy :: Class -> GenType t u -> GenType t u
348 mkDictTy clas ty = DictTy clas ty usageOmega
350 mkRhoTy :: [(Class, GenType t u)] -> GenType t u -> GenType t u
352 foldr (\(c,t) r -> FunTy (DictTy c t usageOmega) r usageOmega) ty theta
354 splitRhoTy :: GenType t u -> ([(Class,GenType t u)], GenType t u)
358 -- See notes on type synonyms above
359 go syn_t (FunTy (DictTy c t _) r _) ts = go r r ((c,t):ts)
360 go syn_t (AppTy (AppTy (TyConTy tycon _) (DictTy c t _)) r) ts
363 go syn_t (SynTy _ _ t) ts = go syn_t t ts
364 go syn_t t ts = (reverse ts, syn_t)
367 mkTheta :: [Type] -> ThetaType
368 -- recover a ThetaType from the types of some dictionaries
372 cvt (DictTy clas ty _) = (clas, ty)
373 cvt other = panic "Type.mkTheta" -- pprPanic "mkTheta:" (pprType PprDebug other)
375 isDictTy (DictTy _ _ _) = True
376 isDictTy (SynTy _ _ t) = isDictTy t
384 mkForAllTy = ForAllTy
386 mkForAllTys :: [t] -> GenType t u -> GenType t u
387 mkForAllTys tyvars ty = foldr ForAllTy ty tyvars
389 getForAllTy_maybe :: GenType t u -> Maybe (t,GenType t u)
390 getForAllTy_maybe (SynTy _ _ t) = getForAllTy_maybe t
391 getForAllTy_maybe (ForAllTy tyvar t) = Just(tyvar,t)
392 getForAllTy_maybe _ = Nothing
394 getForAllTyExpandingDicts_maybe :: Type -> Maybe (TyVar, Type)
395 getForAllTyExpandingDicts_maybe (SynTy _ _ t) = getForAllTyExpandingDicts_maybe t
396 getForAllTyExpandingDicts_maybe (ForAllTy tyvar t) = Just(tyvar,t)
397 getForAllTyExpandingDicts_maybe ty@(DictTy _ _ _) = getForAllTyExpandingDicts_maybe (expandTy ty)
398 getForAllTyExpandingDicts_maybe _ = Nothing
400 splitForAllTy :: GenType t u -> ([t], GenType t u)
401 splitForAllTy t = go t t []
403 -- See notes on type synonyms above
404 go syn_t (ForAllTy tv t) tvs = go t t (tv:tvs)
405 go syn_t (SynTy _ _ t) tvs = go syn_t t tvs
406 go syn_t t tvs = (reverse tvs, syn_t)
408 splitForAllTyExpandingDicts :: Type -> ([TyVar], Type)
409 splitForAllTyExpandingDicts ty
412 go tvs ty = case getForAllTyExpandingDicts_maybe ty of
413 Just (tv, ty') -> go (tv:tvs) ty'
414 Nothing -> (reverse tvs, ty)
418 mkForAllUsageTy :: u -> [u] -> GenType t u -> GenType t u
419 mkForAllUsageTy = ForAllUsageTy
421 getForAllUsageTy :: GenType t u -> Maybe (u,[u],GenType t u)
422 getForAllUsageTy (ForAllUsageTy uvar bounds t) = Just(uvar,bounds,t)
423 getForAllUsageTy (SynTy _ _ t) = getForAllUsageTy t
424 getForAllUsageTy _ = Nothing
427 Applied tycons (includes FunTyCons)
428 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
431 :: GenType tyvar uvar
432 -> Maybe (TyCon, -- the type constructor
433 [GenType tyvar uvar]) -- types to which it is applied
436 = case (getTyCon_maybe app_ty) of
438 Just tycon -> Just (tycon, arg_tys)
440 (app_ty, arg_tys) = splitAppTys ty
444 :: GenType tyvar uvar
445 -> (TyCon, -- the type constructor
446 [GenType tyvar uvar]) -- types to which it is applied
449 = case maybeAppTyCon ty of
452 Nothing -> panic "Type.getAppTyCon" -- (ppr PprShowAll ty)
456 Applied data tycons (give back constrs)
457 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
458 Nota Bene: all these functions suceed for @newtype@ applications too!
462 :: GenType (GenTyVar any) uvar
463 -> Maybe (TyCon, -- the type constructor
464 [GenType (GenTyVar any) uvar], -- types to which it is applied
465 [Id]) -- its family of data-constructors
466 maybeAppDataTyConExpandingDicts, maybeAppSpecDataTyConExpandingDicts
467 :: Type -> Maybe (TyCon, [Type], [Id])
469 maybeAppDataTyCon ty = maybe_app_data_tycon (\x->x) ty
470 maybeAppDataTyConExpandingDicts ty = maybe_app_data_tycon expandTy ty
471 maybeAppSpecDataTyConExpandingDicts ty = maybe_app_data_tycon expandTy ty
474 maybe_app_data_tycon expand ty
476 expanded_ty = expand ty
477 (app_ty, arg_tys) = splitAppTys expanded_ty
479 case (getTyCon_maybe app_ty) of
480 Just tycon | isAlgTyCon tycon && -- NB "Alg"; succeeds for newtype too
481 notArrowKind (typeKind expanded_ty)
482 -- Must be saturated for ty to be a data type
483 -> Just (tycon, arg_tys, tyConDataCons tycon)
487 getAppDataTyCon, getAppSpecDataTyCon
488 :: GenType (GenTyVar any) uvar
489 -> (TyCon, -- the type constructor
490 [GenType (GenTyVar any) uvar], -- types to which it is applied
491 [Id]) -- its family of data-constructors
492 getAppDataTyConExpandingDicts, getAppSpecDataTyConExpandingDicts
493 :: Type -> (TyCon, [Type], [Id])
495 getAppDataTyCon ty = get_app_data_tycon maybeAppDataTyCon ty
496 getAppDataTyConExpandingDicts ty = --pprTrace "getAppDataTyConEx...:" (pprType PprDebug ty) $
497 get_app_data_tycon maybeAppDataTyConExpandingDicts ty
499 -- these should work like the UniTyFuns.getUniDataSpecTyCon* things of old (ToDo)
500 getAppSpecDataTyCon = getAppDataTyCon
501 getAppSpecDataTyConExpandingDicts = getAppDataTyConExpandingDicts
503 get_app_data_tycon maybe ty
507 Nothing -> panic "Type.getAppDataTyCon"-- (pprGenType PprShowAll ty)
511 maybeBoxedPrimType :: Type -> Maybe (Id, Type)
513 maybeBoxedPrimType ty
514 = case (maybeAppDataTyCon ty) of -- Data type,
515 Just (tycon, tys_applied, [data_con]) | isDataTyCon tycon -- with exactly one constructor
516 -> case (dataConArgTys data_con tys_applied) of
517 [data_con_arg_ty] -- Applied to exactly one type,
518 | isPrimType data_con_arg_ty -- which is primitive
519 -> Just (data_con, data_con_arg_ty)
520 other_cases -> Nothing
521 other_cases -> Nothing
525 splitSigmaTy :: GenType t u -> ([t], [(Class,GenType t u)], GenType t u)
529 (tyvars,rho) = splitForAllTy ty
530 (theta,tau) = splitRhoTy rho
532 mkSigmaTy tyvars theta tau = mkForAllTys tyvars (mkRhoTy theta tau)
536 Finding the kind of a type
537 ~~~~~~~~~~~~~~~~~~~~~~~~~~
539 typeKind :: GenType (GenTyVar any) u -> Kind
541 typeKind (TyVarTy tyvar) = tyVarKind tyvar
542 typeKind (TyConTy tycon usage) = tyConKind tycon
543 typeKind (SynTy _ _ ty) = typeKind ty
544 typeKind (FunTy fun arg _) = mkBoxedTypeKind
545 typeKind (DictTy clas arg _) = mkBoxedTypeKind
546 typeKind (AppTy fun arg) = resultKind (typeKind fun)
547 typeKind (ForAllTy _ _) = mkBoxedTypeKind
548 typeKind (ForAllUsageTy _ _ _) = mkBoxedTypeKind
552 Free variables of a type
553 ~~~~~~~~~~~~~~~~~~~~~~~~
555 tyVarsOfType :: GenType (GenTyVar flexi) uvar -> GenTyVarSet flexi
557 tyVarsOfType (TyVarTy tv) = unitTyVarSet tv
558 tyVarsOfType (TyConTy tycon usage) = emptyTyVarSet
559 tyVarsOfType (SynTy _ tys ty) = tyVarsOfTypes tys
560 tyVarsOfType (FunTy arg res _) = tyVarsOfType arg `unionTyVarSets` tyVarsOfType res
561 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionTyVarSets` tyVarsOfType arg
562 tyVarsOfType (DictTy clas ty _) = tyVarsOfType ty
563 tyVarsOfType (ForAllTy tyvar ty) = tyVarsOfType ty `minusTyVarSet` unitTyVarSet tyvar
564 tyVarsOfType (ForAllUsageTy _ _ ty) = tyVarsOfType ty
566 tyVarsOfTypes :: [GenType (GenTyVar flexi) uvar] -> GenTyVarSet flexi
567 tyVarsOfTypes tys = foldr (unionTyVarSets.tyVarsOfType) emptyTyVarSet tys
569 -- Find the free names of a type, including the type constructors and classes it mentions
570 namesOfType :: GenType (GenTyVar flexi) uvar -> NameSet
571 namesOfType (TyVarTy tv) = unitNameSet (getName tv)
572 namesOfType (TyConTy tycon usage) = unitNameSet (getName tycon)
573 namesOfType (SynTy tycon tys ty) = unitNameSet (getName tycon) `unionNameSets`
575 namesOfType (FunTy arg res _) = namesOfType arg `unionNameSets` namesOfType res
576 namesOfType (AppTy fun arg) = namesOfType fun `unionNameSets` namesOfType arg
577 namesOfType (DictTy clas ty _) = unitNameSet (getName clas) `unionNameSets`
579 namesOfType (ForAllTy tyvar ty) = namesOfType ty `minusNameSet` unitNameSet (getName tyvar)
580 namesOfType (ForAllUsageTy _ _ ty) = panic "forall usage"
587 -- applyTy :: GenType (GenTyVar flexi) uvar
588 -- -> GenType (GenTyVar flexi) uvar
589 -- -> GenType (GenTyVar flexi) uvar
591 applyTy :: Type -> Type -> Type
593 applyTy (SynTy _ _ fun) arg = applyTy fun arg
594 applyTy (ForAllTy tv ty) arg = instantiateTy [(tv,arg)] ty
595 applyTy ty@(DictTy _ _ _) arg = applyTy (expandTy ty) arg
596 applyTy other arg = panic "applyTy"
600 instantiateTy :: [(GenTyVar flexi, GenType (GenTyVar flexi) uvar)]
601 -> GenType (GenTyVar flexi) uvar
602 -> GenType (GenTyVar flexi) uvar
604 instantiateTauTy :: Eq tv =>
605 [(tv, GenType tv' u)]
609 applyTypeEnvToTy :: TyVarEnv Type -> SigmaType -> SigmaType
611 -- instantiateTauTy works only (a) on types with no ForAlls,
612 -- and when (b) all the type variables are being instantiated
613 -- In return it is more polymorphic than instantiateTy
615 instant_help ty lookup_tv deflt_tv choose_tycon
616 if_usage if_forall bound_forall_tv_BAD deflt_forall_tv
619 go (TyVarTy tv) = case (lookup_tv tv) of
620 Nothing -> deflt_tv tv
622 go ty@(TyConTy tycon usage) = choose_tycon ty tycon usage
623 go (SynTy tycon tys ty) = SynTy tycon (map go tys) (go ty)
624 go (FunTy arg res usage) = FunTy (go arg) (go res) usage
625 go (AppTy fun arg) = AppTy (go fun) (go arg)
626 go (DictTy clas ty usage) = DictTy clas (go ty) usage
627 go (ForAllUsageTy uvar bds ty) = if_usage $
628 ForAllUsageTy uvar bds (go ty)
629 go (ForAllTy tv ty) = if_forall $
630 (if (bound_forall_tv_BAD && maybeToBool (lookup_tv tv)) then
631 trace "instantiateTy: unexpected forall hit"
633 \x->x) ForAllTy (deflt_forall_tv tv) (go ty)
635 instantiateTy [] ty = ty
637 instantiateTy tenv ty
638 = instant_help ty lookup_tv deflt_tv choose_tycon
639 if_usage if_forall bound_forall_tv_BAD deflt_forall_tv
641 lookup_tv tv = case [ty | (tv',ty) <- tenv, tv == tv'] of
644 _ -> panic "instantiateTy:lookup_tv"
646 deflt_tv tv = TyVarTy tv
647 choose_tycon ty _ _ = ty
650 bound_forall_tv_BAD = True
651 deflt_forall_tv tv = tv
653 instantiateTauTy tenv ty
654 = instant_help ty lookup_tv deflt_tv choose_tycon
655 if_usage if_forall bound_forall_tv_BAD deflt_forall_tv
657 lookup_tv tv = case [ty | (tv',ty) <- tenv, tv == tv'] of
660 _ -> panic "instantiateTauTy:lookup_tv"
662 deflt_tv tv = panic "instantiateTauTy"
663 choose_tycon _ tycon usage = TyConTy tycon usage
664 if_usage ty = panic "instantiateTauTy:ForAllUsageTy"
665 if_forall ty = panic "instantiateTauTy:ForAllTy"
666 bound_forall_tv_BAD = panic "instantiateTauTy:bound_forall_tv"
667 deflt_forall_tv tv = panic "instantiateTauTy:deflt_forall_tv"
669 instantiateThetaTy tenv theta
670 = [(clas,instantiateTauTy tenv ty) | (clas,ty) <- theta]
672 -- applyTypeEnv applies a type environment to a type.
673 -- It can handle shadowing; for example:
674 -- f = /\ t1 t2 -> \ d ->
675 -- letrec f' = /\ t1 -> \x -> ...(f' t1 x')...
677 -- Here, when we clone t1 to t1', say, we'll come across shadowing
678 -- when applying the clone environment to the type of f'.
680 -- As a sanity check, we should also check that name capture
681 -- doesn't occur, but that means keeping track of the free variables of the
682 -- range of the TyVarEnv, which I don't do just yet.
684 -- We don't use instant_help because we need to carry in the environment
686 applyTypeEnvToTy tenv ty
689 go tenv ty@(TyVarTy tv) = case (lookupTyVarEnv tenv tv) of
692 go tenv ty@(TyConTy tycon usage) = ty
693 go tenv (SynTy tycon tys ty) = SynTy tycon (map (go tenv) tys) (go tenv ty)
694 go tenv (FunTy arg res usage) = FunTy (go tenv arg) (go tenv res) usage
695 go tenv (AppTy fun arg) = AppTy (go tenv fun) (go tenv arg)
696 go tenv (DictTy clas ty usage) = DictTy clas (go tenv ty) usage
697 go tenv (ForAllUsageTy uvar bds ty) = ForAllUsageTy uvar bds (go tenv ty)
698 go tenv (ForAllTy tv ty) = ForAllTy tv (go tenv' ty)
700 tenv' = case lookupTyVarEnv tenv tv of
702 Just _ -> delFromTyVarEnv tenv tv
707 :: Ord3 u => [(u, GenType t u')] -> GenType t u -> GenType t u'
709 instantiateUsage = panic "instantiateUsage: not implemented"
714 Removes just the top level of any abbreviations.
717 expandTy :: Type -> Type -- Restricted to Type due to Dict expansion
719 expandTy (FunTy t1 t2 u) = AppTy (AppTy (TyConTy mkFunTyCon u) t1) t2
720 expandTy (SynTy _ _ t) = expandTy t
721 expandTy (DictTy clas ty u)
722 = case all_arg_tys of
724 [] -> voidTy -- Empty dictionary represented by Void
726 [arg_ty] -> expandTy arg_ty -- just the <whatever> itself
728 -- The extra expandTy is to make sure that
729 -- the result isn't still a dict, which it might be
730 -- if the original guy was a dict with one superdict and
733 other -> ASSERT(not (null all_arg_tys))
734 foldl AppTy (TyConTy (tupleTyCon (length all_arg_tys)) u) all_arg_tys
737 -- Note: length of all_arg_tys can be 0 if the class is
738 -- CCallable, CReturnable (and anything else
739 -- *really weird* that the user writes).
741 all_arg_tys = classDictArgTys clas ty
746 At present there are no unboxed non-primitive types, so
747 isUnboxedType is the same as isPrimType.
749 We're a bit cavalier about finding out whether something is
750 primitive/unboxed or not. Rather than deal with the type
751 arguemnts we just zoom into the function part of the type.
752 That is, given (T a) we just recurse into the "T" part,
756 isPrimType, isUnboxedType :: Type -> Bool
758 isPrimType (AppTy ty _) = isPrimType ty
759 isPrimType (SynTy _ _ ty) = isPrimType ty
760 isPrimType (TyConTy tycon _) = case maybeNewTyCon tycon of
761 Just (tyvars, ty) -> isPrimType ty
762 Nothing -> isPrimTyCon tycon
766 isUnboxedType = isPrimType
769 This is *not* right: it is a placeholder (ToDo 96/03 WDP):
771 typePrimRep :: Type -> PrimRep
773 typePrimRep (SynTy _ _ ty) = typePrimRep ty
774 typePrimRep (AppTy ty _) = typePrimRep ty
775 typePrimRep (TyConTy tc _)
776 | isPrimTyCon tc = case (assocMaybe tc_primrep_list (uniqueOf tc)) of
778 Nothing -> panic "Type.typePrimRep" -- pprPanic "typePrimRep:" (pprTyCon PprDebug tc)
780 | otherwise = case maybeNewTyCon tc of
781 Just (tyvars, ty) | isPrimType ty -> typePrimRep ty
782 _ -> PtrRep -- Default
784 typePrimRep _ = PtrRep -- the "default"
787 = [(addrPrimTyConKey, AddrRep)
788 ,(arrayPrimTyConKey, ArrayRep)
789 ,(byteArrayPrimTyConKey, ByteArrayRep)
790 ,(charPrimTyConKey, CharRep)
791 ,(doublePrimTyConKey, DoubleRep)
792 ,(floatPrimTyConKey, FloatRep)
793 ,(foreignObjPrimTyConKey, ForeignObjRep)
794 ,(intPrimTyConKey, IntRep)
795 ,(mutableArrayPrimTyConKey, ArrayRep)
796 ,(mutableByteArrayPrimTyConKey, ByteArrayRep)
797 ,(stablePtrPrimTyConKey, StablePtrRep)
798 ,(statePrimTyConKey, VoidRep)
799 ,(synchVarPrimTyConKey, PtrRep)
800 ,(voidTyConKey, PtrRep) -- Not VoidRep! That's just for Void#
801 -- The type Void is represented by a pointer to
803 ,(wordPrimTyConKey, WordRep)
807 %************************************************************************
809 \subsection{Matching on types}
811 %************************************************************************
813 Matching is a {\em unidirectional} process, matching a type against a
814 template (which is just a type with type variables in it). The
815 matcher assumes that there are no repeated type variables in the
816 template, so that it simply returns a mapping of type variables to
817 types. It also fails on nested foralls.
819 @matchTys@ matches corresponding elements of a list of templates and
823 matchTy :: GenType t1 u1 -- Template
824 -> GenType t2 u2 -- Proposed instance of template
825 -> Maybe [(t1,GenType t2 u2)] -- Matching substitution
828 matchTys :: [GenType t1 u1] -- Templates
829 -> [GenType t2 u2] -- Proposed instance of template
830 -> Maybe ([(t1,GenType t2 u2)],-- Matching substitution
831 [GenType t2 u2]) -- Left over instance types
833 matchTy ty1 ty2 = match ty1 ty2 (\s -> Just s) []
834 matchTys tys1 tys2 = go [] tys1 tys2
836 go s [] tys2 = Just (s,tys2)
837 go s (ty1:tys1) [] = trace "matchTys" Nothing
838 go s (ty1:tys1) (ty2:tys2) = match ty1 ty2 (\s' -> go s' tys1 tys2) s
841 @match@ is the main function.
844 match :: GenType t1 u1 -> GenType t2 u2 -- Current match pair
845 -> ([(t1, GenType t2 u2)] -> Maybe result) -- Continuation
846 -> [(t1, GenType t2 u2)] -- Current substitution
849 match (TyVarTy v) ty k = \s -> k ((v,ty) : s)
850 match (FunTy fun1 arg1 _) (FunTy fun2 arg2 _) k = match fun1 fun2 (match arg1 arg2 k)
851 match (AppTy fun1 arg1) (AppTy fun2 arg2) k = match fun1 fun2 (match arg1 arg2 k)
852 match (TyConTy con1 _) (TyConTy con2 _) k | con1 == con2 = k
853 match (DictTy clas1 ty1 _) (DictTy clas2 ty2 _) k | clas1 == clas2 = match ty1 ty2 k
854 match (SynTy _ _ ty1) ty2 k = match ty1 ty2 k
855 match ty1 (SynTy _ _ ty2) k = match ty1 ty2 k
857 -- With type synonyms, we have to be careful for the exact
858 -- same reasons as in the unifier. Please see the
859 -- considerable commentary there before changing anything
863 match _ _ _ = \s -> Nothing
866 %************************************************************************
868 \subsection{Equality on types}
870 %************************************************************************
872 The functions eqSimpleTy and eqSimpleTheta are polymorphic in the types t
873 and u, but ONLY WORK FOR SIMPLE TYPES (ie. they panic if they see
874 dictionaries or polymorphic types). The function eqTy has a more
875 specific type, but does the `right thing' for all types.
878 eqSimpleTheta :: (Eq t,Eq u) =>
879 [(Class,GenType t u)] -> [(Class,GenType t u)] -> Bool
881 eqSimpleTheta [] [] = True
882 eqSimpleTheta ((c1,t1):th1) ((c2,t2):th2) =
883 c1==c2 && t1 `eqSimpleTy` t2 && th1 `eqSimpleTheta` th2
884 eqSimpleTheta other1 other2 = False
888 eqSimpleTy :: (Eq t,Eq u) => GenType t u -> GenType t u -> Bool
890 (TyVarTy tv1) `eqSimpleTy` (TyVarTy tv2) =
892 (AppTy f1 a1) `eqSimpleTy` (AppTy f2 a2) =
893 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2
894 (TyConTy tc1 u1) `eqSimpleTy` (TyConTy tc2 u2) =
895 tc1 == tc2 --ToDo: later: && u1 == u2
897 (FunTy f1 a1 u1) `eqSimpleTy` (FunTy f2 a2 u2) =
898 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2 && u1 == u2
899 (FunTy f1 a1 u1) `eqSimpleTy` t2 =
900 -- Expand t1 just in case t2 matches that version
901 (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) `eqSimpleTy` t2
902 t1 `eqSimpleTy` (FunTy f2 a2 u2) =
903 -- Expand t2 just in case t1 matches that version
904 t1 `eqSimpleTy` (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
906 (SynTy tc1 ts1 t1) `eqSimpleTy` (SynTy tc2 ts2 t2) =
907 (tc1 == tc2 && and (zipWith eqSimpleTy ts1 ts2) && length ts1 == length ts2)
908 || t1 `eqSimpleTy` t2
909 (SynTy _ _ t1) `eqSimpleTy` t2 =
910 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
911 t1 `eqSimpleTy` (SynTy _ _ t2) =
912 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
914 (DictTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got DictTy"
915 _ `eqSimpleTy` (DictTy _ _ _) = panic "eqSimpleTy: got DictTy"
917 (ForAllTy _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllTy"
918 _ `eqSimpleTy` (ForAllTy _ _) = panic "eqSimpleTy: got ForAllTy"
920 (ForAllUsageTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllUsageTy"
921 _ `eqSimpleTy` (ForAllUsageTy _ _ _) = panic "eqSimpleTy: got ForAllUsageTy"
923 _ `eqSimpleTy` _ = False
926 Types are ordered so we can sort on types in the renamer etc. DNT: Since
927 this class is also used in CoreLint and other such places, we DO expand out
928 Fun/Syn/Dict types (if necessary).
931 eqTy :: Type -> Type -> Bool
934 eq nullTyVarEnv nullUVarEnv t1 t2
936 eq tve uve (TyVarTy tv1) (TyVarTy tv2) =
938 case (lookupTyVarEnv tve tv1) of
941 eq tve uve (AppTy f1 a1) (AppTy f2 a2) =
942 eq tve uve f1 f2 && eq tve uve a1 a2
943 eq tve uve (TyConTy tc1 u1) (TyConTy tc2 u2) =
944 tc1 == tc2 -- ToDo: LATER: && eqUsage uve u1 u2
946 eq tve uve (FunTy f1 a1 u1) (FunTy f2 a2 u2) =
947 eq tve uve f1 f2 && eq tve uve a1 a2 && eqUsage uve u1 u2
948 eq tve uve (FunTy f1 a1 u1) t2 =
949 -- Expand t1 just in case t2 matches that version
950 eq tve uve (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) t2
951 eq tve uve t1 (FunTy f2 a2 u2) =
952 -- Expand t2 just in case t1 matches that version
953 eq tve uve t1 (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
955 eq tve uve (DictTy c1 t1 u1) (DictTy c2 t2 u2)
957 = eq tve uve t1 t2 && eqUsage uve u1 u2
958 -- NB we use a guard for c1==c2 so that if they aren't equal we
959 -- fall through into expanding the type. Why? Because brain-dead
960 -- people might write
961 -- class Foo a => Baz a where {}
962 -- and that means that a Foo dictionary and a Baz dictionary are identical
963 -- Sigh. Let's hope we don't spend too much time in here!
965 eq tve uve t1@(DictTy _ _ _) t2 =
966 eq tve uve (expandTy t1) t2 -- Expand the dictionary and try again
967 eq tve uve t1 t2@(DictTy _ _ _) =
968 eq tve uve t1 (expandTy t2) -- Expand the dictionary and try again
970 eq tve uve (SynTy tc1 ts1 t1) (SynTy tc2 ts2 t2) =
971 (tc1 == tc2 && and (zipWith (eq tve uve) ts1 ts2) && length ts1 == length ts2)
973 eq tve uve (SynTy _ _ t1) t2 =
974 eq tve uve t1 t2 -- Expand the abbrevation and try again
975 eq tve uve t1 (SynTy _ _ t2) =
976 eq tve uve t1 t2 -- Expand the abbrevation and try again
978 eq tve uve (ForAllTy tv1 t1) (ForAllTy tv2 t2) =
979 eq (addOneToTyVarEnv tve tv1 tv2) uve t1 t2
980 eq tve uve (ForAllUsageTy u1 b1 t1) (ForAllUsageTy u2 b2 t2) =
981 eqBounds uve b1 b2 && eq tve (addOneToUVarEnv uve u1 u2) t1 t2
985 eqBounds uve [] [] = True
986 eqBounds uve (u1:b1) (u2:b2) = eqUVar uve u1 u2 && eqBounds uve b1 b2
987 eqBounds uve _ _ = False
991 showTypeCategory :: Type -> Char
993 {C,I,F,D} char, int, float, double
995 S other single-constructor type
996 {c,i,f,d} unboxed ditto
998 s *unpacked" single-cons...
1004 + dictionary, unless it's a ...
1007 M other (multi-constructor) data-con type
1009 - reserved for others to mark as "uninteresting"
1015 case getTyCon_maybe ty of
1016 Nothing -> if maybeToBool (getFunTy_maybe ty)
1021 let utc = uniqueOf tycon in
1022 if utc == charDataConKey then 'C'
1023 else if utc == intDataConKey then 'I'
1024 else if utc == floatDataConKey then 'F'
1025 else if utc == doubleDataConKey then 'D'
1026 else if utc == integerDataConKey then 'J'
1027 else if utc == charPrimTyConKey then 'c'
1028 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
1029 || utc == addrPrimTyConKey) then 'i'
1030 else if utc == floatPrimTyConKey then 'f'
1031 else if utc == doublePrimTyConKey then 'd'
1032 else if isPrimTyCon tycon {- array, we hope -} then 'A'
1033 else if isEnumerationTyCon tycon then 'E'
1034 else if isTupleTyCon tycon then 'T'
1035 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
1036 else if utc == listTyConKey then 'L'
1037 else 'M' -- oh, well...