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

-- | Abstract Haskell syntax for expressions.
17
module GHC.Hs.Expr where
18

19
#include "HsVersions.h"
20 21

-- friends:
22 23
import GhcPrelude

24 25 26 27 28 29 30
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
31 32

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

-- libraries:
49
import Data.Data hiding (Fixity(..))
50
import qualified Data.Data as Data (Fixity(..))
Simon Marlow's avatar
Simon Marlow committed
51
import Data.Maybe (isNothing)
52

53 54 55
import GHCi.RemoteTypes ( ForeignRef )
import qualified Language.Haskell.TH as TH (Q)

Austin Seipp's avatar
Austin Seipp committed
56 57 58
{-
************************************************************************
*                                                                      *
59
\subsection{Expressions proper}
Austin Seipp's avatar
Austin Seipp committed
60 61 62
*                                                                      *
************************************************************************
-}
63

64 65
-- * Expressions proper

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

71 72
  -- For details on above see note [Api annotations] in ApiAnnotation

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

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

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

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

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

123 124 125 126 127 128
-- | 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 }

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

136 137
instance (p ~ GhcPass pass, OutputableBndrId p)
       => Outputable (SyntaxExpr p) where
138 139 140 141 142 143
  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
144 145
      then ppr expr <> braces (pprWithCommas ppr arg_wraps)
                    <> braces (ppr res_wrap)
146 147
      else ppr expr

148
-- | Command Syntax Table (for Arrow syntax)
149
type CmdSyntaxTable p = [(Name, HsExpr p)]
150
-- See Note [CmdSyntaxTable]
151

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

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

* 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
186
-}
187

188
-- | A Haskell expression.
189
data HsExpr p
190 191
  = HsVar     (XVar p)
              (Located (IdP p)) -- ^ Variable
192

193 194
                             -- See Note [Located RdrNames]

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

204 205
  | HsConLikeOut (XConLikeOut p)
                 ConLike     -- ^ After typechecker only; must be different
206 207
                             -- HsVar for pretty printing

208 209
  | HsRecFld  (XRecFld p)
              (AmbiguousFieldOcc p) -- ^ Variable pointing to record selector
210
                                    -- Not in use after typechecking
211

212 213
  | HsOverLabel (XOverLabel p)
                (Maybe (IdP p)) FastString
214 215 216 217
     -- ^ 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
218

219 220 221 222
  | HsIPVar   (XIPVar p)
              HsIPName   -- ^ Implicit parameter (not in use after typechecking)
  | HsOverLit (XOverLitE p)
              (HsOverLit p)  -- ^ Overloaded literals
223

224 225
  | HsLit     (XLitE p)
              (HsLit p)      -- ^ Simple (non-overloaded) literals
226

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

234 235
       -- For details on above see note [Api annotations] in ApiAnnotation

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

242 243
       -- For details on above see note [Api annotations] in ApiAnnotation

244
  | HsApp     (XApp p) (LHsExpr p) (LHsExpr p) -- ^ Application
245

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

253
  -- | Operator applications:
254 255
  -- NB Bracketed ops such as (+) come out as Vars.

256 257 258
  -- 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.

259 260
  | OpApp       (XOpApp p)
                (LHsExpr p)       -- left operand
261 262
                (LHsExpr p)       -- operator
                (LHsExpr p)       -- right operand
Ian Lynagh's avatar
Ian Lynagh committed
263

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

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

Alan Zimmerman's avatar
Alan Zimmerman committed
274 275
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
  --             'ApiAnnotation.AnnClose' @')'@
276 277

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

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

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

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

300 301 302 303 304 305 306
  -- | 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
307
  | ExplicitSum
308
          (XExplicitSum p)
309 310
          ConTag --  Alternative (one-based)
          Arity  --  Sum arity
311
          (LHsExpr p)
312

Alan Zimmerman's avatar
Alan Zimmerman committed
313
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnCase',
Alan Zimmerman's avatar
Alan Zimmerman committed
314 315
  --       'ApiAnnotation.AnnOf','ApiAnnotation.AnnOpen' @'{'@,
  --       'ApiAnnotation.AnnClose' @'}'@
316 317

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

Alan Zimmerman's avatar
Alan Zimmerman committed
322 323
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf',
  --       'ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
324
  --       'ApiAnnotation.AnnThen','ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
325
  --       'ApiAnnotation.AnnElse',
326 327

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

336
  -- | Multi-way if
Alan Zimmerman's avatar
Alan Zimmerman committed
337 338 339
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf'
  --       'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
340 341

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

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

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

Alan Zimmerman's avatar
Alan Zimmerman committed
355 356 357 358
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDo',
  --             'ApiAnnotation.AnnOpen', 'ApiAnnotation.AnnSemi',
  --             'ApiAnnotation.AnnVbar',
  --             'ApiAnnotation.AnnClose'
359 360

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

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

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

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

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

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

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

Alan Zimmerman's avatar
Alan Zimmerman committed
406 407 408
  -- | Expression with an explicit type signature. @e :: type@
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
409 410

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

414
                (LHsExpr p)
415
                (LHsSigWcType (NoGhcTc p))
Ian Lynagh's avatar
Ian Lynagh committed
416

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

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

430
  -- For details on above see note [Api annotations] in ApiAnnotation
431 432
  | HsSCC       (XSCC p)
                SourceText            -- Note [Pragma source text] in BasicTypes
433
                StringLiteral         -- "set cost centre" SCC pragma
434
                (LHsExpr p)           -- expr whose cost is to be measured
Alan Zimmerman's avatar
Alan Zimmerman committed
435 436 437

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

  -- For details on above see note [Api annotations] in ApiAnnotation
440 441
  | HsCoreAnn   (XCoreAnn p)
                SourceText            -- Note [Pragma source text] in BasicTypes
442
                StringLiteral         -- hdaume: core annotation
443
                (LHsExpr p)
Ian Lynagh's avatar
Ian Lynagh committed
444

445
  -----------------------------------------------------------
446
  -- MetaHaskell Extensions
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
447

Alan Zimmerman's avatar
Alan Zimmerman committed
448
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
449 450
  --         'ApiAnnotation.AnnOpenE','ApiAnnotation.AnnOpenEQ',
  --         'ApiAnnotation.AnnClose','ApiAnnotation.AnnCloseQ'
451 452

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

455 456
    -- See Note [Pending Splices]
  | HsRnBracketOut
457
      (XRnBracketOut p)
458
      (HsBracket GhcRn)    -- Output of the renamer is the *original* renamed
459 460
                           -- expression, plus
      [PendingRnSplice]    -- _renamed_ splices to be type checked
gmainland's avatar
gmainland committed
461

462
  | HsTcBracketOut
463
      (XTcBracketOut p)
464
      (HsBracket GhcRn)    -- Output of the type checker is the *original*
465 466 467
                           -- renamed expression, plus
      [PendingTcSplice]    -- _typechecked_ splices to be
                           -- pasted back in by the desugarer
468

Alan Zimmerman's avatar
Alan Zimmerman committed
469 470
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnClose'
471 472

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

475 476 477
  -----------------------------------------------------------
  -- Arrow notation extension

478
  -- | @proc@ notation for Arrows
Alan Zimmerman's avatar
Alan Zimmerman committed
479 480 481
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnProc',
  --          'ApiAnnotation.AnnRarrow'
482 483

  -- For details on above see note [Api annotations] in ApiAnnotation
484 485
  | HsProc      (XProc p)
                (LPat p)               -- arrow abstraction, proc
486 487
                (LHsCmdTop p)          -- body of the abstraction
                                       -- always has an empty stack
488

489 490
  ---------------------------------------
  -- static pointers extension
Alan Zimmerman's avatar
Alan Zimmerman committed
491
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnStatic',
492 493

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

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
497 498 499
  ---------------------------------------
  -- Haskell program coverage (Hpc) Support

Ian Lynagh's avatar
Ian Lynagh committed
500
  | HsTick
501
     (XTick p)
502 503
     (Tickish (IdP p))
     (LHsExpr p)                       -- sub-expression
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
504 505

  | HsBinTick
506
     (XBinTick p)
Ian Lynagh's avatar
Ian Lynagh committed
507 508
     Int                                -- module-local tick number for True
     Int                                -- module-local tick number for False
509
     (LHsExpr p)                        -- sub-expression
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
510

Alan Zimmerman's avatar
Alan Zimmerman committed
511
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
Alan Zimmerman's avatar
Alan Zimmerman committed
512 513 514
  --       'ApiAnnotation.AnnOpen' @'{-\# GENERATED'@,
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnColon','ApiAnnotation.AnnVal',
Alan Zimmerman's avatar
Alan Zimmerman committed
515
  --       'ApiAnnotation.AnnMinus',
Alan Zimmerman's avatar
Alan Zimmerman committed
516 517 518
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnColon',
  --       'ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnClose' @'\#-}'@
519 520

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
521
  | HsTickPragma                      -- A pragma introduced tick
522
     (XTickPragma p)
Alan Zimmerman's avatar
Alan Zimmerman committed
523
     SourceText                       -- Note [Pragma source text] in BasicTypes
524
     (StringLiteral,(Int,Int),(Int,Int))
525
                                      -- external span for this tick
526 527 528
     ((SourceText,SourceText),(SourceText,SourceText))
        -- Source text for the four integers used in the span.
        -- See note [Pragma source text] in BasicTypes
529
     (LHsExpr p)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
530 531 532

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

537 538
  |  HsWrap     (XWrap p)
                HsWrapper    -- TRANSLATION
539
                (HsExpr p)
540

541 542 543 544 545 546 547
  | 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
548
      }
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

-- | 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

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

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
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
581 582 583
type instance XOpApp         GhcRn = Fixity
type instance XOpApp         GhcTc = Fixity

584 585 586 587 588
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
589

590 591
type instance XExplicitSum   GhcPs = NoExtField
type instance XExplicitSum   GhcRn = NoExtField
592 593
type instance XExplicitSum   GhcTc = [Type]

594 595
type instance XCase          (GhcPass _) = NoExtField
type instance XIf            (GhcPass _) = NoExtField
596

597 598
type instance XMultiIf       GhcPs = NoExtField
type instance XMultiIf       GhcRn = NoExtField
599 600
type instance XMultiIf       GhcTc = Type

601
type instance XLet           (GhcPass _) = NoExtField
602

603 604
type instance XDo            GhcPs = NoExtField
type instance XDo            GhcRn = NoExtField
605 606
type instance XDo            GhcTc = Type

607 608
type instance XExplicitList  GhcPs = NoExtField
type instance XExplicitList  GhcRn = NoExtField
609 610
type instance XExplicitList  GhcTc = Type

611 612
type instance XRecordCon     GhcPs = NoExtField
type instance XRecordCon     GhcRn = NoExtField
613 614
type instance XRecordCon     GhcTc = RecordConTc

615 616
type instance XRecordUpd     GhcPs = NoExtField
type instance XRecordUpd     GhcRn = NoExtField
617 618
type instance XRecordUpd     GhcTc = RecordUpdTc

619
type instance XExprWithTySig (GhcPass _) = NoExtField
620

621 622
type instance XArithSeq      GhcPs = NoExtField
type instance XArithSeq      GhcRn = NoExtField
623 624
type instance XArithSeq      GhcTc = PostTcExpr

625 626 627
type instance XSCC           (GhcPass _) = NoExtField
type instance XCoreAnn       (GhcPass _) = NoExtField
type instance XBracket       (GhcPass _) = NoExtField
628

629 630
type instance XRnBracketOut  (GhcPass _) = NoExtField
type instance XTcBracketOut  (GhcPass _) = NoExtField
631

632 633
type instance XSpliceE       (GhcPass _) = NoExtField
type instance XProc          (GhcPass _) = NoExtField
634

635
type instance XStatic        GhcPs = NoExtField
636 637 638
type instance XStatic        GhcRn = NameSet
type instance XStatic        GhcTc = NameSet

639 640 641 642 643
type instance XTick          (GhcPass _) = NoExtField
type instance XBinTick       (GhcPass _) = NoExtField
type instance XTickPragma    (GhcPass _) = NoExtField
type instance XWrap          (GhcPass _) = NoExtField
type instance XXExpr         (GhcPass _) = NoExtCon
644 645

-- ---------------------------------------------------------------------
646

647 648 649 650 651 652
-- | 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))@
653
type LHsTupArg id = Located (HsTupArg id)
Alan Zimmerman's avatar
Alan Zimmerman committed
654
-- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma'
655 656

-- For details on above see note [Api annotations] in ApiAnnotation
657 658

-- | Haskell Tuple Argument
659
data HsTupArg id
660 661 662 663
  = 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

664
type instance XPresent         (GhcPass _) = NoExtField
665

666 667
type instance XMissing         GhcPs = NoExtField
type instance XMissing         GhcRn = NoExtField
668 669
type instance XMissing         GhcTc = Type

670
type instance XXTupArg         (GhcPass _) = NoExtCon
671

672 673 674
tupArgPresent :: LHsTupArg id -> Bool
tupArgPresent (L _ (Present {})) = True
tupArgPresent (L _ (Missing {})) = False
675
tupArgPresent (L _ (XTupArg {})) = False
676

Austin Seipp's avatar
Austin Seipp committed
677
{-
678 679 680 681
Note [Parens in HsSyn]
~~~~~~~~~~~~~~~~~~~~~~
HsPar (and ParPat in patterns, HsParTy in types) is used as follows

682
  * HsPar is required; the pretty printer does not add parens.
683 684 685 686

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

687 688 689 690 691 692
  * 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
693
    not they are strictly necessary. This should be addressed when #13238 is
694 695 696
    completed, to be treated the same as HsPar.


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

706 707 708 709 710 711
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?
712
Because we allow an 'if' to return *unboxed* results, thus
713 714 715 716 717
  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
718 719 720

Note [Record Update HsWrapper]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
721 722 723 724
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
725 726

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

730
  foo = (Just True) { x = False }
Matthew Pickering's avatar
Matthew Pickering committed
731
then `foo` desugars to something like
732 733 734 735
  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
736

737 738 739 740 741 742 743 744 745 746
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.
747 748 749 750 751 752 753 754 755 756 757 758 759 760 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

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
812
-}
813

814
instance (p ~ GhcPass pass, OutputableBndrId p) => Outputable (HsExpr p) where
815
    ppr expr = pprExpr expr
816

817
-----------------------
Ian Lynagh's avatar
Ian Lynagh committed
818
-- pprExpr, pprLExpr, pprBinds call pprDeeper;
819
-- the underscore versions do not
820
pprLExpr :: (OutputableBndrId (GhcPass p)) => LHsExpr (GhcPass p) -> SDoc
821 822
pprLExpr (L _ e) = pprExpr e

823
pprExpr :: (OutputableBndrId (GhcPass p)) => HsExpr (GhcPass p) -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
824 825 826 827 828 829 830
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 (...)
831
isQuietHsExpr (HsPar {})        = True
Ian Lynagh's avatar
Ian Lynagh committed
832
-- applications don't display anything themselves
833 834 835
isQuietHsExpr (HsApp {})        = True
isQuietHsExpr (HsAppType {})    = True
isQuietHsExpr (OpApp {})        = True
Ian Lynagh's avatar
Ian Lynagh committed
836
isQuietHsExpr _ = False
837

838 839
pprBinds :: (OutputableBndrId (GhcPass idL), OutputableBndrId (GhcPass idR))
         => HsLocalBindsLR (GhcPass idL) (GhcPass idR) -> SDoc
840
pprBinds b = pprDeeper (ppr b)
841

842
-----------------------
843
ppr_lexpr :: (OutputableBndrId (GhcPass p)) => LHsExpr (GhcPass p) -> SDoc
844
ppr_lexpr e = ppr_expr (unLoc e)
845

846 847
ppr_expr :: forall p. (OutputableBndrId (GhcPass p))
         => HsExpr (GhcPass p) -> SDoc
848
ppr_expr (HsVar _ (L _ v))  = pprPrefixOcc v
849
ppr_expr (HsUnboundVar _ uv)= pprPrefixOcc uv
850 851 852 853 854 855 856 857
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)

ppr_expr (HsCoreAnn _ stc (StringLiteral sta s) e)
858 859 860
  = vcat [pprWithSourceText stc (text "{-# CORE")
          <+> pprWithSourceText sta (doubleQuotes $ ftext s) <+> text "#-}"
         , ppr_lexpr e]
861

862 863
ppr_expr e@(HsApp {})        = ppr_apps e []
ppr_expr e@(HsAppType {})    = ppr_apps e []
864

865
ppr_expr (OpApp _ e1 op e2)
866
  | Just pp_op <- ppr_infix_expr (unLoc op)
867 868 869 870
  = pp_infixly pp_op
  | otherwise
  = pp_prefixly

871
  where
872 873
    pp_e1 = pprDebugParendExpr opPrec e1   -- In debug mode, add parens
    pp_e2 = pprDebugParendExpr opPrec e2   -- to make precedence clear
874 875

    pp_prefixly
876
      = hang (ppr op) 2 (sep [pp_e1, pp_e2])
877

878 879
    pp_infixly pp_op
      = hang pp_e1 2 (sep [pp_op, nest 2 pp_e2])
880

881
ppr_expr (NegApp _ e _) = char '-' <+> pprDebugParendExpr appPrec e
882

883
ppr_expr (SectionL _ expr op)
884 885 886 887
  | Just pp_op <- ppr_infix_expr (unLoc op)
  = pp_infixly pp_op
  | otherwise
  = pp_prefixly
888
  where
889
    pp_expr = pprDebugParendExpr opPrec expr
890

891
    pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
892
                       4 (hsep [pp_expr, text "x_ )"])
893

894
    pp_infixly v = (sep [pp_expr, v])
895

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

904
    pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, text "x_"])
905
                       4 (pp_expr <> rparen)
906

907
    pp_infixly v = sep [v, pp_expr]
908

909
ppr_expr (ExplicitTuple _ exprs boxity)
910
  = tupleParens (boxityTupleSort boxity) (fcat (ppr_tup_args $ map unLoc exprs))
911 912
  where
    ppr_tup_args []               = []
913 914 915
    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
916 917 918

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

922
ppr_expr (ExplicitSum _ alt arity expr)
923 924 925 926
  = text "(#" <+> ppr_bars (alt - 1) <+> ppr expr <+> ppr_bars (arity - alt) <+> text "#)"
  where
    ppr_bars n = hsep (replicate n (char '|'))

927
ppr_expr (HsLam _ matches)
928
  = pprMatches matches
929

930
ppr_expr (HsLamCase _ matches)
931 932
  = sep [ sep [text "\\case"],
          nest 2 (pprMatches matches) ]
933

934
ppr_expr (HsCase _ expr matches@(MG { mg_alts = L _ [_] }))
935
  = sep [ sep [text "case", nest 4 (ppr expr), ptext (sLit "of {")],
936
          nest 2 (pprMatches matches) <+> char '}']
937
ppr_expr (HsCase _ expr matches)
938 939
  = sep [ sep [text "case", nest 4 (ppr expr), ptext (sLit "of")],
          nest 2 (pprMatches matches) ]
940

941
ppr_expr (HsIf _ _ e1 e2 e3)
942
  = sep [hsep [text "if", nest 2 (ppr e1), ptext (sLit "then")],
Ian Lynagh's avatar
Ian Lynagh committed
943
         nest 4 (ppr e2),
944
         text "else",
Ian Lynagh's avatar
Ian Lynagh committed
945
         nest 4 (ppr e3)]
946

947
ppr_expr (HsMultiIf _ alts)
948
  = hang (text "if") 3  (vcat (map ppr_alt alts))
949
  where ppr_alt (L _ (GRHS _ guards expr)) =
950 951 952 953 954 955
          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) ]
956
        ppr_alt (L _ (XGRHS x)) = ppr x
957

958
-- special case: let ... in let ...
959
ppr_expr (HsLet _ (L _ binds) expr@(L _ (HsLet _ _ _)))
960
  = sep [hang (text "let") 2 (hsep [pprBinds binds, ptext (sLit "in")]),
Ian Lynagh's avatar
Ian Lynagh committed
961
         ppr_lexpr expr]
962

963
ppr_expr (HsLet _ (L _ binds) expr)
964 965
  = sep [hang (text "let") 2 (pprBinds binds),
         hang (text "in")  2 (ppr expr)]
966

967
ppr_expr (HsDo _ do_or_list_comp (L _ stmts)) = pprDo do_or_list_comp stmts
968

969
ppr_expr (ExplicitList _ _ exprs)
970
  = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
971

972
ppr_expr (RecordCon { rcon_con_name = con_id, rcon_flds = rbinds })
Lemmih's avatar
Lemmih committed
973
  = hang (ppr con_id) 2 (ppr rbinds)
974

975
ppr_expr (RecordUpd { rupd_expr = L _ aexp, rupd_flds <