Skip to content

Commit 0d5029f

Browse files
committed
more efficient traversal
1 parent a7e68fc commit 0d5029f

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

src/Control/Optics/Linear/Internal.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ traversal :: (s ->. Batch a b t) -> Traversal a b s t
189189
traversal h = Optical (\k -> dimap h fuse (traverse' k))
190190

191191
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))
192+
traverse' k = dimap fromBatch toBatch (second (k *** traverse' k))
193193

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)
194+
fromBatch :: Batch a b t ->. Either t (a, Batch a b (b ->. t))
195+
fromBatch (Done t) = Left t
196+
fromBatch (More l x) = Right (l, x)
197197

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
198+
toBatch :: Either t (a, Batch a b (b ->. t)) ->. Batch a b t
199+
toBatch (Left t) = Done t
200+
toBatch (Right (l, x)) = More l x
201201

202202
traversed :: Traversable t => Traversal a b (t a) (t b)
203203
traversed = traversal (traverse batch)

src/Data/Functor/Linear/Internal/Traversable.hs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,28 @@ instance Control.Applicative (StateR s) where
8080
where go :: (a, (a ->. b, s)) ->. (b, s)
8181
go (a, (h, s'')) = (h a, s'')
8282

83-
data Batch a b c = P c | Batch a b (b ->. c) :*: a
83+
data Batch a b c = Done c | More a (Batch a b (b ->. c))
8484
deriving (Data.Functor, Data.Applicative) via Control.Data (Batch a b)
85+
8586
instance Control.Functor (Batch a b) where
86-
fmap f (P c) = P (f c)
87-
fmap f (u :*: a) = Control.fmap (f.) u :*: a
87+
fmap f (Done c) = Done (f c)
88+
fmap f (More x l) = More x ((f.) Control.<$> l)
8889

8990
instance Control.Applicative (Batch a b) where
90-
pure = P
91-
P f <*> P x = P (f x)
92-
(u :*: a) <*> P x = ((P $ help x) Control.<*> u) :*: a
93-
u <*> (v :*: a) = (P (.) Control.<*> u Control.<*> v) :*: a
94-
95-
help :: d ->. ((b ->. d ->. e) ->. b ->. e)
96-
help d bde b = bde b d
91+
pure = Done
92+
Done f <*> l' = Control.fmap f l'
93+
More x l <*> l' = More x (flip Control.<$> l Control.<*> l')
9794

9895
batch :: a ->. Batch a b b
99-
batch x = P id :*: x
96+
batch x = More x (Done id)
10097

10198
runWith :: Control.Applicative f => (a ->. f b) -> Batch a b c ->. f c
102-
runWith _ (P x) = Control.pure x
103-
runWith f (u :*: x) = runWith f u Control.<*> f x
99+
runWith _ (Done c) = Control.pure c
100+
runWith f (More x l) = runWith f l Control.<*> f x
104101

105102
fuse :: Batch b b t ->. t
106-
fuse (P i) = i
107-
fuse (u :*: x) = fuse u x
103+
fuse (Done i) = i
104+
fuse (More x l) = fuse l x
108105

109106
------------------------
110107
-- Standard instances --

src/Prelude/Linear.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ maybe _ f (Just y) = f y
8383
forget :: (a ->. b) ->. a -> b
8484
forget f x = f x
8585

86-
-- | Replacement for the flip function with generalized multiplicities.
87-
flip :: (a -->.(p) b -->.(q) c) -->.(r) b -->.(q) a -->.(p) c
88-
flip f b a = f a b
89-
9086
-- | Linearly typed replacement for the standard '(Prelude.<*)' function.
9187
(<*) :: (Data.Applicative f, Consumable b) => f a ->. f b ->. f a
9288
fa <* fb = Data.fmap (flip lseq) fa Data.<*> fb

src/Prelude/Linear/Internal/Simple.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ foldr :: (a ->. b ->. b) -> b ->. [a] ->. b
5656
foldr f z = \case
5757
[] -> z
5858
x:xs -> f x (foldr f z xs)
59+
60+
-- | Replacement for the flip function with generalized multiplicities.
61+
flip :: (a -->.(p) b -->.(q) c) -->.(r) b -->.(q) a -->.(p) c
62+
flip f b a = f a b

0 commit comments

Comments
 (0)