2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsExpr]{Abstract Haskell syntax: expressions}
9 #include "HsVersions.h"
12 import HsDecls ( HsGroup )
14 import HsLit ( HsLit(..), HsOverLit )
15 import HsTypes ( LHsType, PostTcType )
16 import HsImpExp ( isOperator, pprHsVar )
17 import HsBinds ( HsLocalBinds, DictBinds, isEmptyLocalBinds )
20 import Type ( Type, pprParendType )
21 import Var ( TyVar, Id )
23 import BasicTypes ( IPName, Boxity, tupleParens, Fixity(..) )
24 import SrcLoc ( Located(..), unLoc )
30 %************************************************************************
32 \subsection{Expressions proper}
34 %************************************************************************
37 type LHsExpr id = Located (HsExpr id)
39 -------------------------
40 -- PostTcExpr is an evidence expression attached to the
41 -- syntax tree by the type checker (c.f. postTcType)
42 -- We use a PostTcTable where there are a bunch of pieces of
43 -- evidence, more than is convenient to keep individually
44 type PostTcExpr = HsExpr Id
45 type PostTcTable = [(Name, Id)]
47 noPostTcExpr :: PostTcExpr
48 noPostTcExpr = HsLit (HsString FSLIT("noPostTcExpr"))
50 noPostTcTable :: PostTcTable
53 -------------------------
54 -- SyntaxExpr is like PostTcExpr, but it's filled in a little earlier,
55 -- by the renamer. It's used for rebindable syntax.
56 -- E.g. (>>=) is filled in before the renamer by the appropriate Name
57 -- for (>>=), and then instantiated by the type checker with its
60 type SyntaxExpr id = HsExpr id
62 noSyntaxExpr :: SyntaxExpr id -- Before renaming, and sometimes after,
63 -- (if the syntax slot makes no sense)
64 noSyntaxExpr = HsLit (HsString FSLIT("noSyntaxExpr"))
67 type SyntaxTable id = [(Name, SyntaxExpr id)]
68 -- *** Currently used only for CmdTop (sigh) ***
69 -- * Before the renamer, this list is noSyntaxTable
71 -- * After the renamer, it takes the form [(std_name, HsVar actual_name)]
72 -- For example, for the 'return' op of a monad
73 -- normal case: (GHC.Base.return, HsVar GHC.Base.return)
74 -- with rebindable syntax: (GHC.Base.return, return_22)
75 -- where return_22 is whatever "return" is in scope
77 -- * After the type checker, it takes the form [(std_name, <expression>)]
78 -- where <expression> is the evidence for the method
80 noSyntaxTable :: SyntaxTable id
84 -------------------------
86 = HsVar id -- variable
87 | HsIPVar (IPName id) -- implicit parameter
88 | HsOverLit (HsOverLit id) -- Overloaded literals
89 | HsLit HsLit -- Simple (non-overloaded) literals
91 | HsLam (MatchGroup id) -- Currently always a single match
93 | HsApp (LHsExpr id) -- Application
96 -- Operator applications:
97 -- NB Bracketed ops such as (+) come out as Vars.
99 -- NB We need an expr for the operator in an OpApp/Section since
100 -- the typechecker may need to apply the operator to a few types.
102 | OpApp (LHsExpr id) -- left operand
103 (LHsExpr id) -- operator
104 Fixity -- Renamer adds fixity; bottom until then
105 (LHsExpr id) -- right operand
107 | NegApp (LHsExpr id) -- negated expr
108 (SyntaxExpr id) -- Name of 'negate'
110 | HsPar (LHsExpr id) -- parenthesised expr
112 | SectionL (LHsExpr id) -- operand
113 (LHsExpr id) -- operator
114 | SectionR (LHsExpr id) -- operator
115 (LHsExpr id) -- operand
117 | HsCase (LHsExpr id)
120 | HsIf (LHsExpr id) -- predicate
121 (LHsExpr id) -- then part
122 (LHsExpr id) -- else part
124 | HsLet (HsLocalBinds id) -- let(rec)
127 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
128 -- because in this context we never use
129 -- the PatGuard or ParStmt variant
130 [LStmt id] -- "do":one or more stmts
131 (LHsExpr id) -- The body; the last expression in the 'do'
132 -- of [ body | ... ] in a list comp
133 PostTcType -- Type of the whole expression
135 | ExplicitList -- syntactic list
136 PostTcType -- Gives type of components of list
139 | ExplicitPArr -- syntactic parallel array: [:e1, ..., en:]
140 PostTcType -- type of elements of the parallel array
143 | ExplicitTuple -- tuple
145 -- NB: Unit is ExplicitTuple []
146 -- for tuples, we can get the types
147 -- direct from the components
151 -- Record construction
152 | RecordCon (Located id) -- The constructor. After type checking
153 -- it's the dataConWrapId of the constructor
154 PostTcExpr -- Data con Id applied to type args
158 | RecordUpd (LHsExpr id)
160 PostTcType -- Type of *input* record
161 PostTcType -- Type of *result* record (may differ from
162 -- type of input record)
164 | ExprWithTySig -- e :: type
168 | ExprWithTySigOut -- TRANSLATION
170 (LHsType Name) -- Retain the signature for round-tripping purposes
172 | ArithSeq -- arithmetic sequence
176 | PArrSeq -- arith. sequence for parallel array
177 PostTcExpr -- [:e1..e2:] or [:e1, e2..e3:]
180 | HsSCC FastString -- "set cost centre" (_scc_) annotation
181 (LHsExpr id) -- expr whose cost is to be measured
183 | HsCoreAnn FastString -- hdaume: core annotation
186 -----------------------------------------------------------
187 -- MetaHaskell Extensions
188 | HsBracket (HsBracket id)
190 | HsBracketOut (HsBracket Name) -- Output of the type checker is the *original*
191 [PendingSplice] -- renamed expression, plus *typechecked* splices
192 -- to be pasted back in by the desugarer
194 | HsSpliceE (HsSplice id)
196 -----------------------------------------------------------
197 -- Arrow notation extension
199 | HsProc (LPat id) -- arrow abstraction, proc
200 (LHsCmdTop id) -- body of the abstraction
201 -- always has an empty stack
203 ---------------------------------------
204 -- The following are commands, not expressions proper
206 | HsArrApp -- Arrow tail, or arrow application (f -< arg)
207 (LHsExpr id) -- arrow expression, f
208 (LHsExpr id) -- input expression, arg
209 PostTcType -- type of the arrow expressions f,
210 -- of the form a t t', where arg :: t
211 HsArrAppType -- higher-order (-<<) or first-order (-<)
212 Bool -- True => right-to-left (f -< arg)
213 -- False => left-to-right (arg >- f)
215 | HsArrForm -- Command formation, (| e cmd1 .. cmdn |)
216 (LHsExpr id) -- the operator
217 -- after type-checking, a type abstraction to be
218 -- applied to the type of the local environment tuple
219 (Maybe Fixity) -- fixity (filled in by the renamer), for forms that
220 -- were converted from OpApp's by the renamer
221 [LHsCmdTop id] -- argument commands
225 These constructors only appear temporarily in the parser.
226 The renamer translates them into the Right Thing.
229 | EWildPat -- wildcard
231 | EAsPat (Located id) -- as pattern
234 | ELazyPat (LHsExpr id) -- ~ pattern
236 | HsType (LHsType id) -- Explicit type argument; e.g f {| Int |} x y
239 Everything from here on appears only in typechecker output.
242 | TyLam -- TRANSLATION
245 | TyApp -- TRANSLATION
246 (LHsExpr id) -- generated by Spec
249 -- DictLam and DictApp are "inverses"
257 type PendingSplice = (Name, LHsExpr Id) -- Typechecked splices, waiting to be
258 -- pasted back in by the desugarer
261 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
262 @ClassDictLam dictvars methods expr@ is, therefore:
264 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
268 instance OutputableBndr id => Outputable (HsExpr id) where
269 ppr expr = pprExpr expr
273 pprExpr :: OutputableBndr id => HsExpr id -> SDoc
275 pprExpr e = pprDeeper (ppr_expr e)
277 pprBinds :: OutputableBndr id => HsLocalBinds id -> SDoc
278 pprBinds b = pprDeeper (ppr b)
280 ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
281 ppr_lexpr e = ppr_expr (unLoc e)
283 ppr_expr (HsVar v) = pprHsVar v
284 ppr_expr (HsIPVar v) = ppr v
285 ppr_expr (HsLit lit) = ppr lit
286 ppr_expr (HsOverLit lit) = ppr lit
288 ppr_expr (HsApp e1 e2)
289 = let (fun, args) = collect_args e1 [e2] in
290 (ppr_lexpr fun) <+> (sep (map pprParendExpr args))
292 collect_args (L _ (HsApp fun arg)) args = collect_args fun (arg:args)
293 collect_args fun args = (fun, args)
295 ppr_expr (OpApp e1 op fixity e2)
297 HsVar v -> pp_infixly v
300 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
301 pp_e2 = pprParendExpr e2
304 = hang (ppr op) 4 (sep [pp_e1, pp_e2])
307 = sep [pp_e1, hsep [pprInfix v, pp_e2]]
309 ppr_expr (NegApp e _) = char '-' <+> pprParendExpr e
311 ppr_expr (HsPar e) = parens (ppr_lexpr e)
313 ppr_expr (SectionL expr op)
315 HsVar v -> pp_infixly v
318 pp_expr = pprParendExpr expr
320 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
321 4 (hsep [pp_expr, ptext SLIT("x_ )")])
322 pp_infixly v = parens (sep [pp_expr, pprInfix v])
324 ppr_expr (SectionR op expr)
326 HsVar v -> pp_infixly v
329 pp_expr = pprParendExpr expr
331 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
332 4 ((<>) pp_expr rparen)
334 = parens (sep [pprInfix v, pp_expr])
336 ppr_expr (HsLam matches)
337 = pprMatches LambdaExpr matches
339 ppr_expr (HsCase expr matches)
340 = sep [ sep [ptext SLIT("case"), nest 4 (ppr expr), ptext SLIT("of")],
341 nest 2 (pprMatches CaseAlt matches) ]
343 ppr_expr (HsIf e1 e2 e3)
344 = sep [hsep [ptext SLIT("if"), nest 2 (ppr e1), ptext SLIT("then")],
349 -- special case: let ... in let ...
350 ppr_expr (HsLet binds expr@(L _ (HsLet _ _)))
351 = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
354 ppr_expr (HsLet binds expr)
355 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
356 hang (ptext SLIT("in")) 2 (ppr expr)]
358 ppr_expr (HsDo do_or_list_comp stmts body _) = pprDo do_or_list_comp stmts body
360 ppr_expr (ExplicitList _ exprs)
361 = brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
363 ppr_expr (ExplicitPArr _ exprs)
364 = pa_brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
366 ppr_expr (ExplicitTuple exprs boxity)
367 = tupleParens boxity (sep (punctuate comma (map ppr_lexpr exprs)))
369 ppr_expr (RecordCon con_id con_expr rbinds)
370 = pp_rbinds (ppr con_id) rbinds
372 ppr_expr (RecordUpd aexp rbinds _ _)
373 = pp_rbinds (pprParendExpr aexp) rbinds
375 ppr_expr (ExprWithTySig expr sig)
376 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
378 ppr_expr (ExprWithTySigOut expr sig)
379 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
382 ppr_expr (ArithSeq expr info) = brackets (ppr info)
383 ppr_expr (PArrSeq expr info) = pa_brackets (ppr info)
385 ppr_expr EWildPat = char '_'
386 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
387 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
389 ppr_expr (HsSCC lbl expr)
390 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ftext lbl), pprParendExpr expr ]
392 ppr_expr (TyLam tyvars expr)
393 = hang (hsep [ptext SLIT("/\\"),
394 hsep (map (pprBndr LambdaBind) tyvars),
398 ppr_expr (TyApp expr [ty])
399 = hang (ppr_lexpr expr) 4 (pprParendType ty)
401 ppr_expr (TyApp expr tys)
402 = hang (ppr_lexpr expr)
403 4 (brackets (interpp'SP tys))
405 ppr_expr (DictLam dictvars expr)
406 = hang (hsep [ptext SLIT("\\{-dict-}"),
407 hsep (map (pprBndr LambdaBind) dictvars),
411 ppr_expr (DictApp expr [dname])
412 = hang (ppr_lexpr expr) 4 (ppr dname)
414 ppr_expr (DictApp expr dnames)
415 = hang (ppr_lexpr expr)
416 4 (brackets (interpp'SP dnames))
418 ppr_expr (HsType id) = ppr id
420 ppr_expr (HsSpliceE s) = pprSplice s
421 ppr_expr (HsBracket b) = pprHsBracket b
422 ppr_expr (HsBracketOut e []) = ppr e
423 ppr_expr (HsBracketOut e ps) = ppr e $$ ptext SLIT("pending") <+> ppr ps
425 ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
426 = hsep [ptext SLIT("proc"), ppr pat, ptext SLIT("->"), ppr cmd]
428 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True)
429 = hsep [ppr_lexpr arrow, ptext SLIT("-<"), ppr_lexpr arg]
430 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False)
431 = hsep [ppr_lexpr arg, ptext SLIT(">-"), ppr_lexpr arrow]
432 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp True)
433 = hsep [ppr_lexpr arrow, ptext SLIT("-<<"), ppr_lexpr arg]
434 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp False)
435 = hsep [ppr_lexpr arg, ptext SLIT(">>-"), ppr_lexpr arrow]
437 ppr_expr (HsArrForm (L _ (HsVar v)) (Just _) [arg1, arg2])
438 = sep [pprCmdArg (unLoc arg1), hsep [pprInfix v, pprCmdArg (unLoc arg2)]]
439 ppr_expr (HsArrForm op _ args)
440 = hang (ptext SLIT("(|") <> ppr_lexpr op)
441 4 (sep (map (pprCmdArg.unLoc) args) <> ptext SLIT("|)"))
443 pprCmdArg :: OutputableBndr id => HsCmdTop id -> SDoc
444 pprCmdArg (HsCmdTop cmd@(L _ (HsArrForm _ Nothing [])) _ _ _)
446 pprCmdArg (HsCmdTop cmd _ _ _)
447 = parens (ppr_lexpr cmd)
449 -- Put a var in backquotes if it's not an operator already
450 pprInfix :: Outputable name => name -> SDoc
451 pprInfix v | isOperator ppr_v = ppr_v
452 | otherwise = char '`' <> ppr_v <> char '`'
456 -- add parallel array brackets around a document
458 pa_brackets :: SDoc -> SDoc
459 pa_brackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
462 Parenthesize unless very simple:
464 pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
467 pp_as_was = ppr_lexpr expr
468 -- Using ppr_expr here avoids the call to 'deeper'
469 -- Not sure if that's always right.
476 HsIPVar _ -> pp_as_was
477 ExplicitList _ _ -> pp_as_was
478 ExplicitPArr _ _ -> pp_as_was
479 ExplicitTuple _ _ -> pp_as_was
481 HsBracket _ -> pp_as_was
482 HsBracketOut _ [] -> pp_as_was
484 _ -> parens pp_as_was
487 %************************************************************************
489 \subsection{Commands (in arrow abstractions)}
491 %************************************************************************
493 We re-use HsExpr to represent these.
496 type HsCmd id = HsExpr id
498 type LHsCmd id = LHsExpr id
500 data HsArrAppType = HsHigherOrderApp | HsFirstOrderApp
503 The legal constructors for commands are:
505 = HsArrApp ... -- as above
507 | HsArrForm ... -- as above
512 | HsLam (Match id) -- kappa
514 -- the renamer turns this one into HsArrForm
515 | OpApp (HsExpr id) -- left operand
516 (HsCmd id) -- operator
517 Fixity -- Renamer adds fixity; bottom until then
518 (HsCmd id) -- right operand
520 | HsPar (HsCmd id) -- parenthesised command
523 [Match id] -- bodies are HsCmd's
526 | HsIf (HsExpr id) -- predicate
527 (HsCmd id) -- then part
528 (HsCmd id) -- else part
531 | HsLet (HsLocalBinds id) -- let(rec)
534 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
535 -- because in this context we never use
536 -- the PatGuard or ParStmt variant
537 [Stmt id] -- HsExpr's are really HsCmd's
538 PostTcType -- Type of the whole expression
541 Top-level command, introducing a new arrow.
542 This may occur inside a proc (where the stack is empty) or as an
543 argument of a command-forming operator.
546 type LHsCmdTop id = Located (HsCmdTop id)
549 = HsCmdTop (LHsCmd id)
550 [PostTcType] -- types of inputs on the command's stack
551 PostTcType -- return type of the command
553 -- after type checking:
554 -- names used in the command's desugaring
557 %************************************************************************
559 \subsection{Record binds}
561 %************************************************************************
564 type HsRecordBinds id = [(Located id, LHsExpr id)]
566 recBindFields :: HsRecordBinds id -> [id]
567 recBindFields rbinds = [unLoc field | (field,_) <- rbinds]
569 pp_rbinds :: OutputableBndr id => SDoc -> HsRecordBinds id -> SDoc
570 pp_rbinds thing rbinds
572 4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
574 pp_rbind (v, e) = hsep [pprBndr LetBind (unLoc v), char '=', ppr e]
579 %************************************************************************
581 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
583 %************************************************************************
585 @Match@es are sets of pattern bindings and right hand sides for
586 functions, patterns or case branches. For example, if a function @g@
592 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
594 It is always the case that each element of an @[Match]@ list has the
595 same number of @pats@s inside it. This corresponds to saying that
596 a function defined by pattern matching must have the same number of
597 patterns in each equation.
602 [LMatch id] -- The alternatives
603 PostTcType -- The type is the type of the entire group
604 -- t1 -> ... -> tn -> tr
605 -- where there are n patterns
607 type LMatch id = Located (Match id)
611 [LPat id] -- The patterns
612 (Maybe (LHsType id)) -- A type signature for the result of the match
613 -- Nothing after typechecking
616 hsLMatchPats :: LMatch id -> [LPat id]
617 hsLMatchPats (L _ (Match pats _ _)) = pats
619 -- GRHSs are used both for pattern bindings and for Matches
621 = GRHSs [LGRHS id] -- Guarded RHSs
622 (HsLocalBinds id) -- The where clause
624 type LGRHS id = Located (GRHS id)
626 data GRHS id = GRHS [LStmt id] -- Guards
627 (LHsExpr id) -- Right hand side
630 We know the list must have at least one @Match@ in it.
633 pprMatches :: (OutputableBndr id) => HsMatchContext id -> MatchGroup id -> SDoc
634 pprMatches ctxt (MatchGroup matches _) = vcat (map (pprMatch ctxt) (map unLoc matches))
636 -- Exported to HsBinds, which can't see the defn of HsMatchContext
637 pprFunBind :: (OutputableBndr id) => id -> MatchGroup id -> SDoc
638 pprFunBind fun matches = pprMatches (FunRhs fun) matches
640 -- Exported to HsBinds, which can't see the defn of HsMatchContext
641 pprPatBind :: (OutputableBndr bndr, OutputableBndr id)
642 => LPat bndr -> GRHSs id -> SDoc
643 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
646 pprMatch :: OutputableBndr id => HsMatchContext id -> Match id -> SDoc
647 pprMatch ctxt (Match pats maybe_ty grhss)
648 = pp_name ctxt <+> sep [sep (map ppr pats),
650 nest 2 (pprGRHSs ctxt grhss)]
652 pp_name (FunRhs fun) = ppr fun -- Not pprBndr; the AbsBinds will
653 -- have printed the signature
654 pp_name LambdaExpr = char '\\'
655 pp_name other = empty
657 ppr_maybe_ty = case maybe_ty of
658 Just ty -> dcolon <+> ppr ty
662 pprGRHSs :: OutputableBndr id => HsMatchContext id -> GRHSs id -> SDoc
663 pprGRHSs ctxt (GRHSs grhss binds)
664 = vcat (map (pprGRHS ctxt . unLoc) grhss)
666 (if isEmptyLocalBinds binds then empty
667 else text "where" $$ nest 4 (pprBinds binds))
669 pprGRHS :: OutputableBndr id => HsMatchContext id -> GRHS id -> SDoc
671 pprGRHS ctxt (GRHS [] expr)
674 pprGRHS ctxt (GRHS guards expr)
675 = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
677 pp_rhs ctxt rhs = matchSeparator ctxt <+> pprDeeper (ppr rhs)
680 %************************************************************************
682 \subsection{Do stmts and list comprehensions}
684 %************************************************************************
687 type LStmt id = Located (Stmt id)
689 -- The SyntaxExprs in here are used *only* for do-notation, which
690 -- has rebindable syntax. Otherwise they are unused.
694 (SyntaxExpr id) -- The (>>=) operator
695 (SyntaxExpr id) -- The fail operator
696 -- The fail operator is noSyntaxExpr
697 -- if the pattern match can't fail
699 | ExprStmt (LHsExpr id)
700 (SyntaxExpr id) -- The (>>) operator
701 PostTcType -- Element type of the RHS (used for arrows)
703 | LetStmt (HsLocalBinds id)
705 -- ParStmts only occur in a list comprehension
706 | ParStmt [([LStmt id], [id])] -- After renaming, the ids are the binders
707 -- bound by the stmts and used subsequently
709 -- Recursive statement (see Note [RecStmt] below)
711 --- The next two fields are only valid after renaming
712 [id] -- The ids are a subset of the variables bound by the stmts
713 -- that are used in stmts that follow the RecStmt
715 [id] -- Ditto, but these variables are the "recursive" ones, that
716 -- are used before they are bound in the stmts of the RecStmt
717 -- From a type-checking point of view, these ones have to be monomorphic
719 --- These fields are only valid after typechecking
720 [PostTcExpr] -- These expressions correspond
721 -- 1-to-1 with the "recursive" [id], and are the expresions that
722 -- should be returned by the recursion. They may not quite be the
723 -- Ids themselves, because the Id may be *polymorphic*, but
724 -- the returned thing has to be *monomorphic*.
725 (DictBinds id) -- Method bindings of Ids bound by the RecStmt,
726 -- and used afterwards
729 ExprStmts are a bit tricky, because what they mean
730 depends on the context. Consider the following contexts:
732 A do expression of type (m res_ty)
733 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
734 * ExprStmt E any_ty: do { ....; E; ... }
736 Translation: E >> ...
738 A list comprehensions of type [elt_ty]
739 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
740 * ExprStmt E Bool: [ .. | .... E ]
742 [ .. | .... | ..., E | ... ]
744 Translation: if E then fail else ...
746 A guard list, guarding a RHS of type rhs_ty
747 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
748 * ExprStmt E Bool: f x | ..., E, ... = ...rhs...
750 Translation: if E then fail else ...
752 Array comprehensions are handled like list comprehensions -=chak
759 , RecStmt [a::forall a. a -> a, b]
761 [ BindStmt b (return x)
767 Here, the RecStmt binds a,b,c; but
768 - Only a,b are used in the stmts *following* the RecStmt,
769 This 'a' is *polymorphic'
770 - Only a,c are used in the stmts *inside* the RecStmt
771 *before* their bindings
772 This 'a' is monomorphic
774 Nota Bene: the two a's have different types, even though they
779 instance OutputableBndr id => Outputable (Stmt id) where
780 ppr stmt = pprStmt stmt
782 pprStmt (BindStmt pat expr _ _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
783 pprStmt (LetStmt binds) = hsep [ptext SLIT("let"), pprBinds binds]
784 pprStmt (ExprStmt expr _ _) = ppr expr
785 pprStmt (ParStmt stmtss) = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
786 pprStmt (RecStmt segment _ _ _ _) = ptext SLIT("rec") <+> braces (vcat (map ppr segment))
788 pprDo :: OutputableBndr id => HsStmtContext any -> [LStmt id] -> LHsExpr id -> SDoc
789 pprDo DoExpr stmts body = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts) $$ ppr body)
790 pprDo (MDoExpr _) stmts body = hang (ptext SLIT("mdo")) 3 (vcat (map ppr stmts) $$ ppr body)
791 pprDo ListComp stmts body = pprComp brackets stmts body
792 pprDo PArrComp stmts body = pprComp pa_brackets stmts body
794 pprComp :: OutputableBndr id => (SDoc -> SDoc) -> [LStmt id] -> LHsExpr id -> SDoc
795 pprComp brack quals body
797 hang (ppr body <+> char '|')
801 %************************************************************************
803 Template Haskell quotation brackets
805 %************************************************************************
808 data HsSplice id = HsSplice -- $z or $(f 4)
809 id -- The id is just a unique name to
810 (LHsExpr id) -- identify this splice point
812 instance OutputableBndr id => Outputable (HsSplice id) where
815 pprSplice :: OutputableBndr id => HsSplice id -> SDoc
816 pprSplice (HsSplice n e) = char '$' <> brackets (ppr n) <> pprParendExpr e
819 data HsBracket id = ExpBr (LHsExpr id) -- [| expr |]
820 | PatBr (LPat id) -- [p| pat |]
821 | DecBr (HsGroup id) -- [d| decls |]
822 | TypBr (LHsType id) -- [t| type |]
823 | VarBr id -- 'x, ''T
825 instance OutputableBndr id => Outputable (HsBracket id) where
829 pprHsBracket (ExpBr e) = thBrackets empty (ppr e)
830 pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p)
831 pprHsBracket (DecBr d) = thBrackets (char 'd') (ppr d)
832 pprHsBracket (TypBr t) = thBrackets (char 't') (ppr t)
833 pprHsBracket (VarBr n) = char '\'' <> ppr n
834 -- Infelicity: can't show ' vs '', because
835 -- we can't ask n what its OccName is, because the
836 -- pretty-printer for HsExpr doesn't ask for NamedThings
837 -- But the pretty-printer for names will show the OccName class
839 thBrackets pp_kind pp_body = char '[' <> pp_kind <> char '|' <+>
840 pp_body <+> ptext SLIT("|]")
843 %************************************************************************
845 \subsection{Enumerations and list comprehensions}
847 %************************************************************************
852 | FromThen (LHsExpr id)
854 | FromTo (LHsExpr id)
856 | FromThenTo (LHsExpr id)
862 instance OutputableBndr id => Outputable (ArithSeqInfo id) where
863 ppr (From e1) = hcat [ppr e1, pp_dotdot]
864 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
865 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
866 ppr (FromThenTo e1 e2 e3)
867 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
869 pp_dotdot = ptext SLIT(" .. ")
873 %************************************************************************
875 \subsection{HsMatchCtxt}
877 %************************************************************************
880 data HsMatchContext id -- Context of a Match
881 = FunRhs id -- Function binding for f
882 | CaseAlt -- Guard on a case alternative
883 | LambdaExpr -- Pattern of a lambda
884 | ProcExpr -- Pattern of a proc
885 | PatBindRhs -- Pattern binding
886 | RecUpd -- Record update [used only in DsExpr to tell matchWrapper
887 -- what sort of runtime error message to generate]
888 | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt or list comprehension
891 data HsStmtContext id
894 | MDoExpr PostTcTable -- Recursive do-expression
895 -- (tiresomely, it needs table
896 -- of its return/bind ops)
897 | PArrComp -- Parallel array comprehension
898 | PatGuard (HsMatchContext id) -- Pattern guard for specified thing
899 | ParStmtCtxt (HsStmtContext id) -- A branch of a parallel stmt
903 isDoExpr :: HsStmtContext id -> Bool
904 isDoExpr DoExpr = True
905 isDoExpr (MDoExpr _) = True
906 isDoExpr other = False
910 matchSeparator (FunRhs _) = ptext SLIT("=")
911 matchSeparator CaseAlt = ptext SLIT("->")
912 matchSeparator LambdaExpr = ptext SLIT("->")
913 matchSeparator ProcExpr = ptext SLIT("->")
914 matchSeparator PatBindRhs = ptext SLIT("=")
915 matchSeparator (StmtCtxt _) = ptext SLIT("<-")
916 matchSeparator RecUpd = panic "unused"
920 pprMatchContext (FunRhs fun) = ptext SLIT("the definition of") <+> quotes (ppr fun)
921 pprMatchContext CaseAlt = ptext SLIT("a case alternative")
922 pprMatchContext RecUpd = ptext SLIT("a record-update construct")
923 pprMatchContext PatBindRhs = ptext SLIT("a pattern binding")
924 pprMatchContext LambdaExpr = ptext SLIT("a lambda abstraction")
925 pprMatchContext ProcExpr = ptext SLIT("an arrow abstraction")
926 pprMatchContext (StmtCtxt ctxt) = ptext SLIT("a pattern binding in") $$ pprStmtContext ctxt
928 pprMatchRhsContext (FunRhs fun) = ptext SLIT("a right-hand side of function") <+> quotes (ppr fun)
929 pprMatchRhsContext CaseAlt = ptext SLIT("the body of a case alternative")
930 pprMatchRhsContext PatBindRhs = ptext SLIT("the right-hand side of a pattern binding")
931 pprMatchRhsContext LambdaExpr = ptext SLIT("the body of a lambda")
932 pprMatchRhsContext ProcExpr = ptext SLIT("the body of a proc")
933 pprMatchRhsContext RecUpd = panic "pprMatchRhsContext"
935 pprStmtContext (ParStmtCtxt c) = sep [ptext SLIT("a parallel branch of"), pprStmtContext c]
936 pprStmtContext (PatGuard ctxt) = ptext SLIT("a pattern guard for") $$ pprMatchContext ctxt
937 pprStmtContext DoExpr = ptext SLIT("a 'do' expression")
938 pprStmtContext (MDoExpr _) = ptext SLIT("an 'mdo' expression")
939 pprStmtContext ListComp = ptext SLIT("a list comprehension")
940 pprStmtContext PArrComp = ptext SLIT("an array comprehension")
942 -- Used for the result statement of comprehension
943 -- e.g. the 'e' in [ e | ... ]
944 -- or the 'r' in f x = r
945 pprStmtResultContext (PatGuard ctxt) = pprMatchRhsContext ctxt
946 pprStmtResultContext other = ptext SLIT("the result of") <+> pprStmtContext other
949 -- Used to generate the string for a *runtime* error message
950 matchContextErrString (FunRhs fun) = "function " ++ showSDoc (ppr fun)
951 matchContextErrString CaseAlt = "case"
952 matchContextErrString PatBindRhs = "pattern binding"
953 matchContextErrString RecUpd = "record update"
954 matchContextErrString LambdaExpr = "lambda"
955 matchContextErrString ProcExpr = "proc"
956 matchContextErrString (StmtCtxt (ParStmtCtxt c)) = matchContextErrString (StmtCtxt c)
957 matchContextErrString (StmtCtxt (PatGuard _)) = "pattern guard"
958 matchContextErrString (StmtCtxt DoExpr) = "'do' expression"
959 matchContextErrString (StmtCtxt (MDoExpr _)) = "'mdo' expression"
960 matchContextErrString (StmtCtxt ListComp) = "list comprehension"
961 matchContextErrString (StmtCtxt PArrComp) = "array comprehension"