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 )
13 import HsBinds ( HsBinds(..), nullBinds )
15 import HsLit ( HsLit, HsOverLit )
16 import HsTypes ( HsType, PostTcType, SyntaxName )
17 import HsImpExp ( isOperator, pprHsVar )
20 import ForeignCall ( Safety )
21 import PprType ( pprParendType )
22 import Type ( Type, TyThing )
23 import Var ( TyVar, Id )
25 import DataCon ( DataCon )
26 import CStrings ( CLabelString, pprCLabelString )
27 import BasicTypes ( IPName, Boxity, tupleParens, Fixity(..) )
28 import SrcLoc ( SrcLoc )
33 %************************************************************************
35 \subsection{Expressions proper}
37 %************************************************************************
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 (Match id) -- lambda
47 | HsApp (HsExpr 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 (HsExpr id) -- left operand
57 (HsExpr id) -- operator
58 Fixity -- Renamer adds fixity; bottom until then
59 (HsExpr 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 (HsExpr id) -- negated expr
65 SyntaxName -- Name of 'negate' (see RnEnv.lookupSyntaxName)
67 | HsPar (HsExpr id) -- parenthesised expr
69 | SectionL (HsExpr id) -- operand
70 (HsExpr id) -- operator
71 | SectionR (HsExpr id) -- operator
72 (HsExpr id) -- operand
78 | HsIf (HsExpr id) -- predicate
79 (HsExpr id) -- then part
80 (HsExpr id) -- else part
83 | HsLet (HsBinds id) -- let(rec)
86 | HsWith (HsExpr id) -- implicit parameter binding
87 [(IPName id, HsExpr id)]
88 Bool -- True <=> this was a 'with' binding
89 -- (tmp, until 'with' is removed)
91 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
92 -- because in this context we never use
94 [Stmt id] -- "do":one or more stmts
95 [id] -- Ids for [return,fail,>>=,>>]
97 -- Before type checking, used for rebindable syntax
98 PostTcType -- Type of the whole expression
101 | ExplicitList -- syntactic list
102 PostTcType -- Gives type of components of list
105 | ExplicitPArr -- syntactic parallel array: [:e1, ..., en:]
106 PostTcType -- type of elements of the parallel array
109 | ExplicitTuple -- tuple
111 -- NB: Unit is ExplicitTuple []
112 -- for tuples, we can get the types
113 -- direct from the components
117 -- Record construction
118 | RecordCon id -- The constructor
121 | RecordConOut DataCon
122 (HsExpr id) -- Data con Id applied to type args
127 | RecordUpd (HsExpr id)
130 | RecordUpdOut (HsExpr id) -- TRANSLATION
131 Type -- Type of *input* record
132 Type -- Type of *result* record (may differ from
133 -- type of input record)
136 | ExprWithTySig -- signature binding
139 | ArithSeqIn -- arithmetic sequence
142 (HsExpr id) -- (typechecked, of course)
144 | PArrSeqIn -- arith. sequence for parallel array
145 (ArithSeqInfo id) -- [:e1..e2:] or [:e1, e2..e3:]
147 (HsExpr id) -- (typechecked, of course)
150 | HsCCall CLabelString -- call into the C world; string is
151 [HsExpr id] -- the C function; exprs are the
152 -- arguments to pass.
153 Safety -- True <=> might cause Haskell
154 -- garbage-collection (must generate
155 -- more paranoid code)
156 Bool -- True <=> it's really a "casm"
157 -- NOTE: this CCall is the *boxed*
158 -- version; the desugarer will convert
159 -- it into the unboxed "ccall#".
160 PostTcType -- The result type; will be *bottom*
161 -- until the typechecker gets ahold of it
163 | HsSCC FastString -- "set cost centre" (_scc_) annotation
164 (HsExpr id) -- expr whose cost is to be measured
166 -- MetaHaskell Extensions
167 | HsBracket (HsBracket id) SrcLoc
169 | HsBracketOut (HsBracket Name) -- Output of the type checker is the *original*
170 [PendingSplice] -- renamed expression, plus *typechecked* splices
171 -- to be pasted back in by the desugarer
173 | HsSplice id (HsExpr id) SrcLoc -- $z or $(f 4)
174 -- The id is just a unique name to
175 -- identify this splice point
177 | HsReify (HsReify id) -- reifyType t, reifyDecl i, reifyFixity
181 These constructors only appear temporarily in the parser.
182 The renamer translates them into the Right Thing.
185 | EWildPat -- wildcard
187 | EAsPat id -- as pattern
190 | ELazyPat (HsExpr id) -- ~ pattern
192 | HsType (HsType id) -- Explicit type argument; e.g f {| Int |} x y
195 Everything from here on appears only in typechecker output.
198 | TyLam -- TRANSLATION
201 | TyApp -- TRANSLATION
202 (HsExpr id) -- generated by Spec
205 -- DictLam and DictApp are "inverses"
213 type PendingSplice = (Name, HsExpr Id) -- Typechecked splices, waiting to be
214 -- pasted back in by the desugarer
218 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
219 @ClassDictLam dictvars methods expr@ is, therefore:
221 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
225 instance OutputableBndr id => Outputable (HsExpr id) where
226 ppr expr = pprExpr expr
230 pprExpr :: OutputableBndr id => HsExpr id -> SDoc
232 pprExpr e = pprDeeper (ppr_expr e)
233 pprBinds b = pprDeeper (ppr b)
235 ppr_expr (HsVar v) = pprHsVar v
236 ppr_expr (HsIPVar v) = ppr v
237 ppr_expr (HsLit lit) = ppr lit
238 ppr_expr (HsOverLit lit) = ppr lit
240 ppr_expr (HsLam match) = pprMatch LambdaExpr match
242 ppr_expr expr@(HsApp e1 e2)
243 = let (fun, args) = collect_args expr [] in
244 (ppr_expr fun) <+> (sep (map ppr_expr args))
246 collect_args (HsApp fun arg) args = collect_args fun (arg:args)
247 collect_args fun args = (fun, args)
249 ppr_expr (OpApp e1 op fixity e2)
251 HsVar v -> pp_infixly v
254 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
255 pp_e2 = pprParendExpr e2
258 = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
261 = sep [pp_e1, hsep [pp_v_op, pp_e2]]
264 pp_v_op | isOperator ppr_v = ppr_v
265 | otherwise = char '`' <> ppr_v <> char '`'
266 -- Put it in backquotes if it's not an operator already
268 ppr_expr (NegApp e _) = char '-' <+> pprParendExpr e
270 ppr_expr (HsPar e) = parens (ppr_expr e)
272 ppr_expr (SectionL expr op)
274 HsVar v -> pp_infixly v
277 pp_expr = pprParendExpr expr
279 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
280 4 (hsep [pp_expr, ptext SLIT("x_ )")])
281 pp_infixly v = parens (sep [pp_expr, ppr v])
283 ppr_expr (SectionR op expr)
285 HsVar v -> pp_infixly v
288 pp_expr = pprParendExpr expr
290 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
291 4 ((<>) pp_expr rparen)
293 = parens (sep [ppr v, pp_expr])
295 ppr_expr (HsCase expr matches _)
296 = sep [ sep [ptext SLIT("case"), nest 4 (pprExpr expr), ptext SLIT("of")],
297 nest 2 (pprMatches CaseAlt matches) ]
299 ppr_expr (HsIf e1 e2 e3 _)
300 = sep [hsep [ptext SLIT("if"), nest 2 (pprExpr e1), ptext SLIT("then")],
305 -- special case: let ... in let ...
306 ppr_expr (HsLet binds expr@(HsLet _ _))
307 = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
310 ppr_expr (HsLet binds expr)
311 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
312 hang (ptext SLIT("in")) 2 (ppr expr)]
314 ppr_expr (HsWith expr binds is_with)
315 = sep [hang (ptext SLIT("let")) 2 (pp_ipbinds binds),
316 hang (ptext SLIT("in")) 2 (ppr expr)]
318 ppr_expr (HsDo do_or_list_comp stmts _ _ _) = pprDo do_or_list_comp stmts
320 ppr_expr (ExplicitList _ exprs)
321 = brackets (fsep (punctuate comma (map ppr_expr exprs)))
323 ppr_expr (ExplicitPArr _ exprs)
324 = pa_brackets (fsep (punctuate comma (map ppr_expr exprs)))
326 ppr_expr (ExplicitTuple exprs boxity)
327 = tupleParens boxity (sep (punctuate comma (map ppr_expr exprs)))
329 ppr_expr (RecordCon con_id rbinds)
330 = pp_rbinds (ppr con_id) rbinds
331 ppr_expr (RecordConOut data_con con rbinds)
332 = pp_rbinds (ppr con) rbinds
334 ppr_expr (RecordUpd aexp rbinds)
335 = pp_rbinds (pprParendExpr aexp) rbinds
336 ppr_expr (RecordUpdOut aexp _ _ rbinds)
337 = pp_rbinds (pprParendExpr aexp) rbinds
339 ppr_expr (ExprWithTySig expr sig)
340 = hang (nest 2 (ppr_expr expr) <+> dcolon)
343 ppr_expr (ArithSeqIn info)
344 = brackets (ppr info)
345 ppr_expr (ArithSeqOut expr info)
346 = brackets (ppr info)
348 ppr_expr (PArrSeqIn info)
349 = pa_brackets (ppr info)
350 ppr_expr (PArrSeqOut expr info)
351 = pa_brackets (ppr info)
353 ppr_expr EWildPat = char '_'
354 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
355 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
357 ppr_expr (HsCCall fun args _ is_asm result_ty)
359 then ptext SLIT("_casm_ ``") <> pprCLabelString fun <> ptext SLIT("''")
360 else ptext SLIT("_ccall_") <+> pprCLabelString fun)
361 4 (sep (map pprParendExpr args))
363 ppr_expr (HsSCC lbl expr)
364 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ftext lbl), pprParendExpr expr ]
366 ppr_expr (TyLam tyvars expr)
367 = hang (hsep [ptext SLIT("/\\"),
368 hsep (map (pprBndr LambdaBind) tyvars),
372 ppr_expr (TyApp expr [ty])
373 = hang (ppr_expr expr) 4 (pprParendType ty)
375 ppr_expr (TyApp expr tys)
376 = hang (ppr_expr expr)
377 4 (brackets (interpp'SP tys))
379 ppr_expr (DictLam dictvars expr)
380 = hang (hsep [ptext SLIT("\\{-dict-}"),
381 hsep (map (pprBndr LambdaBind) dictvars),
385 ppr_expr (DictApp expr [dname])
386 = hang (ppr_expr expr) 4 (ppr dname)
388 ppr_expr (DictApp expr dnames)
389 = hang (ppr_expr expr)
390 4 (brackets (interpp'SP dnames))
392 ppr_expr (HsType id) = ppr id
394 ppr_expr (HsSplice n e _) = char '$' <> brackets (ppr n) <> pprParendExpr e
395 ppr_expr (HsBracket b _) = pprHsBracket b
396 ppr_expr (HsBracketOut e ps) = ppr e $$ ptext SLIT("where") <+> ppr ps
397 ppr_expr (HsReify r) = ppr r
399 -- add parallel array brackets around a document
401 pa_brackets :: SDoc -> SDoc
402 pa_brackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
405 Parenthesize unless very simple:
407 pprParendExpr :: OutputableBndr id => HsExpr id -> SDoc
411 pp_as_was = pprExpr expr
418 HsIPVar _ -> pp_as_was
419 ExplicitList _ _ -> pp_as_was
420 ExplicitPArr _ _ -> pp_as_was
421 ExplicitTuple _ _ -> pp_as_was
424 _ -> parens pp_as_was
427 %************************************************************************
429 \subsection{Record binds}
431 %************************************************************************
434 type HsRecordBinds id = [(id, HsExpr id)]
436 recBindFields :: HsRecordBinds id -> [id]
437 recBindFields rbinds = [field | (field,_) <- rbinds]
439 pp_rbinds :: OutputableBndr id => SDoc -> HsRecordBinds id -> SDoc
441 pp_rbinds thing rbinds
443 4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
445 pp_rbind (v, e) = hsep [pprBndr LetBind v, char '=', ppr e]
449 pp_ipbinds :: OutputableBndr id => [(IPName id, HsExpr id)] -> SDoc
450 pp_ipbinds pairs = hsep (punctuate semi (map pp_item pairs))
452 pp_item (id,rhs) = pprBndr LetBind id <+> equals <+> ppr_expr rhs
456 %************************************************************************
458 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
460 %************************************************************************
462 @Match@es are sets of pattern bindings and right hand sides for
463 functions, patterns or case branches. For example, if a function @g@
469 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
471 It is always the case that each element of an @[Match]@ list has the
472 same number of @pats@s inside it. This corresponds to saying that
473 a function defined by pattern matching must have the same number of
474 patterns in each equation.
479 [Pat id] -- The patterns
480 (Maybe (HsType id)) -- A type signature for the result of the match
481 -- Nothing after typechecking
485 -- GRHSs are used both for pattern bindings and for Matches
487 = GRHSs [GRHS id] -- Guarded RHSs
488 (HsBinds id) -- The where clause
489 PostTcType -- Type of RHS (after type checking)
492 = GRHS [Stmt id] -- The RHS is the final ResultStmt
495 mkSimpleMatch :: [Pat id] -> HsExpr id -> Type -> SrcLoc -> Match id
496 mkSimpleMatch pats rhs rhs_ty locn
497 = Match pats Nothing (GRHSs (unguardedRHS rhs locn) EmptyBinds rhs_ty)
499 unguardedRHS :: HsExpr id -> SrcLoc -> [GRHS id]
500 unguardedRHS rhs loc = [GRHS [ResultStmt rhs loc] loc]
503 @getMatchLoc@ takes a @Match@ and returns the
504 source-location gotten from the GRHS inside.
505 THis is something of a nuisance, but no more.
508 getMatchLoc :: Match id -> SrcLoc
509 getMatchLoc (Match _ _ (GRHSs (GRHS _ loc : _) _ _)) = loc
512 We know the list must have at least one @Match@ in it.
515 pprMatches :: (OutputableBndr id) => HsMatchContext id -> [Match id] -> SDoc
516 pprMatches ctxt matches = vcat (map (pprMatch ctxt) matches)
518 -- Exported to HsBinds, which can't see the defn of HsMatchContext
519 pprFunBind :: (OutputableBndr id) => id -> [Match id] -> SDoc
520 pprFunBind fun matches = pprMatches (FunRhs fun) matches
522 -- Exported to HsBinds, which can't see the defn of HsMatchContext
523 pprPatBind :: (OutputableBndr id)
524 => Pat id -> GRHSs id -> SDoc
525 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
528 pprMatch :: OutputableBndr id => HsMatchContext id -> Match id -> SDoc
529 pprMatch ctxt (Match pats maybe_ty grhss)
530 = pp_name ctxt <+> sep [sep (map ppr pats),
532 nest 2 (pprGRHSs ctxt grhss)]
534 pp_name (FunRhs fun) = ppr fun -- Not pprBndr; the AbsBinds will
535 -- have printed the signature
536 pp_name LambdaExpr = char '\\'
537 pp_name other = empty
539 ppr_maybe_ty = case maybe_ty of
540 Just ty -> dcolon <+> ppr ty
544 pprGRHSs :: OutputableBndr id => HsMatchContext id -> GRHSs id -> SDoc
545 pprGRHSs ctxt (GRHSs grhss binds ty)
546 = vcat (map (pprGRHS ctxt) grhss)
548 (if nullBinds binds then empty
549 else text "where" $$ nest 4 (pprDeeper (ppr binds)))
552 pprGRHS :: OutputableBndr id => HsMatchContext id -> GRHS id -> SDoc
554 pprGRHS ctxt (GRHS [ResultStmt expr _] locn)
557 pprGRHS ctxt (GRHS guarded locn)
558 = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
560 ResultStmt expr _ = last guarded -- Last stmt should be a ResultStmt for guards
561 guards = init guarded
563 pp_rhs ctxt rhs = matchSeparator ctxt <+> pprDeeper (ppr rhs)
568 %************************************************************************
570 \subsection{Do stmts and list comprehensions}
572 %************************************************************************
576 = BindStmt (Pat id) (HsExpr id) SrcLoc
577 | LetStmt (HsBinds id)
578 | ResultStmt (HsExpr id) SrcLoc -- See notes that follow
579 | ExprStmt (HsExpr id) PostTcType SrcLoc -- See notes that follow
580 -- The type is the *element type* of the expression
582 -- ParStmts only occur in a list comprehension
583 | ParStmt [[Stmt id]] -- List comp only: parallel set of quals
584 | ParStmtOut [([id], [Stmt id])] -- PLC after renaming; the ids are the binders
585 -- bound by the stmts
587 -- mdo-notation (only exists after renamer)
588 -- The ids are a subset of the variables bound by the stmts that
589 -- either (a) are used before they are bound in the stmts
590 -- or (b) are used in stmts that follow the RecStmt
593 [HsExpr id] -- Post type-checking only; these expressions correspond
594 -- 1-to-1 with the [id], and are the expresions that should
595 -- be returned by the recursion. They may not quite be the
596 -- Ids themselves, because the Id may be polymorphic, but
597 -- the returned thing has to be monomorphic.
600 ExprStmts and ResultStmts are a bit tricky, because what they mean
601 depends on the context. Consider the following contexts:
603 A do expression of type (m res_ty)
604 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
605 * ExprStmt E any_ty: do { ....; E; ... }
607 Translation: E >> ...
609 * ResultStmt E: do { ....; E }
613 A list comprehensions of type [elt_ty]
614 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
615 * ExprStmt E Bool: [ .. | .... E ]
617 [ .. | .... | ..., E | ... ]
619 Translation: if E then fail else ...
621 * ResultStmt E: [ E | ... ]
623 Translation: return E
625 A guard list, guarding a RHS of type rhs_ty
626 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
627 * ExprStmt E Bool: f x | ..., E, ... = ...rhs...
629 Translation: if E then fail else ...
631 * ResultStmt E: f x | ...guards... = E
635 Array comprehensions are handled like list comprehensions -=chak
638 consLetStmt :: HsBinds id -> [Stmt id] -> [Stmt id]
639 consLetStmt EmptyBinds stmts = stmts
640 consLetStmt binds stmts = LetStmt binds : stmts
644 instance OutputableBndr id => Outputable (Stmt id) where
645 ppr stmt = pprStmt stmt
647 pprStmt (BindStmt pat expr _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
648 pprStmt (LetStmt binds) = hsep [ptext SLIT("let"), pprBinds binds]
649 pprStmt (ExprStmt expr _ _) = ppr expr
650 pprStmt (ResultStmt expr _) = ppr expr
651 pprStmt (ParStmt stmtss)
652 = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
653 pprStmt (ParStmtOut stmtss)
654 = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
655 pprStmt (RecStmt _ segment _) = vcat (map ppr segment)
657 pprDo :: OutputableBndr id => HsStmtContext any -> [Stmt id] -> SDoc
658 pprDo DoExpr stmts = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
659 pprDo MDoExpr stmts = hang (ptext SLIT("mdo")) 3 (vcat (map ppr stmts))
660 pprDo ListComp stmts = pprComp brackets stmts
661 pprDo PArrComp stmts = pprComp pa_brackets stmts
663 pprComp :: OutputableBndr id => (SDoc -> SDoc) -> [Stmt id] -> SDoc
664 pprComp brack stmts = brack $
665 hang (pprExpr expr <+> char '|')
668 ResultStmt expr _ = last stmts -- Last stmt should
669 quals = init stmts -- be an ResultStmt
672 %************************************************************************
674 Template Haskell quotation brackets
676 %************************************************************************
679 data HsBracket id = ExpBr (HsExpr id)
684 instance OutputableBndr id => Outputable (HsBracket id) where
688 pprHsBracket (ExpBr e) = thBrackets empty (ppr e)
689 pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p)
690 pprHsBracket (DecBr d) = thBrackets (char 'd') (ppr d)
691 pprHsBracket (TypBr t) = thBrackets (char 't') (ppr t)
694 thBrackets pp_kind pp_body = char '[' <> pp_kind <> char '|' <+>
695 pp_body <+> ptext SLIT("|]")
697 data HsReify id = Reify ReifyFlavour id -- Pre typechecking
698 | ReifyOut ReifyFlavour TyThing -- Post typechecking
700 data ReifyFlavour = ReifyDecl | ReifyType | ReifyFixity
702 instance Outputable id => Outputable (HsReify id) where
703 ppr (Reify flavour id) = ppr flavour <+> ppr id
704 ppr (ReifyOut flavour thing) = ppr flavour <+> ppr thing
706 instance Outputable ReifyFlavour where
707 ppr ReifyDecl = ptext SLIT("reifyDecl")
708 ppr ReifyType = ptext SLIT("reifyType")
709 ppr ReifyFixity = ptext SLIT("reifyFixity")
712 %************************************************************************
714 \subsection{Enumerations and list comprehensions}
716 %************************************************************************
721 | FromThen (HsExpr id)
725 | FromThenTo (HsExpr id)
731 instance OutputableBndr id => Outputable (ArithSeqInfo id) where
732 ppr (From e1) = hcat [ppr e1, pp_dotdot]
733 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
734 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
735 ppr (FromThenTo e1 e2 e3)
736 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
738 pp_dotdot = ptext SLIT(" .. ")
742 %************************************************************************
744 \subsection{HsMatchCtxt}
746 %************************************************************************
749 data HsMatchContext id -- Context of a Match
750 = FunRhs id -- Function binding for f
751 | CaseAlt -- Guard on a case alternative
752 | LambdaExpr -- Pattern of a lambda
753 | PatBindRhs -- Pattern binding
754 | RecUpd -- Record update [used only in DsExpr to tell matchWrapper
755 -- what sort of runtime error message to generate]
756 | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt or list comprehension
759 data HsStmtContext id
762 | MDoExpr -- Recursive do-expression
763 | PArrComp -- Parallel array comprehension
764 | PatGuard (HsMatchContext id) -- Pattern guard for specified thing
768 isDoExpr :: HsStmtContext id -> Bool
769 isDoExpr DoExpr = True
770 isDoExpr MDoExpr = True
771 isDoExpr other = False
775 matchSeparator (FunRhs _) = ptext SLIT("=")
776 matchSeparator CaseAlt = ptext SLIT("->")
777 matchSeparator LambdaExpr = ptext SLIT("->")
778 matchSeparator PatBindRhs = ptext SLIT("=")
779 matchSeparator (StmtCtxt _) = ptext SLIT("<-")
780 matchSeparator RecUpd = panic "unused"
784 pprMatchContext (FunRhs fun) = ptext SLIT("the definition of") <+> quotes (ppr fun)
785 pprMatchContext CaseAlt = ptext SLIT("a case alternative")
786 pprMatchContext RecUpd = ptext SLIT("a record-update construct")
787 pprMatchContext PatBindRhs = ptext SLIT("a pattern binding")
788 pprMatchContext LambdaExpr = ptext SLIT("a lambda abstraction")
789 pprMatchContext (StmtCtxt ctxt) = ptext SLIT("a pattern binding in") $$ pprStmtContext ctxt
791 pprMatchRhsContext (FunRhs fun) = ptext SLIT("a right-hand side of function") <+> quotes (ppr fun)
792 pprMatchRhsContext CaseAlt = ptext SLIT("the body of a case alternative")
793 pprMatchRhsContext PatBindRhs = ptext SLIT("the right-hand side of a pattern binding")
794 pprMatchRhsContext LambdaExpr = ptext SLIT("the body of a lambda")
795 pprMatchRhsContext RecUpd = panic "pprMatchRhsContext"
797 pprStmtContext (PatGuard ctxt) = ptext SLIT("a pattern guard for") $$ pprMatchContext ctxt
798 pprStmtContext DoExpr = ptext SLIT("a 'do' expression")
799 pprStmtContext MDoExpr = ptext SLIT("an 'mdo' expression")
800 pprStmtContext ListComp = ptext SLIT("a list comprehension")
801 pprStmtContext PArrComp = ptext SLIT("an array comprehension")
803 -- Used for the result statement of comprehension
804 -- e.g. the 'e' in [ e | ... ]
805 -- or the 'r' in f x = r
806 pprStmtResultContext (PatGuard ctxt) = pprMatchRhsContext ctxt
807 pprStmtResultContext other = ptext SLIT("the result of") <+> pprStmtContext other
810 -- Used to generate the string for a *runtime* error message
811 matchContextErrString (FunRhs fun) = "function " ++ showSDoc (ppr fun)
812 matchContextErrString CaseAlt = "case"
813 matchContextErrString PatBindRhs = "pattern binding"
814 matchContextErrString RecUpd = "record update"
815 matchContextErrString LambdaExpr = "lambda"
816 matchContextErrString (StmtCtxt (PatGuard _)) = "pattern gaurd"
817 matchContextErrString (StmtCtxt DoExpr) = "'do' expression"
818 matchContextErrString (StmtCtxt MDoExpr) = "'mdo' expression"
819 matchContextErrString (StmtCtxt ListComp) = "list comprehension"
820 matchContextErrString (StmtCtxt PArrComp) = "array comprehension"