From: "Krishnaswami, Neel" <neelk@cswcasa.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] A G'Caml question" + additional info
Date: Wed, 11 Jul 2001 19:10:47 -0400 [thread overview]
Message-ID: <B1E4D3274D57D411BE8400D0B783FF322E8657@exchange1.cswv.com> (raw)
Brian Rogoff [mailto:bpr@best.com] wrote:
> >
> > For instance, I recently wrote yet another set
> > implementation, because the functorial interface to the Set module
> > in the standard library wouldn't let me use it in a fully polymorphic
> > fashion. If the Set library had been written using OCaml's object
> > system, then I would not have had to redo my own.
>
> That's odd. If the Set library had been implemented using the object
> system, it seems that you would have less (parametric) polymorphism
> since OCaml doesn't (yet?) have polymorphic methods.
I don't know enough type theory to argue, except that in practice
I seem to be getting more polymorphic types with objects than with
functors. I guess I'll go ahead and post the example -- it's possible
that I just don't know enough yet!
So, let's take the example of a priority queue implemented using a
binary search tree. The usual functorial approach to this looks like:
module type ORD =
sig
type t
val cmp : t -> t -> bool
end
module Queue(Elt : ORD) =
struct
type elt = Elt.t
type t = Nil | Tree of t * elt * t
let empty = Nil
let rec min = function
| Nil -> raise Not_found
| Tree(Nil, x, r) -> x
| Tree(l, x, r) -> min l
let rec add x = function
| Nil ->
Tree(Nil, x, Nil)
| Tree(l, y, r) as t ->
if Elt.cmp x y then
Tree(add x l, y, r)
else if Elt.cmp y x then
Tree(l, y, add x r)
else
Tree(l, x, r)
end
To specialize this a structure satisfying the ORD signature is
applied to the functor. The obvious approach with classes looks
very similar, except that the Elt functor argument becomes a method
to be overridden:
type 'a tree = Nil | Tree of 'a tree * 'a * 'a tree
class virtual ['a] q =
object(self)
val tree = Nil
method virtual cmp : 'a -> 'a -> bool
method min =
let (<) = self#cmp in
let rec loop = function
| Nil -> raise Not_found
| Tree(Nil, x, r) -> x
| Tree(l, x, r) -> loop l
in loop tree
method add x =
let (<) = self#cmp in
let rec loop = function
| Nil ->
Tree(Nil, x, Nil)
| Tree(l, y, r) as t ->
if x < y then
Tree(loop l, y, r)
else if y < x then
Tree(l, y, loop r)
else
Tree(l, x, r)
in
{< tree = loop tree >}
end
To specialize this we just subclass and add a cmp method. So far
so good.
However, suppose I have a type
type 'a tag = int * 'a
This is a value that has an integer priority plus some arbitrary
data, and I'd like to make a priority queue that stores tagged
values. Since the type 'a tag is polymorphic, AFAICT there's no
way of writing a structure that satisfies the ORD signature.
However, writing a class that can accept this is easy --
class ['a] taggedq =
object
inherit ['a] q
constraint 'a = 'b tag
method cmp = fun a b -> (fst a) < (fst b)
end
will Just Work(tm). This is why I called the class "more polymorphic."
If my terminology is wrong, corrections will be gratefully accepted.
--
Neel Krishnaswami
neelk@cswcasa.com
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
next reply other threads:[~2001-07-11 23:08 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-07-11 23:10 Krishnaswami, Neel [this message]
2001-07-12 0:08 ` Brian Rogoff
-- strict thread matches above, loose matches on Subject: below --
2001-07-13 13:12 Krishnaswami, Neel
2001-07-13 13:35 ` Markus Mottl
2001-07-12 21:30 Krishnaswami, Neel
2001-07-13 9:34 ` Markus Mottl
2001-07-11 22:23 Krishnaswami, Neel
2001-07-11 22:47 ` Brian Rogoff
2001-07-12 9:37 ` Markus Mottl
2001-07-14 2:04 ` John Max Skaller
2001-07-14 3:00 ` Alexander V. Voinov
2001-07-14 15:00 ` John Max Skaller
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=B1E4D3274D57D411BE8400D0B783FF322E8657@exchange1.cswv.com \
--to=neelk@cswcasa.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