From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.1 required=5.0 tests=AWL,SPF_SOFTFAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 27C52BBC4 for ; Wed, 4 Mar 2009 15:08:48 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: At8BAFYarknBvsFKmWdsb2JhbACVAQEBAQEBCAsKBxHCUIQIBg X-IronPort-AV: E=Sophos;i="4.38,301,1233529200"; d="scan'208";a="23849281" Received: from dnsp.umh.ac.be (HELO hermes1.umh.ac.be) ([193.190.193.74]) by mail3-smtp-sop.national.inria.fr with ESMTP; 04 Mar 2009 15:08:47 +0100 Received: from poincare.swapping.umh.ac.be (poincare.swapping.umh.ac.be [10.102.100.68]) by hermes1.umh.ac.be (8.14.2/8.13.6) with ESMTP id n24E8V0t651296; Wed, 4 Mar 2009 15:08:32 +0100 Received: from localhost ([127.0.0.1] ident=trch) by poincare.swapping.umh.ac.be with esmtp (Exim 4.69) (envelope-from ) id 1Lerm5-0005gt-D4; Wed, 04 Mar 2009 15:08:41 +0100 Date: Wed, 04 Mar 2009 15:08:41 +0100 (CET) Message-Id: <20090304.150841.990102838962322450.Christophe.Troestler+ocaml@umons.ac.be> To: bhurt@spnz.org, jon@ffconsultancy.com, caml-list@yquem.inria.fr Subject: Re: [Caml-list] stl? From: Christophe TROESTLER In-Reply-To: References: <200903040159.48574.jon@ffconsultancy.com> X-Face: #2fb%mPx>rRL@4ff~TVgZ"<[:,oL"`TUEGK/[8/qb58~C>jR(x4A+v/n)7BgpEtIph_neoLKJBq0JBY9:}8v|j Organization: University of Mons-Hainaut X-Mailer: Mew version 6.2.51 on Emacs 22.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.64 on 193.190.193.76 X-Spam: no; 0.00; stl:01 christophe:01 troestler:01 christophe:01 troestler:01 ocaml:01 reimplement:01 haskell's:01 ocaml:01 sig:01 val:01 val:01 elided:01 delimited:01 overloading:01 On Wed, 4 Mar 2009 01:11:18 -0500, Brian Hurt wrote: > > Try this. Let's reimplement Haskell's Fractional class, in Ocaml, and > then do Newton's method using this new Fractional class. [...] > > module type Fractional = sig > type t > val ( +. ) : t -> t -> t > val ( -. ) : t -> t -> t > val ( *. ) : t -> t -> t > val ( /. ) : t -> t -> t > val fabs : t -> t > (* other functions elided from brevity *) > end;; Small point: you forgot the comparison functions — they are necessary for Ratio. > [...] once you realize that the definitions of Fractional, > FloatFractional, and RatioFractional all should be in the standard > library (and the latter two should be just called Float and Ratio). Well, they sort of are in the standard library already thanks to Delimited Overloading! http://pa-do.forge.ocamlcore.org/ With it, your code can simply be written as a macro: DEFINE NEWTON(M) = M.( let rec newton epsilon f df x = let x' = x - f x / df x in if abs(x - x') < epsilon then x' else newton epsilon f df x' in newton) let float_newton = NEWTON(Float) let ratio_newton = NEWTON(Ratio) (and with no performance lost at all for those concerned with clock cycles). I have released a new version of Delimited Overloading with that new example (ad-hoc-newton.ml). Enjoy, ChriS