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 | HsLit HsLit -- literal
39 | HsLitOut HsLit -- TRANSLATION
40 Type -- (with its type)
42 | HsLam (Match id pat) -- lambda
43 | HsApp (HsExpr id pat) -- application
46 -- Operator applications:
47 -- NB Bracketed ops such as (+) come out as Vars.
49 -- NB We need an expr for the operator in an OpApp/Section since
50 -- the typechecker may need to apply the operator to a few types.
52 | OpApp (HsExpr id pat) -- left operand
53 (HsExpr id pat) -- operator
54 Fixity -- Renamer adds fixity; bottom until then
55 (HsExpr id pat) -- right operand
57 -- We preserve prefix negation and parenthesis for the precedence parser.
58 -- They are eventually removed by the type checker.
60 | NegApp (HsExpr id pat) -- negated expr
61 (HsExpr id pat) -- the negate id (in a HsVar)
63 | HsPar (HsExpr id pat) -- parenthesised expr
65 | SectionL (HsExpr id pat) -- operand
66 (HsExpr id pat) -- operator
67 | SectionR (HsExpr id pat) -- operator
68 (HsExpr id pat) -- operand
70 | HsCase (HsExpr id pat)
74 | HsIf (HsExpr id pat) -- predicate
75 (HsExpr id pat) -- then part
76 (HsExpr id pat) -- else part
79 | HsLet (HsBinds id pat) -- let(rec)
83 [Stmt id pat] -- "do":one or more stmts
87 [Stmt id pat] -- "do":one or more stmts
91 Type -- Type of the whole expression
94 | ExplicitList -- syntactic list
96 | ExplicitListOut -- TRANSLATION
97 Type -- Gives type of components of list
100 | ExplicitTuple -- tuple
102 -- NB: Unit is ExplicitTuple []
103 -- for tuples, we can get the types
104 -- direct from the components
107 | HsCon DataCon -- TRANSLATION; a saturated constructor application
111 -- Record construction
112 | RecordCon id -- The constructor
113 (HsRecordBinds id pat)
115 | RecordConOut DataCon
116 (HsExpr id pat) -- Data con Id applied to type args
117 (HsRecordBinds id pat)
121 | RecordUpd (HsExpr id pat)
122 (HsRecordBinds id pat)
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)
130 | ExprWithTySig -- signature binding
133 | ArithSeqIn -- arithmetic sequence
134 (ArithSeqInfo id pat)
136 (HsExpr id pat) -- (typechecked, of course)
137 (ArithSeqInfo id pat)
139 | CCall FAST_STRING -- call into the C world; string is
140 [HsExpr id pat] -- the C function; exprs are the
141 -- arguments to pass.
142 Bool -- 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
152 | HsSCC FAST_STRING -- "set cost centre" (_scc_) annotation
153 (HsExpr id pat) -- expr whose cost is to be measured
156 These constructors only appear temporarily in the parser.
159 | EWildPat -- wildcard
161 | EAsPat id -- as pattern
164 | ELazyPat (HsExpr id pat) -- ~ pattern
167 Everything from here on appears only in typechecker output.
170 | TyLam -- TRANSLATION
173 | TyApp -- TRANSLATION
174 (HsExpr id pat) -- generated by Spec
177 -- DictLam and DictApp are "inverses"
185 type HsRecordBinds id pat
186 = [(id, HsExpr id pat, Bool)]
187 -- True <=> source code used "punning",
188 -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
191 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
192 @ClassDictLam dictvars methods expr@ is, therefore:
194 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
198 instance (Outputable id, Outputable pat) =>
199 Outputable (HsExpr id pat) where
200 ppr expr = pprExpr expr
204 pprExpr :: (Outputable id, Outputable pat)
205 => HsExpr id pat -> SDoc
207 pprExpr e = pprDeeper (ppr_expr e)
208 pprBinds b = pprDeeper (ppr b)
210 ppr_expr (HsVar v) = ppr v
212 ppr_expr (HsLit lit) = ppr lit
213 ppr_expr (HsLitOut lit _) = ppr lit
215 ppr_expr (HsLam match)
216 = hsep [char '\\', nest 2 (pprMatch (True,empty) match)]
218 ppr_expr expr@(HsApp e1 e2)
219 = let (fun, args) = collect_args expr [] in
220 (ppr_expr fun) <+> (sep (map ppr_expr args))
222 collect_args (HsApp fun arg) args = collect_args fun (arg:args)
223 collect_args fun args = (fun, args)
225 ppr_expr (OpApp e1 op fixity e2)
227 HsVar v -> pp_infixly v
230 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
231 pp_e2 = pprParendExpr e2
234 = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
237 = sep [pp_e1, hsep [pp_v_op, pp_e2]]
240 pp_v_op | isLexId (_PK_ (showSDoc pp_v)) = char '`' <> pp_v <> char '`'
242 -- Put it in backquotes if it's not an operator already
243 -- We use (showSDoc pp_v), rather than isSymOcc (getOccName v) simply so
244 -- that we don't need NamedThing in the context of all these funcions.
245 -- Gruesome, but simple.
247 ppr_expr (NegApp e _)
248 = char '-' <+> pprParendExpr e
250 ppr_expr (HsPar e) = parens (ppr_expr e)
252 ppr_expr (SectionL expr op)
254 HsVar v -> pp_infixly v
257 pp_expr = pprParendExpr expr
259 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
260 4 (hsep [pp_expr, ptext SLIT("x_ )")])
261 pp_infixly v = parens (sep [pp_expr, ppr v])
263 ppr_expr (SectionR op expr)
265 HsVar v -> pp_infixly v
268 pp_expr = pprParendExpr expr
270 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
271 4 ((<>) pp_expr rparen)
273 = parens (sep [ppr v, pp_expr])
275 ppr_expr (HsCase expr matches _)
276 = sep [ sep [ptext SLIT("case"), nest 4 (pprExpr expr), ptext SLIT("of")],
277 nest 2 (pprMatches (True, empty) matches) ]
279 ppr_expr (HsIf e1 e2 e3 _)
280 = sep [hsep [ptext SLIT("if"), nest 2 (pprExpr e1), ptext SLIT("then")],
285 -- special case: let ... in let ...
286 ppr_expr (HsLet binds expr@(HsLet _ _))
287 = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
290 ppr_expr (HsLet binds expr)
291 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
292 hang (ptext SLIT("in")) 2 (ppr expr)]
294 ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts
295 ppr_expr (HsDoOut do_or_list_comp stmts _ _ _ _ _) = pprDo do_or_list_comp stmts
297 ppr_expr (ExplicitList exprs)
298 = brackets (fsep (punctuate comma (map ppr_expr exprs)))
299 ppr_expr (ExplicitListOut ty exprs)
300 = hcat [ brackets (fsep (punctuate comma (map ppr_expr exprs))),
301 ifNotPprForUser ((<>) space (parens (pprType ty))) ]
303 ppr_expr (ExplicitTuple exprs True)
304 = parens (sep (punctuate comma (map ppr_expr exprs)))
306 ppr_expr (ExplicitTuple exprs False)
307 = ptext SLIT("(#") <> sep (punctuate comma (map ppr_expr exprs)) <> ptext SLIT("#)")
309 ppr_expr (HsCon con_id tys args)
310 = ppr con_id <+> sep (map pprParendType tys ++
311 map pprParendExpr args)
313 ppr_expr (RecordCon con_id rbinds)
314 = pp_rbinds (ppr con_id) rbinds
315 ppr_expr (RecordConOut data_con con rbinds)
316 = pp_rbinds (ppr con) rbinds
318 ppr_expr (RecordUpd aexp rbinds)
319 = pp_rbinds (pprParendExpr aexp) rbinds
320 ppr_expr (RecordUpdOut aexp _ _ rbinds)
321 = pp_rbinds (pprParendExpr aexp) rbinds
323 ppr_expr (ExprWithTySig expr sig)
324 = hang (nest 2 (ppr_expr expr) <+> dcolon)
327 ppr_expr (ArithSeqIn info)
328 = brackets (ppr info)
329 ppr_expr (ArithSeqOut expr info)
330 = brackets (ppr info)
332 ppr_expr (CCall fun args _ is_asm result_ty)
334 then ptext SLIT("_casm_ ``") <> ptext fun <> ptext SLIT("''")
335 else ptext SLIT("_ccall_") <+> ptext fun)
336 4 (sep (map pprParendExpr args))
338 ppr_expr (HsSCC label expr)
339 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext label), pprParendExpr expr ]
341 ppr_expr (TyLam tyvars expr)
342 = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
345 ppr_expr (TyApp expr [ty])
346 = hang (ppr_expr expr) 4 (pprParendType ty)
348 ppr_expr (TyApp expr tys)
349 = hang (ppr_expr expr)
350 4 (brackets (interpp'SP tys))
352 ppr_expr (DictLam dictvars expr)
353 = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
356 ppr_expr (DictApp expr [dname])
357 = hang (ppr_expr expr) 4 (ppr dname)
359 ppr_expr (DictApp expr dnames)
360 = hang (ppr_expr expr)
361 4 (brackets (interpp'SP dnames))
365 Parenthesize unless very simple:
367 pprParendExpr :: (Outputable id, Outputable pat)
368 => HsExpr id pat -> SDoc
372 pp_as_was = pprExpr expr
376 HsLitOut l _ -> ppr l
379 ExplicitList _ -> pp_as_was
380 ExplicitListOut _ _ -> pp_as_was
381 ExplicitTuple _ _ -> pp_as_was
384 _ -> parens pp_as_was
387 %************************************************************************
389 \subsection{Record binds}
391 %************************************************************************
394 pp_rbinds :: (Outputable id, Outputable pat)
396 -> HsRecordBinds id pat -> SDoc
398 pp_rbinds thing rbinds
400 4 (braces (hsep (punctuate comma (map (pp_rbind) rbinds))))
402 pp_rbind (v, e, pun_flag)
403 = getPprStyle $ \ sty ->
404 if pun_flag && userStyle sty then
407 hsep [ppr v, char '=', ppr e]
410 %************************************************************************
412 \subsection{Do stmts and list comprehensions}
414 %************************************************************************
417 data StmtCtxt -- Context of a Stmt
418 = DoStmt -- Do Statment
419 | ListComp -- List comprehension
420 | CaseAlt -- Guard on a case alternative
421 | PatBindRhs -- Guard on a pattern binding
422 | FunRhs Name -- Guard on a function defn for f
423 | LambdaBody -- Body of a lambda abstraction
426 = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
429 hang (pprExpr expr <+> char '|')
432 ReturnStmt expr = last stmts -- Last stmt should be a ReturnStmt for list comps
442 | LetStmt (HsBinds id pat)
444 | GuardStmt (HsExpr id pat) -- List comps only
447 | ExprStmt (HsExpr id pat) -- Do stmts; and guarded things at the end
450 | ReturnStmt (HsExpr id pat) -- List comps only, at the end
454 instance (Outputable id, Outputable pat) =>
455 Outputable (Stmt id pat) where
456 ppr stmt = pprStmt stmt
458 pprStmt (BindStmt pat expr _)
459 = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
460 pprStmt (LetStmt binds)
461 = hsep [ptext SLIT("let"), pprBinds binds]
462 pprStmt (ExprStmt expr _)
464 pprStmt (GuardStmt expr _)
466 pprStmt (ReturnStmt expr)
467 = hsep [ptext SLIT("return"), ppr expr]
470 %************************************************************************
472 \subsection{Enumerations and list comprehensions}
474 %************************************************************************
477 data ArithSeqInfo id pat
478 = From (HsExpr id pat)
479 | FromThen (HsExpr id pat)
481 | FromTo (HsExpr id pat)
483 | FromThenTo (HsExpr id pat)
489 instance (Outputable id, Outputable pat) =>
490 Outputable (ArithSeqInfo id pat) where
491 ppr (From e1) = hcat [ppr e1, pp_dotdot]
492 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
493 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
494 ppr (FromThenTo e1 e2 e3)
495 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
497 pp_dotdot = ptext SLIT(" .. ")