coder9
coder9

Reputation: 1549

Type definition not a must for Haskell?

How does Haskell code work even without the Type declaration?

Upvotes: 2

Views: 268

Answers (2)

Waldheinz
Waldheinz

Reputation: 10487

Because very often, the type can be inferred from the context. For example, the function tail has the type [a] -> [a] which say we go from a list to another list of the same type. When you pass a String to that function, it is known that another String is the result, so there is not really a need to explicitly say that. Thus, the type of (tail "hello") is known to be String (or [Char], which is really the same).

Upvotes: 4

sepp2k
sepp2k

Reputation: 370142

Note: I'm assuming that you mean type signatures because if you remove type definitions like type Foo = Bar or data X = Y, the code will not work anymore (assuming the defined type is actually used of course).

Haskell code works without type signatures because the type of a variable/function is simply inferred by the compiler if you do not specify a signature. The algorithm used to make this inference is a variant of the Hindley-Milner type inference algorithm.

Upvotes: 4

Related Questions