Mach7: Pattern Matching for C++
github.com
github.com
This was really interesting to me, because I had never thought of pattern matching and the Visitor pattern as being "competitors", so-to-speak. I guess this would mainly apply in the case that your pattern matching is based on type.
I was further intrigued because this argument (pattern matching is better than Visitor) seems to be the opposite argument made in the Google C++ Style Guide, which I usually agree with. The Google C++ Style Guide argues against using RTTI to do type-based dispatch (https://google-styleguide.googlecode.com/svn/trunk/cppguide....):
"Decision trees based on type are a strong indication that your code is on the wrong track. Code such as this usually breaks when additional subclasses are added to the class hierarchy. Moreover, when properties of a subclass change, it is difficult to find and modify all the affected code segments."
It's very interesting to me that there is such wide disagreement here on which of these two patterns represents better design.
(Better language history buffs than me, feel free to show prior art)
http://c2.com/cgi/wiki?ExpressionProblem
The name comes from choosing the best way to represent a datatype for expressions in a programming language. The pattern-matching approach makes it easy to define new functions on top of ASTs and the OO-approach makes it easy to add new kinds of node to the tree but its really hard to make a system that is easy to extend in both directions.
Robin Milner, you had it all sussed out in the early 1970s, it took the rest of the world about 40 years to catch up!
1. A program that typechecks cannot have type errors during runtime. Dynamic type (i.e. runtime tag) based functions like instanceof break this, and most (all?) OOP languages have it in some form.
2. Type inference: the types of expressions can be unified to the most general type that works for all of them. I don't know any popular language that is able to do this.
Most typing I see added to languages these days are cherry-picking consequences of the HM algorithm. C++'s auto keyword for example is a very simple version of type inference, type hints in dynamic languages aren't checked (or checkable) statically, and when runtime tags are involved it's hard to speak of static type inference.
I don't think type hints do Hindley/Milner/Damas justice.
I agree. Retro-fitting Hindley-Milner would be difficult. Rpython has type-inference for a subset of Python. But it's a special purpose language. I don't know any popular language that is able to do this.
Wikipedia lists ML and Haskell - are they missing some detail?(Without type classes, type inference is decidable in Haskell.)
That said, in practise, HM type inference tends to be very fast, because the worst-case behaviour only shows up in rather artificial programs that human programmers would normally not write. If you bound the depth of let-nesting, HM is polynomial.
[0] http://nickdesaulniers.github.io/blog/2013/05/07/rust-patter...
Although admittedly that's from an older talk, no idea if the current syntax still reflects that sentiment.
"In the future, we would like to implement an actual language extension that will be capable of working with open patterns. Given such an extension and its implementation, we would like to look into how code for such patterns can be optimized without hardcoding the knowledge of the semantics of the patterns into the compiler. We would also like to experiment with other kinds of patterns (including those defined by the user), look at the interaction of patterns with both the standard library and other facilities in the language, and make views less ad-hoc."