[Haskell-beginners] Difference between types and values
Sumit Sahrawat, Maths & Computing, IIT (BHU)
sumit.sahrawat.apm13 at iitbhu.ac.in
Tue Jun 16 12:07:12 UTC 2015
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) <
sumit.sahrawat.apm13 at iitbhu.ac.in> wrote:
> 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.
>
> https://en.wikipedia.org/wiki/Algebraic_data_type
> https://en.wikipedia.org/wiki/Tagged_union
>
> Hope this helps.
>
> On 16 June 2015 at 14:25, Ovidiu Deac <ovidiudeac at gmail.com> wrote:
>
>> I want to add a little more thing that makes me understand this easier:
>>
>> data Bool = True | False
>>
>> You can think if True not as a value but as a function from unit to Bool
>>
>> That being said in Bob's example:
>>
>> data PersonOrPlace = Person String | Place String
>>
>> ...Person is a function from the type String to the type PersonOrPlace
>>
>> 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.
>>
>>
>> On Tue, Jun 16, 2015 at 10:42 AM, Bob Ippolito <bob at redivi.com> wrote:
>>
>>> 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 <
>>> matt.williams45.mw at gmail.com> wrote:
>>>
>>>> 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
>>>> Beginners at haskell.org
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Regards
>
> Sumit Sahrawat
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150616/a7936cfd/attachment-0001.html>
More information about the Beginners
mailing list