[project @ 2004-12-24 11:02:39 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcTyClsDecls.lhs
index e4bc357..b008bbe 100644 (file)
@@ -21,7 +21,7 @@ import HscTypes               ( implicitTyThings )
 import BuildTyCl       ( buildClass, buildAlgTyCon, buildSynTyCon, buildDataCon,
                          mkDataTyConRhs, mkNewTyConRhs )
 import TcRnMonad
-import TcEnv           ( TcTyThing(..), TyThing(..), 
+import TcEnv           ( TyThing(..), 
                          tcLookupLocated, tcLookupLocatedGlobal, 
                          tcExtendGlobalEnv, tcExtendKindEnv,
                          tcExtendRecEnv, tcLookupTyVar )
@@ -29,7 +29,7 @@ import TcTyDecls      ( calcTyConArgVrcs, calcRecFlags, calcClassCycles, calcSynCycle
 import TcClassDcl      ( tcClassSigs, tcAddDeclCtxt )
 import TcHsType                ( kcHsTyVars, kcHsLiftedSigType, kcHsType, 
                          kcHsContext, tcTyVarBndrs, tcHsKindedType, tcHsKindedContext,
-                         kcHsSigType, tcHsBangType, tcLHsConSig )
+                         kcHsSigType, tcHsBangType, tcLHsConSig, tcDataKindSig )
 import TcMType         ( newKindVar, checkValidTheta, checkValidType, checkFreeness, 
                          UserTypeCtxt(..), SourceTyCtxt(..) ) 
 import TcUnify         ( unifyKind )
@@ -41,7 +41,7 @@ import Generics               ( validGenericMethodType, canDoGenerics )
 import Class           ( Class, className, classTyCon, DefMeth(..), classBigSig, classTyVars )
 import TyCon           ( TyCon, ArgVrcs, 
                          tyConDataCons, mkForeignTyCon, isProductTyCon, isRecursiveTyCon,
-                         tyConStupidTheta, getSynTyConDefn, tyConDataCons, isSynTyCon, tyConName )
+                         tyConStupidTheta, getSynTyConDefn, isSynTyCon, tyConName )
 import DataCon         ( DataCon, dataConWrapId, dataConName, dataConSig, 
                          dataConFieldLabels, dataConOrigArgTys, dataConTyCon )
 import Type            ( zipTopTvSubst, substTys )
@@ -115,7 +115,8 @@ tcTyAndClassDecls boot_names decls
   = do {       -- First check for cyclic type synonysm or classes
                -- See notes with checkCycleErrs
          checkCycleErrs decls
-
+       ; mod <- getModule
+       ; traceTc (text "tcTyAndCl" <+> ppr mod <+> ppr boot_names)
        ; (syn_tycons, alg_tyclss) <- fixM (\ ~(rec_syn_tycons, rec_alg_tyclss) ->
          do    { let { -- Calculate variances and rec-flag
                      ; (syn_decls, alg_decls) = partition (isSynDecl . unLoc) decls }
@@ -327,7 +328,7 @@ kcTyClDeclBody decl thing_inside
        ; thing_inside kinded_tvs }
   where
     result_kind (TyData { tcdKindSig = Just kind }) = kind
-    result_kind other                             = liftedTypeKind
+    result_kind other                              = liftedTypeKind
        -- On GADT-style declarations we allow a kind signature
        --      data T :: *->* where { ... }
 
@@ -365,18 +366,23 @@ tcTyClDecl calc_vrcs calc_isrec decl
 
 tcTyClDecl1 calc_vrcs calc_isrec 
   (TyData {tcdND = new_or_data, tcdCtxt = ctxt, tcdTyVars = tvs,
-          tcdLName = L _ tc_name, tcdCons = cons})
-  = tcTyVarBndrs tvs           $ \ tvs' -> do 
-  { stupid_theta <- tcStupidTheta ctxt cons
+          tcdLName = L _ tc_name, tcdKindSig = mb_ksig, tcdCons = cons})
+  = tcTyVarBndrs tvs   $ \ tvs' -> do 
+  { extra_tvs <- tcDataKindSig mb_ksig
+  ; let final_tvs = tvs' ++ extra_tvs
+  ; stupid_theta <- tcStupidTheta ctxt cons
   ; want_generic <- doptM Opt_Generics
   ; tycon <- fixM (\ tycon -> do 
        { unbox_strict <- doptM Opt_UnboxStrictFields
-       ; data_cons <- mappM (addLocM (tcConDecl unbox_strict new_or_data tycon tvs')) cons
+       ; gla_exts <- doptM Opt_GlasgowExts
+       ; checkTc (gla_exts || h98_syntax) (badGadtDecl tc_name)
+
+       ; data_cons <- mappM (addLocM (tcConDecl unbox_strict new_or_data tycon final_tvs)) cons
        ; let tc_rhs = case new_or_data of
                        DataType -> mkDataTyConRhs stupid_theta data_cons
                        NewType  -> ASSERT( isSingleton data_cons )
                                    mkNewTyConRhs tycon (head data_cons)
-       ; buildAlgTyCon tc_name tvs' tc_rhs arg_vrcs is_rec
+       ; buildAlgTyCon tc_name final_tvs tc_rhs arg_vrcs is_rec
                        (want_generic && canDoGenerics data_cons)
        })
   ; return (ATyCon tycon)
@@ -384,6 +390,9 @@ tcTyClDecl1 calc_vrcs calc_isrec
   where
     arg_vrcs = calc_vrcs tc_name
     is_rec   = calc_isrec tc_name
+    h98_syntax = case cons of  -- All constructors have same shape
+                       L _ (GadtDecl {}) : _ -> False
+                       other -> True
 
 tcTyClDecl1 calc_vrcs calc_isrec 
   (ClassDecl {tcdLName = L _ class_name, tcdTyVars = tvs, 
@@ -419,13 +428,30 @@ tcConDecl :: Bool                 -- True <=> -funbox-strict_fields
          -> NewOrData -> TyCon -> [TyVar]
          -> ConDecl Name -> TcM DataCon
 
-tcConDecl unbox_strict new_or_data tycon tc_tvs
+tcConDecl unbox_strict NewType tycon tc_tvs    -- Newtypes
+         (ConDecl name ex_tvs ex_ctxt details)
+  = ASSERT( null ex_tvs && null (unLoc ex_ctxt) )      
+    do { let tc_datacon field_lbls arg_ty
+               = do { arg_ty' <- tcHsKindedType arg_ty -- No bang on newtype
+                    ; buildDataCon (unLoc name) False {- Prefix -} 
+                                   True {- Vanilla -} [NotMarkedStrict]
+                                   (map unLoc field_lbls)
+                                   tc_tvs [] [arg_ty']
+                                   tycon (mkTyVarTys tc_tvs) }
+       ; case details of
+           PrefixCon [arg_ty] -> tc_datacon [] arg_ty
+           RecCon [(field_lbl, arg_ty)] -> tc_datacon [field_lbl] arg_ty }
+
+tcConDecl unbox_strict DataType tycon tc_tvs   -- Ordinary data types
          (ConDecl name ex_tvs ex_ctxt details)
   = tcTyVarBndrs ex_tvs                $ \ ex_tvs' -> do 
     { ex_ctxt' <- tcHsKindedContext ex_ctxt
     ; let 
        is_vanilla = null ex_tvs && null (unLoc ex_ctxt) 
                -- Vanilla iff no ex_tvs and no context
+               -- Must check the context too because of
+               -- implicit params; e.g.
+               --  data T = (?x::Int) => MkT Int
 
        tc_datacon is_infix field_lbls btys
          = do { let { bangs = map getBangStrictness btys }
@@ -440,11 +466,14 @@ tcConDecl unbox_strict new_or_data tycon tc_tvs
     ; case details of
        PrefixCon btys     -> tc_datacon False [] btys
        InfixCon bty1 bty2 -> tc_datacon True [] [bty1,bty2]
-       RecCon fields      -> do { checkTc is_vanilla (exRecConErr name)
+       RecCon fields      -> do { checkTc (null ex_tvs) (exRecConErr name)
+               -- It's ok to have an implicit-parameter context
+               -- for the data constructor, provided it binds
+               -- no type variables
                                 ; let { (field_names, btys) = unzip fields }
                                 ; tc_datacon False field_names btys } }
 
-tcConDecl unbox_strict new_or_data tycon tc_tvs
+tcConDecl unbox_strict DataType tycon tc_tvs   -- GADTs
          decl@(GadtDecl name con_ty)
   = do { traceTc (text "tcConDecl"  <+> ppr name)
        ; (tvs, theta, bangs, arg_tys, tc, res_tys) <- tcLHsConSig con_ty
@@ -466,6 +495,7 @@ tcConDecl unbox_strict new_or_data tycon tc_tvs
                       [{- No field labels -}]
                       tvs' theta arg_tys' tycon res_tys' }
 
+-------------------
 tcStupidTheta :: LHsContext Name -> [LConDecl Name] -> TcM (Maybe ThetaType)
 -- For GADTs we don't allow a context on the data declaration
 -- whereas for standard Haskell style data declarations, we do
@@ -584,7 +614,7 @@ checkValidDataCon tc con
 --     ; checkFreeness tvs ex_theta }
   where
     ctxt = ConArgCtxt (dataConName con) 
-    (tvs, ex_theta, _, _, _) = dataConSig con
+--    (tvs, ex_theta, _, _, _) = dataConSig con
 
 
 -------------------------------
@@ -628,7 +658,7 @@ checkValidClass cls
 
                -- Check that for a generic method, the type of 
                -- the method is sufficiently simple
-       ; checkTc (dm /= GenDefMeth || validGenericMethodType op_ty)
+       ; checkTc (dm /= GenDefMeth || validGenericMethodType tau)
                  (badGenericMethodType op_name op_ty)
        }
        where
@@ -680,7 +710,7 @@ genericMultiParamErr clas
 badGenericMethodType op op_ty
   = hang (ptext SLIT("Generic method type is too complex"))
        4 (vcat [ppr op <+> dcolon <+> ppr op_ty,
-               ptext SLIT("You can only use type variables, arrows, and tuples")])
+               ptext SLIT("You can only use type variables, arrows, lists, and tuples")])
 
 recSynErr syn_decls
   = setSrcSpan (getLoc (head sorted_decls)) $
@@ -711,4 +741,8 @@ exRecConErr name
 badDataConTyCon data_con
   = hang (ptext SLIT("Data constructor does not return its parent type:"))
        2 (ppr data_con)
+
+badGadtDecl tc_name
+  = vcat [ ptext SLIT("Illegal generalised algebraic data declaration for") <+> quotes (ppr tc_name)
+        , nest 2 (parens $ ptext SLIT("Use -fglasgow-exts to allow GADTs")) ]
 \end{code}