    {-# LANGUAGE DeriveDataTypeable #-}

    module GHC.Types.DefaultEnv
       ( ClassDefaults (..)
       , DefaultEnv
       , emptyDefaultEnv
       , isEmptyDefaultEnv
       , defaultEnv
       , unitDefaultEnv
       , lookupDefaultEnv
       , filterDefaultEnv
       , defaultList
       , plusDefaultEnv
       )
    where

    import GHC.Prelude
    import GHC.Core.TyCon (TyCon(tyConName))
    import GHC.Core.TyCon.Env (TyConEnv, emptyTyConEnv, isEmptyTyConEnv, mkTyConEnvWith, unitTyConEnv,
                               filterTyConEnv, nonDetTyConEnvElts, plusTyConEnv)
    import GHC.Hs.Extension (GhcRn)
    import GHC.Tc.Utils.TcType (Type)
    import GHC.Types.Name (Name, nameUnique, stableNameCmp)
    import GHC.Types.Unique.FM (lookupUFM_Directly)
    import GHC.Unit.Module.Warnings (WarningTxt)
    import GHC.Unit.Types (Module)
    import GHC.Utils.Outputable

    import Data.Data (Data)
    import Data.List (sortBy)
    import Data.Function (on)

    -- See Note [Named default declarations] in GHC.Tc.Gen.Default
    -- | Default environment mapping class @TyCon@s to their default type lists
    type DefaultEnv = TyConEnv ClassDefaults

    data ClassDefaults
      = ClassDefaults { ClassDefaults -> TyCon
    cd_class   :: !TyCon  -- ^ always a class constructor
                      , ClassDefaults -> [Type]
    cd_types   :: [Type]
                      , ClassDefaults -> Maybe Module
    cd_module :: Maybe Module
                        -- ^ @Nothing@ for built-in,
                        -- @Just@ the current module or the module whence the default was imported
                        -- see Note [Default exports] in GHC.Tc.Gen.Export
                      , ClassDefaults -> Maybe (WarningTxt GhcRn)
    cd_warn    :: Maybe (WarningTxt GhcRn)
                        -- ^ Warning emitted when the default is used
                      }
      deriving Typeable ClassDefaults
    Typeable ClassDefaults =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> ClassDefaults -> c ClassDefaults)
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c ClassDefaults)
    -> (ClassDefaults -> Constr)
    -> (ClassDefaults -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c ClassDefaults))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e))
        -> Maybe (c ClassDefaults))
    -> ((forall b. Data b => b -> b) -> ClassDefaults -> ClassDefaults)
    -> (forall r r'.
        (r -> r' -> r)
        -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r)
    -> (forall r r'.
        (r' -> r -> r)
        -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r)
    -> (forall u. (forall d. Data d => d -> u) -> ClassDefaults -> [u])
    -> (forall u.
        Int -> (forall d. Data d => d -> u) -> ClassDefaults -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults)
    -> Data ClassDefaults
    ClassDefaults -> Constr
    ClassDefaults -> DataType
    (forall b. Data b => b -> b) -> ClassDefaults -> ClassDefaults
    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) -> ClassDefaults -> u
    forall u. (forall d. Data d => d -> u) -> ClassDefaults -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r
    forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ClassDefaults
    forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ClassDefaults -> c ClassDefaults
    forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ClassDefaults)
    forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ClassDefaults)
    $cgfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ClassDefaults -> c ClassDefaults
    gfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ClassDefaults -> c ClassDefaults
    $cgunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ClassDefaults
    gunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ClassDefaults
    $ctoConstr :: ClassDefaults -> Constr
    toConstr :: ClassDefaults -> Constr
    $cdataTypeOf :: ClassDefaults -> DataType
    dataTypeOf :: ClassDefaults -> DataType
    $cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ClassDefaults)
    dataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ClassDefaults)
    $cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ClassDefaults)
    dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ClassDefaults)
    $cgmapT :: (forall b. Data b => b -> b) -> ClassDefaults -> ClassDefaults
    gmapT :: (forall b. Data b => b -> b) -> ClassDefaults -> ClassDefaults
    $cgmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r
    gmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ClassDefaults -> r
    $cgmapQ :: forall u. (forall d. Data d => d -> u) -> ClassDefaults -> [u]
    gmapQ :: forall u. (forall d. Data d => d -> u) -> ClassDefaults -> [u]
    $cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ClassDefaults -> u
    gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ClassDefaults -> u
    $cgmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    gmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    $cgmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    gmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    $cgmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    gmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ClassDefaults -> m ClassDefaults
    Data

    instance Outputable ClassDefaults where
      ppr :: ClassDefaults -> SDoc
    ppr ClassDefaults {cd_class :: ClassDefaults -> TyCon
    cd_class = TyCon
    cls, cd_types :: ClassDefaults -> [Type]
    cd_types = [Type]
    tys} = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "default" SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> TyCon -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr TyCon
    cls SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc
    parens ([Type] -> SDoc
    forall a. Outputable a => [a] -> SDoc
    interpp'SP [Type]
    tys)

    emptyDefaultEnv :: DefaultEnv
    emptyDefaultEnv :: DefaultEnv
    emptyDefaultEnv = DefaultEnv
    forall a. TyConEnv a
    emptyTyConEnv

    isEmptyDefaultEnv :: DefaultEnv -> Bool
    isEmptyDefaultEnv :: DefaultEnv -> Bool
    isEmptyDefaultEnv = DefaultEnv -> Bool
    forall a. TyConEnv a -> Bool
    isEmptyTyConEnv

    unitDefaultEnv :: ClassDefaults -> DefaultEnv
    unitDefaultEnv :: ClassDefaults -> DefaultEnv
    unitDefaultEnv ClassDefaults
    d = TyCon -> ClassDefaults -> DefaultEnv
    forall a. TyCon -> a -> TyConEnv a
    unitTyConEnv (ClassDefaults -> TyCon
    cd_class ClassDefaults
    d) ClassDefaults
    d

    defaultEnv :: [ClassDefaults] -> DefaultEnv
    defaultEnv :: [ClassDefaults] -> DefaultEnv
    defaultEnv = (ClassDefaults -> TyCon) -> [ClassDefaults] -> DefaultEnv
    forall a. (a -> TyCon) -> [a] -> TyConEnv a
    mkTyConEnvWith ClassDefaults -> TyCon
    cd_class

    defaultList :: DefaultEnv -> [ClassDefaults]
    defaultList :: DefaultEnv -> [ClassDefaults]
    defaultList = (ClassDefaults -> ClassDefaults -> Ordering)
    -> [ClassDefaults] -> [ClassDefaults]
    forall a. (a -> a -> Ordering) -> [a] -> [a]
    sortBy (Name -> Name -> Ordering
    stableNameCmp (Name -> Name -> Ordering)
    -> (ClassDefaults -> Name)
    -> ClassDefaults
    -> ClassDefaults
    -> Ordering
    forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
    `on` (TyCon -> Name
    tyConName (TyCon -> Name)
    -> (ClassDefaults -> TyCon) -> ClassDefaults -> Name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ClassDefaults -> TyCon
    cd_class)) ([ClassDefaults] -> [ClassDefaults])
    -> (DefaultEnv -> [ClassDefaults]) -> DefaultEnv -> [ClassDefaults]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . DefaultEnv -> [ClassDefaults]
    forall a. TyConEnv a -> [a]
    nonDetTyConEnvElts
                  -- sortBy recovers determinism

    lookupDefaultEnv :: DefaultEnv -> Name -> Maybe ClassDefaults
    lookupDefaultEnv :: DefaultEnv -> Name -> Maybe ClassDefaults
    lookupDefaultEnv DefaultEnv
    env = DefaultEnv -> Unique -> Maybe ClassDefaults
    forall {k} (key :: k) elt. UniqFM key elt -> Unique -> Maybe elt
    lookupUFM_Directly DefaultEnv
    env (Unique -> Maybe ClassDefaults)
    -> (Name -> Unique) -> Name -> Maybe ClassDefaults
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Name -> Unique
    nameUnique

    filterDefaultEnv :: (ClassDefaults -> Bool) -> DefaultEnv -> DefaultEnv
    filterDefaultEnv :: (ClassDefaults -> Bool) -> DefaultEnv -> DefaultEnv
    filterDefaultEnv = (ClassDefaults -> Bool) -> DefaultEnv -> DefaultEnv
    forall elt. (elt -> Bool) -> TyConEnv elt -> TyConEnv elt
    filterTyConEnv

    plusDefaultEnv :: DefaultEnv -> DefaultEnv -> DefaultEnv
    plusDefaultEnv :: DefaultEnv -> DefaultEnv -> DefaultEnv
    plusDefaultEnv = DefaultEnv -> DefaultEnv -> DefaultEnv
    forall a. TyConEnv a -> TyConEnv a -> TyConEnv a
    plusTyConEnv