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