[Haskell-cafe] Object-oriented programming, Haskell and existentials

Lennart Augustsson wrote:
I was just pointing out that the mechanism for doing the OO thing exists in Haskell too, albeit looking a little different.
Indeed there is a mechanism for doing OO in Haskell -- several of them. Most of them have nothing to do with Existentials. In the OHaskell paper, http://homepages.cwi.nl/~ralf/OOHaskell/ Ralf Laemmel has collected all known to us methods of doing OO in Haskell. Incidentally, three years ago Lennart Augustsson described a simple, Haskell98 method of emulating OO, without existentials. We give him credit in footnote 4. The OOHaskell paper then goes to demonstrate how to do *all* of the known OO in Haskell, with all its inherent complexity: depth and width subtyping, upcasting and safe downcasting, nominal and structural subtyping, and the whole issue about covariant methods. Derek Elkins wrote:
In general, to encode OO you need quite a bit more than existentials. As you are probably aware, there was a cottage industry in the mid to late '90s working on encodings of OO languages into System F + foo calculi. They just about gave up on a complete encoding until someone figured one out. 'turns out all you needed was recursive bounded existential quantification.
Not necessarily. Again, please see the OOHaskell paper. The full story is that there are several encodings of objects -- using closures and using existentials. The former are *far* simpler. ML-ART (which later evolved in the 'O' of OCaml) chose the more complex encoding -- and hence had to add equi-recursive types, existentials and universals to Caml -- only because of a potential safety issue with closures. A constructor of an object may invoke methods that may access fields that are not initialized yet. This problem is present in all OO languages, and the common `solution' is an admonition ``not to do that''. Clearly Didier Remy has higher standards, and he went into considerable pain to solve the problem. Incidentally, Haskell can solve this problem in a simpler way. We critically rely on the fact that all effects must be done in a monad. Therefore, in OOHaskell we can safely use the simpler encoding for objects. Regarding existentials, the web page http://okmij.org/ftp/Computation/Existentials.html demonstrates how to systematically eliminate existentials. In fact, the object encoding via existentials can be easily transformed into the encoding that uses only simple, first-order types. The web page begs a question if there is ever any real need for existentials.

On Wed, Oct 15, 2008 at 6:05 AM,
Regarding existentials, the web page http://okmij.org/ftp/Computation/Existentials.html
demonstrates how to systematically eliminate existentials. In fact, the object encoding via existentials can be easily transformed into the encoding that uses only simple, first-order types. The web page begs a question if there is ever any real need for existentials.
We might justify existential types on performance grounds. Stream
fusion, for example, uses existentials to replace recursive types and
functions with non-recursive types and functions, which are simpler to
optimize.
--
Dave Menendez

What do you mean by need? From a theoretical or practical perspective?
We don't need them from a theoretical perspective, but in practice I'd
rather use existentials than encodinging them in some tricky way.
On Wed, Oct 15, 2008 at 11:05 AM,
The web page begs a question if there is ever any real need for existentials.

re: the importance of existential-cleansing On the one hand, it's easy to concur that existentials are simpler than the alternatives, the tortuous elimination of CC Shan's "translucent" existential being a case in point. And it's also easy to dismiss such caprice as a penchant for Houdinian escape perversities. Then again, why not? There may never be a real need for anything particular at all, existentials notwithstanding. Affirming that by cracking open the shackles of icons and diabolical shibboleths is arguably the only "real need." Lennart Augustsson wrote:
What do you mean by need? From a theoretical or practical perspective? We don't need them from a theoretical perspective, but in practice I'd rather use existentials than encodinging them in some tricky way.
On Wed, Oct 15, 2008 at 11:05 AM,
wrote: The web page begs a question if there is ever any real need for existentials.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/Object-oriented-programming%2C-Haskell-and-existential... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Lennart Augustsson wrote:
We don't need them [existentials] from a theoretical perspective, but in practice I'd rather use existentials than encodinging them in some tricky way.
If the claim that we don't need existentials theoretically is obvious, I don't have the argument. Still, existentials are the recurrent topic on the OCaml list; quite a few people perceive them as a needed feature and their perceived absence as a drawback of OCaml. The principle of encoding existentials is straightforward: represent the existential datatype by a set of its possible observations. Existentials demonstrate once again what I sloppily call initial-final dichotomy: initial encodings are easier to think of upfront, yet require fancier type systems (second-order types, dependent types, GADTs). Final encodings are elementary, yet take (far) longer to imagine. That difficulty may be just the matter of habit. BTW, wasn't hbc the first Haskell compiler to introduce existentials? The performance grounds for existentials are justified, for example, by the following paper Yasuhiko Minamide, J. Gregory Morrisett and Robert Harper Typed Closure Conversion, POPL 1996, pp. 271--283. http://www.cs.cmu.edu/~rwh/papers/closures/popl96.ps Many object encodings may be considered instances of the type closure conversion. On the other hand, existential elimination may be seen as an inverse process.

Yes, hbc had existential types around 1993.
I've used an encoding of existentials in O'Caml (well F#), and it
works, but I find it painful.
And when a very smart but non-CS person saw it his mind boggled,
whereas he understood the existential version just fine.
On Thu, Oct 16, 2008 at 8:26 AM,
Lennart Augustsson wrote:
We don't need them [existentials] from a theoretical perspective, but in practice I'd rather use existentials than encodinging them in some tricky way.
If the claim that we don't need existentials theoretically is obvious, I don't have the argument. Still, existentials are the recurrent topic on the OCaml list; quite a few people perceive them as a needed feature and their perceived absence as a drawback of OCaml.
The principle of encoding existentials is straightforward: represent the existential datatype by a set of its possible observations. Existentials demonstrate once again what I sloppily call initial-final dichotomy: initial encodings are easier to think of upfront, yet require fancier type systems (second-order types, dependent types, GADTs). Final encodings are elementary, yet take (far) longer to imagine. That difficulty may be just the matter of habit.
BTW, wasn't hbc the first Haskell compiler to introduce existentials?
The performance grounds for existentials are justified, for example, by the following paper
Yasuhiko Minamide, J. Gregory Morrisett and Robert Harper Typed Closure Conversion, POPL 1996, pp. 271--283. http://www.cs.cmu.edu/~rwh/papers/closures/popl96.ps
Many object encodings may be considered instances of the type closure conversion. On the other hand, existential elimination may be seen as an inverse process.

Yes, hbc had existential types around 1993.
I've used an encoding of existentials in O'Caml (well F#), and it works, but I find it painful. And when a very smart but non-CS person saw it his mind boggled, whereas he understood the existential version just fine.
I'm a CS person, and when I saw the encoding of existentials my mind boggled. I just ended up giving up type checking entirely and using obj (the F# equivalent of Data.Dynamic) throughout. Thanks Neil ============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ==============================================================================
participants (5)
-
David Menendez
-
Kim-Ee Yeoh
-
Lennart Augustsson
-
Mitchell, Neil
-
oleg@okmij.org