home mail me! RSS (2.0) feed

Algebraic + abstract = true! (at least in the key of F#)

I earlier wrote about some ideas I had to combine the encapsulation of abstract data types with the swift case analysis plus decomposition of algebraic types. Why not have both worlds?

Ok, when looking more into F# - after a few years of trying to stay away from it - I realize that it does have such facilities, i.e., using the beauty of pattern matching with non-syntactical thingies. They call it Active Patterns. This article is pretty good. Read it, cause it is a really powerful, and apparently often overseen, feature.

Why did not anyone tell me?! You could have saved me hours of thinking about it! Ok, my informal proposal is deeper - of course ;-) - but nevertheless.

davber said,

December 10, 2007 @ 4:24 pm

Well, for the alert reader, we have had a quite common “active pattern” before, in the form of the “n+k” pattern, where the inverse operation is performed on-demand - i.e., subtraction - to get the “k” part. One language that does implement that pattern is Haskell.

davber said,

December 10, 2007 @ 9:14 pm

Inversion…

This is the key to have syntactically - via patterns - decomposable types: to have a homomorphism from the states to the pattern-matching categories, often represented by a (nested or not) structure of constructors. In the “n+k” case it is actually an isomorphism, so no special instructions as to how to get the state from the matching case is necessary. And, in the algebraic type case, the isomorphism even reduces to identity… In the GENERAL case, it is not that easy, and one needs both an inverse from syntactical pattern to a REPRESENTATIVE of states AND further logic, using a guard or similar, to get to the exact state. I.e., one defines an inverse that embeds the patterns into the set of possible states, which then gets to the proper case, after which one needs a “pre-processing” to get to the actual state. I should think about this some day :-)

RSS feed for comments on this post · TrackBack URI

Leave a Comment

You must be logged in to post a comment.