Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Peng Zang <peng.zang@gmail.com>
To: caml-list@yquem.inria.fr
Subject: typeclasses in OCaml
Date: Fri, 22 Aug 2008 08:54:09 -0400	[thread overview]
Message-ID: <200808220854.12598.peng.zang@gmail.com> (raw)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I have been using the Object system in OCaml for the equivalent functionality 
of haskell typeclasses for some time.  It allows nice things like:

  class type ['a] foldable = object
    method fold : 'z. ('z -> 'a -> 'z) -> 'z -> 'z
  end

  let forall (f:'a -> bool) (obj:'a #foldable) = 
    obj#fold (fun acc x -> acc && f x) true


Recently however, I tried to make a "mappable" class.  Something like:

  class type ['a] mappable = object('self)
    method map : 'z. ('a -> 'z) -> 'z 'self
  end

Which of course is invalid syntax.  However, I think the point is clear.  I 
want to specify the type of all container objects that can map their 
contents.  How do you specify this in OCaml?

I realize there are some inherent issues, eg. polymorphic classes are 
invarient.  So I'm willing to accept some limited use of magic as long as it 
can be hidden away and offer a type-safe interface.  Nevertheless, after half 
a day of trying, I cannot seem to figure this out.  I don't even know how to 
specify:

  "  a 'z version of 'self  "

In a syntactically correct way.  Is this possible?  Thank in advance,

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIrrb0fIRcEFL/JewRAuDZAJ4zGo7+B6dF0YbnUFwQPGW+2MenZACfbAJ6
OKVIlsPpwkVwOEefT8KiRMU=
=+8yF
-----END PGP SIGNATURE-----


             reply	other threads:[~2008-08-22 12:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-22 12:54 Peng Zang [this message]
2008-08-23  2:57 ` [Caml-list] " Jacques Garrigue
2008-08-23 12:50   ` Peng Zang

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=200808220854.12598.peng.zang@gmail.com \
    --to=peng.zang@gmail.com \
    --cc=caml-list@yquem.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