[Paper Review] Haskell's overlooked object system
This paper demonstrates that Haskell 98, extended with common features like multi-parameter type classes and functional dependencies, can natively support full-fledged object-oriented programming—including encapsulation, inheritance, subtyping, polymorphism, and mutable state—via the OOHaskell library. The key contribution is showing that advanced OO idioms translate directly into Haskell with minimal syntactic friction and strong type safety, preserving Haskell’s powerful type inference.
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. 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. The OO features are introduced in Haskell as the OOHaskell library. OOHaskell lends itself as a sandbox for typed OO language design.
Motivation & Objective
- To investigate whether Haskell 98 with common extensions can express full-featured object-oriented programming without new language extensions.
- To demonstrate that conventional OO features—such as inheritance, polymorphism, encapsulation, and mutable state—can be faithfully encoded in Haskell.
- To show that Haskell’s type system supports advanced subtyping features like width, depth, and co-variant argument subtyping while preserving type inference.
- To establish OOHaskell as a practical sandbox for experimenting with typed object-oriented language design without compiler changes.
- To resolve the longstanding debate on the relationship between Haskell’s type classes and subtype polymorphism in OO programming.
Proposed method
- Encoding objects as extensible, first-class records using the HList library, which provides heterogeneous, introspectable records with subtyping.
- Using multi-parameter type classes with functional dependencies to model object classes, interfaces, and inheritance hierarchies.
- Implementing object construction via constructor methods and object generators that support implicit polymorphism and open recursion.
- Leveraging recursive record types and type-level programming to safely model self-referential objects and prevent premature access to uninitialized state.
- Supporting safe downcasts and upcasts through dynamic type checks and explicit type constraints, with failures caught at compile time when possible.
- Enabling flexible multiple inheritance and nominal/structural subtyping through type-level manipulation of record fields and method types.
Experimental results
Research questions
- RQ1Can Haskell 98 with standard extensions fully express conventional object-oriented features such as inheritance, encapsulation, and subtyping?
- RQ2To what extent can Haskell’s type system preserve inference and safety while encoding complex OO idioms like virtual methods and mutable state?
- RQ3How can structural and nominal subtyping, including width and depth subtyping, be safely and expressively encoded in Haskell?
- RQ4Can first-class classes and polymorphic methods be implemented in Haskell without language extensions, and how do they interact with object-oriented abstractions?
- RQ5Is it possible to support safe downcasting and co-variant method arguments in a statically typed, functional setting like Haskell?
Key findings
- Haskell 98 with multi-parameter type classes and functional dependencies is sufficient to implement a full-featured object system, without requiring new language extensions.
- The OOHaskell library enables expression-by-expression translation of textbook OO code into Haskell, preserving intuition and structure without global program transformations.
- Safe downcasts are enforced by the type system, with impossible casts flagged as type errors at compile time.
- The system supports advanced subtyping features including width subtyping (field addition), depth subtyping (inheritance), and co-variant method arguments.
- First-class lexically scoped classes and implicitly polymorphic classes are natively supported through type-level programming and HList-based records.
- The type system automatically infers the least general interface for heterogeneous collections of objects, enabling homogeneous treatment through semi-implicit upcasting.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.