@@ -53,15 +53,21 @@ set f (s, b)
53 = f (const b) s
53 = f (const b) s
55view :: Lens s s a a -> s -> a
55view :: Lens s s a a -> s -> a
56−view l = withLens l (\get _ -> get)
56+view l = withLensPrim l (\get _ -> snd . get)
58−withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
58+--withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
59−withLens l k =
59+--ithLens l k =
61+-- ALens _get _set -> k (snd . _get) (\(s, b) -> _set ((fst $ _get s), b))
63+withLensPrim :: Lens s t a b -> (forall c . (s -> (c,a)) -> ((c, b) -> t) -> r) -> r
60 case l idLens of
65 case l idLens of
61 ALens _get _set -> k _get _set
66 ALens _get _set -> k _get _set
63idLens :: ALens a b a b
68idLens :: ALens a b a b
64−idLens = ALens id snd
69+idLens = ALens (fork (const ()) id) snd
66infixr 4 .~
72infixr 4 .~
67(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
73(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
@@ -79,12 +85,12 @@ build p = unTagged . p . Tagged
79-- | Lens focusing on the first element of a product
85-- | Lens focusing on the first element of a product
80first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
86first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
82− = lens (\(a :*: _) -> a) (\(_ :*: b, a') -> a' :*: b)
88+ = lens (\(a :*: b) -> (b,a)) (\(b, a') -> a' :*: b)
84-- | Lens focusing on the second element of a product
90-- | Lens focusing on the second element of a product
85second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
91second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
87− = lens (\(_ :*: b) -> b) (\(a :*: _, b') -> a :*: b')
93+ = lens (\(a :*: b) -> (a,b)) (\(a, b') -> a :*: b')
89fork :: (a -> b) -> (a -> c) -> a -> (b, c)
95fork :: (a -> b) -> (a -> c) -> a -> (b, c)
90fork f g a = (f a, g a)
96fork f g a = (f a, g a)
@@ -137,30 +143,35 @@ stron :: (Either s s', b) -> Either (s, b) (s', b)
137stron (e, b) = bimap (,b) (, b) e
143stron (e, b) = bimap (,b) (, b) e
139choosing :: 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
145choosing :: 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 ->
146+choosing l r = withLensPrim l (\getl setl ->
141− withLens r (\getr setr ->
147+ withLensPrim r (\getr setr ->
142− let g :: Either s s' -> a
148+ let --g :: Either s s' -> a
143− g = either getl getr
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')
144 s = bimap setl setr . stron
152 s = bimap setl setr . stron
145 in lens g s))
153 in lens g s))
147−lens :: (s -> a) -> ((s,b) -> t) -> Lens s t a b
155+lens :: (s -> (c,a)) -> ((c,b) -> t) -> Lens s t a b
148−lens get _set = dimap (fork id get) _set . second'
156+lens get _set = dimap get _set . second'
150------------------------------------------------------------------------------
159------------------------------------------------------------------------------
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)
154instance Functor (ALens a b s) where
163instance Functor (ALens a b s) where
155 fmap f (ALens _get _set) = ALens _get (f . _set)
164 fmap f (ALens _get _set) = ALens _get (f . _set)
157instance Profunctor (ALens a b) where
166instance 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)
160instance Strong (ALens a b) where
169instance 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)
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' #-}
165-- These are specialised versions of the Isos. On GHC 8.0.2, having
176-- These are specialised versions of the Isos. On GHC 8.0.2, having
166-- these functions eta-expanded allows the optimiser to inline these functions.
177-- these functions eta-expanded allows the optimiser to inline these functions.