Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5983101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:10:55+00:00 2026-05-22T22:10:55+00:00

I’m having a simple problem when it comes to writing up typeclasses that inherit

  • 0

I’m having a simple problem when it comes to writing up typeclasses that inherit from one another. I’m trying to create a hierarchy of typeclasses to achieve some level of representational abstraction. Let’s say I want a collections typeclass:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}

class Collection c a where
  isMember :: a -> c -> Bool

And I’ve defined a tree type:

data Tree a = Empty | Node a (Tree a) (Tree a)
  deriving (Show, Eq)

I’d like to make my tree a collection, so:

inOrder :: Tree a -> [a]
inOrder Empty = []
inOrder (Node a l r) = (inOrder l) ++ [a] ++ (inOrder r)

instance (Eq a) => Collection (Tree a) a where
  isMember a c = a `elem` (inOrder c)

This doesn’t quite work right:

*Main> (isMember '2' Empty)
<interactive>:1:1:
    No instance for (Collection (Tree a) Char)
      arising from a use of `isMember' at <interactive>:1:1-18
    Possible fix:
      add an instance declaration for (Collection (Tree a) Char)
    In the expression: (isMember '2' Empty)
    In the definition of `it': it = (isMember '2' Empty)

Presumably the value of the typeclass would be lost if I had to create an implementation for each concrete type. So I’m not writing the instance declaration correctly. But I can’t quite figure out how to proceed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:10:56+00:00Added an answer on May 22, 2026 at 10:10 pm

    The issue here is that, by default, each type class parameter is independent of the others. Simply applying isMember to a Char and a tree of unknown element type isn’t enough to let it deduce that it should use the (seemingly obvious) instance.

    This is clearly not how you want it to work, and a bit silly to boot, but that’s how it works. To deal with the problem, you need to give it some way of making the connection, for which there are other extensions: FunctionalDependencies is the more common, while TypeFamilies is newer and much nicer to use, but still has some rough edges.

    With functional dependencies, you would specify that some subset of the type class parameters fully determine others, e.g. Collection c a | c -> a. This has its own pitfalls, but works well enough in many cases.

    With type families, you would probably reduce this to a single-parameter type class, with the element type associated, e.g.:

    class Collection c where
        type Elem c
        isMember :: Elem c -> c -> Bool
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.