91ddad3b9afe09c1babdf0bc68b3e8180e2b6b0d
[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       ( IPName, 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     (IPName 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                 [(IPName 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)     = 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            => [(IPName id, HsExpr id pat)] -> SDoc
417 pp_ipbinds pairs = hsep (punctuate semi (map pp_item pairs))
418                  where
419                    pp_item (id,rhs) = 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         [pat]                   -- The patterns
447         (Maybe (HsType id))     -- A type signature for the result of the match
448                                 --      Nothing after typechecking
449
450         (GRHSs id pat)
451
452 -- GRHSs are used both for pattern bindings and for Matches
453 data GRHSs id pat       
454   = GRHSs [GRHS id pat]         -- Guarded RHSs
455           (HsBinds id pat)      -- The where clause
456           PostTcType            -- Type of RHS (after type checking)
457
458 data GRHS id pat
459   = GRHS  [Stmt id pat]         -- The RHS is the final ResultStmt
460                                 -- I considered using a RetunStmt, but
461                                 -- it printed 'wrong' in error messages 
462           SrcLoc
463
464 mkSimpleMatch :: [pat] -> HsExpr id pat -> Type -> SrcLoc -> Match id pat
465 mkSimpleMatch pats rhs rhs_ty locn
466   = Match pats Nothing (GRHSs (unguardedRHS rhs locn) EmptyBinds rhs_ty)
467
468 unguardedRHS :: HsExpr id pat -> SrcLoc -> [GRHS id pat]
469 unguardedRHS rhs loc = [GRHS [ResultStmt rhs loc] loc]
470 \end{code}
471
472 @getMatchLoc@ takes a @Match@ and returns the
473 source-location gotten from the GRHS inside.
474 THis is something of a nuisance, but no more.
475
476 \begin{code}
477 getMatchLoc :: Match id pat -> SrcLoc
478 getMatchLoc (Match _ _ (GRHSs (GRHS _ loc : _) _ _)) = loc
479 \end{code}
480
481 We know the list must have at least one @Match@ in it.
482
483 \begin{code}
484 pprMatches :: (Outputable id, Outputable pat)
485            => HsMatchContext id -> [Match id pat] -> SDoc
486 pprMatches ctxt matches = vcat (map (pprMatch ctxt) matches)
487
488 -- Exported to HsBinds, which can't see the defn of HsMatchContext
489 pprFunBind :: (Outputable id, Outputable pat)
490            => id -> [Match id pat] -> SDoc
491 pprFunBind fun matches = pprMatches (FunRhs fun) matches
492
493 -- Exported to HsBinds, which can't see the defn of HsMatchContext
494 pprPatBind :: (Outputable id, Outputable pat)
495            => pat -> GRHSs id pat -> SDoc
496 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
497
498
499 pprMatch :: (Outputable id, Outputable pat)
500            => HsMatchContext id -> Match id pat -> SDoc
501 pprMatch ctxt (Match pats maybe_ty grhss)
502   = pp_name ctxt <+> sep [sep (map ppr pats), 
503                      ppr_maybe_ty,
504                      nest 2 (pprGRHSs ctxt grhss)]
505   where
506     pp_name (FunRhs fun) = ppr fun
507     pp_name other        = empty
508     ppr_maybe_ty = case maybe_ty of
509                         Just ty -> dcolon <+> ppr ty
510                         Nothing -> empty
511
512
513 pprGRHSs :: (Outputable id, Outputable pat)
514          => HsMatchContext id -> GRHSs id pat -> SDoc
515 pprGRHSs ctxt (GRHSs grhss binds ty)
516   = vcat (map (pprGRHS ctxt) grhss)
517     $$
518     (if nullBinds binds then empty
519      else text "where" $$ nest 4 (pprDeeper (ppr binds)))
520
521
522 pprGRHS :: (Outputable id, Outputable pat)
523         => HsMatchContext id -> GRHS id pat -> SDoc
524
525 pprGRHS ctxt (GRHS [ResultStmt expr _] locn)
526  =  pp_rhs ctxt expr
527
528 pprGRHS ctxt (GRHS guarded locn)
529  = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
530  where
531     ResultStmt expr _ = last guarded    -- Last stmt should be a ResultStmt for guards
532     guards            = init guarded
533
534 pp_rhs ctxt rhs = ptext (matchSeparator ctxt) <+> pprDeeper (ppr rhs)
535 \end{code}
536
537
538
539 %************************************************************************
540 %*                                                                      *
541 \subsection{Do stmts and list comprehensions}
542 %*                                                                      *
543 %************************************************************************
544
545 \begin{code}
546 data Stmt id pat
547   = BindStmt    pat (HsExpr id pat) SrcLoc
548   | LetStmt     (HsBinds id pat)
549   | ResultStmt  (HsExpr id pat) SrcLoc                  -- See notes that follow
550   | ExprStmt    (HsExpr id pat) PostTcType SrcLoc       -- See notes that follow
551         -- The type is the *element type* of the expression
552   | ParStmt     [[Stmt id pat]]                         -- List comp only: parallel set of quals
553   | ParStmtOut  [([id], [Stmt id pat])]                 -- PLC after renaming; the ids are the binders
554                                                         -- bound by the stmts
555 \end{code}
556
557 ExprStmts and ResultStmts are a bit tricky, because what they mean
558 depends on the context.  Consider the following contexts:
559
560         A do expression of type (m res_ty)
561         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
562         * ExprStmt E any_ty:   do { ....; E; ... }
563                 E :: m any_ty
564           Translation: E >> ...
565         
566         * ResultStmt E:   do { ....; E }
567                 E :: m res_ty
568           Translation: E
569         
570         A list comprehensions of type [elt_ty]
571         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
572         * ExprStmt E Bool:   [ .. | .... E ]
573                         [ .. | ..., E, ... ]
574                         [ .. | .... | ..., E | ... ]
575                 E :: Bool
576           Translation: if E then fail else ...
577
578         * ResultStmt E:   [ E | ... ]
579                 E :: elt_ty
580           Translation: return E
581         
582         A guard list, guarding a RHS of type rhs_ty
583         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
584         * ExprStmt E Bool:   f x | ..., E, ... = ...rhs...
585                 E :: Bool
586           Translation: if E then fail else ...
587         
588         * ResultStmt E:   f x | ...guards... = E
589                 E :: rhs_ty
590           Translation: E
591
592
593 \begin{code}
594 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
595 consLetStmt EmptyBinds stmts = stmts
596 consLetStmt binds      stmts = LetStmt binds : stmts
597 \end{code}
598
599 \begin{code}
600 instance (Outputable id, Outputable pat) =>
601                 Outputable (Stmt id pat) where
602     ppr stmt = pprStmt stmt
603
604 pprStmt (BindStmt pat expr _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
605 pprStmt (LetStmt binds)       = hsep [ptext SLIT("let"), pprBinds binds]
606 pprStmt (ExprStmt expr _ _)   = ppr expr
607 pprStmt (ResultStmt expr _)   = ppr expr
608 pprStmt (ParStmt stmtss)
609  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
610 pprStmt (ParStmtOut stmtss)
611  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
612
613 pprDo :: (Outputable id, Outputable pat) => HsDoContext -> [Stmt id pat] -> SDoc
614 pprDo DoExpr stmts   = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
615 pprDo ListComp stmts = brackets $
616                        hang (pprExpr expr <+> char '|')
617                           4 (interpp'SP quals)
618                      where
619                        ResultStmt expr _ = last stmts   -- Last stmt should
620                        quals             = init stmts   -- be an ResultStmt
621 \end{code}
622
623 %************************************************************************
624 %*                                                                      *
625 \subsection{Enumerations and list comprehensions}
626 %*                                                                      *
627 %************************************************************************
628
629 \begin{code}
630 data ArithSeqInfo id pat
631   = From            (HsExpr id pat)
632   | FromThen        (HsExpr id pat)
633                     (HsExpr id pat)
634   | FromTo          (HsExpr id pat)
635                     (HsExpr id pat)
636   | FromThenTo      (HsExpr id pat)
637                     (HsExpr id pat)
638                     (HsExpr id pat)
639 \end{code}
640
641 \begin{code}
642 instance (Outputable id, Outputable pat) =>
643                 Outputable (ArithSeqInfo id pat) where
644     ppr (From e1)               = hcat [ppr e1, pp_dotdot]
645     ppr (FromThen e1 e2)        = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
646     ppr (FromTo e1 e3)  = hcat [ppr e1, pp_dotdot, ppr e3]
647     ppr (FromThenTo e1 e2 e3)
648       = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
649
650 pp_dotdot = ptext SLIT(" .. ")
651 \end{code}
652
653
654 %************************************************************************
655 %*                                                                      *
656 \subsection{HsMatchCtxt}
657 %*                                                                      *
658 %************************************************************************
659
660 \begin{code}
661 data HsMatchContext id  -- Context of a Match or Stmt
662   = DoCtxt HsDoContext  -- Do-stmt or list comprehension
663   | FunRhs id           -- Function binding for f
664   | CaseAlt             -- Guard on a case alternative
665   | LambdaExpr          -- Lambda
666   | PatBindRhs          -- Pattern binding
667   | RecUpd              -- Record update
668   deriving ()
669
670 data HsDoContext = ListComp | DoExpr
671 \end{code}
672
673 \begin{code}
674 isDoExpr (DoCtxt DoExpr) = True
675 isDoExpr other           = False
676 \end{code}
677
678 \begin{code}
679 matchSeparator (FunRhs _)   = SLIT("=")
680 matchSeparator CaseAlt      = SLIT("->") 
681 matchSeparator LambdaExpr   = SLIT("->") 
682 matchSeparator PatBindRhs   = SLIT("=") 
683 matchSeparator (DoCtxt _)   = SLIT("<-")  
684 matchSeparator RecUpd       = panic "When is this used?"
685 \end{code}
686
687 \begin{code}
688 pprMatchContext (FunRhs fun)      = ptext SLIT("In the definition of") <+> quotes (ppr fun)
689 pprMatchContext CaseAlt           = ptext SLIT("In a case alternative")
690 pprMatchContext RecUpd            = ptext SLIT("In a record-update construct")
691 pprMatchContext PatBindRhs        = ptext SLIT("In a pattern binding")
692 pprMatchContext LambdaExpr        = ptext SLIT("In a lambda abstraction")
693 pprMatchContext (DoCtxt DoExpr)   = ptext SLIT("In a 'do' expression pattern binding")
694 pprMatchContext (DoCtxt ListComp) = ptext SLIT("In a 'list comprehension' pattern binding")
695
696 -- Used to generate the string for a *runtime* error message
697 matchContextErrString (FunRhs fun)      = "function " ++ showSDoc (ppr fun)
698 matchContextErrString CaseAlt           = "case"
699 matchContextErrString PatBindRhs        = "pattern binding"
700 matchContextErrString RecUpd            = "record update"
701 matchContextErrString LambdaExpr        =  "lambda"
702 matchContextErrString (DoCtxt DoExpr)   = "'do' expression"
703 matchContextErrString (DoCtxt ListComp) = "list comprehension"
704 \end{code}