caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Mutually recursive classes
@ 2003-11-03 17:41 pous damien
  2003-11-04 17:52 ` Damien
  0 siblings, 1 reply; 5+ messages in thread
From: pous damien @ 2003-11-03 17:41 UTC (permalink / raw)
  To: caml-list

On 3 Nov 2003, Didier Remy wrote:

> It looks similar to the subject/observer pattern.  
> Check Section 5.3 of the OCaml manual as well as my 
> APPSEM course notes
  
It only looks similar...
  
I am working with class _types_ :
This hierarchy is included in a large project, and I need
_readable_ interfaces (as well as _writeable_ :-)

I can't write
"constraint 'a = <an enormous explicit object type as 'b that has
already been written half a dozen times as 'a..>"

Furthermore, my classes _are_ mutually recursive : when I retrieve a   
parent from its child, I want a "full-featured parent" (and vice-versa)
 
damien    
 

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


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

* Re: [Caml-list] Mutually recursive classes
  2003-11-03 17:41 [Caml-list] Mutually recursive classes pous damien
@ 2003-11-04 17:52 ` Damien
  0 siblings, 0 replies; 5+ messages in thread
From: Damien @ 2003-11-04 17:52 UTC (permalink / raw)
  To: caml-list

I could solve my problem by first defining two "open class type", and
then unifying 'self with the parameter in two other class types :

<<
class type ['a, 'b] a_ot = object
	constraint 'b = ('a, 'b) #b_ot
	method	 coerce: 'a
	method	b: 'b
end and ['a, 'b] b_ot = object
	constraint 'a = ('a, 'b) #a_ot
	method	 coerce: 'b
	method	a: 'a
end

class type ['b] a_t = object ('s) inherit ['s, 'b] a_ot end
class type ['a] b_t = object ('s) inherit ['a, 's] b_ot end
>>

It was the same kind of problem as the one discussed in
<http://caml.inria.fr/archives/200303/msg00349.html>

the parameters of a class are generalized after its type has been
inferred.
thus, with

<<
class type ['b] a = object ('sa)
  method coerce: 'sa
  method b: 'b
  constraint 'b = 'sa #b
end and ['a] b = object ('sb)
  method coerce: 'sb
  method a: 'a
  constraint 'a = 'sb #a
end
>>

ocaml tries to unify 'sa (resp. 'sb) and 'a (resp. 'b) which is
impossible since this would close the self types

I lack some OO theory knowledges to understand the Jacques Garrigue's
distinction between "structural definition" and "definition by name"
(could someone explain me or send me some useful links ?)

but I think that recursion with distinct type parameters should be
allowed in the case above (recursive class _types_),
disallowing it seems to me like a boring syntactic restriction, since
one can always :
 - duplicate and separate the class type definitions ; (a_ot, b_ot)
 - add parameters for forward type definitions ('a, 'b)
 - glue all these types together to find back the first ones (a_t, b_t)

Does anyone know a case where this is not true ? (with class types)
What kind of "fully-recursive" classes are meaningless (just curious)

thanks,
damien

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


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

* Re: [Caml-list] Mutually recursive classes
  2003-11-03 10:48 Damien
  2003-11-03 13:53 ` Didier Remy
@ 2003-11-07  7:34 ` skaller
  1 sibling, 0 replies; 5+ messages in thread
From: skaller @ 2003-11-07  7:34 UTC (permalink / raw)
  To: Damien; +Cc: caml-list

On Mon, 2003-11-03 at 21:48, Damien wrote:
> Hello,
> 
> I would like to create two mutually recursive classes,

There is a mechanical workaround for this.

You first parameterise your classes,
then you use a type/class definition
instantiating the parameters: the latter
construction allows recursive 'and's.

For example, 

class type A' ['B'] ..
class type B' ['A'] ..

type A = B A' and B = A B'

[and similarly for classes, using let instead]


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


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

* Re: [Caml-list] Mutually recursive classes
  2003-11-03 10:48 Damien
@ 2003-11-03 13:53 ` Didier Remy
  2003-11-07  7:34 ` skaller
  1 sibling, 0 replies; 5+ messages in thread
From: Didier Remy @ 2003-11-03 13:53 UTC (permalink / raw)
  To: Damien; +Cc: caml-list

Damien <Damien.Pous@ens-lyon.fr> writes:

> Hello,
> 
> I would like to create two mutually recursive classes,
> parents (a) and children (b)
> 
> Furthermore, I'd like to define them incrementally
> parents and children,
> parents and children with hands
> parents and children with hands and feet

It looks similar to the subject/observer pattern.  Check Section 5.3 of the
OCaml manual as well as my APPSEM course notes
http://cristal.inria.fr/~remy/cours/appsem/ocaml-objects.html

        Didier


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


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

* [Caml-list] Mutually recursive classes
@ 2003-11-03 10:48 Damien
  2003-11-03 13:53 ` Didier Remy
  2003-11-07  7:34 ` skaller
  0 siblings, 2 replies; 5+ messages in thread
From: Damien @ 2003-11-03 10:48 UTC (permalink / raw)
  To: caml-list

Hello,

I would like to create two mutually recursive classes,
parents (a) and children (b)

Furthermore, I'd like to define them incrementally
parents and children,
parents and children with hands
parents and children with hands and feet
...

let's have a try :

class type ['b] a0 = object ('a)
  method coerce: 'a
  method b: 'b
  constraint 'b = 'a #b0
  (* ... *)
end and ['a] b0 = object ('b)
  method coerce: 'b
  method a: 'a
  constraint 'a = 'b #a0
  (* ... *)
end

class type ['b] a1 = object ('a)
  inherit ['b] a0
  constraint 'b = 'a #b1
  (* ... *)
end and ['a] b1 = object ('b)
  inherit ['a] b0
  constraint 'a = 'b #a1
  (* ... *)
end

...


Unfortunately, this does not work :
>    constraint 'b = 'a #b0
>                    ^^
>This type < b : 'b; coerce : 'a; .. > as 'a should be an instance of
>type 'c 
>Self type cannot escape its class

which is odd from my mind, since the following code is well typed :

class type ['c] c = object
  method c: 'c
end
class type ['b] a = object ('a)
  method b: 'b
  constraint 'b = 'a #c
end


I can get something better, using two type parameters :

class type ['a, 'b] a0 = object
  method coerce: 'b
  method b: 'b
  constraint 'a = ('a, 'b) #a0
  constraint 'b = ('a, 'b) #b0
end and ['a, 'b] b0 = object
  method coerce: 'b
  method a: 'a
  constraint 'a = ('a, 'b) #a0
  constraint 'b = ('a, 'b) #b0
end

but then the "self type" is no longer correlated to 'a/'b  so that I
can't implement the method coerce, 
so, I need to "virtualize" it, and to implement it only at the end of
the class hierarchy, 
where I need to close it, saying 'a = 'self / 'b = 'self

I don't want this, because I'd like be able to use these classes at any
level (yes, a parent without feet does make sense...)


Does this case of "Self type cannot escape its class" restriction really
make sense ?

Do you know how to work-around this ?

thanks,
damien

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


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

end of thread, other threads:[~2003-11-07  8:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-03 17:41 [Caml-list] Mutually recursive classes pous damien
2003-11-04 17:52 ` Damien
  -- strict thread matches above, loose matches on Subject: below --
2003-11-03 10:48 Damien
2003-11-03 13:53 ` Didier Remy
2003-11-07  7:34 ` skaller

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).