caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* mutually dependent class and type
@ 2008-09-25 22:24 Sébastien Hinderer
  2008-09-25 23:41 ` [Caml-list] " Martin Jambon
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sébastien Hinderer @ 2008-09-25 22:24 UTC (permalink / raw)
  To: caml-list

Dear list,

Is there a (clean) way to define simultaneously a class and a type that
are mutually recursive ?
Something like this :
class element (c : content) =
object
  ...
end and type content =
  | Data of string
  | Elements of element list;;

This is of course not a valid OCaml definition, but is there a way to
express it ?

Many thanks in advance for your help,
Sébastien.


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

* Re: [Caml-list] mutually dependent class and type
  2008-09-25 22:24 mutually dependent class and type Sébastien Hinderer
@ 2008-09-25 23:41 ` Martin Jambon
  2008-09-26  6:40 ` Tim Rentsch
  2008-10-05 13:31 ` Yann Régis-Gianas
  2 siblings, 0 replies; 4+ messages in thread
From: Martin Jambon @ 2008-09-25 23:41 UTC (permalink / raw)
  To: Sébastien Hinderer; +Cc: caml-list

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1294 bytes --]

On Fri, 26 Sep 2008, Sébastien Hinderer wrote:

> Dear list,
>
> Is there a (clean) way to define simultaneously a class and a type that
> are mutually recursive ?

I don't think so, but you don't have to.

> Something like this :
> class element (c : content) =
> object
>  ...
> end and type content =
>  | Data of string
>  | Elements of element list;;
>
> This is of course not a valid OCaml definition, but is there a way to
> express it ?

With each class comes a type of the same name. Such type can also be 
defined independently from the notion of class. It is syntactically like 
a record with angle brackets:

type element =
     < content : content >

and content =
     Data of string
   | Elements of element list;;

class element_class (c : content) =
object
   method content = c
end


# new element_class (Elements [ new element_class (Data "abc") ]);;
- : element_class = <obj>


Martin

> Many thanks in advance for your help,
> Sébastien.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

--
http://mjambon.com/

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

* Re: [Caml-list] mutually dependent class and type
  2008-09-25 22:24 mutually dependent class and type Sébastien Hinderer
  2008-09-25 23:41 ` [Caml-list] " Martin Jambon
@ 2008-09-26  6:40 ` Tim Rentsch
  2008-10-05 13:31 ` Yann Régis-Gianas
  2 siblings, 0 replies; 4+ messages in thread
From: Tim Rentsch @ 2008-09-26  6:40 UTC (permalink / raw)
  To: Sebastien.Hinderer; +Cc: caml-list

>  Date: Fri, 26 Sep 2008 00:24:39 +0200
>  From: =?iso-8859-1?Q?S=E9bastien?= Hinderer <Sebastien.Hinderer@ens-lyon.org>
>  
>  Is there a (clean) way to define simultaneously a class and a type that
>  are mutually recursive ?
>  Something like this :
>  class element (c : content) =
>  object
>    ...
>  end and type content =
>    | Data of string
>    | Elements of element list;;
>  
>  This is of course not a valid OCaml definition, but is there a way to
>  express it ?

Frequently a good way around such problems is to abstract the type
definition with respect to the mutually dependent defined item.
Then, supply the appropriate parameter when defining the class:

    type 'a generic_content =
     | Data of string
     | Elements of 'a list

    class element (c : element generic_content) =
       object
	  method c_value = c
       end

Voila!  The class is defined in terms of a type that depends on
the class.

For classes, this technique produces a nicer result if applied to
a class type rather than a class directly.  Then we can define
the desired type 'content' appropriately and use it in the
class definition:

    type 'a generic_content =
     | Data of string
     | Elements of 'a list

    class type element_t =
       object
	  method c_value : element_t generic_content
       end

    type content = element_t generic_content

    class element (c : content) =
       object
	  method c_value = c
       end

This technique of parameterizing a type definition with
respect to a dependent type is a good one to know;  it
also is sometimes useful with modules/functors.


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

* Re: [Caml-list] mutually dependent class and type
  2008-09-25 22:24 mutually dependent class and type Sébastien Hinderer
  2008-09-25 23:41 ` [Caml-list] " Martin Jambon
  2008-09-26  6:40 ` Tim Rentsch
@ 2008-10-05 13:31 ` Yann Régis-Gianas
  2 siblings, 0 replies; 4+ messages in thread
From: Yann Régis-Gianas @ 2008-10-05 13:31 UTC (permalink / raw)
  To: caml-list

Hello Sebastien,

you can use recursive module:

module rec I : sig
  class element : I.content ->  object end
  type content =
    | Data of string
    | Elements of I.element list
end = struct
  class element (c : I.content) = object end
  type content =
    | Data of string
    | Elements of I.element list
end

Best regards,

-- 
Yann Régis-Gianas



On Fri, Sep 26, 2008 at 12:24 AM, Sébastien Hinderer
<Sebastien.Hinderer@ens-lyon.org> wrote:
> Dear list,
>
> Is there a (clean) way to define simultaneously a class and a type that
> are mutually recursive ?
> Something like this :
> class element (c : content) =
> object
>  ...
> end and type content =
>  | Data of string
>  | Elements of element list;;
>
> This is of course not a valid OCaml definition, but is there a way to
> express it ?
>
> Many thanks in advance for your help,
> Sébastien.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

end of thread, other threads:[~2008-10-05 13:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-25 22:24 mutually dependent class and type Sébastien Hinderer
2008-09-25 23:41 ` [Caml-list] " Martin Jambon
2008-09-26  6:40 ` Tim Rentsch
2008-10-05 13:31 ` Yann Régis-Gianas

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