caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Didier Remy <remy@morgon.inria.fr>
To: Tom.Hirschowitz@ens-lyon.fr
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] sum types with objects
Date: 26 Nov 2003 14:37:05 +0100	[thread overview]
Message-ID: <m38llq3l08u.fsf@morgon.inria.fr> (raw)
In-Reply-To: <16324.62497.607234.359869@paille.cri2000.ens-lyon.fr>

Tom Hirschowitz <Tom.Hirschowitz@ens-lyon.fr> writes:

> when trying to implement lists with objects and classes, I run into
> the following issue.
> 
> The idea is to define a class type list, parameterized over the type
> 'a of elements, and containing the methods is_nil, hd, tl, map. Then,
> I planned to define two classes nil and cons for building objects of
> type list.

See polymorphic methods in the manual. 
http://caml.inria.fr/ocaml/htmlman/manual005.html#ss:polymorphic-methods

Still, you should use fold and not map:

    class type ['a] list = object
      method is_nil : bool
      method hd : 'a
      method tl : 'a list
      method fold  : 'b. ('a -> 'b -> 'b) -> 'b -> 'b
    end;;

    exception Nil;;
    class ['a] nil = object (self : 'a #list)
      method is_nil = true
      method hd = raise Nil
      method tl = raise Nil
      method fold f x = x
    end;;

    class ['a] cons h t = object (self : 'a #list)
      method is_nil = false
      method hd = h
      method tl : 'a list = t
      method fold f x = f self#hd (self#tl#fold f x)
    end;;

Otherwise, your are trying to define a type abbreviation 

    type ('a, ...) #list = 
         < hd : 'a
           tl : 'a list
           map : 'b. ('a -> 'b) -> ('b) list; ... >

    and 'b list = ('b, <>) list

which is not allowed because the parameter of list in the body is not the
same as the one in the declaration. Type inference will enforce 'b = 'a, 
which is not what you want.

This is not just a matter of typechecking, though.  In particular, 
if you had in mind an implementation of class cons of the form 

     class cons h t = 
       ...
       method map f = new cons (f self#hd) (self#tl#map f)
     end;;

you would then have problems inheriting, since the constructor "new cons" is
here the one of the class "cons" beeing defined and not the one of a
subclass. Hence, you would also need to abstract cons over the constructor
function of the subclass of cons beeing considered, which becomes closer to
(but still less general than) what fold actually does.

        Didier

-------------------
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:[~2003-11-26 13:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-26 18:42 Tom Hirschowitz
2003-11-26 13:37 ` Didier Remy [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=m38llq3l08u.fsf@morgon.inria.fr \
    --to=remy@morgon.inria.fr \
    --cc=Tom.Hirschowitz@ens-lyon.fr \
    --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).