{-# LANGUAGE FlexibleInstances, FunctionalDependencies, DefaultSignatures, GeneralizedNewtypeDeriving #-}

-- |Module `Containers` include a lot of general methods for working with
--  containers. All methods are scattered by classes.
module Containers (
    S.Set,
    M.Map,
    IM.IsoMap,
    PM.PrismMap,
    P.Pair (..),
    Q.Quadruple (..),
    Index,
    index,
    Sizable (..),
    Nullable (..),
    Singletonable (..),
    Valuable (..),
    Keyable (..),
    Indexable (..),
    Slicable (..),
    Operable (..),
    Listable (..),
    UnsafeListable (..),
    FastUnsafeListable (..),
    Containable (..),
    Gunctor (..),
    Filterable (..),
    Insertable (..),
    Deletable (..),
  ) where

import qualified Containers.Set as S
import qualified Containers.Map as M
import qualified Containers.IsoMap as IM
import qualified Containers.PrismMap as PM
import qualified Containers.Pair as P
import qualified Containers.Quadruple as Q
import Lens

import Control.Monad.State.Lazy (MonadState)
import Data.Maybe (fromMaybe)
import qualified Data.List as L

infixl 9 !?, !<>
infixr 9 .@, .<@>
infixr 7 +>, \>
infixl 6 <+, <\
infixl 5 \/, /\, \\
infixr 4 <+~, <\~, \/~, /\~, \\~, <?>~
infix  4 <+=, <\=, \/=, /\=, \\=, <?>=, <?>, <@>

newtype Index = Index Int
    deriving (Index -> Index -> Bool
(Index -> Index -> Bool) -> (Index -> Index -> Bool) -> Eq Index
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Index -> Index -> Bool
$c/= :: Index -> Index -> Bool
== :: Index -> Index -> Bool
$c== :: Index -> Index -> Bool
Eq, Eq Index
Eq Index
-> (Index -> Index -> Ordering)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Bool)
-> (Index -> Index -> Index)
-> (Index -> Index -> Index)
-> Ord Index
Index -> Index -> Bool
Index -> Index -> Ordering
Index -> Index -> Index
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Index -> Index -> Index
$cmin :: Index -> Index -> Index
max :: Index -> Index -> Index
$cmax :: Index -> Index -> Index
>= :: Index -> Index -> Bool
$c>= :: Index -> Index -> Bool
> :: Index -> Index -> Bool
$c> :: Index -> Index -> Bool
<= :: Index -> Index -> Bool
$c<= :: Index -> Index -> Bool
< :: Index -> Index -> Bool
$c< :: Index -> Index -> Bool
compare :: Index -> Index -> Ordering
$ccompare :: Index -> Index -> Ordering
$cp1Ord :: Eq Index
Ord, Integer -> Index
Index -> Index
Index -> Index -> Index
(Index -> Index -> Index)
-> (Index -> Index -> Index)
-> (Index -> Index -> Index)
-> (Index -> Index)
-> (Index -> Index)
-> (Index -> Index)
-> (Integer -> Index)
-> Num Index
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> Index
$cfromInteger :: Integer -> Index
signum :: Index -> Index
$csignum :: Index -> Index
abs :: Index -> Index
$cabs :: Index -> Index
negate :: Index -> Index
$cnegate :: Index -> Index
* :: Index -> Index -> Index
$c* :: Index -> Index -> Index
- :: Index -> Index -> Index
$c- :: Index -> Index -> Index
+ :: Index -> Index -> Index
$c+ :: Index -> Index -> Index
Num, Int -> Index
Index -> Int
Index -> [Index]
Index -> Index
Index -> Index -> [Index]
Index -> Index -> Index -> [Index]
(Index -> Index)
-> (Index -> Index)
-> (Int -> Index)
-> (Index -> Int)
-> (Index -> [Index])
-> (Index -> Index -> [Index])
-> (Index -> Index -> [Index])
-> (Index -> Index -> Index -> [Index])
-> Enum Index
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Index -> Index -> Index -> [Index]
$cenumFromThenTo :: Index -> Index -> Index -> [Index]
enumFromTo :: Index -> Index -> [Index]
$cenumFromTo :: Index -> Index -> [Index]
enumFromThen :: Index -> Index -> [Index]
$cenumFromThen :: Index -> Index -> [Index]
enumFrom :: Index -> [Index]
$cenumFrom :: Index -> [Index]
fromEnum :: Index -> Int
$cfromEnum :: Index -> Int
toEnum :: Int -> Index
$ctoEnum :: Int -> Index
pred :: Index -> Index
$cpred :: Index -> Index
succ :: Index -> Index
$csucc :: Index -> Index
Enum)

index :: Int -> Index
index :: Int -> Index
index = Int -> Index
Index

class Sizable c where
    size :: c -> Int

instance Sizable [a] where
    size :: [a] -> Int
size = [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length

instance Sizable (S.Set a) where
    size :: Set a -> Int
size = Set a -> Int
forall a. Set a -> Int
S.size

instance Sizable (M.Map k a) where
    size :: Map k a -> Int
size = Map k a -> Int
forall k a. Map k a -> Int
M.size

instance Sizable (IM.IsoMap a b) where
    size :: IsoMap a b -> Int
size = IsoMap a b -> Int
forall a b. IsoMap a b -> Int
IM.size

instance Sizable (PM.PrismMap k a) where
    size :: PrismMap k a -> Int
size = PrismMap k a -> Int
forall k a. PrismMap k a -> Int
PM.size

instance Sizable (P.Pair a) where
    size :: Pair a -> Int
size = Int -> Pair a -> Int
forall a b. a -> b -> a
const Int
2

instance Sizable (Q.Quadruple a) where
    size :: Quadruple a -> Int
size = Int -> Quadruple a -> Int
forall a b. a -> b -> a
const Int
4

class Nullable c where
    nullC :: c -> Bool
    default nullC :: Sizable c => c -> Bool
    nullC c
c = c -> Int
forall c. Sizable c => c -> Int
size c
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
    emptyC :: c

instance Nullable [a] where
    nullC :: [a] -> Bool
nullC = [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
L.null
    emptyC :: [a]
emptyC = []

instance Nullable (S.Set a) where
    nullC :: Set a -> Bool
nullC = Set a -> Bool
forall a. Set a -> Bool
S.null
    emptyC :: Set a
emptyC = Set a
forall a. Set a
S.empty

instance Nullable (M.Map k a) where
    nullC :: Map k a -> Bool
nullC = Map k a -> Bool
forall k a. Map k a -> Bool
M.null
    emptyC :: Map k a
emptyC = Map k a
forall k a. Map k a
M.empty

instance Nullable (IM.IsoMap a b) where
    emptyC :: IsoMap a b
emptyC = IsoMap a b
forall a b. IsoMap a b
IM.empty

instance Nullable (PM.PrismMap k a) where
    nullC :: PrismMap k a -> Bool
nullC = PrismMap k a -> Bool
forall k a. PrismMap k a -> Bool
PM.null
    emptyC :: PrismMap k a
emptyC = PrismMap k a
forall k a. PrismMap k a
PM.empty

class Singletonable c v | c -> v where
    singleton :: v -> c

instance Singletonable [a] a where
    singleton :: a -> [a]
singleton = a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure

instance Singletonable (S.Set a) a where
    singleton :: a -> Set a
singleton = a -> Set a
forall a. a -> Set a
S.singleton

instance Singletonable (M.Map k a) (k, a) where
    singleton :: (k, a) -> Map k a
singleton = (k -> a -> Map k a) -> (k, a) -> Map k a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> a -> Map k a
forall k a. k -> a -> Map k a
M.singleton

instance (Ord a, Ord b) => Singletonable (IM.IsoMap a b) (a, b) where
    singleton :: (a, b) -> IsoMap a b
singleton = (a -> b -> IsoMap a b) -> (a, b) -> IsoMap a b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> IsoMap a b
forall i a b. Iso i a b => a -> b -> i
IM.singleton

instance Singletonable (PM.PrismMap k a) (k, a) where
    singleton :: (k, a) -> PrismMap k a
singleton = (k -> a -> PrismMap k a) -> (k, a) -> PrismMap k a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> a -> PrismMap k a
forall k a. k -> a -> PrismMap k a
PM.singleton

class Valuable c v where
    values :: c -> [v]
    default values :: Ord v => c -> [v]
    values = Set v -> [v]
forall a. Set a -> [a]
S.toList (Set v -> [v]) -> (c -> Set v) -> c -> [v]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Set v
forall c v. (Valuable c v, Ord v) => c -> Set v
valuesSet
    valuesSet :: Ord v => c -> S.Set v
    valuesSet = [v] -> Set v
forall a. Ord a => [a] -> Set a
S.fromList ([v] -> Set v) -> (c -> [v]) -> c -> Set v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> [v]
forall c v. Valuable c v => c -> [v]
values

instance Valuable [a] a where
    values :: [a] -> [a]
values = [a] -> [a]
forall a. a -> a
id

instance Ord a => Valuable (S.Set a) a where
    valuesSet :: Set a -> Set a
valuesSet = Set a -> Set a
forall a. a -> a
id

instance Valuable (M.Map k a) a where
    values :: Map k a -> [a]
values = Map k a -> [a]
forall k a. Map k a -> [a]
M.elems

instance (Ord a, Ord b) => Valuable (IM.IsoMap a b) a where
    valuesSet :: IsoMap a b -> Set a
valuesSet = IsoMap a b -> Set a
forall i a b. Iso i a b => i -> Set a
IM.values

instance (Ord a, Ord b) => Valuable (IM.IsoMap a b) b where
    valuesSet :: IsoMap a b -> Set b
valuesSet = IsoMap a b -> Set b
forall i a b. Iso i a b => i -> Set a
IM.values

instance Valuable (PM.PrismMap k a) a where
    values :: PrismMap k a -> [a]
values = PrismMap k a -> [a]
forall k a. PrismMap k a -> [a]
PM.values

instance Valuable (P.Pair a) a where
    values :: Pair a -> [a]
values = Pair a -> [a]
forall a. Pair a -> [a]
P.toList

instance Valuable (Q.Quadruple a) a where
    values :: Quadruple a -> [a]
values = Quadruple a -> [a]
forall a. Quadruple a -> [a]
Q.toList

class Keyable c k | c -> k where
    keys :: c -> [k]
    default keys :: Ord k => c -> [k]
    keys = Set k -> [k]
forall a. Set a -> [a]
S.toList (Set k -> [k]) -> (c -> Set k) -> c -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Set k
forall c k. (Keyable c k, Ord k) => c -> Set k
keysSet
    keysSet :: Ord k => c -> S.Set k
    keysSet = [k] -> Set k
forall a. Ord a => [a] -> Set a
S.fromList ([k] -> Set k) -> (c -> [k]) -> c -> Set k
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> [k]
forall c k. Keyable c k => c -> [k]
keys

instance Keyable [a] Index where
    keys :: [a] -> [Index]
keys = (\Int
n -> Int -> Index
index (Int -> Index) -> [Int] -> [Index]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1]) (Int -> [Index]) -> ([a] -> Int) -> [a] -> [Index]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
    keysSet :: [a] -> Set Index
keysSet = [Index] -> Set Index
forall a. [a] -> Set a
S.fromDistinctAscList ([Index] -> Set Index) -> ([a] -> [Index]) -> [a] -> Set Index
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [Index]
forall c k. Keyable c k => c -> [k]
keys

instance Ord k => Keyable (M.Map k a) k where
    keys :: Map k a -> [k]
keys = Map k a -> [k]
forall k a. Map k a -> [k]
M.keys
    keysSet :: Map k a -> Set k
keysSet = Map k a -> Set k
forall k a. Map k a -> Set k
M.keysSet

instance Keyable (PM.PrismMap k a) k where
    keys :: PrismMap k a -> [k]
keys = PrismMap k a -> [k]
forall k a. PrismMap k a -> [k]
PM.keys
    keysSet :: PrismMap k a -> Set k
keysSet = PrismMap k a -> Set k
forall k a. PrismMap k a -> Set k
PM.keysSet

instance Keyable (P.Pair a) Index where
    keys :: Pair a -> [Index]
keys = [Index] -> Pair a -> [Index]
forall a b. a -> b -> a
const ([Index] -> Pair a -> [Index]) -> [Index] -> Pair a -> [Index]
forall a b. (a -> b) -> a -> b
$ Int -> Index
index (Int -> Index) -> [Int] -> [Index]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
0, Int
1]
    keysSet :: Pair a -> Set Index
keysSet = [Index] -> Set Index
forall a. [a] -> Set a
S.fromDistinctAscList ([Index] -> Set Index)
-> (Pair a -> [Index]) -> Pair a -> Set Index
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pair a -> [Index]
forall c k. Keyable c k => c -> [k]
keys

instance Keyable (Q.Quadruple a) Index where
    keys :: Quadruple a -> [Index]
keys = [Index] -> Quadruple a -> [Index]
forall a b. a -> b -> a
const ([Index] -> Quadruple a -> [Index])
-> [Index] -> Quadruple a -> [Index]
forall a b. (a -> b) -> a -> b
$ Int -> Index
index (Int -> Index) -> [Int] -> [Index]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
0..Int
3]
    keysSet :: Quadruple a -> Set Index
keysSet = [Index] -> Set Index
forall a. [a] -> Set a
S.fromDistinctAscList ([Index] -> Set Index)
-> (Quadruple a -> [Index]) -> Quadruple a -> Set Index
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quadruple a -> [Index]
forall c k. Keyable c k => c -> [k]
keys

class Indexable c k v | c k -> v, c v -> k where
    (!?) :: MonadFail m => c -> k -> m v
    (!?) c
c k
k =
        let vs :: Set v
vs = c
c c -> k -> Set v
forall c k v. Indexable c k v => c -> k -> Set v
!<> k
k
        in  case Set v -> Int
forall c. Sizable c => c -> Int
size Set v
vs of
                Int
0 -> String -> m v
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Key is not in container"
                Int
1 -> v -> m v
forall (m :: * -> *) a. Monad m => a -> m a
return (v -> m v) -> v -> m v
forall a b. (a -> b) -> a -> b
$ [v] -> v
forall a. [a] -> a
head ([v] -> v) -> [v] -> v
forall a b. (a -> b) -> a -> b
$ Set v -> [v]
forall a. Set a -> [a]
S.toList Set v
vs
                Int
_ -> String -> m v
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"There are more than one element by key in container"
    (!<>) :: c -> k -> S.Set v
    (!<>) c
c k
k =
        case c
c c -> k -> Maybe v
forall c k v (m :: * -> *).
(Indexable c k v, MonadFail m) =>
c -> k -> m v
!? k
k of
            Maybe v
Nothing -> Set v
forall a. Set a
S.empty
            Just v
v  -> v -> Set v
forall a. a -> Set a
S.singleton v
v
    (.@) :: MonadFail m => Getting x y c -> k -> Getting x y (m v)
    (.@) = ((Optic' (->) (Const x) c (m v) -> Getting x y (m v))
-> (k -> Optic' (->) (Const x) c (m v)) -> k -> Getting x y (m v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> m v) -> Optic' (->) (Const x) c (m v)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to ((c -> m v) -> Optic' (->) (Const x) c (m v))
-> (k -> c -> m v) -> k -> Optic' (->) (Const x) c (m v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> k -> m v) -> k -> c -> m v
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> k -> m v
forall c k v (m :: * -> *).
(Indexable c k v, MonadFail m) =>
c -> k -> m v
(!?)) ((Optic' (->) (Const x) c (m v) -> Getting x y (m v))
 -> k -> Getting x y (m v))
-> (Getting x y c
    -> Optic' (->) (Const x) c (m v) -> Getting x y (m v))
-> Getting x y c
-> k
-> Getting x y (m v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting x y c -> Optic' (->) (Const x) c (m v) -> Getting x y (m v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)
    (.<@>) :: Getting x y c -> k -> Getting x y (S.Set v)
    (.<@>) = ((Optic' (->) (Const x) c (Set v) -> Getting x y (Set v))
-> (k -> Optic' (->) (Const x) c (Set v))
-> k
-> Getting x y (Set v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> Set v) -> Optic' (->) (Const x) c (Set v)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to ((c -> Set v) -> Optic' (->) (Const x) c (Set v))
-> (k -> c -> Set v) -> k -> Optic' (->) (Const x) c (Set v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> k -> Set v) -> k -> c -> Set v
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> k -> Set v
forall c k v. Indexable c k v => c -> k -> Set v
(!<>)) ((Optic' (->) (Const x) c (Set v) -> Getting x y (Set v))
 -> k -> Getting x y (Set v))
-> (Getting x y c
    -> Optic' (->) (Const x) c (Set v) -> Getting x y (Set v))
-> Getting x y c
-> k
-> Getting x y (Set v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting x y c
-> Optic' (->) (Const x) c (Set v) -> Getting x y (Set v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)

instance Indexable [a] Index a where
    [a]
l !? :: [a] -> Index -> m a
!? (Index Int
i)
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Negative index"
        | Bool
otherwise = [a] -> Int -> m a
forall (m :: * -> *) t a.
(MonadFail m, Eq t, Num t) =>
[a] -> t -> m a
go [a]
l Int
i where
            go :: [a] -> t -> m a
go [] t
_ = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Index too large"
            go (a
x:[a]
_) t
0 = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
            go (a
_:[a]
xs) t
n = [a] -> t -> m a
go [a]
xs (t
nt -> t -> t
forall a. Num a => a -> a -> a
-t
1)

instance Eq a => Indexable [a] a Index where
    [a]
l !? :: [a] -> a -> m Index
!? a
a =
        m Index -> (Index -> m Index) -> Maybe Index -> m Index
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> m Index
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Element is not in list") Index -> m Index
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Index -> m Index) -> Maybe Index -> m Index
forall a b. (a -> b) -> a -> b
$
            Int -> Index
index (Int -> Index) -> Maybe Int -> Maybe Index
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> [a] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
L.elemIndex a
a [a]
l
    [a]
l !<> :: [a] -> a -> Set Index
!<> a
a = [Index] -> Set Index
forall a. [a] -> Set a
S.fromDistinctAscList ([Index] -> Set Index) -> [Index] -> Set Index
forall a b. (a -> b) -> a -> b
$ Int -> Index
index (Int -> Index) -> [Int] -> [Index]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> [a] -> [Int]
forall a. Eq a => a -> [a] -> [Int]
L.elemIndices a
a [a]
l

instance Indexable (S.Set a) Index a where
    Set a
s !? :: Set a -> Index -> m a
!? (Index Int
i)
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0       = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Negative index"
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Set a -> Int
forall c. Sizable c => c -> Int
size Set a
s = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Index too large"
        | Bool
otherwise   = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ Int -> Set a -> a
forall a. Int -> Set a -> a
S.elemAt Int
i Set a
s

instance Ord k => Indexable (M.Map k a) k a where
    Map k a
m !? :: Map k a -> k -> m a
!? k
k =
        case Map k a
m Map k a -> k -> Maybe a
forall k a. Ord k => Map k a -> k -> Maybe a
M.!? k
k of
            Maybe a
Nothing -> String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Key is not found"
            Just a
v  -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
v

instance (Ord a, Ord b) => Indexable (IM.IsoMap a b) a b where
    !? :: IsoMap a b -> a -> m b
(!?) = IsoMap a b -> a -> m b
forall i a b (m :: * -> *).
(Iso i a b, MonadFail m) =>
i -> a -> m b
(IM.~$)

instance (Ord a, Ord b) => Indexable (IM.IsoMap a b) b a where
    !? :: IsoMap a b -> b -> m a
(!?) = IsoMap a b -> b -> m a
forall i a b (m :: * -> *).
(Iso i a b, MonadFail m) =>
i -> a -> m b
(IM.~$)

instance Ord k => Indexable (PM.PrismMap k a) k a where
    !? :: PrismMap k a -> k -> m a
(!?) = (k -> PrismMap k a -> m a) -> PrismMap k a -> k -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip k -> PrismMap k a -> m a
forall k (m :: * -> *) a.
(Ord k, MonadFail m) =>
k -> PrismMap k a -> m a
PM.lookup

instance Ord a => Indexable (PM.PrismMap k a) a k where
    !<> :: PrismMap k a -> a -> Set k
(!<>) = (a -> PrismMap k a -> Set k) -> PrismMap k a -> a -> Set k
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> PrismMap k a -> Set k
forall a k. Ord a => a -> PrismMap k a -> Set k
PM.lookupKeys

instance Indexable (P.Pair a) Index a where
    (P.Pair (a
x, a
_)) !? :: Pair a -> Index -> m a
!? (Index Int
0) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
    (P.Pair (a
_, a
y)) !? (Index Int
1) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
y
    Pair a
_ !? Index
_ = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Illegal index"

instance Indexable (Q.Quadruple a) Index a where
    (Q.Quadruple (a
x, a
_, a
_, a
_)) !? :: Quadruple a -> Index -> m a
!? (Index Int
0) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
    (Q.Quadruple (a
_, a
y, a
_, a
_)) !? (Index Int
1) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
y
    (Q.Quadruple (a
_, a
_, a
z, a
_)) !? (Index Int
2) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
z
    (Q.Quadruple (a
_, a
_, a
_, a
w)) !? (Index Int
3) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
w
    Quadruple a
_ !? Index
_ = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Illegal index"

class Slicable c k where
    takeC :: k -> c -> c
    dropC :: k -> c -> c
    splitAtC :: k -> c -> (c, c)
    splitAtC k
k c
c = (k -> c -> c
forall c k. Slicable c k => k -> c -> c
takeC k
k c
c, k -> c -> c
forall c k. Slicable c k => k -> c -> c
dropC k
k c
c)

instance Slicable [a] Index where
    takeC :: Index -> [a] -> [a]
takeC (Index Int
i) = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
L.take Int
i
    dropC :: Index -> [a] -> [a]
dropC (Index Int
i) = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
L.drop Int
i
    splitAtC :: Index -> [a] -> ([a], [a])
splitAtC (Index Int
i) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
L.splitAt Int
i

instance Slicable [a] (a -> Bool) where
    takeC :: (a -> Bool) -> [a] -> [a]
takeC = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
L.takeWhile
    dropC :: (a -> Bool) -> [a] -> [a]
dropC = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
L.dropWhile
    splitAtC :: (a -> Bool) -> [a] -> ([a], [a])
splitAtC = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
L.span

instance Slicable (S.Set a) Index where
    takeC :: Index -> Set a -> Set a
takeC (Index Int
i) = Int -> Set a -> Set a
forall a. Int -> Set a -> Set a
S.take Int
i
    dropC :: Index -> Set a -> Set a
dropC (Index Int
i) = Int -> Set a -> Set a
forall a. Int -> Set a -> Set a
S.drop Int
i
    splitAtC :: Index -> Set a -> (Set a, Set a)
splitAtC (Index Int
i) = Int -> Set a -> (Set a, Set a)
forall a. Int -> Set a -> (Set a, Set a)
S.splitAt Int
i

instance Slicable (S.Set a) (a -> Bool) where
    takeC :: (a -> Bool) -> Set a -> Set a
takeC = (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
S.takeWhileAntitone
    dropC :: (a -> Bool) -> Set a -> Set a
dropC = (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
S.dropWhileAntitone
    splitAtC :: (a -> Bool) -> Set a -> (Set a, Set a)
splitAtC = (a -> Bool) -> Set a -> (Set a, Set a)
forall a. (a -> Bool) -> Set a -> (Set a, Set a)
S.spanAntitone

class Operable c where
    union :: c -> c -> c
    (\/) :: c -> c -> c
    (\/) = c -> c -> c
forall c. Operable c => c -> c -> c
union
    (\/~) :: ASetter s t c c -> c -> s -> t
    (\/~) = (((c -> c) -> s -> t) -> (c -> c -> c) -> c -> s -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c -> c) -> c -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> c -> c
forall c. Operable c => c -> c -> c
union) (((c -> c) -> s -> t) -> c -> s -> t)
-> (ASetter s t c c -> (c -> c) -> s -> t)
-> ASetter s t c c
-> c
-> s
-> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s t c c -> (c -> c) -> s -> t
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
(%~)
    (\/=) :: MonadState s m => ASetter s s c c -> c -> m ()
    (\/=) = (((c -> c) -> m ()) -> (c -> c -> c) -> c -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c -> c) -> c -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> c -> c
forall c. Operable c => c -> c -> c
union) (((c -> c) -> m ()) -> c -> m ())
-> (ASetter s s c c -> (c -> c) -> m ())
-> ASetter s s c c
-> c
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s s c c -> (c -> c) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
(%=)
    intersection :: c -> c -> c
    (/\) :: c -> c -> c
    (/\) = c -> c -> c
forall c. Operable c => c -> c -> c
intersection
    (/\~) :: ASetter s t c c -> c -> s -> t
    (/\~) = (((c -> c) -> s -> t) -> (c -> c -> c) -> c -> s -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c -> c) -> c -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> c -> c
forall c. Operable c => c -> c -> c
intersection) (((c -> c) -> s -> t) -> c -> s -> t)
-> (ASetter s t c c -> (c -> c) -> s -> t)
-> ASetter s t c c
-> c
-> s
-> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s t c c -> (c -> c) -> s -> t
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
(%~)
    (/\=) :: MonadState s m => ASetter s s c c -> c -> m ()
    (/\=) = (((c -> c) -> m ()) -> (c -> c -> c) -> c -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c -> c) -> c -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> c -> c
forall c. Operable c => c -> c -> c
intersection) (((c -> c) -> m ()) -> c -> m ())
-> (ASetter s s c c -> (c -> c) -> m ())
-> ASetter s s c c
-> c
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s s c c -> (c -> c) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
(%=)
    difference :: c -> c -> c
    (\\) :: c -> c -> c
    (\\) = c -> c -> c
forall c. Operable c => c -> c -> c
difference
    (\\~) :: ASetter s t c c -> c -> s -> t
    (\\~) = (((c -> c) -> s -> t) -> (c -> c -> c) -> c -> s -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c -> c) -> c -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> c -> c
forall c. Operable c => c -> c -> c
difference) (((c -> c) -> s -> t) -> c -> s -> t)
-> (ASetter s t c c -> (c -> c) -> s -> t)
-> ASetter s t c c
-> c
-> s
-> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s t c c -> (c -> c) -> s -> t
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
(%~)
    (\\=) :: MonadState s m => ASetter s s c c -> c -> m ()
    (\\=) = (((c -> c) -> m ()) -> (c -> c -> c) -> c -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c -> c) -> c -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> c -> c
forall c. Operable c => c -> c -> c
difference) (((c -> c) -> m ()) -> c -> m ())
-> (ASetter s s c c -> (c -> c) -> m ())
-> ASetter s s c c
-> c
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s s c c -> (c -> c) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
(%=)

instance Eq a => Operable [a] where
    union :: [a] -> [a] -> [a]
union = [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
L.union
    intersection :: [a] -> [a] -> [a]
intersection = [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
L.intersect
    difference :: [a] -> [a] -> [a]
difference = [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
(L.\\)

instance Ord a => Operable (S.Set a) where
    union :: Set a -> Set a -> Set a
union = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
S.union
    intersection :: Set a -> Set a -> Set a
intersection = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
S.intersection
    difference :: Set a -> Set a -> Set a
difference = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
S.difference

instance Ord k => Operable (M.Map k a) where
    union :: Map k a -> Map k a -> Map k a
union = Map k a -> Map k a -> Map k a
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union
    intersection :: Map k a -> Map k a -> Map k a
intersection = Map k a -> Map k a -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.intersection
    difference :: Map k a -> Map k a -> Map k a
difference = Map k a -> Map k a -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.difference

instance (Ord k, Ord a) => Operable (PM.PrismMap k a) where
    union :: PrismMap k a -> PrismMap k a -> PrismMap k a
union = PrismMap k a -> PrismMap k a -> PrismMap k a
forall k a.
(Ord k, Ord a) =>
PrismMap k a -> PrismMap k a -> PrismMap k a
PM.union
    intersection :: PrismMap k a -> PrismMap k a -> PrismMap k a
intersection = PrismMap k a -> PrismMap k a -> PrismMap k a
forall k a.
(Ord k, Ord a) =>
PrismMap k a -> PrismMap k a -> PrismMap k a
PM.intersection
    difference :: PrismMap k a -> PrismMap k a -> PrismMap k a
difference = PrismMap k a -> PrismMap k a -> PrismMap k a
forall k a.
(Ord k, Ord a) =>
PrismMap k a -> PrismMap k a -> PrismMap k a
PM.difference

class Listable c v | c -> v where
    toList :: c -> [v]
    default toList :: Valuable c v => c -> [v]
    toList = c -> [v]
forall c v. Valuable c v => c -> [v]
values
    fromList_ :: MonadFail m => [v] -> m c
    default fromList_ :: (UnsafeListable c v, MonadFail m) => [v] -> m c
    fromList_ = c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> ([v] -> c) -> [v] -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [v] -> c
forall c v. UnsafeListable c v => [v] -> c
fromList

instance Listable [a] a

instance Ord a => Listable (S.Set a) a

instance Ord k => Listable (M.Map k a) (k, a) where
    toList :: Map k a -> [(k, a)]
toList = Map k a -> [(k, a)]
forall k a. Map k a -> [(k, a)]
M.toList

instance (Ord a, Ord b) => Listable (IM.IsoMap a b) (a, b) where
    toList :: IsoMap a b -> [(a, b)]
toList = IsoMap a b -> [(a, b)]
forall i a b. Iso i a b => i -> [(a, b)]
IM.toList
    fromList_ :: [(a, b)] -> m (IsoMap a b)
fromList_ = [(a, b)] -> m (IsoMap a b)
forall i a b (m :: * -> *).
(Iso i a b, MonadFail m) =>
[(a, b)] -> m i
IM.fromList

instance (Ord k, Ord a) => Listable (PM.PrismMap k a) (k, a) where
    toList :: PrismMap k a -> [(k, a)]
toList = PrismMap k a -> [(k, a)]
forall k a. PrismMap k a -> [(k, a)]
PM.toList

instance Listable (P.Pair a) a where
    fromList_ :: [a] -> m (Pair a)
fromList_ = [a] -> m (Pair a)
forall (m :: * -> *) a. MonadFail m => [a] -> m (Pair a)
P.fromList

instance Listable (Q.Quadruple a) a where
    fromList_ :: [a] -> m (Quadruple a)
fromList_ = [a] -> m (Quadruple a)
forall (m :: * -> *) a. MonadFail m => [a] -> m (Quadruple a)
Q.fromList

class UnsafeListable c v | c -> v where
    fromList :: [v] -> c

instance UnsafeListable [a] a where
    fromList :: [a] -> [a]
fromList = [a] -> [a]
forall a. a -> a
id

instance Ord a => UnsafeListable (S.Set a) a where
    fromList :: [a] -> Set a
fromList = [a] -> Set a
forall a. Ord a => [a] -> Set a
S.fromList

instance Ord k => UnsafeListable (M.Map k a) (k, a) where
    fromList :: [(k, a)] -> Map k a
fromList = [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList

instance (Ord a, Ord b) => UnsafeListable (IM.IsoMap a b) (a, b) where
    fromList :: [(a, b)] -> IsoMap a b
fromList = [(a, b)] -> IsoMap a b
forall i a b. Iso i a b => [(a, b)] -> i
IM.fromList'

instance (Ord k, Ord a) => UnsafeListable (PM.PrismMap k a) (k, a) where
    fromList :: [(k, a)] -> PrismMap k a
fromList = [(k, a)] -> PrismMap k a
forall k a. (Ord k, Ord a) => [(k, a)] -> PrismMap k a
PM.fromList

class FastUnsafeListable c v | c -> v where
    fromAscList :: [v] -> c
    fromDistinctAscList :: [v] -> c
    fromDescList :: [v] -> c
    fromDistinctDescList :: [v] -> c

instance Eq a => FastUnsafeListable (S.Set a) a where
    fromAscList :: [a] -> Set a
fromAscList = [a] -> Set a
forall a. Eq a => [a] -> Set a
S.fromAscList
    fromDistinctAscList :: [a] -> Set a
fromDistinctAscList = [a] -> Set a
forall a. [a] -> Set a
S.fromDistinctAscList
    fromDescList :: [a] -> Set a
fromDescList = [a] -> Set a
forall a. Eq a => [a] -> Set a
S.fromDescList
    fromDistinctDescList :: [a] -> Set a
fromDistinctDescList = [a] -> Set a
forall a. [a] -> Set a
S.fromDistinctDescList

instance Eq k => FastUnsafeListable (M.Map k a) (k, a) where
    fromAscList :: [(k, a)] -> Map k a
fromAscList = [(k, a)] -> Map k a
forall k a. Eq k => [(k, a)] -> Map k a
M.fromAscList
    fromDistinctAscList :: [(k, a)] -> Map k a
fromDistinctAscList = [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList
    fromDescList :: [(k, a)] -> Map k a
fromDescList = [(k, a)] -> Map k a
forall k a. Eq k => [(k, a)] -> Map k a
M.fromDescList
    fromDistinctDescList :: [(k, a)] -> Map k a
fromDistinctDescList = [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
M.fromDistinctDescList

class Containable c v where
    member :: v -> c -> Bool
    member = (((c -> Bool) -> c -> Bool) -> (v -> c -> Bool) -> v -> c -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap(((c -> Bool) -> c -> Bool) -> (v -> c -> Bool) -> v -> c -> Bool)
-> ((Bool -> Bool) -> (c -> Bool) -> c -> Bool)
-> (Bool -> Bool)
-> (v -> c -> Bool)
-> v
-> c
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Bool -> Bool) -> (c -> Bool) -> c -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) Bool -> Bool
not v -> c -> Bool
forall c v. Containable c v => v -> c -> Bool
notMember
    notMember :: v -> c -> Bool
    notMember = (((c -> Bool) -> c -> Bool) -> (v -> c -> Bool) -> v -> c -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap(((c -> Bool) -> c -> Bool) -> (v -> c -> Bool) -> v -> c -> Bool)
-> ((Bool -> Bool) -> (c -> Bool) -> c -> Bool)
-> (Bool -> Bool)
-> (v -> c -> Bool)
-> v
-> c
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Bool -> Bool) -> (c -> Bool) -> c -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) Bool -> Bool
not v -> c -> Bool
forall c v. Containable c v => v -> c -> Bool
member

instance Eq a => Containable [a] a where
    member :: a -> [a] -> Bool
member = a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem

instance Containable [a] Index where
    member :: Index -> [a] -> Bool
member (Index Int
i) [a]
l = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int -> [a] -> Bool
forall t a. (Eq t, Num t) => t -> [a] -> Bool
atLeast (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [a]
l where
        atLeast :: t -> [a] -> Bool
atLeast t
0 [a]
_ = Bool
True
        atLeast t
_ [] = Bool
False
        atLeast t
n (a
_:[a]
xs) = t -> [a] -> Bool
atLeast (t
nt -> t -> t
forall a. Num a => a -> a -> a
-t
1) [a]
xs

instance Ord a => Containable (S.Set a) a where
    member :: a -> Set a -> Bool
member = a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member

instance Ord k => Containable (M.Map k a) k where
    member :: k -> Map k a -> Bool
member = k -> Map k a -> Bool
forall k a. Ord k => k -> Map k a -> Bool
M.member

instance Eq a => Containable (M.Map k a) a where
    member :: a -> Map k a -> Bool
member = a -> Map k a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem

instance (Ord k, Eq a) => Containable (M.Map k a) (k, a) where
    member :: (k, a) -> Map k a -> Bool
member (k
k, a
a) Map k a
m = Map k a
m Map k a -> k -> Maybe a
forall k a. Ord k => Map k a -> k -> Maybe a
M.!? k
k Maybe a -> Maybe a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Maybe a
forall a. a -> Maybe a
Just a
a

instance (Ord a, Ord b) => Containable (IM.IsoMap a b) a where
    member :: a -> IsoMap a b -> Bool
member = a -> IsoMap a b -> Bool
forall i a b. Iso i a b => a -> i -> Bool
IM.member

instance (Ord a, Ord b) => Containable (IM.IsoMap a b) b where
    member :: b -> IsoMap a b -> Bool
member = b -> IsoMap a b -> Bool
forall i a b. Iso i a b => a -> i -> Bool
IM.member

instance Ord k => Containable (PM.PrismMap k a) k where
    member :: k -> PrismMap k a -> Bool
member = k -> PrismMap k a -> Bool
forall k a. Ord k => k -> PrismMap k a -> Bool
PM.key

instance Ord a => Containable (PM.PrismMap k a) a where
    member :: a -> PrismMap k a -> Bool
member = a -> PrismMap k a -> Bool
forall a k. Ord a => a -> PrismMap k a -> Bool
PM.value

instance (Ord k, Ord a) => Containable (PM.PrismMap k a) (k, a) where
    member :: (k, a) -> PrismMap k a -> Bool
member (k
k, a
a) PrismMap k a
pm = k -> PrismMap k a -> Maybe a
forall k (m :: * -> *) a.
(Ord k, MonadFail m) =>
k -> PrismMap k a -> m a
PM.lookup k
k PrismMap k a
pm Maybe a -> Maybe a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Maybe a
forall a. a -> Maybe a
Just a
a

instance Eq a => Containable (P.Pair a) a where
    member :: a -> Pair a -> Bool
member = a -> Pair a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem

instance Eq a => Containable (Q.Quadruple a) a where
    member :: a -> Quadruple a -> Bool
member = a -> Quadruple a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem

class Gunctor c1 c2 v1 v2 | c1 c2 -> v1 v2 where
    gmap :: (v1 -> v2) -> c1 -> c2
    gmap = (v1 -> v2) -> c1 -> c2
forall c1 c2 v1 v2. Gunctor c1 c2 v1 v2 => (v1 -> v2) -> c1 -> c2
(<@>)
    (<@>) :: (v1 -> v2) -> c1 -> c2
    (<@>) = (v1 -> v2) -> c1 -> c2
forall c1 c2 v1 v2. Gunctor c1 c2 v1 v2 => (v1 -> v2) -> c1 -> c2
gmap

instance Ord b => Gunctor (S.Set a) (S.Set b) a b where
    gmap :: (a -> b) -> Set a -> Set b
gmap = (a -> b) -> Set a -> Set b
forall b a. Ord b => (a -> b) -> Set a -> Set b
S.map

instance Ord l => Gunctor (M.Map k a) (M.Map l a) k l where
    gmap :: (k -> l) -> Map k a -> Map l a
gmap = (k -> l) -> Map k a -> Map l a
forall l k a. Ord l => (k -> l) -> Map k a -> Map l a
M.mapKeys

instance Gunctor (M.Map k a) (M.Map k b) a b where
    gmap :: (a -> b) -> Map k a -> Map k b
gmap = (a -> b) -> Map k a -> Map k b
forall a b k. (a -> b) -> Map k a -> Map k b
M.map

instance Ord a2 => Gunctor (IM.IsoMap a1 b) (IM.IsoMap a2 b) a1 a2 where
    gmap :: (a1 -> a2) -> IsoMap a1 b -> IsoMap a2 b
gmap = (a1 -> a2) -> IsoMap a1 b -> IsoMap a2 b
forall a2 a1 b. Ord a2 => (a1 -> a2) -> IsoMap a1 b -> IsoMap a2 b
IM.mapFst

instance Ord b2 => Gunctor (IM.IsoMap a b1) (IM.IsoMap a b2) b1 b2 where
    gmap :: (b1 -> b2) -> IsoMap a b1 -> IsoMap a b2
gmap = (b1 -> b2) -> IsoMap a b1 -> IsoMap a b2
forall b a c. Ord b => (a -> b) -> IsoMap c a -> IsoMap c b
IM.mapSnd

instance Ord a2 => Gunctor (IM.IsoMap a1 b) (IM.IsoMap b a2) a1 a2 where
    gmap :: (a1 -> a2) -> IsoMap a1 b -> IsoMap b a2
gmap = (a1 -> a2) -> IsoMap a1 b -> IsoMap b a2
forall a2 a1 b. Ord a2 => (a1 -> a2) -> IsoMap a1 b -> IsoMap b a2
IM.mapFstRev

instance Ord b2 => Gunctor (IM.IsoMap a b1) (IM.IsoMap b2 a) b1 b2 where
    gmap :: (b1 -> b2) -> IsoMap a b1 -> IsoMap b2 a
gmap = (b1 -> b2) -> IsoMap a b1 -> IsoMap b2 a
forall b a c. Ord b => (a -> b) -> IsoMap c a -> IsoMap b c
IM.mapSndRev

instance Ord l => Gunctor (PM.PrismMap k a) (PM.PrismMap l a) k l where
    gmap :: (k -> l) -> PrismMap k a -> PrismMap l a
gmap = (k -> l) -> PrismMap k a -> PrismMap l a
forall l k a. Ord l => (k -> l) -> PrismMap k a -> PrismMap l a
PM.mapKeys

instance Ord b => Gunctor (PM.PrismMap k a) (PM.PrismMap k b) a b where
    gmap :: (a -> b) -> PrismMap k a -> PrismMap k b
gmap = (a -> b) -> PrismMap k a -> PrismMap k b
forall b a k. Ord b => (a -> b) -> PrismMap k a -> PrismMap k b
PM.map

class Filterable c v where
    filterC :: (v -> Bool) -> c -> c
    (<?>) :: (v -> Bool) -> c -> c
    (<?>) = (v -> Bool) -> c -> c
forall c v. Filterable c v => (v -> Bool) -> c -> c
filterC
    (<?>~) :: ASetter s t c c -> (v -> Bool) -> s -> t
    (<?>~) = (((c -> c) -> s -> t)
-> ((v -> Bool) -> c -> c) -> (v -> Bool) -> s -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> Bool) -> c -> c
forall c v. Filterable c v => (v -> Bool) -> c -> c
filterC) (((c -> c) -> s -> t) -> (v -> Bool) -> s -> t)
-> (ASetter s t c c -> (c -> c) -> s -> t)
-> ASetter s t c c
-> (v -> Bool)
-> s
-> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s t c c -> (c -> c) -> s -> t
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
(%~)
    (<?>=) :: MonadState s m => ASetter s s c c -> (v -> Bool) -> m ()
    (<?>=) = (((c -> c) -> m ())
-> ((v -> Bool) -> c -> c) -> (v -> Bool) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> Bool) -> c -> c
forall c v. Filterable c v => (v -> Bool) -> c -> c
filterC) (((c -> c) -> m ()) -> (v -> Bool) -> m ())
-> (ASetter s s c c -> (c -> c) -> m ())
-> ASetter s s c c
-> (v -> Bool)
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s s c c -> (c -> c) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
(%=)

instance Filterable [a] a where
    filterC :: (a -> Bool) -> [a] -> [a]
filterC = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
L.filter

instance Filterable (S.Set a) a where
    filterC :: (a -> Bool) -> Set a -> Set a
filterC = (a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
S.filter

instance Filterable (M.Map k a) k where
    filterC :: (k -> Bool) -> Map k a -> Map k a
filterC = (k -> a -> Bool) -> Map k a -> Map k a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
M.filterWithKey ((k -> a -> Bool) -> Map k a -> Map k a)
-> ((k -> Bool) -> k -> a -> Bool)
-> (k -> Bool)
-> Map k a
-> Map k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> a -> Bool) -> (k -> Bool) -> k -> a -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> a -> Bool
forall a b. a -> b -> a
const

instance Filterable (M.Map k a) a where
    filterC :: (a -> Bool) -> Map k a -> Map k a
filterC = (a -> Bool) -> Map k a -> Map k a
forall a k. (a -> Bool) -> Map k a -> Map k a
M.filter

instance Filterable (M.Map k a) (k, a) where
    filterC :: ((k, a) -> Bool) -> Map k a -> Map k a
filterC = (k -> a -> Bool) -> Map k a -> Map k a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
M.filterWithKey ((k -> a -> Bool) -> Map k a -> Map k a)
-> (((k, a) -> Bool) -> k -> a -> Bool)
-> ((k, a) -> Bool)
-> Map k a
-> Map k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> Bool) -> k -> a -> Bool
forall a b c. ((a, b) -> c) -> a -> b -> c
curry

instance (Ord a, Ord b) => Filterable (IM.IsoMap a b) a where
    filterC :: (a -> Bool) -> IsoMap a b -> IsoMap a b
filterC = (a -> Bool) -> IsoMap a b -> IsoMap a b
forall i a b. Iso i a b => (a -> Bool) -> i -> i
IM.filter

instance (Ord a, Ord b) => Filterable (IM.IsoMap a b) b where
    filterC :: (b -> Bool) -> IsoMap a b -> IsoMap a b
filterC = (b -> Bool) -> IsoMap a b -> IsoMap a b
forall i a b. Iso i a b => (a -> Bool) -> i -> i
IM.filter

instance Filterable (PM.PrismMap k a) k where
    filterC :: (k -> Bool) -> PrismMap k a -> PrismMap k a
filterC = (k -> Bool) -> PrismMap k a -> PrismMap k a
forall k a. (k -> Bool) -> PrismMap k a -> PrismMap k a
PM.filterKeys

instance Filterable (PM.PrismMap k a) a where
    filterC :: (a -> Bool) -> PrismMap k a -> PrismMap k a
filterC = (a -> Bool) -> PrismMap k a -> PrismMap k a
forall a k. (a -> Bool) -> PrismMap k a -> PrismMap k a
PM.filter

class Insertable c v where
    insert :: MonadFail m => v -> c -> m c
    default insert :: (Containable c v, MonadFail m) => v -> c -> m c
    insert v
v c
c =
        if v
v v -> c -> Bool
forall c v. Containable c v => v -> c -> Bool
`member` c
c
        then String -> m c
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Element already is in container"
        else c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> c -> m c
forall a b. (a -> b) -> a -> b
$ v -> c -> c
forall c v. Insertable c v => v -> c -> c
unsafeInsert v
v c
c
    unsafeInsert :: v -> c -> c
    unsafeInsert v
v = c -> Maybe c -> c
forall a. a -> Maybe a -> a
fromMaybe (c -> Maybe c -> c) -> (c -> Maybe c) -> c -> c
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> v -> c -> Maybe c
forall c v (m :: * -> *).
(Insertable c v, MonadFail m) =>
v -> c -> m c
insert v
v
    (+>) :: v -> c -> c
    (+>) = v -> c -> c
forall c v. Insertable c v => v -> c -> c
unsafeInsert
    (<+) :: c -> v -> c
    (<+) = (v -> c -> c) -> c -> v -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip v -> c -> c
forall c v. Insertable c v => v -> c -> c
unsafeInsert
    (<+~) :: ASetter s t c c -> v -> s -> t
    (<+~) = (((c -> c) -> s -> t) -> (v -> c -> c) -> v -> s -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> c -> c
forall c v. Insertable c v => v -> c -> c
unsafeInsert) (((c -> c) -> s -> t) -> v -> s -> t)
-> (ASetter s t c c -> (c -> c) -> s -> t)
-> ASetter s t c c
-> v
-> s
-> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s t c c -> (c -> c) -> s -> t
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
(%~)
    (<+=) :: MonadState s m => ASetter s s c c -> v -> m ()
    (<+=) = (((c -> c) -> m ()) -> (v -> c -> c) -> v -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> c -> c
forall c v. Insertable c v => v -> c -> c
unsafeInsert) (((c -> c) -> m ()) -> v -> m ())
-> (ASetter s s c c -> (c -> c) -> m ())
-> ASetter s s c c
-> v
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s s c c -> (c -> c) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
(%=)

instance Insertable [a] a where
    insert :: a -> [a] -> m [a]
insert = ((([a] -> [a]) -> [a] -> m [a])
-> (a -> [a] -> [a]) -> a -> [a] -> m [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap((([a] -> [a]) -> [a] -> m [a])
 -> (a -> [a] -> [a]) -> a -> [a] -> m [a])
-> (([a] -> m [a]) -> ([a] -> [a]) -> [a] -> m [a])
-> ([a] -> m [a])
-> (a -> [a] -> [a])
-> a
-> [a]
-> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.([a] -> m [a]) -> ([a] -> [a]) -> [a] -> m [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return a -> [a] -> [a]
forall c v. Insertable c v => v -> c -> c
unsafeInsert
    unsafeInsert :: a -> [a] -> [a]
unsafeInsert = (:)
    <+ :: [a] -> a -> [a]
(<+) = (([a] -> [a]) -> (a -> [a]) -> a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure) (([a] -> [a]) -> a -> [a])
-> ([a] -> [a] -> [a]) -> [a] -> a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
(++)

instance Ord a => Insertable (S.Set a) a where
    unsafeInsert :: a -> Set a -> Set a
unsafeInsert = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
S.insert

instance (Ord k, Eq a) => Insertable (M.Map k a) (k, a) where
    unsafeInsert :: (k, a) -> Map k a -> Map k a
unsafeInsert = (k -> a -> Map k a -> Map k a) -> (k, a) -> Map k a -> Map k a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert

instance (Ord a, Ord b) => Insertable (IM.IsoMap a b) (a, b) where
    insert :: (a, b) -> IsoMap a b -> m (IsoMap a b)
insert = (a, b) -> IsoMap a b -> m (IsoMap a b)
forall i a b (m :: * -> *).
(Iso i a b, MonadFail m) =>
(a, b) -> i -> m i
IM.insert

instance (Ord a, Ord b) => Insertable (IM.IsoMap a b) (b, a) where
    insert :: (b, a) -> IsoMap a b -> m (IsoMap a b)
insert = (b, a) -> IsoMap a b -> m (IsoMap a b)
forall i a b (m :: * -> *).
(Iso i a b, MonadFail m) =>
(a, b) -> i -> m i
IM.insert

instance (Ord k, Ord a) => Insertable (PM.PrismMap k a) (k, a) where
    unsafeInsert :: (k, a) -> PrismMap k a -> PrismMap k a
unsafeInsert = (k -> a -> PrismMap k a -> PrismMap k a)
-> (k, a) -> PrismMap k a -> PrismMap k a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> a -> PrismMap k a -> PrismMap k a
forall k a.
(Ord k, Ord a) =>
k -> a -> PrismMap k a -> PrismMap k a
PM.insert

class Deletable c v where
    delete :: MonadFail m => v -> c -> m c
    default delete :: (Containable c v, MonadFail m) => v -> c -> m c
    delete v
v c
c =
        if v
v v -> c -> Bool
forall c v. Containable c v => v -> c -> Bool
`member` c
c
        then c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> c -> m c
forall a b. (a -> b) -> a -> b
$ v -> c -> c
forall c v. Deletable c v => v -> c -> c
unsafeDelete v
v c
c
        else String -> m c
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Element is not in container"
    unsafeDelete :: v -> c -> c
    unsafeDelete v
v = c -> Maybe c -> c
forall a. a -> Maybe a -> a
fromMaybe (c -> Maybe c -> c) -> (c -> Maybe c) -> c -> c
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> v -> c -> Maybe c
forall c v (m :: * -> *).
(Deletable c v, MonadFail m) =>
v -> c -> m c
delete v
v
    (\>) :: v -> c -> c
    (\>) = v -> c -> c
forall c v. Deletable c v => v -> c -> c
unsafeDelete
    (<\) :: c -> v -> c
    (<\) = (v -> c -> c) -> c -> v -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip v -> c -> c
forall c v. Deletable c v => v -> c -> c
unsafeDelete
    (<\~) :: ASetter s t c c -> v -> s -> t
    (<\~) = (((c -> c) -> s -> t) -> (v -> c -> c) -> v -> s -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> c -> c
forall c v. Deletable c v => v -> c -> c
unsafeDelete) (((c -> c) -> s -> t) -> v -> s -> t)
-> (ASetter s t c c -> (c -> c) -> s -> t)
-> ASetter s t c c
-> v
-> s
-> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s t c c -> (c -> c) -> s -> t
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
(%~)
    (<\=) :: MonadState s m => ASetter s s c c -> v -> m ()
    (<\=) = (((c -> c) -> m ()) -> (v -> c -> c) -> v -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> c -> c
forall c v. Deletable c v => v -> c -> c
unsafeDelete) (((c -> c) -> m ()) -> v -> m ())
-> (ASetter s s c c -> (c -> c) -> m ())
-> ASetter s s c c
-> v
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter s s c c -> (c -> c) -> m ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
(%=)

instance Eq a => Deletable [a] a where
    unsafeDelete :: a -> [a] -> [a]
unsafeDelete = a -> [a] -> [a]
forall a. Eq a => a -> [a] -> [a]
L.delete

instance Deletable [a] Index where
    unsafeDelete :: Index -> [a] -> [a]
unsafeDelete (Index Int
i) [a]
l = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
i [a]
l [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [a]
l

instance Ord a => Deletable (S.Set a) a where
    unsafeDelete :: a -> Set a -> Set a
unsafeDelete = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
S.delete

instance Ord k => Deletable (M.Map k a) k where
    unsafeDelete :: k -> Map k a -> Map k a
unsafeDelete = k -> Map k a -> Map k a
forall k a. Ord k => k -> Map k a -> Map k a
M.delete

instance (Ord a, Ord b) => Deletable (IM.IsoMap a b) a where
    unsafeDelete :: a -> IsoMap a b -> IsoMap a b
unsafeDelete = a -> IsoMap a b -> IsoMap a b
forall i a b. Iso i a b => a -> i -> i
IM.delete

instance (Ord a, Ord b) => Deletable (IM.IsoMap a b) b where
    unsafeDelete :: b -> IsoMap a b -> IsoMap a b
unsafeDelete = b -> IsoMap a b -> IsoMap a b
forall i a b. Iso i a b => a -> i -> i
IM.delete

instance (Ord k, Ord a) => Deletable (PM.PrismMap k a) k where
    delete :: k -> PrismMap k a -> m (PrismMap k a)
delete = k -> PrismMap k a -> m (PrismMap k a)
forall k a (m :: * -> *).
(Ord k, Ord a, MonadFail m) =>
k -> PrismMap k a -> m (PrismMap k a)
PM.deleteKey

instance (Ord k, Ord a) => Deletable (PM.PrismMap k a) a where
    delete :: a -> PrismMap k a -> m (PrismMap k a)
delete = a -> PrismMap k a -> m (PrismMap k a)
forall k a (m :: * -> *).
(Ord k, Ord a, MonadFail m) =>
a -> PrismMap k a -> m (PrismMap k a)
PM.deleteValue