d03654738aa1b9af0a0deeafcba10867c7c1d598
[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 Outputable       
21 import PprType          ( pprParendType )
22 import Type             ( Type )
23 import Var              ( TyVar )
24 import DataCon          ( DataCon )
25 import CStrings         ( CLabelString, pprCLabelString )
26 import BasicTypes       ( Boxity, tupleParens )
27 import SrcLoc           ( SrcLoc )
28 \end{code}
29
30 %************************************************************************
31 %*                                                                      *
32 \subsection{Expressions proper}
33 %*                                                                      *
34 %************************************************************************
35
36 \begin{code}
37 data HsExpr id pat
38   = HsVar       id              -- variable
39   | HsIPVar     id              -- implicit parameter
40   | HsOverLit   HsOverLit       -- Overloaded literals; eliminated by type checker
41   | HsLit       HsLit           -- Simple (non-overloaded) literals
42
43   | HsLam       (Match  id pat) -- lambda
44   | HsApp       (HsExpr id pat) -- application
45                 (HsExpr id pat)
46
47   -- Operator applications:
48   -- NB Bracketed ops such as (+) come out as Vars.
49
50   -- NB We need an expr for the operator in an OpApp/Section since
51   -- the typechecker may need to apply the operator to a few types.
52
53   | OpApp       (HsExpr id pat) -- left operand
54                 (HsExpr id pat) -- operator
55                 Fixity                          -- Renamer adds fixity; bottom until then
56                 (HsExpr id pat) -- right operand
57
58   -- We preserve prefix negation and parenthesis for the precedence parser.
59   -- They are eventually removed by the type checker.
60
61   | NegApp      (HsExpr id pat) -- negated expr
62
63   | HsPar       (HsExpr id pat) -- parenthesised expr
64
65   | SectionL    (HsExpr id pat) -- operand
66                 (HsExpr id pat) -- operator
67   | SectionR    (HsExpr id pat) -- operator
68                 (HsExpr id pat) -- operand
69                                 
70   | HsCase      (HsExpr id pat)
71                 [Match id pat]
72                 SrcLoc
73
74   | HsIf        (HsExpr id pat) --  predicate
75                 (HsExpr id pat) --  then part
76                 (HsExpr id pat) --  else part
77                 SrcLoc
78
79   | HsLet       (HsBinds id pat)        -- let(rec)
80                 (HsExpr  id pat)
81
82   | HsWith      (HsExpr id pat) -- implicit parameter binding
83                 [(id, HsExpr id pat)]
84
85   | HsDo        HsDoContext
86                 [Stmt id pat]   -- "do":one or more stmts
87                 SrcLoc
88
89   | HsDoOut     HsDoContext
90                 [Stmt id pat]   -- "do":one or more stmts
91                 id              -- id for return
92                 id              -- id for >>=
93                 id              -- id for fail
94                 Type            -- Type of the whole expression
95                 SrcLoc
96
97   | ExplicitList                -- syntactic list
98                 [HsExpr id pat]
99   | ExplicitListOut             -- TRANSLATION
100                 Type    -- Gives type of components of list
101                 [HsExpr id pat]
102
103   | ExplicitTuple               -- tuple
104                 [HsExpr id pat]
105                                 -- NB: Unit is ExplicitTuple []
106                                 -- for tuples, we can get the types
107                                 -- direct from the components
108                 Boxity
109
110
111         -- Record construction
112   | RecordCon   id                              -- The constructor
113                 (HsRecordBinds id pat)
114
115   | RecordConOut DataCon
116                 (HsExpr id pat)         -- Data con Id applied to type args
117                 (HsRecordBinds id pat)
118
119
120         -- Record update
121   | RecordUpd   (HsExpr id pat)
122                 (HsRecordBinds id pat)
123
124   | RecordUpdOut (HsExpr id pat)        -- TRANSLATION
125                  Type                   -- Type of *result* record (may differ from
126                                                 -- type of input record)
127                  [id]                   -- Dicts needed for construction
128                  (HsRecordBinds id pat)
129
130   | ExprWithTySig                       -- signature binding
131                 (HsExpr id pat)
132                 (HsType id)
133   | ArithSeqIn                          -- arithmetic sequence
134                 (ArithSeqInfo id pat)
135   | ArithSeqOut
136                 (HsExpr id pat)         -- (typechecked, of course)
137                 (ArithSeqInfo id pat)
138
139   | HsCCall     CLabelString    -- call into the C world; string is
140                 [HsExpr id pat] -- the C function; exprs are the
141                                 -- arguments to pass.
142                 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                 Type    -- The result type; will be *bottom*
150                                 -- until the typechecker gets ahold of it
151
152   | HsSCC       FAST_STRING     -- "set cost centre" (_scc_) annotation
153                 (HsExpr id pat) -- expr whose cost is to be measured
154
155 \end{code}
156
157 These constructors only appear temporarily in the parser.
158 The renamer translates them into the Right Thing.
159
160 \begin{code}
161   | EWildPat                    -- wildcard
162
163   | EAsPat      id              -- as pattern
164                 (HsExpr id pat)
165
166   | ELazyPat    (HsExpr id pat) -- ~ pattern
167
168   | HsType      (HsType id)     -- Explicit type argument; e.g  f {| Int |} x y
169 \end{code}
170
171 Everything from here on appears only in typechecker output.
172
173 \begin{code}
174   | TyLam                       -- TRANSLATION
175                 [TyVar]
176                 (HsExpr id pat)
177   | TyApp                       -- TRANSLATION
178                 (HsExpr id pat) -- generated by Spec
179                 [Type]
180
181   -- DictLam and DictApp are "inverses"
182   |  DictLam
183                 [id]
184                 (HsExpr id pat)
185   |  DictApp
186                 (HsExpr id pat)
187                 [id]
188
189 type HsRecordBinds id pat
190   = [(id, HsExpr id pat, Bool)]
191         -- True <=> source code used "punning",
192         -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
193 \end{code}
194
195 A @Dictionary@, unless of length 0 or 1, becomes a tuple.  A
196 @ClassDictLam dictvars methods expr@ is, therefore:
197 \begin{verbatim}
198 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
199 \end{verbatim}
200
201 \begin{code}
202 instance (Outputable id, Outputable pat) =>
203                 Outputable (HsExpr id pat) where
204     ppr expr = pprExpr expr
205 \end{code}
206
207 \begin{code}
208 pprExpr :: (Outputable id, Outputable pat)
209         => HsExpr id pat -> SDoc
210
211 pprExpr e = pprDeeper (ppr_expr e)
212 pprBinds b = pprDeeper (ppr b)
213
214 ppr_expr (HsVar v) 
215         -- Put it in parens if it's an operator
216   | isOperator v = parens (ppr v)
217   | otherwise    = ppr v
218
219 ppr_expr (HsIPVar v)     = char '?' <> ppr v
220 ppr_expr (HsLit lit)     = ppr lit
221 ppr_expr (HsOverLit lit) = ppr lit
222
223 ppr_expr (HsLam match)
224   = hsep [char '\\', nest 2 (pprMatch 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 ppr_expr (ExplicitListOut ty exprs)
306   = brackets (fsep (punctuate comma (map ppr_expr exprs)))
307
308 ppr_expr (ExplicitTuple exprs boxity)
309   = tupleParens boxity (sep (punctuate comma (map ppr_expr exprs)))
310
311 ppr_expr (RecordCon con_id rbinds)
312   = pp_rbinds (ppr con_id) rbinds
313 ppr_expr (RecordConOut data_con con rbinds)
314   = pp_rbinds (ppr con) rbinds
315
316 ppr_expr (RecordUpd aexp rbinds)
317   = pp_rbinds (pprParendExpr aexp) rbinds
318 ppr_expr (RecordUpdOut aexp _ _ rbinds)
319   = pp_rbinds (pprParendExpr aexp) rbinds
320
321 ppr_expr (ExprWithTySig expr sig)
322   = hang (nest 2 (ppr_expr expr) <+> dcolon)
323          4 (ppr sig)
324
325 ppr_expr (ArithSeqIn info)
326   = brackets (ppr info)
327 ppr_expr (ArithSeqOut expr info)
328   = brackets (ppr info)
329
330 ppr_expr EWildPat = char '_'
331 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
332 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
333
334 ppr_expr (HsCCall fun args _ is_asm result_ty)
335   = hang (if is_asm
336           then ptext SLIT("_casm_ ``") <> pprCLabelString fun <> ptext SLIT("''")
337           else ptext SLIT("_ccall_") <+> pprCLabelString fun)
338        4 (sep (map pprParendExpr args))
339
340 ppr_expr (HsSCC lbl expr)
341   = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext lbl), pprParendExpr expr ]
342
343 ppr_expr (TyLam tyvars expr)
344   = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
345          4 (ppr_expr expr)
346
347 ppr_expr (TyApp expr [ty])
348   = hang (ppr_expr expr) 4 (pprParendType ty)
349
350 ppr_expr (TyApp expr tys)
351   = hang (ppr_expr expr)
352          4 (brackets (interpp'SP tys))
353
354 ppr_expr (DictLam dictvars expr)
355   = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
356          4 (ppr_expr expr)
357
358 ppr_expr (DictApp expr [dname])
359   = hang (ppr_expr expr) 4 (ppr dname)
360
361 ppr_expr (DictApp expr dnames)
362   = hang (ppr_expr expr)
363          4 (brackets (interpp'SP dnames))
364
365 ppr_expr (HsType id) = ppr id
366     
367 \end{code}
368
369 Parenthesize unless very simple:
370 \begin{code}
371 pprParendExpr :: (Outputable id, Outputable pat)
372               => HsExpr id pat -> SDoc
373
374 pprParendExpr expr
375   = let
376         pp_as_was = pprExpr expr
377     in
378     case expr of
379       HsLit l               -> ppr l
380       HsOverLit l           -> ppr l
381
382       HsVar _               -> pp_as_was
383       HsIPVar _             -> pp_as_was
384       ExplicitList _        -> pp_as_was
385       ExplicitListOut _ _   -> pp_as_was
386       ExplicitTuple _ _     -> pp_as_was
387       HsPar _               -> pp_as_was
388
389       _                     -> parens pp_as_was
390 \end{code}
391
392 %************************************************************************
393 %*                                                                      *
394 \subsection{Record binds}
395 %*                                                                      *
396 %************************************************************************
397
398 \begin{code}
399 pp_rbinds :: (Outputable id, Outputable pat)
400               => SDoc 
401               -> HsRecordBinds id pat -> SDoc
402
403 pp_rbinds thing rbinds
404   = hang thing 
405          4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
406   where
407     pp_rbind (v, e, pun_flag) 
408       = getPprStyle $ \ sty ->
409         if pun_flag && userStyle sty then
410            ppr v
411         else
412            hsep [ppr v, char '=', ppr e]
413 \end{code}
414
415
416
417 %************************************************************************
418 %*                                                                      *
419 \subsection{@Match@, @GRHSs@, and @GRHS@ datatypes}
420 %*                                                                      *
421 %************************************************************************
422
423 @Match@es are sets of pattern bindings and right hand sides for
424 functions, patterns or case branches. For example, if a function @g@
425 is defined as:
426 \begin{verbatim}
427 g (x,y) = y
428 g ((x:ys),y) = y+1,
429 \end{verbatim}
430 then \tr{g} has two @Match@es: @(x,y) = y@ and @((x:ys),y) = y+1@.
431
432 It is always the case that each element of an @[Match]@ list has the
433 same number of @pats@s inside it.  This corresponds to saying that
434 a function defined by pattern matching must have the same number of
435 patterns in each equation.
436
437 \begin{code}
438 data Match id pat
439   = Match
440         [id]                    -- Tyvars wrt which this match is universally quantified
441                                 -- empty after typechecking
442         [pat]                   -- The patterns
443         (Maybe (HsType id))     -- A type signature for the result of the match
444                                 --      Nothing after typechecking
445
446         (GRHSs id pat)
447
448 -- GRHSs are used both for pattern bindings and for Matches
449 data GRHSs id pat       
450   = GRHSs [GRHS id pat]         -- Guarded RHSs
451           (HsBinds id pat)      -- The where clause
452           (Maybe Type)          -- Just rhs_ty after type checking
453
454 data GRHS id pat
455   = GRHS  [Stmt id pat]         -- The RHS is the final ResultStmt
456                                 -- I considered using a RetunStmt, but
457                                 -- it printed 'wrong' in error messages 
458           SrcLoc
459
460 mkSimpleMatch :: [pat] -> HsExpr id pat -> Maybe Type -> SrcLoc -> Match id pat
461 mkSimpleMatch pats rhs maybe_rhs_ty locn
462   = Match [] pats Nothing (GRHSs (unguardedRHS rhs locn) EmptyBinds maybe_rhs_ty)
463
464 unguardedRHS :: HsExpr id pat -> SrcLoc -> [GRHS id pat]
465 unguardedRHS rhs loc = [GRHS [ResultStmt rhs loc] loc]
466 \end{code}
467
468 @getMatchLoc@ takes a @Match@ and returns the
469 source-location gotten from the GRHS inside.
470 THis is something of a nuisance, but no more.
471
472 \begin{code}
473 getMatchLoc :: Match id pat -> SrcLoc
474 getMatchLoc (Match _ _ _ (GRHSs (GRHS _ loc : _) _ _)) = loc
475 \end{code}
476
477 We know the list must have at least one @Match@ in it.
478
479 \begin{code}
480 pprMatches :: (Outputable id, Outputable pat)
481            => HsMatchContext id -> [Match id pat] -> SDoc
482 pprMatches ctxt matches = vcat (map (pprMatch ctxt) matches)
483
484 -- Exported to HsBinds, which can't see the defn of HsMatchContext
485 pprFunBind :: (Outputable id, Outputable pat)
486            => id -> [Match id pat] -> SDoc
487 pprFunBind fun matches = pprMatches (FunRhs fun) matches
488
489 -- Exported to HsBinds, which can't see the defn of HsMatchContext
490 pprPatBind :: (Outputable id, Outputable pat)
491            => pat -> GRHSs id pat -> SDoc
492 pprPatBind pat grhss = sep [ppr pat, nest 4 (pprGRHSs PatBindRhs grhss)]
493
494
495 pprMatch :: (Outputable id, Outputable pat)
496            => HsMatchContext id -> Match id pat -> SDoc
497 pprMatch ctxt (Match _ pats maybe_ty grhss)
498   = pp_name ctxt <+> sep [sep (map ppr pats), 
499                      ppr_maybe_ty,
500                      nest 2 (pprGRHSs ctxt grhss)]
501   where
502     pp_name (FunRhs fun) = ppr fun
503     pp_name other        = empty
504     ppr_maybe_ty = case maybe_ty of
505                         Just ty -> dcolon <+> ppr ty
506                         Nothing -> empty
507
508
509 pprGRHSs :: (Outputable id, Outputable pat)
510          => HsMatchContext id -> GRHSs id pat -> SDoc
511 pprGRHSs ctxt (GRHSs grhss binds maybe_ty)
512   = vcat (map (pprGRHS ctxt) grhss)
513     $$
514     (if nullBinds binds then empty
515      else text "where" $$ nest 4 (pprDeeper (ppr binds)))
516
517
518 pprGRHS :: (Outputable id, Outputable pat)
519         => HsMatchContext id -> GRHS id pat -> SDoc
520
521 pprGRHS ctxt (GRHS [ResultStmt expr _] locn)
522  =  pp_rhs ctxt expr
523
524 pprGRHS ctxt (GRHS guarded locn)
525  = sep [char '|' <+> interpp'SP guards, pp_rhs ctxt expr]
526  where
527     ResultStmt expr _ = last guarded    -- Last stmt should be a ResultStmt for guards
528     guards            = init guarded
529
530 pp_rhs ctxt rhs = ptext (matchSeparator ctxt) <+> pprDeeper (ppr rhs)
531 \end{code}
532
533
534
535 %************************************************************************
536 %*                                                                      *
537 \subsection{Do stmts and list comprehensions}
538 %*                                                                      *
539 %************************************************************************
540
541 \begin{code}
542 data Stmt id pat
543   = BindStmt    pat (HsExpr id pat) SrcLoc
544   | LetStmt     (HsBinds id pat)
545   | ResultStmt  (HsExpr id pat) SrcLoc  -- See notes that follow
546   | ExprStmt    (HsExpr id pat) SrcLoc  -- See notes that follow
547   | ParStmt     [[Stmt id pat]]         -- List comp only: parallel set of quals
548   | ParStmtOut  [([id], [Stmt id pat])] -- PLC after renaming; the ids are the binders
549                                         -- bound by the stmts
550 \end{code}
551
552 ExprStmts and ResultStmts are a bit tricky, because what they mean
553 depends on the context.  Consider the following contexts:
554
555         A do expression of type (m res_ty)
556         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
557         * ExprStmt E:   do { ....; E; ... }
558                 E :: m any_ty
559           Translation: E >> ...
560         
561         * ResultStmt E:   do { ....; E }
562                 E :: m res_ty
563           Translation: E
564         
565         A list comprehensions of type [elt_ty]
566         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
567         * ExprStmt E:   [ .. | .... E ]
568                         [ .. | ..., E, ... ]
569                         [ .. | .... | ..., E | ... ]
570                 E :: Bool
571           Translation: if E then fail else ...
572
573         * ResultStmt E:   [ E | ... ]
574                 E :: elt_ty
575           Translation: return E
576         
577         A guard list, guarding a RHS of type rhs_ty
578         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
579         * ExprStmt E:   f x | ..., E, ... = ...rhs...
580                 E :: Bool
581           Translation: if E then fail else ...
582         
583         * ResultStmt E:   f x | ...guards... = E
584                 E :: rhs_ty
585           Translation: E
586
587
588 \begin{code}
589 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
590 consLetStmt EmptyBinds stmts = stmts
591 consLetStmt binds      stmts = LetStmt binds : stmts
592 \end{code}
593
594 \begin{code}
595 instance (Outputable id, Outputable pat) =>
596                 Outputable (Stmt id pat) where
597     ppr stmt = pprStmt stmt
598
599 pprStmt (BindStmt pat expr _) = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
600 pprStmt (LetStmt binds)       = hsep [ptext SLIT("let"), pprBinds binds]
601 pprStmt (ExprStmt expr _)     = ppr expr
602 pprStmt (ResultStmt expr _)   = ppr expr
603 pprStmt (ParStmt stmtss)
604  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
605 pprStmt (ParStmtOut stmtss)
606  = hsep (map (\stmts -> ptext SLIT("| ") <> ppr stmts) stmtss)
607
608 pprDo :: (Outputable id, Outputable pat) => HsDoContext -> [Stmt id pat] -> SDoc
609 pprDo DoExpr stmts   = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
610 pprDo ListComp stmts = brackets $
611                        hang (pprExpr expr <+> char '|')
612                           4 (interpp'SP quals)
613                      where
614                        ResultStmt expr _ = last stmts   -- Last stmt should
615                        quals             = init stmts   -- be an ResultStmt
616 \end{code}
617
618 %************************************************************************
619 %*                                                                      *
620 \subsection{Enumerations and list comprehensions}
621 %*                                                                      *
622 %************************************************************************
623
624 \begin{code}
625 data ArithSeqInfo id pat
626   = From            (HsExpr id pat)
627   | FromThen        (HsExpr id pat)
628                     (HsExpr id pat)
629   | FromTo          (HsExpr id pat)
630                     (HsExpr id pat)
631   | FromThenTo      (HsExpr id pat)
632                     (HsExpr id pat)
633                     (HsExpr id pat)
634 \end{code}
635
636 \begin{code}
637 instance (Outputable id, Outputable pat) =>
638                 Outputable (ArithSeqInfo id pat) where
639     ppr (From e1)               = hcat [ppr e1, pp_dotdot]
640     ppr (FromThen e1 e2)        = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
641     ppr (FromTo e1 e3)  = hcat [ppr e1, pp_dotdot, ppr e3]
642     ppr (FromThenTo e1 e2 e3)
643       = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
644
645 pp_dotdot = ptext SLIT(" .. ")
646 \end{code}
647
648
649 %************************************************************************
650 %*                                                                      *
651 \subsection{HsMatchCtxt}
652 %*                                                                      *
653 %************************************************************************
654
655 \begin{code}
656 data HsMatchContext id  -- Context of a Match or Stmt
657   = DoCtxt HsDoContext  -- Do-stmt or list comprehension
658   | FunRhs id           -- Function binding for f
659   | CaseAlt             -- Guard on a case alternative
660   | LambdaExpr          -- Lambda
661   | PatBindRhs          -- Pattern binding
662   | RecUpd              -- Record update
663   deriving ()
664
665 data HsDoContext = ListComp | DoExpr
666 \end{code}
667
668 \begin{code}
669 isDoExpr (DoCtxt DoExpr) = True
670 isDoExpr other           = False
671 \end{code}
672
673 \begin{code}
674 matchSeparator (FunRhs _)   = SLIT("=")
675 matchSeparator CaseAlt      = SLIT("->") 
676 matchSeparator LambdaExpr   = SLIT("->") 
677 matchSeparator PatBindRhs   = SLIT("=") 
678 matchSeparator (DoCtxt _)   = SLIT("<-")  
679 matchSeparator RecUpd       = panic "When is this used?"
680 \end{code}
681
682 \begin{code}
683 pprMatchContext (FunRhs fun)      = ptext SLIT("In the definition of") <+> quotes (ppr fun)
684 pprMatchContext CaseAlt           = ptext SLIT("In a case alternative")
685 pprMatchContext RecUpd            = ptext SLIT("In a record-update construct")
686 pprMatchContext PatBindRhs        = ptext SLIT("In a pattern binding")
687 pprMatchContext LambdaExpr        = ptext SLIT("In a lambda abstraction")
688 pprMatchContext (DoCtxt DoExpr)   = ptext SLIT("In a 'do' expression pattern binding")
689 pprMatchContext (DoCtxt ListComp) = ptext SLIT("In a 'list comprehension' pattern binding")
690
691 -- Used to generate the string for a *runtime* error message
692 matchContextErrString (FunRhs fun)      = "function " ++ showSDoc (ppr fun)
693 matchContextErrString CaseAlt           = "case"
694 matchContextErrString PatBindRhs        = "pattern binding"
695 matchContextErrString RecUpd            = "record update"
696 matchContextErrString LambdaExpr        =  "lambda"
697 matchContextErrString (DoCtxt DoExpr)   = "'do' expression"
698 matchContextErrString (DoCtxt ListComp) = "list comprehension"
699 \end{code}