I am new to Haskell and I am trying to understand why one needs to write type declarations. Since Haskell has type inference, when do I need the first line at all? GHCI seems to generate correct output with I use ':t'
The only example I found so far that seems to need a declaration is the following.
maximum' :: (Ord a) => [a] -> a maximum' = foldr1 max
However, if I add "-XNoMonomorphismRestriction" flag declaration is not needed again. Are there specific situations when type inference does not work and one needs to specify types?
Since I could have a bug in type declaration and no direct benefit, I'd rather not write it. Again, I have just started learning Haskell, so please correct me if I am wrong, as I want to develop good habits.
EDIT: It turns out that the Type inference is a double-edged sword section of the Real World Haskell book has a nice discussion of this topic.
Haskell has three basic ways to declare a new type: The data declaration, which defines new data types. The type declaration for type synonyms, that is, alternative names for existing types. The newtype declaration, which defines new data types equivalent to existing ones.
Purpose. A type declaration statement specifies the type, length, and attributes of objects and functions. You can assign initial values to objects. A declaration type specification (declaration_type_spec) is used in a nonexecutable statement.
In Haskell, every statement is considered as a mathematical expression and the category of this expression is called as a Type. You can say that "Type" is the data type of the expression used at compile time.
Type and data type refer to exactly the same concept. The Haskell keywords type and data are different, though: data allows you to introduce a new algebraic data type, while type just makes a type synonym. See the Haskell wiki for details.
undefined
. It everything compiles I know that my idea seems to fit not too bad. Then I go on and replace undefined
by real codeConsider read "5"
. How can Haskell know the type of read "5"
? It can't, because theres no way to resolve the result of the operation, since read
is defined as (Read a) => String -> a
. a
is not dependent on the string, so it must use context.
However usually context is something like Ord
or Num
so its impossible to determine. This is not the monomorphism restriction but rather another case that can never be handled properly.
Examples:
Does not Work:
read "0.5" putStrLn . show . read $ "0.5"
Does Work:
read "0.5" :: Float putStrLn . show . (read :: String -> Float) $ "0.5"
These are necessary because the default Show
instance, if I remember correctly, is Int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With