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