Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- type Fold s a = forall p. (Choice p, Representable p, Applicative (Rep p), forall x. Contravariant (p x)) => Optic' p s a
- type Ixfold i s a = forall p. (Choice p, Representable p, Applicative (Rep p), forall x. Contravariant (p x)) => IndexedOptic' p i s a
- fold_ :: Foldable f => (s -> f a) -> Fold s a
- folding :: Traversable f => (s -> a) -> Fold (f s) a
- foldVl :: (forall f. Applicative f => (a -> f b) -> s -> f t) -> Fold s a
- toFold :: AView s a -> Fold s a
- cloneFold :: Monoid a => AFold a s a -> View s a
- folded :: Traversable f => Fold (f a) a
- folded_ :: Foldable f => Fold (f a) a
- unital :: Foldable f => Foldable g => Monoid r => Semiring r => AFold r (f (g a)) a
- summed :: Foldable f => Monoid r => AFold r (f a) a
- multiplied :: Foldable f => Monoid r => Semiring r => AFold r (f a) a
- withFold :: Monoid r => AFold r s a -> (a -> r) -> s -> r
- withIxfold :: AIxfold r i s a -> (i -> a -> r) -> i -> s -> r
- (^..) :: s -> AFold (Endo [a]) s a -> [a]
- (^??) :: Semigroup a => s -> AFold (Maybe a) s a -> Maybe a
- folds :: Monoid a => AFold a s a -> s -> a
- foldsa :: Applicative f => Monoid (f a) => AFold (f a) s a -> s -> f a
- foldsp :: Monoid r => Semiring r => AFold (Prod r) s a -> (a -> r) -> s -> r
- foldsr :: AFold (Endo r) s a -> (a -> r -> r) -> r -> s -> r
- foldsl :: AFold (Dual (Endo r)) s a -> (r -> a -> r) -> r -> s -> r
- foldsl' :: AFold (Endo (Endo r)) s a -> (r -> a -> r) -> r -> s -> r
- lists :: AFold (Endo [a]) s a -> s -> [a]
- traverses_ :: Applicative f => AFold (Endo (f ())) s a -> (a -> f r) -> s -> f ()
- concats :: AFold [r] s a -> (a -> [r]) -> s -> [r]
- finds :: AFold (Endo (Maybe a)) s a -> (a -> Bool) -> s -> Maybe a
- has :: AFold Any s a -> s -> Bool
- hasnt :: AFold All s a -> s -> Bool
- nulls :: AFold All s a -> s -> Bool
- asums :: Alternative f => AFold (Endo (Endo (f a))) s (f a) -> s -> f a
- joins :: Lattice a => AFold (Endo (Endo a)) s a -> a -> s -> a
- joins' :: Lattice a => Min a => AFold (Endo (Endo a)) s a -> s -> a
- meets :: Lattice a => AFold (Endo (Endo a)) s a -> a -> s -> a
- meets' :: Lattice a => Max a => AFold (Endo (Endo a)) s a -> s -> a
- pelem :: Prd a => AFold Any s a -> a -> s -> Bool
- (^%%) :: Monoid i => s -> AIxfold (Endo [(i, a)]) i s a -> [(i, a)]
- ixfoldsr :: Monoid i => AIxfold (Endo r) i s a -> (i -> a -> r -> r) -> r -> s -> r
- ixfoldsrFrom :: AIxfold (Endo r) i s a -> (i -> a -> r -> r) -> i -> r -> s -> r
- ixfoldsl :: Monoid i => AIxfold (Dual (Endo r)) i s a -> (i -> r -> a -> r) -> r -> s -> r
- ixfoldslFrom :: AIxfold (Dual (Endo r)) i s a -> (i -> r -> a -> r) -> i -> r -> s -> r
- ixfoldsrM :: Monoid i => Monad m => AIxfold (Dual (Endo (r -> m r))) i s a -> (i -> a -> r -> m r) -> r -> s -> m r
- ixfoldsrMFrom :: Monad m => AIxfold (Dual (Endo (r -> m r))) i s a -> (i -> a -> r -> m r) -> i -> r -> s -> m r
- ixfoldslM :: Monoid i => Monad m => AIxfold (Endo (r -> m r)) i s a -> (i -> r -> a -> m r) -> r -> s -> m r
- ixfoldslMFrom :: Monad m => AIxfold (Endo (r -> m r)) i s a -> (i -> r -> a -> m r) -> i -> r -> s -> m r
- ixlists :: Monoid i => AIxfold (Endo [(i, a)]) i s a -> s -> [(i, a)]
- ixlistsFrom :: AIxfold (Endo [(i, a)]) i s a -> i -> s -> [(i, a)]
- ixtraverses_ :: Monoid i => Applicative f => AIxfold (Endo (f ())) i s a -> (i -> a -> f r) -> s -> f ()
- ixconcats :: Monoid i => AIxfold [r] i s a -> (i -> a -> [r]) -> s -> [r]
- ixfinds :: Monoid i => AIxfold (Endo (Maybe (i, a))) i s a -> (i -> a -> Bool) -> s -> Maybe (i, a)
- type All = Prod Bool
- type Any = Bool
- type FoldRep r = Star (Const r)
- type AFold r s a = Optic' (FoldRep r) s a
- type AIxfold r i s a = IndexedOptic' (FoldRep r) i s a
- afold :: ((a -> r) -> s -> r) -> AFold r s a
- newtype Star (f :: Type -> Type) d c = Star {
- runStar :: d -> f c
- newtype Costar (f :: Type -> Type) d c = Costar {
- runCostar :: f d -> c
- class (Sieve p (Rep p), Strong p) => Representable (p :: Type -> Type -> Type) where
- class (Cosieve p (Corep p), Costrong p) => Corepresentable (p :: Type -> Type -> Type) where
- class Contravariant (f :: Type -> Type) where
- class Bifunctor (p :: Type -> Type -> Type) where
Fold & Ixfold
type Fold s a = forall p. (Choice p, Representable p, Applicative (Rep p), forall x. Contravariant (p x)) => Optic' p s a Source #
type Ixfold i s a = forall p. (Choice p, Representable p, Applicative (Rep p), forall x. Contravariant (p x)) => IndexedOptic' p i s a Source #
folding :: Traversable f => (s -> a) -> Fold (f s) a Source #
foldVl :: (forall f. Applicative f => (a -> f b) -> s -> f t) -> Fold s a Source #
Optics
folded :: Traversable f => Fold (f a) a Source #
Obtain a Fold
from a Traversable
functor.
unital :: Foldable f => Foldable g => Monoid r => Semiring r => AFold r (f (g a)) a Source #
Expression in a unital semiring
unital
≡summed
.multiplied
>>>
folds unital [[1,2], [3,4 :: Int]]
14
For semirings without a multiplicative unit this is
equivalent to const mempty
:
>>>
folds unital $ (fmap . fmap) Just [[1,2], [3,4 :: Int]]
Just 0
In this situation you most likely want to use nonunital
.
summed :: Foldable f => Monoid r => AFold r (f a) a Source #
Monoidal sum of a foldable collection.
>>>
1 <> 2 <> 3 <> 4 :: Int
10>>>
folds summed [1,2,3,4 :: Int]
10
summed
and multiplied
compose just as they do in arithmetic:
>>>
1 >< 2 <> 3 >< 4 :: Int
14>>>
folds (summed . multiplied) [[1,2], [3,4 :: Int]]
14>>>
(1 <> 2) >< (3 <> 4) :: Int
21>>>
folds (multiplied . summed) [[1,2], [3,4 :: Int]]
21
multiplied :: Foldable f => Monoid r => Semiring r => AFold r (f a) a Source #
Semiring product of a foldable collection.
>>>
1 >< 2 >< 3 >< 4 :: Int
24>>>
folds multiplied [1,2,3,4 :: Int]
24
For semirings without a multiplicative unit this is
equivalent to const mempty
:
>>>
folds multiplied $ fmap Just [1..(5 :: Int)]
Just 0
In this situation you most likely want to use multiplied1
.
Primitive operators
withFold :: Monoid r => AFold r s a -> (a -> r) -> s -> r Source #
Map an optic to a monoid and combine the results.
foldMap
=withFold
folded_'
>>>
withFold both id (["foo"], ["bar", "baz"])
["foo","bar","baz"]
>>>
:t withFold . fold_
withFold . fold_ :: (Monoid r, Foldable f) => (s -> f a) -> (a -> r) -> s -> r
>>>
:t withFold traversed
withFold traversed :: (Monoid r, Traversable f) => (a -> r) -> f a -> r
>>>
:t withFold left
withFold left :: Monoid r => (a -> r) -> (a + c) -> r
>>>
:t withFold t21
withFold t21 :: Monoid r => (a -> r) -> (a, b) -> r
>>>
:t withFold $ selected even
withFold $ selected even :: (Monoid r, Integral a) => (b -> r) -> (a, b) -> r
>>>
:t flip withFold Seq.singleton
flip withFold Seq.singleton :: AFold (Seq a) s a -> s -> Seq a
withIxfold :: AIxfold r i s a -> (i -> a -> r) -> i -> s -> r Source #
TODO: Document
>>>
:t flip withIxfold Map.singleton
flip withIxfold Map.singleton :: AIxfold (Map i a) i s a -> i -> s -> Map i a
Operators
(^..) :: s -> AFold (Endo [a]) s a -> [a] infixl 8 Source #
Infix version of lists
.
toList
xs ≡ xs^..
folding
(^..
) ≡flip
lists
>>>
[[1,2], [3 :: Int]] ^.. id
[[[1,2],[3]]]>>>
[[1,2], [3 :: Int]] ^.. traversed
[[1,2],[3]]>>>
[[1,2], [3 :: Int]] ^.. traversed . traversed
[1,2,3]
>>>
(1,2) ^.. bitraversed
[1,2]
(^..
) :: s ->View
s a -> a :: s ->Fold
s a -> a :: s ->Lens'
s a -> a :: s ->Iso'
s a -> a :: s ->Traversal'
s a -> a :: s ->Prism'
s a -> a :: s ->Affine'
s a -> [a]
(^??) :: Semigroup a => s -> AFold (Maybe a) s a -> Maybe a infixl 8 Source #
Return a semigroup aggregation of the foci, if they exist.
foldsa :: Applicative f => Monoid (f a) => AFold (f a) s a -> s -> f a Source #
TODO: Document
foldsa :: Fold s a -> s -> [a] foldsa :: Applicative f => Setter s t a b -> s -> f a
foldsp :: Monoid r => Semiring r => AFold (Prod r) s a -> (a -> r) -> s -> r Source #
Compute the semiring product of the foci of an optic.
For semirings without a multiplicative unit this is equivalent to const mempty
:
>>>
foldsp folded Just [1..(5 :: Int)]
Just 0
In this situation you most likely want to use folds1p
.
foldsr :: AFold (Endo r) s a -> (a -> r -> r) -> r -> s -> r Source #
Right fold over an optic.
>>>
foldsr folded (<>) 0 [1..5::Int]
15
foldsl :: AFold (Dual (Endo r)) s a -> (r -> a -> r) -> r -> s -> r Source #
Left fold over an optic.
foldsl' :: AFold (Endo (Endo r)) s a -> (r -> a -> r) -> r -> s -> r Source #
Fold repn the elements of a structure, associating to the left, but strictly.
foldl'
≡foldsl'
folding
foldsl'
::Iso'
s a -> (c -> a -> c) -> c -> s -> cfoldsl'
::Lens'
s a -> (c -> a -> c) -> c -> s -> cfoldsl'
::View
s a -> (c -> a -> c) -> c -> s -> cfoldsl'
::Fold
s a -> (c -> a -> c) -> c -> s -> cfoldsl'
::Traversal'
s a -> (c -> a -> c) -> c -> s -> cfoldsl'
::Traversal0'
s a -> (c -> a -> c) -> c -> s -> c
traverses_ :: Applicative f => AFold (Endo (f ())) s a -> (a -> f r) -> s -> f () Source #
Collect an applicative over the foci of an optic.
>>>
traverses_ both putStrLn ("hello","world")
hello world
traverse_
≡traverses_
folded
joins :: Lattice a => AFold (Endo (Endo a)) s a -> a -> s -> a Source #
Compute the join of the foci of an optic.
joins' :: Lattice a => Min a => AFold (Endo (Endo a)) s a -> s -> a Source #
Compute the join of the foci of an optic including a least element.
meets :: Lattice a => AFold (Endo (Endo a)) s a -> a -> s -> a Source #
Compute the meet of the foci of an optic .
meets' :: Lattice a => Max a => AFold (Endo (Endo a)) s a -> s -> a Source #
Compute the meet of the foci of an optic including a greatest element.
pelem :: Prd a => AFold Any s a -> a -> s -> Bool Source #
Determine whether the foci of an optic contain an element equivalent to a given element.
Indexed operators
(^%%) :: Monoid i => s -> AIxfold (Endo [(i, a)]) i s a -> [(i, a)] infixl 8 Source #
Infix version of ixlists
.
ixfoldsrFrom :: AIxfold (Endo r) i s a -> (i -> a -> r -> r) -> i -> r -> s -> r Source #
Indexed right fold over an indexed optic, using an initial index value.
ixfoldslFrom :: AIxfold (Dual (Endo r)) i s a -> (i -> r -> a -> r) -> i -> r -> s -> r Source #
Indexed left fold over an indexed optic, using an initial index value.
ixfoldsrM :: Monoid i => Monad m => AIxfold (Dual (Endo (r -> m r))) i s a -> (i -> a -> r -> m r) -> r -> s -> m r Source #
ixfoldsrMFrom :: Monad m => AIxfold (Dual (Endo (r -> m r))) i s a -> (i -> a -> r -> m r) -> i -> r -> s -> m r Source #
Indexed monadic right fold over an Ixfold
, using an initial index value.
ixfoldslM :: Monoid i => Monad m => AIxfold (Endo (r -> m r)) i s a -> (i -> r -> a -> m r) -> r -> s -> m r Source #
ixfoldslMFrom :: Monad m => AIxfold (Endo (r -> m r)) i s a -> (i -> r -> a -> m r) -> i -> r -> s -> m r Source #
Indexed monadic left fold over an indexed optic, using an initial index value.
ixlistsFrom :: AIxfold (Endo [(i, a)]) i s a -> i -> s -> [(i, a)] Source #
Extract key-value pairs from the foci of an indexed optic, using an initial index value.
ixtraverses_ :: Monoid i => Applicative f => AIxfold (Endo (f ())) i s a -> (i -> a -> f r) -> s -> f () Source #
Collect an applicative over the foci of an indexed optic.
ixfinds :: Monoid i => AIxfold (Endo (Maybe (i, a))) i s a -> (i -> a -> Bool) -> s -> Maybe (i, a) Source #
Find the first focus of an indexed optic that satisfies a predicate, if one exists.
Auxilliary Types
Carriers
type AIxfold r i s a = IndexedOptic' (FoldRep r) i s a Source #
newtype Star (f :: Type -> Type) d c #
Lift a Functor
into a Profunctor
(forwards).
Instances
Functor f => Representable (Star f) | |
Applicative f => Choice (Star f) | |
Traversable f => Cochoice (Star f) | |
Distributive f => Closed (Star f) | |
Defined in Data.Profunctor.Closed | |
Functor m => Strong (Star m) | |
Functor f => Profunctor (Star f) | |
Defined in Data.Profunctor.Types | |
Functor f => Sieve (Star f) f | |
Defined in Data.Profunctor.Sieve | |
Monad f => Category (Star f :: Type -> Type -> Type) | |
Monad f => Monad (Star f a) | |
Functor f => Functor (Star f a) | |
Applicative f => Applicative (Star f a) | |
Contravariant f => Contravariant (Star f a) Source # | |
Alternative f => Alternative (Star f a) | |
MonadPlus f => MonadPlus (Star f a) | |
Distributive f => Distributive (Star f a) | |
Defined in Data.Profunctor.Types | |
Apply f => Apply (Star f a) Source # | |
type Rep (Star f) | |
Defined in Data.Profunctor.Rep |
newtype Costar (f :: Type -> Type) d c #
Lift a Functor
into a Profunctor
(backwards).
Instances
Contravariant f => Bifunctor (Costar f) Source # | |
Functor f => Corepresentable (Costar f) | |
Traversable w => Choice (Costar w) | |
Applicative f => Cochoice (Costar f) | |
Functor f => Closed (Costar f) | |
Defined in Data.Profunctor.Closed | |
Comonad f => Strong (Costar f) Source # | |
Functor f => Costrong (Costar f) | |
Functor f => Profunctor (Costar f) | |
Defined in Data.Profunctor.Types | |
Functor f => Cosieve (Costar f) f | |
Defined in Data.Profunctor.Sieve | |
Monad (Costar f a) | |
Functor (Costar f a) | |
Applicative (Costar f a) | |
Defined in Data.Profunctor.Types | |
Distributive (Costar f d) | |
Defined in Data.Profunctor.Types | |
type Corep (Costar f) | |
Defined in Data.Profunctor.Rep |
Classes
class (Sieve p (Rep p), Strong p) => Representable (p :: Type -> Type -> Type) where #
A Profunctor
p
is Representable
if there exists a Functor
f
such that
p d c
is isomorphic to d -> f c
.
Instances
(Monad m, Functor m) => Representable (Kleisli m) | |
Functor f => Representable (Star f) | |
Representable (Forget r) | |
Representable (Fold0Rep r) Source # | |
Representable ((->) :: Type -> Type -> Type) | |
Representable (LensRep a b) Source # | |
Representable (Traversal0Rep a b) Source # | |
Defined in Data.Profunctor.Optic.Traversal0 type Rep (Traversal0Rep a b) :: Type -> Type # tabulate :: (d -> Rep (Traversal0Rep a b) c) -> Traversal0Rep a b d c # |
class (Cosieve p (Corep p), Costrong p) => Corepresentable (p :: Type -> Type -> Type) where #
A Profunctor
p
is Corepresentable
if there exists a Functor
f
such that
p d c
is isomorphic to f d -> c
.
cotabulate :: (Corep p d -> c) -> p d c #
Laws:
cotabulate
.
cosieve
≡id
cosieve
.
cotabulate
≡id
Instances
Functor f => Corepresentable (Costar f) | |
Corepresentable (Tagged :: Type -> Type -> Type) | |
Corepresentable ((->) :: Type -> Type -> Type) | |
Defined in Data.Profunctor.Rep cotabulate :: (Corep (->) d -> c) -> d -> c # | |
Functor w => Corepresentable (Cokleisli w) | |
Corepresentable (GrateRep a b) Source # | |
class Contravariant (f :: Type -> Type) where #
The class of contravariant functors.
Whereas in Haskell, one can think of a Functor
as containing or producing
values, a contravariant functor is a functor that can be thought of as
consuming values.
As an example, consider the type of predicate functions a -> Bool
. One
such predicate might be negative x = x < 0
, which
classifies integers as to whether they are negative. However, given this
predicate, we can re-use it in other situations, providing we have a way to
map values to integers. For instance, we can use the negative
predicate
on a person's bank balance to work out if they are currently overdrawn:
newtype Predicate a = Predicate { getPredicate :: a -> Bool } instance Contravariant Predicate where contramap f (Predicate p) = Predicate (p . f) | `- First, map the input... `----- then apply the predicate. overdrawn :: Predicate Person overdrawn = contramap personBankBalance negative
Any instance should be subject to the following laws:
contramap id = id contramap f . contramap g = contramap (g . f)
Note, that the second law follows from the free theorem of the type of
contramap
and the first law, so you need only check that the former
condition holds.
Instances
class Bifunctor (p :: Type -> Type -> Type) where #
A bifunctor is a type constructor that takes
two type arguments and is a functor in both arguments. That
is, unlike with Functor
, a type constructor such as Either
does not need to be partially applied for a Bifunctor
instance, and the methods in this class permit mapping
functions over the Left
value or the Right
value,
or both at the same time.
Formally, the class Bifunctor
represents a bifunctor
from Hask
-> Hask
.
Intuitively it is a bifunctor where both the first and second arguments are covariant.
You can define a Bifunctor
by either defining bimap
or by
defining both first
and second
.
If you supply bimap
, you should ensure that:
bimap
id
id
≡id
If you supply first
and second
, ensure:
first
id
≡id
second
id
≡id
If you supply both, you should also ensure:
bimap
f g ≡first
f.
second
g
These ensure by parametricity:
bimap
(f.
g) (h.
i) ≡bimap
f h.
bimap
g ifirst
(f.
g) ≡first
f.
first
gsecond
(f.
g) ≡second
f.
second
g
Since: base-4.8.0.0
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d #
Map over both arguments at the same time.
bimap
f g ≡first
f.
second
g
Examples
>>>
bimap toUpper (+1) ('j', 3)
('J',4)
>>>
bimap toUpper (+1) (Left 'j')
Left 'J'
>>>
bimap toUpper (+1) (Right 3)
Right 4