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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:41:42+00:00 2026-05-23T22:41:42+00:00

I have a question about functional dependencies. My understanding was that, for example, if

  • 0

I have a question about functional dependencies. My understanding was that, for example, if I write class Graph g a b | g -> a, g -> b, then any specific g can be associated with only one type of a and b. Indeed, trying to declare two instance with the same g and different a and b does not work.

However, the compiler (ghc) seems unable to use the dependency in the following case,

class (Eq a, Eq b) => Graph g a b | g -> a, g -> b where
    edges :: g -> [b]
    src :: g -> b -> a
    dst :: g -> b -> a

    vertices :: g -> [a]
    vertices g = List.nub $ map (src g) (edges g) ++ map (dst g) (edges g)

class Graph g a b => Subgraph g a b | g -> a, g -> b where
    extVertices :: g -> [b]

data Subgraph1 g where
    Subgraph1 :: Graph g a b => g -> [b] -> Subgraph1 g

instance Graph g a b => Graph (Subgraph1 g) a b where
    vertices (Subgraph1 g _) = vertices g
    edges (Subgraph1 g _) = edges g
    src (Subgraph1 g _) = src g
    dst (Subgraph1 g _) = dst g

If I revise Subgraph1 by adding the parameters a and b to the type signature, then everything works out.

data Subgraph1 g a b where
    Subgraph1 :: Graph g a b => g -> [b] -> Subgraph1 g a b
  • 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-23T22:41:42+00:00Added an answer on May 23, 2026 at 10:41 pm

    Don’t use fundeps, they are too much pain. Use associated types.

    class (Eq (Vertex g), Eq (Edge g)) => Graph g where
      type Edge   g :: *
      type Vertex g :: *
    
      edges :: g -> [Edge g]
      src   :: g -> Edge g -> Vertex g
      dst   :: g -> Edge g -> Vertex g
    
      vertices :: g -> [Vertex g]
      vertices g = nub $ map (src g) (edges g) ++ map (dst g) (edges g)
    
    class Graph g => Subgraph g where
      extVertices :: g -> [Edge g]
    
    data Subgraph1 g where
        Subgraph1 :: Graph g => g -> [Edge g] -> Subgraph1 g
    
    instance Graph g => Graph (Subgraph1 g) where
        type Edge (Subgraph1 g) = Edge g
        type Vertex (Subgraph1 g) = Vertex g
        vertices (Subgraph1 g _) = vertices g
        edges (Subgraph1 g _) = edges g
        src (Subgraph1 g _) = src g
        dst (Subgraph1 g _) = dst g
    

    This looks somewhat more readable. Edge g is the type of g‘s edges, etc.

    Note that I translated your code mechanically, without understanding what Subgraph1 does. Why do you need a GADT here, and what the second argument of the data constructor means? It is not used anywhere.

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

Sidebar

Related Questions

Simple question, but one that I've been curious about...is there a functional difference between
I recently asked a question about functional programming, and received (good!) answers that prompted
Several questions about functional programming languages have got me thinking about whether XSLT is
I have a question about the following C code: void my_function() { int i1;
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex
I have a question about locking. This doesn't have to be only about record
I have a question about how to deploy WPF application into a PC without

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.