@@ -1,3 +1,5 @@
1+{-# LANGUAGE FlexibleInstances #-}
2+{-# LANGUAGE MultiParamTypeClasses #-}
13{- |
24Module : Data.Lens.Internal.SemiIso
35Description : Internals of a SemiIso.
@@ -11,6 +13,7 @@ module Control.Lens.Internal.SemiIso where
1113
1214import Control.Monad
1315import Data.Profunctor
16+import Data.Profunctor.Exposed
1417
1518-- | Type used internally to access 'SemiIso'.
1619--
@@ -27,13 +30,40 @@ instance Choice (Barter s t) where
2730 right' (Barter as st) = Barter
2831 (either (\_ -> Left "partial iso failed") as) (fmap Right . st)
2932
30−-- | Provides a profunctor the ability to fail with an error message.
33+-- Proof of 'Exposed' laws:
3134--
32−-- This class could use some laws. It is certainly a bit ad-hoc.
33−class Profunctor p => Failure p where
34− tie :: p a (Either String b) -> p a b
35− attach :: p a b -> p (Either String a) b
36−
37−instance Failure (Barter s t) where
38− tie (Barter f g) = Barter f (join . g)
39− attach (Barter f g) = Barter (>>= f) g
35+-- > merge . rmap return = id
36+--
37+-- merge . rmap return $ Barter l r =
38+-- merge $ Barter l (return . r) =
39+-- Barter l (join . return . r) =
40+-- Barter l r
41+--
42+-- > lmap return . expose = id
43+--
44+-- lmap return . expose $ Barter l r =
45+-- lmap return $ Barter (>>= l) r =
46+-- Barter ((>>= l) . return) r =
47+-- Barter (join . fmap l . return) r =
48+-- Barter (join . return . l) r =
49+-- Barter l r
50+--
51+-- > rmap (>>= f) = merge . rmap (fmap f)
52+--
53+-- rmap (>>= f) $ Barter l r =
54+-- Barter l ((>>= f) . r) =
55+-- Barter l (join . fmap f . r) =
56+-- merge $ Barter l (fmap f . r) =
57+-- merge . rmap (fmap f) $ Barter l r
58+--
59+-- > lmap (fmap f) . expose = expose . lmap f
60+--
61+-- lmap (fmap f) . expose $ Barter l r =
62+-- lmap (fmap f) $ Barter (>>= l) r =
63+-- Barter ((>>= l) . fmap f) r =
64+-- Barter (>>= (l . f)) r =
65+-- expose $ Barter (l . f) r =
66+-- expose . lmap f $ Barter l r
67+instance Exposed (Either String) (Barter s t) where
68+ expose (Barter l r) = Barter (>>= l) r
69+ merge (Barter l r) = Barter l (join . r)