Dear All, I am sure this is a common mistake, and I am happy to be pointed elsewhere for reading. I have spent the last couple of days on the Haskell irc channel, which was very helpful. However, one of the points of discussion left me confused. 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. Any other pointers very welcome. Matt _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
One useful way to understand this is to note you will see T in type
annotations and A, B in your actual code. I.e. T is a type constructor and A, B are data constructors. M. Matt Williams: > Dear All, > > I am sure this is a common mistake, and I am happy to be pointed elsewhere > for reading. > > I have spent the last couple of days on the Haskell irc channel, which was > very helpful. > > However, one of the points of discussion left me confused. > > 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. > > Any other pointers very welcome. > > Matt > > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
In reply to this post by Matt Williams-2
On Mon, Jun 15, 2015, at 11:52 PM, Matt Williams wrote:
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
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
In reply to this post by Matt Williams-2
T is the type. A and B are the only constructors for values of that type. A and B are not terms in the type language. T is not a term in the value language.
It's simpler to consider a type without any fields in the constructor: data Bool = True | False True and False are values, Bool is the type. You can't use Bool as a constructor, and you can't use True or False as a type. When you add fields it can get a bit more confusing, because the fields of a constructor are types, so it looks like "ValueConstructor1 FieldType1 FieldType2 | ValueConstructor2 FieldType3" data PersonOrPlace = Person String | Place String To make it more clear, here the types are annotated with <AngleBrackets> and the constructors annotated with [SquareBrackets]: data <PersonOrPlace> = [Person] <String> | [Place] <String> On Tue, Jun 16, 2015 at 8:52 AM, Matt Williams <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
I want to add a little more thing that makes me understand this easier: data Bool = True | FalseAs a conclusion: Haskell is, as they say, "a strong & static typed purely functional language", everything is either a type or a function. If it's not a type then it must be a function. You can say that even 0 is a function from unit to Int so it works quite nice. On Tue, Jun 16, 2015 at 10:42 AM, Bob Ippolito <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
A short example: data T = Tag1 Type1 Type2 | Tag2 Type3 -- A type T can contain elements of two different types, which can be differentiated in a program by their 'Tag' -- 'Tag1 Type1 Type2' is a product type, just like a cartesian product of sets. It has elements -- of the form (Type1, Type2) but written as 'Tag1 Type1 Type2' for programming convenience. -- Tag2 Type3 is just Type3, with additional syntax to differentiate it from Type3. -- The pipe '|' creates a sum type, just like the union of sets. -- Overall, you have a type which has elements of the form (Type1, Type2) or Type3. Written differently so that -- they can be distinguished from (Type1, Type2) and Type3 elements. -- (x :: Type1, y :: Type2) is not equal to 'Tag1 x y'. -- The first has the type (Type1, Type2) and the second has the type T. -- Thus, Tag1 takes a Type1 and a Type2 and converts them to a T. -- Tag1 :: Type1 -> Type2 -> T -- A data constructor, constructs element of type T using elements of type Type1 and Type2 Read the two pages below, to get more intuition. Will be more helpful if you come from C and know about unions in that language. On 16 June 2015 at 14:25, Ovidiu Deac <[hidden email]> wrote:
Regards Sumit Sahrawat _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Ovidiu, take a look at this eye opener: http://conal.net/blog/posts/everything-is-a-function-in-haskell On 16 June 2015 at 17:35, Sumit Sahrawat, Maths & Computing, IIT (BHU) <[hidden email]> wrote:
Regards Sumit Sahrawat _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |