| 1 | % |
|---|
| 2 | % (c) The University of Glasgow 2006 |
|---|
| 3 | % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 |
|---|
| 4 | % |
|---|
| 5 | \begin{code} |
|---|
| 6 | {-# OPTIONS -fno-warn-tabs #-} |
|---|
| 7 | -- The above warning supression flag is a temporary kludge. |
|---|
| 8 | -- While working on this module you are encouraged to remove it and |
|---|
| 9 | -- detab the module (please do the detabbing in a separate patch). See |
|---|
| 10 | -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces |
|---|
| 11 | -- for details |
|---|
| 12 | |
|---|
| 13 | {-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables #-} |
|---|
| 14 | |
|---|
| 15 | -- | Abstract Haskell syntax for expressions. |
|---|
| 16 | module HsExpr where |
|---|
| 17 | |
|---|
| 18 | #include "HsVersions.h" |
|---|
| 19 | |
|---|
| 20 | -- friends: |
|---|
| 21 | import HsDecls |
|---|
| 22 | import HsPat |
|---|
| 23 | import HsLit |
|---|
| 24 | import HsTypes |
|---|
| 25 | import HsBinds |
|---|
| 26 | |
|---|
| 27 | -- others: |
|---|
| 28 | import TcEvidence |
|---|
| 29 | import CoreSyn |
|---|
| 30 | import Var |
|---|
| 31 | import Name |
|---|
| 32 | import BasicTypes |
|---|
| 33 | import DataCon |
|---|
| 34 | import SrcLoc |
|---|
| 35 | import Util( dropTail ) |
|---|
| 36 | import StaticFlags( opt_PprStyle_Debug ) |
|---|
| 37 | import Outputable |
|---|
| 38 | import FastString |
|---|
| 39 | |
|---|
| 40 | -- libraries: |
|---|
| 41 | import Data.Data hiding (Fixity) |
|---|
| 42 | \end{code} |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | %************************************************************************ |
|---|
| 46 | %* * |
|---|
| 47 | \subsection{Expressions proper} |
|---|
| 48 | %* * |
|---|
| 49 | %************************************************************************ |
|---|
| 50 | |
|---|
| 51 | \begin{code} |
|---|
| 52 | -- * Expressions proper |
|---|
| 53 | |
|---|
| 54 | type LHsExpr id = Located (HsExpr id) |
|---|
| 55 | |
|---|
| 56 | ------------------------- |
|---|
| 57 | -- | PostTcExpr is an evidence expression attached to the syntax tree by the |
|---|
| 58 | -- type checker (c.f. postTcType). |
|---|
| 59 | type PostTcExpr = HsExpr Id |
|---|
| 60 | -- | We use a PostTcTable where there are a bunch of pieces of evidence, more |
|---|
| 61 | -- than is convenient to keep individually. |
|---|
| 62 | type PostTcTable = [(Name, PostTcExpr)] |
|---|
| 63 | |
|---|
| 64 | noPostTcExpr :: PostTcExpr |
|---|
| 65 | noPostTcExpr = HsLit (HsString (fsLit "noPostTcExpr")) |
|---|
| 66 | |
|---|
| 67 | noPostTcTable :: PostTcTable |
|---|
| 68 | noPostTcTable = [] |
|---|
| 69 | |
|---|
| 70 | ------------------------- |
|---|
| 71 | -- | SyntaxExpr is like 'PostTcExpr', but it's filled in a little earlier, |
|---|
| 72 | -- by the renamer. It's used for rebindable syntax. |
|---|
| 73 | -- |
|---|
| 74 | -- E.g. @(>>=)@ is filled in before the renamer by the appropriate 'Name' for |
|---|
| 75 | -- @(>>=)@, and then instantiated by the type checker with its type args |
|---|
| 76 | -- etc |
|---|
| 77 | |
|---|
| 78 | type SyntaxExpr id = HsExpr id |
|---|
| 79 | |
|---|
| 80 | noSyntaxExpr :: SyntaxExpr id -- Before renaming, and sometimes after, |
|---|
| 81 | -- (if the syntax slot makes no sense) |
|---|
| 82 | noSyntaxExpr = HsLit (HsString (fsLit "noSyntaxExpr")) |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | type SyntaxTable id = [(Name, SyntaxExpr id)] |
|---|
| 86 | -- ^ Currently used only for 'CmdTop' (sigh) |
|---|
| 87 | -- |
|---|
| 88 | -- * Before the renamer, this list is 'noSyntaxTable' |
|---|
| 89 | -- |
|---|
| 90 | -- * After the renamer, it takes the form @[(std_name, HsVar actual_name)]@ |
|---|
| 91 | -- For example, for the 'return' op of a monad |
|---|
| 92 | -- |
|---|
| 93 | -- * normal case: @(GHC.Base.return, HsVar GHC.Base.return)@ |
|---|
| 94 | -- |
|---|
| 95 | -- * with rebindable syntax: @(GHC.Base.return, return_22)@ |
|---|
| 96 | -- where @return_22@ is whatever @return@ is in scope |
|---|
| 97 | -- |
|---|
| 98 | -- * After the type checker, it takes the form @[(std_name, <expression>)]@ |
|---|
| 99 | -- where @<expression>@ is the evidence for the method |
|---|
| 100 | |
|---|
| 101 | noSyntaxTable :: SyntaxTable id |
|---|
| 102 | noSyntaxTable = [] |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | ------------------------- |
|---|
| 106 | -- | A Haskell expression. |
|---|
| 107 | data HsExpr id |
|---|
| 108 | = HsVar id -- ^ variable |
|---|
| 109 | | HsIPVar (IPName id) -- ^ implicit parameter |
|---|
| 110 | | HsOverLit (HsOverLit id) -- ^ Overloaded literals |
|---|
| 111 | |
|---|
| 112 | | HsLit HsLit -- ^ Simple (non-overloaded) literals |
|---|
| 113 | |
|---|
| 114 | | HsLam (MatchGroup id) -- Currently always a single match |
|---|
| 115 | |
|---|
| 116 | | HsApp (LHsExpr id) (LHsExpr id) -- Application |
|---|
| 117 | |
|---|
| 118 | -- Operator applications: |
|---|
| 119 | -- NB Bracketed ops such as (+) come out as Vars. |
|---|
| 120 | |
|---|
| 121 | -- NB We need an expr for the operator in an OpApp/Section since |
|---|
| 122 | -- the typechecker may need to apply the operator to a few types. |
|---|
| 123 | |
|---|
| 124 | | OpApp (LHsExpr id) -- left operand |
|---|
| 125 | (LHsExpr id) -- operator |
|---|
| 126 | Fixity -- Renamer adds fixity; bottom until then |
|---|
| 127 | (LHsExpr id) -- right operand |
|---|
| 128 | |
|---|
| 129 | | NegApp (LHsExpr id) -- negated expr |
|---|
| 130 | (SyntaxExpr id) -- Name of 'negate' |
|---|
| 131 | |
|---|
| 132 | | HsPar (LHsExpr id) -- Parenthesised expr; see Note [Parens in HsSyn] |
|---|
| 133 | |
|---|
| 134 | | SectionL (LHsExpr id) -- operand; see Note [Sections in HsSyn] |
|---|
| 135 | (LHsExpr id) -- operator |
|---|
| 136 | | SectionR (LHsExpr id) -- operator; see Note [Sections in HsSyn] |
|---|
| 137 | (LHsExpr id) -- operand |
|---|
| 138 | |
|---|
| 139 | | ExplicitTuple -- Used for explicit tuples and sections thereof |
|---|
| 140 | [HsTupArg id] |
|---|
| 141 | Boxity |
|---|
| 142 | |
|---|
| 143 | | HsCase (LHsExpr id) |
|---|
| 144 | (MatchGroup id) |
|---|
| 145 | |
|---|
| 146 | | HsIf (Maybe (SyntaxExpr id)) -- cond function |
|---|
| 147 | -- Nothing => use the built-in 'if' |
|---|
| 148 | -- See Note [Rebindable if] |
|---|
| 149 | (LHsExpr id) -- predicate |
|---|
| 150 | (LHsExpr id) -- then part |
|---|
| 151 | (LHsExpr id) -- else part |
|---|
| 152 | |
|---|
| 153 | | HsLet (HsLocalBinds id) -- let(rec) |
|---|
| 154 | (LHsExpr id) |
|---|
| 155 | |
|---|
| 156 | | HsDo (HsStmtContext Name) -- The parameterisation is unimportant |
|---|
| 157 | -- because in this context we never use |
|---|
| 158 | -- the PatGuard or ParStmt variant |
|---|
| 159 | [LStmt id] -- "do":one or more stmts |
|---|
| 160 | PostTcType -- Type of the whole expression |
|---|
| 161 | |
|---|
| 162 | | ExplicitList -- syntactic list |
|---|
| 163 | PostTcType -- Gives type of components of list |
|---|
| 164 | [LHsExpr id] |
|---|
| 165 | |
|---|
| 166 | | ExplicitPArr -- syntactic parallel array: [:e1, ..., en:] |
|---|
| 167 | PostTcType -- type of elements of the parallel array |
|---|
| 168 | [LHsExpr id] |
|---|
| 169 | |
|---|
| 170 | -- Record construction |
|---|
| 171 | | RecordCon (Located id) -- The constructor. After type checking |
|---|
| 172 | -- it's the dataConWrapId of the constructor |
|---|
| 173 | PostTcExpr -- Data con Id applied to type args |
|---|
| 174 | (HsRecordBinds id) |
|---|
| 175 | |
|---|
| 176 | -- Record update |
|---|
| 177 | | RecordUpd (LHsExpr id) |
|---|
| 178 | (HsRecordBinds id) |
|---|
| 179 | -- (HsMatchGroup Id) -- Filled in by the type checker to be |
|---|
| 180 | -- -- a match that does the job |
|---|
| 181 | [DataCon] -- Filled in by the type checker to the |
|---|
| 182 | -- _non-empty_ list of DataCons that have |
|---|
| 183 | -- all the upd'd fields |
|---|
| 184 | [PostTcType] -- Argument types of *input* record type |
|---|
| 185 | [PostTcType] -- and *output* record type |
|---|
| 186 | -- For a type family, the arg types are of the *instance* tycon, |
|---|
| 187 | -- not the family tycon |
|---|
| 188 | |
|---|
| 189 | | ExprWithTySig -- e :: type |
|---|
| 190 | (LHsExpr id) |
|---|
| 191 | (LHsType id) |
|---|
| 192 | |
|---|
| 193 | | ExprWithTySigOut -- TRANSLATION |
|---|
| 194 | (LHsExpr id) |
|---|
| 195 | (LHsType Name) -- Retain the signature for |
|---|
| 196 | -- round-tripping purposes |
|---|
| 197 | |
|---|
| 198 | | ArithSeq -- arithmetic sequence |
|---|
| 199 | PostTcExpr |
|---|
| 200 | (ArithSeqInfo id) |
|---|
| 201 | |
|---|
| 202 | | PArrSeq -- arith. sequence for parallel array |
|---|
| 203 | PostTcExpr -- [:e1..e2:] or [:e1, e2..e3:] |
|---|
| 204 | (ArithSeqInfo id) |
|---|
| 205 | |
|---|
| 206 | | HsSCC FastString -- "set cost centre" SCC pragma |
|---|
| 207 | (LHsExpr id) -- expr whose cost is to be measured |
|---|
| 208 | |
|---|
| 209 | | HsCoreAnn FastString -- hdaume: core annotation |
|---|
| 210 | (LHsExpr id) |
|---|
| 211 | |
|---|
| 212 | ----------------------------------------------------------- |
|---|
| 213 | -- MetaHaskell Extensions |
|---|
| 214 | |
|---|
| 215 | | HsBracket (HsBracket id) |
|---|
| 216 | |
|---|
| 217 | | HsBracketOut (HsBracket Name) -- Output of the type checker is |
|---|
| 218 | -- the *original* |
|---|
| 219 | [PendingSplice] -- renamed expression, plus |
|---|
| 220 | -- _typechecked_ splices to be |
|---|
| 221 | -- pasted back in by the desugarer |
|---|
| 222 | |
|---|
| 223 | | HsSpliceE (HsSplice id) |
|---|
| 224 | |
|---|
| 225 | | HsQuasiQuoteE (HsQuasiQuote id) |
|---|
| 226 | -- See Note [Quasi-quote overview] in TcSplice |
|---|
| 227 | |
|---|
| 228 | ----------------------------------------------------------- |
|---|
| 229 | -- Arrow notation extension |
|---|
| 230 | |
|---|
| 231 | | HsProc (LPat id) -- arrow abstraction, proc |
|---|
| 232 | (LHsCmdTop id) -- body of the abstraction |
|---|
| 233 | -- always has an empty stack |
|---|
| 234 | |
|---|
| 235 | --------------------------------------- |
|---|
| 236 | -- The following are commands, not expressions proper |
|---|
| 237 | |
|---|
| 238 | | HsArrApp -- Arrow tail, or arrow application (f -< arg) |
|---|
| 239 | (LHsExpr id) -- arrow expression, f |
|---|
| 240 | (LHsExpr id) -- input expression, arg |
|---|
| 241 | PostTcType -- type of the arrow expressions f, |
|---|
| 242 | -- of the form a t t', where arg :: t |
|---|
| 243 | HsArrAppType -- higher-order (-<<) or first-order (-<) |
|---|
| 244 | Bool -- True => right-to-left (f -< arg) |
|---|
| 245 | -- False => left-to-right (arg >- f) |
|---|
| 246 | |
|---|
| 247 | | HsArrForm -- Command formation, (| e cmd1 .. cmdn |) |
|---|
| 248 | (LHsExpr id) -- the operator |
|---|
| 249 | -- after type-checking, a type abstraction to be |
|---|
| 250 | -- applied to the type of the local environment tuple |
|---|
| 251 | (Maybe Fixity) -- fixity (filled in by the renamer), for forms that |
|---|
| 252 | -- were converted from OpApp's by the renamer |
|---|
| 253 | [LHsCmdTop id] -- argument commands |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | --------------------------------------- |
|---|
| 257 | -- Haskell program coverage (Hpc) Support |
|---|
| 258 | |
|---|
| 259 | | HsTick |
|---|
| 260 | (Tickish id) |
|---|
| 261 | (LHsExpr id) -- sub-expression |
|---|
| 262 | |
|---|
| 263 | | HsBinTick |
|---|
| 264 | Int -- module-local tick number for True |
|---|
| 265 | Int -- module-local tick number for False |
|---|
| 266 | (LHsExpr id) -- sub-expression |
|---|
| 267 | |
|---|
| 268 | | HsTickPragma -- A pragma introduced tick |
|---|
| 269 | (FastString,(Int,Int),(Int,Int)) -- external span for this tick |
|---|
| 270 | (LHsExpr id) |
|---|
| 271 | |
|---|
| 272 | --------------------------------------- |
|---|
| 273 | -- These constructors only appear temporarily in the parser. |
|---|
| 274 | -- The renamer translates them into the Right Thing. |
|---|
| 275 | |
|---|
| 276 | | EWildPat -- wildcard |
|---|
| 277 | |
|---|
| 278 | | EAsPat (Located id) -- as pattern |
|---|
| 279 | (LHsExpr id) |
|---|
| 280 | |
|---|
| 281 | | EViewPat (LHsExpr id) -- view pattern |
|---|
| 282 | (LHsExpr id) |
|---|
| 283 | |
|---|
| 284 | | ELazyPat (LHsExpr id) -- ~ pattern |
|---|
| 285 | |
|---|
| 286 | | HsType (LHsType id) -- Explicit type argument; e.g f {| Int |} x y |
|---|
| 287 | |
|---|
| 288 | --------------------------------------- |
|---|
| 289 | -- Finally, HsWrap appears only in typechecker output |
|---|
| 290 | |
|---|
| 291 | | HsWrap HsWrapper -- TRANSLATION |
|---|
| 292 | (HsExpr id) |
|---|
| 293 | deriving (Data, Typeable) |
|---|
| 294 | |
|---|
| 295 | -- HsTupArg is used for tuple sections |
|---|
| 296 | -- (,a,) is represented by ExplicitTuple [Mising ty1, Present a, Missing ty3] |
|---|
| 297 | -- Which in turn stands for (\x:ty1 \y:ty2. (x,a,y)) |
|---|
| 298 | data HsTupArg id |
|---|
| 299 | = Present (LHsExpr id) -- The argument |
|---|
| 300 | | Missing PostTcType -- The argument is missing, but this is its type |
|---|
| 301 | deriving (Data, Typeable) |
|---|
| 302 | |
|---|
| 303 | tupArgPresent :: HsTupArg id -> Bool |
|---|
| 304 | tupArgPresent (Present {}) = True |
|---|
| 305 | tupArgPresent (Missing {}) = False |
|---|
| 306 | |
|---|
| 307 | type PendingSplice = (Name, LHsExpr Id) -- Typechecked splices, waiting to be |
|---|
| 308 | -- pasted back in by the desugarer |
|---|
| 309 | |
|---|
| 310 | \end{code} |
|---|
| 311 | |
|---|
| 312 | Note [Parens in HsSyn] |
|---|
| 313 | ~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 314 | HsPar (and ParPat in patterns, HsParTy in types) is used as follows |
|---|
| 315 | |
|---|
| 316 | * Generally HsPar is optional; the pretty printer adds parens where |
|---|
| 317 | necessary. Eg (HsApp f (HsApp g x)) is fine, and prints 'f (g x)' |
|---|
| 318 | |
|---|
| 319 | * HsPars are pretty printed as '( .. )' regardless of whether |
|---|
| 320 | or not they are strictly necssary |
|---|
| 321 | |
|---|
| 322 | * HsPars are respected when rearranging operator fixities. |
|---|
| 323 | So a * (b + c) means what it says (where the parens are an HsPar) |
|---|
| 324 | |
|---|
| 325 | Note [Sections in HsSyn] |
|---|
| 326 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 327 | Sections should always appear wrapped in an HsPar, thus |
|---|
| 328 | HsPar (SectionR ...) |
|---|
| 329 | The parser parses sections in a wider variety of situations |
|---|
| 330 | (See Note [Parsing sections]), but the renamer checks for those |
|---|
| 331 | parens. This invariant makes pretty-printing easier; we don't need |
|---|
| 332 | a special case for adding the parens round sections. |
|---|
| 333 | |
|---|
| 334 | Note [Rebindable if] |
|---|
| 335 | ~~~~~~~~~~~~~~~~~~~~ |
|---|
| 336 | The rebindable syntax for 'if' is a bit special, because when |
|---|
| 337 | rebindable syntax is *off* we do not want to treat |
|---|
| 338 | (if c then t else e) |
|---|
| 339 | as if it was an application (ifThenElse c t e). Why not? |
|---|
| 340 | Because we allow an 'if' to return *unboxed* results, thus |
|---|
| 341 | if blah then 3# else 4# |
|---|
| 342 | whereas that would not be possible using a all to a polymorphic function |
|---|
| 343 | (because you can't call a polymorphic function at an unboxed type). |
|---|
| 344 | |
|---|
| 345 | So we use Nothing to mean "use the old built-in typing rule". |
|---|
| 346 | |
|---|
| 347 | \begin{code} |
|---|
| 348 | instance OutputableBndr id => Outputable (HsExpr id) where |
|---|
| 349 | ppr expr = pprExpr expr |
|---|
| 350 | \end{code} |
|---|
| 351 | |
|---|
| 352 | \begin{code} |
|---|
| 353 | ----------------------- |
|---|
| 354 | -- pprExpr, pprLExpr, pprBinds call pprDeeper; |
|---|
| 355 | -- the underscore versions do not |
|---|
| 356 | pprLExpr :: OutputableBndr id => LHsExpr id -> SDoc |
|---|
| 357 | pprLExpr (L _ e) = pprExpr e |
|---|
| 358 | |
|---|
| 359 | pprExpr :: OutputableBndr id => HsExpr id -> SDoc |
|---|
| 360 | pprExpr e | isAtomicHsExpr e || isQuietHsExpr e = ppr_expr e |
|---|
| 361 | | otherwise = pprDeeper (ppr_expr e) |
|---|
| 362 | |
|---|
| 363 | isQuietHsExpr :: HsExpr id -> Bool |
|---|
| 364 | -- Parentheses do display something, but it gives little info and |
|---|
| 365 | -- if we go deeper when we go inside them then we get ugly things |
|---|
| 366 | -- like (...) |
|---|
| 367 | isQuietHsExpr (HsPar _) = True |
|---|
| 368 | -- applications don't display anything themselves |
|---|
| 369 | isQuietHsExpr (HsApp _ _) = True |
|---|
| 370 | isQuietHsExpr (OpApp _ _ _ _) = True |
|---|
| 371 | isQuietHsExpr _ = False |
|---|
| 372 | |
|---|
| 373 | pprBinds :: (OutputableBndr idL, OutputableBndr idR) |
|---|
| 374 | => HsLocalBindsLR idL idR -> SDoc |
|---|
| 375 | pprBinds b = pprDeeper (ppr b) |
|---|
| 376 | |
|---|
| 377 | ----------------------- |
|---|
| 378 | ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc |
|---|
| 379 | ppr_lexpr e = ppr_expr (unLoc e) |
|---|
| 380 | |
|---|
| 381 | ppr_expr :: forall id. OutputableBndr id => HsExpr id -> SDoc |
|---|
| 382 | ppr_expr (HsVar v) = pprPrefixOcc v |
|---|
| 383 | ppr_expr (HsIPVar v) = ppr v |
|---|
| 384 | ppr_expr (HsLit lit) = ppr lit |
|---|
| 385 | ppr_expr (HsOverLit lit) = ppr lit |
|---|
| 386 | ppr_expr (HsPar e) = parens (ppr_lexpr e) |
|---|
| 387 | |
|---|
| 388 | ppr_expr (HsCoreAnn s e) |
|---|
| 389 | = vcat [ptext (sLit "HsCoreAnn") <+> ftext s, ppr_lexpr e] |
|---|
| 390 | |
|---|
| 391 | ppr_expr (HsApp e1 e2) |
|---|
| 392 | = let (fun, args) = collect_args e1 [e2] in |
|---|
| 393 | hang (ppr_lexpr fun) 2 (sep (map pprParendExpr args)) |
|---|
| 394 | where |
|---|
| 395 | collect_args (L _ (HsApp fun arg)) args = collect_args fun (arg:args) |
|---|
| 396 | collect_args fun args = (fun, args) |
|---|
| 397 | |
|---|
| 398 | ppr_expr (OpApp e1 op _ e2) |
|---|
| 399 | = case unLoc op of |
|---|
| 400 | HsVar v -> pp_infixly v |
|---|
| 401 | _ -> pp_prefixly |
|---|
| 402 | where |
|---|
| 403 | pp_e1 = pprDebugParendExpr e1 -- In debug mode, add parens |
|---|
| 404 | pp_e2 = pprDebugParendExpr e2 -- to make precedence clear |
|---|
| 405 | |
|---|
| 406 | pp_prefixly |
|---|
| 407 | = hang (ppr op) 2 (sep [pp_e1, pp_e2]) |
|---|
| 408 | |
|---|
| 409 | pp_infixly v |
|---|
| 410 | = sep [pp_e1, sep [pprInfixOcc v, nest 2 pp_e2]] |
|---|
| 411 | |
|---|
| 412 | ppr_expr (NegApp e _) = char '-' <+> pprDebugParendExpr e |
|---|
| 413 | |
|---|
| 414 | ppr_expr (SectionL expr op) |
|---|
| 415 | = case unLoc op of |
|---|
| 416 | HsVar v -> pp_infixly v |
|---|
| 417 | _ -> pp_prefixly |
|---|
| 418 | where |
|---|
| 419 | pp_expr = pprDebugParendExpr expr |
|---|
| 420 | |
|---|
| 421 | pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op]) |
|---|
| 422 | 4 (hsep [pp_expr, ptext (sLit "x_ )")]) |
|---|
| 423 | pp_infixly v = (sep [pp_expr, pprInfixOcc v]) |
|---|
| 424 | |
|---|
| 425 | ppr_expr (SectionR op expr) |
|---|
| 426 | = case unLoc op of |
|---|
| 427 | HsVar v -> pp_infixly v |
|---|
| 428 | _ -> pp_prefixly |
|---|
| 429 | where |
|---|
| 430 | pp_expr = pprDebugParendExpr expr |
|---|
| 431 | |
|---|
| 432 | pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext (sLit "x_")]) |
|---|
| 433 | 4 ((<>) pp_expr rparen) |
|---|
| 434 | pp_infixly v = sep [pprInfixOcc v, pp_expr] |
|---|
| 435 | |
|---|
| 436 | ppr_expr (ExplicitTuple exprs boxity) |
|---|
| 437 | = tupleParens (boxityNormalTupleSort boxity) (fcat (ppr_tup_args exprs)) |
|---|
| 438 | where |
|---|
| 439 | ppr_tup_args [] = [] |
|---|
| 440 | ppr_tup_args (Present e : es) = (ppr_lexpr e <> punc es) : ppr_tup_args es |
|---|
| 441 | ppr_tup_args (Missing _ : es) = punc es : ppr_tup_args es |
|---|
| 442 | |
|---|
| 443 | punc (Present {} : _) = comma <> space |
|---|
| 444 | punc (Missing {} : _) = comma |
|---|
| 445 | punc [] = empty |
|---|
| 446 | |
|---|
| 447 | --avoid using PatternSignatures for stage1 code portability |
|---|
| 448 | ppr_expr (HsLam matches) |
|---|
| 449 | = pprMatches (LambdaExpr :: HsMatchContext id) matches |
|---|
| 450 | |
|---|
| 451 | ppr_expr (HsCase expr matches) |
|---|
| 452 | = sep [ sep [ptext (sLit "case"), nest 4 (ppr expr), ptext (sLit "of {")], |
|---|
| 453 | nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ] |
|---|
| 454 | |
|---|
| 455 | ppr_expr (HsIf _ e1 e2 e3) |
|---|
| 456 | = sep [hsep [ptext (sLit "if"), nest 2 (ppr e1), ptext (sLit "then")], |
|---|
| 457 | nest 4 (ppr e2), |
|---|
| 458 | ptext (sLit "else"), |
|---|
| 459 | nest 4 (ppr e3)] |
|---|
| 460 | |
|---|
| 461 | -- special case: let ... in let ... |
|---|
| 462 | ppr_expr (HsLet binds expr@(L _ (HsLet _ _))) |
|---|
| 463 | = sep [hang (ptext (sLit "let")) 2 (hsep [pprBinds binds, ptext (sLit "in")]), |
|---|
| 464 | ppr_lexpr expr] |
|---|
| 465 | |
|---|
| 466 | ppr_expr (HsLet binds expr) |
|---|
| 467 | = sep [hang (ptext (sLit "let")) 2 (pprBinds binds), |
|---|
| 468 | hang (ptext (sLit "in")) 2 (ppr expr)] |
|---|
| 469 | |
|---|
| 470 | ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts |
|---|
| 471 | |
|---|
| 472 | ppr_expr (ExplicitList _ exprs) |
|---|
| 473 | = brackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs))) |
|---|
| 474 | |
|---|
| 475 | ppr_expr (ExplicitPArr _ exprs) |
|---|
| 476 | = paBrackets (pprDeeperList fsep (punctuate comma (map ppr_lexpr exprs))) |
|---|
| 477 | |
|---|
| 478 | ppr_expr (RecordCon con_id _ rbinds) |
|---|
| 479 | = hang (ppr con_id) 2 (ppr rbinds) |
|---|
| 480 | |
|---|
| 481 | ppr_expr (RecordUpd aexp rbinds _ _ _) |
|---|
| 482 | = hang (pprParendExpr aexp) 2 (ppr rbinds) |
|---|
| 483 | |
|---|
| 484 | ppr_expr (ExprWithTySig expr sig) |
|---|
| 485 | = hang (nest 2 (ppr_lexpr expr) <+> dcolon) |
|---|
| 486 | 4 (ppr sig) |
|---|
| 487 | ppr_expr (ExprWithTySigOut expr sig) |
|---|
| 488 | = hang (nest 2 (ppr_lexpr expr) <+> dcolon) |
|---|
| 489 | 4 (ppr sig) |
|---|
| 490 | |
|---|
| 491 | ppr_expr (ArithSeq _ info) = brackets (ppr info) |
|---|
| 492 | ppr_expr (PArrSeq _ info) = paBrackets (ppr info) |
|---|
| 493 | |
|---|
| 494 | ppr_expr EWildPat = char '_' |
|---|
| 495 | ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e |
|---|
| 496 | ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e |
|---|
| 497 | ppr_expr (EViewPat p e) = ppr p <+> ptext (sLit "->") <+> ppr e |
|---|
| 498 | |
|---|
| 499 | ppr_expr (HsSCC lbl expr) |
|---|
| 500 | = sep [ ptext (sLit "_scc_") <+> doubleQuotes (ftext lbl), |
|---|
| 501 | pprParendExpr expr ] |
|---|
| 502 | |
|---|
| 503 | ppr_expr (HsWrap co_fn e) = pprHsWrapper (pprExpr e) co_fn |
|---|
| 504 | ppr_expr (HsType id) = ppr id |
|---|
| 505 | |
|---|
| 506 | ppr_expr (HsSpliceE s) = pprSplice s |
|---|
| 507 | ppr_expr (HsBracket b) = pprHsBracket b |
|---|
| 508 | ppr_expr (HsBracketOut e []) = ppr e |
|---|
| 509 | ppr_expr (HsBracketOut e ps) = ppr e $$ ptext (sLit "pending") <+> ppr ps |
|---|
| 510 | ppr_expr (HsQuasiQuoteE qq) = ppr qq |
|---|
| 511 | |
|---|
| 512 | ppr_expr (HsProc pat (L _ (HsCmdTop cmd _ _ _))) |
|---|
| 513 | = hsep [ptext (sLit "proc"), ppr pat, ptext (sLit "->"), ppr cmd] |
|---|
| 514 | |
|---|
| 515 | ppr_expr (HsTick tickish exp) |
|---|
| 516 | = pprTicks (ppr exp) $ |
|---|
| 517 | ppr tickish <+> ppr exp |
|---|
| 518 | ppr_expr (HsBinTick tickIdTrue tickIdFalse exp) |
|---|
| 519 | = pprTicks (ppr exp) $ |
|---|
| 520 | hcat [ptext (sLit "bintick<"), |
|---|
| 521 | ppr tickIdTrue, |
|---|
| 522 | ptext (sLit ","), |
|---|
| 523 | ppr tickIdFalse, |
|---|
| 524 | ptext (sLit ">("), |
|---|
| 525 | ppr exp,ptext (sLit ")")] |
|---|
| 526 | ppr_expr (HsTickPragma externalSrcLoc exp) |
|---|
| 527 | = pprTicks (ppr exp) $ |
|---|
| 528 | hcat [ptext (sLit "tickpragma<"), |
|---|
| 529 | ppr externalSrcLoc, |
|---|
| 530 | ptext (sLit ">("), |
|---|
| 531 | ppr exp, |
|---|
| 532 | ptext (sLit ")")] |
|---|
| 533 | |
|---|
| 534 | ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp True) |
|---|
| 535 | = hsep [ppr_lexpr arrow, ptext (sLit "-<"), ppr_lexpr arg] |
|---|
| 536 | ppr_expr (HsArrApp arrow arg _ HsFirstOrderApp False) |
|---|
| 537 | = hsep [ppr_lexpr arg, ptext (sLit ">-"), ppr_lexpr arrow] |
|---|
| 538 | ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp True) |
|---|
| 539 | = hsep [ppr_lexpr arrow, ptext (sLit "-<<"), ppr_lexpr arg] |
|---|
| 540 | ppr_expr (HsArrApp arrow arg _ HsHigherOrderApp False) |
|---|
| 541 | = hsep [ppr_lexpr arg, ptext (sLit ">>-"), ppr_lexpr arrow] |
|---|
| 542 | |
|---|
| 543 | ppr_expr (HsArrForm (L _ (HsVar v)) (Just _) [arg1, arg2]) |
|---|
| 544 | = sep [pprCmdArg (unLoc arg1), hsep [pprInfixOcc v, pprCmdArg (unLoc arg2)]] |
|---|
| 545 | ppr_expr (HsArrForm op _ args) |
|---|
| 546 | = hang (ptext (sLit "(|") <> ppr_lexpr op) |
|---|
| 547 | 4 (sep (map (pprCmdArg.unLoc) args) <> ptext (sLit "|)")) |
|---|
| 548 | |
|---|
| 549 | pprCmdArg :: OutputableBndr id => HsCmdTop id -> SDoc |
|---|
| 550 | pprCmdArg (HsCmdTop cmd@(L _ (HsArrForm _ Nothing [])) _ _ _) |
|---|
| 551 | = ppr_lexpr cmd |
|---|
| 552 | pprCmdArg (HsCmdTop cmd _ _ _) |
|---|
| 553 | = parens (ppr_lexpr cmd) |
|---|
| 554 | |
|---|
| 555 | instance OutputableBndr id => Outputable (HsCmdTop id) where |
|---|
| 556 | ppr = pprCmdArg |
|---|
| 557 | \end{code} |
|---|
| 558 | |
|---|
| 559 | HsSyn records exactly where the user put parens, with HsPar. |
|---|
| 560 | So generally speaking we print without adding any parens. |
|---|
| 561 | However, some code is internally generated, and in some places |
|---|
| 562 | parens are absolutely required; so for these places we use |
|---|
| 563 | pprParendExpr (but don't print double parens of course). |
|---|
| 564 | |
|---|
| 565 | For operator applications we don't add parens, because the oprerator |
|---|
| 566 | fixities should do the job, except in debug mode (-dppr-debug) so we |
|---|
| 567 | can see the structure of the parse tree. |
|---|
| 568 | |
|---|
| 569 | \begin{code} |
|---|
| 570 | pprDebugParendExpr :: OutputableBndr id => LHsExpr id -> SDoc |
|---|
| 571 | pprDebugParendExpr expr |
|---|
| 572 | = getPprStyle (\sty -> |
|---|
| 573 | if debugStyle sty then pprParendExpr expr |
|---|
| 574 | else pprLExpr expr) |
|---|
| 575 | |
|---|
| 576 | pprParendExpr :: OutputableBndr id => LHsExpr id -> SDoc |
|---|
| 577 | pprParendExpr expr |
|---|
| 578 | | hsExprNeedsParens (unLoc expr) = parens (pprLExpr expr) |
|---|
| 579 | | otherwise = pprLExpr expr |
|---|
| 580 | -- Using pprLExpr makes sure that we go 'deeper' |
|---|
| 581 | -- I think that is usually (always?) right |
|---|
| 582 | |
|---|
| 583 | hsExprNeedsParens :: HsExpr id -> Bool |
|---|
| 584 | -- True of expressions for which '(e)' and 'e' |
|---|
| 585 | -- mean the same thing |
|---|
| 586 | hsExprNeedsParens (ArithSeq {}) = False |
|---|
| 587 | hsExprNeedsParens (PArrSeq {}) = False |
|---|
| 588 | hsExprNeedsParens (HsLit {}) = False |
|---|
| 589 | hsExprNeedsParens (HsOverLit {}) = False |
|---|
| 590 | hsExprNeedsParens (HsVar {}) = False |
|---|
| 591 | hsExprNeedsParens (HsIPVar {}) = False |
|---|
| 592 | hsExprNeedsParens (ExplicitTuple {}) = False |
|---|
| 593 | hsExprNeedsParens (ExplicitList {}) = False |
|---|
| 594 | hsExprNeedsParens (ExplicitPArr {}) = False |
|---|
| 595 | hsExprNeedsParens (HsPar {}) = False |
|---|
| 596 | hsExprNeedsParens (HsBracket {}) = False |
|---|
| 597 | hsExprNeedsParens (HsBracketOut _ []) = False |
|---|
| 598 | hsExprNeedsParens (HsDo sc _ _) |
|---|
| 599 | | isListCompExpr sc = False |
|---|
| 600 | hsExprNeedsParens _ = True |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | isAtomicHsExpr :: HsExpr id -> Bool |
|---|
| 604 | -- True of a single token |
|---|
| 605 | isAtomicHsExpr (HsVar {}) = True |
|---|
| 606 | isAtomicHsExpr (HsLit {}) = True |
|---|
| 607 | isAtomicHsExpr (HsOverLit {}) = True |
|---|
| 608 | isAtomicHsExpr (HsIPVar {}) = True |
|---|
| 609 | isAtomicHsExpr (HsWrap _ e) = isAtomicHsExpr e |
|---|
| 610 | isAtomicHsExpr (HsPar e) = isAtomicHsExpr (unLoc e) |
|---|
| 611 | isAtomicHsExpr _ = False |
|---|
| 612 | \end{code} |
|---|
| 613 | |
|---|
| 614 | %************************************************************************ |
|---|
| 615 | %* * |
|---|
| 616 | \subsection{Commands (in arrow abstractions)} |
|---|
| 617 | %* * |
|---|
| 618 | %************************************************************************ |
|---|
| 619 | |
|---|
| 620 | We re-use HsExpr to represent these. |
|---|
| 621 | |
|---|
| 622 | \begin{code} |
|---|
| 623 | type HsCmd id = HsExpr id |
|---|
| 624 | |
|---|
| 625 | type LHsCmd id = LHsExpr id |
|---|
| 626 | |
|---|
| 627 | data HsArrAppType = HsHigherOrderApp | HsFirstOrderApp |
|---|
| 628 | deriving (Data, Typeable) |
|---|
| 629 | \end{code} |
|---|
| 630 | |
|---|
| 631 | The legal constructors for commands are: |
|---|
| 632 | |
|---|
| 633 | = HsArrApp ... -- as above |
|---|
| 634 | |
|---|
| 635 | | HsArrForm ... -- as above |
|---|
| 636 | |
|---|
| 637 | | HsApp (HsCmd id) |
|---|
| 638 | (HsExpr id) |
|---|
| 639 | |
|---|
| 640 | | HsLam (Match id) -- kappa |
|---|
| 641 | |
|---|
| 642 | -- the renamer turns this one into HsArrForm |
|---|
| 643 | | OpApp (HsExpr id) -- left operand |
|---|
| 644 | (HsCmd id) -- operator |
|---|
| 645 | Fixity -- Renamer adds fixity; bottom until then |
|---|
| 646 | (HsCmd id) -- right operand |
|---|
| 647 | |
|---|
| 648 | | HsPar (HsCmd id) -- parenthesised command |
|---|
| 649 | |
|---|
| 650 | | HsCase (HsExpr id) |
|---|
| 651 | [Match id] -- bodies are HsCmd's |
|---|
| 652 | SrcLoc |
|---|
| 653 | |
|---|
| 654 | | HsIf (Maybe (SyntaxExpr id)) -- cond function |
|---|
| 655 | (HsExpr id) -- predicate |
|---|
| 656 | (HsCmd id) -- then part |
|---|
| 657 | (HsCmd id) -- else part |
|---|
| 658 | SrcLoc |
|---|
| 659 | |
|---|
| 660 | | HsLet (HsLocalBinds id) -- let(rec) |
|---|
| 661 | (HsCmd id) |
|---|
| 662 | |
|---|
| 663 | | HsDo (HsStmtContext Name) -- The parameterisation is unimportant |
|---|
| 664 | -- because in this context we never use |
|---|
| 665 | -- the PatGuard or ParStmt variant |
|---|
| 666 | [Stmt id] -- HsExpr's are really HsCmd's |
|---|
| 667 | PostTcType -- Type of the whole expression |
|---|
| 668 | SrcLoc |
|---|
| 669 | |
|---|
| 670 | Top-level command, introducing a new arrow. |
|---|
| 671 | This may occur inside a proc (where the stack is empty) or as an |
|---|
| 672 | argument of a command-forming operator. |
|---|
| 673 | |
|---|
| 674 | \begin{code} |
|---|
| 675 | type LHsCmdTop id = Located (HsCmdTop id) |
|---|
| 676 | |
|---|
| 677 | data HsCmdTop id |
|---|
| 678 | = HsCmdTop (LHsCmd id) |
|---|
| 679 | [PostTcType] -- types of inputs on the command's stack |
|---|
| 680 | PostTcType -- return type of the command |
|---|
| 681 | (SyntaxTable id) -- after type checking: |
|---|
| 682 | -- names used in the command's desugaring |
|---|
| 683 | deriving (Data, Typeable) |
|---|
| 684 | \end{code} |
|---|
| 685 | |
|---|
| 686 | %************************************************************************ |
|---|
| 687 | %* * |
|---|
| 688 | \subsection{Record binds} |
|---|
| 689 | %* * |
|---|
| 690 | %************************************************************************ |
|---|
| 691 | |
|---|
| 692 | \begin{code} |
|---|
| 693 | type HsRecordBinds id = HsRecFields id (LHsExpr id) |
|---|
| 694 | \end{code} |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | %************************************************************************ |
|---|
| 698 | %* * |
|---|
| 699 | \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes} |
|---|
| 700 | %* * |
|---|
| 701 | %************************************************************************ |
|---|
| 702 | |
|---|
| 703 | @Match@es are sets of pattern bindings and right hand sides for |
|---|
| 704 | functions, patterns or case branches. For example, if a function @g@ |
|---|
| 705 | is defined as: |
|---|
| 706 | \begin{verbatim} |
|---|
| 707 | g (x,y) = y |
|---|
| 708 | g ((x:ys),y) = y+1, |
|---|
| 709 | \end{verbatim} |
|---|
| 710 | then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@. |
|---|
| 711 | |
|---|
| 712 | It is always the case that each element of an @[Match]@ list has the |
|---|
| 713 | same number of @pats@s inside it. This corresponds to saying that |
|---|
| 714 | a function defined by pattern matching must have the same number of |
|---|
| 715 | patterns in each equation. |
|---|
| 716 | |
|---|
| 717 | \begin{code} |
|---|
| 718 | data MatchGroup id |
|---|
| 719 | = MatchGroup |
|---|
| 720 | [LMatch id] -- The alternatives |
|---|
| 721 | PostTcType -- The type is the type of the entire group |
|---|
| 722 | -- t1 -> ... -> tn -> tr |
|---|
| 723 | -- where there are n patterns |
|---|
| 724 | deriving (Data, Typeable) |
|---|
| 725 | |
|---|
| 726 | type LMatch id = Located (Match id) |
|---|
| 727 | |
|---|
| 728 | data Match id |
|---|
| 729 | = Match |
|---|
| 730 | [LPat id] -- The patterns |
|---|
| 731 | (Maybe (LHsType id)) -- A type signature for the result of the match |
|---|
| 732 | -- Nothing after typechecking |
|---|
| 733 | (GRHSs id) |
|---|
| 734 | deriving (Data, Typeable) |
|---|
| 735 | |
|---|
| 736 | isEmptyMatchGroup :: MatchGroup id -> Bool |
|---|
| 737 | isEmptyMatchGroup (MatchGroup ms _) = null ms |
|---|
| 738 | |
|---|
| 739 | matchGroupArity :: MatchGroup id -> Arity |
|---|
| 740 | matchGroupArity (MatchGroup [] _) |
|---|
| 741 | = panic "matchGroupArity" -- Precondition: MatchGroup is non-empty |
|---|
| 742 | matchGroupArity (MatchGroup (match:matches) _) |
|---|
| 743 | = ASSERT( all ((== n_pats) . length . hsLMatchPats) matches ) |
|---|
| 744 | -- Assertion just checks that all the matches have the same number of pats |
|---|
| 745 | n_pats |
|---|
| 746 | where |
|---|
| 747 | n_pats = length (hsLMatchPats match) |
|---|
| 748 | |
|---|
| 749 | hsLMatchPats :: LMatch id -> [LPat id] |
|---|
| 750 | hsLMatchPats (L _ (Match pats _ _)) = pats |
|---|
| 751 | |
|---|
| 752 | -- | GRHSs are used both for pattern bindings and for Matches |
|---|
| 753 | data GRHSs id |
|---|
| 754 | = GRHSs { |
|---|
| 755 | grhssGRHSs :: [LGRHS id], -- ^ Guarded RHSs |
|---|
| 756 | grhssLocalBinds :: (HsLocalBinds id) -- ^ The where clause |
|---|
| 757 | } deriving (Data, Typeable) |
|---|
| 758 | |
|---|
| 759 | type LGRHS id = Located (GRHS id) |
|---|
| 760 | |
|---|
| 761 | -- | Guarded Right Hand Side. |
|---|
| 762 | data GRHS id = GRHS [LStmt id] -- Guards |
|---|
| 763 | (LHsExpr id) -- Right hand side |
|---|
| 764 | deriving (Data, Typeable) |
|---|
| 765 | \end{code} |
|---|
| 766 | |
|---|
| 767 | We know the list must have at least one @Match@ in it. |
|---|
| 768 | |
|---|
| 769 | \begin{code} |
|---|
| 770 | pprMatches :: (OutputableBndr idL, OutputableBndr idR) => HsMatchContext idL -> MatchGroup idR -> SDoc |
|---|
| 771 | pprMatches ctxt (MatchGroup matches _) |
|---|
| 772 | = vcat (map (pprMatch ctxt) (map unLoc matches)) |
|---|
| 773 | -- Don't print the type; it's only a place-holder before typechecking |
|---|
| 774 | |
|---|
| 775 | -- Exported to HsBinds, which can't see the defn of HsMatchContext |
|---|
| 776 | pprFunBind :: (OutputableBndr idL, OutputableBndr idR) => idL -> Bool -> MatchGroup idR -> SDoc |
|---|
| 777 | pprFunBind fun inf matches = pprMatches (FunRhs fun inf) matches |
|---|
| 778 | |
|---|
| 779 | -- Exported to HsBinds, which can't see the defn of HsMatchContext |
|---|
| 780 | pprPatBind :: forall bndr id. (OutputableBndr bndr, OutputableBndr id) |
|---|
| 781 | => LPat bndr -> GRHSs id -> SDoc |
|---|
| 782 | pprPatBind pat (grhss) |
|---|
| 783 | = sep [ppr pat, nest 2 (pprGRHSs (PatBindRhs :: HsMatchContext id) grhss)] |
|---|
| 784 | |
|---|
| 785 | pprMatch :: (OutputableBndr idL, OutputableBndr idR) => HsMatchContext idL -> Match idR -> SDoc |
|---|
| 786 | pprMatch ctxt (Match pats maybe_ty grhss) |
|---|
| 787 | = sep [ sep (herald : map (nest 2 . pprParendLPat) other_pats) |
|---|
| 788 | , nest 2 ppr_maybe_ty |
|---|
| 789 | , nest 2 (pprGRHSs ctxt grhss) ] |
|---|
| 790 | where |
|---|
| 791 | (herald, other_pats) |
|---|
| 792 | = case ctxt of |
|---|
| 793 | FunRhs fun is_infix |
|---|
| 794 | | not is_infix -> (ppr fun, pats) |
|---|
| 795 | -- f x y z = e |
|---|
| 796 | -- Not pprBndr; the AbsBinds will |
|---|
| 797 | -- have printed the signature |
|---|
| 798 | |
|---|
| 799 | | null pats2 -> (pp_infix, []) |
|---|
| 800 | -- x &&& y = e |
|---|
| 801 | |
|---|
| 802 | | otherwise -> (parens pp_infix, pats2) |
|---|
| 803 | -- (x &&& y) z = e |
|---|
| 804 | where |
|---|
| 805 | pp_infix = pprParendLPat pat1 <+> ppr fun <+> pprParendLPat pat2 |
|---|
| 806 | |
|---|
| 807 | LambdaExpr -> (char '\\', pats) |
|---|
| 808 | |
|---|
| 809 | _ -> ASSERT( null pats1 ) |
|---|
| 810 | (ppr pat1, []) -- No parens around the single pat |
|---|
| 811 | |
|---|
| 812 | (pat1:pats1) = pats |
|---|
| 813 | (pat2:pats2) = pats1 |
|---|
| 814 | ppr_maybe_ty = case maybe_ty of |
|---|
| 815 | Just ty -> dcolon <+> ppr ty |
|---|
| 816 | Nothing -> empty |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | pprGRHSs :: (OutputableBndr idL, OutputableBndr idR) |
|---|
| 820 | => HsMatchContext idL -> GRHSs idR -> SDoc |
|---|
| 821 | pprGRHSs ctxt (GRHSs grhss binds) |
|---|
| 822 | = vcat (map (pprGRHS ctxt . unLoc) grhss) |
|---|
| 823 | $$ ppUnless (isEmptyLocalBinds binds) |
|---|
| 824 | (text "where" $$ nest 4 (pprBinds binds)) |
|---|
| 825 | |
|---|
| 826 | pprGRHS :: (OutputableBndr idL, OutputableBndr idR) |
|---|
| 827 | => HsMatchContext idL -> GRHS idR -> SDoc |
|---|
| 828 | |
|---|
| 829 | pprGRHS ctxt (GRHS [] expr) |
|---|
| 830 | = pp_rhs ctxt expr |
|---|
| 831 | |
|---|
| 832 | pprGRHS ctxt (GRHS guards expr) |
|---|
| 833 | = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr] |
|---|
| 834 | |
|---|
| 835 | pp_rhs :: OutputableBndr idR => HsMatchContext idL -> LHsExpr idR -> SDoc |
|---|
| 836 | pp_rhs ctxt rhs = matchSeparator ctxt <+> pprDeeper (ppr rhs) |
|---|
| 837 | \end{code} |
|---|
| 838 | |
|---|
| 839 | %************************************************************************ |
|---|
| 840 | %* * |
|---|
| 841 | \subsection{Do stmts and list comprehensions} |
|---|
| 842 | %* * |
|---|
| 843 | %************************************************************************ |
|---|
| 844 | |
|---|
| 845 | \begin{code} |
|---|
| 846 | type LStmt id = Located (StmtLR id id) |
|---|
| 847 | type LStmtLR idL idR = Located (StmtLR idL idR) |
|---|
| 848 | |
|---|
| 849 | type Stmt id = StmtLR id id |
|---|
| 850 | |
|---|
| 851 | -- The SyntaxExprs in here are used *only* for do-notation and monad |
|---|
| 852 | -- comprehensions, which have rebindable syntax. Otherwise they are unused. |
|---|
| 853 | data StmtLR idL idR |
|---|
| 854 | = LastStmt -- Always the last Stmt in ListComp, MonadComp, PArrComp, |
|---|
| 855 | -- and (after the renamer) DoExpr, MDoExpr |
|---|
| 856 | -- Not used for GhciStmt, PatGuard, which scope over other stuff |
|---|
| 857 | (LHsExpr idR) |
|---|
| 858 | (SyntaxExpr idR) -- The return operator, used only for MonadComp |
|---|
| 859 | -- For ListComp, PArrComp, we use the baked-in 'return' |
|---|
| 860 | -- For DoExpr, MDoExpr, we don't appply a 'return' at all |
|---|
| 861 | -- See Note [Monad Comprehensions] |
|---|
| 862 | | BindStmt (LPat idL) |
|---|
| 863 | (LHsExpr idR) |
|---|
| 864 | (SyntaxExpr idR) -- The (>>=) operator; see Note [The type of bind] |
|---|
| 865 | (SyntaxExpr idR) -- The fail operator |
|---|
| 866 | -- The fail operator is noSyntaxExpr |
|---|
| 867 | -- if the pattern match can't fail |
|---|
| 868 | |
|---|
| 869 | | ExprStmt (LHsExpr idR) -- See Note [ExprStmt] |
|---|
| 870 | (SyntaxExpr idR) -- The (>>) operator |
|---|
| 871 | (SyntaxExpr idR) -- The `guard` operator; used only in MonadComp |
|---|
| 872 | -- See notes [Monad Comprehensions] |
|---|
| 873 | PostTcType -- Element type of the RHS (used for arrows) |
|---|
| 874 | |
|---|
| 875 | | LetStmt (HsLocalBindsLR idL idR) |
|---|
| 876 | |
|---|
| 877 | -- ParStmts only occur in a list/monad comprehension |
|---|
| 878 | | ParStmt [ParStmtBlock idL idR] |
|---|
| 879 | (SyntaxExpr idR) -- Polymorphic `mzip` for monad comprehensions |
|---|
| 880 | (SyntaxExpr idR) -- The `>>=` operator |
|---|
| 881 | -- See notes [Monad Comprehensions] |
|---|
| 882 | -- After renaming, the ids are the binders |
|---|
| 883 | -- bound by the stmts and used after themp |
|---|
| 884 | |
|---|
| 885 | | TransStmt { |
|---|
| 886 | trS_form :: TransForm, |
|---|
| 887 | trS_stmts :: [LStmt idL], -- Stmts to the *left* of the 'group' |
|---|
| 888 | -- which generates the tuples to be grouped |
|---|
| 889 | |
|---|
| 890 | trS_bndrs :: [(idR, idR)], -- See Note [TransStmt binder map] |
|---|
| 891 | |
|---|
| 892 | trS_using :: LHsExpr idR, |
|---|
| 893 | trS_by :: Maybe (LHsExpr idR), -- "by e" (optional) |
|---|
| 894 | -- Invariant: if trS_form = GroupBy, then grp_by = Just e |
|---|
| 895 | |
|---|
| 896 | trS_ret :: SyntaxExpr idR, -- The monomorphic 'return' function for |
|---|
| 897 | -- the inner monad comprehensions |
|---|
| 898 | trS_bind :: SyntaxExpr idR, -- The '(>>=)' operator |
|---|
| 899 | trS_fmap :: SyntaxExpr idR -- The polymorphic 'fmap' function for desugaring |
|---|
| 900 | -- Only for 'group' forms |
|---|
| 901 | } -- See Note [Monad Comprehensions] |
|---|
| 902 | |
|---|
| 903 | -- Recursive statement (see Note [How RecStmt works] below) |
|---|
| 904 | | RecStmt |
|---|
| 905 | { recS_stmts :: [LStmtLR idL idR] |
|---|
| 906 | |
|---|
| 907 | -- The next two fields are only valid after renaming |
|---|
| 908 | , recS_later_ids :: [idR] -- The ids are a subset of the variables bound by the |
|---|
| 909 | -- stmts that are used in stmts that follow the RecStmt |
|---|
| 910 | |
|---|
| 911 | , recS_rec_ids :: [idR] -- Ditto, but these variables are the "recursive" ones, |
|---|
| 912 | -- that are used before they are bound in the stmts of |
|---|
| 913 | -- the RecStmt. |
|---|
| 914 | -- An Id can be in both groups |
|---|
| 915 | -- Both sets of Ids are (now) treated monomorphically |
|---|
| 916 | -- See Note [How RecStmt works] for why they are separate |
|---|
| 917 | |
|---|
| 918 | -- Rebindable syntax |
|---|
| 919 | , recS_bind_fn :: SyntaxExpr idR -- The bind function |
|---|
| 920 | , recS_ret_fn :: SyntaxExpr idR -- The return function |
|---|
| 921 | , recS_mfix_fn :: SyntaxExpr idR -- The mfix function |
|---|
| 922 | |
|---|
| 923 | -- These fields are only valid after typechecking |
|---|
| 924 | , recS_later_rets :: [PostTcExpr] -- (only used in the arrow version) |
|---|
| 925 | , recS_rec_rets :: [PostTcExpr] -- These expressions correspond 1-to-1 |
|---|
| 926 | -- with recS_later_ids and recS_rec_ids, |
|---|
| 927 | -- and are the expressions that should be |
|---|
| 928 | -- returned by the recursion. |
|---|
| 929 | -- They may not quite be the Ids themselves, |
|---|
| 930 | -- because the Id may be *polymorphic*, but |
|---|
| 931 | -- the returned thing has to be *monomorphic*, |
|---|
| 932 | -- so they may be type applications |
|---|
| 933 | |
|---|
| 934 | , recS_ret_ty :: PostTcType -- The type of of do { stmts; return (a,b,c) } |
|---|
| 935 | -- With rebindable syntax the type might not |
|---|
| 936 | -- be quite as simple as (m (tya, tyb, tyc)). |
|---|
| 937 | } |
|---|
| 938 | deriving (Data, Typeable) |
|---|
| 939 | |
|---|
| 940 | data TransForm -- The 'f' below is the 'using' function, 'e' is the by function |
|---|
| 941 | = ThenForm -- then f or then f by e (depending on trS_by) |
|---|
| 942 | | GroupForm -- then group using f or then group by e using f (depending on trS_by) |
|---|
| 943 | deriving (Data, Typeable) |
|---|
| 944 | |
|---|
| 945 | data ParStmtBlock idL idR |
|---|
| 946 | = ParStmtBlock |
|---|
| 947 | [LStmt idL] |
|---|
| 948 | [idR] -- The variables to be returned |
|---|
| 949 | (SyntaxExpr idR) -- The return operator |
|---|
| 950 | deriving( Data, Typeable ) |
|---|
| 951 | \end{code} |
|---|
| 952 | |
|---|
| 953 | Note [The type of bind in Stmts] |
|---|
| 954 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 955 | Some Stmts, notably BindStmt, keep the (>>=) bind operator. |
|---|
| 956 | We do NOT assume that it has type |
|---|
| 957 | (>>=) :: m a -> (a -> m b) -> m b |
|---|
| 958 | In some cases (see Trac #303, #1537) it might have a more |
|---|
| 959 | exotic type, such as |
|---|
| 960 | (>>=) :: m i j a -> (a -> m j k b) -> m i k b |
|---|
| 961 | So we must be careful not to make assumptions about the type. |
|---|
| 962 | In particular, the monad may not be uniform throughout. |
|---|
| 963 | |
|---|
| 964 | Note [TransStmt binder map] |
|---|
| 965 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 966 | The [(idR,idR)] in a TransStmt behaves as follows: |
|---|
| 967 | |
|---|
| 968 | * Before renaming: [] |
|---|
| 969 | |
|---|
| 970 | * After renaming: |
|---|
| 971 | [ (x27,x27), ..., (z35,z35) ] |
|---|
| 972 | These are the variables |
|---|
| 973 | bound by the stmts to the left of the 'group' |
|---|
| 974 | and used either in the 'by' clause, |
|---|
| 975 | or in the stmts following the 'group' |
|---|
| 976 | Each item is a pair of identical variables. |
|---|
| 977 | |
|---|
| 978 | * After typechecking: |
|---|
| 979 | [ (x27:Int, x27:[Int]), ..., (z35:Bool, z35:[Bool]) ] |
|---|
| 980 | Each pair has the same unique, but different *types*. |
|---|
| 981 | |
|---|
| 982 | Note [ExprStmt] |
|---|
| 983 | ~~~~~~~~~~~~~~~ |
|---|
| 984 | ExprStmts are a bit tricky, because what they mean |
|---|
| 985 | depends on the context. Consider the following contexts: |
|---|
| 986 | |
|---|
| 987 | A do expression of type (m res_ty) |
|---|
| 988 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 989 | * ExprStmt E any_ty: do { ....; E; ... } |
|---|
| 990 | E :: m any_ty |
|---|
| 991 | Translation: E >> ... |
|---|
| 992 | |
|---|
| 993 | A list comprehensions of type [elt_ty] |
|---|
| 994 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 995 | * ExprStmt E Bool: [ .. | .... E ] |
|---|
| 996 | [ .. | ..., E, ... ] |
|---|
| 997 | [ .. | .... | ..., E | ... ] |
|---|
| 998 | E :: Bool |
|---|
| 999 | Translation: if E then fail else ... |
|---|
| 1000 | |
|---|
| 1001 | A guard list, guarding a RHS of type rhs_ty |
|---|
| 1002 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 1003 | * ExprStmt E Bool: f x | ..., E, ... = ...rhs... |
|---|
| 1004 | E :: Bool |
|---|
| 1005 | Translation: if E then fail else ... |
|---|
| 1006 | |
|---|
| 1007 | A monad comprehension of type (m res_ty) |
|---|
| 1008 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 1009 | * ExprStmt E Bool: [ .. | .... E ] |
|---|
| 1010 | E :: Bool |
|---|
| 1011 | Translation: guard E >> ... |
|---|
| 1012 | |
|---|
| 1013 | Array comprehensions are handled like list comprehensions. |
|---|
| 1014 | |
|---|
| 1015 | Note [How RecStmt works] |
|---|
| 1016 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 1017 | Example: |
|---|
| 1018 | HsDo [ BindStmt x ex |
|---|
| 1019 | |
|---|
| 1020 | , RecStmt { recS_rec_ids = [a, c] |
|---|
| 1021 | , recS_stmts = [ BindStmt b (return (a,c)) |
|---|
| 1022 | , LetStmt a = ...b... |
|---|
| 1023 | , BindStmt c ec ] |
|---|
| 1024 | , recS_later_ids = [a, b] |
|---|
| 1025 | |
|---|
| 1026 | , return (a b) ] |
|---|
| 1027 | |
|---|
| 1028 | Here, the RecStmt binds a,b,c; but |
|---|
| 1029 | - Only a,b are used in the stmts *following* the RecStmt, |
|---|
| 1030 | - Only a,c are used in the stmts *inside* the RecStmt |
|---|
| 1031 | *before* their bindings |
|---|
| 1032 | |
|---|
| 1033 | Why do we need *both* rec_ids and later_ids? For monads they could be |
|---|
| 1034 | combined into a single set of variables, but not for arrows. That |
|---|
| 1035 | follows from the types of the respective feedback operators: |
|---|
| 1036 | |
|---|
| 1037 | mfix :: MonadFix m => (a -> m a) -> m a |
|---|
| 1038 | loop :: ArrowLoop a => a (b,d) (c,d) -> a b c |
|---|
| 1039 | |
|---|
| 1040 | * For mfix, the 'a' covers the union of the later_ids and the rec_ids |
|---|
| 1041 | * For 'loop', 'c' is the later_ids and 'd' is the rec_ids |
|---|
| 1042 | |
|---|
| 1043 | Note [Typing a RecStmt] |
|---|
| 1044 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 1045 | A (RecStmt stmts) types as if you had written |
|---|
| 1046 | |
|---|
| 1047 | (v1,..,vn, _, ..., _) <- mfix (\~(_, ..., _, r1, ..., rm) -> |
|---|
| 1048 | do { stmts |
|---|
| 1049 | ; return (v1,..vn, r1, ..., rm) }) |
|---|
| 1050 | |
|---|
| 1051 | where v1..vn are the later_ids |
|---|
| 1052 | r1..rm are the rec_ids |
|---|
| 1053 | |
|---|
| 1054 | Note [Monad Comprehensions] |
|---|
| 1055 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 1056 | Monad comprehensions require separate functions like 'return' and |
|---|
| 1057 | '>>=' for desugaring. These functions are stored in the statements |
|---|
| 1058 | used in monad comprehensions. For example, the 'return' of the 'LastStmt' |
|---|
| 1059 | expression is used to lift the body of the monad comprehension: |
|---|
| 1060 | |
|---|
| 1061 | [ body | stmts ] |
|---|
| 1062 | => |
|---|
| 1063 | stmts >>= \bndrs -> return body |
|---|
| 1064 | |
|---|
| 1065 | In transform and grouping statements ('then ..' and 'then group ..') the |
|---|
| 1066 | 'return' function is required for nested monad comprehensions, for example: |
|---|
| 1067 | |
|---|
| 1068 | [ body | stmts, then f, rest ] |
|---|
| 1069 | => |
|---|
| 1070 | f [ env | stmts ] >>= \bndrs -> [ body | rest ] |
|---|
| 1071 | |
|---|
| 1072 | ExprStmts require the 'Control.Monad.guard' function for boolean |
|---|
| 1073 | expressions: |
|---|
| 1074 | |
|---|
| 1075 | [ body | exp, stmts ] |
|---|
| 1076 | => |
|---|
| 1077 | guard exp >> [ body | stmts ] |
|---|
| 1078 | |
|---|
| 1079 | Parallel statements require the 'Control.Monad.Zip.mzip' function: |
|---|
| 1080 | |
|---|
| 1081 | [ body | stmts1 | stmts2 | .. ] |
|---|
| 1082 | => |
|---|
| 1083 | mzip stmts1 (mzip stmts2 (..)) >>= \(bndrs1, (bndrs2, ..)) -> return body |
|---|
| 1084 | |
|---|
| 1085 | In any other context than 'MonadComp', the fields for most of these |
|---|
| 1086 | 'SyntaxExpr's stay bottom. |
|---|
| 1087 | |
|---|
| 1088 | |
|---|
| 1089 | \begin{code} |
|---|
| 1090 | instance (OutputableBndr idL, OutputableBndr idR) |
|---|
| 1091 | => Outputable (ParStmtBlock idL idR) where |
|---|
| 1092 | ppr (ParStmtBlock stmts _ _) = interpp'SP stmts |
|---|
| 1093 | |
|---|
| 1094 | instance (OutputableBndr idL, OutputableBndr idR) => Outputable (StmtLR idL idR) where |
|---|
| 1095 | ppr stmt = pprStmt stmt |
|---|
| 1096 | |
|---|
| 1097 | pprStmt :: (OutputableBndr idL, OutputableBndr idR) => (StmtLR idL idR) -> SDoc |
|---|
| 1098 | pprStmt (LastStmt expr _) = ifPprDebug (ptext (sLit "[last]")) <+> ppr expr |
|---|
| 1099 | pprStmt (BindStmt pat expr _ _) = hsep [ppr pat, ptext (sLit "<-"), ppr expr] |
|---|
| 1100 | pprStmt (LetStmt binds) = hsep [ptext (sLit "let"), pprBinds binds] |
|---|
| 1101 | pprStmt (ExprStmt expr _ _ _) = ppr expr |
|---|
| 1102 | pprStmt (ParStmt stmtss _ _) = sep (punctuate (ptext (sLit " | ")) (map ppr stmtss)) |
|---|
| 1103 | |
|---|
| 1104 | pprStmt (TransStmt { trS_stmts = stmts, trS_by = by, trS_using = using, trS_form = form }) |
|---|
| 1105 | = sep $ punctuate comma (map ppr stmts ++ [pprTransStmt by using form]) |
|---|
| 1106 | |
|---|
| 1107 | pprStmt (RecStmt { recS_stmts = segment, recS_rec_ids = rec_ids |
|---|
| 1108 | , recS_later_ids = later_ids }) |
|---|
| 1109 | = ptext (sLit "rec") <+> |
|---|
| 1110 | vcat [ braces (vcat (map ppr segment)) |
|---|
| 1111 | , ifPprDebug (vcat [ ptext (sLit "rec_ids=") <> ppr rec_ids |
|---|
| 1112 | , ptext (sLit "later_ids=") <> ppr later_ids])] |
|---|
| 1113 | |
|---|
| 1114 | pprTransformStmt :: OutputableBndr id => [id] -> LHsExpr id -> Maybe (LHsExpr id) -> SDoc |
|---|
| 1115 | pprTransformStmt bndrs using by |
|---|
| 1116 | = sep [ ptext (sLit "then") <+> ifPprDebug (braces (ppr bndrs)) |
|---|
| 1117 | , nest 2 (ppr using) |
|---|
| 1118 | , nest 2 (pprBy by)] |
|---|
| 1119 | |
|---|
| 1120 | pprTransStmt :: OutputableBndr id => Maybe (LHsExpr id) |
|---|
| 1121 | -> LHsExpr id -> TransForm |
|---|
| 1122 | -> SDoc |
|---|
| 1123 | pprTransStmt by using ThenForm |
|---|
| 1124 | = sep [ ptext (sLit "then"), nest 2 (ppr using), nest 2 (pprBy by)] |
|---|
| 1125 | pprTransStmt by using GroupForm |
|---|
| 1126 | = sep [ ptext (sLit "then group"), nest 2 (pprBy by), nest 2 (ptext (sLit "using") <+> ppr using)] |
|---|
| 1127 | |
|---|
| 1128 | pprBy :: OutputableBndr id => Maybe (LHsExpr id) -> SDoc |
|---|
| 1129 | pprBy Nothing = empty |
|---|
| 1130 | pprBy (Just e) = ptext (sLit "by") <+> ppr e |
|---|
| 1131 | |
|---|
| 1132 | pprDo :: OutputableBndr id => HsStmtContext any -> [LStmt id] -> SDoc |
|---|
| 1133 | pprDo DoExpr stmts = ptext (sLit "do") <+> ppr_do_stmts stmts |
|---|
| 1134 | pprDo GhciStmt stmts = ptext (sLit "do") <+> ppr_do_stmts stmts |
|---|
| 1135 | pprDo ArrowExpr stmts = ptext (sLit "do") <+> ppr_do_stmts stmts |
|---|
| 1136 | pprDo MDoExpr stmts = ptext (sLit "mdo") <+> ppr_do_stmts stmts |
|---|
| 1137 | pprDo ListComp stmts = brackets $ pprComp stmts |
|---|
| 1138 | pprDo PArrComp stmts = paBrackets $ pprComp stmts |
|---|
| 1139 | pprDo MonadComp stmts = brackets $ pprComp stmts |
|---|
| 1140 | pprDo _ _ = panic "pprDo" -- PatGuard, ParStmtCxt |
|---|
| 1141 | |
|---|
| 1142 | ppr_do_stmts :: OutputableBndr id => [LStmt id] -> SDoc |
|---|
| 1143 | -- Print a bunch of do stmts, with explicit braces and semicolons, |
|---|
| 1144 | -- so that we are not vulnerable to layout bugs |
|---|
| 1145 | ppr_do_stmts stmts |
|---|
| 1146 | = lbrace <+> pprDeeperList vcat (punctuate semi (map ppr stmts)) |
|---|
| 1147 | <+> rbrace |
|---|
| 1148 | |
|---|
| 1149 | pprComp :: OutputableBndr id => [LStmt id] -> SDoc |
|---|
| 1150 | pprComp quals -- Prints: body | qual1, ..., qualn |
|---|
| 1151 | | not (null quals) |
|---|
| 1152 | , L _ (LastStmt body _) <- last quals |
|---|
| 1153 | = hang (ppr body <+> char '|') 2 (pprQuals (dropTail 1 quals)) |
|---|
| 1154 | | otherwise |
|---|
| 1155 | = pprPanic "pprComp" (pprQuals quals) |
|---|
| 1156 | |
|---|
| 1157 | pprQuals :: OutputableBndr id => [LStmt id] -> SDoc |
|---|
| 1158 | -- Show list comprehension qualifiers separated by commas |
|---|
| 1159 | pprQuals quals = interpp'SP quals |
|---|
| 1160 | \end{code} |
|---|
| 1161 | |
|---|
| 1162 | %************************************************************************ |
|---|
| 1163 | %* * |
|---|
| 1164 | Template Haskell quotation brackets |
|---|
| 1165 | %* * |
|---|
| 1166 | %************************************************************************ |
|---|
| 1167 | |
|---|
| 1168 | \begin{code} |
|---|
| 1169 | data HsSplice id = HsSplice -- $z or $(f 4) |
|---|
| 1170 | id -- The id is just a unique name to |
|---|
| 1171 | (LHsExpr id) -- identify this splice point |
|---|
| 1172 | deriving (Data, Typeable) |
|---|
| 1173 | |
|---|
| 1174 | instance OutputableBndr id => Outputable (HsSplice id) where |
|---|
| 1175 | ppr = pprSplice |
|---|
| 1176 | |
|---|
| 1177 | pprSplice :: OutputableBndr id => HsSplice id -> SDoc |
|---|
| 1178 | pprSplice (HsSplice n e) |
|---|
| 1179 | = char '$' <> ifPprDebug (brackets (ppr n)) <> eDoc |
|---|
| 1180 | where |
|---|
| 1181 | -- We use pprLExpr to match pprParendExpr: |
|---|
| 1182 | -- Using pprLExpr makes sure that we go 'deeper' |
|---|
| 1183 | -- I think that is usually (always?) right |
|---|
| 1184 | pp_as_was = pprLExpr e |
|---|
| 1185 | eDoc = case unLoc e of |
|---|
| 1186 | HsPar _ -> pp_as_was |
|---|
| 1187 | HsVar _ -> pp_as_was |
|---|
| 1188 | _ -> parens pp_as_was |
|---|
| 1189 | |
|---|
| 1190 | data HsBracket id = ExpBr (LHsExpr id) -- [| expr |] |
|---|
| 1191 | | PatBr (LPat id) -- [p| pat |] |
|---|
| 1192 | | DecBrL [LHsDecl id] -- [d| decls |]; result of parser |
|---|
| 1193 | | DecBrG (HsGroup id) -- [d| decls |]; result of renamer |
|---|
| 1194 | | TypBr (LHsType id) -- [t| type |] |
|---|
| 1195 | | VarBr Bool id -- True: 'x, False: ''T |
|---|
| 1196 | -- (The Bool flag is used only in pprHsBracket) |
|---|
| 1197 | deriving (Data, Typeable) |
|---|
| 1198 | |
|---|
| 1199 | instance OutputableBndr id => Outputable (HsBracket id) where |
|---|
| 1200 | ppr = pprHsBracket |
|---|
| 1201 | |
|---|
| 1202 | |
|---|
| 1203 | pprHsBracket :: OutputableBndr id => HsBracket id -> SDoc |
|---|
| 1204 | pprHsBracket (ExpBr e) = thBrackets empty (ppr e) |
|---|
| 1205 | pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p) |
|---|
| 1206 | pprHsBracket (DecBrG gp) = thBrackets (char 'd') (ppr gp) |
|---|
| 1207 | pprHsBracket (DecBrL ds) = thBrackets (char 'd') (vcat (map ppr ds)) |
|---|
| 1208 | pprHsBracket (TypBr t) = thBrackets (char 't') (ppr t) |
|---|
| 1209 | pprHsBracket (VarBr True n) = char '\'' <> ppr n |
|---|
| 1210 | pprHsBracket (VarBr False n) = ptext (sLit "''") <> ppr n |
|---|
| 1211 | |
|---|
| 1212 | thBrackets :: SDoc -> SDoc -> SDoc |
|---|
| 1213 | thBrackets pp_kind pp_body = char '[' <> pp_kind <> char '|' <+> |
|---|
| 1214 | pp_body <+> ptext (sLit "|]") |
|---|
| 1215 | \end{code} |
|---|
| 1216 | |
|---|
| 1217 | %************************************************************************ |
|---|
| 1218 | %* * |
|---|
| 1219 | \subsection{Enumerations and list comprehensions} |
|---|
| 1220 | %* * |
|---|
| 1221 | %************************************************************************ |
|---|
| 1222 | |
|---|
| 1223 | \begin{code} |
|---|
| 1224 | data ArithSeqInfo id |
|---|
| 1225 | = From (LHsExpr id) |
|---|
| 1226 | | FromThen (LHsExpr id) |
|---|
| 1227 | (LHsExpr id) |
|---|
| 1228 | | FromTo (LHsExpr id) |
|---|
| 1229 | (LHsExpr id) |
|---|
| 1230 | | FromThenTo (LHsExpr id) |
|---|
| 1231 | (LHsExpr id) |
|---|
| 1232 | (LHsExpr id) |
|---|
| 1233 | deriving (Data, Typeable) |
|---|
| 1234 | \end{code} |
|---|
| 1235 | |
|---|
| 1236 | \begin{code} |
|---|
| 1237 | instance OutputableBndr id => Outputable (ArithSeqInfo id) where |
|---|
| 1238 | ppr (From e1) = hcat [ppr e1, pp_dotdot] |
|---|
| 1239 | ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot] |
|---|
| 1240 | ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3] |
|---|
| 1241 | ppr (FromThenTo e1 e2 e3) |
|---|
| 1242 | = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3] |
|---|
| 1243 | |
|---|
| 1244 | pp_dotdot :: SDoc |
|---|
| 1245 | pp_dotdot = ptext (sLit " .. ") |
|---|
| 1246 | \end{code} |
|---|
| 1247 | |
|---|
| 1248 | |
|---|
| 1249 | %************************************************************************ |
|---|
| 1250 | %* * |
|---|
| 1251 | \subsection{HsMatchCtxt} |
|---|
| 1252 | %* * |
|---|
| 1253 | %************************************************************************ |
|---|
| 1254 | |
|---|
| 1255 | \begin{code} |
|---|
| 1256 | data HsMatchContext id -- Context of a Match |
|---|
| 1257 | = FunRhs id Bool -- Function binding for f; True <=> written infix |
|---|
| 1258 | | LambdaExpr -- Patterns of a lambda |
|---|
| 1259 | | CaseAlt -- Patterns and guards on a case alternative |
|---|
| 1260 | | ProcExpr -- Patterns of a proc |
|---|
| 1261 | | PatBindRhs -- A pattern binding eg [y] <- e = e |
|---|
| 1262 | |
|---|
| 1263 | | RecUpd -- Record update [used only in DsExpr to |
|---|
| 1264 | -- tell matchWrapper what sort of |
|---|
| 1265 | -- runtime error message to generate] |
|---|
| 1266 | |
|---|
| 1267 | | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt, list comprehension, |
|---|
| 1268 | -- pattern guard, etc |
|---|
| 1269 | |
|---|
| 1270 | | ThPatQuote -- A Template Haskell pattern quotation [p| (a,b) |] |
|---|
| 1271 | deriving (Data, Typeable) |
|---|
| 1272 | |
|---|
| 1273 | data HsStmtContext id |
|---|
| 1274 | = ListComp |
|---|
| 1275 | | MonadComp |
|---|
| 1276 | | PArrComp -- Parallel array comprehension |
|---|
| 1277 | |
|---|
| 1278 | | DoExpr -- do { ... } |
|---|
| 1279 | | MDoExpr -- mdo { ... } ie recursive do-expression |
|---|
| 1280 | | ArrowExpr -- do-notation in an arrow-command context |
|---|
| 1281 | |
|---|
| 1282 | | GhciStmt -- A command-line Stmt in GHCi pat <- rhs |
|---|
| 1283 | | PatGuard (HsMatchContext id) -- Pattern guard for specified thing |
|---|
| 1284 | | ParStmtCtxt (HsStmtContext id) -- A branch of a parallel stmt |
|---|
| 1285 | | TransStmtCtxt (HsStmtContext id) -- A branch of a transform stmt |
|---|
| 1286 | deriving (Data, Typeable) |
|---|
| 1287 | \end{code} |
|---|
| 1288 | |
|---|
| 1289 | \begin{code} |
|---|
| 1290 | isListCompExpr :: HsStmtContext id -> Bool |
|---|
| 1291 | -- Uses syntax [ e | quals ] |
|---|
| 1292 | isListCompExpr ListComp = True |
|---|
| 1293 | isListCompExpr PArrComp = True |
|---|
| 1294 | isListCompExpr MonadComp = True |
|---|
| 1295 | isListCompExpr (ParStmtCtxt c) = isListCompExpr c |
|---|
| 1296 | isListCompExpr (TransStmtCtxt c) = isListCompExpr c |
|---|
| 1297 | isListCompExpr _ = False |
|---|
| 1298 | |
|---|
| 1299 | isMonadCompExpr :: HsStmtContext id -> Bool |
|---|
| 1300 | isMonadCompExpr MonadComp = True |
|---|
| 1301 | isMonadCompExpr (ParStmtCtxt ctxt) = isMonadCompExpr ctxt |
|---|
| 1302 | isMonadCompExpr (TransStmtCtxt ctxt) = isMonadCompExpr ctxt |
|---|
| 1303 | isMonadCompExpr _ = False |
|---|
| 1304 | \end{code} |
|---|
| 1305 | |
|---|
| 1306 | \begin{code} |
|---|
| 1307 | matchSeparator :: HsMatchContext id -> SDoc |
|---|
| 1308 | matchSeparator (FunRhs {}) = ptext (sLit "=") |
|---|
| 1309 | matchSeparator CaseAlt = ptext (sLit "->") |
|---|
| 1310 | matchSeparator LambdaExpr = ptext (sLit "->") |
|---|
| 1311 | matchSeparator ProcExpr = ptext (sLit "->") |
|---|
| 1312 | matchSeparator PatBindRhs = ptext (sLit "=") |
|---|
| 1313 | matchSeparator (StmtCtxt _) = ptext (sLit "<-") |
|---|
| 1314 | matchSeparator RecUpd = panic "unused" |
|---|
| 1315 | matchSeparator ThPatQuote = panic "unused" |
|---|
| 1316 | \end{code} |
|---|
| 1317 | |
|---|
| 1318 | \begin{code} |
|---|
| 1319 | pprMatchContext :: Outputable id => HsMatchContext id -> SDoc |
|---|
| 1320 | pprMatchContext ctxt |
|---|
| 1321 | | want_an ctxt = ptext (sLit "an") <+> pprMatchContextNoun ctxt |
|---|
| 1322 | | otherwise = ptext (sLit "a") <+> pprMatchContextNoun ctxt |
|---|
| 1323 | where |
|---|
| 1324 | want_an (FunRhs {}) = True -- Use "an" in front |
|---|
| 1325 | want_an ProcExpr = True |
|---|
| 1326 | want_an _ = False |
|---|
| 1327 | |
|---|
| 1328 | pprMatchContextNoun :: Outputable id => HsMatchContext id -> SDoc |
|---|
| 1329 | pprMatchContextNoun (FunRhs fun _) = ptext (sLit "equation for") |
|---|
| 1330 | <+> quotes (ppr fun) |
|---|
| 1331 | pprMatchContextNoun CaseAlt = ptext (sLit "case alternative") |
|---|
| 1332 | pprMatchContextNoun RecUpd = ptext (sLit "record-update construct") |
|---|
| 1333 | pprMatchContextNoun ThPatQuote = ptext (sLit "Template Haskell pattern quotation") |
|---|
| 1334 | pprMatchContextNoun PatBindRhs = ptext (sLit "pattern binding") |
|---|
| 1335 | pprMatchContextNoun LambdaExpr = ptext (sLit "lambda abstraction") |
|---|
| 1336 | pprMatchContextNoun ProcExpr = ptext (sLit "arrow abstraction") |
|---|
| 1337 | pprMatchContextNoun (StmtCtxt ctxt) = ptext (sLit "pattern binding in") |
|---|
| 1338 | $$ pprStmtContext ctxt |
|---|
| 1339 | |
|---|
| 1340 | ----------------- |
|---|
| 1341 | pprAStmtContext, pprStmtContext :: Outputable id => HsStmtContext id -> SDoc |
|---|
| 1342 | pprAStmtContext ctxt = article <+> pprStmtContext ctxt |
|---|
| 1343 | where |
|---|
| 1344 | pp_an = ptext (sLit "an") |
|---|
| 1345 | pp_a = ptext (sLit "a") |
|---|
| 1346 | article = case ctxt of |
|---|
| 1347 | MDoExpr -> pp_an |
|---|
| 1348 | PArrComp -> pp_an |
|---|
| 1349 | GhciStmt -> pp_an |
|---|
| 1350 | _ -> pp_a |
|---|
| 1351 | |
|---|
| 1352 | |
|---|
| 1353 | ----------------- |
|---|
| 1354 | pprStmtContext GhciStmt = ptext (sLit "interactive GHCi command") |
|---|
| 1355 | pprStmtContext DoExpr = ptext (sLit "'do' block") |
|---|
| 1356 | pprStmtContext MDoExpr = ptext (sLit "'mdo' block") |
|---|
| 1357 | pprStmtContext ArrowExpr = ptext (sLit "'do' block in an arrow command") |
|---|
| 1358 | pprStmtContext ListComp = ptext (sLit "list comprehension") |
|---|
| 1359 | pprStmtContext MonadComp = ptext (sLit "monad comprehension") |
|---|
| 1360 | pprStmtContext PArrComp = ptext (sLit "array comprehension") |
|---|
| 1361 | pprStmtContext (PatGuard ctxt) = ptext (sLit "pattern guard for") $$ pprMatchContext ctxt |
|---|
| 1362 | |
|---|
| 1363 | -- Drop the inner contexts when reporting errors, else we get |
|---|
| 1364 | -- Unexpected transform statement |
|---|
| 1365 | -- in a transformed branch of |
|---|
| 1366 | -- transformed branch of |
|---|
| 1367 | -- transformed branch of monad comprehension |
|---|
| 1368 | pprStmtContext (ParStmtCtxt c) |
|---|
| 1369 | | opt_PprStyle_Debug = sep [ptext (sLit "parallel branch of"), pprAStmtContext c] |
|---|
| 1370 | | otherwise = pprStmtContext c |
|---|
| 1371 | pprStmtContext (TransStmtCtxt c) |
|---|
| 1372 | | opt_PprStyle_Debug = sep [ptext (sLit "transformed branch of"), pprAStmtContext c] |
|---|
| 1373 | | otherwise = pprStmtContext c |
|---|
| 1374 | |
|---|
| 1375 | |
|---|
| 1376 | -- Used to generate the string for a *runtime* error message |
|---|
| 1377 | matchContextErrString :: Outputable id => HsMatchContext id -> SDoc |
|---|
| 1378 | matchContextErrString (FunRhs fun _) = ptext (sLit "function") <+> ppr fun |
|---|
| 1379 | matchContextErrString CaseAlt = ptext (sLit "case") |
|---|
| 1380 | matchContextErrString PatBindRhs = ptext (sLit "pattern binding") |
|---|
| 1381 | matchContextErrString RecUpd = ptext (sLit "record update") |
|---|
| 1382 | matchContextErrString LambdaExpr = ptext (sLit "lambda") |
|---|
| 1383 | matchContextErrString ProcExpr = ptext (sLit "proc") |
|---|
| 1384 | matchContextErrString ThPatQuote = panic "matchContextErrString" -- Not used at runtime |
|---|
| 1385 | matchContextErrString (StmtCtxt (ParStmtCtxt c)) = matchContextErrString (StmtCtxt c) |
|---|
| 1386 | matchContextErrString (StmtCtxt (TransStmtCtxt c)) = matchContextErrString (StmtCtxt c) |
|---|
| 1387 | matchContextErrString (StmtCtxt (PatGuard _)) = ptext (sLit "pattern guard") |
|---|
| 1388 | matchContextErrString (StmtCtxt GhciStmt) = ptext (sLit "interactive GHCi command") |
|---|
| 1389 | matchContextErrString (StmtCtxt DoExpr) = ptext (sLit "'do' block") |
|---|
| 1390 | matchContextErrString (StmtCtxt ArrowExpr) = ptext (sLit "'do' block") |
|---|
| 1391 | matchContextErrString (StmtCtxt MDoExpr) = ptext (sLit "'mdo' block") |
|---|
| 1392 | matchContextErrString (StmtCtxt ListComp) = ptext (sLit "list comprehension") |
|---|
| 1393 | matchContextErrString (StmtCtxt MonadComp) = ptext (sLit "monad comprehension") |
|---|
| 1394 | matchContextErrString (StmtCtxt PArrComp) = ptext (sLit "array comprehension") |
|---|
| 1395 | \end{code} |
|---|
| 1396 | |
|---|
| 1397 | \begin{code} |
|---|
| 1398 | pprMatchInCtxt :: (OutputableBndr idL, OutputableBndr idR) |
|---|
| 1399 | => HsMatchContext idL -> Match idR -> SDoc |
|---|
| 1400 | pprMatchInCtxt ctxt match = hang (ptext (sLit "In") <+> pprMatchContext ctxt <> colon) |
|---|
| 1401 | 4 (pprMatch ctxt match) |
|---|
| 1402 | |
|---|
| 1403 | pprStmtInCtxt :: (OutputableBndr idL, OutputableBndr idR) |
|---|
| 1404 | => HsStmtContext idL -> StmtLR idL idR -> SDoc |
|---|
| 1405 | pprStmtInCtxt ctxt (LastStmt e _) |
|---|
| 1406 | | isListCompExpr ctxt -- For [ e | .. ], do not mutter about "stmts" |
|---|
| 1407 | = hang (ptext (sLit "In the expression:")) 2 (ppr e) |
|---|
| 1408 | |
|---|
| 1409 | pprStmtInCtxt ctxt stmt |
|---|
| 1410 | = hang (ptext (sLit "In a stmt of") <+> pprAStmtContext ctxt <> colon) |
|---|
| 1411 | 2 (ppr_stmt stmt) |
|---|
| 1412 | where |
|---|
| 1413 | -- For Group and Transform Stmts, don't print the nested stmts! |
|---|
| 1414 | ppr_stmt (TransStmt { trS_by = by, trS_using = using |
|---|
| 1415 | , trS_form = form }) = pprTransStmt by using form |
|---|
| 1416 | ppr_stmt stmt = pprStmt stmt |
|---|
| 1417 | \end{code} |
|---|