@@ -28,9 +28,9 @@ module Control.Optics.Linear.Internal
28
28
, over , over'
29
29
, traverseOf , traverseOf'
30
30
, toListOf , lengthOf
31
- , withIso , withPrism
31
+ , withIso , withLens , withPrism
32
32
-- * Constructing optics
33
- , iso , prism
33
+ , iso , lens , prism
34
34
)
35
35
where
36
36
@@ -42,6 +42,8 @@ import Data.Functor.Compose hiding (getCompose)
42
42
import Data.Functor.Linear
43
43
import qualified Data.Profunctor.Kleisli.Linear as Linear
44
44
import Data.Void
45
+ import GHC.Exts (FUN )
46
+ import GHC.Types
45
47
import Prelude.Linear
46
48
import qualified Prelude as P
47
49
@@ -68,6 +70,10 @@ assoc = iso Bifunctor.lassoc Bifunctor.rassoc
68
70
(.>) :: Optic_ arr a b s t -> Optic_ arr x y a b -> Optic_ arr x y s t
69
71
Optical f .> Optical g = Optical (f P. . g)
70
72
73
+
74
+ lens :: (s #-> (a , b #-> t )) -> Lens a b s t
75
+ lens k = Optical $ \ f -> dimap k (\ (x,g) -> g $ x) (first f)
76
+
71
77
prism :: (b #-> t ) -> (s #-> Either t a ) -> Prism a b s t
72
78
prism b s = Optical $ \ f -> dimap s (either id id ) (second (rmap b f))
73
79
@@ -112,9 +118,6 @@ set (Optical l) x = l (const x)
112
118
113
119
setSwap :: Optic_ (Linear. Kleisli (Compose (LinearArrow b ) ((,) a ))) a b s t -> s #-> b #-> (a , t )
114
120
setSwap (Optical l) s = getLA (getCompose (Linear. runKleisli (l (Linear. Kleisli (\ a -> Compose (LA (\ b -> (a,b)))))) s))
115
- where
116
- getCompose :: Compose f g a #-> f (g a )
117
- getCompose (Compose x) = x
118
121
119
122
match :: Optic_ (Market a b ) a b s t -> s #-> Either t a
120
123
match (Optical l) = snd (runMarket (l (Market id Right )))
@@ -149,3 +152,10 @@ withIso (Optical l) f = f fro to
149
152
withPrism :: Optic_ (Market a b ) a b s t -> ((b #-> t ) -> (s #-> Either t a ) -> r ) -> r
150
153
withPrism (Optical l) f = f b m
151
154
where Market b m = l (Market id Right )
155
+
156
+ withLens :: Optic_ (Linear. Kleisli (Compose ((,) a ) (FUN 'One b ))) a b s t -> s #-> (a , b #-> t )
157
+ withLens (Optical l) s = getCompose (Linear. runKleisli (l (Linear. Kleisli (\ a -> Compose (a, id )))) s)
158
+
159
+ -- linear variant of getCompose
160
+ getCompose :: Compose f g a #-> f (g a )
161
+ getCompose (Compose x) = x
0 commit comments