lens achieves this fused traversal using the confusing combinator, which I'm always amused to note, is very aptly named.
confusing ::
Applicative f =>
LensLike (Curried (Yoneda f) (Yoneda f)) s t a b ->
LensLike f s t a b
It is also usually used when you make heavy use of Generic, and would otherwise be round-tripping over and over through an unknown Applicative in separate passes for no good reason.
fusing is a simpler version of this that only handles lens-fusion. You can also do a Coyoneda version of it, which can be better in some circumstances. IIRC, dfeuer used that in containers at some point.
I needed a little more work on top of that to really get the same Core as DeriveTraversable. For recursive traversals, that's different from specializing traverse to Curry 'On' Yoneda. "Undoing" the specialization before calling the traversal recursively can make it lazier.
If I parse you correctly the 'undoing the specialization before recursing' thing usually comes out of using this as part of a definition of plate, so the recursive bits are handled mostly outside. I wrote fusing and later on u/glguy obsessed about finding ways to handle traversal fusion until this combinator finally popped out.
Yeah, plate is a more principled way of handling recursion. The subtlety in generic traverse is that it doesn't distinguish recursive types from others. For example:
data T a = C (T a) a (T a)
The natural definition of traverse is to traverse the three fields in order, agnostic to the fact that the fields contain recursive occurrences of T a, whereas plate is going to ignore the middle field and (AFAICT) it's not possible to inject it back in the traversal as a separate step.
lensachieves this fused traversal using theconfusingcombinator, which I'm always amused to note, is very aptly named.It is also usually used when you make heavy use of
Generic, and would otherwise be round-tripping over and over through an unknown Applicative in separate passes for no good reason.I think glguy wrote up details on this in his "Boggle" type, too:
https://github.com/glguy/generic-traverse/blob/master/src/Boggle.hs
Thanks for the reference!
I saw it at the time and I probably didn't get it so I forgot about it.
I edited the post to add references to it.
I am seeking other examples like Boggle, if you or u/edwardkmett have any.. I'd be grateful.
fusingis a simpler version of this that only handleslens-fusion. You can also do aCoyonedaversion of it, which can be better in some circumstances. IIRC, dfeuer used that incontainersat some point.Oh yes, that's exactly it!
I needed a little more work on top of that to really get the same Core as
DeriveTraversable. For recursive traversals, that's different from specializingtraversetoCurry 'On' Yoneda. "Undoing" the specialization before calling the traversal recursively can make it lazier.If I parse you correctly the 'undoing the specialization before recursing' thing usually comes out of using this as part of a definition of
plate, so the recursive bits are handled mostly outside. I wrotefusingand later on u/glguy obsessed about finding ways to handle traversal fusion until this combinator finally popped out.Yeah,
plateis a more principled way of handling recursion. The subtlety in generictraverseis that it doesn't distinguish recursive types from others. For example:The natural definition of
traverseis to traverse the three fields in order, agnostic to the fact that the fields contain recursive occurrences ofT a, whereasplateis going to ignore the middle field and (AFAICT) it's not possible to inject it back in the traversal as a separate step.I have a more complex version to handle the special cases needed to match the hand written code that uses things like fmap when possible: https://github.com/glguy/generic-traverse/blob/master/src/Boggle.hs