<div dir="ltr"><div>A short example:<br></div><div><br></div><div><font face="monospace, monospace"> </font><span style="font-family:monospace,monospace">data T = Tag1 Type1 Type2 </span></div><div><font face="monospace, monospace"> | Tag2 Type3</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> -- A type T can contain elements of two different types, which can be differentiated in a program by their 'Tag'</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> -- 'Tag1 Type1 Type2' is a product type, just like a cartesian product of sets. It has elements</font></div><div><font face="monospace, monospace"> -- of the form (Type1, Type2) but written as 'Tag1 Type1 Type2' for programming convenience.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> -- Tag2 Type3 is just Type3, with additional syntax to differentiate it from Type3.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> -- The pipe '|' creates a sum type, just like the union of sets.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> -- Overall, you have a type which has elements of the form (Type1, Type2) or Type3. Written differently so that</font></div><div><font face="monospace, monospace"> -- they can be distinguished from (Type1, Type2) and Type3 elements.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> -- (x :: Type1, y :: Type2) is not equal to 'Tag1 x y'.</font></div><div><font face="monospace, monospace"> -- The first has the type (Type1, Type2) and the second has the type T.</font></div><div><font face="monospace, monospace"> -- Thus, Tag1 takes a Type1 and a Type2 and converts them to a T.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> -- Tag1 :: Type1 -> Type2 -> T</font></div><div><font face="monospace, monospace"> -- A data constructor, constructs element of type T using elements of type Type1 and Type2</font></div><div><font face="monospace, monospace"><br></font></div><div>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.<div><br></div><div><a href="https://en.wikipedia.org/wiki/Algebraic_data_type">https://en.wikipedia.org/wiki/Algebraic_data_type</a><br></div><div><a href="https://en.wikipedia.org/wiki/Tagged_union">https://en.wikipedia.org/wiki/Tagged_union</a></div></div><div><font face="monospace, monospace"><br></font></div>Hope this helps.</div><div class="gmail_extra"><br><div class="gmail_quote">On 16 June 2015 at 14:25, Ovidiu Deac <span dir="ltr"><<a href="mailto:ovidiudeac@gmail.com" target="_blank">ovidiudeac@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>I want to add a little more thing that makes me understand this easier:<br><br></div><span class=""> data Bool = True | False<br><br></span></div>You can think if True not as a value but as a function from unit to Bool<br><br></div>That being said in Bob's example:<br><br></div><span class=""> data PersonOrPlace = Person String | Place String<br><br></span></div>...Person is a function from the type String to the type PersonOrPlace<br><br><div><div><div><div><div><div><div><div><div class="gmail_extra">As 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.</div><div><div class="h5"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 16, 2015 at 10:42 AM, Bob Ippolito <span dir="ltr"><<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">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.<div><br></div><div>It's simpler to consider a type without any fields in the constructor:</div><div><br></div><div>data Bool = True | False</div><div><br></div><div>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.</div><div><br></div><div>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"</div><div><br></div><div>data PersonOrPlace = Person String | Place String</div><div><br></div><div>To make it more clear, here the types are annotated with <AngleBrackets> and the constructors annotated with [SquareBrackets]:</div><div><br></div><div>data <PersonOrPlace> = [Person] <String> | [Place] <String></div><div><br></div><div><br></div><div><br></div><div class="gmail_extra"><div class="gmail_quote"><div><div>On Tue, Jun 16, 2015 at 8:52 AM, Matt Williams <span dir="ltr"><<a href="mailto:matt.williams45.mw@gmail.com" target="_blank">matt.williams45.mw@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><p dir="ltr">Dear All,</p>
<p dir="ltr">I am sure this is a common mistake, and I am happy to be pointed elsewhere for reading.</p>
<p dir="ltr">I have spent the last couple of days on the Haskell irc channel, which was very helpful.</p>
<p dir="ltr">However, one of the points of discussion left me confused.</p>
<p dir="ltr">When we have a type, T, with constructors A and B</p>
<p dir="ltr">(e.g. data T = A x y z | B x y)</p>
<p dir="ltr">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.</p>
<p dir="ltr">Any other pointers very welcome.</p><span><font color="#888888">
<p dir="ltr">Matt</p>
</font></span><br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div></div></div></div></div></div></div></div></div></div></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>