haskellghc# Convert Num to Double in Haskell

I know about the existence of `fromRational`

, `fromInteger`

and `fromIntegral`

.

But is there a more generic function to convert any `Num`

to the `Double`

type?

I'm also aware of the fact that `Num`

is a set of types, and possibly that comes with limitations of how to use it.

But it is possible?

Something like:

```
fromNum::(Num a) => a -> Double
```

And preferably, without installing any additional packages.

Solution

There is absolutely not a way to do this, and for a very good reason. `Num`

is a generic type designed to be used for anything vaguely "addable" and "multiply-able". You can *very loosely* think of it as a ring^{[1]}. This means that things like vectors, points, and matrices are `Num`

. And there's no reasonable, canonical way to convert a `Matrix a`

into a `Double`

.

You've already noted that there are several ways to get to `Double`

from more specific typeclasses like `Integral`

.

^{[1]} Yes, `fromInteger`

is also on `Num`

. That's largely considered a misfeature and libraries doing things with matrices usually just provide an awkward default implementation.

- Using pattern synonyms to abstract implementation of text type
- How to avoid listing A as build dependency for internal library/executable E just because E depends on internal library L which depends on A?
- How is the Foldable instance of (,) useful?
- What is the proper way of wrapping an Int (not a general type) in another type if type safety is the only motive?
- What is the most practical way to express a dependency on a library for which we have a local git repository with some changes?
- Htmx POST to haskell servant handling optional field in FormUrlEncoded request
- Haskell fails to infer the return type of a monad after using the sequence operator
- Does extracting values from a multiple Value return in Haskell invoke the function more than once?
- How to specify c/c++ compiler on stack install command
- Why do I get "Unexpected reply type" from notify-send when using this Haskell notification server?
- Don't understand notation of morphisms in Monoid definition
- Foldln in haskell
- Is this property of a functor stronger than a monad?
- How to Instantiate a Custom Data Type with Record Syntax and Multiple Constructors
- How do I make a minimal working example for the a DBus server?
- Is it safe to downgrade Haskell stack version?
- Haskell, list of natural number
- unfamiliar syntax in Haskell / Clash function type signature
- foldM with monad State does not type check
- Why does my Runge-Kutta implementation oscillate to 0?
- How do I get the desired behavior in my TCP server?
- Why does the Haskell PVP describe new functions as non-breaking?
- How do I correctly use toLower in Haskell?
- How can I write a notification server in Haskell?
- Every Lens' is a Traversal'... how?
- How do I crate a value of type a{sv} for a call to org.freedesktop.Notifications.Notify via DBus?
- Web Scraping With Haskell
- Double exclamation marks in Haskell
- Haskell Servant POST FormUrlEncoded for (Vector String) field
- Confusion about list types in Haskell