on master help sign in
Split up Data.Generics.Internal.Lens
commit882b76643253ce7fa38e6b9160d204a44ee74f64 authorCsongor Kiss <kiss.csongor.kiss@gmail.com> dateThu 18 Jan 2018, 19:00 (9 years ago) parent2279d08
30 files changed+698−604
examples/Examples.hs+6−1 generic-lens.cabal+7−1 src/Data/Generics/Internal/Lens.hs−515 src/Data/Generics/Internal/Profunctor/Iso.hs+99 src/Data/Generics/Internal/Profunctor/Lens.hs+175 src/Data/Generics/Internal/Profunctor/Prism.hs+114 src/Data/Generics/Internal/VL/Iso.hs+47 src/Data/Generics/Internal/VL/Lens.hs+51 src/Data/Generics/Internal/VL/Prism.hs+62 src/Data/Generics/Internal/VL/Traversal.hs+74 src/Data/Generics/Product/Any.hs+2−2 src/Data/Generics/Product/Fields.hs+6−5 src/Data/Generics/Product/Internal/Keyed.hs+2−1 src/Data/Generics/Product/Internal/List.hs+3−2 src/Data/Generics/Product/Internal/Positions.hs+2−2 src/Data/Generics/Product/Internal/Subtype.hs+1−1 src/Data/Generics/Product/List.hs+1−1 src/Data/Generics/Product/Positions.hs+4−3 src/Data/Generics/Product/Subtype.hs+5−4 src/Data/Generics/Product/Typed.hs+5−4 src/Data/Generics/Product/Types.hs+1−1 src/Data/Generics/Sum/Any.hs+2−2 src/Data/Generics/Sum/Constructors.hs+4−3 src/Data/Generics/Sum/Internal/Constructors.hs+3−1 src/Data/Generics/Sum/Internal/Subtype.hs+2−1 src/Data/Generics/Sum/Internal/Typed.hs+2−1 src/Data/Generics/Sum/Subtype.hs+7−6 src/Data/Generics/Sum/Typed.hs+6−12 stack.yaml+2−2 test/Spec.hs+3−33
examples/Examples.hs+6−1
@@ -17,11 +17,16 @@
1717module Examples where
1818
1919import Data.Function ((&))
20−import Data.Generics.Internal.Lens
20+import Data.Generics.Internal.VL.Lens
2121import Data.Generics.Product
2222import Data.Generics.Sum
2323import GHC.Generics
2424import Data.Generics.Labels
25+import Data.Generics.Internal.VL.Iso
26+import Data.Generics.Internal.VL.Prism
27+import Data.Generics.Internal.Profunctor.Lens
28+import Data.Generics.Internal.Profunctor.Iso
29+import Data.Generics.Internal.Profunctor.Prism
2530
2631data Animal = Animal
2732 { name :: String
generic-lens.cabal+7−1
@@ -37,7 +37,13 @@ library
3737 , Data.Generics.Sum.Constructors
3838 , Data.Generics.Sum.Typed
3939 , Data.Generics.Sum.Subtype
40− , Data.Generics.Internal.Lens
40+ , Data.Generics.Internal.Profunctor.Lens
41+ , Data.Generics.Internal.Profunctor.Prism
42+ , Data.Generics.Internal.Profunctor.Iso
43+ , Data.Generics.Internal.VL.Lens
44+ , Data.Generics.Internal.VL.Traversal
45+ , Data.Generics.Internal.VL.Prism
46+ , Data.Generics.Internal.VL.Iso
4147
4248 other-modules: Data.Generics.Internal.Families
4349 , Data.Generics.Internal.Families.Changing
src/Data/Generics/Internal/Lens.hsdeleted−515
@@ -1,515 +0,0 @@
1−{-# LANGUAGE GADTs #-}
2−{-# LANGUAGE LambdaCase #-}
3−{-# LANGUAGE Rank2Types #-}
4−{-# LANGUAGE TypeFamilies #-}
5−{-# LANGUAGE TypeFamilyDependencies #-}
6−{-# LANGUAGE TypeOperators #-}
7−{-# LANGUAGE TupleSections #-}
8−{-# LANGUAGE NoMonomorphismRestriction #-}
9−{-# LANGUAGE ScopedTypeVariables #-}
10−
11−-----------------------------------------------------------------------------
12−-- |
13−-- Module : Data.Generics.Internal.Lens
14−-- Copyright : (C) 2017 Csongor Kiss
15−-- License : BSD3
16−-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17−-- Stability : experimental
18−-- Portability : non-portable
19−--
20−-- Internal lens helpers. Only exported for Haddock
21−--
22−-----------------------------------------------------------------------------
23−module Data.Generics.Internal.Lens where
24−
25−import Control.Applicative (Const(..))
26−import Data.Functor.Identity (Identity(..))
27−import Data.Monoid (First (..))
28−import Data.Profunctor (Choice(..), Profunctor(..), Strong(..))
29−import Data.Bifunctor
30−import Data.Profunctor.Unsafe ((#.), (.#))
31−import Data.Tagged
32−import GHC.Generics ((:*:)(..), (:+:)(..), Generic(..), M1(..), K1(..), Rep)
33−import Data.Coerce
34−
35−-- | Type alias for lens
36−type LensVL' s a
37− = Lens s s a a
38−
39−type LensVL s t a b
40− = forall f. Functor f => (a -> f b) -> s -> f t
41−
42−type Lens s t a b
43− = forall p . (Strong p) => p a b -> p s t
44−
45−-- | Type alias for traversal
46−type Traversal' s a
47− = forall f. Applicative f => (a -> f a) -> s -> f s
48−
49−type Traversal s t a b
50− = forall f. Applicative f => (a -> f b) -> s -> f t
51−
52−type LensLike p s t a b
53− = p a b -> p s t
54−
55−-- | Type alias for prism
56−type PrismVL s t a b
57− = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
58−
59−type Prism s t a b
60− = forall p . (Choice p) => p a b -> p s t
61−
62−type Prism' s a = forall p . (Choice p) => p a a -> p s s
63−
64−type PrismVL' s a
65− = PrismVL s s a a
66−
67−type IsoVL' s a
68− = forall p f. (Profunctor p, Functor f) => p a (f a) -> p s (f s)
69−
70−type IsoVL s t a b
71− = forall p f. (Profunctor p, Functor f) => p a (f b) -> p s (f t)
72−
73−type Iso s t a b
74− = forall p. (Profunctor p) => p a b -> p s t
75−
76−type Iso' s a = Iso s s a a
77−
78−-- | Getting
79−(^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
80−s ^. l = getConst (l Const s)
81−infixl 8 ^.
82−
83−-- | Setting
84−set :: ((a -> b) -> s -> t) -> (s, b) -> t
85−set f (s, b)
86− = f (const b) s
87−
88−viewVL :: ((a -> Const a a) -> s -> Const a s) -> s -> a
89−viewVL l s = (^.) s l
90−
91−viewVL' :: ((a -> Const a a) -> s -> Const a s) -> s -> a
92−viewVL' l s = (^.) s l
93−
94−setVL :: LensVL s t a b -> b -> s -> t
95−setVL l x = runIdentity . (l (Identity . const x))
96−
97−
98−view :: Lens s s a a -> s -> a
99−view l = withLens l (\get _ -> get)
100−
101−withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
102−withLens l k =
103− case l idLens of
104− ALens get set -> k get set
105−
106−idLens :: ALens a b a b
107−idLens = ALens id snd
108−
109−infixr 4 .~
110−(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
111−(.~) f b s = set f (s, b)
112−
113−infixl 8 ^?
114−(^?) :: s -> ((a -> Const (First a) a) -> s -> Const (First a) s) -> Maybe a
115−s ^? l = getFirst (fmof l (First #. Just) s)
116− where fmof l' f = getConst #. l' (Const #. f)
117−
118−buildVL :: (Tagged b (Identity b) -> Tagged t (Identity t)) -> b -> t
119−buildVL p = runIdentity #. unTagged #. p .# Tagged .# Identity
120−
121−matchVL :: PrismVL s t a b -> s -> Either t a
122−matchVL k = withPrismVL k $ \_ match -> match
123−
124−
125−
126−build :: (Tagged b b -> Tagged t t) -> b -> t
127−build p = unTagged . p . Tagged
128−
129−
130−-- | Lens focusing on the first element of a product
131−first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
132−first
133− = lens (\(a :*: b) -> a) (\((a :*: b), a') -> a' :*: b)
134−
135−-- | Lens focusing on the second element of a product
136−second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
137−second
138− = lens (\(a :*: b) -> b) (\((a :*: b), b') -> a :*: b')
139−
140−left :: Prism ((a :+: c) x) ((b :+: c) x) (a x) (b x)
141−left = prism L1 $ gsum Right (Left . R1)
142−
143−right :: Prism ((a :+: b) x) ((a :+: c) x) (b x) (c x)
144−right = prism R1 $ gsum (Left . L1) Right
145−
146−gsum :: (a x -> c) -> (b x -> c) -> ((a :+: b) x) -> c
147−gsum f _ (L1 x) = f x
148−gsum _ g (R1 y) = g y
149−
150−prismVL :: (b -> t) -> (s -> Either t a) -> PrismVL s t a b
151−prismVL bt seta eta = dimap (\x -> plus pure id (seta x)) (either id (\x -> fmap bt x)) (right' eta)
152−{-# INLINE prismVL #-}
153−
154−prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
155−prism bt seta eta = dimap seta (either id bt) (right' eta)
156−
157−plus :: (a -> b) -> (c -> d) -> Either a c -> Either b d
158−plus f _ (Left x) = Left (f x)
159−plus _ g (Right y) = Right (g y)
160−
161−fork :: (a -> b) -> (a -> c) -> a -> (b, c)
162−fork f g a = (f a, g a)
163−
164−swap :: (a, b) -> (b, a)
165−swap (a, b) = (b, a)
166−
167−cross :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
168−cross = bimap
169−
170−-- | A type and its generic representation are isomorphic
171−repIsoVL :: (Generic a, Generic b) => IsoVL a b (Rep a x) (Rep b x)
172−repIsoVL = isoVL from to
173−
174−repLens :: (Generic a, Generic b) => Lens a b (Rep a x) (Rep b x)
175−repLens f = repIso f
176−
177−-- | A type and its generic representation are isomorphic
178−repIso :: (Generic a, Generic b) => Iso a b (Rep a x) (Rep b x)
179−repIso = iso from to
180−
181−
182−
183−-- | 'M1' is just a wrapper around `f p`
184−mIsoVL :: IsoVL (M1 i c f p) (M1 i c g p) (f p) (g p)
185−mIsoVL = isoVL unM1 M1
186−
187−-- | 'M1' is just a wrapper around `f p`
188−--mIso :: Iso' (M1 i c f p) (f p)
189−mIso :: Iso (M1 i c f p) (M1 i c g p) (f p) (g p)
190−mIso = iso unM1 M1
191−
192−-- These are specialised versions of the Isos above. On GHC 8.0.2, having
193−-- these functions eta-expanded allows the optimiser to inline these functions.
194−mLens :: Lens (M1 i c f p) (M1 i c g p) (f p) (g p)
195−mLens f = mIso f
196−
197−kIso :: Iso (K1 r a p) (K1 r b p) a b
198−kIso = iso unK1 K1
199−
200−sumIso :: Iso ((a :+: b) x) ((a' :+: b') x) (Either (a x) (b x)) (Either (a' x) (b' x))
201−sumIso = iso back forth
202− where forth (Left l) = (L1 l)
203− forth (Right r) = (R1 r)
204− back (L1 l) = (Left l)
205− back (R1 r) = (Right r)
206−
207−_Left :: Prism (Either a c) (Either b c) a b
208−_Left = prism Left $ either Right (Left . Right)
209−
210−_Right :: Prism (Either c a) (Either c b) a b
211−_Right = prism Right $ either (Left . Left) Right
212−
213−--------------------------------------------------------------------------------
214−
215−data Coyoneda f b = forall a. Coyoneda (a -> b) (f a)
216−
217−instance Functor (Coyoneda f) where
218− fmap f (Coyoneda g fa)
219− = Coyoneda (f . g) fa
220−
221−inj :: Functor f => Coyoneda f a -> f a
222−inj (Coyoneda f a) = fmap f a
223−
224−proj :: Functor f => f a -> Coyoneda f a
225−proj fa = Coyoneda id fa
226−
227−lens2lensvl :: ALens a b s t -> LensVL s t a b
228−lens2lensvl (ALens get set) = lensVL get set
229−
230−ravel :: (ALens a b a b -> ALens a b s t)
231− -> LensVL s t a b
232−ravel l pab = (lens2lensvl $ l idLens) pab
233−
234−prismPRavel ::
235− (Market a b a b -> Market a b s t)
236− -> Prism s t a b
237−prismPRavel l pab = (prism2prismp $ l idPrism) pab
238−
239−prismVLRavel ::
240− (Market a b a b -> Market a b s t)
241− -> PrismVL s t a b
242−prismVLRavel l pab = (prism2prismvl $ l idPrism) pab
243−
244−newtype Alongside p s t a b = Alongside { getAlongside :: p (s, a) (t, b) }
245−
246−instance Profunctor p => Profunctor (Alongside p c d) where
247− dimap f g (Alongside pab) = Alongside $ dimap (fmap f) (fmap g) pab
248−
249−instance Strong p => Strong (Alongside p c d) where
250− second' (Alongside pab) = Alongside . dimap shuffle shuffle . second' $ pab
251− where
252− shuffle (x,(y,z)) = (y,(x,z))
253−
254−(??) :: Functor f => f (a -> b) -> a -> f b
255−fab ?? a = fmap ($ a) fab
256−
257−-- Could implement this using primitives?
258−alongside :: Profunctor p =>
259− LensLike (Alongside p s' t') s t a b
260− -> LensLike (Alongside p a b) s' t' a' b'
261− -> LensLike p (s, s') (t, t') (a, a') (b, b')
262−alongside l1 l2
263− = dimap swap swap . getAlongside . l1 . Alongside . dimap swap swap . getAlongside . l2 . Alongside
264−
265−prodIsoVL :: IsoVL ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
266−prodIsoVL = isoVL (\(a :*: b) -> (a, b)) (\(a, b) -> (a :*: b))
267−
268−prodIso :: Iso ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
269−prodIso = iso (\(a :*: b) -> (a, b)) (\(a, b) -> (a :*: b))
270−
271−prodL :: Lens ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
272−prodL f = prodIso f
273−
274−prodR :: Lens (a' x, b' x) (a x, b x) ((a' :*: b') x) ((a :*: b) x)
275−prodR f = fromIso prodIso f
276−
277−assoc3 :: Iso ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
278−assoc3 = iso (\((a, b), c) -> (a, (b, c))) (\(a, (b, c)) -> ((a, b), c))
279−
280−assoc3L :: Lens ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
281−assoc3L f = assoc3 f
282−
283−assoc3R :: Lens (a', (b', c')) (a, (b, c)) ((a', b'), c') ((a, b), c)
284−assoc3R f = fromIso assoc3 f
285−
286−stron :: (Either s s', b) -> Either (s, b) (s', b)
287−stron (e, b) = bimap (,b) (, b) e
288−
289−choosing :: forall s t a b s' t' . Lens s t a b -> Lens s' t' a b -> Lens (Either s s') (Either t t') a b
290−choosing l r = withLens l (\getl setl ->
291− withLens r (\getr setr ->
292− let g :: Either s s' -> a
293− g = either getl getr
294− s = bimap setl setr . stron
295− in lens g s))
296−
297−lens :: (s -> a) -> ((s,b) -> t) -> Lens s t a b
298−lens get set = dimap (fork id get) set . second'
299−
300−lensVL :: (s -> a) -> ((s, b) -> t) -> LensVL s t a b
301−lensVL get set f x = curry set x <$> f (get x)
302−
303−--------------------------------------------------------------------------------
304−-- Iso stuff
305−
306−data Exchange a b s t = Exchange (s -> a) (b -> t)
307−
308−instance Functor (Exchange a b s) where
309− fmap f (Exchange sa bt) = Exchange sa (f . bt)
310− {-# INLINE fmap #-}
311−
312−instance Profunctor (Exchange a b) where
313− dimap f g (Exchange sa bt) = Exchange (sa . f) (g . bt)
314− {-# INLINE dimap #-}
315− lmap f (Exchange sa bt) = Exchange (sa . f) bt
316− {-# INLINE lmap #-}
317− rmap f (Exchange sa bt) = Exchange sa (f . bt)
318− {-# INLINE rmap #-}
319− ( #. ) _ = coerce
320− {-# INLINE ( #. ) #-}
321− ( .# ) p _ = coerce p
322− {-# INLINE ( .# ) #-}
323−
324−fromIso :: Iso s t a b -> Iso b a t s
325−fromIso l = withIso l $ \ sa bt -> iso bt sa
326−{-# INLINE fromIso #-}
327−
328−isoVL :: (s -> a) -> (b -> t) -> IsoVL s t a b
329−isoVL sa bt = dimap sa (fmap bt)
330−{-# INLINE iso #-}
331−
332−iso :: (s -> a) -> (b -> t) -> Iso s t a b
333−iso = dimap
334−
335−withIso :: Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
336−withIso ai k = case ai (Exchange id id) of
337− Exchange sa bt -> k sa bt
338−
339−pairing :: Iso s t a b -> Iso s' t' a' b' -> Iso (s, s') (t, t') (a, a') (b, b')
340−pairing f g = withIso f $ \ sa bt -> withIso g $ \s'a' b't' ->
341− iso (bmap sa s'a') (bmap bt b't')
342− where bmap f' g' (a, b) = (f' a, g' b)
343−
344−--------------------------------------------------------------------------------
345−-- Prism stuff
346−
347−type APrism s t a b = Market a b a b -> Market a b s t
348−type APrismVL s t a b = Market a b a (Identity b) -> Market a b s (Identity t)
349−
350−-- without :: APrism s t a b
351−-- -> APrism u v c d
352−-- -> Prism (Either s u) (Either t v) (Either a c) (Either b d)
353−-- without k =
354−-- withPrism k $ \bt seta k' ->
355−-- withPrism k' $ \dv uevc ->
356−-- prism (bimapE bt dv) $ \su ->
357−-- case su of
358−-- Left s -> bimapE Left Left (seta s)
359−-- Right u -> bimapE Right Right (uevc u)
360−-- where bimapE :: (a -> b) -> (c -> d) -> Either a c -> Either b d
361−-- bimapE f _ (Left a) = Left (f a)
362−-- bimapE _ g (Right a) = Right (g a)
363−-- {-# INLINE bimapE #-}
364−
365−without' :: Prism s t a b -> Prism s t c d -> Prism s t (Either a c) (Either b d)
366−without' k =
367− withPrism k $ \bt _ k' ->
368− withPrism k' $ \dt setc ->
369− prism (foldEither bt dt) $ \s -> fmap Right (setc s)
370− where foldEither _ g (Right r) = g r
371− foldEither f _ (Left l) = f l
372−
373−withPrism :: APrism s t a b -> ((b -> t) -> (s -> Either t a) -> r) -> r
374−withPrism k f = case k idPrism of
375− Market bt seta -> f bt seta
376−
377−withPrismVL :: APrismVL s t a b -> ((b -> t) -> (s -> Either t a) -> r) -> r
378−withPrismVL k f = case coerce (k (Market Identity Right)) of
379− Market bt seta -> f bt seta
380−
381−--------------------------------------------------------------------------------
382−-- Market
383−
384−data Market a b s t = Market (b -> t) (s -> Either t a)
385−
386−instance Functor (Market a b s) where
387− fmap f (Market bt seta) = Market (f . bt) (either (Left . f) Right . seta)
388− {-# INLINE fmap #-}
389−
390−instance Profunctor (Market a b) where
391− dimap f g (Market bt seta) = Market (g . bt) (either (Left . g) Right . seta . f)
392− {-# INLINE dimap #-}
393− lmap f (Market bt seta) = Market bt (seta . f)
394− {-# INLINE lmap #-}
395− rmap f (Market bt seta) = Market (f . bt) (either (Left . f) Right . seta)
396− {-# INLINE rmap #-}
397− ( #. ) _ = coerce
398− {-# INLINE ( #. ) #-}
399− ( .# ) p _ = coerce p
400− {-# INLINE ( .# ) #-}
401−
402−instance Choice (Market a b) where
403− left' (Market bt seta) = Market (Left . bt) $ \sc -> case sc of
404− Left s -> case seta s of
405− Left t -> Left (Left t)
406− Right a -> Right a
407− Right c -> Left (Right c)
408− {-# INLINE left' #-}
409−
410−prism2prismp :: Market a b s t -> Prism s t a b
411−prism2prismp (Market bt seta) = prism bt seta
412−
413−prism2prismvl :: Market a b s t -> PrismVL s t a b
414−prism2prismvl (Market bt seta) = prismVL bt seta
415−{-# INLINE prism2prismvl #-}
416−
417−idPrism :: Market a b a b
418−idPrism = Market id Right
419−
420−------------------------------------------------------------------------------
421−
422−data ALens a b s t = ALens (s -> a) ((s, b) -> t)
423−
424−instance Functor (ALens a b s) where
425− fmap f (ALens get set) = ALens get (f . set)
426−
427−instance Profunctor (ALens a b) where
428− dimap f g (ALens get set) = ALens (get . f) (g . set . cross f id)
429−
430−instance Strong (ALens a b) where
431− second' (ALens get set) = ALens (get . snd) (bimap id set . assoc)
432− where
433− assoc ((a, b), c) = (a, (b, c))
434−
435−newtype Curried f a =
436− Curried { runCurried :: forall r. f (a -> r) -> f r }
437−
438−instance Functor f => Functor (Curried f) where
439− fmap f (Curried g) = Curried (g . fmap (.f))
440− {-# INLINE fmap #-}
441−
442−instance (Functor f) => Applicative (Curried f) where
443− pure a = Curried (fmap ($ a))
444− {-# INLINE pure #-}
445− Curried mf <*> Curried ma = Curried (ma . mf . fmap (.))
446− {-# INLINE (<*>) #-}
447−
448−-- | The natural isomorphism between @f@ and @Curried f f@.
449−-- @
450−-- 'lowerCurried' '.' 'liftCurried' ≡ 'id'
451−-- 'liftCurried' '.' 'lowerCurried' ≡ 'id'
452−-- @
453−--
454−-- @
455−-- 'lowerCurried' ('liftCurried' x) -- definition
456−-- 'lowerCurried' ('Curried' ('<*>' x)) -- definition
457−-- ('<*>' x) ('pure' 'id') -- beta reduction
458−-- 'pure' 'id' '<*>' x -- Applicative identity law
459−-- x
460−-- @
461−liftCurried :: Applicative f => f a -> Curried f a
462−liftCurried fa = Curried (<*> fa)
463−
464−-- | Lower 'Curried' by applying 'pure' 'id' to the continuation.
465−--
466−-- See 'liftCurried'.
467−lowerCurried :: Applicative f => Curried f a -> f a
468−lowerCurried (Curried f) = f (pure id)
469−
470−confusing :: Applicative f => Traversal s t a b -> (a -> f b) -> s -> f t
471−confusing t = \f -> lowerYoneda . lowerCurried . t (liftCurriedYoneda . f)
472− where
473− liftCurriedYoneda :: Applicative f => f a -> Curried (Yoneda f) a
474− liftCurriedYoneda fa = Curried (`yap` fa)
475− {-# INLINE liftCurriedYoneda #-}
476−
477− yap :: Applicative f => Yoneda f (a -> b) -> f a -> Yoneda f b
478− yap (Yoneda k) fa = Yoneda (\ab_r -> k (ab_r .) <*> fa)
479− {-# INLINE yap #-}
480−{-# INLINE confusing #-}
481−
482−-- | @Yoneda f a@ can be viewed as the partial application of 'fmap' to its second argument.
483−newtype Yoneda f a = Yoneda { runYoneda :: forall b. (a -> b) -> f b }
484−
485−-- | The natural isomorphism between @f@ and @'Yoneda' f@ given by the Yoneda lemma
486−-- is witnessed by 'liftYoneda' and 'lowerYoneda'
487−--
488−-- @
489−-- 'liftYoneda' . 'lowerYoneda' ≡ 'id'
490−-- 'lowerYoneda' . 'liftYoneda' ≡ 'id'
491−-- @
492−--
493−-- @
494−-- lowerYoneda (liftYoneda fa) = -- definition
495−-- lowerYoneda (Yoneda (\f -> fmap f a)) -- definition
496−-- (\f -> fmap f fa) id -- beta reduction
497−-- fmap id fa -- functor law
498−-- fa
499−-- @
500−--
501−-- @
502−-- 'lift' = 'liftYoneda'
503−-- @
504−liftYoneda :: Functor f => f a -> Yoneda f a
505−liftYoneda a = Yoneda (\f -> fmap f a)
506−
507−lowerYoneda :: Yoneda f a -> f a
508−lowerYoneda (Yoneda f) = f id
509−
510−instance Functor (Yoneda f) where
511− fmap f m = Yoneda (\k -> runYoneda m (k . f))
512−
513−instance Applicative f => Applicative (Yoneda f) where
514− pure a = Yoneda (\f -> pure (f a))
515− Yoneda m <*> Yoneda n = Yoneda (\f -> m (f .) <*> n id)
src/Data/Generics/Internal/Profunctor/Iso.hsnew file+99
@@ -0,0 +1,99 @@
1+{-# LANGUAGE GADTs #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE NoMonomorphismRestriction #-}
4+{-# LANGUAGE Rank2Types #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TupleSections #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeFamilyDependencies #-}
9+{-# LANGUAGE TypeOperators #-}
10+
11+-----------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Internal.Profunctor.Iso
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- License : BSD3
16+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Internal lens helpers. Only exported for Haddock
21+--
22+-----------------------------------------------------------------------------
23+module Data.Generics.Internal.Profunctor.Iso where
24+
25+import Data.Profunctor (Profunctor(..))
26+import Data.Profunctor.Unsafe ((#.), (.#))
27+import GHC.Generics ((:*:)(..), (:+:)(..), Generic(..), M1(..), K1(..), Rep)
28+import Data.Coerce
29+
30+type Iso s t a b
31+ = forall p. (Profunctor p) => p a b -> p s t
32+
33+type Iso' s a = Iso s s a a
34+
35+-- | A type and its generic representation are isomorphic
36+repIso :: (Generic a, Generic b) => Iso a b (Rep a x) (Rep b x)
37+repIso = iso from to
38+
39+-- | 'M1' is just a wrapper around `f p`
40+--mIso :: Iso' (M1 i c f p) (f p)
41+mIso :: Iso (M1 i c f p) (M1 i c g p) (f p) (g p)
42+mIso = iso unM1 M1
43+{-# INLINE mIso #-}
44+
45+kIso :: Iso (K1 r a p) (K1 r b p) a b
46+kIso = iso unK1 K1
47+{-# INLINE kIso #-}
48+
49+sumIso :: Iso ((a :+: b) x) ((a' :+: b') x) (Either (a x) (b x)) (Either (a' x) (b' x))
50+sumIso = iso back forth
51+ where forth (Left l) = (L1 l)
52+ forth (Right r) = (R1 r)
53+ back (L1 l) = (Left l)
54+ back (R1 r) = (Right r)
55+
56+prodIso :: Iso ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
57+prodIso = iso (\(a :*: b) -> (a, b)) (\(a, b) -> (a :*: b))
58+
59+assoc3 :: Iso ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
60+assoc3 = iso (\((a, b), c) -> (a, (b, c))) (\(a, (b, c)) -> ((a, b), c))
61+
62+--------------------------------------------------------------------------------
63+-- Iso stuff
64+
65+fromIso :: Iso s t a b -> Iso b a t s
66+fromIso l = withIso l $ \ sa bt -> iso bt sa
67+{-# INLINE fromIso #-}
68+
69+iso :: (s -> a) -> (b -> t) -> Iso s t a b
70+iso = dimap
71+{-# INLINE iso #-}
72+
73+withIso :: Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
74+withIso ai k = case ai (Exchange id id) of
75+ Exchange sa bt -> k sa bt
76+
77+pairing :: Iso s t a b -> Iso s' t' a' b' -> Iso (s, s') (t, t') (a, a') (b, b')
78+pairing f g = withIso f $ \ sa bt -> withIso g $ \s'a' b't' ->
79+ iso (bmap sa s'a') (bmap bt b't')
80+ where bmap f' g' (a, b) = (f' a, g' b)
81+
82+data Exchange a b s t = Exchange (s -> a) (b -> t)
83+
84+instance Functor (Exchange a b s) where
85+ fmap f (Exchange sa bt) = Exchange sa (f . bt)
86+ {-# INLINE fmap #-}
87+
88+instance Profunctor (Exchange a b) where
89+ dimap f g (Exchange sa bt) = Exchange (sa . f) (g . bt)
90+ {-# INLINE dimap #-}
91+ lmap f (Exchange sa bt) = Exchange (sa . f) bt
92+ {-# INLINE lmap #-}
93+ rmap f (Exchange sa bt) = Exchange sa (f . bt)
94+ {-# INLINE rmap #-}
95+ ( #. ) _ = coerce
96+ {-# INLINE ( #. ) #-}
97+ ( .# ) p _ = coerce p
98+ {-# INLINE ( .# ) #-}
99+
src/Data/Generics/Internal/Profunctor/Lens.hsnew file+175
@@ -0,0 +1,175 @@
1+{-# LANGUAGE GADTs #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE NoMonomorphismRestriction #-}
4+{-# LANGUAGE Rank2Types #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TupleSections #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeFamilyDependencies #-}
9+{-# LANGUAGE TypeOperators #-}
10+
11+-----------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Internal.Profunctor.Lens
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- License : BSD3
16+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Internal lens helpers. Only exported for Haddock
21+--
22+-----------------------------------------------------------------------------
23+module Data.Generics.Internal.Profunctor.Lens where
24+
25+import Control.Applicative (Const(..))
26+import Data.Monoid (First (..))
27+import Data.Profunctor (Profunctor(..), Strong(..))
28+import Data.Bifunctor
29+import Data.Profunctor.Unsafe ((#.))
30+import Data.Tagged
31+import GHC.Generics
32+import Data.Generics.Internal.Profunctor.Iso
33+
34+type Lens s t a b
35+ = forall p . (Strong p) => p a b -> p s t
36+
37+type LensLike p s t a b
38+ = p a b -> p s t
39+
40+-- | Getting
41+(^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
42+s ^. l = getConst (l Const s)
43+infixl 8 ^.
44+
45+-- | Setting
46+set :: ((a -> b) -> s -> t) -> (s, b) -> t
47+set f (s, b)
48+ = f (const b) s
49+
50+view :: Lens s s a a -> s -> a
51+view l = withLens l (\get _ -> get)
52+
53+withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
54+withLens l k =
55+ case l idLens of
56+ ALens _get _set -> k _get _set
57+
58+idLens :: ALens a b a b
59+idLens = ALens id snd
60+
61+infixr 4 .~
62+(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
63+(.~) f b s = set f (s, b)
64+
65+infixl 8 ^?
66+(^?) :: s -> ((a -> Const (First a) a) -> s -> Const (First a) s) -> Maybe a
67+s ^? l = getFirst (fmof l (First #. Just) s)
68+ where fmof l' f = getConst #. l' (Const #. f)
69+
70+build :: (Tagged b b -> Tagged t t) -> b -> t
71+build p = unTagged . p . Tagged
72+
73+
74+-- | Lens focusing on the first element of a product
75+first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
76+first
77+ = lens (\(a :*: _) -> a) (\((_ :*: b), a') -> a' :*: b)
78+
79+-- | Lens focusing on the second element of a product
80+second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
81+second
82+ = lens (\(_ :*: b) -> b) (\((a :*: _), b') -> a :*: b')
83+
84+fork :: (a -> b) -> (a -> c) -> a -> (b, c)
85+fork f g a = (f a, g a)
86+
87+swap :: (a, b) -> (b, a)
88+swap (a, b) = (b, a)
89+
90+cross :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
91+cross = bimap
92+
93+--------------------------------------------------------------------------------
94+
95+data Coyoneda f b = forall a. Coyoneda (a -> b) (f a)
96+
97+instance Functor (Coyoneda f) where
98+ fmap f (Coyoneda g fa)
99+ = Coyoneda (f . g) fa
100+
101+inj :: Functor f => Coyoneda f a -> f a
102+inj (Coyoneda f a) = fmap f a
103+
104+proj :: Functor f => f a -> Coyoneda f a
105+proj fa = Coyoneda id fa
106+
107+newtype Alongside p s t a b = Alongside { getAlongside :: p (s, a) (t, b) }
108+
109+instance Profunctor p => Profunctor (Alongside p c d) where
110+ dimap f g (Alongside pab) = Alongside $ dimap (fmap f) (fmap g) pab
111+
112+instance Strong p => Strong (Alongside p c d) where
113+ second' (Alongside pab) = Alongside . dimap shuffle shuffle . second' $ pab
114+ where
115+ shuffle (x,(y,z)) = (y,(x,z))
116+
117+(??) :: Functor f => f (a -> b) -> a -> f b
118+fab ?? a = fmap ($ a) fab
119+
120+-- Could implement this using primitives?
121+alongside :: Profunctor p =>
122+ LensLike (Alongside p s' t') s t a b
123+ -> LensLike (Alongside p a b) s' t' a' b'
124+ -> LensLike p (s, s') (t, t') (a, a') (b, b')
125+alongside l1 l2
126+ = dimap swap swap . getAlongside . l1 . Alongside . dimap swap swap . getAlongside . l2 . Alongside
127+
128+assoc3L :: Lens ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
129+assoc3L f = assoc3 f
130+
131+stron :: (Either s s', b) -> Either (s, b) (s', b)
132+stron (e, b) = bimap (,b) (, b) e
133+
134+choosing :: forall s t a b s' t' . Lens s t a b -> Lens s' t' a b -> Lens (Either s s') (Either t t') a b
135+choosing l r = withLens l (\getl setl ->
136+ withLens r (\getr setr ->
137+ let g :: Either s s' -> a
138+ g = either getl getr
139+ s = bimap setl setr . stron
140+ in lens g s))
141+
142+lens :: (s -> a) -> ((s,b) -> t) -> Lens s t a b
143+lens get _set = dimap (fork id get) _set . second'
144+
145+------------------------------------------------------------------------------
146+
147+data ALens a b s t = ALens (s -> a) ((s, b) -> t)
148+
149+instance Functor (ALens a b s) where
150+ fmap f (ALens _get _set) = ALens _get (f . _set)
151+
152+instance Profunctor (ALens a b) where
153+ dimap f g (ALens get _set) = ALens (get . f) (g . _set . cross f id)
154+
155+instance Strong (ALens a b) where
156+ second' (ALens get _set) = ALens (get . snd) (bimap id _set . assoc)
157+ where
158+ assoc ((a, b), c) = (a, (b, c))
159+
160+-- These are specialised versions of the Isos. On GHC 8.0.2, having
161+-- these functions eta-expanded allows the optimiser to inline these functions.
162+mLens :: Lens (M1 i c f p) (M1 i c g p) (f p) (g p)
163+mLens f = mIso f
164+
165+repLens :: (Generic a, Generic b) => Lens a b (Rep a x) (Rep b x)
166+repLens f = repIso f
167+
168+prodL :: Lens ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
169+prodL f = prodIso f
170+
171+prodR :: Lens (a' x, b' x) (a x, b x) ((a' :*: b') x) ((a :*: b) x)
172+prodR f = fromIso prodIso f
173+
174+assoc3R :: Lens (a', (b', c')) (a, (b, c)) ((a', b'), c') ((a, b), c)
175+assoc3R f = fromIso assoc3 f
src/Data/Generics/Internal/Profunctor/Prism.hsnew file+114
@@ -0,0 +1,114 @@
1+{-# LANGUAGE GADTs #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE NoMonomorphismRestriction #-}
4+{-# LANGUAGE Rank2Types #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TupleSections #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeFamilyDependencies #-}
9+{-# LANGUAGE TypeOperators #-}
10+
11+-----------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Internal.Profunctor.Prism
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- License : BSD3
16+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Internal lens helpers. Only exported for Haddock
21+--
22+-----------------------------------------------------------------------------
23+module Data.Generics.Internal.Profunctor.Prism where
24+
25+import Data.Profunctor (Choice(..), Profunctor(..))
26+import Data.Profunctor.Unsafe ((#.), (.#))
27+import GHC.Generics
28+import Data.Coerce
29+
30+type APrism s t a b = Market a b a b -> Market a b s t
31+
32+type Prism s t a b
33+ = forall p . (Choice p) => p a b -> p s t
34+
35+type Prism' s a = forall p . (Choice p) => p a a -> p s s
36+
37+left :: Prism ((a :+: c) x) ((b :+: c) x) (a x) (b x)
38+left = prism L1 $ gsum Right (Left . R1)
39+
40+right :: Prism ((a :+: b) x) ((a :+: c) x) (b x) (c x)
41+right = prism R1 $ gsum (Left . L1) Right
42+
43+prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
44+prism bt seta eta = dimap seta (either id bt) (right' eta)
45+
46+_Left :: Prism (Either a c) (Either b c) a b
47+_Left = prism Left $ either Right (Left . Right)
48+
49+_Right :: Prism (Either c a) (Either c b) a b
50+_Right = prism Right $ either (Left . Left) Right
51+
52+prismPRavel ::
53+ (Market a b a b -> Market a b s t)
54+ -> Prism s t a b
55+prismPRavel l pab = (prism2prismp $ l idPrism) pab
56+
57+--------------------------------------------------------------------------------
58+-- Prism stuff
59+
60+without' :: Prism s t a b -> Prism s t c d -> Prism s t (Either a c) (Either b d)
61+without' k =
62+ withPrism k $ \bt _ k' ->
63+ withPrism k' $ \dt setc ->
64+ prism (foldEither bt dt) $ \s -> fmap Right (setc s)
65+ where foldEither _ g (Right r) = g r
66+ foldEither f _ (Left l) = f l
67+{-# INLINE without' #-}
68+
69+withPrism :: APrism s t a b -> ((b -> t) -> (s -> Either t a) -> r) -> r
70+withPrism k f = case k idPrism of
71+ Market bt seta -> f bt seta
72+
73+prism2prismp :: Market a b s t -> Prism s t a b
74+prism2prismp (Market bt seta) = prism bt seta
75+
76+idPrism :: Market a b a b
77+idPrism = Market id Right
78+
79+gsum :: (a x -> c) -> (b x -> c) -> ((a :+: b) x) -> c
80+gsum f _ (L1 x) = f x
81+gsum _ g (R1 y) = g y
82+
83+plus :: (a -> b) -> (c -> d) -> Either a c -> Either b d
84+plus f _ (Left x) = Left (f x)
85+plus _ g (Right y) = Right (g y)
86+
87+--------------------------------------------------------------------------------
88+-- Market
89+
90+data Market a b s t = Market (b -> t) (s -> Either t a)
91+
92+instance Functor (Market a b s) where
93+ fmap f (Market bt seta) = Market (f . bt) (either (Left . f) Right . seta)
94+ {-# INLINE fmap #-}
95+
96+instance Profunctor (Market a b) where
97+ dimap f g (Market bt seta) = Market (g . bt) (either (Left . g) Right . seta . f)
98+ {-# INLINE dimap #-}
99+ lmap f (Market bt seta) = Market bt (seta . f)
100+ {-# INLINE lmap #-}
101+ rmap f (Market bt seta) = Market (f . bt) (either (Left . f) Right . seta)
102+ {-# INLINE rmap #-}
103+ ( #. ) _ = coerce
104+ {-# INLINE ( #. ) #-}
105+ ( .# ) p _ = coerce p
106+ {-# INLINE ( .# ) #-}
107+
108+instance Choice (Market a b) where
109+ left' (Market bt seta) = Market (Left . bt) $ \sc -> case sc of
110+ Left s -> case seta s of
111+ Left t -> Left (Left t)
112+ Right a -> Right a
113+ Right c -> Left (Right c)
114+ {-# INLINE left' #-}
src/Data/Generics/Internal/VL/Iso.hsnew file+47
@@ -0,0 +1,47 @@
1+{-# LANGUAGE GADTs #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE NoMonomorphismRestriction #-}
4+{-# LANGUAGE Rank2Types #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TupleSections #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeFamilyDependencies #-}
9+{-# LANGUAGE TypeOperators #-}
10+
11+-----------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Internal.VL.Iso
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- License : BSD3
16+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Internal lens helpers. Only exported for Haddock
21+--
22+-----------------------------------------------------------------------------
23+module Data.Generics.Internal.VL.Iso where
24+
25+import Data.Profunctor (Profunctor(..))
26+import GHC.Generics
27+
28+type Iso' s a
29+ = forall p f. (Profunctor p, Functor f) => p a (f a) -> p s (f s)
30+
31+type Iso s t a b
32+ = forall p f. (Profunctor p, Functor f) => p a (f b) -> p s (f t)
33+
34+-- | A type and its generic representation are isomorphic
35+repIso :: (Generic a, Generic b) => Iso a b (Rep a x) (Rep b x)
36+repIso = iso from to
37+
38+-- | 'M1' is just a wrapper around `f p`
39+mIso :: Iso (M1 i c f p) (M1 i c g p) (f p) (g p)
40+mIso = iso unM1 M1
41+
42+prodIso :: Iso ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
43+prodIso = iso (\(a :*: b) -> (a, b)) (\(a, b) -> (a :*: b))
44+
45+iso :: (s -> a) -> (b -> t) -> Iso s t a b
46+iso sa bt = dimap sa (fmap bt)
47+{-# INLINE iso #-}
src/Data/Generics/Internal/VL/Lens.hsnew file+51
@@ -0,0 +1,51 @@
1+{-# LANGUAGE GADTs #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE NoMonomorphismRestriction #-}
4+{-# LANGUAGE Rank2Types #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TupleSections #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeFamilyDependencies #-}
9+{-# LANGUAGE TypeOperators #-}
10+
11+-----------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Internal.VL.Lens
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- License : BSD3
16+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Internal lens helpers. Only exported for Haddock
21+--
22+-----------------------------------------------------------------------------
23+module Data.Generics.Internal.VL.Lens where
24+
25+import Control.Applicative (Const(..))
26+import Data.Functor.Identity (Identity(..))
27+import Data.Generics.Internal.Profunctor.Lens (ALens (..), (^.), idLens)
28+
29+-- | Type alias for lens
30+type Lens' s a
31+ = Lens s s a a
32+
33+type Lens s t a b
34+ = forall f. Functor f => (a -> f b) -> s -> f t
35+
36+view :: ((a -> Const a a) -> s -> Const a s) -> s -> a
37+view l s = (^.) s l
38+
39+set :: Lens s t a b -> b -> s -> t
40+set l x = runIdentity . (l (Identity . const x))
41+
42+lens2lensvl :: ALens a b s t -> Lens s t a b
43+lens2lensvl (ALens _get _set) = lens _get _set
44+
45+ravel :: (ALens a b a b -> ALens a b s t)
46+ -> Lens s t a b
47+ravel l pab = (lens2lensvl $ l idLens) pab
48+
49+lens :: (s -> a) -> ((s, b) -> t) -> Lens s t a b
50+lens get _set f x = curry _set x <$> f (get x)
51+
src/Data/Generics/Internal/VL/Prism.hsnew file+62
@@ -0,0 +1,62 @@
1+{-# LANGUAGE GADTs #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE NoMonomorphismRestriction #-}
4+{-# LANGUAGE Rank2Types #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TupleSections #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeFamilyDependencies #-}
9+{-# LANGUAGE TypeOperators #-}
10+
11+-----------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Internal.VL.Prism
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- License : BSD3
16+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Internal lens helpers. Only exported for Haddock
21+--
22+-----------------------------------------------------------------------------
23+module Data.Generics.Internal.VL.Prism where
24+
25+import Data.Functor.Identity (Identity(..))
26+import Data.Profunctor (Choice(..), Profunctor(..))
27+import Data.Coerce
28+import Data.Generics.Internal.Profunctor.Prism (Market (..), plus, idPrism)
29+import Data.Tagged
30+import Data.Profunctor.Unsafe ((#.), (.#))
31+
32+-- | Type alias for prism
33+type Prism s t a b
34+ = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
35+
36+type Prism' s a
37+ = Prism s s a a
38+
39+match :: Prism s t a b -> s -> Either t a
40+match k = withPrism k $ \_ _match -> _match
41+
42+prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
43+prism bt seta eta = dimap (\x -> plus pure id (seta x)) (either id (\x -> fmap bt x)) (right' eta)
44+{-# INLINE prism #-}
45+
46+prismRavel :: (Market a b a b -> Market a b s t) -> Prism s t a b
47+prismRavel l pab = (prism2prismvl $ l idPrism) pab
48+{-# INLINE prismRavel #-}
49+
50+type APrismVL s t a b = Market a b a (Identity b) -> Market a b s (Identity t)
51+
52+withPrism :: APrismVL s t a b -> ((b -> t) -> (s -> Either t a) -> r) -> r
53+withPrism k f = case coerce (k (Market Identity Right)) of
54+ Market bt seta -> f bt seta
55+
56+prism2prismvl :: Market a b s t -> Prism s t a b
57+prism2prismvl (Market bt seta) = prism bt seta
58+{-# INLINE prism2prismvl #-}
59+
60+build :: (Tagged b (Identity b) -> Tagged t (Identity t)) -> b -> t
61+build p = runIdentity #. unTagged #. p .# Tagged .# Identity
62+
src/Data/Generics/Internal/VL/Traversal.hsnew file+74
@@ -0,0 +1,74 @@
1+{-# LANGUAGE GADTs #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE NoMonomorphismRestriction #-}
4+{-# LANGUAGE Rank2Types #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TupleSections #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeFamilyDependencies #-}
9+{-# LANGUAGE TypeOperators #-}
10+
11+-----------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Internal.VL.Traversal
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- License : BSD3
16+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Internal lens helpers. Only exported for Haddock
21+--
22+-----------------------------------------------------------------------------
23+module Data.Generics.Internal.VL.Traversal where
24+
25+-- | Type alias for traversal
26+type Traversal' s a
27+ = forall f. Applicative f => (a -> f a) -> s -> f s
28+
29+type Traversal s t a b
30+ = forall f. Applicative f => (a -> f b) -> s -> f t
31+
32+confusing :: Applicative f => Traversal s t a b -> (a -> f b) -> s -> f t
33+confusing t = \f -> lowerYoneda . lowerCurried . t (liftCurriedYoneda . f)
34+ where
35+ liftCurriedYoneda :: Applicative f => f a -> Curried (Yoneda f) a
36+ liftCurriedYoneda fa = Curried (`yap` fa)
37+ {-# INLINE liftCurriedYoneda #-}
38+ yap :: Applicative f => Yoneda f (a -> b) -> f a -> Yoneda f b
39+ yap (Yoneda k) fa = Yoneda (\ab_r -> k (ab_r .) <*> fa)
40+ {-# INLINE yap #-}
41+{-# INLINE confusing #-}
42+
43+newtype Curried f a =
44+ Curried { runCurried :: forall r. f (a -> r) -> f r }
45+
46+instance Functor f => Functor (Curried f) where
47+ fmap f (Curried g) = Curried (g . fmap (.f))
48+ {-# INLINE fmap #-}
49+
50+instance (Functor f) => Applicative (Curried f) where
51+ pure a = Curried (fmap ($ a))
52+ {-# INLINE pure #-}
53+ Curried mf <*> Curried ma = Curried (ma . mf . fmap (.))
54+ {-# INLINE (<*>) #-}
55+
56+liftCurried :: Applicative f => f a -> Curried f a
57+liftCurried fa = Curried (<*> fa)
58+
59+lowerCurried :: Applicative f => Curried f a -> f a
60+lowerCurried (Curried f) = f (pure id)
61+newtype Yoneda f a = Yoneda { runYoneda :: forall b. (a -> b) -> f b }
62+
63+liftYoneda :: Functor f => f a -> Yoneda f a
64+liftYoneda a = Yoneda (\f -> fmap f a)
65+
66+lowerYoneda :: Yoneda f a -> f a
67+lowerYoneda (Yoneda f) = f id
68+
69+instance Functor (Yoneda f) where
70+ fmap f m = Yoneda (\k -> runYoneda m (k . f))
71+
72+instance Applicative f => Applicative (Yoneda f) where
73+ pure a = Yoneda (\f -> pure (f a))
74+ Yoneda m <*> Yoneda n = Yoneda (\f -> m (f .) <*> n id)
src/Data/Generics/Product/Any.hs+2−2
@@ -29,7 +29,7 @@ module Data.Generics.Product.Any
2929 HasAny (..)
3030 ) where
3131
32−import Data.Generics.Internal.Lens
32+import Data.Generics.Internal.VL.Lens
3333import Data.Generics.Product.Fields
3434import Data.Generics.Product.Positions
3535import Data.Generics.Product.Typed
@@ -67,7 +67,7 @@ class HasAny (sel :: k) s t a b | s sel k -> a where
6767 --
6868 -- >>> human ^. the @3
6969 -- "London"
70− the :: LensVL s t a b
70+ the :: Lens s t a b
7171
7272instance HasPosition i s t a b => HasAny i s t a b where
7373 the = position @i
src/Data/Generics/Product/Fields.hs+6−5
@@ -38,13 +38,14 @@ module Data.Generics.Product.Fields
3838 ) where
3939
4040import Data.Generics.Internal.Families
41−import Data.Generics.Internal.Lens
41+import Data.Generics.Internal.VL.Lens as VL
4242import Data.Generics.Internal.Void
4343import Data.Generics.Product.Internal.Keyed
4444
4545import Data.Kind (Constraint, Type)
4646import GHC.Generics
4747import GHC.TypeLits (Symbol, ErrorMessage(..), TypeError)
48+import Data.Generics.Internal.Profunctor.Lens
4849
4950-- $setup
5051-- == /Running example:/
@@ -108,21 +109,21 @@ class HasField (field :: Symbol) s t a b | s field -> a, t field -> b, s field b
108109 -- ... The offending constructors are:
109110 -- ... HumanNoAddress
110111 -- ...
111− field :: LensVL s t a b
112+ field :: VL.Lens s t a b
112113
113114type HasField' field s a = HasField field s s a a
114115
115116-- |
116117-- >>> getField @"age" human
117118-- 50
118−getField :: forall f s t a b . HasField' f s a => s -> a
119−getField = viewVL' (field @f)
119+getField :: forall f a s. HasField' f s a => s -> a
120+getField = VL.view (field @f)
120121
121122-- |
122123-- >>> setField @"age" 60 human
123124-- Human {name = "Tunyasz", age = 60, address = "London", other = False}
124125setField :: forall f s a. HasField' f s a => a -> s -> s
125−setField = setVL (field @f)
126+setField = VL.set (field @f)
126127
127128instance -- see Note [Changing type parameters]
128129 ( Generic s
src/Data/Generics/Product/Internal/Keyed.hs+2−1
@@ -27,10 +27,11 @@ module Data.Generics.Product.Internal.Keyed
2727 , GHasKey'
2828 ) where
2929
30−import Data.Generics.Internal.Lens
3130import Data.Generics.Product.Internal.List
3231import Data.Kind
3332import GHC.Generics
33+import Data.Generics.Internal.Profunctor.Lens
34+import Data.Generics.Internal.Profunctor.Iso
3435
3536class GHasKey (key :: k) (s :: Type -> Type) (t :: Type -> Type) a b | s key -> a, t key -> b where
3637 gkey :: Lens (s x) (t x) a b
src/Data/Generics/Product/Internal/List.hs+3−2
@@ -43,11 +43,12 @@ module Data.Generics.Product.Internal.List
4343 ) where
4444
4545import GHC.TypeLits
46−import Data.Generics.Internal.Lens
4746
4847import Data.Kind (Type)
4948import GHC.Generics
5049import Data.Profunctor
50+import Data.Generics.Internal.Profunctor.Lens
51+import Data.Generics.Internal.Profunctor.Iso
5152
5253data List (as :: [(m, Type)]) where
5354 Nil :: List '[]
@@ -169,7 +170,7 @@ instance {-# OVERLAPPING #-}
169170 ( as ~ ('(f, a) ': as')
170171 , bs ~ ('(f, b) ': as')
171172 ) => IndexList 0 as bs a b where
172− point = lens (\(x :> xs) -> x) (\((x :> xs), x') -> x' :> xs)
173+ point = lens (\(x :> _) -> x) (\((_ :> xs), x') -> x' :> xs)
173174 {-# INLINE point #-}
174175
175176instance
src/Data/Generics/Product/Internal/Positions.hs+2−2
@@ -33,13 +33,13 @@ module Data.Generics.Product.Internal.Positions
3333 , Size
3434 ) where
3535
36−import Data.Generics.Internal.Lens
37−
3836import Data.Generics.Product.Internal.List
3937import Data.Kind (Type)
4038import Data.Type.Bool (If, Not)
4139import GHC.Generics
4240import GHC.TypeLits (type (<=?), type (+), type (-), Nat)
41+import Data.Generics.Internal.Profunctor.Lens
42+import Data.Generics.Internal.Profunctor.Iso
4343
4444-- |As 'HasPosition' but over generic representations as defined by
4545-- "GHC.Generics".
src/Data/Generics/Product/Internal/Subtype.hs+1−1
@@ -31,11 +31,11 @@ module Data.Generics.Product.Internal.Subtype
3131 ) where
3232
3333import Data.Generics.Internal.Families
34−import Data.Generics.Internal.Lens
3534import Data.Generics.Product.Internal.Keyed
3635
3736import Data.Kind (Type)
3837import GHC.Generics
38+import Data.Generics.Internal.Profunctor.Lens
3939
4040--------------------------------------------------------------------------------
4141-- * Generic upcasting
src/Data/Generics/Product/List.hs+1−1
@@ -27,11 +27,11 @@ module Data.Generics.Product.List
2727 , IsRecord'
2828 ) where
2929
30−import Data.Generics.Internal.Lens
3130import Data.Generics.Product.Internal.List
3231import Data.Kind
3332import GHC.Generics
3433import GHC.TypeLits
34+import Data.Generics.Internal.Profunctor.Iso
3535
3636class IsList
3737 (m :: Type)
src/Data/Generics/Product/Positions.hs+4−3
@@ -38,7 +38,7 @@ module Data.Generics.Product.Positions
3838 , setPosition
3939 ) where
4040
41−import Data.Generics.Internal.Lens
41+import Data.Generics.Internal.VL.Lens as VL
4242import Data.Generics.Internal.Void
4343import Data.Generics.Internal.Families
4444import Data.Generics.Product.Internal.Positions
@@ -47,6 +47,7 @@ import Data.Kind (Constraint, Type)
4747import Data.Type.Bool (type (&&))
4848import GHC.Generics
4949import GHC.TypeLits (type (<=?), Nat, TypeError, ErrorMessage(..))
50+import Data.Generics.Internal.Profunctor.Lens
5051
5152-- $setup
5253-- == /Running example:/
@@ -86,7 +87,7 @@ class HasPosition (i :: Nat) s t a b | s i -> a, t i -> b, s i b -> t, t i a ->
8687 -- ...
8788 -- ... The type Human does not contain a field at position 4
8889 -- ...
89− position :: LensVL s t a b
90+ position :: VL.Lens s t a b
9091
9192type HasPosition' i s a = HasPosition i s s a a
9293
@@ -94,7 +95,7 @@ getPosition :: forall i s a. HasPosition' i s a => s -> a
9495getPosition s = s ^. position @i
9596
9697setPosition :: forall i s a. HasPosition' i s a => a -> s -> s
97−setPosition = setVL (position @i)
98+setPosition = VL.set (position @i)
9899
99100instance -- see Note [Changing type parameters]
100101 ( Generic s
src/Data/Generics/Product/Subtype.hs+5−4
@@ -33,13 +33,14 @@ module Data.Generics.Product.Subtype
3333 ) where
3434
3535import Data.Generics.Internal.Families
36−import Data.Generics.Internal.Lens
36+import Data.Generics.Internal.VL.Lens as VL
3737import Data.Generics.Internal.Void
3838import Data.Generics.Product.Internal.Subtype
3939
4040import GHC.Generics (Generic (Rep, to, from) )
4141import GHC.TypeLits (Symbol, TypeError, ErrorMessage (..))
4242import Data.Kind (Type, Constraint)
43+import Data.Generics.Internal.Profunctor.Lens
4344
4445-- $setup
4546-- == /Running example:/
@@ -80,9 +81,9 @@ class Subtype sup sub where
8081 --
8182 -- >>> set (super @Animal) (Animal "dog" 10) human
8283 -- Human {name = "dog", age = 10, address = "London"}
83− super :: LensVL sub sub sup sup
84+ super :: VL.Lens sub sub sup sup
8485 super
85− = lensVL upcast (uncurry smash . swap)
86+ = VL.lens upcast (uncurry smash . swap)
8687
8788 -- |Cast the more specific subtype to the more general supertype
8889 --
@@ -103,7 +104,7 @@ class Subtype sup sub where
103104 -- >>> smash (Animal "dog" 10) human
104105 -- Human {name = "dog", age = 10, address = "London"}
105106 smash :: sup -> sub -> sub
106− smash = setVL (super @sup)
107+ smash = VL.set (super @sup)
107108
108109 {-# MINIMAL super | smash, upcast #-}
109110
src/Data/Generics/Product/Typed.hs+5−4
@@ -31,13 +31,14 @@ module Data.Generics.Product.Typed
3131 ) where
3232
3333import Data.Generics.Internal.Families
34−import Data.Generics.Internal.Lens
34+import Data.Generics.Internal.VL.Lens as VL
3535import Data.Generics.Internal.Void
3636import Data.Generics.Product.Internal.Keyed
3737
3838import Data.Kind (Constraint, Type)
3939import GHC.Generics (Generic (Rep))
4040import GHC.TypeLits (TypeError, ErrorMessage (..))
41+import Data.Generics.Internal.Profunctor.Lens
4142
4243-- $setup
4344-- == /Running example:/
@@ -91,9 +92,9 @@ class HasType a s where
9192 -- ... The offending constructors are:
9293 -- ... HumanNoTall
9394 -- ...
94− typed :: LensVL s s a a
95+ typed :: VL.Lens s s a a
9596 typed
96− = lensVL (getTyped @a) (uncurry (setTyped @a) . swap)
97+ = VL.lens (getTyped @a) (uncurry (setTyped @a) . swap)
9798
9899 -- |Get field at type.
99100 getTyped :: s -> a
@@ -101,7 +102,7 @@ class HasType a s where
101102
102103 -- |Set field at type.
103104 setTyped :: a -> s -> s
104− setTyped = setVL (typed @a)
105+ setTyped = VL.set (typed @a)
105106
106107 {-# MINIMAL typed | setTyped, getTyped #-}
107108
src/Data/Generics/Product/Types.hs+1−1
@@ -30,10 +30,10 @@ module Data.Generics.Product.Types
3030 HasTypes (..)
3131 ) where
3232
33−import Data.Generics.Internal.Lens
3433import Data.Generics.Product.Internal.Types
3534
3635import GHC.Generics (Generic (Rep), from, to)
36+import Data.Generics.Internal.VL.Traversal
3737
3838class HasTypes a s where
3939 types :: Traversal' s a
src/Data/Generics/Sum/Any.hs+2−2
@@ -28,9 +28,9 @@ module Data.Generics.Sum.Any
2828 AsAny (..)
2929 ) where
3030
31−import Data.Generics.Internal.Lens
3231import Data.Generics.Sum.Constructors
3332import Data.Generics.Sum.Typed
33+import Data.Generics.Internal.VL.Prism
3434
3535-- $setup
3636-- == /Running example:/
@@ -86,7 +86,7 @@ class AsAny (sel :: k) a s | s sel k -> a where
8686 --
8787 -- >>> duck ^? _As @Age
8888 -- Just 2
89− _As :: PrismVL s s a a
89+ _As :: Prism s s a a
9090
9191instance AsConstructor ctor s s a a => AsAny ctor a s where
9292 _As = _Ctor @ctor
src/Data/Generics/Sum/Constructors.hs+4−3
@@ -33,13 +33,14 @@ module Data.Generics.Sum.Constructors
3333 ) where
3434
3535import Data.Generics.Internal.Families
36−import Data.Generics.Internal.Lens
3736import Data.Generics.Internal.Void
3837import Data.Generics.Sum.Internal.Constructors
3938
4039import Data.Kind (Constraint, Type)
4140import GHC.Generics (Generic (Rep))
4241import GHC.TypeLits (Symbol, TypeError, ErrorMessage (..))
42+import Data.Generics.Internal.VL.Prism
43+import Data.Generics.Internal.Profunctor.Iso
4344
4445-- $setup
4546-- == /Running example:/
@@ -98,7 +99,7 @@ class AsConstructor (ctor :: Symbol) s t a b | ctor s -> a, ctor t -> b where
9899 -- ...
99100 -- ... The type Animal Int does not contain a constructor named "Turtle"
100101 -- ...
101− _Ctor :: PrismVL s t a b
102+ _Ctor :: Prism s t a b
102103
103104instance
104105 ( Generic s
@@ -121,7 +122,7 @@ instance
121122 , s ~ Infer t b' a
122123 ) => AsConstructor ctor s t a b where
123124
124− _Ctor eta = prismVLRavel (repIso . _GCtor @ctor) eta
125+ _Ctor eta = prismRavel (repIso . _GCtor @ctor) eta
125126 {-# INLINE _Ctor #-}
126127
127128-- See Note [Uncluttering type signatures]
src/Data/Generics/Sum/Internal/Constructors.hs+3−1
@@ -31,11 +31,13 @@ module Data.Generics.Sum.Internal.Constructors
3131
3232import Data.Generics.Internal.Families
3333import Data.Generics.Product.Internal.List
34−import Data.Generics.Internal.Lens
3534
3635import GHC.Generics
3736import GHC.TypeLits (Symbol)
3837import Data.Kind
38+import Data.Generics.Internal.Profunctor.Lens
39+import Data.Generics.Internal.Profunctor.Iso
40+import Data.Generics.Internal.Profunctor.Prism
3941
4042-- |As 'AsConstructor' but over generic representations as defined by
4143-- "GHC.Generics".
src/Data/Generics/Sum/Internal/Subtype.hs+2−1
@@ -27,12 +27,13 @@ module Data.Generics.Sum.Internal.Subtype
2727 ( GAsSubtype (..)
2828 ) where
2929
30−import Data.Generics.Internal.Lens
3130import Data.Generics.Product.Internal.List
3231import Data.Generics.Sum.Internal.Typed
3332
3433import Data.Kind
3534import GHC.Generics
35+import Data.Generics.Internal.Profunctor.Iso
36+import Data.Generics.Internal.Profunctor.Prism
3637
3738-- |As 'AsSubtype' but over generic representations as defined by
3839-- "GHC.Generics".
src/Data/Generics/Sum/Internal/Typed.hs+2−1
@@ -32,7 +32,8 @@ import GHC.Generics
3232
3333import Data.Generics.Internal.Families
3434import Data.Generics.Product.Internal.List
35−import Data.Generics.Internal.Lens
35+import Data.Generics.Internal.Profunctor.Iso
36+import Data.Generics.Internal.Profunctor.Prism
3637
3738-- |As 'AsType' but over generic representations as defined by "GHC.Generics".
3839class GAsType (f :: Type -> Type) (as :: [((), Type)]) where
src/Data/Generics/Sum/Subtype.hs+7−6
@@ -27,11 +27,12 @@ module Data.Generics.Sum.Subtype
2727 AsSubtype (..)
2828 ) where
2929
30−import Data.Generics.Internal.Lens
3130import Data.Generics.Internal.Void
3231import Data.Generics.Sum.Internal.Subtype
3332
3433import GHC.Generics (Generic (Rep))
34+import Data.Generics.Internal.VL.Prism
35+import Data.Generics.Internal.Profunctor.Iso
3536
3637-- $setup
3738-- == /Running example:/
@@ -86,17 +87,17 @@ class AsSubtype sub sup where
8687 --
8788 -- >>> duck ^? _Sub :: Maybe FourLeggedAnimal
8889 -- Nothing
89− _Sub :: PrismVL' sup sub
90− _Sub eta = prismVL injectSub projectSub eta
90+ _Sub :: Prism' sup sub
91+ _Sub eta = prism injectSub projectSub eta
9192 {-# INLINE _Sub #-}
9293
9394 -- |Injects a subtype into a supertype (upcast).
9495 injectSub :: sub -> sup
95− injectSub = buildVL (_Sub @sub @sup)
96+ injectSub = build (_Sub @sub @sup)
9697
9798 -- |Projects a subtype from a supertype (downcast).
9899 projectSub :: sup -> Either sup sub
99− projectSub = matchVL (_Sub @sub @sup)
100+ projectSub = match (_Sub @sub @sup)
100101
101102 {-# MINIMAL (injectSub, projectSub) | _Sub #-}
102103
@@ -106,7 +107,7 @@ instance
106107 , GAsSubtype (Rep sub) (Rep sup)
107108 ) => AsSubtype sub sup where
108109
109− _Sub = prismVLRavel (repIso . _GSub . fromIso repIso)
110+ _Sub = prismRavel (repIso . _GSub . fromIso repIso)
110111
111112-- See Note [Uncluttering type signatures]
112113instance {-# OVERLAPPING #-} AsSubtype a Void where
src/Data/Generics/Sum/Typed.hs+6−12
@@ -36,9 +36,10 @@ import GHC.TypeLits (TypeError, ErrorMessage (..), Symbol)
3636import Data.Generics.Sum.Internal.Typed
3737
3838import Data.Generics.Internal.Families
39−import Data.Generics.Internal.Lens
4039import Data.Generics.Internal.Void
4140import Data.Generics.Product.Internal.List
41+import Data.Generics.Internal.VL.Prism
42+import Data.Generics.Internal.Profunctor.Iso
4243
4344-- $setup
4445-- >>> :set -XTypeApplications
@@ -85,17 +86,17 @@ class AsType a s where
8586 -- ... Duck
8687 -- ... Turtle
8788 -- ...
88− _Typed :: PrismVL' s a
89+ _Typed :: Prism' s a
8990
9091 -- |Inject by type.
9192 injectTyped :: a -> s
9293 injectTyped
93− = buildVL _Typed
94+ = build _Typed
9495
9596 -- |Project by type.
9697 projectTyped :: s -> Maybe a
9798 projectTyped
98− = either (const Nothing) Just . (matchVL _Typed)
99+ = either (const Nothing) Just . (match _Typed)
99100
100101instance
101102 ( Generic s
@@ -105,14 +106,7 @@ instance
105106 , GAsType (Rep s) as
106107 ) => AsType a s where
107108
108−-- _Typed
109−-- = prism injectTyped (either (Left . to) Right . gprojectTyped . from)
110−-- {-# INLINE _Typed #-}
111−-- injectTyped
112−-- = to . ginjectTyped
113−-- projectTyped
114−-- = either (const Nothing) Just . gprojectTyped . from
115− _Typed = prismVLRavel (repIso . _GTyped @_ @as . tupled)
109+ _Typed = prismRavel (repIso . _GTyped @_ @as . tupled)
116110 {-# INLINE _Typed #-}
117111
118112-- See Note [Uncluttering type signatures]
stack.yaml+2−2
@@ -1,10 +1,10 @@
1−resolver: lts-9.2
1+resolver: lts-10.3
22
33packages:
44- '.'
55
66extra-deps:
7−- 'inspection-testing-0.1.1.2'
7+- 'inspection-testing-0.2'
88
99flags: {}
1010
test/Spec.hs+3−33
@@ -10,19 +10,19 @@
1010{-# LANGUAGE ScopedTypeVariables #-}
1111{-# LANGUAGE TypeApplications #-}
1212{-# LANGUAGE TemplateHaskell #-}
13−-- For the VL prism test
1413
1514module Main where
1615
1716import GHC.Generics
18−import Data.Profunctor
1917import Data.Generics.Product
20−import Data.Generics.Internal.Lens (prismPRavel, plus)
2118import Data.Generics.Sum
2219import Test.Inspection
2320import Test.HUnit
2421import Util
2522import System.Exit
23+import Data.Generics.Internal.VL.Lens
24+import Data.Generics.Internal.VL.Prism
25+import Data.Generics.Internal.VL.Traversal
2626
2727main :: IO ()
2828main = do
@@ -45,24 +45,6 @@ data Record3 a = MkRecord3
4545 , fieldB :: Bool
4646 } deriving (Generic, Show)
4747
48−type Lens' s a = Lens s s a a
49−type Prism' s a = Prism s s a a
50−type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
51−type Prism s t a b =
52− forall p f . (Choice p, Applicative f) => p a (f b) -> p s (f t)
53−
54−type PrismP s t a b =
55− forall p . (Choice p) => p a b -> p s t
56−type Traversal' s a = forall f. Applicative f => (a -> f a) -> s -> f s
57−
58−prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
59−prism bt seta eta = dimap (\x -> plus pure id (seta x)) (either id (\x -> fmap bt x)) (right' eta)
60−{-# INLINE prism #-}
61−
62−prismP :: (b -> t) -> (s -> Either t a) -> PrismP s t a b
63−prismP bt seta eta = dimap seta (either id bt) (right' eta)
64−{-# INLINE prismP #-}
65−
6648data Record4 a = MkRecord4
6749 { fieldA :: a
6850 , fieldB :: a
@@ -114,14 +96,6 @@ sum1PrismManual eta = prism g f eta
11496 s -> Left s
11597 g = B
11698
117−sum1PrismPManual :: PrismP Sum1 Sum1 Int Int
118−sum1PrismPManual eta = prismP g f eta
119− where
120− f s1 = case s1 of
121− B i -> Right i
122− s -> Left s
123− g = B
124−
12599subtypePrismManual :: Prism Sum1 Sum1 Sum2 Sum2
126100subtypePrismManual eta = prism g f eta
127101 where
@@ -165,9 +139,6 @@ typeChangingGenericCompose = field @"fieldA" . field @"fieldA"
165139sum1PrismB :: Prism Sum1 Sum1 Int Int
166140sum1PrismB = _Ctor @"B"
167141
168−--sum1PrismBP :: PrismP Sum1 Sum1 Int Int
169−--sum1PrismBP = prismPRavel (_CtorRaw @"B")
170−
171142subtypePrismGeneric :: Prism Sum1 Sum1 Sum2 Sum2
172143subtypePrismGeneric = _Sub
173144
@@ -185,6 +156,5 @@ tests = TestList $ map mkHUnitTest
185156 , $(inspectTest $ 'typeChangingManualCompose === 'typeChangingGenericCompose)
186157 , $(inspectTest $ 'intTraversalManual === 'intTraversalDerived)
187158 , $(inspectTest $ 'sum1PrismManual === 'sum1PrismB)
188− -- , $(inspectTest $ 'sum1PrismPManual === 'sum1PrismBP)
189159 , $(inspectTest $ 'subtypePrismManual === 'subtypePrismGeneric)
190160 , $(inspectTest $ 'sum1PrismManual === 'sum1TypePrism) ]