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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:28:14+00:00 2026-06-05T10:28:14+00:00

Note: I may not need to add Scalar to Eq, although it should solve

  • 0

Note: I may not need to add Scalar to Eq, although it should solve the problem if I could figure out how to do just that.

So I’m trying to add some functionality to the ForceLayout module. Adding mass to Particles like so:

data Particle v = Particle { 
                         _pos   :: Point v
                       , _vel   :: v
                       , _force :: v
                       , _mass :: Scalar v
                       }
    deriving (Eq, Show)

But Scalar is not in Eq or Show! So this wont compile. Mass should be a scalar “compatible” with the other vectors though. How can I reconcile this? I don’t understand type families sufficiently to analyse this situation. I’ve tried but they are mighty hard to grasp. Not sure if adding Scalar to Eq is necessary or possible.

  • 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-05T10:28:16+00:00Added an answer on June 5, 2026 at 10:28 am

    If you want to show the mass field, the show instance is going to have to look like

    instance (Show v, Show (Point v), Show (Scalar v)) => Show (Particle v) where
    

    Arguably, the fact that GHC doesn’t work this out is a bug, or at least a missing feature. Luckily, with some extension heavy lifting, we can give it the context ourselves:

    {-# LANGUAGE StandaloneDeriving, TypeFamilies,
        FlexibleContexts, UndecidableInstances #-}
    {- ... -}
    
    deriving instance (Show v, Show (Point v), Show (Scalar v)) => Show (Particle v)
    

    This is a “standalone deriving declaration” as described in the GHC user guide, and its purpose here is basically so that we can specify a context that GHC wouldn’t normally be able to work out.

    That ought to do the trick. I’m a little worried about the UndecidableInstances, since it does seem like it ought to be possible to define instances of Point and Scalar such that this instance is circular, but despite my best efforts I haven’t been able to make the typechecker loop, so it’s probably fine.

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

Sidebar

Related Questions

(note that this question is not about CAS, it's about the May fail spuriously
I need to add a search feature to a website. Not being adept at
I've just moved a forum from BradPPresents.com to BradP.com. May not be SFW. It's
Note: there may be some typos (capitals etc) as my wireless card has broken
Please note: I am new to TDD & cucumber, so the answer may be
Note: Already asked in this post (asked on StackOverflow) . However, the problem is
NOTE: I am not a native English speaker. Please excuse any grammar mistakes. I
Note: A closure cannot call any user-defined function, because the function's context is not
Note: Before you spend your time reading on, please know that C2DM is itself
Note: I inherited this site, I did not build it. The home page is

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.