如何选择 Hindley-Milner 和双向类型系统
How to Choose Between Hindley-Milner and Bidirectional Typing

原始链接: https://thunderseethe.dev/posts/how-to-choose-between-hm-and-bidir/

## HM 与双向类型系统:为你的语言提出的错误问题 关于 Hindley-Milner (HM) 和双向 (Bidir) 类型系统的争论,常常被框定为二选一的选择,但这是一种错误的二分法。核心问题不是 *选择哪个* 系统,而是 *你的语言是否需要泛型*。 泛型需要合一——推断和求解类型变量的过程(例如 Rust 中的 `Vec`)。HM 本质上包含合一。然而,双向类型系统并不局限于缺乏合一;它 *支持* 合一,使其成为 HM 的超集。你可以实现一个带有或不带有合一的双向系统,提供灵活性。 如果没有泛型,一个更简单的依赖于类型注解的双向系统就足够了,尤其是在学习练习或领域特定语言 (DSL) 中,在这些情况下,最小化复杂性是关键。然而,对于通用语言,泛型越来越被认为是必不可少的,从而推动了对合一的需求。 最终,双向类型系统提供了一种更具适应性的方法。选择它并不会排除合一,并且允许你根据语言的特定需求定制系统——无论是一个功能齐全的泛型语言,还是一个精简的、注重注解的 DSL。

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 如何选择 Hindley-Milner 和双向类型 (thunderseethe.dev) 9 分,thunderseethe 发表于 1 小时前 | 隐藏 | 过去的 | 收藏 | 讨论 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

This question is common enough you’ve probably heard it posed countless times: “Should my new programming language use a Hindley-Milner (HM) type system or a Bidirectional (Bidir) one?” What’s that? I need to understand friends don’t just bring up type inference in casual conversation?

OK, ouch, fair enough. But…whatever. This is my blog. We’re doing it anyway! I don’t know what you expected when you clicked on a programming languages blog.

Picking a type system is a real barrier for would be language developers. Eyes full of trepidation as they navigate the labyrinth of nuanced choice that goes into everything a programming language asks of them. Which type system to choose is just another quandary in the quagmire as they trudge towards a working prototype.

Its understandable they’d want to make a quick decision and return to marching. But this is the wrong question to ask. The question presumes that HM and Bidir are two ends of a spectrum. On one end you have HM with type variables and unification and all that jazz. On the other end you have bidirectional typing where annotations decide your types and little inference is involved. This spectrum, however, is a false dichotomy.

What folks should actually be asking is “Does my language need generics?”. This question frames the problem around what your language needs, rather than an arbitrary choice between two algorithms of abstract tradeoffs. Perhaps more importantly, it determines if you’ll need unification or not.

Generics, generally, require a type system that supports unification. Unification is the process of assigning and solving type variables. If you’ve ever seen Rust infer a type like Vec<T>, that’s unification chugging along.