[project @ 2001-11-26 09:20:25 by simonpj]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsTypes.lhs
index 0ea040c..46dc78e 100644 (file)
@@ -11,7 +11,10 @@ module HsTypes (
         , hsUsOnce, hsUsMany
 
        , mkHsForAllTy, mkHsDictTy, mkHsIParamTy
-       , hsTyVarName, hsTyVarNames, replaceTyVarName
+       , hsTyVarName, hsTyVarNames, replaceTyVarName,
+       
+       -- Type place holder
+       PostTcType, placeHolderType,
 
        -- Printing
        , pprParendHsType, pprHsForAll, pprHsContext, pprHsTyVarBndr
@@ -27,33 +30,57 @@ module HsTypes (
 #include "HsVersions.h"
 
 import Class           ( FunDep )
-import Type            ( Type, Kind, PredType(..), ClassContext,
-                         splitSigmaTy, boxedTypeKind
+import TcType          ( Type, Kind, ThetaType, SourceType(..), IPName,
+                         tcSplitSigmaTy, liftedTypeKind, eqKind, tcEqType
                        )
 import TypeRep         ( Type(..), TyNote(..) )        -- toHsType sees the representation
-import TyCon           ( isTupleTyCon, tupleTyConBoxity, tyConArity, getSynTyConDefn )
+import TyCon           ( isTupleTyCon, tupleTyConBoxity, tyConArity, isNewTyCon, getSynTyConDefn )
 import RdrName         ( RdrName, mkUnqual )
 import Name            ( Name, getName )
 import OccName         ( NameSpace, tvName )
 import Var             ( TyVar, tyVarKind )
-import Subst           ( mkTyVarSubst, substTy )
+import Subst           ( substTyWith )
 import PprType         ( {- instance Outputable Kind -}, pprParendKind )
 import BasicTypes      ( Boxity(..), Arity, tupleParens )
 import PrelNames       ( mkTupConRdrName, listTyConKey, usOnceTyConKey, usManyTyConKey, hasKey,
                          usOnceTyConName, usManyTyConName
                        )
 import FiniteMap
+import Util            ( eqListBy, lengthIs )
 import Outputable
+\end{code}
+
 
+%************************************************************************
+%*                                                                     *
+\subsection{Annotating the syntax}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+type PostTcType = Type         -- Used for slots in the abstract syntax
+                               -- where we want to keep slot for a type
+                               -- to be added by the type checker...but
+                               -- before typechecking it's just bogus
+
+placeHolderType :: PostTcType  -- Used before typechecking
+placeHolderType  = panic "Evaluated the place holder for a PostTcType"
 \end{code}
 
+
+%************************************************************************
+%*                                                                     *
+\subsection{Data types}
+%*                                                                     *
+%************************************************************************
+
 This is the syntax for types as seen in type signatures.
 
 \begin{code}
 type HsContext name = [HsPred name]
 
-data HsPred name = HsPClass name [HsType name]
-                | HsPIParam name (HsType name)
+data HsPred name = HsClassP name [HsType name]
+                | HsIParam (IPName name) (HsType name)
 
 data HsType name
   = HsForAllTy (Maybe [HsTyVarBndr name])      -- Nothing for implicitly quantified signatures
@@ -75,6 +102,7 @@ data HsType name
   -- Generics
   | HsOpTy             (HsType name) name (HsType name)
   | HsNumTy             Integer
+
   -- these next two are only used in interfaces
   | HsPredTy           (HsPred name)
   
@@ -123,8 +151,8 @@ mkHsForAllTy mtvs1     [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus
                                                       (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
 
-mkHsDictTy cls tys = HsPredTy (HsPClass cls tys)
-mkHsIParamTy v ty  = HsPredTy (HsPIParam v ty)
+mkHsDictTy cls tys = HsPredTy (HsClassP cls tys)
+mkHsIParamTy v ty  = HsPredTy (HsIParam v ty)
 
 data HsTyVarBndr name
   = UserTyVar name
@@ -162,12 +190,12 @@ instance (Outputable name) => Outputable (HsTyVarBndr name) where
     ppr (IfaceTyVar name kind) = pprHsTyVarBndr name kind
 
 instance Outputable name => Outputable (HsPred name) where
-    ppr (HsPClass clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
-    ppr (HsPIParam n ty)    = hsep [char '?' <> ppr n, text "::", ppr ty]
+    ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
+    ppr (HsIParam n ty)    = hsep [ppr n, dcolon, ppr ty]
 
 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
-pprHsTyVarBndr name kind | kind == boxedTypeKind = ppr name
-                        | otherwise             = hsep [ppr name, dcolon, pprParendKind kind]
+pprHsTyVarBndr name kind | kind `eqKind` liftedTypeKind = ppr name
+                        | otherwise                    = hsep [ppr name, dcolon, pprParendKind kind]
 
 pprHsForAll []  []  = empty
 pprHsForAll tvs cxt 
@@ -238,8 +266,7 @@ ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
               (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
 
 ppr_mono_ty ctxt_prec (HsPredTy pred) 
-  = maybeParen (ctxt_prec >= pREC_FUN) $
-    braces (ppr pred)
+  = braces (ppr pred)
 
 ppr_mono_ty ctxt_prec (HsUsageTy u ty)
   = maybeParen (ctxt_prec >= pREC_CON)
@@ -275,19 +302,18 @@ toHsType (TyVarTy tv)    = HsTyVar (getName tv)
 toHsType (FunTy arg res) = HsFunTy (toHsType arg) (toHsType res)
 toHsType (AppTy fun arg) = HsAppTy (toHsType fun) (toHsType arg) 
 
-toHsType (NoteTy (SynNote syn_ty) real_ty)
-  | syn_matches = toHsType syn_ty             -- Use synonyms if possible!!
-  | otherwise   = 
+toHsType (NoteTy (SynNote ty@(TyConApp tycon tyargs)) real_ty)
+  | isNewTyCon tycon = toHsType ty
+  | syn_matches      = toHsType ty             -- Use synonyms if possible!!
+  | otherwise        = 
 #ifdef DEBUG
-                  pprTrace "WARNING: synonym info lost in .hi file for " (ppr syn_ty) $
+                       pprTrace "WARNING: synonym info lost in .hi file for " (ppr syn_ty) $
 #endif
-                  toHsType real_ty              -- but drop it if not.
+                       toHsType real_ty              -- but drop it if not.
   where
-    syn_matches               = ty_from_syn == real_ty
-
-    TyConApp syn_tycon tyargs = syn_ty
-    (tyvars,ty)               = getSynTyConDefn syn_tycon
-    ty_from_syn               = substTy (mkTyVarSubst tyvars tyargs) ty
+    syn_matches      = ty_from_syn `tcEqType` real_ty
+    (tyvars,syn_ty)  = getSynTyConDefn tycon
+    ty_from_syn      = substTyWith tyvars tyargs syn_ty
 
     -- We only use the type synonym in the file if this doesn't cause
     -- us to lose important information.  This matters for usage
@@ -300,9 +326,10 @@ toHsType (NoteTy (SynNote syn_ty) real_ty)
     -- error messages, but it's too much work for right now.
     -- KSW 2000-07.
 
-toHsType (NoteTy _ ty)           = toHsType ty
+toHsType (NoteTy _ ty)        = toHsType ty
 
-toHsType (PredTy p)              = HsPredTy (toHsPred p)
+toHsType (SourceTy (NType tc tys)) = foldl HsAppTy (HsTyVar (getName tc)) (map toHsType tys)
+toHsType (SourceTy pred)          = HsPredTy (toHsPred pred)
 
 toHsType ty@(TyConApp tc tys)  -- Must be saturated because toHsType's arg is of kind *
   | not saturated             = generic_case
@@ -314,9 +341,9 @@ toHsType ty@(TyConApp tc tys)       -- Must be saturated because toHsType's arg is of
   where
      generic_case = foldl HsAppTy (HsTyVar (getName tc)) tys'
      tys'         = map toHsType tys
-     saturated    = length tys == tyConArity tc
+     saturated    = tys `lengthIs` tyConArity tc
 
-toHsType ty@(ForAllTy _ _) = case splitSigmaTy ty of
+toHsType ty@(ForAllTy _ _) = case tcSplitSigmaTy ty of
                                (tvs, preds, tau) -> HsForAllTy (Just (map toHsTyVar tvs))
                                                                (map toHsPred preds)
                                                                (toHsType tau)
@@ -325,11 +352,11 @@ toHsType (UsageTy u ty) = HsUsageTy (toHsType u) (toHsType ty)
                           -- **! consider dropping usMany annotations ToDo KSW 2000-10
 
 
-toHsPred (Class cls tys) = HsPClass (getName cls) (map toHsType tys)
-toHsPred (IParam n ty)  = HsPIParam (getName n)  (toHsType ty)
+toHsPred (ClassP cls tys) = HsClassP (getName cls) (map toHsType tys)
+toHsPred (IParam n ty)    = HsIParam n            (toHsType ty)
 
-toHsContext :: ClassContext -> HsContext Name
-toHsContext cxt = [HsPClass (getName cls) (map toHsType tys) | (cls,tys) <- cxt]
+toHsContext :: ThetaType -> HsContext Name
+toHsContext theta = map toHsPred theta
 
 toHsFDs :: [FunDep TyVar] -> [FunDep Name]
 toHsFDs fds = [(map getName ns, map getName ms) | (ns,ms) <- fds]
@@ -385,7 +412,7 @@ eq_hsTyVars env (tv1:tvs1) (tv2:tvs2)  k = eq_hsTyVar env tv1 tv2 $ \ env ->
 eq_hsTyVars env _ _ _ = False
 
 eq_hsTyVar env (UserTyVar v1)     (UserTyVar v2)     k = k (extendEqHsEnv env v1 v2)
-eq_hsTyVar env (IfaceTyVar v1 k1) (IfaceTyVar v2 k2) k = k1 == k2 && k (extendEqHsEnv env v1 v2)
+eq_hsTyVar env (IfaceTyVar v1 k1) (IfaceTyVar v2 k2) k = k1 `eqKind` k2 && k (extendEqHsEnv env v1 v2)
 eq_hsTyVar env _ _ _ = False
 
 eq_hsVars env []       []       k = k env
@@ -439,15 +466,9 @@ eq_hsType env ty1 ty2 = False
 eq_hsContext env a b = eqListBy (eq_hsPred env) a b
 
 -------------------
-eq_hsPred env (HsPClass c1 tys1) (HsPClass c2 tys2)
+eq_hsPred env (HsClassP c1 tys1) (HsClassP c2 tys2)
   = c1 == c2 &&  eq_hsTypes env tys1 tys2
-eq_hsPred env (HsPIParam n1 ty1) (HsPIParam n2 ty2)
+eq_hsPred env (HsIParam n1 ty1) (HsIParam n2 ty2)
   = n1 == n2 && eq_hsType env ty1 ty2
 eq_hsPred env _ _ = False
-
--------------------
-eqListBy :: (a->a->Bool) -> [a] -> [a] -> Bool
-eqListBy eq []     []     = True
-eqListBy eq (x:xs) (y:ys) = eq x y && eqListBy eq xs ys
-eqListBy eq xs     ys     = False
 \end{code}