r/ProgrammingLanguages • u/thunderseethe • Feb 15 '26
Blog post How to Choose Between Hindley-Milner and Bidirectional Typing
https://thunderseethe.dev/posts/how-to-choose-between-hm-and-bidir/
90
Upvotes
17
u/Express-Guest-1061 Feb 15 '26
Thanks for expanding more on this. I am relatively new to compiler development, wanted to go beyond the basic compiler books and add generics. For me adding unification for handling generics was easiest to understand, bidirectional type checking was more difficult to understand, but this made me wanna give it a try.
10
u/mot_hmry Feb 15 '26
I believe you don't need unification to have generics, your type variables just need to be properly introduced (aka no hidden foralls.) I think you can even omit types on bindings so long as they're trivial.
So I'd probably ask "do you need to insert terms" to decide if you need unification.
28
u/augustss Feb 15 '26
The article claims that the
checkfunction could be implemented byinferfollowed by an equality test. This is only true if the language has decidable inference. E.g., some constructs in Haskell cannot be inferred, only checked.9
u/AustinVelonaut Admiran Feb 15 '26
One thing that I don't see mentioned in the article is how bidirectional type checking can help localize where errors are reported, leading to better error messages. I recently added some of this to my HM typechecker, where at the beginning of handling inference for a function definition, I look to see if the definition has a type specification (presumably from a user-supplied annotation), and if so, I bind the function's pattern variables to the supplied types from the type spec in the environment before I continue with recursive inference. Then, way down during unification of an inferred type with the type of the pattern variable, the check can occur right there, rather than unifying a type var and later checking it when we finally return to the top level defn infer. And indeed, it has helped with localization of error reporting.
3
u/FroyoSolid8414 Feb 15 '26
I found this a bit hard to read but I’m very much a beginner. Where would C++’s type system fit in this spectrum for context?
3
u/Tasty_Replacement_29 Bau Feb 16 '26
I personally find type inference (Hindley-Milner and Bidirectional Typing, and similar ones, eg. the one used in Rust) to be problematic, because of the "spooky action at a distance" which is sometimes called "Inference Horizon Problem": at the place where a variable is declared, it is not clear (for the human as well as for the compiler) which type it is. You have to read (or use the IDE) to find out what type it is.
Even worse, a code change, possibly many many lines further down, can then influence the type of the variable, and so cause issues.
I understand type inference might be look nicer, but in reality I think clarity is more important.
3
u/johnwcowan Feb 16 '26
I like the idea of having upward but not downward type inference: all constants manifestly typed and all external procedures required to have explicitly typed arguments. If additional explicit types are present, they are used for consistency checking. However, if the language has subtyping, then explicit types may be needed to give variables wider types.
2
u/jwm3 Feb 27 '26
I consider them all just specific cases of general constraint satisfaction problems. If you think of your type checker as going through your program and generating a list of constraints then the difference between hindley Milner, bidirectional typing, or many other systems is just the tactic you use to solve your generated constraint set and is completely divorced from your language itself.
Swapping out your inference algorithm is as easy as choosing a different constraint satisfaction algo or library and requires no changes to your language front end. If you are at all experimenting with type systems, I can highly recommend using this approach and implementing straightforward hm unification as a trivial initial solver and then when you have your front end kinks worked out, replace the hm constraint solver with a more interesting one. Being able to decouple your type system from your language front end pays off dividends when it comes to debugging and evolving your language.