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 ( 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
57 | NPlusKPatIn name -- n+k pattern
58 HsOverLit -- It'll always be an HsIntegral
60 -- We preserve prefix negation and parenthesis for the precedence parser.
62 | ParPatIn (InPat name) -- parenthesised pattern
64 | ListPatIn [InPat name] -- syntactic list
65 -- must have >= 1 elements
66 | TuplePatIn [InPat name] Boxity -- tuple (boxed?)
68 | RecPatIn name -- record
69 [(name, InPat name, Bool)] -- True <=> source used punning
72 | TypePatIn (HsType name) -- Type pattern for generic definitions
73 -- e.g f{| a+b |} = ...
74 -- These show up only in class
76 -- and should be a top-level pattern
81 = WildPat Type -- wild card
82 | VarPat id -- variable (type is in the Id)
83 | LazyPat (OutPat id) -- lazy pattern
84 | AsPat id -- as pattern
87 | ListPat -- Syntactic list
88 Type -- The type of the elements
91 | TuplePat [OutPat id] -- Tuple
93 -- UnitPat is TuplePat []
96 Type -- the type of the pattern
97 [TyVar] -- Existentially bound type variables
98 [id] -- Ditto dictionaries
101 -- ConOpPats are only used on the input side
103 | RecPat DataCon -- Record constructor
104 Type -- The type of the pattern
105 [TyVar] -- Existentially bound type variables
106 [id] -- Ditto dictionaries
107 [(Id, OutPat id, Bool)] -- True <=> source used punning
109 | LitPat -- Used for *non-overloaded* literal patterns:
110 -- Int#, Char#, Int, Char, String, etc.
112 Type -- Type of pattern
114 | NPat -- Used for *overloaded* literal patterns
115 HsLit -- The literal is retained so that
116 -- the desugarer can readily identify
117 -- equations with identical literal-patterns
118 -- Always HsInt, HsRat or HsString.
119 Type -- Type of pattern, t
120 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
124 Type -- Type of pattern, t
125 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
126 (HsExpr id (OutPat id)) -- Of type t -> t; subtracts k
128 | DictPat -- Used when destructing Dictionaries with an explicit case
129 [id] -- superclass dicts
133 Now name in Inpat is not need to be in NAmedThing to be Outputable.
134 Needed by ../deSugar/Check.lhs
139 instance (Outputable name) => Outputable (InPat name) where
142 pprInPat :: (Outputable name) => InPat name -> SDoc
144 pprInPat (WildPatIn) = char '_'
145 pprInPat (VarPatIn var) = ppr var
146 pprInPat (LitPatIn s) = ppr s
147 pprInPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
148 pprInPat (LazyPatIn pat) = char '~' <> ppr pat
149 pprInPat (AsPatIn name pat) = parens (hcat [ppr name, char '@', ppr pat])
150 pprInPat (ParPatIn pat) = parens (pprInPat pat)
151 pprInPat (ListPatIn pats) = brackets (interpp'SP pats)
152 pprInPat (TuplePatIn pats bx) = tupleParens bx (interpp'SP pats)
153 pprInPat (NPlusKPatIn n k) = parens (hcat [ppr n, char '+', ppr k])
154 pprInPat (NPatIn l) = ppr l
156 pprInPat (ConPatIn c pats)
158 | otherwise = hsep [ppr c, interppSP pats] -- inner ParPats supply the necessary parens.
160 pprInPat (ConOpPatIn pat1 op fixity pat2)
161 = hsep [ppr pat1, ppr op, ppr pat2] -- ParPats put in parens
163 -- ToDo: use pprSym to print op (but this involves fiddling various
164 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
166 pprInPat (RecPatIn con rpats)
167 = hsep [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
169 pp_rpat (v, _, True) = ppr v
170 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
172 pprInPat (TypePatIn ty) = ptext SLIT("{|") <> ppr ty <> ptext SLIT("|}")
176 instance (NamedThing id, Outputable id) => Outputable (OutPat id) where
181 pprOutPat (WildPat ty) = char '_'
182 pprOutPat (VarPat var) = ppr var
183 pprOutPat (LazyPat pat) = hcat [char '~', ppr pat]
184 pprOutPat (AsPat name pat)
185 = parens (hcat [ppr name, char '@', ppr pat])
187 pprOutPat (ConPat name ty [] [] [])
190 -- Kludge to get infix constructors to come out right
191 -- when ppr'ing desugar warnings.
192 pprOutPat (ConPat name ty tyvars dicts pats)
193 = getPprStyle $ \ sty ->
197 | userStyle sty && isDataSymOcc (getOccName name) ->
198 hsep [ppr p1, ppr name, ppr p2]
199 _ -> hsep [ppr name, interppSP tyvars, interppSP dicts, interppSP pats]
201 pprOutPat (ListPat ty pats) = brackets (interpp'SP pats)
202 pprOutPat (TuplePat pats boxity) = tupleParens boxity (interpp'SP pats)
204 pprOutPat (RecPat con ty tvs dicts rpats)
205 = hsep [ppr con, interppSP tvs, interppSP dicts, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
207 pp_rpat (v, _, True) = ppr v
208 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
210 pprOutPat (LitPat l ty) = ppr l -- ToDo: print more
211 pprOutPat (NPat l ty e) = ppr l -- ToDo: print more
212 pprOutPat (NPlusKPat n k ty e1 e2) -- ToDo: print more
213 = parens (hcat [ppr n, char '+', integer k])
215 pprOutPat (DictPat dicts methods)
216 = parens (sep [ptext SLIT("{-dict-}"),
217 brackets (interpp'SP dicts),
218 brackets (interpp'SP methods)])
222 %************************************************************************
224 %* predicates for checking things about pattern-lists in EquationInfo *
226 %************************************************************************
227 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
229 Unlike in the Wadler chapter, where patterns are either ``variables''
230 or ``constructors,'' here we distinguish between:
233 Patterns that cannot fail to match: variables, wildcards, and lazy
236 These are the irrefutable patterns; the two other categories
237 are refutable patterns.
240 A non-literal constructor pattern (see next category).
242 \item[literal patterns:]
243 At least the numeric ones may be overloaded.
246 A pattern is in {\em exactly one} of the above three categories; `as'
247 patterns are treated specially, of course.
249 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
251 irrefutablePats :: [OutPat id] -> Bool
252 irrefutablePats pat_list = all irrefutablePat pat_list
254 irrefutablePat (AsPat _ pat) = irrefutablePat pat
255 irrefutablePat (WildPat _) = True
256 irrefutablePat (VarPat _) = True
257 irrefutablePat (LazyPat _) = True
258 irrefutablePat (DictPat ds ms) = (length ds + length ms) <= 1
259 irrefutablePat other = False
261 failureFreePat :: OutPat id -> Bool
263 failureFreePat (WildPat _) = True
264 failureFreePat (VarPat _) = True
265 failureFreePat (LazyPat _) = True
266 failureFreePat (AsPat _ pat) = failureFreePat pat
267 failureFreePat (ConPat con tys _ _ pats) = only_con con && all failureFreePat pats
268 failureFreePat (RecPat con _ _ _ fields) = only_con con && and [ failureFreePat pat | (_,pat,_) <- fields ]
269 failureFreePat (ListPat _ _) = False
270 failureFreePat (TuplePat pats _) = all failureFreePat pats
271 failureFreePat (DictPat _ _) = True
272 failureFreePat other_pat = False -- Literals, NPat
274 only_con con = maybeToBool (maybeTyConSingleCon (dataConTyCon con))
278 isWildPat (WildPat _) = True
279 isWildPat other = False
281 patsAreAllCons :: [OutPat id] -> Bool
282 patsAreAllCons pat_list = all isConPat pat_list
284 isConPat (AsPat _ pat) = isConPat pat
285 isConPat (ConPat _ _ _ _ _) = True
286 isConPat (ListPat _ _) = True
287 isConPat (TuplePat _ _) = True
288 isConPat (RecPat _ _ _ _ _) = True
289 isConPat (DictPat ds ms) = (length ds + length ms) > 1
290 isConPat other = False
292 patsAreAllLits :: [OutPat id] -> Bool
293 patsAreAllLits pat_list = all isLitPat pat_list
295 isLitPat (AsPat _ pat) = isLitPat pat
296 isLitPat (LitPat _ _) = True
297 isLitPat (NPat _ _ _) = True
298 isLitPat (NPlusKPat _ _ _ _ _) = True
299 isLitPat other = False
302 This function @collectPatBinders@ works with the ``collectBinders''
303 functions for @HsBinds@, etc. The order in which the binders are
304 collected is important; see @HsBinds.lhs@.
307 collectPatBinders :: InPat a -> [a]
308 collectPatBinders pat = collect pat []
310 collectPatsBinders :: [InPat a] -> [a]
311 collectPatsBinders pats = foldr collect [] pats
313 collect WildPatIn bndrs = bndrs
314 collect (VarPatIn var) bndrs = var : bndrs
315 collect (LitPatIn _) bndrs = bndrs
316 collect (SigPatIn pat _) bndrs = collect pat bndrs
317 collect (LazyPatIn pat) bndrs = collect pat bndrs
318 collect (AsPatIn a pat) bndrs = a : collect pat bndrs
319 collect (NPlusKPatIn n _) bndrs = n : bndrs
320 collect (NPatIn _) bndrs = bndrs
321 collect (ConPatIn c pats) bndrs = foldr collect bndrs pats
322 collect (ConOpPatIn p1 c f p2) bndrs = collect p1 (collect p2 bndrs)
323 collect (ParPatIn pat) bndrs = collect pat bndrs
324 collect (ListPatIn pats) bndrs = foldr collect bndrs pats
325 collect (TuplePatIn pats _) bndrs = foldr collect bndrs pats
326 collect (RecPatIn c fields) bndrs = foldr (\ (f,pat,_) bndrs -> collect pat bndrs) bndrs fields
328 collect (TypePatIn ty) bndrs = bndrs
329 -- assume the type variables do not need to be bound
333 collectSigTysFromPats :: [InPat name] -> [HsType name]
334 collectSigTysFromPats pats = foldr collect_pat [] pats
336 collect_pat (SigPatIn pat ty) acc = collect_pat pat (ty:acc)
337 collect_pat WildPatIn acc = acc
338 collect_pat (VarPatIn var) acc = acc
339 collect_pat (LitPatIn _) acc = acc
340 collect_pat (LazyPatIn pat) acc = collect_pat pat acc
341 collect_pat (AsPatIn a pat) acc = collect_pat pat acc
342 collect_pat (NPatIn _) acc = acc
343 collect_pat (NPlusKPatIn n _) acc = acc
344 collect_pat (ConPatIn c pats) acc = foldr collect_pat acc pats
345 collect_pat (ConOpPatIn p1 c f p2) acc = collect_pat p1 (collect_pat p2 acc)
346 collect_pat (ParPatIn pat) acc = collect_pat pat acc
347 collect_pat (ListPatIn pats) acc = foldr collect_pat acc pats
348 collect_pat (TuplePatIn pats _) acc = foldr collect_pat acc pats
349 collect_pat (RecPatIn c fields) acc = foldr (\ (f,pat,_) acc -> collect_pat pat acc) acc fields
351 collect_pat (TypePatIn ty) acc = ty:acc