2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
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 ( NamedThing )
23 import PprType ( pprGenType, pprParendGenType, GenType, GenTyVar )
24 import SrcLoc ( SrcLoc )
27 %************************************************************************
29 \subsection{Expressions proper}
31 %************************************************************************
34 data HsExpr flexi id pat
35 = HsVar id -- variable
36 | HsLit HsLit -- literal
37 | HsLitOut HsLit -- TRANSLATION
38 (GenType flexi) -- (with its type)
40 | HsLam (Match flexi id pat) -- lambda
41 | HsApp (HsExpr flexi id pat) -- application
44 -- Operator applications:
45 -- NB Bracketed ops such as (+) come out as Vars.
47 -- NB We need an expr for the operator in an OpApp/Section since
48 -- the typechecker may need to apply the operator to a few types.
50 | OpApp (HsExpr flexi id pat) -- left operand
51 (HsExpr flexi id pat) -- operator
52 Fixity -- Renamer adds fixity; bottom until then
53 (HsExpr flexi id pat) -- right operand
55 -- We preserve prefix negation and parenthesis for the precedence parser.
56 -- They are eventually removed by the type checker.
58 | NegApp (HsExpr flexi id pat) -- negated expr
59 (HsExpr flexi id pat) -- the negate id (in a HsVar)
61 | HsPar (HsExpr flexi id pat) -- parenthesised expr
63 | SectionL (HsExpr flexi id pat) -- operand
64 (HsExpr flexi id pat) -- operator
65 | SectionR (HsExpr flexi id pat) -- operator
66 (HsExpr flexi id pat) -- operand
68 | HsCase (HsExpr flexi id pat)
69 [Match flexi id pat] -- must have at least one Match
72 | HsIf (HsExpr flexi id pat) -- predicate
73 (HsExpr flexi id pat) -- then part
74 (HsExpr flexi id pat) -- else part
77 | HsLet (HsBinds flexi id pat) -- let(rec)
81 [Stmt flexi id pat] -- "do":one or more stmts
84 | HsDoOut DoOrListComp
85 [Stmt flexi id pat] -- "do":one or more stmts
89 (GenType flexi) -- Type of the whole expression
92 | ExplicitList -- syntactic list
94 | ExplicitListOut -- TRANSLATION
95 (GenType flexi) -- Gives type of components of list
98 | ExplicitTuple -- tuple
100 -- NB: Unit is ExplicitTuple []
101 -- for tuples, we can get the types
102 -- direct from the components
104 | HsCon Id -- TRANSLATION; a saturated constructor application
106 [HsExpr flexi id pat]
108 -- Record construction
109 | RecordCon id -- The constructor
110 (HsExpr flexi id pat) -- Always (HsVar id) until type checker,
111 -- but the latter adds its type args too
112 (HsRecordBinds flexi id pat)
115 | RecordUpd (HsExpr flexi id pat)
116 (HsRecordBinds flexi id pat)
118 | RecordUpdOut (HsExpr flexi id pat) -- TRANSLATION
119 (GenType flexi) -- Type of *result* record (may differ from
120 -- type of input record)
121 [id] -- Dicts needed for construction
122 (HsRecordBinds flexi id pat)
124 | ExprWithTySig -- signature binding
125 (HsExpr flexi id pat)
127 | ArithSeqIn -- arithmetic sequence
128 (ArithSeqInfo flexi id pat)
130 (HsExpr flexi id pat) -- (typechecked, of course)
131 (ArithSeqInfo flexi id pat)
133 | CCall FAST_STRING -- call into the C world; string is
134 [HsExpr flexi id pat] -- the C function; exprs are the
135 -- arguments to pass.
136 Bool -- True <=> might cause Haskell
137 -- garbage-collection (must generate
138 -- more paranoid code)
139 Bool -- True <=> it's really a "casm"
140 -- NOTE: this CCall is the *boxed*
141 -- version; the desugarer will convert
142 -- it into the unboxed "ccall#".
143 (GenType flexi) -- The result type; will be *bottom*
144 -- until the typechecker gets ahold of it
146 | HsSCC FAST_STRING -- "set cost centre" (_scc_) annotation
147 (HsExpr flexi id pat) -- expr whose cost is to be measured
150 Everything from here on appears only in typechecker output.
153 | TyLam -- TRANSLATION
155 (HsExpr flexi id pat)
156 | TyApp -- TRANSLATION
157 (HsExpr flexi id pat) -- generated by Spec
160 -- DictLam and DictApp are "inverses"
163 (HsExpr flexi id pat)
165 (HsExpr flexi id pat)
168 type HsRecordBinds flexi id pat
169 = [(id, HsExpr flexi id pat, Bool)]
170 -- True <=> source code used "punning",
171 -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
174 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
175 @ClassDictLam dictvars methods expr@ is, therefore:
177 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
181 instance (NamedThing id, Outputable id, Outputable pat) =>
182 Outputable (HsExpr flexi id pat) where
183 ppr expr = pprExpr expr
187 pprExpr :: (NamedThing id, Outputable id, Outputable pat)
188 => HsExpr flexi id pat -> SDoc
190 pprExpr e = pprDeeper (ppr_expr e)
192 ppr_expr (HsVar v) = ppr v
194 ppr_expr (HsLit lit) = ppr lit
195 ppr_expr (HsLitOut lit _) = ppr lit
197 ppr_expr (HsLam match)
198 = hsep [char '\\', nest 2 (pprMatch True match)]
200 ppr_expr expr@(HsApp e1 e2)
201 = let (fun, args) = collect_args expr [] in
202 (pprExpr fun) <+> (sep (map pprExpr args))
204 collect_args (HsApp fun arg) args = collect_args fun (arg:args)
205 collect_args fun args = (fun, args)
207 ppr_expr (OpApp e1 op fixity e2)
209 HsVar v -> pp_infixly v
212 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
213 pp_e2 = pprParendExpr e2
216 = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
219 = sep [pp_e1, hsep [ppr v, pp_e2]]
221 ppr_expr (NegApp e _)
222 = (<>) (char '-') (pprParendExpr e)
225 = parens (ppr_expr e)
227 ppr_expr (SectionL expr op)
229 HsVar v -> pp_infixly v
232 pp_expr = pprParendExpr expr
234 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
235 4 (hsep [pp_expr, ptext SLIT("x_ )")])
236 pp_infixly v = parens (sep [pp_expr, ppr v])
238 ppr_expr (SectionR op expr)
240 HsVar v -> pp_infixly v
243 pp_expr = pprParendExpr expr
245 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
246 4 ((<>) pp_expr rparen)
248 = parens (sep [ppr v, pp_expr])
250 ppr_expr (HsCase expr matches _)
251 = sep [ sep [ptext SLIT("case"), nest 4 (ppr_expr expr), ptext SLIT("of")],
252 nest 2 (pprMatches (True, empty) matches) ]
254 ppr_expr (HsIf e1 e2 e3 _)
255 = sep [hsep [ptext SLIT("if"), nest 2 (ppr_expr e1), ptext SLIT("then")],
256 nest 4 (ppr_expr e2),
258 nest 4 (ppr_expr e3)]
260 -- special case: let ... in let ...
261 ppr_expr (HsLet binds expr@(HsLet _ _))
262 = sep [hang (ptext SLIT("let")) 2 (hsep [ppr binds, ptext SLIT("in")]),
265 ppr_expr (HsLet binds expr)
266 = sep [hang (ptext SLIT("let")) 2 (ppr binds),
267 hang (ptext SLIT("in")) 2 (ppr expr)]
269 ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts
270 ppr_expr (HsDoOut do_or_list_comp stmts _ _ _ _ _) = pprDo do_or_list_comp stmts
272 ppr_expr (ExplicitList exprs)
273 = brackets (fsep (punctuate comma (map pprExpr exprs)))
274 ppr_expr (ExplicitListOut ty exprs)
275 = hcat [ brackets (fsep (punctuate comma (map pprExpr exprs))),
276 ifNotPprForUser ((<>) space (parens (pprGenType ty))) ]
278 ppr_expr (ExplicitTuple exprs)
279 = parens (sep (punctuate comma (map pprExpr exprs)))
281 ppr_expr (HsCon con_id tys args)
282 = ppr con_id <+> sep (map pprParendGenType tys ++
283 map pprParendExpr args)
285 ppr_expr (RecordCon con_id con rbinds)
286 = pp_rbinds (ppr con) rbinds
288 ppr_expr (RecordUpd aexp rbinds)
289 = pp_rbinds (pprParendExpr aexp) rbinds
290 ppr_expr (RecordUpdOut aexp _ _ rbinds)
291 = pp_rbinds (pprParendExpr aexp) rbinds
293 ppr_expr (ExprWithTySig expr sig)
294 = hang (nest 2 (pprExpr expr) <+> ptext SLIT("::"))
297 ppr_expr (ArithSeqIn info)
298 = brackets (ppr info)
299 ppr_expr (ArithSeqOut expr info)
300 = brackets (ppr info)
302 ppr_expr (CCall fun args _ is_asm result_ty)
304 then ptext SLIT("_casm_ ``") <> ptext fun <> ptext SLIT("''")
305 else ptext SLIT("_ccall_") <+> ptext fun)
306 4 (sep (map pprParendExpr args))
308 ppr_expr (HsSCC label expr)
309 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext label), pprParendExpr expr ]
311 ppr_expr (TyLam tyvars expr)
312 = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
315 ppr_expr (TyApp expr [ty])
316 = hang (pprExpr expr) 4 (pprParendGenType ty)
318 ppr_expr (TyApp expr tys)
319 = hang (pprExpr expr)
320 4 (brackets (interpp'SP tys))
322 ppr_expr (DictLam dictvars expr)
323 = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
326 ppr_expr (DictApp expr [dname])
327 = hang (pprExpr expr) 4 (ppr dname)
329 ppr_expr (DictApp expr dnames)
330 = hang (pprExpr expr)
331 4 (brackets (interpp'SP dnames))
335 Parenthesize unless very simple:
337 pprParendExpr :: (NamedThing id, Outputable id, Outputable pat)
338 => HsExpr flexi id pat -> SDoc
342 pp_as_was = pprExpr expr
346 HsLitOut l _ -> ppr l
349 ExplicitList _ -> pp_as_was
350 ExplicitListOut _ _ -> pp_as_was
351 ExplicitTuple _ -> pp_as_was
354 _ -> parens pp_as_was
357 %************************************************************************
359 \subsection{Record binds}
361 %************************************************************************
364 pp_rbinds :: (NamedThing id, Outputable id, Outputable pat)
366 -> HsRecordBinds flexi id pat -> SDoc
368 pp_rbinds thing rbinds
370 4 (braces (hsep (punctuate comma (map (pp_rbind) rbinds))))
372 pp_rbind (v, e, pun_flag)
373 = getPprStyle $ \ sty ->
374 if pun_flag && userStyle sty then
377 hsep [ppr v, char '=', ppr e]
380 %************************************************************************
382 \subsection{Do stmts and list comprehensions}
384 %************************************************************************
387 data DoOrListComp = DoStmt | ListComp | Guard
390 = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
393 hang (pprExpr expr <+> char '|')
396 ReturnStmt expr = last stmts -- Last stmt should be a ReturnStmt for list comps
401 data Stmt flexi id pat
403 (HsExpr flexi id pat)
406 | LetStmt (HsBinds flexi id pat)
408 | GuardStmt (HsExpr flexi id pat) -- List comps only
411 | ExprStmt (HsExpr flexi id pat) -- Do stmts only
414 | ReturnStmt (HsExpr flexi id pat) -- List comps only, at the end
418 instance (NamedThing id, Outputable id, Outputable pat) =>
419 Outputable (Stmt flexi id pat) where
420 ppr stmt = pprStmt stmt
422 pprStmt (BindStmt pat expr _)
423 = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
424 pprStmt (LetStmt binds)
425 = hsep [ptext SLIT("let"), ppr binds]
426 pprStmt (ExprStmt expr _)
428 pprStmt (GuardStmt expr _)
430 pprStmt (ReturnStmt expr)
431 = hsep [ptext SLIT("return"), ppr expr]
434 %************************************************************************
436 \subsection{Enumerations and list comprehensions}
438 %************************************************************************
441 data ArithSeqInfo flexi id pat
442 = From (HsExpr flexi id pat)
443 | FromThen (HsExpr flexi id pat)
444 (HsExpr flexi id pat)
445 | FromTo (HsExpr flexi id pat)
446 (HsExpr flexi id pat)
447 | FromThenTo (HsExpr flexi id pat)
448 (HsExpr flexi id pat)
449 (HsExpr flexi id pat)
453 instance (NamedThing id, Outputable id, Outputable pat) =>
454 Outputable (ArithSeqInfo flexi id pat) where
455 ppr (From e1) = hcat [ppr e1, pp_dotdot]
456 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
457 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
458 ppr (FromThenTo e1 e2 e3)
459 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
461 pp_dotdot = ptext SLIT(" .. ")