OO in Python? ^^

Donn Cave donn at u.washington.edu
Mon Dec 12 19:40:23 CET 2005

In article <1h7f90l.l5s3sh1cqq9ozN%aleax at mail.comcast.net>,
 aleax at mail.comcast.net (Alex Martelli) wrote:

> Tom Anderson <twic at urchin.earth.li> wrote:
>    ...
> > Haskell is strongly and statically typed - very strongly and very 
> > statically!
> Sure.
> > However, what it's not is manifestly typed - you don't have to put the
> > types in yourself; rather, the compiler works it out. For example, if i
> > wrote code like this (using python syntax):
> > 
> > def f(x):
> >       return 1 + x
> > 
> > The compiler would think "well, he takes some value x, and he adds it to 1
> > and 1 is an integer, and the only thing you can add to an integer is 
> > another integer, so x must be an integer; he returns whatever 1 + x works
> > out to, and 1 and x are both integers, and adding two integers makes an
> > integer, so the return type must be integer", and concludes that you meant
> hmmm, not exactly -- Haskell's not QUITE as strongly/rigidly typed as
> this... you may have in mind CAML, which AFAIK in all of its variations
> (O'CAML being the best-known one) *does* constrain + so that "the only
> thing you can add to an integer is another integer".  In Haskell, + can
> sum any two instances of types which meet typeclass Num -- including at
> least floats, as well as integers (you can add more types to a typeclass
> by writing the required functions for them, too).  Therefore (after
> loading in ghci a file with
> f x = x + 1
> ), we can verify...:
> *Main> :type f
> f :: (Num a) => a -> a
> A very minor point, but since the need to use +. and the resulting lack
> of polymorphism are part of what keeps me away from O'CAML and makes me
> stick to Haskell, I still wanted to make it;-).

But if you try
   f x = x + 1.0

   f :: (Fractional a) => a -> a

I asserted something like this some time ago here, and was
set straight, I believe by a gentleman from Chalmers.  You're
right that addition is polymorphic, but that doesn't mean
that it can be performed on any two instances of Num.  I had
constructed a test something like that to check my thinking,
but it turns out that Haskell was able to interpret "1" as
Double, for example -- basically, 1's type is Num too.
If you type the constant (f x = x + (1 :: Int)), the function
type would be (f :: Int -> Int).  Basically, it seems (+) has
to resolve to a (single) instance of Num.

   Donn Cave, donn at u.washington.edu

More information about the Python-list mailing list