Polymorphic variants in Haskell

Koji Kagawa
{"title":"Polymorphic variants in Haskell","authors":"Koji Kagawa","doi":"10.1145/1159842.1159848","DOIUrl":null,"url":null,"abstract":"In languages that support polymorphic variants, a single variant value can be passed to many contexts that accept different sets of constructors. Polymorphic variants can be used in order to introduce extensible algebraic datatypes into functional programming languages and are potentially useful for application domains such as interpreters, graphical user interface (GUI) libraries and database interfaces, where the number of necessary constructors cannot be determined in advance. Very few functional languages, however, have a mechanism to extend existing datatypes by adding new constructors. In general, for polymorphic variants to be useful, we would need some mechanisms to reuse existing functions and extend them for new constructors.Actually, the type system of Haskell, when extended with parametric type classes (or multi-parameter type classes with functional dependencies), has enough power not only to mimic polymorphic variants but also to extend existing functions for new constructors.This paper, first, explains how to do this in Haskell's type system (Haskell 98 with popular extensions). However, this encoding of polymorphic variants is difficult to use in practice. This is because it is quite tedious for programmers to write mimic codes by hand and because the problem of ambiguous overloading resolution would embarrass programmers. Therefore, the paper proposes an extension of Haskell's type classes that supports polymorphic variants directly. It has a novel form of instance declarations where records and variants are handled symmetrically.This type system can produce vanilla Haskell codes as a result of type inference. Therefore it behaves as a preprocessor which translates the extended language into plain Haskell. Programmers would be able to use polymorphic variants without worrying nasty problems such as ambiguities.","PeriodicalId":188691,"journal":{"name":"ACM SIGPLAN Symposium/Workshop on Haskell","volume":"22 1","pages":"0"},"PeriodicalIF":0.0000,"publicationDate":"2006-09-17","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"5","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"ACM SIGPLAN Symposium/Workshop on Haskell","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/10.1145/1159842.1159848","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
引用次数: 5

Abstract

In languages that support polymorphic variants, a single variant value can be passed to many contexts that accept different sets of constructors. Polymorphic variants can be used in order to introduce extensible algebraic datatypes into functional programming languages and are potentially useful for application domains such as interpreters, graphical user interface (GUI) libraries and database interfaces, where the number of necessary constructors cannot be determined in advance. Very few functional languages, however, have a mechanism to extend existing datatypes by adding new constructors. In general, for polymorphic variants to be useful, we would need some mechanisms to reuse existing functions and extend them for new constructors.Actually, the type system of Haskell, when extended with parametric type classes (or multi-parameter type classes with functional dependencies), has enough power not only to mimic polymorphic variants but also to extend existing functions for new constructors.This paper, first, explains how to do this in Haskell's type system (Haskell 98 with popular extensions). However, this encoding of polymorphic variants is difficult to use in practice. This is because it is quite tedious for programmers to write mimic codes by hand and because the problem of ambiguous overloading resolution would embarrass programmers. Therefore, the paper proposes an extension of Haskell's type classes that supports polymorphic variants directly. It has a novel form of instance declarations where records and variants are handled symmetrically.This type system can produce vanilla Haskell codes as a result of type inference. Therefore it behaves as a preprocessor which translates the extended language into plain Haskell. Programmers would be able to use polymorphic variants without worrying nasty problems such as ambiguities.
查看原文
分享 分享
微信好友 朋友圈 QQ好友 复制链接
本刊更多论文
Haskell中的多态变体
在支持多态变体的语言中,可以将单个变体值传递给接受不同构造函数集的许多上下文。多态变体可以用于在函数式编程语言中引入可扩展的代数数据类型,并且对于解释器、图形用户界面(GUI)库和数据库接口等应用程序领域具有潜在的用途,在这些领域中无法预先确定必要的构造函数的数量。然而,很少有函数式语言具有通过添加新的构造函数来扩展现有数据类型的机制。一般来说,要使多态变体有用,我们需要一些机制来重用现有函数并为新的构造函数扩展它们。实际上,Haskell的类型系统,当使用参数类型类(或带有函数依赖的多参数类型类)进行扩展时,不仅有足够的能力模拟多态变体,而且还可以为新的构造函数扩展现有的函数。本文首先解释了如何在Haskell的类型系统(Haskell 98和流行的扩展)中做到这一点。然而,这种多态变体的编码在实际应用中是困难的。这是因为程序员手工编写模拟代码非常乏味,而且模糊的重载解析问题会让程序员感到尴尬。因此,本文提出了对Haskell类型类的扩展,直接支持多态变体。它有一种新颖的实例声明形式,其中记录和变量被对称地处理。作为类型推断的结果,这个类型系统可以产生普通的Haskell代码。因此,它作为一个预处理器,将扩展语言翻译成普通的Haskell。程序员将能够使用多态变体,而不必担心诸如歧义之类的讨厌问题。
本文章由计算机程序翻译,如有差异,请以英文原文为准。
求助全文
约1分钟内获得全文 去求助
来源期刊
自引率
0.00%
发文量
0
期刊最新文献
Mio: a high-performance multicore io manager for GHC Understanding idiomatic traversals backwards and forwards An EDSL approach to high performance Haskell programming Causality of optimized Haskell: what is burning our cycles? Splittable pseudorandom number generators using cryptographic hashing
×
引用
GB/T 7714-2015
复制
MLA
复制
APA
复制
导出至
BibTeX EndNote RefMan NoteFirst NoteExpress
×
×
提示
您的信息不完整,为了账户安全,请先补充。
现在去补充
×
提示
您因"违规操作"
具体请查看互助需知
我知道了
×
提示
现在去查看 取消
×
提示
确定
0
微信
客服QQ
Book学术公众号 扫码关注我们
反馈
×
意见反馈
请填写您的意见或建议
请填写您的手机或邮箱
已复制链接
已复制链接
快去分享给好友吧!
我知道了
×
扫码分享
扫码分享
Book学术官方微信
Book学术文献互助
Book学术文献互助群
群 号:481959085
Book学术
文献互助 智能选刊 最新文献 互助须知 联系我们:info@booksci.cn
Book学术提供免费学术资源搜索服务,方便国内外学者检索中英文文献。致力于提供最便捷和优质的服务体验。
Copyright © 2023 Book学术 All rights reserved.
ghs 京公网安备 11010802042870号 京ICP备2023020795号-1