Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Defining an instance: Syntax that works exactly  sometimes
      (Jeffrey Brown)
   2. Re:  Defining an instance: Syntax that works exactly
      sometimes (Ryan Trinkle)
   3. Re:  Defining an instance: Syntax that works exactly
      sometimes (Brandon Allbery)
   4. Re:  Defining an instance: Syntax that works exactly
      sometimes (Michael Orlitzky)
   5. Re:  Defining an instance: Syntax that works exactly
      sometimes (Brandon Allbery)
   6. Re:  Defining an instance: Syntax that works exactly
      sometimes (Michael Orlitzky)
   7. Re:  Defining an instance: Syntax that works exactly
      sometimes (Michael Orlitzky)


----------------------------------------------------------------------

Message: 1
Date: Wed, 21 Jan 2015 16:23:09 -0800
From: Jeffrey Brown <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Defining an instance: Syntax that works
        exactly sometimes
Message-ID:
        <caec4ma0ldft_rnloes+sedp9et1px+igixvvk8iwmwwjxm9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Dear Haskellers,

The following compiles. (Rev stands for Reversible, and Dirn for Direction.)

    class Rev a where
        rev :: a -> a

    data Dirn = Succ | Pred
        deriving (Eq, Show, Ord)

    -- implement Ord
    (<=) Succ Pred = False
    (<=) _ _ = True

    -- implement Rev
    instance Rev Dirn where
        rev Succ = Pred
        rev Pred = Succ

But if I try to define the Rev instance the same way the Ord instance is
being defined, it does not compile:

    class Rev a where
        rev :: a -> a

    data Dirn = Succ | Pred
        deriving (Eq, Show, Ord, Rev)

    -- implement Ord, because Dirn is used as a key in a Map
    (<=) Succ Pred = False
    (<=) _ _ = True

    -- implement Rev
    rev Succ = Pred
    rev Pred = Succ

What's going on?

Many thanks,
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150121/d62f9bbf/attachment-0001.html>

------------------------------

Message: 2
Date: Wed, 21 Jan 2015 19:31:51 -0500
From: Ryan Trinkle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
        works exactly sometimes
Message-ID:
        <cahnepiwwcjfprzh1scq+s_glndepjwglz+ntjfio-xfkf_f...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

In both cases, it's not actually creating a typeclass instance.  However,
because (<=) from Ord is declared in GHC.Classes, you're able to create a
new (but completely unrelated) function named (<=).  The fully qualified
names for these would be GHC.Classes.<= and YourModule.<=, so they don't
clash (but if you tried to use <= without qualifying it, you'd get an
ambiguous reference error).

In the case of Rev, you get an error, though, because both the class method
and the standalone function are declared in YourModule, which is illegal
(multiple declarations of the same name).

So, long story short, go with the "instance" syntax.

On Wed, Jan 21, 2015 at 7:23 PM, Jeffrey Brown <[email protected]>
wrote:

> Dear Haskellers,
>
> The following compiles. (Rev stands for Reversible, and Dirn for
> Direction.)
>
>     class Rev a where
>         rev :: a -> a
>
>     data Dirn = Succ | Pred
>         deriving (Eq, Show, Ord)
>
>     -- implement Ord
>     (<=) Succ Pred = False
>     (<=) _ _ = True
>
>     -- implement Rev
>     instance Rev Dirn where
>         rev Succ = Pred
>         rev Pred = Succ
>
> But if I try to define the Rev instance the same way the Ord instance is
> being defined, it does not compile:
>
>     class Rev a where
>         rev :: a -> a
>
>     data Dirn = Succ | Pred
>         deriving (Eq, Show, Ord, Rev)
>
>     -- implement Ord, because Dirn is used as a key in a Map
>     (<=) Succ Pred = False
>     (<=) _ _ = True
>
>     -- implement Rev
>     rev Succ = Pred
>     rev Pred = Succ
>
> What's going on?
>
> Many thanks,
> Jeff
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150121/79f9bd8d/attachment-0001.html>

------------------------------

Message: 3
Date: Wed, 21 Jan 2015 19:32:38 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
        works exactly sometimes
Message-ID:
        <CAKFCL4XHGiy0fc4SdiB+Hxq0W4=kevxyqgoaejotgv-dhns...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Jan 21, 2015 at 7:23 PM, Jeffrey Brown <[email protected]>
wrote:

> The following compiles. (Rev stands for Reversible, and Dirn for
> Direction.)
>
>     class Rev a where
>         rev :: a -> a
>
>     data Dirn = Succ | Pred
>         deriving (Eq, Show, Ord)
>
>     -- implement Ord
>     (<=) Succ Pred = False
>     (<=) _ _ = True
>

You derived Ord; why are you doing this? You are locally overriding the Ord
instance you derived with a (<=) that is *not* the one that is part of
Ord.. and will therefore not contribute to the other functions from Ord.

If you want to implement an instance manually, use the instance keyword.

    instance Ord Dim where
      (<=) Succ Pred = False
      (<=) _ _ = True

The same applies to Rev later, but in that case it's redefining something
defined in the same module and errors out.

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150121/142ad7a4/attachment-0001.html>

------------------------------

Message: 4
Date: Wed, 21 Jan 2015 19:48:56 -0500
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
        works exactly sometimes
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On 01/21/2015 07:32 PM, Brandon Allbery wrote:
> 
>     instance Ord Dim where

Insufficient keming on that font? =)




------------------------------

Message: 5
Date: Wed, 21 Jan 2015 19:50:43 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
        works exactly sometimes
Message-ID:
        <cakfcl4x80wpzmxqfmackvlskzlqyh0mzsecuovfdeul5xcd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Jan 21, 2015 at 7:48 PM, Michael Orlitzky <[email protected]>
wrote:

> Insufficient keming on that font? =)


Probably... also half blind due to sinuses and the drugs trying to keep
them at bay :/

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150121/e0aa6cf6/attachment-0001.html>

------------------------------

Message: 6
Date: Wed, 21 Jan 2015 19:51:39 -0500
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
        works exactly sometimes
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On 01/21/2015 07:23 PM, Jeffrey Brown wrote:
>    
>     data Dirn = Succ | Pred
>         deriving (Eq, Show, Ord)
> 

When you "derive" a typeclass instance, you get it magically for free.
So you don't need to do this:

>     -- implement Ord
>     (<=) Succ Pred = False
>     (<=) _ _ = True



> 
> But if I try to define the Rev instance the same way the Ord instance is
> being defined, it does not compile:
>
> ...
>        deriving (Eq, Show, Ord, Rev)

But you can't derive classes that you've defined yourself, it only works
for predefined ones. (That's how the compiler can do it magically.) So
"deriving (Rev)" won't work, because you made the "Rev" typeclass yourself.

In the second case, you have to do:

    instance Rev Dirn where
      rev Succ = Pred
      rev Pred = Succ

because you can't derive Rev, because it isn't a predefined typeclass.

One more thing: when "deriving (Ord)", the compiler uses the order that
you've typed the constructors in. So actually, the derived instance
would have Succ <= Pred, probably not what you want. I'd switch it to
"Pred | Succ" if I were you.

The details of why all this failed in a confusing way have been
explained in the other responses.



------------------------------

Message: 7
Date: Wed, 21 Jan 2015 19:52:36 -0500
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
        works exactly sometimes
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On 01/21/2015 07:50 PM, Brandon Allbery wrote:
> On Wed, Jan 21, 2015 at 7:48 PM, Michael Orlitzky <[email protected]
> <mailto:[email protected]>> wrote:
> 
>     Insufficient keming on that font? =)
> 
> 
> Probably... also half blind due to sinuses and the drugs trying to keep
> them at bay :/
> 

I only noticed because I copy/pasted from your response and knew
something was wrong but couldn't figure it out for a good 5 minutes.




------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 79, Issue 24
*****************************************

Reply via email to