Type classes [19, 7] are, according to some authors, ‘the most beloved feature of Haskell’. They provide a clean, modular and elegant way of writing overloaded functions. Type classes are usually implemented using dictionaries—data structures containing functions and other dictionaries—which are passed as extra arguments to the overloaded functions. Thus, programs using type classes are translated into programs with dictionaries that are correct according to the DamasMilner type system. Functional logic programming (FLP) has inherited the Damas-Milner type system from its functional part, so it may seem that using the same translation for these languages is also a good option. However, this translation presents some problems in FLP because of an undesired excess of sharing that appears with non-determinism. In this paper we propose an alternative translation of type classes which uses type witnesses and type-indexed functions (in the sense of [11]) instead of dictionaries. The translated programs are correct according to a new simple extension of the Damas-Milner type system recently proposed for FLP [13]. Apart from solving the mentioned problem of undesired sharing, the proposed translation also obtains interesting efficiency results: in functional languages (GHC, Hugs) it can compete with the built-in translation, and in FLP (Toy, Curry) it obtains an important speedup—between 1.2 ∗This work has been partially supported by the Spanish projects TIN2008-06622-C03-01 and UCMBSCH-GR58/08-910502. and 2.5 in the experiments—compared to the classical translation.
[1]
F. J. López-Fraguas.
Generic Functional Logic Programs
,
2010
.
[2]
Enrique Martin-Martin,et al.
Advances in Type Systems for Functional Logic Programming
,
2008
.
[3]
Robin Milner,et al.
Principal type-schemes for functional programs
,
1982,
POPL '82.
[4]
Michael Hanus,et al.
Curry: an integrated functional logic language (version 0
,
2003
.
[5]
Francisco Javier López-Fraguas,et al.
TOY: A Multiparadigm Declarative System
,
1999,
RTA.
[6]
James Cheney,et al.
First-Class Phantom Types
,
2003
.
[7]
J. C. Gonz Alez-Moreno,et al.
An Approach to Declarative Programming Based on a Rewriting Logic
,
2022
.
[8]
Michael Hanus,et al.
Multi-paradigm Declarative Languages
,
2007,
ICLP.
[9]
Juan Rodríguez-Hortalá,et al.
Rewriting and Call-Time Choice: The HO Case
,
2008,
FLOPS.
[10]
Lennart Augustsson,et al.
Implementing Haskell overloading
,
1993,
FPCA '93.
[11]
Simon L. Peyton Jones,et al.
Type Classes in Haskell
,
1994,
ESOP.
[12]
Mario Rodríguez-Artalejo,et al.
Polymorphic Types in Functional Logic Programming
,
2001,
J. Funct. Log. Program..
[13]
Ralf Hinze,et al.
Generic Programming, Now!
,
2006,
SSDGP.
[14]
Philip Wadler,et al.
How to make ad-hoc polymorphism less ad hoc
,
1989,
POPL '89.
[15]
John Peterson,et al.
Implementing type classes
,
1993,
PLDI '93.
[16]
Alexander A. Stepanov,et al.
Generic Programming
,
1988,
ISSAC.