@@ -47,15 +47,21 @@ set f (s, b)
4747 = f (const b) s
4848
4949view :: Lens s s a a -> s -> a
50−view l = withLens l (\get _ -> get)
50+view l = withLensPrim l (\get _ -> snd . get)
5151
52−withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
53−withLens l k =
52+--withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
53+--ithLens l k =
54+-- case l idLens of
55+-- ALens _get _set -> k (snd . _get) (\(s, b) -> _set ((fst $ _get s), b))
56+
57+withLensPrim :: Lens s t a b -> (forall c . (s -> (c,a)) -> ((c, b) -> t) -> r) -> r
58+withLensPrim l k =
5459 case l idLens of
5560 ALens _get _set -> k _get _set
5661
5762idLens :: ALens a b a b
58−idLens = ALens id snd
63+idLens = ALens (fork (const ()) id) snd
64+{-# INLINE idLens #-}
5965
6066infixr 4 .~
6167(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
@@ -73,12 +79,12 @@ build p = unTagged . p . Tagged
7379-- | Lens focusing on the first element of a product
7480first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
7581first
76− = lens (\(a :*: _) -> a) (\(_ :*: b, a') -> a' :*: b)
82+ = lens (\(a :*: b) -> (b,a)) (\(b, a') -> a' :*: b)
7783
7884-- | Lens focusing on the second element of a product
7985second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
8086second
81− = lens (\(_ :*: b) -> b) (\(a :*: _, b') -> a :*: b')
87+ = lens (\(a :*: b) -> (a,b)) (\(a, b') -> a :*: b')
8288
8389fork :: (a -> b) -> (a -> c) -> a -> (b, c)
8490fork f g a = (f a, g a)
@@ -131,30 +137,35 @@ stron :: (Either s s', b) -> Either (s, b) (s', b)
131137stron (e, b) = bimap (,b) (, b) e
132138
133139choosing :: 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
134−choosing l r = withLens l (\getl setl ->
135− withLens r (\getr setr ->
136− let g :: Either s s' -> a
137− g = either getl getr
140+choosing l r = withLensPrim l (\getl setl ->
141+ withLensPrim r (\getr setr ->
142+ let --g :: Either s s' -> a
143+ g e = case e of
144+ Left v -> let (c, v') = getl v in (Left c, v')
145+ Right v -> let (c, v') = getr v in (Right c, v')
138146 s = bimap setl setr . stron
139147 in lens g s))
140148
141−lens :: (s -> a) -> ((s,b) -> t) -> Lens s t a b
142−lens get _set = dimap (fork id get) _set . second'
149+lens :: (s -> (c,a)) -> ((c,b) -> t) -> Lens s t a b
150+lens get _set = dimap get _set . second'
151+{-# INLINE lens #-}
143152
144153------------------------------------------------------------------------------
145154
146−data ALens a b s t = ALens (s -> a) ((s, b) -> t)
155+data ALens a b s t = forall c . ALens (s -> (c,a)) ((c, b) -> t)
147156
148157instance Functor (ALens a b s) where
149158 fmap f (ALens _get _set) = ALens _get (f . _set)
150159
151160instance Profunctor (ALens a b) where
152− dimap f g (ALens get _set) = ALens (get . f) (g . _set . cross f id)
161+ dimap f g (ALens get _set) = ALens (get . f) (g . _set)
153162
154163instance Strong (ALens a b) where
155− second' (ALens get _set) = ALens (get . snd) (bimap id _set . assoc)
164+ second' (ALens get _set) = ALens get' set' --(bimap id _set . assoc)
156165 where
157− assoc ((a, b), c) = (a, (b, c))
166+ get' (c, a1) = let (c1, a) = get a1 in ((c, c1), a)
167+ set' ((c, c1), b) = (c, _set (c1, b))
168+ {-# INLINE second' #-}
158169
159170-- These are specialised versions of the Isos. On GHC 8.0.2, having
160171-- these functions eta-expanded allows the optimiser to inline these functions.