Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Polymorphic methods (longest error message ever!)
[ 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 methods (longest error message ever!)
From: Brian Smith <brian-l-smith@uiowa.edu>

> First of all, where can I find some documentation about the CVS versino 
> of O'Caml? Is the documentation kept in CVS too?

Yes, but it is not public.
Anyway, it is not updated simultaneously with the source, so you
wont't find the documentation for a new feature there immediately.
So basically, new features in CVS are given as is, without other
documentation than asking the developpers.

In the present case, the release is probably not too far away, so you
should not have to wait too long.

Until that, for polymorphic methods, there are examples in
testlabl/poly.ml, but it's about all.

> Anyway, on to the problem at hand:
> That works fine in O'Caml 3.04. But, I want to use the new polymorphic 
> method feature in O'Caml 3.04+15. So I tried:
> 
>   # class type node_type =  object
>       method as_variant : [> `Node of node_type]
>     end;;
>   class type node_type =
>      object method as_variant : [> `Node of node_type] end
> 
>   # class node : node_type = object (self)
>       method as_variant = `Node (self :> node_type)
>     end;;
>                                  ^^^^
>   This expression cannot be coerced to type
>   node_type = < as_variant : 'a. [> `Node of node_type] as 'a >;
>   it has type < as_variant : 'c; .. > as 'b but is here used with type 'b

That one is correct behaviour: as_variant is inferred to be
monomorphic, since in the first pass the checker doesn't look inside
method, where is your only type annotation.
(Unfortunately the node: node_type constraint is only applied
afterwards, applying it early being rather complex.)

>   # class node : node_type = object (self)
>       method as_variant : 'a. [> `Node of node_type] as 'a
>                         = `Node (self :>  node_type)
>     end;;
> The class type object method as_variant : [> `Node of node_type] end
> is not matched by the class type node_type
> The class type object method as_variant : [> `Node of node_type] end
> is not matched by the class type
>    object method as_variant : [> `Node of node_type] end
> The method as_variant has type 'a. [> `Node of node_type] as 'a
> but is expected to have type 'b. [> `Node of node_type] as 'b
> The universal variable 'b would escape its scope
> 
> I have no idea what the error message means. To me, it is saying that 
> class "node" doesn't match class type "node_type" even though everything 
> has exactly the same type. Could somebody please explain it to me? What 
> is the correct way of doing the above?

As skaller says, this is hardly a long message :-)
It is also clearly wrong: it tells you that two identical types are
incompatible.
The problem seems to come from the node : node_type part. The
following version works:

# class node = object (self)
    method as_variant : 'a. [> `Node of node_type] as 'a
                      = `Node (self :>  node_type)
  end;;
class node : object method as_variant : [> `Node of node_type] end

And that shorter one also:
# class node = object (self : #node_type)
    method as_variant = `Node (self :> node_type)
  end;;
class node : object method as_variant : [> `Node of node_type] end

Thank you for the bug report, I'll try to correct it.

      Jacques
-------------------
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