论类型类领域中的一致性状态
On the State of Coherence in the Land of Type Classes
AI总结:
本文考察Swift、Rust和Scala中类型类的一致性处理,并与Haskell对比,发现三者绕过实例唯一性限制的策略惊人相似。
摘要:
类型类是一种流行的工具,用于实现通用算法和数据结构而不损失效率,从而弥合了参数多态和特设多态之间的差距。自从在 Haskell 中首次开发以来,它们现在已在许多其他工业级编程语言中占据重要地位,特别是包括 Swift、Rust 和 Scala。类型类的成功在很大程度上取决于编译器通过类型导向解析来推断隐式参数参数的能力。这种技术有时被称为**隐式编程**,它允许用户省略语言实现可以从上下文中推导出的信息,例如特定类型类的实现。\n 隐式编程的一个缺点是,类型导向解析可能产生模糊的结果,从而威胁到一致性,即有效程序恰好具有一种含义的属性。这个问题使社区在解决它的正确方法上产生了分歧。一方主张灵活性,其中隐式解析是上下文敏感的,并且通常依赖依赖类型特性来维护健全性。另一方则认为上下文不应妨碍等式推理,并且通常要求类型类实例在整个程序中是唯一的,以抵御模糊性。\n 尽管存在大量关于类型类和隐式编程的工作,但大多数学术文献集中在少数选定的语言上,对其他主流项目提供的见解很少。与此同时,后者以不同的名称演化出类似的特性和/或限制,使得语言用户和设计者难以了解完整的设计空间。为了缓解这个问题,我们着手检查 Swift、Rust 和 Scala,这三种广泛使用类型类的流行语言,并将它们的一致性方法与 Haskell 的方法联系起来。事实证明,除了表面的语法差异之外,Swift、Rust 和 Haskell 实际上惊人地相似,因为这三种语言提供了类似的策略来绕过类型类实例唯一性的限制。
英文摘要:
Type classes are a popular tool for implementing generic algorithms and data structures without loss of efficiency, bridging the gap between parametric and ad-hoc polymorphism. Since their initial development in Haskell, they now feature prominently in numerous other industry-ready programming languages, notably including Swift, Rust, and Scala. The success of type classes hinges in large part on the compilers' ability to infer arguments to implicit parameters by means of a type-directed resolution. This technique, sometimes dubbed **implicit programming**, lets users elide information that the language implementation can deduce from the context, such as the implementation of a particular type class. One drawback of implicit programming is that a type-directed resolution may yield ambiguous results, thereby threatening coherence, the property that valid programs have exactly one meaning. This issue has divided the community on the right approach to address it. One side advocates for flexibility where implicit resolution is context-sensitive and often relies on dependent typing features to uphold soundness. The other holds that context should not stand in the way of equational reasoning and typically imposes that type class instances be unique across the entire program to fend off ambiguities. Although there exists a large body of work on type classes and implicit programming, most of the scholarly literature focuses on a few select languages and offers little insight into other mainstream projects. Meanwhile, the latter have evolved similar features and/or restrictions under different names, making it difficult for language users and designers to get a sense of the full design space. To alleviate this issue, we set to examine Swift, Rust, and Scala, three popular languages featuring type classes heavily, and relate their approach to coherence to Haskell's. It turns out that, beyond superficial syntactic differences, Swift, Rust, and Haskell are actually strikingly similar in that the three languages offer comparable strategies to work around the limitations of the uniqueness of type class instances.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。