Short Question: What is the importance of isomorphic functions in programming (namely in functional programming)?
Long Question: I’m trying to draw some analogs between functional programming and concepts in Category Theory based off of some of the lingo I hear from time-to-time. Essentially I’m trying to “unpackage” that lingo into something concrete I can then expand on. I’ll then be able to use the lingo with an understanding of just-what-the-heck-I’m-talking about. Which is always nice.
One of these terms I hear all the time is Isomorphism, I gather this is about reasoning about equivalence between functions or function compositions. I was wondering if someone could provide some insights into some common patterns where the property of isomorphism comes in handy (in functional programming), and any by-products gained, such as compiler optimizations from reasoning about isomorphic functions.
I take a little issue with the upvoted answer for isomorphism, as the category theory definition of isomorphism says nothing about objects. To see why, let’s review the definition.
Definition
An isomorphism is a pair of morphisms (i.e. functions),
fandg, such that:These morphisms are then called “iso”morphisms. A lot of people don’t catch that the “morphism” in isomorphism refers to the function and not the object. However, you would say that the objects they connect are “isomorphic”, which is what the other answer is describing.
Notice that the definition of isomorphism does not say what (
.),id, or=must be. The only requirement is that, whatever they are, they also satisfy the category laws:Composition (i.e. (
.)) joins two morphisms into one morphism andiddenotes some sort of “identity” transition. This means that if our isomorphisms cancel out to the identity morphismid, then you can think of them as inverses of each other.For the specific case where the morphisms are functions, then
idis defined as the identity function:… and composition is defined as:
… and two functions are isomorphisms if they cancel out to the identity function
idwhen you compose them.Morphisms versus objects
However, there are multiple ways two objects could be isomorphic. For example, given the following two types:
There are two isomorphisms between them:
So that’s why it’s better to describe the isomorphism in terms of the specific functions relating the two objects rather than the two objects, since there may not necessarily be a unique pair of functions between two objects that satisfy the isomorphism laws.
Also, note that it is not sufficient for the functions to be invertible. For example, the following function pairs are not isomorphisms:
Even though no information is lost when you compose
f1 . g2, you don’t return back to your original state, even if the final state has the same type.Also, isomorphisms don’t have to be between concrete data types. Here’s an example of two canonical isomorphisms are not between concrete algebraic data types and instead simply relate functions:
curryanduncurry:Uses for Isomorphisms
Church Encoding
One use of isomorphisms is to Church-encode data types as functions. For example,
Boolis isomorphic toforall a . a -> a -> a:Verify that
f . g = idandg . f = id.The benefit of Church encoding data types is that they sometimes run faster (because Church-encoding is continuation-passing style) and they can be implemented in languages that don’t even have language support for algebraic data types at all.
Translating Implementations
Sometimes one tries to compare one library’s implementation of some feature to another library’s implementation, and if you can prove that they are isomorphic, then you can prove that they are equally powerful. Also, the isomorphisms describe how to translate one library into the other.
For example, there are two approaches that provide the ability to define a monad from a functor’s signature. One is the free monad, provided by the
freepackage and the other is operational semantics, provided by theoperationalpackage.If you look at the two core data types, they look different, especially their second constructors:
… but they are actually isomorphic! That means that both approaches are equally powerful and any code written in one approach can be translated mechanically into the other approach using the isomorphisms.
Isomorphisms that are not functions
Also, isomorphisms are not limited to functions. They are actually defined for any
Categoryand Haskell has lots of categories. This is why it’s more useful to think in terms of morphisms rather than data types.For example, the
Lenstype (fromdata-lens) forms a category where you can compose lenses and have an identity lens. So using our above data type, we can define two lenses that are isomorphisms:Note that there are two isomorphisms in play. One is the isomorphism that is used to build each lens (i.e.
f1andg1) (and that’s also why that construction function is callediso), and then the lenses themselves are also isomorphisms. Note that in the above formulation, the composition (.) used is not function composition but rather lens composition, and theidis not the identity function, but instead is the identity lens:Which means that if we compose our two lenses, the result should be indistinguishable from that identity lens.