arXivDaily arXiv每日学术速递 周一至周五更新
arXiv周末暂无论文更新,休息一下吧,周末愉快~~
arXiv 2511.10343cs.PL
收藏

面向机器学习的全方位类型推断:无论何种方式的主性

Omnidirectional type inference for ML: principality any way

University of Cambridge(剑桥大学);INRIA(法国国家数字与信息技术研究所);IRIF, Université Paris Cité(巴黎西岱大学信息学研究所)

Alistair O'Brien, Didier Rémy, Gabriel Scherer

AI总结:

本文提出面向机器学习的全方位类型推断方法,通过动态类型信息流动恢复主性,解决传统静态推断在处理脆弱特性时的局限,展示其在OCaml记录标签和数据构造器静态重载及半显式一阶多态中的应用。

摘要:

Damas-Hindley-Milner (ML) 类型系统之所以成功,归功于主性,即每个正确类型表达式都有唯一最一般类型。这使得推断可预测且高效。然而,许多 ML 扩展(如 GADTs、高阶多态和静态重载)通过引入_脆弱_构造威胁主性。现有方法通过方向性推断算法恢复主性,但静态推断顺序的刚性常导致正确类型程序被拒绝。本文提出_全方位_类型推断,类型信息以动态顺序流动。类型约束可按任意顺序解决,通过_暂停匹配约束_暂停和恢复,使用_暂停匹配约束_。此方法对简单类型系统易于实现,但扩展到 ML 有挑战,因 let-泛化。现有 ML 推断算法以固定顺序处理 let 绑定:类型 e1,泛化其类型,然后类型 e2。为克服此问题,引入_增量实例化_,允许部分解决的类型方案包含暂停约束被实例化,通过机制逐步更新实例。全方位推断提供恢复主性的通用框架,展示其在 OCaml 记录标签和数据构造器静态重载及半显式一阶多态中的应用,均获得比 OCaml 当前类型检查器更丰富的主类型推断算法。

英文摘要:

The Damas-Hindley-Milner (ML) type system owes its success to principality, the property that every well-typed expression has a unique most general type. This makes inference predictable and efficient. Unfortunately, many extensions of ML (GADTs, higher-rank polymorphism, and static overloading) endanger princpality by introducing _fragile_ constructs that resist principal inference. Existing approaches recover principality through directional inference algorithms, which propagate _known_ type information in a fixed (or static) order (e.g. as in bidirectional typing) to disambiguate such constructs. However, the rigidity of a static inference order often causes otherwise well-typed programs to be rejected. We propose _omnidirectional_ type inference, where type information flows in a dynamic order. Typing constraints may be solved in any order, suspending when progress requires known type information and resuming once it becomes available, using _suspended match constraints_. This approach is straightforward for simply typed systems, but extending it to ML is challenging due to let-generalization. Existing ML inference algorithms type let-bindings (let x = e1 in e2) in a fixed order: type e1, generalize its type, and then type e2. To overcome this, we introduce _incremental instantiation_, allowing partially solved type schemes containing suspended constraints to be instantiated, with a mechanism to incrementally update instances as the scheme is refined. Omnidirectionality provides a general framework for restoring principality in the presence of fragile features. We demonstrate its versatility on two fundamentally different features of OCaml: static overloading of record labels and datatype constructors and semi-explicit first-class polymorphism. In both cases, we obtain a principal type inference algorithm that is more expressive than OCaml's current typechecker.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑