Lambda the Ultimate

inactiveTopic Polymorphic Variants
started 2/28/2004; 4:54:31 AM - last post 2/29/2004; 4:46:08 AM
andrew cooke - Polymorphic Variants  blueArrow
2/28/2004; 4:54:31 AM (reads: 8608, responses: 3)
Polymorphic Variants
We have demonstrated here how polymorphic variants allow one to combine the benefits of algebraic datatypes [...] with code re-use at a level of simplicity comparable with OO languages. The added expressive power is significant: encoding in either traditional variants or OO style would incur problems ranging from [...]

The idea is that you don't define the combination of constructors A|B as some particular type, but instead deal directly with collections of constructors as required. It's then easy to add a bunch of extra constructors later.

I read this paper last night (from here) and was impressed. It seems to be simple and neat, makes variants (sum types - the ones with "|"s in the syntax) a little more general, looks like it might help out when you're maintaining old code and need to get two systems working together, and suggests (to my uneducated eyes at least) a different approach to ad-hoc polymorphism (if functions can be defined multiple times, as in Haskell, with pattern matching to the left of "="). Unfortunately, it does make code more verbose.

(And it's implemented in recent versions of OCaml)
Posted to functional by andrew cooke on 2/28/04; 5:00:37 AM

Sjoerd Visscher - Re: Polymorphic Variants  blueArrow
2/28/2004; 7:11:27 AM (reads: 244, responses: 0)
I think it's all a bit of a mess in OCaml (and other languages).

The categorical duality of sums and products is nowhere to be found. Sums are usually defined using constructors, effectively giving labeled sums. The labels give nice possibilities like these polymorphic variants.

The dual of labeled sums is labeled products. Trying to find them in OCaml returned three results: objects, records and labels. They are all like labeled products, but none of them really are. And even pattern matching on labeled sums could count as labeled products.

Can somebody explain if this is really necessary, or if this is just an unfortunate effect of evolutionary language design?

Neel Krishnaswami - Re: Polymorphic Variants  blueArrow
2/28/2004; 12:07:50 PM (reads: 201, responses: 1)
IIRC, the dual of polymorphic variants are records with row polymorphism, and Caml objects use row polymorphism. However, Caml records are not row-polymorphic: they are dual to regular sum types, right down to the field labels being particular to a type in exactly the same way that the tags of data constructors are. (That's why field names have to be prefixed with the module they come from.)

I don't know much about how Caml labeled functions work, other than that they are very handy in practice. :)

andrew cooke - Re: Polymorphic Variants  blueArrow
2/29/2004; 4:46:08 AM (reads: 166, responses: 0)
IIRC, the dual of polymorphic variants are records with row polymorphism

If anyone else is curious, this seems to be like the way OCaml describes object types - a list of types ending in "..." (ie specifying a minimum set of methods). Couldn't find a good paper, but "..." is also called a (the?) row variable, apparently (in case it helps Googling).