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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:50:35+00:00 2026-06-17T17:50:35+00:00

I have a complex and recursive data structure which I have simplified to the

  • 0

I have a complex and recursive data structure which I have simplified to the following:

data Node = Node { value :: Integer, next :: Node } deriving (Show,Eq)

Given the following expressions:

--Create a circular structure
a = Node 1 b
b = Node 0 a --Tie the knot
c = Node 1 b --Another structure which points to b

The expressions a and c are conceptually equal: they both represent a node which holds the value 1 and points to the expression b. My question is: how do I check that they are indeed equal in a Haskell expression? If I evaluate a == c it will keep evaluating sub-elements in the circular structure forever.

Is it possible to perform such a comparison in Haskell?

EDIT: In my case, I am trying to compare the two for inspection/debugging purposes. But another reason to do this could be for unit testing.

  • 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-17T17:50:37+00:00Added an answer on June 17, 2026 at 5:50 pm

    First of all, a and b are not equal, but a and c are equal, not just conceptually, but they are in fact the same thing.

    To answer your question: there is no drop-in solution to your problem. If you need identity comparison, you first have to establish a notion of identity. One way to do this is to have a Map from keys to nodes:

    data Node k =
        Node {
          nodeValue :: Integer,
          nodeNext  :: k
        }
    

    The idea is that you have a separate Map from keys of type k to nodes. However, you can’t write an Eq instance for that one. A somewhat elegant way to solve this is to use reflection:

    {-# LANGUAGE ScopedTypeVariables #-}
    
    import Data.Reflection
    
    data Node n k =
        Node {
          nodeValue :: Integer,
          nodeNext  :: k
        }
    
    instance (n `Reifies` Map k (Node n k)) => Eq (Node n k) where
        (==) = {- ... -}
            where
            nodeMap :: Map k (Node n k)
            nodeMap = reflect (Proxy :: Proxy n)
    

    Another option that gained some attention recently is the notion of observable sharing.

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

Sidebar

Related Questions

I have a recursive immutable data structure in ocaml which can be simplified to
I have a recursive data structure. Something like... Public Class Comparison Property Id As
I have a nested, mutual recursive data structure , and want to associate computational
I have a complex Python data structure (if it matters, it's a large music21
I have a simple recursive function RCompare() that calls a more complex function Compare()
I am using WCF and REST, and I have complex types, which are working
I have some complex message to show inside of a flash message so instead
I have several complex data structures like Map< A, Set< B > > Set<
I have some complex regular expressions which I need to comment for readability and
I have a somewhat complex custom user control, created in C# .NET, which is

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.