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 308837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:41:13+00:00 2026-05-12T07:41:13+00:00

I’m having trouble with classes in haskell. Basically, I have an algorithm (a weird

  • 0

I’m having trouble with classes in haskell.

Basically, I have an algorithm (a weird sort of graph-traversal algorithm) that takes as input, among other things, a container to store the already-seen nodes (I’m keen on avoiding monads, so let’s move on. :)). The thing is, the function takes the container as a parameter, and calls just one function: “set_contains”, which asks if the container… contains node v. (If you’re curious, another function passed in as a parameter does the actual node-adding).

Basically, I want to try a variety of data structures as parameters. Yet, as there is no overloading, I cannot have more than one data structure work with the all-important contains function!

So, I wanted to make a “Set” class (I shouldn’t roll my own, I know). I already have a pretty nifty Red-Black tree set up, thanks to Chris Okasaki’s book, and now all that’s left is simply making the Set class and declaring RBT, among others, as instances of it.

Here is the following code:

(Note: code heavily updated — e.g., contains now does not call a helper function, but is the class function itself!)

data Color = Red | Black
data (Ord a) => RBT a = Leaf | Tree Color (RBT a) a (RBT a)

instance Show Color where
    show Red = "r"
    show Black = "b"

class Set t where
    contains :: (Ord a) => t-> a-> Bool

-- I know this is nonesense, just showing it can compile.
instance (Ord a) => Eq (RBT a) where
    Leaf == Leaf = True
    (Tree _ _ x _) == (Tree _ _ y _) = x == y

instance (Ord a) => Set (RBT a) where
    contains Leaf b = False
    contains t@(Tree c l x r) b
        | b == x    = True
        | b < x     = contains l b
        | otherwise = contains r b

Note how I have a pretty stupidly-defined Eq instance of RBT. That is intentional — I copied it (but cut corners) from the gentle tutorial.

Basically, my question boils down to this: If I comment out the instantiation statement for Set (RBT a), everything compiles. If I add it back in, I get the following error:

RBTree.hs:21:15:
    Couldn't match expected type `a' against inferred type `a1'
      `a' is a rigid type variable bound by
          the type signature for `contains' at RBTree.hs:11:21
      `a1' is a rigid type variable bound by
           the instance declaration at RBTree.hs:18:14
    In the second argument of `(==)', namely `x'
    In a pattern guard for
       the definition of `contains':
          b == x
    In the definition of `contains':
        contains (t@(Tree c l x r)) b
                   | b == x = True
                   | b < x = contains l b
                   | otherwise = contains r b

And I simply cannot, for the life of me, figure out why that isn’t working. (As a side note, the “contains” function is defined elsewhere, and basically has the actual set_contains logic for the RBT data type.)

Thanks! – Agor

Third edit: removed the previous edits, consolidated above.

  • 1 1 Answer
  • 1 View
  • 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-12T07:41:14+00:00Added an answer on May 12, 2026 at 7:41 am

    You could also use higher-kinded polyphormism. The way your class is defined it sort of expects a type t which has kind *. What you probably want is that your Set class takes a container type, like your RBT which has kind * -> *.

    You can easily modify your class to give your type t a kind * -> * by applying t to a type variable, like this:

    class Set t where
        contains :: (Ord a) => t a -> a -> Bool
    

    and then modify your instance declaration to remove the type variable a:

    instance Set RBT where
        contains Leaf b = False
        contains t@(Tree c l x r) b
            | b == x    = True
            | b < x     = contains l b
            | otherwise = contains r b
    

    So, here is the full modified code with a small example at the end:

    data Color = Red | Black
    data (Ord a) => RBT a = Leaf | Tree Color (RBT a) a (RBT a)
    
    instance Show Color where
        show Red = "r"
        show Black = "b"
    
    class Set t where
        contains :: (Ord a) => t a -> a -> Bool
    
    -- I know this is nonesense, just showing it can compile.
    instance (Ord a) => Eq (RBT a) where
        Leaf == Leaf = True
        (Tree _ _ x _) == (Tree _ _ y _) = x == y
    
    instance Set RBT where
        contains Leaf b = False
        contains t@(Tree c l x r) b
            | b == x    = True
            | b < x     = contains l b
            | otherwise = contains r b
    
    tree = Tree Black (Tree Red Leaf 3 Leaf) 5 (Tree Red Leaf 8 (Tree Black Leaf 12 Leaf))
    
    main =
        putStrLn ("tree contains 3: " ++ test1) >>
        putStrLn ("tree contains 12: " ++ test2) >>
        putStrLn ("tree contains 7: " ++ test3)
        where test1 = f 3
              test2 = f 12
              test3 = f 7
              f = show . contains tree
    

    If you compile this, the output is

    tree contains 3: True
    tree contains 12: True
    tree contains 7: False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could try using a virtual machine. VirtualBox is a… May 13, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer No, servlets provide no api to get at the raw… May 13, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer You probably have a pointer to a Passenger, not a… May 13, 2026 at 1:14 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.