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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:35:32+00:00 2026-05-30T08:35:32+00:00

Is there any way to make a class instance return a value which is

  • 0

Is there any way to make a class instance return a value which is not of the instance’s type? An example is wanting to return a value of type Double for the the scalar product of two vectors:

--  data structure to contain a 3D point in space
data Point3D = Point3D !Double !Double !Double
    deriving (Eq, Ord)

instance Num Point3D where
    -- Multiplication, scalar == Dot product
    Point3D x1 y1 z1 * Point3D x2 y2 z2 = x1*x2 + y1*y2 + z1*z2 :: Double

Furthermore, is there any way to define how operators work between functions of different types? For example, I would like to define Point3D x y z + Double a = Point3D (x + a) (y + a) (z + a)

  • 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-30T08:35:34+00:00Added an answer on May 30, 2026 at 8:35 am

    The numeric operations in the Num typeclass are all defined with the type :: Num n => n -> n -> n, so both operands and the return value must have the same type. There’s no way to alter an existing typeclass, so your options are either to define new operators or hide the existing Num class and replace it completely with your own implementation.

    In order to implement operators that can have different operand types, you are going to need a couple of language extensions.

    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE FunctionalDependencies #-}
    

    Instead of a Num-like class that encompasses +, - and *, it’s more flexible to define different typeclasses for different operands, because while Point3D * Double makes sense, Point3D + Double usually does not. Let’s start with Mul.

    class Mul a b c | a b -> c where
        (|*|) :: a -> b -> c
    

    Without extensions, typeclasses only ever contain a single type parameter, but with MultiParamTypeClasses, we can declare a typeclass like Mul for the combination of types a, b and c. The part after the parameters, | a b -> c is a “functional dependecy” which in this case states that the type c is dependent on a and b. This means that if we have an instance like Mul Double Point3D Point3D the functional dependency states that we can’t have any other instances Mul Double Point3D c, where c something other than Point3D, i.e. the return type of the multiplication is always unambiguously determined by the type of the operands.

    Here’s how we implement instances for Mul:

    instance Mul Double Double Double where
        (|*|) = (*)
    
    instance Mul Point3D Double Point3D where
        Point3D x y z |*| a = Point3D (x*a) (y*a) (z*a)
    
    instance Mul Double Point3D Point3D where
        a |*| Point3D x y z = Point3D (x*a) (y*a) (z*a)
    

    This flexibility does not come without its caveats, though, because it will make type inference a lot more difficult for the compiler. For example, you can’t simply write

    p = Point3D 1 2 3 |*| 5
    

    Because the literal 5 isn’t necessarily of type Double. It can be any Num n => n, and it’s entirely possible that someone declares new instances like Mul Point3D Int Int which behaves completely differently. So what this means is that we need to specify the types of numerical literals explicitly.

    p = Point3D 1 2 3 |*| (5 :: Double)
    

    Now, if instead of defining new operands we wish to override the default Num class from Prelude, we can do it like this

    import Prelude hiding (Num(..))
    import qualified Prelude as P
    
    class Mul a b c | a b -> c where
        (*) :: a -> b -> c
    
    instance Mul Double Double Double where
        (*) = (P.*)
    
    instance Mul Point3D Double Point3D where
        Point3D x y z * a = Point3D (x*a) (y*a) (z*a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to return a readonly instance of an object? public class
In Python is there any way to make a class, then make a second
is there any way to make IE6 understand double classes, say I have a
Is there any way to make Visual Studio 2008 Express store all the files
Is there any way to make Visual Studio word-wrap at 80 characters? I'm using
Is there any way to make a function (the ones I'm thinking of are
Is there any way to make Visual Studio show the code of a control
Is there any way to make sure that a table and cells it contains
Is there any way to make some sections of the web.config file only apply
Is there any way to make private variables (those defined in the constructor), available

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.