caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Recursive classes and subtyping
@ 2002-02-01 12:17 Frederic Tronel
  2002-02-01 13:34 ` Jerome Vouillon
  0 siblings, 1 reply; 9+ messages in thread
From: Frederic Tronel @ 2002-02-01 12:17 UTC (permalink / raw)
  To: caml-list

Hello,

A new question about classes in ocaml:

Why is it that these classes definitions are not accepted by
the compiler :

class a =
  object
    val mutable al = ([] : a list)
    method get_al = al
    method set_al x = al <- x 
  end
and b =
object (this)
  inherit a as super
  val mutable c = new c
  method get_c = c
  method set_c x = c <- x
  initializer this#set_al [(c :> a)] 
end
and c =
object 
  inherit a
  val mutable titi = ""
  method get_titi = titi
  method set_titi x = titi <- x
end

while these ones can be compiled:

class a =
  object
    val mutable al = ([] : a list)
    method get_al = al
    method set_al x = al <- x 
  end
class c =
object 
  inherit a
  val mutable titi = ""
  method get_titi = titi
  method set_titi x = titi <- x
end
class b =
object (this)
  inherit a as super
  val mutable c = new c
  method get_c = c
  method set_c x = c <- x
  initializer this#set_al [(c :> a)] 
end

Where is the fundamental difference ????

Best regards,

Frederic
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 12:17 [Caml-list] Recursive classes and subtyping Frederic Tronel
@ 2002-02-01 13:34 ` Jerome Vouillon
  2002-02-01 13:42   ` Frederic Tronel
                     ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Jerome Vouillon @ 2002-02-01 13:34 UTC (permalink / raw)
  To: Frederic Tronel; +Cc: caml-list

Hello,


> Why is it that these classes definitions are not accepted by
> the compiler :
> 
> class a =
[...]
> and b =
> object (this)
[...]
>   initializer this#set_al [(c :> a)] 
> end
> and c =
[...]
>
> while these ones can be compiled:
> 
> class a =
[...]
> class c =
[...]
> class b =
> object (this)
[...]
>   initializer this#set_al [(c :> a)] 
> end
> 
> Where is the fundamental difference ????

In the first case, the type "a" is not fully known when the coercion
"(c :> a)" is performed, as the classes are mutually recursive.  The
compiler handles this case as if it knew nothing about the structure
of the type "a".  With this assumption, the only case the compiler can
be sure that the type of "c" is a subtype of "a" is when the type of
"c" is "a".  This is not the case, hence the error message.

The reason for this overly conservative (in this case) assumption is
that the result of the type inference algorithm could otherwise depend
on the order in which the program is typed.

If you really need mutually recursive classes, a work-around is to
explicitely define the classe types before defining the classes.

-- Jérôme
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 13:34 ` Jerome Vouillon
@ 2002-02-01 13:42   ` Frederic Tronel
  2002-02-01 14:55   ` Claudio Sacerdoti Coen
  2002-02-01 15:12   ` Claudio Sacerdoti Coen
  2 siblings, 0 replies; 9+ messages in thread
From: Frederic Tronel @ 2002-02-01 13:42 UTC (permalink / raw)
  To: Jerome Vouillon, caml-list

> 
> In the first case, the type "a" is not fully known when the coercion
> "(c :> a)" is performed, as the classes are mutually recursive.  The
> compiler handles this case as if it knew nothing about the structure
> of the type "a".  With this assumption, the only case the compiler can
> be sure that the type of "c" is a subtype of "a" is when the type of
> "c" is "a".  This is not the case, hence the error message.
> 
> The reason for this overly conservative (in this case) assumption is
> that the result of the type inference algorithm could otherwise depend
> on the order in which the program is typed.
> 

Clear !

> If you really need mutually recursive classes, a work-around is to
> explicitely define the classe types before defining the classes.
> 

I don't really need recursive classes, but I started
writing my classes in a top-down manner with forward references to
classes
to be defined. Hence, the use of recursive classes. I can simply change
my definitions order (bottow-up), it solves the problem !

Thanks,

Frederic.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 13:34 ` Jerome Vouillon
  2002-02-01 13:42   ` Frederic Tronel
@ 2002-02-01 14:55   ` Claudio Sacerdoti Coen
  2002-02-01 15:12   ` Claudio Sacerdoti Coen
  2 siblings, 0 replies; 9+ messages in thread
From: Claudio Sacerdoti Coen @ 2002-02-01 14:55 UTC (permalink / raw)
  To: Jerome Vouillon; +Cc: Frederic Tronel, caml-list

> The reason for this overly conservative (in this case) assumption is
> that the result of the type inference algorithm could otherwise depend
> on the order in which the program is typed.

 It is dependent on that order. I have met such an example a few
 days ago. (If you want I can submit the code: it seemed strange, so
 I kept it.)

				Regards,
				C.S.C.

-- 
----------------------------------------------------------------
Real name: Claudio Sacerdoti Coen
PhD Student in Computer Science at University of Bologna
E-mail: sacerdot@cs.unibo.it
http://caristudenti.cs.unibo.it/~sacerdot
----------------------------------------------------------------
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 13:34 ` Jerome Vouillon
  2002-02-01 13:42   ` Frederic Tronel
  2002-02-01 14:55   ` Claudio Sacerdoti Coen
@ 2002-02-01 15:12   ` Claudio Sacerdoti Coen
  2002-02-01 16:41     ` Laurent Vibert
  2 siblings, 1 reply; 9+ messages in thread
From: Claudio Sacerdoti Coen @ 2002-02-01 15:12 UTC (permalink / raw)
  To: Jerome Vouillon; +Cc: caml-list

> In the first case, the type "a" is not fully known when the coercion
> "(c :> a)" is performed, as the classes are mutually recursive.  The
> compiler handles this case as if it knew nothing about the structure
> of the type "a".  With this assumption, the only case the compiler can
> be sure that the type of "c" is a subtype of "a" is when the type of
> "c" is "a".  This is not the case, hence the error message.
>... 
> If you really need mutually recursive classes, a work-around is to
> explicitely define the classe types before defining the classes.

 I suppose this should be related with this other example
 (which is the semplification of a problem I have just met):

# type -'a t;;
type -'a t
# class c (obj : [> `A] t) = object end
  and d (obj : [`A] t) = object method c = new c obj end;;
class c : [ `A] t -> object end
class d : [ `A] t -> object method c : c end

 Is there any way to to obtain the type "class c : [> `A] t -> object end"
 while keeping the mutual recursion?

 I have tried to explicitely define the class types, but I have had no
 success at all.

				Thanks in advance,
                                     C.S.C.

-- 
----------------------------------------------------------------
Real name: Claudio Sacerdoti Coen
PhD Student in Computer Science at University of Bologna
E-mail: sacerdot@cs.unibo.it
http://caristudenti.cs.unibo.it/~sacerdot
----------------------------------------------------------------
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 15:12   ` Claudio Sacerdoti Coen
@ 2002-02-01 16:41     ` Laurent Vibert
  2002-02-01 17:01       ` Claudio Sacerdoti Coen
  0 siblings, 1 reply; 9+ messages in thread
From: Laurent Vibert @ 2002-02-01 16:41 UTC (permalink / raw)
  To: Claudio Sacerdoti Coen; +Cc: caml-list

On Fri, 1 Feb 2002, Claudio Sacerdoti Coen wrote:

> # type -'a t;;
> type -'a t
> # class c (obj : [> `A] t) = object end
>   and d (obj : [`A] t) = object method c = new c obj end;;
> class c : [ `A] t -> object end
> class d : [ `A] t -> object method c : c end
> 
>  Is there any way to to obtain the type "class c : [> `A] t -> object end"
>  while keeping the mutual recursion?

I think this is more or less related to
http://caml.inria.fr/FAQ/FAQ_EXPERT-eng.html#regle_mu

so you could write :

class c (obj : [> `A] t) = object end
and d' (newc : [ `A] t -> c) (obj : [`A] t) = object 
  method c = newc obj
end;;

class d obj = object
  inherit d' (new c) obj
end;;


-- 
Laurent Vibert                        doctorant, équipe Espresso
IRISA-INRIA, Campus de Beaulieu       Tél: +33 (0) 2 99 84 72 33
35042 Rennes Cedex, France            Fax: +33 (0) 2 99 84 71 71




-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 16:41     ` Laurent Vibert
@ 2002-02-01 17:01       ` Claudio Sacerdoti Coen
  2002-02-01 17:20         ` Claudio Sacerdoti Coen
  0 siblings, 1 reply; 9+ messages in thread
From: Claudio Sacerdoti Coen @ 2002-02-01 17:01 UTC (permalink / raw)
  To: Laurent Vibert; +Cc: caml-list

> I think this is more or less related to
> http://caml.inria.fr/FAQ/FAQ_EXPERT-eng.html#regle_mu

 It works, thanks!!! I missed this entry in the FAQ.
 
 Though, the solution is quite heavy (in terms of code length)
 in my (rather patological) case where I have severals mutual
 recursive classes. (It is a binding to a C library automatically
 generated from severals "IDL" files). So, I am still considering
 living with the more constrained type...

				Thanks again,
				   C.S.C.

-- 
----------------------------------------------------------------
Real name: Claudio Sacerdoti Coen
PhD Student in Computer Science at University of Bologna
E-mail: sacerdot@cs.unibo.it
http://caristudenti.cs.unibo.it/~sacerdot
----------------------------------------------------------------
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 17:01       ` Claudio Sacerdoti Coen
@ 2002-02-01 17:20         ` Claudio Sacerdoti Coen
  2002-02-04  2:30           ` Jacques Garrigue
  0 siblings, 1 reply; 9+ messages in thread
From: Claudio Sacerdoti Coen @ 2002-02-01 17:20 UTC (permalink / raw)
  To: Laurent Vibert; +Cc: caml-list

>  It works, thanks!!!

 Obviously, I was a bit too quick! I think that the trick works only up to
 the point where the mutual recursion is fake. This is a slightly more
 complex example:

# type -'a t;;

# let f (x : [> `A] t) = (x : [`A] t);;

# class c (newd : [> `A] t -> d) (obj : [> `A] t) =
 object
  method d = newd (f obj)
 end
and d (newc : [> `A] t -> c) (obj : [> `A] t) =
 object
  method c = newc (f obj)
 end
;;
class c : ([ `A] t -> d) -> [ `A] t -> object method d : d end
class d : ([ `A] t -> c) -> [ `A] t -> object method c : c end


 I leave it for the real ocaml gurus!

					Regards,
					C.S.C.

-- 
----------------------------------------------------------------
Real name: Claudio Sacerdoti Coen
PhD Student in Computer Science at University of Bologna
E-mail: sacerdot@cs.unibo.it
http://caristudenti.cs.unibo.it/~sacerdot
----------------------------------------------------------------
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Recursive classes and subtyping
  2002-02-01 17:20         ` Claudio Sacerdoti Coen
@ 2002-02-04  2:30           ` Jacques Garrigue
  0 siblings, 0 replies; 9+ messages in thread
From: Jacques Garrigue @ 2002-02-04  2:30 UTC (permalink / raw)
  To: sacerdot; +Cc: caml-list

From: Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>

>  Obviously, I was a bit too quick! I think that the trick works only up to
>  the point where the mutual recursion is fake. This is a slightly more
>  complex example:
> 
> # type -'a t;;
> 
> # let f (x : [> `A] t) = (x : [`A] t);;
> val f : [ `A] t -> [ `A] t = <fun>

If you just put two annotations, they will be unified.
You need an explicit coercion to get the type you expect:

# let f x = (x : [> `A] t :> [`A] t);;
val f : [> `A] t -> [ `A] t = <fun>

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-02-04  2:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-01 12:17 [Caml-list] Recursive classes and subtyping Frederic Tronel
2002-02-01 13:34 ` Jerome Vouillon
2002-02-01 13:42   ` Frederic Tronel
2002-02-01 14:55   ` Claudio Sacerdoti Coen
2002-02-01 15:12   ` Claudio Sacerdoti Coen
2002-02-01 16:41     ` Laurent Vibert
2002-02-01 17:01       ` Claudio Sacerdoti Coen
2002-02-01 17:20         ` Claudio Sacerdoti Coen
2002-02-04  2:30           ` Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).