{-# LANGUAGE TypeOperators, TupleSections #-}
module Data.Label.Failing
( Lens
, Failing
, lens
, get
, modify
, set
, embed
, set'
, modify'
)
where
import Control.Applicative
import Control.Arrow
import Control.Category
import Data.Label.Point (Failing)
import Prelude hiding ((.), id)
import qualified Data.Label.Poly as Poly
{-# INLINE lens #-}
{-# INLINE get #-}
{-# INLINE modify #-}
{-# INLINE set #-}
{-# INLINE embed #-}
{-# INLINE set' #-}
{-# INLINE modify' #-}
type Lens e f o = Poly.Lens (Failing e) f o
lens :: (f -> Either e o)
-> ((o -> Either e i) -> f -> Either e g)
-> Lens e (f -> g) (o -> i)
lens :: forall f e o i g.
(f -> Either e o)
-> ((o -> Either e i) -> f -> Either e g)
-> Lens e (f -> g) (o -> i)
lens f -> Either e o
g (o -> Either e i) -> f -> Either e g
s = forall (cat :: * -> * -> *) f o i g.
cat f o -> cat (cat o i, f) g -> Lens cat (f -> g) (o -> i)
Poly.lens (forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli f -> Either e o
g) (forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\(Failing e o i
m, f
f) -> (o -> Either e i) -> f -> Either e g
s (forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli Failing e o i
m) f
f))
get :: Lens e (f -> g) (o -> i) -> f -> Either e o
get :: forall e f g o i. Lens e (f -> g) (o -> i) -> f -> Either e o
get Lens e (f -> g) (o -> i)
l = forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (forall (cat :: * -> * -> *) f g o i.
Lens cat (f -> g) (o -> i) -> cat f o
Poly.get Lens e (f -> g) (o -> i)
l)
modify :: Lens e (f -> g) (o -> i) -> (o -> i) -> f -> Either e g
modify :: forall e f g o i.
Lens e (f -> g) (o -> i) -> (o -> i) -> f -> Either e g
modify Lens e (f -> g) (o -> i)
l o -> i
m = forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (forall (cat :: * -> * -> *) f g o i.
Lens cat (f -> g) (o -> i) -> cat (cat o i, f) g
Poly.modify Lens e (f -> g) (o -> i)
l forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr o -> i
m,))
set :: Lens e (f -> g) (o -> i) -> i -> f -> Either e g
set :: forall e f g o i. Lens e (f -> g) (o -> i) -> i -> f -> Either e g
set Lens e (f -> g) (o -> i)
l i
v = forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (forall (arr :: * -> * -> *) f g o i.
Arrow arr =>
Lens arr (f -> g) (o -> i) -> arr (i, f) g
Poly.set Lens e (f -> g) (o -> i)
l forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (i
v,))
embed :: Poly.Lens (->) (f -> g) (Either e o -> Either e i) -> Lens e (f -> g) (o -> i)
embed :: forall f g e o i.
Lens (->) (f -> g) (Either e o -> Either e i)
-> Lens e (f -> g) (o -> i)
embed Lens (->) (f -> g) (Either e o -> Either e i)
l = forall f e o i g.
(f -> Either e o)
-> ((o -> Either e i) -> f -> Either e g)
-> Lens e (f -> g) (o -> i)
lens (forall (cat :: * -> * -> *) f g o i.
Lens cat (f -> g) (o -> i) -> cat f o
Poly.get Lens (->) (f -> g) (Either e o -> Either e i)
l) (\o -> Either e i
m f
f -> forall a b. a -> b -> a
const (forall (cat :: * -> * -> *) f g o i.
Lens cat (f -> g) (o -> i) -> cat (cat o i, f) g
Poly.modify Lens (->) (f -> g) (Either e o -> Either e i)
l ((forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= o -> Either e i
m), f
f)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (cat :: * -> * -> *) f g o i.
Lens cat (f -> g) (o -> i) -> cat f o
Poly.get Lens (->) (f -> g) (Either e o -> Either e i)
l f
f)
modify' :: Lens e (f -> f) (o -> o) -> (o -> o) -> f -> f
modify' :: forall e f o. Lens e (f -> f) (o -> o) -> (o -> o) -> f -> f
modify' Lens e (f -> f) (o -> o)
l o -> o
m f
f = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const f
f) forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (forall e f g o i.
Lens e (f -> g) (o -> i) -> (o -> i) -> f -> Either e g
modify Lens e (f -> f) (o -> o)
l o -> o
m f
f)
set' :: Lens e (f -> f) (o -> o) -> o -> f -> f
set' :: forall e f o. Lens e (f -> f) (o -> o) -> o -> f -> f
set' Lens e (f -> f) (o -> o)
l o
v f
f = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const f
f) forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (forall e f g o i. Lens e (f -> g) (o -> i) -> i -> f -> Either e g
set Lens e (f -> f) (o -> o)
l o
v f
f)