|
|
|
|
|
by rntz
3253 days ago
|
|
To me the most interesting part of this article is the part that nobody else seems to be commenting on: the ability to automatically invert a function, even a programmer-defined function. That seems magical! What if I define a hash function? How can it possibly invert that? Clearly there's something interesting going on behind the scenes, and I wish the article discussed what it is, and how (and when) it works. Everything else in the article seems like plain old functional programming to me, with a little syntax sugar here and there. |
|
It’s not that the compiler can invert a hash, or automatically derive a logarithm function from an exponentiation function, or anything like that.
Essentially, if you have a composition of functions in a “pipeline” (h ∘ g ∘ f):
Then to get the inverse of the composition, you just invert each function and reverse the whole thing (f⁻¹ ∘ g⁻¹ ∘ h⁻¹): “undo” is defined for a set of primitive words. If there isn’t an inverse defined for some function you used in the composition, then it fails; however, you can just define a custom inverse for your function. And obviously this relies on having some kind of reified representation of functions available.“undo” can be used for things like pattern-matching: a destructuring function, which takes some value and produces its fields, is the inverse of a constructor, which takes the fields and constructs a value.