2 #include "HsVersions.h"
5 GenType(..), Type(..), TauType(..),
7 getTyVar, getTyVar_maybe, isTyVarTy,
8 mkAppTy, mkAppTys, splitAppTy,
9 mkFunTy, mkFunTys, splitFunTy, getFunTy_maybe,
10 mkTyConTy, getTyCon_maybe, applyTyCon,
12 mkForAllTy, mkForAllTys, getForAllTy_maybe, splitForAllTy,
13 mkForAllUsageTy, getForAllUsageTy,
18 RhoType(..), SigmaType(..), ThetaType(..),
21 mkSigmaTy, splitSigmaTy,
23 maybeAppTyCon, getAppTyCon,
24 maybeAppDataTyCon, getAppDataTyCon,
27 matchTy, matchTys, eqTy, eqSimpleTy, eqSimpleTheta,
29 instantiateTy,instantiateUsage,
33 tyVarsOfType, tyVarsOfTypes, getTypeKind
39 import IdLoop -- for paranoia checking
40 import TyLoop -- for paranoia checking
41 import PrelLoop -- for paranoia checking
44 import Class ( getClassSig, getClassOpLocalType, GenClass{-instances-} )
45 import Kind ( mkBoxedTypeKind, resultKind )
46 import TyCon ( mkFunTyCon, mkTupleTyCon, isFunTyCon, isPrimTyCon,
47 getTyConKind, getTyConDataCons, TyCon )
48 import TyVar ( getTyVarKind, GenTyVar{-instances-}, GenTyVarSet(..),
49 emptyTyVarSet, unionTyVarSets, minusTyVarSet,
50 singletonTyVarSet, nullTyVarEnv, lookupTyVarEnv,
51 addOneToTyVarEnv, TyVarEnv(..) )
52 import Usage ( usageOmega, GenUsage, Usage(..), UVar(..), UVarEnv(..),
53 nullUVarEnv, addOneToUVarEnv, lookupUVarEnv, eqUVar,
57 import Util ( thenCmp, zipEqual, panic, panic#, assertPanic,
66 type Type = GenType TyVar UVar -- Used after typechecker
68 data GenType tyvar uvar -- Parameterised over type and usage variables
75 | TyConTy -- Constants of a specified kind
77 (GenUsage uvar) -- Usage gives uvar of the full application,
78 -- iff the full application is of kind Type
79 -- c.f. the Usage field in TyVars
81 | SynTy -- Synonyms must be saturated, and contain their expansion
82 TyCon -- Must be a SynTyCon
84 (GenType tyvar uvar) -- Expansion!
88 (GenType tyvar uvar) -- TypeKind
91 uvar -- Quantify over this
92 [uvar] -- Bounds; the quantified var must be
93 -- less than or equal to all these
96 -- Two special cases that save a *lot* of administrative
99 | FunTy -- BoxedTypeKind
100 (GenType tyvar uvar) -- Both args are of TypeKind
106 (GenType tyvar uvar) -- Arg has kind TypeKind
113 type ThetaType = [(Class, Type)]
114 type SigmaType = Type
120 Removes just the top level of any abbreviations.
123 expandTy :: Type -> Type -- Restricted to Type due to Dict expansion
125 expandTy (FunTy t1 t2 u) = AppTy (AppTy (TyConTy mkFunTyCon u) t1) t2
126 expandTy (SynTy _ _ t) = expandTy t
127 expandTy (DictTy clas ty u)
128 = case all_arg_tys of
130 [arg_ty] -> expandTy arg_ty -- just the <whatever> itself
132 -- The extra expandTy is to make sure that
133 -- the result isn't still a dict, which it might be
134 -- if the original guy was a dict with one superdict and
137 other -> ASSERT(not (null all_arg_tys))
138 foldl AppTy (TyConTy (mkTupleTyCon (length all_arg_tys)) u) all_arg_tys
141 -- Note: length of all_arg_tys can be 0 if the class is
142 -- _CCallable, _CReturnable (and anything else
143 -- *really weird* that the user writes).
145 (tyvar, super_classes, ops) = getClassSig clas
146 super_dict_tys = map mk_super_ty super_classes
147 class_op_tys = map mk_op_ty ops
148 all_arg_tys = super_dict_tys ++ class_op_tys
149 mk_super_ty sc = DictTy sc ty usageOmega
150 mk_op_ty op = instantiateTy [(tyvar,ty)] (getClassOpLocalType op)
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 t = go t []
188 go (AppTy t arg) ts = go t (arg:ts)
189 go (FunTy fun arg u) ts = (TyConTy mkFunTyCon u, fun:arg:ts)
190 go (SynTy _ _ t) ts = go t ts
195 -- NB mkFunTy, mkFunTys puts in Omega usages, for now at least
196 mkFunTy arg res = FunTy arg res usageOmega
198 mkFunTys :: [GenType t u] -> GenType t u -> GenType t u
199 mkFunTys ts t = foldr (\ f a -> FunTy f a usageOmega) t ts
201 getFunTy_maybe :: GenType t u -> Maybe (GenType t u, GenType t u)
202 getFunTy_maybe (FunTy arg result _) = Just (arg,result)
203 getFunTy_maybe (AppTy (AppTy (TyConTy tycon _) arg) res)
204 | isFunTyCon tycon = Just (arg, res)
205 getFunTy_maybe (SynTy _ _ t) = getFunTy_maybe t
206 getFunTy_maybe other = Nothing
208 splitFunTy :: GenType t u -> ([GenType t u], GenType t u)
209 splitFunTy t = go t []
211 go (FunTy arg res _) ts = go res (arg:ts)
212 go (AppTy (AppTy (TyConTy tycon _) arg) res) ts
222 -- NB applyTyCon puts in usageOmega, for now at least
223 mkTyConTy tycon = TyConTy tycon usageOmega
225 applyTyCon :: TyCon -> [GenType t u] -> GenType t u
226 applyTyCon tycon tys = foldl AppTy (TyConTy tycon usageOmega) tys
228 getTyCon_maybe :: GenType t u -> Maybe TyCon
229 getTyCon_maybe (TyConTy tycon _) = Just tycon
230 getTyCon_maybe (SynTy _ _ t) = getTyCon_maybe t
231 getTyCon_maybe other_ty = Nothing
235 mkSynTy syn_tycon tys
236 = SynTy syn_tycon tys (panic "Type.mkSynTy:expansion")
242 isTauTy :: GenType t u -> Bool
243 isTauTy (TyVarTy v) = True
244 isTauTy (TyConTy _ _) = True
245 isTauTy (AppTy a b) = isTauTy a && isTauTy b
246 isTauTy (FunTy a b _) = isTauTy a && isTauTy b
247 isTauTy (SynTy _ _ ty) = isTauTy ty
248 isTauTy other = False
253 NB mkRhoTy and mkDictTy put in usageOmega, for now at least
256 mkDictTy :: Class -> GenType t u -> GenType t u
257 mkDictTy clas ty = DictTy clas ty usageOmega
259 mkRhoTy :: [(Class, GenType t u)] -> GenType t u -> GenType t u
261 foldr (\(c,t) r -> FunTy (DictTy c t usageOmega) r usageOmega) ty theta
263 splitRhoTy :: GenType t u -> ([(Class,GenType t u)], GenType t u)
267 go (FunTy (DictTy c t _) r _) ts = go r ((c,t):ts)
268 go (AppTy (AppTy (TyConTy tycon _) (DictTy c t _)) r) ts
271 go (SynTy _ _ t) ts = go t ts
272 go t ts = (reverse ts, t)
279 mkForAllTy = ForAllTy
281 mkForAllTys :: [t] -> GenType t u -> GenType t u
282 mkForAllTys tyvars ty = foldr ForAllTy ty tyvars
284 getForAllTy_maybe :: GenType t u -> Maybe (t,GenType t u)
285 getForAllTy_maybe (SynTy _ _ t) = getForAllTy_maybe t
286 getForAllTy_maybe (ForAllTy tyvar t) = Just(tyvar,t)
287 getForAllTy_maybe _ = Nothing
289 splitForAllTy :: GenType t u-> ([t], GenType t u)
290 splitForAllTy t = go t []
292 go (ForAllTy tv t) tvs = go t (tv:tvs)
293 go (SynTy _ _ t) tvs = go t tvs
294 go t tvs = (reverse tvs, t)
298 mkForAllUsageTy :: u -> [u] -> GenType t u -> GenType t u
299 mkForAllUsageTy = ForAllUsageTy
301 getForAllUsageTy :: GenType t u -> Maybe (u,[u],GenType t u)
302 getForAllUsageTy (ForAllUsageTy uvar bounds t) = Just(uvar,bounds,t)
303 getForAllUsageTy (SynTy _ _ t) = getForAllUsageTy t
304 getForAllUsageTy _ = Nothing
307 Applied tycons (includes FunTyCons)
308 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
311 :: GenType tyvar uvar
312 -> Maybe (TyCon, -- the type constructor
313 [GenType tyvar uvar]) -- types to which it is applied
316 = case (getTyCon_maybe app_ty) of
318 Just tycon -> Just (tycon, arg_tys)
320 (app_ty, arg_tys) = splitAppTy ty
324 :: GenType tyvar uvar
325 -> (TyCon, -- the type constructor
326 [GenType tyvar uvar]) -- types to which it is applied
329 = case maybeAppTyCon ty of
332 Nothing -> panic "Type.getAppTyCon" -- (ppr PprShowAll ty)
336 Applied data tycons (give back constrs)
337 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340 :: GenType tyvar uvar
341 -> Maybe (TyCon, -- the type constructor
342 [GenType tyvar uvar], -- types to which it is applied
343 [Id]) -- its family of data-constructors
346 = case (getTyCon_maybe app_ty) of
348 Just tycon | isFunTyCon tycon
351 -> Just (tycon, arg_tys, getTyConDataCons tycon)
353 (app_ty, arg_tys) = splitAppTy ty
357 :: GenType tyvar uvar
358 -> (TyCon, -- the type constructor
359 [GenType tyvar uvar], -- types to which it is applied
360 [Id]) -- its family of data-constructors
363 = case maybeAppDataTyCon ty of
366 Nothing -> panic "Type.getAppDataTyCon" -- (ppr PprShowAll ty)
370 maybeBoxedPrimType :: Type -> Maybe (Id, Type)
372 maybeBoxedPrimType ty
373 = case (maybeAppDataTyCon ty) of -- Data type,
374 Just (tycon, tys_applied, [data_con]) -- with exactly one constructor
375 -> case (getInstantiatedDataConSig data_con tys_applied) of
376 (_, [data_con_arg_ty], _) -- Applied to exactly one type,
377 | isPrimType data_con_arg_ty -- which is primitive
378 -> Just (data_con, data_con_arg_ty)
379 other_cases -> Nothing
380 other_cases -> Nothing
384 splitSigmaTy :: GenType t u -> ([t], [(Class,GenType t u)], GenType t u)
388 (tyvars,rho) = splitForAllTy ty
389 (theta,tau) = splitRhoTy rho
391 mkSigmaTy tyvars theta tau = mkForAllTys tyvars (mkRhoTy theta tau)
395 Finding the kind of a type
396 ~~~~~~~~~~~~~~~~~~~~~~~~~~
398 getTypeKind :: GenType (GenTyVar any) u -> Kind
399 getTypeKind (TyVarTy tyvar) = getTyVarKind tyvar
400 getTypeKind (TyConTy tycon usage) = getTyConKind tycon
401 getTypeKind (SynTy _ _ ty) = getTypeKind ty
402 getTypeKind (FunTy fun arg _) = mkBoxedTypeKind
403 getTypeKind (DictTy clas arg _) = mkBoxedTypeKind
404 getTypeKind (AppTy fun arg) = resultKind (getTypeKind fun)
405 getTypeKind (ForAllTy _ _) = mkBoxedTypeKind
406 getTypeKind (ForAllUsageTy _ _ _) = mkBoxedTypeKind
410 Free variables of a type
411 ~~~~~~~~~~~~~~~~~~~~~~~~
413 tyVarsOfType :: GenType (GenTyVar flexi) uvar -> GenTyVarSet flexi
415 tyVarsOfType (TyVarTy tv) = singletonTyVarSet tv
416 tyVarsOfType (TyConTy tycon usage) = emptyTyVarSet
417 tyVarsOfType (SynTy _ tys ty) = tyVarsOfTypes tys
418 tyVarsOfType (FunTy arg res _) = tyVarsOfType arg `unionTyVarSets` tyVarsOfType res
419 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionTyVarSets` tyVarsOfType arg
420 tyVarsOfType (DictTy clas ty _) = tyVarsOfType ty
421 tyVarsOfType (ForAllTy tyvar ty) = tyVarsOfType ty `minusTyVarSet` singletonTyVarSet tyvar
422 tyVarsOfType (ForAllUsageTy _ _ ty) = tyVarsOfType ty
424 tyVarsOfTypes :: [GenType (GenTyVar flexi) uvar] -> GenTyVarSet flexi
425 tyVarsOfTypes tys = foldr (unionTyVarSets.tyVarsOfType) emptyTyVarSet tys
432 applyTy :: Eq t => GenType t u -> GenType t u -> GenType t u
433 applyTy (SynTy _ _ fun) arg = applyTy fun arg
434 applyTy (ForAllTy tv ty) arg = instantiateTy [(tv,arg)] ty
435 applyTy other arg = panic "applyTy"
437 instantiateTy :: Eq t => [(t, GenType t u)] -> GenType t u -> GenType t u
438 instantiateTy tenv ty
441 go (TyVarTy tv) = case [ty | (tv',ty) <- tenv, tv==tv'] of
444 go ty@(TyConTy tycon usage) = ty
445 go (SynTy tycon tys ty) = SynTy tycon (map go tys) (go ty)
446 go (FunTy arg res usage) = FunTy (go arg) (go res) usage
447 go (AppTy fun arg) = AppTy (go fun) (go arg)
448 go (DictTy clas ty usage) = DictTy clas (go ty) usage
449 go (ForAllTy tv ty) = ASSERT(null tv_bound)
452 tv_bound = [() | (tv',_) <- tenv, tv==tv']
454 go (ForAllUsageTy uvar bds ty) = ForAllUsageTy uvar bds (go ty)
457 :: Ord3 u => [(u, GenType t u')] -> GenType t u -> GenType t u'
458 instantiateUsage = error "instantiateUsage: not implemented"
462 isPrimType :: GenType tyvar uvar -> Bool
463 isPrimType (AppTy ty _) = isPrimType ty
464 isPrimType (SynTy _ _ ty) = isPrimType ty
465 isPrimType (TyConTy tycon _) = isPrimTyCon tycon
469 %************************************************************************
471 \subsection{Matching on types}
473 %************************************************************************
475 Matching is a {\em unidirectional} process, matching a type against a
476 template (which is just a type with type variables in it). The
477 matcher assumes that there are no repeated type variables in the
478 template, so that it simply returns a mapping of type variables to
479 types. It also fails on nested foralls.
481 @matchTys@ matches corresponding elements of a list of templates and
485 matchTy :: GenType t1 u1 -- Template
486 -> GenType t2 u2 -- Proposed instance of template
487 -> Maybe [(t1,GenType t2 u2)] -- Matching substitution
489 matchTys :: [GenType t1 u1] -- Templates
490 -> [GenType t2 u2] -- Proposed instance of template
491 -> Maybe [(t1,GenType t2 u2)] -- Matching substitution
493 matchTy ty1 ty2 = match [] [] ty1 ty2
494 matchTys tys1 tys2 = match' [] (zipEqual tys1 tys2)
497 @match@ is the main function.
500 match :: [(t1, GenType t2 u2)] -- r, the accumulating result
501 -> [(GenType t1 u1, GenType t2 u2)] -- w, the work list
502 -> GenType t1 u1 -> GenType t2 u2 -- Current match pair
503 -> Maybe [(t1, GenType t2 u2)]
505 match r w (TyVarTy v) ty = match' ((v,ty) : r) w
506 match r w (FunTy fun1 arg1 _) (FunTy fun2 arg2 _) = match r ((fun1,fun2):w) arg1 arg2
507 match r w (AppTy fun1 arg1) (AppTy fun2 arg2) = match r ((fun1,fun2):w) arg1 arg2
508 match r w (TyConTy con1 _) (TyConTy con2 _) | con1 == con2 = match' r w
509 match r w (DictTy clas1 ty1 _) (DictTy clas2 ty2 _) | clas1 == clas2 = match r w ty1 ty2
510 match r w (SynTy _ _ ty1) ty2 = match r w ty1 ty2
511 match r w ty1 (SynTy _ _ ty2) = match r w ty1 ty2
513 -- With type synonyms, we have to be careful for the exact
514 -- same reasons as in the unifier. Please see the
515 -- considerable commentary there before changing anything
519 match _ _ _ _ = Nothing
522 match' r ((ty1,ty2):w) = match r w ty1 ty2
525 %************************************************************************
527 \subsection{Equality on types}
529 %************************************************************************
531 The functions eqSimpleTy and eqSimpleTheta are polymorphic in the types t
532 and u, but ONLY WORK FOR SIMPLE TYPES (ie. they panic if they see
533 dictionaries or polymorphic types). The function eqTy has a more
534 specific type, but does the `right thing' for all types.
537 eqSimpleTheta :: (Eq t,Eq u) =>
538 [(Class,GenType t u)] -> [(Class,GenType t u)] -> Bool
540 eqSimpleTheta [] [] = True
541 eqSimpleTheta ((c1,t1):th1) ((c2,t2):th2) =
542 c1==c2 && t1 `eqSimpleTy` t2 && th1 `eqSimpleTheta` th2
543 eqSimpleTheta other1 other2 = False
547 eqSimpleTy :: (Eq t,Eq u) => GenType t u -> GenType t u -> Bool
549 (TyVarTy tv1) `eqSimpleTy` (TyVarTy tv2) =
551 (AppTy f1 a1) `eqSimpleTy` (AppTy f2 a2) =
552 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2
553 (TyConTy tc1 u1) `eqSimpleTy` (TyConTy tc2 u2) =
554 tc1 == tc2 && u1 == u2
556 (FunTy f1 a1 u1) `eqSimpleTy` (FunTy f2 a2 u2) =
557 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2 && u1 == u2
558 (FunTy f1 a1 u1) `eqSimpleTy` t2 =
559 -- Expand t1 just in case t2 matches that version
560 (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) `eqSimpleTy` t2
561 t1 `eqSimpleTy` (FunTy f2 a2 u2) =
562 -- Expand t2 just in case t1 matches that version
563 t1 `eqSimpleTy` (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
565 (SynTy tc1 ts1 t1) `eqSimpleTy` (SynTy tc2 ts2 t2) =
566 (tc1 == tc2 && and (zipWith eqSimpleTy ts1 ts2) && length ts1 == length ts2)
567 || t1 `eqSimpleTy` t2
568 (SynTy _ _ t1) `eqSimpleTy` t2 =
569 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
570 t1 `eqSimpleTy` (SynTy _ _ t2) =
571 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
573 (DictTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got DictTy"
574 _ `eqSimpleTy` (DictTy _ _ _) = panic "eqSimpleTy: got DictTy"
576 (ForAllTy _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllTy"
577 _ `eqSimpleTy` (ForAllTy _ _) = panic "eqSimpleTy: got ForAllTy"
579 (ForAllUsageTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllUsageTy"
580 _ `eqSimpleTy` (ForAllUsageTy _ _ _) = panic "eqSimpleTy: got ForAllUsageTy"
582 _ `eqSimpleTy` _ = False
585 Types are ordered so we can sort on types in the renamer etc. DNT: Since
586 this class is also used in CoreLint and other such places, we DO expand out
587 Fun/Syn/Dict types (if necessary).
590 eqTy :: Type -> Type -> Bool
593 eq nullTyVarEnv nullUVarEnv t1 t2
595 eq tve uve (TyVarTy tv1) (TyVarTy tv2) =
597 case (lookupTyVarEnv tve tv1) of
600 eq tve uve (AppTy f1 a1) (AppTy f2 a2) =
601 eq tve uve f1 f2 && eq tve uve a1 a2
602 eq tve uve (TyConTy tc1 u1) (TyConTy tc2 u2) =
603 tc1 == tc2 && eqUsage uve u1 u2
605 eq tve uve (FunTy f1 a1 u1) (FunTy f2 a2 u2) =
606 eq tve uve f1 f2 && eq tve uve a1 a2 && eqUsage uve u1 u2
607 eq tve uve (FunTy f1 a1 u1) t2 =
608 -- Expand t1 just in case t2 matches that version
609 eq tve uve (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) t2
610 eq tve uve t1 (FunTy f2 a2 u2) =
611 -- Expand t2 just in case t1 matches that version
612 eq tve uve t1 (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
614 eq tve uve (DictTy c1 t1 u1) (DictTy c2 t2 u2) =
615 c1 == c2 && eq tve uve t1 t2 && eqUsage uve u1 u2
616 eq tve uve t1@(DictTy _ _ _) t2 =
617 eq tve uve (expandTy t1) t2 -- Expand the dictionary and try again
618 eq tve uve t1 t2@(DictTy _ _ _) =
619 eq tve uve t1 (expandTy t2) -- Expand the dictionary and try again
621 eq tve uve (SynTy tc1 ts1 t1) (SynTy tc2 ts2 t2) =
622 (tc1 == tc2 && and (zipWith (eq tve uve) ts1 ts2) && length ts1 == length ts2)
624 eq tve uve (SynTy _ _ t1) t2 =
625 eq tve uve t1 t2 -- Expand the abbrevation and try again
626 eq tve uve t1 (SynTy _ _ t2) =
627 eq tve uve t1 t2 -- Expand the abbrevation and try again
629 eq tve uve (ForAllTy tv1 t1) (ForAllTy tv2 t2) =
630 eq (addOneToTyVarEnv tve tv1 tv2) uve t1 t2
631 eq tve uve (ForAllUsageTy u1 b1 t1) (ForAllUsageTy u2 b2 t2) =
632 eqBounds uve b1 b2 && eq tve (addOneToUVarEnv uve u1 u2) t1 t2
636 eqBounds uve [] [] = True
637 eqBounds uve (u1:b1) (u2:b2) = eqUVar uve u1 u2 && eqBounds uve b1 b2
638 eqBounds uve _ _ = False