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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:24:08+00:00 2026-06-01T05:24:08+00:00

I am trying to define a function which would take a Double -> Double

  • 0

I am trying to define a function which would take a Double -> Double function and return its mathematical derivative. I have tried doing the following:

der :: (Double -> Double) -> (Double -> Double)
der f
    | f == exp = exp
    | otherwise = undefined

but Haskell does not support == on Double -> Double values. Is what I am trying to do impossible in Haskell?

  • 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-01T05:24:09+00:00Added an answer on June 1, 2026 at 5:24 am

    Yes, what you are trying to do is impossible in Haskell, and in general: deciding whether two functions are equal for all possible inputs (without just checking every input value, if that is even possible) is equivalent to solving the Halting problem.

    However, in your specific case, you can get around it, using a custom type that simulates a Double (i.e. has the same instances, and so can be used in place of it) but instead of evaluating to a number, it constructs an abstract representation of the operations the functions does. Expr represents the right-hand side of a mathematical function definition f(x) = ....

    data Expr = X | Const Double |
                Add Expr Expr | Mult Expr Expr |
                Negate Expr | Inverse Expr |
                Exp Expr | Log Expr | Sin Expr | ...
           deriving (Show, Eq)
    
    instance Num Expr where
        (+) = Add
        (*) = Mult
        ...
    instance Fractional Expr where
        recip = Inverse
        ...
    instance Floating Expr where
        pi = Const pi
        exp = Exp
        log = Log
        sin = Sin
        ...
    

    Then, using rank-2 types, you can define conversion functions that convert between functions that take any Floating and Exprs:

    {-# LANGUAGE Rank2Types #-}
    
    fromFunction :: (forall a. Floating a => (a -> a)) -> Expr
    fromFunction f = f X
    
    toFunction :: Expr -> (Double -> Double)
    toFunction X = \x -> x
    toFunction (Const a) = const a
    toFunction (Add a b) = \x -> (toFunction a x) + (toFunction b x)
    ...
    

    You can also define a function diff :: Expr -> Expr that differentiates the expression:

    diff X = Const 1
    diff (Const _) = Const 0
    diff (Add a b) = Add (diff a) (diff b)
    diff (Exp a) = Mult (diff a) (Exp a)
    ...
    

    Having all these parts should mean that you can differentiate (some) functions, e.g.

    f x = sin x + cos x * exp x
    f' = toFunction . diff . fromFunction $ f
    

    Caveats:

    • this won’t work in general,
    • defining a complete Eq instance for Expr is tricky (it is equivalent to the Halting problem, since it is basically asking if two functions are equal),
    • I haven’t actually tested any of this code,
    • the differentiation and reconstruction are done at runtime, so the resulting function is highly likely to be very slow.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling with PL/SQL functions. I'm trying to write a function which would take
when trying to define a function that would remove the largest subset of set
I am trying to define a function that would to that: var photoWidth =
I'm trying to define a delegate function that will return an IEnumerable. I'm having
I am trying to define a small function, ZipDistance, in VBA, which calculates a
I am trying to define a JQuery statement for finding elements which have a
I would like to define a nullary static template member function which would be
I'm trying to define a function using template template parameters (I just want to
I'm trying to define a user function in vim to change the current color
I am trying to define a generic function to give the largest value of

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.