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 (MatchGroup id) -- Currently always a single match
48 | HsApp (LHsExpr id) -- Application
51 -- Operator applications:
52 -- NB Bracketed ops such as (+) come out as Vars.
54 -- NB We need an expr for the operator in an OpApp/Section since
55 -- the typechecker may need to apply the operator to a few types.
57 | OpApp (LHsExpr id) -- left operand
58 (LHsExpr id) -- operator
59 Fixity -- Renamer adds fixity; bottom until then
60 (LHsExpr id) -- right operand
62 -- We preserve prefix negation and parenthesis for the precedence parser.
63 -- They are eventually removed by the type checker.
65 | NegApp (LHsExpr id) -- negated expr
66 SyntaxName -- Name of 'negate' (see RnEnv.lookupSyntaxName)
68 | HsPar (LHsExpr id) -- parenthesised expr
70 | SectionL (LHsExpr id) -- operand
71 (LHsExpr id) -- operator
72 | SectionR (LHsExpr id) -- operator
73 (LHsExpr id) -- operand
78 | HsIf (LHsExpr id) -- predicate
79 (LHsExpr id) -- then part
80 (LHsExpr id) -- else part
82 | HsLet [HsBindGroup id] -- let(rec)
85 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
86 -- because in this context we never use
87 -- the PatGuard or ParStmt variant
88 [LStmt id] -- "do":one or more stmts
89 (ReboundNames id) -- Ids for [return,fail,>>=,>>]
90 PostTcType -- Type of the whole expression
92 | ExplicitList -- syntactic list
93 PostTcType -- Gives type of components of list
96 | ExplicitPArr -- syntactic parallel array: [:e1, ..., en:]
97 PostTcType -- type of elements of the parallel array
100 | ExplicitTuple -- tuple
102 -- NB: Unit is ExplicitTuple []
103 -- for tuples, we can get the types
104 -- direct from the components
108 -- Record construction
109 | RecordCon (Located id) -- The constructor
112 | RecordConOut DataCon
113 (LHsExpr id) -- Data con Id applied to type args
118 | RecordUpd (LHsExpr id)
121 | RecordUpdOut (LHsExpr id) -- TRANSLATION
122 Type -- Type of *input* record
123 Type -- Type of *result* record (may differ from
124 -- type of input record)
127 | ExprWithTySig -- e :: type
131 | ExprWithTySigOut -- TRANSLATION
133 (LHsType Name) -- Retain the signature for round-tripping purposes
135 | ArithSeqIn -- arithmetic sequence
138 (LHsExpr id) -- (typechecked, of course)
140 | PArrSeqIn -- arith. sequence for parallel array
141 (ArithSeqInfo id) -- [:e1..e2:] or [:e1, e2..e3:]
143 (LHsExpr id) -- (typechecked, of course)
146 | HsSCC FastString -- "set cost centre" (_scc_) annotation
147 (LHsExpr id) -- expr whose cost is to be measured
149 | HsCoreAnn FastString -- hdaume: core annotation
152 -----------------------------------------------------------
153 -- MetaHaskell Extensions
154 | HsBracket (HsBracket id)
156 | HsBracketOut (HsBracket Name) -- Output of the type checker is the *original*
157 [PendingSplice] -- renamed expression, plus *typechecked* splices
158 -- to be pasted back in by the desugarer
160 | HsSpliceE (HsSplice id)
162 -----------------------------------------------------------
163 -- Arrow notation extension
165 | HsProc (LPat id) -- arrow abstraction, proc
166 (LHsCmdTop id) -- body of the abstraction
167 -- always has an empty stack
169 ---------------------------------------
170 -- The following are commands, not expressions proper
172 | HsArrApp -- Arrow tail, or arrow application (f -< arg)
173 (LHsExpr id) -- arrow expression, f
174 (LHsExpr id) -- input expression, arg
175 PostTcType -- type of the arrow expressions f,
176 -- of the form a t t', where arg :: t
177 HsArrAppType -- higher-order (-<<) or first-order (-<)
178 Bool -- True => right-to-left (f -< arg)
179 -- False => left-to-right (arg >- f)
181 | HsArrForm -- Command formation, (| e cmd1 .. cmdn |)
182 (LHsExpr id) -- the operator
183 -- after type-checking, a type abstraction to be
184 -- applied to the type of the local environment tuple
185 (Maybe Fixity) -- fixity (filled in by the renamer), for forms that
186 -- were converted from OpApp's by the renamer
187 [LHsCmdTop id] -- argument commands
191 These constructors only appear temporarily in the parser.
192 The renamer translates them into the Right Thing.
195 | EWildPat -- wildcard
197 | EAsPat (Located id) -- as pattern
200 | ELazyPat (LHsExpr id) -- ~ pattern
202 | HsType (LHsType id) -- Explicit type argument; e.g f {| Int |} x y
205 Everything from here on appears only in typechecker output.
208 | TyLam -- TRANSLATION
211 | TyApp -- TRANSLATION
212 (LHsExpr id) -- generated by Spec
215 -- DictLam and DictApp are "inverses"
223 type PendingSplice = (Name, LHsExpr Id) -- Typechecked splices, waiting to be
224 -- pasted back in by the desugarer
227 Table of bindings of names used in rebindable syntax.
228 This gets filled in by the renamer.
231 type ReboundNames id = [(Name, HsExpr id)]
232 -- * Before the renamer, this list is empty
234 -- * After the renamer, it takes the form [(std_name, HsVar actual_name)]
235 -- For example, for the 'return' op of a monad
236 -- normal case: (GHC.Base.return, HsVar GHC.Base.return)
237 -- with rebindable syntax: (GHC.Base.return, return_22)
238 -- where return_22 is whatever "return" is in scope
240 -- * After the type checker, it takes the form [(std_name, <expression>)]
241 -- where <expression> is the evidence for the method
244 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
245 @ClassDictLam dictvars methods expr@ is, therefore:
247 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
251 instance OutputableBndr id => Outputable (HsExpr id) where
252 ppr expr = pprExpr expr
256 pprExpr :: OutputableBndr id => HsExpr id -> SDoc
258 pprExpr e = pprDeeper (ppr_expr e)
260 pprBinds :: OutputableBndr id => [HsBindGroup id] -> SDoc
261 pprBinds b = pprDeeper (vcat (map ppr b))
263 ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
264 ppr_lexpr e = ppr_expr (unLoc e)
266 ppr_expr (HsVar v) = pprHsVar v
267 ppr_expr (HsIPVar v) = ppr v
268 ppr_expr (HsLit lit) = ppr lit
269 ppr_expr (HsOverLit lit) = ppr lit
271 ppr_expr (HsApp e1 e2)
272 = let (fun, args) = collect_args e1 [e2] in
273 (ppr_lexpr fun) <+> (sep (map pprParendExpr args))
275 collect_args (L _ (HsApp fun arg)) args = collect_args fun (arg:args)
276 collect_args fun args = (fun, args)
278 ppr_expr (OpApp e1 op fixity e2)
280 HsVar v -> pp_infixly v
283 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
284 pp_e2 = pprParendExpr e2
287 = hang (ppr op) 4 (sep [pp_e1, pp_e2])
290 = sep [pp_e1, hsep [pprInfix v, pp_e2]]
292 ppr_expr (NegApp e _) = char '-' <+> pprParendExpr e
294 ppr_expr (HsPar e) = parens (ppr_lexpr e)
296 ppr_expr (SectionL expr op)
298 HsVar v -> pp_infixly v
301 pp_expr = pprParendExpr expr
303 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
304 4 (hsep [pp_expr, ptext SLIT("x_ )")])
305 pp_infixly v = parens (sep [pp_expr, ppr v])
307 ppr_expr (SectionR op expr)
309 HsVar v -> pp_infixly v
312 pp_expr = pprParendExpr expr
314 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
315 4 ((<>) pp_expr rparen)
317 = parens (sep [ppr v, pp_expr])
319 ppr_expr (HsLam matches)
320 = pprMatches LambdaExpr matches
322 ppr_expr (HsCase expr matches)
323 = sep [ sep [ptext SLIT("case"), nest 4 (ppr expr), ptext SLIT("of")],
324 nest 2 (pprMatches CaseAlt matches) ]
326 ppr_expr (HsIf e1 e2 e3)
327 = sep [hsep [ptext SLIT("if"), nest 2 (ppr e1), ptext SLIT("then")],
332 -- special case: let ... in let ...
333 ppr_expr (HsLet binds expr@(L _ (HsLet _ _)))
334 = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
337 ppr_expr (HsLet binds expr)
338 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
339 hang (ptext SLIT("in")) 2 (ppr expr)]
341 ppr_expr (HsDo do_or_list_comp stmts _ _) = pprDo do_or_list_comp stmts
343 ppr_expr (ExplicitList _ exprs)
344 = brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
346 ppr_expr (ExplicitPArr _ exprs)
347 = pa_brackets (fsep (punctuate comma (map ppr_lexpr exprs)))
349 ppr_expr (ExplicitTuple exprs boxity)
350 = tupleParens boxity (sep (punctuate comma (map ppr_lexpr exprs)))
352 ppr_expr (RecordCon con_id rbinds)
353 = pp_rbinds (ppr con_id) rbinds
354 ppr_expr (RecordConOut data_con con rbinds)
355 = pp_rbinds (ppr con) rbinds
357 ppr_expr (RecordUpd aexp rbinds)
358 = pp_rbinds (pprParendExpr aexp) rbinds
359 ppr_expr (RecordUpdOut aexp _ _ rbinds)
360 = pp_rbinds (pprParendExpr aexp) rbinds
362 ppr_expr (ExprWithTySig expr sig)
363 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
365 ppr_expr (ExprWithTySigOut expr sig)
366 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
369 ppr_expr (ArithSeqIn info)
370 = brackets (ppr info)
371 ppr_expr (ArithSeqOut expr info)
372 = brackets (ppr info)
374 ppr_expr (PArrSeqIn info)
375 = pa_brackets (ppr info)
376 ppr_expr (PArrSeqOut expr info)
377 = pa_brackets (ppr info)
379 ppr_expr EWildPat = char '_'
380 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
381 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
383 ppr_expr (HsSCC lbl expr)
384 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ftext lbl), pprParendExpr expr ]
386 ppr_expr (TyLam tyvars expr)
387 = hang (hsep [ptext SLIT("/\\"),
388 hsep (map (pprBndr LambdaBind) tyvars),
392 ppr_expr (TyApp expr [ty])
393 = hang (ppr_lexpr expr) 4 (pprParendType ty)
395 ppr_expr (TyApp expr tys)
396 = hang (ppr_lexpr expr)
397 4 (brackets (interpp'SP tys))
399 ppr_expr (DictLam dictvars expr)
400 = hang (hsep [ptext SLIT("\\{-dict-}"),
401 hsep (map (pprBndr LambdaBind) dictvars),
405 ppr_expr (DictApp expr [dname])
406 = hang (ppr_lexpr expr) 4 (ppr dname)
408 ppr_expr (DictApp expr dnames)
409 = hang (ppr_lexpr expr)
410 4 (brackets (interpp'SP dnames))
412 ppr_expr (HsType id) = ppr id
414 ppr_expr (HsSpliceE s) = pprSplice s
415 ppr_expr (HsBracket b) = pprHsBracket b
416 ppr_expr (HsBracketOut e []) = ppr e
417 ppr_expr (HsBracketOut e ps) = ppr e $$ ptext SLIT("pending") <+> ppr ps
419 ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
420 = hsep [ptext SLIT("proc"), ppr pat, ptext SLIT("->"), ppr cmd]
422 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True)
423 = hsep [ppr_lexpr arrow, ptext SLIT("-<"), ppr_lexpr arg]
424 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False)
425 = hsep [ppr_lexpr arg, ptext SLIT(">-"), ppr_lexpr arrow]
426 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp True)
427 = hsep [ppr_lexpr arrow, ptext SLIT("-<<"), ppr_lexpr arg]
428 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp False)
429 = hsep [ppr_lexpr arg, ptext SLIT(">>-"), ppr_lexpr arrow]
431 ppr_expr (HsArrForm (L _ (HsVar v)) (Just _) [arg1, arg2])
432 = sep [pprCmdArg (unLoc arg1), hsep [pprInfix v, pprCmdArg (unLoc arg2)]]
433 ppr_expr (HsArrForm op _ args)
434 = hang (ptext SLIT("(|") <> ppr_lexpr op)
435 4 (sep (map (pprCmdArg.unLoc) args) <> ptext SLIT("|)"))
437 pprCmdArg :: OutputableBndr id => HsCmdTop id -> SDoc
438 pprCmdArg (HsCmdTop cmd@(L _ (HsArrForm _ Nothing [])) _ _ _)
440 pprCmdArg (HsCmdTop cmd _ _ _)
441 = parens (ppr_lexpr cmd)
443 -- Put a var in backquotes if it's not an operator already
444 pprInfix :: Outputable name => name -> SDoc
445 pprInfix v | isOperator ppr_v = ppr_v
446 | otherwise = char '`' <> ppr_v <> char '`'
450 -- add parallel array brackets around a document
452 pa_brackets :: SDoc -> SDoc
453 pa_brackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
456 Parenthesize unless very simple:
458 pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
461 pp_as_was = ppr_lexpr expr
462 -- Using ppr_expr here avoids the call to 'deeper'
463 -- Not sure if that's always right.
470 HsIPVar _ -> pp_as_was
471 ExplicitList _ _ -> pp_as_was
472 ExplicitPArr _ _ -> pp_as_was
473 ExplicitTuple _ _ -> pp_as_was
475 HsBracket _ -> pp_as_was
476 HsBracketOut _ [] -> pp_as_was
478 _ -> parens pp_as_was
481 %************************************************************************
483 \subsection{Commands (in arrow abstractions)}
485 %************************************************************************
487 We re-use HsExpr to represent these.
490 type HsCmd id = HsExpr id
492 type LHsCmd id = LHsExpr id
494 data HsArrAppType = HsHigherOrderApp | HsFirstOrderApp
497 The legal constructors for commands are:
499 = HsArrApp ... -- as above
501 | HsArrForm ... -- as above
506 | HsLam (Match id) -- kappa
508 -- the renamer turns this one into HsArrForm
509 | OpApp (HsExpr id) -- left operand
510 (HsCmd id) -- operator
511 Fixity -- Renamer adds fixity; bottom until then
512 (HsCmd id) -- right operand
514 | HsPar (HsCmd id) -- parenthesised command
517 [Match id] -- bodies are HsCmd's
520 | HsIf (HsExpr id) -- predicate
521 (HsCmd id) -- then part
522 (HsCmd id) -- else part
525 | HsLet (HsBinds id) -- let(rec)
528 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
529 -- because in this context we never use
530 -- the PatGuard or ParStmt variant
531 [Stmt id] -- HsExpr's are really HsCmd's
533 PostTcType -- Type of the whole expression
536 Top-level command, introducing a new arrow.
537 This may occur inside a proc (where the stack is empty) or as an
538 argument of a command-forming operator.
541 type LHsCmdTop id = Located (HsCmdTop id)
544 = HsCmdTop (LHsCmd id)
545 [PostTcType] -- types of inputs on the command's stack
546 PostTcType -- return type of the command
548 -- after type checking:
549 -- names used in the command's desugaring
552 %************************************************************************
554 \subsection{Record binds}
556 %************************************************************************
559 type HsRecordBinds id = [(Located id, LHsExpr id)]
561 recBindFields :: HsRecordBinds id -> [id]
562 recBindFields rbinds = [unLoc field | (field,_) <- rbinds]
564 pp_rbinds :: OutputableBndr id => SDoc -> HsRecordBinds id -> SDoc
565 pp_rbinds thing rbinds
567 4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
569 pp_rbind (v, e) = hsep [pprBndr LetBind (unLoc v), char '=', ppr e]
574 %************************************************************************
576 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
578 %************************************************************************
580 @Match@es are sets of pattern bindings and right hand sides for
581 functions, patterns or case branches. For example, if a function @g@
587 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
589 It is always the case that each element of an @[Match]@ list has the
590 same number of @pats@s inside it. This corresponds to saying that
591 a function defined by pattern matching must have the same number of
592 patterns in each equation.
597 [LMatch id] -- The alternatives
598 PostTcType -- The type is the type of the entire group
599 -- t1 -> ... -> tn -> tr
600 -- where there are n patterns
602 type LMatch id = Located (Match id)
606 [LPat id] -- The patterns
607 (Maybe (LHsType id)) -- A type signature for the result of the match
608 -- Nothing after typechecking
612 hsLMatchPats :: LMatch id -> [LPat id]
613 hsLMatchPats (L _ (Match pats _ _)) = pats
615 -- GRHSs are used both for pattern bindings and for Matches
617 = GRHSs [LGRHS id] -- Guarded RHSs
618 [HsBindGroup id] -- The where clause
620 -- PostTcType -- Type of RHS (after type checking)
622 type LGRHS id = Located (GRHS id)
625 = GRHS [LStmt id] -- The RHS is the final ResultStmt
628 We know the list must have at least one @Match@ in it.
631 pprMatches :: (OutputableBndr id) => HsMatchContext id -> MatchGroup id -> SDoc
632 pprMatches ctxt (MatchGroup matches _) = vcat (map (pprMatch ctxt) (map unLoc matches))
634 -- Exported to HsBinds, which can't see the defn of HsMatchContext
635 pprFunBind :: (OutputableBndr id) => id -> MatchGroup id -> SDoc
636 pprFunBind fun matches = pprMatches (FunRhs fun) matches
638 -- Exported to HsBinds, which can't see the defn of HsMatchContext
639 pprPatBind :: (OutputableBndr bndr, OutputableBndr id)
640 => LPat bndr -> GRHSs id -> SDoc
641 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
644 pprMatch :: OutputableBndr id => HsMatchContext id -> Match id -> SDoc
646 pprMatch ctxt (Match pats maybe_ty grhss)
647 = pp_name ctxt <+> sep [sep (map ppr pats),
649 nest 2 (pprGRHSs ctxt grhss)]
651 pp_name (FunRhs fun) = ppr fun -- Not pprBndr; the AbsBinds will
652 -- have printed the signature
653 pp_name LambdaExpr = char '\\'
654 pp_name other = empty
656 ppr_maybe_ty = case maybe_ty of
657 Just ty -> dcolon <+> ppr ty
661 pprGRHSs :: OutputableBndr id => HsMatchContext id -> GRHSs id -> SDoc
663 pprGRHSs ctxt (GRHSs grhss binds)
664 = vcat (map (pprGRHS ctxt . unLoc) grhss)
666 (if null binds then empty
667 else text "where" $$ nest 4 (pprBinds binds))
669 pprGRHS :: OutputableBndr id => HsMatchContext id -> GRHS id -> SDoc
671 pprGRHS ctxt (GRHS [L _ (ResultStmt expr)])
674 pprGRHS ctxt (GRHS guarded)
675 = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
677 ResultStmt expr = unLoc (last guarded)
678 -- Last stmt should be a ResultStmt for guards
679 guards = init guarded
681 pp_rhs ctxt rhs = matchSeparator ctxt <+> pprDeeper (ppr rhs)
684 %************************************************************************
686 \subsection{Do stmts and list comprehensions}
688 %************************************************************************
691 type LStmt id = Located (Stmt id)
694 = BindStmt (LPat id) (LHsExpr id)
695 | LetStmt [HsBindGroup id]
696 | ResultStmt (LHsExpr id) -- See notes that follow
697 | ExprStmt (LHsExpr id) PostTcType -- See notes that follow
698 -- The type is the *element type* of the expression
700 -- ParStmts only occur in a list comprehension
701 | ParStmt [([LStmt id], [id])] -- After remaing, the ids are the binders
702 -- bound by the stmts and used subsequently
704 -- Recursive statement (see Note [RecStmt] below)
706 --- The next two fields are only valid after renaming
707 [id] -- The ids are a subset of the variables bound by the stmts
708 -- that are used in stmts that follow the RecStmt
710 [id] -- Ditto, but these variables are the "recursive" ones, that
711 -- are used before they are bound in the stmts of the RecStmt
712 -- From a type-checking point of view, these ones have to be monomorphic
714 --- This field is only valid after typechecking
715 [LHsExpr id] -- These expressions correspond
716 -- 1-to-1 with the "recursive" [id], and are the expresions that
717 -- should be returned by the recursion. They may not quite be the
718 -- Ids themselves, because the Id may be *polymorphic*, but
719 -- the returned thing has to be *monomorphic*.
722 ExprStmts and ResultStmts are a bit tricky, because what they mean
723 depends on the context. Consider the following contexts:
725 A do expression of type (m res_ty)
726 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727 * ExprStmt E any_ty: do { ....; E; ... }
729 Translation: E >> ...
731 * ResultStmt E: do { ....; E }
735 A list comprehensions of type [elt_ty]
736 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
737 * ExprStmt E Bool: [ .. | .... E ]
739 [ .. | .... | ..., E | ... ]
741 Translation: if E then fail else ...
743 * ResultStmt E: [ E | ... ]
745 Translation: return E
747 A guard list, guarding a RHS of type rhs_ty
748 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
749 * ExprStmt E Bool: f x | ..., E, ... = ...rhs...
751 Translation: if E then fail else ...
753 * ResultStmt E: f x | ...guards... = E
757 Array comprehensions are handled like list comprehensions -=chak
764 , RecStmt [a::forall a. a -> a, b]
766 [ BindStmt b (return x)
772 Here, the RecStmt binds a,b,c; but
773 - Only a,b are used in the stmts *following* the RecStmt,
774 This 'a' is *polymorphic'
775 - Only a,c are used in the stmts *inside* the RecStmt
776 *before* their bindings
777 This 'a' is monomorphic
779 Nota Bene: the two a's have different types, even though they
784 instance OutputableBndr id => Outputable (Stmt id) where
785 ppr stmt = pprStmt stmt
787 pprStmt (BindStmt pat expr) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
788 pprStmt (LetStmt binds) = hsep [ptext SLIT("let"), pprBinds binds]
789 pprStmt (ExprStmt expr _) = ppr expr
790 pprStmt (ResultStmt expr) = ppr expr
791 pprStmt (ParStmt stmtss) = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
792 pprStmt (RecStmt segment _ _ _) = ptext SLIT("rec") <+> braces (vcat (map ppr segment))
794 pprDo :: OutputableBndr id => HsStmtContext any -> [LStmt id] -> SDoc
795 pprDo DoExpr stmts = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
796 pprDo MDoExpr stmts = hang (ptext SLIT("mdo")) 3 (vcat (map ppr stmts))
797 pprDo ListComp stmts = pprComp brackets stmts
798 pprDo PArrComp stmts = pprComp pa_brackets stmts
800 pprComp :: OutputableBndr id => (SDoc -> SDoc) -> [LStmt id] -> SDoc
803 hang (ppr expr <+> char '|')
806 ResultStmt expr = unLoc (last stmts) -- Last stmt should
807 quals = init stmts -- be an ResultStmt
810 %************************************************************************
812 Template Haskell quotation brackets
814 %************************************************************************
817 data HsSplice id = HsSplice -- $z or $(f 4)
818 id -- The id is just a unique name to
819 (LHsExpr id) -- identify this splice point
821 instance OutputableBndr id => Outputable (HsSplice id) where
824 pprSplice :: OutputableBndr id => HsSplice id -> SDoc
825 pprSplice (HsSplice n e) = char '$' <> brackets (ppr n) <> pprParendExpr e
828 data HsBracket id = ExpBr (LHsExpr id) -- [| expr |]
829 | PatBr (LPat id) -- [p| pat |]
830 | DecBr (HsGroup id) -- [d| decls |]
831 | TypBr (LHsType id) -- [t| type |]
832 | VarBr id -- 'x, ''T
834 instance OutputableBndr id => Outputable (HsBracket id) where
838 pprHsBracket (ExpBr e) = thBrackets empty (ppr e)
839 pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p)
840 pprHsBracket (DecBr d) = thBrackets (char 'd') (ppr d)
841 pprHsBracket (TypBr t) = thBrackets (char 't') (ppr t)
842 pprHsBracket (VarBr n) = char '\'' <> ppr n
843 -- Infelicity: can't show ' vs '', because
844 -- we can't ask n what its OccName is, because the
845 -- pretty-printer for HsExpr doesn't ask for NamedThings
846 -- But the pretty-printer for names will show the OccName class
848 thBrackets pp_kind pp_body = char '[' <> pp_kind <> char '|' <+>
849 pp_body <+> ptext SLIT("|]")
852 %************************************************************************
854 \subsection{Enumerations and list comprehensions}
856 %************************************************************************
861 | FromThen (LHsExpr id)
863 | FromTo (LHsExpr id)
865 | FromThenTo (LHsExpr id)
871 instance OutputableBndr id => Outputable (ArithSeqInfo id) where
872 ppr (From e1) = hcat [ppr e1, pp_dotdot]
873 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
874 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
875 ppr (FromThenTo e1 e2 e3)
876 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
878 pp_dotdot = ptext SLIT(" .. ")
882 %************************************************************************
884 \subsection{HsMatchCtxt}
886 %************************************************************************
889 data HsMatchContext id -- Context of a Match
890 = FunRhs id -- Function binding for f
891 | CaseAlt -- Guard on a case alternative
892 | LambdaExpr -- Pattern of a lambda
893 | ProcExpr -- Pattern of a proc
894 | PatBindRhs -- Pattern binding
895 | RecUpd -- Record update [used only in DsExpr to tell matchWrapper
896 -- what sort of runtime error message to generate]
897 | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt or list comprehension
900 data HsStmtContext id
903 | MDoExpr -- Recursive do-expression
904 | PArrComp -- Parallel array comprehension
905 | PatGuard (HsMatchContext id) -- Pattern guard for specified thing
906 | ParStmtCtxt (HsStmtContext id) -- A branch of a parallel stmt
910 isDoExpr :: HsStmtContext id -> Bool
911 isDoExpr DoExpr = True
912 isDoExpr MDoExpr = True
913 isDoExpr other = False
917 matchSeparator (FunRhs _) = ptext SLIT("=")
918 matchSeparator CaseAlt = ptext SLIT("->")
919 matchSeparator LambdaExpr = ptext SLIT("->")
920 matchSeparator ProcExpr = ptext SLIT("->")
921 matchSeparator PatBindRhs = ptext SLIT("=")
922 matchSeparator (StmtCtxt _) = ptext SLIT("<-")
923 matchSeparator RecUpd = panic "unused"
927 pprMatchContext (FunRhs fun) = ptext SLIT("the definition of") <+> quotes (ppr fun)
928 pprMatchContext CaseAlt = ptext SLIT("a case alternative")
929 pprMatchContext RecUpd = ptext SLIT("a record-update construct")
930 pprMatchContext PatBindRhs = ptext SLIT("a pattern binding")
931 pprMatchContext LambdaExpr = ptext SLIT("a lambda abstraction")
932 pprMatchContext ProcExpr = ptext SLIT("an arrow abstraction")
933 pprMatchContext (StmtCtxt ctxt) = ptext SLIT("a pattern binding in") $$ pprStmtContext ctxt
935 pprMatchRhsContext (FunRhs fun) = ptext SLIT("a right-hand side of function") <+> quotes (ppr fun)
936 pprMatchRhsContext CaseAlt = ptext SLIT("the body of a case alternative")
937 pprMatchRhsContext PatBindRhs = ptext SLIT("the right-hand side of a pattern binding")
938 pprMatchRhsContext LambdaExpr = ptext SLIT("the body of a lambda")
939 pprMatchRhsContext ProcExpr = ptext SLIT("the body of a proc")
940 pprMatchRhsContext RecUpd = panic "pprMatchRhsContext"
942 pprStmtContext (ParStmtCtxt c) = sep [ptext SLIT("a parallel branch of"), pprStmtContext c]
943 pprStmtContext (PatGuard ctxt) = ptext SLIT("a pattern guard for") $$ pprMatchContext ctxt
944 pprStmtContext DoExpr = ptext SLIT("a 'do' expression")
945 pprStmtContext MDoExpr = ptext SLIT("an 'mdo' expression")
946 pprStmtContext ListComp = ptext SLIT("a list comprehension")
947 pprStmtContext PArrComp = ptext SLIT("an array comprehension")
949 -- Used for the result statement of comprehension
950 -- e.g. the 'e' in [ e | ... ]
951 -- or the 'r' in f x = r
952 pprStmtResultContext (PatGuard ctxt) = pprMatchRhsContext ctxt
953 pprStmtResultContext other = ptext SLIT("the result of") <+> pprStmtContext other
956 -- Used to generate the string for a *runtime* error message
957 matchContextErrString (FunRhs fun) = "function " ++ showSDoc (ppr fun)
958 matchContextErrString CaseAlt = "case"
959 matchContextErrString PatBindRhs = "pattern binding"
960 matchContextErrString RecUpd = "record update"
961 matchContextErrString LambdaExpr = "lambda"
962 matchContextErrString ProcExpr = "proc"
963 matchContextErrString (StmtCtxt (ParStmtCtxt c)) = matchContextErrString (StmtCtxt c)
964 matchContextErrString (StmtCtxt (PatGuard _)) = "pattern guard"
965 matchContextErrString (StmtCtxt DoExpr) = "'do' expression"
966 matchContextErrString (StmtCtxt MDoExpr) = "'mdo' expression"
967 matchContextErrString (StmtCtxt ListComp) = "list comprehension"
968 matchContextErrString (StmtCtxt PArrComp) = "array comprehension"