Multiple declarations of value constructor

Hi, -- tests.hs data MyType1 = Value1 | Value2 data MyType2 = Value1 | Value2 Prelude> :l tests [1 of 1] Compiling Main ( tests.hs, interpreted ) tests.hs:3:16: Multiple declarations of `Main.Value1' Declared at: tests.hs:1:16 tests.hs:3:16 tests.hs:3:25: Multiple declarations of `Main.Value2' Declared at: tests.hs:1:25 tests.hs:3:25 Failed, modules loaded: none. If so, how are numbers defined? I can use 1 :: Int or 1 :: Integer. Why not Value1 :: MyType1 and Value1 :: MyType2? Emanuel

On Wed, Dec 19, 2012 at 10:52 AM, Emanuel Koczwara < poczta@emanuelkoczwara.pl> wrote:
If so, how are numbers defined? I can use 1 :: Int or 1 :: Integer. Why not Value1 :: MyType1 and Value1 :: MyType2?
Numbers are special-cased in the compiler, because they're polymorphic; in effect, the literal is output as a call to "fromIntegral" or "fromRational" as appropriate for the type, on some low level internal representation of the literal. (There is an extension to enable this for strings as well, but not for other types.) You can't do this with user defined types because you can't make a constructor a typeclass function. Note that various things, in particular pattern matching, rely on constructor names unambiguously identifying types. (Again, numeric literals are a special case; patterns involving them actually get rewritten into guards. If you want to know the gory details, they're documented in the Haskell Language Report.) -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Something like this?
class TwoValues a where
val1 :: a
val2 :: a
data MyTypeA = ValueA1 | ValueA2
data MyTypeB = ValueB1 | ValueB2
instance TwoValues MyTypeA where
val1 = ValueA1
val2 = ValueA2
instance TwoValues MyTypeB where
val1 = ValueB1
val2 = ValueB2
---
val1 :: MyTypeA -- ValueA1
val2 :: MyTypeB -- ValueB2
Peter
On 19 December 2012 15:52, Emanuel Koczwara
Hi,
-- tests.hs data MyType1 = Value1 | Value2 data MyType2 = Value1 | Value2
Prelude> :l tests [1 of 1] Compiling Main ( tests.hs, interpreted )
tests.hs:3:16: Multiple declarations of `Main.Value1' Declared at: tests.hs:1:16 tests.hs:3:16
tests.hs:3:25: Multiple declarations of `Main.Value2' Declared at: tests.hs:1:25 tests.hs:3:25 Failed, modules loaded: none.
If so, how are numbers defined? I can use 1 :: Int or 1 :: Integer. Why not Value1 :: MyType1 and Value1 :: MyType2?
Emanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Brandon Allbery
-
Emanuel Koczwara
-
Peter Hall