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 ( pprType, pprParendType )
24 import Type ( GenType )
25 import TyVar ( GenTyVar )
26 import SrcLoc ( SrcLoc )
29 %************************************************************************
31 \subsection{Expressions proper}
33 %************************************************************************
36 data HsExpr flexi id pat
37 = HsVar id -- variable
38 | HsLit HsLit -- literal
39 | HsLitOut HsLit -- TRANSLATION
40 (GenType flexi) -- (with its type)
42 | HsLam (Match flexi id pat) -- lambda
43 | HsApp (HsExpr flexi 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 flexi id pat) -- left operand
53 (HsExpr flexi id pat) -- operator
54 Fixity -- Renamer adds fixity; bottom until then
55 (HsExpr flexi 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 flexi id pat) -- negated expr
61 (HsExpr flexi id pat) -- the negate id (in a HsVar)
63 | HsPar (HsExpr flexi id pat) -- parenthesised expr
65 | SectionL (HsExpr flexi id pat) -- operand
66 (HsExpr flexi id pat) -- operator
67 | SectionR (HsExpr flexi id pat) -- operator
68 (HsExpr flexi id pat) -- operand
70 | HsCase (HsExpr flexi id pat)
71 [Match flexi id pat] -- must have at least one Match
74 | HsIf (HsExpr flexi id pat) -- predicate
75 (HsExpr flexi id pat) -- then part
76 (HsExpr flexi id pat) -- else part
79 | HsLet (HsBinds flexi id pat) -- let(rec)
83 [Stmt flexi id pat] -- "do":one or more stmts
86 | HsDoOut DoOrListComp
87 [Stmt flexi id pat] -- "do":one or more stmts
91 (GenType flexi) -- Type of the whole expression
94 | ExplicitList -- syntactic list
96 | ExplicitListOut -- TRANSLATION
97 (GenType flexi) -- Gives type of components of list
100 | ExplicitTuple -- tuple
101 [HsExpr flexi id pat]
102 -- NB: Unit is ExplicitTuple []
103 -- for tuples, we can get the types
104 -- direct from the components
106 | HsCon Id -- TRANSLATION; a saturated constructor application
108 [HsExpr flexi id pat]
110 -- Record construction
111 | RecordCon id -- The constructor
112 (HsExpr flexi id pat) -- Always (HsVar id) until type checker,
113 -- but the latter adds its type args too
114 (HsRecordBinds flexi id pat)
117 | RecordUpd (HsExpr flexi id pat)
118 (HsRecordBinds flexi id pat)
120 | RecordUpdOut (HsExpr flexi id pat) -- TRANSLATION
121 (GenType flexi) -- Type of *result* record (may differ from
122 -- type of input record)
123 [id] -- Dicts needed for construction
124 (HsRecordBinds flexi id pat)
126 | ExprWithTySig -- signature binding
127 (HsExpr flexi id pat)
129 | ArithSeqIn -- arithmetic sequence
130 (ArithSeqInfo flexi id pat)
132 (HsExpr flexi id pat) -- (typechecked, of course)
133 (ArithSeqInfo flexi id pat)
135 | CCall FAST_STRING -- call into the C world; string is
136 [HsExpr flexi id pat] -- the C function; exprs are the
137 -- arguments to pass.
138 Bool -- True <=> might cause Haskell
139 -- garbage-collection (must generate
140 -- more paranoid code)
141 Bool -- True <=> it's really a "casm"
142 -- NOTE: this CCall is the *boxed*
143 -- version; the desugarer will convert
144 -- it into the unboxed "ccall#".
145 (GenType flexi) -- The result type; will be *bottom*
146 -- until the typechecker gets ahold of it
148 | HsSCC FAST_STRING -- "set cost centre" (_scc_) annotation
149 (HsExpr flexi id pat) -- expr whose cost is to be measured
152 Everything from here on appears only in typechecker output.
155 | TyLam -- TRANSLATION
157 (HsExpr flexi id pat)
158 | TyApp -- TRANSLATION
159 (HsExpr flexi id pat) -- generated by Spec
162 -- DictLam and DictApp are "inverses"
165 (HsExpr flexi id pat)
167 (HsExpr flexi id pat)
170 type HsRecordBinds flexi id pat
171 = [(id, HsExpr flexi id pat, Bool)]
172 -- True <=> source code used "punning",
173 -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
176 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
177 @ClassDictLam dictvars methods expr@ is, therefore:
179 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
183 instance (NamedThing id, Outputable id, Outputable pat) =>
184 Outputable (HsExpr flexi id pat) where
185 ppr expr = pprExpr expr
189 pprExpr :: (NamedThing id, Outputable id, Outputable pat)
190 => HsExpr flexi id pat -> SDoc
192 pprExpr e = pprDeeper (ppr_expr e)
194 ppr_expr (HsVar v) = ppr v
196 ppr_expr (HsLit lit) = ppr lit
197 ppr_expr (HsLitOut lit _) = ppr lit
199 ppr_expr (HsLam match)
200 = hsep [char '\\', nest 2 (pprMatch True match)]
202 ppr_expr expr@(HsApp e1 e2)
203 = let (fun, args) = collect_args expr [] in
204 (ppr_expr fun) <+> (sep (map ppr_expr args))
206 collect_args (HsApp fun arg) args = collect_args fun (arg:args)
207 collect_args fun args = (fun, args)
209 ppr_expr (OpApp e1 op fixity e2)
211 HsVar v -> pp_infixly v
214 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
215 pp_e2 = pprParendExpr e2
218 = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
221 = sep [pp_e1, hsep [ppr v, pp_e2]]
223 ppr_expr (NegApp e _)
224 = (<>) (char '-') (pprParendExpr e)
227 = parens (ppr_expr e)
229 ppr_expr (SectionL expr op)
231 HsVar v -> pp_infixly v
234 pp_expr = pprParendExpr expr
236 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
237 4 (hsep [pp_expr, ptext SLIT("x_ )")])
238 pp_infixly v = parens (sep [pp_expr, ppr v])
240 ppr_expr (SectionR op expr)
242 HsVar v -> pp_infixly v
245 pp_expr = pprParendExpr expr
247 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
248 4 ((<>) pp_expr rparen)
250 = parens (sep [ppr v, pp_expr])
252 ppr_expr (HsCase expr matches _)
253 = sep [ sep [ptext SLIT("case"), nest 4 (pprExpr expr), ptext SLIT("of")],
254 nest 2 (pprMatches (True, empty) matches) ]
256 ppr_expr (HsIf e1 e2 e3 _)
257 = sep [hsep [ptext SLIT("if"), nest 2 (pprExpr e1), ptext SLIT("then")],
262 -- special case: let ... in let ...
263 ppr_expr (HsLet binds expr@(HsLet _ _))
264 = sep [hang (ptext SLIT("let")) 2 (hsep [ppr binds, ptext SLIT("in")]),
267 ppr_expr (HsLet binds expr)
268 = sep [hang (ptext SLIT("let")) 2 (ppr binds),
269 hang (ptext SLIT("in")) 2 (ppr expr)]
271 ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts
272 ppr_expr (HsDoOut do_or_list_comp stmts _ _ _ _ _) = pprDo do_or_list_comp stmts
274 ppr_expr (ExplicitList exprs)
275 = brackets (fsep (punctuate comma (map ppr_expr exprs)))
276 ppr_expr (ExplicitListOut ty exprs)
277 = hcat [ brackets (fsep (punctuate comma (map ppr_expr exprs))),
278 ifNotPprForUser ((<>) space (parens (pprType ty))) ]
280 ppr_expr (ExplicitTuple exprs)
281 = parens (sep (punctuate comma (map ppr_expr exprs)))
283 ppr_expr (HsCon con_id tys args)
284 = ppr con_id <+> sep (map pprParendType tys ++
285 map pprParendExpr args)
287 ppr_expr (RecordCon con_id con rbinds)
288 = pp_rbinds (ppr con) rbinds
290 ppr_expr (RecordUpd aexp rbinds)
291 = pp_rbinds (pprParendExpr aexp) rbinds
292 ppr_expr (RecordUpdOut aexp _ _ rbinds)
293 = pp_rbinds (pprParendExpr aexp) rbinds
295 ppr_expr (ExprWithTySig expr sig)
296 = hang (nest 2 (ppr_expr expr) <+> ptext SLIT("::"))
299 ppr_expr (ArithSeqIn info)
300 = brackets (ppr info)
301 ppr_expr (ArithSeqOut expr info)
302 = brackets (ppr info)
304 ppr_expr (CCall fun args _ is_asm result_ty)
306 then ptext SLIT("_casm_ ``") <> ptext fun <> ptext SLIT("''")
307 else ptext SLIT("_ccall_") <+> ptext fun)
308 4 (sep (map pprParendExpr args))
310 ppr_expr (HsSCC label expr)
311 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext label), pprParendExpr expr ]
313 ppr_expr (TyLam tyvars expr)
314 = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
317 ppr_expr (TyApp expr [ty])
318 = hang (ppr_expr expr) 4 (pprParendType ty)
320 ppr_expr (TyApp expr tys)
321 = hang (ppr_expr expr)
322 4 (brackets (interpp'SP tys))
324 ppr_expr (DictLam dictvars expr)
325 = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
328 ppr_expr (DictApp expr [dname])
329 = hang (ppr_expr expr) 4 (ppr dname)
331 ppr_expr (DictApp expr dnames)
332 = hang (ppr_expr expr)
333 4 (brackets (interpp'SP dnames))
337 Parenthesize unless very simple:
339 pprParendExpr :: (NamedThing id, Outputable id, Outputable pat)
340 => HsExpr flexi id pat -> SDoc
344 pp_as_was = pprExpr expr
348 HsLitOut l _ -> ppr l
351 ExplicitList _ -> pp_as_was
352 ExplicitListOut _ _ -> pp_as_was
353 ExplicitTuple _ -> pp_as_was
356 _ -> parens pp_as_was
359 %************************************************************************
361 \subsection{Record binds}
363 %************************************************************************
366 pp_rbinds :: (NamedThing id, Outputable id, Outputable pat)
368 -> HsRecordBinds flexi id pat -> SDoc
370 pp_rbinds thing rbinds
372 4 (braces (hsep (punctuate comma (map (pp_rbind) rbinds))))
374 pp_rbind (v, e, pun_flag)
375 = getPprStyle $ \ sty ->
376 if pun_flag && userStyle sty then
379 hsep [ppr v, char '=', ppr e]
382 %************************************************************************
384 \subsection{Do stmts and list comprehensions}
386 %************************************************************************
389 data DoOrListComp = DoStmt | ListComp | Guard
392 = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
395 hang (pprExpr expr <+> char '|')
398 ReturnStmt expr = last stmts -- Last stmt should be a ReturnStmt for list comps
403 data Stmt flexi id pat
405 (HsExpr flexi id pat)
408 | LetStmt (HsBinds flexi id pat)
410 | GuardStmt (HsExpr flexi id pat) -- List comps only
413 | ExprStmt (HsExpr flexi id pat) -- Do stmts only
416 | ReturnStmt (HsExpr flexi id pat) -- List comps only, at the end
420 instance (NamedThing id, Outputable id, Outputable pat) =>
421 Outputable (Stmt flexi id pat) where
422 ppr stmt = pprStmt stmt
424 pprStmt (BindStmt pat expr _)
425 = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
426 pprStmt (LetStmt binds)
427 = hsep [ptext SLIT("let"), ppr binds]
428 pprStmt (ExprStmt expr _)
430 pprStmt (GuardStmt expr _)
432 pprStmt (ReturnStmt expr)
433 = hsep [ptext SLIT("return"), ppr expr]
436 %************************************************************************
438 \subsection{Enumerations and list comprehensions}
440 %************************************************************************
443 data ArithSeqInfo flexi id pat
444 = From (HsExpr flexi id pat)
445 | FromThen (HsExpr flexi id pat)
446 (HsExpr flexi id pat)
447 | FromTo (HsExpr flexi id pat)
448 (HsExpr flexi id pat)
449 | FromThenTo (HsExpr flexi id pat)
450 (HsExpr flexi id pat)
451 (HsExpr flexi id pat)
455 instance (NamedThing id, Outputable id, Outputable pat) =>
456 Outputable (ArithSeqInfo flexi id pat) where
457 ppr (From e1) = hcat [ppr e1, pp_dotdot]
458 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
459 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
460 ppr (FromThenTo e1 e2 e3)
461 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
463 pp_dotdot = ptext SLIT(" .. ")