[Haskell-beginners] data declaration using other type's names?

Nikita Danilenko nda at informatik.uni-kiel.de
Thu Jun 27 18:02:00 CEST 2013


Hi, Patrick,

the namespaces for types and constructors are considered disjoint, i.e.
you can use a name in both contexts. A simple example of this feature is
your last definition

> data Bar = Bar Int

or even shorter

> data A = A

This is particularly useful for single-constructor types à la

> data MyType a = MyType a

Clearly, using "Int" or "Float" as constructor names may seem odd, but
when dealing with a simple grammar it is quite natural to write

> data Exp = Num Int | Add Exp Exp

although "Num" is a type class in Haskell.

Best regards,

Nikita

On 27/06/13 17:24, Patrick Redmond wrote:
> Hey Haskellers,
>
> I noticed that ghci lets me do this:
>
>> data Foo = Int Int | Float
>> :t Int
> Int :: Int -> Foo
>> :t Float
> Float :: Foo
>> :t Int 4
> Int 4 :: Foo
>
> It's confusing to have type constructors that use names of existing
> types. It's not intuitive that the name "Int" could refer to two
> different things, which brings me to:
>
>> data Bar = Bar Int
>> :t Bar
> Bar :: Int -> Bar
>
> Yay? I can have a simple type with one constructor named the same as the type.
>
> Why is this allowed? Is it useful somehow?
>
> --Patrick
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list