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

  • SEARCH
  • Home
  • 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 3426226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:41:22+00:00 2026-05-18T06:41:22+00:00

I am trying to represent weighted edges. I eventually want to have OutE to

  • 0

I am trying to represent weighted edges. I eventually want to have OutE to be an instance of Eq and Ord, with the constraint that etype is an instance of Eq and Ord. Assume I have following file as temp.hs:

data (Ord etype)=> OutE vtype etype = OutE {destVertex:: vtype, edgeValue::etype}

applyFunBy accessor ordfun = (\x y -> (ordfun (accessor x) (accessor y)))

instance Eq (OutE vtype etype) where
    --(==) :: Ord etype => (OutE vtype etype) -> (OutE vtype etype) -> Bool
    --(/=) :: Ord etype => (OutE vtype etype) -> (OutE vtype etype) -> Bool
    (==) = applyFunBy edgeValue (==)
    (/=) = applyFunBy edgeValue (/=)

when I load this in ghci, I get the following errors:

temp.hs:10:19:
    Could not deduce (Ord etype)
      from the context (Eq (OutE vtype etype))
      arising from a use of `edgeValue' at temp.hs:10:19-27
    Possible fix:
      add (Ord etype) to the context of the instance declaration
    In the first argument of `applyFunBy', namely `edgeValue'
    In the expression: applyFunBy edgeValue (==)
    In the definition of `==': == = applyFunBy edgeValue (==)

temp.hs:11:19:
    Could not deduce (Ord etype)
      from the context (Eq (OutE vtype etype))
      arising from a use of `edgeValue' at temp.hs:11:19-27
    Possible fix:
      add (Ord etype) to the context of the instance declaration
    In the first argument of `applyFunBy', namely `edgeValue'
    In the expression: applyFunBy edgeValue (/=)
    In the definition of `/=': /= = applyFunBy edgeValue (/=)
Failed, modules loaded: none.

If include the lines for the type signatures for (==) and (\=), I get:

temp.hs:6:1:
    Misplaced type signature:
    == ::
      (Ord etype) => (OutE vtype etype) -> (OutE vtype etype) -> Bool

temp.hs:7:1:
    Misplaced type signature:
    /= ::
      (Ord etype) => (OutE vtype etype) -> (OutE vtype etype) -> Bool
  • 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-18T06:41:22+00:00Added an answer on May 18, 2026 at 6:41 am

    You limited etype to be Ord in the defintion of OutE:

    data (Ord etype) => OutE vtype etype = ...
    

    But in the Eq instance, you’re actually trying to define the instance for any etype unrestrictedly.

    instance Eq (OutE vtype etype) where
    

    Of course this doesn’t work since OutE itself is just defined for Ord etypes, thus you’ll have to add the typeclass constraint to the instance definition as well.

    instance (Ord etype) => Eq (OutE vtype etype) where
    

    Note that one definition of either == or /= is sufficient for the typeclass to work.


    Note that it’s often easier and therefore considered better style not to have typeclass constraints on data-types, but just on instances/methods that actually require the functionality of the typeclass.

    In many cases, one does not need the constraint and just ends up with unnecessarily clumsy type signatures.

    Take e.g. some ordered map type Ord key => Map key value.

    What if we just want to list all keys? Or get the number of elements? We don’t need the keys to be Ord for these, so why not just leave the map unrestricted with simple

    getKeys :: Map key value -> [key]
    getLength :: Map key value -> Int
    

    and just add the typeclass when we really need it in a function like

    insert :: Ord key => key -> value -> Map key value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to represent a graph using disjoint union and record. The following
I am trying to deserialize a list of key value pairs that represent a
Here is my custom class that I have that represents a triangle. I'm trying
I have an interesting problem trying to represent complex schedule data in a database.
I'm trying to represent the following DTD fragment in XSD:- (A | B)* |
Trying to implement a function that will return a list of ints the represent
I'm just trying to get the equivalent HTML code that represent a specific control
I am trying to represent the following situation, relating to the construction of a
overflowians, I am trying to represent a 3 tiered Navigation menu that are inside
I have the following function which is passed an object which i am trying

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.