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

  • Home
  • SEARCH
  • 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 508129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:52:31+00:00 2026-05-13T06:52:31+00:00

I thought I would try modeling some numerical integration on vector quantities of different

  • 0

I thought I would try modeling some numerical integration on vector quantities of different dimensionality, and figured that type classes were the way to go. I needed something to define the difference between two values and to scale it by a multiplier (to get the derivative), as well as being able to take the distance function.

So far I have:

class Integratable a where
    difference :: a -> a -> a
    scale :: Num b => a -> b -> a
    distance :: Num b => a -> a -> b

data Num a => Vector a = Vector1D a | Vector2D a a

instance Num a => Integratable (Vector a) where
    difference (Vector1D x1) (Vector1D x2) = Vector1D (x1 - x2)
    scale (Vector1D x) m = Vector1D (x * m)
    distance (Vector1D x1) (Vector1D x2) = x1 - x2
    difference (Vector2D x1 y1) (Vector2D x2 y2) = Vector2D (x1 - x2) (y1 - y2)
    scale (Vector2D x y) m = Vector2D (x * m) (y * m)
    distance (Vector2D x1 y1) (Vector2D x2 y2) = sqrt((x1-x2)*(x1-x2)
                                                      + (y1-y2)*(y1-y2))

Unfortunately there are a couple of problems here that I haven’t figured out how to resolve. Firstly, the scale function gives errors. GHC can’t tell that m and x are compatible since the rigid type restriction Num is given in the instance in one case, and in the Vector type in the other case… Is there a way to specify that x and m are the same type?

(I realize in fact that even if x and m are both Num, they may not be the same Num. How can I specify this? If I can’t figure it out with Num, using Double would be fine, but I’d rather keep it general.)

There’s a similar problem with distance. Attempting to specify that the return type is Num fails, since it can’t tell in the instance definition that a is going to contain values that are compatible with 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-13T06:52:31+00:00Added an answer on May 13, 2026 at 6:52 am

    EDIT: It seems to me now that the article on functional dependencies from the HaskellWiki provides the key information in the best form that I can find, so I’d suggest reading that instead of my answer here. I’m not removing the rest of the content, though, as it makes clear (I hope) why FDs are useful here.


    Apart from the grouping of definitions issue which Dave pointed out…

    (I realize in fact that even if x and m are both Num, they may not be the same Num. How can I specify this? If I can’t figure it out with Num, using Double would be fine, but I’d rather keep it general.)

    This is the main problem, actually. You can’t multiply an Integer by a Float, say. In effect, you need the x and the m in scale to be of the same type.

    Also, a similar issue arises with distance, with the additional complication that sqrt needs a Floating argument. So I guess you’d need to mention that somewhere too. (Most likely on the instance, I guess).

    EDIT: OK, since sqrt only works on Floating values, you could roll a typeclass for those to upcast Floats to Doubles when needed.

    Another idea involves having a typeclass Scalable:

    data Vector a = Vector1D a | Vector2D a a deriving (Show)                       
    
    class Scalable a b | a -> b where
      scale :: a -> b -> a
    
    instance (Num a) => Scalable (Vector a) a where
      scale (Vector1D x)   m = (Vector1D (x * m))
      scale (Vector2D x y) m = (Vector2D (x * m) (y * m))
    

    This uses a so-called functional dependency in the definition of Scalable. In fact, trying to remember the syntax for that, I found this link… So I guess you should disregard my inferior attempt at being helpful and read the quality info there. 😉

    I think you should be able to use this to solve your original problem.

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

Sidebar

Ask A Question

Stats

  • Questions 297k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Adobe has a beta product called Stratus which allows p2p… May 13, 2026 at 7:25 pm
  • Editorial Team
    Editorial Team added an answer You are missing some fundamentals of how SharePoint development and… May 13, 2026 at 7:25 pm
  • Editorial Team
    Editorial Team added an answer The web is "stateless" meaning you have no intrinsic way… May 13, 2026 at 7:25 pm

Related Questions

I am a somewhat experienced Rails developer and I thought I would try out
A blog I read has some annoying commenters. I thought I would try my
I asked this in the users group with no response so i thought I
As a follow up to the method that collapses overlapping ranges I thought I
I want to implement some various algorithms for practice, just to see how bad

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.