on master help sign in
Reimplement internals using profunctor representation
commit2b4da6928255642689cf62b4f8932242f6a36abb authorMatthew Pickering <matthewtpickering@gmail.com> dateThu 18 Jan 2018, 16:43 (9 years ago) parent5fe8bb5
14 files changed+210−146
src/Data/Generics/Internal/Lens.hs+169−107 src/Data/Generics/Product/Any.hs+1−1 src/Data/Generics/Product/Fields.hs+4−4 src/Data/Generics/Product/Internal/List.hs+7−6 src/Data/Generics/Product/Internal/Subtype.hs+2−2 src/Data/Generics/Product/Positions.hs+2−2 src/Data/Generics/Product/Subtype.hs+4−4 src/Data/Generics/Product/Typed.hs+4−4 src/Data/Generics/Sum/Any.hs+1−1 src/Data/Generics/Sum/Constructors.hs+2−2 src/Data/Generics/Sum/Internal/Constructors.hs+6−6 src/Data/Generics/Sum/Internal/Typed.hs+2−2 src/Data/Generics/Sum/Subtype.hs+1−1 src/Data/Generics/Sum/Typed.hs+5−4
src/Data/Generics/Internal/Lens.hs+169−107
@@ -4,6 +4,9 @@
44{-# LANGUAGE TypeFamilies #-}
55{-# LANGUAGE TypeFamilyDependencies #-}
66{-# LANGUAGE TypeOperators #-}
7+{-# LANGUAGE TupleSections #-}
8+{-# LANGUAGE NoMonomorphismRestriction #-}
9+{-# LANGUAGE ScopedTypeVariables #-}
710
811-----------------------------------------------------------------------------
912-- |
@@ -22,19 +25,23 @@ module Data.Generics.Internal.Lens where
2225import Control.Applicative (Const(..))
2326import Data.Functor.Identity (Identity(..))
2427import Data.Monoid (First (..))
25−import Data.Profunctor (Choice(..), Profunctor(..))
28+import Data.Profunctor (Choice(..), Profunctor(..), Strong(..))
29+import Data.Bifunctor
2630import Data.Profunctor.Unsafe ((#.), (.#))
2731import Data.Tagged
2832import GHC.Generics ((:*:)(..), (:+:)(..), Generic(..), M1(..), K1(..), Rep)
2933import Data.Coerce
3034
3135-- | Type alias for lens
32−type Lens' s a
36+type LensVL' s a
3337 = Lens s s a a
3438
35−type Lens s t a b
39+type LensVL s t a b
3640 = forall f. Functor f => (a -> f b) -> s -> f t
3741
42+type Lens s t a b
43+ = forall p . (Strong p) => p a b -> p s t
44+
3845-- | Type alias for traversal
3946type Traversal' s a
4047 = forall f. Applicative f => (a -> f a) -> s -> f s
@@ -42,127 +49,153 @@ type Traversal' s a
4249type Traversal s t a b
4350 = forall f. Applicative f => (a -> f b) -> s -> f t
4451
45−type LensLike f s t a b
46− = (a -> f b) -> s -> f t
52+type LensLike p s t a b
53+ = p a b -> p s t
4754
4855-- | Type alias for prism
49−type Prism s t a b
56+type PrismVL s t a b
5057 = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
5158
52−type PrismP s t a b
59+type Prism s t a b
5360 = forall p . (Choice p) => p a b -> p s t
5461
55−type Prism' s a
56− = Prism s s a a
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
5766
58−type Iso' s a
67+type IsoVL' s a
5968 = forall p f. (Profunctor p, Functor f) => p a (f a) -> p s (f s)
6069
61−type Iso s t a b
70+type IsoVL s t a b
6271 = forall p f. (Profunctor p, Functor f) => p a (f b) -> p s (f t)
6372
64−type IsoP s t a b
73+type Iso s t a b
6574 = forall p. (Profunctor p) => p a b -> p s t
6675
76+type Iso' s a = Iso s s a a
77+
6778-- | Getting
6879(^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
6980s ^. l = getConst (l Const s)
7081infixl 8 ^.
7182
7283-- | Setting
73−set :: ((a -> Identity b) -> s -> Identity t) -> b -> s -> t
74−set l b
75− = runIdentity . l (\_ -> Identity b)
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
76108
77109infixr 4 .~
78−(.~) :: ((a -> Identity b) -> s -> Identity t) -> b -> s -> t
79−(.~) = set
110+(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
111+(.~) f b s = set f (s, b)
80112
81113infixl 8 ^?
82114(^?) :: s -> ((a -> Const (First a) a) -> s -> Const (First a) s) -> Maybe a
83115s ^? l = getFirst (fmof l (First #. Just) s)
84116 where fmof l' f = getConst #. l' (Const #. f)
85117
86−infixr 8 #
87−(#) :: (Tagged b (Identity b) -> Tagged t (Identity t)) -> b -> t
88−(#) p = runIdentity #. unTagged #. p .# Tagged .# Identity
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+
89129
90130-- | Lens focusing on the first element of a product
91131first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
92−first f (a :*: b)
93− = fmap (:*: b) (f a)
94−{-# INLINE first #-}
132+first
133+ = lens (\(a :*: b) -> a) (\((a :*: b), a') -> a' :*: b)
95134
96135-- | Lens focusing on the second element of a product
97136second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
98−second f (a :*: b)
99− = fmap (a :*:) (f b)
100−{-# INLINE second #-}
137+second
138+ = lens (\(a :*: b) -> b) (\((a :*: b), b') -> a :*: b')
101139
102140left :: Prism ((a :+: c) x) ((b :+: c) x) (a x) (b x)
103141left = prism L1 $ gsum Right (Left . R1)
104−{-# INLINE left #-}
105−
106−leftP :: PrismP ((a :+: c) x) ((b :+: c) x) (a x) (b x)
107−leftP = prismP L1 $ gsum Right (Left . R1)
108142
109143right :: Prism ((a :+: b) x) ((a :+: c) x) (b x) (c x)
110144right = prism R1 $ gsum (Left . L1) Right
111−{-# INLINE right #-}
112−
113−rightP :: PrismP ((a :+: b) x) ((a :+: c) x) (b x) (c x)
114−rightP = prismP R1 $ gsum (Left . L1) Right
115145
116146gsum :: (a x -> c) -> (b x -> c) -> ((a :+: b) x) -> c
117147gsum f _ (L1 x) = f x
118148gsum _ g (R1 y) = g y
119−{-# INLINE gsum #-}
120149
121−prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
122−prism bt seta eta = dimap (\x -> plus pure id (seta x)) (either id (\x -> fmap bt x)) (right' eta)
123−{-# INLINE prism #-}
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 #-}
124153
125−prismP :: (b -> t) -> (s -> Either t a) -> PrismP s t a b
126−prismP bt seta eta = dimap seta (either id bt) (right' eta)
127−{-# INLINE prismP #-}
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)
128156
129157plus :: (a -> b) -> (c -> d) -> Either a c -> Either b d
130158plus f _ (Left x) = Left (f x)
131159plus _ g (Right y) = Right (g y)
132160
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+
133170-- | A type and its generic representation are isomorphic
134−repIso :: (Generic a, Generic b) => Iso a b (Rep a x) (Rep b x)
135−repIso = iso from to
136−{-# INLINE repIso #-}
171+repIsoVL :: (Generic a, Generic b) => IsoVL a b (Rep a x) (Rep b x)
172+repIsoVL = isoVL from to
137173
138174repLens :: (Generic a, Generic b) => Lens a b (Rep a x) (Rep b x)
139−repLens f s = repIso f s
140−{-# INLINE repLens #-}
175+repLens f = repIso f
141176
142177-- | A type and its generic representation are isomorphic
143−repIsoP :: (Generic a, Generic b) => IsoP a b (Rep a x) (Rep b x)
144−repIsoP = dimap from to
178+repIso :: (Generic a, Generic b) => Iso a b (Rep a x) (Rep b x)
179+repIso = iso from to
180+
145181
146182
147183-- | 'M1' is just a wrapper around `f p`
148−mIso :: Iso (M1 i c f p) (M1 i c g p) (f p) (g p)
149−mIso = iso unM1 M1
150−{-# INLINE mIso #-}
184+mIsoVL :: IsoVL (M1 i c f p) (M1 i c g p) (f p) (g p)
185+mIsoVL = isoVL unM1 M1
151186
152187-- | 'M1' is just a wrapper around `f p`
153188--mIso :: Iso' (M1 i c f p) (f p)
154−mIsoP :: IsoP (M1 i c f p) (M1 i c g p) (f p) (g p)
155−mIsoP = dimap unM1 M1
189+mIso :: Iso (M1 i c f p) (M1 i c g p) (f p) (g p)
190+mIso = iso unM1 M1
156191
157192-- These are specialised versions of the Isos above. On GHC 8.0.2, having
158193-- these functions eta-expanded allows the optimiser to inline these functions.
159194mLens :: Lens (M1 i c f p) (M1 i c g p) (f p) (g p)
160−mLens f s = mIso f s
161−{-# INLINE mLens #-}
195+mLens f = mIso f
162196
163197kIso :: Iso (K1 r a p) (K1 r b p) a b
164198kIso = iso unK1 K1
165−{-# INLINE kIso #-}
166199
167200sumIso :: Iso ((a :+: b) x) ((a' :+: b') x) (Either (a x) (b x)) (Either (a' x) (b' x))
168201sumIso = iso back forth
@@ -170,7 +203,6 @@ sumIso = iso back forth
170203 forth (Right r) = (R1 r)
171204 back (L1 l) = (Left l)
172205 back (R1 r) = (Right r)
173−{-# INLINE sumIso #-}
174206
175207_Left :: Prism (Either a c) (Either b c) a b
176208_Left = prism Left $ either Right (Left . Right)
@@ -192,70 +224,81 @@ inj (Coyoneda f a) = fmap f a
192224proj :: Functor f => f a -> Coyoneda f a
193225proj fa = Coyoneda id fa
194226
195−ravel :: Functor f => ((a -> Coyoneda f b) -> s -> Coyoneda f t) -> (a -> f b) -> (s -> f t)
196−ravel coy f s = inj $ coy (\a -> proj (f a)) s
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
197233
198234prismPRavel ::
199235 (Market a b a b -> Market a b s t)
200− -> PrismP s t a b
236+ -> Prism s t a b
201237prismPRavel l pab = (prism2prismp $ l idPrism) pab
202−{-# INLINE prismPRavel #-}
203238
204−prismRavel ::
239+prismVLRavel ::
205240 (Market a b a b -> Market a b s t)
206− -> Prism s t a b
207−prismRavel l pab = (prism2prismvl $ l idPrism) pab
208−{-# INLINE prismRavel #-}
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) }
209245
210−newtype AlongsideLeft f b a = AlongsideLeft { getAlongsideLeft :: f (a, b) }
211−newtype AlongsideRight f b a = AlongsideRight { getAlongsideRight :: f (b, a) }
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))
212253
213254(??) :: Functor f => f (a -> b) -> a -> f b
214255fab ?? a = fmap ($ a) fab
215256
216−instance Functor f => Functor (AlongsideLeft f b) where
217− fmap f = AlongsideLeft . fmap (\(a, b) -> (f a, b)) . getAlongsideLeft
218−
219−instance Functor f => Functor (AlongsideRight f b) where
220− fmap f = AlongsideRight . fmap (\(a, b) -> (a, f b)) . getAlongsideRight
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
221264
222−alongside :: LensLike (AlongsideLeft f b') s t a b
223− -> LensLike (AlongsideRight f t) s' t' a' b'
224− -> LensLike f (s, s') (t, t') (a, a') (b, b')
225−alongside l1 l2 f (a1, a2)
226− = getAlongsideRight $ l2 ?? a2 $ \b2 -> AlongsideRight
227− $ getAlongsideLeft $ l1 ?? a1 $ \b1 -> AlongsideLeft
228− $ f (b1,b2)
229−{-# INLINE alongside #-}
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))
230267
231268prodIso :: Iso ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
232269prodIso = iso (\(a :*: b) -> (a, b)) (\(a, b) -> (a :*: b))
233−{-# INLINE prodIso #-}
234270
235271prodL :: Lens ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
236−prodL f s = prodIso f s
237−{-# INLINE prodL #-}
272+prodL f = prodIso f
238273
239274prodR :: Lens (a' x, b' x) (a x, b x) ((a' :*: b') x) ((a :*: b) x)
240−prodR f s = fromIso prodIso f s
241−{-# INLINE prodR #-}
275+prodR f = fromIso prodIso f
242276
243277assoc3 :: Iso ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
244278assoc3 = iso (\((a, b), c) -> (a, (b, c))) (\(a, (b, c)) -> ((a, b), c))
245−{-# INLINE assoc3 #-}
246279
247280assoc3L :: Lens ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
248−assoc3L f s = assoc3 f s
249−{-# INLINE assoc3L #-}
281+assoc3L f = assoc3 f
250282
251283assoc3R :: Lens (a', (b', c')) (a, (b, c)) ((a', b'), c') ((a, b), c)
252−assoc3R f s = fromIso assoc3 f s
253−{-# INLINE assoc3R #-}
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))
254296
255−choosing :: Lens s t a b -> Lens s' t' a b -> Lens (Either s s') (Either t t') a b
256−choosing l _ f (Left a) = Left <$> l f a
257−choosing _ r f (Right a') = Right <$> r f a'
258−{-# INLINE choosing #-}
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)
259302
260303--------------------------------------------------------------------------------
261304-- Iso stuff
@@ -282,13 +325,16 @@ fromIso :: Iso s t a b -> Iso b a t s
282325fromIso l = withIso l $ \ sa bt -> iso bt sa
283326{-# INLINE fromIso #-}
284327
285−iso :: (s -> a) -> (b -> t) -> Iso s t a b
286−iso sa bt = dimap sa (fmap bt)
328+isoVL :: (s -> a) -> (b -> t) -> IsoVL s t a b
329+isoVL sa bt = dimap sa (fmap bt)
287330{-# INLINE iso #-}
288331
332+iso :: (s -> a) -> (b -> t) -> Iso s t a b
333+iso = dimap
334+
289335withIso :: Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
290−withIso ai k = case ai (Exchange id Identity) of
291− Exchange sa bt -> k sa (runIdentity #. bt)
336+withIso ai k = case ai (Exchange id id) of
337+ Exchange sa bt -> k sa bt
292338
293339pairing :: Iso s t a b -> Iso s' t' a' b' -> Iso (s, s') (t, t') (a, a') (b, b')
294340pairing f g = withIso f $ \ sa bt -> withIso g $ \s'a' b't' ->
@@ -298,7 +344,8 @@ pairing f g = withIso f $ \ sa bt -> withIso g $ \s'a' b't' ->
298344--------------------------------------------------------------------------------
299345-- Prism stuff
300346
301−type APrism s t a b = Market a b a (Identity b) -> Market a b s (Identity t)
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)
302349
303350-- without :: APrism s t a b
304351-- -> APrism u v c d
@@ -315,7 +362,7 @@ type APrism s t a b = Market a b a (Identity b) -> Market a b s (Identity t)
315362-- bimapE _ g (Right a) = Right (g a)
316363-- {-# INLINE bimapE #-}
317364
318−without' :: APrism s t a b -> APrism s t c d -> Prism s t (Either a c) (Either b d)
365+without' :: Prism s t a b -> Prism s t c d -> Prism s t (Either a c) (Either b d)
319366without' k =
320367 withPrism k $ \bt _ k' ->
321368 withPrism k' $ \dt setc ->
@@ -324,9 +371,13 @@ without' k =
324371 foldEither f _ (Left l) = f l
325372
326373withPrism :: APrism s t a b -> ((b -> t) -> (s -> Either t a) -> r) -> r
327−withPrism k f = case coerce (k (Market Identity Right)) of
374+withPrism k f = case k idPrism of
328375 Market bt seta -> f bt seta
329376
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+
330381--------------------------------------------------------------------------------
331382-- Market
332383
@@ -356,11 +407,11 @@ instance Choice (Market a b) where
356407 Right c -> Left (Right c)
357408 {-# INLINE left' #-}
358409
359−prism2prismp :: Market a b s t -> PrismP s t a b
360−prism2prismp (Market bt seta) = prismP bt seta
410+prism2prismp :: Market a b s t -> Prism s t a b
411+prism2prismp (Market bt seta) = prism bt seta
361412
362−prism2prismvl :: Market a b s t -> Prism s t a b
363−prism2prismvl (Market bt seta) = prism bt seta
413+prism2prismvl :: Market a b s t -> PrismVL s t a b
414+prism2prismvl (Market bt seta) = prismVL bt seta
364415{-# INLINE prism2prismvl #-}
365416
366417idPrism :: Market a b a b
@@ -368,6 +419,19 @@ idPrism = Market id Right
368419
369420------------------------------------------------------------------------------
370421
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+
371435newtype Curried f a =
372436 Curried { runCurried :: forall r. f (a -> r) -> f r }
373437
@@ -396,14 +460,12 @@ instance (Functor f) => Applicative (Curried f) where
396460-- @
397461liftCurried :: Applicative f => f a -> Curried f a
398462liftCurried fa = Curried (<*> fa)
399−{-# INLINE liftCurried #-}
400463
401464-- | Lower 'Curried' by applying 'pure' 'id' to the continuation.
402465--
403466-- See 'liftCurried'.
404467lowerCurried :: Applicative f => Curried f a -> f a
405468lowerCurried (Curried f) = f (pure id)
406−{-# INLINE lowerCurried #-}
407469
408470confusing :: Applicative f => Traversal s t a b -> (a -> f b) -> s -> f t
409471confusing t = \f -> lowerYoneda . lowerCurried . t (liftCurriedYoneda . f)
src/Data/Generics/Product/Any.hs+1−1
@@ -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 :: Lens s t a b
70+ the :: LensVL 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+4−4
@@ -108,21 +108,21 @@ class HasField (field :: Symbol) s t a b | s field -> a, t field -> b, s field b
108108 -- ... The offending constructors are:
109109 -- ... HumanNoAddress
110110 -- ...
111− field :: Lens s t a b
111+ field :: LensVL s t a b
112112
113113type HasField' field s a = HasField field s s a a
114114
115115-- |
116116-- >>> getField @"age" human
117117-- 50
118−getField :: forall f s a. HasField' f s a => s -> a
119−getField s = s ^. field @f
118+getField :: forall f s t a b . HasField' f s a => s -> a
119+getField = viewVL' (field @f)
120120
121121-- |
122122-- >>> setField @"age" 60 human
123123-- Human {name = "Tunyasz", age = 60, address = "London", other = False}
124124setField :: forall f s a. HasField' f s a => a -> s -> s
125−setField = set (field @f)
125+setField = setVL (field @f)
126126
127127instance -- see Note [Changing type parameters]
128128 ( Generic s
src/Data/Generics/Product/Internal/List.hs+7−6
@@ -47,6 +47,7 @@ import Data.Generics.Internal.Lens
4747
4848import Data.Kind (Type)
4949import GHC.Generics
50+import Data.Profunctor
5051
5152data List (as :: [(m, Type)]) where
5253 Nil :: List '[]
@@ -80,10 +81,10 @@ class GIsList
8081 glist :: Iso (f x) (g x) (List as) (List bs)
8182
8283 glistL :: Lens (f x) (g x) (List as) (List bs)
83− glistL f s = glist @m f s
84+ glistL f = glist @m f
8485
8586 glistR :: Lens (List bs) (List as) (g x) (f x)
86− glistR f s = fromIso (glist @m) f s
87+ glistR f = fromIso (glist @m) f
8788
8889instance
8990 ( GIsList m l l' as as'
@@ -168,15 +169,15 @@ instance {-# OVERLAPPING #-}
168169 ( as ~ ('(f, a) ': as')
169170 , bs ~ ('(f, b) ': as')
170171 ) => IndexList 0 as bs a b where
171− point f (x :> xs) = (:> xs) <$> f x
172+ point = lens (\(x :> xs) -> x) (\((x :> xs), x') -> x' :> xs)
172173 {-# INLINE point #-}
173174
174175instance
175176 ( IndexList (n - 1) as' bs' a b
176− , as ~ (x ': as')
177− , bs ~ (x ': bs')
177+ , as ~ ('(f, x) ': as')
178+ , bs ~ ('(f, x) ': bs')
178179 ) => IndexList n as bs a b where
179− point f (x :> xs) = (x :>) <$> point @(n - 1) f xs
180+ point = fromIso consing . alongside id (point @(n-1)) . second'
180181 {-# INLINE point #-}
181182
182183--------------------------------------------------------------------------------
src/Data/Generics/Product/Internal/Subtype.hs+2−2
@@ -50,7 +50,7 @@ instance
5050 GHasKey field sub sub t t
5151 => GUpcast sub (S1 ('MetaSel ('Just field) p f b) (Rec0 t)) where
5252
53− gupcast r = M1 (K1 (r ^. gkey @field))
53+ gupcast r = M1 (K1 (view (gkey @field) r))
5454
5555instance GUpcast sub sup => GUpcast sub (C1 c sup) where
5656 gupcast = M1 . gupcast
@@ -86,7 +86,7 @@ class GSmashLeaf sub sup (w :: Bool) where
8686instance
8787 GHasKey field sup sup t t
8888 => GSmashLeaf (S1 ('MetaSel ('Just field) p f b) (Rec0 t)) sup 'True where
89− gsmashLeaf sup _ = M1 (K1 (sup ^. gkey @field))
89+ gsmashLeaf sup _ = M1 (K1 (view (gkey @field) sup))
9090
9191instance GSmashLeaf (S1 ('MetaSel ('Just field) p f b) (Rec0 t)) sup 'False where
9292 gsmashLeaf _ = id
src/Data/Generics/Product/Positions.hs+2−2
@@ -86,7 +86,7 @@ class HasPosition (i :: Nat) s t a b | s i -> a, t i -> b, s i b -> t, t i a ->
8686 -- ...
8787 -- ... The type Human does not contain a field at position 4
8888 -- ...
89− position :: Lens s t a b
89+ position :: LensVL s t a b
9090
9191type HasPosition' i s a = HasPosition i s s a a
9292
@@ -94,7 +94,7 @@ getPosition :: forall i s a. HasPosition' i s a => s -> a
9494getPosition s = s ^. position @i
9595
9696setPosition :: forall i s a. HasPosition' i s a => a -> s -> s
97−setPosition = set (position @i)
97+setPosition = setVL (position @i)
9898
9999instance -- see Note [Changing type parameters]
100100 ( Generic s
src/Data/Generics/Product/Subtype.hs+4−4
@@ -80,9 +80,9 @@ class Subtype sup sub where
8080 --
8181 -- >>> set (super @Animal) (Animal "dog" 10) human
8282 -- Human {name = "dog", age = 10, address = "London"}
83− super :: Lens' sub sup
84− super f sub
85− = fmap (`smash` sub) (f (upcast sub))
83+ super :: LensVL sub sub sup sup
84+ super
85+ = lensVL upcast (uncurry smash . swap)
8686
8787 -- |Cast the more specific subtype to the more general supertype
8888 --
@@ -103,7 +103,7 @@ class Subtype sup sub where
103103 -- >>> smash (Animal "dog" 10) human
104104 -- Human {name = "dog", age = 10, address = "London"}
105105 smash :: sup -> sub -> sub
106− smash = set (super @sup)
106+ smash = setVL (super @sup)
107107
108108 {-# MINIMAL super | smash, upcast #-}
109109
src/Data/Generics/Product/Typed.hs+4−4
@@ -91,9 +91,9 @@ class HasType a s where
9191 -- ... The offending constructors are:
9292 -- ... HumanNoTall
9393 -- ...
94− typed :: Lens' s a
95− typed f t
96− = fmap (flip (setTyped @a) t) (f (getTyped @a t))
94+ typed :: LensVL s s a a
95+ typed
96+ = lensVL (getTyped @a) (uncurry (setTyped @a) . swap)
9797
9898 -- |Get field at type.
9999 getTyped :: s -> a
@@ -101,7 +101,7 @@ class HasType a s where
101101
102102 -- |Set field at type.
103103 setTyped :: a -> s -> s
104− setTyped = set (typed @a)
104+ setTyped = setVL (typed @a)
105105
106106 {-# MINIMAL typed | setTyped, getTyped #-}
107107
src/Data/Generics/Sum/Any.hs+1−1
@@ -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 :: Prism s s a a
89+ _As :: PrismVL 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+2−2
@@ -98,7 +98,7 @@ class AsConstructor (ctor :: Symbol) s t a b | ctor s -> a, ctor t -> b where
9898 -- ...
9999 -- ... The type Animal Int does not contain a constructor named "Turtle"
100100 -- ...
101− _Ctor :: Prism s t a b
101+ _Ctor :: PrismVL s t a b
102102
103103instance
104104 ( Generic s
@@ -121,7 +121,7 @@ instance
121121 , s ~ Infer t b' a
122122 ) => AsConstructor ctor s t a b where
123123
124− _Ctor eta = prismRavel (repIsoP . _GCtor @ctor) eta
124+ _Ctor eta = prismVLRavel (repIso . _GCtor @ctor) eta
125125 {-# INLINE _Ctor #-}
126126
127127-- See Note [Uncluttering type signatures]
src/Data/Generics/Sum/Internal/Constructors.hs+6−6
@@ -40,7 +40,7 @@ import Data.Kind
4040-- |As 'AsConstructor' but over generic representations as defined by
4141-- "GHC.Generics".
4242class GAsConstructor (ctor :: Symbol) s t a b | ctor s -> a, ctor t -> b where
43− _GCtor :: PrismP (s x) (t x) a b
43+ _GCtor :: Prism (s x) (t x) a b
4444
4545type GAsConstructor' ctor s a = GAsConstructor ctor s s a a
4646
@@ -53,7 +53,7 @@ instance
5353
5454-- _GCtor = prismP (M1 . gfromCollection . tupleToList) (Right . listToTuple . gtoCollection . unM1)
5555-- {-# INLINE _GCtor #-}
56− _GCtor = prismP (M1 . (^. fromIso (glist @Type)) . tupleToList) (Right . listToTuple . (^. glist @Type) . unM1)
56+ _GCtor = prism (M1 . (view (fromIso (glist @Type))) . tupleToList) (Right . listToTuple . (view (glist @Type)) . unM1)
5757
5858
5959instance GSumAsConstructor ctor (HasCtorP ctor l) l r l' r' a b => GAsConstructor ctor (l :+: r) (l' :+: r') a b where
@@ -61,16 +61,16 @@ instance GSumAsConstructor ctor (HasCtorP ctor l) l r l' r' a b => GAsConstructo
6161 {-# INLINE _GCtor #-}
6262
6363instance GAsConstructor ctor f f' a b => GAsConstructor ctor (M1 D meta f) (M1 D meta f') a b where
64− _GCtor = mIsoP . _GCtor @ctor
64+ _GCtor = mIso . _GCtor @ctor
6565 {-# INLINE _GCtor #-}
6666
6767class GSumAsConstructor (ctor :: Symbol) (contains :: Bool) l r l' r' a b | ctor l r -> a, ctor l' r' -> b where
68− _GSumCtor :: PrismP ((l :+: r) x) ((l' :+: r') x) a b
68+ _GSumCtor :: Prism ((l :+: r) x) ((l' :+: r') x) a b
6969
7070instance GAsConstructor ctor l l' a b => GSumAsConstructor ctor 'True l r l' r a b where
71− _GSumCtor = leftP . _GCtor @ctor
71+ _GSumCtor = left . _GCtor @ctor
7272 {-# INLINE _GSumCtor #-}
7373
7474instance GAsConstructor ctor r r' a b => GSumAsConstructor ctor 'False l r l r' a b where
75− _GSumCtor = rightP . _GCtor @ctor
75+ _GSumCtor = right . _GCtor @ctor
7676 {-# INLINE _GSumCtor #-}
src/Data/Generics/Sum/Internal/Typed.hs+2−2
@@ -36,7 +36,7 @@ import Data.Generics.Internal.Lens
3636
3737-- |As 'AsType' but over generic representations as defined by "GHC.Generics".
3838class GAsType (f :: Type -> Type) (as :: [((), Type)]) where
39− _GTyped :: Prism' (f x) (List as)
39+ _GTyped :: Prism (f x) (f x) (List as) (List as)
4040
4141instance
4242 ( GIsList () f f as as
@@ -50,7 +50,7 @@ instance GAsType f a => GAsType (M1 D meta f) a where
5050 _GTyped = mIso . _GTyped
5151
5252class GSumAsType (contains :: Bool) l r (a :: [((), Type)]) where
53− _GSumTyped :: Prism' ((l :+: r) x) (List a)
53+ _GSumTyped :: Prism ((l :+: r) x) ((l :+: r) x) (List a) (List a)
5454
5555instance GAsType l a => GSumAsType 'True l r a where
5656 _GSumTyped = left . _GTyped
src/Data/Generics/Sum/Subtype.hs+1−1
@@ -92,7 +92,7 @@ class AsSubtype sub sup where
9292
9393 -- |Injects a subtype into a supertype (upcast).
9494 injectSub :: sub -> sup
95− injectSub = (_Sub @sub @sup #)
95+ injectSub = build (_Sub @sub @sup)
9696
9797 -- |Projects a subtype from a supertype (downcast).
9898 projectSub :: sup -> Either sup sub
src/Data/Generics/Sum/Typed.hs+5−4
@@ -85,17 +85,17 @@ class AsType a s where
8585 -- ... Duck
8686 -- ... Turtle
8787 -- ...
88− _Typed :: Prism' s a
88+ _Typed :: PrismVL' s a
8989
9090 -- |Inject by type.
9191 injectTyped :: a -> s
9292 injectTyped
93− = (_Typed #)
93+ = buildVL _Typed
9494
9595 -- |Project by type.
9696 projectTyped :: s -> Maybe a
9797 projectTyped
98− = either (const Nothing) Just . (withPrism _Typed (\_ b -> b))
98+ = either (const Nothing) Just . (matchVL _Typed)
9999
100100instance
101101 ( Generic s
@@ -112,7 +112,8 @@ instance
112112-- = to . ginjectTyped
113113-- projectTyped
114114-- = either (const Nothing) Just . gprojectTyped . from
115− _Typed = repIso . _GTyped @_ @as . tupled
115+ _Typed = prismVLRavel (repIso . _GTyped @_ @as . tupled)
116+ {-# INLINE _Typed #-}
116117
117118-- See Note [Uncluttering type signatures]
118119instance {-# OVERLAPPING #-} AsType a Void where