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.3 required=5.0 tests=SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 31E2DBC0A for ; Fri, 19 Jan 2007 10:39:28 +0100 (CET) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by discorde.inria.fr (8.13.6/8.13.6) with SMTP id l0J9dRh1022263 for ; Fri, 19 Jan 2007 10:39:27 +0100 Received: (qmail invoked by alias); 19 Jan 2007 09:39:26 -0000 Received: from dialin-145-254-250-151.pools.arcor-ip.net (EHLO dialin-145-254-250-151.pools.arcor-ip.net) [145.254.250.151] by mail.gmx.net (mp041) with SMTP; 19 Jan 2007 10:39:26 +0100 X-Authenticated: #11134691 Received: from dirk by feanor.private-host.de with local (masqmail 0.2.20) id 1H7q1I-10R-00 for ; Fri, 19 Jan 2007 10:26:48 +0100 Date: Fri, 19 Jan 2007 10:26:48 +0100 To: caml-list@yquem.inria.fr Subject: Re: Fwd: [Caml-list] Polymorphic Variants Message-ID: <20070119092648.GA3604@feanor> References: <200701182114.10133.jon@ffconsultancy.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200701182114.10133.jon@ffconsultancy.com> User-Agent: Mutt/1.5.6+20040523i From: Dirk Thierbach X-Y-GMX-Trusted: 0 X-Miltered: at discorde with ID 45B091CF.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; variants:01 sqrt:01 haskell's:01 forall:01 forall:01 sqrt:01 vectors:01 matrices:01 overload:01 literals:01 translated:01 overloading:01 runtime:01 compiler:01 inlining:01 On Thu, Jan 18, 2007 at 09:14:09PM +0000, Jon Harrop wrote: > On Thursday 18 January 2007 16:23, Tom wrote: >> No... i rather thought it that way: >> x is anything >> x * x is either int or float, so x is either int or float >> x * x + x * x is either int or float, so the two (x * x) are either >> both int or both float >> sqrt accepts a float argument, so x * x + x * x must be float, so (x * >> x) must be float, so x must be float. BTW, that's what Haskell's type classes do: Prelude> :t \x -> x \x -> x :: forall t. t -> t Prelude> :t \x -> x * x \x -> x * x :: forall a. (Num a) => a -> a Prelude> :t \x -> (x * x) + (x * x) \x -> (x * x) + (x * x) :: forall a. (Num a) => a -> a Prelude> :t \x -> sqrt x \x -> sqrt x :: forall a. (Floating a) => a -> a In the first case, x can have any type. In the next two, it must be a number (int, float, etc.). In the last case, it must be a specific kind of number, namely "float-like", which includes (single precision) Float, Double, and Complex. > Much better. Except I want + to work on int, float, float32, 'a^n > (static vectors), 'a^n^m (static matrices), 'a array, 'a list, 'a > set. Not only that, I want + and * to work on two values of > different types: date+time, float*vector and so on. Sure. Just write the apropriate instances for the type class, if it isn't defined already. Encoding dimensionalities into types can require a bit of magic :-) > Hell, I want to overload 0 to mean 0, 0., 0. + 0.i, zero vector and > the zero matrix. No problem either: Number literals like "0" are translated into the expression "fromInteger 0", so by overloading fromInteger in the type class, you can generate the apropriate constant. > Of course, the zero matrix has arbitrary dimensionalities that could > be static, so my type now contains variables for those. Again, no problem. Of course, you pay for that flexibility by having to pass around dictionaries at runtime (unless you specialize early, or the compiler is really clever about inlining and specializing). > You see how things will get more complicated to define correctly, slower to > compile and much harder to comprehend when things go wrong? Certainly. One has to be very careful what to allow and what to disallow. But at least those very basic examples work fine. - Dirk