Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is anamorphism related to lens?

How is the Lens, the record accessor, e.g.

http://hackage.haskell.org/packages/archive/lens/3.9.0.2/doc/html/Control-Lens-Type.html#t:Lens

related to anamorphism? e.g.

http://hackage.haskell.org/packages/archive/recursion-schemes/3.0.0.2/doc/html/Data-Functor-Foldable.html#v:ana

Can you make the former an instance of the latter?

I've recently been using lenses a lot. Lenses are in many ways crucial in Haskell programming; they are tools for record access, stateful programs... In hope of better understanding the lenses, I started reading "Bananas, Lenses, Envelopes and Barbed Wire" paper. With help of an "INSIDE 206-105", I slowly re-implemented the -morphisms in Haskell. Then the above question occured to me.

like image 226
nushio Avatar asked Jun 19 '13 17:06

nushio


1 Answers

There is absolutely no connection between the lenses in Bird's SQUIGGOL formalism and the modern notion of lenses as functional references.

Recursion schemes and functional references are unrelated concepts.

This is just an unfortunate coincidence in terminology. It is unfortunate in that it often means that when people see the names they try to go back "to the source" and wind up terribly confused!

If you want to find early papers on the topic of lenses, you'd be better served by going off to read the papers by Benjamin Pierce on Boomerang. In there he defines several notions of lenses. The ones that correspond to the form of functional references used in Haskell would be what he calls a "very well behaved lens". He then proceeds to generalize them in a different direction than the path I take in the lens package. He focuses on using them to restore invariants, while "very well behaved" lenses just don't break the invariants in the first place.

The term "lens" in the functional reference sense refers to the fact that it looks at part of a whole. The term "lens" in a recursion scheme sense refers to the fact that [( and )] syntactically look kind of like concave lenses.

tl;dr They have nothing to do with one another.

like image 51
Edward Kmett Avatar answered Oct 21 '22 20:10

Edward Kmett