Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why write type declarations in Haskell?

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.

like image 684
Vladimir Bychkovsky Avatar asked Jun 05 '11 23:06

Vladimir Bychkovsky


People also ask

What is type declaration in Haskell?

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.

What is the purpose of type declarations What are the components of type declaration?

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.

What does type do in Haskell?

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.

What is the difference between type and data in Haskell?

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.


2 Answers

  • when you have big Haskell programs, having type signatures often gives you better error messages from the compiler
  • sometime you can derive what a function does from its name and its signature
  • often a function is understandable a lot better with type signatures, e.g. if you make use of currying
  • even writing programs gets easier, I often start with type signatures and most functions declared as undefined. It everything compiles I know that my idea seems to fit not too bad. Then I go on and replace undefined by real code
like image 109
Johannes Weiss Avatar answered Sep 17 '22 20:09

Johannes Weiss


Consider 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.

like image 24
alternative Avatar answered Sep 18 '22 20:09

alternative