@@ -47,15 +47,21 @@ set f (s, b)
47 = f (const b) s
47 = f (const b) s
49view :: Lens s s a a -> s -> a
49view :: Lens s s a a -> s -> a
50−view l = withLens l (\get _ -> get)
50+view l = withLensPrim l (\get _ -> snd . get)
52−withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
52+--withLens :: Lens s t a b -> ((s -> a) -> ((s, b) -> t) -> r) -> r
53−withLens l k =
53+--ithLens l k =
55+-- ALens _get _set -> k (snd . _get) (\(s, b) -> _set ((fst $ _get s), b))
57+withLensPrim :: Lens s t a b -> (forall c . (s -> (c,a)) -> ((c, b) -> t) -> r) -> r
54 case l idLens of
59 case l idLens of
55 ALens _get _set -> k _get _set
60 ALens _get _set -> k _get _set
57idLens :: ALens a b a b
62idLens :: ALens a b a b
58−idLens = ALens id snd
63+idLens = ALens (fork (const ()) id) snd
60infixr 4 .~
66infixr 4 .~
61(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
67(.~) :: ((a -> b) -> s -> t) -> b -> s -> t
@@ -73,12 +79,12 @@ build p = unTagged . p . Tagged
73-- | Lens focusing on the first element of a product
79-- | Lens focusing on the first element of a product
74first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
80first :: Lens ((a :*: b) x) ((a' :*: b) x) (a x) (a' x)
76− = lens (\(a :*: _) -> a) (\(_ :*: b, a') -> a' :*: b)
82+ = lens (\(a :*: b) -> (b,a)) (\(b, a') -> a' :*: b)
78-- | Lens focusing on the second element of a product
84-- | Lens focusing on the second element of a product
79second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
85second :: Lens ((a :*: b) x) ((a :*: b') x) (b x) (b' x)
81− = lens (\(_ :*: b) -> b) (\(a :*: _, b') -> a :*: b')
87+ = lens (\(a :*: b) -> (a,b)) (\(a, b') -> a :*: b')
83fork :: (a -> b) -> (a -> c) -> a -> (b, c)
89fork :: (a -> b) -> (a -> c) -> a -> (b, c)
84fork f g a = (f a, g a)
90fork f g a = (f a, g a)
@@ -131,30 +137,35 @@ stron :: (Either s s', b) -> Either (s, b) (s', b)
131stron (e, b) = bimap (,b) (, b) e
137stron (e, b) = bimap (,b) (, b) e
133choosing :: 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
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
134−choosing l r = withLens l (\getl setl ->
140+choosing l r = withLensPrim l (\getl setl ->
135− withLens r (\getr setr ->
141+ withLensPrim r (\getr setr ->
136− let g :: Either s s' -> a
142+ let --g :: Either s s' -> a
137− g = either getl getr
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')
138 s = bimap setl setr . stron
146 s = bimap setl setr . stron
139 in lens g s))
147 in lens g s))
141−lens :: (s -> a) -> ((s,b) -> t) -> Lens s t a b
149+lens :: (s -> (c,a)) -> ((c,b) -> t) -> Lens s t a b
142−lens get _set = dimap (fork id get) _set . second'
150+lens get _set = dimap get _set . second'
144------------------------------------------------------------------------------
153------------------------------------------------------------------------------
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)
148instance Functor (ALens a b s) where
157instance Functor (ALens a b s) where
149 fmap f (ALens _get _set) = ALens _get (f . _set)
158 fmap f (ALens _get _set) = ALens _get (f . _set)
151instance Profunctor (ALens a b) where
160instance 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)
154instance Strong (ALens a b) where
163instance 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)
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' #-}
159-- These are specialised versions of the Isos. On GHC 8.0.2, having
170-- These are specialised versions of the Isos. On GHC 8.0.2, having
160-- these functions eta-expanded allows the optimiser to inline these functions.
171-- these functions eta-expanded allows the optimiser to inline these functions.