2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[PatSyntax]{Abstract Haskell syntax---patterns}
8 Pat(..), InPat, OutPat,
10 HsConDetails(..), hsConArgs,
12 mkPrefixConPat, mkCharLitPat, mkNilPat,
15 patsAreAllCons, isConPat, isSigPat,
16 patsAreAllLits, isLitPat,
17 collectPatBinders, collectPatsBinders,
18 collectSigTysFromPat, collectSigTysFromPats
21 #include "HsVersions.h"
24 import {-# SOURCE #-} HsExpr ( HsExpr )
27 import HsLit ( HsLit(HsCharPrim), HsOverLit )
28 import HsTypes ( HsType, SyntaxName, PostTcType )
29 import BasicTypes ( Boxity, tupleParens )
31 import TysWiredIn ( nilDataCon, charDataCon, charTy )
33 import DataCon ( DataCon )
40 type InPat id = Pat id -- No 'Out' constructors
41 type OutPat id = Pat id -- No 'In' constructors
44 = ------------ Simple patterns ---------------
45 WildPat PostTcType -- Wild card
46 | VarPat id -- Variable
47 | LazyPat (Pat id) -- Lazy pattern
48 | AsPat id (Pat id) -- As pattern
49 | ParPat (Pat id) -- Parenthesised pattern
51 ------------ Lists, tuples, arrays ---------------
52 | ListPat [Pat id] -- Syntactic list
53 PostTcType -- The type of the elements
55 | TuplePat [Pat id] -- Tuple
56 Boxity -- UnitPat is TuplePat []
58 | PArrPat [Pat id] -- Syntactic parallel array
59 PostTcType -- The type of the elements
61 ------------ Constructor patterns ---------------
63 (HsConDetails id (Pat id))
66 (HsConDetails id (Pat id))
67 Type -- The type of the pattern
68 [TyVar] -- Existentially bound type variables
69 [id] -- Ditto dictionaries
71 ------------ Literal and n+k patterns ---------------
72 | LitPat HsLit -- Used for *non-overloaded* literal patterns:
73 -- Int#, Char#, Int, Char, String, etc.
75 | NPatIn HsOverLit -- Always positive
76 (Maybe SyntaxName) -- Just (Name of 'negate') for negative
77 -- patterns, Nothing otherwise
79 | NPatOut HsLit -- Used for literal patterns where there's an equality function to call
80 -- The literal is retained so that the desugarer can readily identify
81 -- equations with identical literal-patterns
82 -- Always HsInteger, HsRat or HsString.
83 -- Always HsInteger, HsRat or HsString.
84 -- *Unlike* NPatIn, for negative literals, the
85 -- literal is acutally negative!
86 Type -- Type of pattern, t
87 (HsExpr id) -- Of type t -> Bool; detects match
89 | NPlusKPatIn id -- n+k pattern
90 HsOverLit -- It'll always be an HsIntegral
91 SyntaxName -- Name of '-' (see RnEnv.lookupSyntaxName)
95 (HsExpr id) -- Of type t -> Bool; detects match
96 (HsExpr id) -- Of type t -> t; subtracts k
99 ------------ Generics ---------------
100 | TypePat (HsType id) -- Type pattern for generic definitions
101 -- e.g f{| a+b |} = ...
102 -- These show up only in class declarations,
103 -- and should be a top-level pattern
105 ------------ Pattern type signatures ---------------
106 | SigPatIn (Pat id) -- Pattern with a type signature
109 | SigPatOut (Pat id) -- Pattern p
110 Type -- Type, t, of the whole pattern
111 (HsExpr id) -- Coercion function,
112 -- of type t -> typeof(p)
114 ------------ Dictionary patterns (translation only) ---------------
115 | DictPat -- Used when destructing Dictionaries with an explicit case
116 [id] -- superclass dicts
120 HsConDetails is use both for patterns and for data type declarations
123 data HsConDetails id arg
124 = PrefixCon [arg] -- C p1 p2 p3
125 | RecCon [(id, arg)] -- C { x = p1, y = p2 }
126 | InfixCon arg arg -- p1 `C` p2
128 hsConArgs :: HsConDetails id arg -> [arg]
129 hsConArgs (PrefixCon ps) = ps
130 hsConArgs (RecCon fs) = map snd fs
131 hsConArgs (InfixCon p1 p2) = [p1,p2]
135 %************************************************************************
139 %************************************************************************
142 instance (OutputableBndr name) => Outputable (Pat name) where
145 pprPat :: (OutputableBndr name) => Pat name -> SDoc
147 pprPat (VarPat var) -- Print with type info if -dppr-debug is on
148 = getPprStyle $ \ sty ->
149 if debugStyle sty then
150 parens (pprBndr LambdaBind var) -- Could pass the site to pprPat
151 -- but is it worth it?
155 pprPat (WildPat _) = char '_'
156 pprPat (LazyPat pat) = char '~' <> ppr pat
157 pprPat (AsPat name pat) = parens (hcat [ppr name, char '@', ppr pat])
158 pprPat (ParPat pat) = parens (pprPat pat)
160 pprPat (ListPat pats _) = brackets (interpp'SP pats)
161 pprPat (PArrPat pats _) = pabrackets (interpp'SP pats)
162 pprPat (TuplePat pats bx) = tupleParens bx (interpp'SP pats)
164 pprPat (ConPatIn c details) = pprConPat c details
165 pprPat (ConPatOut c details _ _ _) = pprConPat c details
167 pprPat (LitPat s) = ppr s
168 pprPat (NPatIn l _) = ppr l
169 pprPat (NPatOut l _ _) = ppr l
170 pprPat (NPlusKPatIn n k _) = hcat [ppr n, char '+', ppr k]
171 pprPat (NPlusKPatOut n k _ _) = hcat [ppr n, char '+', integer k]
173 pprPat (TypePat ty) = ptext SLIT("{|") <> ppr ty <> ptext SLIT("|}")
175 pprPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
176 pprPat (SigPatOut pat ty _) = ppr pat <+> dcolon <+> ppr ty
178 pprPat (DictPat dicts methods)
179 = parens (sep [ptext SLIT("{-dict-}"),
180 brackets (interpp'SP dicts),
181 brackets (interpp'SP methods)])
185 pprConPat con (PrefixCon pats) = ppr con <+> interppSP pats -- inner ParPats supply the necessary parens.
186 pprConPat con (InfixCon pat1 pat2) = hsep [ppr pat1, ppr con, ppr pat2] -- ParPats put in parens
187 -- ToDo: use pprSym to print op (but this involves fiddling various
188 -- contexts & I'm lazy...); *PatIns are *rarely* printed anyway... (WDP)
189 pprConPat con (RecCon rpats)
190 = ppr con <+> braces (hsep (punctuate comma (map (pp_rpat) rpats)))
192 pp_rpat (v, p) = hsep [ppr v, char '=', ppr p]
195 -- add parallel array brackets around a document
197 pabrackets :: SDoc -> SDoc
198 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
202 %************************************************************************
206 %************************************************************************
209 mkPrefixConPat :: DataCon -> [OutPat id] -> Type -> OutPat id
210 -- Make a vanilla Prefix constructor pattern
211 mkPrefixConPat dc pats ty = ConPatOut dc (PrefixCon pats) ty [] []
213 mkNilPat :: Type -> OutPat id
214 mkNilPat ty = mkPrefixConPat nilDataCon [] ty
216 mkCharLitPat :: Int -> OutPat id
217 mkCharLitPat c = mkPrefixConPat charDataCon [LitPat (HsCharPrim c)] charTy
221 %************************************************************************
223 %* Predicates for checking things about pattern-lists in EquationInfo *
225 %************************************************************************
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 isWildPat (WildPat _) = True
252 isWildPat other = False
254 patsAreAllCons :: [Pat id] -> Bool
255 patsAreAllCons pat_list = all isConPat pat_list
257 isConPat (AsPat _ pat) = isConPat pat
258 isConPat (ConPatIn _ _) = True
259 isConPat (ConPatOut _ _ _ _ _) = True
260 isConPat (ListPat _ _) = True
261 isConPat (PArrPat _ _) = True
262 isConPat (TuplePat _ _) = True
263 isConPat (DictPat ds ms) = (length ds + length ms) > 1
264 isConPat other = False
266 isSigPat (SigPatIn _ _) = True
267 isSigPat (SigPatOut _ _ _) = True
268 isSigPat other = False
270 patsAreAllLits :: [Pat id] -> Bool
271 patsAreAllLits pat_list = all isLitPat pat_list
273 isLitPat (AsPat _ pat) = isLitPat pat
274 isLitPat (LitPat _) = True
275 isLitPat (NPatIn _ _) = True
276 isLitPat (NPatOut _ _ _) = True
277 isLitPat (NPlusKPatIn _ _ _) = True
278 isLitPat (NPlusKPatOut _ _ _ _) = True
279 isLitPat other = False
282 %************************************************************************
284 %* Gathering stuff out of patterns
286 %************************************************************************
288 This function @collectPatBinders@ works with the ``collectBinders''
289 functions for @HsBinds@, etc. The order in which the binders are
290 collected is important; see @HsBinds.lhs@.
292 It collects the bounds *value* variables in renamed patterns; type variables
296 collectPatBinders :: Pat a -> [a]
297 collectPatBinders pat = collect pat []
299 collectPatsBinders :: [Pat a] -> [a]
300 collectPatsBinders pats = foldr collect [] pats
302 collect (WildPat _) bndrs = bndrs
303 collect (VarPat var) bndrs = var : bndrs
304 collect (LazyPat pat) bndrs = collect pat bndrs
305 collect (AsPat a pat) bndrs = a : collect pat bndrs
306 collect (ParPat pat) bndrs = collect pat bndrs
308 collect (ListPat pats _) bndrs = foldr collect bndrs pats
309 collect (PArrPat pats _) bndrs = foldr collect bndrs pats
310 collect (TuplePat pats _) bndrs = foldr collect bndrs pats
312 collect (ConPatIn c ps) bndrs = foldr collect bndrs (hsConArgs ps)
313 collect (ConPatOut c ps _ _ ds) bndrs = ds ++ foldr collect bndrs (hsConArgs ps)
315 collect (LitPat _) bndrs = bndrs
316 collect (NPatIn _ _) bndrs = bndrs
317 collect (NPatOut _ _ _) bndrs = bndrs
319 collect (NPlusKPatIn n _ _) bndrs = n : bndrs
320 collect (NPlusKPatOut n _ _ _) bndrs = n : bndrs
322 collect (SigPatIn pat _) bndrs = collect pat bndrs
323 collect (SigPatOut pat _ _) bndrs = collect pat bndrs
324 collect (TypePat ty) bndrs = bndrs
325 collect (DictPat ids1 ids2) bndrs = ids1 ++ ids2 ++ bndrs
329 collectSigTysFromPats :: [InPat name] -> [HsType name]
330 collectSigTysFromPats pats = foldr collect_pat [] pats
332 collectSigTysFromPat :: InPat name -> [HsType name]
333 collectSigTysFromPat pat = collect_pat pat []
335 collect_pat (SigPatIn pat ty) acc = collect_pat pat (ty:acc)
336 collect_pat (TypePat ty) acc = ty:acc
338 collect_pat (LazyPat pat) acc = collect_pat pat acc
339 collect_pat (AsPat a pat) acc = collect_pat pat acc
340 collect_pat (ParPat pat) acc = collect_pat pat acc
341 collect_pat (ListPat pats _) acc = foldr collect_pat acc pats
342 collect_pat (PArrPat pats _) acc = foldr collect_pat acc pats
343 collect_pat (TuplePat pats _) acc = foldr collect_pat acc pats
344 collect_pat (ConPatIn c ps) acc = foldr collect_pat acc (hsConArgs ps)
345 collect_pat other acc = acc -- Literals, vars, wildcard