caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: alex@baretta.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] What about polymorphic methods
Date: Mon, 10 Jun 2002 19:13:55 +0900	[thread overview]
Message-ID: <20020610191355R.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <3D01D3D3.701@baretta.com>

From: Alessandro Baretta <alex@baretta.com>
> > With the CVS version, you would have to write
> > 
> >    method left_iter : 'b. ('a -> 'b) -> 'b list = fun f -> ...
> >    method right_iter : 'b. ('a -> 'b) -> 'b list = fun f -> ...
>
> I do not understand why I have to declare 'b explicitly as a 
> "polymorphic" type variable for the two iterators. If I omit 
> the explicit generalization of 'b in my bidirectional list 
> class, the compiler yields the following error message:
>  > Some type variables are unbound in this type:
>  >   class ['a] bidi_list :
>  >    'a list ->
>  >     object
>  >       val first : 'a bidi
>  >       val last : 'a bidi
>  >       method head : 'a bidi
>  >       method left_iter : ('a -> 'b) -> 'b list
>  >       method right_iter : ('a -> 'c) -> 'c list
>  >       method tail : 'a bidi
>  >     end
>  > The method left_iter has type ('a -> 'b) -> 'b list where
>  > 'b is unbound
> 
> To me, this means that the type checker is able to correctly 
> determine the type of the class and of the polymorphic 
> methods. It simply refuses to generalize unbound type 
> variables without "written consent" from the programmer.

There are two problems. One is theoretical: while implicit
polymorphisation would work in many cases, it would not work in all
cases. For instance, it does not work with polymorphic recursion:
you would be only able to call inherited methods recursively.
And it's not always clear whether the extra polymorphism was really
intended: as polymorphic methods are not compatible with
non-polymorphic ones, choosing between the two is clearly not
principal. Requiring an annotation is on the safe side.

On the practical side, class typing relies heavily on unification.
This is neat: you can view inheritance as unifying with #c, from a
typing point of view.  This means that once you've assumed a
non-polymorphic type (as you usually do to type recursion), you cannot
go back to the polymorphic one (does not unify).  This problem is only
technical, and it also means you cannot refine method types in
subclasses, so I'm no sure it will stay forever.

Also, I have a feeling that classes in caml are an advanced feature.
They can help you modelling easy to use libraries, or building big
programs.  In both cases, the extra verbosity is only on the producer
side, while the consumer can profit from the extra comfort at very
little cost. For instance with my scheme you don't need annotations in
subclasses.

So I hope these annotations will not mean lots of trouble.
Yet, this is a new feature, and experience will tell us.
In particular if beginners start writing visitor patterns all around,
this may not work that well.

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:[~2002-06-10 10:15 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-25  3:45 [Caml-list] What about polymorphic methods? Alessandro Baretta
2002-05-25  3:55 ` Jacques Garrigue
2002-05-25  5:42   ` John Prevost
2002-05-27 19:10   ` Alessandro Baretta
2002-05-27 18:22     ` John Max Skaller
2002-05-30  0:02       ` Alessandro Baretta
2002-05-27 18:27     ` [Caml-list] Possible use for camlp4 John Max Skaller
2002-05-27 18:35       ` Alexander V. Voinov
2002-05-27 19:33       ` Daniel de Rauglaudre
2002-05-29 19:25         ` John Max Skaller
2002-05-30  2:42           ` Jacques Garrigue
2002-05-30  8:15           ` Daniel de Rauglaudre
2002-05-27 19:37       ` Daniel de Rauglaudre
2002-06-02  8:34   ` [Caml-list] What about polymorphic methods? Lauri Alanko
2002-06-03 23:57     ` Jacques Garrigue
2002-06-08  9:52   ` [Caml-list] What about polymorphic methods Alessandro Baretta
2002-06-10 10:13     ` 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=20020610191355R.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=alex@baretta.com \
    --cc=caml-list@inria.fr \
    /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).