5 ms·
Curious: I don't see the `^` op defined, or is it translated inti `exp` im guessing?
by green_on_black 4y ago
Curious: I don't see the `^` op defined, or is it translated inti `exp` im guessing?
- ayjtyjtyjgfjc 4y agoIt is one of three exponentiation operators in standard Haskell, no translation involved. https://stackoverflow.com/a/6400630/14768587 https://stackoverflow.com/a/6400630/14768587
- ttesmer 4y agoSince the type was made an instance of the Num typeclass, any function that can be used with Num's, can now be used on the type (Dual d). As per the Prelude[1], ^ is part of the Num typeclass. Same thing for * for Floating[2]. The hyperbolic tangent can also be used without being explicitly coded, as it can be derived using cosh and sinh! EDIT: As for the differentiation, it works for ^ since it is just multiplication (https://hackage.haskell.org/package/base-4.17.0.0/docs/src/GHC.Real.html#%5E https://hackage.haskell.org/package/base-4.17.0.0/docs/src/G...) for which the derivative was defined using the product rule. [1]: https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html#v:-94- https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelu... [2]: https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html#v:-42--42- https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelu...
- tikhonj 4y agoThe ^ operator is defined in Haskell's standard library for raising values of any numeric type to non-negative, integral powers. Conceptually a ^ n just expands to a * a * ... * a, but the actual code is a bit more complex[1] for performance reasons. The neat thing with this approach is that ^ works for any numeric type, including user-defined types like Dual in this example. Since the Dual type can handle calculating derivatives for *, it gets derivatives for ^ for free. [1]: https://hackage.haskell.org/package/base-4.17.0.0/docs/src/GHC.Real.html#%5E https://hackage.haskell.org/package/base-4.17.0.0/docs/src/G...
- green_on_black 4y agoAh, `^` being naturals-only makes more sense in terms of how it could work with no special logic!
- lalaithion 4y agoHaskell has three exponentiation operators: (^) :: (Num a, Integral b) => a -> b -> a (^^) :: (Fractional a, Integral b) => a -> b -> a (**) :: Floating a => a -> a -> a The first one has the un-typed requirement that `b` be nonnegative, and basically allows any value with multiplication (implements the Num typeclass) to be raised to a natural power. The second one allows any value with multiplication and multiplicative inverses (Fractional) to be raised to an integral power. The third one allows any value which supports exponents, trig functions, and logarithms to be raised to a power with the same type. `*` is in fact one of the functions one must implement to implement the Floating typeclass.
- cryptonector 4y agoWhat Haskell calls a "class" is what Java calls an "interface". What Haskell calls an "instance" of an interface is what Java would call a class implementing that interface. `Num`, then, is a Haskell class (Java interface). The `Num` class will have a bunch of what Java would call "default methods". Now, the "instance" of `Num` defined here has only a few methods defined, but the other default methods of `Num` will use those. So if `Num` has a `^` defined in terms of ``, and you define an instance of `Num` that defines ``, then you get `^` for free if you don't implement it.