The Mondrian introduction to functional optics

https://news.ycombinator.com/rss Hits: 8
Summary

In this post I’d like to try to discuss what functional optics are, without going too much into why they are so cool, and you should use them, or how they are implemented and should be used with a specific language and library. I personally think that functional optics should be a really easy concept to grasp, but currently learning them is harder than it should be mostly due to library implementation details, quite obscure documentation and an exotic usage of weird symbols. Since a picture is worth a thousand words, I will introduce and use a graphical notation to illustrate the concepts we will discuss. Types and values Let’s start introducing our graphical notation from its basic building blocks. We can represent a type with a simple coloured rectangle A value for a given type will be represented as a horizontal line spanning the width of the rectangle Sums and products When considering algebraic data types, we have two ways of combining types, using products and sums. The product of two types A and B is a new type, which we will denote by A*B, whose values are composed of a value of type A and a value of type B. An example of a product type is a tuple like (Int, String) where each value is pair composed of an integer and a string. Graphically we can represent a product type as two side by side rectangles When it comes to values, we need to upgrade a little bit our graphical interpretation. Since a value in a product type is composed of values of its components, we will just represent it as piecewise horizontal line, composed by horizontal lines (possible at different heights) spanning its horizontal sub-rectangles. On the other hand, the sum of two types is represented by two rectangles one on top of the other A value of a sum type is a horizontal line spanning the width of the whole rectangle. If it is a horizontal line in the top rectangle, it means that we are selecting the first type, and we’re using one of its values. If it is a horizontal line in the botto...

First seen: 2025-10-07 11:09

Last seen: 2025-10-07 18:10