arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2603.23360cs.PL
收藏

让函数说话:通过域和范围类型实现轻量级参数多态

Let Functions Speak: Lightweight Parametric Polymorphism via Domain and Range Types

Purdue University(普渡大学)

Siyuan He, Songlin Jia, Tiark Rompf

AI总结:

本文提出F<:DR算法,通过引入域和范围投影类型Dom(T)和Range(T),实现轻量级参数多态,补充而非替代传统箭头应用,通过子类型在System F<:中保持有效性。

摘要:

动态语言如Python和JavaScript广泛使用函数装饰器扩展行为。在TypeScript中,常用Parameters<T>和ReturnType<T>来类型化此类模式。实际上,这种惯用法依赖于通过不安全类型any表达的函数类型界限,削弱了静态保证。核心是一种标准的类型化原则:只有当被调用者暴露为箭头类型时,应用才被正当化。我们提出F<:DR,一种添加域和范围投影类型的算法,允许对任意类型T进行类型化应用:Dom(T)类型的参数证明了可调用性,结果被类型化为Range(T)。此设计补充而非替代传统箭头应用,通过子类型在System F<:中保持有效性。我们用Rocq实现F<:DR,并通过逻辑关系与路径选择证明语义类型安全性,该技术也扩展到其他投影类型,如原始对,即积类型。

英文摘要:

Subtyping provides polymorphism at a low cost: a concise type such as Int $\to$ Top covers every function that accepts an integer, whatever it returns, whereas the parametric alternative $\forall B.\,$ Int $\to$ B spends a quantifier and a type parameter to express the same. This cognitive overhead compounds as types nest. Subtyping, however, is not always enough: upcasting to Int $\to$ Top forgets the result type, so a program that needs the precise result must return to full parameterization. The missing middle ground is a lightweight form of polymorphism in the spirit of Hofmann and Pierce's type destructors and TypeScript's Parameters<T> and ReturnType<T>, which keeps the concise types of subtyping yet recovers the precision of parametricity. However, Hofmann and Pierce did not address contravariant positions such as function arguments, and TypeScript's utility types rely on the unsound `any` type. To address this gap, we present $F^{DR}_{<:}$, an extension of System $F_{<:}$ with first-class domain and range projection types, Dom(T) and Range(T), and an application rule that types f(x) from f : F and x : Dom(F) for an arbitrary type F. The demand that F be exposed as an arrow type is delayed from the definition site to the call site and discharged by the argument itself, as a value of type Dom(F) exists only when F is a subtype of an arrow type. A function type variable then needs no informative bound, only $<:$ Top, while its applications still type precisely, reaching the middle ground through a boundless quantification. We prove semantic type soundness and weak normalization by logical relations, where path selection delays a projection's interpretation until the underlying arrow type is resolved. The same machinery extends to product projections, which cooperate with domain and range in one system. All are mechanized in Rocq.

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

↑