profunctor-optics-0.0.0.5: An optics library compatible with the typeclasses in 'profunctors'.

Safe HaskellNone
LanguageHaskell2010

Data.Profunctor.Optic.Grate

Contents

Synopsis

Grate & Cxgrate

type Grate s t a b = forall p. Closed p => Optic p s t a b Source #

Grates access the codomain of a function.

\( \mathsf{Grate}\;S\;A = \exists I, S \cong I \to A \)

type Grate' s a = Grate s s a a Source #

type Cxgrate k s t a b = forall p. Closed p => CoindexedOptic p k s t a b Source #

type Cxgrate' k s a = Cxgrate k s s a a Source #

Constructors

grate :: (((s -> a) -> b) -> t) -> Grate s t a b Source #

Obtain a Grate from a nested continuation.

The resulting optic is the corepresentable counterpart to Lens, and sits between Iso and Setter.

A Grate lets you lift a profunctor through any representable functor (aka Naperian container). In the special case where the indexing type is finitary (e.g. Bool) then the tabulated type is isomorphic to a fied length vector (e.g. 'V2 a').

The identity container is representable, and representable functors are closed under composition.

See https://www.cs.ox.ac.uk/jeremy.gibbons/publications/proyo.pdf section 4.6 for more background on Grates, and compare to the lens-family version.

Caution: In order for the generated optic to be well-defined, you must ensure that the input function satisfies the following properties:

  • sabt ($ s) ≡ s
  • sabt (k -> f (k . sabt)) ≡ sabt (k -> f ($ k))

More generally, a profunctor optic must be monoidal as a natural transformation:

See Property.

grateVl :: (forall f. Functor f => (f a -> b) -> f s -> t) -> Grate s t a b Source #

Transform a Van Laarhoven grate into a profunctor grate.

Compare lensVl & cotraversalVl.

Caution: In order for the generated family to be well-defined, you must ensure that the traversal1 law holds for the input function:

  • abst runIdentity ≡ runIdentity
  • abst f . fmap (abst g) ≡ abst (f . fmap g . getCompose) . Compose

See Property.

kgrateVl :: (forall f. Functor f => (k -> f a -> b) -> f s -> t) -> Cxgrate k s t a b Source #

TODO: Document

inverting :: (s -> a) -> (b -> t) -> Grate s t a b Source #

Construct a Grate from a pair of inverses.

cloneGrate :: AGrate s t a b -> Grate s t a b Source #

TODO: Document

Optics

represented :: Representable f => Grate (f a) (f b) a b Source #

Obtain a Grate from a Representable functor.

distributed :: Distributive f => Grate (f a) (f b) a b Source #

Obtain a Grate from a distributive functor.

endomorphed :: Grate' (Endo a) a Source #

Obtain a Grate from an endomorphism.

>>> flip appEndo 2 $ zipsWith endomorphed (+) (Endo (*3)) (Endo (*4))
14

connected :: Conn s a -> Grate' s a Source #

Obtain a Grate from a Galois connection.

Useful for giving precise semantics to numerical computations.

This is an example of a Grate that would not be a legal Iso, as Galois connections are not in general inverses.

>>> zipsWith (connected i08i16) (+) 126 1
127
>>> zipsWith (connected i08i16) (+) 126 2
127

continued :: Grate a (Cont r a) r r Source #

Obtain a Grate from a continuation.

zipsWith continued :: (r -> r -> r) -> s -> s -> Cont r s

continuedT :: Grate a (ContT r m a) (m r) (m r) Source #

Obtain a Grate from a continuation.

zipsWith continued :: (m r -> m r -> m r) -> s -> s -> ContT r m s 

calledCC :: MonadCont m => Grate a (m a) (m b) (m a) Source #

Lift the current continuation into the calling context.

zipsWith calledCC :: MonadCont m => (m b -> m b -> m s) -> s -> s -> m s

unlifted :: MonadUnliftIO m => Grate (m a) (m b) (IO a) (IO b) Source #

Unlift an action into an IO context.

liftIOcoview unlifted
>>> let catchA = catch @ArithException
>>> zipsWith unlifted (flip catchA . const) (throwIO Overflow) (print "caught")
"caught" 

Indexed optics

kclosed :: Cxgrate k (c -> a) (c -> b) a b Source #

kfirst :: Cxgrate k a b (a, c) (b, c) Source #

TODO: Document

ksecond :: Cxgrate k a b (c, a) (c, b) Source #

TODO: Document

coindexed :: Representable f => Monoid (Rep f) => Cxgrate (Rep f) (f a) (f b) a b Source #

Obtain a Cxgrate from a representable functor.

>>> kzipsWith (coindexed @Complex) (\t -> if t then (<>) else (><)) (2 :+ 2) (3 :+ 4)
6 :+ 6

See also indexed.

Primitive operators

withGrate :: AGrate s t a b -> ((((s -> a) -> b) -> t) -> r) -> r Source #

Extract the function that characterizes a Grate.

withGrateVl :: Functor f => AGrate s t a b -> (f a -> b) -> f s -> t Source #

Extract the higher order function that characterizes a Grate.

The grate laws can be stated in terms or withGrate:

Identity:

withGrateVl o runIdentity ≡ runIdentity

Composition:

withGrateVl o f . fmap (withGrateVl o g) ≡ withGrateVl o (f . fmap g . getCompose) . Compose

Operators

coview :: AGrate s t a b -> b -> t Source #

Set all fields to the given value.

This is essentially a restricted variant of review.

zipsWith :: AGrate s t a b -> (a -> a -> b) -> s -> s -> t Source #

Zip over a Grate.

f -> zipsWith closed (zipsWith closed f) ≡ zipsWith (closed . closed)

kzipsWith :: Monoid k => ACxgrate k s t a b -> (k -> a -> a -> b) -> s -> s -> t Source #

zipsWith3 :: AGrate s t a b -> (a -> a -> a -> b) -> s -> s -> s -> t Source #

Zip over a Grate with 3 arguments.

zipsWith4 :: AGrate s t a b -> (a -> a -> a -> a -> b) -> s -> s -> s -> s -> t Source #

Zip over a Grate with 4 arguments.

toClosure :: Closed p => AGrate s t a b -> p a b -> Closure p s t Source #

Use a Grate to construct a Closure.

toEnvironment :: Closed p => AGrate s t a b -> p a b -> Environment p s t Source #

Use a Grate to construct an Environment.

Classes

class Profunctor p => Closed (p :: Type -> Type -> Type) where #

A strong profunctor allows the monoidal structure to pass through.

A closed profunctor allows the closed structure to pass through.

Methods

closed :: p a b -> p (x -> a) (x -> b) #

Instances
(Distributive f, Monad f) => Closed (Kleisli f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Kleisli f a b -> Kleisli f (x -> a) (x -> b) #

Closed (Environment p) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Environment p a b -> Environment p (x -> a) (x -> b) #

Closed p => Closed (Coyoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

closed :: Coyoneda p a b -> Coyoneda p (x -> a) (x -> b) #

Closed p => Closed (Yoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

closed :: Yoneda p a b -> Yoneda p (x -> a) (x -> b) #

Profunctor p => Closed (Closure p) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Closure p a b -> Closure p (x -> a) (x -> b) #

Distributive f => Closed (Star f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Star f a b -> Star f (x -> a) (x -> b) #

Functor f => Closed (Costar f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Costar f a b -> Costar f (x -> a) (x -> b) #

Closed (Tagged :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Tagged a b -> Tagged (x -> a) (x -> b) #

Closed (Conjoin j) Source # 
Instance details

Defined in Data.Profunctor.Optic.Index

Methods

closed :: Conjoin j a b -> Conjoin j (x -> a) (x -> b) #

Closed ((->) :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: (a -> b) -> (x -> a) -> (x -> b) #

Functor f => Closed (Cokleisli f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Cokleisli f a b -> Cokleisli f (x -> a) (x -> b) #

Closed (GrismRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

closed :: GrismRep a b a0 b0 -> GrismRep a b (x -> a0) (x -> b0) #

Closed (GrateRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

closed :: GrateRep a b a0 b0 -> GrateRep a b (x -> a0) (x -> b0) #

Closed (CxgrateRep k a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

closed :: CxgrateRep k a b a0 b0 -> CxgrateRep k a b (x -> a0) (x -> b0) #

(Closed p, Closed q) => Closed (Sum p q) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Sum p q a b -> Sum p q (x -> a) (x -> b) #

(Closed p, Closed q) => Closed (Product p q) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Product p q a b -> Product p q (x -> a) (x -> b) #

(Functor f, Closed p) => Closed (Tannen f p) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Tannen f p a b -> Tannen f p (x -> a) (x -> b) #

class Profunctor p => Costrong (p :: Type -> Type -> Type) where #

Analogous to ArrowLoop, loop = unfirst

Minimal complete definition

unfirst | unsecond

Methods

unfirst :: p (a, d) (b, d) -> p a b #

Laws:

unfirstunsecond . dimap swap swap
lmap (,()) ≡ unfirst . rmap (,())
unfirst . lmap (second f) ≡ unfirst . rmap (second f)
unfirst . unfirst = unfirst . dimap assoc unassoc where
  assoc ((a,b),c) = (a,(b,c))
  unassoc (a,(b,c)) = ((a,b),c)

unsecond :: p (d, a) (d, b) -> p a b #

Laws:

unsecondunfirst . dimap swap swap
lmap ((),) ≡ unsecond . rmap ((),)
unsecond . lmap (first f) ≡ unsecond . rmap (first f)
unsecond . unsecond = unsecond . dimap unassoc assoc where
  assoc ((a,b),c) = (a,(b,c))
  unassoc (a,(b,c)) = ((a,b),c)
Instances
MonadFix m => Costrong (Kleisli m) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Kleisli m (a, d) (b, d) -> Kleisli m a b #

unsecond :: Kleisli m (d, a) (d, b) -> Kleisli m a b #

Costrong p => Costrong (Coyoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

unfirst :: Coyoneda p (a, d) (b, d) -> Coyoneda p a b #

unsecond :: Coyoneda p (d, a) (d, b) -> Coyoneda p a b #

Costrong p => Costrong (Yoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

unfirst :: Yoneda p (a, d) (b, d) -> Yoneda p a b #

unsecond :: Yoneda p (d, a) (d, b) -> Yoneda p a b #

Costrong (Cotambara p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Cotambara p (a, d) (b, d) -> Cotambara p a b #

unsecond :: Cotambara p (d, a) (d, b) -> Cotambara p a b #

Costrong (Copastro p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Copastro p (a, d) (b, d) -> Copastro p a b #

unsecond :: Copastro p (d, a) (d, b) -> Copastro p a b #

Functor f => Costrong (Costar f) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Costar f (a, d) (b, d) -> Costar f a b #

unsecond :: Costar f (d, a) (d, b) -> Costar f a b #

ArrowLoop p => Costrong (WrappedArrow p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: WrappedArrow p (a, d) (b, d) -> WrappedArrow p a b #

unsecond :: WrappedArrow p (d, a) (d, b) -> WrappedArrow p a b #

Costrong (Tagged :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Tagged (a, d) (b, d) -> Tagged a b #

unsecond :: Tagged (d, a) (d, b) -> Tagged a b #

Costrong (Conjoin j) Source # 
Instance details

Defined in Data.Profunctor.Optic.Index

Methods

unfirst :: Conjoin j (a, d) (b, d) -> Conjoin j a b #

unsecond :: Conjoin j (d, a) (d, b) -> Conjoin j a b #

Costrong ((->) :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: ((a, d) -> (b, d)) -> a -> b #

unsecond :: ((d, a) -> (d, b)) -> a -> b #

Functor f => Costrong (Cokleisli f) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Cokleisli f (a, d) (b, d) -> Cokleisli f a b #

unsecond :: Cokleisli f (d, a) (d, b) -> Cokleisli f a b #

Costrong (GrateRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

unfirst :: GrateRep a b (a0, d) (b0, d) -> GrateRep a b a0 b0 #

unsecond :: GrateRep a b (d, a0) (d, b0) -> GrateRep a b a0 b0 #

Strong p => Costrong (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

unfirst :: Re p s t (a, d) (b, d) -> Re p s t a b #

unsecond :: Re p s t (d, a) (d, b) -> Re p s t a b #

(Costrong p, Costrong q) => Costrong (Sum p q) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Sum p q (a, d) (b, d) -> Sum p q a b #

unsecond :: Sum p q (d, a) (d, b) -> Sum p q a b #

(Costrong p, Costrong q) => Costrong (Product p q) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Product p q (a, d) (b, d) -> Product p q a b #

unsecond :: Product p q (d, a) (d, b) -> Product p q a b #

(Functor f, Costrong p) => Costrong (Tannen f p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Tannen f p (a, d) (b, d) -> Tannen f p a b #

unsecond :: Tannen f p (d, a) (d, b) -> Tannen f p a b #