@@ -20,10 +20,11 @@ module Data.Generics.Internal.Lens where
20import Control.Applicative (Const(..))
20import Control.Applicative (Const(..))
21import Data.Functor.Identity (Identity(..))
21import Data.Functor.Identity (Identity(..))
22import Data.Monoid (First (..))
22import Data.Monoid (First (..))
23−import Data.Profunctor (Choice(right'), Profunctor(dimap))
23+import Data.Profunctor (Choice(right'), Profunctor(dimap, rmap))
24import Data.Profunctor.Unsafe ((#.), (.#))
24import Data.Profunctor.Unsafe ((#.), (.#))
25import Data.Tagged
25import Data.Tagged
26import GHC.Generics ((:*:)(..), (:+:)(..), Generic(..), M1(..), Rep)
26import GHC.Generics ((:*:)(..), (:+:)(..), Generic(..), M1(..), Rep)
27+import Boggle hiding (Dirty)
28-- | Type alias for lens
29-- | Type alias for lens
29type Lens' s a
30type Lens' s a
@@ -150,9 +151,24 @@ ravel coy f s = inj $ coy (\a -> proj (f a)) s
151-- Pull dimaps to left and rights to right
152-- Pull dimaps to left and rights to right
154+newtype MergeRight p a b = MergeRight (PrismBoggle p a b)
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))
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))
153data PrismBoggle p a b where
167data 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)
155 DMap :: (c -> a) -> (b -> d) -> (p a b) -> PrismBoggle p c d
169 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)
156 Dirty :: p a b -> PrismBoggle p a b
172 Dirty :: p a b -> PrismBoggle p a b
158instance Choice p => Profunctor (PrismBoggle p) where
174instance Choice p => Profunctor (PrismBoggle p) where
@@ -165,9 +181,26 @@ instance Choice p => Choice (PrismBoggle p) where
165 right' (DMap f g pab) = DMap (fmap f) (fmap g) (right' pab)
181 right' (DMap f g pab) = DMap (fmap f) (fmap g) (right' pab)
166 right' (DRight pab) = DRight (right' pab)
182 right' (DRight pab) = DRight (right' pab)
184+assoc :: Either a (Either b c) -> Either (Either a b) c
186+ Left a -> Left (Left a)
187+ Right ebc -> case ebc of
188+ Left b -> Left (Right b)
191+assoc' :: Either (Either a b) c -> Either a (Either b c)
193+ Right c -> Right (Right c)
194+ Left eab -> case eab of
196+ Right b -> Right (Left b)
168projPrism :: p a b -> PrismBoggle p a b
198projPrism :: p a b -> PrismBoggle p a b
169projPrism pab = Dirty pab
199projPrism pab = Dirty pab
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))))))
172injPrism :: Choice p => PrismBoggle p a b -> p a b
205injPrism :: Choice p => PrismBoggle p a b -> p a b
173injPrism (Dirty pab) = pab
206injPrism (Dirty pab) = pab
@@ -177,5 +210,6 @@ injPrism (DRight pab) = right' pab
177prismRavel :: (Choice p, Applicative f)
210prismRavel :: (Choice p, Applicative f)
178 => (PrismBoggle p a (Boggle f b) -> PrismBoggle p s (Boggle f t))
211 => (PrismBoggle p a (Boggle f b) -> PrismBoggle p s (Boggle f t))
179 -> (p a (f b) -> p s (f t))
212 -> (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 #-}