[project @ 2001-03-19 16:15:29 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcModule.lhs
index 7987d4f..f6c9f64 100644 (file)
@@ -5,30 +5,32 @@
 
 \begin{code}
 module TcModule (
-       typecheckModule, typecheckIface, typecheckStmt, TcResults(..)
+       typecheckModule, typecheckIface, typecheckStmt, typecheckExpr,
+       TcResults(..)
     ) where
 
 #include "HsVersions.h"
 
-import CmdLineOpts     ( DynFlag(..), DynFlags, opt_PprStyle_Debug )
+import CmdLineOpts     ( DynFlag(..), DynFlags )
 import HsSyn           ( HsBinds(..), MonoBinds(..), HsDecl(..), HsExpr(..),
-                         Stmt(..), InPat(..), HsMatchContext(..),
+                         Stmt(..), InPat(..), HsMatchContext(..), RuleDecl(..),
                          isIfaceRuleDecl, nullBinds, andMonoBindList, mkSimpleMatch
                        )
-import HsTypes         ( toHsType )
 import PrelNames       ( SyntaxMap, mAIN_Name, mainName, ioTyConName, printName,
                          returnIOName, bindIOName, failIOName, 
                          itName
                        )
 import MkId            ( unsafeCoerceId )
-import RnHsSyn         ( RenamedHsBinds, RenamedHsDecl, RenamedStmt )
+import RnHsSyn         ( RenamedHsBinds, RenamedHsDecl, RenamedStmt,
+                         RenamedHsExpr )
 import TcHsSyn         ( TypecheckedMonoBinds, TypecheckedHsExpr,
                          TypecheckedForeignDecl, TypecheckedRuleDecl,
                          zonkTopBinds, zonkForeignExports, zonkRules, mkHsLet,
                          zonkExpr, zonkIdBndr
                        )
 
-
+import MkIface         ( pprModDetails )
+import TcExpr          ( tcMonoExpr )
 import TcMonad
 import TcType          ( newTyVarTy, zonkTcType, tcInstType )
 import TcMatches       ( tcStmtsAndThen )
@@ -37,38 +39,39 @@ import Inst         ( emptyLIE, plusLIE )
 import TcBinds         ( tcTopBinds )
 import TcClassDcl      ( tcClassDecls2 )
 import TcDefaults      ( tcDefaults, defaultDefaultTys )
-import TcEnv           ( TcEnv, RecTcEnv, InstInfo, tcExtendGlobalValEnv, tcLookup_maybe,
+import TcEnv           ( TcEnv, RecTcEnv, InstInfo(iDFunId), tcExtendGlobalValEnv, tcLookup_maybe,
                          isLocalThing, tcSetEnv, tcSetInstEnv, initTcEnv, getTcGEnv,
                          tcExtendGlobalTypeEnv, tcLookupGlobalId, tcLookupTyCon,
-                         TcTyThing(..), tcLookupId
+                         TcTyThing(..), tcLookupId 
                        )
 import TcRules         ( tcIfaceRules, tcSourceRules )
 import TcForeign       ( tcForeignImports, tcForeignExports )
 import TcIfaceSig      ( tcInterfaceSigs )
 import TcInstDcls      ( tcInstDecls1, tcInstDecls2 )
-import TcSimplify      ( tcSimplifyTop )
+import TcSimplify      ( tcSimplifyTop, tcSimplifyInfer )
 import TcTyClsDecls    ( tcTyAndClassDecls )
 
 import CoreUnfold      ( unfoldingTemplate, hasUnfolding )
 import TysWiredIn      ( mkListTy, unitTy )
-import Type            ( funResultTy, splitForAllTys, 
-                         liftedTypeKind, mkTyConApp, tidyType )
-import ErrUtils                ( printErrorsAndWarnings, errorsFound, dumpIfSet_dyn, showPass )
-import Id              ( Id, idType, idName, isLocalId, idUnfolding )
-import Module           ( Module, isHomeModule, moduleName )
-import Name            ( Name, toRdrName, isGlobalName )
-import Name            ( nameEnvElts, lookupNameEnv )
+import Type
+import ErrUtils                ( printErrorsAndWarnings, errorsFound, 
+                         dumpIfSet_dyn, dumpIfSet_dyn_or, showPass )
+import Id              ( Id, idType, idUnfolding )
+import Module           ( Module, moduleName )
+import Name            ( Name )
+import NameEnv         ( nameEnvElts, lookupNameEnv )
 import TyCon           ( tyConGenInfo )
-import Util
 import BasicTypes       ( EP(..), Fixity, RecFlag(..) )
 import SrcLoc          ( noSrcLoc )
 import Outputable
 import HscTypes                ( PersistentCompilerState(..), HomeSymbolTable, 
                          PackageTypeEnv, ModIface(..),
+                         ModDetails(..), DFunId,
                          TypeEnv, extendTypeEnvList, 
                          TyThing(..), implicitTyThingIds, 
                          mkTypeEnv
                        )
+import VarSet
 \end{code}
 
 
@@ -79,19 +82,23 @@ import HscTypes             ( PersistentCompilerState(..), HomeSymbolTable,
 %************************************************************************
 
 \begin{code}
-typecheckStmt :: DynFlags
-             -> PersistentCompilerState
-             -> HomeSymbolTable
-             -> TypeEnv                -- The interactive context's type envt 
-             -> PrintUnqualified       -- For error printing
-             -> Module                 -- Is this really needed
-             -> [Name]                 -- Names bound by the Stmt (empty for expressions)
-             -> (SyntaxMap,
-                 RenamedStmt,          -- The stmt itself
-                 [RenamedHsDecl])      -- Plus extra decls it sucked in from interface files
-             -> IO (Maybe (PersistentCompilerState, TypecheckedHsExpr, [Id]))
-                       -- The returned [Name] is the same as the input except for
-                       -- ExprStmt, in which case the returned [Name] is [itName]
+typecheckStmt
+   :: DynFlags
+   -> PersistentCompilerState
+   -> HomeSymbolTable
+   -> TypeEnv             -- The interactive context's type envt 
+   -> PrintUnqualified    -- For error printing
+   -> Module              -- Is this really needed
+   -> [Name]              -- Names bound by the Stmt (empty for expressions)
+   -> (SyntaxMap,
+       RenamedStmt,       -- The stmt itself
+       [RenamedHsDecl])           -- Plus extra decls it sucked in from interface files
+   -> IO (Maybe (PersistentCompilerState, 
+                TypecheckedHsExpr, 
+                [Id],
+                Type))
+               -- The returned [Id] is the same as the input except for
+               -- ExprStmt, in which case the returned [Name] is [itName]
 
 typecheckStmt dflags pcs hst ic_type_env unqual this_mod names (syn_map, stmt, iface_decls)
   = typecheck dflags syn_map pcs hst unqual $
@@ -118,11 +125,11 @@ typecheckStmt dflags pcs hst ic_type_env unqual this_mod names (syn_map, stmt, i
     ioToTc (dumpIfSet_dyn dflags Opt_D_dump_tc "Bound Ids" (vcat (map ppr zonked_ids)))        `thenNF_Tc_`
     ioToTc (dumpIfSet_dyn dflags Opt_D_dump_tc "Typechecked" (ppr zonked_expr))                `thenNF_Tc_`
 
-    returnTc (new_pcs, zonked_expr, zonked_ids)
+    returnTc (new_pcs, zonked_expr, zonked_ids, error "typecheckStmt: no type")
 
   where
     get_fixity :: Name -> Maybe Fixity
-    get_fixity n = pprPanic "typecheckExpr" (ppr n)
+    get_fixity n = pprPanic "typecheckStmt" (ppr n)
 \end{code}
 
 Here is the grand plan, implemented in tcUserStmt
@@ -209,6 +216,72 @@ tc_stmts names stmts
     combine stmt (ids, stmts) = (ids, stmt:stmts)
 \end{code}
 
+%************************************************************************
+%*                                                                     *
+\subsection{Typechecking an expression}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+typecheckExpr :: DynFlags
+             -> PersistentCompilerState
+             -> HomeSymbolTable
+             -> TypeEnv           -- The interactive context's type envt 
+             -> PrintUnqualified       -- For error printing
+             -> Module
+             -> (SyntaxMap,
+                 RenamedHsExpr,        -- The expression itself
+                 [RenamedHsDecl])      -- Plus extra decls it sucked in from interface files
+             -> IO (Maybe (PersistentCompilerState, 
+                           TypecheckedHsExpr, 
+                           [Id],       -- always empty (matches typecheckStmt)
+                           Type))
+
+typecheckExpr dflags pcs hst ic_type_env unqual this_mod (syn_map, expr, decls)
+  = typecheck dflags syn_map pcs hst unqual $
+
+        -- use the default default settings, i.e. [Integer, Double]
+    tcSetDefaultTys defaultDefaultTys $
+
+       -- Typecheck the extra declarations
+    fixTc (\ ~(unf_env, _, _, _, _) ->
+       tcImports unf_env pcs hst get_fixity this_mod decls
+    )                  `thenTc` \ (env, new_pcs, local_inst_info, deriv_binds, local_rules) ->
+    ASSERT( null local_inst_info && nullBinds deriv_binds && null local_rules )
+
+       -- Now typecheck the expression
+    tcSetEnv env                       $
+    tcExtendGlobalTypeEnv ic_type_env  $
+
+    newTyVarTy openTypeKind            `thenTc` \ ty ->
+    tcMonoExpr expr ty                         `thenTc` \ (e', lie) ->
+    tcSimplifyInfer smpl_doc (varSetElems (tyVarsOfType ty)) lie 
+                       `thenTc` \ (qtvs, lie_free, dict_binds, dict_ids) ->
+    tcSimplifyTop lie_free             `thenTc` \ const_binds ->
+
+    let all_expr = mkHsLet const_binds $
+                  TyLam qtvs           $
+                  DictLam dict_ids     $
+                  mkHsLet dict_binds   $       
+                  e'
+
+       all_expr_ty = mkForAllTys qtvs  $
+                     mkFunTys (map idType dict_ids) $
+                     ty
+    in
+
+    zonkExpr all_expr                          `thenNF_Tc` \ zonked_expr ->
+    zonkTcType all_expr_ty                     `thenNF_Tc` \ zonked_ty ->
+    ioToTc (dumpIfSet_dyn dflags 
+               Opt_D_dump_tc "Typechecked" (ppr zonked_expr)) `thenNF_Tc_`
+    returnTc (new_pcs, zonked_expr, [], zonked_ty) 
+
+  where
+    get_fixity :: Name -> Maybe Fixity
+    get_fixity n = pprPanic "typecheckExpr" (ppr n)
+
+    smpl_doc = ptext SLIT("main expression")
+\end{code}
 
 %************************************************************************
 %*                                                                     *
@@ -232,9 +305,10 @@ data TcResults
   = TcResults {
        -- All these fields have info *just for this module*
        tc_env     :: TypeEnv,                  -- The top level TypeEnv
+       tc_insts   :: [DFunId],                 -- Instances 
+       tc_rules   :: [TypecheckedRuleDecl],    -- Transformation rules
        tc_binds   :: TypecheckedMonoBinds,     -- Bindings
-       tc_fords   :: [TypecheckedForeignDecl], -- Foreign import & exports.
-       tc_rules   :: [TypecheckedRuleDecl]     -- Transformation rules
+       tc_fords   :: [TypecheckedForeignDecl]  -- Foreign import & exports.
     }
 
 
@@ -294,12 +368,24 @@ tcModule pcs hst get_fixity this_mod decls
        tcForeignExports decls                  `thenTc`    \ (lie_fodecls,   foe_binds, foe_decls) ->
        tcSourceRules source_rules              `thenNF_Tc` \ (lie_rules,     more_local_rules) ->
        
+               -- CHECK THAT main IS DEFINED WITH RIGHT TYPE, IF REQUIRED
+        traceTc (text "Tc6")                   `thenNF_Tc_`
+       tcCheckMain this_mod                    `thenTc_`
+
             -- Deal with constant or ambiguous InstIds.  How could
             -- there be ambiguous ones?  They can only arise if a
             -- top-level decl falls under the monomorphism
             -- restriction, and no subsequent decl instantiates its
             -- type.  (Usually, ambiguous type variables are resolved
             -- during the generalisation step.)
+            --
+            -- Note that we must do this *after* tcCheckMain, because of the
+            -- following bizarre case: 
+            --         main = return ()
+            -- Here, we infer main :: forall a. m a, where m is a free
+            -- type variable.  tcCheckMain will unify it with IO, and that
+            -- must happen before tcSimplifyTop, since the latter will report
+            -- m as ambiguous
        let
            lie_alldecls = lie_valdecls  `plusLIE`
                           lie_instdecls `plusLIE`
@@ -307,11 +393,7 @@ tcModule pcs hst get_fixity this_mod decls
                           lie_fodecls   `plusLIE`
                           lie_rules
        in
-        traceTc (text "Tc6")                           `thenNF_Tc_`
-       tcSimplifyTop lie_alldecls                      `thenTc` \ const_inst_binds ->
-       
-               -- CHECK THAT main IS DEFINED WITH RIGHT TYPE, IF REQUIRED
-       tcCheckMain this_mod            `thenTc_`
+       tcSimplifyTop lie_alldecls      `thenTc` \ const_inst_binds ->
        
            -- Backsubstitution.    This must be done last.
            -- Even tcSimplifyTop may do some unification.
@@ -353,6 +435,7 @@ tcModule pcs hst get_fixity this_mod decls
        returnTc (final_env,
                  new_pcs,
                  TcResults { tc_env     = local_type_env,
+                             tc_insts   = map iDFunId local_insts,
                              tc_binds   = implicit_binds `AndMonoBinds` all_binds', 
                              tc_fords   = foi_decls ++ foe_decls',
                              tc_rules   = all_local_rules
@@ -380,12 +463,9 @@ typecheckIface
        -> HomeSymbolTable
        -> ModIface             -- Iface for this module (just module & fixities)
        -> (SyntaxMap, [RenamedHsDecl])
-       -> IO (Maybe (PersistentCompilerState, TypeEnv, [TypecheckedRuleDecl]))
+       -> IO (Maybe (PersistentCompilerState, ModDetails))
                        -- The new PCS is Augmented with imported information,
                        -- (but not stuff from this module).
-                       -- The TcResults returned contains only the environment
-                       -- and rules.
-
 
 typecheckIface dflags pcs hst mod_iface (syn_map, decls)
   = do { maybe_tc_stuff <- typecheck dflags syn_map pcs hst alwaysQualify $
@@ -406,15 +486,15 @@ typecheckIface dflags pcs hst mod_iface (syn_map, decls)
                            deriv_binds, local_rules) ->
          ASSERT(nullBinds deriv_binds)
          let 
-             local_things = filter (isLocalThing this_mod) 
-                                       (nameEnvElts (getTcGEnv env))
-             local_type_env :: TypeEnv
-             local_type_env = mkTypeEnv local_things
-         in
-
-         -- throw away local_inst_info
-          returnTc (new_pcs, local_type_env, local_rules)
+             local_things = filter (isLocalThing this_mod) (nameEnvElts (getTcGEnv env))
 
+             mod_details = ModDetails { md_types = mkTypeEnv local_things,
+                                        md_insts = map iDFunId local_inst_info,
+                                        md_rules = [(id,rule) | IfaceRuleOut id rule <- local_rules],
+                                        md_binds = [] }
+                       -- All the rules from an interface are of the IfaceRuleOut form
+         in
+          returnTc (new_pcs, mod_details)
 
 tcImports :: RecTcEnv
          -> PersistentCompilerState
@@ -426,9 +506,9 @@ tcImports :: RecTcEnv
                         RenamedHsBinds, [TypecheckedRuleDecl])
 
 -- tcImports is a slight mis-nomer.  
--- It deals with everythign that could be an import:
+-- It deals with everything that could be an import:
 --     type and class decls
---     interface signatures
+--     interface signatures (checked lazily)
 --     instance decls
 --     rule decls
 -- These can occur in source code too, of course
@@ -463,7 +543,7 @@ tcImports unf_env pcs hst get_fixity this_mod decls
     --   any type errors are found (ie there's an inconsistency)
     --   we silently discard the pragma
     traceTc (text "Tc3")                       `thenNF_Tc_`
-    tcInterfaceSigs unf_env tycl_decls         `thenTc` \ sig_ids ->
+    tcInterfaceSigs unf_env this_mod tycl_decls        `thenTc` \ sig_ids ->
     tcExtendGlobalValEnv sig_ids               $
     
     
@@ -480,11 +560,8 @@ tcImports unf_env pcs hst get_fixity this_mod decls
          -- (on the GHCi command line, for example).  In this case, we
          -- want to treat everything we pulled in as an imported thing.
         imported_things
-         | isHomeModule this_mod
-         = filter (not . isLocalThing this_mod) all_things
-         | otherwise
-         = all_things
-    
+                 = filter (not . isLocalThing this_mod) all_things
+        
         new_pte :: PackageTypeEnv
         new_pte = extendTypeEnvList (pcs_PTE pcs) imported_things
         
@@ -593,47 +670,31 @@ typecheck dflags syn_map pcs hst unqual thing_inside
 \begin{code}
 printTcDump dflags Nothing = return ()
 printTcDump dflags (Just (_, results))
-  = do dumpIfSet_dyn dflags Opt_D_dump_types 
-                     "Type signatures" (dump_sigs (tc_env results))
-       dumpIfSet_dyn dflags Opt_D_dump_tc    
-                     "Typechecked" (dump_tc results) 
+  = do dumpIfSet_dyn_or dflags [Opt_D_dump_types, Opt_D_dump_tc]
+                     "Interface" (dump_tc_iface results)
 
-printIfaceDump dflags Nothing = return ()
-printIfaceDump dflags (Just (_, env, rules))
-  = do dumpIfSet_dyn dflags Opt_D_dump_types 
-                     "Type signatures" (dump_sigs env)
        dumpIfSet_dyn dflags Opt_D_dump_tc    
-                     "Typechecked" (dump_iface env rules) 
+                     "Typechecked" (ppr (tc_binds results))
 
-dump_tc results
-  = vcat [ppr (tc_binds results),
-         pp_rules (tc_rules results),
-         ppr_gen_tycons [tc | ATyCon tc <- nameEnvElts (tc_env results)]
-    ]
+         
+printIfaceDump dflags Nothing = return ()
+printIfaceDump dflags (Just (_, details))
+  = dumpIfSet_dyn_or dflags [Opt_D_dump_types, Opt_D_dump_tc]
+                     "Interface" (pprModDetails details)
 
-dump_iface env rules
-  = vcat [pp_rules rules,
-         ppr_gen_tycons [tc | ATyCon tc <- nameEnvElts env]
-    ]
+dump_tc_iface results
+  = vcat [pprModDetails (ModDetails {md_types = tc_env results, 
+                                    md_insts = tc_insts results,
+                                    md_rules = [], md_binds = []}) ,
+         ppr_rules (tc_rules results),
 
-dump_sigs env  -- Print type signatures
-  =    -- Convert to HsType so that we get source-language style printing
-       -- And sort by RdrName
-    vcat $ map ppr_sig $ sortLt lt_sig $
-    [ (toRdrName id, toHsType (idType id))
-    | AnId id <- nameEnvElts env,
-      want_sig id
+         ppr_gen_tycons [tc | ATyCon tc <- nameEnvElts (tc_env results)]
     ]
-  where
-    lt_sig (n1,_) (n2,_) = n1 < n2
-    ppr_sig (n,t)        = ppr n <+> dcolon <+> ppr t
 
-    want_sig id | opt_PprStyle_Debug = True
-               | otherwise          = isLocalId id && isGlobalName (idName id)
-       -- isLocalId ignores data constructors, records selectors etc
-       -- The isGlobalName ignores local dictionary and method bindings
-       -- that the type checker has invented.  User-defined things have
-       -- Global names.
+ppr_rules [] = empty
+ppr_rules rs = vcat [ptext SLIT("{-# RULES"),
+                     nest 4 (vcat (map ppr rs)),
+                     ptext SLIT("#-}")]
 
 ppr_gen_tycons tcs = vcat [ptext SLIT("{-# Generic type constructor details"),
                           vcat (map ppr_gen_tycon tcs),
@@ -655,8 +716,4 @@ ppr_ep (EP from to)
   where
     (_,from_tau) = splitForAllTys (idType from)
 
-pp_rules [] = empty
-pp_rules rs = vcat [ptext SLIT("{-# RULES"),
-                   nest 4 (vcat (map ppr rs)),
-                   ptext SLIT("#-}")]
 \end{code}