[project @ 2001-10-19 10:04:37 by sewardj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcExpr.lhs
index 37fdce6..cb57efd 100644 (file)
@@ -9,60 +9,58 @@ module TcExpr ( tcApp, tcExpr, tcMonoExpr, tcPolyExpr, tcId ) where
 #include "HsVersions.h"
 
 import HsSyn           ( HsExpr(..), HsLit(..), ArithSeqInfo(..), 
-                         HsMatchContext(..), mkMonoBind
+                         HsMatchContext(..), HsDoContext(..), mkMonoBind
                        )
 import RnHsSyn         ( RenamedHsExpr, RenamedRecordBinds )
 import TcHsSyn         ( TcExpr, TcRecordBinds, mkHsLet )
 
 import TcMonad
-import BasicTypes      ( RecFlag(..) )
-
+import BasicTypes      ( RecFlag(..),  isMarkedStrict )
 import Inst            ( InstOrigin(..), 
                          LIE, mkLIE, emptyLIE, unitLIE, plusLIE, plusLIEs,
                          newOverloadedLit, newMethod, newIPDict,
-                         newDicts, newClassDicts,
+                         newDicts, 
                          instToId, tcInstId
                        )
 import TcBinds         ( tcBindsAndThen )
 import TcEnv           ( tcLookupClass, tcLookupGlobalId, tcLookupGlobal_maybe,
                          tcLookupTyCon, tcLookupDataCon, tcLookupId,
-                         tcExtendGlobalTyVars, tcLookupSyntaxName
+                         tcExtendGlobalTyVars
                        )
 import TcMatches       ( tcMatchesCase, tcMatchLambda, tcStmts )
-import TcMonoType      ( tcHsSigType, checkSigTyVars, sigCtxt )
+import TcMonoType      ( tcHsSigType, UserTypeCtxt(..), checkSigTyVars, sigCtxt )
 import TcPat           ( badFieldCon, simpleHsLitTy )
 import TcSimplify      ( tcSimplifyCheck, tcSimplifyIPs )
-import TcType          ( TcType, TcTauType,
-                         tcInstTyVars, tcInstType, 
-                         newTyVarTy, newTyVarTys, zonkTcType )
-
-import FieldLabel      ( fieldLabelName, fieldLabelType, fieldLabelTyCon )
-import Id              ( idType, recordSelectorFieldLabel, isRecordSelector )
-import DataCon         ( dataConFieldLabels, dataConSig, 
-                         dataConStrictMarks, StrictnessMark(..)
+import TcMType         ( tcInstTyVars, tcInstType, 
+                         newTyVarTy, newTyVarTys, zonkTcType,
+                         unifyTauTy, unifyFunTy, unifyListTy, unifyTupleTy
                        )
-import Name            ( Name )
-import Type            ( mkFunTy, mkAppTy, mkTyConTy,
-                         splitFunTy_maybe, splitFunTys,
-                         mkTyConApp, splitSigmaTy, 
+import TcType          ( tcSplitFunTys, tcSplitTyConApp,
+                         isQualifiedTy, 
+                         mkFunTy, mkAppTy, mkTyConTy,
+                         mkTyConApp, mkClassPred, tcFunArgTy,
                          isTauTy, tyVarsOfType, tyVarsOfTypes, 
-                         isSigmaTy, splitAlgTyConApp, splitAlgTyConApp_maybe,
                          liftedTypeKind, openTypeKind, mkArrowKind,
+                         tcSplitSigmaTy, tcTyConAppTyCon,
                          tidyOpenType
                        )
-import TyCon           ( TyCon, tyConTyVars )
-import Subst           ( mkTopTyVarSubst, substClasses, substTy )
+import FieldLabel      ( FieldLabel, fieldLabelName, fieldLabelType, fieldLabelTyCon )
+import Id              ( idType, recordSelectorFieldLabel, isRecordSelector )
+import DataCon         ( dataConFieldLabels, dataConSig, 
+                         dataConStrictMarks
+                       )
+import Name            ( Name )
+import TyCon           ( TyCon, tyConTyVars, isAlgTyCon, tyConDataCons )
+import Subst           ( mkTopTyVarSubst, substTheta, substTy )
 import VarSet          ( elemVarSet )
 import TysWiredIn      ( boolTy, mkListTy, listTyCon )
-import TcUnify         ( unifyTauTy, unifyFunTy, unifyListTy, unifyTupleTy )
 import PrelNames       ( cCallableClassName, 
                          cReturnableClassName, 
-                         enumFromName, enumFromThenName, negateName,
+                         enumFromName, enumFromThenName, 
                          enumFromToName, enumFromThenToName,
                          thenMName, failMName, returnMName, ioTyConName
                        )
 import Outputable
-import Maybes          ( maybeToBool, mapMaybe )
 import ListSetOps      ( minusList )
 import Util
 import CmdLineOpts
@@ -81,12 +79,12 @@ tcExpr :: RenamedHsExpr                     -- Expession to type check
        -> TcType                       -- Expected type (could be a polytpye)
        -> TcM (TcExpr, LIE)
 
-tcExpr expr ty | isSigmaTy ty = -- Polymorphic case
-                               tcPolyExpr expr ty      `thenTc` \ (expr', lie, _, _, _) ->
-                               returnTc (expr', lie)
+tcExpr expr ty | isQualifiedTy ty = -- Polymorphic case
+                                   tcPolyExpr expr ty  `thenTc` \ (expr', lie, _, _, _) ->
+                                   returnTc (expr', lie)
 
-              | otherwise    = -- Monomorphic case
-                               tcMonoExpr expr ty
+              | otherwise        = -- Monomorphic case
+                                   tcMonoExpr expr ty
 \end{code}
 
 
@@ -195,9 +193,8 @@ tcMonoExpr (HsLit lit)     res_ty = tcLit lit res_ty
 tcMonoExpr (HsOverLit lit) res_ty = newOverloadedLit (LiteralOrigin lit) lit res_ty
 tcMonoExpr (HsPar expr)    res_ty = tcMonoExpr expr res_ty
 
-tcMonoExpr (NegApp expr) res_ty
-  = tcLookupSyntaxName negateName      `thenNF_Tc` \ neg ->
-    tcMonoExpr (HsApp (HsVar neg) expr) res_ty
+tcMonoExpr (NegApp expr neg_name) res_ty
+  = tcMonoExpr (HsApp (HsVar neg_name) expr) res_ty
 
 tcMonoExpr (HsLam match) res_ty
   = tcMatchLambda match res_ty                 `thenTc` \ (match',lie) ->
@@ -261,15 +258,25 @@ arg/result types); unify them with the args/result; and store them for
 later use.
 
 \begin{code}
-tcMonoExpr (HsCCall lbl args may_gc is_asm ignored_fake_result_ty) res_ty
-  =    -- Get the callable and returnable classes.
+tcMonoExpr e0@(HsCCall lbl args may_gc is_casm ignored_fake_result_ty) res_ty
+
+  = getDOptsTc                         `thenNF_Tc` \ dflags ->
+
+    checkTc (not (is_casm && dopt_HscLang dflags /= HscC)) 
+        (vcat [text "_casm_ is only supported when compiling via C (-fvia-C).",
+               text "Either compile with -fvia-C, or, better, rewrite your code",
+               text "to use the foreign function interface.  _casm_s are deprecated",
+               text "and support for them may one day disappear."])
+                                       `thenTc_`
+
+    -- Get the callable and returnable classes.
     tcLookupClass cCallableClassName   `thenNF_Tc` \ cCallableClass ->
     tcLookupClass cReturnableClassName `thenNF_Tc` \ cReturnableClass ->
     tcLookupTyCon ioTyConName          `thenNF_Tc` \ ioTyCon ->
     let
        new_arg_dict (arg, arg_ty)
-         = newClassDicts (CCallOrigin (_UNPK_ lbl) (Just arg))
-                         [(cCallableClass, [arg_ty])]  `thenNF_Tc` \ arg_dicts ->
+         = newDicts (CCallOrigin (_UNPK_ lbl) (Just arg))
+                    [mkClassPred cCallableClass [arg_ty]]      `thenNF_Tc` \ arg_dicts ->
            returnNF_Tc arg_dicts       -- Actually a singleton bag
 
        result_origin = CCallOrigin (_UNPK_ lbl) Nothing {- Not an arg -}
@@ -295,8 +302,8 @@ tcMonoExpr (HsCCall lbl args may_gc is_asm ignored_fake_result_ty) res_ty
        -- Construct the extra insts, which encode the
        -- constraints on the argument and result types.
     mapNF_Tc new_arg_dict (zipEqual "tcMonoExpr:CCall" args arg_tys)   `thenNF_Tc` \ ccarg_dicts_s ->
-    newClassDicts result_origin [(cReturnableClass, [result_ty])]      `thenNF_Tc` \ ccres_dict ->
-    returnTc (HsCCall lbl args' may_gc is_asm io_result_ty,
+    newDicts result_origin [mkClassPred cReturnableClass [result_ty]]  `thenNF_Tc` \ ccres_dict ->
+    returnTc (HsCCall lbl args' may_gc is_casm io_result_ty,
              mkLIE (ccres_dict ++ concat ccarg_dicts_s) `plusLIE` args_lie)
 \end{code}
 
@@ -359,10 +366,10 @@ tcMonoExpr expr@(HsDo do_or_lc stmts src_loc) res_ty
 \end{code}
 
 \begin{code}
-tcMonoExpr in_expr@(ExplicitList exprs) res_ty -- Non-empty list
+tcMonoExpr in_expr@(ExplicitList _ exprs) res_ty       -- Non-empty list
   = unifyListTy res_ty                        `thenTc` \ elt_ty ->  
     mapAndUnzipTc (tc_elt elt_ty) exprs              `thenTc` \ (exprs', lies) ->
-    returnTc (ExplicitListOut elt_ty exprs', plusLIEs lies)
+    returnTc (ExplicitList elt_ty exprs', plusLIEs lies)
   where
     tc_elt elt_ty expr
       = tcAddErrCtxt (listCtxt expr) $
@@ -379,10 +386,10 @@ tcMonoExpr expr@(RecordCon con_name rbinds) res_ty
   = tcAddErrCtxt (recordConCtxt expr)          $
     tcId con_name                      `thenNF_Tc` \ (con_expr, con_lie, con_tau) ->
     let
-       (_, record_ty) = splitFunTys con_tau
-       (tycon, ty_args, _) = splitAlgTyConApp record_ty
+       (_, record_ty)   = tcSplitFunTys con_tau
+       (tycon, ty_args) = tcSplitTyConApp record_ty
     in
-    ASSERT( maybeToBool (splitAlgTyConApp_maybe record_ty ) )
+    ASSERT( isAlgTyCon tycon )
     unifyTauTy res_ty record_ty          `thenTc_`
 
        -- Check that the record bindings match the constructor
@@ -400,14 +407,11 @@ tcMonoExpr expr@(RecordCon con_name rbinds) res_ty
     tcRecordBinds tycon ty_args rbinds         `thenTc` \ (rbinds', rbinds_lie) ->
     
     let
-      missing_s_fields = missingStrictFields rbinds data_con
+      (missing_s_fields, missing_fields) = missingFields rbinds data_con
     in
     checkTcM (null missing_s_fields)
        (mapNF_Tc (addErrTc . missingStrictFieldCon con_name) missing_s_fields `thenNF_Tc_`
         returnNF_Tc ())  `thenNF_Tc_`
-    let
-      missing_fields = missingFields rbinds data_con
-    in
     doptsTc Opt_WarnMissingFields `thenNF_Tc` \ warn ->
     checkTcM (not (warn && not (null missing_fields)))
        (mapNF_Tc ((warnTc True) . missingFieldCon con_name) missing_fields `thenNF_Tc_`
@@ -464,11 +468,13 @@ tcMonoExpr expr@(RecordUpd record_expr rbinds) res_ty
        -- STEP 1
        -- Figure out the tycon and data cons from the first field name
     let
-       (Just (AnId sel_id) : _)  = maybe_sel_ids
-       (_, _, tau)               = splitSigmaTy (idType sel_id)        -- Selectors can be overloaded
+               -- It's OK to use the non-tc splitters here (for a selector)
+       (Just (AnId sel_id) : _)    = maybe_sel_ids
+       (_, _, tau)                 = tcSplitSigmaTy (idType sel_id)    -- Selectors can be overloaded
                                                                        -- when the data type has a context
-       Just (data_ty, _)         = splitFunTy_maybe tau        -- Must succeed since sel_id is a selector
-       (tycon, _, data_cons)       = splitAlgTyConApp data_ty
+       data_ty                     = tcFunArgTy tau                    -- Must succeed since sel_id is a selector
+       tycon                       = tcTyConAppTyCon data_ty
+       data_cons                   = tyConDataCons tycon
        (con_tyvars, _, _, _, _, _) = dataConSig (head data_cons)
     in
     tcInstTyVars con_tyvars                    `thenNF_Tc` \ (_, result_inst_tys, _) ->
@@ -532,12 +538,12 @@ tcMonoExpr expr@(RecordUpd record_expr rbinds) res_ty
     let
        (tyvars, theta, _, _, _, _) = dataConSig (head data_cons)
        inst_env = mkTopTyVarSubst tyvars result_inst_tys
-       theta'   = substClasses inst_env theta
+       theta'   = substTheta inst_env theta
     in
-    newClassDicts RecordUpdOrigin theta'       `thenNF_Tc` \ dicts ->
+    newDicts RecordUpdOrigin theta'    `thenNF_Tc` \ dicts ->
 
        -- Phew!
-    returnTc (RecordUpdOut record_expr' result_record_ty (map instToId dicts) rbinds', 
+    returnTc (RecordUpdOut record_expr' record_ty result_record_ty (map instToId dicts) rbinds', 
              mkLIE dicts `plusLIE` record_lie `plusLIE` rbinds_lie)
 
 tcMonoExpr (ArithSeqIn seq@(From expr)) res_ty
@@ -597,10 +603,10 @@ tcMonoExpr in_expr@(ArithSeqIn seq@(FromThenTo expr1 expr2 expr3)) res_ty
 
 \begin{code}
 tcMonoExpr in_expr@(ExprWithTySig expr poly_ty) res_ty
- = tcSetErrCtxt (exprSigCtxt in_expr)  $
-   tcHsSigType  poly_ty                `thenTc` \ sig_tc_ty ->
+ = tcHsSigType ExprSigCtxt poly_ty     `thenTc` \ sig_tc_ty ->
 
-   if not (isSigmaTy sig_tc_ty) then
+   tcAddErrCtxt (exprSigCtxt in_expr)  $
+   if not (isQualifiedTy sig_tc_ty) then
        -- Easy case
        unifyTauTy sig_tc_ty res_ty     `thenTc_`
        tcMonoExpr expr sig_tc_ty
@@ -629,7 +635,10 @@ Implicit Parameter bindings.
 tcMonoExpr (HsWith expr binds) res_ty
   = tcMonoExpr expr res_ty                     `thenTc` \ (expr', expr_lie) ->
     mapAndUnzipTc tcIPBind binds               `thenTc` \ (pairs, bind_lies) ->
-    tcSimplifyIPs (map fst binds) expr_lie     `thenTc` \ (expr_lie', dict_binds) ->
+
+       -- If the binding binds ?x = E, we  must now 
+       -- discharge any ?x constraints in expr_lie
+    tcSimplifyIPs (map fst pairs) expr_lie     `thenTc` \ (expr_lie', dict_binds) ->
     let
        binds' = [(instToId ip, rhs) | (ip,rhs) <- pairs]
        expr'' = HsLet (mkMonoBind dict_binds [] Recursive) expr'
@@ -692,8 +701,8 @@ checkArgsCtxt fun args expected_res_ty actual_res_ty tidy_env
     let
       (env1, exp_ty'') = tidyOpenType tidy_env exp_ty'
       (env2, act_ty'') = tidyOpenType env1     act_ty'
-      (exp_args, _) = splitFunTys exp_ty''
-      (act_args, _) = splitFunTys act_ty''
+      (exp_args, _)    = tcSplitFunTys exp_ty''
+      (act_args, _)    = tcSplitFunTys act_ty''
 
       message | length exp_args < length act_args = wrongArgsCtxt "too few" fun args
               | length exp_args > length act_args = wrongArgsCtxt "too many" fun args
@@ -774,11 +783,11 @@ tcDoStmts do_or_lc stmts src_loc res_ty
 
        _       -> newTyVarTy (mkArrowKind liftedTypeKind liftedTypeKind)       `thenNF_Tc` \ m_ty ->
                   newTyVarTy liftedTypeKind                                    `thenNF_Tc` \ elt_ty ->
-                  unifyTauTy res_ty (mkAppTy m_ty elt_ty)                              `thenTc_`
+                  unifyTauTy res_ty (mkAppTy m_ty elt_ty)                      `thenTc_`
                   returnNF_Tc (m_ty, (mkAppTy m_ty, elt_ty))
     )                                                  `thenNF_Tc` \ (tc_ty, m_ty) ->
 
-    tcStmts do_or_lc m_ty stmts                                `thenTc`   \ (stmts', stmts_lie) ->
+    tcStmts (DoCtxt do_or_lc) m_ty stmts               `thenTc`   \ (stmts', stmts_lie) ->
 
        -- Build the then and zero methods in case we need them
        -- It's important that "then" and "return" appear just once in the final LIE,
@@ -865,35 +874,32 @@ badFields rbinds data_con
   where
     field_names = map fieldLabelName (dataConFieldLabels data_con)
 
-missingStrictFields rbinds data_con
-  = [ fn | fn <- strict_field_names,
-                not (fn `elem` field_names_used)
-    ]
-  where
-    field_names_used = [ field_name | (field_name, _, _) <- rbinds ]
-    strict_field_names = mapMaybe isStrict field_info
-
-    isStrict (fl, MarkedStrict) = Just (fieldLabelName fl)
-    isStrict _                 = Nothing
-
-    field_info = zip (dataConFieldLabels data_con)
-                    (dataConStrictMarks data_con)
-
 missingFields rbinds data_con
-  = [ fn | fn <- non_strict_field_names, not (fn `elem` field_names_used) ]
+  | null field_labels = ([], [])       -- Not declared as a record;
+                                       -- But C{} is still valid
+  | otherwise  
+  = (missing_strict_fields, other_missing_fields)
   where
-    field_names_used = [ field_name | (field_name, _, _) <- rbinds ]
-
-     -- missing strict fields have already been flagged as 
-     -- being so, so leave them out here.
-    non_strict_field_names = mapMaybe isn'tStrict field_info
-
-    isn'tStrict (fl, MarkedStrict) = Nothing
-    isn'tStrict (fl, _)            = Just (fieldLabelName fl)
-
-    field_info = zip (dataConFieldLabels data_con)
-                    (dataConStrictMarks data_con)
+    missing_strict_fields
+       = [ fl | (fl, str) <- field_info,
+                isMarkedStrict str,
+                not (fieldLabelName fl `elem` field_names_used)
+         ]
+    other_missing_fields
+       = [ fl | (fl, str) <- field_info,
+                not (isMarkedStrict str),
+                not (fieldLabelName fl `elem` field_names_used)
+         ]
 
+    field_names_used = [ field_name | (field_name, _, _) <- rbinds ]
+    field_labels     = dataConFieldLabels data_con
+
+    field_info = zipEqual "missingFields"
+                         field_labels
+                         (drop (length ex_theta) (dataConStrictMarks data_con))
+       -- The 'drop' is because dataConStrictMarks
+       -- includes the existential dictionaries
+    (_, _, _, ex_theta, _, _) = dataConSig data_con
 \end{code}
 
 %************************************************************************
@@ -925,8 +931,8 @@ Overloaded literals.
 tcLit :: HsLit -> TcType -> TcM (TcExpr, LIE)
 tcLit (HsLitLit s _) res_ty
   = tcLookupClass cCallableClassName                   `thenNF_Tc` \ cCallableClass ->
-    newClassDicts (LitLitOrigin (_UNPK_ s))
-                 [(cCallableClass,[res_ty])]           `thenNF_Tc` \ dicts ->
+    newDicts (LitLitOrigin (_UNPK_ s))
+            [mkClassPred cCallableClass [res_ty]]      `thenNF_Tc` \ dicts ->
     returnTc (HsLit (HsLitLit s res_ty), mkLIE dicts)
 
 tcLit lit res_ty 
@@ -943,11 +949,6 @@ tcLit lit res_ty
 
 Mini-utils:
 
-\begin{code}
-pp_nest_hang :: String -> SDoc -> SDoc
-pp_nest_hang lbl stuff = nest 2 (hang (text lbl) 4 stuff)
-\end{code}
-
 Boring and alphabetical:
 \begin{code}
 arithSeqCtxt expr
@@ -1010,12 +1011,12 @@ recordConCtxt expr = ptext SLIT("In the record construction:") <+> ppr expr
 notSelector field
   = hsep [quotes (ppr field), ptext SLIT("is not a record selector")]
 
-missingStrictFieldCon :: Name -> Name -> SDoc
+missingStrictFieldCon :: Name -> FieldLabel -> SDoc
 missingStrictFieldCon con field
   = hsep [ptext SLIT("Constructor") <+> quotes (ppr con),
          ptext SLIT("does not have the required strict field"), quotes (ppr field)]
 
-missingFieldCon :: Name -> Name -> SDoc
+missingFieldCon :: Name -> FieldLabel -> SDoc
 missingFieldCon con field
   = hsep [ptext SLIT("Field") <+> quotes (ppr field),
          ptext SLIT("is not initialised")]