[project @ 1998-01-27 14:53:40 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / Unify.lhs
index cd218cb..439ccda 100644 (file)
@@ -1,5 +1,5 @@
 %
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
+% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
 %
 \section[Unify]{Unifier}
 
@@ -7,47 +7,33 @@ The unifier is now squarely in the typechecker monad (because of the
 updatable substitution).
 
 \begin{code}
-#include "HsVersions.h"
-
-module Unify ( unifyTauTy, unifyTauTyList, unifyTauTyLists ) where
+module Unify ( unifyTauTy, unifyTauTyList, unifyTauTyLists, 
+              unifyFunTy, unifyListTy, unifyTupleTy,
+              Subst, unifyTysX, unifyTyListsX
+ ) where
 
-IMPORT_Trace           -- ToDo: rm (debugging only)
-import Outputable
-import Pretty
+#include "HsVersions.h"
 
-import AbsSyn
+-- friends: 
 import TcMonad
-
-import CmdLineOpts     ( GlobalSwitch(..) )
-import Errors          ( unifyErr, UnifyErrInfo(..), UnifyErrContext  )
-import Id              ( Id, DataCon(..), Inst )
-import Maybes          ( Maybe(..) )
-import Subst           ( extendSubst, SubstResult(..), Subst )
-#if USE_ATTACK_PRAGMAS
-import Class           ( Class(..), cmpClass ) -- .. for pragmas only
-import TyCon           ( TyCon(..), isBoxedTyCon, isVisibleSynTyCon, cmpTyCon )
-                       -- .. on TyCon is for pragmas only
-import TyVar           -- make all visible for pragmas
-import UniTyFuns       ( pprUniType, pprTyCon ) 
-#else
-import Class           ( Class )
-import TyVar           ( TyVar(..), TyVarTemplate )
-import TyCon           ( TyCon, isBoxedTyCon, isVisibleSynTyCon )
-#endif
-import UniType         ( UniType(..), TauType(..)
-                         IF_ATTACK_PRAGMAS(COMMA cmpUniType)
-                       )
+import Type    ( GenType(..), Type, tyVarsOfType,
+                 typeKind, mkFunTy, splitFunTy_maybe, splitAppTys, splitTyConApp_maybe )
+import TyCon   ( TyCon, mkFunTyCon, isTupleTyCon, tyConArity, Arity )
+import TyVar   ( GenTyVar(..), TyVar, tyVarKind, tyVarSetToList,
+                 TyVarEnv, lookupTyVarEnv, emptyTyVarEnv, addToTyVarEnv
+               )
+import TcType  ( TcType, TcMaybe(..), TcTauType, TcTyVar,
+                 newTyVarTy, tcReadTyVar, tcWriteTyVar, zonkTcType
+               )
+-- others:
+import Kind    ( Kind, hasMoreBoxityInfo, mkTypeKind, mkBoxedTypeKind )
+import TysWiredIn ( listTyCon, mkListTy, mkTupleTy )
+import Maybes  ( maybeToBool )
+import PprType ()              -- Instances
 import Util
+import Outputable
 \end{code}
 
-%************************************************************************
-%*                                                                     *
-\subsection[Unify-spec]{Specification}
-%*                                                                     *
-%************************************************************************
-
-CLAIM: the unifier works correctly even if the types to be unified are not
-fixed points of the substitution.
 
 %************************************************************************
 %*                                                                     *
@@ -61,18 +47,23 @@ non-exported generic functions.
 Unify two @TauType@s.  Dead straightforward.
 
 \begin{code}
-unifyTauTy :: TauType -> TauType -> UnifyErrContext -> TcM ()
-
-unifyTauTy ty1 ty2 err_ctxt = uTys ty1 ty1 ty2 ty2 err_ctxt
+unifyTauTy :: TcTauType s -> TcTauType s -> TcM s ()
+unifyTauTy ty1 ty2     -- ty1 expected, ty2 inferred
+  = tcAddErrCtxtM (unifyCtxt ty1 ty2) $
+    uTys ty1 ty1 ty2 ty2
 \end{code}
 
-@unifyTauTyLists@ unifies corresponding elements of its two list
-arguments.  The lists should be of equal length.
+@unifyTauTyList@ unifies corresponding elements of two lists of
+@TauType@s.  It uses @uTys@ to do the real work.  The lists should be
+of equal length.  We charge down the list explicitly so that we can
+complain if their lengths differ.
 
 \begin{code}
-unifyTauTyLists :: [TauType] -> [TauType] -> UnifyErrContext -> TcM ()
-
-unifyTauTyLists tys1 tys2 err_ctxt = uList tys1 tys2 err_ctxt
+unifyTauTyLists :: [TcTauType s] -> [TcTauType s] ->  TcM s ()
+unifyTauTyLists []          []         = returnTc ()
+unifyTauTyLists (ty1:tys1) (ty2:tys2) = uTys ty1 ty1 ty2 ty2   `thenTc_`
+                                       unifyTauTyLists tys1 tys2
+unifyTauTyLists ty1s ty2s = panic "Unify.unifyTauTyLists: mismatched type lists!"
 \end{code}
 
 @unifyTauTyList@ takes a single list of @TauType@s and unifies them
@@ -80,38 +71,11 @@ all together.  It is used, for example, when typechecking explicit
 lists, when all the elts should be of the same type.
 
 \begin{code}
-unifyTauTyList :: [TauType] -> UnifyErrContext -> TcM ()
-
-unifyTauTyList []   _ = returnTc ()
-unifyTauTyList [ty] _ = returnTc ()
-
-unifyTauTyList (ty1:tys@(ty2:_)) err_ctxt
-  = unifyTauTy ty1 ty2 err_ctxt        `thenTc_`
-    unifyTauTyList tys err_ctxt
-\end{code}
-
-%************************************************************************
-%*                                                                     *
-\subsection[Unify-lists-of-types]{@uList@}
-%*                                                                     *
-%************************************************************************
-
-@uList@ unifies corresponding elements of two lists of @TauType@s.  It
-uses @uTys@ to do the real work.  We charge down the list explicitly
-so that we can complain if their lengths differ.
-
-\begin{code}
-uList :: [TauType] -> [TauType]
-      -> UnifyErrContext
-      -> TcM ()
-
-uList [] [] _ = returnTc ()
-
-uList (ty1:tys1) (ty2:tys2) err_ctxt
-  = uTys ty1 ty1 ty2 ty2 err_ctxt   `thenTc_`
-    uList tys1 tys2 err_ctxt
-
-uList ty1s ty2s _ = panic "Unify.uList: mismatched type lists!"
+unifyTauTyList :: [TcTauType s] -> TcM s ()
+unifyTauTyList []               = returnTc ()
+unifyTauTyList [ty]             = returnTc ()
+unifyTauTyList (ty1:tys@(ty2:_)) = unifyTauTy ty1 ty2  `thenTc_`
+                                  unifyTauTyList tys
 \end{code}
 
 %************************************************************************
@@ -129,96 +93,71 @@ de-synonym'd version.  This way we get better error messages.
 We call the first one \tr{ps_ty1}, \tr{ps_ty2} for ``possible synomym''.
 
 \begin{code}
-uTys :: TauType -> TauType     -- Error reporting ty1 and real ty1
-     -> TauType -> TauType     -- Error reporting ty2 and real ty2
-     -> UnifyErrContext
-     -> TcM ()
-\end{code}
-
-%********************************************************
-%*                                                     *
-Sanity check: should never find a UniTyVarTemplate
-%*                                                     *
-%********************************************************
-
-\begin{code}
-#ifdef DEBUG
-
-uTys ps_ty1 ty1@(UniTyVarTemplate tv1) ps_ty2 ty2 err_ctxt
-  = pprPanic "Unify:uTys:unifying w/ UniTyVarTemplate(1):" (ppCat [ppr PprDebug tv1, ppr PprDebug ty2])
-
-uTys ps_ty1 ty1 ps_ty2 ty2@(UniTyVarTemplate tv2) err_ctxt
-  = pprPanic "Unify:uTys:unifying w/ UniTyVarTemplate(2):" (ppCat [ppr PprDebug ty1, ppr PprDebug tv2])
-
-#endif {-DEBUG-}
-\end{code}
-
-%********************************************************
-%*                                                     *
-Both variables:
-%*                                                     *
-%********************************************************
-
-\begin{code}
-uTys ps_ty1 (UniTyVar tyvar1) ps_ty2 ty2 err_ctxt = uVar tyvar1 ps_ty2 ty2 err_ctxt
-uTys ps_ty1 ty1 ps_ty2 (UniTyVar tyvar2) err_ctxt = uVar tyvar2 ps_ty1 ty1 err_ctxt
-\end{code}
-
-%********************************************************
-%*                                                     *
-Both function constructors:
-%*                                                     *
-%********************************************************
-
-\begin{code}
-uTys _ (UniFun fun1 arg1) _ (UniFun fun2 arg2) err_ctxt
-  = uList [fun1, arg1] [fun2, arg2] err_ctxt
-\end{code}
-
-%********************************************************
-%*                                                     *
-Both datatype constructors:
-%*                                                     *
-%********************************************************
-
-\begin{code}
-uTys ps_ty1 ty1@(UniData con1 args1) ps_ty2 ty2@(UniData con2 args2) err_ctxt
-  = if (con1 == con2) then
-       -- Same constructors, just unify the arguments
-       uList args1 args2 err_ctxt
-    else
-       -- Different constructors: disaster
-       getSrcLocTc             `thenNF_Tc` \ src_loc ->
-       failTc (unifyErr (UnifyMisMatch ps_ty1 ps_ty2) err_ctxt src_loc)
-\end{code}
+uTys :: TcTauType s -> TcTauType s     -- Error reporting ty1 and real ty1
+     -> TcTauType s -> TcTauType s     -- Error reporting ty2 and real ty2
+     -> TcM s ()
+
+       -- Always expand synonyms (see notes at end)
+uTys ps_ty1 (SynTy _ ty1) ps_ty2 ty2 = uTys ps_ty1 ty1 ps_ty2 ty2
+uTys ps_ty1 ty1 ps_ty2 (SynTy _ ty2) = uTys ps_ty1 ty1 ps_ty2 ty2
+
+       -- Variables; go for uVar
+uTys ps_ty1 (TyVarTy tyvar1) ps_ty2 ty2 = uVar tyvar1 ps_ty2 ty2
+uTys ps_ty1 ty1 ps_ty2 (TyVarTy tyvar2) = uVar tyvar2 ps_ty1 ty1
+
+       -- Functions; just check the two parts
+uTys _ (FunTy fun1 arg1) _ (FunTy fun2 arg2)
+  = uTys fun1 fun1 fun2 fun2   `thenTc_`    uTys arg1 arg1 arg2 arg2
+
+       -- Type constructors must match
+uTys ps_ty1 (TyConApp con1 tys1) ps_ty2 (TyConApp con2 tys2)
+  = checkTc (con1 == con2 && length tys1 == length tys2) 
+           (unifyMisMatch ps_ty1 ps_ty2)               `thenTc_`
+    unifyTauTyLists tys1 tys2
+
+       -- Applications need a bit of care!
+       -- They can match FunTy and TyConApp
+uTys _ (AppTy s1 t1) _ (AppTy s2 t2)
+  = uTys s1 s1 s2 s2   `thenTc_`    uTys t1 t1 t2 t2
+
+uTys _ (AppTy s1 t1) _ (FunTy fun2 arg2)
+  = uTys s1 s1 s2 s2   `thenTc_`    uTys t1 t1 t2 t2
+  where
+        -- Converts  a -> b to (->) a b
+    s2 = TyConApp mkFunTyCon [fun2]
+    t2 = arg2
 
-%********************************************************
-%*                                                     *
-Type synonyms:
-%*                                                     *
-%********************************************************
+uTys _ (AppTy s1 t1) _ (TyConApp tc tys@(_:_))
+  = case snocView tys of
+       (ts2, t2) -> uTys s1 s1 s2 s2   `thenTc_`   uTys t1 t1 t2 t2
+                 where
+                       -- Not efficient, but simple
+                    s2 = TyConApp tc ts2
 
-If just one or the other is a synonym, just expand it.
+uTys ps1 s1 ps2 s2@(AppTy _ _) = uTys ps2 s2 ps1 s1
+       -- Swap arguments if the App is in the second argument
 
-\begin{code}
-uTys ps_ty1 (UniSyn con1 args1 ty1) ps_ty2 ty2 err_ctxt
- | isVisibleSynTyCon con1
- = uTys ps_ty1 ty1 ps_ty2 ty2 err_ctxt
+       -- Not expecting for-alls in unification
+#ifdef DEBUG
+uTys ps_ty1 (ForAllTy _ _)       ps_ty2 ty2 = panic "Unify.uTys:ForAllTy (1st arg)"
+uTys ps_ty1 ty1 ps_ty2       (ForAllTy _ _) = panic "Unify.uTys:ForAllTy (2nd arg)"
+#endif
 
-uTys ps_ty1 ty1 ps_ty2 (UniSyn con2 args2 ty2) err_ctxt
- | isVisibleSynTyCon con2
- = uTys ps_ty1 ty1 ps_ty2 ty2 err_ctxt
+       -- Anything else fails
+uTys ps_ty1 ty1 ps_ty2 ty2  = failWithTc (unifyMisMatch ps_ty1 ps_ty2)
 \end{code}
 
+Notes on synonyms
+~~~~~~~~~~~~~~~~~
 If you are tempted to make a short cut on synonyms, as in this
 pseudocode...
 
 \begin{verbatim}
-uTys (UniSyn con1 args1 ty1) (UniSyn con2 args2 ty2)
+uTys (SynTy con1 args1 ty1) (SynTy con2 args2 ty2)
   = if (con1 == con2) then
        -- Good news!  Same synonym constructors, so we can shortcut
        -- by unifying their arguments and ignoring their expansions.
-       uList args1 args2
+       unifyTauTypeLists args1 args2
     else
        -- Never mind.  Just expand them and try again
        uTys ty1 ty2
@@ -259,34 +198,6 @@ somehow as needing expansion, perhaps also issuing a warning to the
 user.
 \end{quotation}
 
-Still, if the synonym is abstract, we can only just go ahead and try!
-
-\begin{code}
-uTys ps_ty1 (UniSyn con1 args1 ty1) ps_ty2 (UniSyn con2 args2 ty2) err_ctxt
-  -- Both must be abstract (i.e., non "visible" -- not done yet)
-  = if (con1 == con2) then
-       -- Good news!  Same synonym constructors, so we can shortcut
-       -- by unifying their arguments and ignoring their expansions.
-       uList args1 args2 err_ctxt
-    else
-       -- Bad news; mis-matched type constructors
-       getSrcLocTc             `thenNF_Tc` \ src_loc ->
-       failTc (unifyErr (UnifyMisMatch ps_ty1 ps_ty2) err_ctxt src_loc)
-\end{code}
-
-%********************************************************
-%*                                                     *
-Catch-all case---just fails:
-%*                                                     *
-%********************************************************
-
-Anything else fails. For example, matching a @UniFun@ against
-a @UniData@.
-\begin{code}
-uTys ps_ty1 ty1 ps_ty2 ty2 err_ctxt
-  = getSrcLocTc                `thenNF_Tc` \ src_loc ->
-    failTc (unifyErr (UnifyMisMatch ps_ty1 ps_ty2) err_ctxt src_loc)
-\end{code}
 
 %************************************************************************
 %*                                                                     *
@@ -305,56 +216,283 @@ an unboxed type.  In fact this can't happen, because the Open ones are
 always the ones which are unified away.
 
 \begin{code}
-uVar :: TyVar
-     -> UniType -> UniType     -- printing and real versions
-     -> UnifyErrContext
-     -> TcM ()
+uVar :: TcTyVar s
+     -> TcTauType s -> TcTauType s     -- printing and real versions
+     -> TcM s ()
+
+uVar tv1 ps_ty2 ty2
+  = tcReadTyVar tv1    `thenNF_Tc` \ maybe_ty1 ->
+    case maybe_ty1 of
+       BoundTo ty1 -> uTys ty1 ty1 ps_ty2 ty2
+       other       -> uUnboundVar tv1 maybe_ty1 ps_ty2 ty2
 
-uVar tv1 ps_ty2 ty2 err_ctxt
-  = do tv1 ty2
-  where
        -- Expand synonyms
-    do _ (UniSyn _ _ ty2) = do tv1 ty2
+uUnboundVar tv1 maybe_ty1 ps_ty2 (SynTy _ ty2)
+  = uUnboundVar tv1 maybe_ty1 ps_ty2 ty2
+
+
+       -- The both-type-variable case
+uUnboundVar tv1@(TyVar uniq1 kind1 name1 box1)
+           maybe_ty1
+           ps_ty2
+           ty2@(TyVarTy tv2@(TyVar uniq2 kind2 name2 box2))
+
+       -- Same type variable => no-op
+  | uniq1 == uniq2
+  = returnTc ()
+
+       -- Distinct type variables
+       -- ASSERT maybe_ty1 /= BoundTo
+  | otherwise
+  = tcReadTyVar tv2    `thenNF_Tc` \ maybe_ty2 ->
+    case maybe_ty2 of
+       BoundTo ty2' -> uUnboundVar tv1 maybe_ty1 ty2' ty2'
+
+       UnBound |  (kind1 == kind2 && not (maybeToBool name1))  -- Same kinds and tv1 is anonymous
+                                                               -- so update tv1
+               -> tcWriteTyVar tv1 ps_ty2              `thenNF_Tc_` returnTc ()
+       
+               |  kind1 `hasMoreBoxityInfo` kind2              -- Update tv2 if possible
+               -> tcWriteTyVar tv2 (TyVarTy tv1)       `thenNF_Tc_` returnTc ()
+
+               | kind2 `hasMoreBoxityInfo` kind1               -- Update tv1 if possible
+               -> tcWriteTyVar tv1 ps_ty2              `thenNF_Tc_` returnTc ()
+       
+       other   -> failWithTc (unifyKindErr tv1 ps_ty2)
+
+       -- Second one isn't a type variable
+uUnboundVar tv1@(TyVar uniq1 kind1 name1 box1) maybe_ty1 ps_ty2 non_var_ty2
+  |  typeKind non_var_ty2 `hasMoreBoxityInfo` kind1
+  =  occur_check non_var_ty2                   `thenTc_`
+     tcWriteTyVar tv1 ps_ty2                   `thenNF_Tc_`
+     returnTc ()
+
+  | otherwise 
+  = failWithTc (unifyKindErr tv1 ps_ty2)
+
+  where
+    occur_check ty = mapTc occur_check_tv (tyVarSetToList (tyVarsOfType ty))   `thenTc_`
+                    returnTc ()
+
+    occur_check_tv tv2@(TyVar uniq2 _ _ box2)
+       | uniq1 == uniq2                -- Same tyvar; fail
+       = failWithTc (unifyOccurCheck tv1 ps_ty2)
+
+       | otherwise             -- A different tyvar
+       = tcReadTyVar tv2       `thenNF_Tc` \ maybe_ty2 ->
+        case maybe_ty2 of
+               BoundTo ty2' -> occur_check ty2'
+               other        -> returnTc ()
+\end{code}
+
+%************************************************************************
+%*                                                                     *
+\subsection[Unify-fun]{@unifyFunTy@}
+%*                                                                     *
+%************************************************************************
 
-       -- Commit any open type variable
-    do (OpenSysTyVar _) ty2                                = tv1 `bindTo` ps_ty2
-    do _               ty2@(UniTyVar tv2@(OpenSysTyVar _)) = tv2 `bindTo` ty1
+@unifyFunTy@ is used to avoid the fruitless creation of type variables.
 
-       -- Eliminate Poly in favour of User
-    do (PolySysTyVar _) ty2@(UniTyVar (UserTyVar _ _))      = tv1 `bindTo` ps_ty2
-    do (PolySysTyVar _) ty2@(UniTyVar (PolySysTyVar _))     = tv1 `bindTo` ps_ty2
-    do (UserTyVar _ _)  ty2@(UniTyVar tv2@(PolySysTyVar _)) = tv2 `bindTo` ty1
-    do (UserTyVar _ _)  ty2@(UniTyVar (UserTyVar _ _))      = tv1 `bindTo` ps_ty2
+\begin{code}
+unifyFunTy :: TcType s                         -- Fail if ty isn't a function type
+          -> TcM s (TcType s, TcType s)        -- otherwise return arg and result types
+
+unifyFunTy ty@(TyVarTy tyvar)
+  = tcReadTyVar tyvar  `thenNF_Tc` \ maybe_ty ->
+    case maybe_ty of
+       BoundTo ty' -> unifyFunTy ty'
+       other       -> unify_fun_ty_help ty
+
+unifyFunTy ty
+  = case splitFunTy_maybe ty of
+       Just arg_and_res -> returnTc arg_and_res
+       Nothing          -> unify_fun_ty_help ty
+
+unify_fun_ty_help ty   -- Special cases failed, so revert to ordinary unification
+  = newTyVarTy mkTypeKind              `thenNF_Tc` \ arg ->
+    newTyVarTy mkTypeKind              `thenNF_Tc` \ res ->
+    unifyTauTy ty (mkFunTy arg res)    `thenTc_`
+    returnTc (arg,res)
+\end{code}
+
+\begin{code}
+unifyListTy :: TcType s              -- expected list type
+           -> TcM s (TcType s)      -- list element type
+
+unifyListTy ty@(TyVarTy tyvar)
+  = tcReadTyVar tyvar  `thenNF_Tc` \ maybe_ty ->
+    case maybe_ty of
+       BoundTo ty' -> unifyListTy ty'
+       other       -> unify_list_ty_help ty
+
+unifyListTy ty
+  = case splitTyConApp_maybe ty of
+       Just (tycon, [arg_ty]) | tycon == listTyCon -> returnTc arg_ty
+       other                                       -> unify_list_ty_help ty
+
+unify_list_ty_help ty  -- Revert to ordinary unification
+  = newTyVarTy mkBoxedTypeKind         `thenNF_Tc` \ elt_ty ->
+    unifyTauTy ty (mkListTy elt_ty)    `thenTc_`
+    returnTc elt_ty
+\end{code}
+
+\begin{code}
+unifyTupleTy :: Arity -> TcType s -> TcM s [TcType s]
+unifyTupleTy arity ty@(TyVarTy tyvar)
+  = tcReadTyVar tyvar  `thenNF_Tc` \ maybe_ty ->
+    case maybe_ty of
+       BoundTo ty' -> unifyTupleTy arity ty'
+       other       -> unify_tuple_ty_help arity ty
+
+unifyTupleTy arity ty
+  = case splitTyConApp_maybe ty of
+       Just (tycon, arg_tys) |  isTupleTyCon tycon 
+                        && tyConArity tycon == arity
+                        -> returnTc arg_tys
+       other -> unify_tuple_ty_help arity ty
+
+unify_tuple_ty_help arity ty
+  = mapNF_Tc (\ _ -> newTyVarTy mkBoxedTypeKind) [1..arity]    `thenNF_Tc` \ arg_tys ->
+    unifyTauTy ty (mkTupleTy arity arg_tys)                    `thenTc_`
+    returnTc arg_tys
+\end{code}
+
+%************************************************************************
+%*                                                                     *
+\subsection{Unification wih a explicit substitution}
+%*                                                                     *
+%************************************************************************
+
+Unify types with an explicit substitution and no monad.
+
+\begin{code}
+type Subst  = TyVarEnv Type    -- Not necessarily idempotent
 
-       -- Matching for boxed data types
-    do (PolySysTyVar _) ty2@(UniData con _) | isBoxedTyCon con  = tv1 `bindTo` ps_ty2
-    do (UserTyVar _ _)  ty2@(UniData con _) | isBoxedTyCon con  = tv1 `bindTo` ps_ty2
+unifyTysX :: Type -> Type -> Maybe Subst
+unifyTysX ty1 ty2 = uTysX ty1 ty2 (\s -> Just s) emptyTyVarEnv
 
-       -- Matching for unboxed data types:
-       --   requires specialisation w.r.t. the unboxed type
-    do (PolySysTyVar _) ty2@(UniData con _)  = tv1 `bindToUnboxed` ps_ty2
-    do (UserTyVar _ _)  ty2@(UniData con _)  = tv1 `bindToUnboxed` ps_ty2
+unifyTyListsX :: [Type] -> [Type] -> Maybe Subst
+unifyTyListsX tys1 tys2 = uTyListsX tys1 tys2 (\s -> Just s) emptyTyVarEnv
 
-       -- Matching for function types
-    do (PolySysTyVar _) ty2@(UniFun _ _)     = tv1 `bindTo` ps_ty2
-    do (UserTyVar _ _)  ty2@(UniFun _ _)     = tv1 `bindTo` ps_ty2
 
-       -- Default
-    do _ _ = getSrcLocTc `thenNF_Tc` \ src_loc ->
-             failTc (unifyErr (UnifyMisMatch ty1 ps_ty2) err_ctxt src_loc)
+uTysX :: Type -> Type
+      -> (Subst -> Maybe Subst)
+      -> Subst
+      -> Maybe Subst
 
-       ----------- END OF CASES ---------------
+uTysX ty1 (SynTy _ ty2) k subst = uTysX ty1 ty2 k subst
 
-    ty1 = UniTyVar tv1
+       -- Variables; go for uVar
+uTysX (TyVarTy tyvar1) ty2 k subst = uVarX tyvar1 ty2 k subst
+uTysX ty1 (TyVarTy tyvar2) k subst = uVarX tyvar2 ty1 k subst
 
-    tyvar1 `bindTo` ty2 
-       = extendSubstTc tyvar1 ty2 err_ctxt
+       -- Functions; just check the two parts
+uTysX (FunTy fun1 arg1) (FunTy fun2 arg2) k subst
+  = uTysX fun1 fun2 (uTysX arg1 arg2 k) subst
 
-    tyvar1 `bindToUnboxed` ty2
-       = getSwitchCheckerTc    `thenNF_Tc` \ sw_chkr ->
-         if sw_chkr SpecialiseUnboxed then
-             extendSubstTc tyvar1 ty2 err_ctxt
-         else
-             getSrcLocTc       `thenNF_Tc` \ src_loc ->
-              failTc (unifyErr (UnifyMisMatch ty1 ps_ty2) err_ctxt src_loc)
+       -- Type constructors must match
+uTysX (TyConApp con1 tys1) (TyConApp con2 tys2) k subst
+  | (con1 == con2 && length tys1 == length tys2)
+  = uTyListsX tys1 tys2 k subst
+
+       -- Applications need a bit of care!
+       -- They can match FunTy and TyConApp
+uTysX (AppTy s1 t1) (AppTy s2 t2) k subst
+  = uTysX s1 s2 (uTysX t1 t2 k) subst
+
+uTysX (AppTy s1 t1) (FunTy fun2 arg2) k subst
+  = uTysX s1 s2 (uTysX t1 t2 k) subst
+  where
+        -- Converts  a -> b to (->) a b
+    s2 = TyConApp mkFunTyCon [fun2]
+    t2 = arg2
+
+uTysX (AppTy s1 t1) (TyConApp tc tys@(_:_)) k subst
+  = case snocView tys of
+       (ts2, t2) -> uTysX s1 s2 (uTysX t1 t2 k) subst
+                 where
+                       -- Not efficient, but simple
+                    s2 = TyConApp tc ts2
+
+uTysX s1 s2@(AppTy _ _) k subst = uTysX s2 s1 k subst
+       -- Swap arguments if the App is in the second argument
+
+       -- Not expecting for-alls in unification
+#ifdef DEBUG
+uTysX (ForAllTy _ _) ty2 k subst = panic "Unify.uTysX subst:ForAllTy (1st arg)"
+uTysX ty1 (ForAllTy _ _) k subst = panic "Unify.uTysX subst:ForAllTy (2nd arg)"
+#endif
+
+       -- Anything else fails
+uTysX ty1 ty2 k subst = Nothing
+
+
+uTyListsX []         []         k subst = k subst
+uTyListsX (ty1:tys1) (ty2:tys2) k subst = uTysX ty1 ty2 (uTyListsX tys1 tys2 k) subst
+uTyListsX tys1      tys2       k subst = Nothing   -- Fail if the lists are different lengths
+\end{code}
+
+\begin{code}
+uVarX tv1 (TyVarTy tv2) k subst | tv1 == tv2 = k subst
+      -- Binding a variable to itself is a no-op
+
+uVarX tv1 ty2 k subst
+  = case lookupTyVarEnv subst tv1 of
+      Just ty1 ->    -- Already bound
+                    uTysX ty1 ty2 k subst
+
+      Nothing       -- Not already bound
+              |  typeKind ty2 `hasMoreBoxityInfo` tyVarKind tv1
+              && occur_check_ok ty2
+              ->     -- No kind mismatch nor occur check
+                 k (addToTyVarEnv subst tv1 ty2)
+
+              | otherwise -> Nothing   -- Fail if kind mis-match or occur check
+  where
+    occur_check_ok ty = all occur_check_ok_tv (tyVarSetToList (tyVarsOfType ty))
+    occur_check_ok_tv tv | tv1 == tv = False
+                        | otherwise = case lookupTyVarEnv subst tv of
+                                        Nothing -> True
+                                        Just ty -> occur_check_ok ty
 \end{code}
+
+
+%************************************************************************
+%*                                                                     *
+\subsection[Unify-context]{Errors and contexts}
+%*                                                                     *
+%************************************************************************
+
+Errors
+~~~~~~
+
+\begin{code}
+unifyCtxt ty1 ty2              -- ty1 expected, ty2 inferred
+  = zonkTcType ty1     `thenNF_Tc` \ ty1' ->
+    zonkTcType ty2     `thenNF_Tc` \ ty2' ->
+    returnNF_Tc (err ty1' ty2')
+  where
+    err ty1' ty2' = vcat [
+                          hsep [ptext SLIT("Expected:"), ppr ty1'],
+                          hsep [ptext SLIT("Inferred:"), ppr ty2']
+                       ]
+
+unifyMisMatch ty1 ty2
+  = hang (ptext SLIT("Couldn't match the type"))
+        4 (sep [quotes (ppr ty1), ptext SLIT("against"), quotes (ppr ty2)])
+
+expectedFunErr ty
+  = hang (text "Function type expected, but found the type")
+        4 (ppr ty)
+
+unifyKindErr tyvar ty
+  = hang (ptext SLIT("Compiler bug: kind mis-match between"))
+        4 (sep [quotes (hsep [ppr tyvar, ptext SLIT("::"), ppr (tyVarKind tyvar)]),
+                ptext SLIT("and"), 
+                quotes (hsep [ppr ty, ptext SLIT("::"), ppr (typeKind ty)])])
+
+unifyOccurCheck tyvar ty
+  = hang (ptext SLIT("Occurs check: cannot construct the infinite type:"))
+        8 (sep [ppr tyvar, char '=', ppr ty])
+\end{code}
+