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,
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
59 | NPlusKPatIn name -- n+k pattern
60 HsOverLit -- It'll always be an HsIntegral
61 Name -- Name of '-' (see RnEnv.lookupSyntaxName)
63 -- We preserve prefix negation and parenthesis for the precedence parser.
65 | ParPatIn (InPat name) -- parenthesised pattern
67 | ListPatIn [InPat name] -- syntactic list
68 -- must have >= 1 elements
69 | TuplePatIn [InPat name] Boxity -- tuple (boxed?)
71 | RecPatIn name -- record
72 [(name, InPat name, Bool)] -- True <=> source used punning
75 | TypePatIn (HsType name) -- Type pattern for generic definitions
76 -- e.g f{| a+b |} = ...
77 -- These show up only in class
79 -- and should be a top-level pattern
84 = WildPat Type -- wild card
85 | VarPat id -- variable (type is in the Id)
86 | LazyPat (OutPat id) -- lazy pattern
87 | AsPat id -- as pattern
90 | SigPat (OutPat id) -- Pattern p
91 Type -- Type, t, of the whole pattern
92 (HsExpr id (OutPat id))
94 -- of type t -> typeof(p)
96 | ListPat -- Syntactic list
97 Type -- The type of the elements
100 | TuplePat [OutPat id] -- Tuple
102 -- UnitPat is TuplePat []
105 Type -- the type of the pattern
106 [TyVar] -- Existentially bound type variables
107 [id] -- Ditto dictionaries
110 -- ConOpPats are only used on the input side
112 | RecPat DataCon -- Record constructor
113 Type -- The type of the pattern
114 [TyVar] -- Existentially bound type variables
115 [id] -- Ditto dictionaries
116 [(Id, OutPat id, Bool)] -- True <=> source used punning
118 | LitPat -- Used for *non-overloaded* literal patterns:
119 -- Int#, Char#, Int, Char, String, etc.
121 Type -- Type of pattern
123 | NPat -- Used for literal patterns where there's an equality function to call
124 HsLit -- The literal is retained so that
125 -- the desugarer can readily identify
126 -- equations with identical literal-patterns
127 -- Always HsInteger, HsRat or HsString.
128 Type -- Type of pattern, t
129 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
133 Type -- Type of pattern, t
134 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
135 (HsExpr id (OutPat id)) -- Of type t -> t; subtracts k
137 | DictPat -- Used when destructing Dictionaries with an explicit case
138 [id] -- superclass dicts
142 Now name in Inpat is not need to be in NAmedThing to be Outputable.
143 Needed by ../deSugar/Check.lhs
148 instance (Outputable name) => Outputable (InPat name) where
151 pprInPat :: (Outputable name) => InPat name -> SDoc
153 pprInPat (WildPatIn) = char '_'
154 pprInPat (VarPatIn var) = ppr var
155 pprInPat (LitPatIn s) = ppr s
156 pprInPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
157 pprInPat (LazyPatIn pat) = char '~' <> ppr pat
158 pprInPat (AsPatIn name pat) = parens (hcat [ppr name, char '@', ppr pat])
159 pprInPat (ParPatIn pat) = parens (pprInPat pat)
160 pprInPat (ListPatIn pats) = brackets (interpp'SP pats)
161 pprInPat (TuplePatIn pats bx) = tupleParens bx (interpp'SP pats)
162 pprInPat (NPlusKPatIn n k _) = parens (hcat [ppr n, char '+', ppr k])
163 pprInPat (NPatIn l) = ppr l
165 pprInPat (ConPatIn c pats)
167 | otherwise = hsep [ppr c, interppSP pats] -- inner ParPats supply the necessary parens.
169 pprInPat (ConOpPatIn pat1 op fixity pat2)
170 = hsep [ppr pat1, ppr op, ppr pat2] -- ParPats put in parens
172 -- ToDo: use pprSym to print op (but this involves fiddling various
173 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
175 pprInPat (RecPatIn con rpats)
176 = hsep [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
178 pp_rpat (v, _, True) = ppr v
179 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
181 pprInPat (TypePatIn ty) = ptext SLIT("{|") <> ppr ty <> ptext SLIT("|}")
185 instance (NamedThing id, Outputable id) => Outputable (OutPat id) where
190 pprOutPat (WildPat ty) = char '_'
191 pprOutPat (VarPat var) = ppr var
192 pprOutPat (LazyPat pat) = hcat [char '~', ppr pat]
193 pprOutPat (AsPat name pat)
194 = parens (hcat [ppr name, char '@', ppr pat])
196 pprOutPat (SigPat pat ty _) = ppr pat <+> dcolon <+> ppr ty
198 pprOutPat (ConPat name ty [] [] [])
201 -- Kludge to get infix constructors to come out right
202 -- when ppr'ing desugar warnings.
203 pprOutPat (ConPat name ty tyvars dicts pats)
204 = getPprStyle $ \ sty ->
208 | userStyle sty && isDataSymOcc (getOccName name) ->
209 hsep [ppr p1, ppr name, ppr p2]
210 _ -> hsep [ppr name, interppSP tyvars, interppSP dicts, interppSP pats]
212 pprOutPat (ListPat ty pats) = brackets (interpp'SP pats)
213 pprOutPat (TuplePat pats boxity) = tupleParens boxity (interpp'SP pats)
215 pprOutPat (RecPat con ty tvs dicts rpats)
216 = hsep [ppr con, interppSP tvs, interppSP dicts, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
218 pp_rpat (v, _, True) = ppr v
219 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
221 pprOutPat (LitPat l ty) = ppr l -- ToDo: print more
222 pprOutPat (NPat l ty e) = ppr l -- ToDo: print more
223 pprOutPat (NPlusKPat n k ty e1 e2) -- ToDo: print more
224 = parens (hcat [ppr n, char '+', integer k])
226 pprOutPat (DictPat dicts methods)
227 = parens (sep [ptext SLIT("{-dict-}"),
228 brackets (interpp'SP dicts),
229 brackets (interpp'SP methods)])
233 %************************************************************************
235 %* predicates for checking things about pattern-lists in EquationInfo *
237 %************************************************************************
238 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
240 Unlike in the Wadler chapter, where patterns are either ``variables''
241 or ``constructors,'' here we distinguish between:
244 Patterns that cannot fail to match: variables, wildcards, and lazy
247 These are the irrefutable patterns; the two other categories
248 are refutable patterns.
251 A non-literal constructor pattern (see next category).
253 \item[literal patterns:]
254 At least the numeric ones may be overloaded.
257 A pattern is in {\em exactly one} of the above three categories; `as'
258 patterns are treated specially, of course.
260 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
262 irrefutablePats :: [OutPat id] -> Bool
263 irrefutablePats pat_list = all irrefutablePat pat_list
265 irrefutablePat (AsPat _ pat) = irrefutablePat pat
266 irrefutablePat (WildPat _) = True
267 irrefutablePat (VarPat _) = True
268 irrefutablePat (LazyPat _) = True
269 irrefutablePat (DictPat ds ms) = (length ds + length ms) <= 1
270 irrefutablePat other = False
272 failureFreePat :: OutPat id -> Bool
274 failureFreePat (WildPat _) = True
275 failureFreePat (VarPat _) = True
276 failureFreePat (LazyPat _) = True
277 failureFreePat (AsPat _ pat) = failureFreePat pat
278 failureFreePat (ConPat con tys _ _ pats) = only_con con && all failureFreePat pats
279 failureFreePat (RecPat con _ _ _ fields) = only_con con && and [ failureFreePat pat | (_,pat,_) <- fields ]
280 failureFreePat (ListPat _ _) = False
281 failureFreePat (TuplePat pats _) = all failureFreePat pats
282 failureFreePat (DictPat _ _) = True
283 failureFreePat other_pat = False -- Literals, NPat
285 only_con con = maybeToBool (maybeTyConSingleCon (dataConTyCon con))
289 isWildPat (WildPat _) = True
290 isWildPat other = False
292 patsAreAllCons :: [OutPat id] -> Bool
293 patsAreAllCons pat_list = all isConPat pat_list
295 isConPat (AsPat _ pat) = isConPat pat
296 isConPat (ConPat _ _ _ _ _) = True
297 isConPat (ListPat _ _) = True
298 isConPat (TuplePat _ _) = True
299 isConPat (RecPat _ _ _ _ _) = True
300 isConPat (DictPat ds ms) = (length ds + length ms) > 1
301 isConPat other = False
303 patsAreAllLits :: [OutPat id] -> Bool
304 patsAreAllLits pat_list = all isLitPat pat_list
306 isLitPat (AsPat _ pat) = isLitPat pat
307 isLitPat (LitPat _ _) = True
308 isLitPat (NPat _ _ _) = True
309 isLitPat (NPlusKPat _ _ _ _ _) = True
310 isLitPat other = False
313 This function @collectPatBinders@ works with the ``collectBinders''
314 functions for @HsBinds@, etc. The order in which the binders are
315 collected is important; see @HsBinds.lhs@.
318 collectPatBinders :: InPat a -> [a]
319 collectPatBinders pat = collect pat []
321 collectPatsBinders :: [InPat a] -> [a]
322 collectPatsBinders pats = foldr collect [] pats
324 collect WildPatIn bndrs = bndrs
325 collect (VarPatIn var) bndrs = var : bndrs
326 collect (LitPatIn _) bndrs = bndrs
327 collect (SigPatIn pat _) bndrs = collect pat bndrs
328 collect (LazyPatIn pat) bndrs = collect pat bndrs
329 collect (AsPatIn a pat) bndrs = a : collect pat bndrs
330 collect (NPlusKPatIn n _ _) bndrs = n : bndrs
331 collect (NPatIn _) bndrs = bndrs
332 collect (ConPatIn c pats) bndrs = foldr collect bndrs pats
333 collect (ConOpPatIn p1 c f p2) bndrs = collect p1 (collect p2 bndrs)
334 collect (ParPatIn pat) bndrs = collect pat bndrs
335 collect (ListPatIn pats) bndrs = foldr collect bndrs pats
336 collect (TuplePatIn pats _) bndrs = foldr collect bndrs pats
337 collect (RecPatIn c fields) bndrs = foldr (\ (f,pat,_) bndrs -> collect pat bndrs) bndrs fields
339 collect (TypePatIn ty) bndrs = bndrs
340 -- assume the type variables do not need to be bound
344 collectSigTysFromPats :: [InPat name] -> [HsType name]
345 collectSigTysFromPats pats = foldr collect_pat [] pats
347 collectSigTysFromPat :: InPat name -> [HsType name]
348 collectSigTysFromPat pat = collect_pat pat []
350 collect_pat (SigPatIn pat ty) acc = collect_pat pat (ty:acc)
351 collect_pat WildPatIn acc = acc
352 collect_pat (VarPatIn var) acc = acc
353 collect_pat (LitPatIn _) acc = acc
354 collect_pat (LazyPatIn pat) acc = collect_pat pat acc
355 collect_pat (AsPatIn a pat) acc = collect_pat pat acc
356 collect_pat (NPatIn _) acc = acc
357 collect_pat (NPlusKPatIn n _ _) acc = acc
358 collect_pat (ConPatIn c pats) acc = foldr collect_pat acc pats
359 collect_pat (ConOpPatIn p1 c f p2) acc = collect_pat p1 (collect_pat p2 acc)
360 collect_pat (ParPatIn pat) acc = collect_pat pat acc
361 collect_pat (ListPatIn pats) acc = foldr collect_pat acc pats
362 collect_pat (TuplePatIn pats _) acc = foldr collect_pat acc pats
363 collect_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> collect_pat pat acc) acc fields
365 collect_pat (TypePatIn ty) acc = ty:acc