@@ -53,15 +53,21 @@ set f (s, b)
5353 = f (const b) s
5454
5555view :: Lens s s a a -> s -> a
56−view l = withLens l (\get _ -> get)
56+view l = withLensPrim l (\get _ -> snd . get)
5757
58−withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
59−withLens l k =
58+--withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
59+--ithLens l k =
60+-- case l idLens of
61+-- ALens _get _set -> k (snd . _get) (\(s, b) -> _set ((fst $ _get s), b))
62+
63+withLensPrim :: Lens s t a b -> (forall c . (s -> (c,a)) -> ((c, b) -> t) -> r) -> r
64+withLensPrim l k =
6065 case l idLens of
6166 ALens _get _set -> k _get _set
6267
6368idLens :: ALens a b a b
64−idLens = ALens id snd
69+idLens = ALens (fork (const ()) id) snd
70+{-# INLINE idLens #-}
6571
6672infixr 4 .~
6773(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
@@ -79,12 +85,12 @@ build p = unTagged . p . Tagged
7985-- | Lens focusing on the first element of a product
8086first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
8187first
82− = lens (\(a :*: _) -> a) (\(_ :*: b, a') -> a' :*: b)
88+ = lens (\(a :*: b) -> (b,a)) (\(b, a') -> a' :*: b)
8389
8490-- | Lens focusing on the second element of a product
8591second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
8692second
87− = lens (\(_ :*: b) -> b) (\(a :*: _, b') -> a :*: b')
93+ = lens (\(a :*: b) -> (a,b)) (\(a, b') -> a :*: b')
8894
8995fork :: (a -> b) -> (a -> c) -> a -> (b, c)
9096fork f g a = (f a, g a)
@@ -137,30 +143,35 @@ stron :: (Either s s', b) -> Either (s, b) (s', b)
137143stron (e, b) = bimap (,b) (, b) e
138144
139145choosing :: 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
140−choosing l r = withLens l (\getl setl ->
141− withLens r (\getr setr ->
142− let g :: Either s s' -> a
143− g = either getl getr
146+choosing l r = withLensPrim l (\getl setl ->
147+ withLensPrim r (\getr setr ->
148+ let --g :: Either s s' -> a
149+ g e = case e of
150+ Left v -> let (c, v') = getl v in (Left c, v')
151+ Right v -> let (c, v') = getr v in (Right c, v')
144152 s = bimap setl setr . stron
145153 in lens g s))
146154
147−lens :: (s -> a) -> ((s,b) -> t) -> Lens s t a b
148−lens get _set = dimap (fork id get) _set . second'
155+lens :: (s -> (c,a)) -> ((c,b) -> t) -> Lens s t a b
156+lens get _set = dimap get _set . second'
157+{-# INLINE lens #-}
149158
150159------------------------------------------------------------------------------
151160
152−data ALens a b s t = ALens (s -> a) ((s, b) -> t)
161+data ALens a b s t = forall c . ALens (s -> (c,a)) ((c, b) -> t)
153162
154163instance Functor (ALens a b s) where
155164 fmap f (ALens _get _set) = ALens _get (f . _set)
156165
157166instance Profunctor (ALens a b) where
158− dimap f g (ALens get _set) = ALens (get . f) (g . _set . cross f id)
167+ dimap f g (ALens get _set) = ALens (get . f) (g . _set)
159168
160169instance Strong (ALens a b) where
161− second' (ALens get _set) = ALens (get . snd) (bimap id _set . assoc)
170+ second' (ALens get _set) = ALens get' set' --(bimap id _set . assoc)
162171 where
163− assoc ((a, b), c) = (a, (b, c))
172+ get' (c, a1) = let (c1, a) = get a1 in ((c, c1), a)
173+ set' ((c, c1), b) = (c, _set (c1, b))
174+ {-# INLINE second' #-}
164175
165176-- These are specialised versions of the Isos. On GHC 8.0.2, having
166177-- these functions eta-expanded allows the optimiser to inline these functions.