From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 0220CBB9A for ; Sat, 22 Oct 2005 03:04:52 +0200 (CEST) Received: from vms044pub.verizon.net (vms044pub.verizon.net [206.46.252.44]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j9M14plq025803 for ; Sat, 22 Oct 2005 03:04:51 +0200 Received: from Nodens ([71.255.34.158]) by vms044.mailsrvcs.net (Sun Java System Messaging Server 6.2 HotFix 0.04 (built Dec 24 2004)) with ESMTPA id <0IOQ006I1LNY4OK2@vms044.mailsrvcs.net> for caml-list@yquem.inria.fr; Fri, 21 Oct 2005 20:04:50 -0500 (CDT) Date: Fri, 21 Oct 2005 21:04:47 -0400 From: "Stephen Brackin" Subject: Duplicate functionality? To: Message-id: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Mailer: Microsoft Office Outlook, Build 11.0.6353 Content-type: multipart/alternative; boundary="----=_NextPart_000_0000_01C5D683.124B9410" Thread-index: AcXWpJjOBCKJSp8tRgWjUwnVMbB9UA== X-Miltered: at concorde with ID 43599033.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; sml:01 sml:01 ocaml:01 ocaml:01 subtleties:01 subtleties:01 verizon:98 iym:98 lex:01 lex:01 modules:01 modules:01 objects:02 objects:02 programming:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.8 required=5.0 tests=DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST,HTML_MESSAGE autolearn=disabled version=3.0.3 This is a multi-part message in MIME format. ------=_NextPart_000_0000_01C5D683.124B9410 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit I've had extensive programming experience with C/Lex/Yacc/SML, some user's exposure to the SML module system, and a little Java programming experience, but I'm new to OCaml and didn't follow the OCaml Introduction's Objects subtleties. Would people please recommend documents, preferably on-line, that would explain the issues and intended uses for me? My biggest initial question is why OCaml has both a modules system and objects: Aren't they different ways of accomplishing the same things? Steve ------=_NextPart_000_0000_01C5D683.124B9410 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

I've had extensive programming experience = with C/Lex/Yacc/SML, some user's exposure to the SML module system, and a = little Java programming experience, but I’m new to OCaml and didn’t = follow the OCaml Introduction’s Objects subtleties.  Would people = please recommend documents, preferably on-line, that would explain the issues = and intended uses for me?

 

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

 

Steve

------=_NextPart_000_0000_01C5D683.124B9410-- From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id C0B08BB9A for ; Sat, 22 Oct 2005 05:35:26 +0200 (CEST) Received: from ptb-relay03.plus.net (ptb-relay03.plus.net [212.159.14.214]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j9M3ZQcJ014678 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sat, 22 Oct 2005 05:35:26 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay03.plus.net with esmtp (Exim) id 1ETAAG-0006D4-4y for caml-list@yquem.inria.fr; Sat, 22 Oct 2005 04:35:24 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Duplicate functionality? Date: Sat, 22 Oct 2005 04:31:17 +0100 User-Agent: KMail/1.7.2 References: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> In-Reply-To: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200510220431.18749.jon@ffconsultancy.com> X-Miltered: at nez-perce with ID 4359B37E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 ocaml:01 frog:98 wrote:01 modules:01 modules:01 caml:02 tend:02 checking:02 objective:02 objects:02 objects:02 dynamic:03 static:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Saturday 22 October 2005 02:04, Stephen Brackin wrote: > My biggest initial question is why OCaml has both a modules system and > objects: Aren't they different ways of accomplishing the same things? Both modules and objects allow you to encapsulate related definitions, yes. However, modules are much more static by nature and objects are much more dynamic, i.e. you get stronger static checking using modules than you do using objects, giving more comprehensible error messages and more robust and faster code. Consequently, OCaml programmers tend to prefer modules over objects where possible. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 8419EBB9A for ; Sat, 22 Oct 2005 07:49:30 +0200 (CEST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j9M5nSJ0020003 for ; Sat, 22 Oct 2005 07:49:29 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j9M5nHh8014061; Sat, 22 Oct 2005 14:49:18 +0900 (JST) Date: Sat, 22 Oct 2005 14:49:16 +0900 (JST) Message-Id: <20051022.144916.23305070.garrigue@math.nagoya-u.ac.jp> To: stephen.brackin@verizon.net Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Duplicate functionality? From: Jacques Garrigue In-Reply-To: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> References: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 4359D2E8.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 ocaml:01 subtyping:01 functors:01 recursive:01 constructors:01 functors:01 constructors:01 inference:01 subtyping:01 reuse:01 late-binding:01 verbose:01 model:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=DNS_FROM_RFC_ABUSE autolearn=disabled version=3.0.3 From: "Stephen Brackin" > 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 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 24C20BB9A for ; Sat, 22 Oct 2005 07:57:09 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j9M5v8lo027673 for ; Sat, 22 Oct 2005 07:57:08 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA09653 for ; Sat, 22 Oct 2005 07:57:08 +0200 (MET DST) Received: from mz1.forethought.net (mzpi3.forethought.net [216.241.36.12]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j9M5v6aY020708 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Sat, 22 Oct 2005 07:57:07 +0200 Received: from [216.241.35.41] (helo=[10.0.0.2]) by mz1.forethought.net with esmtp (Exim 4.51) id 1ETCNK-000176-K0 for caml-list@inria.fr; Fri, 21 Oct 2005 23:57:02 -0600 Message-ID: <4359D4B2.2000509@gushee.net> Date: Fri, 21 Oct 2005 23:57:06 -0600 From: Matt Gushee User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051002) X-Accept-Language: en-us, en MIME-Version: 1.0 To: caml-list@inria.fr Subject: Re: [Caml-list] Duplicate functionality? References: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> <200510220431.18749.jon@ffconsultancy.com> In-Reply-To: <200510220431.18749.jon@ffconsultancy.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4359D4B4.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 4359D4B2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 debugging:01 recursive:01 functor:01 foo:01 foo:01 ocaml:01 ...:98 wrote:01 wrote:01 extensible:01 modules:01 modules:01 checking:02 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Jon Harrop wrote: >> On Saturday 22 October 2005 02:04, Stephen Brackin wrote: >> > >>>>My biggest initial question is why OCaml has both a modules system and >>>>objects: Aren't they different ways of accomplishing the same things? > >> >> Both modules and objects allow you to encapsulate related definitions, yes. >> >> However, modules are much more static by nature and objects are much more >> dynamic, i.e. you get stronger static checking using modules than you do >> using objects, giving more comprehensible error messages and more robust and >> faster code. That's certainly true ... I've probably spent twice as much time debugging type errors in mutually recursive objects as I have on all other kinds of errors put together. On the other hand (as Jon's statement implies), it's much easier to write extensible code with objects. When you call a function on a module (except within a functor) you are using a specific implementation, whereas you can call an object method, say foo#display (), where foo can be any object with a 'display' method that matches the expected type. And even if modules and objects were functionally equivalent, I think one of the great things about OCaml is the freedom it gives you to structure your application in the best way for the problem. -- Matt Gushee Englewood, CO, USA From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 630DCBB9A for ; Sat, 22 Oct 2005 10:47:23 +0200 (CEST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.173]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j9M8lNri006142 for ; Sat, 22 Oct 2005 10:47:23 +0200 Received: from [212.227.126.160] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1ETF28-0003uU-00; Sat, 22 Oct 2005 10:47:20 +0200 Received: from [84.58.137.103] (helo=gate.lan.gerd-stolpmann.de) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1ETF28-0005wC-00; Sat, 22 Oct 2005 10:47:20 +0200 Received: from flakew.lan.gerd-stolpmann.de (flakew.lan.gerd-stolpmann.de [192.168.0.32]) by gate.lan.gerd-stolpmann.de (Postfix) with ESMTP id 0993EC0AD; Sat, 22 Oct 2005 10:47:20 +0200 (CEST) Subject: Re: [Caml-list] Duplicate functionality? From: Gerd Stolpmann To: Jon Harrop Cc: caml-list@yquem.inria.fr In-Reply-To: <200510220431.18749.jon@ffconsultancy.com> References: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> <200510220431.18749.jon@ffconsultancy.com> Content-Type: text/plain Date: Sat, 22 Oct 2005 10:47:19 +0200 Message-Id: <1129970839.27787.64.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.2.1.1 Content-Transfer-Encoding: 7bit X-Provags-ID: kundenserver.de abuse@kundenserver.de auth:a6865a839c0178d9aa0ce41878507ea2 X-Miltered: at concorde with ID 4359FC9B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 gerd:01 stolpmann:01 ocaml:01 gerd:01 stolpmann:01 viktoriastr:01 64293:01 darmstadt:01 thesis:01 modules:01 tend:02 telefon:02 objects:02 module:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 Am Samstag, den 22.10.2005, 04:31 +0100 schrieb Jon Harrop: > Consequently, OCaml programmers tend to prefer modules over objects where > possible. Once you get accustomed to the class system, which is definitely more complicated than the module system, this is no longer true. I'm the counterexample of your thesis. Gerd -- ------------------------------------------------------------ Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de Telefon: 06151/153855 Telefax: 06151/997714 ------------------------------------------------------------ From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 72E19BB9A for ; Sat, 22 Oct 2005 12:07:26 +0200 (CEST) Received: from sender-01.it.helsinki.fi (sender-01.it.helsinki.fi [128.214.205.139]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j9MA7PSC016905 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Sat, 22 Oct 2005 12:07:26 +0200 Received: from la.homedns.org (kruuna.helsinki.fi [128.214.205.14]) by sender-01.it.helsinki.fi (8.13.3/8.13.3) with ESMTP id j9MA7M6m002452 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Sat, 22 Oct 2005 13:07:23 +0300 Received: from la by la.homedns.org with local (Exim 4.54) id 1ETGHa-0001ka-Oc; Sat, 22 Oct 2005 13:07:22 +0300 Date: Sat, 22 Oct 2005 13:07:22 +0300 From: Lauri Alanko To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Duplicate functionality? Message-ID: <20051022100722.GA5833@la.iki.fi> References: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> <20051022.144916.23305070.garrigue@math.nagoya-u.ac.jp> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20051022.144916.23305070.garrigue@math.nagoya-u.ac.jp> User-Agent: Mutt/1.5.11 X-Miltered: at nez-perce with ID 435A0F5D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; lauri:01 alanko:01 caml-list:01 ocaml:01 icfp:01 icfp:01 lauri:01 wrote:01 slides:01 xleroy:01 jacques:01 modules:01 objects:02 garrigue:03 talks:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 On Sat, Oct 22, 2005 at 02:49:16PM +0900, Jacques Garrigue wrote: > I'm not aware of any detailed comparison between objects and modules > in ocaml. The slides from Xavier Leroy's talk at ICFP '99 might at least be useful to the original poster: http://pauillac.inria.fr/~xleroy/talks/icfp99.ps.gz Lauri From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 1B140BB9A for ; Sat, 22 Oct 2005 23:10:05 +0200 (CEST) Received: from conn.mc.mpls.visi.com (conn.mc.mpls.visi.com [208.42.156.2]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j9MLA355010565 for ; Sat, 22 Oct 2005 23:10:04 +0200 Received: from [192.168.42.2] (bhurt.dsl.visi.com [208.42.141.66]) by conn.mc.mpls.visi.com (Postfix) with ESMTP id 4246581D1; Sat, 22 Oct 2005 16:10:03 -0500 (CDT) Date: Sat, 22 Oct 2005 16:12:34 -0500 (CDT) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Stephen Brackin Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Duplicate functionality? In-Reply-To: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> Message-ID: References: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at nez-perce with ID 435AAAAB.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 ocaml:01 reals:01 integers:01 vectors:01 reals:01 rationals:01 subtraction:01 sig:01 vectors:01 val:01 subtraction:01 val:01 bool:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Fri, 21 Oct 2005, Stephen Brackin wrote: > My biggest initial question is why OCaml has both a modules system and > objects: Aren't they different ways of accomplishing the same things? No. And this is one of the most misunderstood things about Ocaml modules- that they are NOT just a weird and broken implementation of objects. A better point of reference would probably be C++ templates (although Ocaml modules fix the complaints I have with C++ templates). Modules are used for code that is "based on" other code. An example makes this clearer. Let's consider Newton's method for root find. This is an incredibly powerfull algorithm that can be applied to a huge number of different number systems. For example, it works on reals, it works on complexs, it works on quaterions, it works on integers, and it works on systems of non-linear equations (vectors and matricies). And when I say it works on reals, I mean it works on all representations of reals- it works on IEEE floating point, it works on arbitrary precision rationals, it works on arbitrary precision floats, it works on fixed precision floats, etc. All we need to implement Newton's method is a small set of basic operations. We need subtraction, we need to be able to solve Ax = b given A and b (which is just x = b/A in most number systems, but we express it this way as it's more "natural" if A is the Jacobian matrix of partial derivitives and b is the vector residual), and we need some way to say we're "close enough" to the solution that we can stop. With modules, we might write: module type Req = sig type t (* the basic type of the number system *) type dt (* the type of derivitives- generally, this will be the same * as t, but in the case of vectors/matricies, t is a vector * while dt is a matrix. *) type et (* The type of the error value. Often times this will be * the same as t above, but for "constructed" types * like complexes and vectors, type et will be whatever * real type they are made out of. *) val sub : t -> t -> t (* subtraction *) val solve : et -> dt -> t -> t (* Solve Ax = b to within epsilon. * Generally epsilon will be ignored, * and the solution will just be * x = b/A. *) val default_epsilon : et val within_epsilon : et -> t -> t -> bool (* Returns true if the two value differ by a factor of less * than epsilon. *) end;; module Make(Alg: Req) = struct exception Max_iters_reached of Alg.t let meth ?(epsilon = Alg.default_epsilon) ?(max_iters = max_int) f df x = (* Find a root to f given f and it's derivitive df starting * at an initial guess of x. *) let rec loop i x = if i > max_iters then raise (Max_iters_reached x) else let x' = Alg.sub x (Alg.solve epsilon (df x) (f x)) in if Alg.within_epsilon epsilon x x' then x' else loop (i+1) x' in loop 0 x end;; Now, we could probably implement something like this in objects- Newtons would be a base class calling out to virtual functions like solve and sub which would be implemented in concrete subclasses. But there are several large advantages to using modules in this case. The first one is types. Note that were I to implement Newtons with floating point numbers, like: module Float = struct type t = float type dt = float type et = float let sub = ( -. ) let solve (_: et) a b = b /. a let default_epsilon = 1.e-14;; let within_epsilon e x y = ((abs_float ((x -. y) /. x)) < e) || ((abs_float (x -. y)) < e) end;; module FloatNewtons = Make(Float);; Ocaml figures out the t, dt, and et are "all the same type", and I have a function with the right signature. This is doable with dynamic type checking and objects, but not static. Or at least I've never seen it done in a static language. Another advantage of modules over structures in the type arena is to be able to express the "these two objects need to be of the same implementation" as a type constraint. An example of this is the compare function in Map or Set. The compare function wants to gaurentee that the two maps or sets you are comparing have the same ordering. They can do that using modules and functors. Another advantage of modules and functors is, with ocamldefun, the ability to remove the layer of vituralization, and actually have myself a high-performance Newton's method implementation. Another advantage is that it allows objects to be objects, and stop having to be things they're not, like name spaces. Ocaml objects have a hard time implementing the Singleton pattern, for example. Of course they do- you should be using modules for that. Brian From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id ABAB9BB81 for ; Mon, 24 Oct 2005 05:14:25 +0200 (CEST) Received: from web26803.mail.ukl.yahoo.com (web26803.mail.ukl.yahoo.com [217.146.176.79]) by nez-perce.inria.fr (8.13.0/8.13.0) with SMTP id j9O3EPOc030520 for ; Mon, 24 Oct 2005 05:14:25 +0200 Received: (qmail 56992 invoked by uid 60001); 24 Oct 2005 03:14:24 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.de; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=qHll4itIhkOLUy6L8j+yb0piVaoKlRcX/GGlaKw9BRV3D0EIga+YvqvvZTKvlc3ANIUTjb67yiLGJb3Y/shHhd8SbXWyYG6t6jYYTQK60DQhPJZD4KOsnQJREiP9HBCNjtR54wUdLUBQ0sEhxN3WptpYN6HRGzUuKSKq6kDsX9w= ; Message-ID: <20051024031424.56990.qmail@web26803.mail.ukl.yahoo.com> Received: from [83.176.49.18] by web26803.mail.ukl.yahoo.com via HTTP; Mon, 24 Oct 2005 05:14:24 CEST Date: Mon, 24 Oct 2005 05:14:24 +0200 (CEST) From: Martin Chabr Subject: Ant: [Caml-list] Duplicate functionality? To: Stephen Brackin Cc: caml-list@yquem.inria.fr In-Reply-To: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Miltered: at nez-perce with ID 435C5191.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 o'reilly:01 ocaml:01 chailloux:01 manoury:01 compares:01 oreilly-book:01 ocaml:01 sml:01 sml:01 subtleties:01 caml-list:01 beginner's:01 beginners:01 bug:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=DNS_FROM_RFC_ABUSE autolearn=disabled version=3.0.3 There have alrady been some excellent answers to your question up to now, but I would still like to draw your attention to the O'Reilly book on OCaml by Emmanuel Chailloux, Pascal Manoury and Bruno Pagano. The english translation is available for free on the Web. The book uses both the module and the object approach and compares them as well: http://caml.inria.fr/pub/docs/oreilly-book/ Another document which describes both is the Introduction to OCaml by Jason Hickey, obtainable as a pdf on the web as well: http://www.nuprl.org/documents/Hickey/02caltech-ocaml.html I hope this helps. Martin --- Stephen Brackin schrieb: > I've had extensive programming experience with > C/Lex/Yacc/SML, some user's > exposure to the SML module system, and a little Java > programming experience, > but I'm new to OCaml and didn't follow the OCaml > Introduction's Objects > subtleties. Would people please recommend > documents, preferably on-line, > that would explain the issues and intended uses for > me? > > > > My biggest initial question is why OCaml has both a > modules system and > objects: Aren't they different ways of accomplishing > the same things? > > > > Steve > > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: > http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 15443BB9A for ; Tue, 25 Oct 2005 18:25:54 +0200 (CEST) Received: from arbois.inria.fr (arbois.inria.fr [128.93.11.104]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j9PGPmgs026559 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 25 Oct 2005 18:25:48 +0200 Received: from arbois.inria.fr (localhost.inria.fr [127.0.0.1]) by arbois.inria.fr (8.12.11/8.12.11) with ESMTP id j9PGPmXt018374; Tue, 25 Oct 2005 18:25:48 +0200 Received: (from remy@localhost) by arbois.inria.fr (8.12.11/8.12.11/Submit) id j9PGPmib018373; Tue, 25 Oct 2005 18:25:48 +0200 Date: Tue, 25 Oct 2005 18:25:48 +0200 From: Didier Remy To: Martin Chabr Cc: Stephen Brackin , caml-list@yquem.inria.fr Subject: Re: Ant: [Caml-list] Duplicate functionality? Message-ID: <20051025162539.GA18326@arbois.inria.fr> Reply-To: Didier.Remy@inria.fr References: <0IOQ006I7LO24OK2@vms044.mailsrvcs.net> <20051024031424.56990.qmail@web26803.mail.ukl.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20051024031424.56990.qmail@web26803.mail.ukl.yahoo.com> User-Agent: Mutt/1.4.1i Organization: INRIA, BP 105, F-78153 Le Chesnay Cedex Phone: (33) 1 3963 5317 -- Sec: (33) 1 3963 5207 -- Fax: (33) 1 3963 5193 Web: http://cristal.inria.fr/~remy X-Miltered: at nez-perce with ID 435E5C8C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; didier:01 didier:01 caml-list:01 appsem:01 appsem:01 ...:98 modules:01 remy:01 remy:01 caml:02 objects:02 inria:05 inria:05 overlapping:06 cgi:91 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 And also http://pauillac.inria.fr/~remy/cours/appsem/ http://pauillac.inria.fr/~remy/cours/appsem/ocaml-mixins.html or... http://caml.inria.fr//cgi-bin/search.en.cgi?words=overlapping+objects+modules Didier