[project @ 2001-11-29 13:47:09 by simonpj]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsExpr.lhs
index d036547..91ddad3 100644 (file)
@@ -10,20 +10,22 @@ module HsExpr where
 
 -- friends:
 import HsBinds         ( HsBinds(..), nullBinds )
+import HsTypes         ( PostTcType )
 import HsLit           ( HsLit, HsOverLit )
 import BasicTypes      ( Fixity(..) )
 import HsTypes         ( HsType )
 import HsImpExp                ( isOperator )
 
 -- others:
+import Name            ( Name )
 import ForeignCall     ( Safety )
 import Outputable      
 import PprType         ( pprParendType )
-import Type            ( Type )
+import Type            ( Type  )
 import Var             ( TyVar )
 import DataCon         ( DataCon )
 import CStrings                ( CLabelString, pprCLabelString )
-import BasicTypes      ( Boxity, tupleParens )
+import BasicTypes      ( IPName, Boxity, tupleParens )
 import SrcLoc          ( SrcLoc )
 \end{code}
 
@@ -36,7 +38,7 @@ import SrcLoc         ( SrcLoc )
 \begin{code}
 data HsExpr id pat
   = HsVar      id              -- variable
-  | HsIPVar    id              -- implicit parameter
+  | HsIPVar    (IPName id)     -- implicit parameter
   | HsOverLit  HsOverLit       -- Overloaded literals; eliminated by type checker
   | HsLit      HsLit           -- Simple (non-overloaded) literals
 
@@ -59,6 +61,7 @@ data HsExpr id pat
   -- They are eventually removed by the type checker.
 
   | NegApp     (HsExpr id pat) -- negated expr
+               Name            -- Name of 'negate' (see RnEnv.lookupSyntaxName)
 
   | HsPar      (HsExpr id pat) -- parenthesised expr
 
@@ -80,7 +83,7 @@ data HsExpr id pat
                (HsExpr  id pat)
 
   | HsWith     (HsExpr id pat) -- implicit parameter binding
-               [(id, HsExpr id pat)]
+               [(IPName id, HsExpr id pat)]
 
   | HsDo       HsDoContext
                [Stmt id pat]   -- "do":one or more stmts
@@ -95,9 +98,7 @@ data HsExpr id pat
                SrcLoc
 
   | ExplicitList               -- syntactic list
-               [HsExpr id pat]
-  | ExplicitListOut            -- TRANSLATION
-               Type    -- Gives type of components of list
+               PostTcType      -- Gives type of components of list
                [HsExpr id pat]
 
   | ExplicitTuple              -- tuple
@@ -122,8 +123,9 @@ data HsExpr id pat
                (HsRecordBinds id pat)
 
   | RecordUpdOut (HsExpr id pat)       -- TRANSLATION
+                Type                   -- Type of *input* record
                 Type                   -- Type of *result* record (may differ from
-                                               -- type of input record)
+                                       --      type of input record)
                 [id]                   -- Dicts needed for construction
                 (HsRecordBinds id pat)
 
@@ -146,7 +148,7 @@ data HsExpr id pat
                                -- NOTE: this CCall is the *boxed*
                                -- version; the desugarer will convert
                                -- it into the unboxed "ccall#".
-               Type    -- The result type; will be *bottom*
+               PostTcType      -- The result type; will be *bottom*
                                -- until the typechecker gets ahold of it
 
   | HsSCC      FAST_STRING     -- "set cost centre" (_scc_) annotation
@@ -216,7 +218,7 @@ ppr_expr (HsVar v)
   | isOperator v = parens (ppr v)
   | otherwise    = ppr v
 
-ppr_expr (HsIPVar v)     = char '?' <> ppr v
+ppr_expr (HsIPVar v)     = ppr v
 ppr_expr (HsLit lit)     = ppr lit
 ppr_expr (HsOverLit lit) = ppr lit
 
@@ -248,7 +250,7 @@ ppr_expr (OpApp e1 op fixity e2)
                | otherwise    = char '`' <> ppr v <> char '`'
                -- Put it in backquotes if it's not an operator already
 
-ppr_expr (NegApp e) = char '-' <+> pprParendExpr e
+ppr_expr (NegApp e _) = char '-' <+> pprParendExpr e
 
 ppr_expr (HsPar e) = parens (ppr_expr e)
 
@@ -295,14 +297,12 @@ ppr_expr (HsLet binds expr)
         hang (ptext SLIT("in"))  2 (ppr expr)]
 
 ppr_expr (HsWith expr binds)
-  = hsep [ppr expr, ptext SLIT("with"), ppr binds]
+  = hsep [ppr expr, ptext SLIT("with"), pp_ipbinds binds]
 
 ppr_expr (HsDo do_or_list_comp stmts _)            = pprDo do_or_list_comp stmts
 ppr_expr (HsDoOut do_or_list_comp stmts _ _ _ _ _) = pprDo do_or_list_comp stmts
 
-ppr_expr (ExplicitList exprs)
-  = brackets (fsep (punctuate comma (map ppr_expr exprs)))
-ppr_expr (ExplicitListOut ty exprs)
+ppr_expr (ExplicitList _ exprs)
   = brackets (fsep (punctuate comma (map ppr_expr exprs)))
 
 ppr_expr (ExplicitTuple exprs boxity)
@@ -315,7 +315,7 @@ ppr_expr (RecordConOut data_con con rbinds)
 
 ppr_expr (RecordUpd aexp rbinds)
   = pp_rbinds (pprParendExpr aexp) rbinds
-ppr_expr (RecordUpdOut aexp _ _ rbinds)
+ppr_expr (RecordUpdOut aexp _ _ _ rbinds)
   = pp_rbinds (pprParendExpr aexp) rbinds
 
 ppr_expr (ExprWithTySig expr sig)
@@ -381,8 +381,7 @@ pprParendExpr expr
 
       HsVar _              -> pp_as_was
       HsIPVar _                    -> pp_as_was
-      ExplicitList _       -> pp_as_was
-      ExplicitListOut _ _   -> pp_as_was
+      ExplicitList _ _      -> pp_as_was
       ExplicitTuple _ _            -> pp_as_was
       HsPar _              -> pp_as_was
 
@@ -412,6 +411,13 @@ pp_rbinds thing rbinds
           hsep [ppr v, char '=', ppr e]
 \end{code}
 
+\begin{code}
+pp_ipbinds :: (Outputable id, Outputable pat)
+          => [(IPName id, HsExpr id pat)] -> SDoc
+pp_ipbinds pairs = hsep (punctuate semi (map pp_item pairs))
+                where
+                  pp_item (id,rhs) = ppr id <+> equals <+> ppr_expr rhs
+\end{code}
 
 
 %************************************************************************
@@ -437,8 +443,6 @@ patterns in each equation.
 \begin{code}
 data Match id pat
   = Match
-       [id]                    -- Tyvars wrt which this match is universally quantified
-                               -- empty after typechecking
        [pat]                   -- The patterns
        (Maybe (HsType id))     -- A type signature for the result of the match
                                --      Nothing after typechecking
@@ -449,7 +453,7 @@ data Match id pat
 data GRHSs id pat      
   = GRHSs [GRHS id pat]                -- Guarded RHSs
          (HsBinds id pat)      -- The where clause
-         (Maybe Type)          -- Just rhs_ty after type checking
+         PostTcType            -- Type of RHS (after type checking)
 
 data GRHS id pat
   = GRHS  [Stmt id pat]                -- The RHS is the final ResultStmt
@@ -457,9 +461,9 @@ data GRHS id pat
                                -- it printed 'wrong' in error messages 
          SrcLoc
 
-mkSimpleMatch :: [pat] -> HsExpr id pat -> Maybe Type -> SrcLoc -> Match id pat
-mkSimpleMatch pats rhs maybe_rhs_ty locn
-  = Match [] pats Nothing (GRHSs (unguardedRHS rhs locn) EmptyBinds maybe_rhs_ty)
+mkSimpleMatch :: [pat] -> HsExpr id pat -> Type -> SrcLoc -> Match id pat
+mkSimpleMatch pats rhs rhs_ty locn
+  = Match pats Nothing (GRHSs (unguardedRHS rhs locn) EmptyBinds rhs_ty)
 
 unguardedRHS :: HsExpr id pat -> SrcLoc -> [GRHS id pat]
 unguardedRHS rhs loc = [GRHS [ResultStmt rhs loc] loc]
@@ -471,7 +475,7 @@ THis is something of a nuisance, but no more.
 
 \begin{code}
 getMatchLoc :: Match id pat -> SrcLoc
-getMatchLoc (Match _ _ _ (GRHSs (GRHS _ loc : _) _ _)) = loc
+getMatchLoc (Match _ _ (GRHSs (GRHS _ loc : _) _ _)) = loc
 \end{code}
 
 We know the list must have at least one @Match@ in it.
@@ -494,7 +498,7 @@ pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
 
 pprMatch :: (Outputable id, Outputable pat)
           => HsMatchContext id -> Match id pat -> SDoc
-pprMatch ctxt (Match _ pats maybe_ty grhss)
+pprMatch ctxt (Match pats maybe_ty grhss)
   = pp_name ctxt <+> sep [sep (map ppr pats), 
                     ppr_maybe_ty,
                     nest 2 (pprGRHSs ctxt grhss)]
@@ -508,7 +512,7 @@ pprMatch ctxt (Match _ pats maybe_ty grhss)
 
 pprGRHSs :: (Outputable id, Outputable pat)
         => HsMatchContext id -> GRHSs id pat -> SDoc
-pprGRHSs ctxt (GRHSs grhss binds maybe_ty)
+pprGRHSs ctxt (GRHSs grhss binds ty)
   = vcat (map (pprGRHS ctxt) grhss)
     $$
     (if nullBinds binds then empty
@@ -542,11 +546,12 @@ pp_rhs ctxt rhs = ptext (matchSeparator ctxt) <+> pprDeeper (ppr rhs)
 data Stmt id pat
   = BindStmt   pat (HsExpr id pat) SrcLoc
   | LetStmt    (HsBinds id pat)
-  | ResultStmt (HsExpr id pat) SrcLoc  -- See notes that follow
-  | ExprStmt   (HsExpr id pat) SrcLoc  -- See notes that follow
-  | ParStmt    [[Stmt id pat]]         -- List comp only: parallel set of quals
-  | ParStmtOut [([id], [Stmt id pat])] -- PLC after renaming; the ids are the binders
-                                       -- bound by the stmts
+  | ResultStmt (HsExpr id pat) SrcLoc                  -- See notes that follow
+  | ExprStmt   (HsExpr id pat) PostTcType SrcLoc       -- See notes that follow
+       -- The type is the *element type* of the expression
+  | ParStmt    [[Stmt id pat]]                         -- List comp only: parallel set of quals
+  | ParStmtOut [([id], [Stmt id pat])]                 -- PLC after renaming; the ids are the binders
+                                                       -- bound by the stmts
 \end{code}
 
 ExprStmts and ResultStmts are a bit tricky, because what they mean
@@ -554,7 +559,7 @@ depends on the context.  Consider the following contexts:
 
        A do expression of type (m res_ty)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-       * ExprStmt E:   do { ....; E; ... }
+       * ExprStmt E any_ty:   do { ....; E; ... }
                E :: m any_ty
          Translation: E >> ...
        
@@ -564,7 +569,7 @@ depends on the context.  Consider the following contexts:
        
        A list comprehensions of type [elt_ty]
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-       * ExprStmt E:   [ .. | .... E ]
+       * ExprStmt E Bool:   [ .. | .... E ]
                        [ .. | ..., E, ... ]
                        [ .. | .... | ..., E | ... ]
                E :: Bool
@@ -576,7 +581,7 @@ depends on the context.  Consider the following contexts:
        
        A guard list, guarding a RHS of type rhs_ty
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-       * ExprStmt E:   f x | ..., E, ... = ...rhs...
+       * ExprStmt E Bool:   f x | ..., E, ... = ...rhs...
                E :: Bool
          Translation: if E then fail else ...
        
@@ -598,7 +603,7 @@ instance (Outputable id, Outputable pat) =>
 
 pprStmt (BindStmt pat expr _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
 pprStmt (LetStmt binds)       = hsep [ptext SLIT("let"), pprBinds binds]
-pprStmt (ExprStmt expr _)     = ppr expr
+pprStmt (ExprStmt expr _ _)   = ppr expr
 pprStmt (ResultStmt expr _)   = ppr expr
 pprStmt (ParStmt stmtss)
  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)