2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[PatSyntax]{Abstract Haskell syntax---patterns}
11 irrefutablePat, irrefutablePats,
13 patsAreAllCons, isConPat,
14 patsAreAllLits, isLitPat,
18 #include "HsVersions.h"
21 import HsBasic ( HsLit )
22 import HsExpr ( HsExpr )
23 import BasicTypes ( Fixity )
26 import Var ( Id, GenTyVar )
27 import DataCon ( DataCon, dataConTyCon )
28 import Maybes ( maybeToBool )
30 import TyCon ( maybeTyConSingleCon )
31 import Type ( GenType )
34 Patterns come in distinct before- and after-typechecking flavo(u)rs.
37 = WildPatIn -- wild card
38 | VarPatIn name -- variable
39 | LitPatIn HsLit -- literal
40 | LazyPatIn (InPat name) -- lazy pattern
41 | AsPatIn name -- as pattern
43 | ConPatIn name -- constructed type
45 | ConOpPatIn (InPat name)
47 Fixity -- c.f. OpApp in HsExpr
50 | NPlusKPatIn name -- n+k pattern
53 -- We preserve prefix negation and parenthesis for the precedence parser.
55 | NegPatIn (InPat name) -- negated pattern
56 | ParPatIn (InPat name) -- parenthesised pattern
58 | ListPatIn [InPat name] -- syntactic list
59 -- must have >= 1 elements
60 | TuplePatIn [InPat name] Bool -- tuple (boxed?)
62 | RecPatIn name -- record
63 [(name, InPat name, Bool)] -- True <=> source used punning
66 = WildPat (GenType flexi) -- wild card
68 | VarPat id -- variable (type is in the Id)
70 | LazyPat (OutPat flexi id) -- lazy pattern
72 | AsPat id -- as pattern
75 | ListPat -- syntactic list
76 (GenType flexi) -- the type of the elements
79 | TuplePat [OutPat flexi id] -- tuple
81 -- UnitPat is TuplePat []
84 (GenType flexi) -- the type of the pattern
85 [GenTyVar flexi] -- Existentially bound type variables
86 [id] -- Ditto dictionaries
89 -- ConOpPats are only used on the input side
91 | RecPat DataCon -- record constructor
92 (GenType flexi) -- the type of the pattern
93 [GenTyVar flexi] -- Existentially bound type variables
94 [id] -- Ditto dictionaries
95 [(Id, OutPat flexi id, Bool)] -- True <=> source used punning
97 | LitPat -- Used for *non-overloaded* literal patterns:
98 -- Int#, Char#, Int, Char, String, etc.
100 (GenType flexi) -- type of pattern
102 | NPat -- Used for *overloaded* literal patterns
103 HsLit -- the literal is retained so that
104 -- the desugarer can readily identify
105 -- equations with identical literal-patterns
106 (GenType flexi) -- type of pattern, t
107 (HsExpr flexi id (OutPat flexi id))
108 -- of type t -> Bool; detects match
111 HsLit -- Same reason as for LitPat
112 -- (This could be an Integer, but then
113 -- it's harder to partitionEqnsByLit
114 -- in the desugarer.)
115 (GenType flexi) -- Type of pattern, t
116 (HsExpr flexi id (OutPat flexi id)) -- Of type t -> Bool; detects match
117 (HsExpr flexi id (OutPat flexi id)) -- Of type t -> t; subtracts k
119 | DictPat -- Used when destructing Dictionaries with an explicit case
120 [id] -- superclass dicts
124 Now name in Inpat is not need to be in NAmedThing to be Outputable.
125 Needed by ../deSugar/Check.lhs
130 instance (Outputable name) => Outputable (InPat name) where
133 pprInPat :: (Outputable name) => InPat name -> SDoc
135 pprInPat (WildPatIn) = char '_'
136 pprInPat (VarPatIn var) = ppr var
137 pprInPat (LitPatIn s) = ppr s
138 pprInPat (LazyPatIn pat) = char '~' <> ppr pat
139 pprInPat (AsPatIn name pat) = parens (hcat [ppr name, char '@', ppr pat])
141 pprInPat (ConPatIn c pats)
143 | otherwise = hsep [ppr c, interppSP pats] -- ParPats put in the parens
145 pprInPat (ConOpPatIn pat1 op fixity pat2)
146 = hsep [ppr pat1, ppr op, ppr pat2] -- ParPats put in parens
148 -- ToDo: use pprSym to print op (but this involves fiddling various
149 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
151 pprInPat (NegPatIn pat)
153 pp_pat = pprInPat pat
161 pprInPat (ParPatIn pat)
162 = parens (pprInPat pat)
164 pprInPat (ListPatIn pats)
165 = brackets (interpp'SP pats)
166 pprInPat (TuplePatIn pats False)
167 = text "(#" <> (interpp'SP pats) <> text "#)"
168 pprInPat (TuplePatIn pats True)
169 = parens (interpp'SP pats)
170 pprInPat (NPlusKPatIn n k)
171 = parens (hcat [ppr n, char '+', ppr k])
173 pprInPat (RecPatIn con rpats)
174 = hsep [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
176 pp_rpat (v, _, True) = ppr v
177 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
181 instance (Outputable id) => Outputable (OutPat flexi id) where
186 pprOutPat (WildPat ty) = char '_'
187 pprOutPat (VarPat var) = ppr var
188 pprOutPat (LazyPat pat) = hcat [char '~', ppr pat]
189 pprOutPat (AsPat name pat)
190 = parens (hcat [ppr name, char '@', ppr pat])
192 pprOutPat (ConPat name ty [] [] [])
195 pprOutPat (ConPat name ty tyvars dicts pats)
196 = parens (hsep [ppr name, interppSP tyvars, interppSP dicts, interppSP pats])
198 pprOutPat (ListPat ty pats)
199 = brackets (interpp'SP pats)
200 pprOutPat (TuplePat pats boxed@True)
201 = parens (interpp'SP pats)
202 pprOutPat (TuplePat pats unboxed@False)
203 = text "(#" <> (interpp'SP pats) <> text "#)"
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 '+', ppr 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 a b] -> 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 a b -> 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 patsAreAllCons :: [OutPat a b] -> Bool
280 patsAreAllCons pat_list = all isConPat pat_list
282 isConPat (AsPat _ pat) = isConPat pat
283 isConPat (ConPat _ _ _ _ _) = True
284 isConPat (ListPat _ _) = True
285 isConPat (TuplePat _ _) = True
286 isConPat (RecPat _ _ _ _ _) = True
287 isConPat (DictPat ds ms) = (length ds + length ms) > 1
288 isConPat other = False
290 patsAreAllLits :: [OutPat a b] -> Bool
291 patsAreAllLits pat_list = all isLitPat pat_list
293 isLitPat (AsPat _ pat) = isLitPat pat
294 isLitPat (LitPat _ _) = True
295 isLitPat (NPat _ _ _) = True
296 isLitPat (NPlusKPat _ _ _ _ _) = True
297 isLitPat other = False
300 This function @collectPatBinders@ works with the ``collectBinders''
301 functions for @HsBinds@, etc. The order in which the binders are
302 collected is important; see @HsBinds.lhs@.
304 collectPatBinders :: InPat a -> [a]
306 collectPatBinders WildPatIn = []
307 collectPatBinders (VarPatIn var) = [var]
308 collectPatBinders (LitPatIn _) = []
309 collectPatBinders (LazyPatIn pat) = collectPatBinders pat
310 collectPatBinders (AsPatIn a pat) = a : collectPatBinders pat
311 collectPatBinders (NPlusKPatIn n _) = [n]
312 collectPatBinders (ConPatIn c pats) = concat (map collectPatBinders pats)
313 collectPatBinders (ConOpPatIn p1 c f p2) = collectPatBinders p1 ++ collectPatBinders p2
314 collectPatBinders (NegPatIn pat) = collectPatBinders pat
315 collectPatBinders (ParPatIn pat) = collectPatBinders pat
316 collectPatBinders (ListPatIn pats) = concat (map collectPatBinders pats)
317 collectPatBinders (TuplePatIn pats _) = concat (map collectPatBinders pats)
318 collectPatBinders (RecPatIn c fields) = concat (map (\ (f,pat,_) -> collectPatBinders pat) fields)