136 points · by krat0sprakhar · 10y ago · 28 comments
It's a little unfortunate that this version doesn't include let-polymorphism, since that's necessary for parametrically polymorphic functions to be actually polymorphic, and it's not a recent addition — it's in the original Damas-Milner paper. All the other stuff about tuples, lists, sum types, and whatnot can be emulated with functions, but not let-polymorphism, not without a potentially exponential blowup in the program size.
I'm assuming a lot developers like me would be keen to understand how HM works, so I was considering writing a detailed tutorial of the unification algorithm step-by-step. Would you be interested in something along those lines?
[0] - http://www.cs.columbia.edu/~sedwards/classes/2016/4115-sprin...
[1] - http://dev.stephendiehl.com/fun/006_hindley_milner.html
So, yes! I'd love a detailed tutorial of unification step-by-step (or perhaps recursive function by recursive function, if that turns out to be simpler). Maybe it already exists somewhere, perhaps in a textbook, and I just haven't found it. Or maybe I got intimidated and need to calm down and work some exercises instead of going off to read Facebook and HN.
The second thing I would like to say. And I've said this before. Languages like Ocaml and Haskell (never mind Agda, Coq, what have you) already have too much typing 'smarts' built into them. I would like to see -- hint, I'm probably going to have to get my hands dirty! -- an implementation in either a dynamically typed language like Ruby/Python/Perl/PHP/Javascript/… or a non-functional language like C/C++/Obj-C/…
What think ye?
Yes! That would be very cool to read.
I've been working on an implementation of Mini-ML in Python using Robert Small's Hindley-Milner in Python [0] (and also eagerly awaiting the rest of Diehl's series on writing a Haskell).
I understand the fundamentals of the algorithm, but even now I don't think I could yet implement it from scratch. I'd love to read a detailed tutorial.
By the way, those Cornell lecture notes you referenced have also been very helpful to me.
0. http://smallshire.org.uk/sufficientlysmall/2010/04/11/a-hind...
[1]. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.65....
He was studying computer science at the University of Illinois, and at one point he asked me in private message if I was interested in OCaml. I said sure, I've been playing around with it. Then he said he was on a bender and asked if I wanted to do his homework for him... due in a few days...
The assignment was to implement the Hindley-Milner type inference algorithm in OCaml. And that's how I got started with learning about type theory and language implementation. Thanks, guy, whereever you are. Last I heard you were in the military.
Same. When I was just starting to learn about type systems, I found it very useful to have the concepts explained alongside an implementation, to see how they fit together. I don’t think I’ve ever gone so quickly from “I have no idea how this works” to “I could write this from memory” as I did when following that tutorial.
Damas-Milner type inference uses an "occurs check" to prevent the type equation solver from unifying a type variable with a compound type expression containing the same variable. The "obviously correct" way to perform this check is eagerly - as soon as possible.
However, performance-wise, delaying the occurs check can speed up the inference process - and this is exactly what OCaml does. The downside is that the resulting algorithm is quite involved, because the solver can now run into type expressions containing cycles, so naively recursively walking type expressions can cause an infinite loop.
[0]: https://github.com/amnn/typed_geomlab/blob/c79c4bbb3179ef66b...
https://www21.in.tum.de/~nipkow/TRaAT/
From there it is a simple means to form and solve the type equations needed for Hindley Milner.
http://smallshire.org.uk/sufficientlysmall/2010/04/11/a-hind...
Here is the paper we did: http://www1.cs.columbia.edu/~sedwards/classes/2013/w4115-fal... and the full information is available here: http://www1.cs.columbia.edu/~sedwards/classes/2013/w4115-fal... our project was called "pubCrawl".