caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Dmitry Grebeniuk <gdsfh1@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] recursive module, object types, tying knot
Date: Thu, 8 Sep 2011 06:44:02 +0900	[thread overview]
Message-ID: <14203FCD-811F-41CC-A0D7-5A360D4815CA@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAPi0vKXHQ3SyChtphJ7OAtAnDNK5TpfXqq_nKBF7aA0nGHovHw@mail.gmail.com>

On 2011/09/08, at 6:29, Dmitry Grebeniuk wrote:

> Hello.
> 
>  I have an usual question.  I want to make some object
> types (container-like) that can map the stored contents
> with its method.  For example, let the container will be
> the simple list:
> class ['a] (lst : list 'a) = object ...
>  method map : ('a -> 'b) -> 'b lst
> 
>  The compiler yields an error:
> # class ['a] lst x = object
>   method map : 'b . ('a -> 'b) -> 'b lst
>   = fun f -> List.map f x end;;
> Error: This type scheme cannot quantify 'b :
> it escapes this scope.

The answer is unfortunately short: there is no solution.
Namely recursive types in ocaml must be regular.
I.e., they must expand to a finite graph.
In particular this means that, inside the class lst, all occurences
of lst must have exactly the parameter 'a.
As you have already found, you can avoid this problem by
defining a record which "hides" the use of lst.
Namely, you must break the cycle with a datatype,
either record or sum type.
Unfortunately this also means that this type has to belong
to some module, and you also lose subtyping.

Jacques Garrigue

  reply	other threads:[~2011-09-07 21:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-07 21:29 Dmitry Grebeniuk
2011-09-07 21:44 ` Jacques Garrigue [this message]
2011-09-09 11:14   ` Dmitry Grebeniuk
2011-09-09 13:50     ` Jacques Garrigue
2011-09-13  4:56       ` Dmitry Grebeniuk
2011-09-13  7:22         ` Gabriel Scherer

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=14203FCD-811F-41CC-A0D7-5A360D4815CA@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=gdsfh1@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).