Version française
Home     About     Download     Resources     Contact us    
Browse thread
ambitious proposal: polymorphic arithmetics
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Marcin 'Qrczak' Kowalczyk <qrczak@k...>
Subject: Re: [Caml-list] ambitious proposal: polymorphic arithmetics
Eijiro Sumii <eijiro_sumii@anet.ne.jp> writes:

> So here it goes: why don't we have polymorphic +, -, etc. while we
> have polymorphic =, <, etc.?

=, < have type 'a -> 'a -> bool, so they appear to be applicable
to *all* types. It happens that their implementation doesn't need
to distinguish different types which currently have the same
representation, it just examines the representation at runtime. 0 < 1
uses the same code as false < true.

(Modulo special code generated based on the static type when it's
known, but in polymorphic functions it is not known, so this must be
optional, can't be relied on.)

+ can't be treated in the same way, because it won't distinguish
whether it's called as 1 + 1 or true + true. If it returns 2 in the
former case, it would produce a nonsensical bool value in the latter
case.

OCaml doesn't have a mechanism for making +, - applicable to a limited
set of types and for dispatching their implementation based on the type
rather than on the physical representation.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/