[TYPES] The type/object distinction and possible synthesis of OOP and imperative programming languages

oleg at okmij.org oleg at okmij.org
Sat Apr 20 01:26:08 EDT 2013

Jonathan Aldrich wrote:
> A critical property of objects, which is used architecturally in many OO 
> systems, is support for heterogeneous data structures: e.g. putting 
> several different implementations of an abstraction into a list.  You 
> can do this is Haskell only through a "slightly clumsy" encoding that 
> wraps a type class in another data structure, thereby existentially 
> quantifying over the type class used.  

> With pure row polymorphism, just as with type classes 
> in Haskell, you can't implement heterogeneous data structures unless you 
> pack the row variable in an existential.

I'm afraid you are short-changing Haskell. There are ways to build
heterogeneous collections and extensible (row-polymorphic) records
that have nothing to do with existentials. It was possible back in
2004. With data kinds and kind polymorphism, the approach became much
more convenient. The approach is used in practice, btw, for example,
for embedding DSL of first-class attribute grammars. (See
``Attribute Grammars Fly First-Class: How to do aspect oriented
programming in Haskell'' Marcos Viera, S. Doaitse Swierstra and Wouter
S. Swierstra. ICFP 2009)

The OOHaskell paper

describes how to do bona fide OOP in Haskell. Haskell lets us not only
represent object hierarchies with identity, internal state and virtual
methods, but also represent and investigate complex issues like
self-types, co-variance a la Eiffel (and how to make it sound) -- and
a particularly thorny problem of preventing constructors from calling
(virtual) methods on the not-yet constructed object.

Section 3 of the OOHaskell paper describes several different ways of
representing extensible records. Existentials is one way -- and not
the best one. OOHaskell paper also showed two different ways of
putting differently-typed objects into a single list; one approach
permits sound downcast.

More information about the Types-list mailing list