caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Dmitri Boulytchev <dboulytchev@gmail.com>
Cc: Alain Frisch <alain@frisch.fr>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Confusing behaviour of type inference for polymorphic classes.
Date: Sun, 8 Dec 2013 01:15:38 +0000	[thread overview]
Message-ID: <CAAxsn=EmYQjqOn1psUsZGUqRc2=SfR4F+Dk7kKqZFAzCAf=OoQ@mail.gmail.com> (raw)
In-Reply-To: <529E198F.7060704@gmail.com>

On 3 December 2013 17:49, Dmitri Boulytchev <dboulytchev@gmail.com> wrote:
>   Here subm is a legitimate subclass of m with the desired behaviour [...]. So I don't see why I'm loosing the ability to subtype.

You're quite right, of course: you still have subtyping.  What you're
giving up are some of the benefits of inheritance, since the
"recursive" calls go through the instance you pass in rather than
through a self variable.  If you don't have other reasons for using
objects and classes it might be worth considering using records
instead, since they're semantically much simpler.  The example should
translate directly:

  type ('a, 'b, 'ta, 'tb) m' =  {
    t : ('a -> 'ta) -> ('b -> 'tb) -> ('a, 'b) t -> ('ta, 'tb) t
  }

  let proto_m f = {
    t = fun fa fb s ->
    ...

  let m' f =
    let super = proto_m f in {
      super with t = fun fa fb s ->
    ...

> It seems to me that this workaround can be used to overcome the "regular class" restriction completely in a syntax-conversion manner. I appreciate any comments.

Yes, it's a neat approach!  One small suggestion: in order to handle
the case where the type you're traversing has multiple recursive
instantations with different parameters you might consider passing the
instance creation function in a way that allows it to be called
polymorphically (e.g. by wrapping it in a record with a polymorphic
field):

  type new_proto_m = {
     new_proto_m : 'a 'b 'ta 'tb .
         unit -> <t : ('a -> 'ta) -> ('b -> 'tb) ->
                      ('a, 'b) t -> ('ta, 'tb) t>
  }

There is one last thing I'm curious about.  The difficulties that
you're avoiding with open recursion seem to arise from the
representation of the traversal as a parameterised class with a
monomorphic method.  Have you considered switching things round and
using an unparameterised class with polymorphic methods instead?  I
think everything turns out much simpler if you do: you can use
straightforward inheritance, calls through self, etc.

  class m =
  object (self)
    method t : 'a 'b 'ta 'tb. ('a -> 'ta) -> ('b -> 'tb) ->
                              ('a, 'b) t -> ('ta, 'tb) t =
      fun fa fb s ->
        match s with
        | A (a, bat) -> A (fa a, self#t fb fa bat)
        | B  a       -> B (fa a)
  end

The map and fold generators supplied with Camlp4 use a variant of this
approach, and it seems to work well in practice.

  reply	other threads:[~2013-12-08  1:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-01 21:33 Dmitri Boulytchev
2013-12-02 14:41 ` Goswin von Brederlow
2013-12-02 15:05   ` Dmitri Boulytchev
2013-12-05 15:13     ` Goswin von Brederlow
2013-12-02 15:24 ` Jeremy Yallop
2013-12-03  8:35   ` Alain Frisch
2013-12-03 10:17     ` Jeremy Yallop
2013-12-03 12:33       ` Alain Frisch
2013-12-03 12:58         ` Jeremy Yallop
2013-12-03 17:49           ` Dmitri Boulytchev
2013-12-08  1:15             ` Jeremy Yallop [this message]
2013-12-09 13:48               ` Dmitri Boulytchev

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='CAAxsn=EmYQjqOn1psUsZGUqRc2=SfR4F+Dk7kKqZFAzCAf=OoQ@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=alain@frisch.fr \
    --cc=caml-list@inria.fr \
    --cc=dboulytchev@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).