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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:24:31+00:00 2026-06-07T20:24:31+00:00

I am new to Haskell and was trying to create a class with an

  • 0

I am new to Haskell and was trying to create a class with an instance.

I have the following code and I am trying to say that:
1) Branch can hold any type in its first position,
2) Make a class Linear which takes the linear thing and returns a number
3) Make Branch an instance of Linear if the first thing the Branch holds is something that is part of the Num class.

data Branch a = Branch a Integer deriving (Show, Eq)

class Linear l where
    length :: (Num a) => l -> a

instance (Num a) => Linear (Branch a) where
    length (Branch len _) = len

I get the error:
Could not deduce (a ~ a1)
from the context (Num a) bound by the instance declaration at…..

Does anybody know how to express in Haskell what it is I’m trying to say?

  • 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-07T20:24:33+00:00Added an answer on June 7, 2026 at 8:24 pm

    Your class definition says that length must be able to return any Num type that the user requests. So if the user wants an Integer, the length method must give him an Integer. And if he wants an Int or a Double instead, length must give him that too.

    However the length function you supply in your instance declaration does not meet the requirement. For example when you call length on a Branch Integer, length will return an Integer. It won’t return an Int or a Double even if the user asks for it.

    One way to make your code work would be to use multi-parameter type classes to define Linear with two parameters where the second type is the numeric type length should return. You could then have an instance for Linear (Branch a) a. You may also want to use the Functional Dependencies extension to make this more usable. Instead of a second type parameter, you may also use the Type Families extension to achieve a similar effect.

    Another way would be to change the instance declaration to require Integral a instead of Num a and then use fromIntegral len as the return value. This will convert the integral type stored in the Branch to whichever numeric type the User requested. The caveat is, of course, that you branches with non-integral numeric values, won’t be instances of Linear this way.

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

Sidebar

Related Questions

I'm new to haskell, and so I'm trying to recreate the following C++ code
i'm new in Haskell. I have to write function that takes list of Integers
Hi i'm new to haskell and i'm trying to implement the following and I
New to Haskell and have a stumbling block. I'm trying to filter a list
I'm new to haskell and I'm trying to do my exercise for my class,
I'm trying to add an instance declaration in Haskell for a new data type
Still new to Haskell, I have hit a wall with the following: I am
[Disclaimer] I am very new to Haskell (and any FPL for that matter), just
I am new to Haskell and trying to fiddle with some test cases I
Hello I am new at Haskell and i'm having problems trying to get this

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.