Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between type and data type in Haskell?

I'm a little confused as to the difference between a type and a data type in Haskell.

From the literature I have encountered I got the impression they were different concepts.

like image 251
user997112 Avatar asked Oct 27 '11 13:10

user997112


People also ask

What is datatype in Haskell?

In Haskell, you can have many constructors for your data type, separated by a vertical bar | . Each of your constructors then has its own list of data types! So different constructors of the same type can have different underlying data! We refer to a type with multiple constructors as a “sum” type.

Does Haskell have different types?

Everything in Haskell has a type, so the compiler can reason quite a lot about your program before compiling it. Unlike Java or Pascal, Haskell has type inference.

What is the difference between data type and return type?

The result of a function is called its return value and the data type of the return value is called the return type. Every function declaration and definition must specify a return type, whether or not it actually returns a value.

How do you declare types 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.


2 Answers

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.

like image 92
Fred Foo Avatar answered Oct 05 '22 09:10

Fred Foo


The terms are sometimes mixed, but usually a "data type" refers to a type introduced using the data keyword, which has constructors you can pattern match on. These are also called algebraic data types".

Just "type" is a more general term which also includes types created using newtype, function types and so on.

like image 26
hammar Avatar answered Oct 05 '22 11:10

hammar