2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[PatSyntax]{Abstract Haskell syntax---patterns}
11 irrefutablePat, irrefutablePats,
12 failureFreePat, isWildPat,
13 patsAreAllCons, isConPat,
14 patsAreAllLits, isLitPat,
15 collectPatBinders, collectOutPatBinders, collectPatsBinders,
16 collectSigTysFromPat, collectSigTysFromPats
19 #include "HsVersions.h"
23 import HsLit ( HsLit, HsOverLit )
24 import HsExpr ( HsExpr )
25 import HsTypes ( HsType )
26 import BasicTypes ( Fixity, Boxity, tupleParens )
30 import Var ( Id, TyVar )
31 import DataCon ( DataCon, dataConTyCon )
32 import Name ( isDataSymOcc, getOccName, NamedThing )
33 import Maybes ( maybeToBool )
35 import TyCon ( maybeTyConSingleCon )
39 Patterns come in distinct before- and after-typechecking flavo(u)rs.
42 = WildPatIn -- wild card
43 | VarPatIn name -- variable
44 | LitPatIn HsLit -- literal
45 | LazyPatIn (InPat name) -- lazy pattern
46 | AsPatIn name -- as pattern
48 | SigPatIn (InPat name)
50 | ConPatIn name -- constructed type
52 | ConOpPatIn (InPat name)
54 Fixity -- c.f. OpApp in HsExpr
57 | NPatIn HsOverLit -- Always positive
58 (Maybe Name) -- Just (Name of 'negate') for negative
59 -- patterns, Nothing otherwise
60 -- (see RnEnv.lookupSyntaxName)
62 | NPlusKPatIn name -- n+k pattern
63 HsOverLit -- It'll always be an HsIntegral
64 Name -- Name of '-' (see RnEnv.lookupSyntaxName)
66 -- We preserve prefix negation and parenthesis for the precedence parser.
68 | ParPatIn (InPat name) -- parenthesised pattern
70 | ListPatIn [InPat name] -- syntactic list
71 -- must have >= 1 elements
72 | PArrPatIn [InPat name] -- syntactic parallel array
73 -- must have >= 1 elements
74 | TuplePatIn [InPat name] Boxity -- tuple (boxed?)
76 | RecPatIn name -- record
77 [(name, InPat name, Bool)] -- True <=> source used punning
80 | TypePatIn (HsType name) -- Type pattern for generic definitions
81 -- e.g f{| a+b |} = ...
82 -- These show up only in class
84 -- and should be a top-level pattern
89 = WildPat Type -- wild card
90 | VarPat id -- variable (type is in the Id)
91 | LazyPat (OutPat id) -- lazy pattern
92 | AsPat id -- as pattern
95 | SigPat (OutPat id) -- Pattern p
96 Type -- Type, t, of the whole pattern
97 (HsExpr id (OutPat id))
99 -- of type t -> typeof(p)
101 | ListPat -- Syntactic list
102 Type -- The type of the elements
104 | PArrPat -- Syntactic parallel array
105 Type -- The type of the elements
108 | TuplePat [OutPat id] -- Tuple
110 -- UnitPat is TuplePat []
113 Type -- the type of the pattern
114 [TyVar] -- Existentially bound type variables
115 [id] -- Ditto dictionaries
118 -- ConOpPats are only used on the input side
120 | RecPat DataCon -- Record constructor
121 Type -- The type of the pattern
122 [TyVar] -- Existentially bound type variables
123 [id] -- Ditto dictionaries
124 [(Id, OutPat id, Bool)] -- True <=> source used punning
126 | LitPat -- Used for *non-overloaded* literal patterns:
127 -- Int#, Char#, Int, Char, String, etc.
129 Type -- Type of pattern
131 | NPat -- Used for literal patterns where there's an equality function to call
132 HsLit -- The literal is retained so that
133 -- the desugarer can readily identify
134 -- equations with identical literal-patterns
135 -- Always HsInteger, HsRat or HsString.
136 -- *Unlike* NPatIn, for negative literals, the
137 -- literal is acutally negative!
138 Type -- Type of pattern, t
139 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
143 Type -- Type of pattern, t
144 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
145 (HsExpr id (OutPat id)) -- Of type t -> t; subtracts k
147 | DictPat -- Used when destructing Dictionaries with an explicit case
148 [id] -- superclass dicts
152 Now name in Inpat is not need to be in NAmedThing to be Outputable.
153 Needed by ../deSugar/Check.lhs
158 instance (Outputable name) => Outputable (InPat name) where
161 pprInPat :: (Outputable name) => InPat name -> SDoc
163 pprInPat (WildPatIn) = char '_'
164 pprInPat (VarPatIn var) = ppr var
165 pprInPat (LitPatIn s) = ppr s
166 pprInPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
167 pprInPat (LazyPatIn pat) = char '~' <> ppr pat
168 pprInPat (AsPatIn name pat) = parens (hcat [ppr name, char '@', ppr pat])
169 pprInPat (ParPatIn pat) = parens (pprInPat pat)
170 pprInPat (ListPatIn pats) = brackets (interpp'SP pats)
171 pprInPat (PArrPatIn pats) = pabrackets (interpp'SP pats)
172 pprInPat (TuplePatIn pats bx) = tupleParens bx (interpp'SP pats)
173 pprInPat (NPlusKPatIn n k _) = parens (hcat [ppr n, char '+', ppr k])
174 pprInPat (NPatIn l _) = ppr l
176 pprInPat (ConPatIn c pats)
178 | otherwise = hsep [ppr c, interppSP pats] -- inner ParPats supply the necessary parens.
180 pprInPat (ConOpPatIn pat1 op fixity pat2)
181 = hsep [ppr pat1, ppr op, ppr pat2] -- ParPats put in parens
183 -- ToDo: use pprSym to print op (but this involves fiddling various
184 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
186 pprInPat (RecPatIn con rpats)
187 = hsep [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
189 pp_rpat (v, _, True) = ppr v
190 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
192 pprInPat (TypePatIn ty) = ptext SLIT("{|") <> ppr ty <> ptext SLIT("|}")
194 -- add parallel array brackets around a document
196 pabrackets :: SDoc -> SDoc
197 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
201 instance (NamedThing id, Outputable id) => Outputable (OutPat id) where
206 pprOutPat (WildPat ty) = char '_'
207 pprOutPat (VarPat var) = ppr var
208 pprOutPat (LazyPat pat) = hcat [char '~', ppr pat]
209 pprOutPat (AsPat name pat)
210 = parens (hcat [ppr name, char '@', ppr pat])
212 pprOutPat (SigPat pat ty _) = ppr pat <+> dcolon <+> ppr ty
214 pprOutPat (ConPat name ty [] [] [])
217 -- Kludge to get infix constructors to come out right
218 -- when ppr'ing desugar warnings.
219 pprOutPat (ConPat name ty tyvars dicts pats)
220 = getPprStyle $ \ sty ->
224 | userStyle sty && isDataSymOcc (getOccName name) ->
225 hsep [ppr p1, ppr name, ppr p2]
226 _ -> hsep [ppr name, interppSP tyvars, interppSP dicts, interppSP pats]
228 pprOutPat (ListPat ty pats) = brackets (interpp'SP pats)
229 pprOutPat (PArrPat ty pats) = pabrackets (interpp'SP pats)
230 pprOutPat (TuplePat pats boxity) = tupleParens boxity (interpp'SP pats)
232 pprOutPat (RecPat con ty tvs dicts rpats)
233 = hsep [ppr con, interppSP tvs, interppSP dicts, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
235 pp_rpat (v, _, True) = ppr v
236 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
238 pprOutPat (LitPat l ty) = ppr l -- ToDo: print more
239 pprOutPat (NPat l ty e) = ppr l -- ToDo: print more
240 pprOutPat (NPlusKPat n k ty e1 e2) -- ToDo: print more
241 = parens (hcat [ppr n, char '+', integer k])
243 pprOutPat (DictPat dicts methods)
244 = parens (sep [ptext SLIT("{-dict-}"),
245 brackets (interpp'SP dicts),
246 brackets (interpp'SP methods)])
250 %************************************************************************
252 %* predicates for checking things about pattern-lists in EquationInfo *
254 %************************************************************************
255 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
257 Unlike in the Wadler chapter, where patterns are either ``variables''
258 or ``constructors,'' here we distinguish between:
261 Patterns that cannot fail to match: variables, wildcards, and lazy
264 These are the irrefutable patterns; the two other categories
265 are refutable patterns.
268 A non-literal constructor pattern (see next category).
270 \item[literal patterns:]
271 At least the numeric ones may be overloaded.
274 A pattern is in {\em exactly one} of the above three categories; `as'
275 patterns are treated specially, of course.
277 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
279 irrefutablePats :: [OutPat id] -> Bool
280 irrefutablePats pat_list = all irrefutablePat pat_list
282 irrefutablePat (AsPat _ pat) = irrefutablePat pat
283 irrefutablePat (WildPat _) = True
284 irrefutablePat (VarPat _) = True
285 irrefutablePat (LazyPat _) = True
286 irrefutablePat (DictPat ds ms) = (length ds + length ms) <= 1
287 irrefutablePat other = False
289 failureFreePat :: OutPat id -> Bool
291 failureFreePat (WildPat _) = True
292 failureFreePat (VarPat _) = True
293 failureFreePat (LazyPat _) = True
294 failureFreePat (AsPat _ pat) = failureFreePat pat
295 failureFreePat (ConPat con tys _ _ pats) = only_con con && all failureFreePat pats
296 failureFreePat (RecPat con _ _ _ fields) = only_con con && and [ failureFreePat pat | (_,pat,_) <- fields ]
297 failureFreePat (ListPat _ _) = False
298 failureFreePat (PArrPat _ _) = False
299 failureFreePat (TuplePat pats _) = all failureFreePat pats
300 failureFreePat (DictPat _ _) = True
301 failureFreePat other_pat = False -- Literals, NPat
303 only_con con = maybeToBool (maybeTyConSingleCon (dataConTyCon con))
307 isWildPat (WildPat _) = True
308 isWildPat other = False
310 patsAreAllCons :: [OutPat id] -> Bool
311 patsAreAllCons pat_list = all isConPat pat_list
313 isConPat (AsPat _ pat) = isConPat pat
314 isConPat (ConPat _ _ _ _ _) = True
315 isConPat (ListPat _ _) = True
316 isConPat (PArrPat _ _) = True
317 isConPat (TuplePat _ _) = True
318 isConPat (RecPat _ _ _ _ _) = True
319 isConPat (DictPat ds ms) = (length ds + length ms) > 1
320 isConPat other = False
322 patsAreAllLits :: [OutPat id] -> Bool
323 patsAreAllLits pat_list = all isLitPat pat_list
325 isLitPat (AsPat _ pat) = isLitPat pat
326 isLitPat (LitPat _ _) = True
327 isLitPat (NPat _ _ _) = True
328 isLitPat (NPlusKPat _ _ _ _ _) = True
329 isLitPat other = False
332 This function @collectPatBinders@ works with the ``collectBinders''
333 functions for @HsBinds@, etc. The order in which the binders are
334 collected is important; see @HsBinds.lhs@.
337 collectPatBinders :: InPat a -> [a]
338 collectPatBinders pat = collect pat []
340 collectOutPatBinders :: OutPat a -> [a]
341 collectOutPatBinders pat = collectOut pat []
343 collectPatsBinders :: [InPat a] -> [a]
344 collectPatsBinders pats = foldr collect [] pats
346 collect WildPatIn bndrs = bndrs
347 collect (VarPatIn var) bndrs = var : bndrs
348 collect (LitPatIn _) bndrs = bndrs
349 collect (SigPatIn pat _) bndrs = collect pat bndrs
350 collect (LazyPatIn pat) bndrs = collect pat bndrs
351 collect (AsPatIn a pat) bndrs = a : collect pat bndrs
352 collect (NPlusKPatIn n _ _) bndrs = n : bndrs
353 collect (NPatIn _ _) bndrs = bndrs
354 collect (ConPatIn c pats) bndrs = foldr collect bndrs pats
355 collect (ConOpPatIn p1 c f p2) bndrs = collect p1 (collect p2 bndrs)
356 collect (ParPatIn pat) bndrs = collect pat bndrs
357 collect (ListPatIn pats) bndrs = foldr collect bndrs pats
358 collect (PArrPatIn pats) bndrs = foldr collect bndrs pats
359 collect (TuplePatIn pats _) bndrs = foldr collect bndrs pats
360 collect (RecPatIn c fields) bndrs = foldr (\ (f,pat,_) bndrs -> collect pat bndrs) bndrs fields
362 collect (TypePatIn ty) bndrs = bndrs
363 -- assume the type variables do not need to be bound
365 -- collect the bounds *value* variables in renamed patterns; type variables
366 -- are *not* collected
368 collectOut (WildPat _) bndrs = bndrs
369 collectOut (VarPat var) bndrs = var : bndrs
370 collectOut (LazyPat pat) bndrs = collectOut pat bndrs
371 collectOut (AsPat a pat) bndrs = a : collectOut pat bndrs
372 collectOut (ListPat _ pats) bndrs = foldr collectOut bndrs pats
373 collectOut (PArrPat _ pats) bndrs = foldr collectOut bndrs pats
374 collectOut (TuplePat pats _) bndrs = foldr collectOut bndrs pats
375 collectOut (ConPat _ _ _ ds pats) bndrs = ds ++ foldr collectOut bndrs pats
376 collectOut (RecPat _ _ _ ds fields) bndrs = ds ++ foldr comb bndrs fields
378 comb (_, pat, _) bndrs = collectOut pat bndrs
379 collectOut (LitPat _ _) bndrs = bndrs
380 collectOut (NPat _ _ _) bndrs = bndrs
381 collectOut (NPlusKPat n _ _ _ _) bndrs = n : bndrs
382 collectOut (DictPat ids1 ids2) bndrs = ids1 ++ ids2 ++ bndrs
386 collectSigTysFromPats :: [InPat name] -> [HsType name]
387 collectSigTysFromPats pats = foldr collect_pat [] pats
389 collectSigTysFromPat :: InPat name -> [HsType name]
390 collectSigTysFromPat pat = collect_pat pat []
392 collect_pat (SigPatIn pat ty) acc = collect_pat pat (ty:acc)
393 collect_pat WildPatIn acc = acc
394 collect_pat (VarPatIn var) acc = acc
395 collect_pat (LitPatIn _) acc = acc
396 collect_pat (LazyPatIn pat) acc = collect_pat pat acc
397 collect_pat (AsPatIn a pat) acc = collect_pat pat acc
398 collect_pat (NPatIn _ _) acc = acc
399 collect_pat (NPlusKPatIn n _ _) acc = acc
400 collect_pat (ConPatIn c pats) acc = foldr collect_pat acc pats
401 collect_pat (ConOpPatIn p1 c f p2) acc = collect_pat p1 (collect_pat p2 acc)
402 collect_pat (ParPatIn pat) acc = collect_pat pat acc
403 collect_pat (ListPatIn pats) acc = foldr collect_pat acc pats
404 collect_pat (PArrPatIn pats) acc = foldr collect_pat acc pats
405 collect_pat (TuplePatIn pats _) acc = foldr collect_pat acc pats
406 collect_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> collect_pat pat acc) acc fields
408 collect_pat (TypePatIn ty) acc = ty:acc