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, SyntaxName )
16 import HsImpExp ( isOperator, pprHsVar )
17 import HsBinds ( HsBindGroup )
20 import Type ( Type, pprParendType )
21 import Var ( TyVar, Id )
23 import DataCon ( DataCon )
24 import BasicTypes ( IPName, Boxity, tupleParens, Fixity(..) )
25 import SrcLoc ( Located(..), unLoc )
31 %************************************************************************
33 \subsection{Expressions proper}
35 %************************************************************************
38 type LHsExpr id = Located (HsExpr id)
41 = HsVar id -- variable
42 | HsIPVar (IPName id) -- implicit parameter
43 | HsOverLit HsOverLit -- Overloaded literals; eliminated by type checker
44 | HsLit HsLit -- Simple (non-overloaded) literals
46 | HsLam (LMatch id) -- lambda
47 | HsApp (LHsExpr id) -- application
50 -- Operator applications:
51 -- NB Bracketed ops such as (+) come out as Vars.
53 -- NB We need an expr for the operator in an OpApp/Section since
54 -- the typechecker may need to apply the operator to a few types.
56 | OpApp (LHsExpr id) -- left operand
57 (LHsExpr id) -- operator
58 Fixity -- Renamer adds fixity; bottom until then
59 (LHsExpr id) -- right operand
61 -- We preserve prefix negation and parenthesis for the precedence parser.
62 -- They are eventually removed by the type checker.
64 | NegApp (LHsExpr id) -- negated expr
65 SyntaxName -- Name of 'negate' (see RnEnv.lookupSyntaxName)
67 | HsPar (LHsExpr id) -- parenthesised expr
69 | SectionL (LHsExpr id) -- operand
70 (LHsExpr id) -- operator
71 | SectionR (LHsExpr id) -- operator
72 (LHsExpr id) -- operand
77 | HsIf (LHsExpr id) -- predicate
78 (LHsExpr id) -- then part
79 (LHsExpr id) -- else part
81 | HsLet [HsBindGroup id] -- let(rec)
84 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
85 -- because in this context we never use
86 -- the PatGuard or ParStmt variant
87 [LStmt id] -- "do":one or more stmts
88 (ReboundNames id) -- Ids for [return,fail,>>=,>>]
89 PostTcType -- Type of the whole expression
91 | ExplicitList -- syntactic list
92 PostTcType -- Gives type of components of list
95 | ExplicitPArr -- syntactic parallel array: [:e1, ..., en:]
96 PostTcType -- type of elements of the parallel array
99 | ExplicitTuple -- tuple
101 -- NB: Unit is ExplicitTuple []
102 -- for tuples, we can get the types
103 -- direct from the components
107 -- Record construction
108 | RecordCon (Located id) -- The constructor
111 | RecordConOut DataCon
112 (LHsExpr id) -- Data con Id applied to type args
117 | RecordUpd (LHsExpr id)
120 | RecordUpdOut (LHsExpr id) -- TRANSLATION
121 Type -- Type of *input* record
122 Type -- Type of *result* record (may differ from
123 -- type of input record)
126 | ExprWithTySig -- signature binding
129 | ArithSeqIn -- arithmetic sequence
132 (LHsExpr id) -- (typechecked, of course)
134 | PArrSeqIn -- arith. sequence for parallel array
135 (ArithSeqInfo id) -- [:e1..e2:] or [:e1, e2..e3:]
137 (LHsExpr id) -- (typechecked, of course)
140 | HsSCC FastString -- "set cost centre" (_scc_) annotation
141 (LHsExpr id) -- expr whose cost is to be measured
143 | HsCoreAnn FastString -- hdaume: core annotation
146 -----------------------------------------------------------
147 -- MetaHaskell Extensions
148 | HsBracket (HsBracket id)
150 | HsBracketOut (HsBracket Name) -- Output of the type checker is the *original*
151 [PendingSplice] -- renamed expression, plus *typechecked* splices
152 -- to be pasted back in by the desugarer
154 | HsSpliceE (HsSplice id)
156 -----------------------------------------------------------
157 -- Arrow notation extension
159 | HsProc (LPat id) -- arrow abstraction, proc
160 (LHsCmdTop id) -- body of the abstraction
161 -- always has an empty stack
163 ---------------------------------------
164 -- The following are commands, not expressions proper
166 | HsArrApp -- Arrow tail, or arrow application (f -< arg)
167 (LHsExpr id) -- arrow expression, f
168 (LHsExpr id) -- input expression, arg
169 PostTcType -- type of the arrow expressions f,
170 -- of the form a t t', where arg :: t
171 HsArrAppType -- higher-order (-<<) or first-order (-<)
172 Bool -- True => right-to-left (f -< arg)
173 -- False => left-to-right (arg >- f)
175 | HsArrForm -- Command formation, (| e cmd1 .. cmdn |)
176 (LHsExpr id) -- the operator
177 -- after type-checking, a type abstraction to be
178 -- applied to the type of the local environment tuple
179 (Maybe Fixity) -- fixity (filled in by the renamer), for forms that
180 -- were converted from OpApp's by the renamer
181 [LHsCmdTop id] -- argument commands
185 These constructors only appear temporarily in the parser.
186 The renamer translates them into the Right Thing.
189 | EWildPat -- wildcard
191 | EAsPat (Located id) -- as pattern
194 | ELazyPat (LHsExpr id) -- ~ pattern
196 | HsType (LHsType id) -- Explicit type argument; e.g f {| Int |} x y
199 Everything from here on appears only in typechecker output.
202 | TyLam -- TRANSLATION
205 | TyApp -- TRANSLATION
206 (LHsExpr id) -- generated by Spec
209 -- DictLam and DictApp are "inverses"
217 type PendingSplice = (Name, LHsExpr Id) -- Typechecked splices, waiting to be
218 -- pasted back in by the desugarer
221 Table of bindings of names used in rebindable syntax.
222 This gets filled in by the renamer.
225 type ReboundNames id = [(Name, LHsExpr id)]
226 -- * Before the renamer, this list is empty
228 -- * After the renamer, it takes the form [(std_name, HsVar actual_name)]
229 -- For example, for the 'return' op of a monad
230 -- normal case: (GHC.Base.return, HsVar GHC.Base.return)
231 -- with rebindable syntax: (GHC.Base.return, return_22)
232 -- where return_22 is whatever "return" is in scope
234 -- * After the type checker, it takes the form [(std_name, <expression>)]
235 -- where <expression> is the evidence for the method
238 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
239 @ClassDictLam dictvars methods expr@ is, therefore:
241 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
245 instance OutputableBndr id => Outputable (HsExpr id) where
246 ppr expr = pprExpr expr
250 pprExpr :: OutputableBndr id => HsExpr id -> SDoc
252 pprExpr e = pprDeeper (ppr_expr e)
254 pprBinds :: OutputableBndr id => [HsBindGroup id] -> SDoc
255 pprBinds b = pprDeeper (vcat (map ppr b))
257 ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
258 ppr_lexpr e = ppr_expr (unLoc e)
260 ppr_expr (HsVar v) = pprHsVar v
261 ppr_expr (HsIPVar v) = ppr v
262 ppr_expr (HsLit lit) = ppr lit
263 ppr_expr (HsOverLit lit) = ppr lit
265 ppr_expr (HsLam match) = pprMatch LambdaExpr (unLoc match)
267 ppr_expr (HsApp e1 e2)
268 = let (fun, args) = collect_args e1 [e2] in
269 (ppr_lexpr fun) <+> (sep (map pprParendExpr args))
271 collect_args (L _ (HsApp fun arg)) args = collect_args fun (arg:args)
272 collect_args fun args = (fun, args)
274 ppr_expr (OpApp e1 op fixity e2)
276 HsVar v -> pp_infixly v
279 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
280 pp_e2 = pprParendExpr e2
283 = hang (ppr op) 4 (sep [pp_e1, pp_e2])
286 = sep [pp_e1, hsep [pprInfix v, pp_e2]]
288 ppr_expr (NegApp e _) = char '-' <+> pprParendExpr e
290 ppr_expr (HsPar e) = parens (ppr_lexpr e)
292 ppr_expr (SectionL expr op)
294 HsVar v -> pp_infixly v
297 pp_expr = pprParendExpr expr
299 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
300 4 (hsep [pp_expr, ptext SLIT("x_ )")])
301 pp_infixly v = parens (sep [pp_expr, ppr v])
303 ppr_expr (SectionR op expr)
305 HsVar v -> pp_infixly v
308 pp_expr = pprParendExpr expr
310 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
311 4 ((<>) pp_expr rparen)
313 = parens (sep [ppr v, pp_expr])
315 ppr_expr (HsCase expr matches)
316 = sep [ sep [ptext SLIT("case"), nest 4 (ppr expr), ptext SLIT("of")],
317 nest 2 (pprMatches CaseAlt matches) ]
319 ppr_expr (HsIf e1 e2 e3)
320 = sep [hsep [ptext SLIT("if"), nest 2 (ppr e1), ptext SLIT("then")],
325 -- special case: let ... in let ...
326 ppr_expr (HsLet binds expr@(L _ (HsLet _ _)))
327 = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
330 ppr_expr (HsLet binds expr)
331 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
332 hang (ptext SLIT("in")) 2 (ppr expr)]
334 ppr_expr (HsDo do_or_list_comp stmts _ _) = pprDo do_or_list_comp stmts
336 ppr_expr (ExplicitList _ exprs)
337 = brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
339 ppr_expr (ExplicitPArr _ exprs)
340 = pa_brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
342 ppr_expr (ExplicitTuple exprs boxity)
343 = tupleParens boxity (sep (punctuate comma (map ppr_lexpr exprs)))
345 ppr_expr (RecordCon con_id rbinds)
346 = pp_rbinds (ppr con_id) rbinds
347 ppr_expr (RecordConOut data_con con rbinds)
348 = pp_rbinds (ppr con) rbinds
350 ppr_expr (RecordUpd aexp rbinds)
351 = pp_rbinds (pprParendExpr aexp) rbinds
352 ppr_expr (RecordUpdOut aexp _ _ rbinds)
353 = pp_rbinds (pprParendExpr aexp) rbinds
355 ppr_expr (ExprWithTySig expr sig)
356 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
359 ppr_expr (ArithSeqIn info)
360 = brackets (ppr info)
361 ppr_expr (ArithSeqOut expr info)
362 = brackets (ppr info)
364 ppr_expr (PArrSeqIn info)
365 = pa_brackets (ppr info)
366 ppr_expr (PArrSeqOut expr info)
367 = pa_brackets (ppr info)
369 ppr_expr EWildPat = char '_'
370 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
371 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
373 ppr_expr (HsSCC lbl expr)
374 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ftext lbl), pprParendExpr expr ]
376 ppr_expr (TyLam tyvars expr)
377 = hang (hsep [ptext SLIT("/\\"),
378 hsep (map (pprBndr LambdaBind) tyvars),
382 ppr_expr (TyApp expr [ty])
383 = hang (ppr_lexpr expr) 4 (pprParendType ty)
385 ppr_expr (TyApp expr tys)
386 = hang (ppr_lexpr expr)
387 4 (brackets (interpp'SP tys))
389 ppr_expr (DictLam dictvars expr)
390 = hang (hsep [ptext SLIT("\\{-dict-}"),
391 hsep (map (pprBndr LambdaBind) dictvars),
395 ppr_expr (DictApp expr [dname])
396 = hang (ppr_lexpr expr) 4 (ppr dname)
398 ppr_expr (DictApp expr dnames)
399 = hang (ppr_lexpr expr)
400 4 (brackets (interpp'SP dnames))
402 ppr_expr (HsType id) = ppr id
404 ppr_expr (HsSpliceE s) = pprSplice s
405 ppr_expr (HsBracket b) = pprHsBracket b
406 ppr_expr (HsBracketOut e ps) = ppr e $$ ptext SLIT("where") <+> ppr ps
408 ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
409 = hsep [ptext SLIT("proc"), ppr pat, ptext SLIT("->"), ppr cmd]
411 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True)
412 = hsep [ppr_lexpr arrow, ptext SLIT("-<"), ppr_lexpr arg]
413 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False)
414 = hsep [ppr_lexpr arg, ptext SLIT(">-"), ppr_lexpr arrow]
415 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp True)
416 = hsep [ppr_lexpr arrow, ptext SLIT("-<<"), ppr_lexpr arg]
417 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp False)
418 = hsep [ppr_lexpr arg, ptext SLIT(">>-"), ppr_lexpr arrow]
420 ppr_expr (HsArrForm (L _ (HsVar v)) (Just _) [arg1, arg2])
421 = sep [pprCmdArg (unLoc arg1), hsep [pprInfix v, pprCmdArg (unLoc arg2)]]
422 ppr_expr (HsArrForm op _ args)
423 = hang (ptext SLIT("(|") <> ppr_lexpr op)
424 4 (sep (map (pprCmdArg.unLoc) args) <> ptext SLIT("|)"))
426 pprCmdArg :: OutputableBndr id => HsCmdTop id -> SDoc
427 pprCmdArg (HsCmdTop cmd@(L _ (HsArrForm _ Nothing [])) _ _ _)
429 pprCmdArg (HsCmdTop cmd _ _ _)
430 = parens (ppr_lexpr cmd)
432 -- Put a var in backquotes if it's not an operator already
433 pprInfix :: Outputable name => name -> SDoc
434 pprInfix v | isOperator ppr_v = ppr_v
435 | otherwise = char '`' <> ppr_v <> char '`'
439 -- add parallel array brackets around a document
441 pa_brackets :: SDoc -> SDoc
442 pa_brackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
445 Parenthesize unless very simple:
447 pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
450 pp_as_was = ppr_lexpr expr
451 -- Using ppr_expr here avoids the call to 'deeper'
452 -- Not sure if that's always right.
459 HsIPVar _ -> pp_as_was
460 ExplicitList _ _ -> pp_as_was
461 ExplicitPArr _ _ -> pp_as_was
462 ExplicitTuple _ _ -> pp_as_was
465 _ -> parens pp_as_was
468 %************************************************************************
470 \subsection{Commands (in arrow abstractions)}
472 %************************************************************************
474 We re-use HsExpr to represent these.
477 type HsCmd id = HsExpr id
479 type LHsCmd id = LHsExpr id
481 data HsArrAppType = HsHigherOrderApp | HsFirstOrderApp
484 The legal constructors for commands are:
486 = HsArrApp ... -- as above
488 | HsArrForm ... -- as above
493 | HsLam (Match id) -- kappa
495 -- the renamer turns this one into HsArrForm
496 | OpApp (HsExpr id) -- left operand
497 (HsCmd id) -- operator
498 Fixity -- Renamer adds fixity; bottom until then
499 (HsCmd id) -- right operand
501 | HsPar (HsCmd id) -- parenthesised command
504 [Match id] -- bodies are HsCmd's
507 | HsIf (HsExpr id) -- predicate
508 (HsCmd id) -- then part
509 (HsCmd id) -- else part
512 | HsLet (HsBinds id) -- let(rec)
515 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
516 -- because in this context we never use
517 -- the PatGuard or ParStmt variant
518 [Stmt id] -- HsExpr's are really HsCmd's
520 PostTcType -- Type of the whole expression
523 Top-level command, introducing a new arrow.
524 This may occur inside a proc (where the stack is empty) or as an
525 argument of a command-forming operator.
528 type LHsCmdTop id = Located (HsCmdTop id)
531 = HsCmdTop (LHsCmd id)
532 [PostTcType] -- types of inputs on the command's stack
533 PostTcType -- return type of the command
535 -- after type checking:
536 -- names used in the command's desugaring
539 %************************************************************************
541 \subsection{Record binds}
543 %************************************************************************
546 type HsRecordBinds id = [(Located id, LHsExpr id)]
548 recBindFields :: HsRecordBinds id -> [id]
549 recBindFields rbinds = [unLoc field | (field,_) <- rbinds]
551 pp_rbinds :: OutputableBndr id => SDoc -> HsRecordBinds id -> SDoc
552 pp_rbinds thing rbinds
554 4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
556 pp_rbind (v, e) = hsep [pprBndr LetBind (unLoc v), char '=', ppr e]
561 %************************************************************************
563 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
565 %************************************************************************
567 @Match@es are sets of pattern bindings and right hand sides for
568 functions, patterns or case branches. For example, if a function @g@
574 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
576 It is always the case that each element of an @[Match]@ list has the
577 same number of @pats@s inside it. This corresponds to saying that
578 a function defined by pattern matching must have the same number of
579 patterns in each equation.
582 type LMatch id = Located (Match id)
586 [LPat id] -- The patterns
587 (Maybe (LHsType id)) -- A type signature for the result of the match
588 -- Nothing after typechecking
592 -- GRHSs are used both for pattern bindings and for Matches
594 = GRHSs [LGRHS id] -- Guarded RHSs
595 [HsBindGroup id] -- The where clause
596 PostTcType -- Type of RHS (after type checking)
598 type LGRHS id = Located (GRHS id)
601 = GRHS [LStmt id] -- The RHS is the final ResultStmt
604 We know the list must have at least one @Match@ in it.
607 pprMatches :: (OutputableBndr id) => HsMatchContext id -> [LMatch id] -> SDoc
608 pprMatches ctxt matches = vcat (map (pprMatch ctxt) (map unLoc matches))
610 -- Exported to HsBinds, which can't see the defn of HsMatchContext
611 pprFunBind :: (OutputableBndr id) => id -> [LMatch id] -> SDoc
612 pprFunBind fun matches = pprMatches (FunRhs fun) matches
614 -- Exported to HsBinds, which can't see the defn of HsMatchContext
615 pprPatBind :: (OutputableBndr id)
616 => LPat id -> GRHSs id -> SDoc
617 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
620 pprMatch :: OutputableBndr id => HsMatchContext id -> Match id -> SDoc
621 pprMatch ctxt (Match pats maybe_ty grhss)
622 = pp_name ctxt <+> sep [sep (map ppr pats),
624 nest 2 (pprGRHSs ctxt grhss)]
626 pp_name (FunRhs fun) = ppr fun -- Not pprBndr; the AbsBinds will
627 -- have printed the signature
628 pp_name LambdaExpr = char '\\'
629 pp_name other = empty
631 ppr_maybe_ty = case maybe_ty of
632 Just ty -> dcolon <+> ppr ty
636 pprGRHSs :: OutputableBndr id => HsMatchContext id -> GRHSs id -> SDoc
637 pprGRHSs ctxt (GRHSs grhss binds ty)
638 = vcat (map (pprGRHS ctxt . unLoc) grhss)
640 (if null binds then empty
641 else text "where" $$ nest 4 (pprBinds binds))
643 pprGRHS :: OutputableBndr id => HsMatchContext id -> GRHS id -> SDoc
645 pprGRHS ctxt (GRHS [L _ (ResultStmt expr)])
648 pprGRHS ctxt (GRHS guarded)
649 = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
651 ResultStmt expr = unLoc (last guarded)
652 -- Last stmt should be a ResultStmt for guards
653 guards = init guarded
655 pp_rhs ctxt rhs = matchSeparator ctxt <+> pprDeeper (ppr rhs)
658 %************************************************************************
660 \subsection{Do stmts and list comprehensions}
662 %************************************************************************
665 type LStmt id = Located (Stmt id)
668 = BindStmt (LPat id) (LHsExpr id)
669 | LetStmt [HsBindGroup id]
670 | ResultStmt (LHsExpr id) -- See notes that follow
671 | ExprStmt (LHsExpr id) PostTcType -- See notes that follow
672 -- The type is the *element type* of the expression
674 -- ParStmts only occur in a list comprehension
675 | ParStmt [([LStmt id], [id])] -- After remaing, the ids are the binders
676 -- bound by the stmts and used subsequently
678 -- Recursive statement
680 --- The next two fields are only valid after renaming
681 [id] -- The ids are a subset of the variables bound by the stmts
682 -- that are used in stmts that follow the RecStmt
684 [id] -- Ditto, but these variables are the "recursive" ones, that
685 -- are used before they are bound in the stmts of the RecStmt
686 -- From a type-checking point of view, these ones have to be monomorphic
688 --- This field is only valid after typechecking
689 [LHsExpr id] -- These expressions correspond
690 -- 1-to-1 with the "recursive" [id], and are the expresions that
691 -- should be returned by the recursion. They may not quite be the
692 -- Ids themselves, because the Id may be *polymorphic*, but
693 -- the returned thing has to be *monomorphic*.
696 ExprStmts and ResultStmts are a bit tricky, because what they mean
697 depends on the context. Consider the following contexts:
699 A do expression of type (m res_ty)
700 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
701 * ExprStmt E any_ty: do { ....; E; ... }
703 Translation: E >> ...
705 * ResultStmt E: do { ....; E }
709 A list comprehensions of type [elt_ty]
710 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
711 * ExprStmt E Bool: [ .. | .... E ]
713 [ .. | .... | ..., E | ... ]
715 Translation: if E then fail else ...
717 * ResultStmt E: [ E | ... ]
719 Translation: return E
721 A guard list, guarding a RHS of type rhs_ty
722 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
723 * ExprStmt E Bool: f x | ..., E, ... = ...rhs...
725 Translation: if E then fail else ...
727 * ResultStmt E: f x | ...guards... = E
731 Array comprehensions are handled like list comprehensions -=chak
734 instance OutputableBndr id => Outputable (Stmt id) where
735 ppr stmt = pprStmt stmt
737 pprStmt (BindStmt pat expr) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
738 pprStmt (LetStmt binds) = hsep [ptext SLIT("let"), pprBinds binds]
739 pprStmt (ExprStmt expr _) = ppr expr
740 pprStmt (ResultStmt expr) = ppr expr
741 pprStmt (ParStmt stmtss) = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
742 pprStmt (RecStmt segment _ _ _) = ptext SLIT("rec") <+> braces (vcat (map ppr segment))
744 pprDo :: OutputableBndr id => HsStmtContext any -> [LStmt id] -> SDoc
745 pprDo DoExpr stmts = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
746 pprDo MDoExpr stmts = hang (ptext SLIT("mdo")) 3 (vcat (map ppr stmts))
747 pprDo ListComp stmts = pprComp brackets stmts
748 pprDo PArrComp stmts = pprComp pa_brackets stmts
750 pprComp :: OutputableBndr id => (SDoc -> SDoc) -> [LStmt id] -> SDoc
753 hang (ppr expr <+> char '|')
756 ResultStmt expr = unLoc (last stmts) -- Last stmt should
757 quals = init stmts -- be an ResultStmt
760 %************************************************************************
762 Template Haskell quotation brackets
764 %************************************************************************
767 data HsSplice id = HsSplice -- $z or $(f 4)
768 id -- The id is just a unique name to
769 (LHsExpr id) -- identify this splice point
771 instance OutputableBndr id => Outputable (HsSplice id) where
774 pprSplice :: OutputableBndr id => HsSplice id -> SDoc
775 pprSplice (HsSplice n e) = char '$' <> brackets (ppr n) <> pprParendExpr e
778 data HsBracket id = ExpBr (LHsExpr id) -- [| expr |]
779 | PatBr (LPat id) -- [p| pat |]
780 | DecBr (HsGroup id) -- [d| decls |]
781 | TypBr (LHsType id) -- [t| type |]
782 | VarBr id -- 'x, ''T
784 instance OutputableBndr id => Outputable (HsBracket id) where
788 pprHsBracket (ExpBr e) = thBrackets empty (ppr e)
789 pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p)
790 pprHsBracket (DecBr d) = thBrackets (char 'd') (ppr d)
791 pprHsBracket (TypBr t) = thBrackets (char 't') (ppr t)
792 pprHsBracket (VarBr n) = char '\'' <> ppr n
793 -- Infelicity: can't show ' vs '', because
794 -- we can't ask n what its OccName is, because the
795 -- pretty-printer for HsExpr doesn't ask for NamedThings
796 -- But the pretty-printer for names will show the OccName class
798 thBrackets pp_kind pp_body = char '[' <> pp_kind <> char '|' <+>
799 pp_body <+> ptext SLIT("|]")
802 %************************************************************************
804 \subsection{Enumerations and list comprehensions}
806 %************************************************************************
811 | FromThen (LHsExpr id)
813 | FromTo (LHsExpr id)
815 | FromThenTo (LHsExpr id)
821 instance OutputableBndr id => Outputable (ArithSeqInfo id) where
822 ppr (From e1) = hcat [ppr e1, pp_dotdot]
823 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
824 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
825 ppr (FromThenTo e1 e2 e3)
826 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
828 pp_dotdot = ptext SLIT(" .. ")
832 %************************************************************************
834 \subsection{HsMatchCtxt}
836 %************************************************************************
839 data HsMatchContext id -- Context of a Match
840 = FunRhs id -- Function binding for f
841 | CaseAlt -- Guard on a case alternative
842 | LambdaExpr -- Pattern of a lambda
843 | ProcExpr -- Pattern of a proc
844 | PatBindRhs -- Pattern binding
845 | RecUpd -- Record update [used only in DsExpr to tell matchWrapper
846 -- what sort of runtime error message to generate]
847 | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt or list comprehension
850 data HsStmtContext id
853 | MDoExpr -- Recursive do-expression
854 | PArrComp -- Parallel array comprehension
855 | PatGuard (HsMatchContext id) -- Pattern guard for specified thing
856 | ParStmtCtxt (HsStmtContext id) -- A branch of a parallel stmt
860 isDoExpr :: HsStmtContext id -> Bool
861 isDoExpr DoExpr = True
862 isDoExpr MDoExpr = True
863 isDoExpr other = False
867 matchSeparator (FunRhs _) = ptext SLIT("=")
868 matchSeparator CaseAlt = ptext SLIT("->")
869 matchSeparator LambdaExpr = ptext SLIT("->")
870 matchSeparator ProcExpr = ptext SLIT("->")
871 matchSeparator PatBindRhs = ptext SLIT("=")
872 matchSeparator (StmtCtxt _) = ptext SLIT("<-")
873 matchSeparator RecUpd = panic "unused"
877 pprMatchContext (FunRhs fun) = ptext SLIT("the definition of") <+> quotes (ppr fun)
878 pprMatchContext CaseAlt = ptext SLIT("a case alternative")
879 pprMatchContext RecUpd = ptext SLIT("a record-update construct")
880 pprMatchContext PatBindRhs = ptext SLIT("a pattern binding")
881 pprMatchContext LambdaExpr = ptext SLIT("a lambda abstraction")
882 pprMatchContext ProcExpr = ptext SLIT("an arrow abstraction")
883 pprMatchContext (StmtCtxt ctxt) = ptext SLIT("a pattern binding in") $$ pprStmtContext ctxt
885 pprMatchRhsContext (FunRhs fun) = ptext SLIT("a right-hand side of function") <+> quotes (ppr fun)
886 pprMatchRhsContext CaseAlt = ptext SLIT("the body of a case alternative")
887 pprMatchRhsContext PatBindRhs = ptext SLIT("the right-hand side of a pattern binding")
888 pprMatchRhsContext LambdaExpr = ptext SLIT("the body of a lambda")
889 pprMatchRhsContext ProcExpr = ptext SLIT("the body of a proc")
890 pprMatchRhsContext RecUpd = panic "pprMatchRhsContext"
892 pprStmtContext (ParStmtCtxt c) = sep [ptext SLIT("a parallel branch of"), pprStmtContext c]
893 pprStmtContext (PatGuard ctxt) = ptext SLIT("a pattern guard for") $$ pprMatchContext ctxt
894 pprStmtContext DoExpr = ptext SLIT("a 'do' expression")
895 pprStmtContext MDoExpr = ptext SLIT("an 'mdo' expression")
896 pprStmtContext ListComp = ptext SLIT("a list comprehension")
897 pprStmtContext PArrComp = ptext SLIT("an array comprehension")
899 -- Used for the result statement of comprehension
900 -- e.g. the 'e' in [ e | ... ]
901 -- or the 'r' in f x = r
902 pprStmtResultContext (PatGuard ctxt) = pprMatchRhsContext ctxt
903 pprStmtResultContext other = ptext SLIT("the result of") <+> pprStmtContext other
906 -- Used to generate the string for a *runtime* error message
907 matchContextErrString (FunRhs fun) = "function " ++ showSDoc (ppr fun)
908 matchContextErrString CaseAlt = "case"
909 matchContextErrString PatBindRhs = "pattern binding"
910 matchContextErrString RecUpd = "record update"
911 matchContextErrString LambdaExpr = "lambda"
912 matchContextErrString ProcExpr = "proc"
913 matchContextErrString (StmtCtxt (ParStmtCtxt c)) = matchContextErrString (StmtCtxt c)
914 matchContextErrString (StmtCtxt (PatGuard _)) = "pattern guard"
915 matchContextErrString (StmtCtxt DoExpr) = "'do' expression"
916 matchContextErrString (StmtCtxt MDoExpr) = "'mdo' expression"
917 matchContextErrString (StmtCtxt ListComp) = "list comprehension"
918 matchContextErrString (StmtCtxt PArrComp) = "array comprehension"