module Control.Lens.Internal.SemiIso where
import Control.Monad
import Data.Profunctor
data Barter s t a b = Barter (a -> Either String s) (t -> Either String b)
instance Profunctor (Barter s t) where
lmap f (Barter l r) = Barter (l . f) r
rmap f (Barter l r) = Barter l (fmap f . r)
instance Choice (Barter s t) where
left' (Barter as st) = Barter
(either as (\_ -> Left "partial iso failed")) (fmap Left . st)
right' (Barter as st) = Barter
(either (\_ -> Left "partial iso failed") as) (fmap Right . st)
class Profunctor p => Failure p where
tie :: p a (Either String b) -> p a b
attach :: p a b -> p (Either String a) b
instance Failure (Barter s t) where
tie (Barter f g) = Barter f (join . g)
attach (Barter f g) = Barter (>>= f) g