caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Raphael Proust <raphlalou@gmail.com>
To: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Recursive Parametric class type Typing
Date: Sat, 19 Mar 2011 21:36:24 +0100	[thread overview]
Message-ID: <201103192136.24416.raphlalou@gmail.com> (raw)
In-Reply-To: <20110319194533.GT20405@localhost>

Le samedi 19 mars 2011 20:45:33, Guillaume Yziquel a écrit :
> Le Saturday 19 Mar 2011 à 20:20:36 (+0100), Raphael Proust a écrit :
> > Le samedi 19 mars 2011 18:48:03, Guillaume Yziquel a écrit :
> > > Le Saturday 19 Mar 2011 à 16:48:40 (+0100), Raphael Proust a écrit :
> > > > Hi list,
> > > > 
> > > > Trying to bind a javascript library for js_of_ocaml, I encountered
> > > > the following pattern (here drastically simplified):
> > > > 
> > > > class type ['t] c =
> > > >   object
> > > >     method plus: 't -> unit
> > > >     method minus: unit -> 't
> > > >     method container: unit -> container
> > > >   end
> > > > and container =
> > > >   object
> > > >     method int: unit -> int c
> > > >     method string: unit -> string c
> > > >   end
> > > > 
> > > > The following error is raised at compile time:
> > > > Error: This type string should be an instance of type int
> > > > 
> > > > In the use case:
> > > > - instead of [int] and [string] there are six different [class
> > > > type]s; - [classe type]s have more methods; and
> > > > - there are additional recursive [class type]s
> > > > 
> > > > Why is there such a limitation on the types? Is there a work around
> > > > that wouldn't induce (too  much) code duplication?
> > > 
> > > The reason why there is this behaviour is unification within the 'and'
> > > declaration. method int says that 't in ['t] c is of type int,
> > > therefore it should also be an int c in method string instead of a
> > > string c.
> > 
> > It makes sens…
> > 
> > > Workaround: Use recursive modules. They are a solution for keeping
> > > recursion while breaking type unification. Something in the taste of:
> > > 
> > > yziquel@seldon:~$ ocaml
> > > […]
> > 
> > Because I want both [t] and [e] available (in fact there are 4 recursive
> > classes I want available), I made several recursive modules. Beside
> > having each type written twice, it still had a typing error:
> > 
> > Error: In the definition of Paper.paper, type
> > 
> >        Svg.circle_attr Elem.element
> >        should be
> >        'a Elem.element
> 
> Noticed it too. Do not know where that comes from.
> 
> > […]
> 
> A perhaps more natural way would be to use object types. Something like
> 
> type t = private < z : int e; u : string e >
> 
> is easier to declare, and easier to declare recursively. Drawbacks: you
> still have issues when you want to declare the meat of your classes; and
> doesn't work well with ocamldoc.

Well my classes don't have meat: they only serve as cast from js objects (sort
of…).

I used this approach (save the [private] inducing an Error about types not
having a row variable) and it works. I just  have to duplicate a few things due
to inheritance not being available.

Thanks,
-- 
______________
Raphaël Proust


  reply	other threads:[~2011-03-19 20:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-19 15:48 Raphael Proust
2011-03-19 17:48 ` Guillaume Yziquel
2011-03-19 19:20   ` Raphael Proust
2011-03-19 19:45     ` Guillaume Yziquel
2011-03-19 20:36       ` Raphael Proust [this message]
2011-03-19 20:02 ` Philippe Strauss

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=201103192136.24416.raphlalou@gmail.com \
    --to=raphlalou@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=guillaume.yziquel@citycable.ch \
    /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).