Version française
Home     About     Download     Resources     Contact us    
Browse thread
Polymorphic variants in class methods
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Rogoff <bpr@b...>
Subject: Re: Polymorphic variants in class methods
On Mon, 9 Oct 2000, malc wrote:
> # class foo = object method moo = `Bar end;;
> Some type variables are unbound in this type:
>   class foo : object method moo : [> `Bar] end
> The method moo has type [> `Bar] where 'a is unbound
> 
> Someone care to comment on that?

As Jacques Garrigue has pointed out, typing of polymorphic variants is
subtle, and the interaction of polymorphic variant and object typing is 
very subtle. Let me try and explain simply and an expert can tell if I am 
wrong or inaccurate...

Types described by "[>" are polymorphic and can be extended with new
tags, so they have an implicit type variable.

OCaml classes can be polymorphic, but methods must be monomorphic, so 
your example is similar to 

# class foo = object method moo = List.length end;;
Some type variables are unbound in this type:
  class foo : object method moo : 'a list -> int end
The method moo has type 'a list -> int where 'a is unbound

Now, an oft discussed extension to OCaml is polymorphic methods (and an 
even more oft discussed extension would allow polymorphic recursion in 
the core (non OO) language) which would have you explicitly provide 
the type for that method. This was supported in OLabl. I don't know the
OLabl incantation to type your program, but for the example I gave I
think it is 

class foo = object method moo : 'a . 'a list -> int = List.length end;;

One way to make your program typecheck is to define the set of variants
you want to deal with, like so. 

# type t = [`Foo | `Bar | `Baz];;
type t = [ `Foo | `Bar | `Baz]
# class foo = object method moo : t = `Bar end;;  
class foo : object method moo : t end

> P.S. Uhm, like im totaly lost on whats going on here.

Ummm, like I totally hope this helps, dude. :-)

I think you'll want to at least read "Programming with Polymorphic
Variants" which you can download from

    http://wwwfun.kurims.kyoto-u.ac.jp/soft/olabl/

-- Brian