Version française
Home     About     Download     Resources     Contact us    
Browse thread
Structural subtyping problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Andreas Rossberg <rossberg@m...>
Subject: Re: [Caml-list] Structural subtyping problem

On Mar 28, 2010, at 18.38 h, Dario Teixeira wrote:

> Hi,
>
> I'm using the structural subtyping aspects of Ocaml's object system  
> to emulate width
> subtyping.  I've come across a problem which does not type-check,  
> though intuitively
> it seems correct.  I reckon that the compiler may need some help in  
> the form of type
> annotations and/or coercions, though their exact shape elludes me.
>
> A simple nonsensical example that illustrates the problem is listed  
> below; the
> type-checking error occurs in function "step1", where the optional  
> parameter "story"
> is used as an object of type "< title:string; .. >".  In function  
> "step3", this
> parameter "story" is actually instantiated with objects of type "<  
> title:string >"
> and "< title:string; count:int >".
>
> Anyway, am I correct in assuming this should be feasible?  And if  
> so, what coercions
> are required to make this compile?

You problem is just that you try to use step1 polymorphically within a  
recursion. Polymorphic recursion is not (yet) allowed in OCaml.

You can side-step it by coercing the argument to a suitable  
monomorphic type in all uses of step1. In your example, that solution  
just consists of changing step3 as follows:

> and step3 ~story = match story#count with
> 	| 0 ->
> 		let story = (story :> <title:string>)
>                 in step1 ~story ()
> 	| 1 ->
> 		...

/Andreas