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, collectPatsBinders,
19 #include "HsVersions.h"
22 import HsLit ( HsLit, HsOverLit )
23 import HsExpr ( HsExpr )
24 import HsTypes ( HsType )
25 import BasicTypes ( Fixity, Boxity, tupleParens )
28 import Var ( Id, TyVar )
29 import DataCon ( DataCon, dataConTyCon )
30 import Name ( Name, isDataSymOcc, getOccName, NamedThing )
31 import Maybes ( maybeToBool )
33 import TyCon ( maybeTyConSingleCon )
37 Patterns come in distinct before- and after-typechecking flavo(u)rs.
40 = WildPatIn -- wild card
41 | VarPatIn name -- variable
42 | LitPatIn HsLit -- literal
43 | LazyPatIn (InPat name) -- lazy pattern
44 | AsPatIn name -- as pattern
46 | SigPatIn (InPat name)
48 | ConPatIn name -- constructed type
50 | ConOpPatIn (InPat name)
52 Fixity -- c.f. OpApp in HsExpr
55 | NPatIn (HsOverLit name)
57 | NPlusKPatIn name -- n+k pattern
58 (HsOverLit name) -- It'll always be an HsIntegral, but
59 -- we need those names to support -fuser-numerics
60 name -- Name for "-"; this supports -fuser-numerics
61 -- We don't do the same for >= because that isn't
62 -- affected by -fuser-numerics
64 -- We preserve prefix negation and parenthesis for the precedence parser.
66 | ParPatIn (InPat name) -- parenthesised pattern
68 | ListPatIn [InPat name] -- syntactic list
69 -- must have >= 1 elements
70 | TuplePatIn [InPat name] Boxity -- tuple (boxed?)
72 | RecPatIn name -- record
73 [(name, InPat name, Bool)] -- True <=> source used punning
76 | TypePatIn (HsType name) -- Type pattern for generic definitions
77 -- e.g f{| a+b |} = ...
78 -- These show up only in class
80 -- and should be a top-level pattern
85 = WildPat Type -- wild card
86 | VarPat id -- variable (type is in the Id)
87 | LazyPat (OutPat id) -- lazy pattern
88 | AsPat id -- as pattern
91 | ListPat -- Syntactic list
92 Type -- The type of the elements
95 | TuplePat [OutPat id] -- Tuple
97 -- UnitPat is TuplePat []
100 Type -- the type of the pattern
101 [TyVar] -- Existentially bound type variables
102 [id] -- Ditto dictionaries
105 -- ConOpPats are only used on the input side
107 | RecPat DataCon -- Record constructor
108 Type -- The type of the pattern
109 [TyVar] -- Existentially bound type variables
110 [id] -- Ditto dictionaries
111 [(Id, OutPat id, Bool)] -- True <=> source used punning
113 | LitPat -- Used for *non-overloaded* literal patterns:
114 -- Int#, Char#, Int, Char, String, etc.
116 Type -- Type of pattern
118 | NPat -- Used for *overloaded* literal patterns
119 HsLit -- The literal is retained so that
120 -- the desugarer can readily identify
121 -- equations with identical literal-patterns
122 -- Always HsInt, HsRat or HsString.
123 Type -- Type of pattern, t
124 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
128 Type -- Type of pattern, t
129 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
130 (HsExpr id (OutPat id)) -- Of type t -> t; subtracts k
132 | DictPat -- Used when destructing Dictionaries with an explicit case
133 [id] -- superclass dicts
137 Now name in Inpat is not need to be in NAmedThing to be Outputable.
138 Needed by ../deSugar/Check.lhs
143 instance (Outputable name) => Outputable (InPat name) where
146 pprInPat :: (Outputable name) => InPat name -> SDoc
148 pprInPat (WildPatIn) = char '_'
149 pprInPat (VarPatIn var) = ppr var
150 pprInPat (LitPatIn s) = ppr s
151 pprInPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
152 pprInPat (LazyPatIn pat) = char '~' <> ppr pat
153 pprInPat (AsPatIn name pat) = parens (hcat [ppr name, char '@', ppr pat])
154 pprInPat (ParPatIn pat) = parens (pprInPat pat)
155 pprInPat (ListPatIn pats) = brackets (interpp'SP pats)
156 pprInPat (TuplePatIn pats bx) = tupleParens bx (interpp'SP pats)
157 pprInPat (NPlusKPatIn n k _) = parens (hcat [ppr n, char '+', ppr k])
158 pprInPat (NPatIn l) = ppr l
160 pprInPat (ConPatIn c pats)
162 | otherwise = hsep [ppr c, interppSP pats] -- inner ParPats supply the necessary parens.
164 pprInPat (ConOpPatIn pat1 op fixity pat2)
165 = hsep [ppr pat1, ppr op, ppr pat2] -- ParPats put in parens
167 -- ToDo: use pprSym to print op (but this involves fiddling various
168 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
170 pprInPat (RecPatIn con rpats)
171 = hsep [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
173 pp_rpat (v, _, True) = ppr v
174 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
176 pprInPat (TypePatIn ty) = ptext SLIT("{|") <> ppr ty <> ptext SLIT("|}")
180 instance (NamedThing id, Outputable id) => Outputable (OutPat id) where
185 pprOutPat (WildPat ty) = char '_'
186 pprOutPat (VarPat var) = ppr var
187 pprOutPat (LazyPat pat) = hcat [char '~', ppr pat]
188 pprOutPat (AsPat name pat)
189 = parens (hcat [ppr name, char '@', ppr pat])
191 pprOutPat (ConPat name ty [] [] [])
194 -- Kludge to get infix constructors to come out right
195 -- when ppr'ing desugar warnings.
196 pprOutPat (ConPat name ty tyvars dicts pats)
197 = getPprStyle $ \ sty ->
201 | userStyle sty && isDataSymOcc (getOccName name) ->
202 hsep [ppr p1, ppr name, ppr p2]
203 _ -> hsep [ppr name, interppSP tyvars, interppSP dicts, interppSP pats]
205 pprOutPat (ListPat ty pats) = brackets (interpp'SP pats)
206 pprOutPat (TuplePat pats boxity) = tupleParens boxity (interpp'SP pats)
208 pprOutPat (RecPat con ty tvs dicts rpats)
209 = hsep [ppr con, interppSP tvs, interppSP dicts, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
211 pp_rpat (v, _, True) = ppr v
212 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
214 pprOutPat (LitPat l ty) = ppr l -- ToDo: print more
215 pprOutPat (NPat l ty e) = ppr l -- ToDo: print more
216 pprOutPat (NPlusKPat n k ty e1 e2) -- ToDo: print more
217 = parens (hcat [ppr n, char '+', integer k])
219 pprOutPat (DictPat dicts methods)
220 = parens (sep [ptext SLIT("{-dict-}"),
221 brackets (interpp'SP dicts),
222 brackets (interpp'SP methods)])
226 %************************************************************************
228 %* predicates for checking things about pattern-lists in EquationInfo *
230 %************************************************************************
231 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
233 Unlike in the Wadler chapter, where patterns are either ``variables''
234 or ``constructors,'' here we distinguish between:
237 Patterns that cannot fail to match: variables, wildcards, and lazy
240 These are the irrefutable patterns; the two other categories
241 are refutable patterns.
244 A non-literal constructor pattern (see next category).
246 \item[literal patterns:]
247 At least the numeric ones may be overloaded.
250 A pattern is in {\em exactly one} of the above three categories; `as'
251 patterns are treated specially, of course.
253 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
255 irrefutablePats :: [OutPat id] -> Bool
256 irrefutablePats pat_list = all irrefutablePat pat_list
258 irrefutablePat (AsPat _ pat) = irrefutablePat pat
259 irrefutablePat (WildPat _) = True
260 irrefutablePat (VarPat _) = True
261 irrefutablePat (LazyPat _) = True
262 irrefutablePat (DictPat ds ms) = (length ds + length ms) <= 1
263 irrefutablePat other = False
265 failureFreePat :: OutPat id -> Bool
267 failureFreePat (WildPat _) = True
268 failureFreePat (VarPat _) = True
269 failureFreePat (LazyPat _) = True
270 failureFreePat (AsPat _ pat) = failureFreePat pat
271 failureFreePat (ConPat con tys _ _ pats) = only_con con && all failureFreePat pats
272 failureFreePat (RecPat con _ _ _ fields) = only_con con && and [ failureFreePat pat | (_,pat,_) <- fields ]
273 failureFreePat (ListPat _ _) = False
274 failureFreePat (TuplePat pats _) = all failureFreePat pats
275 failureFreePat (DictPat _ _) = True
276 failureFreePat other_pat = False -- Literals, NPat
278 only_con con = maybeToBool (maybeTyConSingleCon (dataConTyCon con))
282 isWildPat (WildPat _) = True
283 isWildPat other = False
285 patsAreAllCons :: [OutPat id] -> Bool
286 patsAreAllCons pat_list = all isConPat pat_list
288 isConPat (AsPat _ pat) = isConPat pat
289 isConPat (ConPat _ _ _ _ _) = True
290 isConPat (ListPat _ _) = True
291 isConPat (TuplePat _ _) = True
292 isConPat (RecPat _ _ _ _ _) = True
293 isConPat (DictPat ds ms) = (length ds + length ms) > 1
294 isConPat other = False
296 patsAreAllLits :: [OutPat id] -> Bool
297 patsAreAllLits pat_list = all isLitPat pat_list
299 isLitPat (AsPat _ pat) = isLitPat pat
300 isLitPat (LitPat _ _) = True
301 isLitPat (NPat _ _ _) = True
302 isLitPat (NPlusKPat _ _ _ _ _) = True
303 isLitPat other = False
306 This function @collectPatBinders@ works with the ``collectBinders''
307 functions for @HsBinds@, etc. The order in which the binders are
308 collected is important; see @HsBinds.lhs@.
311 collectPatBinders :: InPat a -> [a]
312 collectPatBinders pat = collect pat []
314 collectPatsBinders :: [InPat a] -> [a]
315 collectPatsBinders pats = foldr collect [] pats
317 collect WildPatIn bndrs = bndrs
318 collect (VarPatIn var) bndrs = var : bndrs
319 collect (LitPatIn _) bndrs = bndrs
320 collect (SigPatIn pat _) bndrs = collect pat bndrs
321 collect (LazyPatIn pat) bndrs = collect pat bndrs
322 collect (AsPatIn a pat) bndrs = a : collect pat bndrs
323 collect (NPlusKPatIn n _ _) bndrs = n : bndrs
324 collect (NPatIn _) bndrs = bndrs
325 collect (ConPatIn c pats) bndrs = foldr collect bndrs pats
326 collect (ConOpPatIn p1 c f p2) bndrs = collect p1 (collect p2 bndrs)
327 collect (ParPatIn pat) bndrs = collect pat bndrs
328 collect (ListPatIn pats) bndrs = foldr collect bndrs pats
329 collect (TuplePatIn pats _) bndrs = foldr collect bndrs pats
330 collect (RecPatIn c fields) bndrs = foldr (\ (f,pat,_) bndrs -> collect pat bndrs) bndrs fields
332 collect (TypePatIn ty) bndrs = bndrs
333 -- assume the type variables do not need to be bound
337 collectSigTysFromPats :: [InPat name] -> [HsType name]
338 collectSigTysFromPats pats = foldr collect_pat [] pats
340 collect_pat (SigPatIn pat ty) acc = collect_pat pat (ty:acc)
341 collect_pat WildPatIn acc = acc
342 collect_pat (VarPatIn var) acc = acc
343 collect_pat (LitPatIn _) acc = acc
344 collect_pat (LazyPatIn pat) acc = collect_pat pat acc
345 collect_pat (AsPatIn a pat) acc = collect_pat pat acc
346 collect_pat (NPatIn _) acc = acc
347 collect_pat (NPlusKPatIn n _ _) acc = acc
348 collect_pat (ConPatIn c pats) acc = foldr collect_pat acc pats
349 collect_pat (ConOpPatIn p1 c f p2) acc = collect_pat p1 (collect_pat p2 acc)
350 collect_pat (ParPatIn pat) acc = collect_pat pat acc
351 collect_pat (ListPatIn pats) acc = foldr collect_pat acc pats
352 collect_pat (TuplePatIn pats _) acc = foldr collect_pat acc pats
353 collect_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> collect_pat pat acc) acc fields
355 collect_pat (TypePatIn ty) acc = ty:acc