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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:26:49+00:00 2026-06-14T18:26:49+00:00

This is what I’m trying to do: data X = I Int | D

  • 0

This is what I’m trying to do:

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

{-
-- A normal declaration which works fine
instance Num X where
  (I a) + (I b) = I $ a + b
  (D a) + (D b) = D $ a + b
  -- ...   
-}                                          

coerce :: Num a => X -> X -> (a -> a -> a) -> X
coerce (I a) (I b) op = I $ a `op` b
coerce (D a) (D b) op = D $ a `op` b

instance Num X where
  a + b = coerce a b (+)

When compiling I get an error:

 tc.hs:18:29:
     Couldn't match type `Double' with `Int'
     In the second argument of `($)', namely `a `op` b'
     In the expression: I $ a `op` b
     In an equation for `coerce': coerce (I a) (I b) op = I $ a `op` b

In coerce I’d like to interpret op as both Int -> Int -> Int and Double -> Double -> Double. I think I should be able to do this because op is of type Num a => a -> a -> a.

My main goal is to abstract away the repetition needed in the functioning Num subclass: I’d much rather write it like I did in the uncommented version.

  • 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-14T18:26:50+00:00Added an answer on June 14, 2026 at 6:26 pm

    Your definition of coerce restricts type of op to Int -> Int -> Int by first definition and Double -> Double -> Double by second. If you really want to say that op is polymorphic in a for all Num class then you should use Rank2Types to make it work.

    coerce :: X -> X -> (forall a . Num a => a -> a -> a) -> X
    coerce (I a) (I b) op = I $ a `op` b
    coerce (D a) (D b) op = D $ a `op` b
    coerce (I a) (D b) op = D $ op (fromIntegral a) b
    coerce (D a) (I b) op = D $ op a (fromIntegral b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This works. $(document).ready(function(){ $(.items article).click(function(){ window.location=$(this).find(a).attr(href); return false; }); }); However , when the
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This is my first attempts at creating a data-mart/warehouse and I am a little
This is what I came up with: #include <stdio.h> int main (void) { int
This is my code: OleDbConnection connection = new OleDbConnection( Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\Offline.accdb;Persist Security Info=False); connection.Open();
This one really has me stumped. I'm trying to deserialize the following JSON string
this is my first question in here, and I would like to ask if
This question is directly related to this SO question I posed about 15 minutes

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.