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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:13:48+00:00 2026-06-02T06:13:48+00:00

I have a type data Value = Int Integer | Float Double | Complex

  • 0

I have a type

data Value = Int Integer
           | Float Double
           | Complex (Complex Double)
           | ... (other, non-numeric types)

with an associate error type

data ValueError = TypeMismatch Value | ... (other constructors)

type ThrowsError = Either ValueError

and I want to implement generic binary operations over the type, with automatic coercion to the highest type in the heirarchy, and error signalling in case one of the operands isn’t a numeric type, i.e. a function

binaryOp :: Num a => (a -> a -> a) -> Value -> Value -> ThrowsError Value 

so that I could write, for example,

(binaryOp (+)) (Int 1) (Int 1)      ==> Right (Int 2)
(binaryOp (+)) (Int 1) (Float 1.0)  ==> Right (Float 2.0)
(binaryOp (+)) (Int 1) (String "1") ==> Left (TypeMismatch (String "1"))

Is there a simple way to do this? My first thought was to define something like

data NumType = IntType | FloatType | ComplexType

along with functions

typeOf :: Value -> NumType
typeOf (Int _) = IntType
...

promote :: Value -> Value
promote (Int n)   = Float (fromInteger n)
promote (Float n) = Complex (n :+ 0)

but I’m having difficulty making it work. Any advice?


A bit more context. I’m writing a Scheme interpreter, and I want to implement the Scheme numeric tower.

In fact I want to achieve something slightly more complicated than what I explained, because I want something applicable to an arbitrary number of arguments, along the lines of

binaryOp :: Num a => (a -> a -> a) -> [Value] -> ThrowsError Value

which would be implemented with foldl1, but I feel that if I can solve the simpler problem then I will be able to solve this more complicated one.

  • 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-02T06:13:49+00:00Added an answer on June 2, 2026 at 6:13 am

    Something like this:

    data NumType = IntType | FloatType | ComplexType | NotANumType
      deriving (Eq, Ord)
    
    binaryOp :: (forall a. Num a => a -> a -> a) -> Number -> Number -> ThrowsError Number
    binaryOp op x y
       = case typeOf x `max` typeOf y of
              ComplexType -> Complex (asComplex x `op` asComplex y)
              ...
    

    I think you will need to enable the Rank2Types extension (insert {-# LANGUAGE Rank2Types #-} at the top of your source file) to properly state the type of binaryOp, and I’m not sure I’ve got the syntax right…

    The type of binaryOp is more complex than you thought because binaryOp chooses what a is when it calls op. What you wrote would have binaryOp‘s caller choosing what a is, which is not what you want.

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

Sidebar

Related Questions

I have a type class IntegerAsType a where value :: a -> Integer data
I have a universal data type, which is passed by value, but does not
From my experience I have learn that using an surrogate INT data type column
In my mssql tables i have a clomun that data type is integer. and
I am passing data value, to a database, using integer data type (accepting null
I have a sqlite table that has amount field with NUMERIC(10,5) value(I know data
I have this type of data: entity, account, month1, month2, ..., month12 abc, 2000.02,
I have a data type that contains a set and a method that expects
The table columns have the data type BLOB and CLOB. What are the corresponding
The task: I am trying to create a custom data type and have it

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.