{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PatternSynonyms #-}

{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}

-- | Utility types used within the constraint solver
module GHC.Tc.Solver.Types (
    -- Inert CDictCans
    DictMap, emptyDictMap, findDictsByClass, addDict, addDictCt,
    addDictsByClass, delDict, foldDicts, filterDicts, findDict,
    dictsToBag, partitionDicts,

    FunEqMap, emptyFunEqs, foldFunEqs, findFunEq, insertFunEq,
    findFunEqsByTyCon,

    TcAppMap, isEmptyTcAppMap, insertTcApp, alterTcApp, filterTcAppMap,

    EqualCtList, pattern EqualCtList,
    equalCtListToList, filterEqualCtList, unitEqualCtList,
    listToEqualCtList, addToEqualCtList,
  ) where

import GHC.Prelude

import GHC.Tc.Types.Constraint
import GHC.Tc.Types.Origin
import GHC.Tc.Utils.TcType

import GHC.Core.Class
import GHC.Core.Map.Type
import GHC.Core.Predicate
import GHC.Core.TyCon
import GHC.Core.TyCon.Env

import GHC.Data.Bag
import GHC.Data.Maybe
import GHC.Data.TrieMap
import GHC.Utils.Outputable
import GHC.Utils.Panic

import Data.Foldable
import Data.List.NonEmpty ( NonEmpty(..), nonEmpty, cons )
import qualified Data.List.NonEmpty as NE

{- *********************************************************************
*                                                                      *
                   TcAppMap
*                                                                      *
************************************************************************

Note [Use loose types in inert set]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Whenever we are looking up an inert dictionary (CDictCan) or function
equality (CEqCan), we use a TcAppMap, which uses the Unique of the
class/type family tycon and then a trie which maps the arguments. This
trie does *not* need to match the kinds of the arguments; this Note
explains why.

Consider the types ty0 = (T ty1 ty2 ty3 ty4) and ty0' = (T ty1' ty2' ty3' ty4'),
where ty4 and ty4' have different kinds. Let's further assume that both types
ty0 and ty0' are well-typed. Because the kind of T is closed, it must be that
one of the ty1..ty3 does not match ty1'..ty3' (and that the kind of the fourth
argument to T is dependent on whichever one changed). Since we are matching
all arguments, during the inert-set lookup, we know that ty1..ty3 do indeed
match ty1'..ty3'. Therefore, the kind of ty4 and ty4' must match, too --
without ever looking at it.

Accordingly, we use LooseTypeMap, which skips the kind check when looking
up a type. I (Richard E) believe this is just an optimization, and that
looking at kinds would be harmless.

-}

type TcAppMap a = DTyConEnv (ListMap LooseTypeMap a)
    -- Indexed by tycon then the arg types, using "loose" matching, where
    -- we don't require kind equality. This allows, for example, (a |> co)
    -- to match (a).
    -- See Note [Use loose types in inert set]
    -- Used for types and classes; hence UniqDFM
    -- See Note [foldTM determinism] in GHC.Data.TrieMap for why we use DTyConEnv here

isEmptyTcAppMap :: TcAppMap a -> Bool
isEmptyTcAppMap :: TcAppMap a -> Bool
isEmptyTcAppMap TcAppMap a
m = TcAppMap a -> Bool
forall a. DTyConEnv a -> Bool
isEmptyDTyConEnv TcAppMap a
m

emptyTcAppMap :: TcAppMap a
emptyTcAppMap :: TcAppMap a
emptyTcAppMap = TcAppMap a
forall a. DTyConEnv a
emptyDTyConEnv

findTcApp :: TcAppMap a -> TyCon -> [Type] -> Maybe a
findTcApp :: TcAppMap a -> TyCon -> [Type] -> Maybe a
findTcApp TcAppMap a
m TyCon
tc [Type]
tys = do { ListMap LooseTypeMap a
tys_map <- TcAppMap a -> TyCon -> Maybe (ListMap LooseTypeMap a)
forall a. DTyConEnv a -> TyCon -> Maybe a
lookupDTyConEnv TcAppMap a
m TyCon
tc
                        ; Key (ListMap LooseTypeMap) -> ListMap LooseTypeMap a -> Maybe a
forall (m :: * -> *) b. TrieMap m => Key m -> m b -> Maybe b
lookupTM [Type]
Key (ListMap LooseTypeMap)
tys ListMap LooseTypeMap a
tys_map }

delTcApp :: TcAppMap a -> TyCon -> [Type] -> TcAppMap a
delTcApp :: TcAppMap a -> TyCon -> [Type] -> TcAppMap a
delTcApp TcAppMap a
m TyCon
tc [Type]
tys = (ListMap LooseTypeMap a -> ListMap LooseTypeMap a)
-> TcAppMap a -> TyCon -> TcAppMap a
forall a. (a -> a) -> DTyConEnv a -> TyCon -> DTyConEnv a
adjustDTyConEnv (Key (ListMap LooseTypeMap)
-> ListMap LooseTypeMap a -> ListMap LooseTypeMap a
forall (m :: * -> *) a. TrieMap m => Key m -> m a -> m a
deleteTM [Type]
Key (ListMap LooseTypeMap)
tys) TcAppMap a
m TyCon
tc

insertTcApp :: TcAppMap a -> TyCon -> [Type] -> a -> TcAppMap a
insertTcApp :: TcAppMap a -> TyCon -> [Type] -> a -> TcAppMap a
insertTcApp TcAppMap a
m TyCon
tc [Type]
tys a
ct = (Maybe (ListMap LooseTypeMap a) -> Maybe (ListMap LooseTypeMap a))
-> TcAppMap a -> TyCon -> TcAppMap a
forall a.
(Maybe a -> Maybe a) -> DTyConEnv a -> TyCon -> DTyConEnv a
alterDTyConEnv Maybe (ListMap LooseTypeMap a) -> Maybe (ListMap LooseTypeMap a)
alter_tm TcAppMap a
m TyCon
tc
  where
    alter_tm :: Maybe (ListMap LooseTypeMap a) -> Maybe (ListMap LooseTypeMap a)
alter_tm Maybe (ListMap LooseTypeMap a)
mb_tm = ListMap LooseTypeMap a -> Maybe (ListMap LooseTypeMap a)
forall a. a -> Maybe a
Just (Key (ListMap LooseTypeMap)
-> a -> ListMap LooseTypeMap a -> ListMap LooseTypeMap a
forall (m :: * -> *) a. TrieMap m => Key m -> a -> m a -> m a
insertTM [Type]
Key (ListMap LooseTypeMap)
tys a
ct (Maybe (ListMap LooseTypeMap a)
mb_tm Maybe (ListMap LooseTypeMap a)
-> ListMap LooseTypeMap a -> ListMap LooseTypeMap a
forall a. Maybe a -> a -> a
`orElse` ListMap LooseTypeMap a
forall (m :: * -> *) a. TrieMap m => m a
emptyTM))

alterTcApp :: forall a. TcAppMap a -> TyCon -> [Type] -> XT a -> TcAppMap a
alterTcApp :: TcAppMap a -> TyCon -> [Type] -> XT a -> TcAppMap a
alterTcApp TcAppMap a
m TyCon
tc [Type]
tys XT a
upd = (Maybe (ListMap LooseTypeMap a) -> Maybe (ListMap LooseTypeMap a))
-> TcAppMap a -> TyCon -> TcAppMap a
forall a.
(Maybe a -> Maybe a) -> DTyConEnv a -> TyCon -> DTyConEnv a
alterDTyConEnv Maybe (ListMap LooseTypeMap a) -> Maybe (ListMap LooseTypeMap a)
alter_tm TcAppMap a
m TyCon
tc
  where
    alter_tm :: Maybe (ListMap LooseTypeMap a) -> Maybe (ListMap LooseTypeMap a)
    alter_tm :: Maybe (ListMap LooseTypeMap a) -> Maybe (ListMap LooseTypeMap a)
alter_tm Maybe (ListMap LooseTypeMap a)
m_elt = ListMap LooseTypeMap a -> Maybe (ListMap LooseTypeMap a)
forall a. a -> Maybe a
Just (Key (ListMap LooseTypeMap)
-> XT a -> ListMap LooseTypeMap a -> ListMap LooseTypeMap a
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM [Type]
Key (ListMap LooseTypeMap)
tys XT a
upd (Maybe (ListMap LooseTypeMap a)
m_elt Maybe (ListMap LooseTypeMap a)
-> ListMap LooseTypeMap a -> ListMap LooseTypeMap a
forall a. Maybe a -> a -> a
`orElse` ListMap LooseTypeMap a
forall (m :: * -> *) a. TrieMap m => m a
emptyTM))

filterTcAppMap :: forall a. (a -> Bool) -> TcAppMap a -> TcAppMap a
filterTcAppMap :: (a -> Bool) -> TcAppMap a -> TcAppMap a
filterTcAppMap a -> Bool
f TcAppMap a
m = (ListMap LooseTypeMap a -> Maybe (ListMap LooseTypeMap a))
-> TcAppMap a -> TcAppMap a
forall a b. (a -> Maybe b) -> DTyConEnv a -> DTyConEnv b
mapMaybeDTyConEnv ListMap LooseTypeMap a -> Maybe (ListMap LooseTypeMap a)
one_tycon TcAppMap a
m
  where
    one_tycon :: ListMap LooseTypeMap a -> Maybe (ListMap LooseTypeMap a)
    one_tycon :: ListMap LooseTypeMap a -> Maybe (ListMap LooseTypeMap a)
one_tycon ListMap LooseTypeMap a
tm
      | ListMap LooseTypeMap a -> Bool
forall (m :: * -> *) a. TrieMap m => m a -> Bool
isEmptyTM ListMap LooseTypeMap a
filtered_tm = Maybe (ListMap LooseTypeMap a)
forall a. Maybe a
Nothing
      | Bool
otherwise             = ListMap LooseTypeMap a -> Maybe (ListMap LooseTypeMap a)
forall a. a -> Maybe a
Just ListMap LooseTypeMap a
filtered_tm
      where
        filtered_tm :: ListMap LooseTypeMap a
filtered_tm = (a -> Bool) -> ListMap LooseTypeMap a -> ListMap LooseTypeMap a
forall (m :: * -> *) a. TrieMap m => (a -> Bool) -> m a -> m a
filterTM a -> Bool
f ListMap LooseTypeMap a
tm

tcAppMapToBag :: TcAppMap a -> Bag a
tcAppMapToBag :: TcAppMap a -> Bag a
tcAppMapToBag TcAppMap a
m = (a -> Bag a -> Bag a) -> TcAppMap a -> Bag a -> Bag a
forall a b. (a -> b -> b) -> TcAppMap a -> b -> b
foldTcAppMap a -> Bag a -> Bag a
forall a. a -> Bag a -> Bag a
consBag TcAppMap a
m Bag a
forall a. Bag a
emptyBag

foldTcAppMap :: (a -> b -> b) -> TcAppMap a -> b -> b
foldTcAppMap :: (a -> b -> b) -> TcAppMap a -> b -> b
foldTcAppMap a -> b -> b
k TcAppMap a
m b
z = (ListMap LooseTypeMap a -> b -> b) -> b -> TcAppMap a -> b
forall elt a. (elt -> a -> a) -> a -> DTyConEnv elt -> a
foldDTyConEnv ((a -> b -> b) -> ListMap LooseTypeMap a -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> m a -> b -> b
foldTM a -> b -> b
k) b
z TcAppMap a
m

{- *********************************************************************
*                                                                      *
                   DictMap
*                                                                      *
********************************************************************* -}

type DictMap a = TcAppMap a

emptyDictMap :: DictMap a
emptyDictMap :: DictMap a
emptyDictMap = DictMap a
forall a. TcAppMap a
emptyTcAppMap

findDict :: DictMap a -> CtLoc -> Class -> [Type] -> Maybe a
findDict :: DictMap a -> CtLoc -> Class -> [Type] -> Maybe a
findDict DictMap a
m CtLoc
loc Class
cls [Type]
tys
  | Class -> [Type] -> Bool
hasIPSuperClasses Class
cls [Type]
tys -- See Note [Tuples hiding implicit parameters]
  = Maybe a
forall a. Maybe a
Nothing

  | Just {} <- Class -> [Type] -> Maybe FastString
isCallStackPred Class
cls [Type]
tys
  , OccurrenceOf {} <- CtLoc -> CtOrigin
ctLocOrigin CtLoc
loc
  = Maybe a
forall a. Maybe a
Nothing             -- See Note [Solving CallStack constraints]

  | Bool
otherwise
  = DictMap a -> TyCon -> [Type] -> Maybe a
forall a. TcAppMap a -> TyCon -> [Type] -> Maybe a
findTcApp DictMap a
m (Class -> TyCon
classTyCon Class
cls) [Type]
tys

findDictsByClass :: DictMap a -> Class -> Bag a
findDictsByClass :: DictMap a -> Class -> Bag a
findDictsByClass DictMap a
m Class
cls
  | Just ListMap LooseTypeMap a
tm <- DictMap a -> TyCon -> Maybe (ListMap LooseTypeMap a)
forall a. DTyConEnv a -> TyCon -> Maybe a
lookupDTyConEnv DictMap a
m (Class -> TyCon
classTyCon Class
cls) = (a -> Bag a -> Bag a) -> ListMap LooseTypeMap a -> Bag a -> Bag a
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> m a -> b -> b
foldTM a -> Bag a -> Bag a
forall a. a -> Bag a -> Bag a
consBag ListMap LooseTypeMap a
tm Bag a
forall a. Bag a
emptyBag
  | Bool
otherwise                                     = Bag a
forall a. Bag a
emptyBag

delDict :: DictMap a -> Class -> [Type] -> DictMap a
delDict :: DictMap a -> Class -> [Type] -> DictMap a
delDict DictMap a
m Class
cls [Type]
tys = DictMap a -> TyCon -> [Type] -> DictMap a
forall a. TcAppMap a -> TyCon -> [Type] -> TcAppMap a
delTcApp DictMap a
m (Class -> TyCon
classTyCon Class
cls) [Type]
tys

addDict :: DictMap a -> Class -> [Type] -> a -> DictMap a
addDict :: DictMap a -> Class -> [Type] -> a -> DictMap a
addDict DictMap a
m Class
cls [Type]
tys a
item = DictMap a -> TyCon -> [Type] -> a -> DictMap a
forall a. TcAppMap a -> TyCon -> [Type] -> a -> TcAppMap a
insertTcApp DictMap a
m (Class -> TyCon
classTyCon Class
cls) [Type]
tys a
item

addDictCt :: DictMap Ct -> Class -> [Type] -> Ct -> DictMap Ct
-- Like addDict, but combines [W] and [D] to [WD]
-- See Note [KeepBoth] in GHC.Tc.Solver.Interact
addDictCt :: DictMap Ct -> Class -> [Type] -> Ct -> DictMap Ct
addDictCt DictMap Ct
m Class
cls [Type]
tys Ct
new_ct = DictMap Ct -> TyCon -> [Type] -> XT Ct -> DictMap Ct
forall a. TcAppMap a -> TyCon -> [Type] -> XT a -> TcAppMap a
alterTcApp DictMap Ct
m (Class -> TyCon
classTyCon Class
cls) [Type]
tys XT Ct
xt_ct
  where
    new_ct_ev :: CtEvidence
new_ct_ev = Ct -> CtEvidence
ctEvidence Ct
new_ct

    xt_ct :: Maybe Ct -> Maybe Ct
    xt_ct :: XT Ct
xt_ct (Just Ct
old_ct)
      | CtWanted { ctev_nosh :: CtEvidence -> ShadowInfo
ctev_nosh = ShadowInfo
WOnly } <- CtEvidence
old_ct_ev
      , CtDerived {} <- CtEvidence
new_ct_ev
      = Ct -> Maybe Ct
forall a. a -> Maybe a
Just (Ct
old_ct { cc_ev :: CtEvidence
cc_ev = CtEvidence
old_ct_ev { ctev_nosh :: ShadowInfo
ctev_nosh = ShadowInfo
WDeriv }})
      | CtDerived {} <- CtEvidence
old_ct_ev
      , CtWanted { ctev_nosh :: CtEvidence -> ShadowInfo
ctev_nosh = ShadowInfo
WOnly } <- CtEvidence
new_ct_ev
      = Ct -> Maybe Ct
forall a. a -> Maybe a
Just (Ct
new_ct { cc_ev :: CtEvidence
cc_ev = CtEvidence
new_ct_ev { ctev_nosh :: ShadowInfo
ctev_nosh = ShadowInfo
WDeriv }})
      where
        old_ct_ev :: CtEvidence
old_ct_ev = Ct -> CtEvidence
ctEvidence Ct
old_ct

    xt_ct Maybe Ct
_ = Ct -> Maybe Ct
forall a. a -> Maybe a
Just Ct
new_ct

addDictsByClass :: DictMap Ct -> Class -> Bag Ct -> DictMap Ct
addDictsByClass :: DictMap Ct -> Class -> Bag Ct -> DictMap Ct
addDictsByClass DictMap Ct
m Class
cls Bag Ct
items
  = DictMap Ct -> TyCon -> ListMap LooseTypeMap Ct -> DictMap Ct
forall a. DTyConEnv a -> TyCon -> a -> DTyConEnv a
extendDTyConEnv DictMap Ct
m (Class -> TyCon
classTyCon Class
cls) ((Ct -> ListMap LooseTypeMap Ct -> ListMap LooseTypeMap Ct)
-> ListMap LooseTypeMap Ct -> Bag Ct -> ListMap LooseTypeMap Ct
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Ct -> ListMap LooseTypeMap Ct -> ListMap LooseTypeMap Ct
forall (m :: * -> *).
(TrieMap m, Key m ~ [Type]) =>
Ct -> m Ct -> m Ct
add ListMap LooseTypeMap Ct
forall (m :: * -> *) a. TrieMap m => m a
emptyTM Bag Ct
items)
  where
    add :: Ct -> m Ct -> m Ct
add ct :: Ct
ct@(CDictCan { cc_tyargs :: Ct -> [Type]
cc_tyargs = [Type]
tys }) m Ct
tm = Key m -> Ct -> m Ct -> m Ct
forall (m :: * -> *) a. TrieMap m => Key m -> a -> m a -> m a
insertTM [Type]
Key m
tys Ct
ct m Ct
tm
    add Ct
ct m Ct
_ = String -> SDoc -> m Ct
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"addDictsByClass" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)

filterDicts :: (Ct -> Bool) -> DictMap Ct -> DictMap Ct
filterDicts :: (Ct -> Bool) -> DictMap Ct -> DictMap Ct
filterDicts Ct -> Bool
f DictMap Ct
m = (Ct -> Bool) -> DictMap Ct -> DictMap Ct
forall a. (a -> Bool) -> TcAppMap a -> TcAppMap a
filterTcAppMap Ct -> Bool
f DictMap Ct
m

partitionDicts :: (Ct -> Bool) -> DictMap Ct -> (Bag Ct, DictMap Ct)
partitionDicts :: (Ct -> Bool) -> DictMap Ct -> (Bag Ct, DictMap Ct)
partitionDicts Ct -> Bool
f DictMap Ct
m = (Ct -> (Bag Ct, DictMap Ct) -> (Bag Ct, DictMap Ct))
-> DictMap Ct -> (Bag Ct, DictMap Ct) -> (Bag Ct, DictMap Ct)
forall a b. (a -> b -> b) -> TcAppMap a -> b -> b
foldTcAppMap Ct -> (Bag Ct, DictMap Ct) -> (Bag Ct, DictMap Ct)
k DictMap Ct
m (Bag Ct
forall a. Bag a
emptyBag, DictMap Ct
forall a. TcAppMap a
emptyDictMap)
  where
    k :: Ct -> (Bag Ct, DictMap Ct) -> (Bag Ct, DictMap Ct)
k Ct
ct (Bag Ct
yeses, DictMap Ct
noes) | Ct -> Bool
f Ct
ct      = (Ct
ct Ct -> Bag Ct -> Bag Ct
forall a. a -> Bag a -> Bag a
`consBag` Bag Ct
yeses, DictMap Ct
noes)
                       | Bool
otherwise = (Bag Ct
yeses,              Ct -> DictMap Ct -> DictMap Ct
add Ct
ct DictMap Ct
noes)
    add :: Ct -> DictMap Ct -> DictMap Ct
add ct :: Ct
ct@(CDictCan { cc_class :: Ct -> Class
cc_class = Class
cls, cc_tyargs :: Ct -> [Type]
cc_tyargs = [Type]
tys }) DictMap Ct
m
      = DictMap Ct -> Class -> [Type] -> Ct -> DictMap Ct
forall a. DictMap a -> Class -> [Type] -> a -> DictMap a
addDict DictMap Ct
m Class
cls [Type]
tys Ct
ct
    add Ct
ct DictMap Ct
_ = String -> SDoc -> DictMap Ct
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"partitionDicts" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)

dictsToBag :: DictMap a -> Bag a
dictsToBag :: DictMap a -> Bag a
dictsToBag = DictMap a -> Bag a
forall a. TcAppMap a -> Bag a
tcAppMapToBag

foldDicts :: (a -> b -> b) -> DictMap a -> b -> b
foldDicts :: (a -> b -> b) -> DictMap a -> b -> b
foldDicts = (a -> b -> b) -> DictMap a -> b -> b
forall a b. (a -> b -> b) -> TcAppMap a -> b -> b
foldTcAppMap

{- Note [Tuples hiding implicit parameters]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
   f,g :: (?x::Int, C a) => a -> a
   f v = let ?x = 4 in g v

The call to 'g' gives rise to a Wanted constraint (?x::Int, C a).
We must /not/ solve this from the Given (?x::Int, C a), because of
the intervening binding for (?x::Int).  #14218.

We deal with this by arranging that we always fail when looking up a
tuple constraint that hides an implicit parameter. Not that this applies
  * both to the inert_dicts (lookupInertDict)
  * and to the solved_dicts (looukpSolvedDict)
An alternative would be not to extend these sets with such tuple
constraints, but it seemed more direct to deal with the lookup.

Note [Solving CallStack constraints]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppose f :: HasCallStack => blah.  Then

* Each call to 'f' gives rise to
    [W] s1 :: IP "callStack" CallStack    -- CtOrigin = OccurrenceOf f
  with a CtOrigin that says "OccurrenceOf f".
  Remember that HasCallStack is just shorthand for
    IP "callStack CallStack
  See Note [Overview of implicit CallStacks] in GHC.Tc.Types.Evidence

* We cannonicalise such constraints, in GHC.Tc.Solver.Canonical.canClassNC, by
  pushing the call-site info on the stack, and changing the CtOrigin
  to record that has been done.
   Bind:  s1 = pushCallStack <site-info> s2
   [W] s2 :: IP "callStack" CallStack   -- CtOrigin = IPOccOrigin

* Then, and only then, we can solve the constraint from an enclosing
  Given.

So we must be careful /not/ to solve 's1' from the Givens.  Again,
we ensure this by arranging that findDict always misses when looking
up souch constraints.
-}

{- *********************************************************************
*                                                                      *
                   FunEqMap
*                                                                      *
********************************************************************* -}

type FunEqMap a = TcAppMap a  -- A map whose key is a (TyCon, [Type]) pair

emptyFunEqs :: TcAppMap a
emptyFunEqs :: TcAppMap a
emptyFunEqs = TcAppMap a
forall a. TcAppMap a
emptyTcAppMap

findFunEq :: FunEqMap a -> TyCon -> [Type] -> Maybe a
findFunEq :: FunEqMap a -> TyCon -> [Type] -> Maybe a
findFunEq FunEqMap a
m TyCon
tc [Type]
tys = FunEqMap a -> TyCon -> [Type] -> Maybe a
forall a. TcAppMap a -> TyCon -> [Type] -> Maybe a
findTcApp FunEqMap a
m TyCon
tc [Type]
tys

findFunEqsByTyCon :: FunEqMap a -> TyCon -> [a]
-- Get inert function equation constraints that have the given tycon
-- in their head.  Not that the constraints remain in the inert set.
-- We use this to check for derived interactions with built-in type-function
-- constructors.
findFunEqsByTyCon :: FunEqMap a -> TyCon -> [a]
findFunEqsByTyCon FunEqMap a
m TyCon
tc
  | Just ListMap LooseTypeMap a
tm <- FunEqMap a -> TyCon -> Maybe (ListMap LooseTypeMap a)
forall a. DTyConEnv a -> TyCon -> Maybe a
lookupDTyConEnv FunEqMap a
m TyCon
tc = (a -> [a] -> [a]) -> ListMap LooseTypeMap a -> [a] -> [a]
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> m a -> b -> b
foldTM (:) ListMap LooseTypeMap a
tm []
  | Bool
otherwise                       = []

foldFunEqs :: (a -> b -> b) -> FunEqMap a -> b -> b
foldFunEqs :: (a -> b -> b) -> FunEqMap a -> b -> b
foldFunEqs = (a -> b -> b) -> FunEqMap a -> b -> b
forall a b. (a -> b -> b) -> TcAppMap a -> b -> b
foldTcAppMap

insertFunEq :: FunEqMap a -> TyCon -> [Type] -> a -> FunEqMap a
insertFunEq :: FunEqMap a -> TyCon -> [Type] -> a -> FunEqMap a
insertFunEq FunEqMap a
m TyCon
tc [Type]
tys a
val = FunEqMap a -> TyCon -> [Type] -> a -> FunEqMap a
forall a. TcAppMap a -> TyCon -> [Type] -> a -> TcAppMap a
insertTcApp FunEqMap a
m TyCon
tc [Type]
tys a
val

{- *********************************************************************
*                                                                      *
                   EqualCtList
*                                                                      *
********************************************************************* -}

{- Note [EqualCtList invariants]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    * All are equalities
    * All these equalities have the same LHS
    * The list is never empty
    * No element of the list can rewrite any other
    * Derived before Wanted

From the fourth invariant it follows that the list is
   - A single [G], or
   - Zero or one [D] or [WD], followed by any number of [W]

The Wanteds can't rewrite anything which is why we put them last
-}

newtype EqualCtList = MkEqualCtList (NonEmpty Ct)
  deriving newtype EqualCtList -> SDoc
(EqualCtList -> SDoc) -> Outputable EqualCtList
forall a. (a -> SDoc) -> Outputable a
ppr :: EqualCtList -> SDoc
$cppr :: EqualCtList -> SDoc
Outputable
  -- See Note [EqualCtList invariants]

-- | Pattern synonym for easy unwrapping. NB: unidirectional to
-- preserve invariants.
pattern EqualCtList :: NonEmpty Ct -> EqualCtList
pattern $mEqualCtList :: forall r. EqualCtList -> (NonEmpty Ct -> r) -> (Void# -> r) -> r
EqualCtList cts <- MkEqualCtList cts
{-# COMPLETE EqualCtList #-}

unitEqualCtList :: Ct -> EqualCtList
unitEqualCtList :: Ct -> EqualCtList
unitEqualCtList Ct
ct = NonEmpty Ct -> EqualCtList
MkEqualCtList (Ct
ct Ct -> [Ct] -> NonEmpty Ct
forall a. a -> [a] -> NonEmpty a
:| [])

addToEqualCtList :: Ct -> EqualCtList -> EqualCtList
-- NB: This function maintains the "derived-before-wanted" invariant of EqualCtList,
-- but not the others. See Note [EqualCtList invariants]
addToEqualCtList :: Ct -> EqualCtList -> EqualCtList
addToEqualCtList Ct
ct (MkEqualCtList NonEmpty Ct
old_eqs)
  | Ct -> Bool
isWantedCt Ct
ct
  , Ct
eq1 :| [Ct]
eqs <- NonEmpty Ct
old_eqs
  = NonEmpty Ct -> EqualCtList
MkEqualCtList (Ct
eq1 Ct -> [Ct] -> NonEmpty Ct
forall a. a -> [a] -> NonEmpty a
:| Ct
ct Ct -> [Ct] -> [Ct]
forall a. a -> [a] -> [a]
: [Ct]
eqs)
  | Bool
otherwise
  = NonEmpty Ct -> EqualCtList
MkEqualCtList (Ct
ct Ct -> NonEmpty Ct -> NonEmpty Ct
forall a. a -> NonEmpty a -> NonEmpty a
`cons` NonEmpty Ct
old_eqs)

filterEqualCtList :: (Ct -> Bool) -> EqualCtList -> Maybe EqualCtList
filterEqualCtList :: (Ct -> Bool) -> EqualCtList -> Maybe EqualCtList
filterEqualCtList Ct -> Bool
pred (MkEqualCtList NonEmpty Ct
cts)
  = (NonEmpty Ct -> EqualCtList)
-> Maybe (NonEmpty Ct) -> Maybe EqualCtList
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty Ct -> EqualCtList
MkEqualCtList ([Ct] -> Maybe (NonEmpty Ct)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ([Ct] -> Maybe (NonEmpty Ct)) -> [Ct] -> Maybe (NonEmpty Ct)
forall a b. (a -> b) -> a -> b
$ (Ct -> Bool) -> NonEmpty Ct -> [Ct]
forall a. (a -> Bool) -> NonEmpty a -> [a]
NE.filter Ct -> Bool
pred NonEmpty Ct
cts)

equalCtListToList :: EqualCtList -> [Ct]
equalCtListToList :: EqualCtList -> [Ct]
equalCtListToList (MkEqualCtList NonEmpty Ct
cts) = NonEmpty Ct -> [Ct]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty Ct
cts

listToEqualCtList :: [Ct] -> Maybe EqualCtList
-- NB: This does not maintain invariants other than having the EqualCtList be
-- non-empty
listToEqualCtList :: [Ct] -> Maybe EqualCtList
listToEqualCtList [Ct]
cts = NonEmpty Ct -> EqualCtList
MkEqualCtList (NonEmpty Ct -> EqualCtList)
-> Maybe (NonEmpty Ct) -> Maybe EqualCtList
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Ct] -> Maybe (NonEmpty Ct)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [Ct]
cts