caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
To: Jacques Le Normand <rathereasy@gmail.com>
Cc: caml-list caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] recursive types
Date: Mon, 24 Mar 2008 08:37:33 +0000	[thread overview]
Message-ID: <47E7684D.5050201@ed.ac.uk> (raw)
In-Reply-To: <f74178430803232016g5953f457yf93502a81e30769a@mail.gmail.com>

Jacques Le Normand wrote:
> is it possible to have mutually recursive classes and types?

Yes, using recursive modules.  The syntax is a little heavy, though, 
since you have to write some of the definitions in both the signature 
and the implementation.

> class type node_wrapper =
> object
>   method identify : string
>   method get_child_location : location
> end
> 
> class virtual nodeable =
> object(self)
>   method virtual to_node_wrapper : node_wrapper
> end
> 
> type path = (nodeable list * location * nodeable list) option
> and location = Loc of nodeable * path

This might be written as

   module rec M :
   sig
     class type node_wrapper =
     object
       method identify : string
       method get_child_location : M.location
     end

     class virtual nodeable :
     object
       method virtual to_node_wrapper : node_wrapper
     end

     type path = (nodeable list * M.location * nodeable list) option
     type location = Loc of nodeable * path
   end =
   struct
     class type node_wrapper =
     object
       method identify : string
       method get_child_location : M.location
     end

     class virtual nodeable =
     object
       method virtual to_node_wrapper : node_wrapper
     end

     type path = (nodeable list * M.location * nodeable list) option
     type location = Loc of nodeable * path
   end

The definitions which merely define type aliases (node_wrapper and path) 
can be shortened in the implementation section if you prefer:

   module rec M :
   sig
      [elided]
   end =
   struct
     class type node_wrapper = M.node_wrapper

     class virtual nodeable =
     object
       method virtual to_node_wrapper : node_wrapper
     end

     type path = M.path
     type location = Loc of nodeable * path
   end

Jeremy.


  parent reply	other threads:[~2008-03-24  8:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-24  3:16 Jacques Le Normand
2008-03-24  3:51 ` [Caml-list] " Erik de Castro Lopo
2008-03-24  3:51 ` Erik de Castro Lopo
2008-03-24  8:37 ` Jeremy Yallop [this message]
2008-03-24  9:02 ` Remi Vanicat
     [not found] <20050506044107.1698.70519.Mailman@yquem.inria.fr>
2005-11-15 22:44 ` Recursive types Swaroop Sridhar
2005-11-15 23:40   ` [Caml-list] " Jacques Garrigue
2005-11-16  2:20     ` Keiko Nakata
2005-11-16  6:47       ` Alain Frisch
2005-11-16  7:40         ` Keiko Nakata
2005-11-16  8:55           ` Jacques Garrigue
2005-11-17  1:45             ` Keiko Nakata
2005-11-16  3:28     ` Swaroop Sridhar
2005-11-16  8:38       ` Jacques Garrigue
2005-11-16 23:00         ` Swaroop Sridhar
2005-11-16 23:56           ` Swaroop Sridhar
  -- strict thread matches above, loose matches on Subject: below --
2004-12-13  9:44 recursive types nakata keiko
2004-12-13  9:58 ` [Caml-list] " Damien Pous
2004-12-13 12:31   ` skaller

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=47E7684D.5050201@ed.ac.uk \
    --to=jeremy.yallop@ed.ac.uk \
    --cc=caml-list@yquem.inria.fr \
    --cc=rathereasy@gmail.com \
    /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).