HsExpr.hs 89.3 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1
2
3
4
5
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998
-}

6
{-# LANGUAGE CPP, DeriveDataTypeable, ScopedTypeVariables #-}
7
8
9
10
11
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
                                      -- in module PlaceHolder
{-# LANGUAGE ConstraintKinds #-}
12
{-# LANGUAGE ExistentialQuantification #-}
13
14

-- | Abstract Haskell syntax for expressions.
15
16
module HsExpr where

17
#include "HsVersions.h"
18
19

-- friends:
20
21
22
import HsDecls
import HsPat
import HsLit
23
import PlaceHolder ( PostTc,PostRn,DataId )
24
25
import HsTypes
import HsBinds
26
27

-- others:
28
import TcEvidence
29
import CoreSyn
30
import Var
31
import DynFlags ( gopt, GeneralFlag(Opt_PrintExplicitCoercions) )
32
import Name
33
import NameSet
34
import RdrName  ( GlobalRdrEnv )
35
import BasicTypes
Matthew Pickering's avatar
Matthew Pickering committed
36
import ConLike
37
import SrcLoc
38
import Util
39
import StaticFlags( opt_PprStyle_Debug )
Ian Lynagh's avatar
Ian Lynagh committed
40
import Outputable
41
import FastString
42
import Type
43
44
45

-- libraries:
import Data.Data hiding (Fixity)
Simon Marlow's avatar
Simon Marlow committed
46
import Data.Maybe (isNothing)
47

Austin Seipp's avatar
Austin Seipp committed
48
49
50
{-
************************************************************************
*                                                                      *
51
\subsection{Expressions proper}
Austin Seipp's avatar
Austin Seipp committed
52
53
54
*                                                                      *
************************************************************************
-}
55

56
57
-- * Expressions proper

58
type LHsExpr id = Located (HsExpr id)
Alan Zimmerman's avatar
Alan Zimmerman committed
59
60
  -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
  --   in a list
61

62
63
  -- For details on above see note [Api annotations] in ApiAnnotation

64
-------------------------
65
66
-- | PostTcExpr is an evidence expression attached to the syntax tree by the
-- type checker (c.f. postTcType).
67
type PostTcExpr  = HsExpr Id
68
69
-- | We use a PostTcTable where there are a bunch of pieces of evidence, more
-- than is convenient to keep individually.
70
type PostTcTable = [(Name, PostTcExpr)]
71
72

noPostTcExpr :: PostTcExpr
73
noPostTcExpr = HsLit (HsString "" (fsLit "noPostTcExpr"))
74
75
76
77
78

noPostTcTable :: PostTcTable
noPostTcTable = []

-------------------------
79
-- | SyntaxExpr is like 'PostTcExpr', but it's filled in a little earlier,
Ian Lynagh's avatar
Ian Lynagh committed
80
-- by the renamer.  It's used for rebindable syntax.
81
82
83
--
-- E.g. @(>>=)@ is filled in before the renamer by the appropriate 'Name' for
--      @(>>=)@, and then instantiated by the type checker with its type args
84
--      etc
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
--
-- This should desugar to
--
-- > syn_res_wrap $ syn_expr (syn_arg_wraps[0] arg0)
-- >                         (syn_arg_wraps[1] arg1) ...
--
-- where the actual arguments come from elsewhere in the AST.
-- This could be defined using @PostRn@ and @PostTc@ and such, but it's
-- harder to get it all to work out that way. ('noSyntaxExpr' is hard to
-- write, for example.)
data SyntaxExpr id = SyntaxExpr { syn_expr      :: HsExpr id
                                , syn_arg_wraps :: [HsWrapper]
                                , syn_res_wrap  :: HsWrapper }
  deriving (Typeable)
deriving instance (DataId id) => Data (SyntaxExpr id)
100

101
102
103
104
-- | This is used for rebindable-syntax pieces that are too polymorphic
-- for tcSyntaxOp (trS_fmap and the mzip in ParStmt)
noExpr :: HsExpr id
noExpr = HsLit (HsString "" (fsLit "noExpr"))
105

Ian Lynagh's avatar
Ian Lynagh committed
106
107
noSyntaxExpr :: SyntaxExpr id -- Before renaming, and sometimes after,
                              -- (if the syntax slot makes no sense)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
noSyntaxExpr = SyntaxExpr { syn_expr      = HsLit (HsString "" (fsLit "noSyntaxExpr"))
                          , syn_arg_wraps = []
                          , syn_res_wrap  = WpHole }

-- | Make a 'SyntaxExpr Name' (the "rn" is because this is used in the
-- renamer), missing its HsWrappers.
mkRnSyntaxExpr :: Name -> SyntaxExpr Name
mkRnSyntaxExpr name = SyntaxExpr { syn_expr      = HsVar $ noLoc name
                                 , syn_arg_wraps = []
                                 , syn_res_wrap  = WpHole }
  -- don't care about filling in syn_arg_wraps because we're clearly
  -- not past the typechecker

instance OutputableBndr id => Outputable (SyntaxExpr id) where
  ppr (SyntaxExpr { syn_expr      = expr
                  , syn_arg_wraps = arg_wraps
                  , syn_res_wrap  = res_wrap })
    = sdocWithDynFlags $ \ dflags ->
      getPprStyle $ \s ->
      if debugStyle s || gopt Opt_PrintExplicitCoercions dflags
128
129
      then ppr expr <> braces (pprWithCommas ppr arg_wraps)
                    <> braces (ppr res_wrap)
130
131
132
      else ppr expr

type CmdSyntaxTable id = [(Name, HsExpr id)]
133
-- See Note [CmdSyntaxTable]
134

Austin Seipp's avatar
Austin Seipp committed
135
{-
136
137
138
139
140
Note [CmdSyntaxtable]
~~~~~~~~~~~~~~~~~~~~~
Used only for arrow-syntax stuff (HsCmdTop), the CmdSyntaxTable keeps
track of the methods needed for a Cmd.

141
* Before the renamer, this list is an empty list
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

* After the renamer, it takes the form @[(std_name, HsVar actual_name)]@
  For example, for the 'arr' method
   * normal case:            (GHC.Control.Arrow.arr, HsVar GHC.Control.Arrow.arr)
   * with rebindable syntax: (GHC.Control.Arrow.arr, arr_22)
             where @arr_22@ is whatever 'arr' is in scope

* After the type checker, it takes the form [(std_name, <expression>)]
  where <expression> is the evidence for the method.  This evidence is
  instantiated with the class, but is still polymorphic in everything
  else.  For example, in the case of 'arr', the evidence has type
         forall b c. (b->c) -> a b c
  where 'a' is the ambient type of the arrow.  This polymorphism is
  important because the desugarer uses the same evidence at multiple
  different types.

This is Less Cool than what we normally do for rebindable syntax, which is to
make fully-instantiated piece of evidence at every use site.  The Cmd way
is Less Cool because
  * The renamer has to predict which methods are needed.
    See the tedious RnExpr.methodNamesCmd.

  * The desugarer has to know the polymorphic type of the instantiated
    method. This is checked by Inst.tcSyntaxName, but is less flexible
    than the rest of rebindable syntax, where the type is less
    pre-ordained.  (And this flexibility is useful; for example we can
    typecheck do-notation with (>>=) :: m1 a -> (a -> m2 b) -> m2 b.)
Austin Seipp's avatar
Austin Seipp committed
169
-}
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-- | An unbound variable; used for treating out-of-scope variables as
-- expression holes
data UnboundVar
  = OutOfScope OccName GlobalRdrEnv  -- ^ An (unqualified) out-of-scope
                                     -- variable, together with the GlobalRdrEnv
                                     -- with respect to which it is unbound

                                     -- See Note [OutOfScope and GlobalRdrEnv]

  | TrueExprHole OccName             -- ^ A "true" expression hole (_ or _x)

  deriving (Data, Typeable)

instance Outputable UnboundVar where
    ppr = ppr . unboundVarOcc

unboundVarOcc :: UnboundVar -> OccName
unboundVarOcc (OutOfScope occ _) = occ
unboundVarOcc (TrueExprHole occ) = occ

{-
Note [OutOfScope and GlobalRdrEnv]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To understand why we bundle a GlobalRdrEnv with an out-of-scope variable,
consider the following module:

    module A where

    foo :: ()
    foo = bar

    bat :: [Double]
    bat = [1.2, 3.4]

    $(return [])

    bar = ()
    bad = False

When A is compiled, the renamer determines that `bar` is not in scope in the
declaration of `foo` (since `bar` is declared in the following inter-splice
group).  Once it has finished typechecking the entire module, the typechecker
then generates the associated error message, which specifies both the type of
`bar` and a list of possible in-scope alternatives:

    A.hs:6:7: error:
        • Variable not in scope: bar :: ()
        • ‘bar’ (line 13) is not in scope before the splice on line 11
          Perhaps you meant ‘bat’ (line 9)

When it calls RnEnv.unknownNameSuggestions to identify these alternatives, the
typechecker must provide a GlobalRdrEnv.  If it provided the current one, which
contains top-level declarations for the entire module, the error message would
incorrectly suggest the out-of-scope `bar` and `bad` as possible alternatives
for `bar` (see Trac #11680).  Instead, the typechecker must use the same
GlobalRdrEnv the renamer used when it determined that `bar` is out-of-scope.

To obtain this GlobalRdrEnv, can the typechecker simply use the out-of-scope
`bar`'s location to either reconstruct it (from the current GlobalRdrEnv) or to
look it up in some global store?  Unfortunately, no.  The problem is that
location information is not always sufficient for this task.  This is most
apparent when dealing with the TH function addTopDecls, which adds its
declarations to the FOLLOWING inter-splice group.  Consider these declarations:

    ex9 = cat               -- cat is NOT in scope here

    $(do -------------------------------------------------------------
        ds <- [d| f = cab   -- cat and cap are both in scope here
                  cat = ()
                |]
        addTopDecls ds
        [d| g = cab         -- only cap is in scope here
            cap = True
          |])

    ex10 = cat              -- cat is NOT in scope here

    $(return []) -----------------------------------------------------

    ex11 = cat              -- cat is in scope

Here, both occurrences of `cab` are out-of-scope, and so the typechecker needs
the GlobalRdrEnvs which were used when they were renamed.  These GlobalRdrEnvs
are different (`cat` is present only in the GlobalRdrEnv for f's `cab'), but the
locations of the two `cab`s are the same (they are both created in the same
splice).  Thus, we must include some additional information with each `cab` to
allow the typechecker to obtain the correct GlobalRdrEnv.  Clearly, the simplest
information to use is the GlobalRdrEnv itself.
-}

261
-- | A Haskell expression.
262
data HsExpr id
263
  = HsVar     (Located id)   -- ^ Variable
264

265
266
                             -- See Note [Located RdrNames]

267
268
269
270
271
272
273
  | HsUnboundVar UnboundVar  -- ^ Unbound variable; also used for "holes"
                             --   (_ or _x).
                             -- Turned from HsVar to HsUnboundVar by the
                             --   renamer, when it finds an out-of-scope
                             --   variable or hole.
                             -- Turned into HsVar by type checker, to support
                             --   deferred type errors.
274

275
  | HsRecFld (AmbiguousFieldOcc id) -- ^ Variable pointing to record selector
Adam Gundry's avatar
Adam Gundry committed
276

Adam Gundry's avatar
Adam Gundry committed
277
278
  | HsOverLabel FastString   -- ^ Overloaded label (See Note [Overloaded labels]
                             --   in GHC.OverloadedLabels)
279
280
281
282
  | HsIPVar   HsIPName       -- ^ Implicit parameter
  | HsOverLit (HsOverLit id) -- ^ Overloaded literals

  | HsLit     HsLit          -- ^ Simple (non-overloaded) literals
283

284
  | HsLam     (MatchGroup id (LHsExpr id)) -- ^ Lambda abstraction. Currently always a single match
Alan Zimmerman's avatar
Alan Zimmerman committed
285
286
287
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
       --       'ApiAnnotation.AnnRarrow',
288

289
290
       -- For details on above see note [Api annotations] in ApiAnnotation

Simon Peyton Jones's avatar
Simon Peyton Jones committed
291
  | HsLamCase (MatchGroup id (LHsExpr id)) -- ^ Lambda-case
Alan Zimmerman's avatar
Alan Zimmerman committed
292
293
294
295
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
       --           'ApiAnnotation.AnnCase','ApiAnnotation.AnnOpen',
       --           'ApiAnnotation.AnnClose'
Alan Zimmerman's avatar
Alan Zimmerman committed
296

297
298
       -- For details on above see note [Api annotations] in ApiAnnotation

299
  | HsApp     (LHsExpr id) (LHsExpr id) -- ^ Application
300

301
302
303
304
305
306
307
308
309
310
  | HsAppType (LHsExpr id) (LHsWcType id) -- ^ Visible type application
       --
       -- Explicit type argument; e.g  f @Int x y
       -- NB: Has wildcards, but no implicit quantification
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnAt',

  | HsAppTypeOut (LHsExpr id) (LHsWcType Name) -- just for pretty-printing


311
  -- | Operator applications:
312
313
  -- NB Bracketed ops such as (+) come out as Vars.

314
315
316
  -- NB We need an expr for the operator in an OpApp/Section since
  -- the typechecker may need to apply the operator to a few types.

Ian Lynagh's avatar
Ian Lynagh committed
317
318
  | OpApp       (LHsExpr id)    -- left operand
                (LHsExpr id)    -- operator
319
                (PostRn id Fixity) -- Renamer adds fixity; bottom until then
Ian Lynagh's avatar
Ian Lynagh committed
320
321
                (LHsExpr id)    -- right operand

322
  -- | Negation operator. Contains the negated expression and the name
Alan Zimmerman's avatar
Alan Zimmerman committed
323
324
325
  -- of 'negate'
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnMinus'
326
327

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
328
329
330
  | NegApp      (LHsExpr id)
                (SyntaxExpr id)

Alan Zimmerman's avatar
Alan Zimmerman committed
331
332
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
  --             'ApiAnnotation.AnnClose' @')'@
333
334

  -- For details on above see note [Api annotations] in ApiAnnotation
335
  | HsPar       (LHsExpr id)    -- ^ Parenthesised expr; see Note [Parens in HsSyn]
Ian Lynagh's avatar
Ian Lynagh committed
336

337
  | SectionL    (LHsExpr id)    -- operand; see Note [Sections in HsSyn]
Ian Lynagh's avatar
Ian Lynagh committed
338
                (LHsExpr id)    -- operator
339
  | SectionR    (LHsExpr id)    -- operator; see Note [Sections in HsSyn]
Ian Lynagh's avatar
Ian Lynagh committed
340
341
                (LHsExpr id)    -- operand

342
  -- | Used for explicit tuples and sections thereof
Alan Zimmerman's avatar
Alan Zimmerman committed
343
344
345
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnClose'
346
347

  -- For details on above see note [Api annotations] in ApiAnnotation
348
349
  | ExplicitTuple
        [LHsTupArg id]
350
351
        Boxity

Alan Zimmerman's avatar
Alan Zimmerman committed
352
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnCase',
Alan Zimmerman's avatar
Alan Zimmerman committed
353
354
  --       'ApiAnnotation.AnnOf','ApiAnnotation.AnnOpen' @'{'@,
  --       'ApiAnnotation.AnnClose' @'}'@
355
356

  -- For details on above see note [Api annotations] in ApiAnnotation
Ian Lynagh's avatar
Ian Lynagh committed
357
  | HsCase      (LHsExpr id)
358
                (MatchGroup id (LHsExpr id))
Ian Lynagh's avatar
Ian Lynagh committed
359

Alan Zimmerman's avatar
Alan Zimmerman committed
360
361
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf',
  --       'ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
362
  --       'ApiAnnotation.AnnThen','ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
363
  --       'ApiAnnotation.AnnElse',
364
365

  -- For details on above see note [Api annotations] in ApiAnnotation
366
  | HsIf        (Maybe (SyntaxExpr id)) -- cond function
367
368
                                        -- Nothing => use the built-in 'if'
                                        -- See Note [Rebindable if]
369
                (LHsExpr id)    --  predicate
Ian Lynagh's avatar
Ian Lynagh committed
370
371
372
                (LHsExpr id)    --  then part
                (LHsExpr id)    --  else part

373
  -- | Multi-way if
Alan Zimmerman's avatar
Alan Zimmerman committed
374
375
376
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf'
  --       'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
377
378

  -- For details on above see note [Api annotations] in ApiAnnotation
379
  | HsMultiIf   (PostTc id Type) [LGRHS id (LHsExpr id)]
380

381
  -- | let(rec)
Alan Zimmerman's avatar
Alan Zimmerman committed
382
383
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLet',
Alan Zimmerman's avatar
Alan Zimmerman committed
384
385
  --       'ApiAnnotation.AnnOpen' @'{'@,
  --       'ApiAnnotation.AnnClose' @'}'@,'ApiAnnotation.AnnIn'
386
387

  -- For details on above see note [Api annotations] in ApiAnnotation
388
  | HsLet       (Located (HsLocalBinds id))
Ian Lynagh's avatar
Ian Lynagh committed
389
390
                (LHsExpr  id)

Alan Zimmerman's avatar
Alan Zimmerman committed
391
392
393
394
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDo',
  --             'ApiAnnotation.AnnOpen', 'ApiAnnotation.AnnSemi',
  --             'ApiAnnotation.AnnVbar',
  --             'ApiAnnotation.AnnClose'
395
396

  -- For details on above see note [Api annotations] in ApiAnnotation
397
398
399
400
401
  | HsDo        (HsStmtContext Name)     -- The parameterisation is unimportant
                                         -- because in this context we never use
                                         -- the PatGuard or ParStmt variant
                (Located [ExprLStmt id]) -- "do":one or more stmts
                (PostTc id Type)         -- Type of the whole expression
Ian Lynagh's avatar
Ian Lynagh committed
402

403
  -- | Syntactic list: [a,b,c,...]
Alan Zimmerman's avatar
Alan Zimmerman committed
404
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
405
406
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
  --              'ApiAnnotation.AnnClose' @']'@
407
408

  -- For details on above see note [Api annotations] in ApiAnnotation
409
410
  | ExplicitList
                (PostTc id Type)        -- Gives type of components of list
411
                (Maybe (SyntaxExpr id)) -- For OverloadedLists, the fromListN witness
Ian Lynagh's avatar
Ian Lynagh committed
412
413
                [LHsExpr id]

414
  -- | Syntactic parallel array: [:e1, ..., en:]
Alan Zimmerman's avatar
Alan Zimmerman committed
415
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
416
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
Alan Zimmerman's avatar
Alan Zimmerman committed
417
418
  --              'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnComma',
  --              'ApiAnnotation.AnnVbar'
Alan Zimmerman's avatar
Alan Zimmerman committed
419
  --              'ApiAnnotation.AnnClose' @':]'@
420
421

  -- For details on above see note [Api annotations] in ApiAnnotation
422
423
  | ExplicitPArr
                (PostTc id Type)   -- type of elements of the parallel array
Ian Lynagh's avatar
Ian Lynagh committed
424
425
                [LHsExpr id]

426
  -- | Record construction
Alan Zimmerman's avatar
Alan Zimmerman committed
427
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
428
429
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
  --         'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
430
431

  -- For details on above see note [Api annotations] in ApiAnnotation
432
433
434
435
436
437
  | RecordCon
      { rcon_con_name :: Located id         -- The constructor name;
                                            --  not used after type checking
      , rcon_con_like :: PostTc id ConLike  -- The data constructor or pattern synonym
      , rcon_con_expr :: PostTcExpr         -- Instantiated constructor function
      , rcon_flds     :: HsRecordBinds id } -- The fields
Ian Lynagh's avatar
Ian Lynagh committed
438

439
  -- | Record update
Alan Zimmerman's avatar
Alan Zimmerman committed
440
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
441
442
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
  --         'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
443
444

  -- For details on above see note [Api annotations] in ApiAnnotation
445
446
447
448
  | RecordUpd
      { rupd_expr :: LHsExpr id
      , rupd_flds :: [LHsRecUpdField id]
      , rupd_cons :: PostTc id [ConLike]
449
450
451
                -- Filled in by the type checker to the
                -- _non-empty_ list of DataCons that have
                -- all the upd'd fields
452
453
454
455
456
457
458

      , rupd_in_tys  :: PostTc id [Type]  -- Argument types of *input* record type
      , rupd_out_tys :: PostTc id [Type]  --              and  *output* record type
                                          -- The original type can be reconstructed
                                          -- with conLikeResTy
      , rupd_wrap :: PostTc id HsWrapper  -- See note [Record Update HsWrapper]
      }
Ian Lynagh's avatar
Ian Lynagh committed
459
460
461
  -- For a type family, the arg types are of the *instance* tycon,
  -- not the family tycon

Alan Zimmerman's avatar
Alan Zimmerman committed
462
463
464
  -- | Expression with an explicit type signature. @e :: type@
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
465
466

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
467
  | ExprWithTySig
468
                (LHsExpr id)
469
                (LHsSigWcType id)
Ian Lynagh's avatar
Ian Lynagh committed
470

471
  | ExprWithTySigOut              -- Post typechecking
Ian Lynagh's avatar
Ian Lynagh committed
472
                (LHsExpr id)
473
474
475
                (LHsSigWcType Name)  -- Retain the signature,
                                     -- as HsSigType Name, for
                                     -- round-tripping purposes
Ian Lynagh's avatar
Ian Lynagh committed
476

477
  -- | Arithmetic sequence
Alan Zimmerman's avatar
Alan Zimmerman committed
478
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
479
480
481
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
  --              'ApiAnnotation.AnnComma','ApiAnnotation.AnnDotdot',
  --              'ApiAnnotation.AnnClose' @']'@
482
483

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
484
  | ArithSeq
Ian Lynagh's avatar
Ian Lynagh committed
485
                PostTcExpr
486
                (Maybe (SyntaxExpr id))   -- For OverloadedLists, the fromList witness
Ian Lynagh's avatar
Ian Lynagh committed
487
488
                (ArithSeqInfo id)

489
  -- | Arithmetic sequence for parallel array
Alan Zimmerman's avatar
Alan Zimmerman committed
490
491
492
493
494
495
496
  --
  -- > [:e1..e2:] or [:e1, e2..e3:]
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
  --              'ApiAnnotation.AnnComma','ApiAnnotation.AnnDotdot',
  --              'ApiAnnotation.AnnVbar',
  --              'ApiAnnotation.AnnClose' @':]'@
497
498

  -- For details on above see note [Api annotations] in ApiAnnotation
Austin Seipp's avatar
Austin Seipp committed
499
  | PArrSeq
Alan Zimmerman's avatar
Alan Zimmerman committed
500
                PostTcExpr
Ian Lynagh's avatar
Ian Lynagh committed
501
502
                (ArithSeqInfo id)

Alan Zimmerman's avatar
Alan Zimmerman committed
503
504
505
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{-\# SCC'@,
  --             'ApiAnnotation.AnnVal' or 'ApiAnnotation.AnnValStr',
  --              'ApiAnnotation.AnnClose' @'\#-}'@
506
507

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
508
  | HsSCC       SourceText            -- Note [Pragma source text] in BasicTypes
509
510
                StringLiteral         -- "set cost centre" SCC pragma
                (LHsExpr id)          -- expr whose cost is to be measured
Alan Zimmerman's avatar
Alan Zimmerman committed
511
512
513

  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{-\# CORE'@,
  --             'ApiAnnotation.AnnVal', 'ApiAnnotation.AnnClose' @'\#-}'@
514
515

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
516
  | HsCoreAnn   SourceText            -- Note [Pragma source text] in BasicTypes
517
                StringLiteral         -- hdaume: core annotation
Ian Lynagh's avatar
Ian Lynagh committed
518
519
                (LHsExpr id)

520
  -----------------------------------------------------------
521
  -- MetaHaskell Extensions
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
522

Alan Zimmerman's avatar
Alan Zimmerman committed
523
524
525
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
  --         'ApiAnnotation.AnnClose'
526
527

  -- For details on above see note [Api annotations] in ApiAnnotation
528
  | HsBracket    (HsBracket id)
529

530
531
532
533
534
    -- See Note [Pending Splices]
  | HsRnBracketOut
      (HsBracket Name)     -- Output of the renamer is the *original* renamed
                           -- expression, plus
      [PendingRnSplice]    -- _renamed_ splices to be type checked
gmainland's avatar
gmainland committed
535

536
537
538
539
540
  | HsTcBracketOut
      (HsBracket Name)     -- Output of the type checker is the *original*
                           -- renamed expression, plus
      [PendingTcSplice]    -- _typechecked_ splices to be
                           -- pasted back in by the desugarer
541

Alan Zimmerman's avatar
Alan Zimmerman committed
542
543
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnClose'
544
545

  -- For details on above see note [Api annotations] in ApiAnnotation
546
  | HsSpliceE  (HsSplice id)
547

548
549
550
  -----------------------------------------------------------
  -- Arrow notation extension

551
  -- | @proc@ notation for Arrows
Alan Zimmerman's avatar
Alan Zimmerman committed
552
553
554
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnProc',
  --          'ApiAnnotation.AnnRarrow'
555
556

  -- For details on above see note [Api annotations] in ApiAnnotation
Ian Lynagh's avatar
Ian Lynagh committed
557
558
559
  | HsProc      (LPat id)               -- arrow abstraction, proc
                (LHsCmdTop id)          -- body of the abstraction
                                        -- always has an empty stack
560

Facundo Domínguez's avatar
Facundo Domínguez committed
561
562
  ---------------------------------------
  -- static pointers extension
Alan Zimmerman's avatar
Alan Zimmerman committed
563
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnStatic',
564
565

  -- For details on above see note [Api annotations] in ApiAnnotation
566
567
  | HsStatic (PostRn id NameSet) -- Free variables of the body
             (LHsExpr id)        -- Body
Facundo Domínguez's avatar
Facundo Domínguez committed
568

569
570
  ---------------------------------------
  -- The following are commands, not expressions proper
571
  -- They are only used in the parsing stage and are removed
572
  --    immediately in parser.RdrHsSyn.checkCommand
Alan Zimmerman's avatar
Alan Zimmerman committed
573
574
575
576

  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.Annlarrowtail',
  --          'ApiAnnotation.Annrarrowtail','ApiAnnotation.AnnLarrowtail',
  --          'ApiAnnotation.AnnRarrowtail'
577
578

  -- For details on above see note [Api annotations] in ApiAnnotation
579
580
581
582
583
584
585
586
587
  | HsArrApp             -- Arrow tail, or arrow application (f -< arg)
        (LHsExpr id)     -- arrow expression, f
        (LHsExpr id)     -- input expression, arg
        (PostTc id Type) -- type of the arrow expressions f,
                         -- of the form a t t', where arg :: t
        HsArrAppType     -- higher-order (-<<) or first-order (-<)
        Bool             -- True => right-to-left (f -< arg)
                         -- False => left-to-right (arg >- f)

Alan Zimmerman's avatar
Alan Zimmerman committed
588
589
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(|'@,
  --         'ApiAnnotation.AnnClose' @'|)'@
590
591

  -- For details on above see note [Api annotations] in ApiAnnotation
592
593
594
595
596
597
598
  | HsArrForm            -- Command formation,  (| e cmd1 .. cmdn |)
        (LHsExpr id)     -- the operator
                         -- after type-checking, a type abstraction to be
                         -- applied to the type of the local environment tuple
        (Maybe Fixity)   -- fixity (filled in by the renamer), for forms that
                         -- were converted from OpApp's by the renamer
        [LHsCmdTop id]   -- argument commands
599

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
600
601
602
  ---------------------------------------
  -- Haskell program coverage (Hpc) Support

Ian Lynagh's avatar
Ian Lynagh committed
603
  | HsTick
604
     (Tickish id)
Ian Lynagh's avatar
Ian Lynagh committed
605
     (LHsExpr id)                       -- sub-expression
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
606
607

  | HsBinTick
Ian Lynagh's avatar
Ian Lynagh committed
608
609
610
     Int                                -- module-local tick number for True
     Int                                -- module-local tick number for False
     (LHsExpr id)                       -- sub-expression
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
611

Alan Zimmerman's avatar
Alan Zimmerman committed
612
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
Alan Zimmerman's avatar
Alan Zimmerman committed
613
614
615
  --       'ApiAnnotation.AnnOpen' @'{-\# GENERATED'@,
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnColon','ApiAnnotation.AnnVal',
Alan Zimmerman's avatar
Alan Zimmerman committed
616
  --       'ApiAnnotation.AnnMinus',
Alan Zimmerman's avatar
Alan Zimmerman committed
617
618
619
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnColon',
  --       'ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnClose' @'\#-}'@
620
621

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
622
623
  | HsTickPragma                      -- A pragma introduced tick
     SourceText                       -- Note [Pragma source text] in BasicTypes
624
     (StringLiteral,(Int,Int),(Int,Int))
625
                                      -- external span for this tick
626
627
628
     ((SourceText,SourceText),(SourceText,SourceText))
        -- Source text for the four integers used in the span.
        -- See note [Pragma source text] in BasicTypes
Ian Lynagh's avatar
Ian Lynagh committed
629
     (LHsExpr id)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
630
631
632
633

  ---------------------------------------
  -- These constructors only appear temporarily in the parser.
  -- The renamer translates them into the Right Thing.
634

Ian Lynagh's avatar
Ian Lynagh committed
635
  | EWildPat                 -- wildcard
636

Alan Zimmerman's avatar
Alan Zimmerman committed
637
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnAt'
638
639

  -- For details on above see note [Api annotations] in ApiAnnotation
Ian Lynagh's avatar
Ian Lynagh committed
640
641
  | EAsPat      (Located id) -- as pattern
                (LHsExpr id)
642

Alan Zimmerman's avatar
Alan Zimmerman committed
643
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow'
644
645

  -- For details on above see note [Api annotations] in ApiAnnotation
Ian Lynagh's avatar
Ian Lynagh committed
646
647
  | EViewPat    (LHsExpr id) -- view pattern
                (LHsExpr id)
648

Alan Zimmerman's avatar
Alan Zimmerman committed
649
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
650
651

  -- For details on above see note [Api annotations] in ApiAnnotation
Ian Lynagh's avatar
Ian Lynagh committed
652
  | ELazyPat    (LHsExpr id) -- ~ pattern
653

654

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
655
656
  ---------------------------------------
  -- Finally, HsWrap appears only in typechecker output
657

Ian Lynagh's avatar
Ian Lynagh committed
658
659
  |  HsWrap     HsWrapper    -- TRANSLATION
                (HsExpr id)
660

661
662
  deriving (Typeable)
deriving instance (DataId id) => Data (HsExpr id)
663

664
-- | HsTupArg is used for tuple sections
665
--  (,a,) is represented by  ExplicitTuple [Missing ty1, Present a, Missing ty3]
666
--  Which in turn stands for (\x:ty1 \y:ty2. (x,a,y))
667
type LHsTupArg id = Located (HsTupArg id)
Alan Zimmerman's avatar
Alan Zimmerman committed
668
-- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma'
669
670

-- For details on above see note [Api annotations] in ApiAnnotation
671
data HsTupArg id
672
673
674
675
  = Present (LHsExpr id)     -- ^ The argument
  | Missing (PostTc id Type) -- ^ The argument is missing, but this is its type
  deriving (Typeable)
deriving instance (DataId id) => Data (HsTupArg id)
676

677
678
679
tupArgPresent :: LHsTupArg id -> Bool
tupArgPresent (L _ (Present {})) = True
tupArgPresent (L _ (Missing {})) = False
680

Austin Seipp's avatar
Austin Seipp committed
681
{-
682
683
684
685
686
687
688
Note [Parens in HsSyn]
~~~~~~~~~~~~~~~~~~~~~~
HsPar (and ParPat in patterns, HsParTy in types) is used as follows

  * Generally HsPar is optional; the pretty printer adds parens where
    necessary.  Eg (HsApp f (HsApp g x)) is fine, and prints 'f (g x)'

689
  * HsPars are pretty printed as '( .. )' regardless of whether
690
691
692
693
694
695
696
697
    or not they are strictly necssary

  * HsPars are respected when rearranging operator fixities.
    So   a * (b + c)  means what it says (where the parens are an HsPar)

Note [Sections in HsSyn]
~~~~~~~~~~~~~~~~~~~~~~~~
Sections should always appear wrapped in an HsPar, thus
698
699
         HsPar (SectionR ...)
The parser parses sections in a wider variety of situations
700
(See Note [Parsing sections]), but the renamer checks for those
701
parens.  This invariant makes pretty-printing easier; we don't need
702
703
a special case for adding the parens round sections.

704
705
706
707
708
709
Note [Rebindable if]
~~~~~~~~~~~~~~~~~~~~
The rebindable syntax for 'if' is a bit special, because when
rebindable syntax is *off* we do not want to treat
   (if c then t else e)
as if it was an application (ifThenElse c t e).  Why not?
710
Because we allow an 'if' to return *unboxed* results, thus
711
712
713
714
715
  if blah then 3# else 4#
whereas that would not be possible using a all to a polymorphic function
(because you can't call a polymorphic function at an unboxed type).

So we use Nothing to mean "use the old built-in typing rule".
Matthew Pickering's avatar
Matthew Pickering committed
716
717
718

Note [Record Update HsWrapper]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
719
720
721
722
There is a wrapper in RecordUpd which is used for the *required*
constraints for pattern synonyms. This wrapper is created in the
typechecking and is then directly used in the desugaring without
modification.
Matthew Pickering's avatar
Matthew Pickering committed
723
724

For example, if we have the record pattern synonym P,
725
726
  pattern P :: (Show a) => a -> Maybe a
  pattern P{x} = Just x
Matthew Pickering's avatar
Matthew Pickering committed
727

728
  foo = (Just True) { x = False }
Matthew Pickering's avatar
Matthew Pickering committed
729
then `foo` desugars to something like
730
731
732
733
  foo = case Just True of
          P x -> P False
hence we need to provide the correct dictionaries to P's matcher on
the RHS so that we can build the expression.
Matthew Pickering's avatar
Matthew Pickering committed
734

735
736
737
738
739
740
741
742
743
744
Note [Located RdrNames]
~~~~~~~~~~~~~~~~~~~~~~~
A number of syntax elements have seemingly redundant locations attached to them.
This is deliberate, to allow transformations making use of the API Annotations
to easily correlate a Located Name in the RenamedSource with a Located RdrName
in the ParsedSource.

There are unfortunately enough differences between the ParsedSource and the
RenamedSource that the API Annotations cannot be used directly with
RenamedSource, so this allows a simple mapping to be used based on the location.
Austin Seipp's avatar
Austin Seipp committed
745
-}
746

747
instance OutputableBndr id => Outputable (HsExpr id) where
748
    ppr expr = pprExpr expr
749

750
-----------------------
Ian Lynagh's avatar
Ian Lynagh committed
751
-- pprExpr, pprLExpr, pprBinds call pprDeeper;
752
753
-- the underscore versions do not
pprLExpr :: OutputableBndr id => LHsExpr id -> SDoc
754
755
756
pprLExpr (L _ e) = pprExpr e

pprExpr :: OutputableBndr id => HsExpr id -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
757
758
759
760
761
762
763
pprExpr e | isAtomicHsExpr e || isQuietHsExpr e =            ppr_expr e
          | otherwise                           = pprDeeper (ppr_expr e)

isQuietHsExpr :: HsExpr id -> Bool
-- Parentheses do display something, but it gives little info and
-- if we go deeper when we go inside them then we get ugly things
-- like (...)
764
isQuietHsExpr (HsPar _)          = True
Ian Lynagh's avatar
Ian Lynagh committed
765
-- applications don't display anything themselves
766
767
768
769
isQuietHsExpr (HsApp _ _)        = True
isQuietHsExpr (HsAppType _ _)    = True
isQuietHsExpr (HsAppTypeOut _ _) = True
isQuietHsExpr (OpApp _ _ _ _)    = True
Ian Lynagh's avatar
Ian Lynagh committed
770
isQuietHsExpr _ = False
771

Ian Lynagh's avatar
Ian Lynagh committed
772
773
pprBinds :: (OutputableBndr idL, OutputableBndr idR)
         => HsLocalBindsLR idL idR -> SDoc
774
pprBinds b = pprDeeper (ppr b)
775

776
-----------------------
777
778
ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
ppr_lexpr e = ppr_expr (unLoc e)
779

780
ppr_expr :: forall id. OutputableBndr id => HsExpr id -> SDoc
781
ppr_expr (HsVar (L _ v))  = pprPrefixOcc v
782
ppr_expr (HsUnboundVar uv)= pprPrefixOcc (unboundVarOcc uv)
783
ppr_expr (HsIPVar v)      = ppr v
Adam Gundry's avatar
Adam Gundry committed
784
ppr_expr (HsOverLabel l)  = char '#' <> ppr l
785
786
787
ppr_expr (HsLit lit)      = ppr lit
ppr_expr (HsOverLit lit)  = ppr lit
ppr_expr (HsPar e)        = parens (ppr_lexpr e)
788

789
ppr_expr (HsCoreAnn _ (StringLiteral _ s) e)
790
  = vcat [text "HsCoreAnn" <+> ftext s, ppr_lexpr e]
791

792
793
794
ppr_expr e@(HsApp {})        = ppr_apps e []
ppr_expr e@(HsAppType {})    = ppr_apps e []
ppr_expr e@(HsAppTypeOut {}) = ppr_apps e []
795

Ian Lynagh's avatar
Ian Lynagh committed
796
ppr_expr (OpApp e1 op _ e2)
797
  = case unLoc op of
798
      HsVar (L _ v) -> pp_infixly v
799
      HsRecFld f    -> pp_infixly f
800
      _             -> pp_prefixly
801
  where
Ian Lynagh's avatar
Ian Lynagh committed
802
    pp_e1 = pprDebugParendExpr e1   -- In debug mode, add parens
803
    pp_e2 = pprDebugParendExpr e2   -- to make precedence clear
804
805

    pp_prefixly
806
      = hang (ppr op) 2 (sep [pp_e1, pp_e2])
807
808

    pp_infixly v
809
      = sep [pp_e1, sep [pprInfixOcc v, nest 2 pp_e2]]
810

811
ppr_expr (NegApp e _) = char '-' <+> pprDebugParendExpr e
812

813
ppr_expr (SectionL expr op)
814
  = case unLoc op of
815
816
      HsVar (L _ v) -> pp_infixly v
      _             -> pp_prefixly
817
  where
818
    pp_expr = pprDebugParendExpr expr
819

820
    pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
821
                       4 (hsep [pp_expr, text "x_ )"])
822
    pp_infixly v = (sep [pp_expr, pprInfixOcc v])
823

824
ppr_expr (SectionR op expr)
825
  = case unLoc op of
826
827
      HsVar (L _ v) -> pp_infixly v
      _             -> pp_prefixly
828
  where
829
    pp_expr = pprDebugParendExpr expr
830

831
    pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, text "x_"])
832
                       4 (pp_expr <> rparen)
833
    pp_infixly v = sep [pprInfixOcc v, pp_expr]
834

835
ppr_expr (ExplicitTuple exprs boxity)
836
  = tupleParens (boxityTupleSort boxity) (fcat (ppr_tup_args $ map unLoc exprs))
837
838
839
  where
    ppr_tup_args []               = []
    ppr_tup_args (Present e : es) = (ppr_lexpr e <> punc es) : ppr_tup_args es
840
    ppr_tup_args (Missing _ : es) = punc es : ppr_tup_args es
841
842
843
844
845

    punc (Present {} : _) = comma <> space
    punc (Missing {} : _) = comma
    punc []               = empty

846
847
ppr_expr (HsLam matches)
  = pprMatches (LambdaExpr :: HsMatchContext id) matches
848

Simon Peyton Jones's avatar
Simon Peyton Jones committed
849
ppr_expr (HsLamCase matches)
850
  = sep [ sep [text "\\case {"],
851
852
          nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ]

853
ppr_expr (HsCase expr matches)
854
  = sep [ sep [text "case", nest 4 (ppr expr), ptext (sLit "of {")],
855
          nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ]
856

857
ppr_expr (HsIf _ e1 e2 e3)
858
  = sep [hsep [text "if", nest 2 (ppr e1), ptext (sLit "then")],
Ian Lynagh's avatar
Ian Lynagh committed
859
         nest 4 (ppr e2),
860
         text "else",
Ian Lynagh's avatar
Ian Lynagh committed
861
         nest 4 (ppr e3)]
862

863
ppr_expr (HsMultiIf _ alts)
864
  = sep $ text "if" : map ppr_alt alts
865
  where ppr_alt (L _ (GRHS guards expr)) =
866
          sep [ vbar <+> interpp'SP guards
867
              , text "->" <+> pprDeeper (ppr expr) ]
868

869
-- special case: let ... in let ...
870
ppr_expr (HsLet (L _ binds) expr@(L _ (HsLet _ _)))
871
  = sep [hang (text "let") 2 (hsep [pprBinds binds, ptext (sLit "in")]),
Ian Lynagh's avatar
Ian Lynagh committed
872
         ppr_lexpr expr]
873

874
ppr_expr (HsLet (L _ binds) expr)
875
876
  = sep [hang (text "let") 2 (pprBinds binds),
         hang (text "in")  2 (ppr expr)]
877

878
ppr_expr (HsDo do_or_list_comp (L _ stmts) _) = pprDo do_or_list_comp stmts
879

880
ppr_expr (ExplicitList _ _ exprs)
881
  = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
882

chak's avatar
chak committed
883
ppr_expr (ExplicitPArr _ exprs)
884
  = paBrackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
chak's avatar
chak committed
885

886
ppr_expr (RecordCon { rcon_con_name = con_id, rcon_flds = rbinds })
887
  = hang (ppr con_id) 2 (ppr rbinds)
888

889
890
ppr_expr (RecordUpd { rupd_expr = L _ aexp, rupd_flds = rbinds })
  = hang (pprParendExpr aexp) 2 (braces (fsep (punctuate comma (map ppr rbinds))))
891

892
ppr_expr (ExprWithTySig expr sig)
893
  = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
Ian Lynagh's avatar
Ian Lynagh committed
894
         4 (ppr sig)
895
896
ppr_expr (ExprWithTySigOut expr sig)
  = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
Ian Lynagh's avatar
Ian Lynagh committed
897
         4 (ppr sig)
898

899
ppr_expr (ArithSeq _ _ info) = brackets (ppr info)
900
ppr_expr (PArrSeq  _ info) = paBrackets (ppr info)
chak's avatar
chak committed
901

Ian Lynagh's avatar
Ian Lynagh committed
902
ppr_expr EWildPat       = char '_'
903
904
ppr_expr (ELazyPat e)   = char '~' <> pprParendLExpr e
ppr_expr (EAsPat v e)   = ppr v <> char '@' <> pprParendLExpr e
905
ppr_expr (EViewPat p e) = ppr p <+> text "->" <+> ppr e
906

907
ppr_expr (HsSCC _ (StringLiteral _ lbl) expr)
908
  = sep [ text "{-# SCC" <+> doubleQuotes (ftext lbl) <+> ptext (sLit "#-}"),
909
910
911
912
913
          pprParendLExpr expr ]

ppr_expr (HsWrap co_fn e)
  = pprHsWrapper co_fn (\parens -> if parens then pprParendExpr e
                                             else pprExpr       e)
914

915
ppr_expr (HsSpliceE s)         = pprSplice s
916
ppr_expr (HsBracket b)         = pprHsBracket b
917
ppr_expr (HsRnBracketOut e []) = ppr e
918
ppr_expr (HsRnBracketOut e ps) = ppr e $$ text "pending(rn)" <+> ppr ps
919
ppr_expr (HsTcBracketOut e []) = ppr e
920
ppr_expr (HsTcBracketOut e ps) = ppr e $$ text "pending(tc)" <+> ppr ps
921

922
ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
923
  = hsep [text "proc", ppr pat, ptext (sLit "->"), ppr cmd]
924

925
ppr_expr (HsStatic _ e)
926
  = hsep [text "static", pprParendLExpr e]
Facundo Domínguez's avatar
Facundo Domínguez committed
927

928
ppr_expr (HsTick tickish exp)
929
  = pprTicks (ppr exp) $
Peter Wortmann's avatar
Peter Wortmann committed
930
    ppr tickish <+> ppr_lexpr exp
andy@galois.com's avatar
andy@galois.com committed
931
ppr_expr (HsBinTick tickIdTrue tickIdFalse exp)
932
  = pprTicks (ppr exp) $
933
    hcat [text "bintick<",
Ian Lynagh's avatar
Ian Lynagh committed
934
          ppr tickIdTrue,
935
          text ",",
Ian Lynagh's avatar
Ian Lynagh committed
936
          ppr tickIdFalse,
937
938
          text ">(",
          ppr exp, text ")"]
939
ppr_expr (HsTickPragma _ externalSrcLoc _ exp)
940
  = pprTicks (ppr exp) $
941
    hcat [text "tickpragma<",
942
          pprExternalSrcLoc externalSrcLoc,
943
          text ">(",
Ian Lynagh's avatar
Ian Lynagh committed
944
          ppr exp,