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,HTML_MESSAGE,SPF_NEUTRAL 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 ACBA5BC0A for ; Fri, 19 Jan 2007 11:35:39 +0100 (CET) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.173]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l0JAZdrC000317 for ; Fri, 19 Jan 2007 11:35:39 +0100 Received: by ug-out-1314.google.com with SMTP id q2so462423uge for ; Fri, 19 Jan 2007 02:35:37 -0800 (PST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=ptoveDDe65NJBKWBNmW5lhbl7mKEnVrU44poOGnvb+VGBcCZk3vyWUjgqSShf/Dv32FsduZYnsPv8z6Lu378zFAzknDmeJslI14eLOczhxEfcBMpLO8o3WXk/DwnicVMJfJLtwES5MWBC/nalSCrA+tla6I1xtvgnkM6SrDrfIg= Received: by 10.82.152.16 with SMTP id z16mr476295bud.1169202936731; Fri, 19 Jan 2007 02:35:36 -0800 (PST) Received: by 10.82.100.16 with HTTP; Fri, 19 Jan 2007 02:35:36 -0800 (PST) Message-ID: Date: Fri, 19 Jan 2007 11:35:36 +0100 From: Tom To: "Dirk Thierbach" Subject: Re: Fwd: [Caml-list] Polymorphic Variants Cc: caml-list@yquem.inria.fr In-Reply-To: <20070119092648.GA3604@feanor> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_201951_28156451.1169202936441" References: <200701182114.10133.jon@ffconsultancy.com> <20070119092648.GA3604@feanor> X-j-chkmail-Score: MSGID : 45B09EFB.000 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 45B09EFB.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; variants:01 sqrt:01 haskell's:01 overloading:01 haskell's:01 compiler:01 compiler:01 typeclass:01 forall:01 forall:01 bignum:01 bignum:01 notation:01 overload:01 literals:01 X-Attachments: cset="UTF-8" cset="UTF-8" ------=_Part_201951_28156451.1169202936441 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 19/01/07, Dirk Thierbach wrote: > > 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: Well, in some sense, generic value overloading is somewhat like Haskell's type classes, with an advantage that type classes are infered automatically by the compiler (or, actually, are not named/declared - the compiler simply lists all types belonging to the current typeclass Prelude> :t \x -> x > \x -> x :: forall t. t -> t > > Prelude> :t \x -> x * x > \x -> x * x :: forall a. (Num a) => a -> a forall a . (int, float, complex, fraction, bignum, int32, vector2, vector3, string) => a -> a or, what I would prefer: [int -> int | float -> float | complex -> complex | fraction -> fraction | bignum -> bignum | int32 -> int32 | vector2 -> vector2 | vector3 -> vector3 | string -> string] (Yes, it seems a lot of writing... but remember that it is not you who writes that, it's the compiler. While for such short types, a -> a, Haskell's notation is better, it could become hard to understand with more complex types: forall a . (float, complex, fraction) => forall b . (int, string) => a -> a -> b -> b -> (a, b) Now, go figure all the possibilities... It's much simpler when the compiler lists all the combinations for you. > 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. Can Haskell overload values? And functions by their return type? - Tom ------=_Part_201951_28156451.1169202936441 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline

On 19/01/07, Dirk Thierbach <dthierbach@gmx.de> wrote:
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:


Well, in some sense, generic value overloading is somewhat like Haskell's type classes, with an advantage that type classes are infered automatically by the compiler (or, actually, are not named/declared - the compiler simply lists all types belonging to the current typeclass 

Prelude> :t \x -> x
\x -> x :: forall t. t -> t

Prelude> :t \x -> x * x
\x -> x * x :: forall a. (Num a) => a -> a

     forall a . (int, float, complex, fraction, bignum, int32, vector2, vector3, string) => a -> a

or, what I would prefer:

     [int -> int | float -> float | complex -> complex | fraction -> fraction | bignum -> bignum | int32 -> int32 | vector2 -> vector2 | vector3 -> vector3 | string -> string]

(Yes, it seems a lot of writing... but remember that it is not you who writes that, it's the compiler. While for such short types, a -> a, Haskell's notation is better, it could become hard to understand with more complex types:
     forall a . (float, complex, fraction) => forall b . (int, string) => a -> a -> b -> b -> (a, b)
Now, go figure all the possibilities... It's much simpler when the compiler lists all the combinations for you.

> 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.

Can Haskell overload values? And functions by their return type?

- Tom
------=_Part_201951_28156451.1169202936441--