top | item 7051611

Hindley-Milner in Clojure

142 points| ericn | 12 years ago |lispcast.com

77 comments

order
[+] exDM69|12 years ago|reply
Here's the Hindley-Milner implementation (in Haskell) from a toy compiler project of mine. It was really enlightening to write it and surprisingly simple.

https://github.com/rikusalminen/funfun/blob/master/FunFun/Ty...

This was also the first time I used monad transformers and almost the first non-IO monad application (I've used ST and Parsec before) I have dealt with. If you compare my code with the book source (Peter Hancock's type checker in Peyton-Jones' "Implementation of Functional Programming Languages", link in source code comment), my version using monads is a lot simpler to follow than the original, written in a pre-Haskell functional programming language called Miranda with no monads.

The type checker is a "pure function", it has inputs and outputs but no side-effects but in the code you need to 1) generate unique "names" 2) bail out early on type errors. I solved this problem using Error and State monads. The Miranda code used an infinite list of numbers for unique names and cumbersome tricks to handle type errors.

[+] sfvisser|12 years ago|reply
Nice job! Seems you could have put the TypeEnv in a ReaderT as well, might make things slightly easier.
[+] willismichael|12 years ago|reply
I find it curious that the venn diagram seems to indicate that a sizable subset of people who are familiar with type theory don't advocate either static typing or dynamic typing.
[+] aidenn0|12 years ago|reply
I would suspect that a large fraction of them are mathematicians and not particularly interested in how people program.
[+] technomancy|12 years ago|reply
I wouldn't say I'm intimately familiar with type theory, but I'm certainly old slash grumpy enough to not be an "advocate" of anything except learning both and thinking for yourself about the problem at hand.
[+] brudgers|12 years ago|reply
CS departments are where people exposed to Type Theory tend to be exposed to it. The sorts of languages that tend to be taken seriously in such departments [yes, I know there are exceptions] tend to be statically typed. Java and C++ just have more gravitas than Ruby and JavaScript and Lisps are for hippies.
[+] TeeWEE|12 years ago|reply
I'm familiar with type theorie but i'm not a proponent of either of them. Sometimes its better to use static typing and sometimes its better to use dynamic typing. Howeover most of the times I would prefer static typing.

I think in this subset you're mentioning?

[+] octo_t|12 years ago|reply
I love type theory, but I think a language like Sage[1] is probably the most interesting way forward.

[1] - http://sage.soe.ucsc.edu/

[+] zephjc|12 years ago|reply
I think that was the joke :)
[+] hardboiled|12 years ago|reply
Disagree about the idea that those who are unfamiliar with type theory prefer dynamic typing.

Typing preferences are usually due to trends in language usage having little to do with knowledge.

Plenty of java programmers use static typing without ever having to understand type theory.

But looking to history of language designers/implementers

Dan Friedman

Gilad Bracha http://www.infoq.com/presentations/functional-pros-cons

Guy Steele

Rich Hickey

All of these guys have worked on static languages, have a keener understanding of type theory than most, and yet they seem to promote dynamic languages at least when it comes to their pet languages.

[+] the_af|12 years ago|reply
I'm not disagreeing with you (that a lot of people go with what's trendy or what they already know), but I wouldn't put Gilad Bracha in the list of knowledgeable people. I've seen the talk you are linking to and it's not very impressive... he sounds mostly whiny to me. In his own blog, when he writes about about functional programming or type theory, he gets called out by the people who really know about it.
[+] coolsunglasses|12 years ago|reply
Gilad Bracha has no idea what the hell he's talking about.

Rich hasn't worked on static languages and I'm not familiar with him having done anything in type theory. He wanted a nicer, practical Lisp first and foremost. A helpful compiler wasn't high on his list of priorities.

Guy Steele's most recent work has involved functional, statically typed programming languages: http://en.wikipedia.org/wiki/Fortress_(programming_language)

One of Friedman's most recent books http://www.ccs.neu.edu/home/matthias/BTML/ was on ML which is a statically typed, functional programming language.

The smart people that weren't using static types back in the 70s and 80s weren't using them because the statically typed languages available back then were fuckin' awful except for ML and Miranda.

We can do a lot better as programmers these days. Stop giving yourself an excuse to not learn new things.

[+] kd0amg|12 years ago|reply
FWIW, I don't think the diagram is claiming that people who are unfamiliar with type theory tend to prefer dynamic typing, rather that people who prefer dynamic typing tend to be unfamiliar with type theory.
[+] rtfeldman|12 years ago|reply
The most useful takeaway from that graph is the insight into how many type theory aficionados look at the world.
[+] nabla9|12 years ago|reply
Why not both?

ghc compiler in Haskell has -fdefer-type-errors flag. SBCL Common Lisp implementation has option to turn type warnings into errors. Extending -fdefer-type-errors function and creating better type checker for dynamic languages could achieve best of both worlds.

[+] ufo|12 years ago|reply
The actual reason for why not both is that its actually very hard to do and is still kind of an open problem. Some things are easy to do statically but hard to check dynamically (for example, parametric polymorphism or type-based overloading and name resolution) and some things are easy to check dynamically but hard to specify using static types (for example, array bounds checking).
[+] emiljbs|12 years ago|reply
I agree. Good dynamic languages (Common Lisp, Smalltalk, Factor) has a lot going for them but static typing is also really nice. A mix between the two (preferably something that starts out as a dynamic lang and slowly moves towards being static) would be great (Common Lisp kinda does this with optional static typing).
[+] chongli|12 years ago|reply
Dynamic typing is just a special case of static typing where there is only one type!
[+] ufo|12 years ago|reply
While this is technically correct (the best kind of correct) it also trivializes the argument and somewhat misses the point. For example, in a dynamic language all the values are belong to a single type and carry a tag at runtime to identify their category. Static languages also do that but on more restricted scope - you aren't allowed to accidentally mix strings and integers in Haskell but you can accidentally mix empty/non-empty lists (getting a runtime exception on `head`) or zero/nonzero numbers (breaking division). You can sort of fix this if you go up to dependent types but then the type system gets much more complicated and its not always easy to statically create the proofs for everything so you start to see the appeal of dynamically checking some of the stuff.
[+] sfvisser|12 years ago|reply
Even when this is true, this special case of static typing gives you exactly none of the advantages of general static typing.
[+] dmytrish|12 years ago|reply
That's not quite true: there are many types, but the type checking is delayed into run time. Though your point of view may be also correct with respect to definitions (if you assume that types exist only during compile time).
[+] Dewie|12 years ago|reply
No. That sounds more like an untyped language.
[+] __--__|12 years ago|reply
All this talk about formal type theory, but where are the references to the relevant studies? Where's the data? The few studies[1][2][3][4] I've found are inconclusive one way or the other and none of them focus on error rates. I found another conversation about how to go about studying error rate in dynamically vs statically typed languages, but all I really found was this article studying the affect of hair style on language design[5].

[1] http://pleiad.dcc.uchile.cl/papers/2012/kleinschmagerAl-icpc... - maintainability

[2] http://dl.acm.org/citation.cfm?id=2047861&CFID=399382397&CFT... - development time

[3] https://courses.cs.washington.edu/courses/cse590n/10au/hanen... - development time, take 2

[4] http://pleiad.dcc.uchile.cl/papers/2012/mayerAl-oopsla2012.p... - usability

[5] http://z.caudate.me/language-hair-and-popularity/

[+] exDM69|12 years ago|reply
I glanced over the studies you linked to and in all of them, the languages used as examples of static typing are Java, C and C++. There's no mention of type inference or any languages that have a more advanced static typing scheme like ML or Haskell. A lot of it seemed to be a "Java vs. Ruby fight" with a slight bias towards the latter in the authors.

To joke and exaggerate a little, those studies seem to be done by people who belong to the "proponents of dynamic typing" and not "familiar with type theory" bin of people in the Venn diagram in the OP.

[+] jgg|12 years ago|reply
Are you trolling? I seriously can't tell...
[+] kd0amg|12 years ago|reply
I think you should implement Hindley-Milner in the language of your choice for a small toy λ-calculus.

Did this a little while ago (as a stepping stone to building an inference system for a more complicated calculus).

https://gist.github.com/jrslepak/6158954

[+] michaelochurch|12 years ago|reply
I'm familiar with type theory and (often) a proponent of dynamic typing.

It depends on what you're doing. If you're building cathedrals-- high-quality, performance-critical software that can never fail-- then static typing is a great tool, because it can do things that are very hard to do with unit testing, and you only pay the costs once in compilation. There are plenty of use cases in which I'd want to be using a statically typed language like OCaml (or, possibly, Rust).

If you're out in the bazaar-- say, building a web app that will have to contend with constant API changes and shifting needs, or building distributed systems designed to last decades without total failure (that may, like the Ship of Theseus, have all parts replaced) despite constant environmental change-- then dynamic typing often wins.

What I like about Clojure is that, being such a powerful language, you can get contracts and types and schemas but aren't bound to them. I like static typing in many ways, but Scala left me asking the question, any time someone insists that static typing is necessary: which static type system?

[+] hesselink|12 years ago|reply
I've heard this argument a lot, and I disagree. If your software is changing a lot, that is where types really shine. Refactoring is a breeze when you have types: you just change the code you want to improve, and all use sites are pointed to by the compiler. This is taken from daily experience: I work on a code base that is about 25K lines of Haskell and 35K lines of Javascript. Refactoring the Haskell is a pleasure. Refactoring the Javascript is something we dread, and always introduces bugs, some of which might linger for up to a year.
[+] coolsunglasses|12 years ago|reply
Hum, no.

I use Clojure at work and the single biggest drain on my productivity is a lack of a sensible, static type system. Yearn for Haskell big time.

I'd use Rust if I had to really get down and dirty, otherwise Haskell would be my first choice. OCaml isn't that great at all in practice.

[+] augustl|12 years ago|reply
Interesting insights. Would love to read more aobut this. Usually the arguments are "I'm a bad programmer like everyone else so I need verification" or "the world is dynamic".
[+] elwell|12 years ago|reply
Douglas Crockford is a proponent of dynamic typing. (At least from what I read in the beginning of "JavaScript: The Good Parts)
[+] Semiapies|12 years ago|reply
What's the relevance of that point? He doesn't appear to be mentioned in the post.