caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tim Rentsch <txr@alumni.caltech.edu>
To: Sebastien.Hinderer@ens-lyon.org
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] mutually dependent class and type
Date: Thu, 25 Sep 2008 23:40:49 -0700 (PDT)	[thread overview]
Message-ID: <200809260640.m8Q6enK4019826@alumnus.caltech.edu> (raw)
In-Reply-To: <20080925222439.GA7968@galois> (message from =?iso-8859-1?Q?S=E9bastien?= Hinderer on Fri, 26 Sep 2008 00:24:39 +0200)

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


  parent reply	other threads:[~2008-09-26  6:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-25 22:24 Sébastien Hinderer
2008-09-25 23:41 ` [Caml-list] " Martin Jambon
2008-09-26  6:40 ` Tim Rentsch [this message]
2008-10-05 13:31 ` Yann Régis-Gianas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200809260640.m8Q6enK4019826@alumnus.caltech.edu \
    --to=txr@alumni.caltech.edu \
    --cc=Sebastien.Hinderer@ens-lyon.org \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).