Hacker News new | ask | show | jobs
by edwardkmett 4342 days ago
On the other hand, here's a problem.

Extend your numeric type tower to handle whatever new numeric types I come up with in an internally consistent manner.

In haskell I have number types for things that automatically compute derivatives. I have number types for functions that go to number types so all vector spaces work like numbers as well. I have number types for arbitrary precision floating point numbers, and I can build these things on top of each other.

It is a trade-off. We give up a bit of one thing to get something else.

You can take either side of the deal. I'd argue that the weight of benefit is on the side where we don't have a magic type tower to reason about, but it is a perfectly reasonable stance to say that the thing you want is more important to you.

On the other hand, I can turn around and argue that if what I really want is an ad hoc tower of numeric types I can just make a type for that and work inside it.

data Number = Int Int | Rational Rational | Double Double | Complex (Complex Double) | ...

instance Num Number

Now I can opt into your model. Can you opt into mine?

2 comments

You're totally right that there's a tradeoff here -- in Typed Racket, where we have strong types for the numeric tower plus the flexibility that the parent wants, arithmetic isn't extensible.

However, it's not the case that `data Number = ...` gets you everything. In particular, it gives up on the types! :)

For example, in Typed Racket:

-> (: norm : Real Real -> Real)

-> (define (norm x y) (sqrt (+ (sqr x) (sqr y))))

-> (norm -3 12)

- : Real

12.36931687685298

We've proved that the `norm` function always produces `Real` answers, even though `sqrt` might produce complex numbers given negative inputs. The sum type you've given won't let you prove that.

>Can you opt into mine?

In Python, not only can you "opt in" to those features (in a completely natural way) but those features already exist in the form of highly popular libraries:

NumPy - http://www.numpy.org/

SymPy - http://www.sympygamma.com/input/?i=diff%28x**2%29

mpmath - http://mpmath.org/

...and the story is similar with Common Lisp, whose generic functions dispatch on function parameter type (just like Haskell allows with type classes), allowing you to create libraries for vector arithmetic, symbolic algebra, and arbitrary precision floating point math, again in a completely natural style.

The best forms of language advocacy probably involve showing the great things you've done with a language and forgetting about comparisons to the competition. Build it, and they will come.

"The best forms of language advocacy probably involve showing the great things you've done with a language and forgetting about comparisons to the competition."

Great things the parent has done with the language:

https://www.youtube.com/watch?v=cefnmjtAolY