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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:41:01+00:00 2026-06-16T13:41:01+00:00

I’m pretty new to Haskell, so I hope this isn’t a stupid question. I

  • 0

I’m pretty new to Haskell, so I hope this isn’t a stupid question. I have this data type:

data N = I Int | D Double deriving (Show, Eq)

I’m trying to write a function with the signature (Num a) => (a -> a -> a) -> N -> N -> N which applies the function to the numbers within the Ns and returns an N with the result. If the Ns are both Ds, it should just apply the function and return a D; if one is an I and the other is a D, it should convert the Int in the I to a Double, apply the function to the two Doubles, and return a D; and if both are Is, it should apply the function and return an I. Here’s the (broken) code I have so far:

widen :: N -> N -> (N, N)
widen (I i) d@(D _) = (D (fromIntegral i), d)
widen d@(D _) i@(I _) = widen i d
widen x y = (x, y)

numOp :: (Num a) => (a -> a -> a) -> N -> N -> N
numOp op x y = case widen x y of (D x', D y') -> D $ x' `op` y'
                                 (I x', I y') -> I $ x' `op` y'

I get an error on both lines of numOp, though. The first one is:

Could not deduce (a ~ Double)
from the context (Num a)
  bound by the type signature for
             numOp :: Num a => (a -> a -> a) -> N -> N -> N
  at <line num>
In the second argument of `($)', namely x' `op` y'
In the expression: D $ x' `op` y'
In a case alternative: (D x', D y') -> D $ x' `op` y'

And the second:

Couldn't match type `Double' with `Int'
Expected type: Int
  Actual type: a
In the second argument of `($), namely x' `op` y'
In the expression: I $ x' `op` y'
In a case alternative: (I x', I y') -> I $ x' `op` y'

I’m pretty sure I understand what both errors mean; I think the first one is saying that the information in my type signature isn’t enough for GHC to assume that op returns a Double, which is required by the D value constructor, and the second one is saying that since the first line implies that a is Double, this line can’t use a value of type a as though it’s an Int. I don’t have any idea where to start looking for the right way to do this, though.

If it helps, the reason I’m trying to get this to work is that I’m following along with the Write Yourself a Scheme tutorial; all the examples in the tutorial (specifically in the Evaluation section) only deal with integers, but as an exercise I’d like to add the ability to support both integral and floating point numbers so that e.g. (+ 1 2.5 2.5) returns 6.0 and (+ 1 2 3) returns 6. If I’m thinking about this the wrong way or there’s an easier way to accomplish it, I’d love to hear suggestions.

  • 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-16T13:41:03+00:00Added an answer on June 16, 2026 at 1:41 pm

    The signature

    numOp :: (Num a) => (a -> a -> a) -> N -> N -> N
    

    says that numOp takes any monomorphic function of type a -> a -> a for every specific instance of Num and two Ns and from that computes an N. So for example, a function of type

    Complex Float -> Complex Float -> Complex Float
    

    or

    approxRational :: RealFrac a => a -> a -> Rational
    

    (specialised to a = Rational) would be legitimate first arguments.

    What you need is a polymorphic function that can handle all Num instances as the first argument, i.e. the rank 2 type

    numOp :: (forall a. Num a => a -> a -> a) -> N -> N -> N
    

    (you need the RankNTypes language extension for that).

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

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.