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,
16 isPrimType, isUnboxedType, typePrimRep,
18 RhoType(..), SigmaType(..), ThetaType(..),
21 mkSigmaTy, splitSigmaTy,
23 maybeAppTyCon, getAppTyCon,
24 maybeAppDataTyCon, getAppDataTyCon,
27 matchTy, matchTys, eqTy, eqSimpleTy, eqSimpleTheta,
29 instantiateTy, instantiateTauTy, instantiateUsage,
34 tyVarsOfType, tyVarsOfTypes, getTypeKind
40 import IdLoop -- for paranoia checking
41 import TyLoop -- for paranoia checking
42 import PrelLoop -- for paranoia checking
45 --import PprType ( pprGenType ) -- ToDo: rm
46 --import PprStyle ( PprStyle(..) )
47 --import Util ( pprPanic )
50 import Class ( getClassSig, getClassOpLocalType, GenClass{-instances-} )
51 import Kind ( mkBoxedTypeKind, resultKind )
52 import TyCon ( mkFunTyCon, mkTupleTyCon, isFunTyCon, isPrimTyCon, isDataTyCon, tyConArity,
53 tyConKind, tyConDataCons, getSynTyConDefn, TyCon )
54 import TyVar ( getTyVarKind, GenTyVar{-instances-}, GenTyVarSet(..),
55 emptyTyVarSet, unionTyVarSets, minusTyVarSet,
56 unitTyVarSet, nullTyVarEnv, lookupTyVarEnv,
57 addOneToTyVarEnv, TyVarEnv(..) )
58 import Usage ( usageOmega, GenUsage, Usage(..), UVar(..), UVarEnv(..),
59 nullUVarEnv, addOneToUVarEnv, lookupUVarEnv, eqUVar,
63 import PrimRep ( PrimRep(..) )
64 import Util ( thenCmp, zipEqual, panic, panic#, assertPanic,
73 type Type = GenType TyVar UVar -- Used after typechecker
75 data GenType tyvar uvar -- Parameterised over type and usage variables
82 | TyConTy -- Constants of a specified kind
84 (GenUsage uvar) -- Usage gives uvar of the full application,
85 -- iff the full application is of kind Type
86 -- c.f. the Usage field in TyVars
88 | SynTy -- Synonyms must be saturated, and contain their expansion
89 TyCon -- Must be a SynTyCon
91 (GenType tyvar uvar) -- Expansion!
95 (GenType tyvar uvar) -- TypeKind
98 uvar -- Quantify over this
99 [uvar] -- Bounds; the quantified var must be
100 -- less than or equal to all these
103 -- Two special cases that save a *lot* of administrative
106 | FunTy -- BoxedTypeKind
107 (GenType tyvar uvar) -- Both args are of TypeKind
113 (GenType tyvar uvar) -- Arg has kind TypeKind
120 type ThetaType = [(Class, Type)]
121 type SigmaType = Type
127 Removes just the top level of any abbreviations.
130 expandTy :: Type -> Type -- Restricted to Type due to Dict expansion
132 expandTy (FunTy t1 t2 u) = AppTy (AppTy (TyConTy mkFunTyCon u) t1) t2
133 expandTy (SynTy _ _ t) = expandTy t
134 expandTy (DictTy clas ty u)
135 = case all_arg_tys of
137 [arg_ty] -> expandTy arg_ty -- just the <whatever> itself
139 -- The extra expandTy is to make sure that
140 -- the result isn't still a dict, which it might be
141 -- if the original guy was a dict with one superdict and
144 other -> ASSERT(not (null all_arg_tys))
145 foldl AppTy (TyConTy (mkTupleTyCon (length all_arg_tys)) u) all_arg_tys
148 -- Note: length of all_arg_tys can be 0 if the class is
149 -- _CCallable, _CReturnable (and anything else
150 -- *really weird* that the user writes).
152 (tyvar, super_classes, ops) = getClassSig clas
153 super_dict_tys = map mk_super_ty super_classes
154 class_op_tys = map mk_op_ty ops
155 all_arg_tys = super_dict_tys ++ class_op_tys
156 mk_super_ty sc = DictTy sc ty usageOmega
157 mk_op_ty op = instantiateTy [(tyvar,ty)] (getClassOpLocalType op)
162 Simple construction and analysis functions
163 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165 mkTyVarTy :: t -> GenType t u
166 mkTyVarTys :: [t] -> [GenType t y]
168 mkTyVarTys = map mkTyVarTy -- a common use of mkTyVarTy
170 getTyVar :: String -> GenType t u -> t
171 getTyVar msg (TyVarTy tv) = tv
172 getTyVar msg (SynTy _ _ t) = getTyVar msg t
173 getTyVar msg other = panic ("getTyVar: " ++ msg)
175 getTyVar_maybe :: GenType t u -> Maybe t
176 getTyVar_maybe (TyVarTy tv) = Just tv
177 getTyVar_maybe (SynTy _ _ t) = getTyVar_maybe t
178 getTyVar_maybe other = Nothing
180 isTyVarTy :: GenType t u -> Bool
181 isTyVarTy (TyVarTy tv) = True
182 isTyVarTy (SynTy _ _ t) = isTyVarTy t
183 isTyVarTy other = False
189 mkAppTys :: GenType t u -> [GenType t u] -> GenType t u
190 mkAppTys t ts = foldl AppTy t ts
192 splitAppTy :: GenType t u -> (GenType t u, [GenType t u])
193 splitAppTy t = go t []
195 go (AppTy t arg) ts = go t (arg:ts)
196 go (FunTy fun arg u) ts = (TyConTy mkFunTyCon u, fun:arg:ts)
197 go (SynTy _ _ t) ts = go t ts
202 -- NB mkFunTy, mkFunTys puts in Omega usages, for now at least
203 mkFunTy arg res = FunTy arg res usageOmega
205 mkFunTys :: [GenType t u] -> GenType t u -> GenType t u
206 mkFunTys ts t = foldr (\ f a -> FunTy f a usageOmega) t ts
208 getFunTy_maybe :: GenType t u -> Maybe (GenType t u, GenType t u)
209 getFunTy_maybe (FunTy arg result _) = Just (arg,result)
210 getFunTy_maybe (AppTy (AppTy (TyConTy tycon _) arg) res)
211 | isFunTyCon tycon = Just (arg, res)
212 getFunTy_maybe (SynTy _ _ t) = getFunTy_maybe t
213 getFunTy_maybe other = Nothing
215 splitFunTy :: GenType t u -> ([GenType t u], GenType t u)
216 splitFunTy t = go t []
218 go (FunTy arg res _) ts = go res (arg:ts)
219 go (AppTy (AppTy (TyConTy tycon _) arg) res) ts
229 -- NB applyTyCon puts in usageOmega, for now at least
230 mkTyConTy tycon = TyConTy tycon usageOmega
232 applyTyCon :: TyCon -> [GenType t u] -> GenType t u
233 applyTyCon tycon tys = foldl AppTy (TyConTy tycon usageOmega) tys
235 getTyCon_maybe :: GenType t u -> Maybe TyCon
236 getTyCon_maybe (TyConTy tycon _) = Just tycon
237 getTyCon_maybe (SynTy _ _ t) = getTyCon_maybe t
238 getTyCon_maybe other_ty = Nothing
242 mkSynTy syn_tycon tys
243 = SynTy syn_tycon tys (instantiateTauTy (zipEqual tyvars tys) body)
245 (tyvars, body) = getSynTyConDefn syn_tycon
251 isTauTy :: GenType t u -> Bool
252 isTauTy (TyVarTy v) = True
253 isTauTy (TyConTy _ _) = True
254 isTauTy (AppTy a b) = isTauTy a && isTauTy b
255 isTauTy (FunTy a b _) = isTauTy a && isTauTy b
256 isTauTy (SynTy _ _ ty) = isTauTy ty
257 isTauTy other = False
262 NB mkRhoTy and mkDictTy put in usageOmega, for now at least
265 mkDictTy :: Class -> GenType t u -> GenType t u
266 mkDictTy clas ty = DictTy clas ty usageOmega
268 mkRhoTy :: [(Class, GenType t u)] -> GenType t u -> GenType t u
270 foldr (\(c,t) r -> FunTy (DictTy c t usageOmega) r usageOmega) ty theta
272 splitRhoTy :: GenType t u -> ([(Class,GenType t u)], GenType t u)
276 go (FunTy (DictTy c t _) r _) ts = go r ((c,t):ts)
277 go (AppTy (AppTy (TyConTy tycon _) (DictTy c t _)) r) ts
280 go (SynTy _ _ t) ts = go t ts
281 go t ts = (reverse ts, t)
288 mkForAllTy = ForAllTy
290 mkForAllTys :: [t] -> GenType t u -> GenType t u
291 mkForAllTys tyvars ty = foldr ForAllTy ty tyvars
293 getForAllTy_maybe :: GenType t u -> Maybe (t,GenType t u)
294 getForAllTy_maybe (SynTy _ _ t) = getForAllTy_maybe t
295 getForAllTy_maybe (ForAllTy tyvar t) = Just(tyvar,t)
296 getForAllTy_maybe _ = Nothing
298 splitForAllTy :: GenType t u-> ([t], GenType t u)
299 splitForAllTy t = go t []
301 go (ForAllTy tv t) tvs = go t (tv:tvs)
302 go (SynTy _ _ t) tvs = go t tvs
303 go t tvs = (reverse tvs, t)
307 mkForAllUsageTy :: u -> [u] -> GenType t u -> GenType t u
308 mkForAllUsageTy = ForAllUsageTy
310 getForAllUsageTy :: GenType t u -> Maybe (u,[u],GenType t u)
311 getForAllUsageTy (ForAllUsageTy uvar bounds t) = Just(uvar,bounds,t)
312 getForAllUsageTy (SynTy _ _ t) = getForAllUsageTy t
313 getForAllUsageTy _ = Nothing
316 Applied tycons (includes FunTyCons)
317 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
320 :: GenType tyvar uvar
321 -> Maybe (TyCon, -- the type constructor
322 [GenType tyvar uvar]) -- types to which it is applied
325 = case (getTyCon_maybe app_ty) of
327 Just tycon -> Just (tycon, arg_tys)
329 (app_ty, arg_tys) = splitAppTy ty
333 :: GenType tyvar uvar
334 -> (TyCon, -- the type constructor
335 [GenType tyvar uvar]) -- types to which it is applied
338 = case maybeAppTyCon ty of
341 Nothing -> panic "Type.getAppTyCon" -- (ppr PprShowAll ty)
345 Applied data tycons (give back constrs)
346 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
349 :: GenType tyvar uvar
350 -> Maybe (TyCon, -- the type constructor
351 [GenType tyvar uvar], -- types to which it is applied
352 [Id]) -- its family of data-constructors
355 = case (getTyCon_maybe app_ty) of
356 Just tycon | isDataTyCon tycon &&
357 tyConArity tycon == length arg_tys
358 -- Must be saturated for ty to be a data type
359 -> Just (tycon, arg_tys, tyConDataCons tycon)
363 (app_ty, arg_tys) = splitAppTy ty
367 :: GenType tyvar uvar
368 -> (TyCon, -- the type constructor
369 [GenType tyvar uvar], -- types to which it is applied
370 [Id]) -- its family of data-constructors
373 = case maybeAppDataTyCon ty of
376 Nothing -> panic "Type.getAppDataTyCon: " -- (pprGenType PprShowAll ty)
380 maybeBoxedPrimType :: Type -> Maybe (Id, Type)
382 maybeBoxedPrimType ty
383 = case (maybeAppDataTyCon ty) of -- Data type,
384 Just (tycon, tys_applied, [data_con]) -- with exactly one constructor
385 -> case (dataConArgTys data_con tys_applied) of
386 [data_con_arg_ty] -- Applied to exactly one type,
387 | isPrimType data_con_arg_ty -- which is primitive
388 -> Just (data_con, data_con_arg_ty)
389 other_cases -> Nothing
390 other_cases -> Nothing
394 splitSigmaTy :: GenType t u -> ([t], [(Class,GenType t u)], GenType t u)
398 (tyvars,rho) = splitForAllTy ty
399 (theta,tau) = splitRhoTy rho
401 mkSigmaTy tyvars theta tau = mkForAllTys tyvars (mkRhoTy theta tau)
405 Finding the kind of a type
406 ~~~~~~~~~~~~~~~~~~~~~~~~~~
408 getTypeKind :: GenType (GenTyVar any) u -> Kind
409 getTypeKind (TyVarTy tyvar) = getTyVarKind tyvar
410 getTypeKind (TyConTy tycon usage) = tyConKind tycon
411 getTypeKind (SynTy _ _ ty) = getTypeKind ty
412 getTypeKind (FunTy fun arg _) = mkBoxedTypeKind
413 getTypeKind (DictTy clas arg _) = mkBoxedTypeKind
414 getTypeKind (AppTy fun arg) = resultKind (getTypeKind fun)
415 getTypeKind (ForAllTy _ _) = mkBoxedTypeKind
416 getTypeKind (ForAllUsageTy _ _ _) = mkBoxedTypeKind
420 Free variables of a type
421 ~~~~~~~~~~~~~~~~~~~~~~~~
423 tyVarsOfType :: GenType (GenTyVar flexi) uvar -> GenTyVarSet flexi
425 tyVarsOfType (TyVarTy tv) = unitTyVarSet tv
426 tyVarsOfType (TyConTy tycon usage) = emptyTyVarSet
427 tyVarsOfType (SynTy _ tys ty) = tyVarsOfTypes tys
428 tyVarsOfType (FunTy arg res _) = tyVarsOfType arg `unionTyVarSets` tyVarsOfType res
429 tyVarsOfType (AppTy fun arg) = tyVarsOfType fun `unionTyVarSets` tyVarsOfType arg
430 tyVarsOfType (DictTy clas ty _) = tyVarsOfType ty
431 tyVarsOfType (ForAllTy tyvar ty) = tyVarsOfType ty `minusTyVarSet` unitTyVarSet tyvar
432 tyVarsOfType (ForAllUsageTy _ _ ty) = tyVarsOfType ty
434 tyVarsOfTypes :: [GenType (GenTyVar flexi) uvar] -> GenTyVarSet flexi
435 tyVarsOfTypes tys = foldr (unionTyVarSets.tyVarsOfType) emptyTyVarSet tys
442 applyTy :: Eq t => GenType t u -> GenType t u -> GenType t u
443 applyTy (SynTy _ _ fun) arg = applyTy fun arg
444 applyTy (ForAllTy tv ty) arg = instantiateTy [(tv,arg)] ty
445 applyTy other arg = panic "applyTy"
447 instantiateTy :: Eq t => [(t, GenType t u)] -> GenType t u -> GenType t u
448 instantiateTy tenv ty
451 go (TyVarTy tv) = case [ty | (tv',ty) <- tenv, tv==tv'] of
454 go ty@(TyConTy tycon usage) = ty
455 go (SynTy tycon tys ty) = SynTy tycon (map go tys) (go ty)
456 go (FunTy arg res usage) = FunTy (go arg) (go res) usage
457 go (AppTy fun arg) = AppTy (go fun) (go arg)
458 go (DictTy clas ty usage) = DictTy clas (go ty) usage
459 go (ForAllTy tv ty) = ASSERT(null tv_bound)
462 tv_bound = [() | (tv',_) <- tenv, tv==tv']
464 go (ForAllUsageTy uvar bds ty) = ForAllUsageTy uvar bds (go ty)
467 -- instantiateTauTy works only (a) on types with no ForAlls,
468 -- and when (b) all the type variables are being instantiated
469 -- In return it is more polymorphic than instantiateTy
471 instantiateTauTy :: Eq t => [(t, GenType t' u)] -> GenType t u -> GenType t' u
472 instantiateTauTy tenv ty
475 go (TyVarTy tv) = case [ty | (tv',ty) <- tenv, tv==tv'] of
477 [] -> panic "instantiateTauTy"
478 go (TyConTy tycon usage) = TyConTy tycon usage
479 go (SynTy tycon tys ty) = SynTy tycon (map go tys) (go ty)
480 go (FunTy arg res usage) = FunTy (go arg) (go res) usage
481 go (AppTy fun arg) = AppTy (go fun) (go arg)
482 go (DictTy clas ty usage) = DictTy clas (go ty) usage
485 :: Ord3 u => [(u, GenType t u')] -> GenType t u -> GenType t u'
486 instantiateUsage = error "instantiateUsage: not implemented"
490 type TypeEnv = TyVarEnv Type
492 applyTypeEnvToTy :: TypeEnv -> SigmaType -> SigmaType
493 applyTypeEnvToTy tenv ty
494 = mapOverTyVars v_fn ty
496 v_fn v = case (lookupTyVarEnv tenv v) of
501 @mapOverTyVars@ is a local function which actually does the work. It
502 does no cloning or other checks for shadowing, so be careful when
503 calling this on types with Foralls in them.
506 mapOverTyVars :: (TyVar -> Type) -> Type -> Type
508 mapOverTyVars v_fn ty
510 mapper = mapOverTyVars v_fn
514 SynTy c as e -> SynTy c (map mapper as) (mapper e)
515 FunTy a r u -> FunTy (mapper a) (mapper r) u
516 AppTy f a -> AppTy (mapper f) (mapper a)
517 DictTy c t u -> DictTy c (mapper t) u
518 ForAllTy v t -> ForAllTy v (mapper t)
519 tc@(TyConTy _ _) -> tc
522 At present there are no unboxed non-primitive types, so
523 isUnboxedType is the same as isPrimType.
526 isPrimType, isUnboxedType :: GenType tyvar uvar -> Bool
528 isPrimType (AppTy ty _) = isPrimType ty
529 isPrimType (SynTy _ _ ty) = isPrimType ty
530 isPrimType (TyConTy tycon _) = isPrimTyCon tycon
533 isUnboxedType = isPrimType
536 This is *not* right: it is a placeholder (ToDo 96/03 WDP):
538 typePrimRep :: GenType tyvar uvar -> PrimRep
540 typePrimRep (SynTy _ _ ty) = typePrimRep ty
541 typePrimRep (TyConTy tc _) = if isPrimTyCon tc then panic "typePrimRep:PrimTyCon" else PtrRep
542 typePrimRep (AppTy ty _) = typePrimRep ty
543 typePrimRep _ = PtrRep -- the "default"
546 %************************************************************************
548 \subsection{Matching on types}
550 %************************************************************************
552 Matching is a {\em unidirectional} process, matching a type against a
553 template (which is just a type with type variables in it). The
554 matcher assumes that there are no repeated type variables in the
555 template, so that it simply returns a mapping of type variables to
556 types. It also fails on nested foralls.
558 @matchTys@ matches corresponding elements of a list of templates and
562 matchTy :: GenType t1 u1 -- Template
563 -> GenType t2 u2 -- Proposed instance of template
564 -> Maybe [(t1,GenType t2 u2)] -- Matching substitution
566 matchTys :: [GenType t1 u1] -- Templates
567 -> [GenType t2 u2] -- Proposed instance of template
568 -> Maybe [(t1,GenType t2 u2)] -- Matching substitution
570 matchTy ty1 ty2 = match [] [] ty1 ty2
571 matchTys tys1 tys2 = match' [] (zipEqual tys1 tys2)
574 @match@ is the main function.
577 match :: [(t1, GenType t2 u2)] -- r, the accumulating result
578 -> [(GenType t1 u1, GenType t2 u2)] -- w, the work list
579 -> GenType t1 u1 -> GenType t2 u2 -- Current match pair
580 -> Maybe [(t1, GenType t2 u2)]
582 match r w (TyVarTy v) ty = match' ((v,ty) : r) w
583 match r w (FunTy fun1 arg1 _) (FunTy fun2 arg2 _) = match r ((fun1,fun2):w) arg1 arg2
584 match r w (AppTy fun1 arg1) (AppTy fun2 arg2) = match r ((fun1,fun2):w) arg1 arg2
585 match r w (TyConTy con1 _) (TyConTy con2 _) | con1 == con2 = match' r w
586 match r w (DictTy clas1 ty1 _) (DictTy clas2 ty2 _) | clas1 == clas2 = match r w ty1 ty2
587 match r w (SynTy _ _ ty1) ty2 = match r w ty1 ty2
588 match r w ty1 (SynTy _ _ ty2) = match r w ty1 ty2
590 -- With type synonyms, we have to be careful for the exact
591 -- same reasons as in the unifier. Please see the
592 -- considerable commentary there before changing anything
596 match _ _ _ _ = Nothing
599 match' r ((ty1,ty2):w) = match r w ty1 ty2
602 %************************************************************************
604 \subsection{Equality on types}
606 %************************************************************************
608 The functions eqSimpleTy and eqSimpleTheta are polymorphic in the types t
609 and u, but ONLY WORK FOR SIMPLE TYPES (ie. they panic if they see
610 dictionaries or polymorphic types). The function eqTy has a more
611 specific type, but does the `right thing' for all types.
614 eqSimpleTheta :: (Eq t,Eq u) =>
615 [(Class,GenType t u)] -> [(Class,GenType t u)] -> Bool
617 eqSimpleTheta [] [] = True
618 eqSimpleTheta ((c1,t1):th1) ((c2,t2):th2) =
619 c1==c2 && t1 `eqSimpleTy` t2 && th1 `eqSimpleTheta` th2
620 eqSimpleTheta other1 other2 = False
624 eqSimpleTy :: (Eq t,Eq u) => GenType t u -> GenType t u -> Bool
626 (TyVarTy tv1) `eqSimpleTy` (TyVarTy tv2) =
628 (AppTy f1 a1) `eqSimpleTy` (AppTy f2 a2) =
629 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2
630 (TyConTy tc1 u1) `eqSimpleTy` (TyConTy tc2 u2) =
631 tc1 == tc2 && u1 == u2
633 (FunTy f1 a1 u1) `eqSimpleTy` (FunTy f2 a2 u2) =
634 f1 `eqSimpleTy` f2 && a1 `eqSimpleTy` a2 && u1 == u2
635 (FunTy f1 a1 u1) `eqSimpleTy` t2 =
636 -- Expand t1 just in case t2 matches that version
637 (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) `eqSimpleTy` t2
638 t1 `eqSimpleTy` (FunTy f2 a2 u2) =
639 -- Expand t2 just in case t1 matches that version
640 t1 `eqSimpleTy` (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
642 (SynTy tc1 ts1 t1) `eqSimpleTy` (SynTy tc2 ts2 t2) =
643 (tc1 == tc2 && and (zipWith eqSimpleTy ts1 ts2) && length ts1 == length ts2)
644 || t1 `eqSimpleTy` t2
645 (SynTy _ _ t1) `eqSimpleTy` t2 =
646 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
647 t1 `eqSimpleTy` (SynTy _ _ t2) =
648 t1 `eqSimpleTy` t2 -- Expand the abbrevation and try again
650 (DictTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got DictTy"
651 _ `eqSimpleTy` (DictTy _ _ _) = panic "eqSimpleTy: got DictTy"
653 (ForAllTy _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllTy"
654 _ `eqSimpleTy` (ForAllTy _ _) = panic "eqSimpleTy: got ForAllTy"
656 (ForAllUsageTy _ _ _) `eqSimpleTy` _ = panic "eqSimpleTy: got ForAllUsageTy"
657 _ `eqSimpleTy` (ForAllUsageTy _ _ _) = panic "eqSimpleTy: got ForAllUsageTy"
659 _ `eqSimpleTy` _ = False
662 Types are ordered so we can sort on types in the renamer etc. DNT: Since
663 this class is also used in CoreLint and other such places, we DO expand out
664 Fun/Syn/Dict types (if necessary).
667 eqTy :: Type -> Type -> Bool
670 eq nullTyVarEnv nullUVarEnv t1 t2
672 eq tve uve (TyVarTy tv1) (TyVarTy tv2) =
674 case (lookupTyVarEnv tve tv1) of
677 eq tve uve (AppTy f1 a1) (AppTy f2 a2) =
678 eq tve uve f1 f2 && eq tve uve a1 a2
679 eq tve uve (TyConTy tc1 u1) (TyConTy tc2 u2) =
680 tc1 == tc2 && eqUsage uve u1 u2
682 eq tve uve (FunTy f1 a1 u1) (FunTy f2 a2 u2) =
683 eq tve uve f1 f2 && eq tve uve a1 a2 && eqUsage uve u1 u2
684 eq tve uve (FunTy f1 a1 u1) t2 =
685 -- Expand t1 just in case t2 matches that version
686 eq tve uve (AppTy (AppTy (TyConTy mkFunTyCon u1) f1) a1) t2
687 eq tve uve t1 (FunTy f2 a2 u2) =
688 -- Expand t2 just in case t1 matches that version
689 eq tve uve t1 (AppTy (AppTy (TyConTy mkFunTyCon u2) f2) a2)
691 eq tve uve (DictTy c1 t1 u1) (DictTy c2 t2 u2) =
692 c1 == c2 && eq tve uve t1 t2 && eqUsage uve u1 u2
693 eq tve uve t1@(DictTy _ _ _) t2 =
694 eq tve uve (expandTy t1) t2 -- Expand the dictionary and try again
695 eq tve uve t1 t2@(DictTy _ _ _) =
696 eq tve uve t1 (expandTy t2) -- Expand the dictionary and try again
698 eq tve uve (SynTy tc1 ts1 t1) (SynTy tc2 ts2 t2) =
699 (tc1 == tc2 && and (zipWith (eq tve uve) ts1 ts2) && length ts1 == length ts2)
701 eq tve uve (SynTy _ _ t1) t2 =
702 eq tve uve t1 t2 -- Expand the abbrevation and try again
703 eq tve uve t1 (SynTy _ _ t2) =
704 eq tve uve t1 t2 -- Expand the abbrevation and try again
706 eq tve uve (ForAllTy tv1 t1) (ForAllTy tv2 t2) =
707 eq (addOneToTyVarEnv tve tv1 tv2) uve t1 t2
708 eq tve uve (ForAllUsageTy u1 b1 t1) (ForAllUsageTy u2 b2 t2) =
709 eqBounds uve b1 b2 && eq tve (addOneToUVarEnv uve u1 u2) t1 t2
713 eqBounds uve [] [] = True
714 eqBounds uve (u1:b1) (u2:b2) = eqUVar uve u1 u2 && eqBounds uve b1 b2
715 eqBounds uve _ _ = False