[project @ 2005-10-14 11:22:41 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcType.lhs
index b9ff393..08d122c 100644 (file)
@@ -38,14 +38,14 @@ module TcType (
   tcSplitFunTy_maybe, tcSplitFunTys, tcFunArgTy, tcFunResultTy,
   tcSplitTyConApp, tcSplitTyConApp_maybe, tcTyConAppTyCon, tcTyConAppArgs,
   tcSplitAppTy_maybe, tcSplitAppTy, tcSplitAppTys, tcSplitSigmaTy,
-  tcGetTyVar_maybe, tcGetTyVar,
+  tcValidInstHeadTy, tcGetTyVar_maybe, tcGetTyVar,
 
   ---------------------------------
   -- Predicates. 
   -- Again, newtypes are opaque
   tcEqType, tcEqTypes, tcEqPred, tcCmpType, tcCmpTypes, tcCmpPred, tcEqTypeX,
   isSigmaTy, isOverloadedTy, 
-  isDoubleTy, isFloatTy, isIntTy,
+  isDoubleTy, isFloatTy, isIntTy, isStringTy,
   isIntegerTy, isAddrTy, isBoolTy, isUnitTy,
   isTauTy, tcIsTyVarTy, tcIsForAllTy,
 
@@ -62,6 +62,7 @@ module TcType (
   mkDictTy, tcSplitPredTy_maybe, 
   isPredTy, isDictTy, tcSplitDFunTy, tcSplitDFunHead, predTyUnique, 
   mkClassPred, isInheritablePred, isLinearPred, isIPPred, mkPredName, 
+  dataConsStupidTheta, 
 
   ---------------------------------
   -- Foreign import and export
@@ -105,7 +106,7 @@ module TcType (
 
   tidyTopType, tidyType, tidyPred, tidyTypes, tidyFreeTyVars, tidyOpenType, tidyOpenTypes,
   tidyTyVarBndr, tidyOpenTyVar, tidyOpenTyVars, tidySkolemTyVar,
-  typeKind, 
+  typeKind, tidyKind,
 
   tyVarsOfType, tyVarsOfTypes, tyVarsOfPred, tyVarsOfTheta,
 
@@ -138,7 +139,7 @@ import Type         (       -- Re-exports
                          tidyTopType, tidyType, tidyPred, tidyTypes,
                          tidyFreeTyVars, tidyOpenType, tidyOpenTypes,
                          tidyTyVarBndr, tidyOpenTyVar,
-                         tidyOpenTyVars, 
+                         tidyOpenTyVars, tidyKind,
                          isSubKind, deShadowTy,
 
                          tcEqType, tcEqTypes, tcCmpType, tcCmpTypes, 
@@ -149,22 +150,24 @@ import Type               (       -- Re-exports
                          mkOpenTvSubst, zipOpenTvSubst, zipTopTvSubst, mkTopTvSubst,
                          getTvSubstEnv, setTvSubstEnv, getTvInScope, extendTvInScope,
                          extendTvSubst, extendTvSubstList, isInScope,
-                         substTy, substTys, substTyWith, substTheta, substTyVar, substTyVarBndr,
+                         substTy, substTys, substTyWith, substTheta, 
+                         substTyVar, substTyVarBndr, substPred,
 
                          typeKind, repType,
                          pprKind, pprParendKind,
                          pprType, pprParendType, pprTyThingCategory,
                          pprPred, pprTheta, pprThetaArrow, pprClassPred
                        )
-import TyCon           ( TyCon, isUnLiftedTyCon, tyConUnique )
-import DataCon         ( DataCon )
+import TyCon           ( TyCon, isUnLiftedTyCon, isSynTyCon, tyConUnique )
+import DataCon         ( DataCon, dataConStupidTheta, dataConResTys )
 import Class           ( Class )
 import Var             ( TyVar, Id, isTcTyVar, mkTcTyVar, tyVarName, tyVarKind, tcTyVarDetails )
 import ForeignCall     ( Safety, playSafe, DNType(..) )
+import Unify           ( tcMatchTys )
 import VarSet
 
 -- others:
-import CmdLineOpts     ( DynFlags, DynFlag( Opt_GlasgowExts ), dopt )
+import DynFlags                ( DynFlags, DynFlag( Opt_GlasgowExts ), dopt )
 import Name            ( Name, NamedThing(..), mkInternalName, getSrcLoc )
 import NameSet
 import VarEnv          ( TidyEnv )
@@ -173,8 +176,10 @@ import PrelNames   -- Lots (e.g. in isFFIArgumentTy)
 import TysWiredIn      ( unitTyCon, charTyCon, listTyCon )
 import BasicTypes      ( IPName(..), ipNameName )
 import SrcLoc          ( SrcLoc, SrcSpan )
-import Util            ( snocView )
-import Maybes          ( maybeToBool, expectJust )
+import Util            ( snocView, equalLength )
+import Maybes          ( maybeToBool, expectJust, mapCatMaybes )
+import ListSetOps      ( hasNoDups )
+import List            ( nubBy )
 import Outputable
 import DATA_IOREF
 \end{code}
@@ -486,6 +491,30 @@ tcSplitTyConApp_maybe (NoteTy n ty)     = tcSplitTyConApp_maybe ty
        -- as tycon applications by the type checker
 tcSplitTyConApp_maybe other                    = Nothing
 
+tcValidInstHeadTy :: Type -> Bool
+-- Used in Haskell-98 mode, for the argument types of an instance head
+-- These must not be type synonyms, but everywhere else type synonyms
+-- are transparent, so we need a special function here
+tcValidInstHeadTy ty 
+  = case ty of
+       TyConApp tc tys -> ASSERT( not (isSynTyCon tc) ) ok tys
+                          -- A synonym would be a NoteTy
+       FunTy arg res        -> ok [arg, res]
+       NoteTy (SynNote _) _ -> False
+       NoteTy other_note ty -> tcValidInstHeadTy ty
+       other                -> False
+  where
+       -- Check that all the types are type variables,
+       -- and that each is distinct
+    ok tys = equalLength tvs tys && hasNoDups tvs
+          where
+            tvs = mapCatMaybes get_tv tys
+
+    get_tv (TyVarTy tv)          = Just tv       -- Again, do not look
+    get_tv (NoteTy (SynNote _) _) = Nothing    -- through synonyms
+    get_tv (NoteTy other_note ty) = get_tv ty
+    get_tv other                 = Nothing
+
 tcSplitFunTys :: Type -> ([Type], Type)
 tcSplitFunTys ty = case tcSplitFunTy_maybe ty of
                        Nothing        -> ([], ty)
@@ -624,6 +653,27 @@ isLinearPred (IParam (Linear n) _) = True
 isLinearPred other                = False
 \end{code}
 
+--------------------- The stupid theta (sigh) ---------------------------------
+
+\begin{code}
+dataConsStupidTheta :: [DataCon] -> ThetaType
+-- Union the stupid thetas from all the specified constructors (non-empty)
+-- All the constructors should have the same result type, modulo alpha conversion
+-- The resulting ThetaType uses type variables from the *first* constructor in the list
+--
+-- It's here because it's used in MkId.mkRecordSelId, and in TcExpr
+dataConsStupidTheta (con1:cons)
+  = nubBy tcEqPred all_preds
+  where
+    all_preds    = dataConStupidTheta con1 ++ other_stupids
+    res_tys1     = dataConResTys con1
+    tvs1         = tyVarsOfTypes res_tys1
+    other_stupids = [ substPred subst pred
+                   | con <- cons
+                   , let Just subst = tcMatchTys tvs1 res_tys1 (dataConResTys con)
+                   , pred <- dataConStupidTheta con ]
+\end{code}
+
 
 %************************************************************************
 %*                                                                     *
@@ -753,18 +803,9 @@ hoistForAllTys ty
 
 \begin{code}
 deNoteType :: Type -> Type
-       -- Remove synonyms, but not predicate types
-deNoteType ty@(TyVarTy tyvar)  = ty
-deNoteType (TyConApp tycon tys) = TyConApp tycon (map deNoteType tys)
-deNoteType (PredTy p)          = PredTy (deNotePredType p)
-deNoteType (NoteTy _ ty)       = deNoteType ty
-deNoteType (AppTy fun arg)     = AppTy (deNoteType fun) (deNoteType arg)
-deNoteType (FunTy fun arg)     = FunTy (deNoteType fun) (deNoteType arg)
-deNoteType (ForAllTy tv ty)    = ForAllTy tv (deNoteType ty)
-
-deNotePredType :: PredType -> PredType
-deNotePredType (ClassP c tys)   = ClassP c (map deNoteType tys)
-deNotePredType (IParam n ty)    = IParam n (deNoteType ty)
+-- Remove *outermost* type synonyms and other notes
+deNoteType (NoteTy _ ty) = deNoteType ty
+deNoteType ty           = ty
 \end{code}
 
 Find the free tycons and classes of a type.  This is used in the front
@@ -776,8 +817,8 @@ tyClsNamesOfType (TyVarTy tv)                   = emptyNameSet
 tyClsNamesOfType (TyConApp tycon tys)      = unitNameSet (getName tycon) `unionNameSets` tyClsNamesOfTypes tys
 tyClsNamesOfType (NoteTy (SynNote ty1) ty2) = tyClsNamesOfType ty1
 tyClsNamesOfType (NoteTy other_note    ty2) = tyClsNamesOfType ty2
-tyClsNamesOfType (PredTy (IParam n ty))   = tyClsNamesOfType ty
-tyClsNamesOfType (PredTy (ClassP cl tys)) = unitNameSet (getName cl) `unionNameSets` tyClsNamesOfTypes tys
+tyClsNamesOfType (PredTy (IParam n ty))     = tyClsNamesOfType ty
+tyClsNamesOfType (PredTy (ClassP cl tys))   = unitNameSet (getName cl) `unionNameSets` tyClsNamesOfTypes tys
 tyClsNamesOfType (FunTy arg res)           = tyClsNamesOfType arg `unionNameSets` tyClsNamesOfType res
 tyClsNamesOfType (AppTy fun arg)           = tyClsNamesOfType fun `unionNameSets` tyClsNamesOfType arg
 tyClsNamesOfType (ForAllTy tyvar ty)       = tyClsNamesOfType ty