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, isLexSym )
22 import PprType ( pprType, pprParendType )
24 import Var ( TyVar, Id )
25 import DataCon ( DataCon )
26 import CStrings ( CLabelString, pprCLabelString )
27 import BasicTypes ( Boxity, tupleParens )
28 import SrcLoc ( SrcLoc )
31 %************************************************************************
33 \subsection{Expressions proper}
35 %************************************************************************
39 = HsVar id -- variable
40 | HsIPVar id -- implicit parameter
41 | HsLit HsLit -- literal
42 | HsLitOut HsLit -- TRANSLATION
43 Type -- (with its type)
45 | HsLam (Match id pat) -- lambda
46 | HsApp (HsExpr id pat) -- application
49 -- Operator applications:
50 -- NB Bracketed ops such as (+) come out as Vars.
52 -- NB We need an expr for the operator in an OpApp/Section since
53 -- the typechecker may need to apply the operator to a few types.
55 | OpApp (HsExpr id pat) -- left operand
56 (HsExpr id pat) -- operator
57 Fixity -- Renamer adds fixity; bottom until then
58 (HsExpr id pat) -- right operand
60 -- We preserve prefix negation and parenthesis for the precedence parser.
61 -- They are eventually removed by the type checker.
63 | NegApp (HsExpr id pat) -- negated expr
64 (HsExpr id pat) -- the negate id (in a HsVar)
66 | HsPar (HsExpr id pat) -- parenthesised expr
68 | SectionL (HsExpr id pat) -- operand
69 (HsExpr id pat) -- operator
70 | SectionR (HsExpr id pat) -- operator
71 (HsExpr id pat) -- operand
73 | HsCase (HsExpr id pat)
77 | HsIf (HsExpr id pat) -- predicate
78 (HsExpr id pat) -- then part
79 (HsExpr id pat) -- else part
82 | HsLet (HsBinds id pat) -- let(rec)
85 | HsWith (HsExpr id pat) -- implicit parameter binding
89 [Stmt id pat] -- "do":one or more stmts
93 [Stmt id pat] -- "do":one or more stmts
97 Type -- Type of the whole expression
100 | ExplicitList -- syntactic list
102 | ExplicitListOut -- TRANSLATION
103 Type -- Gives type of components of list
106 | ExplicitTuple -- tuple
108 -- NB: Unit is ExplicitTuple []
109 -- for tuples, we can get the types
110 -- direct from the components
114 -- Record construction
115 | RecordCon id -- The constructor
116 (HsRecordBinds id pat)
118 | RecordConOut DataCon
119 (HsExpr id pat) -- Data con Id applied to type args
120 (HsRecordBinds id pat)
124 | RecordUpd (HsExpr id pat)
125 (HsRecordBinds id pat)
127 | RecordUpdOut (HsExpr id pat) -- TRANSLATION
128 Type -- Type of *result* record (may differ from
129 -- type of input record)
130 [id] -- Dicts needed for construction
131 (HsRecordBinds id pat)
133 | ExprWithTySig -- signature binding
136 | ArithSeqIn -- arithmetic sequence
137 (ArithSeqInfo id pat)
139 (HsExpr id pat) -- (typechecked, of course)
140 (ArithSeqInfo id pat)
142 | HsCCall CLabelString -- call into the C world; string is
143 [HsExpr id pat] -- the C function; exprs are the
144 -- arguments to pass.
145 Bool -- True <=> might cause Haskell
146 -- garbage-collection (must generate
147 -- more paranoid code)
148 Bool -- True <=> it's really a "casm"
149 -- NOTE: this CCall is the *boxed*
150 -- version; the desugarer will convert
151 -- it into the unboxed "ccall#".
152 Type -- The result type; will be *bottom*
153 -- until the typechecker gets ahold of it
155 | HsSCC FAST_STRING -- "set cost centre" (_scc_) annotation
156 (HsExpr id pat) -- expr whose cost is to be measured
159 These constructors only appear temporarily in the parser.
160 The renamer translates them into the Right Thing.
163 | EWildPat -- wildcard
165 | EAsPat id -- as pattern
168 | ELazyPat (HsExpr id pat) -- ~ pattern
171 Everything from here on appears only in typechecker output.
174 | TyLam -- TRANSLATION
177 | TyApp -- TRANSLATION
178 (HsExpr id pat) -- generated by Spec
181 -- DictLam and DictApp are "inverses"
189 type HsRecordBinds id pat
190 = [(id, HsExpr id pat, Bool)]
191 -- True <=> source code used "punning",
192 -- i.e. {op1, op2} rather than {op1=e1, op2=e2}
195 A @Dictionary@, unless of length 0 or 1, becomes a tuple. A
196 @ClassDictLam dictvars methods expr@ is, therefore:
198 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
202 instance (Outputable id, Outputable pat) =>
203 Outputable (HsExpr id pat) where
204 ppr expr = pprExpr expr
208 pprExpr :: (Outputable id, Outputable pat)
209 => HsExpr id pat -> SDoc
211 pprExpr e = pprDeeper (ppr_expr e)
212 pprBinds b = pprDeeper (ppr b)
215 -- Put it in parens if it's an operator
216 | isOperator v = parens (ppr v)
219 ppr_expr (HsIPVar v) = {- char '?' <> -} ppr v
221 ppr_expr (HsLit lit) = ppr lit
222 ppr_expr (HsLitOut lit _) = ppr lit
224 ppr_expr (HsLam match)
225 = hsep [char '\\', nest 2 (pprMatch (True,empty) match)]
227 ppr_expr expr@(HsApp e1 e2)
228 = let (fun, args) = collect_args expr [] in
229 (ppr_expr fun) <+> (sep (map ppr_expr args))
231 collect_args (HsApp fun arg) args = collect_args fun (arg:args)
232 collect_args fun args = (fun, args)
234 ppr_expr (OpApp e1 op fixity e2)
236 HsVar v -> pp_infixly v
239 pp_e1 = pprParendExpr e1 -- Add parens to make precedence clear
240 pp_e2 = pprParendExpr e2
243 = hang (pprExpr op) 4 (sep [pp_e1, pp_e2])
246 = sep [pp_e1, hsep [pp_v_op, pp_e2]]
248 pp_v_op | isOperator v = ppr v
249 | otherwise = char '`' <> ppr v <> char '`'
250 -- Put it in backquotes if it's not an operator already
252 ppr_expr (NegApp e _)
253 = char '-' <+> pprParendExpr e
255 ppr_expr (HsPar e) = parens (ppr_expr e)
257 ppr_expr (SectionL expr op)
259 HsVar v -> pp_infixly v
262 pp_expr = pprParendExpr expr
264 pp_prefixly = hang (hsep [text " \\ x_ ->", ppr op])
265 4 (hsep [pp_expr, ptext SLIT("x_ )")])
266 pp_infixly v = parens (sep [pp_expr, ppr v])
268 ppr_expr (SectionR op expr)
270 HsVar v -> pp_infixly v
273 pp_expr = pprParendExpr expr
275 pp_prefixly = hang (hsep [text "( \\ x_ ->", ppr op, ptext SLIT("x_")])
276 4 ((<>) pp_expr rparen)
278 = parens (sep [ppr v, pp_expr])
280 ppr_expr (HsCase expr matches _)
281 = sep [ sep [ptext SLIT("case"), nest 4 (pprExpr expr), ptext SLIT("of")],
282 nest 2 (pprMatches (True, empty) matches) ]
284 ppr_expr (HsIf e1 e2 e3 _)
285 = sep [hsep [ptext SLIT("if"), nest 2 (pprExpr e1), ptext SLIT("then")],
290 -- special case: let ... in let ...
291 ppr_expr (HsLet binds expr@(HsLet _ _))
292 = sep [hang (ptext SLIT("let")) 2 (hsep [pprBinds binds, ptext SLIT("in")]),
295 ppr_expr (HsLet binds expr)
296 = sep [hang (ptext SLIT("let")) 2 (pprBinds binds),
297 hang (ptext SLIT("in")) 2 (ppr expr)]
299 ppr_expr (HsWith expr binds)
300 = hsep [ppr expr, ptext SLIT("with"), ppr binds]
302 ppr_expr (HsDo do_or_list_comp stmts _) = pprDo do_or_list_comp stmts
303 ppr_expr (HsDoOut do_or_list_comp stmts _ _ _ _ _) = pprDo do_or_list_comp stmts
305 ppr_expr (ExplicitList exprs)
306 = brackets (fsep (punctuate comma (map ppr_expr exprs)))
307 ppr_expr (ExplicitListOut ty exprs)
308 = hcat [ brackets (fsep (punctuate comma (map ppr_expr exprs))),
309 ifNotPprForUser ((<>) space (parens (pprType ty))) ]
311 ppr_expr (ExplicitTuple exprs boxity)
312 = tupleParens boxity (sep (punctuate comma (map ppr_expr exprs)))
314 ppr_expr (RecordCon con_id rbinds)
315 = pp_rbinds (ppr con_id) rbinds
316 ppr_expr (RecordConOut data_con con rbinds)
317 = pp_rbinds (ppr con) rbinds
319 ppr_expr (RecordUpd aexp rbinds)
320 = pp_rbinds (pprParendExpr aexp) rbinds
321 ppr_expr (RecordUpdOut aexp _ _ rbinds)
322 = pp_rbinds (pprParendExpr aexp) rbinds
324 ppr_expr (ExprWithTySig expr sig)
325 = hang (nest 2 (ppr_expr expr) <+> dcolon)
328 ppr_expr (ArithSeqIn info)
329 = brackets (ppr info)
330 ppr_expr (ArithSeqOut expr info)
331 = brackets (ppr info)
333 ppr_expr EWildPat = char '_'
334 ppr_expr (ELazyPat e) = char '~' <> pprParendExpr e
335 ppr_expr (EAsPat v e) = ppr v <> char '@' <> pprParendExpr e
337 ppr_expr (HsCCall fun args _ is_asm result_ty)
339 then ptext SLIT("_casm_ ``") <> pprCLabelString fun <> ptext SLIT("''")
340 else ptext SLIT("_ccall_") <+> pprCLabelString fun)
341 4 (sep (map pprParendExpr args))
343 ppr_expr (HsSCC lbl expr)
344 = sep [ ptext SLIT("_scc_") <+> doubleQuotes (ptext lbl), pprParendExpr expr ]
346 ppr_expr (TyLam tyvars expr)
347 = hang (hsep [ptext SLIT("/\\"), interppSP tyvars, ptext SLIT("->")])
350 ppr_expr (TyApp expr [ty])
351 = hang (ppr_expr expr) 4 (pprParendType ty)
353 ppr_expr (TyApp expr tys)
354 = hang (ppr_expr expr)
355 4 (brackets (interpp'SP tys))
357 ppr_expr (DictLam dictvars expr)
358 = hang (hsep [ptext SLIT("\\{-dict-}"), interppSP dictvars, ptext SLIT("->")])
361 ppr_expr (DictApp expr [dname])
362 = hang (ppr_expr expr) 4 (ppr dname)
364 ppr_expr (DictApp expr dnames)
365 = hang (ppr_expr expr)
366 4 (brackets (interpp'SP dnames))
370 Parenthesize unless very simple:
372 pprParendExpr :: (Outputable id, Outputable pat)
373 => HsExpr id pat -> SDoc
377 pp_as_was = pprExpr expr
381 HsLitOut l _ -> ppr l
384 HsIPVar _ -> pp_as_was
385 ExplicitList _ -> pp_as_was
386 ExplicitListOut _ _ -> pp_as_was
387 ExplicitTuple _ _ -> pp_as_was
390 _ -> parens pp_as_was
394 isOperator :: Outputable a => a -> Bool
395 isOperator v = isLexSym (_PK_ (showSDoc (ppr v)))
396 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
397 -- that we don't need NamedThing in the context of all these functions.
398 -- Gruesome, but simple.
401 %************************************************************************
403 \subsection{Record binds}
405 %************************************************************************
408 pp_rbinds :: (Outputable id, Outputable pat)
410 -> HsRecordBinds id pat -> SDoc
412 pp_rbinds thing rbinds
414 4 (braces (sep (punctuate comma (map (pp_rbind) rbinds))))
416 pp_rbind (v, e, pun_flag)
417 = getPprStyle $ \ sty ->
418 if pun_flag && userStyle sty then
421 hsep [ppr v, char '=', ppr e]
424 %************************************************************************
426 \subsection{Do stmts and list comprehensions}
428 %************************************************************************
431 data StmtCtxt -- Context of a Stmt
432 = DoStmt -- Do Statment
433 | ListComp -- List comprehension
434 | CaseAlt -- Guard on a case alternative
435 | PatBindRhs -- Guard on a pattern binding
436 | FunRhs Name -- Guard on a function defn for f
437 | LambdaBody -- Body of a lambda abstraction
440 = hang (ptext SLIT("do")) 2 (vcat (map ppr stmts))
443 hang (pprExpr expr <+> char '|')
446 ReturnStmt expr = last stmts -- Last stmt should be a ReturnStmt for list comps
456 | LetStmt (HsBinds id pat)
458 | GuardStmt (HsExpr id pat) -- List comps only
461 | ExprStmt (HsExpr id pat) -- Do stmts; and guarded things at the end
464 | ReturnStmt (HsExpr id pat) -- List comps only, at the end
466 consLetStmt :: HsBinds id pat -> [Stmt id pat] -> [Stmt id pat]
467 consLetStmt EmptyBinds stmts = stmts
468 consLetStmt binds stmts = LetStmt binds : stmts
472 instance (Outputable id, Outputable pat) =>
473 Outputable (Stmt id pat) where
474 ppr stmt = pprStmt stmt
476 pprStmt (BindStmt pat expr _)
477 = hsep [ppr pat, ptext SLIT("<-"), ppr expr]
478 pprStmt (LetStmt binds)
479 = hsep [ptext SLIT("let"), pprBinds binds]
480 pprStmt (ExprStmt expr _)
482 pprStmt (GuardStmt expr _)
484 pprStmt (ReturnStmt expr)
485 = hsep [ptext SLIT("return"), ppr expr]
488 %************************************************************************
490 \subsection{Enumerations and list comprehensions}
492 %************************************************************************
495 data ArithSeqInfo id pat
496 = From (HsExpr id pat)
497 | FromThen (HsExpr id pat)
499 | FromTo (HsExpr id pat)
501 | FromThenTo (HsExpr id pat)
507 instance (Outputable id, Outputable pat) =>
508 Outputable (ArithSeqInfo id pat) where
509 ppr (From e1) = hcat [ppr e1, pp_dotdot]
510 ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot]
511 ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3]
512 ppr (FromThenTo e1 e2 e3)
513 = hcat [ppr e1, comma, space, ppr e2, pp_dotdot, ppr e3]
515 pp_dotdot = ptext SLIT(" .. ")