data declaration using other type's names?

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

On Thu, Jun 27, 2013 at 11:24 AM, Patrick Redmond
I noticed that ghci lets me do this:
Not just ghci, but ghc as well.
Yay? I can have a simple type with one constructor named the same as the type. Why is this allowed? Is it useful somehow?
It's convenient for pretty much the situation you showed, where the type constructor and data constructor have the same name. A number of people do advocate that it not be used, though, because it can be confusing for people. (Not for the compiler; data and type constructors can't be used in the same places, it never has trouble keeping straight which is which.) It might be best to consider this as "there is no good reason to *prevent* it from happening, from a language standpoint". -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Brandon Allbery
-
Nikita Danilenko
-
Patrick Redmond