2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 {-# LANGUAGE DeriveDataTypeable #-}
8 -- | Abstract Haskell syntax for expressions.
11 #include "HsVersions.h"
26 import Util( dropTail )
27 import StaticFlags( opt_PprStyle_Debug )
32 import Data.Data hiding (Fixity)
36 %************************************************************************
38 \subsection{Expressions proper}
40 %************************************************************************
43 -- * Expressions proper
45 type LHsExpr id = Located (HsExpr id)
47 -------------------------
48 -- | PostTcExpr is an evidence expression attached to the syntax tree by the
49 -- type checker (c.f. postTcType).
50 type PostTcExpr = HsExpr Id
51 -- | We use a PostTcTable where there are a bunch of pieces of evidence, more
52 -- than is convenient to keep individually.
53 type PostTcTable = [(Name, PostTcExpr)]
55 noPostTcExpr :: PostTcExpr
56 noPostTcExpr = HsLit (HsString (fsLit "noPostTcExpr"))
58 noPostTcTable :: PostTcTable
61 -------------------------
62 -- | SyntaxExpr is like 'PostTcExpr', but it's filled in a little earlier,
63 -- by the renamer. It's used for rebindable syntax.
65 -- E.g. @(>>=)@ is filled in before the renamer by the appropriate 'Name' for
66 -- @(>>=)@, and then instantiated by the type checker with its type args
69 type SyntaxExpr id = HsExpr id
71 noSyntaxExpr :: SyntaxExpr id -- Before renaming, and sometimes after,
72 -- (if the syntax slot makes no sense)
73 noSyntaxExpr = HsLit (HsString (fsLit "noSyntaxExpr"))
76 type SyntaxTable id = [(Name, SyntaxExpr id)]
77 -- ^ Currently used only for 'CmdTop' (sigh)
79 -- * Before the renamer, this list is 'noSyntaxTable'
81 -- * After the renamer, it takes the form @[(std_name, HsVar actual_name)]@
82 -- For example, for the 'return' op of a monad
84 -- * normal case: @(GHC.Base.return, HsVar GHC.Base.return)@
86 -- * with rebindable syntax: @(GHC.Base.return, return_22)@
87 -- where @return_22@ is whatever @return@ is in scope
89 -- * After the type checker, it takes the form @[(std_name, <expression>)]@
90 -- where @<expression>@ is the evidence for the method
92 noSyntaxTable :: SyntaxTable id
96 -------------------------
97 -- | A Haskell expression.
99 = HsVar id -- ^ variable
100 | HsIPVar (IPName id) -- ^ implicit parameter
101 | HsOverLit (HsOverLit id) -- ^ Overloaded literals
103 | HsLit HsLit -- ^ Simple (non-overloaded) literals
105 | HsLam (MatchGroup id) -- Currently always a single match
107 | HsApp (LHsExpr id) (LHsExpr id) -- Application
109 -- Operator applications:
110 -- NB Bracketed ops such as (+) come out as Vars.
112 -- NB We need an expr for the operator in an OpApp/Section since
113 -- the typechecker may need to apply the operator to a few types.
115 | OpApp (LHsExpr id) -- left operand
116 (LHsExpr id) -- operator
117 Fixity -- Renamer adds fixity; bottom until then
118 (LHsExpr id) -- right operand
120 | NegApp (LHsExpr id) -- negated expr
121 (SyntaxExpr id) -- Name of 'negate'
123 | HsPar (LHsExpr id) -- parenthesised expr
125 | SectionL (LHsExpr id) -- operand
126 (LHsExpr id) -- operator
127 | SectionR (LHsExpr id) -- operator
128 (LHsExpr id) -- operand
130 | ExplicitTuple -- Used for explicit tuples and sections thereof
134 | HsCase (LHsExpr id)
137 | HsIf (Maybe (SyntaxExpr id)) -- cond function
138 -- Nothing => use the built-in 'if'
139 -- See Note [Rebindable if]
140 (LHsExpr id) -- predicate
141 (LHsExpr id) -- then part
142 (LHsExpr id) -- else part
144 | HsLet (HsLocalBinds id) -- let(rec)
147 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
148 -- because in this context we never use
149 -- the PatGuard or ParStmt variant
150 [LStmt id] -- "do":one or more stmts
151 PostTcType -- Type of the whole expression
153 | ExplicitList -- syntactic list
154 PostTcType -- Gives type of components of list
157 | ExplicitPArr -- syntactic parallel array: [:e1, ..., en:]
158 PostTcType -- type of elements of the parallel array
161 -- Record construction
162 | RecordCon (Located id) -- The constructor. After type checking
163 -- it's the dataConWrapId of the constructor
164 PostTcExpr -- Data con Id applied to type args
168 | RecordUpd (LHsExpr id)
170 -- (HsMatchGroup Id) -- Filled in by the type checker to be
171 -- -- a match that does the job
172 [DataCon] -- Filled in by the type checker to the
173 -- _non-empty_ list of DataCons that have
174 -- all the upd'd fields
175 [PostTcType] -- Argument types of *input* record type
176 [PostTcType] -- and *output* record type
177 -- For a type family, the arg types are of the *instance* tycon,
178 -- not the family tycon
180 | ExprWithTySig -- e :: type
184 | ExprWithTySigOut -- TRANSLATION
186 (LHsType Name) -- Retain the signature for
187 -- round-tripping purposes
189 | ArithSeq -- arithmetic sequence
193 | PArrSeq -- arith. sequence for parallel array
194 PostTcExpr -- [:e1..e2:] or [:e1, e2..e3:]
197 | HsSCC FastString -- "set cost centre" SCC pragma
198 (LHsExpr id) -- expr whose cost is to be measured
200 | HsCoreAnn FastString -- hdaume: core annotation
203 -----------------------------------------------------------
204 -- MetaHaskell Extensions
206 | HsBracket (HsBracket id)
208 | HsBracketOut (HsBracket Name) -- Output of the type checker is
210 [PendingSplice] -- renamed expression, plus
211 -- _typechecked_ splices to be
212 -- pasted back in by the desugarer
214 | HsSpliceE (HsSplice id)
216 | HsQuasiQuoteE (HsQuasiQuote id)
217 -- See Note [Quasi-quote overview] in TcSplice
219 -----------------------------------------------------------
220 -- Arrow notation extension
222 | HsProc (LPat id) -- arrow abstraction, proc
223 (LHsCmdTop id) -- body of the abstraction
224 -- always has an empty stack
226 ---------------------------------------
227 -- The following are commands, not expressions proper
229 | HsArrApp -- Arrow tail, or arrow application (f -< arg)
230 (LHsExpr id) -- arrow expression, f
231 (LHsExpr id) -- input expression, arg
232 PostTcType -- type of the arrow expressions f,
233 -- of the form a t t', where arg :: t
234 HsArrAppType -- higher-order (-<<) or first-order (-<)
235 Bool -- True => right-to-left (f -< arg)
236 -- False => left-to-right (arg >- f)
238 | HsArrForm -- Command formation, (| e cmd1 .. cmdn |)
239 (LHsExpr id) -- the operator
240 -- after type-checking, a type abstraction to be
241 -- applied to the type of the local environment tuple
242 (Maybe Fixity) -- fixity (filled in by the renamer), for forms that
243 -- were converted from OpApp's by the renamer
244 [LHsCmdTop id] -- argument commands
247 ---------------------------------------
248 -- Haskell program coverage (Hpc) Support
251 Int -- module-local tick number
252 [id] -- variables in scope
253 (LHsExpr id) -- sub-expression
256 Int -- module-local tick number for True
257 Int -- module-local tick number for False
258 (LHsExpr id) -- sub-expression
260 | HsTickPragma -- A pragma introduced tick
261 (FastString,(Int,Int),(Int,Int)) -- external span for this tick
264 ---------------------------------------
265 -- These constructors only appear temporarily in the parser.
266 -- The renamer translates them into the Right Thing.
268 | EWildPat -- wildcard
270 | EAsPat (Located id) -- as pattern
273 | EViewPat (LHsExpr id) -- view pattern
276 | ELazyPat (LHsExpr id) -- ~ pattern
278 | HsType (LHsType id) -- Explicit type argument; e.g f {| Int |} x y
280 ---------------------------------------
281 -- Finally, HsWrap appears only in typechecker output
283 | HsWrap HsWrapper -- TRANSLATION
285 deriving (Data, Typeable)
287 -- HsTupArg is used for tuple sections
288 -- (,a,) is represented by ExplicitTuple [Mising ty1, Present a, Missing ty3]
289 -- Which in turn stands for (\x:ty1 \y:ty2. (x,a,y))
291 = Present (LHsExpr id) -- The argument
292 | Missing PostTcType -- The argument is missing, but this is its type
293 deriving (Data, Typeable)
295 tupArgPresent :: HsTupArg id -> Bool
296 tupArgPresent (Present {}) = True
297 tupArgPresent (Missing {}) = False
299 type PendingSplice = (Name, LHsExpr Id) -- Typechecked splices, waiting to be
300 -- pasted back in by the desugarer
305 The rebindable syntax for 'if' is a bit special, because when
306 rebindable syntax is *off* we do not want to treat
308 as if it was an application (ifThenElse c t e). Why not?
309 Because we allow an 'if' to return *unboxed* results, thus
310 if blah then 3# else 4#
311 whereas that would not be possible using a all to a polymorphic function
312 (because you can't call a polymorphic function at an unboxed type).
314 So we use Nothing to mean "use the old built-in typing rule".
317 instance OutputableBndr id => Outputable (HsExpr id) where
318 ppr expr = pprExpr expr
322 -----------------------
323 -- pprExpr, pprLExpr, pprBinds call pprDeeper;
324 -- the underscore versions do not
325 pprLExpr :: OutputableBndr id => LHsExpr id -> SDoc
326 pprLExpr (L _ e) = pprExpr e
328 pprExpr :: OutputableBndr id => HsExpr id -> SDoc
329 pprExpr e | isAtomicHsExpr e || isQuietHsExpr e = ppr_expr e
330 | otherwise = pprDeeper (ppr_expr e)
332 isQuietHsExpr :: HsExpr id -> Bool
333 -- Parentheses do display something, but it gives little info and
334 -- if we go deeper when we go inside them then we get ugly things
336 isQuietHsExpr (HsPar _) = True
337 -- applications don't display anything themselves
338 isQuietHsExpr (HsApp _ _) = True
339 isQuietHsExpr (OpApp _ _ _ _) = True
340 isQuietHsExpr _ = False
342 pprBinds :: (OutputableBndr idL, OutputableBndr idR)
343 => HsLocalBindsLR idL idR -> SDoc
344 pprBinds b = pprDeeper (ppr b)
346 -----------------------
347 ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
348 ppr_lexpr e = ppr_expr (unLoc e)
350 ppr_expr :: OutputableBndr id => HsExpr id -> SDoc
351 ppr_expr (HsVar v) = pprHsVar v
352 ppr_expr (HsIPVar v) = ppr v
353 ppr_expr (HsLit lit) = ppr lit
354 ppr_expr (HsOverLit lit) = ppr lit
355 ppr_expr (HsPar e) = parens (ppr_lexpr e)
357 ppr_expr (HsCoreAnn s e)
358 = vcat [ptext (sLit "HsCoreAnn") <+> ftext s, ppr_lexpr e]
360 ppr_expr (HsApp e1 e2)
361 = let (fun, args) = collect_args e1 [e2] in
362 hang (ppr_lexpr fun) 2 (sep (map pprParendExpr args))
364 collect_args (L _ (HsApp fun arg)) args = collect_args fun (arg:args)
365 collect_args fun args = (fun, args)
367 ppr_expr (OpApp e1 op _ e2)
369 HsVar v -> pp_infixly v
372 pp_e1 = pprDebugParendExpr e1 -- In debug mode, add parens
373 pp_e2 = pprDebugParendExpr e2 -- to make precedence clear
376 = hang (ppr op) 2 (sep [pp_e1, pp_e2])
379 = sep [nest 2 pp_e1, pprHsInfix v, nest 2 pp_e2]
381 ppr_expr (NegApp e _) = char '-' <+> pprDebugParendExpr e
383 ppr_expr (SectionL expr op)
385 HsVar v -> pp_infixly v
388 pp_expr = pprDebugParendExpr expr
390 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
391 4 (hsep [pp_expr, ptext (sLit "x_ )")])
392 pp_infixly v = (sep [pp_expr, pprHsInfix v])
394 ppr_expr (SectionR op expr)
396 HsVar v -> pp_infixly v
399 pp_expr = pprDebugParendExpr expr
401 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext (sLit "x_")])
402 4 ((<>) pp_expr rparen)
404 = (sep [pprHsInfix v, pp_expr])
406 ppr_expr (ExplicitTuple exprs boxity)
407 = tupleParens boxity (fcat (ppr_tup_args exprs))
410 ppr_tup_args (Present e : es) = (ppr_lexpr e <> punc es) : ppr_tup_args es
411 ppr_tup_args (Missing _ : es) = punc es : ppr_tup_args es
413 punc (Present {} : _) = comma <> space
414 punc (Missing {} : _) = comma
417 --avoid using PatternSignatures for stage1 code portability
418 ppr_expr exprType@(HsLam matches)
419 = pprMatches (LambdaExpr `asTypeOf` idType exprType) matches
420 where idType :: HsExpr id -> HsMatchContext id; idType = undefined
422 ppr_expr exprType@(HsCase expr matches)
423 = sep [ sep [ptext (sLit "case"), nest 4 (ppr expr), ptext (sLit "of {")],
424 nest 2 (pprMatches (CaseAlt `asTypeOf` idType exprType) matches <+> char '}') ]
425 where idType :: HsExpr id -> HsMatchContext id; idType = undefined
427 ppr_expr (HsIf _ e1 e2 e3)
428 = sep [hsep [ptext (sLit "if"), nest 2 (ppr e1), ptext (sLit "then")],
433 -- special case: let ... in let ...
434 ppr_expr (HsLet binds expr@(L _ (HsLet _ _)))
435 = sep [hang (ptext (sLit "let")) 2 (hsep [pprBinds binds, ptext (sLit "in")]),
438 ppr_expr (HsLet binds expr)
439 = sep [hang (ptext (sLit "let")) 2 (pprBinds binds),
440 hang (ptext (sLit "in")) 2 (ppr expr)]
442 ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts
444 ppr_expr (ExplicitList _ exprs)
445 = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
447 ppr_expr (ExplicitPArr _ exprs)
448 = pa_brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
450 ppr_expr (RecordCon con_id _ rbinds)
451 = hang (ppr con_id) 2 (ppr rbinds)
453 ppr_expr (RecordUpd aexp rbinds _ _ _)
454 = hang (pprParendExpr aexp) 2 (ppr rbinds)
456 ppr_expr (ExprWithTySig expr sig)
457 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
459 ppr_expr (ExprWithTySigOut expr sig)
460 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
463 ppr_expr (ArithSeq _ info) = brackets (ppr info)
464 ppr_expr (PArrSeq _ info) = pa_brackets (ppr info)
466 ppr_expr EWildPat = char '_'
467 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
468 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
469 ppr_expr (EViewPat p e) = ppr p <+> ptext (sLit "->") <+> ppr e
471 ppr_expr (HsSCC lbl expr)
472 = sep [ ptext (sLit "_scc_") <+> doubleQuotes (ftext lbl),
475 ppr_expr (HsWrap co_fn e) = pprHsWrapper (pprExpr e) co_fn
476 ppr_expr (HsType id) = ppr id
478 ppr_expr (HsSpliceE s) = pprSplice s
479 ppr_expr (HsBracket b) = pprHsBracket b
480 ppr_expr (HsBracketOut e []) = ppr e
481 ppr_expr (HsBracketOut e ps) = ppr e $$ ptext (sLit "pending") <+> ppr ps
482 ppr_expr (HsQuasiQuoteE qq) = ppr qq
484 ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
485 = hsep [ptext (sLit "proc"), ppr pat, ptext (sLit "->"), ppr cmd]
487 ppr_expr (HsTick tickId vars exp)
488 = pprTicks (ppr exp) $
489 hcat [ptext (sLit "tick<"),
492 hsep (map pprHsVar vars),
495 ppr_expr (HsBinTick tickIdTrue tickIdFalse exp)
496 = pprTicks (ppr exp) $
497 hcat [ptext (sLit "bintick<"),
502 ppr exp,ptext (sLit ")")]
503 ppr_expr (HsTickPragma externalSrcLoc exp)
504 = pprTicks (ppr exp) $
505 hcat [ptext (sLit "tickpragma<"),
511 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True)
512 = hsep [ppr_lexpr arrow, ptext (sLit "-<"), ppr_lexpr arg]
513 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False)
514 = hsep [ppr_lexpr arg, ptext (sLit ">-"), ppr_lexpr arrow]
515 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp True)
516 = hsep [ppr_lexpr arrow, ptext (sLit "-<<"), ppr_lexpr arg]
517 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp False)
518 = hsep [ppr_lexpr arg, ptext (sLit ">>-"), ppr_lexpr arrow]
520 ppr_expr (HsArrForm (L _ (HsVar v)) (Just _) [arg1, arg2])
521 = sep [pprCmdArg (unLoc arg1), hsep [pprHsInfix v, pprCmdArg (unLoc arg2)]]
522 ppr_expr (HsArrForm op _ args)
523 = hang (ptext (sLit "(|") <> ppr_lexpr op)
524 4 (sep (map (pprCmdArg.unLoc) args) <> ptext (sLit "|)"))
526 pprCmdArg :: OutputableBndr id => HsCmdTop id -> SDoc
527 pprCmdArg (HsCmdTop cmd@(L _ (HsArrForm _ Nothing [])) _ _ _)
529 pprCmdArg (HsCmdTop cmd _ _ _)
530 = parens (ppr_lexpr cmd)
532 instance OutputableBndr id => Outputable (HsCmdTop id) where
535 -- add parallel array brackets around a document
537 pa_brackets :: SDoc -> SDoc
538 pa_brackets p = ptext (sLit "[:") <> p <> ptext (sLit ":]")
541 HsSyn records exactly where the user put parens, with HsPar.
542 So generally speaking we print without adding any parens.
543 However, some code is internally generated, and in some places
544 parens are absolutely required; so for these places we use
545 pprParendExpr (but don't print double parens of course).
547 For operator applications we don't add parens, because the oprerator
548 fixities should do the job, except in debug mode (-dppr-debug) so we
549 can see the structure of the parse tree.
552 pprDebugParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
553 pprDebugParendExpr expr
554 = getPprStyle (\sty ->
555 if debugStyle sty then pprParendExpr expr
558 pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
561 pp_as_was = pprLExpr expr
562 -- Using pprLExpr makes sure that we go 'deeper'
563 -- I think that is usually (always?) right
566 ArithSeq {} -> pp_as_was
567 PArrSeq {} -> pp_as_was
568 HsLit {} -> pp_as_was
569 HsOverLit {} -> pp_as_was
570 HsVar {} -> pp_as_was
571 HsIPVar {} -> pp_as_was
572 ExplicitTuple {} -> pp_as_was
573 ExplicitList {} -> pp_as_was
574 ExplicitPArr {} -> pp_as_was
575 HsPar {} -> pp_as_was
576 HsBracket {} -> pp_as_was
577 HsBracketOut _ [] -> pp_as_was
579 | isListCompExpr sc -> pp_as_was
580 _ -> parens pp_as_was
582 isAtomicHsExpr :: HsExpr id -> Bool -- A single token
583 isAtomicHsExpr (HsVar {}) = True
584 isAtomicHsExpr (HsLit {}) = True
585 isAtomicHsExpr (HsOverLit {}) = True
586 isAtomicHsExpr (HsIPVar {}) = True
587 isAtomicHsExpr (HsWrap _ e) = isAtomicHsExpr e
588 isAtomicHsExpr (HsPar e) = isAtomicHsExpr (unLoc e)
589 isAtomicHsExpr _ = False
592 %************************************************************************
594 \subsection{Commands (in arrow abstractions)}
596 %************************************************************************
598 We re-use HsExpr to represent these.
601 type HsCmd id = HsExpr id
603 type LHsCmd id = LHsExpr id
605 data HsArrAppType = HsHigherOrderApp | HsFirstOrderApp
606 deriving (Data, Typeable)
609 The legal constructors for commands are:
611 = HsArrApp ... -- as above
613 | HsArrForm ... -- as above
618 | HsLam (Match id) -- kappa
620 -- the renamer turns this one into HsArrForm
621 | OpApp (HsExpr id) -- left operand
622 (HsCmd id) -- operator
623 Fixity -- Renamer adds fixity; bottom until then
624 (HsCmd id) -- right operand
626 | HsPar (HsCmd id) -- parenthesised command
629 [Match id] -- bodies are HsCmd's
632 | HsIf (Maybe (SyntaxExpr id)) -- cond function
633 (HsExpr id) -- predicate
634 (HsCmd id) -- then part
635 (HsCmd id) -- else part
638 | HsLet (HsLocalBinds id) -- let(rec)
641 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
642 -- because in this context we never use
643 -- the PatGuard or ParStmt variant
644 [Stmt id] -- HsExpr's are really HsCmd's
645 PostTcType -- Type of the whole expression
648 Top-level command, introducing a new arrow.
649 This may occur inside a proc (where the stack is empty) or as an
650 argument of a command-forming operator.
653 type LHsCmdTop id = Located (HsCmdTop id)
656 = HsCmdTop (LHsCmd id)
657 [PostTcType] -- types of inputs on the command's stack
658 PostTcType -- return type of the command
659 (SyntaxTable id) -- after type checking:
660 -- names used in the command's desugaring
661 deriving (Data, Typeable)
664 %************************************************************************
666 \subsection{Record binds}
668 %************************************************************************
671 type HsRecordBinds id = HsRecFields id (LHsExpr id)
676 %************************************************************************
678 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
680 %************************************************************************
682 @Match@es are sets of pattern bindings and right hand sides for
683 functions, patterns or case branches. For example, if a function @g@
689 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
691 It is always the case that each element of an @[Match]@ list has the
692 same number of @pats@s inside it. This corresponds to saying that
693 a function defined by pattern matching must have the same number of
694 patterns in each equation.
699 [LMatch id] -- The alternatives
700 PostTcType -- The type is the type of the entire group
701 -- t1 -> ... -> tn -> tr
702 -- where there are n patterns
703 deriving (Data, Typeable)
705 type LMatch id = Located (Match id)
709 [LPat id] -- The patterns
710 (Maybe (LHsType id)) -- A type signature for the result of the match
711 -- Nothing after typechecking
713 deriving (Data, Typeable)
715 isEmptyMatchGroup :: MatchGroup id -> Bool
716 isEmptyMatchGroup (MatchGroup ms _) = null ms
718 matchGroupArity :: MatchGroup id -> Arity
719 matchGroupArity (MatchGroup [] _)
720 = panic "matchGroupArity" -- Precondition: MatchGroup is non-empty
721 matchGroupArity (MatchGroup (match:matches) _)
722 = ASSERT( all ((== n_pats) . length . hsLMatchPats) matches )
723 -- Assertion just checks that all the matches have the same number of pats
726 n_pats = length (hsLMatchPats match)
728 hsLMatchPats :: LMatch id -> [LPat id]
729 hsLMatchPats (L _ (Match pats _ _)) = pats
731 -- | GRHSs are used both for pattern bindings and for Matches
734 grhssGRHSs :: [LGRHS id], -- ^ Guarded RHSs
735 grhssLocalBinds :: (HsLocalBinds id) -- ^ The where clause
736 } deriving (Data, Typeable)
738 type LGRHS id = Located (GRHS id)
740 -- | Guarded Right Hand Side.
741 data GRHS id = GRHS [LStmt id] -- Guards
742 (LHsExpr id) -- Right hand side
743 deriving (Data, Typeable)
746 We know the list must have at least one @Match@ in it.
749 pprMatches :: (OutputableBndr idL, OutputableBndr idR) => HsMatchContext idL -> MatchGroup idR -> SDoc
750 pprMatches ctxt (MatchGroup matches _)
751 = vcat (map (pprMatch ctxt) (map unLoc matches))
752 -- Don't print the type; it's only a place-holder before typechecking
754 -- Exported to HsBinds, which can't see the defn of HsMatchContext
755 pprFunBind :: (OutputableBndr idL, OutputableBndr idR) => idL -> Bool -> MatchGroup idR -> SDoc
756 pprFunBind fun inf matches = pprMatches (FunRhs fun inf) matches
758 -- Exported to HsBinds, which can't see the defn of HsMatchContext
759 pprPatBind :: (OutputableBndr bndr, OutputableBndr id)
760 => LPat bndr -> GRHSs id -> SDoc
761 pprPatBind pat ty@(grhss)
762 = sep [ppr pat, nest 2 (pprGRHSs (PatBindRhs `asTypeOf` idType ty) grhss)]
763 --avoid using PatternSignatures for stage1 code portability
764 where idType :: GRHSs id -> HsMatchContext id; idType = undefined
767 pprMatch :: (OutputableBndr idL, OutputableBndr idR) => HsMatchContext idL -> Match idR -> SDoc
768 pprMatch ctxt (Match pats maybe_ty grhss)
769 = sep [ sep (herald : map (nest 2 . pprParendLPat) other_pats)
770 , nest 2 ppr_maybe_ty
771 , nest 2 (pprGRHSs ctxt grhss) ]
776 | not is_infix -> (ppr fun, pats)
778 -- Not pprBndr; the AbsBinds will
779 -- have printed the signature
781 | null pats2 -> (pp_infix, [])
784 | otherwise -> (parens pp_infix, pats2)
787 pp_infix = pprParendLPat pat1 <+> ppr fun <+> pprParendLPat pat2
789 LambdaExpr -> (char '\\', pats)
791 _ -> ASSERT( null pats1 )
792 (ppr pat1, []) -- No parens around the single pat
796 ppr_maybe_ty = case maybe_ty of
797 Just ty -> dcolon <+> ppr ty
801 pprGRHSs :: (OutputableBndr idL, OutputableBndr idR)
802 => HsMatchContext idL -> GRHSs idR -> SDoc
803 pprGRHSs ctxt (GRHSs grhss binds)
804 = vcat (map (pprGRHS ctxt . unLoc) grhss)
805 $$ ppUnless (isEmptyLocalBinds binds)
806 (text "where" $$ nest 4 (pprBinds binds))
808 pprGRHS :: (OutputableBndr idL, OutputableBndr idR)
809 => HsMatchContext idL -> GRHS idR -> SDoc
811 pprGRHS ctxt (GRHS [] expr)
814 pprGRHS ctxt (GRHS guards expr)
815 = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
817 pp_rhs :: OutputableBndr idR => HsMatchContext idL -> LHsExpr idR -> SDoc
818 pp_rhs ctxt rhs = matchSeparator ctxt <+> pprDeeper (ppr rhs)
821 %************************************************************************
823 \subsection{Do stmts and list comprehensions}
825 %************************************************************************
828 type LStmt id = Located (StmtLR id id)
829 type LStmtLR idL idR = Located (StmtLR idL idR)
831 type Stmt id = StmtLR id id
833 -- The SyntaxExprs in here are used *only* for do-notation and monad
834 -- comprehensions, which have rebindable syntax. Otherwise they are unused.
836 = LastStmt -- Always the last Stmt in ListComp, MonadComp, PArrComp, DoExpr, MDoExpr
837 -- Not used for GhciStmt, PatGuard, which scope over other stuff
839 (SyntaxExpr idR) -- The return operator, used only for MonadComp
840 -- For ListComp, PArrComp, we use the baked-in 'return'
841 -- For DoExpr, MDoExpr, we don't appply a 'return' at all
842 -- See Note [Monad Comprehensions]
843 | BindStmt (LPat idL)
845 (SyntaxExpr idR) -- The (>>=) operator; see Note [The type of bind]
846 (SyntaxExpr idR) -- The fail operator
847 -- The fail operator is noSyntaxExpr
848 -- if the pattern match can't fail
850 | ExprStmt (LHsExpr idR) -- See Note [ExprStmt]
851 (SyntaxExpr idR) -- The (>>) operator
852 (SyntaxExpr idR) -- The `guard` operator; used only in MonadComp
853 -- See notes [Monad Comprehensions]
854 PostTcType -- Element type of the RHS (used for arrows)
856 | LetStmt (HsLocalBindsLR idL idR)
858 -- ParStmts only occur in a list/monad comprehension
859 | ParStmt [([LStmt idL], [idR])]
860 (SyntaxExpr idR) -- Polymorphic `mzip` for monad comprehensions
861 (SyntaxExpr idR) -- The `>>=` operator
862 (SyntaxExpr idR) -- Polymorphic `return` operator
863 -- with type (forall a. a -> m a)
864 -- See notes [Monad Comprehensions]
865 -- After renaming, the ids are the binders
866 -- bound by the stmts and used after them
868 -- "qs, then f by e" ==> TransformStmt qs binders f (Just e) (return) (>>=)
869 -- "qs, then f" ==> TransformStmt qs binders f Nothing (return) (>>=)
871 [LStmt idL] -- Stmts are the ones to the left of the 'then'
873 [idR] -- After renaming, the Ids are the binders occurring
874 -- within this transform statement that are used after it
876 (LHsExpr idR) -- "then f"
878 (Maybe (LHsExpr idR)) -- "by e" (optional)
880 (SyntaxExpr idR) -- The 'return' function for inner monad
882 (SyntaxExpr idR) -- The '(>>=)' operator.
883 -- See Note [Monad Comprehensions]
886 grpS_stmts :: [LStmt idL], -- Stmts to the *left* of the 'group'
887 -- which generates the tuples to be grouped
889 grpS_bndrs :: [(idR, idR)], -- See Note [GroupStmt binder map]
891 grpS_by :: Maybe (LHsExpr idR), -- "by e" (optional)
893 grpS_using :: LHsExpr idR,
894 grpS_explicit :: Bool, -- True <=> explicit "using f"
895 -- False <=> implicit; grpS_using is filled in with
896 -- 'groupWith' (list comprehensions) or
897 -- 'groupM' (monad comprehensions)
899 -- Invariant: if grpS_explicit = False, then grp_by = Just e
900 -- That is, we can have group by e
902 -- group by e using f
904 grpS_ret :: SyntaxExpr idR, -- The 'return' function for inner monad
906 grpS_bind :: SyntaxExpr idR, -- The '(>>=)' operator
907 grpS_fmap :: SyntaxExpr idR -- The polymorphic 'fmap' function for desugaring
908 } -- See Note [Monad Comprehensions]
910 -- Recursive statement (see Note [How RecStmt works] below)
912 { recS_stmts :: [LStmtLR idL idR]
914 -- The next two fields are only valid after renaming
915 , recS_later_ids :: [idR] -- The ids are a subset of the variables bound by the
916 -- stmts that are used in stmts that follow the RecStmt
918 , recS_rec_ids :: [idR] -- Ditto, but these variables are the "recursive" ones,
919 -- that are used before they are bound in the stmts of
921 -- An Id can be in both groups
922 -- Both sets of Ids are (now) treated monomorphically
923 -- See Note [How RecStmt works] for why they are separate
926 , recS_bind_fn :: SyntaxExpr idR -- The bind function
927 , recS_ret_fn :: SyntaxExpr idR -- The return function
928 , recS_mfix_fn :: SyntaxExpr idR -- The mfix function
930 -- These fields are only valid after typechecking
931 , recS_rec_rets :: [PostTcExpr] -- These expressions correspond 1-to-1 with
932 -- recS_rec_ids, and are the
933 -- expressions that should be returned by
935 -- They may not quite be the Ids themselves,
936 -- because the Id may be *polymorphic*, but
937 -- the returned thing has to be *monomorphic*,
938 -- so they may be type applications
940 , recS_ret_ty :: PostTcType -- The type of of do { stmts; return (a,b,c) }
941 -- With rebindable syntax the type might not
942 -- be quite as simple as (m (tya, tyb, tyc)).
944 deriving (Data, Typeable)
947 Note [The type of bind in Stmts]
948 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
949 Some Stmts, notably BindStmt, keep the (>>=) bind operator.
950 We do NOT assume that it has type
951 (>>=) :: m a -> (a -> m b) -> m b
952 In some cases (see Trac #303, #1537) it might have a more
954 (>>=) :: m i j a -> (a -> m j k b) -> m i k b
955 So we must be careful not to make assumptions about the type.
956 In particular, the monad may not be uniform throughout.
958 Note [GroupStmt binder map]
959 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
960 The [(idR,idR)] in a GroupStmt behaves as follows:
962 * Before renaming: []
965 [ (x27,x27), ..., (z35,z35) ]
966 These are the variables
967 bound by the stmts to the left of the 'group'
968 and used either in the 'by' clause,
969 or in the stmts following the 'group'
970 Each item is a pair of identical variables.
972 * After typechecking:
973 [ (x27:Int, x27:[Int]), ..., (z35:Bool, z35:[Bool]) ]
974 Each pair has the same unique, but different *types*.
978 ExprStmts are a bit tricky, because what they mean
979 depends on the context. Consider the following contexts:
981 A do expression of type (m res_ty)
982 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
983 * ExprStmt E any_ty: do { ....; E; ... }
985 Translation: E >> ...
987 A list comprehensions of type [elt_ty]
988 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
989 * ExprStmt E Bool: [ .. | .... E ]
991 [ .. | .... | ..., E | ... ]
993 Translation: if E then fail else ...
995 A guard list, guarding a RHS of type rhs_ty
996 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
997 * ExprStmt E Bool: f x | ..., E, ... = ...rhs...
999 Translation: if E then fail else ...
1001 A monad comprehension of type (m res_ty)
1002 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1003 * ExprStmt E Bool: [ .. | .... E ]
1005 Translation: guard E >> ...
1007 Array comprehensions are handled like list comprehensions.
1009 Note [How RecStmt works]
1010 ~~~~~~~~~~~~~~~~~~~~~~~~
1012 HsDo [ BindStmt x ex
1014 , RecStmt { recS_rec_ids = [a, c]
1015 , recS_stmts = [ BindStmt b (return (a,c))
1016 , LetStmt a = ...b...
1018 , recS_later_ids = [a, b]
1022 Here, the RecStmt binds a,b,c; but
1023 - Only a,b are used in the stmts *following* the RecStmt,
1024 - Only a,c are used in the stmts *inside* the RecStmt
1025 *before* their bindings
1027 Why do we need *both* rec_ids and later_ids? For monads they could be
1028 combined into a single set of variables, but not for arrows. That
1029 follows from the types of the respective feedback operators:
1031 mfix :: MonadFix m => (a -> m a) -> m a
1032 loop :: ArrowLoop a => a (b,d) (c,d) -> a b c
1034 * For mfix, the 'a' covers the union of the later_ids and the rec_ids
1035 * For 'loop', 'c' is the later_ids and 'd' is the rec_ids
1037 Note [Typing a RecStmt]
1038 ~~~~~~~~~~~~~~~~~~~~~~~
1039 A (RecStmt stmts) types as if you had written
1041 (v1,..,vn, _, ..., _) <- mfix (\~(_, ..., _, r1, ..., rm) ->
1043 ; return (v1,..vn, r1, ..., rm) })
1045 where v1..vn are the later_ids
1046 r1..rm are the rec_ids
1048 Note [Monad Comprehensions]
1049 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1050 Monad comprehensions require separate functions like 'return' and
1051 '>>=' for desugaring. These functions are stored in the statements
1052 used in monad comprehensions. For example, the 'return' of the 'LastStmt'
1053 expression is used to lift the body of the monad comprehension:
1057 stmts >>= \bndrs -> return body
1059 In transform and grouping statements ('then ..' and 'then group ..') the
1060 'return' function is required for nested monad comprehensions, for example:
1062 [ body | stmts, then f, rest ]
1064 f [ env | stmts ] >>= \bndrs -> [ body | rest ]
1066 ExprStmts require the 'Control.Monad.guard' function for boolean
1069 [ body | exp, stmts ]
1071 guard exp >> [ body | stmts ]
1073 Grouping/parallel statements require the 'Control.Monad.Group.groupM' and
1074 'Control.Monad.Zip.mzip' functions:
1076 [ body | stmts, then group by e, rest]
1078 groupM [ body | stmts ] >>= \bndrs -> [ body | rest ]
1080 [ body | stmts1 | stmts2 | .. ]
1082 mzip stmts1 (mzip stmts2 (..)) >>= \(bndrs1, (bndrs2, ..)) -> return body
1084 In any other context than 'MonadComp', the fields for most of these
1085 'SyntaxExpr's stay bottom.
1089 instance (OutputableBndr idL, OutputableBndr idR) => Outputable (StmtLR idL idR) where
1090 ppr stmt = pprStmt stmt
1092 pprStmt :: (OutputableBndr idL, OutputableBndr idR) => (StmtLR idL idR) -> SDoc
1093 pprStmt (LastStmt expr _) = ppr expr
1094 pprStmt (BindStmt pat expr _ _) = hsep [ppr pat, ptext (sLit "<-"), ppr expr]
1095 pprStmt (LetStmt binds) = hsep [ptext (sLit "let"), pprBinds binds]
1096 pprStmt (ExprStmt expr _ _ _) = ppr expr
1097 pprStmt (ParStmt stmtss _ _ _) = hsep (map doStmts stmtss)
1098 where doStmts stmts = ptext (sLit "| ") <> ppr stmts
1100 pprStmt (TransformStmt stmts bndrs using by _ _)
1101 = sep (ppr_lc_stmts stmts ++ [pprTransformStmt bndrs using by])
1103 pprStmt (GroupStmt { grpS_stmts = stmts, grpS_by = by, grpS_using = using, grpS_explicit = explicit })
1104 = sep (ppr_lc_stmts stmts ++ [pprGroupStmt by using explicit])
1106 pprStmt (RecStmt { recS_stmts = segment, recS_rec_ids = rec_ids
1107 , recS_later_ids = later_ids })
1108 = ptext (sLit "rec") <+>
1109 vcat [ braces (vcat (map ppr segment))
1110 , ifPprDebug (vcat [ ptext (sLit "rec_ids=") <> ppr rec_ids
1111 , ptext (sLit "later_ids=") <> ppr later_ids])]
1113 pprTransformStmt :: OutputableBndr id => [id] -> LHsExpr id -> Maybe (LHsExpr id) -> SDoc
1114 pprTransformStmt bndrs using by
1115 = sep [ ptext (sLit "then") <+> ifPprDebug (braces (ppr bndrs))
1116 , nest 2 (ppr using)
1117 , nest 2 (pprBy by)]
1119 pprGroupStmt :: OutputableBndr id => Maybe (LHsExpr id)
1120 -> LHsExpr id -> Bool
1122 pprGroupStmt by using explicit
1123 = sep [ ptext (sLit "then group"), nest 2 (pprBy by), nest 2 pp_using ]
1125 pp_using | explicit = ptext (sLit "using") <+> ppr using
1128 pprBy :: OutputableBndr id => Maybe (LHsExpr id) -> SDoc
1129 pprBy Nothing = empty
1130 pprBy (Just e) = ptext (sLit "by") <+> ppr e
1132 pprDo :: OutputableBndr id => HsStmtContext any -> [LStmt id] -> SDoc
1133 pprDo DoExpr stmts = ptext (sLit "do") <+> ppr_do_stmts stmts
1134 pprDo GhciStmt stmts = ptext (sLit "do") <+> ppr_do_stmts stmts
1135 pprDo MDoExpr stmts = ptext (sLit "mdo") <+> ppr_do_stmts stmts
1136 pprDo ListComp stmts = brackets $ pprComp stmts
1137 pprDo PArrComp stmts = pa_brackets $ pprComp stmts
1138 pprDo MonadComp stmts = brackets $ pprComp stmts
1139 pprDo _ _ = panic "pprDo" -- PatGuard, ParStmtCxt
1141 ppr_do_stmts :: OutputableBndr id => [LStmt id] -> SDoc
1142 -- Print a bunch of do stmts, with explicit braces and semicolons,
1143 -- so that we are not vulnerable to layout bugs
1145 = lbrace <+> pprDeeperList vcat (punctuate semi (map ppr stmts))
1148 ppr_lc_stmts :: OutputableBndr id => [LStmt id] -> [SDoc]
1149 ppr_lc_stmts stmts = [ppr s <> comma | s <- stmts]
1151 pprComp :: OutputableBndr id => [LStmt id] -> SDoc
1152 pprComp quals -- Prints: body | qual1, ..., qualn
1154 , L _ (LastStmt body _) <- last quals
1155 = hang (ppr body <+> char '|') 2 (interpp'SP (dropTail 1 quals))
1157 = pprPanic "pprComp" (interpp'SP quals)
1160 %************************************************************************
1162 Template Haskell quotation brackets
1164 %************************************************************************
1167 data HsSplice id = HsSplice -- $z or $(f 4)
1168 id -- The id is just a unique name to
1169 (LHsExpr id) -- identify this splice point
1170 deriving (Data, Typeable)
1172 instance OutputableBndr id => Outputable (HsSplice id) where
1175 pprSplice :: OutputableBndr id => HsSplice id -> SDoc
1176 pprSplice (HsSplice n e)
1177 = char '$' <> ifPprDebug (brackets (ppr n)) <> eDoc
1179 -- We use pprLExpr to match pprParendExpr:
1180 -- Using pprLExpr makes sure that we go 'deeper'
1181 -- I think that is usually (always?) right
1182 pp_as_was = pprLExpr e
1183 eDoc = case unLoc e of
1184 HsPar _ -> pp_as_was
1185 HsVar _ -> pp_as_was
1186 _ -> parens pp_as_was
1188 data HsBracket id = ExpBr (LHsExpr id) -- [| expr |]
1189 | PatBr (LPat id) -- [p| pat |]
1190 | DecBrL [LHsDecl id] -- [d| decls |]; result of parser
1191 | DecBrG (HsGroup id) -- [d| decls |]; result of renamer
1192 | TypBr (LHsType id) -- [t| type |]
1193 | VarBr id -- 'x, ''T
1194 deriving (Data, Typeable)
1196 instance OutputableBndr id => Outputable (HsBracket id) where
1200 pprHsBracket :: OutputableBndr id => HsBracket id -> SDoc
1201 pprHsBracket (ExpBr e) = thBrackets empty (ppr e)
1202 pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p)
1203 pprHsBracket (DecBrG gp) = thBrackets (char 'd') (ppr gp)
1204 pprHsBracket (DecBrL ds) = thBrackets (char 'd') (vcat (map ppr ds))
1205 pprHsBracket (TypBr t) = thBrackets (char 't') (ppr t)
1206 pprHsBracket (VarBr n) = char '\'' <> ppr n
1207 -- Infelicity: can't show ' vs '', because
1208 -- we can't ask n what its OccName is, because the
1209 -- pretty-printer for HsExpr doesn't ask for NamedThings
1210 -- But the pretty-printer for names will show the OccName class
1212 thBrackets :: SDoc -> SDoc -> SDoc
1213 thBrackets pp_kind pp_body = char '[' <> pp_kind <> char '|' <+>
1214 pp_body <+> ptext (sLit "|]")
1217 %************************************************************************
1219 \subsection{Enumerations and list comprehensions}
1221 %************************************************************************
1224 data ArithSeqInfo id
1226 | FromThen (LHsExpr id)
1228 | FromTo (LHsExpr id)
1230 | FromThenTo (LHsExpr id)
1233 deriving (Data, Typeable)
1237 instance OutputableBndr id => Outputable (ArithSeqInfo id) where
1238 ppr (From e1) = hcat [ppr e1, pp_dotdot]
1239 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
1240 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
1241 ppr (FromThenTo e1 e2 e3)
1242 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
1245 pp_dotdot = ptext (sLit " .. ")
1249 %************************************************************************
1251 \subsection{HsMatchCtxt}
1253 %************************************************************************
1256 data HsMatchContext id -- Context of a Match
1257 = FunRhs id Bool -- Function binding for f; True <=> written infix
1258 | LambdaExpr -- Patterns of a lambda
1259 | CaseAlt -- Patterns and guards on a case alternative
1260 | ProcExpr -- Patterns of a proc
1261 | PatBindRhs -- A pattern binding eg [y] <- e = e
1263 | RecUpd -- Record update [used only in DsExpr to
1264 -- tell matchWrapper what sort of
1265 -- runtime error message to generate]
1267 | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt, list comprehension,
1268 -- pattern guard, etc
1270 | ThPatQuote -- A Template Haskell pattern quotation [p| (a,b) |]
1271 deriving (Data, Typeable)
1273 data HsStmtContext id
1276 | PArrComp -- Parallel array comprehension
1278 | DoExpr -- do { ... }
1279 | MDoExpr -- mdo { ... } ie recursive do-expression
1281 | GhciStmt -- A command-line Stmt in GHCi pat <- rhs
1282 | PatGuard (HsMatchContext id) -- Pattern guard for specified thing
1283 | ParStmtCtxt (HsStmtContext id) -- A branch of a parallel stmt
1284 | TransformStmtCtxt (HsStmtContext id) -- A branch of a transform stmt
1285 deriving (Data, Typeable)
1289 isDoExpr :: HsStmtContext id -> Bool
1290 isDoExpr DoExpr = True
1291 isDoExpr MDoExpr = True
1292 isDoExpr GhciStmt = True
1295 isListCompExpr :: HsStmtContext id -> Bool
1296 isListCompExpr ListComp = True
1297 isListCompExpr PArrComp = True
1298 isListCompExpr MonadComp = True
1299 isListCompExpr _ = False
1301 isMonadCompExpr :: HsStmtContext id -> Bool
1302 isMonadCompExpr MonadComp = True
1303 isMonadCompExpr (ParStmtCtxt ctxt) = isMonadCompExpr ctxt
1304 isMonadCompExpr (TransformStmtCtxt ctxt) = isMonadCompExpr ctxt
1305 isMonadCompExpr _ = False
1309 matchSeparator :: HsMatchContext id -> SDoc
1310 matchSeparator (FunRhs {}) = ptext (sLit "=")
1311 matchSeparator CaseAlt = ptext (sLit "->")
1312 matchSeparator LambdaExpr = ptext (sLit "->")
1313 matchSeparator ProcExpr = ptext (sLit "->")
1314 matchSeparator PatBindRhs = ptext (sLit "=")
1315 matchSeparator (StmtCtxt _) = ptext (sLit "<-")
1316 matchSeparator RecUpd = panic "unused"
1317 matchSeparator ThPatQuote = panic "unused"
1321 pprMatchContext :: Outputable id => HsMatchContext id -> SDoc
1322 pprMatchContext ctxt
1323 | want_an ctxt = ptext (sLit "an") <+> pprMatchContextNoun ctxt
1324 | otherwise = ptext (sLit "a") <+> pprMatchContextNoun ctxt
1326 want_an (FunRhs {}) = True -- Use "an" in front
1327 want_an ProcExpr = True
1330 pprMatchContextNoun :: Outputable id => HsMatchContext id -> SDoc
1331 pprMatchContextNoun (FunRhs fun _) = ptext (sLit "equation for")
1332 <+> quotes (ppr fun)
1333 pprMatchContextNoun CaseAlt = ptext (sLit "case alternative")
1334 pprMatchContextNoun RecUpd = ptext (sLit "record-update construct")
1335 pprMatchContextNoun ThPatQuote = ptext (sLit "Template Haskell pattern quotation")
1336 pprMatchContextNoun PatBindRhs = ptext (sLit "pattern binding")
1337 pprMatchContextNoun LambdaExpr = ptext (sLit "lambda abstraction")
1338 pprMatchContextNoun ProcExpr = ptext (sLit "arrow abstraction")
1339 pprMatchContextNoun (StmtCtxt ctxt) = ptext (sLit "pattern binding in")
1340 $$ pprStmtContext ctxt
1343 pprAStmtContext, pprStmtContext :: Outputable id => HsStmtContext id -> SDoc
1344 pprAStmtContext ctxt = article <+> pprStmtContext ctxt
1346 pp_an = ptext (sLit "an")
1347 pp_a = ptext (sLit "a")
1348 article = case ctxt of
1356 pprStmtContext GhciStmt = ptext (sLit "interactive GHCi command")
1357 pprStmtContext DoExpr = ptext (sLit "'do' expression")
1358 pprStmtContext MDoExpr = ptext (sLit "'mdo' expression")
1359 pprStmtContext ListComp = ptext (sLit "list comprehension")
1360 pprStmtContext MonadComp = ptext (sLit "monad comprehension")
1361 pprStmtContext PArrComp = ptext (sLit "array comprehension")
1362 pprStmtContext (PatGuard ctxt) = ptext (sLit "pattern guard for") $$ pprMatchContext ctxt
1364 -- Drop the inner contexts when reporting errors, else we get
1365 -- Unexpected transform statement
1366 -- in a transformed branch of
1367 -- transformed branch of
1368 -- transformed branch of monad comprehension
1369 pprStmtContext (ParStmtCtxt c)
1370 | opt_PprStyle_Debug = sep [ptext (sLit "parallel branch of"), pprAStmtContext c]
1371 | otherwise = pprStmtContext c
1372 pprStmtContext (TransformStmtCtxt c)
1373 | opt_PprStyle_Debug = sep [ptext (sLit "transformed branch of"), pprAStmtContext c]
1374 | otherwise = pprStmtContext c
1377 -- Used to generate the string for a *runtime* error message
1378 matchContextErrString :: Outputable id => HsMatchContext id -> SDoc
1379 matchContextErrString (FunRhs fun _) = ptext (sLit "function") <+> ppr fun
1380 matchContextErrString CaseAlt = ptext (sLit "case")
1381 matchContextErrString PatBindRhs = ptext (sLit "pattern binding")
1382 matchContextErrString RecUpd = ptext (sLit "record update")
1383 matchContextErrString LambdaExpr = ptext (sLit "lambda")
1384 matchContextErrString ProcExpr = ptext (sLit "proc")
1385 matchContextErrString ThPatQuote = panic "matchContextErrString" -- Not used at runtime
1386 matchContextErrString (StmtCtxt (ParStmtCtxt c)) = matchContextErrString (StmtCtxt c)
1387 matchContextErrString (StmtCtxt (TransformStmtCtxt c)) = matchContextErrString (StmtCtxt c)
1388 matchContextErrString (StmtCtxt (PatGuard _)) = ptext (sLit "pattern guard")
1389 matchContextErrString (StmtCtxt GhciStmt) = ptext (sLit "interactive GHCi command")
1390 matchContextErrString (StmtCtxt DoExpr) = ptext (sLit "'do' expression")
1391 matchContextErrString (StmtCtxt MDoExpr) = ptext (sLit "'mdo' expression")
1392 matchContextErrString (StmtCtxt ListComp) = ptext (sLit "list comprehension")
1393 matchContextErrString (StmtCtxt MonadComp) = ptext (sLit "monad comprehension")
1394 matchContextErrString (StmtCtxt PArrComp) = ptext (sLit "array comprehension")
1398 pprMatchInCtxt :: (OutputableBndr idL, OutputableBndr idR)
1399 => HsMatchContext idL -> Match idR -> SDoc
1400 pprMatchInCtxt ctxt match = hang (ptext (sLit "In") <+> pprMatchContext ctxt <> colon)
1401 4 (pprMatch ctxt match)
1403 pprStmtInCtxt :: (OutputableBndr idL, OutputableBndr idR)
1404 => HsStmtContext idL -> StmtLR idL idR -> SDoc
1405 pprStmtInCtxt ctxt stmt = hang (ptext (sLit "In a stmt of") <+> pprAStmtContext ctxt <> colon)
1408 -- For Group and Transform Stmts, don't print the nested stmts!
1409 ppr_stmt (GroupStmt { grpS_by = by, grpS_using = using
1410 , grpS_explicit = explicit }) = pprGroupStmt by using explicit
1411 ppr_stmt (TransformStmt _ bndrs using by _ _) = pprTransformStmt bndrs using by
1412 ppr_stmt stmt = pprStmt stmt