Add typed holes support in Template Haskell.
[ghc.git] / compiler / hsSyn / HsExpr.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 -}
5
6 {-# LANGUAGE CPP, DeriveDataTypeable, ScopedTypeVariables #-}
7 {-# LANGUAGE StandaloneDeriving #-}
8 {-# LANGUAGE FlexibleContexts #-}
9 {-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
10 -- in module PlaceHolder
11 {-# LANGUAGE ConstraintKinds #-}
12
13 -- | Abstract Haskell syntax for expressions.
14 module HsExpr where
15
16 #include "HsVersions.h"
17
18 -- friends:
19 import HsDecls
20 import HsPat
21 import HsLit
22 import PlaceHolder ( PostTc,PostRn,DataId )
23 import HsTypes
24 import HsBinds
25
26 -- others:
27 import TcEvidence
28 import CoreSyn
29 import Var
30 import Name
31 import BasicTypes
32 import DataCon
33 import SrcLoc
34 import Util
35 import StaticFlags( opt_PprStyle_Debug )
36 import Outputable
37 import FastString
38 import Type
39
40 -- libraries:
41 import Data.Data hiding (Fixity)
42 import Data.Maybe (isNothing)
43
44 {-
45 ************************************************************************
46 * *
47 \subsection{Expressions proper}
48 * *
49 ************************************************************************
50 -}
51
52 -- * Expressions proper
53
54 type LHsExpr id = Located (HsExpr id)
55 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
56 -- in a list
57
58 -- For details on above see note [Api annotations] in ApiAnnotation
59
60 -------------------------
61 -- | PostTcExpr is an evidence expression attached to the syntax tree by the
62 -- type checker (c.f. postTcType).
63 type PostTcExpr = HsExpr Id
64 -- | We use a PostTcTable where there are a bunch of pieces of evidence, more
65 -- than is convenient to keep individually.
66 type PostTcTable = [(Name, PostTcExpr)]
67
68 noPostTcExpr :: PostTcExpr
69 noPostTcExpr = HsLit (HsString "" (fsLit "noPostTcExpr"))
70
71 noPostTcTable :: PostTcTable
72 noPostTcTable = []
73
74 -------------------------
75 -- | SyntaxExpr is like 'PostTcExpr', but it's filled in a little earlier,
76 -- by the renamer. It's used for rebindable syntax.
77 --
78 -- E.g. @(>>=)@ is filled in before the renamer by the appropriate 'Name' for
79 -- @(>>=)@, and then instantiated by the type checker with its type args
80 -- etc
81
82 type SyntaxExpr id = HsExpr id
83
84 noSyntaxExpr :: SyntaxExpr id -- Before renaming, and sometimes after,
85 -- (if the syntax slot makes no sense)
86 noSyntaxExpr = HsLit (HsString "" (fsLit "noSyntaxExpr"))
87
88
89 type CmdSyntaxTable id = [(Name, SyntaxExpr id)]
90 -- See Note [CmdSyntaxTable]
91
92 {-
93 Note [CmdSyntaxtable]
94 ~~~~~~~~~~~~~~~~~~~~~
95 Used only for arrow-syntax stuff (HsCmdTop), the CmdSyntaxTable keeps
96 track of the methods needed for a Cmd.
97
98 * Before the renamer, this list is an empty list
99
100 * After the renamer, it takes the form @[(std_name, HsVar actual_name)]@
101 For example, for the 'arr' method
102 * normal case: (GHC.Control.Arrow.arr, HsVar GHC.Control.Arrow.arr)
103 * with rebindable syntax: (GHC.Control.Arrow.arr, arr_22)
104 where @arr_22@ is whatever 'arr' is in scope
105
106 * After the type checker, it takes the form [(std_name, <expression>)]
107 where <expression> is the evidence for the method. This evidence is
108 instantiated with the class, but is still polymorphic in everything
109 else. For example, in the case of 'arr', the evidence has type
110 forall b c. (b->c) -> a b c
111 where 'a' is the ambient type of the arrow. This polymorphism is
112 important because the desugarer uses the same evidence at multiple
113 different types.
114
115 This is Less Cool than what we normally do for rebindable syntax, which is to
116 make fully-instantiated piece of evidence at every use site. The Cmd way
117 is Less Cool because
118 * The renamer has to predict which methods are needed.
119 See the tedious RnExpr.methodNamesCmd.
120
121 * The desugarer has to know the polymorphic type of the instantiated
122 method. This is checked by Inst.tcSyntaxName, but is less flexible
123 than the rest of rebindable syntax, where the type is less
124 pre-ordained. (And this flexibility is useful; for example we can
125 typecheck do-notation with (>>=) :: m1 a -> (a -> m2 b) -> m2 b.)
126 -}
127
128 -- | A Haskell expression.
129 data HsExpr id
130 = HsVar id -- ^ Variable
131
132 | HsUnboundVar OccName -- ^ Unbound variable; also used for "holes" _, or _x.
133 -- Turned from HsVar to HsUnboundVar by the renamer, when
134 -- it finds an out-of-scope variable
135 -- Turned into HsVar by type checker, to support deferred
136 -- type errors. (The HsUnboundVar only has an OccName.)
137
138 | HsSingleRecFld (FieldOcc id) -- ^ Variable that corresponds to a record selector
139
140 | HsIPVar HsIPName -- ^ Implicit parameter
141 | HsOverLit (HsOverLit id) -- ^ Overloaded literals
142
143 | HsLit HsLit -- ^ Simple (non-overloaded) literals
144
145 | HsLam (MatchGroup id (LHsExpr id)) -- ^ Lambda abstraction. Currently always a single match
146 --
147 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
148 -- 'ApiAnnotation.AnnRarrow',
149
150 -- For details on above see note [Api annotations] in ApiAnnotation
151
152 | HsLamCase (PostTc id Type) (MatchGroup id (LHsExpr id)) -- ^ Lambda-case
153 --
154 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
155 -- 'ApiAnnotation.AnnCase','ApiAnnotation.AnnOpen',
156 -- 'ApiAnnotation.AnnClose'
157
158 -- For details on above see note [Api annotations] in ApiAnnotation
159
160 | HsApp (LHsExpr id) (LHsExpr id) -- ^ Application
161
162 -- | Operator applications:
163 -- NB Bracketed ops such as (+) come out as Vars.
164
165 -- NB We need an expr for the operator in an OpApp/Section since
166 -- the typechecker may need to apply the operator to a few types.
167
168 | OpApp (LHsExpr id) -- left operand
169 (LHsExpr id) -- operator
170 (PostRn id Fixity) -- Renamer adds fixity; bottom until then
171 (LHsExpr id) -- right operand
172
173 -- | Negation operator. Contains the negated expression and the name
174 -- of 'negate'
175 --
176 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnMinus'
177
178 -- For details on above see note [Api annotations] in ApiAnnotation
179 | NegApp (LHsExpr id)
180 (SyntaxExpr id)
181
182 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
183 -- 'ApiAnnotation.AnnClose' @')'@
184
185 -- For details on above see note [Api annotations] in ApiAnnotation
186 | HsPar (LHsExpr id) -- ^ Parenthesised expr; see Note [Parens in HsSyn]
187
188 | SectionL (LHsExpr id) -- operand; see Note [Sections in HsSyn]
189 (LHsExpr id) -- operator
190 | SectionR (LHsExpr id) -- operator; see Note [Sections in HsSyn]
191 (LHsExpr id) -- operand
192
193 -- | Used for explicit tuples and sections thereof
194 --
195 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
196 -- 'ApiAnnotation.AnnClose'
197
198 -- For details on above see note [Api annotations] in ApiAnnotation
199 | ExplicitTuple
200 [LHsTupArg id]
201 Boxity
202
203 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnCase',
204 -- 'ApiAnnotation.AnnOf','ApiAnnotation.AnnOpen' @'{'@,
205 -- 'ApiAnnotation.AnnClose' @'}'@
206
207 -- For details on above see note [Api annotations] in ApiAnnotation
208 | HsCase (LHsExpr id)
209 (MatchGroup id (LHsExpr id))
210
211 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf',
212 -- 'ApiAnnotation.AnnSemi',
213 -- 'ApiAnnotation.AnnThen','ApiAnnotation.AnnSemi',
214 -- 'ApiAnnotation.AnnElse',
215
216 -- For details on above see note [Api annotations] in ApiAnnotation
217 | HsIf (Maybe (SyntaxExpr id)) -- cond function
218 -- Nothing => use the built-in 'if'
219 -- See Note [Rebindable if]
220 (LHsExpr id) -- predicate
221 (LHsExpr id) -- then part
222 (LHsExpr id) -- else part
223
224 -- | Multi-way if
225 --
226 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf'
227 -- 'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
228
229 -- For details on above see note [Api annotations] in ApiAnnotation
230 | HsMultiIf (PostTc id Type) [LGRHS id (LHsExpr id)]
231
232 -- | let(rec)
233 --
234 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLet',
235 -- 'ApiAnnotation.AnnOpen' @'{'@,
236 -- 'ApiAnnotation.AnnClose' @'}'@,'ApiAnnotation.AnnIn'
237
238 -- For details on above see note [Api annotations] in ApiAnnotation
239 | HsLet (HsLocalBinds id)
240 (LHsExpr id)
241
242 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDo',
243 -- 'ApiAnnotation.AnnOpen', 'ApiAnnotation.AnnSemi',
244 -- 'ApiAnnotation.AnnVbar',
245 -- 'ApiAnnotation.AnnClose'
246
247 -- For details on above see note [Api annotations] in ApiAnnotation
248 | HsDo (HsStmtContext Name) -- The parameterisation is unimportant
249 -- because in this context we never use
250 -- the PatGuard or ParStmt variant
251 [ExprLStmt id] -- "do":one or more stmts
252 (PostTc id Type) -- Type of the whole expression
253
254 -- | Syntactic list: [a,b,c,...]
255 --
256 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
257 -- 'ApiAnnotation.AnnClose' @']'@
258
259 -- For details on above see note [Api annotations] in ApiAnnotation
260 | ExplicitList
261 (PostTc id Type) -- Gives type of components of list
262 (Maybe (SyntaxExpr id)) -- For OverloadedLists, the fromListN witness
263 [LHsExpr id]
264
265 -- | Syntactic parallel array: [:e1, ..., en:]
266 --
267 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
268 -- 'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnComma',
269 -- 'ApiAnnotation.AnnVbar'
270 -- 'ApiAnnotation.AnnClose' @':]'@
271
272 -- For details on above see note [Api annotations] in ApiAnnotation
273 | ExplicitPArr
274 (PostTc id Type) -- type of elements of the parallel array
275 [LHsExpr id]
276
277 -- | Record construction
278 --
279 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
280 -- 'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
281
282 -- For details on above see note [Api annotations] in ApiAnnotation
283 | RecordCon (Located id) -- The constructor. After type checking
284 -- it's the dataConWrapId of the constructor
285 PostTcExpr -- Data con Id applied to type args
286 (HsRecordBinds id)
287
288 -- | Record update
289 --
290 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
291 -- 'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
292
293 -- For details on above see note [Api annotations] in ApiAnnotation
294 | RecordUpd (LHsExpr id)
295 [LHsRecUpdField id]
296 -- (HsMatchGroup Id) -- Filled in by the type checker to be
297 -- -- a match that does the job
298 (PostTc id [DataCon])
299 -- Filled in by the type checker to the
300 -- _non-empty_ list of DataCons that have
301 -- all the upd'd fields
302 (PostTc id [Type]) -- Argument types of *input* record type
303 (PostTc id [Type]) -- and *output* record type
304 -- For a type family, the arg types are of the *instance* tycon,
305 -- not the family tycon
306
307 -- | Expression with an explicit type signature. @e :: type@
308 --
309 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
310
311 -- For details on above see note [Api annotations] in ApiAnnotation
312 | ExprWithTySig
313 (LHsExpr id)
314 (LHsType id)
315 (PostRn id [Name]) -- After renaming, the list of Names
316 -- contains the named and unnamed
317 -- wildcards brought in scope by the
318 -- signature
319
320 | ExprWithTySigOut -- TRANSLATION
321 (LHsExpr id)
322 (LHsType Name) -- Retain the signature for
323 -- round-tripping purposes
324
325 -- | Arithmetic sequence
326 --
327 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
328 -- 'ApiAnnotation.AnnComma','ApiAnnotation.AnnDotdot',
329 -- 'ApiAnnotation.AnnClose' @']'@
330
331 -- For details on above see note [Api annotations] in ApiAnnotation
332 | ArithSeq
333 PostTcExpr
334 (Maybe (SyntaxExpr id)) -- For OverloadedLists, the fromList witness
335 (ArithSeqInfo id)
336
337 -- | Arithmetic sequence for parallel array
338 --
339 -- > [:e1..e2:] or [:e1, e2..e3:]
340 --
341 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
342 -- 'ApiAnnotation.AnnComma','ApiAnnotation.AnnDotdot',
343 -- 'ApiAnnotation.AnnVbar',
344 -- 'ApiAnnotation.AnnClose' @':]'@
345
346 -- For details on above see note [Api annotations] in ApiAnnotation
347 | PArrSeq
348 PostTcExpr
349 (ArithSeqInfo id)
350
351 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{-\# SCC'@,
352 -- 'ApiAnnotation.AnnVal' or 'ApiAnnotation.AnnValStr',
353 -- 'ApiAnnotation.AnnClose' @'\#-}'@
354
355 -- For details on above see note [Api annotations] in ApiAnnotation
356 | HsSCC SourceText -- Note [Pragma source text] in BasicTypes
357 StringLiteral -- "set cost centre" SCC pragma
358 (LHsExpr id) -- expr whose cost is to be measured
359
360 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{-\# CORE'@,
361 -- 'ApiAnnotation.AnnVal', 'ApiAnnotation.AnnClose' @'\#-}'@
362
363 -- For details on above see note [Api annotations] in ApiAnnotation
364 | HsCoreAnn SourceText -- Note [Pragma source text] in BasicTypes
365 StringLiteral -- hdaume: core annotation
366 (LHsExpr id)
367
368 -----------------------------------------------------------
369 -- MetaHaskell Extensions
370
371 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
372 -- 'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
373 -- 'ApiAnnotation.AnnClose'
374
375 -- For details on above see note [Api annotations] in ApiAnnotation
376 | HsBracket (HsBracket id)
377
378 -- See Note [Pending Splices]
379 | HsRnBracketOut
380 (HsBracket Name) -- Output of the renamer is the *original* renamed
381 -- expression, plus
382 [PendingRnSplice] -- _renamed_ splices to be type checked
383
384 | HsTcBracketOut
385 (HsBracket Name) -- Output of the type checker is the *original*
386 -- renamed expression, plus
387 [PendingTcSplice] -- _typechecked_ splices to be
388 -- pasted back in by the desugarer
389
390 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
391 -- 'ApiAnnotation.AnnClose'
392
393 -- For details on above see note [Api annotations] in ApiAnnotation
394 | HsSpliceE (HsSplice id)
395
396 -----------------------------------------------------------
397 -- Arrow notation extension
398
399 -- | @proc@ notation for Arrows
400 --
401 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnProc',
402 -- 'ApiAnnotation.AnnRarrow'
403
404 -- For details on above see note [Api annotations] in ApiAnnotation
405 | HsProc (LPat id) -- arrow abstraction, proc
406 (LHsCmdTop id) -- body of the abstraction
407 -- always has an empty stack
408
409 ---------------------------------------
410 -- static pointers extension
411 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnStatic',
412
413 -- For details on above see note [Api annotations] in ApiAnnotation
414 | HsStatic (LHsExpr id)
415
416 ---------------------------------------
417 -- The following are commands, not expressions proper
418 -- They are only used in the parsing stage and are removed
419 -- immediately in parser.RdrHsSyn.checkCommand
420
421 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.Annlarrowtail',
422 -- 'ApiAnnotation.Annrarrowtail','ApiAnnotation.AnnLarrowtail',
423 -- 'ApiAnnotation.AnnRarrowtail'
424
425 -- For details on above see note [Api annotations] in ApiAnnotation
426 | HsArrApp -- Arrow tail, or arrow application (f -< arg)
427 (LHsExpr id) -- arrow expression, f
428 (LHsExpr id) -- input expression, arg
429 (PostTc id Type) -- type of the arrow expressions f,
430 -- of the form a t t', where arg :: t
431 HsArrAppType -- higher-order (-<<) or first-order (-<)
432 Bool -- True => right-to-left (f -< arg)
433 -- False => left-to-right (arg >- f)
434
435 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(|'@,
436 -- 'ApiAnnotation.AnnClose' @'|)'@
437
438 -- For details on above see note [Api annotations] in ApiAnnotation
439 | HsArrForm -- Command formation, (| e cmd1 .. cmdn |)
440 (LHsExpr id) -- the operator
441 -- after type-checking, a type abstraction to be
442 -- applied to the type of the local environment tuple
443 (Maybe Fixity) -- fixity (filled in by the renamer), for forms that
444 -- were converted from OpApp's by the renamer
445 [LHsCmdTop id] -- argument commands
446
447 ---------------------------------------
448 -- Haskell program coverage (Hpc) Support
449
450 | HsTick
451 (Tickish id)
452 (LHsExpr id) -- sub-expression
453
454 | HsBinTick
455 Int -- module-local tick number for True
456 Int -- module-local tick number for False
457 (LHsExpr id) -- sub-expression
458
459 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
460 -- 'ApiAnnotation.AnnOpen' @'{-\# GENERATED'@,
461 -- 'ApiAnnotation.AnnVal','ApiAnnotation.AnnVal',
462 -- 'ApiAnnotation.AnnColon','ApiAnnotation.AnnVal',
463 -- 'ApiAnnotation.AnnMinus',
464 -- 'ApiAnnotation.AnnVal','ApiAnnotation.AnnColon',
465 -- 'ApiAnnotation.AnnVal',
466 -- 'ApiAnnotation.AnnClose' @'\#-}'@
467
468 -- For details on above see note [Api annotations] in ApiAnnotation
469 | HsTickPragma -- A pragma introduced tick
470 SourceText -- Note [Pragma source text] in BasicTypes
471 (StringLiteral,(Int,Int),(Int,Int))
472 -- external span for this tick
473 (LHsExpr id)
474
475 ---------------------------------------
476 -- These constructors only appear temporarily in the parser.
477 -- The renamer translates them into the Right Thing.
478
479 | EWildPat -- wildcard
480
481 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnAt'
482
483 -- For details on above see note [Api annotations] in ApiAnnotation
484 | EAsPat (Located id) -- as pattern
485 (LHsExpr id)
486
487 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow'
488
489 -- For details on above see note [Api annotations] in ApiAnnotation
490 | EViewPat (LHsExpr id) -- view pattern
491 (LHsExpr id)
492
493 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
494
495 -- For details on above see note [Api annotations] in ApiAnnotation
496 | ELazyPat (LHsExpr id) -- ~ pattern
497
498 | HsType (LHsType id) -- Explicit type argument; e.g f {| Int |} x y
499
500 ---------------------------------------
501 -- Finally, HsWrap appears only in typechecker output
502
503 | HsWrap HsWrapper -- TRANSLATION
504 (HsExpr id)
505
506 deriving (Typeable)
507 deriving instance (DataId id) => Data (HsExpr id)
508
509 -- | HsTupArg is used for tuple sections
510 -- (,a,) is represented by ExplicitTuple [Missing ty1, Present a, Missing ty3]
511 -- Which in turn stands for (\x:ty1 \y:ty2. (x,a,y))
512 type LHsTupArg id = Located (HsTupArg id)
513 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma'
514
515 -- For details on above see note [Api annotations] in ApiAnnotation
516 data HsTupArg id
517 = Present (LHsExpr id) -- ^ The argument
518 | Missing (PostTc id Type) -- ^ The argument is missing, but this is its type
519 deriving (Typeable)
520 deriving instance (DataId id) => Data (HsTupArg id)
521
522 tupArgPresent :: LHsTupArg id -> Bool
523 tupArgPresent (L _ (Present {})) = True
524 tupArgPresent (L _ (Missing {})) = False
525
526 {-
527 Note [Parens in HsSyn]
528 ~~~~~~~~~~~~~~~~~~~~~~
529 HsPar (and ParPat in patterns, HsParTy in types) is used as follows
530
531 * Generally HsPar is optional; the pretty printer adds parens where
532 necessary. Eg (HsApp f (HsApp g x)) is fine, and prints 'f (g x)'
533
534 * HsPars are pretty printed as '( .. )' regardless of whether
535 or not they are strictly necssary
536
537 * HsPars are respected when rearranging operator fixities.
538 So a * (b + c) means what it says (where the parens are an HsPar)
539
540 Note [Sections in HsSyn]
541 ~~~~~~~~~~~~~~~~~~~~~~~~
542 Sections should always appear wrapped in an HsPar, thus
543 HsPar (SectionR ...)
544 The parser parses sections in a wider variety of situations
545 (See Note [Parsing sections]), but the renamer checks for those
546 parens. This invariant makes pretty-printing easier; we don't need
547 a special case for adding the parens round sections.
548
549 Note [Rebindable if]
550 ~~~~~~~~~~~~~~~~~~~~
551 The rebindable syntax for 'if' is a bit special, because when
552 rebindable syntax is *off* we do not want to treat
553 (if c then t else e)
554 as if it was an application (ifThenElse c t e). Why not?
555 Because we allow an 'if' to return *unboxed* results, thus
556 if blah then 3# else 4#
557 whereas that would not be possible using a all to a polymorphic function
558 (because you can't call a polymorphic function at an unboxed type).
559
560 So we use Nothing to mean "use the old built-in typing rule".
561 -}
562
563 instance OutputableBndr id => Outputable (HsExpr id) where
564 ppr expr = pprExpr expr
565
566 -----------------------
567 -- pprExpr, pprLExpr, pprBinds call pprDeeper;
568 -- the underscore versions do not
569 pprLExpr :: OutputableBndr id => LHsExpr id -> SDoc
570 pprLExpr (L _ e) = pprExpr e
571
572 pprExpr :: OutputableBndr id => HsExpr id -> SDoc
573 pprExpr e | isAtomicHsExpr e || isQuietHsExpr e = ppr_expr e
574 | otherwise = pprDeeper (ppr_expr e)
575
576 isQuietHsExpr :: HsExpr id -> Bool
577 -- Parentheses do display something, but it gives little info and
578 -- if we go deeper when we go inside them then we get ugly things
579 -- like (...)
580 isQuietHsExpr (HsPar _) = True
581 -- applications don't display anything themselves
582 isQuietHsExpr (HsApp _ _) = True
583 isQuietHsExpr (OpApp _ _ _ _) = True
584 isQuietHsExpr _ = False
585
586 pprBinds :: (OutputableBndr idL, OutputableBndr idR)
587 => HsLocalBindsLR idL idR -> SDoc
588 pprBinds b = pprDeeper (ppr b)
589
590 -----------------------
591 ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
592 ppr_lexpr e = ppr_expr (unLoc e)
593
594 ppr_expr :: forall id. OutputableBndr id => HsExpr id -> SDoc
595 ppr_expr (HsVar v) = pprPrefixOcc v
596 ppr_expr (HsUnboundVar v) = pprPrefixOcc v
597 ppr_expr (HsIPVar v) = ppr v
598 ppr_expr (HsLit lit) = ppr lit
599 ppr_expr (HsOverLit lit) = ppr lit
600 ppr_expr (HsPar e) = parens (ppr_lexpr e)
601
602 ppr_expr (HsCoreAnn _ (StringLiteral _ s) e)
603 = vcat [ptext (sLit "HsCoreAnn") <+> ftext s, ppr_lexpr e]
604
605 ppr_expr (HsApp e1 e2)
606 = let (fun, args) = collect_args e1 [e2] in
607 hang (ppr_lexpr fun) 2 (sep (map pprParendExpr args))
608 where
609 collect_args (L _ (HsApp fun arg)) args = collect_args fun (arg:args)
610 collect_args fun args = (fun, args)
611
612 ppr_expr (OpApp e1 op _ e2)
613 = case unLoc op of
614 HsVar v -> pp_infixly v
615 _ -> pp_prefixly
616 where
617 pp_e1 = pprDebugParendExpr e1 -- In debug mode, add parens
618 pp_e2 = pprDebugParendExpr e2 -- to make precedence clear
619
620 pp_prefixly
621 = hang (ppr op) 2 (sep [pp_e1, pp_e2])
622
623 pp_infixly v
624 = sep [pp_e1, sep [pprInfixOcc v, nest 2 pp_e2]]
625
626 ppr_expr (NegApp e _) = char '-' <+> pprDebugParendExpr e
627
628 ppr_expr (SectionL expr op)
629 = case unLoc op of
630 HsVar v -> pp_infixly v
631 _ -> pp_prefixly
632 where
633 pp_expr = pprDebugParendExpr expr
634
635 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
636 4 (hsep [pp_expr, ptext (sLit "x_ )")])
637 pp_infixly v = (sep [pp_expr, pprInfixOcc v])
638
639 ppr_expr (SectionR op expr)
640 = case unLoc op of
641 HsVar v -> pp_infixly v
642 _ -> pp_prefixly
643 where
644 pp_expr = pprDebugParendExpr expr
645
646 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext (sLit "x_")])
647 4 (pp_expr <> rparen)
648 pp_infixly v = sep [pprInfixOcc v, pp_expr]
649
650 ppr_expr (ExplicitTuple exprs boxity)
651 = tupleParens (boxityTupleSort boxity) (fcat (ppr_tup_args $ map unLoc exprs))
652 where
653 ppr_tup_args [] = []
654 ppr_tup_args (Present e : es) = (ppr_lexpr e <> punc es) : ppr_tup_args es
655 ppr_tup_args (Missing _ : es) = punc es : ppr_tup_args es
656
657 punc (Present {} : _) = comma <> space
658 punc (Missing {} : _) = comma
659 punc [] = empty
660
661 ppr_expr (HsLam matches)
662 = pprMatches (LambdaExpr :: HsMatchContext id) matches
663
664 ppr_expr (HsLamCase _ matches)
665 = sep [ sep [ptext (sLit "\\case {")],
666 nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ]
667
668 ppr_expr (HsCase expr matches)
669 = sep [ sep [ptext (sLit "case"), nest 4 (ppr expr), ptext (sLit "of {")],
670 nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ]
671
672 ppr_expr (HsIf _ e1 e2 e3)
673 = sep [hsep [ptext (sLit "if"), nest 2 (ppr e1), ptext (sLit "then")],
674 nest 4 (ppr e2),
675 ptext (sLit "else"),
676 nest 4 (ppr e3)]
677
678 ppr_expr (HsMultiIf _ alts)
679 = sep $ ptext (sLit "if") : map ppr_alt alts
680 where ppr_alt (L _ (GRHS guards expr)) =
681 sep [ char '|' <+> interpp'SP guards
682 , ptext (sLit "->") <+> pprDeeper (ppr expr) ]
683
684 -- special case: let ... in let ...
685 ppr_expr (HsLet binds expr@(L _ (HsLet _ _)))
686 = sep [hang (ptext (sLit "let")) 2 (hsep [pprBinds binds, ptext (sLit "in")]),
687 ppr_lexpr expr]
688
689 ppr_expr (HsLet binds expr)
690 = sep [hang (ptext (sLit "let")) 2 (pprBinds binds),
691 hang (ptext (sLit "in")) 2 (ppr expr)]
692
693 ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts
694
695 ppr_expr (ExplicitList _ _ exprs)
696 = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
697
698 ppr_expr (ExplicitPArr _ exprs)
699 = paBrackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
700
701 ppr_expr (RecordCon con_id _ rbinds)
702 = hang (ppr con_id) 2 (ppr rbinds)
703
704 ppr_expr (RecordUpd aexp rbinds _ _ _)
705 = hang (pprLExpr aexp) 2 (braces (fsep (punctuate comma (map ppr rbinds))))
706
707 ppr_expr (ExprWithTySig expr sig _)
708 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
709 4 (ppr sig)
710 ppr_expr (ExprWithTySigOut expr sig)
711 = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
712 4 (ppr sig)
713
714 ppr_expr (ArithSeq _ _ info) = brackets (ppr info)
715 ppr_expr (PArrSeq _ info) = paBrackets (ppr info)
716
717 ppr_expr EWildPat = char '_'
718 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
719 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
720 ppr_expr (EViewPat p e) = ppr p <+> ptext (sLit "->") <+> ppr e
721
722 ppr_expr (HsSCC _ (StringLiteral _ lbl) expr)
723 = sep [ ptext (sLit "{-# SCC") <+> doubleQuotes (ftext lbl) <+> ptext (sLit "#-}"),
724 pprParendExpr expr ]
725
726 ppr_expr (HsWrap co_fn e) = pprHsWrapper (pprExpr e) co_fn
727 ppr_expr (HsType id) = ppr id
728
729 ppr_expr (HsSpliceE s) = pprSplice s
730 ppr_expr (HsBracket b) = pprHsBracket b
731 ppr_expr (HsRnBracketOut e []) = ppr e
732 ppr_expr (HsRnBracketOut e ps) = ppr e $$ ptext (sLit "pending(rn)") <+> ppr ps
733 ppr_expr (HsTcBracketOut e []) = ppr e
734 ppr_expr (HsTcBracketOut e ps) = ppr e $$ ptext (sLit "pending(tc)") <+> ppr ps
735
736 ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
737 = hsep [ptext (sLit "proc"), ppr pat, ptext (sLit "->"), ppr cmd]
738
739 ppr_expr (HsStatic e)
740 = hsep [ptext (sLit "static"), pprParendExpr e]
741
742 ppr_expr (HsTick tickish exp)
743 = pprTicks (ppr exp) $
744 ppr tickish <+> ppr_lexpr exp
745 ppr_expr (HsBinTick tickIdTrue tickIdFalse exp)
746 = pprTicks (ppr exp) $
747 hcat [ptext (sLit "bintick<"),
748 ppr tickIdTrue,
749 ptext (sLit ","),
750 ppr tickIdFalse,
751 ptext (sLit ">("),
752 ppr exp,ptext (sLit ")")]
753 ppr_expr (HsTickPragma _ externalSrcLoc exp)
754 = pprTicks (ppr exp) $
755 hcat [ptext (sLit "tickpragma<"),
756 pprExternalSrcLoc externalSrcLoc,
757 ptext (sLit ">("),
758 ppr exp,
759 ptext (sLit ")")]
760
761 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True)
762 = hsep [ppr_lexpr arrow, larrowt, ppr_lexpr arg]
763 ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False)
764 = hsep [ppr_lexpr arg, arrowt, ppr_lexpr arrow]
765 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp True)
766 = hsep [ppr_lexpr arrow, larrowtt, ppr_lexpr arg]
767 ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp False)
768 = hsep [ppr_lexpr arg, arrowtt, ppr_lexpr arrow]
769
770 ppr_expr (HsArrForm (L _ (HsVar v)) (Just _) [arg1, arg2])
771 = sep [pprCmdArg (unLoc arg1), hsep [pprInfixOcc v, pprCmdArg (unLoc arg2)]]
772 ppr_expr (HsArrForm op _ args)
773 = hang (ptext (sLit "(|") <+> ppr_lexpr op)
774 4 (sep (map (pprCmdArg.unLoc) args) <+> ptext (sLit "|)"))
775 ppr_expr (HsSingleRecFld f) = ppr f
776
777 pprExternalSrcLoc :: (StringLiteral,(Int,Int),(Int,Int)) -> SDoc
778 pprExternalSrcLoc (StringLiteral _ src,(n1,n2),(n3,n4))
779 = ppr (src,(n1,n2),(n3,n4))
780
781 {-
782 HsSyn records exactly where the user put parens, with HsPar.
783 So generally speaking we print without adding any parens.
784 However, some code is internally generated, and in some places
785 parens are absolutely required; so for these places we use
786 pprParendExpr (but don't print double parens of course).
787
788 For operator applications we don't add parens, because the operator
789 fixities should do the job, except in debug mode (-dppr-debug) so we
790 can see the structure of the parse tree.
791 -}
792
793 pprDebugParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
794 pprDebugParendExpr expr
795 = getPprStyle (\sty ->
796 if debugStyle sty then pprParendExpr expr
797 else pprLExpr expr)
798
799 pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
800 pprParendExpr expr
801 | hsExprNeedsParens (unLoc expr) = parens (pprLExpr expr)
802 | otherwise = pprLExpr expr
803 -- Using pprLExpr makes sure that we go 'deeper'
804 -- I think that is usually (always?) right
805
806 hsExprNeedsParens :: HsExpr id -> Bool
807 -- True of expressions for which '(e)' and 'e'
808 -- mean the same thing
809 hsExprNeedsParens (ArithSeq {}) = False
810 hsExprNeedsParens (PArrSeq {}) = False
811 hsExprNeedsParens (HsLit {}) = False
812 hsExprNeedsParens (HsOverLit {}) = False
813 hsExprNeedsParens (HsVar {}) = False
814 hsExprNeedsParens (HsUnboundVar {}) = False
815 hsExprNeedsParens (HsIPVar {}) = False
816 hsExprNeedsParens (ExplicitTuple {}) = False
817 hsExprNeedsParens (ExplicitList {}) = False
818 hsExprNeedsParens (ExplicitPArr {}) = False
819 hsExprNeedsParens (RecordCon {}) = False
820 hsExprNeedsParens (RecordUpd {}) = False
821 hsExprNeedsParens (HsPar {}) = False
822 hsExprNeedsParens (HsBracket {}) = False
823 hsExprNeedsParens (HsRnBracketOut {}) = False
824 hsExprNeedsParens (HsTcBracketOut {}) = False
825 hsExprNeedsParens (HsDo sc _ _)
826 | isListCompExpr sc = False
827 hsExprNeedsParens (HsSingleRecFld{}) = False
828 hsExprNeedsParens _ = True
829
830
831 isAtomicHsExpr :: HsExpr id -> Bool
832 -- True of a single token
833 isAtomicHsExpr (HsVar {}) = True
834 isAtomicHsExpr (HsLit {}) = True
835 isAtomicHsExpr (HsOverLit {}) = True
836 isAtomicHsExpr (HsIPVar {}) = True
837 isAtomicHsExpr (HsUnboundVar {}) = True
838 isAtomicHsExpr (HsWrap _ e) = isAtomicHsExpr e
839 isAtomicHsExpr (HsPar e) = isAtomicHsExpr (unLoc e)
840 isAtomicHsExpr (HsSingleRecFld{}) = True
841 isAtomicHsExpr _ = False
842
843 {-
844 ************************************************************************
845 * *
846 \subsection{Commands (in arrow abstractions)}
847 * *
848 ************************************************************************
849
850 We re-use HsExpr to represent these.
851 -}
852
853 type LHsCmd id = Located (HsCmd id)
854
855 data HsCmd id
856 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.Annlarrowtail',
857 -- 'ApiAnnotation.Annrarrowtail','ApiAnnotation.AnnLarrowtail',
858 -- 'ApiAnnotation.AnnRarrowtail'
859
860 -- For details on above see note [Api annotations] in ApiAnnotation
861 = HsCmdArrApp -- Arrow tail, or arrow application (f -< arg)
862 (LHsExpr id) -- arrow expression, f
863 (LHsExpr id) -- input expression, arg
864 (PostTc id Type) -- type of the arrow expressions f,
865 -- of the form a t t', where arg :: t
866 HsArrAppType -- higher-order (-<<) or first-order (-<)
867 Bool -- True => right-to-left (f -< arg)
868 -- False => left-to-right (arg >- f)
869
870 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(|'@,
871 -- 'ApiAnnotation.AnnClose' @'|)'@
872
873 -- For details on above see note [Api annotations] in ApiAnnotation
874 | HsCmdArrForm -- Command formation, (| e cmd1 .. cmdn |)
875 (LHsExpr id) -- the operator
876 -- after type-checking, a type abstraction to be
877 -- applied to the type of the local environment tuple
878 (Maybe Fixity) -- fixity (filled in by the renamer), for forms that
879 -- were converted from OpApp's by the renamer
880 [LHsCmdTop id] -- argument commands
881
882 | HsCmdApp (LHsCmd id)
883 (LHsExpr id)
884
885 | HsCmdLam (MatchGroup id (LHsCmd id)) -- kappa
886 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
887 -- 'ApiAnnotation.AnnRarrow',
888
889 -- For details on above see note [Api annotations] in ApiAnnotation
890
891 | HsCmdPar (LHsCmd id) -- parenthesised command
892 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
893 -- 'ApiAnnotation.AnnClose' @')'@
894
895 -- For details on above see note [Api annotations] in ApiAnnotation
896
897 | HsCmdCase (LHsExpr id)
898 (MatchGroup id (LHsCmd id)) -- bodies are HsCmd's
899 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnCase',
900 -- 'ApiAnnotation.AnnOf','ApiAnnotation.AnnOpen' @'{'@,
901 -- 'ApiAnnotation.AnnClose' @'}'@
902
903 -- For details on above see note [Api annotations] in ApiAnnotation
904
905 | HsCmdIf (Maybe (SyntaxExpr id)) -- cond function
906 (LHsExpr id) -- predicate
907 (LHsCmd id) -- then part
908 (LHsCmd id) -- else part
909 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf',
910 -- 'ApiAnnotation.AnnSemi',
911 -- 'ApiAnnotation.AnnThen','ApiAnnotation.AnnSemi',
912 -- 'ApiAnnotation.AnnElse',
913
914 -- For details on above see note [Api annotations] in ApiAnnotation
915
916 | HsCmdLet (HsLocalBinds id) -- let(rec)
917 (LHsCmd id)
918 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLet',
919 -- 'ApiAnnotation.AnnOpen' @'{'@,
920 -- 'ApiAnnotation.AnnClose' @'}'@,'ApiAnnotation.AnnIn'
921
922 -- For details on above see note [Api annotations] in ApiAnnotation
923
924 | HsCmdDo [CmdLStmt id]
925 (PostTc id Type) -- Type of the whole expression
926 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDo',
927 -- 'ApiAnnotation.AnnOpen', 'ApiAnnotation.AnnSemi',
928 -- 'ApiAnnotation.AnnVbar',
929 -- 'ApiAnnotation.AnnClose'
930
931 -- For details on above see note [Api annotations] in ApiAnnotation
932
933 | HsCmdCast TcCoercion -- A simpler version of HsWrap in HsExpr
934 (HsCmd id) -- If cmd :: arg1 --> res
935 -- co :: arg1 ~ arg2
936 -- Then (HsCmdCast co cmd) :: arg2 --> res
937 deriving (Typeable)
938 deriving instance (DataId id) => Data (HsCmd id)
939
940 data HsArrAppType = HsHigherOrderApp | HsFirstOrderApp
941 deriving (Data, Typeable)
942
943
944 {- | Top-level command, introducing a new arrow.
945 This may occur inside a proc (where the stack is empty) or as an
946 argument of a command-forming operator.
947 -}
948
949 type LHsCmdTop id = Located (HsCmdTop id)
950
951 data HsCmdTop id
952 = HsCmdTop (LHsCmd id)
953 (PostTc id Type) -- Nested tuple of inputs on the command's stack
954 (PostTc id Type) -- return type of the command
955 (CmdSyntaxTable id) -- See Note [CmdSyntaxTable]
956 deriving (Typeable)
957 deriving instance (DataId id) => Data (HsCmdTop id)
958
959 instance OutputableBndr id => Outputable (HsCmd id) where
960 ppr cmd = pprCmd cmd
961
962 -----------------------
963 -- pprCmd and pprLCmd call pprDeeper;
964 -- the underscore versions do not
965 pprLCmd :: OutputableBndr id => LHsCmd id -> SDoc
966 pprLCmd (L _ c) = pprCmd c
967
968 pprCmd :: OutputableBndr id => HsCmd id -> SDoc
969 pprCmd c | isQuietHsCmd c = ppr_cmd c
970 | otherwise = pprDeeper (ppr_cmd c)
971
972 isQuietHsCmd :: HsCmd id -> Bool
973 -- Parentheses do display something, but it gives little info and
974 -- if we go deeper when we go inside them then we get ugly things
975 -- like (...)
976 isQuietHsCmd (HsCmdPar _) = True
977 -- applications don't display anything themselves
978 isQuietHsCmd (HsCmdApp _ _) = True
979 isQuietHsCmd _ = False
980
981 -----------------------
982 ppr_lcmd :: OutputableBndr id => LHsCmd id -> SDoc
983 ppr_lcmd c = ppr_cmd (unLoc c)
984
985 ppr_cmd :: forall id. OutputableBndr id => HsCmd id -> SDoc
986 ppr_cmd (HsCmdPar c) = parens (ppr_lcmd c)
987
988 ppr_cmd (HsCmdApp c e)
989 = let (fun, args) = collect_args c [e] in
990 hang (ppr_lcmd fun) 2 (sep (map pprParendExpr args))
991 where
992 collect_args (L _ (HsCmdApp fun arg)) args = collect_args fun (arg:args)
993 collect_args fun args = (fun, args)
994
995 ppr_cmd (HsCmdLam matches)
996 = pprMatches (LambdaExpr :: HsMatchContext id) matches
997
998 ppr_cmd (HsCmdCase expr matches)
999 = sep [ sep [ptext (sLit "case"), nest 4 (ppr expr), ptext (sLit "of {")],
1000 nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ]
1001
1002 ppr_cmd (HsCmdIf _ e ct ce)
1003 = sep [hsep [ptext (sLit "if"), nest 2 (ppr e), ptext (sLit "then")],
1004 nest 4 (ppr ct),
1005 ptext (sLit "else"),
1006 nest 4 (ppr ce)]
1007
1008 -- special case: let ... in let ...
1009 ppr_cmd (HsCmdLet binds cmd@(L _ (HsCmdLet _ _)))
1010 = sep [hang (ptext (sLit "let")) 2 (hsep [pprBinds binds, ptext (sLit "in")]),
1011 ppr_lcmd cmd]
1012
1013 ppr_cmd (HsCmdLet binds cmd)
1014 = sep [hang (ptext (sLit "let")) 2 (pprBinds binds),
1015 hang (ptext (sLit "in")) 2 (ppr cmd)]
1016
1017 ppr_cmd (HsCmdDo stmts _) = pprDo ArrowExpr stmts
1018 ppr_cmd (HsCmdCast co cmd) = sep [ ppr_cmd cmd
1019 , ptext (sLit "|>") <+> ppr co ]
1020
1021 ppr_cmd (HsCmdArrApp arrow arg _ HsFirstOrderApp True)
1022 = hsep [ppr_lexpr arrow, larrowt, ppr_lexpr arg]
1023 ppr_cmd (HsCmdArrApp arrow arg _ HsFirstOrderApp False)
1024 = hsep [ppr_lexpr arg, arrowt, ppr_lexpr arrow]
1025 ppr_cmd (HsCmdArrApp arrow arg _ HsHigherOrderApp True)
1026 = hsep [ppr_lexpr arrow, larrowtt, ppr_lexpr arg]
1027 ppr_cmd (HsCmdArrApp arrow arg _ HsHigherOrderApp False)
1028 = hsep [ppr_lexpr arg, arrowtt, ppr_lexpr arrow]
1029
1030 ppr_cmd (HsCmdArrForm (L _ (HsVar v)) (Just _) [arg1, arg2])
1031 = sep [pprCmdArg (unLoc arg1), hsep [pprInfixOcc v, pprCmdArg (unLoc arg2)]]
1032 ppr_cmd (HsCmdArrForm op _ args)
1033 = hang (ptext (sLit "(|") <> ppr_lexpr op)
1034 4 (sep (map (pprCmdArg.unLoc) args) <> ptext (sLit "|)"))
1035
1036 pprCmdArg :: OutputableBndr id => HsCmdTop id -> SDoc
1037 pprCmdArg (HsCmdTop cmd@(L _ (HsCmdArrForm _ Nothing [])) _ _ _)
1038 = ppr_lcmd cmd
1039 pprCmdArg (HsCmdTop cmd _ _ _)
1040 = parens (ppr_lcmd cmd)
1041
1042 instance OutputableBndr id => Outputable (HsCmdTop id) where
1043 ppr = pprCmdArg
1044
1045 {-
1046 ************************************************************************
1047 * *
1048 \subsection{Record binds}
1049 * *
1050 ************************************************************************
1051 -}
1052
1053 type HsRecordBinds id = HsRecFields id (LHsExpr id)
1054
1055 {-
1056 ************************************************************************
1057 * *
1058 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
1059 * *
1060 ************************************************************************
1061
1062 @Match@es are sets of pattern bindings and right hand sides for
1063 functions, patterns or case branches. For example, if a function @g@
1064 is defined as:
1065 \begin{verbatim}
1066 g (x,y) = y
1067 g ((x:ys),y) = y+1,
1068 \end{verbatim}
1069 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
1070
1071 It is always the case that each element of an @[Match]@ list has the
1072 same number of @pats@s inside it. This corresponds to saying that
1073 a function defined by pattern matching must have the same number of
1074 patterns in each equation.
1075 -}
1076
1077 data MatchGroup id body
1078 = MG { mg_alts :: [LMatch id body] -- The alternatives
1079 , mg_arg_tys :: [PostTc id Type] -- Types of the arguments, t1..tn
1080 , mg_res_ty :: PostTc id Type -- Type of the result, tr
1081 , mg_origin :: Origin }
1082 -- The type is the type of the entire group
1083 -- t1 -> ... -> tn -> tr
1084 -- where there are n patterns
1085 deriving (Typeable)
1086 deriving instance (Data body,DataId id) => Data (MatchGroup id body)
1087
1088 type LMatch id body = Located (Match id body)
1089 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnSemi' when in a
1090 -- list
1091
1092 -- For details on above see note [Api annotations] in ApiAnnotation
1093 data Match id body
1094 = Match {
1095 m_fun_id_infix :: (Maybe (Located id,Bool)),
1096 -- fun_id and fun_infix for functions with multiple equations
1097 -- only present for a RdrName. See note [fun_id in Match]
1098 m_pats :: [LPat id], -- The patterns
1099 m_type :: (Maybe (LHsType id)),
1100 -- A type signature for the result of the match
1101 -- Nothing after typechecking
1102 m_grhss :: (GRHSs id body)
1103 } deriving (Typeable)
1104 deriving instance (Data body,DataId id) => Data (Match id body)
1105
1106 {-
1107 Note [fun_id in Match]
1108 ~~~~~~~~~~~~~~~~~~~~~~
1109
1110 The parser initially creates a FunBind with a single Match in it for
1111 every function definition it sees.
1112
1113 These are then grouped together by getMonoBind into a single FunBind,
1114 where all the Matches are combined.
1115
1116 In the process, all the original FunBind fun_id's bar one are
1117 discarded, including the locations.
1118
1119 This causes a problem for source to source conversions via API
1120 Annotations, so the original fun_ids and infix flags are preserved in
1121 the Match, when it originates from a FunBind.
1122
1123 Example infix function definition requiring individual API Annotations
1124
1125 (&&& ) [] [] = []
1126 xs &&& [] = xs
1127 ( &&& ) [] ys = ys
1128
1129
1130 -}
1131
1132 isEmptyMatchGroup :: MatchGroup id body -> Bool
1133 isEmptyMatchGroup (MG { mg_alts = ms }) = null ms
1134
1135 matchGroupArity :: MatchGroup id body -> Arity
1136 -- Precondition: MatchGroup is non-empty
1137 -- This is called before type checking, when mg_arg_tys is not set
1138 matchGroupArity (MG { mg_alts = alts })
1139 | (alt1:_) <- alts = length (hsLMatchPats alt1)
1140 | otherwise = panic "matchGroupArity"
1141
1142 hsLMatchPats :: LMatch id body -> [LPat id]
1143 hsLMatchPats (L _ (Match _ pats _ _)) = pats
1144
1145 -- | GRHSs are used both for pattern bindings and for Matches
1146 --
1147 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnVbar',
1148 -- 'ApiAnnotation.AnnEqual','ApiAnnotation.AnnWhere',
1149 -- 'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose'
1150 -- 'ApiAnnotation.AnnRarrow','ApiAnnotation.AnnSemi'
1151
1152 -- For details on above see note [Api annotations] in ApiAnnotation
1153 data GRHSs id body
1154 = GRHSs {
1155 grhssGRHSs :: [LGRHS id body], -- ^ Guarded RHSs
1156 grhssLocalBinds :: (HsLocalBinds id) -- ^ The where clause
1157 } deriving (Typeable)
1158 deriving instance (Data body,DataId id) => Data (GRHSs id body)
1159
1160 type LGRHS id body = Located (GRHS id body)
1161
1162 -- | Guarded Right Hand Side.
1163 data GRHS id body = GRHS [GuardLStmt id] -- Guards
1164 body -- Right hand side
1165 deriving (Typeable)
1166 deriving instance (Data body,DataId id) => Data (GRHS id body)
1167
1168 -- We know the list must have at least one @Match@ in it.
1169
1170 pprMatches :: (OutputableBndr idL, OutputableBndr idR, Outputable body)
1171 => HsMatchContext idL -> MatchGroup idR body -> SDoc
1172 pprMatches ctxt (MG { mg_alts = matches })
1173 = vcat (map (pprMatch ctxt) (map unLoc matches))
1174 -- Don't print the type; it's only a place-holder before typechecking
1175
1176 -- Exported to HsBinds, which can't see the defn of HsMatchContext
1177 pprFunBind :: (OutputableBndr idL, OutputableBndr idR, Outputable body)
1178 => idL -> Bool -> MatchGroup idR body -> SDoc
1179 pprFunBind fun inf matches = pprMatches (FunRhs fun inf) matches
1180
1181 -- Exported to HsBinds, which can't see the defn of HsMatchContext
1182 pprPatBind :: forall bndr id body. (OutputableBndr bndr, OutputableBndr id, Outputable body)
1183 => LPat bndr -> GRHSs id body -> SDoc
1184 pprPatBind pat (grhss)
1185 = sep [ppr pat, nest 2 (pprGRHSs (PatBindRhs :: HsMatchContext id) grhss)]
1186
1187 pprMatch :: (OutputableBndr idL, OutputableBndr idR, Outputable body)
1188 => HsMatchContext idL -> Match idR body -> SDoc
1189 pprMatch ctxt (Match _ pats maybe_ty grhss)
1190 = sep [ sep (herald : map (nest 2 . pprParendLPat) other_pats)
1191 , nest 2 ppr_maybe_ty
1192 , nest 2 (pprGRHSs ctxt grhss) ]
1193 where
1194 (herald, other_pats)
1195 = case ctxt of
1196 FunRhs fun is_infix
1197 | not is_infix -> (pprPrefixOcc fun, pats)
1198 -- f x y z = e
1199 -- Not pprBndr; the AbsBinds will
1200 -- have printed the signature
1201
1202 | null pats2 -> (pp_infix, [])
1203 -- x &&& y = e
1204
1205 | otherwise -> (parens pp_infix, pats2)
1206 -- (x &&& y) z = e
1207 where
1208 pp_infix = pprParendLPat pat1 <+> pprInfixOcc fun <+> pprParendLPat pat2
1209
1210 LambdaExpr -> (char '\\', pats)
1211
1212 _ -> ASSERT( null pats1 )
1213 (ppr pat1, []) -- No parens around the single pat
1214
1215 (pat1:pats1) = pats
1216 (pat2:pats2) = pats1
1217 ppr_maybe_ty = case maybe_ty of
1218 Just ty -> dcolon <+> ppr ty
1219 Nothing -> empty
1220
1221
1222 pprGRHSs :: (OutputableBndr idR, Outputable body)
1223 => HsMatchContext idL -> GRHSs idR body -> SDoc
1224 pprGRHSs ctxt (GRHSs grhss binds)
1225 = vcat (map (pprGRHS ctxt . unLoc) grhss)
1226 $$ ppUnless (isEmptyLocalBinds binds)
1227 (text "where" $$ nest 4 (pprBinds binds))
1228
1229 pprGRHS :: (OutputableBndr idR, Outputable body)
1230 => HsMatchContext idL -> GRHS idR body -> SDoc
1231 pprGRHS ctxt (GRHS [] body)
1232 = pp_rhs ctxt body
1233
1234 pprGRHS ctxt (GRHS guards body)
1235 = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt body]
1236
1237 pp_rhs :: Outputable body => HsMatchContext idL -> body -> SDoc
1238 pp_rhs ctxt rhs = matchSeparator ctxt <+> pprDeeper (ppr rhs)
1239
1240 {-
1241 ************************************************************************
1242 * *
1243 \subsection{Do stmts and list comprehensions}
1244 * *
1245 ************************************************************************
1246 -}
1247
1248 type LStmt id body = Located (StmtLR id id body)
1249 type LStmtLR idL idR body = Located (StmtLR idL idR body)
1250
1251 type Stmt id body = StmtLR id id body
1252
1253 type CmdLStmt id = LStmt id (LHsCmd id)
1254 type CmdStmt id = Stmt id (LHsCmd id)
1255 type ExprLStmt id = LStmt id (LHsExpr id)
1256 type ExprStmt id = Stmt id (LHsExpr id)
1257
1258 type GuardLStmt id = LStmt id (LHsExpr id)
1259 type GuardStmt id = Stmt id (LHsExpr id)
1260 type GhciLStmt id = LStmt id (LHsExpr id)
1261 type GhciStmt id = Stmt id (LHsExpr id)
1262
1263 -- The SyntaxExprs in here are used *only* for do-notation and monad
1264 -- comprehensions, which have rebindable syntax. Otherwise they are unused.
1265 -- | API Annotations when in qualifier lists or guards
1266 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnVbar',
1267 -- 'ApiAnnotation.AnnComma','ApiAnnotation.AnnThen',
1268 -- 'ApiAnnotation.AnnBy','ApiAnnotation.AnnBy',
1269 -- 'ApiAnnotation.AnnGroup','ApiAnnotation.AnnUsing'
1270
1271 -- For details on above see note [Api annotations] in ApiAnnotation
1272 data StmtLR idL idR body -- body should always be (LHs**** idR)
1273 = LastStmt -- Always the last Stmt in ListComp, MonadComp, PArrComp,
1274 -- and (after the renamer) DoExpr, MDoExpr
1275 -- Not used for GhciStmtCtxt, PatGuard, which scope over other stuff
1276 body
1277 Bool -- True <=> return was stripped by ApplicativeDo
1278 (SyntaxExpr idR) -- The return operator, used only for
1279 -- MonadComp For ListComp, PArrComp, we
1280 -- use the baked-in 'return' For DoExpr,
1281 -- MDoExpr, we don't apply a 'return' at
1282 -- all See Note [Monad Comprehensions] |
1283 -- - 'ApiAnnotation.AnnKeywordId' :
1284 -- 'ApiAnnotation.AnnLarrow'
1285
1286 -- For details on above see note [Api annotations] in ApiAnnotation
1287 | BindStmt (LPat idL)
1288 body
1289 (SyntaxExpr idR) -- The (>>=) operator; see Note [The type of bind]
1290 (SyntaxExpr idR) -- The fail operator
1291 -- The fail operator is noSyntaxExpr
1292 -- if the pattern match can't fail
1293
1294 -- | 'ApplicativeStmt' represents an applicative expression built with
1295 -- <$> and <*>. It is generated by the renamer, and is desugared into the
1296 -- appropriate applicative expression by the desugarer, but it is intended
1297 -- to be invisible in error messages.
1298 --
1299 -- For full details, see Note [ApplicativeDo] in RnExpr
1300 --
1301 | ApplicativeStmt
1302 [ ( SyntaxExpr idR
1303 , ApplicativeArg idL idR) ]
1304 -- [(<$>, e1), (<*>, e2), ..., (<*>, en)]
1305 (Maybe (SyntaxExpr idR)) -- 'join', if necessary
1306 (PostTc idR Type) -- Type of the body
1307
1308 | BodyStmt body -- See Note [BodyStmt]
1309 (SyntaxExpr idR) -- The (>>) operator
1310 (SyntaxExpr idR) -- The `guard` operator; used only in MonadComp
1311 -- See notes [Monad Comprehensions]
1312 (PostTc idR Type) -- Element type of the RHS (used for arrows)
1313
1314 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLet'
1315 -- 'ApiAnnotation.AnnOpen' @'{'@,'ApiAnnotation.AnnClose' @'}'@,
1316
1317 -- For details on above see note [Api annotations] in ApiAnnotation
1318 | LetStmt (HsLocalBindsLR idL idR)
1319
1320 -- ParStmts only occur in a list/monad comprehension
1321 | ParStmt [ParStmtBlock idL idR]
1322 (SyntaxExpr idR) -- Polymorphic `mzip` for monad comprehensions
1323 (SyntaxExpr idR) -- The `>>=` operator
1324 -- See notes [Monad Comprehensions]
1325 -- After renaming, the ids are the binders
1326 -- bound by the stmts and used after themp
1327
1328 | TransStmt {
1329 trS_form :: TransForm,
1330 trS_stmts :: [ExprLStmt idL], -- Stmts to the *left* of the 'group'
1331 -- which generates the tuples to be grouped
1332
1333 trS_bndrs :: [(idR, idR)], -- See Note [TransStmt binder map]
1334
1335 trS_using :: LHsExpr idR,
1336 trS_by :: Maybe (LHsExpr idR), -- "by e" (optional)
1337 -- Invariant: if trS_form = GroupBy, then grp_by = Just e
1338
1339 trS_ret :: SyntaxExpr idR, -- The monomorphic 'return' function for
1340 -- the inner monad comprehensions
1341 trS_bind :: SyntaxExpr idR, -- The '(>>=)' operator
1342 trS_fmap :: SyntaxExpr idR -- The polymorphic 'fmap' function for desugaring
1343 -- Only for 'group' forms
1344 } -- See Note [Monad Comprehensions]
1345
1346 -- Recursive statement (see Note [How RecStmt works] below)
1347 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRec'
1348
1349 -- For details on above see note [Api annotations] in ApiAnnotation
1350 | RecStmt
1351 { recS_stmts :: [LStmtLR idL idR body]
1352
1353 -- The next two fields are only valid after renaming
1354 , recS_later_ids :: [idR] -- The ids are a subset of the variables bound by the
1355 -- stmts that are used in stmts that follow the RecStmt
1356
1357 , recS_rec_ids :: [idR] -- Ditto, but these variables are the "recursive" ones,
1358 -- that are used before they are bound in the stmts of
1359 -- the RecStmt.
1360 -- An Id can be in both groups
1361 -- Both sets of Ids are (now) treated monomorphically
1362 -- See Note [How RecStmt works] for why they are separate
1363
1364 -- Rebindable syntax
1365 , recS_bind_fn :: SyntaxExpr idR -- The bind function
1366 , recS_ret_fn :: SyntaxExpr idR -- The return function
1367 , recS_mfix_fn :: SyntaxExpr idR -- The mfix function
1368
1369 -- These fields are only valid after typechecking
1370 , recS_later_rets :: [PostTcExpr] -- (only used in the arrow version)
1371 , recS_rec_rets :: [PostTcExpr] -- These expressions correspond 1-to-1
1372 -- with recS_later_ids and recS_rec_ids,
1373 -- and are the expressions that should be
1374 -- returned by the recursion.
1375 -- They may not quite be the Ids themselves,
1376 -- because the Id may be *polymorphic*, but
1377 -- the returned thing has to be *monomorphic*,
1378 -- so they may be type applications
1379
1380 , recS_ret_ty :: PostTc idR Type -- The type of
1381 -- do { stmts; return (a,b,c) }
1382 -- With rebindable syntax the type might not
1383 -- be quite as simple as (m (tya, tyb, tyc)).
1384 }
1385 deriving (Typeable)
1386 deriving instance (Data body, DataId idL, DataId idR)
1387 => Data (StmtLR idL idR body)
1388
1389 data TransForm -- The 'f' below is the 'using' function, 'e' is the by function
1390 = ThenForm -- then f or then f by e (depending on trS_by)
1391 | GroupForm -- then group using f or then group by e using f (depending on trS_by)
1392 deriving (Data, Typeable)
1393
1394 data ParStmtBlock idL idR
1395 = ParStmtBlock
1396 [ExprLStmt idL]
1397 [idR] -- The variables to be returned
1398 (SyntaxExpr idR) -- The return operator
1399 deriving( Typeable )
1400 deriving instance (DataId idL, DataId idR) => Data (ParStmtBlock idL idR)
1401
1402 data ApplicativeArg idL idR
1403 = ApplicativeArgOne -- pat <- expr (pat must be irrefutable)
1404 (LPat idL)
1405 (LHsExpr idL)
1406 | ApplicativeArgMany -- do { stmts; return vars }
1407 [ExprLStmt idL] -- stmts
1408 (SyntaxExpr idL) -- return (v1,..,vn), or just (v1,..,vn)
1409 (LPat idL) -- (v1,...,vn)
1410 deriving( Typeable )
1411 deriving instance (DataId idL, DataId idR) => Data (ApplicativeArg idL idR)
1412
1413 {-
1414 Note [The type of bind in Stmts]
1415 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1416 Some Stmts, notably BindStmt, keep the (>>=) bind operator.
1417 We do NOT assume that it has type
1418 (>>=) :: m a -> (a -> m b) -> m b
1419 In some cases (see Trac #303, #1537) it might have a more
1420 exotic type, such as
1421 (>>=) :: m i j a -> (a -> m j k b) -> m i k b
1422 So we must be careful not to make assumptions about the type.
1423 In particular, the monad may not be uniform throughout.
1424
1425 Note [TransStmt binder map]
1426 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1427 The [(idR,idR)] in a TransStmt behaves as follows:
1428
1429 * Before renaming: []
1430
1431 * After renaming:
1432 [ (x27,x27), ..., (z35,z35) ]
1433 These are the variables
1434 bound by the stmts to the left of the 'group'
1435 and used either in the 'by' clause,
1436 or in the stmts following the 'group'
1437 Each item is a pair of identical variables.
1438
1439 * After typechecking:
1440 [ (x27:Int, x27:[Int]), ..., (z35:Bool, z35:[Bool]) ]
1441 Each pair has the same unique, but different *types*.
1442
1443 Note [BodyStmt]
1444 ~~~~~~~~~~~~~~~
1445 BodyStmts are a bit tricky, because what they mean
1446 depends on the context. Consider the following contexts:
1447
1448 A do expression of type (m res_ty)
1449 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1450 * BodyStmt E any_ty: do { ....; E; ... }
1451 E :: m any_ty
1452 Translation: E >> ...
1453
1454 A list comprehensions of type [elt_ty]
1455 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1456 * BodyStmt E Bool: [ .. | .... E ]
1457 [ .. | ..., E, ... ]
1458 [ .. | .... | ..., E | ... ]
1459 E :: Bool
1460 Translation: if E then fail else ...
1461
1462 A guard list, guarding a RHS of type rhs_ty
1463 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1464 * BodyStmt E BooParStmtBlockl: f x | ..., E, ... = ...rhs...
1465 E :: Bool
1466 Translation: if E then fail else ...
1467
1468 A monad comprehension of type (m res_ty)
1469 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1470 * BodyStmt E Bool: [ .. | .... E ]
1471 E :: Bool
1472 Translation: guard E >> ...
1473
1474 Array comprehensions are handled like list comprehensions.
1475
1476 Note [How RecStmt works]
1477 ~~~~~~~~~~~~~~~~~~~~~~~~
1478 Example:
1479 HsDo [ BindStmt x ex
1480
1481 , RecStmt { recS_rec_ids = [a, c]
1482 , recS_stmts = [ BindStmt b (return (a,c))
1483 , LetStmt a = ...b...
1484 , BindStmt c ec ]
1485 , recS_later_ids = [a, b]
1486
1487 , return (a b) ]
1488
1489 Here, the RecStmt binds a,b,c; but
1490 - Only a,b are used in the stmts *following* the RecStmt,
1491 - Only a,c are used in the stmts *inside* the RecStmt
1492 *before* their bindings
1493
1494 Why do we need *both* rec_ids and later_ids? For monads they could be
1495 combined into a single set of variables, but not for arrows. That
1496 follows from the types of the respective feedback operators:
1497
1498 mfix :: MonadFix m => (a -> m a) -> m a
1499 loop :: ArrowLoop a => a (b,d) (c,d) -> a b c
1500
1501 * For mfix, the 'a' covers the union of the later_ids and the rec_ids
1502 * For 'loop', 'c' is the later_ids and 'd' is the rec_ids
1503
1504 Note [Typing a RecStmt]
1505 ~~~~~~~~~~~~~~~~~~~~~~~
1506 A (RecStmt stmts) types as if you had written
1507
1508 (v1,..,vn, _, ..., _) <- mfix (\~(_, ..., _, r1, ..., rm) ->
1509 do { stmts
1510 ; return (v1,..vn, r1, ..., rm) })
1511
1512 where v1..vn are the later_ids
1513 r1..rm are the rec_ids
1514
1515 Note [Monad Comprehensions]
1516 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1517 Monad comprehensions require separate functions like 'return' and
1518 '>>=' for desugaring. These functions are stored in the statements
1519 used in monad comprehensions. For example, the 'return' of the 'LastStmt'
1520 expression is used to lift the body of the monad comprehension:
1521
1522 [ body | stmts ]
1523 =>
1524 stmts >>= \bndrs -> return body
1525
1526 In transform and grouping statements ('then ..' and 'then group ..') the
1527 'return' function is required for nested monad comprehensions, for example:
1528
1529 [ body | stmts, then f, rest ]
1530 =>
1531 f [ env | stmts ] >>= \bndrs -> [ body | rest ]
1532
1533 BodyStmts require the 'Control.Monad.guard' function for boolean
1534 expressions:
1535
1536 [ body | exp, stmts ]
1537 =>
1538 guard exp >> [ body | stmts ]
1539
1540 Parallel statements require the 'Control.Monad.Zip.mzip' function:
1541
1542 [ body | stmts1 | stmts2 | .. ]
1543 =>
1544 mzip stmts1 (mzip stmts2 (..)) >>= \(bndrs1, (bndrs2, ..)) -> return body
1545
1546 In any other context than 'MonadComp', the fields for most of these
1547 'SyntaxExpr's stay bottom.
1548 -}
1549
1550 instance (OutputableBndr idL)
1551 => Outputable (ParStmtBlock idL idR) where
1552 ppr (ParStmtBlock stmts _ _) = interpp'SP stmts
1553
1554 instance (OutputableBndr idL, OutputableBndr idR, Outputable body)
1555 => Outputable (StmtLR idL idR body) where
1556 ppr stmt = pprStmt stmt
1557
1558 pprStmt :: forall idL idR body . (OutputableBndr idL, OutputableBndr idR, Outputable body)
1559 => (StmtLR idL idR body) -> SDoc
1560 pprStmt (LastStmt expr ret_stripped _)
1561 = ifPprDebug (ptext (sLit "[last]")) <+>
1562 (if ret_stripped then ptext (sLit "return") else empty) <+>
1563 ppr expr
1564 pprStmt (BindStmt pat expr _ _) = hsep [ppr pat, larrow, ppr expr]
1565 pprStmt (LetStmt binds) = hsep [ptext (sLit "let"), pprBinds binds]
1566 pprStmt (BodyStmt expr _ _ _) = ppr expr
1567 pprStmt (ParStmt stmtss _ _) = sep (punctuate (ptext (sLit " | ")) (map ppr stmtss))
1568
1569 pprStmt (TransStmt { trS_stmts = stmts, trS_by = by, trS_using = using, trS_form = form })
1570 = sep $ punctuate comma (map ppr stmts ++ [pprTransStmt by using form])
1571
1572 pprStmt (RecStmt { recS_stmts = segment, recS_rec_ids = rec_ids
1573 , recS_later_ids = later_ids })
1574 = ptext (sLit "rec") <+>
1575 vcat [ ppr_do_stmts segment
1576 , ifPprDebug (vcat [ ptext (sLit "rec_ids=") <> ppr rec_ids
1577 , ptext (sLit "later_ids=") <> ppr later_ids])]
1578
1579 pprStmt (ApplicativeStmt args mb_join _)
1580 = getPprStyle $ \style ->
1581 if userStyle style
1582 then pp_for_user
1583 else pp_debug
1584 where
1585 -- make all the Applicative stuff invisible in error messages by
1586 -- flattening the whole ApplicativeStmt nest back to a sequence
1587 -- of statements.
1588 pp_for_user = vcat $ punctuate semi $ concatMap flattenArg args
1589
1590 -- ppr directly rather than transforming here, becuase we need to
1591 -- inject a "return" which is hard when we're polymorphic in the id
1592 -- type.
1593 flattenStmt :: ExprLStmt idL -> [SDoc]
1594 flattenStmt (L _ (ApplicativeStmt args _ _)) = concatMap flattenArg args
1595 flattenStmt stmt = [ppr stmt]
1596
1597 flattenArg (_, ApplicativeArgOne pat expr) =
1598 [ppr (BindStmt pat expr noSyntaxExpr noSyntaxExpr :: ExprStmt idL)]
1599 flattenArg (_, ApplicativeArgMany stmts _ _) =
1600 concatMap flattenStmt stmts
1601
1602 pp_debug =
1603 let
1604 ap_expr = sep (punctuate (ptext (sLit " |")) (map pp_arg args))
1605 in
1606 if isNothing mb_join
1607 then ap_expr
1608 else ptext (sLit "join") <+> parens ap_expr
1609
1610 pp_arg (_, ApplicativeArgOne pat expr) =
1611 ppr (BindStmt pat expr noSyntaxExpr noSyntaxExpr :: ExprStmt idL)
1612 pp_arg (_, ApplicativeArgMany stmts return pat) =
1613 ppr pat <+>
1614 ptext (sLit "<-") <+>
1615 ppr (HsDo DoExpr (stmts ++ [noLoc (LastStmt (noLoc return) False noSyntaxExpr)])
1616 (error "pprStmt"))
1617
1618 pprTransformStmt :: OutputableBndr id => [id] -> LHsExpr id -> Maybe (LHsExpr id) -> SDoc
1619 pprTransformStmt bndrs using by
1620 = sep [ ptext (sLit "then") <+> ifPprDebug (braces (ppr bndrs))
1621 , nest 2 (ppr using)
1622 , nest 2 (pprBy by)]
1623
1624 pprTransStmt :: Outputable body => Maybe body -> body -> TransForm -> SDoc
1625 pprTransStmt by using ThenForm
1626 = sep [ ptext (sLit "then"), nest 2 (ppr using), nest 2 (pprBy by)]
1627 pprTransStmt by using GroupForm
1628 = sep [ ptext (sLit "then group"), nest 2 (pprBy by), nest 2 (ptext (sLit "using") <+> ppr using)]
1629
1630 pprBy :: Outputable body => Maybe body -> SDoc
1631 pprBy Nothing = empty
1632 pprBy (Just e) = ptext (sLit "by") <+> ppr e
1633
1634 pprDo :: (OutputableBndr id, Outputable body)
1635 => HsStmtContext any -> [LStmt id body] -> SDoc
1636 pprDo DoExpr stmts = ptext (sLit "do") <+> ppr_do_stmts stmts
1637 pprDo GhciStmtCtxt stmts = ptext (sLit "do") <+> ppr_do_stmts stmts
1638 pprDo ArrowExpr stmts = ptext (sLit "do") <+> ppr_do_stmts stmts
1639 pprDo MDoExpr stmts = ptext (sLit "mdo") <+> ppr_do_stmts stmts
1640 pprDo ListComp stmts = brackets $ pprComp stmts
1641 pprDo PArrComp stmts = paBrackets $ pprComp stmts
1642 pprDo MonadComp stmts = brackets $ pprComp stmts
1643 pprDo _ _ = panic "pprDo" -- PatGuard, ParStmtCxt
1644
1645 ppr_do_stmts :: (OutputableBndr idL, OutputableBndr idR, Outputable body)
1646 => [LStmtLR idL idR body] -> SDoc
1647 -- Print a bunch of do stmts, with explicit braces and semicolons,
1648 -- so that we are not vulnerable to layout bugs
1649 ppr_do_stmts stmts
1650 = lbrace <+> pprDeeperList vcat (punctuate semi (map ppr stmts))
1651 <+> rbrace
1652
1653 pprComp :: (OutputableBndr id, Outputable body)
1654 => [LStmt id body] -> SDoc
1655 pprComp quals -- Prints: body | qual1, ..., qualn
1656 | not (null quals)
1657 , L _ (LastStmt body _ _) <- last quals
1658 = hang (ppr body <+> char '|') 2 (pprQuals (dropTail 1 quals))
1659 | otherwise
1660 = pprPanic "pprComp" (pprQuals quals)
1661
1662 pprQuals :: (OutputableBndr id, Outputable body)
1663 => [LStmt id body] -> SDoc
1664 -- Show list comprehension qualifiers separated by commas
1665 pprQuals quals = interpp'SP quals
1666
1667 {-
1668 ************************************************************************
1669 * *
1670 Template Haskell quotation brackets
1671 * *
1672 ************************************************************************
1673 -}
1674
1675 data HsSplice id
1676 = HsTypedSplice -- $$z or $$(f 4)
1677 id -- A unique name to identify this splice point
1678 (LHsExpr id) -- See Note [Pending Splices]
1679
1680 | HsUntypedSplice -- $z or $(f 4)
1681 id -- A unique name to identify this splice point
1682 (LHsExpr id) -- See Note [Pending Splices]
1683
1684 | HsQuasiQuote -- See Note [Quasi-quote overview] in TcSplice
1685 id -- Splice point
1686 id -- Quoter
1687 SrcSpan -- The span of the enclosed string
1688 FastString -- The enclosed string
1689 deriving (Typeable )
1690
1691 deriving instance (DataId id) => Data (HsSplice id)
1692
1693 isTypedSplice :: HsSplice id -> Bool
1694 isTypedSplice (HsTypedSplice {}) = True
1695 isTypedSplice _ = False -- Quasi-quotes are untyped splices
1696
1697 -- See Note [Pending Splices]
1698 type SplicePointName = Name
1699
1700 data PendingRnSplice
1701 = PendingRnSplice UntypedSpliceFlavour SplicePointName (LHsExpr Name)
1702 deriving (Data, Typeable)
1703
1704 data UntypedSpliceFlavour
1705 = UntypedExpSplice
1706 | UntypedPatSplice
1707 | UntypedTypeSplice
1708 | UntypedDeclSplice
1709 deriving( Data, Typeable )
1710
1711 data PendingTcSplice
1712 = PendingTcSplice SplicePointName (LHsExpr Id)
1713 deriving( Data, Typeable )
1714
1715
1716 {-
1717 Note [Pending Splices]
1718 ~~~~~~~~~~~~~~~~~~~~~~
1719 When we rename an untyped bracket, we name and lift out all the nested
1720 splices, so that when the typechecker hits the bracket, it can
1721 typecheck those nested splices without having to walk over the untyped
1722 bracket code. So for example
1723 [| f $(g x) |]
1724 looks like
1725
1726 HsBracket (HsApp (HsVar "f") (HsSpliceE _ (g x)))
1727
1728 which the renamer rewrites to
1729
1730 HsRnBracketOut (HsApp (HsVar f) (HsSpliceE sn (g x)))
1731 [PendingRnSplice UntypedExpSplice sn (g x)]
1732
1733 * The 'sn' is the Name of the splice point, the SplicePointName
1734
1735 * The PendingRnExpSplice gives the splice that splice-point name maps to;
1736 and the typechecker can now conveniently find these sub-expressions
1737
1738 * The other copy of the splice, in the second argument of HsSpliceE
1739 in the renamed first arg of HsRnBracketOut
1740 is used only for pretty printing
1741
1742 There are four varieties of pending splices generated by the renamer,
1743 distinguished by their UntypedSpliceFlavour
1744
1745 * Pending expression splices (UntypedExpSplice), e.g.,
1746 [|$(f x) + 2|]
1747
1748 UntypedExpSplice is also used for
1749 * quasi-quotes, where the pending expression expands to
1750 $(quoter "...blah...")
1751 (see RnSplice.makePending, HsQuasiQuote case)
1752
1753 * cross-stage lifting, where the pending expression expands to
1754 $(lift x)
1755 (see RnSplice.checkCrossStageLifting)
1756
1757 * Pending pattern splices (UntypedPatSplice), e.g.,
1758 [| \$(f x) -> x |]
1759
1760 * Pending type splices (UntypedTypeSplice), e.g.,
1761 [| f :: $(g x) |]
1762
1763 * Pending declaration (UntypedDeclSplice), e.g.,
1764 [| let $(f x) in ... |]
1765
1766 There is a fifth variety of pending splice, which is generated by the type
1767 checker:
1768
1769 * Pending *typed* expression splices, (PendingTcSplice), e.g.,
1770 [||1 + $$(f 2)||]
1771
1772 It would be possible to eliminate HsRnBracketOut and use HsBracketOut for the
1773 output of the renamer. However, when pretty printing the output of the renamer,
1774 e.g., in a type error message, we *do not* want to print out the pending
1775 splices. In contrast, when pretty printing the output of the type checker, we
1776 *do* want to print the pending splices. So splitting them up seems to make
1777 sense, although I hate to add another constructor to HsExpr.
1778 -}
1779
1780 instance OutputableBndr id => Outputable (HsSplice id) where
1781 ppr s = pprSplice s
1782
1783 pprPendingSplice :: OutputableBndr id => SplicePointName -> LHsExpr id -> SDoc
1784 pprPendingSplice n e = angleBrackets (ppr n <> comma <+> ppr e)
1785
1786 pprSplice :: OutputableBndr id => HsSplice id -> SDoc
1787 pprSplice (HsTypedSplice n e) = ppr_splice (ptext (sLit "$$")) n e
1788 pprSplice (HsUntypedSplice n e) = ppr_splice (ptext (sLit "$")) n e
1789 pprSplice (HsQuasiQuote n q _ s) = ppr_quasi n q s
1790
1791 ppr_quasi :: OutputableBndr id => id -> id -> FastString -> SDoc
1792 ppr_quasi n quoter quote = ifPprDebug (brackets (ppr n)) <>
1793 char '[' <> ppr quoter <> ptext (sLit "|") <>
1794 ppr quote <> ptext (sLit "|]")
1795
1796 ppr_splice :: OutputableBndr id => SDoc -> id -> LHsExpr id -> SDoc
1797 ppr_splice herald n e
1798 = herald <> ifPprDebug (brackets (ppr n)) <> eDoc
1799 where
1800 -- We use pprLExpr to match pprParendExpr:
1801 -- Using pprLExpr makes sure that we go 'deeper'
1802 -- I think that is usually (always?) right
1803 pp_as_was = pprLExpr e
1804 eDoc = case unLoc e of
1805 HsPar _ -> pp_as_was
1806 HsVar _ -> pp_as_was
1807 _ -> parens pp_as_was
1808
1809 data HsBracket id = ExpBr (LHsExpr id) -- [| expr |]
1810 | PatBr (LPat id) -- [p| pat |]
1811 | DecBrL [LHsDecl id] -- [d| decls |]; result of parser
1812 | DecBrG (HsGroup id) -- [d| decls |]; result of renamer
1813 | TypBr (LHsType id) -- [t| type |]
1814 | VarBr Bool id -- True: 'x, False: ''T
1815 -- (The Bool flag is used only in pprHsBracket)
1816 | TExpBr (LHsExpr id) -- [|| expr ||]
1817 deriving (Typeable)
1818 deriving instance (DataId id) => Data (HsBracket id)
1819
1820 isTypedBracket :: HsBracket id -> Bool
1821 isTypedBracket (TExpBr {}) = True
1822 isTypedBracket _ = False
1823
1824 instance OutputableBndr id => Outputable (HsBracket id) where
1825 ppr = pprHsBracket
1826
1827
1828 pprHsBracket :: OutputableBndr id => HsBracket id -> SDoc
1829 pprHsBracket (ExpBr e) = thBrackets empty (ppr e)
1830 pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p)
1831 pprHsBracket (DecBrG gp) = thBrackets (char 'd') (ppr gp)
1832 pprHsBracket (DecBrL ds) = thBrackets (char 'd') (vcat (map ppr ds))
1833 pprHsBracket (TypBr t) = thBrackets (char 't') (ppr t)
1834 pprHsBracket (VarBr True n) = char '\'' <> ppr n
1835 pprHsBracket (VarBr False n) = ptext (sLit "''") <> ppr n
1836 pprHsBracket (TExpBr e) = thTyBrackets (ppr e)
1837
1838 thBrackets :: SDoc -> SDoc -> SDoc
1839 thBrackets pp_kind pp_body = char '[' <> pp_kind <> char '|' <+>
1840 pp_body <+> ptext (sLit "|]")
1841
1842 thTyBrackets :: SDoc -> SDoc
1843 thTyBrackets pp_body = ptext (sLit "[||") <+> pp_body <+> ptext (sLit "||]")
1844
1845 instance Outputable PendingRnSplice where
1846 ppr (PendingRnSplice _ n e) = pprPendingSplice n e
1847
1848 instance Outputable PendingTcSplice where
1849 ppr (PendingTcSplice n e) = pprPendingSplice n e
1850
1851 {-
1852 ************************************************************************
1853 * *
1854 \subsection{Enumerations and list comprehensions}
1855 * *
1856 ************************************************************************
1857 -}
1858
1859 data ArithSeqInfo id
1860 = From (LHsExpr id)
1861 | FromThen (LHsExpr id)
1862 (LHsExpr id)
1863 | FromTo (LHsExpr id)
1864 (LHsExpr id)
1865 | FromThenTo (LHsExpr id)
1866 (LHsExpr id)
1867 (LHsExpr id)
1868 deriving (Typeable)
1869 deriving instance (DataId id) => Data (ArithSeqInfo id)
1870
1871 instance OutputableBndr id => Outputable (ArithSeqInfo id) where
1872 ppr (From e1) = hcat [ppr e1, pp_dotdot]
1873 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
1874 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
1875 ppr (FromThenTo e1 e2 e3)
1876 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
1877
1878 pp_dotdot :: SDoc
1879 pp_dotdot = ptext (sLit " .. ")
1880
1881 {-
1882 ************************************************************************
1883 * *
1884 \subsection{HsMatchCtxt}
1885 * *
1886 ************************************************************************
1887 -}
1888
1889 data HsMatchContext id -- Context of a Match
1890 = FunRhs id Bool -- Function binding for f; True <=> written infix
1891 | LambdaExpr -- Patterns of a lambda
1892 | CaseAlt -- Patterns and guards on a case alternative
1893 | IfAlt -- Guards of a multi-way if alternative
1894 | ProcExpr -- Patterns of a proc
1895 | PatBindRhs -- A pattern binding eg [y] <- e = e
1896
1897 | RecUpd -- Record update [used only in DsExpr to
1898 -- tell matchWrapper what sort of
1899 -- runtime error message to generate]
1900
1901 | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt, list comprehension,
1902 -- pattern guard, etc
1903
1904 | ThPatSplice -- A Template Haskell pattern splice
1905 | ThPatQuote -- A Template Haskell pattern quotation [p| (a,b) |]
1906 | PatSyn -- A pattern synonym declaration
1907 deriving (Data, Typeable)
1908
1909 data HsStmtContext id
1910 = ListComp
1911 | MonadComp
1912 | PArrComp -- Parallel array comprehension
1913
1914 | DoExpr -- do { ... }
1915 | MDoExpr -- mdo { ... } ie recursive do-expression
1916 | ArrowExpr -- do-notation in an arrow-command context
1917
1918 | GhciStmtCtxt -- A command-line Stmt in GHCi pat <- rhs
1919 | PatGuard (HsMatchContext id) -- Pattern guard for specified thing
1920 | ParStmtCtxt (HsStmtContext id) -- A branch of a parallel stmt
1921 | TransStmtCtxt (HsStmtContext id) -- A branch of a transform stmt
1922 deriving (Data, Typeable)
1923
1924 isListCompExpr :: HsStmtContext id -> Bool
1925 -- Uses syntax [ e | quals ]
1926 isListCompExpr ListComp = True
1927 isListCompExpr PArrComp = True
1928 isListCompExpr MonadComp = True
1929 isListCompExpr (ParStmtCtxt c) = isListCompExpr c
1930 isListCompExpr (TransStmtCtxt c) = isListCompExpr c
1931 isListCompExpr _ = False
1932
1933 isMonadCompExpr :: HsStmtContext id -> Bool
1934 isMonadCompExpr MonadComp = True
1935 isMonadCompExpr (ParStmtCtxt ctxt) = isMonadCompExpr ctxt
1936 isMonadCompExpr (TransStmtCtxt ctxt) = isMonadCompExpr ctxt
1937 isMonadCompExpr _ = False
1938
1939 matchSeparator :: HsMatchContext id -> SDoc
1940 matchSeparator (FunRhs {}) = ptext (sLit "=")
1941 matchSeparator CaseAlt = ptext (sLit "->")
1942 matchSeparator IfAlt = ptext (sLit "->")
1943 matchSeparator LambdaExpr = ptext (sLit "->")
1944 matchSeparator ProcExpr = ptext (sLit "->")
1945 matchSeparator PatBindRhs = ptext (sLit "=")
1946 matchSeparator (StmtCtxt _) = ptext (sLit "<-")
1947 matchSeparator RecUpd = panic "unused"
1948 matchSeparator ThPatSplice = panic "unused"
1949 matchSeparator ThPatQuote = panic "unused"
1950 matchSeparator PatSyn = panic "unused"
1951
1952 pprMatchContext :: Outputable id => HsMatchContext id -> SDoc
1953 pprMatchContext ctxt
1954 | want_an ctxt = ptext (sLit "an") <+> pprMatchContextNoun ctxt
1955 | otherwise = ptext (sLit "a") <+> pprMatchContextNoun ctxt
1956 where
1957 want_an (FunRhs {}) = True -- Use "an" in front
1958 want_an ProcExpr = True
1959 want_an _ = False
1960
1961 pprMatchContextNoun :: Outputable id => HsMatchContext id -> SDoc
1962 pprMatchContextNoun (FunRhs fun _) = ptext (sLit "equation for")
1963 <+> quotes (ppr fun)
1964 pprMatchContextNoun CaseAlt = ptext (sLit "case alternative")
1965 pprMatchContextNoun IfAlt = ptext (sLit "multi-way if alternative")
1966 pprMatchContextNoun RecUpd = ptext (sLit "record-update construct")
1967 pprMatchContextNoun ThPatSplice = ptext (sLit "Template Haskell pattern splice")
1968 pprMatchContextNoun ThPatQuote = ptext (sLit "Template Haskell pattern quotation")
1969 pprMatchContextNoun PatBindRhs = ptext (sLit "pattern binding")
1970 pprMatchContextNoun LambdaExpr = ptext (sLit "lambda abstraction")
1971 pprMatchContextNoun ProcExpr = ptext (sLit "arrow abstraction")
1972 pprMatchContextNoun (StmtCtxt ctxt) = ptext (sLit "pattern binding in")
1973 $$ pprStmtContext ctxt
1974 pprMatchContextNoun PatSyn = ptext (sLit "pattern synonym declaration")
1975
1976 -----------------
1977 pprAStmtContext, pprStmtContext :: Outputable id => HsStmtContext id -> SDoc
1978 pprAStmtContext ctxt = article <+> pprStmtContext ctxt
1979 where
1980 pp_an = ptext (sLit "an")
1981 pp_a = ptext (sLit "a")
1982 article = case ctxt of
1983 MDoExpr -> pp_an
1984 PArrComp -> pp_an
1985 GhciStmtCtxt -> pp_an
1986 _ -> pp_a
1987
1988
1989 -----------------
1990 pprStmtContext GhciStmtCtxt = ptext (sLit "interactive GHCi command")
1991 pprStmtContext DoExpr = ptext (sLit "'do' block")
1992 pprStmtContext MDoExpr = ptext (sLit "'mdo' block")
1993 pprStmtContext ArrowExpr = ptext (sLit "'do' block in an arrow command")
1994 pprStmtContext ListComp = ptext (sLit "list comprehension")
1995 pprStmtContext MonadComp = ptext (sLit "monad comprehension")
1996 pprStmtContext PArrComp = ptext (sLit "array comprehension")
1997 pprStmtContext (PatGuard ctxt) = ptext (sLit "pattern guard for") $$ pprMatchContext ctxt
1998
1999 -- Drop the inner contexts when reporting errors, else we get
2000 -- Unexpected transform statement
2001 -- in a transformed branch of
2002 -- transformed branch of
2003 -- transformed branch of monad comprehension
2004 pprStmtContext (ParStmtCtxt c)
2005 | opt_PprStyle_Debug = sep [ptext (sLit "parallel branch of"), pprAStmtContext c]
2006 | otherwise = pprStmtContext c
2007 pprStmtContext (TransStmtCtxt c)
2008 | opt_PprStyle_Debug = sep [ptext (sLit "transformed branch of"), pprAStmtContext c]
2009 | otherwise = pprStmtContext c
2010
2011
2012 -- Used to generate the string for a *runtime* error message
2013 matchContextErrString :: Outputable id => HsMatchContext id -> SDoc
2014 matchContextErrString (FunRhs fun _) = ptext (sLit "function") <+> ppr fun
2015 matchContextErrString CaseAlt = ptext (sLit "case")
2016 matchContextErrString IfAlt = ptext (sLit "multi-way if")
2017 matchContextErrString PatBindRhs = ptext (sLit "pattern binding")
2018 matchContextErrString RecUpd = ptext (sLit "record update")
2019 matchContextErrString LambdaExpr = ptext (sLit "lambda")
2020 matchContextErrString ProcExpr = ptext (sLit "proc")
2021 matchContextErrString ThPatSplice = panic "matchContextErrString" -- Not used at runtime
2022 matchContextErrString ThPatQuote = panic "matchContextErrString" -- Not used at runtime
2023 matchContextErrString PatSyn = panic "matchContextErrString" -- Not used at runtime
2024 matchContextErrString (StmtCtxt (ParStmtCtxt c)) = matchContextErrString (StmtCtxt c)
2025 matchContextErrString (StmtCtxt (TransStmtCtxt c)) = matchContextErrString (StmtCtxt c)
2026 matchContextErrString (StmtCtxt (PatGuard _)) = ptext (sLit "pattern guard")
2027 matchContextErrString (StmtCtxt GhciStmtCtxt) = ptext (sLit "interactive GHCi command")
2028 matchContextErrString (StmtCtxt DoExpr) = ptext (sLit "'do' block")
2029 matchContextErrString (StmtCtxt ArrowExpr) = ptext (sLit "'do' block")
2030 matchContextErrString (StmtCtxt MDoExpr) = ptext (sLit "'mdo' block")
2031 matchContextErrString (StmtCtxt ListComp) = ptext (sLit "list comprehension")
2032 matchContextErrString (StmtCtxt MonadComp) = ptext (sLit "monad comprehension")
2033 matchContextErrString (StmtCtxt PArrComp) = ptext (sLit "array comprehension")
2034
2035 pprMatchInCtxt :: (OutputableBndr idL, OutputableBndr idR, Outputable body)
2036 => HsMatchContext idL -> Match idR body -> SDoc
2037 pprMatchInCtxt ctxt match = hang (ptext (sLit "In") <+> pprMatchContext ctxt <> colon)
2038 4 (pprMatch ctxt match)
2039
2040 pprStmtInCtxt :: (OutputableBndr idL, OutputableBndr idR, Outputable body)
2041 => HsStmtContext idL -> StmtLR idL idR body -> SDoc
2042 pprStmtInCtxt ctxt (LastStmt e _ _)
2043 | isListCompExpr ctxt -- For [ e | .. ], do not mutter about "stmts"
2044 = hang (ptext (sLit "In the expression:")) 2 (ppr e)
2045
2046 pprStmtInCtxt ctxt stmt
2047 = hang (ptext (sLit "In a stmt of") <+> pprAStmtContext ctxt <> colon)
2048 2 (ppr_stmt stmt)
2049 where
2050 -- For Group and Transform Stmts, don't print the nested stmts!
2051 ppr_stmt (TransStmt { trS_by = by, trS_using = using
2052 , trS_form = form }) = pprTransStmt by using form
2053 ppr_stmt stmt = pprStmt stmt