变体的变体
Variations on Variants
摘要:
可扩展变体提高了程序设计语言的模块性和表达能力:它们允许将程序功能分解为独立的块,并允许使用现有数据类型的新情况和对这些数据类型的新操作来无缝扩展现有代码。本文考虑了在Haskell中提供可扩展变体的三种方法。行类型是一种长期理解的用于对可扩展记录和变量进行类型化的机制,但它的采用需要扩展Haskell的核心类型系统。或者,我们可能希望根据现有机制(如类型类)对可扩展变体进行编码。我们描述了一个编码的可扩展的变体,使用实例链,建议扩展的类系统。与许多以前的可扩展变量编码不同,我们的编码不需要为每个使用变量的函数定义新的类型类。最后,我们将我们的编码转换为使用封闭类型族,这是GHC的一个现有功能。这样做演示了封闭类型族中实例链和函数依赖项的解释。像我们这样的编码的一个问题是,它们与编码系统的匹配程度如何。我们比较我们的编码的表现力与彼此和系统的基础上行类型。我们发现,虽然等价的条款在每个系统中的类型,这两个编码需要明确的类型注释,以解决不存在行类型系统中的类型的歧义,类型家族实现保留更多的约束,在主类型比实例链实现。我们提出了一个通用的机制来指导歧义类型变量的实例化,表明它消除了我们的编码类型注释的需要,并讨论了条件下,它保持一致性。
英文摘要:
Extensible variants improve the modularity and expressiveness of programming languages: they allow program functionality to be decomposed into independent blocks, and allow seamless extension of existing code with both new cases of existing data types and new operations over those data types. This paper considers three approaches to providing extensible variants in Haskell. Row typing is a long understood mechanism for typing extensible records and variants, but its adoption would require extension of Haskell's core type system. Alternatively, we might hope to encode extensible variants in terms of existing mechanisms, such as type classes. We describe an encoding of extensible variants using instance chains, a proposed extension of the class system. Unlike many previous encodings of extensible variants, ours does not require the definition of a new type class for each function that consumes variants. Finally, we translate our encoding to use closed type families, an existing feature of GHC. Doing so demonstrates the interpretation of instances chains and functional dependencies in closed type families. One concern with encodings like ours is how completely they match the encoded system. We compare the expressiveness of our encodings with each other and with systems based on row types. We find that, while equivalent terms are typable in each system, both encodings require explicit type annotations to resolve ambiguities in typing not present in row type systems, and the type family implementation retains more constraints in principal types than does the instance chain implementation. We propose a general mechanism to guide the instantiation of ambiguous type variables, show that it eliminates the need for type annotations in our encodings, and discuss conditions under which it preserves coherence.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。