Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Re: ocaml polymorphic variants and double coercion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-07-01 (08:03)
From: Jacques Garrigue <see@m...>
Subject: [Caml-list] Re: ocaml polymorphic variants and double coercion
The following message is a courtesy copy of an article
that has been posted to comp.lang.functional as well.

Shaddin Doghmi <> writes:

> What is the difference between double corcion and single coersion? what 
> can double coercion do that single coercion cant?

Lots of things :-)
In a single coercion, the expected type is directly built from the
target type in a predefined way. The algorithm attempts to be clever,
but if the target type is recursive, or already has a polymorphic
structure, there is no unique most general expected type. Moreover,
there are extra restrictions to avoid a blow-up in type inference
time, namely left hand sides of arrows are not available for subtyping
(but other contravariant types are...)

Here are typical examples involving polymorphic variants

fun x -> (x :> [`Nil | `Cons of 'a * 'b] as 'b)
- : [< `Cons of 'b * ([ `Cons of 'a | `Nil ] as 'c) as 'a | `Nil ] -> 'c
Note that the expected type has only been partially open.

fun x ->
  (x : [< `Nil | `Cons of 'a * 'b] as 'b :> [`Nil | `Cons of 'a * 'c] as 'c)
With a double coercion we can get recursion on the expected side.

fun x ->
  (x : [< `Nil | `Cons of 'a * 'b] as 'b :> [> `Nil | `Cons of 'a * 'c] as 'c)
This wouldn't work with a single coercion, as the target is polymorphic.

If there is no recursion, you can get this last effect through dispatch:
function `None | `Some _ as x -> x
- : [< `None | `Some of 'a ] -> [> `None | `Some of 'a ]

> Also, there doesnt seem to be any good detailed references for 
> polymorphic variants. Both the ocaml manual and the oreilly book dont 
> mention lots of details. Anybody know of any good references?

See my publications for examples and theory with polymorphic variants.
In particular the paper on code reuse is a good introduction, and the
syntax is (almost) the current one.

There is a very small bit on coercions in "Programming with ..."
(which uses an older syntax, by the way).
Basically they work just like for object types, which get a reasonably
detailed account in the reference manual. The main differences are
that (1)there is no concept of class type with variants, so you always
need double coercions to introduce recursion, and (2)with polymorphic
variants a closed type vt has both a polymorphic subtype [< vt] and a
polymorphic supertype [> vt], while an object type has only a subtype

P.S. I forward to the caml-list to be in the logs...

Jacques Garrigue      Kyoto University     garrigue at
		<A HREF=>JG</A>

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: