{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
module Optics.IxFold
(
IxFold
, ifoldVL
, ifoldMapOf
, ifoldrOf
, ifoldlOf'
, itoListOf
, itraverseOf_
, iforOf_
, ifolded
, ifolding
, ifoldring
, iheadOf
, ilastOf
, ianyOf
, iallOf
, inoneOf
, ifindOf
, ifindMOf
, ipre
, ifiltered
, ibackwards_
, isumming
, ifailing
, A_Fold
, FoldableWithIndex(..)
) where
import Control.Applicative.Backwards
import Data.Monoid
import Data.Profunctor.Indexed
import Optics.Internal.Bi
import Optics.Internal.Indexed
import Optics.Internal.Indexed.Classes
import Optics.Internal.Fold
import Optics.Internal.IxFold
import Optics.Internal.Optic
import Optics.Internal.Utils
import Optics.IxAffineFold
import Optics.Fold
type IxFold i s a = Optic' A_Fold (WithIx i) s a
ifoldVL
:: (forall f. Applicative f => (i -> a -> f u) -> s -> f v)
-> IxFold i s a
ifoldVL :: forall i a u s v.
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u) -> s -> f v)
-> IxFold i s a
ifoldVL forall (f :: * -> *). Applicative f => (i -> a -> f u) -> s -> f v
f = forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic (forall (p :: * -> * -> * -> *) i a u s v j t b.
(Bicontravariant p, Traversing p) =>
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u) -> s -> f v)
-> Optic__ p j (i -> j) s t a b
ifoldVL__ forall (f :: * -> *). Applicative f => (i -> a -> f u) -> s -> f v
f)
{-# INLINE ifoldVL #-}
ifoldMapOf
:: (Is k A_Fold, Monoid m, is `HasSingleIndex` i)
=> Optic' k is s a
-> (i -> a -> m) -> s -> m
ifoldMapOf :: forall k m (is :: IxList) i s a.
(Is k A_Fold, Monoid m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m) -> s -> m
ifoldMapOf Optic' k is s a
o = \i -> a -> m
f -> forall r i a b. IxForget r i a b -> i -> a -> r
runIxForget (forall (p :: * -> * -> * -> *) k (is :: IxList) s t a b i.
Profunctor p =>
Optic k is s t a b -> Optic_ k p i (Curry is i) s t a b
getOptic (forall destKind srcKind (is :: IxList) s t a b.
Is srcKind destKind =>
Optic srcKind is s t a b -> Optic destKind is s t a b
castOptic @A_Fold Optic' k is s a
o) (forall r i a b. (i -> a -> r) -> IxForget r i a b
IxForget i -> a -> m
f)) forall a. a -> a
id
{-# INLINE ifoldMapOf #-}
ifoldrOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a
-> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf :: forall k (is :: IxList) i s a r.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf Optic' k is s a
o = \i -> a -> r -> r
iarr r
r0 s
s -> (\Endo r
e -> forall a. Endo a -> a -> a
appEndo Endo r
e r
r0) forall a b. (a -> b) -> a -> b
$ forall k m (is :: IxList) i s a.
(Is k A_Fold, Monoid m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m) -> s -> m
ifoldMapOf Optic' k is s a
o (\i
i -> forall a. (a -> a) -> Endo a
Endo forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. i -> a -> r -> r
iarr i
i) s
s
{-# INLINE ifoldrOf #-}
ifoldlOf'
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a
-> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf' :: forall k (is :: IxList) i s a r.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> r -> a -> r) -> r -> s -> r
ifoldlOf' Optic' k is s a
o = \i -> r -> a -> r
irar r
r0 s
s -> forall k (is :: IxList) i s a r.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf Optic' k is s a
o (\i
i a
a r -> r
rr r
r -> r -> r
rr forall a b. (a -> b) -> a -> b
$! i -> r -> a -> r
irar i
i r
r a
a) forall a. a -> a
id s
s r
r0
{-# INLINE ifoldlOf' #-}
itoListOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a
-> s -> [(i, a)]
itoListOf :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> s -> [(i, a)]
itoListOf Optic' k is s a
o = forall k (is :: IxList) i s a r.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf Optic' k is s a
o (\i
i -> (:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i
i, )) []
{-# INLINE itoListOf #-}
itraverseOf_
:: (Is k A_Fold, Applicative f, is `HasSingleIndex` i)
=> Optic' k is s a
-> (i -> a -> f r) -> s -> f ()
#if __GLASGOW_HASKELL__ == 802
itraverseOf_ o = \f ->
#else
itraverseOf_ :: forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ Optic' k is s a
o i -> a -> f r
f =
#endif
forall (f :: * -> *) a. Functor f => Traversed f a -> f ()
runTraversed forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k m (is :: IxList) i s a.
(Is k A_Fold, Monoid m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m) -> s -> m
ifoldMapOf Optic' k is s a
o (\i
i -> forall (f :: * -> *) a. f a -> Traversed f a
Traversed forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. i -> a -> f r
f i
i)
{-# INLINE itraverseOf_ #-}
iforOf_
:: (Is k A_Fold, Applicative f, is `HasSingleIndex` i)
=> Optic' k is s a
-> s -> (i -> a -> f r) -> f ()
iforOf_ :: forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> s -> (i -> a -> f r) -> f ()
iforOf_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_
{-# INLINE iforOf_ #-}
ifolded :: FoldableWithIndex i f => IxFold i (f a) a
ifolded :: forall i (f :: * -> *) a. FoldableWithIndex i f => IxFold i (f a) a
ifolded = forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic forall (p :: * -> * -> * -> *) i (f :: * -> *) j a t b.
(Bicontravariant p, Traversing p, FoldableWithIndex i f) =>
Optic__ p j (i -> j) (f a) t a b
ifolded__
{-# INLINE ifolded #-}
ifolding :: FoldableWithIndex i f => (s -> f a) -> IxFold i s a
ifolding :: forall i (f :: * -> *) s a.
FoldableWithIndex i f =>
(s -> f a) -> IxFold i s a
ifolding s -> f a
f = forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> * -> *) b a i c.
Bicontravariant p =>
(b -> a) -> p i a c -> p i b c
contrafirst s -> f a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> * -> *) i (f :: * -> *) j a t b.
(Bicontravariant p, Traversing p, FoldableWithIndex i f) =>
Optic__ p j (i -> j) (f a) t a b
ifolded__
{-# INLINE ifolding #-}
ifoldring
:: (forall f. Applicative f => (i -> a -> f u -> f u) -> f v -> s -> f w)
-> IxFold i s a
ifoldring :: forall i a u v s w.
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u -> f u) -> f v -> s -> f w)
-> IxFold i s a
ifoldring forall (f :: * -> *).
Applicative f =>
(i -> a -> f u -> f u) -> f v -> s -> f w
fr = forall k (is :: IxList) s t a b.
(forall (p :: * -> * -> * -> *) i.
Profunctor p =>
Optic_ k p i (Curry is i) s t a b)
-> Optic k is s t a b
Optic (forall (p :: * -> * -> * -> *) i a u v s w j t b.
(Bicontravariant p, Traversing p) =>
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u -> f u) -> f v -> s -> f w)
-> Optic__ p j (i -> j) s t a b
ifoldring__ forall (f :: * -> *).
Applicative f =>
(i -> a -> f u -> f u) -> f v -> s -> f w
fr)
{-# INLINE ifoldring #-}
ipre
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a
-> IxAffineFold i s a
ipre :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> IxAffineFold i s a
ipre = forall s i a. (s -> Maybe (i, a)) -> IxAffineFold i s a
iafolding forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> s -> Maybe (i, a)
iheadOf
{-# INLINE ipre #-}
ifiltered
:: (Is k A_Fold, is `HasSingleIndex` i)
=> (i -> a -> Bool)
-> Optic' k is s a
-> IxFold i s a
ifiltered :: forall k (is :: IxList) i a s.
(Is k A_Fold, HasSingleIndex is i) =>
(i -> a -> Bool) -> Optic' k is s a -> IxFold i s a
ifiltered i -> a -> Bool
p Optic' k is s a
o = forall i a u s v.
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u) -> s -> f v)
-> IxFold i s a
ifoldVL forall a b. (a -> b) -> a -> b
$ \i -> a -> f ()
f ->
forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ Optic' k is s a
o (\i
i a
a -> if i -> a -> Bool
p i
i a
a then i -> a -> f ()
f i
i a
a else forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
{-# INLINE ifiltered #-}
ibackwards_
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a
-> IxFold i s a
ibackwards_ :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> IxFold i s a
ibackwards_ Optic' k is s a
o = forall (is :: IxList) i k s t a b.
HasSingleIndex is i =>
Optic k '[] s t a b -> Optic k is s t a b -> Optic k is s t a b
conjoined (forall k (is :: IxList) s a.
Is k A_Fold =>
Optic' k is s a -> Fold s a
backwards_ Optic' k is s a
o) forall a b. (a -> b) -> a -> b
$ forall i a u s v.
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u) -> s -> f v)
-> IxFold i s a
ifoldVL forall a b. (a -> b) -> a -> b
$ \i -> a -> f Any
f ->
forall {k} (f :: k -> *) (a :: k). Backwards f a -> f a
forwards forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ Optic' k is s a
o (\i
i -> forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. i -> a -> f Any
f i
i)
{-# INLINE ibackwards_ #-}
isumming
:: (Is k A_Fold, Is l A_Fold,
is1 `HasSingleIndex` i, is2 `HasSingleIndex` i)
=> Optic' k is1 s a
-> Optic' l is2 s a
-> IxFold i s a
isumming :: forall k l (is1 :: IxList) i (is2 :: IxList) s a.
(Is k A_Fold, Is l A_Fold, HasSingleIndex is1 i,
HasSingleIndex is2 i) =>
Optic' k is1 s a -> Optic' l is2 s a -> IxFold i s a
isumming Optic' k is1 s a
a Optic' l is2 s a
b = forall (is :: IxList) i k s t a b.
HasSingleIndex is i =>
Optic k '[] s t a b -> Optic k is s t a b -> Optic k is s t a b
conjoined (forall k l (is :: IxList) s a (js :: IxList).
(Is k A_Fold, Is l A_Fold) =>
Optic' k is s a -> Optic' l js s a -> Fold s a
summing Optic' k is1 s a
a Optic' l is2 s a
b) forall a b. (a -> b) -> a -> b
$ forall i a u s v.
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u) -> s -> f v)
-> IxFold i s a
ifoldVL forall a b. (a -> b) -> a -> b
$ \i -> a -> f Any
f s
s ->
forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ Optic' k is1 s a
a i -> a -> f Any
f s
s forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ Optic' l is2 s a
b i -> a -> f Any
f s
s
infixr 6 `isumming`
{-# INLINE isumming #-}
ifailing
:: (Is k A_Fold, Is l A_Fold, is1 `HasSingleIndex` i, is2 `HasSingleIndex` i)
=> Optic' k is1 s a
-> Optic' l is2 s a
-> IxFold i s a
ifailing :: forall k l (is1 :: IxList) i (is2 :: IxList) s a.
(Is k A_Fold, Is l A_Fold, HasSingleIndex is1 i,
HasSingleIndex is2 i) =>
Optic' k is1 s a -> Optic' l is2 s a -> IxFold i s a
ifailing Optic' k is1 s a
a Optic' l is2 s a
b = forall (is :: IxList) i k s t a b.
HasSingleIndex is i =>
Optic k '[] s t a b -> Optic k is s t a b -> Optic k is s t a b
conjoined (forall k l (is :: IxList) s a (js :: IxList).
(Is k A_Fold, Is l A_Fold) =>
Optic' k is s a -> Optic' l js s a -> Fold s a
failing Optic' k is1 s a
a Optic' l is2 s a
b) forall a b. (a -> b) -> a -> b
$ forall i a u s v.
(forall (f :: * -> *).
Applicative f =>
(i -> a -> f u) -> s -> f v)
-> IxFold i s a
ifoldVL forall a b. (a -> b) -> a -> b
$ \i -> a -> f Any
f s
s ->
let OrT Bool
visited f ()
fu = forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ Optic' k is1 s a
a (\i
i -> forall (f :: * -> *) a. f a -> OrT f a
wrapOrT forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> a -> f Any
f i
i) s
s
in if Bool
visited
then f ()
fu
else forall k (f :: * -> *) (is :: IxList) i s a r.
(Is k A_Fold, Applicative f, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> f r) -> s -> f ()
itraverseOf_ Optic' l is2 s a
b i -> a -> f Any
f s
s
infixl 3 `ifailing`
{-# INLINE ifailing #-}
iheadOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a -> s -> Maybe (i, a)
iheadOf :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> s -> Maybe (i, a)
iheadOf Optic' k is s a
o = forall a. Leftmost a -> Maybe a
getLeftmost forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k m (is :: IxList) i s a.
(Is k A_Fold, Monoid m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m) -> s -> m
ifoldMapOf Optic' k is s a
o (\i
i -> forall a. a -> Leftmost a
LLeaf forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i
i, ))
{-# INLINE iheadOf #-}
ilastOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a -> s -> Maybe (i, a)
ilastOf :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> s -> Maybe (i, a)
ilastOf Optic' k is s a
o = forall a. Rightmost a -> Maybe a
getRightmost forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k m (is :: IxList) i s a.
(Is k A_Fold, Monoid m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m) -> s -> m
ifoldMapOf Optic' k is s a
o (\i
i -> forall a. a -> Rightmost a
RLeaf forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i
i, ))
{-# INLINE ilastOf #-}
ianyOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a -> (i -> a -> Bool) -> s -> Bool
ianyOf :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> Bool) -> s -> Bool
ianyOf Optic' k is s a
o = \i -> a -> Bool
f -> Any -> Bool
getAny forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. forall k m (is :: IxList) i s a.
(Is k A_Fold, Monoid m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m) -> s -> m
ifoldMapOf Optic' k is s a
o (\i
i -> Bool -> Any
Any forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. i -> a -> Bool
f i
i)
{-# INLINE ianyOf #-}
iallOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a -> (i -> a -> Bool) -> s -> Bool
iallOf :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> Bool) -> s -> Bool
iallOf Optic' k is s a
o = \i -> a -> Bool
f -> All -> Bool
getAll forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. forall k m (is :: IxList) i s a.
(Is k A_Fold, Monoid m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m) -> s -> m
ifoldMapOf Optic' k is s a
o (\i
i -> Bool -> All
All forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. i -> a -> Bool
f i
i)
{-# INLINE iallOf #-}
inoneOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a -> (i -> a -> Bool) -> s -> Bool
inoneOf :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> Bool) -> s -> Bool
inoneOf Optic' k is s a
o = \i -> a -> Bool
f -> Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> Bool) -> s -> Bool
ianyOf Optic' k is s a
o i -> a -> Bool
f
{-# INLINE inoneOf #-}
ifindOf
:: (Is k A_Fold, is `HasSingleIndex` i)
=> Optic' k is s a -> (i -> a -> Bool) -> s -> Maybe (i, a)
ifindOf :: forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> Bool) -> s -> Maybe (i, a)
ifindOf Optic' k is s a
o = \i -> a -> Bool
p -> forall k (is :: IxList) i s a.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> s -> Maybe (i, a)
iheadOf (forall k (is :: IxList) i a s.
(Is k A_Fold, HasSingleIndex is i) =>
(i -> a -> Bool) -> Optic' k is s a -> IxFold i s a
ifiltered i -> a -> Bool
p Optic' k is s a
o)
{-# INLINE ifindOf #-}
ifindMOf
:: (Is k A_Fold, Monad m, is `HasSingleIndex` i)
=> Optic' k is s a -> (i -> a -> m Bool) -> s -> m (Maybe (i, a))
ifindMOf :: forall k (m :: * -> *) (is :: IxList) i s a.
(Is k A_Fold, Monad m, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> m Bool) -> s -> m (Maybe (i, a))
ifindMOf Optic' k is s a
o = \i -> a -> m Bool
f -> forall k (is :: IxList) i s a r.
(Is k A_Fold, HasSingleIndex is i) =>
Optic' k is s a -> (i -> a -> r -> r) -> r -> s -> r
ifoldrOf Optic' k is s a
o
(\i
i a
a m (Maybe (i, a))
y -> i -> a -> m Bool
f i
i a
a forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Bool
r -> if Bool
r then forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. a -> Maybe a
Just (i
i, a
a)) else m (Maybe (i, a))
y)
(forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing)
{-# INLINE ifindMOf #-}