Saturday, December 02, 2006

Strong Typing without the Typing

Functions, Types, Function Types, and Type Inference "One of the most important things to recognize about Haskell's type system is that it's based on type inference. What that means is that in general, you don't need to provide type declarations. Based on how you use a value, the compiler can usually figure out what type it is. The net effect is that in many Haskell programs, you don't write any type declarations, but your program is still carefully type-checked."

"If we look at that type, and think about what the factorial function actually does, there's a problem. That type isn't correct, because factorial is only defined for integers, and if we pass it a non-integer value as a parameter, it will never terminate! But Haskell can't figure that out for itself - all it knows is that we do three things with the parameter to our function: we compare it to zero, we subtract from it, and we multiply by it. So Haskell's most general type for that is a general numeric type. So since we'd like to prevent anyone from mis-calling factorial by passing it a fraction..."

Currently I'm enjoying the print version "The Haskell Road to Logic, Math and Programming" (links to the PDF version). I haven't appreciated the writing style used in this math textbook this much since, "Introduction to Graph Theory". I'm still waiting for Haskell to reach 0.1% popularity.

No comments: