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