a2b703fe2d153865a683b1a1dc95c84958db5bdf
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsExpr.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[HsExpr]{Abstract Haskell syntax: expressions}
5
6 \begin{code}
7 module HsExpr where
8
9 #include "HsVersions.h"
10
11 -- friends:
12 import HsBinds          ( HsBinds(..), nullBinds )
13 import HsLit            ( HsLit, HsOverLit )
14 import BasicTypes       ( Fixity(..) )
15 import HsTypes          ( HsType )
16 import HsImpExp         ( isOperator )
17
18 -- others:
19 import Name             ( Name )
20 import Outputable       
21 import PprType          ( pprParendType )
22 import Type             ( Type )
23 import Var              ( TyVar )
24 import DataCon          ( DataCon )
25 import CStrings         ( CLabelString, pprCLabelString )
26 import BasicTypes       ( Boxity, tupleParens )
27 import SrcLoc           ( SrcLoc )
28 \end{code}
29
30 %************************************************************************
31 %*                                                                      *
32 \subsection{Expressions proper}
33 %*                                                                      *
34 %************************************************************************
35
36 \begin{code}
37 data HsExpr id pat
38   = HsVar       id              -- variable
39   | HsIPVar     id              -- implicit parameter
40   | HsOverLit   HsOverLit       -- Overloaded literals; eliminated by type checker
41   | HsLit       HsLit           -- Simple (non-overloaded) literals
42
43   | HsLam       (Match  id pat) -- lambda
44   | HsApp       (HsExpr id pat) -- application
45                 (HsExpr id pat)
46
47   -- Operator applications:
48   -- NB Bracketed ops such as (+) come out as Vars.
49
50   -- NB We need an expr for the operator in an OpApp/Section since
51   -- the typechecker may need to apply the operator to a few types.
52
53   | OpApp       (HsExpr id pat) -- left operand
54                 (HsExpr id pat) -- operator
55                 Fixity                          -- Renamer adds fixity; bottom until then
56                 (HsExpr id pat) -- right operand
57
58   -- We preserve prefix negation and parenthesis for the precedence parser.
59   -- They are eventually removed by the type checker.
60
61   | NegApp      (HsExpr id pat) -- negated expr
62
63   | HsPar       (HsExpr id pat) -- parenthesised expr
64
65   | SectionL    (HsExpr id pat) -- operand
66                 (HsExpr id pat) -- operator
67   | SectionR    (HsExpr id pat) -- operator
68                 (HsExpr id pat) -- operand
69                                 
70   | HsCase      (HsExpr id pat)
71                 [Match id pat]
72                 SrcLoc
73
74   | HsIf        (HsExpr id pat) --  predicate
75                 (HsExpr id pat) --  then part
76                 (HsExpr id pat) --  else part
77                 SrcLoc
78
79   | HsLet       (HsBinds id pat)        -- let(rec)
80                 (HsExpr  id pat)
81
82   | HsWith      (HsExpr id pat) -- implicit parameter binding
83                 [(id, HsExpr id pat)]
84
85   | HsDo        HsMatchContext
86                 [Stmt id pat]   -- "do":one or more stmts
87                 SrcLoc
88
89   | HsDoOut     HsMatchContext
90                 [Stmt id pat]   -- "do":one or more stmts
91                 id              -- id for return
92                 id              -- id for >>=
93                 id              -- id for fail
94                 Type            -- Type of the whole expression
95                 SrcLoc
96
97   | ExplicitList                -- syntactic list
98                 [HsExpr id pat]
99   | ExplicitListOut             -- TRANSLATION
100                 Type    -- Gives type of components of list
101                 [HsExpr id pat]
102
103   | ExplicitTuple               -- tuple
104                 [HsExpr id pat]
105                                 -- NB: Unit is ExplicitTuple []
106                                 -- for tuples, we can get the types
107                                 -- direct from the components
108                 Boxity
109
110
111         -- Record construction
112   | RecordCon   id                              -- The constructor
113                 (HsRecordBinds id pat)
114
115   | RecordConOut DataCon
116                 (HsExpr id pat)         -- Data con Id applied to type args
117                 (HsRecordBinds id pat)
118
119
120         -- Record update
121   | RecordUpd   (HsExpr id pat)
122                 (HsRecordBinds id pat)
123
124   | RecordUpdOut (HsExpr id pat)        -- TRANSLATION
125                  Type                   -- Type of *result* record (may differ from
126                                                 -- type of input record)
127                  [id]                   -- Dicts needed for construction
128                  (HsRecordBinds id pat)
129
130   | ExprWithTySig                       -- signature binding
131                 (HsExpr id pat)
132                 (HsType id)
133   | ArithSeqIn                          -- arithmetic sequence
134                 (ArithSeqInfo id pat)
135   | ArithSeqOut
136                 (HsExpr id pat)         -- (typechecked, of course)
137                 (ArithSeqInfo id pat)
138
139   | HsCCall     CLabelString    -- call into the C world; string is
140                 [HsExpr id pat] -- the C function; exprs are the
141                                 -- arguments to pass.
142                 Bool            -- True <=> might cause Haskell
143                                 -- garbage-collection (must generate
144                                 -- more paranoid code)
145                 Bool            -- True <=> it's really a "casm"
146                                 -- NOTE: this CCall is the *boxed*
147                                 -- version; the desugarer will convert
148                                 -- it into the unboxed "ccall#".
149                 Type    -- The result type; will be *bottom*
150                                 -- until the typechecker gets ahold of it
151
152   | HsSCC       FAST_STRING     -- "set cost centre" (_scc_) annotation
153                 (HsExpr id pat) -- expr whose cost is to be measured
154
155 \end{code}
156
157 These constructors only appear temporarily in the parser.
158 The renamer translates them into the Right Thing.
159
160 \begin{code}
161   | EWildPat                    -- wildcard
162
163   | EAsPat      id              -- as pattern
164                 (HsExpr id pat)
165
166   | ELazyPat    (HsExpr id pat) -- ~ pattern
167
168   | HsType      (HsType id)     -- Explicit type argument; e.g  f {| Int |} x y
169 \end{code}
170
171 Everything from here on appears only in typechecker output.
172
173 \begin{code}
174   | TyLam                       -- TRANSLATION
175                 [TyVar]
176                 (HsExpr id pat)
177   | TyApp                       -- TRANSLATION
178                 (HsExpr id pat) -- generated by Spec
179                 [Type]
180
181   -- DictLam and DictApp are "inverses"
182   |  DictLam
183                 [id]
184                 (HsExpr id pat)
185   |  DictApp
186                 (HsExpr id pat)
187                 [id]
188
189 type HsRecordBinds id pat
190   = [(id, HsExpr id pat, Bool)]
191         -- True <=> source code used "punning",
192         -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
193 \end{code}
194
195 A @Dictionary@, unless of length 0 or 1, becomes a tuple.  A
196 @ClassDictLam dictvars methods expr@ is, therefore:
197 \begin{verbatim}
198 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
199 \end{verbatim}
200
201 \begin{code}
202 instance (Outputable id, Outputable pat) =>
203                 Outputable (HsExpr id pat) where
204     ppr expr = pprExpr expr
205 \end{code}
206
207 \begin{code}
208 pprExpr :: (Outputable id, Outputable pat)
209         => HsExpr id pat -> SDoc
210
211 pprExpr e = pprDeeper (ppr_expr e)
212 pprBinds b = pprDeeper (ppr b)
213
214 ppr_expr (HsVar v) 
215         -- Put it in parens if it's an operator
216   | isOperator v = parens (ppr v)
217   | otherwise    = ppr v
218
219 ppr_expr (HsIPVar v)     = {- char '?' <> -} ppr v
220 ppr_expr (HsLit lit)     = ppr lit
221 ppr_expr (HsOverLit lit) = ppr lit
222
223 ppr_expr (HsLam match)
224   = hsep [char '\\', nest 2 (pprMatch (True,empty) match)]
225
226 ppr_expr expr@(HsApp e1 e2)
227   = let (fun, args) = collect_args expr [] in
228     (ppr_expr fun) <+> (sep (map ppr_expr args))
229   where
230     collect_args (HsApp fun arg) args = collect_args fun (arg:args)
231     collect_args fun             args = (fun, args)
232
233 ppr_expr (OpApp e1 op fixity e2)
234   = case op of
235       HsVar v -> pp_infixly v
236       _       -> pp_prefixly
237   where
238     pp_e1 = pprParendExpr e1            -- Add parens to make precedence clear
239     pp_e2 = pprParendExpr e2
240
241     pp_prefixly
242       = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
243
244     pp_infixly v
245       = sep [pp_e1, hsep [pp_v_op, pp_e2]]
246       where
247         pp_v_op | isOperator v = ppr v
248                 | otherwise    = char '`' <> ppr v <> char '`'
249                 -- Put it in backquotes if it's not an operator already
250
251 ppr_expr (NegApp e) = char '-' <+> pprParendExpr e
252
253 ppr_expr (HsPar e) = parens (ppr_expr e)
254
255 ppr_expr (SectionL expr op)
256   = case op of
257       HsVar v -> pp_infixly v
258       _       -> pp_prefixly
259   where
260     pp_expr = pprParendExpr expr
261
262     pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
263                        4 (hsep [pp_expr, ptext SLIT("x_ )")])
264     pp_infixly v = parens (sep [pp_expr, ppr v])
265
266 ppr_expr (SectionR op expr)
267   = case op of
268       HsVar v -> pp_infixly v
269       _       -> pp_prefixly
270   where
271     pp_expr = pprParendExpr expr
272
273     pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
274                        4 ((<>) pp_expr rparen)
275     pp_infixly v
276       = parens (sep [ppr v, pp_expr])
277
278 ppr_expr (HsCase expr matches _)
279   = sep [ sep [ptext SLIT("case"), nest 4 (pprExpr expr), ptext SLIT("of")],
280             nest 2 (pprMatches (True, empty) matches) ]
281
282 ppr_expr (HsIf e1 e2 e3 _)
283   = sep [hsep [ptext SLIT("if"), nest 2 (pprExpr e1), ptext SLIT("then")],
284            nest 4 (pprExpr e2),
285            ptext SLIT("else"),
286            nest 4 (pprExpr e3)]
287
288 -- special case: let ... in let ...
289 ppr_expr (HsLet binds expr@(HsLet _ _))
290   = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
291          pprExpr expr]
292
293 ppr_expr (HsLet binds expr)
294   = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
295          hang (ptext SLIT("in"))  2 (ppr expr)]
296
297 ppr_expr (HsWith expr binds)
298   = hsep [ppr expr, ptext SLIT("with"), ppr binds]
299
300 ppr_expr (HsDo do_or_list_comp stmts _)            = pprDo do_or_list_comp stmts
301 ppr_expr (HsDoOut do_or_list_comp stmts _ _ _ _ _) = pprDo do_or_list_comp stmts
302
303 ppr_expr (ExplicitList exprs)
304   = brackets (fsep (punctuate comma (map ppr_expr exprs)))
305 ppr_expr (ExplicitListOut ty exprs)
306   = brackets (fsep (punctuate comma (map ppr_expr exprs)))
307
308 ppr_expr (ExplicitTuple exprs boxity)
309   = tupleParens boxity (sep (punctuate comma (map ppr_expr exprs)))
310
311 ppr_expr (RecordCon con_id rbinds)
312   = pp_rbinds (ppr con_id) rbinds
313 ppr_expr (RecordConOut data_con con rbinds)
314   = pp_rbinds (ppr con) rbinds
315
316 ppr_expr (RecordUpd aexp rbinds)
317   = pp_rbinds (pprParendExpr aexp) rbinds
318 ppr_expr (RecordUpdOut aexp _ _ rbinds)
319   = pp_rbinds (pprParendExpr aexp) rbinds
320
321 ppr_expr (ExprWithTySig expr sig)
322   = hang (nest 2 (ppr_expr expr) <+> dcolon)
323          4 (ppr sig)
324
325 ppr_expr (ArithSeqIn info)
326   = brackets (ppr info)
327 ppr_expr (ArithSeqOut expr info)
328   = brackets (ppr info)
329
330 ppr_expr EWildPat = char '_'
331 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
332 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
333
334 ppr_expr (HsCCall fun args _ is_asm result_ty)
335   = hang (if is_asm
336           then ptext SLIT("_casm_ ``") <> pprCLabelString fun <> ptext SLIT("''")
337           else ptext SLIT("_ccall_") <+> pprCLabelString fun)
338        4 (sep (map pprParendExpr args))
339
340 ppr_expr (HsSCC lbl expr)
341   = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext lbl), pprParendExpr expr ]
342
343 ppr_expr (TyLam tyvars expr)
344   = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
345          4 (ppr_expr expr)
346
347 ppr_expr (TyApp expr [ty])
348   = hang (ppr_expr expr) 4 (pprParendType ty)
349
350 ppr_expr (TyApp expr tys)
351   = hang (ppr_expr expr)
352          4 (brackets (interpp'SP tys))
353
354 ppr_expr (DictLam dictvars expr)
355   = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
356          4 (ppr_expr expr)
357
358 ppr_expr (DictApp expr [dname])
359   = hang (ppr_expr expr) 4 (ppr dname)
360
361 ppr_expr (DictApp expr dnames)
362   = hang (ppr_expr expr)
363          4 (brackets (interpp'SP dnames))
364
365 ppr_expr (HsType id) = ppr id
366     
367 \end{code}
368
369 Parenthesize unless very simple:
370 \begin{code}
371 pprParendExpr :: (Outputable id, Outputable pat)
372               => HsExpr id pat -> SDoc
373
374 pprParendExpr expr
375   = let
376         pp_as_was = pprExpr expr
377     in
378     case expr of
379       HsLit l               -> ppr l
380       HsOverLit l           -> ppr l
381
382       HsVar _               -> pp_as_was
383       HsIPVar _             -> pp_as_was
384       ExplicitList _        -> pp_as_was
385       ExplicitListOut _ _   -> pp_as_was
386       ExplicitTuple _ _     -> pp_as_was
387       HsPar _               -> pp_as_was
388
389       _                     -> parens pp_as_was
390 \end{code}
391
392 %************************************************************************
393 %*                                                                      *
394 \subsection{Record binds}
395 %*                                                                      *
396 %************************************************************************
397
398 \begin{code}
399 pp_rbinds :: (Outputable id, Outputable pat)
400               => SDoc 
401               -> HsRecordBinds id pat -> SDoc
402
403 pp_rbinds thing rbinds
404   = hang thing 
405          4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
406   where
407     pp_rbind (v, e, pun_flag) 
408       = getPprStyle $ \ sty ->
409         if pun_flag && userStyle sty then
410            ppr v
411         else
412            hsep [ppr v, char '=', ppr e]
413 \end{code}
414
415
416
417 %************************************************************************
418 %*                                                                      *
419 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
420 %*                                                                      *
421 %************************************************************************
422
423 @Match@es are sets of pattern bindings and right hand sides for
424 functions, patterns or case branches. For example, if a function @g@
425 is defined as:
426 \begin{verbatim}
427 g (x,y) = y
428 g ((x:ys),y) = y+1,
429 \end{verbatim}
430 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
431
432 It is always the case that each element of an @[Match]@ list has the
433 same number of @pats@s inside it.  This corresponds to saying that
434 a function defined by pattern matching must have the same number of
435 patterns in each equation.
436
437 \begin{code}
438 data Match id pat
439   = Match
440         [id]                    -- Tyvars wrt which this match is universally quantified
441                                 -- empty after typechecking
442         [pat]                   -- The patterns
443         (Maybe (HsType id))     -- A type signature for the result of the match
444                                 --      Nothing after typechecking
445
446         (GRHSs id pat)
447
448 -- GRHSs are used both for pattern bindings and for Matches
449 data GRHSs id pat       
450   = GRHSs [GRHS id pat]         -- Guarded RHSs
451           (HsBinds id pat)      -- The where clause
452           (Maybe Type)          -- Just rhs_ty after type checking
453
454 data GRHS id pat
455   = GRHS  [Stmt id pat]         -- The RHS is the final ExprStmt
456                                 -- I considered using a RetunStmt, but
457                                 -- it printed 'wrong' in error messages 
458           SrcLoc
459
460 mkSimpleMatch :: [pat] -> HsExpr id pat -> Maybe Type -> SrcLoc -> Match id pat
461 mkSimpleMatch pats rhs maybe_rhs_ty locn
462   = Match [] pats Nothing (GRHSs (unguardedRHS rhs locn) EmptyBinds maybe_rhs_ty)
463
464 unguardedRHS :: HsExpr id pat -> SrcLoc -> [GRHS id pat]
465 unguardedRHS rhs loc = [GRHS [ExprStmt rhs loc] loc]
466 \end{code}
467
468 @getMatchLoc@ takes a @Match@ and returns the
469 source-location gotten from the GRHS inside.
470 THis is something of a nuisance, but no more.
471
472 \begin{code}
473 getMatchLoc :: Match id pat -> SrcLoc
474 getMatchLoc (Match _ _ _ (GRHSs (GRHS _ loc : _) _ _)) = loc
475 \end{code}
476
477 We know the list must have at least one @Match@ in it.
478
479 \begin{code}
480 pprMatches :: (Outputable id, Outputable pat)
481            => (Bool, SDoc) -> [Match id pat] -> SDoc
482 pprMatches print_info matches = vcat (map (pprMatch print_info) matches)
483
484
485 pprMatch :: (Outputable id, Outputable pat)
486            => (Bool, SDoc) -> Match id pat -> SDoc
487 pprMatch print_info@(is_case, name) (Match _ pats maybe_ty grhss)
488   = maybe_name <+> sep [sep (map ppr pats), 
489                         ppr_maybe_ty,
490                         nest 2 (pprGRHSs is_case grhss)]
491   where
492     maybe_name | is_case   = empty
493                | otherwise = name
494     ppr_maybe_ty = case maybe_ty of
495                         Just ty -> dcolon <+> ppr ty
496                         Nothing -> empty
497
498
499 pprGRHSs :: (Outputable id, Outputable pat)
500          => Bool -> GRHSs id pat -> SDoc
501 pprGRHSs is_case (GRHSs grhss binds maybe_ty)
502   = vcat (map (pprGRHS is_case) grhss)
503     $$
504     (if nullBinds binds then empty
505      else text "where" $$ nest 4 (pprDeeper (ppr binds)))
506
507
508 pprGRHS :: (Outputable id, Outputable pat)
509         => Bool -> GRHS id pat -> SDoc
510
511 pprGRHS is_case (GRHS [ExprStmt expr _] locn)
512  =  text (if is_case then "->" else "=") <+> pprDeeper (ppr expr)
513
514 pprGRHS is_case (GRHS guarded locn)
515  = sep [char '|' <+> interpp'SP guards,
516         text (if is_case then "->" else "=") <+> pprDeeper (ppr expr)
517    ]
518  where
519     ExprStmt expr _ = last guarded      -- Last stmt should be a ExprStmt for guards
520     guards          = init guarded
521 \end{code}
522
523
524
525 %************************************************************************
526 %*                                                                      *
527 \subsection{Do stmts and list comprehensions}
528 %*                                                                      *
529 %************************************************************************
530
531 \begin{code}
532 data Stmt id pat
533   = BindStmt    pat (HsExpr id pat) SrcLoc
534   | LetStmt     (HsBinds id pat)
535   | ExprStmt    (HsExpr id pat) SrcLoc  -- See notes that follow
536   | ParStmt     [[Stmt id pat]]         -- List comp only: parallel set of quals
537   | ParStmtOut  [([id], [Stmt id pat])] -- PLC after renaming
538 \end{code}
539
540 ExprStmts are a bit tricky, because what 
541 they mean depends on the context.  Consider 
542                 ExprStmt E
543 in the following contexts:
544
545         A do expression of type (m res_ty)
546         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
547         * Non-last stmt in list:   do { ....; E; ... }
548                 E :: m any_ty
549           Translation: E >> ...
550         
551         * Last stmt in list:   do { ....; E }
552                 E :: m res_ty
553           Translation: E
554         
555         A list comprehensions of type [elt_ty]
556         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
557         * Non-last stmt in list:   [ .. | ..., E, ... ]
558                 E :: Bool
559           Translation: if E then fail else ...
560         
561         * Last stmt in list:   [ E | ... ]
562                 E :: elt_ty
563           Translation: return E
564         
565         A guard list, guarding a RHS of type rhs_ty
566         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
567         * Non-last stmt in list:   f x | ..., E, ... = ...rhs...
568                 E :: Bool
569           Translation: if E then fail else ...
570         
571         * Last stmt in list:   f x | ...guards... = E
572                 E :: rhs_ty
573           Translation: E
574
575 \begin{code}
576 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
577 consLetStmt EmptyBinds stmts = stmts
578 consLetStmt binds      stmts = LetStmt binds : stmts
579 \end{code}
580
581 \begin{code}
582 instance (Outputable id, Outputable pat) =>
583                 Outputable (Stmt id pat) where
584     ppr stmt = pprStmt stmt
585
586 pprStmt (ParStmt stmtss)
587  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
588 pprStmt (ParStmtOut stmtss)
589  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
590 pprStmt (BindStmt pat expr _)
591  = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
592 pprStmt (LetStmt binds)
593  = hsep [ptext SLIT("let"), pprBinds binds]
594 pprStmt (ExprStmt expr _)
595  = ppr expr
596
597 pprDo :: (Outputable id, Outputable pat) => HsMatchContext -> [Stmt id pat] -> SDoc
598 pprDo DoExpr stmts   = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
599 pprDo ListComp stmts = brackets $
600                        hang (pprExpr expr <+> char '|')
601                           4 (interpp'SP quals)
602                      where
603                        ExprStmt expr _ = last stmts     -- Last stmt should
604                        quals           = init stmts     -- be an ExprStmt
605 \end{code}
606
607 %************************************************************************
608 %*                                                                      *
609 \subsection{Enumerations and list comprehensions}
610 %*                                                                      *
611 %************************************************************************
612
613 \begin{code}
614 data ArithSeqInfo id pat
615   = From            (HsExpr id pat)
616   | FromThen        (HsExpr id pat)
617                     (HsExpr id pat)
618   | FromTo          (HsExpr id pat)
619                     (HsExpr id pat)
620   | FromThenTo      (HsExpr id pat)
621                     (HsExpr id pat)
622                     (HsExpr id pat)
623 \end{code}
624
625 \begin{code}
626 instance (Outputable id, Outputable pat) =>
627                 Outputable (ArithSeqInfo id pat) where
628     ppr (From e1)               = hcat [ppr e1, pp_dotdot]
629     ppr (FromThen e1 e2)        = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
630     ppr (FromTo e1 e3)  = hcat [ppr e1, pp_dotdot, ppr e3]
631     ppr (FromThenTo e1 e2 e3)
632       = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
633
634 pp_dotdot = ptext SLIT(" .. ")
635 \end{code}
636
637
638 %************************************************************************
639 %*                                                                      *
640 \subsection{HsMatchCtxt}
641 %*                                                                      *
642 %************************************************************************
643
644 \begin{code}
645 data HsMatchContext     -- Context of a Match or Stmt
646   = ListComp            -- List comprehension
647   | DoExpr              -- Do Statment
648
649   | FunRhs Name         -- Function binding for f
650   | CaseAlt             -- Guard on a case alternative
651   | LambdaExpr          -- Lambda
652   | PatBindRhs          -- Pattern binding
653   | RecUpd              -- Record update
654   deriving ()
655
656 -- It's convenient to have FunRhs as a Name
657 -- throughout so that HsMatchContext doesn't
658 -- need to be parameterised.
659 -- In the RdrName world we never use the FunRhs variant.
660 \end{code}
661
662 \begin{code}
663 isDoExpr DoExpr = True
664 isDoExpr other  = False
665
666 isDoOrListComp ListComp = True
667 isDoOrListComp DoExpr   = True
668 isDoOrListComp other    = False
669 \end{code}
670
671 \begin{code}
672 matchSeparator (FunRhs _)   = SLIT("=")
673 matchSeparator CaseAlt      = SLIT("->") 
674 matchSeparator LambdaExpr   = SLIT("->") 
675 matchSeparator PatBindRhs   = SLIT("=") 
676 matchSeparator DoExpr       = SLIT("<-")  
677 matchSeparator ListComp     = SLIT("<-")  
678 matchSeparator RecUpd       = panic "When is this used?"
679 \end{code}
680
681 \begin{code}
682 pprMatchContext (FunRhs fun) = ptext SLIT("in the definition of function") <+> quotes (ppr fun)
683 pprMatchContext CaseAlt      = ptext SLIT("in a group of case alternatives beginning")
684 pprMatchContext RecUpd       = ptext SLIT("in a record-update construct")
685 pprMatchContext PatBindRhs   = ptext SLIT("in a pattern binding")
686 pprMatchContext LambdaExpr   = ptext SLIT("in a lambda abstraction")
687 pprMatchContext DoExpr       = ptext SLIT("in a `do' expression pattern binding")
688 pprMatchContext ListComp     = ptext SLIT("in a `list comprension' pattern binding")
689 \end{code}