@@ -20,10 +20,11 @@ module Data.Generics.Internal.Lens where
2020import Control.Applicative (Const(..))
2121import Data.Functor.Identity (Identity(..))
2222import Data.Monoid (First (..))
23−import Data.Profunctor (Choice(right'), Profunctor(dimap))
23+import Data.Profunctor (Choice(right'), Profunctor(dimap, rmap))
2424import Data.Profunctor.Unsafe ((#.), (.#))
2525import Data.Tagged
2626import GHC.Generics ((:*:)(..), (:+:)(..), Generic(..), M1(..), Rep)
27+import Boggle hiding (Dirty)
2728
2829-- | Type alias for lens
2930type Lens' s a
@@ -150,9 +151,24 @@ ravel coy f s = inj $ coy (\a -> proj (f a)) s
150151
151152-- Pull dimaps to left and rights to right
152153
154+newtype MergeRight p a b = MergeRight (PrismBoggle p a b)
155+{-
156+instance Choice p => Profunctor (MergeRight p) where
157+ dimap f g (MergeRight (Dirty pab)) = MergeRight (DMap f g pab)
158+ dimap f g (MergeRight (DMap f' g' pab)) = MergeRight (DMap (f' . f) (g . g') pab)
159+ dimap f g (MergeRight (DRight pab)) = MergeRight (DMap f g (right' pab))
160+
161+instance Choice p => Choice (MergeRight p) where
162+ right' (MergeRight (Dirty pab)) = MergeRight (DRight pab)
163+ right' (MergeRight (DMap f g pab)) = MergeRight (DMap (fmap f) (fmap g) (right' pab))
164+ right' (MergeRight (DRight pab)) = MergeRight (DMap assoc' assoc (right' pab))
165+ -}
166+
153167data PrismBoggle p a b where
154− DRight :: (p x y) -> PrismBoggle p (Either z x) (Either z y)
168+ DRight :: Bool -> (p x y) -> PrismBoggle p (Either z x) (Either z y)
155169 DMap :: (c -> a) -> (b -> d) -> (p a b) -> PrismBoggle p c d
170+ --DAssoc :: p (Either (Either a b) c) (Either (Either a b) c) -> PrismBoggle p (Either a (Either b c)) (Either a (Either b c))
171+-- DAssoc :: p (Either a (Either b c)) (Either a (Either b c)) -> PrismBoggle p (Either (Either a b)c) (Either (Either a b) c)
156172 Dirty :: p a b -> PrismBoggle p a b
157173
158174instance Choice p => Profunctor (PrismBoggle p) where
@@ -165,9 +181,26 @@ instance Choice p => Choice (PrismBoggle p) where
165181 right' (DMap f g pab) = DMap (fmap f) (fmap g) (right' pab)
166182 right' (DRight pab) = DRight (right' pab)
167183
184+assoc :: Either a (Either b c) -> Either (Either a b) c
185+assoc e = case e of
186+ Left a -> Left (Left a)
187+ Right ebc -> case ebc of
188+ Left b -> Left (Right b)
189+ Right c -> Right c
190+
191+assoc' :: Either (Either a b) c -> Either a (Either b c)
192+assoc' e = case e of
193+ Right c -> Right (Right c)
194+ Left eab -> case eab of
195+ Left a -> Left a
196+ Right b -> Right (Left b)
197+
168198projPrism :: p a b -> PrismBoggle p a b
169199projPrism pab = Dirty pab
170200
201+--rightFuse :: Choice p => p a b -> p (Either c (Either d a)) (Either c (Either d b))
202+rightFuse x = injPrism (projPrism (injPrism (right' (right' (right' (projPrism x))))))
203+
171204
172205injPrism :: Choice p => PrismBoggle p a b -> p a b
173206injPrism (Dirty pab) = pab
@@ -177,5 +210,6 @@ injPrism (DRight pab) = right' pab
177210prismRavel :: (Choice p, Applicative f)
178211 => (PrismBoggle p a (Boggle f b) -> PrismBoggle p s (Boggle f t))
179212 -> (p a (f b) -> p s (f t))
180−prismRavel coy pafb = (rmap inj (injPrism (coy (projPrism (rmap proj pafb)))))
213+prismRavel coy pafb = injPrism (dimap id lowerBoggle (coy (dimap id liftBoggle (projPrism pafb))))
214+{-# INLINE prismRavel #-}
181215