@@ -0,0 +1,589 @@
2+{-# LANGUAGE TypeApplications #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+{-# LANGUAGE RankNTypes #-}
5+{-# LANGUAGE DeriveGeneric #-}
6+module Data.Profunctor.Extra where
13+-- Pull dimaps to left and rights to right
15+data Boggle p a b where
16+ DRight :: (p x y) -> Boggle p (Either z x) (Either z y)
17+ DMap :: (c -> a) -> (b -> d) -> (p a b) -> Boggle p c d
18+ Dirty :: p a b -> Boggle p a b
20+instance Choice p => Profunctor (Boggle p) where
21+ dimap f g (Dirty pab) = DMap f g pab
22+ dimap f g (DMap f' g' pab) = DMap (f' . f) (g . g') pab
23+ dimap f g (DRight pab) = DMap f g (right' pab)
25+instance Choice p => Choice (Boggle p) where
26+ right' (Dirty pab) = DRight pab
27+ right' (DMap f g pab) = DMap (fmap f) (fmap g) (right' pab)
28+ right' (DRight pab) = DRight (right' pab)
30+lift :: p a b -> Boggle p a b
34+lower :: Choice p => Boggle p a b -> p a b
35+lower (Dirty pab) = pab
36+lower (DMap f g pab) = dimap f g pab
37+lower (DRight pab) = right' pab
39+test1 :: Choice p => p Int Int -> p (Either z Int) (Either z Int)
40+test1 pab = right' (dimap (+1) (subtract 1) pab)
42+test1spec :: Choice p => p Int Int -> p (Either z Int) (Either z Int)
43+test1spec = lower . test1 . lift
47+------------------------------------------------------------------------------
49+--- This type fuses together uses of `dimap` we want to do this last
50+-- as we first need to rearrange the "Eithers"
51+newtype DimapK p b c = DimapK (forall a d . (Fun a b -> Fun c d -> p a d))
53+liftDimapK :: MProfunctor p => p a b -> DimapK p a b
54+liftDimapK pa = DimapK (\f g -> mdimap f g pa)
56+lowerDimapK :: DimapK p a b -> p a b
57+lowerDimapK (DimapK pa) = pa (liftFun id) (liftFun id)
59+-- Note that here because we use (<.>) we potentially flatten "either"
60+instance MProfunctor (DimapK p) where
61+ mdimap f g (DimapK x) = DimapK (\y z -> x (f <.> y) (z <.> g))
62+ {-# INLINE mdimap #-}
64+fmdimap :: Fun a b -> Fun c d -> DimapK p b c -> p a d
65+fmdimap f g (DimapK x) = x f g
67+------------------------------------------------------------------------------
68+-- Wrapper to ensure that a liftDimapK followed by lowerDimapK doesn't
71+data DimapK1 p b c = DimapK1 (p b c) (DimapK p b c)
72+ | DirtyDimapK1 (p b c)
74+liftDimapK1 :: MProfunctor p => p a b -> DimapK1 p a b
75+liftDimapK1 pab = DimapK1 pab (liftDimapK pab)
77+lowerDimapK1 :: DimapK1 p a b -> p a b
78+lowerDimapK1 (DimapK1 pab _) = pab
79+lowerDimapK1 (DirtyDimapK1 pab) = pab
81+instance MProfunctor p => MProfunctor (DimapK1 p) where
82+ mdimap f g (DimapK1 _ d) = DimapK1 (fmdimap f g d) (mdimap f g d)
83+ mdimap f g (DirtyDimapK1 pab) = DimapK1 (mdimap f g pab) (mdimap f g (liftDimapK pab))
84+ {-# INLINE mdimap #-}
86+--instance MRight p => MRight (DiMapK1 p) where
88+instance MRight p => MRight (DimapK1 p) where
89+ mright (DimapK1 f g) = DirtyDimapK1 (mright f)
90+ mright (DirtyDimapK1 pab) = DirtyDimapK1 (mright pab)
91+ {-# INLINE mright #-}
93+-- Should be the identity, works
94+testDimapK1 :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
95+testDimapK1 pab = lowerDimapK1 (liftDimapK1 pab)
101+-- Should observe one dimap in the specialised version
102+testDimapK1A :: (MProfunctor p) => p Int Int -> p Int Int
103+testDimapK1A pab = mdimap a b (mdimap a b pab)
105+ravelDimapK1 :: (MProfunctor p)
106+ => (DimapK1 p a b -> DimapK1 p c d)
108+ravelDimapK1 l pab = lowerDimapK1 (l (liftDimapK1 pab))
111+specDimapK1A :: (MProfunctor p) => p Int Int -> p Int Int
112+specDimapK1A = ravelDimapK1 testDimapK1A
117+-------------------------------------------------------------------------------
118+-- Pull Dimaps to the left
120+data DimapSyntax p c d where
121+ DMSMap :: Fun c a -> Fun b d -> p a b -> DimapSyntax p c d
122+ DMSDirty :: p a b -> DimapSyntax p a b
124+liftDimapSyntax :: p a b -> DimapSyntax p a b
125+liftDimapSyntax pab = DMSDirty pab
127+lowerDimapSyntax :: MProfunctor p => DimapSyntax p a b -> p a b
128+lowerDimapSyntax (DMSDirty pab) = pab
129+lowerDimapSyntax (DMSMap pca pbd pab) = mdimap pca pbd pab
131+instance MRight p => MRight (DimapSyntax p) where
132+ mright (DMSDirty pab) = DMSDirty (mright pab)
133+ mright (DMSMap fca fbd pab) = DMSMap (femap fca) (femap fbd) (mright pab)
134+ {-# INLINE mright #-}
136+-- Don't fuse here, we do that in DimapK1 at the end
137+instance MProfunctor p => MProfunctor (DimapSyntax p) where
138+ mdimap f g (DMSDirty p) = DMSMap f g p
139+ mdimap f g (DMSMap f' g' p) = DMSMap (f' <.> f) (g <.> g') p
140+ {-# INLINE mdimap #-}
142+f :: Fun Int (Either Int Int)
144+g :: Fun (Either Int Int) Int
148+testDimapSyntax :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
149+testDimapSyntax pab = mdimap f g (mright (mdimap f g (mright pab)))
151+ravelDimapSyntax :: (MProfunctor p, MRight p) =>
152+ (DimapSyntax p a b -> DimapSyntax p s t)
154+ravelDimapSyntax l pab = lowerDimapSyntax (l (liftDimapSyntax pab))
156+ravelDimapSyntaxFF :: (MProfunctor p, MRight p) =>
157+ (DimapSyntax (RightFuse (RightFreeTheorem p)) a b -> DimapSyntax (RightFuse (RightFreeTheorem p)) s t)
159+ravelDimapSyntaxFF l pab = lowerRightFreeTheorem (lowerRightFuse (lowerDimapSyntax (l (liftDimapSyntax (liftRightFuse (liftRightFreeTheorem pab))))))
162+specDimapSyntax :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
163+specDimapSyntax = ravelDimapSyntax testDimapSyntax
166+-------------------------------------------------------------------------------
167+-- Fuse together rights
169+data RightSyntax p c d where
170+ RSRight :: Fun c a -> Fun b d -> p a b -> DimapSyntax p c d
171+ RSDirty :: p a b -> DimapSyntax p a b
173+liftRightSyntax :: p a b -> RightSyntax p a b
174+liftRightSyntax pab = RSDirty pab
176+lowerRightSyntax :: MRight p => RightSyntax p a b -> p a b
177+lowerRightSyntax (RSDirty pab) = pab
178+lowerRightSyntax (RSRight paa) = mright pa
180+instance MRight p => MRight (DimapSyntax p) where
181+ mright (DMSDirty pab) = DMSDirty (mright pab)
182+ mright (DMSMap fca fbd pab) = DMSMap (ffmap fca) (ffmap fbd) (mright pab)
184+-- Don't fuse here, we do that in DimapK1 at the end
185+instance MProfunctor p => MProfunctor (DimapSyntax p) where
186+ mdimap f g (DMSDirty p) = DMSMap f g p
187+ mdimap f g (DMSMap f' g' p) = DMSMap f g (mdimap f' g' p)
189+-- We always want to ensure that "RightFuse p b c ~ p b c" So after
190+-- applying the "RightFuse" constructor when we encounter a right, we need
191+-- to ensure the type is "p (Either z b) (Either z c) as that's the type of
193+data RightFuse p b c where
194+ RightFuse :: (forall a d z . Fun a (Either z b) -> Fun (Either z c) d -> p a d) -> RightFuse p (Either z b) (Either z c)
195+ DirtyRightFuse :: p b c -> RightFuse p b c
197+initRightFuse :: (MRight p, MProfunctor p) => p a b -> RightFuse p (Either z a) (Either z b)
198+initRightFuse pa = RightFuse (\f g -> mdimap f g (mright pa))
200+liftRightFuse :: p a b -> RightFuse p a b
201+liftRightFuse pa = DirtyRightFuse pa
203+lowerRightFuse :: RightFuse p a b -> p a b
204+lowerRightFuse (DirtyRightFuse pa) = pa
205+lowerRightFuse (RightFuse pa) = pa (liftFun id) (liftFun id)
207+instance (MProfunctor p, MRight p) => MRight (RightFuse p) where
208+ -- Both these work but this version maintains the EitherFun invariant
209+ mright (RightFuse r) = RightFuse (\f1 f2 -> r (assocNew <.> f1) (f2 <.> assoc'New))
210+-- mright (RightFuse r) = RightFuse (\f1 f2 -> r (assoc <.> f1) (f2 <.> assoc'))
211+ mright (DirtyRightFuse pab) = initRightFuse pab
212+ {-# INLINE mright #-}
214+-- Once we encounter a dimap we're done. As expressions are already in
215+-- a normal form. | We are here
217+-- dimap . dimap . dimap . <-| right . right . right
218+instance MProfunctor p => MProfunctor (RightFuse p) where
219+ mdimap f g (RightFuse p) = DirtyRightFuse (p f g)
220+-- RightFuse (\aezb ezcd -> p (EitherFun Left (Right . lowerFun f) <.> aezb)
221+-- (ezcd <.> EitherFun Left (Right . lowerFun g)))
223+ -- We apply this adapther after floating all dimaps to the left so that
224+ -- this should never happen
225+ mdimap f g (DirtyRightFuse p) = DirtyRightFuse (mdimap f g p)
226+ {-# INLINE mdimap #-}
228+-- These are assoc and assoc' but with the free theorem applied
229+assocNew = WrappedFun (plus assocg id) <.> assoc
230+assoc'New = EitherFun id (\c -> Right (Right c))
232+assocg = (\eab -> case eab of
234+ Right b -> Right (Left b))
236+assoc :: Fun (Either a (Either b c)) (Either (Either a b) c)
237+assoc = EitherFun (\a -> Left (Left a))
240+ Left b -> Left (Right b)
243+assoc' :: Fun (Either (Either a b) c) (Either a (Either b c))
245+ (\eab -> case eab of
247+ Right b -> Right (Left b))
248+ (\c -> Right (Right c) )
250+-- Want to only have one mright
251+testRightFuse :: (MProfunctor p, MRight p) => p Int Int -> p (Either z2 (Either z (Either z1 Int))) (Either z2 ((Either z (Either z1 Int))))
252+testRightFuse pab = mright (mright (mright pab))
254+ravelRightFuse :: (MProfunctor p, MRight p) =>
255+ (RightFuse p a b -> RightFuse p s t)
257+ravelRightFuse l pab = lowerRightFuse (l (liftRightFuse pab))
260+specRightFuse :: (MProfunctor p, MRight p) => p Int Int -> p (Either z2 (Either z (Either z1 Int))) (Either z2 ((Either z (Either z1 Int))))
261+specRightFuse = ravelRightFuse testRightFuse
263+------------------------------------------------------------------------------
264+-- Then only start after a right
265+-- Don't think this is needed actually
268+data RightFuse1 p b c where
269+ RightFuse1 :: p (Either z b) (Either z c) -> (RightFuse p (Either z b) (Either z c)) -> RightFuse1 p (Either z b) (Either z c)
270+ DirtyRightFuse1 :: (p b c) -> RightFuse1 p b c
272+initRightFuse1 :: (MRight p, MProfunctor p) => p a b -> RightFuse1 p (Either z a) (Either z b)
273+initRightFuse1 pab = RightFuse1 (mright pab) (initRightFuse pab)
275+liftRightFuse1 = DirtyRightFuse1
277+lowerRightFuse1 :: RightFuse1 p a b -> p a b
278+lowerRightFuse1 (RightFuse1 pab _) = pab
279+lowerRightFuse1 (DirtyRightFuse1 pbc) = pbc
281+rfdimap :: Fun c a -> Fun b d -> RightFuse p a b -> p c d
282+rfdimap f g (RightFuse rf) = rf f g
284+-- Once we see a dimap, it's over, remember the normal form invariant
285+instance MProfunctor p => MProfunctor (RightFuse1 p) where
286+ mdimap f g (RightFuse1 _ d) = DirtyRightFuse1 (rfdimap f g d) -- RightFuse1 (rfdimap f g d) (mdimap f g d)
287+ mdimap f g (DirtyRightFuse1 pbc) = DirtyRightFuse1 (mdimap f g pbc)
289+instance (MProfunctor p, MRight p) => MRight (RightFuse1 p) where
290+ mright (DirtyRightFuse1 pbc) = initRightFuse1 pbc
291+ mright (RightFuse1 _ d) = RightFuse1 (mright (lowerRightFuse d)) (mright d)
293+-- Want to only have one mright
294+testRightFuse1 :: (MProfunctor p, MRight p) => p Int Int -> p (Either z (Either z1 Int)) ((Either z (Either z1 Int)))
295+testRightFuse1 pab = mright (mright pab)
297+ravelRightFuse1 :: (MProfunctor p, MRight p) =>
298+ (RightFuse1 p a b -> RightFuse1 p s t)
300+ravelRightFuse1 l pab = lowerRightFuse1 (l (liftRightFuse1 pab))
303+specRightFuse1 :: (MProfunctor p, MRight p) => p Int Int -> p (Either z (Either z1 Int)) ((Either z (Either z1 Int)))
304+specRightFuse1 = ravelRightFuse1 testRightFuse1
306+------------------------------------------------------------------------------
307+-- Now apply a specialised version of the free theorem, do this before
308+-- fusing anything or we risk losing information
309+-- dimap f (either g h) (right p) = dimap (either g f) (either id h) (right p)
311+data RightFreeTheorem p a b where
312+ RFRight :: p a b -> RightFreeTheorem p (Either z a) (Either z b)
313+-- RFDMap :: Fun c a -> Fun b d -> p a b -> RightFreeTheorem p c d
314+ RFDirty :: p a b -> RightFreeTheorem p a b
316+liftRightFreeTheorem :: p a b -> RightFreeTheorem p a b
317+liftRightFreeTheorem = RFDirty
319+lowerRightFreeTheorem :: (MProfunctor p, MRight p) => RightFreeTheorem p a b -> p a b
320+lowerRightFreeTheorem (RFDirty pab) = pab
321+--lowerRightFreeTheorem (RFDMap fa fb pab) = mdimap fa fb pab
322+lowerRightFreeTheorem (RFRight pab) = mright pab
324+instance (MRight p, MProfunctor p) => MProfunctor (RightFreeTheorem p) where
325+ -- We are interested in dimap ... (right so dimap after a dirty is
326+ -- pointless so just delegate
327+ mdimap f g (RFDirty pab) = RFDirty (mdimap f g pab)
328+ -- Actually I we need to do fusion in the same pass otherwise we just do
329+ -- this for dimaps immediately next to a right BUT we do a partial fusion
330+ -- pass first which spots these opporunities
331+ mdimap f (EitherFun g h) (RFRight pab) = RFDirty (mdimap (liftFun (plus g id) <.> f) (EitherFun id h) (mright pab))
332+ mdimap f g (RFRight pab) = RFDirty (mdimap f g (mright pab))
333+ {-# INLINE mdimap #-}
335+instance MRight p => MRight (RightFreeTheorem p) where
336+ mright (RFDirty pab) = RFRight pab
337+ mright (RFRight pab) = RFRight (mright pab)
338+ {-# INLINE mright #-}
341+plus :: (a -> b) -> (c -> d) -> Either a c -> Either b d
342+plus f g (Left x) = Left (f x)
343+plus f g (Right x) = Right (g x)
346+-- Want to only have one mright
347+testRightFreeTheorem :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
348+testRightFreeTheorem pab = mdimap f g (mright (mdimap f g (mright pab)))
350+ f :: Fun Int (Either Int Int)
353+ g :: Fun (Either Int Int) Int
354+ g = EitherFun (const 0) (const 0)
356+ravelRightFreeTheorem :: (MProfunctor p, MRight p) =>
357+ (RightFreeTheorem p a b -> RightFreeTheorem p s t)
359+ravelRightFreeTheorem l pab = lowerRightFreeTheorem (l (liftRightFreeTheorem pab))
362+specRightFreeTheorem :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
363+specRightFreeTheorem = ravelRightFreeTheorem testRightFreeTheorem
365+------------------------------------------------------------------------------
366+-- A partial dimap fusion pass which only fuses functions such that
367+-- we still have an EitherFun in the 2nd argument of dimap.
369+data PartialFusion p a b where
370+ PFDMap :: Fun c a -> Fun b d -> p a b -> PartialFusion p c d
371+ PFDirty :: p a b -> PartialFusion p a b
373+liftPartialFusion :: p a b -> PartialFusion p a b
374+liftPartialFusion = PFDirty
376+lowerPartialFusion :: MProfunctor p => PartialFusion p a b -> p a b
377+lowerPartialFusion (PFDirty pab) = pab
378+lowerPartialFusion (PFDMap fca fbd pcd) = mdimap fca fbd pcd
380+instance (MRight p, MProfunctor p) => MProfunctor (PartialFusion p) where
381+ mdimap f g (PFDirty pab) = PFDMap f g pab
382+ mdimap f g (PFDMap f' (EitherFun l r) pab) = PFDMap (f' <.> f) (EitherFun ((lowerFun g) . l) ((lowerFun g) . r)) pab
383+ mdimap f g (PFDMap f' g' pab) = PFDMap f g (mdimap f' g' pab)
385+instance (MProfunctor p, MRight p) => MRight (PartialFusion p) where
386+ mright pab = PFDirty (mright (lowerPartialFusion pab))
389+-- We want g and g' to fuse but not the innermost dimap so we are left with
391+testPartialFusion :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
392+testPartialFusion pab = (mdimap id' g (mdimap (WrappedFun Left) g' (mdimap id' id' (mright pab))))
396+ g' :: Fun (Either Int Int) Int
397+ g' = EitherFun (const 0) (const 0)
400+ g = WrappedFun (const 2)
403+ravelPartialFusion :: (MProfunctor p, MRight p) =>
404+ (PartialFusion p a b -> PartialFusion p s t)
406+ravelPartialFusion l pab = lowerPartialFusion (l (liftPartialFusion pab))
409+specPartialFusion :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
410+specPartialFusion = ravelPartialFusion testPartialFusion
412+------------------------------------------------------------------------------
413+-- We also lift the function space so that we can rewrite
414+-- dimap f (either g h) (right r) = dimap (either g f) (either id h) (right r)
417+ WrappedFun :: (a -> b) -> Fun a b
418+-- PlusFun :: (x1 -> x2) -> (y1 -> y2) -> Fun (Either x1 y1) (Either x2 y2)
419+ EitherFun :: (x1 -> z) -> (x2 -> z) -> Fun (Either x1 x2) z
421+lowerFun :: Fun a b -> a -> b
422+lowerFun (WrappedFun f) = f
423+lowerFun (EitherFun l r) = either l r
425+liftFun :: (a -> b) -> Fun a b
428+ffmap :: Functor f => Fun a b -> Fun (f a) (f b)
429+ffmap fab = liftFun (fmap (lowerFun fab))
431+ddimap :: MProfunctor p => (c -> a) -> (b -> d) -> p a b -> p c d
432+ddimap f g = mdimap (liftFun f) (liftFun g)
434+femap :: Fun a b -> Fun (Either z a) (Either z b)
435+femap f = EitherFun (Left . id) (Right . lowerFun f)
437+-- Note that (<.>) never destroys an inner EitherFun, that is good
438+-- for the free theorem as it means we are free to fuse dimaps whenever
439+-- we like without fear of destroying this chance. For if we had the chance
440+-- before, we still will afterwards.
441+(<.>) :: Fun b c -> Fun a b -> Fun a c
442+(<.>) cb (EitherFun l r) = EitherFun (lowerFun cb . l) (lowerFun cb . r)
443+(<.>) f g = liftFun (lowerFun f . lowerFun g)
445+(<.) :: Fun b c -> (a -> b) -> Fun a c
446+(<.) f g = f <.> liftFun g
448+(.>) :: (b -> c) -> Fun a b -> Fun a c
449+(.>) f g = liftFun f <.> g
451+-------------------------------------------------------------------------------
453+class MProfunctor p where
454+ mdimap :: Fun c a -> Fun b d -> p a b -> p c d
457+ mright :: p x y -> p (Either z x) (Either z y)
460+-------------------------------------------------------------------------------
461+-- Note [Optimisating Prisms Plan]
462+-- We want to write prisms in a normal form of
463+-- dimap f (EitherFun id g) . right
465+-- To that end, the optimisation plan is
467+-- 1. Float dimaps to the left and rights to the right, fusing together
469+-- 2. Fuse together rights (taking care not to introduce EitherFun with
470+-- non-id first argument
471+-- 3. Apply the right free theorem to replace EitherFun f g with EitherFun
474+-- There are three different adapters for this purpose
478+-- 3. RightFreeTheorem
480+-------------------------------------------------------------------------------
483+-- Run each stage individually
485+stage1, stage2, stage3, stage4, stage5 :: (MProfunctor p, MRight p) => (forall p . (MProfunctor p, MRight p) => p a b) -> p a b
486+stage1 = lowerDimapSyntax
487+stage2 = lowerPartialFusion
488+stage3 = lowerRightFreeTheorem
489+stage4 = lowerRightFuse
490+stage5 = lowerDimapK1
492+ravelStage1, ravelStage2, ravelStage3, ravelStage4, ravelStage5, ravelStage3a
493+ :: (MProfunctor p, MRight p)
494+ => (forall p . (MProfunctor p, MRight p) => p a b -> p s t) ->
497+ravelStage1 = ravelDimapSyntax
498+ravelStage2 = ravelPartialFusion
499+ravelStage3 = ravelRightFreeTheorem
500+ravelStage3a = ravelDimapSyntaxFF
501+ravelStage4 = ravelRightFuse
502+ravelStage5 = ravelDimapK1
504+type FusionStack p a b = DimapSyntax (RightFuse (RightFreeTheorem p)) a b
506+liftFusionStack :: MProfunctor p => p a b -> FusionStack p a b
507+liftFusionStack = liftDimapSyntax . liftRightFuse . liftRightFreeTheorem
509+lowerFusionStack :: (MProfunctor p, MRight p) => FusionStack p a b -> p a b
510+lowerFusionStack = lowerRightFreeTheorem . lowerRightFuse . lowerDimapSyntax
511+{-# INLINE lowerFusionStack #-}
513+ravelFusionStack :: (MProfunctor p, MRight p) =>
514+ (FusionStack p a b -> FusionStack p s t)
516+ravelFusionStack l pab = lowerFusionStack (l (liftFusionStack pab))
517+{-# INLINE ravelFusionStack #-}
520+specFusionStack1 :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
521+specFusionStack1 pab = ravelFusionStack testPartialFusion pab
523+specFusionStack2 :: (MProfunctor p, MRight p) => p Int Int -> p Int Int
524+specFusionStack2 pab = ravelFusionStack testRightFreeTheorem pab
526+-------------------------------------------------------------------------------
527+-- A wrapped profunctor which we can make an instance of our classes.
530+newtype WrappedProfunctor p a b = WrappedProfunctor { wrappedProfunctor :: (p a b) }
532+instance Profunctor p => MProfunctor (WrappedProfunctor p) where
533+ mdimap f g (WrappedProfunctor p) = WrappedProfunctor (dimap (lowerFun f) (lowerFun g) p)
534+ {-# INLINE mdimap #-}
536+instance Choice p => MRight (WrappedProfunctor p) where
537+ mright (WrappedProfunctor p) = WrappedProfunctor (right' p)
538+ {-# INLINE mright #-}
543+instance MoreProfunctor p => MoreProfunctor (MoreBoggle p) where
544+ mdimap f g (MDirty pab) = MDMap f g pab
545+ mdimap f g (MDMap f' g' pab) = MDMap (f' <.> f) (g <.> g') pab
546+ mdimap f (EitherFun l r) (MDRight pab)
547+ = MDMap (liftFun (plus l id) <.> f) (EitherFun id r) (mright pab)
548+ mdimap f g (MDRight pab) = MDMap f g (mright pab)
550+ mright (MDirty pab) = MDRight pab
551+ mright (MDMap f g pab) = MDMap (ffmap f) (ffmap g) (mright pab)
552+ mright (MDRight pab) = MDMap assoc assoc' (mright pab)
556+data WInt = Wrap { unWrap :: !Int }
558+--test :: MoreProfunctor p => p Int Int -> p (Either WInt Int) (Either WInt Int)
559+--test pii = mdimap (PlusFun unWrap id) (liftFun id) (mplusp Wrap (mright pii))
561+test :: MoreProfunctor p => p Int Int -> p WInt WInt
562+test pii = mdimap (liftFun too) (EitherFun Wrap Wrap) (mright pii)
564+ too :: WInt -> Either Int Int
565+ too (Wrap 0) = Left 0
566+ too (Wrap n) = Right n
568+spec1 :: forall p . Choice p => p Int Int -> p WInt WInt
569+spec1 eta = wrappedProfunctor (test (WrappedProfunctor eta))
571+spec :: Choice p => p Int Int -> p WInt WInt
572+spec eta = prismRavel test eta
574+injPrism :: MoreProfunctor p => MoreBoggle p s t -> p s t
575+injPrism (MDRight pa) = mright pa
576+injPrism (MDirty pa) = pa
577+injPrism (MDMap f g pa) = mdimap f g pa
581+prismRavel :: (Choice p)
582+ => (MoreBoggle (WrappedProfunctor p) a b -> MoreBoggle (WrappedProfunctor p) s t)
584+prismRavel coy pafb = wrappedProfunctor (injPrism (coy (projPrism (WrappedProfunctor pafb))))
585+{-# INLINE prismRavel #-}