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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:08:48+00:00 2026-06-06T12:08:48+00:00

I am writing a simple hash tree structure in the following program hash_lookup.hs :

  • 0

I am writing a simple hash tree structure in the following program hash_lookup.hs:

module Main where

    data (Eq a) => HashTable a b = HashChildren (a, [HashTable a b]) | Hash (a, b) deriving (Show)

    getKey :: HashTable a b -> a

    getKey (HashChildren (k, hs)) = k
    getKey (Hash (k, h)) = k

    lookUp :: [a] -> HashTable a b -> Maybe (HashTable a b)

    lookUp [] table = return table
    lookUp _ (Hash _) = Nothing
    lookUp (p:path) (HashChildren (_, ts) ) = lookUp path ( head ( dropWhile (\x -> (getKey x) /= p) ts ) )

getKey is intended to retrieve the root key of a given HashTable, and lookUp takes a list of strings and is meant to follow the first path it finds until it either reaches the full path or fails (I’m aware that this isn’t natural behaviour for a tree but it’s what my tutorial wants).

I have two questions:
1) Why do I get an error message telling me that a /= a (from the final line) is not allowed as there is No instance for (Eq a) (the error message in terminal), despite (Eq a) in the data declaration?

2) Other than the error I’m getting and the seemingly strange behaviour of the lookup function, is this good or idiomatic Haskell?

Thanks

  • 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-06-06T12:08:50+00:00Added an answer on June 6, 2026 at 12:08 pm

    One of the common “gotchas” in Haskell is that if you put class constraints on a data declaration, every function that uses the type must have the class constraints too. For this reason, your data declaration is not idiomatic in Haskell, and you should eliminate its class constraint. It doesn’t gain you anything to declare the class constraint in the data declaration anyway.

    Basically, function types must repeat the class constraint. How else is a user of the function to know that they must use an instance of the classes in question? Notice that you can have a function f defined in terms of a function g defined in terms of an h that’s defined in terms of your HashTable a b type, such that f doesn’t mention your HashTable type at all, yet because of the type dependencies it takes an argument of type a that indirectly and ultimately is used as the first parameter of your HashTable type. f‘s type must have the class constraint, Haskell correctly infers so, and it will reject your type annotations if they lack it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm writing a simple heap data structure for my own convienience and realized
I'm writing a simple content management system. I need to store SHA1 hash values
I am writing simple parallel program in C++ using OpenMP. I am working on
I'm writing simple program to communicate between smart devices and I receive 11001 when
I need a collection data-structure that can do the following: Be sorted Allow me
I'm in the middle of writing a simple hash function which get the input
I am writing a simple app with 24 items in a hash to be
Writing a simple program that will find exact duplicate files on my computer, but
I'm writing a simple program for file encryption. Mostly as an academic exercise but
I'm writing a simple hash map class: template <class K, class V> class HashMap;

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.