3 ms·
Hindley Milner type inference in 112 lines of Haskell: http://github.com/nostrademons/eve-language/tree/dc33a5a187bd05fcdeb239fce3a73b562ab2c844/bootstrap/Type
by nostrademons 18y ago
Hindley Milner type inference in 112 lines of Haskell:
http://github.com/nostrademons/eve-language/tree/dc33a5a187bd05fcdeb239fce3a73b562ab2c844/bootstrap/TypeCheck.hs http://github.com/nostrademons/eve-language/tree/dc33a5a187b...
I'm working on a constraint-typing algorithm now (like Haskell's) that has records and polymorphism. It's quite a bit harder, but vanilla Hindley-Milner makes it impossible to have any sort of subtyping or ad-hoc polymorphism, both of which are pretty necessary for practical programming.
If you're into this stuff, I'd check out the paper "Typing Haskell in Haskell", which presents a full implementation of Haskell's typechecker as a literate Haskell program.
- silentbicycle 18y agoHaskell has typeclasses (http://haskell.org/haskellwiki/A_brief_introduction_to_Haskell#Overloading http://haskell.org/haskellwiki/A_brief_introduction_to_Haske...), though. They allow inference of required type characteristics ("requires a type which can be sorted", "requires a numeric type", etc.), rather than type identity, which can solve many of the same problems.
- nostrademons 18y agoRight, that's part of what I'm trying to implement. They're well-covered in THiH. Typeclasses have some pitfalls though: in particular, they are not types, so you can't have a typeclass as a field in a data structure. (You can have them as part of the context of an explicit forall, eg. existential types, but those are more complicated than the average programmer can understand.) Bruno Oliveira and Martin Sulzmann have recently done some interesting work with unifying typeclasses and ordinary algebraic data types: http://www.comlab.ox.ac.uk/people/Bruno.Oliveira/objects.pdf http://www.comlab.ox.ac.uk/people/Bruno.Oliveira/objects.pdf But currently type inference for this is "left to a future paper", so it's not all that useful for my purposes. I suspect it may be undecidable, given the known undecidability results for System F-omega and other type systems with subtyping.