Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Polymorphic class recursion (?)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Polymorphic class recursion (?)
From: "Lukasz Stafiniak" <luk_stafi@wp.pl>

> # class c1 = object val v1 : unit #c2 option = None val v2 : bool #c2 option
> = None end and ['a] c2 = object val v1 : #c1 option = None end;;
> Characters 59-67:
>   class c1 = object val v1 : unit #c2 option = None val v2 : bool #c2 option
> = None end and ['a] c2 = object val v1 : #c1 option = None end;;
>                                                              ^^^^^^^^
> This type unit should be an instance of type bool
> 
> Why? This works for records. (Switching to class types (class --> class
> type, without = parts) doesn't help.)

The difference is that records types have an identity, while object
types are equal modulo structure.
What this means is that record and sum types may freely use
polymorphic recursion, but object and polymorphic variant types are
limited to regular recursion, as they are expanded to a regular tree
during unification (to check equality).

What this means in practice is there is no simple way to have
polymorphically recursive types: you must necessarily introduce either
a record or a sum type in the recursion.
Even if you do that, the next problem is that you cannot use your own
constructor polymorphically, since this would mean polymorphic
recursion at the expression level. There are two workarounds for that:
use a mutable polymorphic record field, or use the new recursive
modules in ocaml 3.07.
Anyway, that's really hard.

Sorry for the inconvenience, but this kind of polymorphic methods is
easier to define outside of the class.

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners