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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:47:26+00:00 2026-06-07T12:47:26+00:00

I am trying to make a simple graph structure and I wrote the following.

  • 0

I am trying to make a simple graph structure and I wrote the following. But GHG raises error and I stacked there. This is the first time I make my own typeclass so maybe I am doing something terribly wrong. Can somebody explain what is wrong?

I found a similar question but I don’t think it applies to my case.:
Error binding type variables in instance of typeclass

class Link l where
  node :: (Node n) => l -> n

class Node n where
  links :: (Link l) => n -> [l]

data (Node n) => SimpleLink n =
  SimpleLink
  { simpleLinkNode :: n
  } deriving (Show, Read, Eq)

instance (Node n) => Link (SimpleLink n) where
  node = simpleLinkNode

data (Link l) => SimpleNode l =
  SimpleNode
  { simpleNodeLinks :: [l]
  } deriving (Show, Read, Eq)

instance (Link l) => Node (SimpleNode l) where
  links = simpleNodeLinks

This is the error message I’ve got:

***.hs:13:10:Could not deduce (n ~ n1)
from the context (Node n)
  bound by the instance declaration
  at ***.hs:12:10-40
or from (Node n1)
  bound by the type signature for
             node :: Node n1 => SimpleLink n -> n1
  at ***.hs:13:3-23
  `n' is a rigid type variable bound by
      the instance declaration
      at ***.hs:12:16
  `n1' is a rigid type variable bound by
       the type signature for node :: Node n1 => SimpleLink n -> n1
       at ***.hs:13:3
Expected type: SimpleLink n -> n1
  Actual type: SimpleLink n -> n
In the expression: simpleLinkNode
In an equation for `node': node = simpleLinkNode

***.hs:21:11:Could not deduce (l ~ l1)
from the context (Link l)
  bound by the instance declaration
  at ***.hs:20:10-40
or from (Link l1)
  bound by the type signature for
             links :: Link l1 => SimpleNode l -> [l1]
  at ***.hs:21:3-25
  `l' is a rigid type variable bound by
      the instance declaration
      at ***.hs:20:16
  `l1' is a rigid type variable bound by
       the type signature for links :: Link l1 => SimpleNode l -> [l1]
       at ***.hs:21:3
Expected type: SimpleNode l -> [l1]
  Actual type: SimpleNode l -> [l]
In the expression: simpleNodeLinks
In an equation for `links': links = simpleNodeLinks

Edit 1

I tried some of Daniel’s suggestions.
But I couldn’t make them work.

constructor class

Got: “`n’ is not applied to enough type arguments”

class Link l n where
  node :: Node n l => l n -> n l
class Node n l where
  links :: Link l n => n l -> [l n]

multi-parameter type class (MPTC)

Got: “Cycle in class declarations (via superclasses)”

class (Node n) => Link l n where
  node :: l -> n
class (Link l) => Node n l where
  links :: n -> [l]

MPTC with functional dependencies

Got: “Cycle in class declarations (via superclasses)”

class (Node n) => Link l n | l -> n where
  node :: l -> n
class (Link l) => Node n l | n -> l where
  links :: n -> [l]

Goal (Edit 2)

What I want to implement is a directed acyclic graph structure like the following (more specifically, a Factor graph).

PRML Figure 8.51
(source: microsoft.com)

There are two kinds of node (white circle and red square) and they connect only to the different type of node, meaning that there are two kinds of links.

I want different version of nodes and links which have data (arrays) attached to them. I also want “vanilla” DAG which has only one type of node and link. But for traversing them, I want only one interface to do that.

  • 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-07T12:47:28+00:00Added an answer on June 7, 2026 at 12:47 pm

    I am breaking stack overflow etiquette and adding a second answer to keep this separate. This is a simple code example for a bipartite undirected graph with unlabeled edges, which might be useful to model a Factor Graph:

    -- Bipartite graph representation, unlabeled edges
    
    -- Data types to hold information about nodes, e.g. ID number
    data VariableVertex = VV { vvID :: Int }  deriving (Show)
    data FactorVertex = FV { fvID :: Int }  deriving (Show)
    
    -- Node holds itself and a list of neighbors of the oppostite type
    data Node selfType adjacentType =
      N { self :: selfType
        , adj :: [Node adjacentType selfType] }
    
    -- A custom Show for Node to prevent infinite output
    instance (Show a, Show b) => Show (Node a b) where
      show (N x ys) = "Node "++ show x ++ " near " ++ show (map self ys)
    
    -- Type aliases for the two node types that will be used
    type VariableNode = Node VariableVertex FactorVertex
    type FactorNode = Node FactorVertex VariableVertex
    
    data FactorGraph = FG [VariableNode] [FactorNode]  deriving (Show)
    
    v1 = N (VV 1) [f1,f2]
    v2 = N (VV 2) [f2]
    v3 = N (VV 3) [f1,f3]
    f1 = N (FV 1) [v1,v3]
    f2 = N (FV 2) [v1,v2]
    f3 = N (FV 3) [v3]
    
    g = FG [v1,v2,v3] [f1,f2,f3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make simple minesweeper game in python, but have one problem. I have
Trying to make a simple number clicker control for BlackBerry 6/7, like this: At
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
I'm trying make my first python app. I want make simple email sender form.
Im trying to make a simple downloader, but i cant make it to update
I am trying to make simple testing but failed because I have no clue
I am trying to make a simple program that handles files and directories, but
I am trying to make a simple page where there will be a Highstock
I am trying to make a graph of a simple function y=k*x+b . A

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.