Great question! You're touching on a subtle and interesting point about function types, invertibility, and the symmetry of input/output types in functional programming and type theory.
Restating the problem
You have a function:
f :: a -> b -> (a, b)
which is invertible. Its inverse, logically, would be:
f' :: (a, b) -> (a, b)
But this is asymmetric: the original function is curried with two inputs and one output (a pair), while the inverse is uncurried with one input (a pair) and one output (a pair).
If instead you start with:
f :: (a, b) -> (a, b)
then the inverse naturally has the same type, which is symmetric and aesthetically pleasing.
The tension: currying vs. symmetry
- Currying:
a -> b -> c is isomorphic to (a, b) -> c but not equal in type.
- Invertibility: If
f is invertible, then f is a bijection between its input and output types.
- Symmetry: For invertibility, the input and output types must be isomorphic (or equal), so the inverse has the same type shape.
Why the asymmetry?
- The curried form
a -> b -> (a, b) is a function taking two arguments sequentially, returning a pair.
- The inverse
f' must take a pair (a, b) and return a pair (a, b).
- The difference arises because the original function's input is two separate arguments, but the output is a pair.
- The inverse must "undo" the pairing, so it naturally takes a pair as input.
Is there a theory that addresses this?
Yes, several areas of theory and practice touch on this:
1. Isomorphisms in Category Theory
- In category theory, an isomorphism between objects
A and B is a pair of morphisms f: A -> B and f⁻¹: B -> A such that f⁻¹ . f = id_A and f . f⁻¹ = id_B.
- If you consider the category Hask (Haskell types and functions), the isomorphisms correspond to invertible functions.
- The key is that the domain and codomain must be isomorphic types.
- Currying is an isomorphism between
a -> b -> c and (a, b) -> c, but they are not equal types.
- So invertibility is naturally expressed on the uncurried form
(a, b) -> (a, b) for symmetry.
2. Linear/Invertible/ Reversible Programming Languages
- Languages like Theseus, Janus, or Invertible Haskell explore invertible functions explicitly.
- They often require functions to be expressed in a form where input and output types are the same or isomorphic.
- This often means working with tuples or records rather than curried arguments, to maintain symmetry and invertibility.
3. Dependent Types and Sigma Types
- In dependently typed languages (Agda, Idris), you can express functions with multiple outputs more naturally using dependent pairs (Σ-types).
- The inverse can then be expressed as a function on these dependent pairs, maintaining symmetry.
- However, this is more expressive than Haskell's type system.
4. Multi-Return and Multi-Argument Functions
- The asymmetry arises because Haskell functions are fundamentally unary (one argument).
- Multi-argument functions are just curried unary functions.
- Multi-return values are usually encoded as tuples or algebraic data types.
- If you want symmetry, you want a function from a product type to a product type.
Can we have a "nice" theory of multiple return types and invertibility?
- Yes, but it usually involves treating functions as morphisms between product types (tuples, records).
- The "nice" invertible functions are those that are isomorphisms between product types.
- Currying is a convenience for partial application, but it breaks the symmetry needed for invertibility.
- So the "natural" setting for invertible functions with multiple inputs and outputs is uncurried functions on product types.
Summary
- The asymmetry you observe is a fundamental consequence of currying vs. uncurrying.
- Invertible functions naturally live between isomorphic types, so
(a, b) -> (a, b) is the natural form.
- Currying is an isomorphism but not equality, so invertibility is more naturally expressed on uncurried functions.
- Theories of invertible programming, category theory, and dependently typed programming all support this view.
- If you want to keep currying and invertibility, you must accept the asymmetry or explicitly encode the isomorphisms.
Further reading
- "Invertible Programming" by James Cheney et al.
- Category Theory for Programmers by Bartosz Milewski (especially sections on isomorphisms and currying)
- Papers on Reversible Computing and Invertible Functional Programming
- Dependent type theory texts on Σ-types and isomorphisms
If you want, I can help you explore a concrete example or a library that supports invertible functions in Haskell or another language!