{-# LANGUAGE RankNTypes #-}
module Fresnel.Fold
( -- * Folds
  Fold
, IsFold
  -- * Construction
, folded
, unfolded
, folding
, foldring
, foldMapping
, foldMap1ing
, ignored
, backwards
, iterated
, filtered
, repeated
, replicated
, cycled
, takingWhile
  -- * Elimination
, has
, hasn't
, foldMapOf
, foldMapByOf
, foldrOf
, foldlOf'
, foldOf
, foldByOf
, sequenceOf_
, traverseOf_
, forOf_
, toListOf
, anyOf
, allOf
, noneOf
, andOf
, orOf
, productOf
, sumOf
, altOf
, asumOf
, concatOf
, concatMapOf
, elemOf
, notElemOf
, lengthOf
, nullOf
, notNullOf
, firstOf
, lastOf
, minimumOf
, minimumByOf
, maximumOf
, maximumByOf
, findOf
, findMOf
, lookupOf
, previews
, preview
, (^?)
, Failover(..)
, Union(..)
) where

import Control.Applicative (Alternative(..))
import Control.Applicative.Backwards
import Data.Foldable (traverse_)
import Data.Functor (void)
import Data.Functor.Contravariant
import Data.Monoid
import Data.Profunctor
import Data.Profunctor.Unsafe ((#.), (.#))
import Fresnel.Bifunctor.Contravariant
import Fresnel.Fold1.Internal (IsFold1)
import Fresnel.Functor.Traversed
import Fresnel.Monoid.Cons as Cons
import Fresnel.Monoid.Fork as Fork
import Fresnel.Monoid.Snoc as Snoc
import Fresnel.Optic
import Fresnel.OptionalFold.Internal (IsOptionalFold)
import Fresnel.Traversal (IsTraversal, ignored, traversal)

-- Folds

type Fold s a = forall p . IsFold p => Optic' p s a

class (IsOptionalFold p, IsFold1 p, IsTraversal p) => IsFold p

instance Monoid r => IsFold (Forget r)
instance (Applicative f, Traversable f, Contravariant f) => IsFold (Star f)


-- Construction

folded :: Foldable f => Fold (f a) a
folded :: forall (f :: * -> *) a. Foldable f => Fold (f a) a
folded = p (f a) () -> p (f a) (f a)
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p (f a) () -> p (f a) (f a))
-> (p a a -> p (f a) ()) -> p a a -> p (f a) (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> f a -> f ())
-> Traversal (f a) () a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (a -> f a) -> f a -> f ()
forall (f :: * -> *). Applicative f => (a -> f a) -> f a -> f ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_

unfolded :: (s -> Maybe (a, s)) -> Fold s a
unfolded :: forall s a. (s -> Maybe (a, s)) -> Fold s a
unfolded s -> Maybe (a, s)
coalg = p s () -> p s s
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p s () -> p s s) -> (p a a -> p s ()) -> p a a -> p s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> s -> f ())
-> Traversal s () a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
f -> let loop :: s -> f ()
loop = f () -> ((a, s) -> f ()) -> Maybe (a, s) -> f ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (\ (a
a, s
s) -> a -> f a
f a
a f a -> f () -> f ()
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> s -> f ()
loop s
s) (Maybe (a, s) -> f ()) -> (s -> Maybe (a, s)) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
coalg in s -> f ()
loop)

folding :: Foldable f => (s -> f a) -> Fold s a
folding :: forall (f :: * -> *) s a. Foldable f => (s -> f a) -> Fold s a
folding s -> f a
f = (s -> f a) -> (s -> ()) -> p (f a) () -> p s s
forall a' a b' b. (a' -> a) -> (b' -> b) -> p a b -> p a' b'
forall (p :: * -> * -> *) a' a b' b.
Bicontravariant p =>
(a' -> a) -> (b' -> b) -> p a b -> p a' b'
contrabimap s -> f a
f (() -> s -> ()
forall a b. a -> b -> a
const ()) (p (f a) () -> p s s) -> (p a a -> p (f a) ()) -> p a a -> p s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> f a -> f ())
-> Traversal (f a) () a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (a -> f a) -> f a -> f ()
forall (f :: * -> *). Applicative f => (a -> f a) -> f a -> f ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_

foldring :: (forall f . Applicative f => (a -> f u -> f u) -> f v -> s -> f w) -> Fold s a
foldring :: forall a u v s w.
(forall (f :: * -> *).
 Applicative f =>
 (a -> f u -> f u) -> f v -> s -> f w)
-> Fold s a
foldring forall (f :: * -> *).
Applicative f =>
(a -> f u -> f u) -> f v -> s -> f w
fr = p s w -> p s s
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p s w -> p s s) -> (p a a -> p s w) -> p a a -> p s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> s -> f w)
-> Traversal s w a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
f -> (a -> f u -> f u) -> f v -> s -> f w
forall (f :: * -> *).
Applicative f =>
(a -> f u -> f u) -> f v -> s -> f w
fr (\ a
a -> (a -> f a
f a
a f a -> f u -> f u
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>)) (v -> f v
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure v
forall {a}. a
v)) where
  v :: a
v = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"foldring: value used"

-- | Make a 'Fold' by lifting a 'foldMap'-like function.
--
-- @
-- 'foldMapping' 'foldMap' = 'folded'
-- @
foldMapping :: (forall m . Monoid m => (a -> m) -> (s -> m)) -> Fold s a
foldMapping :: forall a s. (forall m. Monoid m => (a -> m) -> s -> m) -> Fold s a
foldMapping forall m. Monoid m => (a -> m) -> s -> m
fm = p s () -> p s s
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p s () -> p s s) -> (p a a -> p s ()) -> p a a -> p s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> s -> f ())
-> Traversal s () a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
f -> Ap f () -> f ()
forall {k} (f :: k -> *) (a :: k). Ap f a -> f a
getAp (Ap f () -> f ()) -> (s -> Ap f ()) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Ap f ()) -> s -> Ap f ()
forall m. Monoid m => (a -> m) -> s -> m
fm (f () -> Ap f ()
forall {k} (f :: k -> *) (a :: k). f a -> Ap f a
Ap (f () -> Ap f ()) -> (a -> f ()) -> a -> Ap f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> f ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (f a -> f ()) -> (a -> f a) -> a -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f a
f))

-- | Make a 'Fold' by lifting a 'foldMap1'-like function.
foldMap1ing :: (forall m . Semigroup m => (a -> m) -> (s -> m)) -> Fold s a
foldMap1ing :: forall a s.
(forall m. Semigroup m => (a -> m) -> s -> m) -> Fold s a
foldMap1ing forall m. Semigroup m => (a -> m) -> s -> m
fm = p s () -> p s s
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p s () -> p s s) -> (p a a -> p s ()) -> p a a -> p s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> s -> f ())
-> Traversal s () a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
f -> Ap f () -> f ()
forall {k} (f :: k -> *) (a :: k). Ap f a -> f a
getAp (Ap f () -> f ()) -> (s -> Ap f ()) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Ap f ()) -> s -> Ap f ()
forall m. Semigroup m => (a -> m) -> s -> m
fm (f () -> Ap f ()
forall {k} (f :: k -> *) (a :: k). f a -> Ap f a
Ap (f () -> Ap f ()) -> (a -> f ()) -> a -> Ap f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> f ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (f a -> f ()) -> (a -> f a) -> a -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f a
f))

backwards :: Fold s a -> Fold s a
backwards :: forall s a. Fold s a -> Fold s a
backwards Fold s a
o = p s () -> p s s
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p s () -> p s s) -> (p a a -> p s ()) -> p a a -> p s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> s -> f ())
-> Traversal s () a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
f -> Backwards f () -> f ()
forall {k} (f :: k -> *) (a :: k). Backwards f a -> f a
forwards (Backwards f () -> f ()) -> (s -> Backwards f ()) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fold s a -> (a -> Backwards f a) -> s -> Backwards f ()
forall (f :: * -> *) s a r.
Applicative f =>
Fold s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' p s a
Fold s a
o (f a -> Backwards f a
forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f a -> Backwards f a) -> (a -> f a) -> a -> Backwards f a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. a -> f a
f))

iterated :: (a -> a) -> Fold a a
iterated :: forall a. (a -> a) -> Fold a a
iterated a -> a
f = p a Any -> p a a
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p a Any -> p a a) -> (p a a -> p a Any) -> p a a -> p a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> a -> f Any)
-> Traversal a Any a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
g -> let loop :: a -> f b
loop a
a = a -> f a
g a
a f a -> f b -> f b
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> a -> f b
loop (a -> a
f a
a) in a -> f Any
forall {b}. a -> f b
loop)

filtered :: (a -> Bool) -> Fold a a
filtered :: forall a. (a -> Bool) -> Fold a a
filtered a -> Bool
p = (a -> Maybe a) -> Fold a a
forall (f :: * -> *) s a. Foldable f => (s -> f a) -> Fold s a
folding (\ a
a -> if a -> Bool
p a
a then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
forall a. Maybe a
Nothing)

-- | An infinite fold repeatedly producing its input.
--
-- @
-- 'toListOf' 'repeated' a = 'repeat' a
-- @
repeated :: Fold a a
repeated :: forall a (p :: * -> * -> *). IsFold p => Optic' p a a
repeated = p a Any -> p a a
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p a Any -> p a a) -> (p a a -> p a Any) -> p a a -> p a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> a -> f Any)
-> Traversal a Any a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
f a
a -> let loop :: f b
loop = a -> f a
f a
a f a -> f b -> f b
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f b
loop in f Any
forall {b}. f b
loop)

-- | A fold producing its input a finite number of times.
--
-- @
-- 'toListOf' ('replicated' n) a = 'replicate' n a
-- @
replicated :: Int -> Fold a a
replicated :: forall a. Int -> Fold a a
replicated Int
n0 = p a () -> p a a
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p a () -> p a a) -> (p a a -> p a ()) -> p a a -> p a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f a) -> a -> f ())
-> Traversal a () a a
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f a
f -> let loop :: t -> a -> f ()
loop t
n a
a = if t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 then () -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure () else a -> f a
f a
a f a -> f () -> f ()
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> t -> a -> f ()
loop (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
1) a
a in Int -> a -> f ()
forall {t}. (Ord t, Num t) => t -> a -> f ()
loop Int
n0)

cycled :: Fold s a -> Fold s a
cycled :: forall s a. Fold s a -> Fold s a
cycled Fold s a
f = (forall (f :: * -> *).
 Applicative f =>
 (a -> f Any -> f Any) -> f Any -> s -> f Any)
-> Fold s a
forall a u v s w.
(forall (f :: * -> *).
 Applicative f =>
 (a -> f u -> f u) -> f v -> s -> f w)
-> Fold s a
foldring (\ a -> f Any -> f Any
cons f Any
_ s
s -> let loop :: f Any
loop = Fold s a -> (a -> f Any -> f Any) -> f Any -> s -> f Any
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
f a -> f Any -> f Any
cons f Any
loop s
s in f Any
loop)

takingWhile :: (a -> Bool) -> Fold s a -> Fold s a
takingWhile :: forall a s. (a -> Bool) -> Fold s a -> Fold s a
takingWhile a -> Bool
p Fold s a
f = (forall (f :: * -> *).
 Applicative f =>
 (a -> f Any -> f Any) -> f Any -> s -> f Any)
-> Fold s a
forall a u v s w.
(forall (f :: * -> *).
 Applicative f =>
 (a -> f u -> f u) -> f v -> s -> f w)
-> Fold s a
foldring (\ a -> f Any -> f Any
cons f Any
nil -> Fold s a -> (a -> f Any -> f Any) -> f Any -> s -> f Any
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
f (\ a
a f Any
as -> if a -> Bool
p a
a then a -> f Any -> f Any
cons a
a f Any
as else f Any
nil) f Any
nil)


-- Elimination

has :: Fold s a -> (s -> Bool)
has :: forall s a. Fold s a -> s -> Bool
has = Fold s a -> s -> Bool
forall s a. Fold s a -> s -> Bool
notNullOf

hasn't :: Fold s a -> (s -> Bool)
hasn't :: forall s a. Fold s a -> s -> Bool
hasn't = Fold s a -> s -> Bool
forall s a. Fold s a -> s -> Bool
nullOf


foldMapOf :: Monoid m => Fold s a -> ((a -> m) -> (s -> m))
foldMapOf :: forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Fold s a
o = Forget m s s -> s -> m
forall {k} r a (b :: k). Forget r a b -> a -> r
runForget (Forget m s s -> s -> m)
-> (Forget m a a -> Forget m s s) -> Forget m a a -> s -> m
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Forget m a a -> Forget m s s
Fold s a
o (Forget m a a -> s -> m)
-> ((a -> m) -> Forget m a a) -> (a -> m) -> s -> m
forall a b c (q :: * -> * -> *).
Coercible b a =>
(b -> c) -> q a b -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# (a -> m) -> Forget m a a
forall {k} r a (b :: k). (a -> r) -> Forget r a b
Forget

foldMapByOf :: Fold s a -> ((r -> r -> r) -> r -> (a -> r) -> (s -> r))
foldMapByOf :: forall s a r. Fold s a -> (r -> r -> r) -> r -> (a -> r) -> s -> r
foldMapByOf Fold s a
o r -> r -> r
fork r
nil a -> r
leaf s
s = Fork a -> forall r. (r -> r -> r) -> (a -> r) -> r -> r
forall a. Fork a -> forall r. (r -> r -> r) -> (a -> r) -> r -> r
runFork (Forget (Fork a) s s -> s -> Fork a
forall {k} r a (b :: k). Forget r a b -> a -> r
runForget (Optic' (Forget (Fork a)) s a
Fold s a
o ((a -> Fork a) -> Forget (Fork a) a a
forall {k} r a (b :: k). (a -> r) -> Forget r a b
Forget a -> Fork a
forall a. a -> Fork a
Fork.singleton)) s
s) r -> r -> r
fork a -> r
leaf r
nil

foldrOf :: Fold s a -> ((a -> r -> r) -> r -> s -> r)
foldrOf :: forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Fold s a
o a -> r -> r
cons r
nil s
s = Cons a -> forall r. (a -> r -> r) -> r -> r
forall a. Cons a -> forall r. (a -> r -> r) -> r -> r
runCons (Forget (Cons a) s s -> s -> Cons a
forall {k} r a (b :: k). Forget r a b -> a -> r
runForget (Optic' (Forget (Cons a)) s a
Fold s a
o ((a -> Cons a) -> Forget (Cons a) a a
forall {k} r a (b :: k). (a -> r) -> Forget r a b
Forget a -> Cons a
forall a. a -> Cons a
Cons.singleton)) s
s) a -> r -> r
cons r
nil

foldlOf' :: Fold s a -> ((r -> a -> r) -> r -> s -> r)
foldlOf' :: forall s a r. Fold s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Fold s a
o r -> a -> r
snoc r
nil s
s = Snoc a -> forall r. (r -> a -> r) -> r -> r
forall a. Snoc a -> forall r. (r -> a -> r) -> r -> r
runSnoc (Forget (Snoc a) s s -> s -> Snoc a
forall {k} r a (b :: k). Forget r a b -> a -> r
runForget (Optic' (Forget (Snoc a)) s a
Fold s a
o ((a -> Snoc a) -> Forget (Snoc a) a a
forall {k} r a (b :: k). (a -> r) -> Forget r a b
Forget a -> Snoc a
forall a. a -> Snoc a
Snoc.singleton)) s
s) r -> a -> r
snoc r
nil

foldOf :: Monoid a => Fold s a -> (s -> a)
foldOf :: forall a s. Monoid a => Fold s a -> s -> a
foldOf Fold s a
o = Fold s a -> (a -> a) -> s -> a
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s a
Fold s a
o a -> a
forall a. a -> a
id

foldByOf :: Fold s a -> ((a -> a -> a) -> a -> (s -> a))
foldByOf :: forall s a. Fold s a -> (a -> a -> a) -> a -> s -> a
foldByOf Fold s a
o a -> a -> a
fork a
nil s
s = Fork a -> forall r. (r -> r -> r) -> (a -> r) -> r -> r
forall a. Fork a -> forall r. (r -> r -> r) -> (a -> r) -> r -> r
runFork (Forget (Fork a) s s -> s -> Fork a
forall {k} r a (b :: k). Forget r a b -> a -> r
runForget (Optic' (Forget (Fork a)) s a
Fold s a
o ((a -> Fork a) -> Forget (Fork a) a a
forall {k} r a (b :: k). (a -> r) -> Forget r a b
Forget a -> Fork a
forall a. a -> Fork a
Fork.singleton)) s
s) a -> a -> a
fork a -> a
forall a. a -> a
id a
nil

sequenceOf_ :: Applicative f => Fold s (f a) -> (s -> f ())
sequenceOf_ :: forall (f :: * -> *) s a.
Applicative f =>
Fold s (f a) -> s -> f ()
sequenceOf_ Fold s (f a)
o = Traversed f a -> f ()
forall (f :: * -> *) a. Functor f => Traversed f a -> f ()
runTraversed (Traversed f a -> f ()) -> (s -> Traversed f a) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fold s (f a) -> (f a -> Traversed f a) -> s -> Traversed f a
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s (f a)
Fold s (f a)
o f a -> Traversed f a
forall (f :: * -> *) a. f a -> Traversed f a
Traversed

traverseOf_ :: Applicative f => Fold s a -> ((a -> f r) -> (s -> f ()))
traverseOf_ :: forall (f :: * -> *) s a r.
Applicative f =>
Fold s a -> (a -> f r) -> s -> f ()
traverseOf_ Fold s a
o a -> f r
f = Traversed f r -> f ()
forall (f :: * -> *) a. Functor f => Traversed f a -> f ()
runTraversed (Traversed f r -> f ()) -> (s -> Traversed f r) -> s -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fold s a -> (a -> Traversed f r) -> s -> Traversed f r
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s a
Fold s a
o (f r -> Traversed f r
forall (f :: * -> *) a. f a -> Traversed f a
Traversed (f r -> Traversed f r) -> (a -> f r) -> a -> Traversed f r
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. a -> f r
f)

forOf_ :: Applicative f => Fold s a -> (s -> (a -> f r) -> f ())
forOf_ :: forall (f :: * -> *) s a r.
Applicative f =>
Fold s a -> s -> (a -> f r) -> f ()
forOf_ Fold s a
o = ((a -> f r) -> s -> f ()) -> s -> (a -> f r) -> f ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Fold s a -> (a -> f r) -> s -> f ()
forall (f :: * -> *) s a r.
Applicative f =>
Fold s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' p s a
Fold s a
o)

toListOf :: Fold s a -> s -> [a]
toListOf :: forall s a. Fold s a -> s -> [a]
toListOf Fold s a
o = Fold s a -> (a -> [a] -> [a]) -> [a] -> s -> [a]
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
o (:) []

anyOf :: Fold s a -> (a -> Bool) -> (s -> Bool)
anyOf :: forall s a. Fold s a -> (a -> Bool) -> s -> Bool
anyOf Fold s a
o = Fold s a
-> (Bool -> Bool -> Bool) -> Bool -> (a -> Bool) -> s -> Bool
forall s a r. Fold s a -> (r -> r -> r) -> r -> (a -> r) -> s -> r
foldMapByOf Optic' p s a
Fold s a
o Bool -> Bool -> Bool
(||) Bool
False

allOf :: Fold s a -> (a -> Bool) -> (s -> Bool)
allOf :: forall s a. Fold s a -> (a -> Bool) -> s -> Bool
allOf Fold s a
o = Fold s a
-> (Bool -> Bool -> Bool) -> Bool -> (a -> Bool) -> s -> Bool
forall s a r. Fold s a -> (r -> r -> r) -> r -> (a -> r) -> s -> r
foldMapByOf Optic' p s a
Fold s a
o Bool -> Bool -> Bool
(&&) Bool
True

noneOf :: Fold s a -> (a -> Bool) -> (s -> Bool)
noneOf :: forall s a. Fold s a -> (a -> Bool) -> s -> Bool
noneOf Fold s a
o a -> Bool
p = Fold s a -> (a -> Bool) -> s -> Bool
forall s a. Fold s a -> (a -> Bool) -> s -> Bool
anyOf Optic' p s a
Fold s a
o (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)

andOf :: Fold s Bool -> (s -> Bool)
andOf :: forall s. Fold s Bool -> s -> Bool
andOf Fold s Bool
o = All -> Bool
getAll (All -> Bool) -> (s -> All) -> s -> Bool
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s Bool -> (Bool -> All) -> s -> All
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s Bool
Fold s Bool
o Bool -> All
All

orOf :: Fold s Bool -> (s -> Bool)
orOf :: forall s. Fold s Bool -> s -> Bool
orOf Fold s Bool
o = Any -> Bool
getAny (Any -> Bool) -> (s -> Any) -> s -> Bool
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s Bool -> (Bool -> Any) -> s -> Any
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s Bool
Fold s Bool
o Bool -> Any
Any

productOf :: Num a => Fold s a -> (s -> a)
productOf :: forall a s. Num a => Fold s a -> s -> a
productOf Fold s a
o = Product a -> a
forall a. Product a -> a
getProduct (Product a -> a) -> (s -> Product a) -> s -> a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s a -> (a -> Product a) -> s -> Product a
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s a
Fold s a
o a -> Product a
forall a. a -> Product a
Product

sumOf :: Num a => Fold s a -> (s -> a)
sumOf :: forall a s. Num a => Fold s a -> s -> a
sumOf Fold s a
o = Sum a -> a
forall a. Sum a -> a
getSum (Sum a -> a) -> (s -> Sum a) -> s -> a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s a -> (a -> Sum a) -> s -> Sum a
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s a
Fold s a
o a -> Sum a
forall a. a -> Sum a
Sum

altOf :: Alternative f => Fold s a -> (s -> f a)
altOf :: forall (f :: * -> *) s a. Alternative f => Fold s a -> s -> f a
altOf Fold s a
o = Alt f a -> f a
forall {k} (f :: k -> *) (a :: k). Alt f a -> f a
getAlt (Alt f a -> f a) -> (s -> Alt f a) -> s -> f a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s a -> (a -> Alt f a) -> s -> Alt f a
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s a
Fold s a
o (f a -> Alt f a
forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Alt (f a -> Alt f a) -> (a -> f a) -> a -> Alt f a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. a -> f a
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)

asumOf :: Alternative f => Fold s (f a) -> (s -> f a)
asumOf :: forall (f :: * -> *) s a. Alternative f => Fold s (f a) -> s -> f a
asumOf Fold s (f a)
o = Alt f a -> f a
forall {k} (f :: k -> *) (a :: k). Alt f a -> f a
getAlt (Alt f a -> f a) -> (s -> Alt f a) -> s -> f a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s (f a) -> (f a -> Alt f a) -> s -> Alt f a
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s (f a)
Fold s (f a)
o f a -> Alt f a
forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Alt

concatOf :: Fold s [a] -> (s -> [a])
concatOf :: forall s a. Fold s [a] -> s -> [a]
concatOf = Fold s [a] -> s -> [a]
forall a s. Monoid a => Fold s a -> s -> a
foldOf

concatMapOf :: Fold s a -> ((a -> [r]) -> (s -> [r]))
concatMapOf :: forall s a r. Fold s a -> (a -> [r]) -> s -> [r]
concatMapOf = Fold s a -> (a -> [r]) -> s -> [r]
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf

elemOf :: Eq a => Fold s a -> a -> s -> Bool
elemOf :: forall a s. Eq a => Fold s a -> a -> s -> Bool
elemOf Fold s a
o = Fold s a -> (a -> Bool) -> s -> Bool
forall s a. Fold s a -> (a -> Bool) -> s -> Bool
anyOf Optic' p s a
Fold s a
o ((a -> Bool) -> s -> Bool) -> (a -> a -> Bool) -> a -> s -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)

notElemOf :: Eq a => Fold s a -> a -> s -> Bool
notElemOf :: forall a s. Eq a => Fold s a -> a -> s -> Bool
notElemOf Fold s a
o = Fold s a -> (a -> Bool) -> s -> Bool
forall s a. Fold s a -> (a -> Bool) -> s -> Bool
noneOf Optic' p s a
Fold s a
o ((a -> Bool) -> s -> Bool) -> (a -> a -> Bool) -> a -> s -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)

lengthOf :: Fold s a -> (s -> Int)
lengthOf :: forall s a. Fold s a -> s -> Int
lengthOf Fold s a
o = Fold s a -> (a -> Int -> Int) -> Int -> s -> Int
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
o ((Int -> Int) -> a -> Int -> Int
forall a b. a -> b -> a
const (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) Int
0

nullOf :: Fold s a -> (s -> Bool)
nullOf :: forall s a. Fold s a -> s -> Bool
nullOf Fold s a
o = Fold s a -> (a -> Bool -> Bool) -> Bool -> s -> Bool
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
o (\ a
_ Bool
_ -> Bool
False) Bool
True

notNullOf :: Fold s a -> (s -> Bool)
notNullOf :: forall s a. Fold s a -> s -> Bool
notNullOf Fold s a
o = Fold s a -> (a -> Bool -> Bool) -> Bool -> s -> Bool
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
o (\ a
_ Bool
_ -> Bool
True) Bool
False

firstOf :: Fold s a -> (s -> Maybe a)
firstOf :: forall s a. Fold s a -> s -> Maybe a
firstOf Fold s a
o = Fold s a -> (a -> Maybe a -> Maybe a) -> Maybe a -> s -> Maybe a
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
o (\ a
a Maybe a
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
a) Maybe a
forall a. Maybe a
Nothing

lastOf :: Fold s a -> (s -> Maybe a)
lastOf :: forall s a. Fold s a -> s -> Maybe a
lastOf Fold s a
o = Last a -> Maybe a
forall a. Last a -> Maybe a
getLast (Last a -> Maybe a) -> (s -> Last a) -> s -> Maybe a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s a -> (a -> Last a) -> s -> Last a
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s a
Fold s a
o (Maybe a -> Last a
forall a. Maybe a -> Last a
Last (Maybe a -> Last a) -> (a -> Maybe a) -> a -> Last a
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. a -> Maybe a
forall a. a -> Maybe a
Just)

minimumOf :: Ord a => Fold s a -> (s -> Maybe a)
minimumOf :: forall a s. Ord a => Fold s a -> s -> Maybe a
minimumOf Fold s a
o = Fold s a -> (a -> a -> Ordering) -> s -> Maybe a
forall s a. Fold s a -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf Optic' p s a
Fold s a
o a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare

minimumByOf :: Fold s a -> (a -> a -> Ordering) -> (s -> Maybe a)
minimumByOf :: forall s a. Fold s a -> (a -> a -> Ordering) -> s -> Maybe a
minimumByOf Fold s a
o a -> a -> Ordering
cmp = Fold s a -> (Maybe a -> a -> Maybe a) -> Maybe a -> s -> Maybe a
forall s a r. Fold s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' p s a
Fold s a
o (\ Maybe a
a a
b -> a -> Maybe a
forall a. a -> Maybe a
Just (case Maybe a
a of
  Maybe a
Nothing -> a
b
  Just a
a
    | Ordering
GT <- a -> a -> Ordering
cmp a
a a
b -> a
b
    | Bool
otherwise     -> a
a)) Maybe a
forall a. Maybe a
Nothing

maximumOf :: Ord a => Fold s a -> (s -> Maybe a)
maximumOf :: forall a s. Ord a => Fold s a -> s -> Maybe a
maximumOf Fold s a
o = Fold s a -> (a -> a -> Ordering) -> s -> Maybe a
forall s a. Fold s a -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf Optic' p s a
Fold s a
o a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare

maximumByOf :: Fold s a -> (a -> a -> Ordering) -> (s -> Maybe a)
maximumByOf :: forall s a. Fold s a -> (a -> a -> Ordering) -> s -> Maybe a
maximumByOf Fold s a
o a -> a -> Ordering
cmp = Fold s a -> (Maybe a -> a -> Maybe a) -> Maybe a -> s -> Maybe a
forall s a r. Fold s a -> (r -> a -> r) -> r -> s -> r
foldlOf' Optic' p s a
Fold s a
o (\ Maybe a
a a
b -> a -> Maybe a
forall a. a -> Maybe a
Just (case Maybe a
a of
  Maybe a
Nothing -> a
b
  Just a
a
    | Ordering
LT <- a -> a -> Ordering
cmp a
a a
b -> a
b
    | Bool
otherwise     -> a
a)) Maybe a
forall a. Maybe a
Nothing

findOf :: Fold s a -> (a -> Bool) -> (s -> Maybe a)
findOf :: forall s a. Fold s a -> (a -> Bool) -> s -> Maybe a
findOf Fold s a
o a -> Bool
p = Fold s a -> (a -> Maybe a -> Maybe a) -> Maybe a -> s -> Maybe a
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
o (\ a
a Maybe a
as -> if a -> Bool
p a
a then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
as) Maybe a
forall a. Maybe a
Nothing

findMOf :: Monad m => Fold s a -> (a -> m Bool) -> (s -> m (Maybe a))
findMOf :: forall (m :: * -> *) s a.
Monad m =>
Fold s a -> (a -> m Bool) -> s -> m (Maybe a)
findMOf Fold s a
o a -> m Bool
p = Fold s a
-> (a -> m (Maybe a) -> m (Maybe a))
-> m (Maybe a)
-> s
-> m (Maybe a)
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
o (\ a
a m (Maybe a)
as -> do
  c <- a -> m Bool
p a
a
  if c then
    return (Just a)
  else
    as) (Maybe a -> m (Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing)

lookupOf :: Eq k => Fold s (k, v) -> (k -> s -> Maybe v)
lookupOf :: forall k s v. Eq k => Fold s (k, v) -> k -> s -> Maybe v
lookupOf Fold s (k, v)
o k
k = ((k, v) -> v) -> Maybe (k, v) -> Maybe v
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (k, v) -> v
forall a b. (a, b) -> b
snd (Maybe (k, v) -> Maybe v) -> (s -> Maybe (k, v)) -> s -> Maybe v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fold s (k, v) -> ((k, v) -> Bool) -> s -> Maybe (k, v)
forall s a. Fold s a -> (a -> Bool) -> s -> Maybe a
findOf Optic' p s (k, v)
Fold s (k, v)
o ((k -> k -> Bool
forall a. Eq a => a -> a -> Bool
== k
k) (k -> Bool) -> ((k, v) -> k) -> (k, v) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k, v) -> k
forall a b. (a, b) -> a
fst)


previews :: Fold s a -> (a -> r) -> (s -> Maybe r)
previews :: forall s a r. Fold s a -> (a -> r) -> s -> Maybe r
previews Fold s a
o a -> r
f = First r -> Maybe r
forall a. First a -> Maybe a
getFirst (First r -> Maybe r) -> (s -> First r) -> s -> Maybe r
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Fold s a -> (a -> First r) -> s -> First r
forall m s a. Monoid m => Fold s a -> (a -> m) -> s -> m
foldMapOf Optic' p s a
Fold s a
o (Maybe r -> First r
forall a. Maybe a -> First a
First (Maybe r -> First r) -> (a -> Maybe r) -> a -> First r
forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. r -> Maybe r
forall a. a -> Maybe a
Just (r -> Maybe r) -> (a -> r) -> a -> Maybe r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> r
f)

preview :: Fold s a -> s -> Maybe a
preview :: forall s a. Fold s a -> s -> Maybe a
preview Fold s a
o = Fold s a -> (a -> a) -> s -> Maybe a
forall s a r. Fold s a -> (a -> r) -> s -> Maybe r
previews Optic' p s a
Fold s a
o a -> a
forall a. a -> a
id

(^?) :: s -> Fold s a -> Maybe a
s
s ^? :: forall s a. s -> Fold s a -> Maybe a
^? Fold s a
o = Fold s a -> s -> Maybe a
forall s a. Fold s a -> s -> Maybe a
preview Optic' p s a
Fold s a
o s
s

infixl 8 ^?


newtype Failover s a = Failover { forall s a. Failover s a -> Fold s a
getFailover :: Fold s a }

instance Semigroup (Failover s a) where
  Failover Fold s a
a1 <> :: Failover s a -> Failover s a -> Failover s a
<> Failover Fold s a
a2 = Fold s a -> Failover s a
forall s a. Fold s a -> Failover s a
Failover ((s -> Cons a) -> Fold s a
forall (f :: * -> *) s a. Foldable f => (s -> f a) -> Fold s a
folding (\ s
s -> (forall r. (a -> r -> r) -> r -> r) -> Cons a
forall a. (forall r. (a -> r -> r) -> r -> r) -> Cons a
Cons (\ a -> r -> r
cons r
nil -> r -> ((a, r) -> r) -> Maybe (a, r) -> r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Fold s a -> (a -> r -> r) -> r -> s -> r
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
a2 a -> r -> r
cons r
nil s
s) ((a -> r -> r) -> (a, r) -> r
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> r -> r
cons) (Fold s a
-> (a -> Maybe (a, r) -> Maybe (a, r))
-> Maybe (a, r)
-> s
-> Maybe (a, r)
forall s a r. Fold s a -> (a -> r -> r) -> r -> s -> r
foldrOf Optic' p s a
Fold s a
a1 (\ a
a -> (a, r) -> Maybe (a, r)
forall a. a -> Maybe a
Just ((a, r) -> Maybe (a, r))
-> (Maybe (a, r) -> (a, r)) -> Maybe (a, r) -> Maybe (a, r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) a
a (r -> (a, r)) -> (Maybe (a, r) -> r) -> Maybe (a, r) -> (a, r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> ((a, r) -> r) -> Maybe (a, r) -> r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe r
nil ((a -> r -> r) -> (a, r) -> r
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> r -> r
cons)) Maybe (a, r)
forall a. Maybe a
Nothing s
s))))

instance Monoid (Failover s a) where
  mempty :: Failover s a
mempty = Fold s a -> Failover s a
forall s a. Fold s a -> Failover s a
Failover Optic p s s a a
forall s a b (p :: * -> * -> *). IsTraversal p => Optic p s s a b
Fold s a
ignored


newtype Union s a = Union { forall s a. Union s a -> Fold s a
getUnion :: Fold s a }

instance Semigroup (Union s a) where
  Union Fold s a
a1 <> :: Union s a -> Union s a -> Union s a
<> Union Fold s a
a2 = Fold s a -> Union s a
forall s a. Fold s a -> Union s a
Union (p s () -> p s s
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom (p s () -> p s s) -> (p a a -> p s ()) -> p a a -> p s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *). Applicative f => (a -> f Any) -> s -> f ())
-> Traversal s () a Any
forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> Traversal s t a b
traversal (\ a -> f Any
f s
s -> Fold s a -> (a -> f Any) -> s -> f ()
forall (f :: * -> *) s a r.
Applicative f =>
Fold s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' p s a
Fold s a
a1 a -> f Any
f s
s f () -> f () -> f ()
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Fold s a -> (a -> f Any) -> s -> f ()
forall (f :: * -> *) s a r.
Applicative f =>
Fold s a -> (a -> f r) -> s -> f ()
traverseOf_ Optic' p s a
Fold s a
a2 a -> f Any
f s
s) Optic p s () a Any -> (p a a -> p a Any) -> p a a -> p s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p a a -> p a Any
forall (p :: * -> * -> *) a b c.
(Profunctor p, Bicontravariant p) =>
p a b -> p a c
rphantom)

instance Monoid (Union s a) where
  mempty :: Union s a
mempty = Fold s a -> Union s a
forall s a. Fold s a -> Union s a
Union Optic p s s a a
forall s a b (p :: * -> * -> *). IsTraversal p => Optic p s s a b
Fold s a
ignored