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