Expr.hs 109 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
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
9
{-# LANGUAGE FlexibleInstances #-}
10
{-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
11
                                      -- in module GHC.Hs.PlaceHolder
12
{-# LANGUAGE ConstraintKinds #-}
13
{-# LANGUAGE ExistentialQuantification #-}
14
{-# LANGUAGE DeriveFunctor #-}
15
{-# LANGUAGE TypeFamilies #-}
16
{-# LANGUAGE ViewPatterns #-}
17 18

-- | Abstract Haskell syntax for expressions.
19
module GHC.Hs.Expr where
20

21
#include "HsVersions.h"
22 23

-- friends:
24 25
import GhcPrelude

26 27 28 29 30 31 32
import GHC.Hs.Decls
import GHC.Hs.Pat
import GHC.Hs.Lit
import GHC.Hs.PlaceHolder ( NameOrRdrName )
import GHC.Hs.Extension
import GHC.Hs.Types
import GHC.Hs.Binds
33 34

-- others:
35
import TcEvidence
36
import CoreSyn
37
import DynFlags ( gopt, GeneralFlag(Opt_PrintExplicitCoercions) )
38
import Name
39
import NameSet
40
import BasicTypes
Matthew Pickering's avatar
Matthew Pickering committed
41
import ConLike
42
import SrcLoc
43
import Util
Ian Lynagh's avatar
Ian Lynagh committed
44
import Outputable
45
import FastString
46
import Type
47
import TysWiredIn (mkTupleStr)
48 49
import TcType (TcType)
import {-# SOURCE #-} TcRnTypes (TcLclEnv)
50 51

-- libraries:
52
import Data.Data hiding (Fixity(..))
53
import qualified Data.Data as Data (Fixity(..))
Simon Marlow's avatar
Simon Marlow committed
54
import Data.Maybe (isNothing)
55

56 57 58
import GHCi.RemoteTypes ( ForeignRef )
import qualified Language.Haskell.TH as TH (Q)

Austin Seipp's avatar
Austin Seipp committed
59 60 61
{-
************************************************************************
*                                                                      *
62
\subsection{Expressions proper}
Austin Seipp's avatar
Austin Seipp committed
63 64 65
*                                                                      *
************************************************************************
-}
66

67 68
-- * Expressions proper

69
-- | Located Haskell Expression
70
type LHsExpr p = Located (HsExpr p)
Alan Zimmerman's avatar
Alan Zimmerman committed
71 72
  -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
  --   in a list
73

74 75
  -- For details on above see note [Api annotations] in ApiAnnotation

76
-------------------------
77 78 79
-- | Post-Type checking Expression
--
-- PostTcExpr is an evidence expression attached to the syntax tree by the
80
-- type checker (c.f. postTcType).
81
type PostTcExpr  = HsExpr GhcTc
82 83 84 85

-- | Post-Type checking Table
--
-- We use a PostTcTable where there are a bunch of pieces of evidence, more
86
-- than is convenient to keep individually.
87
type PostTcTable = [(Name, PostTcExpr)]
88 89

-------------------------
90 91 92
-- | Syntax Expression
--
-- SyntaxExpr is like 'PostTcExpr', but it's filled in a little earlier,
Ian Lynagh's avatar
Ian Lynagh committed
93
-- by the renamer.  It's used for rebindable syntax.
94 95 96
--
-- E.g. @(>>=)@ is filled in before the renamer by the appropriate 'Name' for
--      @(>>=)@, and then instantiated by the type checker with its type args
97
--      etc
98 99 100 101 102 103 104
--
-- 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.
105
-- This could be defined using @GhcPass p@ and such, but it's
106 107
-- harder to get it all to work out that way. ('noSyntaxExpr' is hard to
-- write, for example.)
108 109 110
data SyntaxExpr p = SyntaxExpr { syn_expr      :: HsExpr p
                               , syn_arg_wraps :: [HsWrapper]
                               , syn_res_wrap  :: HsWrapper }
111

112 113
-- | This is used for rebindable-syntax pieces that are too polymorphic
-- for tcSyntaxOp (trS_fmap and the mzip in ParStmt)
114
noExpr :: HsExpr (GhcPass p)
115
noExpr = HsLit noExtField (HsString (SourceText  "noExpr") (fsLit "noExpr"))
116

117
noSyntaxExpr :: SyntaxExpr (GhcPass p)
118
                              -- Before renaming, and sometimes after,
Ian Lynagh's avatar
Ian Lynagh committed
119
                              -- (if the syntax slot makes no sense)
120 121 122
noSyntaxExpr = SyntaxExpr { syn_expr      = HsLit noExtField
                                                  (HsString NoSourceText
                                                  (fsLit "noSyntaxExpr"))
123 124 125
                          , syn_arg_wraps = []
                          , syn_res_wrap  = WpHole }

126 127 128 129 130 131
-- | Make a 'SyntaxExpr (HsExpr _)', missing its HsWrappers.
mkSyntaxExpr :: HsExpr (GhcPass p) -> SyntaxExpr (GhcPass p)
mkSyntaxExpr expr = SyntaxExpr { syn_expr      = expr
                               , syn_arg_wraps = []
                               , syn_res_wrap  = WpHole }

132 133
-- | Make a 'SyntaxExpr Name' (the "rn" is because this is used in the
-- renamer), missing its HsWrappers.
134
mkRnSyntaxExpr :: Name -> SyntaxExpr GhcRn
135
mkRnSyntaxExpr name = mkSyntaxExpr $ HsVar noExtField $ noLoc name
136 137 138
  -- don't care about filling in syn_arg_wraps because we're clearly
  -- not past the typechecker

139 140
instance OutputableBndrId p
       => Outputable (SyntaxExpr (GhcPass p)) where
141 142 143 144 145 146
  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
147 148
      then ppr expr <> braces (pprWithCommas ppr arg_wraps)
                    <> braces (ppr res_wrap)
149 150
      else ppr expr

151
-- | Command Syntax Table (for Arrow syntax)
152
type CmdSyntaxTable p = [(Name, HsExpr p)]
153
-- See Note [CmdSyntaxTable]
154

Austin Seipp's avatar
Austin Seipp committed
155
{-
156 157 158 159 160
Note [CmdSyntaxtable]
~~~~~~~~~~~~~~~~~~~~~
Used only for arrow-syntax stuff (HsCmdTop), the CmdSyntaxTable keeps
track of the methods needed for a Cmd.

161
* Before the renamer, this list is an empty list
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

* 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
189
-}
190

191
-- | A Haskell expression.
192
data HsExpr p
193 194
  = HsVar     (XVar p)
              (Located (IdP p)) -- ^ Variable
195

196 197
                             -- See Note [Located RdrNames]

198
  | HsUnboundVar (XUnboundVar p)
199
                 OccName     -- ^ Unbound variable; also used for "holes"
200 201 202 203 204 205
                             --   (_ 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.
206

207 208
  | HsConLikeOut (XConLikeOut p)
                 ConLike     -- ^ After typechecker only; must be different
209 210
                             -- HsVar for pretty printing

211 212
  | HsRecFld  (XRecFld p)
              (AmbiguousFieldOcc p) -- ^ Variable pointing to record selector
213
                                    -- Not in use after typechecking
214

215 216
  | HsOverLabel (XOverLabel p)
                (Maybe (IdP p)) FastString
217 218 219 220
     -- ^ Overloaded label (Note [Overloaded labels] in GHC.OverloadedLabels)
     --   @Just id@ means @RebindableSyntax@ is in use, and gives the id of the
     --   in-scope 'fromLabel'.
     --   NB: Not in use after typechecking
221

222 223 224 225
  | HsIPVar   (XIPVar p)
              HsIPName   -- ^ Implicit parameter (not in use after typechecking)
  | HsOverLit (XOverLitE p)
              (HsOverLit p)  -- ^ Overloaded literals
226

227 228
  | HsLit     (XLitE p)
              (HsLit p)      -- ^ Simple (non-overloaded) literals
229

230 231
  | HsLam     (XLam p)
              (MatchGroup p (LHsExpr p))
232
                       -- ^ Lambda abstraction. Currently always a single match
Alan Zimmerman's avatar
Alan Zimmerman committed
233 234 235
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
       --       'ApiAnnotation.AnnRarrow',
236

237 238
       -- For details on above see note [Api annotations] in ApiAnnotation

239
  | HsLamCase (XLamCase p) (MatchGroup p (LHsExpr p)) -- ^ Lambda-case
Alan Zimmerman's avatar
Alan Zimmerman committed
240 241 242 243
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
       --           'ApiAnnotation.AnnCase','ApiAnnotation.AnnOpen',
       --           'ApiAnnotation.AnnClose'
Alan Zimmerman's avatar
Alan Zimmerman committed
244

245 246
       -- For details on above see note [Api annotations] in ApiAnnotation

247
  | HsApp     (XApp p) (LHsExpr p) (LHsExpr p) -- ^ Application
248

249
  | HsAppType (XAppTypeE p) (LHsExpr p) (LHsWcType (NoGhcTc p))  -- ^ Visible type application
250 251 252 253 254 255
       --
       -- Explicit type argument; e.g  f @Int x y
       -- NB: Has wildcards, but no implicit quantification
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnAt',

256
  -- | Operator applications:
257 258
  -- NB Bracketed ops such as (+) come out as Vars.

259 260 261
  -- 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.

262 263
  | OpApp       (XOpApp p)
                (LHsExpr p)       -- left operand
264 265
                (LHsExpr p)       -- operator
                (LHsExpr p)       -- right operand
Ian Lynagh's avatar
Ian Lynagh committed
266

267
  -- | Negation operator. Contains the negated expression and the name
Alan Zimmerman's avatar
Alan Zimmerman committed
268 269 270
  -- of 'negate'
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnMinus'
271 272

  -- For details on above see note [Api annotations] in ApiAnnotation
273 274
  | NegApp      (XNegApp p)
                (LHsExpr p)
275
                (SyntaxExpr p)
Alan Zimmerman's avatar
Alan Zimmerman committed
276

Alan Zimmerman's avatar
Alan Zimmerman committed
277 278
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
  --             'ApiAnnotation.AnnClose' @')'@
279 280

  -- For details on above see note [Api annotations] in ApiAnnotation
281 282
  | HsPar       (XPar p)
                (LHsExpr p)  -- ^ Parenthesised expr; see Note [Parens in HsSyn]
Ian Lynagh's avatar
Ian Lynagh committed
283

284 285
  | SectionL    (XSectionL p)
                (LHsExpr p)    -- operand; see Note [Sections in HsSyn]
286
                (LHsExpr p)    -- operator
287 288
  | SectionR    (XSectionR p)
                (LHsExpr p)    -- operator; see Note [Sections in HsSyn]
289
                (LHsExpr p)    -- operand
Ian Lynagh's avatar
Ian Lynagh committed
290

291
  -- | Used for explicit tuples and sections thereof
Alan Zimmerman's avatar
Alan Zimmerman committed
292 293 294
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnClose'
295 296

  -- For details on above see note [Api annotations] in ApiAnnotation
297
  -- Note [ExplicitTuple]
298
  | ExplicitTuple
299
        (XExplicitTuple p)
300
        [LHsTupArg p]
301 302
        Boxity

303 304 305 306 307 308 309
  -- | Used for unboxed sum types
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(#'@,
  --          'ApiAnnotation.AnnVbar', 'ApiAnnotation.AnnClose' @'#)'@,
  --
  --  There will be multiple 'ApiAnnotation.AnnVbar', (1 - alternative) before
  --  the expression, (arity - alternative) after it
310
  | ExplicitSum
311
          (XExplicitSum p)
312 313
          ConTag --  Alternative (one-based)
          Arity  --  Sum arity
314
          (LHsExpr p)
315

Alan Zimmerman's avatar
Alan Zimmerman committed
316
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnCase',
Alan Zimmerman's avatar
Alan Zimmerman committed
317 318
  --       'ApiAnnotation.AnnOf','ApiAnnotation.AnnOpen' @'{'@,
  --       'ApiAnnotation.AnnClose' @'}'@
319 320

  -- For details on above see note [Api annotations] in ApiAnnotation
321 322
  | HsCase      (XCase p)
                (LHsExpr p)
323
                (MatchGroup p (LHsExpr p))
Ian Lynagh's avatar
Ian Lynagh committed
324

Alan Zimmerman's avatar
Alan Zimmerman committed
325 326
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf',
  --       'ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
327
  --       'ApiAnnotation.AnnThen','ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
328
  --       'ApiAnnotation.AnnElse',
329 330

  -- For details on above see note [Api annotations] in ApiAnnotation
331 332
  | HsIf        (XIf p)
                (Maybe (SyntaxExpr p)) -- cond function
333 334
                                        -- Nothing => use the built-in 'if'
                                        -- See Note [Rebindable if]
335 336 337
                (LHsExpr p)    --  predicate
                (LHsExpr p)    --  then part
                (LHsExpr p)    --  else part
Ian Lynagh's avatar
Ian Lynagh committed
338

339
  -- | Multi-way if
Alan Zimmerman's avatar
Alan Zimmerman committed
340 341 342
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf'
  --       'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
343 344

  -- For details on above see note [Api annotations] in ApiAnnotation
345
  | HsMultiIf   (XMultiIf p) [LGRHS p (LHsExpr p)]
346

347
  -- | let(rec)
Alan Zimmerman's avatar
Alan Zimmerman committed
348 349
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLet',
Alan Zimmerman's avatar
Alan Zimmerman committed
350 351
  --       'ApiAnnotation.AnnOpen' @'{'@,
  --       'ApiAnnotation.AnnClose' @'}'@,'ApiAnnotation.AnnIn'
352 353

  -- For details on above see note [Api annotations] in ApiAnnotation
354 355
  | HsLet       (XLet p)
                (LHsLocalBinds p)
356
                (LHsExpr  p)
Ian Lynagh's avatar
Ian Lynagh committed
357

Alan Zimmerman's avatar
Alan Zimmerman committed
358 359 360 361
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDo',
  --             'ApiAnnotation.AnnOpen', 'ApiAnnotation.AnnSemi',
  --             'ApiAnnotation.AnnVbar',
  --             'ApiAnnotation.AnnClose'
362 363

  -- For details on above see note [Api annotations] in ApiAnnotation
364 365
  | HsDo        (XDo p)                  -- Type of the whole expression
                (HsStmtContext Name)     -- The parameterisation is unimportant
366 367
                                         -- because in this context we never use
                                         -- the PatGuard or ParStmt variant
368
                (Located [ExprLStmt p]) -- "do":one or more stmts
Ian Lynagh's avatar
Ian Lynagh committed
369

370
  -- | Syntactic list: [a,b,c,...]
Alan Zimmerman's avatar
Alan Zimmerman committed
371
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
372 373
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
  --              'ApiAnnotation.AnnClose' @']'@
374 375

  -- For details on above see note [Api annotations] in ApiAnnotation
376
  -- See Note [Empty lists]
377
  | ExplicitList
378
                (XExplicitList p)  -- Gives type of components of list
379 380 381
                (Maybe (SyntaxExpr p))
                                   -- For OverloadedLists, the fromListN witness
                [LHsExpr p]
Ian Lynagh's avatar
Ian Lynagh committed
382

383
  -- | Record construction
Alan Zimmerman's avatar
Alan Zimmerman committed
384
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
385 386
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
  --         'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
387 388

  -- For details on above see note [Api annotations] in ApiAnnotation
389
  | RecordCon
390 391
      { rcon_ext      :: XRecordCon p
      , rcon_con_name :: Located (IdP p)    -- The constructor name;
392
                                            --  not used after type checking
393
      , rcon_flds     :: HsRecordBinds p }  -- The fields
Ian Lynagh's avatar
Ian Lynagh committed
394

395
  -- | Record update
Alan Zimmerman's avatar
Alan Zimmerman committed
396
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
397 398
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
  --         'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
399 400

  -- For details on above see note [Api annotations] in ApiAnnotation
401
  | RecordUpd
402 403
      { rupd_ext  :: XRecordUpd p
      , rupd_expr :: LHsExpr p
404
      , rupd_flds :: [LHsRecUpdField p]
405
      }
Ian Lynagh's avatar
Ian Lynagh committed
406 407 408
  -- For a type family, the arg types are of the *instance* tycon,
  -- not the family tycon

Alan Zimmerman's avatar
Alan Zimmerman committed
409 410 411
  -- | Expression with an explicit type signature. @e :: type@
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
412 413

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
414
  | ExprWithTySig
415 416
                (XExprWithTySig p)

417
                (LHsExpr p)
418
                (LHsSigWcType (NoGhcTc p))
Ian Lynagh's avatar
Ian Lynagh committed
419

420
  -- | Arithmetic sequence
Alan Zimmerman's avatar
Alan Zimmerman committed
421
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
422 423 424
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
  --              'ApiAnnotation.AnnComma','ApiAnnotation.AnnDotdot',
  --              'ApiAnnotation.AnnClose' @']'@
425 426

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
427
  | ArithSeq
428
                (XArithSeq p)
429 430 431
                (Maybe (SyntaxExpr p))
                                  -- For OverloadedLists, the fromList witness
                (ArithSeqInfo p)
Ian Lynagh's avatar
Ian Lynagh committed
432

433
  -- For details on above see note [Api annotations] in ApiAnnotation
Ian Lynagh's avatar
Ian Lynagh committed
434

435
  -----------------------------------------------------------
436
  -- MetaHaskell Extensions
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
437

Alan Zimmerman's avatar
Alan Zimmerman committed
438
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
439 440
  --         'ApiAnnotation.AnnOpenE','ApiAnnotation.AnnOpenEQ',
  --         'ApiAnnotation.AnnClose','ApiAnnotation.AnnCloseQ'
441 442

  -- For details on above see note [Api annotations] in ApiAnnotation
443
  | HsBracket    (XBracket p) (HsBracket p)
444

445 446
    -- See Note [Pending Splices]
  | HsRnBracketOut
447
      (XRnBracketOut p)
448
      (HsBracket GhcRn)    -- Output of the renamer is the *original* renamed
449 450
                           -- expression, plus
      [PendingRnSplice]    -- _renamed_ splices to be type checked
gmainland's avatar
gmainland committed
451

452
  | HsTcBracketOut
453
      (XTcBracketOut p)
454
      (HsBracket GhcRn)    -- Output of the type checker is the *original*
455 456 457
                           -- renamed expression, plus
      [PendingTcSplice]    -- _typechecked_ splices to be
                           -- pasted back in by the desugarer
458

Alan Zimmerman's avatar
Alan Zimmerman committed
459 460
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnClose'
461 462

  -- For details on above see note [Api annotations] in ApiAnnotation
463
  | HsSpliceE  (XSpliceE p) (HsSplice p)
464

465 466 467
  -----------------------------------------------------------
  -- Arrow notation extension

468
  -- | @proc@ notation for Arrows
Alan Zimmerman's avatar
Alan Zimmerman committed
469 470 471
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnProc',
  --          'ApiAnnotation.AnnRarrow'
472 473

  -- For details on above see note [Api annotations] in ApiAnnotation
474 475
  | HsProc      (XProc p)
                (LPat p)               -- arrow abstraction, proc
476 477
                (LHsCmdTop p)          -- body of the abstraction
                                       -- always has an empty stack
478

479 480
  ---------------------------------------
  -- static pointers extension
Alan Zimmerman's avatar
Alan Zimmerman committed
481
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnStatic',
482 483

  -- For details on above see note [Api annotations] in ApiAnnotation
484
  | HsStatic (XStatic p) -- Free variables of the body
485
             (LHsExpr p)        -- Body
486

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
487 488 489
  ---------------------------------------
  -- Haskell program coverage (Hpc) Support

Ian Lynagh's avatar
Ian Lynagh committed
490
  | HsTick
491
     (XTick p)
492 493
     (Tickish (IdP p))
     (LHsExpr p)                       -- sub-expression
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
494 495

  | HsBinTick
496
     (XBinTick p)
Ian Lynagh's avatar
Ian Lynagh committed
497 498
     Int                                -- module-local tick number for True
     Int                                -- module-local tick number for False
499
     (LHsExpr p)                        -- sub-expression
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
500

501 502 503
  ---------------------------------------
  -- Expressions annotated with pragmas, written as {-# ... #-}
  | HsPragE (XPragE p) (HsPragE p) (LHsExpr p)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
504 505 506

  ---------------------------------------
  -- Finally, HsWrap appears only in typechecker output
507 508
  -- The contained Expr is *NOT* itself an HsWrap.
  -- See Note [Detecting forced eta expansion] in DsExpr. This invariant
509
  -- is maintained by GHC.Hs.Utils.mkHsWrap.
510

511 512
  |  HsWrap     (XWrap p)
                HsWrapper    -- TRANSLATION
513
                (HsExpr p)
514

515 516 517 518 519 520 521
  | XExpr       (XXExpr p) -- Note [Trees that Grow] extension constructor


-- | Extra data fields for a 'RecordCon', added by the type checker
data RecordConTc = RecordConTc
      { rcon_con_like :: ConLike      -- The data constructor or pattern synonym
      , rcon_con_expr :: PostTcExpr   -- Instantiated constructor function
522
      }
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

-- | Extra data fields for a 'RecordUpd', added by the type checker
data RecordUpdTc = RecordUpdTc
      { rupd_cons :: [ConLike]
                -- Filled in by the type checker to the
                -- _non-empty_ list of DataCons that have
                -- all the upd'd fields

      , rupd_in_tys  :: [Type] -- Argument types of *input* record type
      , rupd_out_tys :: [Type] --             and  *output* record type
                               -- The original type can be reconstructed
                               -- with conLikeResTy
      , rupd_wrap :: HsWrapper -- See note [Record Update HsWrapper]
      } deriving Data

-- ---------------------------------------------------------------------

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
type instance XVar           (GhcPass _) = NoExtField
type instance XUnboundVar    (GhcPass _) = NoExtField
type instance XConLikeOut    (GhcPass _) = NoExtField
type instance XRecFld        (GhcPass _) = NoExtField
type instance XOverLabel     (GhcPass _) = NoExtField
type instance XIPVar         (GhcPass _) = NoExtField
type instance XOverLitE      (GhcPass _) = NoExtField
type instance XLitE          (GhcPass _) = NoExtField
type instance XLam           (GhcPass _) = NoExtField
type instance XLamCase       (GhcPass _) = NoExtField
type instance XApp           (GhcPass _) = NoExtField

type instance XAppTypeE      (GhcPass _) = NoExtField

type instance XOpApp         GhcPs = NoExtField
555 556 557
type instance XOpApp         GhcRn = Fixity
type instance XOpApp         GhcTc = Fixity

558 559 560 561 562
type instance XNegApp        (GhcPass _) = NoExtField
type instance XPar           (GhcPass _) = NoExtField
type instance XSectionL      (GhcPass _) = NoExtField
type instance XSectionR      (GhcPass _) = NoExtField
type instance XExplicitTuple (GhcPass _) = NoExtField
563

564 565
type instance XExplicitSum   GhcPs = NoExtField
type instance XExplicitSum   GhcRn = NoExtField
566 567
type instance XExplicitSum   GhcTc = [Type]

568 569
type instance XCase          (GhcPass _) = NoExtField
type instance XIf            (GhcPass _) = NoExtField
570

571 572
type instance XMultiIf       GhcPs = NoExtField
type instance XMultiIf       GhcRn = NoExtField
573 574
type instance XMultiIf       GhcTc = Type

575
type instance XLet           (GhcPass _) = NoExtField
576

577 578
type instance XDo            GhcPs = NoExtField
type instance XDo            GhcRn = NoExtField
579 580
type instance XDo            GhcTc = Type

581 582
type instance XExplicitList  GhcPs = NoExtField
type instance XExplicitList  GhcRn = NoExtField
583 584
type instance XExplicitList  GhcTc = Type

585 586
type instance XRecordCon     GhcPs = NoExtField
type instance XRecordCon     GhcRn = NoExtField
587 588
type instance XRecordCon     GhcTc = RecordConTc

589 590
type instance XRecordUpd     GhcPs = NoExtField
type instance XRecordUpd     GhcRn = NoExtField
591 592
type instance XRecordUpd     GhcTc = RecordUpdTc

593
type instance XExprWithTySig (GhcPass _) = NoExtField
594

595 596
type instance XArithSeq      GhcPs = NoExtField
type instance XArithSeq      GhcRn = NoExtField
597 598
type instance XArithSeq      GhcTc = PostTcExpr

599
type instance XBracket       (GhcPass _) = NoExtField
600

601 602
type instance XRnBracketOut  (GhcPass _) = NoExtField
type instance XTcBracketOut  (GhcPass _) = NoExtField
603

604 605
type instance XSpliceE       (GhcPass _) = NoExtField
type instance XProc          (GhcPass _) = NoExtField
606

607
type instance XStatic        GhcPs = NoExtField
608 609 610
type instance XStatic        GhcRn = NameSet
type instance XStatic        GhcTc = NameSet

611 612
type instance XTick          (GhcPass _) = NoExtField
type instance XBinTick       (GhcPass _) = NoExtField
613 614 615

type instance XPragE         (GhcPass _) = NoExtField

616 617
type instance XWrap          (GhcPass _) = NoExtField
type instance XXExpr         (GhcPass _) = NoExtCon
618 619

-- ---------------------------------------------------------------------
620

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
-- | A pragma, written as {-# ... #-}, that may appear within an expression.
data HsPragE p
  = HsPragSCC   (XSCC p)
                SourceText            -- Note [Pragma source text] in BasicTypes
                StringLiteral         -- "set cost centre" SCC pragma

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

  -- For details on above see note [Api annotations] in ApiAnnotation
  | HsPragCore  (XCoreAnn p)
                SourceText            -- Note [Pragma source text] in BasicTypes
                StringLiteral         -- hdaume: core annotation

  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --       'ApiAnnotation.AnnOpen' @'{-\# GENERATED'@,
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnColon','ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnMinus',
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnColon',
  --       'ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnClose' @'\#-}'@

  -- For details on above see note [Api annotations] in ApiAnnotation
  | HsPragTick                        -- A pragma introduced tick
     (XTickPragma p)
     SourceText                       -- Note [Pragma source text] in BasicTypes
     (StringLiteral,(Int,Int),(Int,Int))
                                      -- external span for this tick
     ((SourceText,SourceText),(SourceText,SourceText))
        -- Source text for the four integers used in the span.
        -- See note [Pragma source text] in BasicTypes

  | XHsPragE (XXPragE p)

type instance XSCC           (GhcPass _) = NoExtField
type instance XCoreAnn       (GhcPass _) = NoExtField
type instance XTickPragma    (GhcPass _) = NoExtField
type instance XXPragE        (GhcPass _) = NoExtCon

661 662 663 664 665 666
-- | Located Haskell Tuple Argument
--
-- 'HsTupArg' is used for tuple sections
-- @(,a,)@ is represented by
-- @ExplicitTuple [Missing ty1, Present a, Missing ty3]@
-- 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 672

-- | Haskell Tuple Argument
673
data HsTupArg id
674 675 676 677
  = Present (XPresent id) (LHsExpr id)     -- ^ The argument
  | Missing (XMissing id)    -- ^ The argument is missing, but this is its type
  | XTupArg (XXTupArg id)    -- ^ Note [Trees that Grow] extension point

678
type instance XPresent         (GhcPass _) = NoExtField
679

680 681
type instance XMissing         GhcPs = NoExtField
type instance XMissing         GhcRn = NoExtField
682 683
type instance XMissing         GhcTc = Type

684
type instance XXTupArg         (GhcPass _) = NoExtCon
685

686 687 688
tupArgPresent :: LHsTupArg id -> Bool
tupArgPresent (L _ (Present {})) = True
tupArgPresent (L _ (Missing {})) = False
689
tupArgPresent (L _ (XTupArg {})) = False
690

Austin Seipp's avatar
Austin Seipp committed
691
{-
692 693 694 695
Note [Parens in HsSyn]
~~~~~~~~~~~~~~~~~~~~~~
HsPar (and ParPat in patterns, HsParTy in types) is used as follows

696
  * HsPar is required; the pretty printer does not add parens.
697 698 699 700

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

701 702 703 704 705 706
  * For ParPat and HsParTy the pretty printer does add parens but this should be
    a no-op for ParsedSource, based on the pretty printer round trip feature
    introduced in
    https://phabricator.haskell.org/rGHC499e43824bda967546ebf95ee33ec1f84a114a7c

  * ParPat and HsParTy are pretty printed as '( .. )' regardless of whether or
James Michael DuPont's avatar
James Michael DuPont committed
707
    not they are strictly necessary. This should be addressed when #13238 is
708 709 710
    completed, to be treated the same as HsPar.


711 712 713
Note [Sections in HsSyn]
~~~~~~~~~~~~~~~~~~~~~~~~
Sections should always appear wrapped in an HsPar, thus
714 715
         HsPar (SectionR ...)
The parser parses sections in a wider variety of situations
716
(See Note [Parsing sections]), but the renamer checks for those
717
parens.  This invariant makes pretty-printing easier; we don't need
718 719
a special case for adding the parens round sections.

720 721 722 723 724 725
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?
726
Because we allow an 'if' to return *unboxed* results, thus
727 728 729 730 731
  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
732 733 734

Note [Record Update HsWrapper]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
735 736 737 738
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
739 740

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

744
  foo = (Just True) { x = False }
Matthew Pickering's avatar
Matthew Pickering committed
745
then `foo` desugars to something like
746 747 748 749
  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
750

751 752 753 754 755 756 757 758 759 760
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.
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825

Note [ExplicitTuple]
~~~~~~~~~~~~~~~~~~~~
An ExplicitTuple is never just a data constructor like (,,,).
That is, the `[LHsTupArg p]` argument of `ExplicitTuple` has at least
one `Present` member (and is thus never empty).

A tuple data constructor like () or (,,,) is parsed as an `HsVar`, not an
`ExplicitTuple`, and stays that way. This is important for two reasons:

  1. We don't need -XTupleSections for (,,,)
  2. The type variables in (,,,) can be instantiated with visible type application.
     That is,

       (,,)     :: forall a b c. a -> b -> c -> (a,b,c)
       (True,,) :: forall {b} {c}. b -> c -> (Bool,b,c)

     Note that the tuple section has *inferred* arguments, while the data
     constructor has *specified* ones.
     (See Note [Required, Specified, and Inferred for types] in TcTyClsDecls
     for background.)

Sadly, the grammar for this is actually ambiguous, and it's only thanks to the
preference of a shift in a shift/reduce conflict that the parser works as this
Note details. Search for a reference to this Note in Parser.y for further
explanation.

Note [Empty lists]
~~~~~~~~~~~~~~~~~~
An empty list could be considered either a data constructor (stored with
HsVar) or an ExplicitList. This Note describes how empty lists flow through the
various phases and why.

Parsing
-------
An empty list is parsed by the sysdcon nonterminal. It thus comes to life via
HsVar nilDataCon (defined in TysWiredIn). A freshly-parsed (HsExpr GhcPs) empty list
is never a ExplicitList.

Renaming
--------
If -XOverloadedLists is enabled, we must type-check the empty list as if it
were a call to fromListN. (This is true regardless of the setting of
-XRebindableSyntax.) This is very easy if the empty list is an ExplicitList,
but an annoying special case if it's an HsVar. So the renamer changes a
HsVar nilDataCon to an ExplicitList [], but only if -XOverloadedLists is on.
(Why not always? Read on, dear friend.) This happens in the HsVar case of rnExpr.

Type-checking
-------------
We want to accept an expression like [] @Int. To do this, we must infer that
[] :: forall a. [a]. This is easy if [] is a HsVar with the right DataCon inside.
However, the type-checking for explicit lists works differently: [x,y,z] is never
polymorphic. Instead, we unify the types of x, y, and z together, and use the
unified type as the argument to the cons and nil constructors. Thus, treating
[] as an empty ExplicitList in the type-checker would prevent [] @Int from working.

However, if -XOverloadedLists is on, then [] @Int really shouldn't be allowed:
it's just like fromListN 0 [] @Int. Since
  fromListN :: forall list. IsList list => Int -> [Item list] -> list
that expression really should be rejected. Thus, the renamer's behaviour is
exactly what we want: treat [] as a datacon when -XNoOverloadedLists, and as
an empty ExplicitList when -XOverloadedLists.

See also #13680, which requested [] @Int to work.
Austin Seipp's avatar
Austin Seipp committed
826
-}
827

828
instance (OutputableBndrId p) => Outputable (HsExpr (GhcPass p)) where
829
    ppr expr = pprExpr expr
830

831
-----------------------
Ian Lynagh's avatar
Ian Lynagh committed
832
-- pprExpr, pprLExpr, pprBinds call pprDeeper;
833
-- the underscore versions do not
834
pprLExpr :: (OutputableBndrId p) => LHsExpr (GhcPass p) -> SDoc
835 836
pprLExpr (L _ e) = pprExpr e

837
pprExpr :: (OutputableBndrId p) => HsExpr (GhcPass p) -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
838 839 840 841 842 843 844
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 (...)
845
isQuietHsExpr (HsPar {})        = True
Ian Lynagh's avatar
Ian Lynagh committed
846
-- applications don't display anything themselves
847 848 849
isQuietHsExpr (HsApp {})        = True
isQuietHsExpr (HsAppType {})    = True
isQuietHsExpr (OpApp {})        = True
Ian Lynagh's avatar
Ian Lynagh committed
850
isQuietHsExpr _ = False
851

852
pprBinds :: (OutputableBndrId idL, OutputableBndrId idR)
853
         => HsLocalBindsLR (GhcPass idL) (GhcPass idR) -> SDoc
854
pprBinds b = pprDeeper (ppr b)
855

856
-----------------------
857
ppr_lexpr :: (OutputableBndrId p) => LHsExpr (GhcPass p) -> SDoc
858
ppr_lexpr e = ppr_expr (unLoc e)
859

860
ppr_expr :: forall p. (OutputableBndrId p)
861
         => HsExpr (GhcPass p) -> SDoc
862
ppr_expr (HsVar _ (L _ v))  = pprPrefixOcc v
863
ppr_expr (HsUnboundVar _ uv)= pprPrefixOcc uv
864 865 866 867 868 869 870
ppr_expr (HsConLikeOut _ c) = pprPrefixOcc c
ppr_expr (HsIPVar _ v)      = ppr v
ppr_expr (HsOverLabel _ _ l)= char '#' <> ppr l
ppr_expr (HsLit _ lit)      = ppr lit
ppr_expr (HsOverLit _ lit)  = ppr lit
ppr_expr (HsPar _ e)        = parens (ppr_lexpr e)

871
ppr_expr (HsPragE _ prag e) = sep [ppr prag, ppr_lexpr e]
872

873 874
ppr_expr e@(HsApp {})        = ppr_apps e []
ppr_expr e@(HsAppType {})    = ppr_apps e []
875

876
ppr_expr (OpApp _ e1 op e2)
877
  | Just pp_op <- ppr_infix_expr (unLoc op)
878 879 880 881
  = pp_infixly pp_op
  | otherwise
  = pp_prefixly

882
  where
883 884
    pp_e1 = pprDebugParendExpr opPrec e1   -- In debug mode, add parens
    pp_e2 = pprDebugParendExpr opPrec e2   -- to make precedence clear
885 886

    pp_prefixly
887
      = hang (ppr op) 2 (sep [pp_e1, pp_e2])
888

889 890
    pp_infixly pp_op
      = hang pp_e1 2 (sep [pp_op, nest 2 pp_e2])
891

892
ppr_expr (NegApp _ e _) = char '-' <+> pprDebugParendExpr appPrec e
893

894
ppr_expr (SectionL _ expr op)
895 896 897 898
  | Just pp_op <- ppr_infix_expr (unLoc op)
  = pp_infixly pp_op
  | otherwise
  = pp_prefixly
899
  where
900
    pp_expr = pprDebugParendExpr opPrec expr
901

902
    pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
903
                       4 (hsep [pp_expr, text "x_ )"])
904

905
    pp_infixly v = (sep [pp_expr, v])
906

907
ppr_expr (SectionR _ op expr)
908 909 910 911
  | Just pp_op <- ppr_infix_expr (unLoc op)
  = pp_infixly pp_op
  | otherwise
  = pp_prefixly
912
  where
913
    pp_expr = pprDebugParendExpr opPrec expr
914

915
    pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, text "x_"])
916
                       4 (pp_expr <> rparen)
917

918
    pp_infixly v = sep [v, pp_expr]
919

920
ppr_expr (ExplicitTuple _ exprs boxity)
921 922
    -- Special-case unary boxed tuples so that they are pretty-printed as
    -- `Unit x`, not `(x)`
923
  | [L _ (Present _ expr)] <- exprs
924 925 926
  , Boxed <- boxity
  = hsep [text (mkTupleStr Boxed 1), ppr expr]
  | otherwise
927
  = tupleParens (boxityTupleSort boxity) (fcat (ppr_tup_args $ map unLoc exprs))
928 929
  where
    ppr_tup_args []               = []
930 931 932
    ppr_tup_args (Present _ e : es) = (ppr_lexpr e <> punc es) : ppr_tup_args es
    ppr_tup_args (Missing _   : es) = punc es : ppr_tup_args es
    ppr_tup_args (XTupArg x   : es) = (ppr x <> punc es) : ppr_tup_args es
933 934 935

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

939
ppr_expr (ExplicitSum _ alt arity expr)
940 941 942 943
  = text "(#" <+> ppr_bars (alt - 1) <+> ppr expr <+> ppr_bars (arity - alt) <+> text "#)"
  where
    ppr_bars n = hsep (replicate n (char '|'))

944
ppr_expr (HsLam _ matches)
945
  = pprMatches matches
946

947
ppr_expr (HsLamCase _ matches)
948 949
  = sep [ sep [text "\\case"],
          nest 2 (pprMatches matches) ]
950

951
ppr_expr (HsCase _ expr matches@(MG { mg_alts = L _ [_] }))
952
  = sep [ sep [text "case", nest 4 (ppr expr), ptext (sLit "of {")],
953
          nest 2 (pprMatches matches) <+> char '}']
954
ppr_expr (HsCase _ expr matches)
955 956
  = sep [ sep [text "case", nest 4 (ppr expr), ptext (sLit "of")],
          nest 2 (pprMatches matches) ]
957

958
ppr_expr (HsIf _ _ e1 e2 e3)
959
  = sep [hsep [text "if", nest 2 (ppr e1), ptext (sLit "then")],
Ian Lynagh's avatar
Ian Lynagh committed
960
         nest 4 (ppr e2),
961
         text "else",
Ian Lynagh's avatar
Ian Lynagh committed
962
         nest 4 (ppr e3)]
963

964
ppr_expr (HsMultiIf _ alts)
965
  = hang (text "if") 3  (vcat (map ppr_alt alts))
966
  where ppr_alt (L _ (GRHS _ guards expr)) =
967 968 969 970 971 972
          hang vbar 2 (ppr_one one_alt)
          where
            ppr_one [] = panic "ppr_exp HsMultiIf"
            ppr_one (h:t) = hang h 2 (sep t)
            one_alt = [ interpp'SP guards
                      , text "->" <+> pprDeeper (ppr expr) ]
973
        ppr_alt (L _ (XGRHS x)) = ppr x
974

975
-- special case: let ... in let ...
976
ppr_expr (HsLet _ (L _ binds) expr@(L _ (HsLet _ _ _)))
977
  = sep [hang (text "let") 2 (hsep [pprBinds binds, ptext (sLit "in")]),
Ian Lynagh's avatar
Ian Lynagh committed
978
         ppr_lexpr expr]
979

980
ppr_expr (HsLet _ (L _ binds) expr)
981 982
  = sep [hang (text "let") 2 (pprBinds binds),
         hang (text "in")  2 (ppr expr)]
983

984
ppr_expr (HsDo _ do_or_list_comp (L _ stmts)) = pprDo do_or_list_comp stmts
985

986
ppr_expr (ExplicitList _ _ exprs)
987
  = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
988

989
ppr_expr (RecordCon { rcon_con_name = con_id, rcon_flds = rbinds })
990
  = hang (ppr con_id) 2 (ppr rbinds)
991

992
ppr_expr (RecordUpd { rupd_expr = L _ aexp, rupd_flds = rbinds })
993
  = hang (ppr aexp) 2 (braces (fsep (punctuate comma (map ppr rbinds))))
994