{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

\section[PatSyntax]{Abstract Haskell syntax---patterns}
-}

{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
                                      -- in module PlaceHolder
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns      #-}
{-# LANGUAGE FlexibleInstances #-}

module HsPat (
        Pat(..), InPat, OutPat, LPat,
        ListPatTc(..),

        HsConPatDetails, hsConPatArgs,
        HsRecFields(..), HsRecField'(..), LHsRecField',
        HsRecField, LHsRecField,
        HsRecUpdField, LHsRecUpdField,
        hsRecFields, hsRecFieldSel, hsRecFieldId, hsRecFieldsArgs,
        hsRecUpdFieldId, hsRecUpdFieldOcc, hsRecUpdFieldRdr,

        mkPrefixConPat, mkCharLitPat, mkNilPat,

        looksLazyPatBind,
        isBangedLPat,
        patNeedsParens, parenthesizePat,
        isIrrefutableHsPat,

        collectEvVarsPat, collectEvVarsPats,

        pprParendLPat, pprConArgs
    ) where

import GhcPrelude

import {-# SOURCE #-} HsExpr            (SyntaxExpr, LHsExpr, HsSplice, pprLExpr, pprSplice)

-- friends:
import HsBinds
import HsLit
import HsExtension
import HsTypes
import TcEvidence
import BasicTypes
-- others:
import PprCore          ( {- instance OutputableBndr TyVar -} )
import TysWiredIn
import Var
import RdrName ( RdrName )
import ConLike
import DataCon
import TyCon
import Outputable
import Type
import SrcLoc
import Bag -- collect ev vars from pats
import DynFlags( gopt, GeneralFlag(..) )
import Maybes
-- libraries:
import Data.Data hiding (TyCon,Fixity)

type InPat p  = LPat p        -- No 'Out' constructors
type OutPat p = LPat p        -- No 'In' constructors

type LPat p = Pat p

-- | Pattern
--
-- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnBang'

-- For details on above see note [Api annotations] in ApiAnnotation
data Pat p
  =     ------------ Simple patterns ---------------
    WildPat     (XWildPat p)        -- ^ Wildcard Pattern
        -- The sole reason for a type on a WildPat is to
        -- support hsPatType :: Pat Id -> Type

       -- AZ:TODO above comment needs to be updated
  | VarPat      (XVarPat p)
                (Located (IdP p))  -- ^ Variable Pattern

                             -- See Note [Located RdrNames] in HsExpr
  | LazyPat     (XLazyPat p)
                (LPat p)                -- ^ Lazy Pattern
    -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'

    -- For details on above see note [Api annotations] in ApiAnnotation

  | AsPat       (XAsPat p)
                (Located (IdP p)) (LPat p)    -- ^ As pattern
    -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnAt'

    -- For details on above see note [Api annotations] in ApiAnnotation

  | ParPat      (XParPat p)
                (LPat p)                -- ^ Parenthesised pattern
                                        -- See Note [Parens in HsSyn] in HsExpr
    -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
    --                                    'ApiAnnotation.AnnClose' @')'@

    -- For details on above see note [Api annotations] in ApiAnnotation
  | BangPat     (XBangPat p)
                (LPat p)                -- ^ Bang pattern
    -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnBang'

    -- For details on above see note [Api annotations] in ApiAnnotation

        ------------ Lists, tuples, arrays ---------------
  | ListPat     (XListPat p)
                [LPat p]
                   -- For OverloadedLists a Just (ty,fn) gives
                   -- overall type of the pattern, and the toList
-- function to convert the scrutinee to a list value

    -- ^ Syntactic List
    --
    -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
    --                                    'ApiAnnotation.AnnClose' @']'@

    -- For details on above see note [Api annotations] in ApiAnnotation

  | TuplePat    (XTuplePat p)
                  -- after typechecking, holds the types of the tuple components
                [LPat p]         -- Tuple sub-patterns
                Boxity           -- UnitPat is TuplePat []
        -- You might think that the post typechecking Type was redundant,
        -- because we can get the pattern type by getting the types of the
        -- sub-patterns.
        -- But it's essential
        --      data T a where
        --        T1 :: Int -> T Int
        --      f :: (T a, a) -> Int
        --      f (T1 x, z) = z
        -- When desugaring, we must generate
        --      f = /\a. \v::a.  case v of (t::T a, w::a) ->
        --                       case t of (T1 (x::Int)) ->
        -- Note the (w::a), NOT (w::Int), because we have not yet
        -- refined 'a' to Int.  So we must know that the second component
        -- of the tuple is of type 'a' not Int.  See selectMatchVar
        -- (June 14: I'm not sure this comment is right; the sub-patterns
        --           will be wrapped in CoPats, no?)
    -- ^ Tuple sub-patterns
    --
    -- - 'ApiAnnotation.AnnKeywordId' :
    --            'ApiAnnotation.AnnOpen' @'('@ or @'(#'@,
    --            'ApiAnnotation.AnnClose' @')'@ or  @'#)'@

  | SumPat      (XSumPat p)        -- PlaceHolder before typechecker, filled in
                                   -- afterwards with the types of the
                                   -- alternative
                (LPat p)           -- Sum sub-pattern
                ConTag             -- Alternative (one-based)
                Arity              -- Arity (INVARIANT: ≥ 2)
    -- ^ Anonymous sum pattern
    --
    -- - 'ApiAnnotation.AnnKeywordId' :
    --            'ApiAnnotation.AnnOpen' @'(#'@,
    --            'ApiAnnotation.AnnClose' @'#)'@

    -- For details on above see note [Api annotations] in ApiAnnotation

        ------------ Constructor patterns ---------------
  | ConPatIn    (Located (IdP p))
                (HsConPatDetails p)
    -- ^ Constructor Pattern In

  | ConPatOut {
        Pat p -> Located ConLike
pat_con     :: Located ConLike,
        Pat p -> [Type]
pat_arg_tys :: [Type],          -- The universal arg types, 1-1 with the universal
                                        -- tyvars of the constructor/pattern synonym
                                        --   Use (conLikeResTy pat_con pat_arg_tys) to get
                                        --   the type of the pattern

        Pat p -> [TyVar]
pat_tvs   :: [TyVar],           -- Existentially bound type variables
                                        -- in correctly-scoped order e.g. [k:*, x:k]
        Pat p -> [TyVar]
pat_dicts :: [EvVar],           -- Ditto *coercion variables* and *dictionaries*
                                        -- One reason for putting coercion variable here, I think,
                                        --      is to ensure their kinds are zonked

        Pat p -> TcEvBinds
pat_binds :: TcEvBinds,         -- Bindings involving those dictionaries
        Pat p -> HsConPatDetails p
pat_args  :: HsConPatDetails p,
        Pat p -> HsWrapper
pat_wrap  :: HsWrapper          -- Extra wrapper to pass to the matcher
                                        -- Only relevant for pattern-synonyms;
                                        --   ignored for data cons
    }
    -- ^ Constructor Pattern Out

        ------------ View patterns ---------------
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow'

  -- For details on above see note [Api annotations] in ApiAnnotation
  | ViewPat       (XViewPat p)     -- The overall type of the pattern
                                   -- (= the argument type of the view function)
                                   -- for hsPatType.
                  (LHsExpr p)
                  (LPat p)
    -- ^ View Pattern

        ------------ Pattern splices ---------------
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@
  --        'ApiAnnotation.AnnClose' @')'@

  -- For details on above see note [Api annotations] in ApiAnnotation
  | SplicePat       (XSplicePat p)
                    (HsSplice p)    -- ^ Splice Pattern (Includes quasi-quotes)

        ------------ Literal and n+k patterns ---------------
  | LitPat          (XLitPat p)
                    (HsLit p)           -- ^ Literal Pattern
                                        -- Used for *non-overloaded* literal patterns:
                                        -- Int#, Char#, Int, Char, String, etc.

  | NPat                -- Natural Pattern
                        -- Used for all overloaded literals,
                        -- including overloaded strings with -XOverloadedStrings
                    (XNPat p)            -- Overall type of pattern. Might be
                                         -- different than the literal's type
                                         -- if (==) or negate changes the type
                    (Located (HsOverLit p))     -- ALWAYS positive
                    (Maybe (SyntaxExpr p)) -- Just (Name of 'negate') for
                                           -- negative patterns, Nothing
                                           -- otherwise
                    (SyntaxExpr p)       -- Equality checker, of type t->t->Bool

  -- ^ Natural Pattern
  --
  -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnVal' @'+'@

  -- For details on above see note [Api annotations] in ApiAnnotation
  | NPlusKPat       (XNPlusKPat p)           -- Type of overall pattern
                    (Located (IdP p))        -- n+k pattern
                    (Located (HsOverLit p))  -- It'll always be an HsIntegral
                    (HsOverLit p)       -- See Note [NPlusK patterns] in TcPat
                     -- NB: This could be (PostTc ...), but that induced a
                     -- a new hs-boot file. Not worth it.

                    (SyntaxExpr p)   -- (>=) function, of type t1->t2->Bool
                    (SyntaxExpr p)   -- Name of '-' (see RnEnv.lookupSyntaxName)
  -- ^ n+k pattern

        ------------ Pattern type signatures ---------------
  -- | - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'

  -- For details on above see note [Api annotations] in ApiAnnotation
  | SigPat          (XSigPat p)             -- After typechecker: Type
                    (LPat p)                -- Pattern with a type signature
                    (LHsSigWcType (NoGhcTc p)) --  Signature can bind both
                                               --  kind and type vars

    -- ^ Pattern with a type signature

        ------------ Pattern coercions (translation only) ---------------
  | CoPat       (XCoPat p)
                HsWrapper           -- Coercion Pattern
                                    -- If co :: t1 ~ t2, p :: t2,
                                    -- then (CoPat co p) :: t1
                (Pat p)             -- Why not LPat?  Ans: existing locn will do
                Type                -- Type of whole pattern, t1
        -- During desugaring a (CoPat co pat) turns into a cast with 'co' on
        -- the scrutinee, followed by a match on 'pat'
    -- ^ Coercion Pattern

  -- | Trees that Grow extension point for new constructors
  | XPat
      (XXPat p)

-- ---------------------------------------------------------------------

data ListPatTc
  = ListPatTc
      Type                             -- The type of the elements
      (Maybe (Type, SyntaxExpr GhcTc)) -- For rebindable syntax

type instance XWildPat GhcPs = NoExt
type instance XWildPat GhcRn = NoExt
type instance XWildPat GhcTc = Type

type instance XVarPat  (GhcPass _) = NoExt
type instance XLazyPat (GhcPass _) = NoExt
type instance XAsPat   (GhcPass _) = NoExt
type instance XParPat  (GhcPass _) = NoExt
type instance XBangPat (GhcPass _) = NoExt

-- Note: XListPat cannot be extended when using GHC 8.0.2 as the bootstrap
-- compiler, as it triggers https://ghc.haskell.org/trac/ghc/ticket/14396 for
-- `SyntaxExpr`
type instance XListPat GhcPs = NoExt
type instance XListPat GhcRn = Maybe (SyntaxExpr GhcRn)
type instance XListPat GhcTc = ListPatTc

type instance XTuplePat GhcPs = NoExt
type instance XTuplePat GhcRn = NoExt
type instance XTuplePat GhcTc = [Type]

type instance XSumPat GhcPs = NoExt
type instance XSumPat GhcRn = NoExt
type instance XSumPat GhcTc = [Type]

type instance XViewPat GhcPs = NoExt
type instance XViewPat GhcRn = NoExt
type instance XViewPat GhcTc = Type

type instance XSplicePat (GhcPass _) = NoExt
type instance XLitPat    (GhcPass _) = NoExt

type instance XNPat GhcPs = NoExt
type instance XNPat GhcRn = NoExt
type instance XNPat GhcTc = Type

type instance XNPlusKPat GhcPs = NoExt
type instance XNPlusKPat GhcRn = NoExt
type instance XNPlusKPat GhcTc = Type

type instance XSigPat GhcPs = NoExt
type instance XSigPat GhcRn = NoExt
type instance XSigPat GhcTc = Type

type instance XCoPat  (GhcPass _) = NoExt
type instance XXPat   (GhcPass p) = Located (Pat (GhcPass p))


{-
************************************************************************
*                                                                      *
*              HasSrcSpan Instance
*                                                                      *
************************************************************************
-}

type instance SrcSpanLess (LPat (GhcPass p)) = Pat (GhcPass p)
instance HasSrcSpan (LPat (GhcPass p)) where
  -- NB: The following chooses the behaviour of the outer location
  --     wrapper replacing the inner ones.
  composeSrcSpan :: Located (SrcSpanLess (LPat (GhcPass p))) -> LPat (GhcPass p)
composeSrcSpan (L sp :: SrcSpan
sp p :: SrcSpanLess (LPat (GhcPass p))
p) =  if SrcSpan
sp SrcSpan -> SrcSpan -> Bool
forall a. Eq a => a -> a -> Bool
== SrcSpan
noSrcSpan
                             then SrcSpanLess (LPat (GhcPass p))
LPat (GhcPass p)
p
                             else XXPat (GhcPass p) -> LPat (GhcPass p)
forall p. XXPat p -> Pat p
XPat (SrcSpan
-> LPat (GhcPass p) -> GenLocated SrcSpan (LPat (GhcPass p))
forall l e. l -> e -> GenLocated l e
L SrcSpan
sp (LPat (GhcPass p) -> LPat (GhcPass p)
forall (p :: Pass). LPat (GhcPass p) -> LPat (GhcPass p)
stripSrcSpanPat SrcSpanLess (LPat (GhcPass p))
LPat (GhcPass p)
p))

  -- NB: The following only returns the top-level location, if any.
  decomposeSrcSpan :: LPat (GhcPass p) -> Located (SrcSpanLess (LPat (GhcPass p)))
decomposeSrcSpan (XPat (L sp p)) = SrcSpan
-> LPat (GhcPass p) -> GenLocated SrcSpan (LPat (GhcPass p))
forall l e. l -> e -> GenLocated l e
L SrcSpan
sp (LPat (GhcPass p) -> LPat (GhcPass p)
forall (p :: Pass). LPat (GhcPass p) -> LPat (GhcPass p)
stripSrcSpanPat LPat (GhcPass p)
p)
  decomposeSrcSpan p :: LPat (GhcPass p)
p               = SrcSpan
-> LPat (GhcPass p) -> GenLocated SrcSpan (LPat (GhcPass p))
forall l e. l -> e -> GenLocated l e
L SrcSpan
noSrcSpan LPat (GhcPass p)
p

stripSrcSpanPat :: LPat (GhcPass p) -> Pat (GhcPass p)
stripSrcSpanPat :: LPat (GhcPass p) -> LPat (GhcPass p)
stripSrcSpanPat (XPat (L _  p)) = LPat (GhcPass p) -> LPat (GhcPass p)
forall (p :: Pass). LPat (GhcPass p) -> LPat (GhcPass p)
stripSrcSpanPat LPat (GhcPass p)
p
stripSrcSpanPat p :: LPat (GhcPass p)
p               = LPat (GhcPass p)
p



-- ---------------------------------------------------------------------


-- | Haskell Constructor Pattern Details
type HsConPatDetails p = HsConDetails (LPat p) (HsRecFields p (LPat p))

hsConPatArgs :: HsConPatDetails p -> [LPat p]
hsConPatArgs :: HsConPatDetails p -> [LPat p]
hsConPatArgs (PrefixCon ps :: [LPat p]
ps)   = [LPat p]
ps
hsConPatArgs (RecCon fs :: HsRecFields p (LPat p)
fs)      = (LHsRecField p (LPat p) -> LPat p)
-> [LHsRecField p (LPat p)] -> [LPat p]
forall a b. (a -> b) -> [a] -> [b]
map (HsRecField' (FieldOcc p) (LPat p) -> LPat p
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg (HsRecField' (FieldOcc p) (LPat p) -> LPat p)
-> (LHsRecField p (LPat p) -> HsRecField' (FieldOcc p) (LPat p))
-> LHsRecField p (LPat p)
-> LPat p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecField p (LPat p) -> HsRecField' (FieldOcc p) (LPat p)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc) (HsRecFields p (LPat p) -> [LHsRecField p (LPat p)]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields p (LPat p)
fs)
hsConPatArgs (InfixCon p1 :: LPat p
p1 p2 :: LPat p
p2) = [LPat p
p1,LPat p
p2]

-- | Haskell Record Fields
--
-- HsRecFields is used only for patterns and expressions (not data type
-- declarations)
data HsRecFields p arg         -- A bunch of record fields
                                --      { x = 3, y = True }
        -- Used for both expressions and patterns
  = HsRecFields { HsRecFields p arg -> [LHsRecField p arg]
rec_flds   :: [LHsRecField p arg],
                  HsRecFields p arg -> Maybe Int
rec_dotdot :: Maybe Int }  -- Note [DotDot fields]
  deriving (a -> HsRecFields p b -> HsRecFields p a
(a -> b) -> HsRecFields p a -> HsRecFields p b
(forall a b. (a -> b) -> HsRecFields p a -> HsRecFields p b)
-> (forall a b. a -> HsRecFields p b -> HsRecFields p a)
-> Functor (HsRecFields p)
forall a b. a -> HsRecFields p b -> HsRecFields p a
forall a b. (a -> b) -> HsRecFields p a -> HsRecFields p b
forall p a b. a -> HsRecFields p b -> HsRecFields p a
forall p a b. (a -> b) -> HsRecFields p a -> HsRecFields p b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> HsRecFields p b -> HsRecFields p a
$c<$ :: forall p a b. a -> HsRecFields p b -> HsRecFields p a
fmap :: (a -> b) -> HsRecFields p a -> HsRecFields p b
$cfmap :: forall p a b. (a -> b) -> HsRecFields p a -> HsRecFields p b
Functor, HsRecFields p a -> Bool
(a -> m) -> HsRecFields p a -> m
(a -> b -> b) -> b -> HsRecFields p a -> b
(forall m. Monoid m => HsRecFields p m -> m)
-> (forall m a. Monoid m => (a -> m) -> HsRecFields p a -> m)
-> (forall m a. Monoid m => (a -> m) -> HsRecFields p a -> m)
-> (forall a b. (a -> b -> b) -> b -> HsRecFields p a -> b)
-> (forall a b. (a -> b -> b) -> b -> HsRecFields p a -> b)
-> (forall b a. (b -> a -> b) -> b -> HsRecFields p a -> b)
-> (forall b a. (b -> a -> b) -> b -> HsRecFields p a -> b)
-> (forall a. (a -> a -> a) -> HsRecFields p a -> a)
-> (forall a. (a -> a -> a) -> HsRecFields p a -> a)
-> (forall a. HsRecFields p a -> [a])
-> (forall a. HsRecFields p a -> Bool)
-> (forall a. HsRecFields p a -> Int)
-> (forall a. Eq a => a -> HsRecFields p a -> Bool)
-> (forall a. Ord a => HsRecFields p a -> a)
-> (forall a. Ord a => HsRecFields p a -> a)
-> (forall a. Num a => HsRecFields p a -> a)
-> (forall a. Num a => HsRecFields p a -> a)
-> Foldable (HsRecFields p)
forall a. Eq a => a -> HsRecFields p a -> Bool
forall a. Num a => HsRecFields p a -> a
forall a. Ord a => HsRecFields p a -> a
forall m. Monoid m => HsRecFields p m -> m
forall a. HsRecFields p a -> Bool
forall a. HsRecFields p a -> Int
forall a. HsRecFields p a -> [a]
forall a. (a -> a -> a) -> HsRecFields p a -> a
forall p a. Eq a => a -> HsRecFields p a -> Bool
forall p a. Num a => HsRecFields p a -> a
forall p a. Ord a => HsRecFields p a -> a
forall m a. Monoid m => (a -> m) -> HsRecFields p a -> m
forall p m. Monoid m => HsRecFields p m -> m
forall p a. HsRecFields p a -> Bool
forall p a. HsRecFields p a -> Int
forall p a. HsRecFields p a -> [a]
forall b a. (b -> a -> b) -> b -> HsRecFields p a -> b
forall a b. (a -> b -> b) -> b -> HsRecFields p a -> b
forall p a. (a -> a -> a) -> HsRecFields p a -> a
forall p m a. Monoid m => (a -> m) -> HsRecFields p a -> m
forall p b a. (b -> a -> b) -> b -> HsRecFields p a -> b
forall p a b. (a -> b -> b) -> b -> HsRecFields p a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: HsRecFields p a -> a
$cproduct :: forall p a. Num a => HsRecFields p a -> a
sum :: HsRecFields p a -> a
$csum :: forall p a. Num a => HsRecFields p a -> a
minimum :: HsRecFields p a -> a
$cminimum :: forall p a. Ord a => HsRecFields p a -> a
maximum :: HsRecFields p a -> a
$cmaximum :: forall p a. Ord a => HsRecFields p a -> a
elem :: a -> HsRecFields p a -> Bool
$celem :: forall p a. Eq a => a -> HsRecFields p a -> Bool
length :: HsRecFields p a -> Int
$clength :: forall p a. HsRecFields p a -> Int
null :: HsRecFields p a -> Bool
$cnull :: forall p a. HsRecFields p a -> Bool
toList :: HsRecFields p a -> [a]
$ctoList :: forall p a. HsRecFields p a -> [a]
foldl1 :: (a -> a -> a) -> HsRecFields p a -> a
$cfoldl1 :: forall p a. (a -> a -> a) -> HsRecFields p a -> a
foldr1 :: (a -> a -> a) -> HsRecFields p a -> a
$cfoldr1 :: forall p a. (a -> a -> a) -> HsRecFields p a -> a
foldl' :: (b -> a -> b) -> b -> HsRecFields p a -> b
$cfoldl' :: forall p b a. (b -> a -> b) -> b -> HsRecFields p a -> b
foldl :: (b -> a -> b) -> b -> HsRecFields p a -> b
$cfoldl :: forall p b a. (b -> a -> b) -> b -> HsRecFields p a -> b
foldr' :: (a -> b -> b) -> b -> HsRecFields p a -> b
$cfoldr' :: forall p a b. (a -> b -> b) -> b -> HsRecFields p a -> b
foldr :: (a -> b -> b) -> b -> HsRecFields p a -> b
$cfoldr :: forall p a b. (a -> b -> b) -> b -> HsRecFields p a -> b
foldMap' :: (a -> m) -> HsRecFields p a -> m
$cfoldMap' :: forall p m a. Monoid m => (a -> m) -> HsRecFields p a -> m
foldMap :: (a -> m) -> HsRecFields p a -> m
$cfoldMap :: forall p m a. Monoid m => (a -> m) -> HsRecFields p a -> m
fold :: HsRecFields p m -> m
$cfold :: forall p m. Monoid m => HsRecFields p m -> m
Foldable, Functor (HsRecFields p)
Foldable (HsRecFields p)
(Functor (HsRecFields p), Foldable (HsRecFields p)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> HsRecFields p a -> f (HsRecFields p b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    HsRecFields p (f a) -> f (HsRecFields p a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> HsRecFields p a -> m (HsRecFields p b))
-> (forall (m :: * -> *) a.
    Monad m =>
    HsRecFields p (m a) -> m (HsRecFields p a))
-> Traversable (HsRecFields p)
(a -> f b) -> HsRecFields p a -> f (HsRecFields p b)
forall p. Functor (HsRecFields p)
forall p. Foldable (HsRecFields p)
forall p (m :: * -> *) a.
Monad m =>
HsRecFields p (m a) -> m (HsRecFields p a)
forall p (f :: * -> *) a.
Applicative f =>
HsRecFields p (f a) -> f (HsRecFields p a)
forall p (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HsRecFields p a -> m (HsRecFields p b)
forall p (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HsRecFields p a -> f (HsRecFields p b)
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
HsRecFields p (m a) -> m (HsRecFields p a)
forall (f :: * -> *) a.
Applicative f =>
HsRecFields p (f a) -> f (HsRecFields p a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HsRecFields p a -> m (HsRecFields p b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HsRecFields p a -> f (HsRecFields p b)
sequence :: HsRecFields p (m a) -> m (HsRecFields p a)
$csequence :: forall p (m :: * -> *) a.
Monad m =>
HsRecFields p (m a) -> m (HsRecFields p a)
mapM :: (a -> m b) -> HsRecFields p a -> m (HsRecFields p b)
$cmapM :: forall p (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HsRecFields p a -> m (HsRecFields p b)
sequenceA :: HsRecFields p (f a) -> f (HsRecFields p a)
$csequenceA :: forall p (f :: * -> *) a.
Applicative f =>
HsRecFields p (f a) -> f (HsRecFields p a)
traverse :: (a -> f b) -> HsRecFields p a -> f (HsRecFields p b)
$ctraverse :: forall p (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HsRecFields p a -> f (HsRecFields p b)
$cp2Traversable :: forall p. Foldable (HsRecFields p)
$cp1Traversable :: forall p. Functor (HsRecFields p)
Traversable)


-- Note [DotDot fields]
-- ~~~~~~~~~~~~~~~~~~~~
-- The rec_dotdot field means this:
--   Nothing => the normal case
--   Just n  => the group uses ".." notation,
--
-- In the latter case:
--
--   *before* renamer: rec_flds are exactly the n user-written fields
--
--   *after* renamer:  rec_flds includes *all* fields, with
--                     the first 'n' being the user-written ones
--                     and the remainder being 'filled in' implicitly

-- | Located Haskell Record Field
type LHsRecField' p arg = Located (HsRecField' p arg)

-- | Located Haskell Record Field
type LHsRecField  p arg = Located (HsRecField  p arg)

-- | Located Haskell Record Update Field
type LHsRecUpdField p   = Located (HsRecUpdField p)

-- | Haskell Record Field
type HsRecField    p arg = HsRecField' (FieldOcc p) arg

-- | Haskell Record Update Field
type HsRecUpdField p     = HsRecField' (AmbiguousFieldOcc p) (LHsExpr p)

-- | Haskell Record Field
--
-- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnEqual',
--
-- For details on above see note [Api annotations] in ApiAnnotation
data HsRecField' id arg = HsRecField {
        HsRecField' id arg -> Located id
hsRecFieldLbl :: Located id,
        HsRecField' id arg -> arg
hsRecFieldArg :: arg,           -- ^ Filled in by renamer when punning
        HsRecField' id arg -> Bool
hsRecPun      :: Bool           -- ^ Note [Punning]
  } deriving (Typeable (HsRecField' id arg)
DataType
Constr
Typeable (HsRecField' id arg) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g)
 -> HsRecField' id arg
 -> c (HsRecField' id arg))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (HsRecField' id arg))
-> (HsRecField' id arg -> Constr)
-> (HsRecField' id arg -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (HsRecField' id arg)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (HsRecField' id arg)))
-> ((forall b. Data b => b -> b)
    -> HsRecField' id arg -> HsRecField' id arg)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> HsRecField' id arg -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> HsRecField' id arg -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> HsRecField' id arg -> m (HsRecField' id arg))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> HsRecField' id arg -> m (HsRecField' id arg))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> HsRecField' id arg -> m (HsRecField' id arg))
-> Data (HsRecField' id arg)
HsRecField' id arg -> DataType
HsRecField' id arg -> Constr
(forall b. Data b => b -> b)
-> HsRecField' id arg -> HsRecField' id arg
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HsRecField' id arg
-> c (HsRecField' id arg)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HsRecField' id arg)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HsRecField' id arg))
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> HsRecField' id arg -> u
forall u. (forall d. Data d => d -> u) -> HsRecField' id arg -> [u]
forall id arg. (Data id, Data arg) => Typeable (HsRecField' id arg)
forall id arg.
(Data id, Data arg) =>
HsRecField' id arg -> DataType
forall id arg. (Data id, Data arg) => HsRecField' id arg -> Constr
forall id arg.
(Data id, Data arg) =>
(forall b. Data b => b -> b)
-> HsRecField' id arg -> HsRecField' id arg
forall id arg u.
(Data id, Data arg) =>
Int -> (forall d. Data d => d -> u) -> HsRecField' id arg -> u
forall id arg u.
(Data id, Data arg) =>
(forall d. Data d => d -> u) -> HsRecField' id arg -> [u]
forall id arg r r'.
(Data id, Data arg) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
forall id arg r r'.
(Data id, Data arg) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
forall id arg (m :: * -> *).
(Data id, Data arg, Monad m) =>
(forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
forall id arg (m :: * -> *).
(Data id, Data arg, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
forall id arg (c :: * -> *).
(Data id, Data arg) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HsRecField' id arg)
forall id arg (c :: * -> *).
(Data id, Data arg) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HsRecField' id arg
-> c (HsRecField' id arg)
forall id arg (t :: * -> *) (c :: * -> *).
(Data id, Data arg, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (HsRecField' id arg))
forall id arg (t :: * -> * -> *) (c :: * -> *).
(Data id, Data arg, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HsRecField' id arg))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HsRecField' id arg)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HsRecField' id arg
-> c (HsRecField' id arg)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (HsRecField' id arg))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HsRecField' id arg))
$cHsRecField :: Constr
$tHsRecField' :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
$cgmapMo :: forall id arg (m :: * -> *).
(Data id, Data arg, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
gmapMp :: (forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
$cgmapMp :: forall id arg (m :: * -> *).
(Data id, Data arg, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
gmapM :: (forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
$cgmapM :: forall id arg (m :: * -> *).
(Data id, Data arg, Monad m) =>
(forall d. Data d => d -> m d)
-> HsRecField' id arg -> m (HsRecField' id arg)
gmapQi :: Int -> (forall d. Data d => d -> u) -> HsRecField' id arg -> u
$cgmapQi :: forall id arg u.
(Data id, Data arg) =>
Int -> (forall d. Data d => d -> u) -> HsRecField' id arg -> u
gmapQ :: (forall d. Data d => d -> u) -> HsRecField' id arg -> [u]
$cgmapQ :: forall id arg u.
(Data id, Data arg) =>
(forall d. Data d => d -> u) -> HsRecField' id arg -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
$cgmapQr :: forall id arg r r'.
(Data id, Data arg) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
$cgmapQl :: forall id arg r r'.
(Data id, Data arg) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HsRecField' id arg -> r
gmapT :: (forall b. Data b => b -> b)
-> HsRecField' id arg -> HsRecField' id arg
$cgmapT :: forall id arg.
(Data id, Data arg) =>
(forall b. Data b => b -> b)
-> HsRecField' id arg -> HsRecField' id arg
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HsRecField' id arg))
$cdataCast2 :: forall id arg (t :: * -> * -> *) (c :: * -> *).
(Data id, Data arg, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HsRecField' id arg))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (HsRecField' id arg))
$cdataCast1 :: forall id arg (t :: * -> *) (c :: * -> *).
(Data id, Data arg, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (HsRecField' id arg))
dataTypeOf :: HsRecField' id arg -> DataType
$cdataTypeOf :: forall id arg.
(Data id, Data arg) =>
HsRecField' id arg -> DataType
toConstr :: HsRecField' id arg -> Constr
$ctoConstr :: forall id arg. (Data id, Data arg) => HsRecField' id arg -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HsRecField' id arg)
$cgunfold :: forall id arg (c :: * -> *).
(Data id, Data arg) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HsRecField' id arg)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HsRecField' id arg
-> c (HsRecField' id arg)
$cgfoldl :: forall id arg (c :: * -> *).
(Data id, Data arg) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HsRecField' id arg
-> c (HsRecField' id arg)
$cp1Data :: forall id arg. (Data id, Data arg) => Typeable (HsRecField' id arg)
Data, a -> HsRecField' id b -> HsRecField' id a
(a -> b) -> HsRecField' id a -> HsRecField' id b
(forall a b. (a -> b) -> HsRecField' id a -> HsRecField' id b)
-> (forall a b. a -> HsRecField' id b -> HsRecField' id a)
-> Functor (HsRecField' id)
forall a b. a -> HsRecField' id b -> HsRecField' id a
forall a b. (a -> b) -> HsRecField' id a -> HsRecField' id b
forall id a b. a -> HsRecField' id b -> HsRecField' id a
forall id a b. (a -> b) -> HsRecField' id a -> HsRecField' id b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> HsRecField' id b -> HsRecField' id a
$c<$ :: forall id a b. a -> HsRecField' id b -> HsRecField' id a
fmap :: (a -> b) -> HsRecField' id a -> HsRecField' id b
$cfmap :: forall id a b. (a -> b) -> HsRecField' id a -> HsRecField' id b
Functor, HsRecField' id a -> Bool
(a -> m) -> HsRecField' id a -> m
(a -> b -> b) -> b -> HsRecField' id a -> b
(forall m. Monoid m => HsRecField' id m -> m)
-> (forall m a. Monoid m => (a -> m) -> HsRecField' id a -> m)
-> (forall m a. Monoid m => (a -> m) -> HsRecField' id a -> m)
-> (forall a b. (a -> b -> b) -> b -> HsRecField' id a -> b)
-> (forall a b. (a -> b -> b) -> b -> HsRecField' id a -> b)
-> (forall b a. (b -> a -> b) -> b -> HsRecField' id a -> b)
-> (forall b a. (b -> a -> b) -> b -> HsRecField' id a -> b)
-> (forall a. (a -> a -> a) -> HsRecField' id a -> a)
-> (forall a. (a -> a -> a) -> HsRecField' id a -> a)
-> (forall a. HsRecField' id a -> [a])
-> (forall a. HsRecField' id a -> Bool)
-> (forall a. HsRecField' id a -> Int)
-> (forall a. Eq a => a -> HsRecField' id a -> Bool)
-> (forall a. Ord a => HsRecField' id a -> a)
-> (forall a. Ord a => HsRecField' id a -> a)
-> (forall a. Num a => HsRecField' id a -> a)
-> (forall a. Num a => HsRecField' id a -> a)
-> Foldable (HsRecField' id)
forall a. Eq a => a -> HsRecField' id a -> Bool
forall a. Num a => HsRecField' id a -> a
forall a. Ord a => HsRecField' id a -> a
forall m. Monoid m => HsRecField' id m -> m
forall a. HsRecField' id a -> Bool
forall a. HsRecField' id a -> Int
forall a. HsRecField' id a -> [a]
forall a. (a -> a -> a) -> HsRecField' id a -> a
forall id a. Eq a => a -> HsRecField' id a -> Bool
forall id a. Num a => HsRecField' id a -> a
forall id a. Ord a => HsRecField' id a -> a
forall m a. Monoid m => (a -> m) -> HsRecField' id a -> m
forall id m. Monoid m => HsRecField' id m -> m
forall id a. HsRecField' id a -> Bool
forall id a. HsRecField' id a -> Int
forall id a. HsRecField' id a -> [a]
forall b a. (b -> a -> b) -> b -> HsRecField' id a -> b
forall a b. (a -> b -> b) -> b -> HsRecField' id a -> b
forall id a. (a -> a -> a) -> HsRecField' id a -> a
forall id m a. Monoid m => (a -> m) -> HsRecField' id a -> m
forall id b a. (b -> a -> b) -> b -> HsRecField' id a -> b
forall id a b. (a -> b -> b) -> b -> HsRecField' id a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: HsRecField' id a -> a
$cproduct :: forall id a. Num a => HsRecField' id a -> a
sum :: HsRecField' id a -> a
$csum :: forall id a. Num a => HsRecField' id a -> a
minimum :: HsRecField' id a -> a
$cminimum :: forall id a. Ord a => HsRecField' id a -> a
maximum :: HsRecField' id a -> a
$cmaximum :: forall id a. Ord a => HsRecField' id a -> a
elem :: a -> HsRecField' id a -> Bool
$celem :: forall id a. Eq a => a -> HsRecField' id a -> Bool
length :: HsRecField' id a -> Int
$clength :: forall id a. HsRecField' id a -> Int
null :: HsRecField' id a -> Bool
$cnull :: forall id a. HsRecField' id a -> Bool
toList :: HsRecField' id a -> [a]
$ctoList :: forall id a. HsRecField' id a -> [a]
foldl1 :: (a -> a -> a) -> HsRecField' id a -> a
$cfoldl1 :: forall id a. (a -> a -> a) -> HsRecField' id a -> a
foldr1 :: (a -> a -> a) -> HsRecField' id a -> a
$cfoldr1 :: forall id a. (a -> a -> a) -> HsRecField' id a -> a
foldl' :: (b -> a -> b) -> b -> HsRecField' id a -> b
$cfoldl' :: forall id b a. (b -> a -> b) -> b -> HsRecField' id a -> b
foldl :: (b -> a -> b) -> b -> HsRecField' id a -> b
$cfoldl :: forall id b a. (b -> a -> b) -> b -> HsRecField' id a -> b
foldr' :: (a -> b -> b) -> b -> HsRecField' id a -> b
$cfoldr' :: forall id a b. (a -> b -> b) -> b -> HsRecField' id a -> b
foldr :: (a -> b -> b) -> b -> HsRecField' id a -> b
$cfoldr :: forall id a b. (a -> b -> b) -> b -> HsRecField' id a -> b
foldMap' :: (a -> m) -> HsRecField' id a -> m
$cfoldMap' :: forall id m a. Monoid m => (a -> m) -> HsRecField' id a -> m
foldMap :: (a -> m) -> HsRecField' id a -> m
$cfoldMap :: forall id m a. Monoid m => (a -> m) -> HsRecField' id a -> m
fold :: HsRecField' id m -> m
$cfold :: forall id m. Monoid m => HsRecField' id m -> m
Foldable, Functor (HsRecField' id)
Foldable (HsRecField' id)
(Functor (HsRecField' id), Foldable (HsRecField' id)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> HsRecField' id a -> f (HsRecField' id b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    HsRecField' id (f a) -> f (HsRecField' id a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> HsRecField' id a -> m (HsRecField' id b))
-> (forall (m :: * -> *) a.
    Monad m =>
    HsRecField' id (m a) -> m (HsRecField' id a))
-> Traversable (HsRecField' id)
(a -> f b) -> HsRecField' id a -> f (HsRecField' id b)
forall id. Functor (HsRecField' id)
forall id. Foldable (HsRecField' id)
forall id (m :: * -> *) a.
Monad m =>
HsRecField' id (m a) -> m (HsRecField' id a)
forall id (f :: * -> *) a.
Applicative f =>
HsRecField' id (f a) -> f (HsRecField' id a)
forall id (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HsRecField' id a -> m (HsRecField' id b)
forall id (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HsRecField' id a -> f (HsRecField' id b)
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
HsRecField' id (m a) -> m (HsRecField' id a)
forall (f :: * -> *) a.
Applicative f =>
HsRecField' id (f a) -> f (HsRecField' id a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HsRecField' id a -> m (HsRecField' id b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HsRecField' id a -> f (HsRecField' id b)
sequence :: HsRecField' id (m a) -> m (HsRecField' id a)
$csequence :: forall id (m :: * -> *) a.
Monad m =>
HsRecField' id (m a) -> m (HsRecField' id a)
mapM :: (a -> m b) -> HsRecField' id a -> m (HsRecField' id b)
$cmapM :: forall id (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HsRecField' id a -> m (HsRecField' id b)
sequenceA :: HsRecField' id (f a) -> f (HsRecField' id a)
$csequenceA :: forall id (f :: * -> *) a.
Applicative f =>
HsRecField' id (f a) -> f (HsRecField' id a)
traverse :: (a -> f b) -> HsRecField' id a -> f (HsRecField' id b)
$ctraverse :: forall id (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HsRecField' id a -> f (HsRecField' id b)
$cp2Traversable :: forall id. Foldable (HsRecField' id)
$cp1Traversable :: forall id. Functor (HsRecField' id)
Traversable)


-- Note [Punning]
-- ~~~~~~~~~~~~~~
-- If you write T { x, y = v+1 }, the HsRecFields will be
--      HsRecField x x True ...
--      HsRecField y (v+1) False ...
-- That is, for "punned" field x is expanded (in the renamer)
-- to x=x; but with a punning flag so we can detect it later
-- (e.g. when pretty printing)
--
-- If the original field was qualified, we un-qualify it, thus
--    T { A.x } means T { A.x = x }


-- Note [HsRecField and HsRecUpdField]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- A HsRecField (used for record construction and pattern matching)
-- contains an unambiguous occurrence of a field (i.e. a FieldOcc).
-- We can't just store the Name, because thanks to
-- DuplicateRecordFields this may not correspond to the label the user
-- wrote.
--
-- A HsRecUpdField (used for record update) contains a potentially
-- ambiguous occurrence of a field (an AmbiguousFieldOcc).  The
-- renamer will fill in the selector function if it can, but if the
-- selector is ambiguous the renamer will defer to the typechecker.
-- After the typechecker, a unique selector will have been determined.
--
-- The renamer produces an Unambiguous result if it can, rather than
-- just doing the lookup in the typechecker, so that completely
-- unambiguous updates can be represented by 'DsMeta.repUpdFields'.
--
-- For example, suppose we have:
--
--     data S = MkS { x :: Int }
--     data T = MkT { x :: Int }
--
--     f z = (z { x = 3 }) :: S
--
-- The parsed HsRecUpdField corresponding to the record update will have:
--
--     hsRecFieldLbl = Unambiguous "x" NoExt :: AmbiguousFieldOcc RdrName
--
-- After the renamer, this will become:
--
--     hsRecFieldLbl = Ambiguous   "x" NoExt :: AmbiguousFieldOcc Name
--
-- (note that the Unambiguous constructor is not type-correct here).
-- The typechecker will determine the particular selector:
--
--     hsRecFieldLbl = Unambiguous "x" $sel:x:MkS  :: AmbiguousFieldOcc Id
--
-- See also Note [Disambiguating record fields] in TcExpr.

hsRecFields :: HsRecFields p arg -> [XCFieldOcc p]
hsRecFields :: HsRecFields p arg -> [XCFieldOcc p]
hsRecFields rbinds :: HsRecFields p arg
rbinds = (LHsRecField p arg -> XCFieldOcc p)
-> [LHsRecField p arg] -> [XCFieldOcc p]
forall a b. (a -> b) -> [a] -> [b]
map (Located (XCFieldOcc p) -> XCFieldOcc p
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (Located (XCFieldOcc p) -> XCFieldOcc p)
-> (LHsRecField p arg -> Located (XCFieldOcc p))
-> LHsRecField p arg
-> XCFieldOcc p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField p arg -> Located (XCFieldOcc p)
forall pass arg. HsRecField pass arg -> Located (XCFieldOcc pass)
hsRecFieldSel (HsRecField p arg -> Located (XCFieldOcc p))
-> (LHsRecField p arg -> HsRecField p arg)
-> LHsRecField p arg
-> Located (XCFieldOcc p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecField p arg -> HsRecField p arg
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc) (HsRecFields p arg -> [LHsRecField p arg]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields p arg
rbinds)

-- Probably won't typecheck at once, things have changed :/
hsRecFieldsArgs :: HsRecFields p arg -> [arg]
hsRecFieldsArgs :: HsRecFields p arg -> [arg]
hsRecFieldsArgs rbinds :: HsRecFields p arg
rbinds = (LHsRecField p arg -> arg) -> [LHsRecField p arg] -> [arg]
forall a b. (a -> b) -> [a] -> [b]
map (HsRecField' (FieldOcc p) arg -> arg
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg (HsRecField' (FieldOcc p) arg -> arg)
-> (LHsRecField p arg -> HsRecField' (FieldOcc p) arg)
-> LHsRecField p arg
-> arg
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecField p arg -> HsRecField' (FieldOcc p) arg
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc) (HsRecFields p arg -> [LHsRecField p arg]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields p arg
rbinds)

hsRecFieldSel :: HsRecField pass arg -> Located (XCFieldOcc pass)
hsRecFieldSel :: HsRecField pass arg -> Located (XCFieldOcc pass)
hsRecFieldSel = (FieldOcc pass -> XCFieldOcc pass)
-> GenLocated SrcSpan (FieldOcc pass) -> Located (XCFieldOcc pass)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FieldOcc pass -> XCFieldOcc pass
forall pass. FieldOcc pass -> XCFieldOcc pass
extFieldOcc (GenLocated SrcSpan (FieldOcc pass) -> Located (XCFieldOcc pass))
-> (HsRecField pass arg -> GenLocated SrcSpan (FieldOcc pass))
-> HsRecField pass arg
-> Located (XCFieldOcc pass)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField pass arg -> GenLocated SrcSpan (FieldOcc pass)
forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl

hsRecFieldId :: HsRecField GhcTc arg -> Located Id
hsRecFieldId :: HsRecField GhcTc arg -> Located TyVar
hsRecFieldId = HsRecField GhcTc arg -> Located TyVar
forall pass arg. HsRecField pass arg -> Located (XCFieldOcc pass)
hsRecFieldSel

hsRecUpdFieldRdr :: HsRecUpdField (GhcPass p) -> Located RdrName
hsRecUpdFieldRdr :: HsRecUpdField (GhcPass p) -> Located RdrName
hsRecUpdFieldRdr = (AmbiguousFieldOcc (GhcPass p) -> RdrName)
-> GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass p))
-> Located RdrName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AmbiguousFieldOcc (GhcPass p) -> RdrName
forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc (GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass p))
 -> Located RdrName)
-> (HsRecUpdField (GhcPass p)
    -> GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass p)))
-> HsRecUpdField (GhcPass p)
-> Located RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecUpdField (GhcPass p)
-> GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass p))
forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl

hsRecUpdFieldId :: HsRecField' (AmbiguousFieldOcc GhcTc) arg -> Located Id
hsRecUpdFieldId :: HsRecField' (AmbiguousFieldOcc GhcTc) arg -> Located TyVar
hsRecUpdFieldId = (FieldOcc GhcTc -> TyVar)
-> GenLocated SrcSpan (FieldOcc GhcTc) -> Located TyVar
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FieldOcc GhcTc -> TyVar
forall pass. FieldOcc pass -> XCFieldOcc pass
extFieldOcc (GenLocated SrcSpan (FieldOcc GhcTc) -> Located TyVar)
-> (HsRecField' (AmbiguousFieldOcc GhcTc) arg
    -> GenLocated SrcSpan (FieldOcc GhcTc))
-> HsRecField' (AmbiguousFieldOcc GhcTc) arg
-> Located TyVar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField' (AmbiguousFieldOcc GhcTc) arg
-> GenLocated SrcSpan (FieldOcc GhcTc)
forall arg.
HsRecField' (AmbiguousFieldOcc GhcTc) arg
-> GenLocated SrcSpan (FieldOcc GhcTc)
hsRecUpdFieldOcc

hsRecUpdFieldOcc :: HsRecField' (AmbiguousFieldOcc GhcTc) arg -> LFieldOcc GhcTc
hsRecUpdFieldOcc :: HsRecField' (AmbiguousFieldOcc GhcTc) arg
-> GenLocated SrcSpan (FieldOcc GhcTc)
hsRecUpdFieldOcc = (AmbiguousFieldOcc GhcTc -> FieldOcc GhcTc)
-> GenLocated SrcSpan (AmbiguousFieldOcc GhcTc)
-> GenLocated SrcSpan (FieldOcc GhcTc)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AmbiguousFieldOcc GhcTc -> FieldOcc GhcTc
unambiguousFieldOcc (GenLocated SrcSpan (AmbiguousFieldOcc GhcTc)
 -> GenLocated SrcSpan (FieldOcc GhcTc))
-> (HsRecField' (AmbiguousFieldOcc GhcTc) arg
    -> GenLocated SrcSpan (AmbiguousFieldOcc GhcTc))
-> HsRecField' (AmbiguousFieldOcc GhcTc) arg
-> GenLocated SrcSpan (FieldOcc GhcTc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField' (AmbiguousFieldOcc GhcTc) arg
-> GenLocated SrcSpan (AmbiguousFieldOcc GhcTc)
forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl


{-
************************************************************************
*                                                                      *
*              Printing patterns
*                                                                      *
************************************************************************
-}

instance (p ~ GhcPass pass, OutputableBndrId p) => Outputable (Pat p) where
    ppr :: Pat p -> SDoc
ppr = Pat p -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
Pat (GhcPass p) -> SDoc
pprPat

pprPatBndr :: OutputableBndr name => name -> SDoc
pprPatBndr :: name -> SDoc
pprPatBndr var :: name
var                  -- Print with type info if -dppr-debug is on
  = (PprStyle -> SDoc) -> SDoc
getPprStyle ((PprStyle -> SDoc) -> SDoc) -> (PprStyle -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ sty :: PprStyle
sty ->
    if PprStyle -> Bool
debugStyle PprStyle
sty then
        SDoc -> SDoc
parens (BindingSite -> name -> SDoc
forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LambdaBind name
var)         -- Could pass the site to pprPat
                                                -- but is it worth it?
    else
        name -> SDoc
forall a. OutputableBndr a => a -> SDoc
pprPrefixOcc name
var

pprParendLPat :: (OutputableBndrId (GhcPass p))
              => PprPrec -> LPat (GhcPass p) -> SDoc
pprParendLPat :: PprPrec -> LPat (GhcPass p) -> SDoc
pprParendLPat p :: PprPrec
p = PprPrec -> LPat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendPat PprPrec
p (LPat (GhcPass p) -> SDoc)
-> (LPat (GhcPass p) -> LPat (GhcPass p))
-> LPat (GhcPass p)
-> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LPat (GhcPass p) -> LPat (GhcPass p)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc

pprParendPat :: (OutputableBndrId (GhcPass p))
             => PprPrec -> Pat (GhcPass p) -> SDoc
pprParendPat :: PprPrec -> Pat (GhcPass p) -> SDoc
pprParendPat p :: PprPrec
p pat :: Pat (GhcPass p)
pat = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ dflags :: DynFlags
dflags ->
                     if DynFlags -> Pat (GhcPass p) -> Bool
need_parens DynFlags
dflags Pat (GhcPass p)
pat
                     then SDoc -> SDoc
parens (Pat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
Pat (GhcPass p) -> SDoc
pprPat Pat (GhcPass p)
pat)
                     else  Pat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
Pat (GhcPass p) -> SDoc
pprPat Pat (GhcPass p)
pat
  where
    need_parens :: DynFlags -> Pat (GhcPass p) -> Bool
need_parens dflags :: DynFlags
dflags pat :: Pat (GhcPass p)
pat
      | CoPat {} <- Pat (GhcPass p)
pat = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_PrintTypecheckerElaboration DynFlags
dflags
      | Bool
otherwise       = PprPrec -> Pat (GhcPass p) -> Bool
forall p. PprPrec -> Pat p -> Bool
patNeedsParens PprPrec
p Pat (GhcPass p)
pat
      -- For a CoPat we need parens if we are going to show it, which
      -- we do if -fprint-typechecker-elaboration is on (c.f. pprHsWrapper)
      -- But otherwise the CoPat is discarded, so it
      -- is the pattern inside that matters.  Sigh.

pprPat :: (OutputableBndrId (GhcPass p)) => Pat (GhcPass p) -> SDoc
pprPat :: Pat (GhcPass p) -> SDoc
pprPat (VarPat _ lvar :: Located (IdP (GhcPass p))
lvar)          = IdP (GhcPass p) -> SDoc
forall a. OutputableBndr a => a -> SDoc
pprPatBndr (Located (IdP (GhcPass p))
-> SrcSpanLess (Located (IdP (GhcPass p)))
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Located (IdP (GhcPass p))
lvar)
pprPat (WildPat _)              = Char -> SDoc
char '_'
pprPat (LazyPat _ pat :: Pat (GhcPass p)
pat)          = Char -> SDoc
char '~' SDoc -> SDoc -> SDoc
<> PprPrec -> Pat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendLPat PprPrec
appPrec Pat (GhcPass p)
pat
pprPat (BangPat _ pat :: Pat (GhcPass p)
pat)          = Char -> SDoc
char '!' SDoc -> SDoc -> SDoc
<> PprPrec -> Pat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendLPat PprPrec
appPrec Pat (GhcPass p)
pat
pprPat (AsPat _ name :: Located (IdP (GhcPass p))
name pat :: Pat (GhcPass p)
pat)       = [SDoc] -> SDoc
hcat [IdP (GhcPass p) -> SDoc
forall a. OutputableBndr a => a -> SDoc
pprPrefixOcc (Located (IdP (GhcPass p))
-> SrcSpanLess (Located (IdP (GhcPass p)))
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Located (IdP (GhcPass p))
name), Char -> SDoc
char '@',
                                        PprPrec -> Pat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendLPat PprPrec
appPrec Pat (GhcPass p)
pat]
pprPat (ViewPat _ expr :: LHsExpr (GhcPass p)
expr pat :: Pat (GhcPass p)
pat)     = [SDoc] -> SDoc
hcat [LHsExpr (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
LHsExpr (GhcPass p) -> SDoc
pprLExpr LHsExpr (GhcPass p)
expr, String -> SDoc
text " -> ", Pat (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat (GhcPass p)
pat]
pprPat (ParPat _ pat :: Pat (GhcPass p)
pat)           = SDoc -> SDoc
parens (Pat (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat (GhcPass p)
pat)
pprPat (LitPat _ s :: HsLit (GhcPass p)
s)             = HsLit (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsLit (GhcPass p)
s
pprPat (NPat _ l :: Located (HsOverLit (GhcPass p))
l Nothing  _)    = Located (HsOverLit (GhcPass p)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located (HsOverLit (GhcPass p))
l
pprPat (NPat _ l :: Located (HsOverLit (GhcPass p))
l (Just _) _)    = Char -> SDoc
char '-' SDoc -> SDoc -> SDoc
<> Located (HsOverLit (GhcPass p)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located (HsOverLit (GhcPass p))
l
pprPat (NPlusKPat _ n :: Located (IdP (GhcPass p))
n k :: Located (HsOverLit (GhcPass p))
k _ _ _)  = [SDoc] -> SDoc
hcat [Located (IdP (GhcPass p)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located (IdP (GhcPass p))
n, Char -> SDoc
char '+', Located (HsOverLit (GhcPass p)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located (HsOverLit (GhcPass p))
k]
pprPat (SplicePat _ splice :: HsSplice (GhcPass p)
splice)     = HsSplice (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
HsSplice (GhcPass p) -> SDoc
pprSplice HsSplice (GhcPass p)
splice
pprPat (CoPat _ co :: HsWrapper
co pat :: Pat (GhcPass p)
pat _)       = HsWrapper -> (Bool -> SDoc) -> SDoc
pprHsWrapper HsWrapper
co ((Bool -> SDoc) -> SDoc) -> (Bool -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \parens :: Bool
parens
                                            -> if Bool
parens
                                                 then PprPrec -> Pat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendPat PprPrec
appPrec Pat (GhcPass p)
pat
                                                 else Pat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
Pat (GhcPass p) -> SDoc
pprPat Pat (GhcPass p)
pat
pprPat (SigPat _ pat :: Pat (GhcPass p)
pat ty :: LHsSigWcType (NoGhcTc (GhcPass p))
ty)        = Pat (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat (GhcPass p)
pat SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> LHsSigWcType (GhcPass (NoGhcTcPass (NoGhcTcPass p))) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsSigWcType (NoGhcTc (GhcPass p))
LHsSigWcType (GhcPass (NoGhcTcPass (NoGhcTcPass p)))
ty
pprPat (ListPat _ pats :: [Pat (GhcPass p)]
pats)         = SDoc -> SDoc
brackets ([Pat (GhcPass p)] -> SDoc
forall a. Outputable a => [a] -> SDoc
interpp'SP [Pat (GhcPass p)]
pats)
pprPat (TuplePat _ pats :: [Pat (GhcPass p)]
pats bx :: Boxity
bx)     = TupleSort -> SDoc -> SDoc
tupleParens (Boxity -> TupleSort
boxityTupleSort Boxity
bx)
                                              ((Pat (GhcPass p) -> SDoc) -> [Pat (GhcPass p)] -> SDoc
forall a. (a -> SDoc) -> [a] -> SDoc
pprWithCommas Pat (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Pat (GhcPass p)]
pats)
pprPat (SumPat _ pat :: Pat (GhcPass p)
pat alt :: Int
alt arity :: Int
arity) = SDoc -> SDoc
sumParens ((Pat (GhcPass p) -> SDoc) -> Pat (GhcPass p) -> Int -> Int -> SDoc
forall a. (a -> SDoc) -> a -> Int -> Int -> SDoc
pprAlternative Pat (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat (GhcPass p)
pat Int
alt Int
arity)
pprPat (ConPatIn con :: Located (IdP (GhcPass p))
con details :: HsConPatDetails (GhcPass p)
details)   = IdP (GhcPass p) -> HsConPatDetails (GhcPass p) -> SDoc
forall con (p :: Pass).
(OutputableBndr con, OutputableBndrId (GhcPass p)) =>
con -> HsConPatDetails (GhcPass p) -> SDoc
pprUserCon (Located (IdP (GhcPass p))
-> SrcSpanLess (Located (IdP (GhcPass p)))
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Located (IdP (GhcPass p))
con) HsConPatDetails (GhcPass p)
details
pprPat (ConPatOut { pat_con :: forall p. Pat p -> Located ConLike
pat_con = Located ConLike
con
                  , pat_tvs :: forall p. Pat p -> [TyVar]
pat_tvs = [TyVar]
tvs
                  , pat_dicts :: forall p. Pat p -> [TyVar]
pat_dicts = [TyVar]
dicts
                  , pat_binds :: forall p. Pat p -> TcEvBinds
pat_binds = TcEvBinds
binds
                  , pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args = HsConPatDetails (GhcPass p)
details })
  = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
       -- Tiresome; in TcBinds.tcRhs we print out a
       -- typechecked Pat in an error message,
       -- and we want to make sure it prints nicely
    if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_PrintTypecheckerElaboration DynFlags
dflags then
        Located ConLike -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located ConLike
con
          SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
braces ([SDoc] -> SDoc
sep [ [SDoc] -> SDoc
hsep ((TyVar -> SDoc) -> [TyVar] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map TyVar -> SDoc
forall a. OutputableBndr a => a -> SDoc
pprPatBndr ([TyVar]
tvs [TyVar] -> [TyVar] -> [TyVar]
forall a. [a] -> [a] -> [a]
++ [TyVar]
dicts))
                         , TcEvBinds -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcEvBinds
binds])
          SDoc -> SDoc -> SDoc
<+> HsConPatDetails (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
HsConPatDetails (GhcPass p) -> SDoc
pprConArgs HsConPatDetails (GhcPass p)
details
    else ConLike -> HsConPatDetails (GhcPass p) -> SDoc
forall con (p :: Pass).
(OutputableBndr con, OutputableBndrId (GhcPass p)) =>
con -> HsConPatDetails (GhcPass p) -> SDoc
pprUserCon (Located ConLike -> SrcSpanLess (Located ConLike)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Located ConLike
con) HsConPatDetails (GhcPass p)
details
pprPat (XPat x :: XXPat (GhcPass p)
x)               = Located (Pat (GhcPass p)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located (Pat (GhcPass p))
XXPat (GhcPass p)
x


pprUserCon :: (OutputableBndr con, OutputableBndrId (GhcPass p))
           => con -> HsConPatDetails (GhcPass p) -> SDoc
pprUserCon :: con -> HsConPatDetails (GhcPass p) -> SDoc
pprUserCon c :: con
c (InfixCon p1 :: LPat (GhcPass p)
p1 p2 :: LPat (GhcPass p)
p2) = LPat (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LPat (GhcPass p)
p1 SDoc -> SDoc -> SDoc
<+> con -> SDoc
forall a. OutputableBndr a => a -> SDoc
pprInfixOcc con
c SDoc -> SDoc -> SDoc
<+> LPat (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LPat (GhcPass p)
p2
pprUserCon c :: con
c details :: HsConPatDetails (GhcPass p)
details          = con -> SDoc
forall a. OutputableBndr a => a -> SDoc
pprPrefixOcc con
c SDoc -> SDoc -> SDoc
<+> HsConPatDetails (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
HsConPatDetails (GhcPass p) -> SDoc
pprConArgs HsConPatDetails (GhcPass p)
details

pprConArgs :: (OutputableBndrId (GhcPass p))
           => HsConPatDetails (GhcPass p) -> SDoc
pprConArgs :: HsConPatDetails (GhcPass p) -> SDoc
pprConArgs (PrefixCon pats :: [LPat (GhcPass p)]
pats) = [SDoc] -> SDoc
sep ((LPat (GhcPass p) -> SDoc) -> [LPat (GhcPass p)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (PprPrec -> LPat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendLPat PprPrec
appPrec) [LPat (GhcPass p)]
pats)
pprConArgs (InfixCon p1 :: LPat (GhcPass p)
p1 p2 :: LPat (GhcPass p)
p2) = [SDoc] -> SDoc
sep [ PprPrec -> LPat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendLPat PprPrec
appPrec LPat (GhcPass p)
p1
                                  , PprPrec -> LPat (GhcPass p) -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
PprPrec -> Pat (GhcPass p) -> SDoc
pprParendLPat PprPrec
appPrec LPat (GhcPass p)
p2 ]
pprConArgs (RecCon rpats :: HsRecFields (GhcPass p) (LPat (GhcPass p))
rpats)   = HsRecFields (GhcPass p) (LPat (GhcPass p)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsRecFields (GhcPass p) (LPat (GhcPass p))
rpats

instance (Outputable arg)
      => Outputable (HsRecFields p arg) where
  ppr :: HsRecFields p arg -> SDoc
ppr (HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField p arg]
flds, rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe Int
rec_dotdot = Maybe Int
Nothing })
        = SDoc -> SDoc
braces ([SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ((LHsRecField p arg -> SDoc) -> [LHsRecField p arg] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map LHsRecField p arg -> SDoc
forall a. Outputable a => a -> SDoc
ppr [LHsRecField p arg]
flds)))
  ppr (HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField p arg]
flds, rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe Int
rec_dotdot = Just n :: Int
n })
        = SDoc -> SDoc
braces ([SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ((LHsRecField p arg -> SDoc) -> [LHsRecField p arg] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map LHsRecField p arg -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Int -> [LHsRecField p arg] -> [LHsRecField p arg]
forall a. Int -> [a] -> [a]
take Int
n [LHsRecField p arg]
flds) [SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ [SDoc
dotdot])))
        where
          dotdot :: SDoc
dotdot = String -> SDoc
text ".." SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
whenPprDebug ([LHsRecField p arg] -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Int -> [LHsRecField p arg] -> [LHsRecField p arg]
forall a. Int -> [a] -> [a]
drop Int
n [LHsRecField p arg]
flds))

instance (Outputable p, Outputable arg)
      => Outputable (HsRecField' p arg) where
  ppr :: HsRecField' p arg -> SDoc
ppr (HsRecField { hsRecFieldLbl :: forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl = Located p
f, hsRecFieldArg :: forall id arg. HsRecField' id arg -> arg
hsRecFieldArg = arg
arg,
                    hsRecPun :: forall id a. HsRecField' id a -> Bool
hsRecPun = Bool
pun })
    = Located p -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located p
f SDoc -> SDoc -> SDoc
<+> (Bool -> SDoc -> SDoc
ppUnless Bool
pun (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ SDoc
equals SDoc -> SDoc -> SDoc
<+> arg -> SDoc
forall a. Outputable a => a -> SDoc
ppr arg
arg)


{-
************************************************************************
*                                                                      *
*              Building patterns
*                                                                      *
************************************************************************
-}

mkPrefixConPat :: DataCon ->
                  [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
-- Make a vanilla Prefix constructor pattern
mkPrefixConPat :: DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
mkPrefixConPat dc :: DataCon
dc pats :: [OutPat (GhcPass p)]
pats tys :: [Type]
tys
  = SrcSpanLess (OutPat (GhcPass p)) -> OutPat (GhcPass p)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (OutPat (GhcPass p)) -> OutPat (GhcPass p))
-> SrcSpanLess (OutPat (GhcPass p)) -> OutPat (GhcPass p)
forall a b. (a -> b) -> a -> b
$ ConPatOut :: forall p.
Located ConLike
-> [Type]
-> [TyVar]
-> [TyVar]
-> TcEvBinds
-> HsConPatDetails p
-> HsWrapper
-> Pat p
ConPatOut { pat_con :: Located ConLike
pat_con = SrcSpanLess (Located ConLike) -> Located ConLike
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (DataCon -> ConLike
RealDataCon DataCon
dc)
                      , pat_tvs :: [TyVar]
pat_tvs = []
                      , pat_dicts :: [TyVar]
pat_dicts = []
                      , pat_binds :: TcEvBinds
pat_binds = TcEvBinds
emptyTcEvBinds
                      , pat_args :: HsConPatDetails (GhcPass p)
pat_args = [OutPat (GhcPass p)] -> HsConPatDetails (GhcPass p)
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [OutPat (GhcPass p)]
pats
                      , pat_arg_tys :: [Type]
pat_arg_tys = [Type]
tys
                      , pat_wrap :: HsWrapper
pat_wrap = HsWrapper
idHsWrapper }

mkNilPat :: Type -> OutPat (GhcPass p)
mkNilPat :: Type -> OutPat (GhcPass p)
mkNilPat ty :: Type
ty = DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
forall (p :: Pass).
DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
mkPrefixConPat DataCon
nilDataCon [] [Type
ty]

mkCharLitPat :: SourceText -> Char -> OutPat (GhcPass p)
mkCharLitPat :: SourceText -> Char -> OutPat (GhcPass p)
mkCharLitPat src :: SourceText
src c :: Char
c = DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
forall (p :: Pass).
DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
mkPrefixConPat DataCon
charDataCon
                          [SrcSpanLess (OutPat (GhcPass p)) -> OutPat (GhcPass p)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (OutPat (GhcPass p)) -> OutPat (GhcPass p))
-> SrcSpanLess (OutPat (GhcPass p)) -> OutPat (GhcPass p)
forall a b. (a -> b) -> a -> b
$ XLitPat (GhcPass p) -> HsLit (GhcPass p) -> OutPat (GhcPass p)
forall p. XLitPat p -> HsLit p -> Pat p
LitPat XLitPat (GhcPass p)
NoExt
NoExt (XHsCharPrim (GhcPass p) -> Char -> HsLit (GhcPass p)
forall x. XHsCharPrim x -> Char -> HsLit x
HsCharPrim SourceText
XHsCharPrim (GhcPass p)
src Char
c)] []

{-
************************************************************************
*                                                                      *
* Predicates for checking things about pattern-lists in EquationInfo   *
*                                                                      *
************************************************************************

\subsection[Pat-list-predicates]{Look for interesting things in patterns}

Unlike in the Wadler chapter, where patterns are either ``variables''
or ``constructors,'' here we distinguish between:
\begin{description}
\item[unfailable:]
Patterns that cannot fail to match: variables, wildcards, and lazy
patterns.

These are the irrefutable patterns; the two other categories
are refutable patterns.

\item[constructor:]
A non-literal constructor pattern (see next category).

\item[literal patterns:]
At least the numeric ones may be overloaded.
\end{description}

A pattern is in {\em exactly one} of the above three categories; `as'
patterns are treated specially, of course.

The 1.3 report defines what ``irrefutable'' and ``failure-free'' patterns are.
-}

isBangedLPat :: LPat (GhcPass p) -> Bool
isBangedLPat :: LPat (GhcPass p) -> Bool
isBangedLPat = LPat (GhcPass p) -> Bool
forall (p :: Pass). Pat (GhcPass p) -> Bool
isBangedPat (LPat (GhcPass p) -> Bool)
-> (LPat (GhcPass p) -> LPat (GhcPass p))
-> LPat (GhcPass p)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LPat (GhcPass p) -> LPat (GhcPass p)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc

isBangedPat :: Pat (GhcPass p) -> Bool
isBangedPat :: Pat (GhcPass p) -> Bool
isBangedPat (ParPat _ p :: Pat (GhcPass p)
p) = Pat (GhcPass p) -> Bool
forall (p :: Pass). Pat (GhcPass p) -> Bool
isBangedLPat Pat (GhcPass p)
p
isBangedPat (BangPat {}) = Bool
True
isBangedPat _            = Bool
False

looksLazyPatBind :: HsBind (GhcPass p) -> Bool
-- Returns True of anything *except*
--     a StrictHsBind (as above) or
--     a VarPat
-- In particular, returns True of a pattern binding with a compound pattern, like (I# x)
-- Looks through AbsBinds
looksLazyPatBind :: HsBind (GhcPass p) -> Bool
looksLazyPatBind (PatBind { pat_lhs :: forall idL idR. HsBindLR idL idR -> LPat idL
pat_lhs = LPat (GhcPass p)
p })
  = LPat (GhcPass p) -> Bool
forall (p :: Pass). Pat (GhcPass p) -> Bool
looksLazyLPat LPat (GhcPass p)
p
looksLazyPatBind (AbsBinds { abs_binds :: forall idL idR. HsBindLR idL idR -> LHsBinds idL
abs_binds = LHsBinds (GhcPass p)
binds })
  = (LHsBindLR (GhcPass p) (GhcPass p) -> Bool)
-> LHsBinds (GhcPass p) -> Bool
forall a. (a -> Bool) -> Bag a -> Bool
anyBag (HsBind (GhcPass p) -> Bool
forall (p :: Pass). HsBind (GhcPass p) -> Bool
looksLazyPatBind (HsBind (GhcPass p) -> Bool)
-> (LHsBindLR (GhcPass p) (GhcPass p) -> HsBind (GhcPass p))
-> LHsBindLR (GhcPass p) (GhcPass p)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsBindLR (GhcPass p) (GhcPass p) -> HsBind (GhcPass p)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc) LHsBinds (GhcPass p)
binds
looksLazyPatBind _
  = Bool
False

looksLazyLPat :: LPat (GhcPass p) -> Bool
looksLazyLPat :: LPat (GhcPass p) -> Bool
looksLazyLPat = LPat (GhcPass p) -> Bool
forall (p :: Pass). Pat (GhcPass p) -> Bool
looksLazyPat (LPat (GhcPass p) -> Bool)
-> (LPat (GhcPass p) -> LPat (GhcPass p))
-> LPat (GhcPass p)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LPat (GhcPass p) -> LPat (GhcPass p)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc

looksLazyPat :: Pat (GhcPass p) -> Bool
looksLazyPat :: Pat (GhcPass p) -> Bool
looksLazyPat (ParPat _ p :: Pat (GhcPass p)
p)  = Pat (GhcPass p) -> Bool
forall (p :: Pass). Pat (GhcPass p) -> Bool
looksLazyLPat Pat (GhcPass p)
p
looksLazyPat (AsPat _ _ p :: Pat (GhcPass p)
p) = Pat (GhcPass p) -> Bool
forall (p :: Pass). Pat (GhcPass p) -> Bool
looksLazyLPat Pat (GhcPass p)
p
looksLazyPat (BangPat {})  = Bool
False
looksLazyPat (VarPat {})   = Bool
False
looksLazyPat (WildPat {})  = Bool
False
looksLazyPat _             = Bool
True

isIrrefutableHsPat :: (OutputableBndrId (GhcPass p)) => LPat (GhcPass p) -> Bool
-- (isIrrefutableHsPat p) is true if matching against p cannot fail,
-- in the sense of falling through to the next pattern.
--      (NB: this is not quite the same as the (silly) defn
--      in 3.17.2 of the Haskell 98 report.)
--
-- WARNING: isIrrefutableHsPat returns False if it's in doubt.
-- Specifically on a ConPatIn, which is what it sees for a
-- (LPat Name) in the renamer, it doesn't know the size of the
-- constructor family, so it returns False.  Result: only
-- tuple patterns are considered irrefuable at the renamer stage.
--
-- But if it returns True, the pattern is definitely irrefutable
isIrrefutableHsPat :: LPat (GhcPass p) -> Bool
isIrrefutableHsPat
  = LPat (GhcPass p) -> Bool
goL
  where
    goL :: LPat (GhcPass p) -> Bool
goL = LPat (GhcPass p) -> Bool
go (LPat (GhcPass p) -> Bool)
-> (LPat (GhcPass p) -> LPat (GhcPass p))
-> LPat (GhcPass p)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LPat (GhcPass p) -> LPat (GhcPass p)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc

    go :: LPat (GhcPass p) -> Bool
go (WildPat {})        = Bool
True
    go (VarPat {})         = Bool
True
    go (LazyPat {})        = Bool
True
    go (BangPat _ pat :: LPat (GhcPass p)
pat)     = LPat (GhcPass p) -> Bool
goL LPat (GhcPass p)
pat
    go (CoPat _ _ pat :: LPat (GhcPass p)
pat _)   = LPat (GhcPass p) -> Bool
go  LPat (GhcPass p)
pat
    go (ParPat _ pat :: LPat (GhcPass p)
pat)      = LPat (GhcPass p) -> Bool
goL LPat (GhcPass p)
pat
    go (AsPat _ _ pat :: LPat (GhcPass p)
pat)     = LPat (GhcPass p) -> Bool
goL LPat (GhcPass p)
pat
    go (ViewPat _ _ pat :: LPat (GhcPass p)
pat)   = LPat (GhcPass p) -> Bool
goL LPat (GhcPass p)
pat
    go (SigPat _ pat :: LPat (GhcPass p)
pat _)    = LPat (GhcPass p) -> Bool
goL LPat (GhcPass p)
pat
    go (TuplePat _ pats :: [LPat (GhcPass p)]
pats _) = (LPat (GhcPass p) -> Bool) -> [LPat (GhcPass p)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all LPat (GhcPass p) -> Bool
goL [LPat (GhcPass p)]
pats
    go (SumPat {})         = Bool
False
                    -- See Note [Unboxed sum patterns aren't irrefutable]
    go (ListPat {})        = Bool
False

    go (ConPatIn {})       = Bool
False     -- Conservative
    go (ConPatOut
        { pat_con :: forall p. Pat p -> Located ConLike
pat_con  = (Located ConLike -> Located (SrcSpanLess (Located ConLike))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (RealDataCon con))
        , pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args = HsConPatDetails (GhcPass p)
details })
                           =
      Maybe DataCon -> Bool
forall a. Maybe a -> Bool
isJust (TyCon -> Maybe DataCon
tyConSingleDataCon_maybe (DataCon -> TyCon
dataConTyCon DataCon
con))
      -- NB: tyConSingleDataCon_maybe, *not* isProductTyCon, because
      -- the latter is false of existentials. See Trac #4439
      Bool -> Bool -> Bool
&& (LPat (GhcPass p) -> Bool) -> [LPat (GhcPass p)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all LPat (GhcPass p) -> Bool
goL (HsConPatDetails (GhcPass p) -> [LPat (GhcPass p)]
forall p. HsConPatDetails p -> [LPat p]
hsConPatArgs HsConPatDetails (GhcPass p)
details)
    go (ConPatOut
        { pat_con :: forall p. Pat p -> Located ConLike
pat_con = (Located ConLike -> Located (SrcSpanLess (Located ConLike))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (PatSynCon _pat)) })
                           = Bool
False -- Conservative
    go (ConPatOut{})       = String -> Bool
forall a. String -> a
panic "ConPatOut:Impossible Match" -- due to #15884
    go (LitPat {})         = Bool
False
    go (NPat {})           = Bool
False
    go (NPlusKPat {})      = Bool
False

    -- We conservatively assume that no TH splices are irrefutable
    -- since we cannot know until the splice is evaluated.
    go (SplicePat {})      = Bool
False

    go (XPat {})           = Bool
False

{- Note [Unboxed sum patterns aren't irrefutable]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unlike unboxed tuples, unboxed sums are *not* irrefutable when used as
patterns. A simple example that demonstrates this is from #14228:

  pattern Just' x = (# x | #)
  pattern Nothing' = (# | () #)

  foo x = case x of
    Nothing' -> putStrLn "nothing"
    Just'    -> putStrLn "just"

In foo, the pattern Nothing' (that is, (# x | #)) is certainly not irrefutable,
as does not match an unboxed sum value of the same arity—namely, (# | y #)
(covered by Just'). In fact, no unboxed sum pattern is irrefutable, since the
minimum unboxed sum arity is 2.

Failing to mark unboxed sum patterns as non-irrefutable would cause the Just'
case in foo to be unreachable, as GHC would mistakenly believe that Nothing'
is the only thing that could possibly be matched!
-}

-- | @'patNeedsParens' p pat@ returns 'True' if the pattern @pat@ needs
-- parentheses under precedence @p@.
patNeedsParens :: PprPrec -> Pat p -> Bool
patNeedsParens :: PprPrec -> Pat p -> Bool
patNeedsParens p :: PprPrec
p = Pat p -> Bool
go
  where
    go :: Pat p -> Bool
go (NPlusKPat {})    = PprPrec
p PprPrec -> PprPrec -> Bool
forall a. Ord a => a -> a -> Bool
> PprPrec
opPrec
    go (SplicePat {})    = Bool
False
    go (ConPatIn _ ds :: HsConPatDetails p
ds)   = PprPrec -> HsConPatDetails p -> Bool
forall a b. PprPrec -> HsConDetails a b -> Bool
conPatNeedsParens PprPrec
p HsConPatDetails p
ds
    go cp :: Pat p
cp@(ConPatOut {}) = PprPrec -> HsConPatDetails p -> Bool
forall a b. PprPrec -> HsConDetails a b -> Bool
conPatNeedsParens PprPrec
p (Pat p -> HsConPatDetails p
forall p. Pat p -> HsConPatDetails p
pat_args Pat p
cp)
    go (SigPat {})       = PprPrec
p PprPrec -> PprPrec -> Bool
forall a. Ord a => a -> a -> Bool
>= PprPrec
sigPrec
    go (ViewPat {})      = Bool
True
    go (CoPat _ _ p :: Pat p
p _)   = Pat p -> Bool
go Pat p
p
    go (WildPat {})      = Bool
False
    go (VarPat {})       = Bool
False
    go (LazyPat {})      = Bool
False
    go (BangPat {})      = Bool
False
    go (ParPat {})       = Bool
False
    go (AsPat {})        = Bool
False
    go (TuplePat {})     = Bool
False
    go (SumPat {})       = Bool
False
    go (ListPat {})      = Bool
False
    go (LitPat _ l :: HsLit p
l)      = PprPrec -> HsLit p -> Bool
forall x. PprPrec -> HsLit x -> Bool
hsLitNeedsParens PprPrec
p HsLit p
l
    go (NPat _ lol :: Located (HsOverLit p)
lol _ _)  = PprPrec -> HsOverLit p -> Bool
forall x. PprPrec -> HsOverLit x -> Bool
hsOverLitNeedsParens PprPrec
p (Located (HsOverLit p) -> SrcSpanLess (Located (HsOverLit p))
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Located (HsOverLit p)
lol)
    go (XPat {})         = Bool
True -- conservative default

-- | @'conPatNeedsParens' p cp@ returns 'True' if the constructor patterns @cp@
-- needs parentheses under precedence @p@.
conPatNeedsParens :: PprPrec -> HsConDetails a b -> Bool
conPatNeedsParens :: PprPrec -> HsConDetails a b -> Bool
conPatNeedsParens p :: PprPrec
p = HsConDetails a b -> Bool
go
  where
    go :: HsConDetails a b -> Bool
go (PrefixCon args :: [a]
args) = PprPrec
p PprPrec -> PprPrec -> Bool
forall a. Ord a => a -> a -> Bool
>= PprPrec
appPrec Bool -> Bool -> Bool
&& Bool -> Bool
not ([a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
args)
    go (InfixCon {})    = PprPrec
p PprPrec -> PprPrec -> Bool
forall a. Ord a => a -> a -> Bool
>= PprPrec
opPrec
    go (RecCon {})      = Bool
False

-- | @'parenthesizePat' p pat@ checks if @'patNeedsParens' p pat@ is true, and
-- if so, surrounds @pat@ with a 'ParPat'. Otherwise, it simply returns @pat@.
parenthesizePat :: PprPrec -> LPat (GhcPass p) -> LPat (GhcPass p)
parenthesizePat :: PprPrec -> LPat (GhcPass p) -> LPat (GhcPass p)
parenthesizePat p :: PprPrec
p lpat :: LPat (GhcPass p)
lpat@(LPat (GhcPass p) -> Located (SrcSpanLess (LPat (GhcPass p)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc pat :: SrcSpanLess (LPat (GhcPass p))
pat)
  | PprPrec -> LPat (GhcPass p) -> Bool
forall p. PprPrec -> Pat p -> Bool
patNeedsParens PprPrec
p SrcSpanLess (LPat (GhcPass p))
LPat (GhcPass p)
pat = SrcSpan -> SrcSpanLess (LPat (GhcPass p)) -> LPat (GhcPass p)
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
loc (XParPat (GhcPass p) -> LPat (GhcPass p) -> LPat (GhcPass p)
forall p. XParPat p -> Pat p -> Pat p
ParPat XParPat (GhcPass p)
NoExt
NoExt LPat (GhcPass p)
lpat)
  | Bool
otherwise            = LPat (GhcPass p)
lpat

{-
% Collect all EvVars from all constructor patterns
-}

-- May need to add more cases
collectEvVarsPats :: [Pat GhcTc] -> Bag EvVar
collectEvVarsPats :: [Pat GhcTc] -> Bag TyVar
collectEvVarsPats = [Bag TyVar] -> Bag TyVar
forall a. [Bag a] -> Bag a
unionManyBags ([Bag TyVar] -> Bag TyVar)
-> ([Pat GhcTc] -> [Bag TyVar]) -> [Pat GhcTc] -> Bag TyVar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pat GhcTc -> Bag TyVar) -> [Pat GhcTc] -> [Bag TyVar]
forall a b. (a -> b) -> [a] -> [b]
map Pat GhcTc -> Bag TyVar
collectEvVarsPat

collectEvVarsLPat :: LPat GhcTc -> Bag EvVar
collectEvVarsLPat :: Pat GhcTc -> Bag TyVar
collectEvVarsLPat = Pat GhcTc -> Bag TyVar
collectEvVarsPat (Pat GhcTc -> Bag TyVar)
-> (Pat GhcTc -> Pat GhcTc) -> Pat GhcTc -> Bag TyVar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pat GhcTc -> Pat GhcTc
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc

collectEvVarsPat :: Pat GhcTc -> Bag EvVar
collectEvVarsPat :: Pat GhcTc -> Bag TyVar
collectEvVarsPat pat :: Pat GhcTc
pat =
  case Pat GhcTc
pat of
    LazyPat _ p :: Pat GhcTc
p      -> Pat GhcTc -> Bag TyVar
collectEvVarsLPat Pat GhcTc
p
    AsPat _ _ p :: Pat GhcTc
p      -> Pat GhcTc -> Bag TyVar
collectEvVarsLPat Pat GhcTc
p
    ParPat  _ p :: Pat GhcTc
p      -> Pat GhcTc -> Bag TyVar
collectEvVarsLPat Pat GhcTc
p
    BangPat _ p :: Pat GhcTc
p      -> Pat GhcTc -> Bag TyVar
collectEvVarsLPat Pat GhcTc
p
    ListPat _ ps :: [Pat GhcTc]
ps     -> [Bag TyVar] -> Bag TyVar
forall a. [Bag a] -> Bag a
unionManyBags ([Bag TyVar] -> Bag TyVar) -> [Bag TyVar] -> Bag TyVar
forall a b. (a -> b) -> a -> b
$ (Pat GhcTc -> Bag TyVar) -> [Pat GhcTc] -> [Bag TyVar]
forall a b. (a -> b) -> [a] -> [b]
map Pat GhcTc -> Bag TyVar
collectEvVarsLPat [Pat GhcTc]
ps
    TuplePat _ ps :: [Pat GhcTc]
ps _  -> [Bag TyVar] -> Bag TyVar
forall a. [Bag a] -> Bag a
unionManyBags ([Bag TyVar] -> Bag TyVar) -> [Bag TyVar] -> Bag TyVar
forall a b. (a -> b) -> a -> b
$ (Pat GhcTc -> Bag TyVar) -> [Pat GhcTc] -> [Bag TyVar]
forall a b. (a -> b) -> [a] -> [b]
map Pat GhcTc -> Bag TyVar
collectEvVarsLPat [Pat GhcTc]
ps
    SumPat _ p :: Pat GhcTc
p _ _   -> Pat GhcTc -> Bag TyVar
collectEvVarsLPat Pat GhcTc
p
    ConPatOut {pat_dicts :: forall p. Pat p -> [TyVar]
pat_dicts = [TyVar]
dicts, pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args  = HsConPatDetails GhcTc
args}
                     -> Bag TyVar -> Bag TyVar -> Bag TyVar
forall a. Bag a -> Bag a -> Bag a
unionBags ([TyVar] -> Bag TyVar
forall a. [a] -> Bag a
listToBag [TyVar]
dicts)
                                   (Bag TyVar -> Bag TyVar) -> Bag TyVar -> Bag TyVar
forall a b. (a -> b) -> a -> b
$ [Bag TyVar] -> Bag TyVar
forall a. [Bag a] -> Bag a
unionManyBags
                                   ([Bag TyVar] -> Bag TyVar) -> [Bag TyVar] -> Bag TyVar
forall a b. (a -> b) -> a -> b
$ (Pat GhcTc -> Bag TyVar) -> [Pat GhcTc] -> [Bag TyVar]
forall a b. (a -> b) -> [a] -> [b]
map Pat GhcTc -> Bag TyVar
collectEvVarsLPat
                                   ([Pat GhcTc] -> [Bag TyVar]) -> [Pat GhcTc] -> [Bag TyVar]
forall a b. (a -> b) -> a -> b
$ HsConPatDetails GhcTc -> [Pat GhcTc]
forall p. HsConPatDetails p -> [LPat p]
hsConPatArgs HsConPatDetails GhcTc
args
    SigPat  _ p :: Pat GhcTc
p _    -> Pat GhcTc -> Bag TyVar
collectEvVarsLPat Pat GhcTc
p
    CoPat _ _ p :: Pat GhcTc
p _    -> Pat GhcTc -> Bag TyVar
collectEvVarsPat  Pat GhcTc
p
    ConPatIn _  _    -> String -> Bag TyVar
forall a. String -> a
panic "foldMapPatBag: ConPatIn"
    _other_pat :: Pat GhcTc
_other_pat       -> Bag TyVar
forall a. Bag a
emptyBag