[project @ 2001-06-11 12:24:51 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        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                 [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 LambdaExpr 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 CaseAlt 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            => HsMatchContext id -> [Match id pat] -> SDoc
483 pprMatches ctxt matches = vcat (map (pprMatch ctxt) matches)
484
485 -- Exported to HsBinds, which can't see the defn of HsMatchContext
486 pprFunBind :: (Outputable id, Outputable pat)
487            => id -> [Match id pat] -> SDoc
488 pprFunBind fun matches = pprMatches (FunRhs fun) matches
489
490 -- Exported to HsBinds, which can't see the defn of HsMatchContext
491 pprPatBind :: (Outputable id, Outputable pat)
492            => pat -> GRHSs id pat -> SDoc
493 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
494
495
496 pprMatch :: (Outputable id, Outputable pat)
497            => HsMatchContext id -> Match id pat -> SDoc
498 pprMatch ctxt (Match _ pats maybe_ty grhss)
499   = pp_name ctxt <+> sep [sep (map ppr pats), 
500                      ppr_maybe_ty,
501                      nest 2 (pprGRHSs ctxt grhss)]
502   where
503     pp_name (FunRhs fun) = ppr fun
504     pp_name other        = empty
505     ppr_maybe_ty = case maybe_ty of
506                         Just ty -> dcolon <+> ppr ty
507                         Nothing -> empty
508
509
510 pprGRHSs :: (Outputable id, Outputable pat)
511          => HsMatchContext id -> GRHSs id pat -> SDoc
512 pprGRHSs ctxt (GRHSs grhss binds maybe_ty)
513   = vcat (map (pprGRHS ctxt) grhss)
514     $$
515     (if nullBinds binds then empty
516      else text "where" $$ nest 4 (pprDeeper (ppr binds)))
517
518
519 pprGRHS :: (Outputable id, Outputable pat)
520         => HsMatchContext id -> GRHS id pat -> SDoc
521
522 pprGRHS ctxt (GRHS [ResultStmt expr _] locn)
523  =  pp_rhs ctxt expr
524
525 pprGRHS ctxt (GRHS guarded locn)
526  = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
527  where
528     ResultStmt expr _ = last guarded    -- Last stmt should be a ResultStmt for guards
529     guards            = init guarded
530
531 pp_rhs ctxt rhs = ptext (matchSeparator ctxt) <+> pprDeeper (ppr rhs)
532 \end{code}
533
534
535
536 %************************************************************************
537 %*                                                                      *
538 \subsection{Do stmts and list comprehensions}
539 %*                                                                      *
540 %************************************************************************
541
542 \begin{code}
543 data Stmt id pat
544   = BindStmt    pat (HsExpr id pat) SrcLoc
545   | LetStmt     (HsBinds id pat)
546   | ResultStmt  (HsExpr id pat) SrcLoc  -- See notes that follow
547   | ExprStmt    (HsExpr id pat) SrcLoc  -- See notes that follow
548   | ParStmt     [[Stmt id pat]]         -- List comp only: parallel set of quals
549   | ParStmtOut  [([id], [Stmt id pat])] -- PLC after renaming; the ids are the binders
550                                         -- bound by the stmts
551 \end{code}
552
553 ExprStmts and ResultStmts are a bit tricky, because what they mean
554 depends on the context.  Consider the following contexts:
555
556         A do expression of type (m res_ty)
557         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
558         * ExprStmt E:   do { ....; E; ... }
559                 E :: m any_ty
560           Translation: E >> ...
561         
562         * ResultStmt E:   do { ....; E }
563                 E :: m res_ty
564           Translation: E
565         
566         A list comprehensions of type [elt_ty]
567         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
568         * ExprStmt E:   [ .. | .... E ]
569                         [ .. | ..., E, ... ]
570                         [ .. | .... | ..., E | ... ]
571                 E :: Bool
572           Translation: if E then fail else ...
573
574         * ResultStmt E:   [ E | ... ]
575                 E :: elt_ty
576           Translation: return E
577         
578         A guard list, guarding a RHS of type rhs_ty
579         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
580         * ExprStmt E:   f x | ..., E, ... = ...rhs...
581                 E :: Bool
582           Translation: if E then fail else ...
583         
584         * ResultStmt E:   f x | ...guards... = E
585                 E :: rhs_ty
586           Translation: E
587
588
589 \begin{code}
590 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
591 consLetStmt EmptyBinds stmts = stmts
592 consLetStmt binds      stmts = LetStmt binds : stmts
593 \end{code}
594
595 \begin{code}
596 instance (Outputable id, Outputable pat) =>
597                 Outputable (Stmt id pat) where
598     ppr stmt = pprStmt stmt
599
600 pprStmt (BindStmt pat expr _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
601 pprStmt (LetStmt binds)       = hsep [ptext SLIT("let"), pprBinds binds]
602 pprStmt (ExprStmt expr _)     = ppr expr
603 pprStmt (ResultStmt expr _)   = ppr expr
604 pprStmt (ParStmt stmtss)
605  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
606 pprStmt (ParStmtOut stmtss)
607  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
608
609 pprDo :: (Outputable id, Outputable pat) => HsDoContext -> [Stmt id pat] -> SDoc
610 pprDo DoExpr stmts   = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
611 pprDo ListComp stmts = brackets $
612                        hang (pprExpr expr <+> char '|')
613                           4 (interpp'SP quals)
614                      where
615                        ResultStmt expr _ = last stmts   -- Last stmt should
616                        quals             = init stmts   -- be an ResultStmt
617 \end{code}
618
619 %************************************************************************
620 %*                                                                      *
621 \subsection{Enumerations and list comprehensions}
622 %*                                                                      *
623 %************************************************************************
624
625 \begin{code}
626 data ArithSeqInfo id pat
627   = From            (HsExpr id pat)
628   | FromThen        (HsExpr id pat)
629                     (HsExpr id pat)
630   | FromTo          (HsExpr id pat)
631                     (HsExpr id pat)
632   | FromThenTo      (HsExpr id pat)
633                     (HsExpr id pat)
634                     (HsExpr id pat)
635 \end{code}
636
637 \begin{code}
638 instance (Outputable id, Outputable pat) =>
639                 Outputable (ArithSeqInfo id pat) where
640     ppr (From e1)               = hcat [ppr e1, pp_dotdot]
641     ppr (FromThen e1 e2)        = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
642     ppr (FromTo e1 e3)  = hcat [ppr e1, pp_dotdot, ppr e3]
643     ppr (FromThenTo e1 e2 e3)
644       = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
645
646 pp_dotdot = ptext SLIT(" .. ")
647 \end{code}
648
649
650 %************************************************************************
651 %*                                                                      *
652 \subsection{HsMatchCtxt}
653 %*                                                                      *
654 %************************************************************************
655
656 \begin{code}
657 data HsMatchContext id  -- Context of a Match or Stmt
658   = DoCtxt HsDoContext  -- Do-stmt or list comprehension
659   | FunRhs id           -- Function binding for f
660   | CaseAlt             -- Guard on a case alternative
661   | LambdaExpr          -- Lambda
662   | PatBindRhs          -- Pattern binding
663   | RecUpd              -- Record update
664   deriving ()
665
666 data HsDoContext = ListComp | DoExpr
667 \end{code}
668
669 \begin{code}
670 isDoExpr (DoCtxt DoExpr) = True
671 isDoExpr other           = False
672 \end{code}
673
674 \begin{code}
675 matchSeparator (FunRhs _)   = SLIT("=")
676 matchSeparator CaseAlt      = SLIT("->") 
677 matchSeparator LambdaExpr   = SLIT("->") 
678 matchSeparator PatBindRhs   = SLIT("=") 
679 matchSeparator (DoCtxt _)   = 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 case alternative")
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 (DoCtxt DoExpr)   = ptext SLIT("In a 'do' expression pattern binding")
690 pprMatchContext (DoCtxt ListComp) = ptext SLIT("In a 'list comprehension' pattern binding")
691
692 -- Used to generate the string for a *runtime* error message
693 matchContextErrString (FunRhs fun)      = "function " ++ showSDoc (ppr fun)
694 matchContextErrString CaseAlt           = "case"
695 matchContextErrString PatBindRhs        = "pattern binding"
696 matchContextErrString RecUpd            = "record update"
697 matchContextErrString LambdaExpr        =  "lambda"
698 matchContextErrString (DoCtxt DoExpr)   = "'do' expression"
699 matchContextErrString (DoCtxt ListComp) = "list comprehension"
700 \end{code}