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 CStrings ( CLabelString, pprCLabelString )
27 import SrcLoc ( SrcLoc )
30 %************************************************************************
32 \subsection{Expressions proper}
34 %************************************************************************
38 = HsVar id -- variable
39 | HsIPVar id -- implicit parameter
40 | HsLit HsLit -- literal
41 | HsLitOut HsLit -- TRANSLATION
42 Type -- (with its type)
44 | HsLam (Match id pat) -- lambda
45 | HsApp (HsExpr id pat) -- application
48 -- Operator applications:
49 -- NB Bracketed ops such as (+) come out as Vars.
51 -- NB We need an expr for the operator in an OpApp/Section since
52 -- the typechecker may need to apply the operator to a few types.
54 | OpApp (HsExpr id pat) -- left operand
55 (HsExpr id pat) -- operator
56 Fixity -- Renamer adds fixity; bottom until then
57 (HsExpr id pat) -- right operand
59 -- We preserve prefix negation and parenthesis for the precedence parser.
60 -- They are eventually removed by the type checker.
62 | NegApp (HsExpr id pat) -- negated expr
63 (HsExpr id pat) -- the negate id (in a HsVar)
65 | HsPar (HsExpr id pat) -- parenthesised expr
67 | SectionL (HsExpr id pat) -- operand
68 (HsExpr id pat) -- operator
69 | SectionR (HsExpr id pat) -- operator
70 (HsExpr id pat) -- operand
72 | HsCase (HsExpr id pat)
76 | HsIf (HsExpr id pat) -- predicate
77 (HsExpr id pat) -- then part
78 (HsExpr id pat) -- else part
81 | HsLet (HsBinds id pat) -- let(rec)
84 | HsWith (HsExpr id pat) -- implicit parameter binding
88 [Stmt id pat] -- "do":one or more stmts
92 [Stmt id pat] -- "do":one or more stmts
96 Type -- Type of the whole expression
99 | ExplicitList -- syntactic list
101 | ExplicitListOut -- TRANSLATION
102 Type -- Gives type of components of list
105 | ExplicitTuple -- tuple
107 -- NB: Unit is ExplicitTuple []
108 -- for tuples, we can get the types
109 -- direct from the components
113 -- Record construction
114 | RecordCon id -- The constructor
115 (HsRecordBinds id pat)
117 | RecordConOut DataCon
118 (HsExpr id pat) -- Data con Id applied to type args
119 (HsRecordBinds id pat)
123 | RecordUpd (HsExpr id pat)
124 (HsRecordBinds id pat)
126 | RecordUpdOut (HsExpr id pat) -- TRANSLATION
127 Type -- Type of *result* record (may differ from
128 -- type of input record)
129 [id] -- Dicts needed for construction
130 (HsRecordBinds id pat)
132 | ExprWithTySig -- signature binding
135 | ArithSeqIn -- arithmetic sequence
136 (ArithSeqInfo id pat)
138 (HsExpr id pat) -- (typechecked, of course)
139 (ArithSeqInfo id pat)
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 Bool -- 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 Type -- The result type; will be *bottom*
152 -- until the typechecker gets ahold of it
154 | HsSCC FAST_STRING -- "set cost centre" (_scc_) annotation
155 (HsExpr id pat) -- expr whose cost is to be measured
158 These constructors only appear temporarily in the parser.
159 The renamer translates them into the Right Thing.
162 | EWildPat -- wildcard
164 | EAsPat id -- as pattern
167 | ELazyPat (HsExpr id pat) -- ~ pattern
170 Everything from here on appears only in typechecker output.
173 | TyLam -- TRANSLATION
176 | TyApp -- TRANSLATION
177 (HsExpr id pat) -- generated by Spec
180 -- DictLam and DictApp are "inverses"
188 type HsRecordBinds id pat
189 = [(id, HsExpr id pat, Bool)]
190 -- True <=> source code used "punning",
191 -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
194 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
195 @ClassDictLam dictvars methods expr@ is, therefore:
197 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
201 instance (Outputable id, Outputable pat) =>
202 Outputable (HsExpr id pat) where
203 ppr expr = pprExpr expr
207 pprExpr :: (Outputable id, Outputable pat)
208 => HsExpr id pat -> SDoc
210 pprExpr e = pprDeeper (ppr_expr e)
211 pprBinds b = pprDeeper (ppr b)
213 ppr_expr (HsVar v) = ppr v
214 ppr_expr (HsIPVar v) = {- char '?' <> -} ppr v
216 ppr_expr (HsLit lit) = ppr lit
217 ppr_expr (HsLitOut lit _) = ppr lit
219 ppr_expr (HsLam match)
220 = hsep [char '\\', nest 2 (pprMatch (True,empty) match)]
222 ppr_expr expr@(HsApp e1 e2)
223 = let (fun, args) = collect_args expr [] in
224 (ppr_expr fun) <+> (sep (map ppr_expr args))
226 collect_args (HsApp fun arg) args = collect_args fun (arg:args)
227 collect_args fun args = (fun, args)
229 ppr_expr (OpApp e1 op fixity e2)
231 HsVar v -> pp_infixly v
234 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
235 pp_e2 = pprParendExpr e2
238 = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
241 = sep [pp_e1, hsep [pp_v_op, pp_e2]]
244 pp_v_op | isLexId (_PK_ (showSDoc pp_v)) = char '`' <> pp_v <> char '`'
246 -- Put it in backquotes if it's not an operator already
247 -- We use (showSDoc pp_v), rather than isSymOcc (getOccName v) simply so
248 -- that we don't need NamedThing in the context of all these funcions.
249 -- Gruesome, but simple.
251 ppr_expr (NegApp e _)
252 = char '-' <+> pprParendExpr e
254 ppr_expr (HsPar e) = parens (ppr_expr e)
256 ppr_expr (SectionL expr op)
258 HsVar v -> pp_infixly v
261 pp_expr = pprParendExpr expr
263 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
264 4 (hsep [pp_expr, ptext SLIT("x_ )")])
265 pp_infixly v = parens (sep [pp_expr, ppr v])
267 ppr_expr (SectionR op expr)
269 HsVar v -> pp_infixly v
272 pp_expr = pprParendExpr expr
274 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
275 4 ((<>) pp_expr rparen)
277 = parens (sep [ppr v, pp_expr])
279 ppr_expr (HsCase expr matches _)
280 = sep [ sep [ptext SLIT("case"), nest 4 (pprExpr expr), ptext SLIT("of")],
281 nest 2 (pprMatches (True, empty) matches) ]
283 ppr_expr (HsIf e1 e2 e3 _)
284 = sep [hsep [ptext SLIT("if"), nest 2 (pprExpr e1), ptext SLIT("then")],
289 -- special case: let ... in let ...
290 ppr_expr (HsLet binds expr@(HsLet _ _))
291 = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
294 ppr_expr (HsLet binds expr)
295 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
296 hang (ptext SLIT("in")) 2 (ppr expr)]
298 ppr_expr (HsWith expr binds)
299 = hsep [ppr expr, ptext SLIT("with"), ppr binds]
301 ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts
302 ppr_expr (HsDoOut do_or_list_comp stmts _ _ _ _ _) = pprDo do_or_list_comp stmts
304 ppr_expr (ExplicitList exprs)
305 = brackets (fsep (punctuate comma (map ppr_expr exprs)))
306 ppr_expr (ExplicitListOut ty exprs)
307 = hcat [ brackets (fsep (punctuate comma (map ppr_expr exprs))),
308 ifNotPprForUser ((<>) space (parens (pprType ty))) ]
310 ppr_expr (ExplicitTuple exprs True)
311 = parens (sep (punctuate comma (map ppr_expr exprs)))
313 ppr_expr (ExplicitTuple exprs False)
314 = ptext SLIT("(#") <> sep (punctuate comma (map ppr_expr exprs)) <> ptext SLIT("#)")
316 ppr_expr (RecordCon con_id rbinds)
317 = pp_rbinds (ppr con_id) rbinds
318 ppr_expr (RecordConOut data_con con rbinds)
319 = pp_rbinds (ppr con) rbinds
321 ppr_expr (RecordUpd aexp rbinds)
322 = pp_rbinds (pprParendExpr aexp) rbinds
323 ppr_expr (RecordUpdOut aexp _ _ rbinds)
324 = pp_rbinds (pprParendExpr aexp) rbinds
326 ppr_expr (ExprWithTySig expr sig)
327 = hang (nest 2 (ppr_expr expr) <+> dcolon)
330 ppr_expr (ArithSeqIn info)
331 = brackets (ppr info)
332 ppr_expr (ArithSeqOut expr info)
333 = brackets (ppr info)
335 ppr_expr EWildPat = char '_'
336 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
337 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
339 ppr_expr (HsCCall fun args _ is_asm result_ty)
341 then ptext SLIT("_casm_ ``") <> pprCLabelString fun <> ptext SLIT("''")
342 else ptext SLIT("_ccall_") <+> pprCLabelString fun)
343 4 (sep (map pprParendExpr args))
345 ppr_expr (HsSCC lbl expr)
346 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext lbl), pprParendExpr expr ]
348 ppr_expr (TyLam tyvars expr)
349 = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
352 ppr_expr (TyApp expr [ty])
353 = hang (ppr_expr expr) 4 (pprParendType ty)
355 ppr_expr (TyApp expr tys)
356 = hang (ppr_expr expr)
357 4 (brackets (interpp'SP tys))
359 ppr_expr (DictLam dictvars expr)
360 = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
363 ppr_expr (DictApp expr [dname])
364 = hang (ppr_expr expr) 4 (ppr dname)
366 ppr_expr (DictApp expr dnames)
367 = hang (ppr_expr expr)
368 4 (brackets (interpp'SP dnames))
372 Parenthesize unless very simple:
374 pprParendExpr :: (Outputable id, Outputable pat)
375 => HsExpr id pat -> SDoc
379 pp_as_was = pprExpr expr
383 HsLitOut l _ -> ppr l
386 HsIPVar _ -> pp_as_was
387 ExplicitList _ -> pp_as_was
388 ExplicitListOut _ _ -> pp_as_was
389 ExplicitTuple _ _ -> pp_as_was
392 _ -> parens pp_as_was
395 %************************************************************************
397 \subsection{Record binds}
399 %************************************************************************
402 pp_rbinds :: (Outputable id, Outputable pat)
404 -> HsRecordBinds id pat -> SDoc
406 pp_rbinds thing rbinds
408 4 (braces (hsep (punctuate comma (map (pp_rbind) rbinds))))
410 pp_rbind (v, e, pun_flag)
411 = getPprStyle $ \ sty ->
412 if pun_flag && userStyle sty then
415 hsep [ppr v, char '=', ppr e]
418 %************************************************************************
420 \subsection{Do stmts and list comprehensions}
422 %************************************************************************
425 data StmtCtxt -- Context of a Stmt
426 = DoStmt -- Do Statment
427 | ListComp -- List comprehension
428 | CaseAlt -- Guard on a case alternative
429 | PatBindRhs -- Guard on a pattern binding
430 | FunRhs Name -- Guard on a function defn for f
431 | LambdaBody -- Body of a lambda abstraction
434 = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
437 hang (pprExpr expr <+> char '|')
440 ReturnStmt expr = last stmts -- Last stmt should be a ReturnStmt for list comps
450 | LetStmt (HsBinds id pat)
452 | GuardStmt (HsExpr id pat) -- List comps only
455 | ExprStmt (HsExpr id pat) -- Do stmts; and guarded things at the end
458 | ReturnStmt (HsExpr id pat) -- List comps only, at the end
460 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
461 consLetStmt EmptyBinds stmts = stmts
462 consLetStmt binds stmts = LetStmt binds : stmts
466 instance (Outputable id, Outputable pat) =>
467 Outputable (Stmt id pat) where
468 ppr stmt = pprStmt stmt
470 pprStmt (BindStmt pat expr _)
471 = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
472 pprStmt (LetStmt binds)
473 = hsep [ptext SLIT("let"), pprBinds binds]
474 pprStmt (ExprStmt expr _)
476 pprStmt (GuardStmt expr _)
478 pprStmt (ReturnStmt expr)
479 = hsep [ptext SLIT("return"), ppr expr]
482 %************************************************************************
484 \subsection{Enumerations and list comprehensions}
486 %************************************************************************
489 data ArithSeqInfo id pat
490 = From (HsExpr id pat)
491 | FromThen (HsExpr id pat)
493 | FromTo (HsExpr id pat)
495 | FromThenTo (HsExpr id pat)
501 instance (Outputable id, Outputable pat) =>
502 Outputable (ArithSeqInfo id pat) where
503 ppr (From e1) = hcat [ppr e1, pp_dotdot]
504 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
505 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
506 ppr (FromThenTo e1 e2 e3)
507 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
509 pp_dotdot = ptext SLIT(" .. ")