[project @ 2001-05-10 09:00:28 by simonmar]
[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 ResultStmt
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 [ResultStmt 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 [ResultStmt expr _] locn)
512  =  pp_rhs is_case expr
513
514 pprGRHS is_case (GRHS guarded locn)
515  = sep [char '|' <+> interpp'SP guards, pp_rhs is_case expr]
516  where
517     ResultStmt expr _ = last guarded    -- Last stmt should be a ResultStmt for guards
518     guards            = init guarded
519
520 pp_rhs is_case rhs = text (if is_case then "->" else "=") <+> pprDeeper (ppr rhs)
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   | ResultStmt  (HsExpr id pat) SrcLoc  -- See notes that follow
536   | ExprStmt    (HsExpr id pat) SrcLoc  -- See notes that follow
537   | ParStmt     [[Stmt id pat]]         -- List comp only: parallel set of quals
538   | ParStmtOut  [([id], [Stmt id pat])] -- PLC after renaming; the ids are the binders
539                                         -- bound by the stmts
540 \end{code}
541
542 ExprStmts and ResultStmts are a bit tricky, because what they mean
543 depends on the context.  Consider the following contexts:
544
545         A do expression of type (m res_ty)
546         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
547         * ExprStmt E:   do { ....; E; ... }
548                 E :: m any_ty
549           Translation: E >> ...
550         
551         * ResultStmt E:   do { ....; E }
552                 E :: m res_ty
553           Translation: E
554         
555         A list comprehensions of type [elt_ty]
556         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
557         * ExprStmt E:   [ .. | .... E ]
558                         [ .. | ..., E, ... ]
559                         [ .. | .... | ..., E | ... ]
560                 E :: Bool
561           Translation: if E then fail else ...
562
563         * ResultStmt E:   [ E | ... ]
564                 E :: elt_ty
565           Translation: return E
566         
567         A guard list, guarding a RHS of type rhs_ty
568         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
569         * ExprStmt E:   f x | ..., E, ... = ...rhs...
570                 E :: Bool
571           Translation: if E then fail else ...
572         
573         * ResultStmt E:   f x | ...guards... = E
574                 E :: rhs_ty
575           Translation: E
576
577
578 \begin{code}
579 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
580 consLetStmt EmptyBinds stmts = stmts
581 consLetStmt binds      stmts = LetStmt binds : stmts
582 \end{code}
583
584 \begin{code}
585 instance (Outputable id, Outputable pat) =>
586                 Outputable (Stmt id pat) where
587     ppr stmt = pprStmt stmt
588
589 pprStmt (BindStmt pat expr _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
590 pprStmt (LetStmt binds)       = hsep [ptext SLIT("let"), pprBinds binds]
591 pprStmt (ExprStmt expr _)     = ppr expr
592 pprStmt (ResultStmt expr _)   = ppr expr
593 pprStmt (ParStmt stmtss)
594  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
595 pprStmt (ParStmtOut stmtss)
596  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
597
598 pprDo :: (Outputable id, Outputable pat) => HsMatchContext -> [Stmt id pat] -> SDoc
599 pprDo DoExpr stmts   = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
600 pprDo ListComp stmts = brackets $
601                        hang (pprExpr expr <+> char '|')
602                           4 (interpp'SP quals)
603                      where
604                        ResultStmt expr _ = last stmts   -- Last stmt should
605                        quals             = init stmts   -- be an ResultStmt
606 \end{code}
607
608 %************************************************************************
609 %*                                                                      *
610 \subsection{Enumerations and list comprehensions}
611 %*                                                                      *
612 %************************************************************************
613
614 \begin{code}
615 data ArithSeqInfo id pat
616   = From            (HsExpr id pat)
617   | FromThen        (HsExpr id pat)
618                     (HsExpr id pat)
619   | FromTo          (HsExpr id pat)
620                     (HsExpr id pat)
621   | FromThenTo      (HsExpr id pat)
622                     (HsExpr id pat)
623                     (HsExpr id pat)
624 \end{code}
625
626 \begin{code}
627 instance (Outputable id, Outputable pat) =>
628                 Outputable (ArithSeqInfo id pat) where
629     ppr (From e1)               = hcat [ppr e1, pp_dotdot]
630     ppr (FromThen e1 e2)        = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
631     ppr (FromTo e1 e3)  = hcat [ppr e1, pp_dotdot, ppr e3]
632     ppr (FromThenTo e1 e2 e3)
633       = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
634
635 pp_dotdot = ptext SLIT(" .. ")
636 \end{code}
637
638
639 %************************************************************************
640 %*                                                                      *
641 \subsection{HsMatchCtxt}
642 %*                                                                      *
643 %************************************************************************
644
645 \begin{code}
646 data HsMatchContext     -- Context of a Match or Stmt
647   = ListComp            -- List comprehension
648   | DoExpr              -- Do Statment
649
650   | FunRhs Name         -- Function binding for f
651   | CaseAlt             -- Guard on a case alternative
652   | LambdaExpr          -- Lambda
653   | PatBindRhs          -- Pattern binding
654   | RecUpd              -- Record update
655   deriving ()
656
657 -- It's convenient to have FunRhs as a Name
658 -- throughout so that HsMatchContext doesn't
659 -- need to be parameterised.
660 -- In the RdrName world we never use the FunRhs variant.
661 \end{code}
662
663 \begin{code}
664 isDoExpr DoExpr = True
665 isDoExpr other  = False
666
667 isDoOrListComp ListComp = True
668 isDoOrListComp DoExpr   = True
669 isDoOrListComp other    = False
670 \end{code}
671
672 \begin{code}
673 matchSeparator (FunRhs _)   = SLIT("=")
674 matchSeparator CaseAlt      = SLIT("->") 
675 matchSeparator LambdaExpr   = SLIT("->") 
676 matchSeparator PatBindRhs   = SLIT("=") 
677 matchSeparator DoExpr       = SLIT("<-")  
678 matchSeparator ListComp     = SLIT("<-")  
679 matchSeparator RecUpd       = panic "When is this used?"
680 \end{code}
681
682 \begin{code}
683 pprMatchContext (FunRhs fun) = ptext SLIT("In the definition of") <+> quotes (ppr fun)
684 pprMatchContext CaseAlt      = ptext SLIT("In a group of case alternatives beginning")
685 pprMatchContext RecUpd       = ptext SLIT("In a record-update construct")
686 pprMatchContext PatBindRhs   = ptext SLIT("In a pattern binding")
687 pprMatchContext LambdaExpr   = ptext SLIT("In a lambda abstraction")
688 pprMatchContext DoExpr       = ptext SLIT("In a 'do' expression pattern binding")
689 pprMatchContext ListComp     = ptext SLIT("In a 'list comprehension' pattern binding")
690 \end{code}