| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Data.Profunctor.Optic.Setter
Synopsis
- type Setter s t a b = forall p. (Closed p, Choice p, Representable p, Applicative (Rep p), Distributive (Rep p)) => Optic p s t a b
- type Setter' s a = Setter s s a a
- setter :: ((a -> b) -> s -> t) -> Setter s t a b
- ixsetter :: ((i -> a -> b) -> s -> t) -> Ixsetter i s t a b
- closing :: (((s -> a) -> b) -> t) -> Setter s t a b
- type Resetter s t a b = forall p. (Closed p, Cochoice p, Corepresentable p, Apply (Corep p), Traversable (Corep p)) => Optic p s t a b
- type Resetter' s a = Resetter s s a a
- resetter :: ((a -> t) -> s -> t) -> Resetter s t a t
- cxsetter :: ((k -> a -> t) -> s -> t) -> Cxsetter k s t a t
- cod :: Profunctor p => Setter (p r a) (p r b) a b
- dom :: Profunctor p => Setter (p b r) (p a r) a b
- bound :: Monad m => Setter (m a) (m b) a (m b)
- fmapped :: Functor f => Setter (f a) (f b) a b
- contramapped :: Contravariant f => Setter (f b) (f a) a b
- setmapped :: Ord b => Setter (Set a) (Set b) a b
- isetmapped :: Setter' IntSet Int
- foldmapped :: Foldable f => Monoid m => Setter (f a) m a m
- liftedA :: Applicative f => Setter (f a) (f b) a b
- liftedM :: Monad m => Setter (m a) (m b) a b
- locally :: Setter (ReaderT r2 m a) (ReaderT r1 m a) r1 r2
- zipped :: Setter (u -> v -> a) (u -> v -> b) a b
- cond :: (a -> Bool) -> Setter' a a
- modded :: (a -> Bool) -> Setter' (a -> b) b
- reviewed :: Setter (b -> t) (((s -> a) -> b) -> t) s a
- composed :: Setter (s -> a) ((a -> b) -> s -> t) b t
- exmapped :: Exception e1 => Exception e2 => Setter s s e1 e2
- over :: ASetter s t a b -> (a -> b) -> s -> t
- ixover :: Monoid i => AIxsetter i s t a b -> (i -> a -> b) -> s -> t
- under :: AResetter s t a b -> (a -> b) -> s -> t
- cxover :: Monoid k => ACxsetter k s t a b -> (k -> a -> b) -> s -> t
- through :: Optic (->) s t a b -> (a -> b) -> s -> t
- assignA :: Category p => Strong p => ASetter s t a b -> Optic p s t s b
- set :: ASetter s t a b -> b -> s -> t
- ixset :: Monoid i => AIxsetter i s t a b -> (i -> b) -> s -> t
- reset :: AResetter s t a b -> b -> s -> t
- cxset :: Monoid k => ACxsetter k s t a b -> (k -> b) -> s -> t
- (.~) :: ASetter s t a b -> b -> s -> t
- (..~) :: ASetter s t a b -> (a -> b) -> s -> t
- (/~) :: AResetter s t a b -> b -> s -> t
- (//~) :: AResetter s t a b -> (a -> b) -> s -> t
- (?~) :: ASetter s t a (Maybe b) -> b -> s -> t
- (<>~) :: Semigroup a => ASetter s t a a -> a -> s -> t
- (><~) :: Semiring a => ASetter s t a a -> a -> s -> t
- (%~) :: Monoid i => AIxsetter i s t a b -> (i -> b) -> s -> t
- (%%~) :: Monoid i => AIxsetter i s t a b -> (i -> a -> b) -> s -> t
- (#~) :: Monoid k => ACxsetter k s t a b -> (k -> b) -> s -> t
- (##~) :: Monoid k => ACxsetter k s t a b -> (k -> a -> b) -> s -> t
- assigns :: MonadState s m => ASetter s s a b -> b -> m ()
- modifies :: MonadState s m => ASetter s s a b -> (a -> b) -> m ()
- (.=) :: MonadState s m => ASetter s s a b -> b -> m ()
- (..=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m ()
- (%=) :: MonadState s m => Monoid i => AIxsetter i s s a b -> (i -> b) -> m ()
- (%%=) :: MonadState s m => Monoid i => AIxsetter i s s a b -> (i -> a -> b) -> m ()
- (//=) :: MonadState s m => AResetter s s a b -> (a -> b) -> m ()
- (#=) :: MonadState s m => Monoid k => ACxsetter k s s a b -> (k -> b) -> m ()
- (##=) :: MonadState s m => Monoid k => ACxsetter k s s a b -> (k -> a -> b) -> m ()
- (?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m ()
- (<>=) :: MonadState s m => Semigroup a => ASetter' s a -> a -> m ()
- (><=) :: MonadState s m => Semiring a => ASetter' s a -> a -> m ()
- zoom :: Functor m => Optic' (Star (Compose m ((,) c))) ta a -> StateT a m c -> StateT ta m c
- type ASetter s t a b = ARepn Identity s t a b
- type ASetter' s a = ASetter s s a a
- newtype Star (f :: Type -> Type) d c = Star {
- runStar :: d -> f c
- type AResetter s t a b = ACorepn Identity s t a b
- type AResetter' s a = AResetter s s a a
- 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
Setter
type Setter s t a b = forall p. (Closed p, Choice p, Representable p, Applicative (Rep p), Distributive (Rep p)) => Optic p s t a b Source #
A Setter modifies part of a structure.
\( \mathsf{Setter}\;S\;A = \exists F : \mathsf{Functor}, S \equiv F\,A \)
setter :: ((a -> b) -> s -> t) -> Setter s t a b Source #
To demote an optic to a semantic edit combinator, use the section (l ..~) or over l.
>>>[("The",0),("quick",1),("brown",1),("fox",2)] & setter fmap . t21 ..~ Prelude.length[(3,0),(5,1),(5,1),(3,2)]
Caution: In order for the generated optic to be well-defined, you must ensure that the input function satisfies the following properties:
abst id ≡ id
abst f . abst g ≡ abst (f . g)
More generally, a profunctor optic must be monoidal as a natural transformation:
o id ≡ id
o (
Procomposep q) ≡Procompose(o p) (o q)
See Property.
ixsetter :: ((i -> a -> b) -> s -> t) -> Ixsetter i s t a b Source #
Build an Ixsetter from an indexed function.
ixsetter.ixover≡idixover.ixsetter≡id
Caution: In order for the generated optic to be well-defined, you must ensure that the input satisfies the following properties:
iabst (const id) ≡ id
fmap (iabst $ const f) . (iabst $ const g) ≡ iabst (const $ f . g)
See Property.
Resetter
type Resetter s t a b = forall p. (Closed p, Cochoice p, Corepresentable p, Apply (Corep p), Traversable (Corep p)) => Optic p s t a b Source #
cxsetter :: ((k -> a -> t) -> s -> t) -> Cxsetter k s t a t Source #
TODO: Document
Caution: In order for the generated optic to be well-defined, you must ensure that the input satisfies the following properties:
kabst (const id) ≡ id
fmap (kabst $ const f) . (kabst $ const g) ≡ kabst (const $ f . g)
See Property.
Optics
cod :: Profunctor p => Setter (p r a) (p r b) a b Source #
Map covariantly over the output of a Profunctor.
The most common profunctor to use this with is (->).
(dom ..~ f) g x ≡ f (g x) cod @(->) ≡withGrateclosedclosing
>>>(cod ..~ show) length [1,2,3]"3"
dom :: Profunctor p => Setter (p b r) (p a r) a b Source #
Map contravariantly over the input of a Profunctor.
The most common profunctor to use this with is (->).
(dom ..~ f) g x ≡ g (f x)
>>>(dom ..~ show) length [1,2,3]7
bound :: Monad m => Setter (m a) (m b) a (m b) Source #
Setter for monadically transforming a monadic value.
contramapped :: Contravariant f => Setter (f b) (f a) a b Source #
This Setter can be used to map over all of the inputs to a Contravariant.
contramap≡overcontramapped
>>>getPredicate (over contramapped (*2) (Predicate even)) 5True
>>>getOp (over contramapped (*5) (Op show)) 100"500"
setmapped :: Ord b => Setter (Set a) (Set b) a b Source #
>>>over setmapped (+1) (Set.fromList [1,2,3,4])fromList [2,3,4,5]
isetmapped :: Setter' IntSet Int Source #
>>>over isetmapped (+1) (IntSet.fromList [1,2,3,4])fromList [2,3,4,5]
liftedA :: Applicative f => Setter (f a) (f b) a b Source #
This setter can be used to modify all of the values in an Applicative.
liftA≡setterliftedA
>>>setter liftedA Identity [1,2,3][Identity 1,Identity 2,Identity 3]
>>>set liftedA 2 (Just 1)Just 2
locally :: Setter (ReaderT r2 m a) (ReaderT r1 m a) r1 r2 Source #
Modify the local environment of a Reader.
Use to lift reader actions into a larger environment:
>>>runReader ( ask & locally ..~ fst ) (1,2)1
cond :: (a -> Bool) -> Setter' a a Source #
Apply a function only when the given condition holds.
See also predicated & filtered.
exmapped :: Exception e1 => Exception e2 => Setter s s e1 e2 Source #
Map one exception into another as proposed in the paper "A semantics for imprecise exceptions".
>>>handles (only Overflow) (\_ -> return "caught") $ assert False (return "uncaught") & (exmapped ..~ \ (AssertionFailed _) -> Overflow)"caught"
exmapped :: Exception e => Setter s s SomeException e
Primitive operators
over :: ASetter s t a b -> (a -> b) -> s -> t Source #
Extract a SEC from a Setter.
Used to modify the target of a Lens or all the targets of a Setter
or Traversal.
overoid≡idovero f.overo g ≡overo (f.g)setter.over≡idover.setter≡id
>>>over fmapped (+1) (Just 1)Just 2
>>>over fmapped (*10) [1,2,3][10,20,30]
>>>over t21 (+1) (1,2)(2,2)
>>>over t21 show (10,20)("10",20)
over :: Setter s t a b -> (a -> r) -> s -> r over :: Monoid r => Fold s t a b -> (a -> r) -> s -> r
ixover :: Monoid i => AIxsetter i s t a b -> (i -> a -> b) -> s -> t Source #
>>>ixover (ixat 1) (+) [1,2,3 :: Int][1,3,3]
>>>ixover (ixat 5) (+) [1,2,3 :: Int][1,2,3]
under :: AResetter s t a b -> (a -> b) -> s -> t Source #
Extract a SEC from a Resetter.
underoid≡idundero f.undero g ≡undero (f.g)resetter.under≡idunder.resetter≡id
Note that under (more properly co-over) is distinct from reover:
>>>:t under $ wrapped @(Identity Int)under $ wrapped @(Identity Int) :: (Int -> Int) -> Identity Int -> Identity Int>>>:t over $ wrapped @(Identity Int)over $ wrapped @(Identity Int) :: (Int -> Int) -> Identity Int -> Identity Int>>>:t over . re $ wrapped @(Identity Int)over . re $ wrapped @(Identity Int) :: (Identity Int -> Identity Int) -> Int -> Int>>>:t reover $ wrapped @(Identity Int)reover $ wrapped @(Identity Int) :: (Identity Int -> Identity Int) -> Int -> Int
Compare to the lens-family version.
cxover :: Monoid k => ACxsetter k s t a b -> (k -> a -> b) -> s -> t Source #
>>>cxover (catchOn 42) (\k msg -> show k ++ ": " ++ msg) $ Just "foo"Just "0: foo"
>>>cxover (catchOn 42) (\k msg -> show k ++ ": " ++ msg) NothingNothing
>>>cxover (catchOn 0) (\k msg -> show k ++ ": " ++ msg) NothingJust "caught"
Operators
assignA :: Category p => Strong p => ASetter s t a b -> Optic p s t s b Source #
Run a profunctor arrow command and set the optic targets to the result.
Similar to assign, except that the type of the object being modified can change.
>>>getVal1 = Right 3>>>getVal2 = Right False>>>action = assignA t21 (Kleisli (const getVal1)) >>> assignA t22 (Kleisli (const getVal2))>>>runKleisli action ((), ())Right (3,False)
assignA::Categoryp =>Isos t a b ->Lenslikep s t s bassignA::Categoryp =>Lenss t a b ->Lenslikep s t s bassignA::Categoryp =>Grates t a b ->Lenslikep s t s bassignA::Categoryp =>Setters t a b ->Lenslikep s t s bassignA::Categoryp =>Traversals t a b ->Lenslikep s t s b
cxset :: Monoid k => ACxsetter k s t a b -> (k -> b) -> s -> t Source #
Dual set with index. Equivalent to cxover with the current value ignored.
>>>cxset (catchOn 42) show $ Just "foo"Just "0"
>>>cxset (catchOn 42) show NothingNothing
>>>cxset (catchOn 0) show NothingJust "caught"
(..~) :: ASetter s t a b -> (a -> b) -> s -> t infixr 4 Source #
TODO: Document
>>>Nothing & just ..~ (+1)Nothing
(?~) :: ASetter s t a (Maybe b) -> b -> s -> t infixr 4 Source #
Set the target of a settable optic to Just a value.
l?~t ≡setl (Justt)
>>>Nothing & id ?~ 1Just 1
?~ can be used type-changily:
>>>('a', ('b', 'c')) & t22 . both ?~ 'x'('a',(Just 'x',Just 'x'))
(?~) ::Isos t a (Maybeb) -> b -> s -> t (?~) ::Lenss t a (Maybeb) -> b -> s -> t (?~) ::Grates t a (Maybeb) -> b -> s -> t (?~) ::Setters t a (Maybeb) -> b -> s -> t (?~) ::Traversals t a (Maybeb) -> b -> s -> t
(<>~) :: Semigroup a => ASetter s t a a -> a -> s -> t infixr 4 Source #
Modify the target by adding another value.
>>>both <>~ False $ (False,True)(False,True)
>>>both <>~ "!!!" $ ("hello","world")("hello!!!","world!!!")
(<>~) ::Semigroupa =>Isos t a a -> a -> s -> t (<>~) ::Semigroupa =>Lenss t a a -> a -> s -> t (<>~) ::Semigroupa =>Grates t a a -> a -> s -> t (<>~) ::Semigroupa =>Setters t a a -> a -> s -> t (<>~) ::Semigroupa =>Traversals t a a -> a -> s -> t
(><~) :: Semiring a => ASetter s t a a -> a -> s -> t infixr 4 Source #
Modify the target by multiplying by another value.
>>>both ><~ False $ (False,True)(False,False)
(><~) ::Semiringa =>Isos t a a -> a -> s -> t (><~) ::Semiringa =>Lenss t a a -> a -> s -> t (><~) ::Semiringa =>Grates t a a -> a -> s -> t (><~) ::Semiringa =>Setters t a a -> a -> s -> t (><~) ::Semiringa =>Traversals t a a -> a -> s -> t
Indexed Operators
MonadState
assigns :: MonadState s m => ASetter s s a b -> b -> m () Source #
Replace the target(s) of a settable in a monadic state.
assigns::MonadStates m =>Iso's a -> a -> m ()assigns::MonadStates m =>Lens's a -> a -> m ()assigns::MonadStates m =>Grate's a -> a -> m ()assigns::MonadStates m =>Prism's a -> a -> m ()assigns::MonadStates m =>Setter's a -> a -> m ()assigns::MonadStates m =>Traversal's a -> a -> m ()
modifies :: MonadState s m => ASetter s s a b -> (a -> b) -> m () Source #
Map over the target(s) of a Setter in a monadic state.
modifies::MonadStates m =>Iso's a -> (a -> a) -> m ()modifies::MonadStates m =>Lens's a -> (a -> a) -> m ()modifies::MonadStates m =>Grate's a -> (a -> a) -> m ()modifies::MonadStates m =>Prism's a -> (a -> a) -> m ()modifies::MonadStates m =>Setter's a -> (a -> a) -> m ()modifies::MonadStates m =>Traversal's a -> (a -> a) -> m ()
(.=) :: MonadState s m => ASetter s s a b -> b -> m () infix 4 Source #
Replace the target(s) of a settable in a monadic state.
This is an infix version of assigns.
>>>execState (do t21 .= 1; t22 .= 2) (3,4)(1,2)
>>>execState (both .= 3) (1,2)(3,3)
(.=) ::MonadStates m =>Iso's a -> a -> m () (.=) ::MonadStates m =>Lens's a -> a -> m () (.=) ::MonadStates m =>Grate's a -> a -> m () (.=) ::MonadStates m =>Prism's a -> a -> m () (.=) ::MonadStates m =>Setter's a -> a -> m () (.=) ::MonadStates m =>Traversal's a -> a -> m ()
(..=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m () infix 4 Source #
Map over the target(s) of a Setter in a monadic state.
This is an infix version of modifies.
>>>execState (do just ..= (+1) ) NothingNothing
>>>execState (do t21 ..= (+1) ;t22 ..= (+2)) (1,2)(2,4)
>>>execState (do both ..= (+1)) (1,2)(2,3)
(..=) ::MonadStates m =>Iso's a -> (a -> a) -> m () (..=) ::MonadStates m =>Lens's a -> (a -> a) -> m () (..=) ::MonadStates m =>Grate's a -> (a -> a) -> m () (..=) ::MonadStates m =>Prism's a -> (a -> a) -> m () (..=) ::MonadStates m =>Setter's a -> (a -> a) -> m () (..=) ::MonadStates m =>Traversal's a -> (a -> a) -> m ()
(%=) :: MonadState s m => Monoid i => AIxsetter i s s a b -> (i -> b) -> m () infix 4 Source #
TODO: Document
(%%=) :: MonadState s m => Monoid i => AIxsetter i s s a b -> (i -> a -> b) -> m () infix 4 Source #
TODO: Document
(//=) :: MonadState s m => AResetter s s a b -> (a -> b) -> m () infix 4 Source #
TODO: Document
(#=) :: MonadState s m => Monoid k => ACxsetter k s s a b -> (k -> b) -> m () infix 4 Source #
TODO: Document
(##=) :: MonadState s m => Monoid k => ACxsetter k s s a b -> (k -> a -> b) -> m () infix 4 Source #
TODO: Document
(?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m () infix 4 Source #
Replace the target(s) of a settable optic with Just a new value.
>>>execState (do t21 ?= 1; t22 ?= 2) (Just 1, Nothing)(Just 1,Just 2)
(?=) ::MonadStates m =>Iso's (Maybea) -> a -> m () (?=) ::MonadStates m =>Lens's (Maybea) -> a -> m () (?=) ::MonadStates m =>Grate's (Maybea) -> a -> m () (?=) ::MonadStates m =>Prism's (Maybea) -> a -> m () (?=) ::MonadStates m =>Setter's (Maybea) -> a -> m () (?=) ::MonadStates m =>Traversal's (Maybea) -> a -> m ()
(<>=) :: MonadState s m => Semigroup a => ASetter' s a -> a -> m () infix 4 Source #
Modify the target(s) of a settable optic by adding a value.
>>>execState (both <>= False) (False,True)(False,True)
>>>execState (both <>= "!!!") ("hello","world")("hello!!!","world!!!")
(<>=) ::MonadStates m =>Semigroupa =>Iso's a -> a -> m () (<>=) ::MonadStates m =>Semigroupa =>Lens's a -> a -> m () (<>=) ::MonadStates m =>Semigroupa =>Grate's a -> a -> m () (<>=) ::MonadStates m =>Semigroupa =>Prism's a -> a -> m () (<>=) ::MonadStates m =>Semigroupa =>Setter's a -> a -> m () (<>=) ::MonadStates m =>Semigroupa =>Traversal's a -> a -> m ()
(><=) :: MonadState s m => Semiring a => ASetter' s a -> a -> m () infix 4 Source #
Modify the target(s) of a settable optic by mulitiplying by a value.
>>>execState (both ><= False) (False,True)(False,False)
(><=) ::MonadStates m =>Semiringa =>Iso's a -> a -> m () (><=) ::MonadStates m =>Semiringa =>Lens's a -> a -> m () (><=) ::MonadStates m =>Semiringa =>Grate's a -> a -> m () (><=) ::MonadStates m =>Semiringa =>Prism's a -> a -> m () (><=) ::MonadStates m =>Semiringa =>Setter's a -> a -> m () (><=) ::MonadStates m =>Semiringa =>Traversal's a -> a -> m ()
zoom :: Functor m => Optic' (Star (Compose m ((,) c))) ta a -> StateT a m c -> StateT ta m c Source #
Carriers
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 | |
type AResetter' s a = AResetter s s a a Source #
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 Associated Types type Rep (Traversal0Rep a b) :: Type -> Type # Methods 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.
Methods
cotabulate :: (Corep p d -> c) -> p d c #
Laws:
cotabulate.cosieve≡idcosieve.cotabulate≡id
Instances
| Functor f => Corepresentable (Costar f) | |
| Corepresentable (Tagged :: Type -> Type -> Type) | |
| Corepresentable ((->) :: Type -> Type -> Type) | |
Defined in Data.Profunctor.Rep Methods cotabulate :: (Corep (->) d -> c) -> d -> c # | |
| Functor w => Corepresentable (Cokleisli w) | |
| Corepresentable (GrateRep a b) Source # | |