2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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 Id ( Id, dataConTyCon, GenId )
27 import Maybes ( maybeToBool )
29 import TyCon ( maybeTyConSingleCon )
30 import PprType ( GenType )
31 import Name ( NamedThing )
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] -- tuple
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 | ConPat Id -- Constructor is always an Id
76 (GenType flexi) -- the type of the pattern
79 | ConOpPat (OutPat flexi id) -- just a special case...
83 | ListPat -- syntactic list
84 (GenType flexi) -- the type of the elements
87 | TuplePat [OutPat flexi id] -- tuple
88 -- UnitPat is TuplePat []
90 | RecPat Id -- record constructor
91 (GenType flexi) -- the type of the pattern
92 [(Id, OutPat flexi id, Bool)] -- True <=> source used punning
94 | LitPat -- Used for *non-overloaded* literal patterns:
95 -- Int#, Char#, Int, Char, String, etc.
97 (GenType flexi) -- type of pattern
99 | NPat -- Used for *overloaded* literal patterns
100 HsLit -- the literal is retained so that
101 -- the desugarer can readily identify
102 -- equations with identical literal-patterns
103 (GenType flexi) -- type of pattern, t
104 (HsExpr flexi id (OutPat flexi id))
105 -- of type t -> Bool; detects match
108 HsLit -- Same reason as for LitPat
109 -- (This could be an Integer, but then
110 -- it's harder to partitionEqnsByLit
111 -- in the desugarer.)
112 (GenType flexi) -- Type of pattern, t
113 (HsExpr flexi id (OutPat flexi id)) -- Of type t -> Bool; detects match
114 (HsExpr flexi id (OutPat flexi id)) -- Of type t -> t; subtracts k
116 | DictPat -- Used when destructing Dictionaries with an explicit case
117 [id] -- superclass dicts
121 Now name in Inpat is not need to be in NAmedThing to be Outputable.
122 Needed by ../deSugar/Check.lhs
127 instance (Outputable name) => Outputable (InPat name) where
130 pprInPat :: (Outputable name) => InPat name -> SDoc
132 pprInPat (WildPatIn) = char '_'
133 pprInPat (VarPatIn var) = ppr var
134 pprInPat (LitPatIn s) = ppr s
135 pprInPat (LazyPatIn pat) = char '~' <> ppr pat
136 pprInPat (AsPatIn name pat) = parens (hcat [ppr name, char '@', ppr pat])
138 pprInPat (ConPatIn c pats)
140 | otherwise = hsep [ppr c, interppSP pats] -- ParPats put in the parens
142 pprInPat (ConOpPatIn pat1 op fixity pat2)
143 = hsep [ppr pat1, ppr op, ppr pat2] -- ParPats put in parens
145 -- ToDo: use pprSym to print op (but this involves fiddling various
146 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
148 pprInPat (NegPatIn pat)
150 pp_pat = pprInPat pat
158 pprInPat (ParPatIn pat)
159 = parens (pprInPat pat)
161 pprInPat (ListPatIn pats)
162 = brackets (interpp'SP pats)
163 pprInPat (TuplePatIn pats)
164 = parens (interpp'SP pats)
165 pprInPat (NPlusKPatIn n k)
166 = parens (hcat [ppr n, char '+', ppr k])
168 pprInPat (RecPatIn con rpats)
169 = hsep [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
171 pp_rpat (v, _, True) = ppr v
172 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
176 instance (Outputable id) => Outputable (OutPat flexi 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 pprOutPat (ConPat name ty pats)
191 = hcat [parens (hcat [ppr name, space, interppSP pats])]
193 pprOutPat (ConOpPat pat1 op pat2 ty)
194 = parens (hcat [ppr pat1, space, ppr op, space, ppr pat2])
196 pprOutPat (ListPat ty pats)
197 = brackets (interpp'SP pats)
198 pprOutPat (TuplePat pats)
199 = parens (interpp'SP pats)
201 pprOutPat (RecPat con ty rpats)
202 = hcat [ppr con, braces (hsep (punctuate comma (map (pp_rpat) rpats)))]
204 pp_rpat (v, _, True) = ppr v
205 pp_rpat (v, p, _) = hsep [ppr v, char '=', ppr p]
207 pprOutPat (LitPat l ty) = ppr l -- ToDo: print more
208 pprOutPat (NPat l ty e) = ppr l -- ToDo: print more
209 pprOutPat (NPlusKPat n k ty e1 e2) -- ToDo: print more
210 = parens (hcat [ppr n, char '+', ppr k])
212 pprOutPat (DictPat dicts methods)
213 = parens (sep [ptext SLIT("{-dict-}"),
214 brackets (interpp'SP dicts),
215 brackets (interpp'SP methods)])
221 %************************************************************************
223 %* predicates for checking things about pattern-lists in EquationInfo *
225 %************************************************************************
226 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
228 Unlike in the Wadler chapter, where patterns are either ``variables''
229 or ``constructors,'' here we distinguish between:
232 Patterns that cannot fail to match: variables, wildcards, and lazy
235 These are the irrefutable patterns; the two other categories
236 are refutable patterns.
239 A non-literal constructor pattern (see next category).
241 \item[literal patterns:]
242 At least the numeric ones may be overloaded.
245 A pattern is in {\em exactly one} of the above three categories; `as'
246 patterns are treated specially, of course.
248 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
250 irrefutablePats :: [OutPat a b] -> Bool
251 irrefutablePats pat_list = all irrefutablePat pat_list
253 irrefutablePat (AsPat _ pat) = irrefutablePat pat
254 irrefutablePat (WildPat _) = True
255 irrefutablePat (VarPat _) = True
256 irrefutablePat (LazyPat _) = True
257 irrefutablePat (DictPat ds ms) = (length ds + length ms) <= 1
258 irrefutablePat other = False
260 failureFreePat :: OutPat a b -> Bool
262 failureFreePat (WildPat _) = True
263 failureFreePat (VarPat _) = True
264 failureFreePat (LazyPat _) = True
265 failureFreePat (AsPat _ pat) = failureFreePat pat
266 failureFreePat (ConPat con tys pats) = only_con con && all failureFreePat pats
267 failureFreePat (ConOpPat pat1 con pat2 _) = only_con con && failureFreePat pat1 && failureFreePat pat1
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 patsAreAllCons :: [OutPat a b] -> Bool
279 patsAreAllCons pat_list = all isConPat pat_list
281 isConPat (AsPat _ pat) = isConPat pat
282 isConPat (ConPat _ _ _) = True
283 isConPat (ConOpPat _ _ _ _) = 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)