Add the rest of the notes for Located RdrName
[ghc.git] / compiler / hsSyn / HsPat.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5 \section[PatSyntax]{Abstract Haskell syntax---patterns}
6 -}
7
8 {-# LANGUAGE DeriveDataTypeable #-}
9 {-# LANGUAGE DeriveDataTypeable #-}
10 {-# LANGUAGE StandaloneDeriving #-}
11 {-# LANGUAGE FlexibleContexts #-}
12 {-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
13 -- in module PlaceHolder
14 {-# LANGUAGE ConstraintKinds #-}
15 {-# LANGUAGE TypeFamilies #-}
16
17 module HsPat (
18 Pat(..), InPat, OutPat, LPat,
19
20 HsConDetails(..),
21 HsConPatDetails, hsConPatArgs,
22 HsRecFields(..), HsRecField'(..), LHsRecField',
23 HsRecField, LHsRecField,
24 HsRecUpdField, LHsRecUpdField,
25 hsRecFields, hsRecFieldSel, hsRecFieldId,
26 hsRecUpdFieldId, hsRecUpdFieldOcc, hsRecUpdFieldRdr,
27
28 mkPrefixConPat, mkCharLitPat, mkNilPat,
29
30 isUnliftedHsBind, looksLazyPatBind,
31 isUnliftedLPat, isBangedLPat, isBangedPatBind,
32 hsPatNeedsParens,
33 isIrrefutableHsPat,
34
35 pprParendLPat, pprConArgs
36 ) where
37
38 import {-# SOURCE #-} HsExpr (SyntaxExpr, LHsExpr, HsSplice, pprLExpr, pprSplice)
39
40 -- friends:
41 import HsBinds
42 import HsLit
43 import PlaceHolder -- ( PostRn,PostTc,DataId )
44 import HsTypes
45 import TcEvidence
46 import BasicTypes
47 -- others:
48 import PprCore ( {- instance OutputableBndr TyVar -} )
49 import TysWiredIn
50 import Var
51 import RdrName ( RdrName )
52 import ConLike
53 import DataCon
54 import TyCon
55 import Outputable
56 import Type
57 import SrcLoc
58 import FastString
59 import Maybes
60 -- libraries:
61 import Data.Data hiding (TyCon,Fixity)
62
63 type InPat id = LPat id -- No 'Out' constructors
64 type OutPat id = LPat id -- No 'In' constructors
65
66 type LPat id = Located (Pat id)
67
68 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnBang'
69
70 -- For details on above see note [Api annotations] in ApiAnnotation
71 data Pat id
72 = ------------ Simple patterns ---------------
73 WildPat (PostTc id Type) -- Wild card
74 -- The sole reason for a type on a WildPat is to
75 -- support hsPatType :: Pat Id -> Type
76
77 | VarPat (Located id) -- Variable
78 -- See Note [Located RdrNames] in HsExpr
79 | LazyPat (LPat id) -- Lazy pattern
80 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
81
82 -- For details on above see note [Api annotations] in ApiAnnotation
83
84 | AsPat (Located id) (LPat id) -- As pattern
85 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnAt'
86
87 -- For details on above see note [Api annotations] in ApiAnnotation
88
89 | ParPat (LPat id) -- Parenthesised pattern
90 -- See Note [Parens in HsSyn] in HsExpr
91 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
92 -- 'ApiAnnotation.AnnClose' @')'@
93
94 -- For details on above see note [Api annotations] in ApiAnnotation
95 | BangPat (LPat id) -- Bang pattern
96 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnBang'
97
98 -- For details on above see note [Api annotations] in ApiAnnotation
99
100 ------------ Lists, tuples, arrays ---------------
101 | ListPat [LPat id] -- Syntactic list
102 (PostTc id Type) -- The type of the elements
103 (Maybe (PostTc id Type, SyntaxExpr id)) -- For rebindable syntax
104 -- For OverloadedLists a Just (ty,fn) gives
105 -- overall type of the pattern, and the toList
106 -- function to convert the scrutinee to a list value
107 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
108 -- 'ApiAnnotation.AnnClose' @']'@
109
110 -- For details on above see note [Api annotations] in ApiAnnotation
111
112 | TuplePat [LPat id] -- Tuple sub-patterns
113 Boxity -- UnitPat is TuplePat []
114 [PostTc id Type] -- [] before typechecker, filled in afterwards
115 -- with the types of the tuple components
116 -- You might think that the PostTc id Type was redundant, because we can
117 -- get the pattern type by getting the types of the sub-patterns.
118 -- But it's essential
119 -- data T a where
120 -- T1 :: Int -> T Int
121 -- f :: (T a, a) -> Int
122 -- f (T1 x, z) = z
123 -- When desugaring, we must generate
124 -- f = /\a. \v::a. case v of (t::T a, w::a) ->
125 -- case t of (T1 (x::Int)) ->
126 -- Note the (w::a), NOT (w::Int), because we have not yet
127 -- refined 'a' to Int. So we must know that the second component
128 -- of the tuple is of type 'a' not Int. See selectMatchVar
129 -- (June 14: I'm not sure this comment is right; the sub-patterns
130 -- will be wrapped in CoPats, no?)
131 -- ^ - 'ApiAnnotation.AnnKeywordId' :
132 -- 'ApiAnnotation.AnnOpen' @'('@ or @'(#'@,
133 -- 'ApiAnnotation.AnnClose' @')'@ or @'#)'@
134
135 -- For details on above see note [Api annotations] in ApiAnnotation
136 | PArrPat [LPat id] -- Syntactic parallel array
137 (PostTc id Type) -- The type of the elements
138 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
139 -- 'ApiAnnotation.AnnClose' @':]'@
140
141 -- For details on above see note [Api annotations] in ApiAnnotation
142 ------------ Constructor patterns ---------------
143 | ConPatIn (Located id)
144 (HsConPatDetails id)
145
146 | ConPatOut {
147 pat_con :: Located ConLike,
148 pat_arg_tys :: [Type], -- The universal arg types, 1-1 with the universal
149 -- tyvars of the constructor/pattern synonym
150 -- Use (conLikeResTy pat_con pat_arg_tys) to get
151 -- the type of the pattern
152
153 pat_tvs :: [TyVar], -- Existentially bound type variables (tyvars only)
154 pat_dicts :: [EvVar], -- Ditto *coercion variables* and *dictionaries*
155 -- One reason for putting coercion variable here, I think,
156 -- is to ensure their kinds are zonked
157 pat_binds :: TcEvBinds, -- Bindings involving those dictionaries
158 pat_args :: HsConPatDetails id,
159 pat_wrap :: HsWrapper -- Extra wrapper to pass to the matcher
160 }
161
162 ------------ View patterns ---------------
163 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow'
164
165 -- For details on above see note [Api annotations] in ApiAnnotation
166 | ViewPat (LHsExpr id)
167 (LPat id)
168 (PostTc id Type) -- The overall type of the pattern
169 -- (= the argument type of the view function)
170 -- for hsPatType.
171
172 ------------ Pattern splices ---------------
173 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@
174 -- 'ApiAnnotation.AnnClose' @')'@
175
176 -- For details on above see note [Api annotations] in ApiAnnotation
177 | SplicePat (HsSplice id) -- Includes quasi-quotes
178
179 ------------ Literal and n+k patterns ---------------
180 | LitPat HsLit -- Used for *non-overloaded* literal patterns:
181 -- Int#, Char#, Int, Char, String, etc.
182
183 | NPat -- Used for all overloaded literals,
184 -- including overloaded strings with -XOverloadedStrings
185 (Located (HsOverLit id)) -- ALWAYS positive
186 (Maybe (SyntaxExpr id)) -- Just (Name of 'negate') for negative
187 -- patterns, Nothing otherwise
188 (SyntaxExpr id) -- Equality checker, of type t->t->Bool
189
190 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnVal' @'+'@
191
192 -- For details on above see note [Api annotations] in ApiAnnotation
193 | NPlusKPat (Located id) -- n+k pattern
194 (Located (HsOverLit id)) -- It'll always be an HsIntegral
195 (SyntaxExpr id) -- (>=) function, of type t->t->Bool
196 (SyntaxExpr id) -- Name of '-' (see RnEnv.lookupSyntaxName)
197
198 ------------ Pattern type signatures ---------------
199 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
200
201 -- For details on above see note [Api annotations] in ApiAnnotation
202 | SigPatIn (LPat id) -- Pattern with a type signature
203 (HsWithBndrs id (LHsType id)) -- Signature can bind both
204 -- kind and type vars
205
206 | SigPatOut (LPat id) -- Pattern with a type signature
207 Type
208
209 ------------ Pattern coercions (translation only) ---------------
210 | CoPat HsWrapper -- If co :: t1 ~ t2, p :: t2,
211 -- then (CoPat co p) :: t1
212 (Pat id) -- Why not LPat? Ans: existing locn will do
213 Type -- Type of whole pattern, t1
214 -- During desugaring a (CoPat co pat) turns into a cast with 'co' on
215 -- the scrutinee, followed by a match on 'pat'
216 deriving (Typeable)
217 deriving instance (DataId id) => Data (Pat id)
218
219 -- HsConDetails is use for patterns/expressions *and* for data type declarations
220
221 data HsConDetails arg rec
222 = PrefixCon [arg] -- C p1 p2 p3
223 | RecCon rec -- C { x = p1, y = p2 }
224 | InfixCon arg arg -- p1 `C` p2
225 deriving (Data, Typeable)
226
227 type HsConPatDetails id = HsConDetails (LPat id) (HsRecFields id (LPat id))
228
229 hsConPatArgs :: HsConPatDetails id -> [LPat id]
230 hsConPatArgs (PrefixCon ps) = ps
231 hsConPatArgs (RecCon fs) = map (hsRecFieldArg . unLoc) (rec_flds fs)
232 hsConPatArgs (InfixCon p1 p2) = [p1,p2]
233
234 {-
235 However HsRecFields is used only for patterns and expressions
236 (not data type declarations)
237 -}
238
239 data HsRecFields id arg -- A bunch of record fields
240 -- { x = 3, y = True }
241 -- Used for both expressions and patterns
242 = HsRecFields { rec_flds :: [LHsRecField id arg],
243 rec_dotdot :: Maybe Int } -- Note [DotDot fields]
244 deriving (Typeable)
245 deriving instance (DataId id, Data arg) => Data (HsRecFields id arg)
246
247 -- Note [DotDot fields]
248 -- ~~~~~~~~~~~~~~~~~~~~
249 -- The rec_dotdot field means this:
250 -- Nothing => the normal case
251 -- Just n => the group uses ".." notation,
252 --
253 -- In the latter case:
254 --
255 -- *before* renamer: rec_flds are exactly the n user-written fields
256 --
257 -- *after* renamer: rec_flds includes *all* fields, with
258 -- the first 'n' being the user-written ones
259 -- and the remainder being 'filled in' implicitly
260
261 type LHsRecField' id arg = Located (HsRecField' id arg)
262 type LHsRecField id arg = Located (HsRecField id arg)
263 type LHsRecUpdField id = Located (HsRecUpdField id)
264
265 type HsRecField id arg = HsRecField' (FieldOcc id) arg
266 type HsRecUpdField id = HsRecField' (AmbiguousFieldOcc id) (LHsExpr id)
267
268 -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnEqual',
269 --
270 -- For details on above see note [Api annotations] in ApiAnnotation
271 data HsRecField' id arg = HsRecField {
272 hsRecFieldLbl :: Located id,
273 hsRecFieldArg :: arg, -- ^ Filled in by renamer when punning
274 hsRecPun :: Bool -- ^ Note [Punning]
275 } deriving (Data, Typeable)
276
277
278 -- Note [Punning]
279 -- ~~~~~~~~~~~~~~
280 -- If you write T { x, y = v+1 }, the HsRecFields will be
281 -- HsRecField x x True ...
282 -- HsRecField y (v+1) False ...
283 -- That is, for "punned" field x is expanded (in the renamer)
284 -- to x=x; but with a punning flag so we can detect it later
285 -- (e.g. when pretty printing)
286 --
287 -- If the original field was qualified, we un-qualify it, thus
288 -- T { A.x } means T { A.x = x }
289
290
291 -- Note [HsRecField and HsRecUpdField]
292 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
293
294 -- A HsRecField (used for record construction and pattern matching)
295 -- contains an unambiguous occurrence of a field (i.e. a FieldOcc).
296 -- We can't just store the Name, because thanks to
297 -- DuplicateRecordFields this may not correspond to the label the user
298 -- wrote.
299 --
300 -- A HsRecUpdField (used for record update) contains a potentially
301 -- ambiguous occurrence of a field (an AmbiguousFieldOcc). The
302 -- renamer will fill in the selector function if it can, but if the
303 -- selector is ambiguous the renamer will defer to the typechecker.
304 -- After the typechecker, a unique selector will have been determined.
305 --
306 -- The renamer produces an Unambiguous result if it can, rather than
307 -- just doing the lookup in the typechecker, so that completely
308 -- unambiguous updates can be represented by 'DsMeta.repUpdFields'.
309 --
310 -- For example, suppose we have:
311 --
312 -- data S = MkS { x :: Int }
313 -- data T = MkT { x :: Int }
314 --
315 -- f z = (z { x = 3 }) :: S
316 --
317 -- The parsed HsRecUpdField corresponding to the record update will have:
318 --
319 -- hsRecFieldLbl = Unambiguous "x" PlaceHolder :: AmbiguousFieldOcc RdrName
320 --
321 -- After the renamer, this will become:
322 --
323 -- hsRecFieldLbl = Ambiguous "x" PlaceHolder :: AmbiguousFieldOcc Name
324 --
325 -- (note that the Unambiguous constructor is not type-correct here).
326 -- The typechecker will determine the particular selector:
327 --
328 -- hsRecFieldLbl = Unambiguous "x" $sel:x:MkS :: AmbiguousFieldOcc Id
329 --
330 -- See also Note [Disambiguating record fields] in TcExpr.
331
332 hsRecFields :: HsRecFields id arg -> [PostRn id id]
333 hsRecFields rbinds = map (unLoc . hsRecFieldSel . unLoc) (rec_flds rbinds)
334
335 hsRecFieldSel :: HsRecField name arg -> Located (PostRn name name)
336 hsRecFieldSel = fmap selectorFieldOcc . hsRecFieldLbl
337
338 hsRecFieldId :: HsRecField Id arg -> Located Id
339 hsRecFieldId = hsRecFieldSel
340
341 hsRecUpdFieldRdr :: HsRecUpdField id -> Located RdrName
342 hsRecUpdFieldRdr = fmap rdrNameAmbiguousFieldOcc . hsRecFieldLbl
343
344 hsRecUpdFieldId :: HsRecField' (AmbiguousFieldOcc Id) arg -> Located Id
345 hsRecUpdFieldId = fmap selectorFieldOcc . hsRecUpdFieldOcc
346
347 hsRecUpdFieldOcc :: HsRecField' (AmbiguousFieldOcc Id) arg -> LFieldOcc Id
348 hsRecUpdFieldOcc = fmap unambiguousFieldOcc . hsRecFieldLbl
349
350
351 {-
352 ************************************************************************
353 * *
354 * Printing patterns
355 * *
356 ************************************************************************
357 -}
358
359 instance (OutputableBndr name) => Outputable (Pat name) where
360 ppr = pprPat
361
362 pprPatBndr :: OutputableBndr name => name -> SDoc
363 pprPatBndr var -- Print with type info if -dppr-debug is on
364 = getPprStyle $ \ sty ->
365 if debugStyle sty then
366 parens (pprBndr LambdaBind var) -- Could pass the site to pprPat
367 -- but is it worth it?
368 else
369 pprPrefixOcc var
370
371 pprParendLPat :: (OutputableBndr name) => LPat name -> SDoc
372 pprParendLPat (L _ p) = pprParendPat p
373
374 pprParendPat :: (OutputableBndr name) => Pat name -> SDoc
375 pprParendPat p = getPprStyle $ \ sty ->
376 if need_parens sty p
377 then parens (pprPat p)
378 else pprPat p
379 where
380 need_parens sty p
381 | CoPat {} <- p -- In debug style we print the cast
382 , debugStyle sty = True -- (see pprHsWrapper) so parens are needed
383 | otherwise = hsPatNeedsParens p
384 -- But otherwise the CoPat is discarded, so it
385 -- is the pattern inside that matters. Sigh.
386
387 pprPat :: (OutputableBndr name) => Pat name -> SDoc
388 pprPat (VarPat (L _ var)) = pprPatBndr var
389 pprPat (WildPat _) = char '_'
390 pprPat (LazyPat pat) = char '~' <> pprParendLPat pat
391 pprPat (BangPat pat) = char '!' <> pprParendLPat pat
392 pprPat (AsPat name pat) = hcat [pprPrefixOcc (unLoc name), char '@', pprParendLPat pat]
393 pprPat (ViewPat expr pat _) = hcat [pprLExpr expr, text " -> ", ppr pat]
394 pprPat (ParPat pat) = parens (ppr pat)
395 pprPat (LitPat s) = ppr s
396 pprPat (NPat l Nothing _) = ppr l
397 pprPat (NPat l (Just _) _) = char '-' <> ppr l
398 pprPat (NPlusKPat n k _ _) = hcat [ppr n, char '+', ppr k]
399 pprPat (SplicePat splice) = pprSplice splice
400 pprPat (CoPat co pat _) = pprHsWrapper (ppr pat) co
401 pprPat (SigPatIn pat ty) = ppr pat <+> dcolon <+> ppr ty
402 pprPat (SigPatOut pat ty) = ppr pat <+> dcolon <+> ppr ty
403 pprPat (ListPat pats _ _) = brackets (interpp'SP pats)
404 pprPat (PArrPat pats _) = paBrackets (interpp'SP pats)
405 pprPat (TuplePat pats bx _) = tupleParens (boxityTupleSort bx) (pprWithCommas ppr pats)
406 pprPat (ConPatIn con details) = pprUserCon (unLoc con) details
407 pprPat (ConPatOut { pat_con = con, pat_tvs = tvs, pat_dicts = dicts,
408 pat_binds = binds, pat_args = details })
409 = getPprStyle $ \ sty -> -- Tiresome; in TcBinds.tcRhs we print out a
410 if debugStyle sty then -- typechecked Pat in an error message,
411 -- and we want to make sure it prints nicely
412 ppr con
413 <> braces (sep [ hsep (map pprPatBndr (tvs ++ dicts))
414 , ppr binds])
415 <+> pprConArgs details
416 else pprUserCon (unLoc con) details
417
418
419 pprUserCon :: (OutputableBndr con, OutputableBndr id) => con -> HsConPatDetails id -> SDoc
420 pprUserCon c (InfixCon p1 p2) = ppr p1 <+> pprInfixOcc c <+> ppr p2
421 pprUserCon c details = pprPrefixOcc c <+> pprConArgs details
422
423 pprConArgs :: OutputableBndr id => HsConPatDetails id -> SDoc
424 pprConArgs (PrefixCon pats) = sep (map pprParendLPat pats)
425 pprConArgs (InfixCon p1 p2) = sep [pprParendLPat p1, pprParendLPat p2]
426 pprConArgs (RecCon rpats) = ppr rpats
427
428 instance (Outputable arg)
429 => Outputable (HsRecFields id arg) where
430 ppr (HsRecFields { rec_flds = flds, rec_dotdot = Nothing })
431 = braces (fsep (punctuate comma (map ppr flds)))
432 ppr (HsRecFields { rec_flds = flds, rec_dotdot = Just n })
433 = braces (fsep (punctuate comma (map ppr (take n flds) ++ [dotdot])))
434 where
435 dotdot = ptext (sLit "..") <+> ifPprDebug (ppr (drop n flds))
436
437 instance (Outputable id, Outputable arg)
438 => Outputable (HsRecField' id arg) where
439 ppr (HsRecField { hsRecFieldLbl = f, hsRecFieldArg = arg,
440 hsRecPun = pun })
441 = ppr f <+> (ppUnless pun $ equals <+> ppr arg)
442
443
444 {-
445 ************************************************************************
446 * *
447 * Building patterns
448 * *
449 ************************************************************************
450 -}
451
452 mkPrefixConPat :: DataCon -> [OutPat id] -> [Type] -> OutPat id
453 -- Make a vanilla Prefix constructor pattern
454 mkPrefixConPat dc pats tys
455 = noLoc $ ConPatOut { pat_con = noLoc (RealDataCon dc), pat_tvs = [], pat_dicts = [],
456 pat_binds = emptyTcEvBinds, pat_args = PrefixCon pats,
457 pat_arg_tys = tys, pat_wrap = idHsWrapper }
458
459 mkNilPat :: Type -> OutPat id
460 mkNilPat ty = mkPrefixConPat nilDataCon [] [ty]
461
462 mkCharLitPat :: String -> Char -> OutPat id
463 mkCharLitPat src c = mkPrefixConPat charDataCon
464 [noLoc $ LitPat (HsCharPrim src c)] []
465
466 {-
467 ************************************************************************
468 * *
469 * Predicates for checking things about pattern-lists in EquationInfo *
470 * *
471 ************************************************************************
472
473 \subsection[Pat-list-predicates]{Look for interesting things in patterns}
474
475 Unlike in the Wadler chapter, where patterns are either ``variables''
476 or ``constructors,'' here we distinguish between:
477 \begin{description}
478 \item[unfailable:]
479 Patterns that cannot fail to match: variables, wildcards, and lazy
480 patterns.
481
482 These are the irrefutable patterns; the two other categories
483 are refutable patterns.
484
485 \item[constructor:]
486 A non-literal constructor pattern (see next category).
487
488 \item[literal patterns:]
489 At least the numeric ones may be overloaded.
490 \end{description}
491
492 A pattern is in {\em exactly one} of the above three categories; `as'
493 patterns are treated specially, of course.
494
495 The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
496 -}
497
498 isUnliftedLPat :: LPat id -> Bool
499 isUnliftedLPat (L _ (ParPat p)) = isUnliftedLPat p
500 isUnliftedLPat (L _ (TuplePat _ Unboxed _)) = True
501 isUnliftedLPat _ = False
502
503 isUnliftedHsBind :: HsBind id -> Bool
504 -- A pattern binding with an outermost bang or unboxed tuple must be matched strictly
505 -- Defined in this module because HsPat is above HsBinds in the import graph
506 isUnliftedHsBind (PatBind { pat_lhs = p }) = isUnliftedLPat p
507 isUnliftedHsBind _ = False
508
509 isBangedPatBind :: HsBind id -> Bool
510 isBangedPatBind (PatBind {pat_lhs = pat}) = isBangedLPat pat
511 isBangedPatBind _ = False
512
513 isBangedLPat :: LPat id -> Bool
514 isBangedLPat (L _ (ParPat p)) = isBangedLPat p
515 isBangedLPat (L _ (BangPat {})) = True
516 isBangedLPat _ = False
517
518 looksLazyPatBind :: HsBind id -> Bool
519 -- Returns True of anything *except*
520 -- a StrictHsBind (as above) or
521 -- a VarPat
522 -- In particular, returns True of a pattern binding with a compound pattern, like (I# x)
523 looksLazyPatBind (PatBind { pat_lhs = p }) = looksLazyLPat p
524 looksLazyPatBind _ = False
525
526 looksLazyLPat :: LPat id -> Bool
527 looksLazyLPat (L _ (ParPat p)) = looksLazyLPat p
528 looksLazyLPat (L _ (AsPat _ p)) = looksLazyLPat p
529 looksLazyLPat (L _ (BangPat {})) = False
530 looksLazyLPat (L _ (TuplePat _ Unboxed _)) = False
531 looksLazyLPat (L _ (VarPat {})) = False
532 looksLazyLPat (L _ (WildPat {})) = False
533 looksLazyLPat _ = True
534
535 isIrrefutableHsPat :: OutputableBndr id => LPat id -> Bool
536 -- (isIrrefutableHsPat p) is true if matching against p cannot fail,
537 -- in the sense of falling through to the next pattern.
538 -- (NB: this is not quite the same as the (silly) defn
539 -- in 3.17.2 of the Haskell 98 report.)
540 --
541 -- isIrrefutableHsPat returns False if it's in doubt; specifically
542 -- on a ConPatIn it doesn't know the size of the constructor family
543 -- But if it returns True, the pattern is definitely irrefutable
544 isIrrefutableHsPat pat
545 = go pat
546 where
547 go (L _ pat) = go1 pat
548
549 go1 (WildPat {}) = True
550 go1 (VarPat {}) = True
551 go1 (LazyPat {}) = True
552 go1 (BangPat pat) = go pat
553 go1 (CoPat _ pat _) = go1 pat
554 go1 (ParPat pat) = go pat
555 go1 (AsPat _ pat) = go pat
556 go1 (ViewPat _ pat _) = go pat
557 go1 (SigPatIn pat _) = go pat
558 go1 (SigPatOut pat _) = go pat
559 go1 (TuplePat pats _ _) = all go pats
560 go1 (ListPat {}) = False
561 go1 (PArrPat {}) = False -- ?
562
563 go1 (ConPatIn {}) = False -- Conservative
564 go1 (ConPatOut{ pat_con = L _ (RealDataCon con), pat_args = details })
565 = isJust (tyConSingleDataCon_maybe (dataConTyCon con))
566 -- NB: tyConSingleDataCon_maybe, *not* isProductTyCon, because
567 -- the latter is false of existentials. See Trac #4439
568 && all go (hsConPatArgs details)
569 go1 (ConPatOut{ pat_con = L _ (PatSynCon _pat) })
570 = False -- Conservative
571
572 go1 (LitPat {}) = False
573 go1 (NPat {}) = False
574 go1 (NPlusKPat {}) = False
575
576 -- Both should be gotten rid of by renamer before
577 -- isIrrefutablePat is called
578 go1 (SplicePat {}) = urk pat
579
580 urk pat = pprPanic "isIrrefutableHsPat:" (ppr pat)
581
582 hsPatNeedsParens :: Pat a -> Bool
583 hsPatNeedsParens (NPlusKPat {}) = True
584 hsPatNeedsParens (SplicePat {}) = False
585 hsPatNeedsParens (ConPatIn _ ds) = conPatNeedsParens ds
586 hsPatNeedsParens p@(ConPatOut {}) = conPatNeedsParens (pat_args p)
587 hsPatNeedsParens (SigPatIn {}) = True
588 hsPatNeedsParens (SigPatOut {}) = True
589 hsPatNeedsParens (ViewPat {}) = True
590 hsPatNeedsParens (CoPat _ p _) = hsPatNeedsParens p
591 hsPatNeedsParens (WildPat {}) = False
592 hsPatNeedsParens (VarPat {}) = False
593 hsPatNeedsParens (LazyPat {}) = False
594 hsPatNeedsParens (BangPat {}) = False
595 hsPatNeedsParens (ParPat {}) = False
596 hsPatNeedsParens (AsPat {}) = False
597 hsPatNeedsParens (TuplePat {}) = False
598 hsPatNeedsParens (ListPat {}) = False
599 hsPatNeedsParens (PArrPat {}) = False
600 hsPatNeedsParens (LitPat {}) = False
601 hsPatNeedsParens (NPat {}) = False
602
603 conPatNeedsParens :: HsConDetails a b -> Bool
604 conPatNeedsParens (PrefixCon args) = not (null args)
605 conPatNeedsParens (InfixCon {}) = True
606 conPatNeedsParens (RecCon {}) = True