2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsExpr]{Abstract Haskell syntax: expressions}
9 #include "HsVersions.h"
12 import {-# SOURCE #-} HsMatches ( pprMatches, pprMatch, Match )
14 import HsBinds ( HsBinds(..) )
15 import HsBasic ( HsLit )
16 import BasicTypes ( Fixity(..), FixityDirection(..) )
17 import HsTypes ( HsType )
20 import Name ( Name, isLexId )
22 import PprType ( pprType, pprParendType )
24 import Var ( TyVar, Id )
25 import DataCon ( DataCon )
26 import SrcLoc ( SrcLoc )
29 %************************************************************************
31 \subsection{Expressions proper}
33 %************************************************************************
37 = HsVar id -- variable
38 | HsIPVar id -- implicit parameter
39 | HsLit HsLit -- literal
40 | HsLitOut HsLit -- TRANSLATION
41 Type -- (with its type)
43 | HsLam (Match id pat) -- lambda
44 | HsApp (HsExpr id pat) -- application
47 -- Operator applications:
48 -- NB Bracketed ops such as (+) come out as Vars.
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.
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
58 -- We preserve prefix negation and parenthesis for the precedence parser.
59 -- They are eventually removed by the type checker.
61 | NegApp (HsExpr id pat) -- negated expr
62 (HsExpr id pat) -- the negate id (in a HsVar)
64 | HsPar (HsExpr id pat) -- parenthesised expr
66 | SectionL (HsExpr id pat) -- operand
67 (HsExpr id pat) -- operator
68 | SectionR (HsExpr id pat) -- operator
69 (HsExpr id pat) -- operand
71 | HsCase (HsExpr id pat)
75 | HsIf (HsExpr id pat) -- predicate
76 (HsExpr id pat) -- then part
77 (HsExpr id pat) -- else part
80 | HsLet (HsBinds id pat) -- let(rec)
83 | HsWith (HsExpr id pat) -- implicit parameter binding
87 [Stmt id pat] -- "do":one or more stmts
91 [Stmt id pat] -- "do":one or more stmts
95 Type -- Type of the whole expression
98 | ExplicitList -- syntactic list
100 | ExplicitListOut -- TRANSLATION
101 Type -- Gives type of components of list
104 | ExplicitTuple -- tuple
106 -- NB: Unit is ExplicitTuple []
107 -- for tuples, we can get the types
108 -- direct from the components
112 -- Record construction
113 | RecordCon id -- The constructor
114 (HsRecordBinds id pat)
116 | RecordConOut DataCon
117 (HsExpr id pat) -- Data con Id applied to type args
118 (HsRecordBinds id pat)
122 | RecordUpd (HsExpr id pat)
123 (HsRecordBinds id pat)
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)
131 | ExprWithTySig -- signature binding
134 | ArithSeqIn -- arithmetic sequence
135 (ArithSeqInfo id pat)
137 (HsExpr id pat) -- (typechecked, of course)
138 (ArithSeqInfo id pat)
140 | HsCCall FAST_STRING -- call into the C world; string is
141 [HsExpr id pat] -- the C function; exprs are the
142 -- arguments to pass.
143 Bool -- 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
153 | HsSCC FAST_STRING -- "set cost centre" (_scc_) annotation
154 (HsExpr id pat) -- expr whose cost is to be measured
157 These constructors only appear temporarily in the parser.
158 The renamer translates them into the Right Thing.
161 | EWildPat -- wildcard
163 | EAsPat id -- as pattern
166 | ELazyPat (HsExpr id pat) -- ~ pattern
169 Everything from here on appears only in typechecker output.
172 | TyLam -- TRANSLATION
175 | TyApp -- TRANSLATION
176 (HsExpr id pat) -- generated by Spec
179 -- DictLam and DictApp are "inverses"
187 type HsRecordBinds id pat
188 = [(id, HsExpr id pat, Bool)]
189 -- True <=> source code used "punning",
190 -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
193 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
194 @ClassDictLam dictvars methods expr@ is, therefore:
196 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
200 instance (Outputable id, Outputable pat) =>
201 Outputable (HsExpr id pat) where
202 ppr expr = pprExpr expr
206 pprExpr :: (Outputable id, Outputable pat)
207 => HsExpr id pat -> SDoc
209 pprExpr e = pprDeeper (ppr_expr e)
210 pprBinds b = pprDeeper (ppr b)
212 ppr_expr (HsVar v) = ppr v
213 ppr_expr (HsIPVar v) = {- char '?' <> -} ppr v
215 ppr_expr (HsLit lit) = ppr lit
216 ppr_expr (HsLitOut lit _) = ppr lit
218 ppr_expr (HsLam match)
219 = hsep [char '\\', nest 2 (pprMatch (True,empty) match)]
221 ppr_expr expr@(HsApp e1 e2)
222 = let (fun, args) = collect_args expr [] in
223 (ppr_expr fun) <+> (sep (map ppr_expr args))
225 collect_args (HsApp fun arg) args = collect_args fun (arg:args)
226 collect_args fun args = (fun, args)
228 ppr_expr (OpApp e1 op fixity e2)
230 HsVar v -> pp_infixly v
233 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
234 pp_e2 = pprParendExpr e2
237 = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
240 = sep [pp_e1, hsep [pp_v_op, pp_e2]]
243 pp_v_op | isLexId (_PK_ (showSDoc pp_v)) = char '`' <> pp_v <> char '`'
245 -- Put it in backquotes if it's not an operator already
246 -- We use (showSDoc pp_v), rather than isSymOcc (getOccName v) simply so
247 -- that we don't need NamedThing in the context of all these funcions.
248 -- Gruesome, but simple.
250 ppr_expr (NegApp e _)
251 = char '-' <+> pprParendExpr e
253 ppr_expr (HsPar e) = parens (ppr_expr e)
255 ppr_expr (SectionL expr op)
257 HsVar v -> pp_infixly v
260 pp_expr = pprParendExpr expr
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])
266 ppr_expr (SectionR op expr)
268 HsVar v -> pp_infixly v
271 pp_expr = pprParendExpr expr
273 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
274 4 ((<>) pp_expr rparen)
276 = parens (sep [ppr v, pp_expr])
278 ppr_expr (HsCase expr matches _)
279 = sep [ sep [ptext SLIT("case"), nest 4 (pprExpr expr), ptext SLIT("of")],
280 nest 2 (pprMatches (True, empty) matches) ]
282 ppr_expr (HsIf e1 e2 e3 _)
283 = sep [hsep [ptext SLIT("if"), nest 2 (pprExpr e1), ptext SLIT("then")],
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")]),
293 ppr_expr (HsLet binds expr)
294 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
295 hang (ptext SLIT("in")) 2 (ppr expr)]
297 ppr_expr (HsWith expr binds)
298 = hsep [ppr expr, ptext SLIT("with"), ppr binds]
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
303 ppr_expr (ExplicitList exprs)
304 = brackets (fsep (punctuate comma (map ppr_expr exprs)))
305 ppr_expr (ExplicitListOut ty exprs)
306 = hcat [ brackets (fsep (punctuate comma (map ppr_expr exprs))),
307 ifNotPprForUser ((<>) space (parens (pprType ty))) ]
309 ppr_expr (ExplicitTuple exprs True)
310 = parens (sep (punctuate comma (map ppr_expr exprs)))
312 ppr_expr (ExplicitTuple exprs False)
313 = ptext SLIT("(#") <> sep (punctuate comma (map ppr_expr exprs)) <> ptext SLIT("#)")
315 ppr_expr (RecordCon con_id rbinds)
316 = pp_rbinds (ppr con_id) rbinds
317 ppr_expr (RecordConOut data_con con rbinds)
318 = pp_rbinds (ppr con) rbinds
320 ppr_expr (RecordUpd aexp rbinds)
321 = pp_rbinds (pprParendExpr aexp) rbinds
322 ppr_expr (RecordUpdOut aexp _ _ rbinds)
323 = pp_rbinds (pprParendExpr aexp) rbinds
325 ppr_expr (ExprWithTySig expr sig)
326 = hang (nest 2 (ppr_expr expr) <+> dcolon)
329 ppr_expr (ArithSeqIn info)
330 = brackets (ppr info)
331 ppr_expr (ArithSeqOut expr info)
332 = brackets (ppr info)
334 ppr_expr EWildPat = char '_'
335 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
336 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
338 ppr_expr (HsCCall fun args _ is_asm result_ty)
340 then ptext SLIT("_casm_ ``") <> ptext fun <> ptext SLIT("''")
341 else ptext SLIT("_ccall_") <+> ptext fun)
342 4 (sep (map pprParendExpr args))
344 ppr_expr (HsSCC lbl expr)
345 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext lbl), pprParendExpr expr ]
347 ppr_expr (TyLam tyvars expr)
348 = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
351 ppr_expr (TyApp expr [ty])
352 = hang (ppr_expr expr) 4 (pprParendType ty)
354 ppr_expr (TyApp expr tys)
355 = hang (ppr_expr expr)
356 4 (brackets (interpp'SP tys))
358 ppr_expr (DictLam dictvars expr)
359 = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
362 ppr_expr (DictApp expr [dname])
363 = hang (ppr_expr expr) 4 (ppr dname)
365 ppr_expr (DictApp expr dnames)
366 = hang (ppr_expr expr)
367 4 (brackets (interpp'SP dnames))
371 Parenthesize unless very simple:
373 pprParendExpr :: (Outputable id, Outputable pat)
374 => HsExpr id pat -> SDoc
378 pp_as_was = pprExpr expr
382 HsLitOut l _ -> ppr l
385 HsIPVar _ -> pp_as_was
386 ExplicitList _ -> pp_as_was
387 ExplicitListOut _ _ -> pp_as_was
388 ExplicitTuple _ _ -> pp_as_was
391 _ -> parens pp_as_was
394 %************************************************************************
396 \subsection{Record binds}
398 %************************************************************************
401 pp_rbinds :: (Outputable id, Outputable pat)
403 -> HsRecordBinds id pat -> SDoc
405 pp_rbinds thing rbinds
407 4 (braces (hsep (punctuate comma (map (pp_rbind) rbinds))))
409 pp_rbind (v, e, pun_flag)
410 = getPprStyle $ \ sty ->
411 if pun_flag && userStyle sty then
414 hsep [ppr v, char '=', ppr e]
417 %************************************************************************
419 \subsection{Do stmts and list comprehensions}
421 %************************************************************************
424 data StmtCtxt -- Context of a Stmt
425 = DoStmt -- Do Statment
426 | ListComp -- List comprehension
427 | CaseAlt -- Guard on a case alternative
428 | PatBindRhs -- Guard on a pattern binding
429 | FunRhs Name -- Guard on a function defn for f
430 | LambdaBody -- Body of a lambda abstraction
433 = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
436 hang (pprExpr expr <+> char '|')
439 ReturnStmt expr = last stmts -- Last stmt should be a ReturnStmt for list comps
449 | LetStmt (HsBinds id pat)
451 | GuardStmt (HsExpr id pat) -- List comps only
454 | ExprStmt (HsExpr id pat) -- Do stmts; and guarded things at the end
457 | ReturnStmt (HsExpr id pat) -- List comps only, at the end
459 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
460 consLetStmt EmptyBinds stmts = stmts
461 consLetStmt binds stmts = LetStmt binds : stmts
465 instance (Outputable id, Outputable pat) =>
466 Outputable (Stmt id pat) where
467 ppr stmt = pprStmt stmt
469 pprStmt (BindStmt pat expr _)
470 = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
471 pprStmt (LetStmt binds)
472 = hsep [ptext SLIT("let"), pprBinds binds]
473 pprStmt (ExprStmt expr _)
475 pprStmt (GuardStmt expr _)
477 pprStmt (ReturnStmt expr)
478 = hsep [ptext SLIT("return"), ppr expr]
481 %************************************************************************
483 \subsection{Enumerations and list comprehensions}
485 %************************************************************************
488 data ArithSeqInfo id pat
489 = From (HsExpr id pat)
490 | FromThen (HsExpr id pat)
492 | FromTo (HsExpr id pat)
494 | FromThenTo (HsExpr id pat)
500 instance (Outputable id, Outputable pat) =>
501 Outputable (ArithSeqInfo id pat) where
502 ppr (From e1) = hcat [ppr e1, pp_dotdot]
503 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
504 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
505 ppr (FromThenTo e1 e2 e3)
506 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
508 pp_dotdot = ptext SLIT(" .. ")