b7d45737a9219a28f42953dae2caa67a022a4b13
[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 HsTypes          ( PostTcType )
14 import HsLit            ( HsLit, HsOverLit )
15 import BasicTypes       ( Fixity(..) )
16 import HsTypes          ( HsType )
17 import HsImpExp         ( isOperator )
18
19 -- others:
20 import ForeignCall      ( Safety )
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        HsDoContext
87                 [Stmt id pat]   -- "do":one or more stmts
88                 SrcLoc
89
90   | HsDoOut     HsDoContext
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                 PostTcType      -- Gives type of components of list
100                 [HsExpr id pat]
101
102   | ExplicitTuple               -- tuple
103                 [HsExpr id pat]
104                                 -- NB: Unit is ExplicitTuple []
105                                 -- for tuples, we can get the types
106                                 -- direct from the components
107                 Boxity
108
109
110         -- Record construction
111   | RecordCon   id                              -- The constructor
112                 (HsRecordBinds id pat)
113
114   | RecordConOut DataCon
115                 (HsExpr id pat)         -- Data con Id applied to type args
116                 (HsRecordBinds id pat)
117
118
119         -- Record update
120   | RecordUpd   (HsExpr id pat)
121                 (HsRecordBinds id pat)
122
123   | RecordUpdOut (HsExpr id pat)        -- TRANSLATION
124                  Type                   -- Type of *input* record
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                 Safety          -- 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                 PostTcType      -- 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 LambdaExpr 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 CaseAlt 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
306 ppr_expr (ExplicitTuple exprs boxity)
307   = tupleParens boxity (sep (punctuate comma (map ppr_expr exprs)))
308
309 ppr_expr (RecordCon con_id rbinds)
310   = pp_rbinds (ppr con_id) rbinds
311 ppr_expr (RecordConOut data_con con rbinds)
312   = pp_rbinds (ppr con) rbinds
313
314 ppr_expr (RecordUpd aexp rbinds)
315   = pp_rbinds (pprParendExpr aexp) rbinds
316 ppr_expr (RecordUpdOut aexp _ _ _ rbinds)
317   = pp_rbinds (pprParendExpr aexp) rbinds
318
319 ppr_expr (ExprWithTySig expr sig)
320   = hang (nest 2 (ppr_expr expr) <+> dcolon)
321          4 (ppr sig)
322
323 ppr_expr (ArithSeqIn info)
324   = brackets (ppr info)
325 ppr_expr (ArithSeqOut expr info)
326   = brackets (ppr info)
327
328 ppr_expr EWildPat = char '_'
329 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
330 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
331
332 ppr_expr (HsCCall fun args _ is_asm result_ty)
333   = hang (if is_asm
334           then ptext SLIT("_casm_ ``") <> pprCLabelString fun <> ptext SLIT("''")
335           else ptext SLIT("_ccall_") <+> pprCLabelString fun)
336        4 (sep (map pprParendExpr args))
337
338 ppr_expr (HsSCC lbl expr)
339   = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext lbl), pprParendExpr expr ]
340
341 ppr_expr (TyLam tyvars expr)
342   = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
343          4 (ppr_expr expr)
344
345 ppr_expr (TyApp expr [ty])
346   = hang (ppr_expr expr) 4 (pprParendType ty)
347
348 ppr_expr (TyApp expr tys)
349   = hang (ppr_expr expr)
350          4 (brackets (interpp'SP tys))
351
352 ppr_expr (DictLam dictvars expr)
353   = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
354          4 (ppr_expr expr)
355
356 ppr_expr (DictApp expr [dname])
357   = hang (ppr_expr expr) 4 (ppr dname)
358
359 ppr_expr (DictApp expr dnames)
360   = hang (ppr_expr expr)
361          4 (brackets (interpp'SP dnames))
362
363 ppr_expr (HsType id) = ppr id
364     
365 \end{code}
366
367 Parenthesize unless very simple:
368 \begin{code}
369 pprParendExpr :: (Outputable id, Outputable pat)
370               => HsExpr id pat -> SDoc
371
372 pprParendExpr expr
373   = let
374         pp_as_was = pprExpr expr
375     in
376     case expr of
377       HsLit l               -> ppr l
378       HsOverLit l           -> ppr l
379
380       HsVar _               -> pp_as_was
381       HsIPVar _             -> pp_as_was
382       ExplicitList _ _      -> pp_as_was
383       ExplicitTuple _ _     -> pp_as_was
384       HsPar _               -> pp_as_was
385
386       _                     -> parens pp_as_was
387 \end{code}
388
389 %************************************************************************
390 %*                                                                      *
391 \subsection{Record binds}
392 %*                                                                      *
393 %************************************************************************
394
395 \begin{code}
396 pp_rbinds :: (Outputable id, Outputable pat)
397               => SDoc 
398               -> HsRecordBinds id pat -> SDoc
399
400 pp_rbinds thing rbinds
401   = hang thing 
402          4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
403   where
404     pp_rbind (v, e, pun_flag) 
405       = getPprStyle $ \ sty ->
406         if pun_flag && userStyle sty then
407            ppr v
408         else
409            hsep [ppr v, char '=', ppr e]
410 \end{code}
411
412
413
414 %************************************************************************
415 %*                                                                      *
416 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
417 %*                                                                      *
418 %************************************************************************
419
420 @Match@es are sets of pattern bindings and right hand sides for
421 functions, patterns or case branches. For example, if a function @g@
422 is defined as:
423 \begin{verbatim}
424 g (x,y) = y
425 g ((x:ys),y) = y+1,
426 \end{verbatim}
427 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
428
429 It is always the case that each element of an @[Match]@ list has the
430 same number of @pats@s inside it.  This corresponds to saying that
431 a function defined by pattern matching must have the same number of
432 patterns in each equation.
433
434 \begin{code}
435 data Match id pat
436   = Match
437         [id]                    -- Tyvars wrt which this match is universally quantified
438                                 -- empty after typechecking
439         [pat]                   -- The patterns
440         (Maybe (HsType id))     -- A type signature for the result of the match
441                                 --      Nothing after typechecking
442
443         (GRHSs id pat)
444
445 -- GRHSs are used both for pattern bindings and for Matches
446 data GRHSs id pat       
447   = GRHSs [GRHS id pat]         -- Guarded RHSs
448           (HsBinds id pat)      -- The where clause
449           PostTcType            -- Type of RHS (after type checking)
450
451 data GRHS id pat
452   = GRHS  [Stmt id pat]         -- The RHS is the final ResultStmt
453                                 -- I considered using a RetunStmt, but
454                                 -- it printed 'wrong' in error messages 
455           SrcLoc
456
457 mkSimpleMatch :: [pat] -> HsExpr id pat -> Type -> SrcLoc -> Match id pat
458 mkSimpleMatch pats rhs rhs_ty locn
459   = Match [] pats Nothing (GRHSs (unguardedRHS rhs locn) EmptyBinds rhs_ty)
460
461 unguardedRHS :: HsExpr id pat -> SrcLoc -> [GRHS id pat]
462 unguardedRHS rhs loc = [GRHS [ResultStmt rhs loc] loc]
463 \end{code}
464
465 @getMatchLoc@ takes a @Match@ and returns the
466 source-location gotten from the GRHS inside.
467 THis is something of a nuisance, but no more.
468
469 \begin{code}
470 getMatchLoc :: Match id pat -> SrcLoc
471 getMatchLoc (Match _ _ _ (GRHSs (GRHS _ loc : _) _ _)) = loc
472 \end{code}
473
474 We know the list must have at least one @Match@ in it.
475
476 \begin{code}
477 pprMatches :: (Outputable id, Outputable pat)
478            => HsMatchContext id -> [Match id pat] -> SDoc
479 pprMatches ctxt matches = vcat (map (pprMatch ctxt) matches)
480
481 -- Exported to HsBinds, which can't see the defn of HsMatchContext
482 pprFunBind :: (Outputable id, Outputable pat)
483            => id -> [Match id pat] -> SDoc
484 pprFunBind fun matches = pprMatches (FunRhs fun) matches
485
486 -- Exported to HsBinds, which can't see the defn of HsMatchContext
487 pprPatBind :: (Outputable id, Outputable pat)
488            => pat -> GRHSs id pat -> SDoc
489 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
490
491
492 pprMatch :: (Outputable id, Outputable pat)
493            => HsMatchContext id -> Match id pat -> SDoc
494 pprMatch ctxt (Match _ pats maybe_ty grhss)
495   = pp_name ctxt <+> sep [sep (map ppr pats), 
496                      ppr_maybe_ty,
497                      nest 2 (pprGRHSs ctxt grhss)]
498   where
499     pp_name (FunRhs fun) = ppr fun
500     pp_name other        = empty
501     ppr_maybe_ty = case maybe_ty of
502                         Just ty -> dcolon <+> ppr ty
503                         Nothing -> empty
504
505
506 pprGRHSs :: (Outputable id, Outputable pat)
507          => HsMatchContext id -> GRHSs id pat -> SDoc
508 pprGRHSs ctxt (GRHSs grhss binds ty)
509   = vcat (map (pprGRHS ctxt) grhss)
510     $$
511     (if nullBinds binds then empty
512      else text "where" $$ nest 4 (pprDeeper (ppr binds)))
513
514
515 pprGRHS :: (Outputable id, Outputable pat)
516         => HsMatchContext id -> GRHS id pat -> SDoc
517
518 pprGRHS ctxt (GRHS [ResultStmt expr _] locn)
519  =  pp_rhs ctxt expr
520
521 pprGRHS ctxt (GRHS guarded locn)
522  = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
523  where
524     ResultStmt expr _ = last guarded    -- Last stmt should be a ResultStmt for guards
525     guards            = init guarded
526
527 pp_rhs ctxt rhs = ptext (matchSeparator ctxt) <+> pprDeeper (ppr rhs)
528 \end{code}
529
530
531
532 %************************************************************************
533 %*                                                                      *
534 \subsection{Do stmts and list comprehensions}
535 %*                                                                      *
536 %************************************************************************
537
538 \begin{code}
539 data Stmt id pat
540   = BindStmt    pat (HsExpr id pat) SrcLoc
541   | LetStmt     (HsBinds id pat)
542   | ResultStmt  (HsExpr id pat) SrcLoc                  -- See notes that follow
543   | ExprStmt    (HsExpr id pat) PostTcType SrcLoc       -- See notes that follow
544         -- The type is the *element type* of the expression
545   | ParStmt     [[Stmt id pat]]                         -- List comp only: parallel set of quals
546   | ParStmtOut  [([id], [Stmt id pat])]                 -- PLC after renaming; the ids are the binders
547                                                         -- bound by the stmts
548 \end{code}
549
550 ExprStmts and ResultStmts are a bit tricky, because what they mean
551 depends on the context.  Consider the following contexts:
552
553         A do expression of type (m res_ty)
554         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
555         * ExprStmt E any_ty:   do { ....; E; ... }
556                 E :: m any_ty
557           Translation: E >> ...
558         
559         * ResultStmt E:   do { ....; E }
560                 E :: m res_ty
561           Translation: E
562         
563         A list comprehensions of type [elt_ty]
564         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
565         * ExprStmt E Bool:   [ .. | .... E ]
566                         [ .. | ..., E, ... ]
567                         [ .. | .... | ..., E | ... ]
568                 E :: Bool
569           Translation: if E then fail else ...
570
571         * ResultStmt E:   [ E | ... ]
572                 E :: elt_ty
573           Translation: return E
574         
575         A guard list, guarding a RHS of type rhs_ty
576         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
577         * ExprStmt E Bool:   f x | ..., E, ... = ...rhs...
578                 E :: Bool
579           Translation: if E then fail else ...
580         
581         * ResultStmt E:   f x | ...guards... = E
582                 E :: rhs_ty
583           Translation: E
584
585
586 \begin{code}
587 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
588 consLetStmt EmptyBinds stmts = stmts
589 consLetStmt binds      stmts = LetStmt binds : stmts
590 \end{code}
591
592 \begin{code}
593 instance (Outputable id, Outputable pat) =>
594                 Outputable (Stmt id pat) where
595     ppr stmt = pprStmt stmt
596
597 pprStmt (BindStmt pat expr _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
598 pprStmt (LetStmt binds)       = hsep [ptext SLIT("let"), pprBinds binds]
599 pprStmt (ExprStmt expr _ _)   = ppr expr
600 pprStmt (ResultStmt expr _)   = ppr expr
601 pprStmt (ParStmt stmtss)
602  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
603 pprStmt (ParStmtOut stmtss)
604  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
605
606 pprDo :: (Outputable id, Outputable pat) => HsDoContext -> [Stmt id pat] -> SDoc
607 pprDo DoExpr stmts   = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
608 pprDo ListComp stmts = brackets $
609                        hang (pprExpr expr <+> char '|')
610                           4 (interpp'SP quals)
611                      where
612                        ResultStmt expr _ = last stmts   -- Last stmt should
613                        quals             = init stmts   -- be an ResultStmt
614 \end{code}
615
616 %************************************************************************
617 %*                                                                      *
618 \subsection{Enumerations and list comprehensions}
619 %*                                                                      *
620 %************************************************************************
621
622 \begin{code}
623 data ArithSeqInfo id pat
624   = From            (HsExpr id pat)
625   | FromThen        (HsExpr id pat)
626                     (HsExpr id pat)
627   | FromTo          (HsExpr id pat)
628                     (HsExpr id pat)
629   | FromThenTo      (HsExpr id pat)
630                     (HsExpr id pat)
631                     (HsExpr id pat)
632 \end{code}
633
634 \begin{code}
635 instance (Outputable id, Outputable pat) =>
636                 Outputable (ArithSeqInfo id pat) where
637     ppr (From e1)               = hcat [ppr e1, pp_dotdot]
638     ppr (FromThen e1 e2)        = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
639     ppr (FromTo e1 e3)  = hcat [ppr e1, pp_dotdot, ppr e3]
640     ppr (FromThenTo e1 e2 e3)
641       = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
642
643 pp_dotdot = ptext SLIT(" .. ")
644 \end{code}
645
646
647 %************************************************************************
648 %*                                                                      *
649 \subsection{HsMatchCtxt}
650 %*                                                                      *
651 %************************************************************************
652
653 \begin{code}
654 data HsMatchContext id  -- Context of a Match or Stmt
655   = DoCtxt HsDoContext  -- Do-stmt or list comprehension
656   | FunRhs id           -- Function binding for f
657   | CaseAlt             -- Guard on a case alternative
658   | LambdaExpr          -- Lambda
659   | PatBindRhs          -- Pattern binding
660   | RecUpd              -- Record update
661   deriving ()
662
663 data HsDoContext = ListComp | DoExpr
664 \end{code}
665
666 \begin{code}
667 isDoExpr (DoCtxt DoExpr) = True
668 isDoExpr 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 (DoCtxt _)   = SLIT("<-")  
677 matchSeparator RecUpd       = panic "When is this used?"
678 \end{code}
679
680 \begin{code}
681 pprMatchContext (FunRhs fun)      = ptext SLIT("In the definition of") <+> quotes (ppr fun)
682 pprMatchContext CaseAlt           = ptext SLIT("In a case alternative")
683 pprMatchContext RecUpd            = ptext SLIT("In a record-update construct")
684 pprMatchContext PatBindRhs        = ptext SLIT("In a pattern binding")
685 pprMatchContext LambdaExpr        = ptext SLIT("In a lambda abstraction")
686 pprMatchContext (DoCtxt DoExpr)   = ptext SLIT("In a 'do' expression pattern binding")
687 pprMatchContext (DoCtxt ListComp) = ptext SLIT("In a 'list comprehension' pattern binding")
688
689 -- Used to generate the string for a *runtime* error message
690 matchContextErrString (FunRhs fun)      = "function " ++ showSDoc (ppr fun)
691 matchContextErrString CaseAlt           = "case"
692 matchContextErrString PatBindRhs        = "pattern binding"
693 matchContextErrString RecUpd            = "record update"
694 matchContextErrString LambdaExpr        =  "lambda"
695 matchContextErrString (DoCtxt DoExpr)   = "'do' expression"
696 matchContextErrString (DoCtxt ListComp) = "list comprehension"
697 \end{code}