Help with a data type declaration

How can I interpret the following data type declaration? The book where I am studying (and other sources I have read as well) only show more simple examples. This is what I can say about it: * "Tree" is the name of the new type. * "Branch" and "Leaf" are the type constructors. * What is "a" and "b"? * It seems to me that this type is kind of "recursively" defined but I do not know exactly. data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a I will very much appreciate your feedback. Regards, JAMB

a = left b = right
That would at least make the sentiment clearer.
On 29 September 2015 at 15:10,
How can I interpret the following data type declaration? The book where I am studying (and other sources I have read as well) only show more simple examples. This is what I can say about it:
* "Tree" is the name of the new type. * "Branch" and "Leaf" are the type constructors. * What is "a" and "b"? * It seems to me that this type is kind of "recursively" defined but I do not know exactly.
data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a
I will very much appreciate your feedback.
Regards, JAMB _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Is the definition of Branch correct though, shouldn't it be Branch (Tree a
b) (Tree a b) ...?
On 29 September 2015 at 16:10, emacstheviking
a = left b = right
That would at least make the sentiment clearer.
On 29 September 2015 at 15:10,
wrote: How can I interpret the following data type declaration? The book where I am studying (and other sources I have read as well) only show more simple examples. This is what I can say about it:
* "Tree" is the name of the new type. * "Branch" and "Leaf" are the type constructors. * What is "a" and "b"? * It seems to me that this type is kind of "recursively" defined but I do not know exactly.
data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a
I will very much appreciate your feedback.
Regards, JAMB _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

It just stores a 'b' on every branch and an 'a' on every leaf. I'm not
sure what you'd use it for, but there's nothing wrong with it.
On Tue, Sep 29, 2015 at 11:10 AM, emacstheviking
Is the definition of Branch correct though, shouldn't it be Branch (Tree a b) (Tree a b) ...?
On 29 September 2015 at 16:10, emacstheviking
wrote: a = left b = right
That would at least make the sentiment clearer.
On 29 September 2015 at 15:10,
wrote: How can I interpret the following data type declaration? The book where I am studying (and other sources I have read as well) only show more simple examples. This is what I can say about it:
* "Tree" is the name of the new type. * "Branch" and "Leaf" are the type constructors. * What is "a" and "b"? * It seems to me that this type is kind of "recursively" defined but I do not know exactly.
data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a
I will very much appreciate your feedback.
Regards, JAMB _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Tue, Sep 29, 2015, at 07:10 AM, jamb@hinojosa.com wrote:
* "Tree" is the name of the new type. * "Branch" and "Leaf" are the type constructors. * What is "a" and "b"?
"Tree" is not quite the name of a type. It is the name of a type constructor - in other words, it is a "type-level function". This also explains "a" and "b" - they are the parameters to the type constructor (which means they are "type variables"). To get a type, you have to apply the type constructor. So, for example, "Tree Char Int" is a type. "Branch" and "Leaf" are not type constructors - they are *data* constructors.
* It seems to me that this type is kind of "recursively" defined but I do not know exactly.
Yes. It's recursive because a value of type "Tree a b" can be built up from other values of type "Tree a b" (if you use the "Branch" constructor). -Karl

This Wikipedia article is a good read for getting the concepts right:
https://en.wikipedia.org/wiki/Algebraic_data_type
On 29 September 2015 at 20:57, Karl Voelker
On Tue, Sep 29, 2015, at 07:10 AM, jamb@hinojosa.com wrote:
* "Tree" is the name of the new type. * "Branch" and "Leaf" are the type constructors. * What is "a" and "b"?
"Tree" is not quite the name of a type. It is the name of a type constructor - in other words, it is a "type-level function". This also explains "a" and "b" - they are the parameters to the type constructor (which means they are "type variables").
To get a type, you have to apply the type constructor. So, for example, "Tree Char Int" is a type.
"Branch" and "Leaf" are not type constructors - they are *data* constructors.
* It seems to me that this type is kind of "recursively" defined but I do not know exactly.
Yes. It's recursive because a value of type "Tree a b" can be built up from other values of type "Tree a b" (if you use the "Branch" constructor).
-Karl _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards Sumit Sahrawat
participants (5)
-
David McBride
-
emacstheviking
-
jamb@hinojosa.com
-
Karl Voelker
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)