HsExpr.hs 82.1 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 13

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

16
#include "HsVersions.h"
17 18

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

-- others:
27
import TcEvidence
28
import CoreSyn
29 30 31
import Var
import Name
import BasicTypes
Matthew Pickering's avatar
Matthew Pickering committed
32
import ConLike
33
import SrcLoc
34
import Util
35
import StaticFlags( opt_PprStyle_Debug )
Ian Lynagh's avatar
Ian Lynagh committed
36
import Outputable
37
import FastString
38
import Type
39 40 41

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

Austin Seipp's avatar
Austin Seipp committed
44 45 46
{-
************************************************************************
*                                                                      *
47
\subsection{Expressions proper}
Austin Seipp's avatar
Austin Seipp committed
48 49 50
*                                                                      *
************************************************************************
-}
51

52 53
-- * Expressions proper

54
type LHsExpr id = Located (HsExpr id)
Alan Zimmerman's avatar
Alan Zimmerman committed
55 56
  -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
  --   in a list
57

58 59
  -- For details on above see note [Api annotations] in ApiAnnotation

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

noPostTcExpr :: PostTcExpr
69
noPostTcExpr = HsLit (HsString "" (fsLit "noPostTcExpr"))
70 71 72 73 74

noPostTcTable :: PostTcTable
noPostTcTable = []

-------------------------
75
-- | SyntaxExpr is like 'PostTcExpr', but it's filled in a little earlier,
Ian Lynagh's avatar
Ian Lynagh committed
76
-- by the renamer.  It's used for rebindable syntax.
77 78 79
--
-- E.g. @(>>=)@ is filled in before the renamer by the appropriate 'Name' for
--      @(>>=)@, and then instantiated by the type checker with its type args
80
--      etc
81 82 83

type SyntaxExpr id = HsExpr id

Ian Lynagh's avatar
Ian Lynagh committed
84 85
noSyntaxExpr :: SyntaxExpr id -- Before renaming, and sometimes after,
                              -- (if the syntax slot makes no sense)
86
noSyntaxExpr = HsLit (HsString "" (fsLit "noSyntaxExpr"))
87 88


89 90
type CmdSyntaxTable id = [(Name, SyntaxExpr id)]
-- See Note [CmdSyntaxTable]
91

Austin Seipp's avatar
Austin Seipp committed
92
{-
93 94 95 96 97
Note [CmdSyntaxtable]
~~~~~~~~~~~~~~~~~~~~~
Used only for arrow-syntax stuff (HsCmdTop), the CmdSyntaxTable keeps
track of the methods needed for a Cmd.

98
* Before the renamer, this list is an empty list
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

* 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
126
-}
127

128
-- | A Haskell expression.
129
data HsExpr id
130
  = HsVar     id             -- ^ Variable
131

132 133 134 135 136 137
  | HsUnboundVar OccName     -- ^ Unbound variable; also used for "holes" _, or _x.
                             -- Turned from HsVar to HsUnboundVar by the renamer, when
                             --   it finds an out-of-scope variable
                             -- Turned into HsVar by type checker, to support deferred
                             --   type errors.  (The HsUnboundVar only has an OccName.)

138
  | HsRecFld (AmbiguousFieldOcc id) -- ^ Variable pointing to record selector
139

Adam Gundry's avatar
Adam Gundry committed
140 141
  | HsOverLabel FastString   -- ^ Overloaded label (See Note [Overloaded labels]
                             --   in GHC.OverloadedLabels)
142 143 144 145
  | HsIPVar   HsIPName       -- ^ Implicit parameter
  | HsOverLit (HsOverLit id) -- ^ Overloaded literals

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

147
  | HsLam     (MatchGroup id (LHsExpr id)) -- ^ Lambda abstraction. Currently always a single match
Alan Zimmerman's avatar
Alan Zimmerman committed
148 149 150
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
       --       'ApiAnnotation.AnnRarrow',
151

152 153
       -- For details on above see note [Api annotations] in ApiAnnotation

154
  | HsLamCase (PostTc id Type) (MatchGroup id (LHsExpr id)) -- ^ Lambda-case
Alan Zimmerman's avatar
Alan Zimmerman committed
155 156 157 158
       --
       -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLam',
       --           'ApiAnnotation.AnnCase','ApiAnnotation.AnnOpen',
       --           'ApiAnnotation.AnnClose'
Alan Zimmerman's avatar
Alan Zimmerman committed
159

160 161
       -- For details on above see note [Api annotations] in ApiAnnotation

162
  | HsApp     (LHsExpr id) (LHsExpr id) -- ^ Application
163

164
  -- | Operator applications:
165 166
  -- NB Bracketed ops such as (+) come out as Vars.

167 168 169
  -- 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
170 171
  | OpApp       (LHsExpr id)    -- left operand
                (LHsExpr id)    -- operator
172
                (PostRn id Fixity) -- Renamer adds fixity; bottom until then
Ian Lynagh's avatar
Ian Lynagh committed
173 174
                (LHsExpr id)    -- right operand

175
  -- | Negation operator. Contains the negated expression and the name
Alan Zimmerman's avatar
Alan Zimmerman committed
176 177 178
  -- of 'negate'
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnMinus'
179 180

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

Alan Zimmerman's avatar
Alan Zimmerman committed
184 185
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
  --             'ApiAnnotation.AnnClose' @')'@
186 187

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

190
  | SectionL    (LHsExpr id)    -- operand; see Note [Sections in HsSyn]
Ian Lynagh's avatar
Ian Lynagh committed
191
                (LHsExpr id)    -- operator
192
  | SectionR    (LHsExpr id)    -- operator; see Note [Sections in HsSyn]
Ian Lynagh's avatar
Ian Lynagh committed
193 194
                (LHsExpr id)    -- operand

195
  -- | Used for explicit tuples and sections thereof
Alan Zimmerman's avatar
Alan Zimmerman committed
196 197 198
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnClose'
199 200

  -- For details on above see note [Api annotations] in ApiAnnotation
201 202
  | ExplicitTuple
        [LHsTupArg id]
203 204
        Boxity

Alan Zimmerman's avatar
Alan Zimmerman committed
205
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnCase',
Alan Zimmerman's avatar
Alan Zimmerman committed
206 207
  --       'ApiAnnotation.AnnOf','ApiAnnotation.AnnOpen' @'{'@,
  --       'ApiAnnotation.AnnClose' @'}'@
208 209

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

Alan Zimmerman's avatar
Alan Zimmerman committed
213 214
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf',
  --       'ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
215
  --       'ApiAnnotation.AnnThen','ApiAnnotation.AnnSemi',
Alan Zimmerman's avatar
Alan Zimmerman committed
216
  --       'ApiAnnotation.AnnElse',
217 218

  -- For details on above see note [Api annotations] in ApiAnnotation
219
  | HsIf        (Maybe (SyntaxExpr id)) -- cond function
220 221
                                        -- Nothing => use the built-in 'if'
                                        -- See Note [Rebindable if]
222
                (LHsExpr id)    --  predicate
Ian Lynagh's avatar
Ian Lynagh committed
223 224 225
                (LHsExpr id)    --  then part
                (LHsExpr id)    --  else part

226
  -- | Multi-way if
Alan Zimmerman's avatar
Alan Zimmerman committed
227 228 229
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnIf'
  --       'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
230 231

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

234
  -- | let(rec)
Alan Zimmerman's avatar
Alan Zimmerman committed
235 236
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnLet',
Alan Zimmerman's avatar
Alan Zimmerman committed
237 238
  --       'ApiAnnotation.AnnOpen' @'{'@,
  --       'ApiAnnotation.AnnClose' @'}'@,'ApiAnnotation.AnnIn'
239 240

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

Alan Zimmerman's avatar
Alan Zimmerman committed
244 245 246 247
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDo',
  --             'ApiAnnotation.AnnOpen', 'ApiAnnotation.AnnSemi',
  --             'ApiAnnotation.AnnVbar',
  --             'ApiAnnotation.AnnClose'
248 249

  -- For details on above see note [Api annotations] in ApiAnnotation
250 251 252 253 254
  | 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
255

256
  -- | Syntactic list: [a,b,c,...]
Alan Zimmerman's avatar
Alan Zimmerman committed
257
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
258 259
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
  --              'ApiAnnotation.AnnClose' @']'@
260 261

  -- For details on above see note [Api annotations] in ApiAnnotation
262 263
  | ExplicitList
                (PostTc id Type)        -- Gives type of components of list
264
                (Maybe (SyntaxExpr id)) -- For OverloadedLists, the fromListN witness
Ian Lynagh's avatar
Ian Lynagh committed
265 266
                [LHsExpr id]

267
  -- | Syntactic parallel array: [:e1, ..., en:]
Alan Zimmerman's avatar
Alan Zimmerman committed
268
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
269
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
Alan Zimmerman's avatar
Alan Zimmerman committed
270 271
  --              'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnComma',
  --              'ApiAnnotation.AnnVbar'
Alan Zimmerman's avatar
Alan Zimmerman committed
272
  --              'ApiAnnotation.AnnClose' @':]'@
273 274

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

279
  -- | Record construction
Alan Zimmerman's avatar
Alan Zimmerman committed
280
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
281 282
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
  --         'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
283 284

  -- For details on above see note [Api annotations] in ApiAnnotation
285 286 287 288 289 290
  | 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
291

292
  -- | Record update
Alan Zimmerman's avatar
Alan Zimmerman committed
293
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
294 295
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
  --         'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose' @'}'@
296 297

  -- For details on above see note [Api annotations] in ApiAnnotation
298 299 300 301
  | RecordUpd
      { rupd_expr :: LHsExpr id
      , rupd_flds :: [LHsRecUpdField id]
      , rupd_cons :: PostTc id [ConLike]
302 303 304
                -- Filled in by the type checker to the
                -- _non-empty_ list of DataCons that have
                -- all the upd'd fields
305 306 307 308 309 310 311

      , 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
312 313 314
  -- For a type family, the arg types are of the *instance* tycon,
  -- not the family tycon

Alan Zimmerman's avatar
Alan Zimmerman committed
315 316 317
  -- | Expression with an explicit type signature. @e :: type@
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
318 319

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
320
  | ExprWithTySig
321
                (LHsExpr id)
Ian Lynagh's avatar
Ian Lynagh committed
322
                (LHsType id)
thomasw's avatar
thomasw committed
323 324 325 326
                (PostRn id [Name])      -- After renaming, the list of Names
                                        -- contains the named and unnamed
                                        -- wildcards brought in scope by the
                                        -- signature
Ian Lynagh's avatar
Ian Lynagh committed
327 328 329 330 331 332

  | ExprWithTySigOut                    -- TRANSLATION
                (LHsExpr id)
                (LHsType Name)          -- Retain the signature for
                                        -- round-tripping purposes

333
  -- | Arithmetic sequence
Alan Zimmerman's avatar
Alan Zimmerman committed
334
  --
Alan Zimmerman's avatar
Alan Zimmerman committed
335 336 337
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
  --              'ApiAnnotation.AnnComma','ApiAnnotation.AnnDotdot',
  --              'ApiAnnotation.AnnClose' @']'@
338 339

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
340
  | ArithSeq
Ian Lynagh's avatar
Ian Lynagh committed
341
                PostTcExpr
342
                (Maybe (SyntaxExpr id))   -- For OverloadedLists, the fromList witness
Ian Lynagh's avatar
Ian Lynagh committed
343 344
                (ArithSeqInfo id)

345
  -- | Arithmetic sequence for parallel array
Alan Zimmerman's avatar
Alan Zimmerman committed
346 347 348 349 350 351 352
  --
  -- > [:e1..e2:] or [:e1, e2..e3:]
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
  --              'ApiAnnotation.AnnComma','ApiAnnotation.AnnDotdot',
  --              'ApiAnnotation.AnnVbar',
  --              'ApiAnnotation.AnnClose' @':]'@
353 354

  -- For details on above see note [Api annotations] in ApiAnnotation
Austin Seipp's avatar
Austin Seipp committed
355
  | PArrSeq
Alan Zimmerman's avatar
Alan Zimmerman committed
356
                PostTcExpr
Ian Lynagh's avatar
Ian Lynagh committed
357 358
                (ArithSeqInfo id)

Alan Zimmerman's avatar
Alan Zimmerman committed
359 360 361
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{-\# SCC'@,
  --             'ApiAnnotation.AnnVal' or 'ApiAnnotation.AnnValStr',
  --              'ApiAnnotation.AnnClose' @'\#-}'@
362 363

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
364
  | HsSCC       SourceText            -- Note [Pragma source text] in BasicTypes
365 366
                StringLiteral         -- "set cost centre" SCC pragma
                (LHsExpr id)          -- expr whose cost is to be measured
Alan Zimmerman's avatar
Alan Zimmerman committed
367 368 369

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

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

376
  -----------------------------------------------------------
377
  -- MetaHaskell Extensions
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
378

Alan Zimmerman's avatar
Alan Zimmerman committed
379 380 381
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose',
  --         'ApiAnnotation.AnnClose'
382 383

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

386 387 388 389 390
    -- 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
391

392 393 394 395 396
  | 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
397

Alan Zimmerman's avatar
Alan Zimmerman committed
398 399
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
  --         'ApiAnnotation.AnnClose'
400 401

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

404 405 406
  -----------------------------------------------------------
  -- Arrow notation extension

407
  -- | @proc@ notation for Arrows
Alan Zimmerman's avatar
Alan Zimmerman committed
408 409 410
  --
  --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnProc',
  --          'ApiAnnotation.AnnRarrow'
411 412

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

417 418
  ---------------------------------------
  -- static pointers extension
Alan Zimmerman's avatar
Alan Zimmerman committed
419
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnStatic',
420 421

  -- For details on above see note [Api annotations] in ApiAnnotation
422 423
  | HsStatic    (LHsExpr id)

424 425
  ---------------------------------------
  -- The following are commands, not expressions proper
426
  -- They are only used in the parsing stage and are removed
427
  --    immediately in parser.RdrHsSyn.checkCommand
Alan Zimmerman's avatar
Alan Zimmerman committed
428 429 430 431

  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.Annlarrowtail',
  --          'ApiAnnotation.Annrarrowtail','ApiAnnotation.AnnLarrowtail',
  --          'ApiAnnotation.AnnRarrowtail'
432 433

  -- For details on above see note [Api annotations] in ApiAnnotation
434 435 436 437 438 439 440 441 442
  | 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
443 444
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(|'@,
  --         'ApiAnnotation.AnnClose' @'|)'@
445 446

  -- For details on above see note [Api annotations] in ApiAnnotation
447 448 449 450 451 452 453
  | 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
454

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
455 456 457
  ---------------------------------------
  -- Haskell program coverage (Hpc) Support

Ian Lynagh's avatar
Ian Lynagh committed
458
  | HsTick
459
     (Tickish id)
Ian Lynagh's avatar
Ian Lynagh committed
460
     (LHsExpr id)                       -- sub-expression
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
461 462

  | HsBinTick
Ian Lynagh's avatar
Ian Lynagh committed
463 464 465
     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
466

Alan Zimmerman's avatar
Alan Zimmerman committed
467
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
Alan Zimmerman's avatar
Alan Zimmerman committed
468 469 470
  --       'ApiAnnotation.AnnOpen' @'{-\# GENERATED'@,
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnColon','ApiAnnotation.AnnVal',
Alan Zimmerman's avatar
Alan Zimmerman committed
471
  --       'ApiAnnotation.AnnMinus',
Alan Zimmerman's avatar
Alan Zimmerman committed
472 473 474
  --       'ApiAnnotation.AnnVal','ApiAnnotation.AnnColon',
  --       'ApiAnnotation.AnnVal',
  --       'ApiAnnotation.AnnClose' @'\#-}'@
475 476

  -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
477 478
  | HsTickPragma                      -- A pragma introduced tick
     SourceText                       -- Note [Pragma source text] in BasicTypes
479
     (StringLiteral,(Int,Int),(Int,Int))
480
                                      -- external span for this tick
Ian Lynagh's avatar
Ian Lynagh committed
481
     (LHsExpr id)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
482 483 484 485

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

Ian Lynagh's avatar
Ian Lynagh committed
487
  | EWildPat                 -- wildcard
488

Alan Zimmerman's avatar
Alan Zimmerman committed
489
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnAt'
490 491

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

Alan Zimmerman's avatar
Alan Zimmerman committed
495
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow'
496 497

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

Alan Zimmerman's avatar
Alan Zimmerman committed
501
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
502 503

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

Ian Lynagh's avatar
Ian Lynagh committed
506
  | HsType      (LHsType id) -- Explicit type argument; e.g  f {| Int |} x y
507

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
508 509
  ---------------------------------------
  -- Finally, HsWrap appears only in typechecker output
510

Ian Lynagh's avatar
Ian Lynagh committed
511 512
  |  HsWrap     HsWrapper    -- TRANSLATION
                (HsExpr id)
513

514 515
  deriving (Typeable)
deriving instance (DataId id) => Data (HsExpr id)
516

517
-- | HsTupArg is used for tuple sections
518
--  (,a,) is represented by  ExplicitTuple [Missing ty1, Present a, Missing ty3]
519
--  Which in turn stands for (\x:ty1 \y:ty2. (x,a,y))
520
type LHsTupArg id = Located (HsTupArg id)
Alan Zimmerman's avatar
Alan Zimmerman committed
521
-- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma'
522 523

-- For details on above see note [Api annotations] in ApiAnnotation
524
data HsTupArg id
525 526 527 528
  = 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)
529

530 531 532
tupArgPresent :: LHsTupArg id -> Bool
tupArgPresent (L _ (Present {})) = True
tupArgPresent (L _ (Missing {})) = False
533

Austin Seipp's avatar
Austin Seipp committed
534
{-
535 536 537 538 539 540 541
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)'

542
  * HsPars are pretty printed as '( .. )' regardless of whether
543 544 545 546 547 548 549 550
    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
551 552
         HsPar (SectionR ...)
The parser parses sections in a wider variety of situations
553
(See Note [Parsing sections]), but the renamer checks for those
554
parens.  This invariant makes pretty-printing easier; we don't need
555 556
a special case for adding the parens round sections.

557 558 559 560 561 562
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?
563
Because we allow an 'if' to return *unboxed* results, thus
564 565 566 567 568
  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
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

Note [Record Update HsWrapper]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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.

For example, if we have the record pattern synonym P,

```
pattern P :: (Show a) => a -> Maybe a
pattern P{x} = Just x

foo = (Just True) { x = False }
```

then `foo` desugars to something like

```
P x = P False
```

hence we need to provide the correct dictionaries to P on the RHS so that we can
build the expression.

Austin Seipp's avatar
Austin Seipp committed
595
-}
596

597
instance OutputableBndr id => Outputable (HsExpr id) where
598
    ppr expr = pprExpr expr
599

600
-----------------------
Ian Lynagh's avatar
Ian Lynagh committed
601
-- pprExpr, pprLExpr, pprBinds call pprDeeper;
602 603
-- the underscore versions do not
pprLExpr :: OutputableBndr id => LHsExpr id -> SDoc
604 605 606
pprLExpr (L _ e) = pprExpr e

pprExpr :: OutputableBndr id => HsExpr id -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
607 608 609 610 611 612 613 614 615 616 617 618
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 (...)
isQuietHsExpr (HsPar _) = True
-- applications don't display anything themselves
isQuietHsExpr (HsApp _ _) = True
isQuietHsExpr (OpApp _ _ _ _) = True
isQuietHsExpr _ = False
619

Ian Lynagh's avatar
Ian Lynagh committed
620 621
pprBinds :: (OutputableBndr idL, OutputableBndr idR)
         => HsLocalBindsLR idL idR -> SDoc
622
pprBinds b = pprDeeper (ppr b)
623

624
-----------------------
625 626
ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc
ppr_lexpr e = ppr_expr (unLoc e)
627

628
ppr_expr :: forall id. OutputableBndr id => HsExpr id -> SDoc
629 630 631
ppr_expr (HsVar v)        = pprPrefixOcc v
ppr_expr (HsUnboundVar v) = pprPrefixOcc v
ppr_expr (HsIPVar v)      = ppr v
Adam Gundry's avatar
Adam Gundry committed
632
ppr_expr (HsOverLabel l)  = char '#' <> ppr l
633 634 635
ppr_expr (HsLit lit)      = ppr lit
ppr_expr (HsOverLit lit)  = ppr lit
ppr_expr (HsPar e)        = parens (ppr_lexpr e)
636

637
ppr_expr (HsCoreAnn _ (StringLiteral _ s) e)
Ian Lynagh's avatar
Ian Lynagh committed
638
  = vcat [ptext (sLit "HsCoreAnn") <+> ftext s, ppr_lexpr e]
639

640 641
ppr_expr (HsApp e1 e2)
  = let (fun, args) = collect_args e1 [e2] in
642
    hang (ppr_lexpr fun) 2 (sep (map pprParendExpr args))
643
  where
644 645
    collect_args (L _ (HsApp fun arg)) args = collect_args fun (arg:args)
    collect_args fun args = (fun, args)
646

Ian Lynagh's avatar
Ian Lynagh committed
647
ppr_expr (OpApp e1 op _ e2)
648
  = case unLoc op of
649
      HsVar v -> pp_infixly v
Ian Lynagh's avatar
Ian Lynagh committed
650
      _       -> pp_prefixly
651
  where
Ian Lynagh's avatar
Ian Lynagh committed
652
    pp_e1 = pprDebugParendExpr e1   -- In debug mode, add parens
653
    pp_e2 = pprDebugParendExpr e2   -- to make precedence clear
654 655

    pp_prefixly
656
      = hang (ppr op) 2 (sep [pp_e1, pp_e2])
657 658

    pp_infixly v
659
      = sep [pp_e1, sep [pprInfixOcc v, nest 2 pp_e2]]
660

661
ppr_expr (NegApp e _) = char '-' <+> pprDebugParendExpr e
662

663
ppr_expr (SectionL expr op)
664
  = case unLoc op of
665
      HsVar v -> pp_infixly v
Ian Lynagh's avatar
Ian Lynagh committed
666
      _       -> pp_prefixly
667
  where
668
    pp_expr = pprDebugParendExpr expr
669

670
    pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
Ian Lynagh's avatar
Ian Lynagh committed
671
                       4 (hsep [pp_expr, ptext (sLit "x_ )")])
672
    pp_infixly v = (sep [pp_expr, pprInfixOcc v])
673

674
ppr_expr (SectionR op expr)
675
  = case unLoc op of
676
      HsVar v -> pp_infixly v
Ian Lynagh's avatar
Ian Lynagh committed
677
      _       -> pp_prefixly
678
  where
679
    pp_expr = pprDebugParendExpr expr
680

Ian Lynagh's avatar
Ian Lynagh committed
681
    pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext (sLit "x_")])
682
                       4 (pp_expr <> rparen)
683
    pp_infixly v = sep [pprInfixOcc v, pp_expr]
684

685
ppr_expr (ExplicitTuple exprs boxity)
686
  = tupleParens (boxityTupleSort boxity) (fcat (ppr_tup_args $ map unLoc exprs))
687 688 689
  where
    ppr_tup_args []               = []
    ppr_tup_args (Present e : es) = (ppr_lexpr e <> punc es) : ppr_tup_args es
690
    ppr_tup_args (Missing _ : es) = punc es : ppr_tup_args es
691 692 693 694 695

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

696 697
ppr_expr (HsLam matches)
  = pprMatches (LambdaExpr :: HsMatchContext id) matches
698

699 700 701 702
ppr_expr (HsLamCase _ matches)
  = sep [ sep [ptext (sLit "\\case {")],
          nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ]

703
ppr_expr (HsCase expr matches)
704
  = sep [ sep [ptext (sLit "case"), nest 4 (ppr expr), ptext (sLit "of {")],
705
          nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ]
706

707
ppr_expr (HsIf _ e1 e2 e3)
Ian Lynagh's avatar
Ian Lynagh committed
708
  = sep [hsep [ptext (sLit "if"), nest 2 (ppr e1), ptext (sLit "then")],
Ian Lynagh's avatar
Ian Lynagh committed
709
         nest 4 (ppr e2),
Ian Lynagh's avatar
Ian Lynagh committed
710
         ptext (sLit "else"),
Ian Lynagh's avatar
Ian Lynagh committed
711
         nest 4 (ppr e3)]
712

713 714 715
ppr_expr (HsMultiIf _ alts)
  = sep $ ptext (sLit "if") : map ppr_alt alts
  where ppr_alt (L _ (GRHS guards expr)) =
716
          sep [ vbar <+> interpp'SP guards
717 718
              , ptext (sLit "->") <+> pprDeeper (ppr expr) ]

719
-- special case: let ... in let ...
720
ppr_expr (HsLet (L _ binds) expr@(L _ (HsLet _ _)))
Ian Lynagh's avatar
Ian Lynagh committed
721
  = sep [hang (ptext (sLit "let")) 2 (hsep [pprBinds binds, ptext (sLit "in")]),
Ian Lynagh's avatar
Ian Lynagh committed
722
         ppr_lexpr expr]
723

724
ppr_expr (HsLet (L _ binds) expr)
Ian Lynagh's avatar
Ian Lynagh committed
725 726
  = sep [hang (ptext (sLit "let")) 2 (pprBinds binds),
         hang (ptext (sLit "in"))  2 (ppr expr)]
727

728
ppr_expr (HsDo do_or_list_comp (L _ stmts) _) = pprDo do_or_list_comp stmts
729

730
ppr_expr (ExplicitList _ _ exprs)
731
  = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
732

733
ppr_expr (ExplicitPArr _ exprs)
734
  = paBrackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs)))
735

736
ppr_expr (RecordCon { rcon_con_name = con_id, rcon_flds = rbinds })
737
  = hang (ppr con_id) 2 (ppr rbinds)
738

739
ppr_expr (RecordUpd { rupd_expr = aexp, rupd_flds = rbinds })
740
  = hang (pprLExpr aexp) 2 (braces (fsep (punctuate comma (map ppr rbinds))))
741

thomasw's avatar
thomasw committed
742
ppr_expr (ExprWithTySig expr sig _)
743
  = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
Ian Lynagh's avatar
Ian Lynagh committed
744
         4 (ppr sig)
745 746
ppr_expr (ExprWithTySigOut expr sig)
  = hang (nest 2 (ppr_lexpr expr) <+> dcolon)
Ian Lynagh's avatar
Ian Lynagh committed
747
         4 (ppr sig)
748

749
ppr_expr (ArithSeq _ _ info) = brackets (ppr info)
750
ppr_expr (PArrSeq  _ info) = paBrackets (ppr info)
751

Ian Lynagh's avatar
Ian Lynagh committed
752 753 754
ppr_expr EWildPat       = char '_'
ppr_expr (ELazyPat e)   = char '~' <> pprParendExpr e
ppr_expr (EAsPat v e)   = ppr v <> char '@' <> pprParendExpr e
Ian Lynagh's avatar
Ian Lynagh committed
755
ppr_expr (EViewPat p e) = ppr p <+> ptext (sLit "->") <+> ppr e
756

757
ppr_expr (HsSCC _ (StringLiteral _ lbl) expr)
758
  = sep [ ptext (sLit "{-# SCC") <+> doubleQuotes (ftext lbl) <+> ptext (sLit "#-}"),
Ian Lynagh's avatar
Ian Lynagh committed
759
          pprParendExpr expr ]
760

761
ppr_expr (HsWrap co_fn e) = pprHsWrapper (pprExpr e) co_fn
Ian Lynagh's avatar
Ian Lynagh committed
762
ppr_expr (HsType id)      = ppr id
763

764
ppr_expr (HsSpliceE s)         = pprSplice s
765
ppr_expr (HsBracket b)         = pprHsBracket b
766 767
ppr_expr (HsRnBracketOut e []) = ppr e
ppr_expr (HsRnBracketOut e ps) = ppr e $$ ptext (sLit "pending(rn)") <+> ppr ps
768 769
ppr_expr (HsTcBracketOut e []) = ppr e
ppr_expr (HsTcBracketOut e ps) = ppr e $$ ptext (sLit "pending(tc)") <+> ppr ps
770

771
ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _)))
Ian Lynagh's avatar
Ian Lynagh committed
772
  = hsep [ptext (sLit "proc"), ppr pat, ptext (sLit "->"), ppr cmd]
773

774 775 776
ppr_expr (HsStatic e)
  = hsep [ptext (sLit "static"), pprParendExpr e]

777
ppr_expr (HsTick tickish exp)
778
  = pprTicks (ppr exp) $
Peter Wortmann's avatar
Peter Wortmann committed
779
    ppr tickish <+> ppr_lexpr exp
andy@galois.com's avatar
andy@galois.com committed
780
ppr_expr (HsBinTick tickIdTrue tickIdFalse exp)
781 782
  = pprTicks (ppr exp) $
    hcat [ptext (sLit "bintick<"),
Ian Lynagh's avatar
Ian Lynagh committed
783
          ppr tickIdTrue,
Ian Lynagh's avatar
Ian Lynagh committed
784
          ptext (sLit ","),
Ian Lynagh's avatar
Ian Lynagh committed
785
          ppr tickIdFalse,
Ian Lynagh's avatar
Ian Lynagh committed
786 787
          ptext (sLit ">("),
          ppr exp,ptext (sLit ")")]
Alan Zimmerman's avatar
Alan Zimmerman committed
788
ppr_expr (HsTickPragma _ externalSrcLoc exp)
789 790
  = pprTicks (ppr exp) $
    hcat [ptext (sLit "tickpragma<"),
791
          pprExternalSrcLoc externalSrcLoc,
Ian Lynagh's avatar
Ian Lynagh committed
792
          ptext (sLit ">("),
Ian Lynagh's avatar
Ian Lynagh committed
793
          ppr exp,
Ian Lynagh's avatar
Ian Lynagh committed
794
          ptext (sLit ")")]
andy@galois.com's avatar
andy@galois.com committed
795

796
ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True)
797
  = hsep [ppr_lexpr arrow, larrowt, ppr_lexpr arg]
798
ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False)
799
  = hsep [ppr_lexpr arg, arrowt, ppr_lexpr arrow]
800
ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp True)
801
  = hsep [ppr_lexpr arrow, larrowtt, ppr_lexpr arg]
802
ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp False)
803
  = hsep [ppr_lexpr arg, arrowtt, ppr_lexpr arrow]
804

805
ppr_expr (HsArrForm (L _ (HsVar v)) (Just _) [arg1, arg2])
806
  = sep [pprCmdArg (unLoc arg1), hsep [pprInfixOcc v, pprCmdArg (unLoc arg2)]]
807
ppr_expr (HsArrForm op _ args)
808 809
  = hang (ptext (sLit "(|") <+> ppr_lexpr op)
         4 (sep (map (pprCmdArg.unLoc) args) <+> ptext (sLit "|)"))
810
ppr_expr (HsRecFld f) = ppr f
811

812 813 814 815
pprExternalSrcLoc :: (StringLiteral,(Int,Int),(Int,Int)) -> SDoc
pprExternalSrcLoc (StringLiteral _ src,(n1,n2),(n3,n4))
  = ppr (src,(n1,n2),(n3,n4))

Austin Seipp's avatar
Austin Seipp committed
816
{-
817 818 819 820 821 822
HsSyn records exactly where the user put parens, with HsPar.
So generally speaking we print without adding any parens.
However, some code is internally generated, and in some places
parens are absolutely required; so for these places we use
pprParendExpr (but don't print double parens of course).

thomie's avatar
thomie committed
823
For operator applications we don't add parens, because the operator
824 825
fixities should do the job, except in debug mode (-dppr-debug) so we
can see the structure of the parse tree.
Austin Seipp's avatar
Austin Seipp committed
826
-}
827 828 829 830 831

pprDebugParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
pprDebugParendExpr expr
  = getPprStyle (\sty ->
    if debugStyle sty then pprParendExpr expr
Ian Lynagh's avatar
Ian Lynagh committed
832 833
                      else pprLExpr      expr)

834
pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc
835
pprParendExpr expr
836 837
  | hsExprNeedsParens (unLoc expr) = parens (pprLExpr expr)
  | otherwise                      = pprLExpr expr
Ian Lynagh's avatar
Ian Lynagh committed
838 839
        -- Using pprLExpr makes sure that we go 'deeper'
        -- I think that is usually (always?) right
840 841

hsExprNeedsParens :: HsExpr id -> Bool
842
-- True of expressions for which '(e)' and 'e'
843 844 845 846 847 848
-- mean the same thing
hsExprNeedsParens (ArithSeq {})       = False
hsExprNeedsParens (PArrSeq {})        = False
hsExprNeedsParens (HsLit {})          = False
hsExprNeedsParens (HsOverLit {})      = False
hsExprNeedsParens (HsVar {})          = False
849
hsExprNeedsParens (HsUnboundVar {})   = False
850
hsExprNeedsParens (HsIPVar {})        = False
Adam Gundry's avatar
Adam Gundry committed
851
hsExprNeedsParens (HsOverLabel {})    = False
852 853 854
hsExprNeedsParens (ExplicitTuple {})  = False
hsExprNeedsParens (ExplicitList {})   = False
hsExprNeedsParens (ExplicitPArr {})   = False
855 856
hsExprNeedsParens (RecordCon {})      = False
hsExprNeedsParens (RecordUpd {})      = False
857 858
hsExprNeedsParens (HsPar {})          = False
hsExprNeedsParens (HsBracket {})      = False
gmainland's avatar
gmainland committed
859
hsExprNeedsParens (HsRnBracketOut {}) = False
860
hsExprNeedsParens (HsTcBracketOut {}) = False
861 862
hsExprNeedsParens (HsDo sc _ _)
       | isListCompExpr sc            = False
863
hsExprNeedsParens (HsRecFld{})        = False
864 865 866
hsExprNeedsParens _ = True


867
isAtomicHsExpr :: HsExpr id -> Bool
868
-- True of a single token
869 870 871 872
isAtomicHsExpr (HsVar {})        = True
isAtomicHsExpr (HsLit {})        = True
isAtomicHsExpr (HsOverLit {})    = True
isAtomicHsExpr (HsIPVar {})      = True
Adam Gundry's avatar
Adam Gundry committed
873
isAtomicHsExpr (HsOverLabel {})  = True
874
isAtomicHsExpr (HsUnboundVar {}) = True
875 876
isAtomicHsExpr (HsWrap _ e)      = isAtomicHsExpr e
isAtomicHsExpr (HsPar e)         = isAtomicHsExpr (unLoc e)
877
isAtomicHsExpr (HsRecFld{})      = True
878
isAtomicHsExpr _                 = False
879

Austin Seipp's avatar
Austin Seipp committed
880 881 882
{-
************************************************************************
*                                                                      *
883
\subsection{Commands (in arrow abstractions)}
Austin Seipp's avatar
Austin Seipp committed
884 885
*                                                                      *
************************************************************************
886 887

We re-use HsExpr to represent these.
Austin Seipp's avatar
Austin Seipp committed
888
-}
889

890
type LHsCmd id = Located (HsCmd id)
891

892
data HsCmd id
Alan Zimmerman's avatar
Alan Zimmerman committed
893 894 895
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.Annlarrowtail',
  --          'ApiAnnotation.Annrarrowtail','ApiAnnotation.AnnLarrowtail',
  --          'ApiAnnotation.AnnRarrowtail'
896 897

  -- For details on above see note [Api annotations] in ApiAnnotation
898 899 900 901 902 903 904 905 906
  = HsCmdArrApp          -- 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
907 908
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(|'@,
  --         'ApiAnnotation.AnnClose' @'|)'@
909 910

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