Haskell's overlooked object system

Important note: The code base for this paper has evolved over the years and is available and maintained at http://code.haskell.org/OOHaskell/. The paper has evolved since its last public release in Sep 2005, but we have not completed any revision we would like to share.
Authors
Oleg Kiselyov and Ralf Lämmel

Abstract
Haskell provides type-class-bounded and parametric polymorphism as opposed to subtype polymorphism of object-oriented languages such as Java and OCaml. It is a contentious question whether Haskell~98 without extensions, or with common extensions, or with new extensions can fully support conventional object-oriented programming with encapsulation, mutable state, inheritance, overriding, statically checked implicit and explicit subtyping, and so on.

In a first phase, we demonstrate how far we can get with object-oriented functional programming, if we restrict ourselves to plain Haskell~98. In the second and major phase, we systematically substantiate that Haskell~98, with some common extensions, supports all the conventional OO features plus more advanced ones, including first-class lexically scoped classes, implicitly polymorphic classes, flexible multiple inheritance, safe downcasts and safe co-variant arguments. Haskell indeed can support width and depth, structural and nominal subtyping. We address the particular challenge to preserve Haskell's type inference even for objects and object-operating functions. Advanced type inference is a strength of Haskell that is worth preserving. Many of the features we get ``for free'': the type system of Haskell turns out to be a great help and a guide rather than a hindrance.

The OO features are introduced in Haskell as the OOHaskell library, non-trivially based on the HList library of extensible polymorphic records with first-class labels and subtyping. The library sample code, which is patterned after the examples found in OO textbooks and programming language tutorials, including the OCaml object tutorial, demonstrates that OO code translates into OOHaskell in an intuition-preserving way: essentially expression-by-expression, without requiring global transformations.

OOHaskell lends itself as a sandbox for typed OO language design.

Keywords
Object-oriented functional programming, Object type inference, Typed object-oriented language design, Heterogeneous collections, ML-ART, Mutable objects, Type-Class-based programming, Haskell, Haskell~98, Structural subtyping, Duck typing, Nominal subtyping, Width subtyping, Deep subtyping, Co-variance

Bibtex entries
@unpublished{OOHaskell,
 author = "Oleg Kiselyov and Ralf L{\"a}mmel",
 title = "{Haskell's overlooked object system}",
 year = 2005,
 note = "Draft"
}

Links


Website maintained by Ralf Lämmel (Email: rlaemmel@gmail.com)