Default implementation of type class member function not working as expected.

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

Default implementation of type class member function not working as expected.

David Banas-2
Hi all,

I have a typeclass, which defines the following two member functions:
(t is a Rose Tree.)

    getCompNodes   :: t -> [CompNode a]

    getAllCompNodes :: t -> [CompNode a]
    getAllCompNodes t = getCompNodes t
                     ++ (concatMap getAllCompNodes (subForest t))

The first one must be defined uniquely by each instance, but the second never needs a unique definition.
So, I provided its implementation in the typeclass definition, as shown.
However, I don’t get the expected behavior!
If I copy and paste the code, above, for getAllCompNodes into my instance definition, then I get the expected results.
Does anyone know why this is happening?

Thanks,
-db


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Default implementation of type class member function not working as expected.

Brandon Allbery
On Tue, May 20, 2014 at 9:34 PM, David Banas <[hidden email]> wrote:
The first one must be defined uniquely by each instance, but the second never needs a unique definition.
So, I provided its implementation in the typeclass definition, as shown.

This makes no sense; if there's only one definition, then define it *outside* the typeclass. A definition *inside* the class is there to be overridden by specific instances that need to do so.

That said, you haven't said what you are getting that is not expected, and there isn't enough information here to really say what might or might not be going on.

--
brandon s allbery kf8nh                               sine nomine associates
[hidden email]                                  [hidden email]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Default implementation of type class member function not working as expected.

David Banas-2
In reply to this post by David Banas-2
That worked, Brandon; thanks!

Gurus, I’d still love to understand exactly what I broke, trying to do it the other way. Any thoughts?

Thanks,
-db

On May 21, 2014, at 12:14 AM, [hidden email] wrote:

if there's only one definition, then define it
*outside* the typeclass.


I have a typeclass, which defines the following two member functions:
(t is a Rose Tree.)

   getCompNodes   :: t -> [CompNode a]

   getAllCompNodes :: t -> [CompNode a]
   getAllCompNodes t = getCompNodes t
                    ++ (concatMap getAllCompNodes (subForest t))

The first one must be defined uniquely by each instance, but the second
never needs a unique definition.
So, I provided its implementation in the typeclass definition, as shown.


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Default implementation of type class member function not working as expected.

Kim-Ee Yeoh
Administrator

On Wed, May 21, 2014 at 8:12 PM, David Banas <[hidden email]> wrote:
Gurus, I’d still love to understand exactly what I broke, trying to do it the other way. Any thoughts?

Provide more information.

If you re-read what you sent to the list, you'll find there's nothing to go by.

Things you need to provide:

* actual error messages
* the client code using the type classes
* best of all, minimized but fully compilable code exhibiting the problem

-- Kim-Ee

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Default implementation of type class member function not working as expected.

Sylvain HENRY
In reply to this post by David Banas-2
Are you trying to do something like that?

data Rose a = Rose a [Rose a]

class TreeLike t where
   getNodes :: t a -> [t a]
  
   getAllNodes :: t a -> [t a]
   getAllNodes t = getNodes t ++ (concatMap getAllNodes (getNodes t))

instance TreeLike Rose where
   getNodes (Rose _ xs) = xs


-Sylvain


2014-05-21 15:12 GMT+02:00 David Banas <[hidden email]>:
That worked, Brandon; thanks!

Gurus, I’d still love to understand exactly what I broke, trying to do it the other way. Any thoughts?

Thanks,
-db

On May 21, 2014, at 12:14 AM, [hidden email] wrote:

if there's only one definition, then define it
*outside* the typeclass.


I have a typeclass, which defines the following two member functions:
(t is a Rose Tree.)

   getCompNodes   :: t -> [CompNode a]

   getAllCompNodes :: t -> [CompNode a]
   getAllCompNodes t = getCompNodes t
                    ++ (concatMap getAllCompNodes (subForest t))

The first one must be defined uniquely by each instance, but the second
never needs a unique definition.
So, I provided its implementation in the typeclass definition, as shown.


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe