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 )
29 import Var ( Id, TyVar )
30 import DataCon ( DataCon, dataConTyCon )
31 import Name ( isDataSymOcc, getOccName, NamedThing )
32 import Maybes ( maybeToBool )
34 import TyCon ( maybeTyConSingleCon )
38 Patterns come in distinct before- and after-typechecking flavo(u)rs.
41 = WildPatIn -- wild card
42 | VarPatIn name -- variable
43 | LitPatIn HsLit -- literal
44 | LazyPatIn (InPat name) -- lazy pattern
45 | AsPatIn name -- as pattern
47 | SigPatIn (InPat name)
49 | ConPatIn name -- constructed type
51 | ConOpPatIn (InPat name)
53 Fixity -- c.f. OpApp in HsExpr
58 | NPlusKPatIn name -- n+k pattern
59 HsOverLit -- It'll always be an HsIntegral
61 -- We preserve prefix negation and parenthesis for the precedence parser.
63 | ParPatIn (InPat name) -- parenthesised pattern
65 | ListPatIn [InPat name] -- syntactic list
66 -- must have >= 1 elements
67 | TuplePatIn [InPat name] Boxity -- tuple (boxed?)
69 | RecPatIn name -- record
70 [(name, InPat name, Bool)] -- True <=> source used punning
73 | TypePatIn (HsType name) -- Type pattern for generic definitions
74 -- e.g f{| a+b |} = ...
75 -- These show up only in class
77 -- and should be a top-level pattern
82 = WildPat Type -- wild card
83 | VarPat id -- variable (type is in the Id)
84 | LazyPat (OutPat id) -- lazy pattern
85 | AsPat id -- as pattern
88 | ListPat -- Syntactic list
89 Type -- The type of the elements
92 | TuplePat [OutPat id] -- Tuple
94 -- UnitPat is TuplePat []
97 Type -- the type of the pattern
98 [TyVar] -- Existentially bound type variables
99 [id] -- Ditto dictionaries
102 -- ConOpPats are only used on the input side
104 | RecPat DataCon -- Record constructor
105 Type -- The type of the pattern
106 [TyVar] -- Existentially bound type variables
107 [id] -- Ditto dictionaries
108 [(Id, OutPat id, Bool)] -- True <=> source used punning
110 | LitPat -- Used for *non-overloaded* literal patterns:
111 -- Int#, Char#, Int, Char, String, etc.
113 Type -- Type of pattern
115 | NPat -- Used for *overloaded* literal patterns
116 HsLit -- The literal is retained so that
117 -- the desugarer can readily identify
118 -- equations with identical literal-patterns
119 -- Always HsInt, HsRat or HsString.
120 Type -- Type of pattern, t
121 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
125 Type -- Type of pattern, t
126 (HsExpr id (OutPat id)) -- Of type t -> Bool; detects match
127 (HsExpr id (OutPat id)) -- Of type t -> t; subtracts k
129 | DictPat -- Used when destructing Dictionaries with an explicit case
130 [id] -- superclass dicts
134 Now name in Inpat is not need to be in NAmedThing to be Outputable.
135 Needed by ../deSugar/Check.lhs
140 instance (Outputable name) => Outputable (InPat name) where
143 pprInPat :: (Outputable name) => InPat name -> SDoc
145 pprInPat (WildPatIn) = char '_'
146 pprInPat (VarPatIn var) = ppr var
147 pprInPat (LitPatIn s) = ppr s
148 pprInPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
149 pprInPat (LazyPatIn pat) = char '~' <> ppr pat
150 pprInPat (AsPatIn name pat) = parens (hcat [ppr name, char '@', ppr pat])
151 pprInPat (ParPatIn pat) = parens (pprInPat pat)
152 pprInPat (ListPatIn pats) = brackets (interpp'SP pats)
153 pprInPat (TuplePatIn pats bx) = tupleParens bx (interpp'SP pats)
154 pprInPat (NPlusKPatIn n k) = parens (hcat [ppr n, char '+', ppr k])
155 pprInPat (NPatIn l) = ppr l
157 pprInPat (ConPatIn c pats)
159 | otherwise = hsep [ppr c, interppSP pats] -- inner ParPats supply the necessary parens.
161 pprInPat (ConOpPatIn pat1 op fixity pat2)
162 = hsep [ppr pat1, ppr op, ppr pat2] -- ParPats put in parens
164 -- ToDo: use pprSym to print op (but this involves fiddling various
165 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
167 pprInPat (RecPatIn con rpats)
168 = hsep [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
170 pp_rpat (v, _, True) = ppr v
171 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
173 pprInPat (TypePatIn ty) = ptext SLIT("{|") <> ppr ty <> ptext SLIT("|}")
177 instance (NamedThing id, Outputable id) => Outputable (OutPat id) where
182 pprOutPat (WildPat ty) = char '_'
183 pprOutPat (VarPat var) = ppr var
184 pprOutPat (LazyPat pat) = hcat [char '~', ppr pat]
185 pprOutPat (AsPat name pat)
186 = parens (hcat [ppr name, char '@', ppr pat])
188 pprOutPat (ConPat name ty [] [] [])
191 -- Kludge to get infix constructors to come out right
192 -- when ppr'ing desugar warnings.
193 pprOutPat (ConPat name ty tyvars dicts pats)
194 = getPprStyle $ \ sty ->
198 | userStyle sty && isDataSymOcc (getOccName name) ->
199 hsep [ppr p1, ppr name, ppr p2]
200 _ -> hsep [ppr name, interppSP tyvars, interppSP dicts, interppSP pats]
202 pprOutPat (ListPat ty pats) = brackets (interpp'SP pats)
203 pprOutPat (TuplePat pats boxity) = tupleParens boxity (interpp'SP pats)
205 pprOutPat (RecPat con ty tvs dicts rpats)
206 = hsep [ppr con, interppSP tvs, interppSP dicts, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
208 pp_rpat (v, _, True) = ppr v
209 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
211 pprOutPat (LitPat l ty) = ppr l -- ToDo: print more
212 pprOutPat (NPat l ty e) = ppr l -- ToDo: print more
213 pprOutPat (NPlusKPat n k ty e1 e2) -- ToDo: print more
214 = parens (hcat [ppr n, char '+', integer k])
216 pprOutPat (DictPat dicts methods)
217 = parens (sep [ptext SLIT("{-dict-}"),
218 brackets (interpp'SP dicts),
219 brackets (interpp'SP methods)])
223 %************************************************************************
225 %* predicates for checking things about pattern-lists in EquationInfo *
227 %************************************************************************
228 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
230 Unlike in the Wadler chapter, where patterns are either ``variables''
231 or ``constructors,'' here we distinguish between:
234 Patterns that cannot fail to match: variables, wildcards, and lazy
237 These are the irrefutable patterns; the two other categories
238 are refutable patterns.
241 A non-literal constructor pattern (see next category).
243 \item[literal patterns:]
244 At least the numeric ones may be overloaded.
247 A pattern is in {\em exactly one} of the above three categories; `as'
248 patterns are treated specially, of course.
250 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
252 irrefutablePats :: [OutPat id] -> Bool
253 irrefutablePats pat_list = all irrefutablePat pat_list
255 irrefutablePat (AsPat _ pat) = irrefutablePat pat
256 irrefutablePat (WildPat _) = True
257 irrefutablePat (VarPat _) = True
258 irrefutablePat (LazyPat _) = True
259 irrefutablePat (DictPat ds ms) = (length ds + length ms) <= 1
260 irrefutablePat other = False
262 failureFreePat :: OutPat id -> Bool
264 failureFreePat (WildPat _) = True
265 failureFreePat (VarPat _) = True
266 failureFreePat (LazyPat _) = True
267 failureFreePat (AsPat _ pat) = failureFreePat pat
268 failureFreePat (ConPat con tys _ _ pats) = only_con con && all failureFreePat pats
269 failureFreePat (RecPat con _ _ _ fields) = only_con con && and [ failureFreePat pat | (_,pat,_) <- fields ]
270 failureFreePat (ListPat _ _) = False
271 failureFreePat (TuplePat pats _) = all failureFreePat pats
272 failureFreePat (DictPat _ _) = True
273 failureFreePat other_pat = False -- Literals, NPat
275 only_con con = maybeToBool (maybeTyConSingleCon (dataConTyCon con))
279 isWildPat (WildPat _) = True
280 isWildPat other = False
282 patsAreAllCons :: [OutPat id] -> Bool
283 patsAreAllCons pat_list = all isConPat pat_list
285 isConPat (AsPat _ pat) = isConPat pat
286 isConPat (ConPat _ _ _ _ _) = True
287 isConPat (ListPat _ _) = True
288 isConPat (TuplePat _ _) = True
289 isConPat (RecPat _ _ _ _ _) = True
290 isConPat (DictPat ds ms) = (length ds + length ms) > 1
291 isConPat other = False
293 patsAreAllLits :: [OutPat id] -> Bool
294 patsAreAllLits pat_list = all isLitPat pat_list
296 isLitPat (AsPat _ pat) = isLitPat pat
297 isLitPat (LitPat _ _) = True
298 isLitPat (NPat _ _ _) = True
299 isLitPat (NPlusKPat _ _ _ _ _) = True
300 isLitPat other = False
303 This function @collectPatBinders@ works with the ``collectBinders''
304 functions for @HsBinds@, etc. The order in which the binders are
305 collected is important; see @HsBinds.lhs@.
308 collectPatBinders :: InPat a -> [a]
309 collectPatBinders pat = collect pat []
311 collectPatsBinders :: [InPat a] -> [a]
312 collectPatsBinders pats = foldr collect [] pats
314 collect WildPatIn bndrs = bndrs
315 collect (VarPatIn var) bndrs = var : bndrs
316 collect (LitPatIn _) bndrs = bndrs
317 collect (SigPatIn pat _) bndrs = collect pat bndrs
318 collect (LazyPatIn pat) bndrs = collect pat bndrs
319 collect (AsPatIn a pat) bndrs = a : collect pat bndrs
320 collect (NPlusKPatIn n _) bndrs = n : bndrs
321 collect (NPatIn _) bndrs = bndrs
322 collect (ConPatIn c pats) bndrs = foldr collect bndrs pats
323 collect (ConOpPatIn p1 c f p2) bndrs = collect p1 (collect p2 bndrs)
324 collect (ParPatIn pat) bndrs = collect pat bndrs
325 collect (ListPatIn pats) bndrs = foldr collect bndrs pats
326 collect (TuplePatIn pats _) bndrs = foldr collect bndrs pats
327 collect (RecPatIn c fields) bndrs = foldr (\ (f,pat,_) bndrs -> collect pat bndrs) bndrs fields
329 collect (TypePatIn ty) bndrs = bndrs
330 -- assume the type variables do not need to be bound
334 collectSigTysFromPats :: [InPat name] -> [HsType name]
335 collectSigTysFromPats pats = foldr collect_pat [] pats
337 collectSigTysFromPat :: InPat name -> [HsType name]
338 collectSigTysFromPat pat = collect_pat pat []
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