Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: williamc@paneris.org
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Recursive class types (long-standing question ...)
Date: Sat, 10 Jan 2004 13:43:33 +0900	[thread overview]
Message-ID: <20040110134333C.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20031230210229.ABA5C96BAB@beertje.william.bogus>

From: William Chesters <williamc@paneris.org>

> Here's something I've wondered about for a while and just encountered again.
> How come this class ..
> 
>     class virtual ['a] c = object
>       method virtual b: bool c
>     end
> 
> ... gets a constraint 'a = bool?  I.e. for instance this doesn't compile:
> 
>     class virtual ['a] c = object
>       method virtual b: bool c
>       method virtual i: int c
>           (* "This type int should be an instance of type bool" *)
>     end

This is almost a FAQ.
The deep reason is that ocaml object types are defined by structure,
and as a result only regular recursion is allowed. If we were to allow
polymorphic recursion in class types as required by your example, the
type could not be represented as a regular tree anymore.
(to be fully exact, in this particular case it is possible to
represent it as a regular tree, but this would break the stronger
invariant used by the compiler, which requires all occurences of a
type in its own expansion to have the same parameters)

Note that this applies only to structural types.
For defined types you can use polymorphic recursion.

type 'a t = { b: bool t }

> Note that the following does work in C++:
> 
>     template <class a>
>     struct c {
>       virtual c<bool> b() = 0;
>       virtual c<int> i() = 0;
>     };
> 
> I guess there are workarounds but it seems unnecessary that ML should find
> recursive parameterised classes harder than C++?

In C++ classes are not equal by structure, so the second case applies.

Jacques Garrigue

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


      reply	other threads:[~2004-01-10  4:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-30 21:02 William Chesters
2004-01-10  4:43 ` Jacques Garrigue [this message]

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=20040110134333C.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=williamc@paneris.org \
    /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