2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[PatSyntax]{Abstract Haskell syntax---patterns}
9 Pat(..), InPat, OutPat, LPat,
11 HsConDetails(..), hsConArgs,
12 HsRecField(..), mkRecField,
14 mkPrefixConPat, mkCharLitPat, mkNilPat, mkCoPat,
17 patsAreAllCons, isConPat, isSigPat, isWildPat,
18 patsAreAllLits, isLitPat, isIrrefutableHsPat
21 #include "HsVersions.h"
24 import {-# SOURCE #-} HsExpr ( SyntaxExpr )
33 import PprCore ( {- instance OutputableBndr TyVar -} )
45 type InPat id = LPat id -- No 'Out' constructors
46 type OutPat id = LPat id -- No 'In' constructors
48 type LPat id = Located (Pat id)
51 = ------------ Simple patterns ---------------
52 WildPat PostTcType -- Wild card
53 -- The sole reason for a type on a WildPat is to
54 -- support hsPatType :: Pat Id -> Type
56 | VarPat id -- Variable
57 | VarPatOut id (DictBinds id) -- Used only for overloaded Ids; the
58 -- bindings give its overloaded instances
59 | LazyPat (LPat id) -- Lazy pattern
60 | AsPat (Located id) (LPat id) -- As pattern
61 | ParPat (LPat id) -- Parenthesised pattern
62 | BangPat (LPat id) -- Bang patterng
64 ------------ Lists, tuples, arrays ---------------
65 | ListPat [LPat id] -- Syntactic list
66 PostTcType -- The type of the elements
68 | TuplePat [LPat id] -- Tuple
69 Boxity -- UnitPat is TuplePat []
71 -- You might think that the PostTcType was redundant, but it's essential
74 -- f :: (T a, a) -> Int
76 -- When desugaring, we must generate
77 -- f = /\a. \v::a. case v of (t::T a, w::a) ->
78 -- case t of (T1 (x::Int)) ->
79 -- Note the (w::a), NOT (w::Int), because we have not yet
80 -- refined 'a' to Int. So we must know that the second component
81 -- of the tuple is of type 'a' not Int. See selectMatchVar
83 | PArrPat [LPat id] -- Syntactic parallel array
84 PostTcType -- The type of the elements
86 ------------ Constructor patterns ---------------
87 | ConPatIn (Located id)
88 (HsConDetails id (LPat id))
91 pat_con :: Located DataCon,
92 pat_tvs :: [TyVar], -- Existentially bound type variables
93 -- including any bound coercion variables
94 pat_dicts :: [id], -- Ditto dictionaries
95 pat_binds :: DictBinds id, -- Bindings involving those dictionaries
96 pat_args :: HsConDetails id (LPat id),
97 pat_ty :: Type -- The type of the pattern
100 ------------ Literal and n+k patterns ---------------
101 | LitPat HsLit -- Used for *non-overloaded* literal patterns:
102 -- Int#, Char#, Int, Char, String, etc.
104 | NPat (HsOverLit id) -- ALWAYS positive
105 (Maybe (SyntaxExpr id)) -- Just (Name of 'negate') for negative
106 -- patterns, Nothing otherwise
107 (SyntaxExpr id) -- Equality checker, of type t->t->Bool
108 PostTcType -- Type of the pattern
110 | NPlusKPat (Located id) -- n+k pattern
111 (HsOverLit id) -- It'll always be an HsIntegral
112 (SyntaxExpr id) -- (>=) function, of type t->t->Bool
113 (SyntaxExpr id) -- Name of '-' (see RnEnv.lookupSyntaxName)
115 ------------ Generics ---------------
116 | TypePat (LHsType id) -- Type pattern for generic definitions
117 -- e.g f{| a+b |} = ...
118 -- These show up only in class declarations,
119 -- and should be a top-level pattern
121 ------------ Pattern type signatures ---------------
122 | SigPatIn (LPat id) -- Pattern with a type signature
125 | SigPatOut (LPat id) -- Pattern with a type signature
128 ------------ Pattern coercions (translation only) ---------------
129 | CoPat HsWrapper -- If co::t1 -> t2, p::t2,
130 -- then (CoPat co p) :: t1
131 (Pat id) -- Why not LPat? Ans: existing locn will do
132 Type -- Type of whole pattern, t1
133 -- During desugaring a (CoPat co pat) turns into a cast with 'co' on
134 -- the scrutinee, followed by a match on 'pat'
137 HsConDetails is use both for patterns and for data type declarations
140 data HsConDetails id arg
141 = PrefixCon [arg] -- C p1 p2 p3
142 | RecCon [HsRecField id arg] -- C { x = p1, y = p2 }
143 | InfixCon arg arg -- p1 `C` p2
145 data HsRecField id arg = HsRecField {
146 hsRecFieldId :: Located id,
147 hsRecFieldArg :: arg,
148 hsRecFieldDoc :: Maybe (LHsDoc id)
151 mkRecField id arg = HsRecField id arg Nothing
153 hsConArgs :: HsConDetails id arg -> [arg]
154 hsConArgs (PrefixCon ps) = ps
155 hsConArgs (RecCon fs) = map hsRecFieldArg fs
156 hsConArgs (InfixCon p1 p2) = [p1,p2]
160 %************************************************************************
164 %************************************************************************
167 instance (OutputableBndr name) => Outputable (Pat name) where
170 pprPatBndr :: OutputableBndr name => name -> SDoc
171 pprPatBndr var -- Print with type info if -dppr-debug is on
172 = getPprStyle $ \ sty ->
173 if debugStyle sty then
174 parens (pprBndr LambdaBind var) -- Could pass the site to pprPat
175 -- but is it worth it?
179 pprPat :: (OutputableBndr name) => Pat name -> SDoc
180 pprPat (VarPat var) = pprPatBndr var
181 pprPat (VarPatOut var bs) = parens (pprPatBndr var <+> braces (ppr bs))
182 pprPat (WildPat _) = char '_'
183 pprPat (LazyPat pat) = char '~' <> ppr pat
184 pprPat (BangPat pat) = char '!' <> ppr pat
185 pprPat (AsPat name pat) = parens (hcat [ppr name, char '@', ppr pat])
186 pprPat (ParPat pat) = parens (ppr pat)
187 pprPat (ListPat pats _) = brackets (interpp'SP pats)
188 pprPat (PArrPat pats _) = pabrackets (interpp'SP pats)
189 pprPat (TuplePat pats bx _) = tupleParens bx (interpp'SP pats)
191 pprPat (ConPatIn con details) = pprUserCon con details
192 pprPat (ConPatOut { pat_con = con, pat_tvs = tvs, pat_dicts = dicts,
193 pat_binds = binds, pat_args = details })
194 = getPprStyle $ \ sty -> -- Tiresome; in TcBinds.tcRhs we print out a
195 if debugStyle sty then -- typechecked Pat in an error message,
196 -- and we want to make sure it prints nicely
197 ppr con <+> sep [ hsep (map pprPatBndr tvs) <+> hsep (map pprPatBndr dicts),
198 pprLHsBinds binds, pprConArgs details]
199 else pprUserCon con details
201 pprPat (LitPat s) = ppr s
202 pprPat (NPat l Nothing _ _) = ppr l
203 pprPat (NPat l (Just _) _ _) = char '-' <> ppr l
204 pprPat (NPlusKPat n k _ _) = hcat [ppr n, char '+', ppr k]
205 pprPat (TypePat ty) = ptext SLIT("{|") <> ppr ty <> ptext SLIT("|}")
206 pprPat (CoPat co pat _) = parens (pprHsWrapper (ppr pat) co)
207 pprPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
208 pprPat (SigPatOut pat ty) = ppr pat <+> dcolon <+> ppr ty
210 pprUserCon c (InfixCon p1 p2) = ppr p1 <+> ppr c <+> ppr p2
211 pprUserCon c details = ppr c <+> pprConArgs details
213 pprConArgs (PrefixCon pats) = interppSP pats
214 pprConArgs (InfixCon p1 p2) = interppSP [p1,p2]
215 pprConArgs (RecCon rpats) = braces (hsep (punctuate comma (map (pp_rpat) rpats)))
217 pp_rpat (HsRecField v p _d) =
218 hsep [ppr v, char '=', ppr p]
220 -- add parallel array brackets around a document
222 pabrackets :: SDoc -> SDoc
223 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
225 instance (OutputableBndr id, Outputable arg) =>
226 Outputable (HsRecField id arg) where
227 ppr (HsRecField n ty doc) = ppr n <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
231 %************************************************************************
235 %************************************************************************
238 mkPrefixConPat :: DataCon -> [OutPat id] -> Type -> OutPat id
239 -- Make a vanilla Prefix constructor pattern
240 mkPrefixConPat dc pats ty
241 = noLoc $ ConPatOut { pat_con = noLoc dc, pat_tvs = [], pat_dicts = [],
242 pat_binds = emptyLHsBinds, pat_args = PrefixCon pats,
245 mkNilPat :: Type -> OutPat id
246 mkNilPat ty = mkPrefixConPat nilDataCon [] ty
248 mkCharLitPat :: Char -> OutPat id
249 mkCharLitPat c = mkPrefixConPat charDataCon [noLoc $ LitPat (HsCharPrim c)] charTy
251 mkCoPat :: HsWrapper -> OutPat id -> Type -> OutPat id
252 mkCoPat co lpat@(L loc pat) ty
253 | isIdHsWrapper co = lpat
254 | otherwise = L loc (CoPat co pat ty)
258 %************************************************************************
260 %* Predicates for checking things about pattern-lists in EquationInfo *
262 %************************************************************************
264 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
266 Unlike in the Wadler chapter, where patterns are either ``variables''
267 or ``constructors,'' here we distinguish between:
270 Patterns that cannot fail to match: variables, wildcards, and lazy
273 These are the irrefutable patterns; the two other categories
274 are refutable patterns.
277 A non-literal constructor pattern (see next category).
279 \item[literal patterns:]
280 At least the numeric ones may be overloaded.
283 A pattern is in {\em exactly one} of the above three categories; `as'
284 patterns are treated specially, of course.
286 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
288 isWildPat (WildPat _) = True
289 isWildPat other = False
291 patsAreAllCons :: [Pat id] -> Bool
292 patsAreAllCons pat_list = all isConPat pat_list
294 isConPat (AsPat _ pat) = isConPat (unLoc pat)
295 isConPat (ConPatIn {}) = True
296 isConPat (ConPatOut {}) = True
297 isConPat (ListPat {}) = True
298 isConPat (PArrPat {}) = True
299 isConPat (TuplePat {}) = True
300 isConPat other = False
302 isSigPat (SigPatIn _ _) = True
303 isSigPat (SigPatOut _ _) = True
304 isSigPat other = False
306 patsAreAllLits :: [Pat id] -> Bool
307 patsAreAllLits pat_list = all isLitPat pat_list
309 isLitPat (AsPat _ pat) = isLitPat (unLoc pat)
310 isLitPat (LitPat _) = True
311 isLitPat (NPat _ _ _ _) = True
312 isLitPat (NPlusKPat _ _ _ _) = True
313 isLitPat other = False
315 isBangHsBind :: HsBind id -> Bool
316 -- In this module because HsPat is above HsBinds in the import graph
317 isBangHsBind (PatBind { pat_lhs = L _ (BangPat p) }) = True
318 isBangHsBind bind = False
320 isIrrefutableHsPat :: LPat id -> Bool
321 -- This function returns False if it's in doubt; specifically
322 -- on a ConPatIn it doesn't know the size of the constructor family
323 -- But if it returns True, the pattern is definitely irrefutable
324 isIrrefutableHsPat pat
327 go (L _ pat) = go1 pat
329 go1 (WildPat _) = True
330 go1 (VarPat _) = True
331 go1 (VarPatOut _ _) = True
332 go1 (LazyPat pat) = True
333 go1 (BangPat pat) = go pat
334 go1 (CoPat _ pat _) = go1 pat
335 go1 (ParPat pat) = go pat
336 go1 (AsPat _ pat) = go pat
337 go1 (SigPatIn pat _) = go pat
338 go1 (SigPatOut pat _) = go pat
339 go1 (TuplePat pats _ _) = all go pats
340 go1 (ListPat pats _) = False
341 go1 (PArrPat pats _) = False -- ?
343 go1 (ConPatIn _ _) = False -- Conservative
344 go1 (ConPatOut{ pat_con = L _ con, pat_args = details })
345 = isProductTyCon (dataConTyCon con)
346 && all go (hsConArgs details)
348 go1 (LitPat _) = False
349 go1 (NPat _ _ _ _) = False
350 go1 (NPlusKPat _ _ _ _) = False
352 go1 (TypePat _) = panic "isIrrefutableHsPat: type pattern"