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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:31:10+00:00 2026-06-15T00:31:10+00:00

I have my own data type to represent nodes and edges of a graph

  • 0
  • I have my own data type to represent nodes and edges of a graph as follows:

    data Node a = Node a deriving (Show, Eq)
    
    data Label a = Label a deriving (Show)
    
    data Cost = CostI Int | CostF Float deriving (Show)
    
    data Edge label node = Edge (Label label, (Node node,Node node), Cost) deriving (Show)
    
  • Now, I create a function to check whether an edge contains 2 nodes or not as follows:

    isEdge:: (Eq n) => (Edge l n) -> (Node n, Node n) -> Bool
    isEdge (Edge (_, (n1,n2), _)) (n3, n4) = result
         where result = (n1 == n3) && (n2 == n4)
    
  • The function works well, the problem here is if I remove (Eq n) from the function, it fails. So, why is that, even though in the declaration above I declared Node as deriving from Eq class?

    data Node a = Node a deriving (Show, Eq)
    
  • 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-15T00:31:12+00:00Added an answer on June 15, 2026 at 12:31 am

    The Eq instance GHC derives for Node a is something like this:

    instance Eq a => Eq (Node a) where
        (Node x) == (Node y) = x == y
        (Node x) /= (Node y) = x /= y
    

    You can view the generated code by compiling with -ddump-deriv. The Eq a constraint is needed for obvious reasons. So, GHC couldn’t infer an instance of Eq for, say, Node (a -> b) since functions can’t be compared.

    However, the fact that GHC can’t infer an instance of Eq for Node a for some a doesn’t mean it will stop you from constructing a values of type Node a where a isn’t an equality type.


    If you wanted to stop people from constructing non-comparable Nodes, you could try putting a constraint like this:

    data Eq a => Node a = Node a deriving (Eq, Show)
    

    But now GHC tells us we need a compiler pragma:

    Illegal datatype context (use -XDatatypeContexts): Eq a =>
    

    OK, let’s add it to the top of our file:

    {-# LANGUAGE DatatypeContexts #-}
    

    Now compile:

    /tmp/foo.hs:1:41: Warning: -XDatatypeContexts is deprecated: It was widely
    considered a misfeature, and has been removed from the Haskell language.
    

    The problem is that now every function using Nodes will need an Eq class constraint, which is annoying (your functions still need the constraint!). (Also, if your user wants to create Nodes using a non-equality type but never tests them for equality, what’s the problem?)


    There’s actually a way to get GHC to do what you want, however: Generalized Algebraic Data Types (GADTs):

    {-# LANGUAGE GADTs, StandaloneDeriving #-}
    
    data Node a where
      Node :: Eq a => a -> Node a
    

    This looks just like your original definition, except that it emphasizes the Node value constructor (the one formerly on the right hand side of the data declaration) is just a function, which you can add constraints to. Now GHC knows that only equality types can be put into Nodes, and unlike our earlier attempted solution, we can make new functions that don’t need a constraint:

    fromNode :: Node a -> a
    fromNode (Node x) = x
    

    We can still derive Eq and Show instances, but with a slightly different syntax:

    deriving instance Eq   (Node a)
    deriving instance Show (Node a)
    

    (Hence the StandaloneDeriving pragma above.)

    For this to work, GHC also requires us to add a Show constraint to our GADT (if you look at the generated code again, you’ll see the constraints are now gone):

    data Node a where
      Node :: (Eq a, Show a) => a -> Node a
    

    And now we can take the Eq constraint off isEdge, since GHC can infer it!

    (This is definitely overkill for such a simple situation — again, if people want to construct nodes with functions inside them, why shouldn’t they? However, GADTs are extremely useful in pretty similar situations when you want to enforce certain properties of your data types. See a cool example).


    EDIT (from the future): you can also write

    data Node a = (Eq a, Show a) => Node a
    

    but you still need to enable GADT extensions and derive instances separately. See this thread.

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

Sidebar

Related Questions

I have declared my own type: data Book = Bookinfo { bookId :: Int,
I have my own data type: type Types = String data MyType = MyType
I have my own classes that represent image data. They have various underlying structure
We have our own data streaming algorithm that include some metadata+records+fields values. Currently we
I've got an asp.net application where each client will have their own data entry
The main concept goes like this. I have four listviews with its own data
I have create my own NSOpenGLView class, right now the data that i want
I have some data which look like that: PMID- 19587274 OWN - NLM DP
Trying a simple project to create my own (very basic) data binding. I have
I think I have a problem with Haskell type inference. I made my own

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.