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 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.
The type system of Haskell, when extended with parametric type classes (or multi-parameter type classes with functional dependencies), has enough power to mimic polymorphic variants. This paper, first, explains how to encode polymorphic variants in Haskell's type system (Haskell 98 + popular extensions). However, this encoding of polymorphic variants are rarely used in practice. This is probably because it is quite tedious for programmers to write mimic codes by hand and because the problem of ambiguity would embarrass programmers.
Therefore, the paper proposes an extension of Haskell's type classes that supports polymorphic variants directly. This type system can produce vanilla Haskell codes as a result of type inference. Therefore it behaves as a preprocessor which translate the extended language into plain old Haskell. Programmers would be able to use polymorphic variants without worrying nasty problems such as ambiguities.
The translated codes above are reformatted (indented) by hand (and some variables are renamed) for readability.
RISE, Kagawa University
2217-20 Hayashi-cho Takamatsu, Kagawa, 761-0396 JAPAN