Multiple declarations of value constructor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple declarations of value constructor

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





Reply | Threaded
Open this post in threaded view
|

Multiple declarations of value constructor

Brandon Allbery
On Wed, Dec 19, 2012 at 10:52 AM, Emanuel Koczwara <
poczta at 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 at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121219/15474058/attachment-0001.htm>

Reply | Threaded
Open this post in threaded view
|

Multiple declarations of value constructor

Peter Hall
In reply to this post by Emanuel Koczwara
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 <poczta at emanuelkoczwara.pl>wrote:

> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121219/44144f21/attachment.htm>