Print infix function definitions correctly in HsSyn
[ghc-hetmet.git] / compiler / hsSyn / HsExpr.lhs
index 2d111ee..8830155 100644 (file)
@@ -1,7 +1,9 @@
 %
+% (c) The University of Glasgow 2006
 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
 %
-\section[HsExpr]{Abstract Haskell syntax: expressions}
+
+HsExpr: Abstract Haskell syntax: expressions
 
 \begin{code}
 module HsExpr where
@@ -9,19 +11,19 @@ module HsExpr where
 #include "HsVersions.h"
 
 -- friends:
-import HsDecls         ( HsGroup )
-import HsPat           ( LPat )
-import HsLit           ( HsLit(..), HsOverLit )
-import HsTypes         ( LHsType, PostTcType )
-import HsImpExp                ( isOperator, pprHsVar )
-import HsBinds         ( HsLocalBinds, DictBinds, isEmptyLocalBinds,
-                         HsWrapper, pprHsWrapper )
+import HsDecls
+import HsPat
+import HsLit
+import HsTypes
+import HsImpExp
+import HsBinds
 
 -- others:
-import Var             ( Id )
-import Name            ( Name )
-import BasicTypes      ( IPName, Boxity, tupleParens, Arity, Fixity(..) )
-import SrcLoc          ( Located(..), unLoc )
+import Var
+import Name
+import BasicTypes
+import DataCon
+import SrcLoc
 import Outputable      
 import FastString
 \end{code}
@@ -157,9 +159,11 @@ data HsExpr id
        -- Record update
   | RecordUpd  (LHsExpr id)
                (HsRecordBinds id)
-               PostTcType              -- Type of *input* record
-               PostTcType              -- Type of *result* record (may differ from
-                                       --      type of input record)
+               [DataCon]               -- Filled in by the type checker to the *non-empty*
+                                       -- list of DataCons that have all the upd'd fields
+               [PostTcType]            -- Argument types of *input* record type
+               [PostTcType]            --              and  *output* record type
+       -- For a type family, the arg types are of the *instance* tycon, not the family tycon
 
   | ExprWithTySig                      -- e :: type
                (LHsExpr id)
@@ -185,6 +189,7 @@ data HsExpr id
                
   -----------------------------------------------------------
   -- MetaHaskell Extensions
+
   | HsBracket    (HsBracket id)
 
   | HsBracketOut (HsBracket Name)      -- Output of the type checker is the *original*
@@ -219,13 +224,29 @@ data HsExpr id
        (Maybe Fixity)  -- fixity (filled in by the renamer), for forms that
                        -- were converted from OpApp's by the renamer
        [LHsCmdTop id]  -- argument commands
-\end{code}
 
 
-These constructors only appear temporarily in the parser.
-The renamer translates them into the Right Thing.
+  ---------------------------------------
+  -- Haskell program coverage (Hpc) Support
+
+  | HsTick 
+     Int                               -- module-local tick number
+     [id]                               -- variables in scope
+     (LHsExpr id)                      -- sub-expression
+
+  | HsBinTick
+     Int                               -- module-local tick number for True
+     Int                               -- module-local tick number for False
+     (LHsExpr id)                      -- sub-expression
+
+  | HsTickPragma                       -- A pragma introduced tick
+     (FastString,(Int,Int),(Int,Int))   -- external span for this tick    
+     (LHsExpr id)     
+
+  ---------------------------------------
+  -- These constructors only appear temporarily in the parser.
+  -- The renamer translates them into the Right Thing.
 
-\begin{code}
   | EWildPat                   -- wildcard
 
   | EAsPat     (Located id)    -- as pattern
@@ -234,11 +255,10 @@ The renamer translates them into the Right Thing.
   | ELazyPat   (LHsExpr id) -- ~ pattern
 
   | HsType      (LHsType id)     -- Explicit type argument; e.g  f {| Int |} x y
-\end{code}
 
-Everything from here on appears only in typechecker output.
+  ---------------------------------------
+  -- Finally, HsWrap appears only in typechecker output
 
-\begin{code}
   |  HsWrap    HsWrapper       -- TRANSLATION
                (HsExpr id)
 
@@ -258,13 +278,30 @@ instance OutputableBndr id => Outputable (HsExpr id) where
 \end{code}
 
 \begin{code}
-pprExpr :: OutputableBndr id => HsExpr id -> SDoc
+-----------------------
+-- pprExpr, pprLExpr, pprBinds call pprDeeper; 
+-- the underscore versions do not
+pprLExpr :: OutputableBndr id => LHsExpr id -> SDoc
+pprLExpr (L _ e) = pprExpr e
 
-pprExpr  e = pprDeeper (ppr_expr e)
+pprExpr :: OutputableBndr id => HsExpr id -> SDoc
+pprExpr e | isAtomicHsExpr e || isQuietHsExpr e =            ppr_expr e
+          | otherwise                           = pprDeeper (ppr_expr e)
+
+isQuietHsExpr :: HsExpr id -> Bool
+-- Parentheses do display something, but it gives little info and
+-- if we go deeper when we go inside them then we get ugly things
+-- like (...)
+isQuietHsExpr (HsPar _) = True
+-- applications don't display anything themselves
+isQuietHsExpr (HsApp _ _) = True
+isQuietHsExpr (OpApp _ _ _ _) = True
+isQuietHsExpr _ = False
 
 pprBinds :: OutputableBndr id => HsLocalBinds id -> SDoc
 pprBinds b = pprDeeper (ppr b)
 
+-----------------------
 ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
 ppr_lexpr e = ppr_expr (unLoc e)
 
@@ -289,8 +326,8 @@ ppr_expr (OpApp e1 op fixity e2)
       HsVar v -> pp_infixly v
       _              -> pp_prefixly
   where
-    pp_e1 = pprParendExpr e1           -- Add parens to make precedence clear
-    pp_e2 = pprParendExpr e2
+    pp_e1 = pprDebugParendExpr e1   -- In debug mode, add parens 
+    pp_e2 = pprDebugParendExpr e2   -- to make precedence clear
 
     pp_prefixly
       = hang (ppr op) 2 (sep [pp_e1, pp_e2])
@@ -298,14 +335,14 @@ ppr_expr (OpApp e1 op fixity e2)
     pp_infixly v
       = sep [nest 2 pp_e1, pprInfix v, nest 2 pp_e2]
 
-ppr_expr (NegApp e _) = char '-' <+> pprParendExpr e
+ppr_expr (NegApp e _) = char '-' <+> pprDebugParendExpr e
 
 ppr_expr (SectionL expr op)
   = case unLoc op of
       HsVar v -> pp_infixly v
       _              -> pp_prefixly
   where
-    pp_expr = pprParendExpr expr
+    pp_expr = pprDebugParendExpr expr
 
     pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
                       4 (hsep [pp_expr, ptext SLIT("x_ )")])
@@ -316,7 +353,7 @@ ppr_expr (SectionR op expr)
       HsVar v -> pp_infixly v
       _              -> pp_prefixly
   where
-    pp_expr = pprParendExpr expr
+    pp_expr = pprDebugParendExpr expr
 
     pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
                       4 ((<>) pp_expr rparen)
@@ -348,19 +385,19 @@ ppr_expr (HsLet binds expr)
 ppr_expr (HsDo do_or_list_comp stmts body _) = pprDo do_or_list_comp stmts body
 
 ppr_expr (ExplicitList _ exprs)
-  = brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
+  = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
 
 ppr_expr (ExplicitPArr _ exprs)
-  = pa_brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
+  = pa_brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
 
 ppr_expr (ExplicitTuple exprs boxity)
   = tupleParens boxity (sep (punctuate comma (map ppr_lexpr exprs)))
 
 ppr_expr (RecordCon con_id con_expr rbinds)
-  = pp_rbinds (ppr con_id) rbinds
+  = hang (ppr con_id) 2 (ppr rbinds)
 
-ppr_expr (RecordUpd aexp rbinds _ _)
-  = pp_rbinds (pprParendExpr aexp) rbinds
+ppr_expr (RecordUpd aexp rbinds _ _ _)
+  = hang (pprParendExpr aexp) 2 (ppr rbinds)
 
 ppr_expr (ExprWithTySig expr sig)
   = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
@@ -379,7 +416,7 @@ ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
 ppr_expr (HsSCC lbl expr)
   = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ftext lbl), pprParendExpr expr ]
 
-ppr_expr (HsWrap co_fn e) = pprHsWrapper (ppr_expr e) co_fn
+ppr_expr (HsWrap co_fn e) = pprHsWrapper (pprExpr e) co_fn
 ppr_expr (HsType id)       = ppr id
 
 ppr_expr (HsSpliceE s)       = pprSplice s
@@ -390,6 +427,18 @@ ppr_expr (HsBracketOut e ps) = ppr e $$ ptext SLIT("pending") <+> ppr ps
 ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
   = hsep [ptext SLIT("proc"), ppr pat, ptext SLIT("->"), ppr cmd]
 
+ppr_expr (HsTick tickId vars exp)
+  = hcat [ptext SLIT("tick<"), ppr tickId,ptext SLIT(">("), hsep (map pprHsVar vars), ppr exp,ptext SLIT(")")]
+ppr_expr (HsBinTick tickIdTrue tickIdFalse exp)
+  = hcat [ptext SLIT("bintick<"), 
+         ppr tickIdTrue,
+         ptext SLIT(","),
+         ppr tickIdFalse,
+         ptext SLIT(">("), 
+         ppr exp,ptext SLIT(")")]
+ppr_expr (HsTickPragma externalSrcLoc exp)
+  = hcat [ptext SLIT("tickpragma<"), ppr externalSrcLoc,ptext SLIT(">("), ppr exp,ptext SLIT(")")]
+
 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True)
   = hsep [ppr_lexpr arrow, ptext SLIT("-<"), ppr_lexpr arg]
 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False)
@@ -424,29 +473,53 @@ pa_brackets :: SDoc -> SDoc
 pa_brackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")    
 \end{code}
 
-Parenthesize unless very simple:
+HsSyn records exactly where the user put parens, with HsPar.
+So generally speaking we print without adding any parens.
+However, some code is internally generated, and in some places
+parens are absolutely required; so for these places we use
+pprParendExpr (but don't print double parens of course).
+
+For operator applications we don't add parens, because the oprerator
+fixities should do the job, except in debug mode (-dppr-debug) so we
+can see the structure of the parse tree.
+
 \begin{code}
+pprDebugParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
+pprDebugParendExpr expr
+  = getPprStyle (\sty ->
+    if debugStyle sty then pprParendExpr expr
+                     else pprLExpr      expr)
+  
 pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
 pprParendExpr expr
   = let
-       pp_as_was = ppr_lexpr expr
-       -- Using ppr_expr here avoids the call to 'deeper'
-       -- Not sure if that's always right.
+       pp_as_was = pprLExpr expr
+       -- Using pprLExpr makes sure that we go 'deeper'
+       -- I think that is usually (always?) right
     in
     case unLoc expr of
-      HsLit l          -> ppr l
-      HsOverLit l      -> ppr l
-                       
-      HsVar _          -> pp_as_was
-      HsIPVar _                -> pp_as_was
-      ExplicitList _ _  -> pp_as_was
-      ExplicitPArr _ _  -> pp_as_was
-      ExplicitTuple _ _        -> pp_as_was
-      HsPar _          -> pp_as_was
-      HsBracket _      -> pp_as_was
-      HsBracketOut _ []        -> pp_as_was
-                       
-      _                        -> parens pp_as_was
+      HsLit l             -> pp_as_was
+      HsOverLit l         -> pp_as_was
+      HsVar _             -> pp_as_was
+      HsIPVar _                   -> pp_as_was
+      ExplicitList _ _     -> pp_as_was
+      ExplicitPArr _ _     -> pp_as_was
+      ExplicitTuple _ _           -> pp_as_was
+      HsPar _             -> pp_as_was
+      HsBracket _         -> pp_as_was
+      HsBracketOut _ []           -> pp_as_was
+      HsDo sc _ _ _
+       | isListCompExpr sc -> pp_as_was
+      _                           -> parens pp_as_was
+
+isAtomicHsExpr :: HsExpr id -> Bool    -- A single token
+isAtomicHsExpr (HsVar {})     = True
+isAtomicHsExpr (HsLit {})     = True
+isAtomicHsExpr (HsOverLit {}) = True
+isAtomicHsExpr (HsIPVar {})   = True
+isAtomicHsExpr (HsWrap _ e)   = isAtomicHsExpr e
+isAtomicHsExpr (HsPar e)      = isAtomicHsExpr (unLoc e)
+isAtomicHsExpr e             = False
 \end{code}
 
 %************************************************************************
@@ -526,17 +599,7 @@ data HsCmdTop id
 %************************************************************************
 
 \begin{code}
-type HsRecordBinds id = [(Located id, LHsExpr id)]
-
-recBindFields :: HsRecordBinds id -> [id]
-recBindFields rbinds = [unLoc field | (field,_) <- rbinds]
-
-pp_rbinds :: OutputableBndr id => SDoc -> HsRecordBinds id -> SDoc
-pp_rbinds thing rbinds
-  = hang thing 
-        4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
-  where
-    pp_rbind (v, e) = hsep [pprBndr LetBind (unLoc v), char '=', ppr e]
+type HsRecordBinds id = HsRecFields id (LHsExpr id)
 \end{code}
 
 
@@ -611,8 +674,8 @@ pprMatches ctxt (MatchGroup matches ty) = vcat (map (pprMatch ctxt) (map unLoc m
                                           -- a place-holder before typechecking
 
 -- Exported to HsBinds, which can't see the defn of HsMatchContext
-pprFunBind :: (OutputableBndr id) => id -> MatchGroup id -> SDoc
-pprFunBind fun matches = pprMatches (FunRhs fun) matches
+pprFunBind :: (OutputableBndr id) => id -> Bool -> MatchGroup id -> SDoc
+pprFunBind fun inf matches = pprMatches (FunRhs fun inf) matches
 
 -- Exported to HsBinds, which can't see the defn of HsMatchContext
 pprPatBind :: (OutputableBndr bndr, OutputableBndr id)
@@ -622,14 +685,29 @@ pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
 
 pprMatch :: OutputableBndr id => HsMatchContext id -> Match id -> SDoc
 pprMatch ctxt (Match pats maybe_ty grhss)
-  = pp_name ctxt <+> sep [sep (map ppr pats), 
-                    ppr_maybe_ty, 
-                    nest 2 (pprGRHSs ctxt grhss)]
+  = herald <+> sep [sep (map ppr other_pats), 
+                   ppr_maybe_ty, 
+                   nest 2 (pprGRHSs ctxt grhss)]
   where
-    pp_name (FunRhs fun) = ppr fun     -- Not pprBndr; the AbsBinds will
-                                       -- have printed the signature
-    pp_name LambdaExpr   = char '\\'
-    pp_name other       = empty
+    (herald, other_pats) 
+       = case ctxt of
+           FunRhs fun is_infix
+               | not is_infix -> (ppr fun, pats)
+                       -- f x y z = e
+                       -- Not pprBndr; the AbsBinds will
+                       -- have printed the signature
+
+               | null pats3 -> (pp_infix, [])
+                       -- x &&& y = e
+
+               | otherwise -> (parens pp_infix, pats3)
+                       -- (x &&& y) z = e
+               where
+                 (pat1:pat2:pats3) = pats
+                 pp_infix = ppr pat1 <+> ppr fun <+> ppr pat2
+
+           LambdaExpr -> (char '\\', pats)
+           other      -> (empty,     pats)
 
     ppr_maybe_ty = case maybe_ty of
                        Just ty -> dcolon <+> ppr ty
@@ -639,9 +717,8 @@ pprMatch ctxt (Match pats maybe_ty grhss)
 pprGRHSs :: OutputableBndr id => HsMatchContext id -> GRHSs id -> SDoc
 pprGRHSs ctxt (GRHSs grhss binds)
   = vcat (map (pprGRHS ctxt . unLoc) grhss)
-    $$
-    (if isEmptyLocalBinds binds then empty
-     else text "where" $$ nest 4 (pprBinds binds))
+ $$ if isEmptyLocalBinds binds then empty
+                               else text "where" $$ nest 4 (pprBinds binds)
 
 pprGRHS :: OutputableBndr id => HsMatchContext id -> GRHS id -> SDoc
 
@@ -763,8 +840,8 @@ pprStmt (ParStmt stmtss)          = hsep (map (\stmts -> ptext SLIT("| ") <> ppr
 pprStmt (RecStmt segment _ _ _ _) = ptext SLIT("rec") <+> braces (vcat (map ppr segment))
 
 pprDo :: OutputableBndr id => HsStmtContext any -> [LStmt id] -> LHsExpr id -> SDoc
-pprDo DoExpr      stmts body = ptext SLIT("do")  <+> (vcat (map ppr stmts) $$ ppr body)
-pprDo (MDoExpr _) stmts body = ptext SLIT("mdo") <+> (vcat (map ppr stmts) $$ ppr body)
+pprDo DoExpr      stmts body = ptext SLIT("do")  <+> pprDeeperList vcat (map ppr stmts ++ [ppr body])
+pprDo (MDoExpr _) stmts body = ptext SLIT("mdo") <+> pprDeeperList vcat (map ppr stmts ++ [ppr body])
 pprDo ListComp    stmts body = pprComp brackets    stmts body
 pprDo PArrComp    stmts body = pprComp pa_brackets stmts body
 pprDo other      stmts body = panic "pprDo"    -- PatGuard, ParStmtCxt
@@ -856,7 +933,7 @@ pp_dotdot = ptext SLIT(" .. ")
 
 \begin{code}
 data HsMatchContext id -- Context of a Match
-  = FunRhs id                  -- Function binding for f
+  = FunRhs id Bool             -- Function binding for f; True <=> written infix
   | CaseAlt                    -- Guard on a case alternative
   | LambdaExpr                 -- Pattern of a lambda
   | ProcExpr                   -- Pattern of a proc
@@ -881,11 +958,16 @@ data HsStmtContext id
 isDoExpr :: HsStmtContext id -> Bool
 isDoExpr DoExpr      = True
 isDoExpr (MDoExpr _) = True
-isDoExpr other       = False
+isDoExpr _           = False
+
+isListCompExpr :: HsStmtContext id -> Bool
+isListCompExpr ListComp = True
+isListCompExpr PArrComp = True
+isListCompExpr _        = False
 \end{code}
 
 \begin{code}
-matchSeparator (FunRhs _)   = ptext SLIT("=")
+matchSeparator (FunRhs {})  = ptext SLIT("=")
 matchSeparator CaseAlt      = ptext SLIT("->") 
 matchSeparator LambdaExpr   = ptext SLIT("->") 
 matchSeparator ProcExpr     = ptext SLIT("->") 
@@ -895,7 +977,7 @@ matchSeparator RecUpd       = panic "unused"
 \end{code}
 
 \begin{code}
-pprMatchContext (FunRhs fun)     = ptext SLIT("the definition of") <+> quotes (ppr fun)
+pprMatchContext (FunRhs fun _)           = ptext SLIT("the definition of") <+> quotes (ppr fun)
 pprMatchContext CaseAlt                  = ptext SLIT("a case alternative")
 pprMatchContext RecUpd           = ptext SLIT("a record-update construct")
 pprMatchContext PatBindRhs       = ptext SLIT("a pattern binding")
@@ -926,7 +1008,7 @@ pprStmtResultContext other      = ptext SLIT("the result of") <+> pprStmtContext
 -}
 
 -- Used to generate the string for a *runtime* error message
-matchContextErrString (FunRhs fun)              = "function " ++ showSDoc (ppr fun)
+matchContextErrString (FunRhs fun _)                    = "function " ++ showSDoc (ppr fun)
 matchContextErrString CaseAlt                   = "case"
 matchContextErrString PatBindRhs                = "pattern binding"
 matchContextErrString RecUpd                    = "record update"