@@ -13,17 +13,16 @@ module Control.Optics.Linear.Internal
13
13
, Iso , Iso'
14
14
, Lens , Lens'
15
15
, Prism , Prism'
16
- , PTraversal , PTraversal'
17
- , DTraversal , DTraversal'
16
+ , Traversal , Traversal'
18
17
-- * Composing optics
19
18
, (.>)
20
19
-- * Common optics
21
20
, swap , assoc
22
21
, _1 , _2
23
22
, _Left , _Right
24
23
, _Just , _Nothing
25
- , ptraversed , dtraversed
26
- , both , both'
24
+ , traversed
25
+ , both
27
26
-- * Using optics
28
27
, get , set , gets
29
28
, set' , set''
@@ -32,8 +31,8 @@ module Control.Optics.Linear.Internal
32
31
, over , over'
33
32
, traverseOf , traverseOf'
34
33
, lengthOf
35
- , withIso , withLens , withPrism
36
34
, toListOf
35
+ , withIso , withLens , withPrism , withTraversal
37
36
-- * Constructing optics
38
37
, iso , prism , lens
39
38
)
@@ -42,6 +41,7 @@ module Control.Optics.Linear.Internal
42
41
import qualified Control.Arrow as NonLinear
43
42
import qualified Data.Bifunctor.Linear as Bifunctor
44
43
import qualified Control.Monad.Linear as Control
44
+ import Data.Functor.Linear.Internal.Traversable
45
45
import Data.Bifunctor.Linear (SymmetricMonoidal )
46
46
import Data.Monoid.Linear
47
47
import Data.Functor.Const
@@ -66,12 +66,8 @@ type Lens a b s t = Optic (Strong (,) ()) a b s t
66
66
type Lens' a s = Lens a a s s
67
67
type Prism a b s t = Optic (Strong Either Void ) a b s t
68
68
type Prism' a s = Prism a a s s
69
- type PTraversal a b s t = Optic PWandering a b s t
70
- type PTraversal' a s = PTraversal a a s s
71
- type DTraversal a b s t = Optic DWandering a b s t
72
- type DTraversal' a s = DTraversal a a s s
73
- -- XXX: these will unify into
74
- -- type Traversal (p :: Multiplicity) a b s t = Optic (Wandering p) a b s t
69
+ type Traversal a b s t = Optic Traversing a b s t
70
+ type Traversal' a s = Traversal a a s s
75
71
76
72
swap :: SymmetricMonoidal m u => Iso (a `m ` b ) (c `m ` d ) (b `m ` a ) (d `m ` c )
77
73
swap = iso Bifunctor. swap Bifunctor. swap
@@ -97,13 +93,8 @@ _1 = Optical first
97
93
_2 :: Lens a b (c ,a ) (c ,b )
98
94
_2 = Optical second
99
95
100
- -- XXX: these will unify to
101
- -- > both :: forall (p :: Multiplicity). Traversal p a b (a,a) (b,b)
102
- both' :: PTraversal a b (a ,a ) (b ,b )
103
- both' = _Pairing .> ptraversed
104
-
105
- both :: DTraversal a b (a ,a ) (b ,b )
106
- both = _Pairing .> dtraversed
96
+ both :: Traversal a b (a ,a ) (b ,b )
97
+ both = _Pairing .> traversed
107
98
108
99
-- XXX: these are a special case of Bitraversable, but just the simple case
109
100
-- is included here for now
@@ -118,10 +109,6 @@ instance P.Functor Pair where
118
109
fmap f (Paired (x,y)) = Paired (f x, f y)
119
110
instance Functor Pair where
120
111
fmap f (Paired (x,y)) = Paired (f x, f y)
121
- instance Foldable Pair where
122
- foldMap f (Paired (x,y)) = f x P. <> f y
123
- instance P. Traversable Pair where
124
- traverse f (Paired (x,y)) = Paired P. <$> ((,) P. <$> f x P. <*> f y)
125
112
instance Traversable Pair where
126
113
traverse f (Paired (x,y)) = Paired <$> ((,) <$> f x <*> f y)
127
114
@@ -140,12 +127,6 @@ _Just = prism Just (maybe (Left Nothing) Right)
140
127
_Nothing :: Prism' () (Maybe a )
141
128
_Nothing = prism (\ () -> Nothing ) Left
142
129
143
- ptraversed :: P. Traversable t => PTraversal a b (t a ) (t b )
144
- ptraversed = Optical pwander
145
-
146
- dtraversed :: Traversable t => DTraversal a b (t a ) (t b )
147
- dtraversed = Optical dwander
148
-
149
130
over :: Optic_ LinearArrow a b s t -> (a ->. b ) -> s ->. t
150
131
over (Optical l) f = getLA (l (LA f))
151
132
@@ -168,7 +149,7 @@ set'' :: Optic_ (NonLinear.Kleisli (Control.Reader b)) a b s t -> b ->. s -> t
168
149
set'' (Optical l) b s = Control. runReader (NonLinear. runKleisli (l (NonLinear. Kleisli (const (Control. reader id )))) s) b
169
150
170
151
set :: Optic_ (-> ) a b s t -> b -> s -> t
171
- set ( Optical l) x = l (const x )
152
+ set l b = over' l (const b )
172
153
173
154
match :: Optic_ (Market a b ) a b s t -> s ->. Either t a
174
155
match (Optical l) = snd (runMarket (l (Market id Right )))
@@ -203,3 +184,23 @@ withIso (Optical l) f = f fro to
203
184
withPrism :: Optic_ (Market a b ) a b s t -> ((b ->. t ) -> (s ->. Either t a ) -> r ) -> r
204
185
withPrism (Optical l) f = f b m
205
186
where Market b m = l (Market id Right )
187
+
188
+ traversal :: (s ->. Batch a b t ) -> Traversal a b s t
189
+ traversal h = Optical (\ k -> dimap h fuse (traverse' k))
190
+
191
+ traverse' :: (Strong Either Void arr , Monoidal (,) () arr ) => a `arr ` b -> Batch a c t `arr ` Batch b c t
192
+ traverse' k = dimap out inn (second (traverse' k *** k))
193
+
194
+ out :: Batch a b t ->. Either t (Batch a b (b ->. t ), a )
195
+ out (P t) = Left t
196
+ out (l :*: x) = Right (l,x)
197
+
198
+ inn :: Either t (Batch a b (b ->. t ), a ) ->. Batch a b t
199
+ inn (Left t) = P t
200
+ inn (Right (l,x)) = l :*: x
201
+
202
+ traversed :: Traversable t => Traversal a b (t a ) (t b )
203
+ traversed = traversal (traverse batch)
204
+
205
+ withTraversal :: Optic_ (Linear. Kleisli (Batch a b )) a b s t -> s ->. Batch a b t
206
+ withTraversal (Optical l) = Linear. runKleisli (l (Linear. Kleisli batch))
0 commit comments