2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PatSyntax]{Abstract Haskell syntax---patterns}
7 #include "HsVersions.h"
13 unfailablePats, unfailablePat,
14 patsAreAllCons, isConPat,
15 patsAreAllLits, isLitPat,
23 import HsLit ( HsLit )
24 import HsLoop ( HsExpr )
27 import Id ( GenId, dataConSig )
28 import Maybes ( maybeToBool )
29 import Name ( pprOp, pprNonOp )
30 import Outputable ( interppSP, interpp'SP, ifPprShowAll )
31 import PprStyle ( PprStyle(..) )
33 import TyCon ( maybeTyConSingleCon )
34 import PprType ( GenType )
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 | ConPatIn name -- constructed type
48 | ConOpPatIn (InPat name)
52 -- We preserve prefix negation and parenthesis for the precedence parser.
54 | NegPatIn (InPat name) -- negated pattern
55 | ParPatIn (InPat name) -- parenthesised pattern
57 | ListPatIn [InPat name] -- syntactic list
58 -- must have >= 1 elements
59 | TuplePatIn [InPat name] -- tuple
61 | RecPatIn name -- record
62 [(name, InPat name, Bool)] -- True <=> source used punning
64 data OutPat tyvar uvar id
65 = WildPat (GenType tyvar uvar) -- wild card
67 | VarPat id -- variable (type is in the Id)
69 | LazyPat (OutPat tyvar uvar id) -- lazy pattern
71 | AsPat id -- as pattern
72 (OutPat tyvar uvar id)
74 | ConPat Id -- Constructor is always an Id
75 (GenType tyvar uvar) -- the type of the pattern
76 [(OutPat tyvar uvar id)]
78 | ConOpPat (OutPat tyvar uvar id) -- just a special case...
80 (OutPat tyvar uvar id)
82 | ListPat -- syntactic list
83 (GenType tyvar uvar) -- the type of the elements
84 [(OutPat tyvar uvar id)]
86 | TuplePat [(OutPat tyvar uvar id)] -- tuple
87 -- UnitPat is TuplePat []
89 | RecPat Id -- record constructor
90 (GenType tyvar uvar) -- the type of the pattern
91 [(id, OutPat tyvar uvar id, Bool)] -- True <=> source used punning
93 | LitPat -- Used for *non-overloaded* literal patterns:
94 -- Int#, Char#, Int, Char, String, etc.
96 (GenType tyvar uvar) -- type of pattern
98 | NPat -- Used for *overloaded* literal patterns
99 HsLit -- the literal is retained so that
100 -- the desugarer can readily identify
101 -- equations with identical literal-patterns
102 (GenType tyvar uvar) -- type of pattern, t
103 (HsExpr tyvar uvar id (OutPat tyvar uvar id))
104 -- of type t -> Bool; detects match
106 | DictPat -- Used when destructing Dictionaries with an explicit case
107 [id] -- superclass dicts
112 instance (Outputable name, NamedThing name) => Outputable (InPat name) where
115 pprInPat :: (Outputable name, NamedThing name) => PprStyle -> InPat name -> Pretty
117 pprInPat sty (WildPatIn) = ppStr "_"
118 pprInPat sty (VarPatIn var) = pprNonOp sty var
119 pprInPat sty (LitPatIn s) = ppr sty s
120 pprInPat sty (LazyPatIn pat) = ppBeside (ppChar '~') (ppr sty pat)
121 pprInPat sty (AsPatIn name pat)
122 = ppBesides [ppLparen, ppr sty name, ppChar '@', ppr sty pat, ppRparen]
124 pprInPat sty (ConPatIn c pats)
128 ppBesides [ppLparen, ppr sty c, ppSP, interppSP sty pats, ppRparen]
131 pprInPat sty (ConOpPatIn pat1 op pat2)
132 = ppBesides [ppLparen, ppr sty pat1, ppSP, ppr sty op, ppSP, ppr sty pat2, ppRparen]
134 -- ToDo: use pprOp to print op (but this involves fiddling various
135 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
137 pprInPat sty (NegPatIn pat)
138 = ppBeside (ppChar '-') (ppParens (pprInPat sty pat))
140 pprInPat sty (ParPatIn pat)
141 = ppParens (pprInPat sty pat)
144 pprInPat sty (ListPatIn pats)
145 = ppBesides [ppLbrack, interpp'SP sty pats, ppRbrack]
146 pprInPat sty (TuplePatIn pats)
147 = ppBesides [ppLparen, interpp'SP sty pats, ppRparen]
149 pprInPat sty (RecPatIn con rpats)
150 = ppBesides [ppr sty con, ppSP, ppChar '{', ppInterleave ppComma (map pp_rpat rpats), ppChar '}']
152 pp_rpat (v, _, True{-pun-}) = ppr sty v
153 pp_rpat (v, p, _) = ppCat [ppr sty v, ppStr "<-", ppr sty p]
157 instance (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar,
158 NamedThing id, Outputable id)
159 => Outputable (OutPat tyvar uvar id) where
164 pprOutPat sty (WildPat ty) = ppChar '_'
165 pprOutPat sty (VarPat var) = pprNonOp sty var
166 pprOutPat sty (LazyPat pat) = ppBesides [ppChar '~', ppr sty pat]
167 pprOutPat sty (AsPat name pat)
168 = ppBesides [ppLparen, ppr sty name, ppChar '@', ppr sty pat, ppRparen]
170 pprOutPat sty (ConPat name ty [])
171 = ppBeside (ppr sty name)
172 (ifPprShowAll sty (pprConPatTy sty ty))
174 pprOutPat sty (ConPat name ty pats)
175 = ppBesides [ppLparen, ppr sty name, ppSP,
176 interppSP sty pats, ppRparen,
177 ifPprShowAll sty (pprConPatTy sty ty) ]
179 pprOutPat sty (ConOpPat pat1 op pat2 ty)
180 = ppBesides [ppLparen, ppr sty pat1, ppSP, pprOp sty op, ppSP, ppr sty pat2, ppRparen]
182 pprOutPat sty (ListPat ty pats)
183 = ppBesides [ppLbrack, interpp'SP sty pats, ppRbrack]
184 pprOutPat sty (TuplePat pats)
185 = ppBesides [ppLparen, interpp'SP sty pats, ppRparen]
187 pprOutPat sty (RecPat con ty rpats)
188 = ppBesides [ppr sty con, ppChar '{', ppInterleave ppComma (map pp_rpat rpats), ppChar '}']
190 -- pp_rpat (v, _, True{-pun-}) = ppr sty v
191 pp_rpat (v, p, _) = ppBesides [ppr sty v, ppStr "<-", ppr sty p]
193 pprOutPat sty (LitPat l ty) = ppr sty l -- ToDo: print more
194 pprOutPat sty (NPat l ty e) = ppr sty l -- ToDo: print more
196 pprOutPat sty (DictPat dicts methods)
197 = ppSep [ppBesides [ppLparen, ppPStr SLIT("{-dict-}")],
198 ppBracket (interpp'SP sty dicts),
199 ppBesides [ppBracket (interpp'SP sty methods), ppRparen]]
202 = ppParens (ppr sty ty)
205 %************************************************************************
207 %* predicates for checking things about pattern-lists in EquationInfo *
209 %************************************************************************
210 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
212 Unlike in the Wadler chapter, where patterns are either ``variables''
213 or ``constructors,'' here we distinguish between:
216 Patterns that cannot fail to match: variables, wildcards, and lazy
219 These are the irrefutable patterns; the two other categories
220 are refutable patterns.
223 A non-literal constructor pattern (see next category).
225 \item[literal patterns:]
226 At least the numeric ones may be overloaded.
229 A pattern is in {\em exactly one} of the above three categories; `as'
230 patterns are treated specially, of course.
233 unfailablePats :: [OutPat a b c] -> Bool
234 unfailablePats pat_list = all unfailablePat pat_list
236 unfailablePat (AsPat _ pat) = unfailablePat pat
237 unfailablePat (WildPat _) = True
238 unfailablePat (VarPat _) = True
239 unfailablePat (LazyPat _) = True
240 unfailablePat (DictPat ds ms) = (length ds + length ms) <= 1
241 unfailablePat other = False
243 patsAreAllCons :: [OutPat a b c] -> Bool
244 patsAreAllCons pat_list = all isConPat pat_list
246 isConPat (AsPat _ pat) = isConPat pat
247 isConPat (ConPat _ _ _) = True
248 isConPat (ConOpPat _ _ _ _) = True
249 isConPat (ListPat _ _) = True
250 isConPat (TuplePat _) = True
251 isConPat (DictPat ds ms) = (length ds + length ms) > 1
252 isConPat other = False
254 patsAreAllLits :: [OutPat a b c] -> Bool
255 patsAreAllLits pat_list = all isLitPat pat_list
257 isLitPat (AsPat _ pat) = isLitPat pat
258 isLitPat (LitPat _ _) = True
259 isLitPat (NPat _ _ _) = True
260 isLitPat other = False
263 A pattern is irrefutable if a match on it cannot fail
266 irrefutablePat :: OutPat a b c -> Bool
268 irrefutablePat (WildPat _) = True
269 irrefutablePat (VarPat _) = True
270 irrefutablePat (LazyPat _) = True
271 irrefutablePat (AsPat _ pat) = irrefutablePat pat
272 irrefutablePat (ConPat con tys pats) = all irrefutablePat pats && only_con con
273 irrefutablePat (ConOpPat pat1 con pat2 _) = irrefutablePat pat1 && irrefutablePat pat1 && only_con con
274 irrefutablePat (ListPat _ _) = False
275 irrefutablePat (TuplePat pats) = all irrefutablePat pats
276 irrefutablePat (DictPat _ _) = True
277 irrefutablePat other_pat = False -- Literals, NPat
279 only_con con = maybeToBool (maybeTyConSingleCon tycon)
281 (_,_,_,tycon) = dataConSig con
284 This function @collectPatBinders@ works with the ``collectBinders''
285 functions for @HsBinds@, etc. The order in which the binders are
286 collected is important; see @HsBinds.lhs@.
288 collectPatBinders :: InPat a -> [a]
290 collectPatBinders (VarPatIn var) = [var]
291 collectPatBinders (LazyPatIn pat) = collectPatBinders pat
292 collectPatBinders (AsPatIn a pat) = a : collectPatBinders pat
293 collectPatBinders (ConPatIn c pats) = concat (map collectPatBinders pats)
294 collectPatBinders (ConOpPatIn p1 c p2)= collectPatBinders p1 ++ collectPatBinders p2
295 collectPatBinders (ListPatIn pats) = concat (map collectPatBinders pats)
296 collectPatBinders (TuplePatIn pats) = concat (map collectPatBinders pats)
297 collectPatBinders any_other_pat = [ {-no binders-} ]