[Haskell-beginners] Difference between types and values
Karl Voelker
karl at karlv.net
Tue Jun 16 07:42:16 UTC 2015
On Mon, Jun 15, 2015, at 11:52 PM, Matt Williams wrote:
> When we have a type, T, with constructors A and B
> (e.g. data T = A x y z | B x y)
> How do I understand the relationship between A, B and T? I had
> thought I could use the sub-class relationship, but that doesn't seem
> to be true.
You are correct that A and B are not types in Haskell.
The relationship is that there are two different ways to construct a
value of type T. Whenever a T is needed, you can use either A or B. That
means, on the other hand, that whenever a T is consumed, you have to
handle two cases: A and B.
These data types are called "algebraic data types," which might help you
find more to read about them. The wiki has a page:
https://wiki.haskell.org/Algebraic_data_type.
Lastly, as a bit of a digression, you could imagine an alternate
language in which A and B are subtypes of T, such that constructor A
returns a value of type A, and constructor B returns a value of type B.
I'm not an expert on the theory behind all of this, but I know that
doing type inference would be much harder in such a language.
-Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150616/6bfd1e8d/attachment.html>
More information about the Beginners
mailing list