caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: stephen.brackin@verizon.net
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Duplicate functionality?
Date: Sat, 22 Oct 2005 14:49:16 +0900 (JST)	[thread overview]
Message-ID: <20051022.144916.23305070.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net>

From: "Stephen Brackin" <stephen.brackin@verizon.net>

> My biggest initial question is why OCaml has both a modules system and
> objects: Aren't they different ways of accomplishing the same things?

I'm not aware of any detailed comparison between objects and modules
in ocaml. However I'll try to give my thoughts on that.

Both modules and object have multiple roles, and they only overlap
for some.
For instance, modules offer some kind of namespace management, which
objects do not. This is different from Java which has no modules, and
where classes have a role in namespace management.
Similarly object subtyping allows the creation of heterogeneous
collections of values, which is impossible with modules.

Another specificity of objects is their extensibility through
inheritance, with late binding (i.e. components can depend on each
other.) You can try to do this with functors and recursive modules,
but this quickly becomes rather heavy-weight.

The real overlap between the two concepts is in their ability to
create collections of methods, and to have parameterized constructors
(functors for modules). 
Indeed, apart of the capacity to hide types, one could argue that
objects alone can encode the whole module system, thanks to
polymorphic methods. Practice shows that it ends up being rather
clumsy: type parameters can only be expressed by type variables, which
do not behave exactly like type constructors.  Moreover object typing
relies strongly on type inference, which can cause confusing error
messages.
Encoding objects into modules, as abstract data types, generally works
well, but you lose object subtyping, and you must specify the module
name for each method call (a light burden in general, but not so nice
when using functors, and you end up with lots of module names around.)

So yes, one could say that objects and modules provide different ways
to do some similar things. This is particularly true for simple data
structures like stacks or queues: there is no good reason to use one
approach over the other.
In practice they provide very different tradeoffs between flexibility
and robustness. Modules are extremely robust, in that you are so
explicit that the origin of any problem should be immediately
clear. But this also means more verbosity, and some difficulties in
reuse: you may have to parameterize on more modules than you
would like to.  On the other hand objects let you pass your
implementation around with your data, making things much simpler at
first. But their typing being more brittle, it can hurt when something
goes wrong.

Since one uses ML mostly for robustness, I suppose that when there is
no explicit need for subtyping or late-binding, the natural way to go
is modules. But when they become so verbose as to be cumbersome, and
the underlying model is object-oriented, objects may be more
comfortable. 
Both approaches expose their weaknesses when pattern-matching is
involved: this essential feature of functional programming doesn't
like any form of abstraction.

By the way, I believe there is one further hurdle when using objects:
their definitions are not compositional. That is, a module is just a
collection of functions, which can exist themselves independently from
one another (yet not independently of type defintions), but methods
are not just functions (for instance you cannot pass the complete self to
a function), and they cannot exist independently from objects. This
makes harder the transition from the core language, where one usually
starts writing, to the object language, where one might want to move.

Jacques Garrigue


  parent reply	other threads:[~2005-10-22  5:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-22  1:04 Stephen Brackin
2005-10-22  3:31 ` [Caml-list] " Jon Harrop
2005-10-22  5:57   ` Matt Gushee
2005-10-22  8:47   ` Gerd Stolpmann
2005-10-22  5:49 ` Jacques Garrigue [this message]
2005-10-22 10:07   ` Lauri Alanko
2005-10-22 21:12 ` Brian Hurt
2005-10-24  3:14 ` Ant: " Martin Chabr
2005-10-25 16:25   ` Didier Remy

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=20051022.144916.23305070.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=stephen.brackin@verizon.net \
    /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).