{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wall #-}

-- | Field classes
module NumHask.Algebra.Field
  ( Field,
    ExpField (..),
    QuotientField (..),
    infinity,
    negInfinity,
    nan,
    TrigField (..),
    half,
  )
where

import Data.Bool (bool)
import NumHask.Algebra.Additive (Additive (..), Subtractive (..), (-))
import NumHask.Algebra.Multiplicative
  ( Divisive (..),
    Multiplicative (..),
    (/),
  )
import NumHask.Algebra.Ring (Distributive, two)
import NumHask.Data.Integral (Integral, even)
import Prelude ((.))
import qualified Prelude as P

-- $setup
--
-- >>> :set -XRebindableSyntax
-- >>> :set -XScopedTypeVariables
-- >>> import NumHask.Prelude

-- | A <https://en.wikipedia.org/wiki/Field_(mathematics) Field> is a set
--   on which addition, subtraction, multiplication, and division are defined. It is also assumed that multiplication is distributive over addition.
--
-- A summary of the rules inherited from super-classes of Field:
--
-- > zero + a == a
-- > a + zero == a
-- > ((a + b) + c) (a + (b + c))
-- > a + b == b + a
-- > a - a == zero
-- > negate a == zero - a
-- > negate a + a == zero
-- > a + negate a == zero
-- > one * a == a
-- > a * one == a
-- > ((a * b) * c) == (a * (b * c))
-- > (a * (b + c)) == (a * b + a * c)
-- > ((a + b) * c) == (a * c + b * c)
-- > a * zero == zero
-- > zero * a == zero
-- > a / a == one || a == zero
-- > recip a == one / a || a == zero
-- > recip a * a == one || a == zero
-- > a * recip a == one || a == zero
class
  (Distributive a, Subtractive a, Divisive a) =>
  Field a

instance Field P.Double

instance Field P.Float

instance (Field b) => Field (a -> b)

-- | A hyperbolic field class
--
-- prop> \a -> a < zero || (sqrt . (**2)) a == a
-- prop> \a -> a < zero || (log . exp) a ~= a
-- prop> \a b -> (b < zero) || a <= zero || a == 1 || abs (a ** logBase a b - b) < 10 * epsilon
class
  (Field a) =>
  ExpField a
  where
  exp :: a -> a
  log :: a -> a
  (**) :: a -> a -> a
  (**) a
a a
b = forall a. ExpField a => a -> a
exp (forall a. ExpField a => a -> a
log a
a forall a. Multiplicative a => a -> a -> a
* a
b)

  -- | log to the base of
  --
  -- >>> logBase 2 8
  -- 2.9999999999999996
  logBase :: a -> a -> a
  logBase a
a a
b = forall a. ExpField a => a -> a
log a
b forall a. Divisive a => a -> a -> a
/ forall a. ExpField a => a -> a
log a
a

  -- | square root
  --
  -- >>> sqrt 4
  -- 2.0
  sqrt :: a -> a
  sqrt a
a = a
a forall a. ExpField a => a -> a -> a
** (forall a. Multiplicative a => a
one forall a. Divisive a => a -> a -> a
/ (forall a. Multiplicative a => a
one forall a. Additive a => a -> a -> a
+ forall a. Multiplicative a => a
one))

instance ExpField P.Double where
  exp :: Double -> Double
exp = forall a. Floating a => a -> a
P.exp
  log :: Double -> Double
log = forall a. Floating a => a -> a
P.log
  ** :: Double -> Double -> Double
(**) = forall a. Floating a => a -> a -> a
(P.**)

instance ExpField P.Float where
  exp :: Float -> Float
exp = forall a. Floating a => a -> a
P.exp
  log :: Float -> Float
log = forall a. Floating a => a -> a
P.log
  ** :: Float -> Float -> Float
(**) = forall a. Floating a => a -> a -> a
(P.**)

instance (ExpField b) => ExpField (a -> b) where
  exp :: (a -> b) -> a -> b
exp a -> b
f = forall a. ExpField a => a -> a
exp forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  log :: (a -> b) -> a -> b
log a -> b
f = forall a. ExpField a => a -> a
log forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f

-- | Conversion from a 'Field' to a 'NumHask.Algebra.Ring'
--
-- See [Field of fractions](https://en.wikipedia.org/wiki/Field_of_fractions)
--
-- > \a -> a - one < floor a <= a <= ceiling a < a + one
-- prop> (\a -> a - one < fromIntegral (floor a :: Int) && fromIntegral (floor a :: Int) <= a && a <= fromIntegral (ceiling a :: Int) && fromIntegral (ceiling a :: Int) <= a + one) :: Double -> Bool
-- prop> \a -> (round a :: Int) ~= (floor (a + half) :: Int)
class (Field a, Multiplicative b, Additive b) => QuotientField a b where
  properFraction :: a -> (b, a)

  -- | round to the nearest integral
  --
  -- Exact ties are managed by rounding down ties if the whole component is even.
  --
  -- >>> round (1.5 :: Double) :: Int
  -- 2
  --
  -- >>> round (2.5 :: Double) :: Int
  -- 2
  round :: a -> b
  default round :: (P.Ord a, P.Ord b, Subtractive b, Integral b) => a -> b
  round a
x = case forall a b. QuotientField a b => a -> (b, a)
properFraction a
x of
    (b
n, a
r) ->
      let m :: b
m = forall a. a -> a -> Bool -> a
bool (b
n forall a. Additive a => a -> a -> a
+ forall a. Multiplicative a => a
one) (b
n forall a. Subtractive a => a -> a -> a
- forall a. Multiplicative a => a
one) (a
r forall a. Ord a => a -> a -> Bool
P.< forall a. Additive a => a
zero)
          half_down :: a
half_down = forall {a}. (Ord a, Subtractive a) => a -> a
abs' a
r forall a. Subtractive a => a -> a -> a
- (forall a. Multiplicative a => a
one forall a. Divisive a => a -> a -> a
/ (forall a. Multiplicative a => a
one forall a. Additive a => a -> a -> a
+ forall a. Multiplicative a => a
one))
          abs' :: a -> a
abs' a
a
            | a
a forall a. Ord a => a -> a -> Bool
P.< forall a. Additive a => a
zero = forall a. Subtractive a => a -> a
negate a
a
            | Bool
P.otherwise = a
a
       in case forall a. Ord a => a -> a -> Ordering
P.compare a
half_down forall a. Additive a => a
zero of
            Ordering
P.LT -> b
n
            Ordering
P.EQ -> forall a. a -> a -> Bool -> a
bool b
m b
n (forall a. (Eq a, Integral a) => a -> Bool
even b
n)
            Ordering
P.GT -> b
m

  -- | supply the next upper whole component
  --
  -- >>> ceiling (1.001 :: Double) :: Int
  -- 2
  ceiling :: a -> b
  default ceiling :: (P.Ord a) => a -> b
  ceiling a
x = forall a. a -> a -> Bool -> a
bool b
n (b
n forall a. Additive a => a -> a -> a
+ forall a. Multiplicative a => a
one) (a
r forall a. Ord a => a -> a -> Bool
P.>= forall a. Additive a => a
zero)
    where
      (b
n, a
r) = forall a b. QuotientField a b => a -> (b, a)
properFraction a
x

  -- | supply the previous lower whole component
  --
  -- >>> floor (1.001 :: Double) :: Int
  -- 1
  floor :: a -> b
  default floor :: (P.Ord a, Subtractive b) => a -> b
  floor a
x = forall a. a -> a -> Bool -> a
bool b
n (b
n forall a. Subtractive a => a -> a -> a
- forall a. Multiplicative a => a
one) (a
r forall a. Ord a => a -> a -> Bool
P.< forall a. Additive a => a
zero)
    where
      (b
n, a
r) = forall a b. QuotientField a b => a -> (b, a)
properFraction a
x

  -- | supply the whole component closest to zero
  --
  -- >>> floor (-1.001 :: Double) :: Int
  -- -2
  --
  -- >>> truncate (-1.001 :: Double) :: Int
  -- -1
  truncate :: a -> b
  default truncate :: (P.Ord a) => a -> b
  truncate a
x = forall a. a -> a -> Bool -> a
bool (forall a b. QuotientField a b => a -> b
ceiling a
x) (forall a b. QuotientField a b => a -> b
floor a
x) (a
x forall a. Ord a => a -> a -> Bool
P.> forall a. Additive a => a
zero)

instance QuotientField P.Float P.Integer where
  properFraction :: Float -> (Integer, Float)
properFraction = forall a b. (RealFrac a, Integral b) => a -> (b, a)
P.properFraction

instance QuotientField P.Double P.Integer where
  properFraction :: Double -> (Integer, Double)
properFraction = forall a b. (RealFrac a, Integral b) => a -> (b, a)
P.properFraction

instance QuotientField P.Float P.Int where
  properFraction :: Float -> (Int, Float)
properFraction = forall a b. (RealFrac a, Integral b) => a -> (b, a)
P.properFraction

instance QuotientField P.Double P.Int where
  properFraction :: Double -> (Int, Double)
properFraction = forall a b. (RealFrac a, Integral b) => a -> (b, a)
P.properFraction

instance (QuotientField b c) => QuotientField (a -> b) (a -> c) where
  properFraction :: (a -> b) -> (a -> c, a -> b)
properFraction a -> b
f = (forall a b. (a, b) -> a
P.fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> (c, b)
frac, forall a b. (a, b) -> b
P.snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> (c, b)
frac)
    where
      frac :: a -> (c, b)
frac a
a = forall a b. QuotientField a b => a -> (b, a)
properFraction @b @c (a -> b
f a
a)
  round :: (a -> b) -> a -> c
round a -> b
f = forall a b. QuotientField a b => a -> b
round forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  ceiling :: (a -> b) -> a -> c
ceiling a -> b
f = forall a b. QuotientField a b => a -> b
ceiling forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  floor :: (a -> b) -> a -> c
floor a -> b
f = forall a b. QuotientField a b => a -> b
floor forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  truncate :: (a -> b) -> a -> c
truncate a -> b
f = forall a b. QuotientField a b => a -> b
truncate forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f

-- | infinity is defined for any 'Field'.
--
-- >>> one / zero + infinity
-- Infinity
--
-- >>> infinity + 1
-- Infinity
infinity :: (Field a) => a
infinity :: forall a. Field a => a
infinity = forall a. Multiplicative a => a
one forall a. Divisive a => a -> a -> a
/ forall a. Additive a => a
zero

-- | nan is defined as zero/zero
--
-- but note the (social) law:
--
-- >>> nan == zero / zero
-- False
nan :: (Field a) => a
nan :: forall a. Field a => a
nan = forall a. Additive a => a
zero forall a. Divisive a => a -> a -> a
/ forall a. Additive a => a
zero

-- | negative infinity
--
-- >>> negInfinity + infinity
-- NaN
negInfinity :: (Field a) => a
negInfinity :: forall a. Field a => a
negInfinity = forall a. Subtractive a => a -> a
negate forall a. Field a => a
infinity

-- | Trigonometric Field
--
-- The list of laws is quite long: <https://en.wikipedia.org/wiki/List_of_trigonometric_identities trigonometric identities>
class
  (Field a) =>
  TrigField a
  where
  pi :: a
  sin :: a -> a
  cos :: a -> a
  tan :: a -> a
  tan a
x = forall a. TrigField a => a -> a
sin a
x forall a. Divisive a => a -> a -> a
/ forall a. TrigField a => a -> a
cos a
x
  asin :: a -> a
  acos :: a -> a
  atan :: a -> a
  atan2 :: a -> a -> a
  sinh :: a -> a
  cosh :: a -> a
  tanh :: a -> a
  tanh a
x = forall a. TrigField a => a -> a
sinh a
x forall a. Divisive a => a -> a -> a
/ forall a. TrigField a => a -> a
cosh a
x
  asinh :: a -> a
  acosh :: a -> a
  atanh :: a -> a

instance TrigField P.Double where
  pi :: Double
pi = forall a. Floating a => a
P.pi
  sin :: Double -> Double
sin = forall a. Floating a => a -> a
P.sin
  cos :: Double -> Double
cos = forall a. Floating a => a -> a
P.cos
  asin :: Double -> Double
asin = forall a. Floating a => a -> a
P.asin
  acos :: Double -> Double
acos = forall a. Floating a => a -> a
P.acos
  atan :: Double -> Double
atan = forall a. Floating a => a -> a
P.atan
  atan2 :: Double -> Double -> Double
atan2 = forall a. RealFloat a => a -> a -> a
P.atan2
  sinh :: Double -> Double
sinh = forall a. Floating a => a -> a
P.sinh
  cosh :: Double -> Double
cosh = forall a. Floating a => a -> a
P.cosh
  asinh :: Double -> Double
asinh = forall a. Floating a => a -> a
P.sinh
  acosh :: Double -> Double
acosh = forall a. Floating a => a -> a
P.acosh
  atanh :: Double -> Double
atanh = forall a. Floating a => a -> a
P.atanh

instance TrigField P.Float where
  pi :: Float
pi = forall a. Floating a => a
P.pi
  sin :: Float -> Float
sin = forall a. Floating a => a -> a
P.sin
  cos :: Float -> Float
cos = forall a. Floating a => a -> a
P.cos
  asin :: Float -> Float
asin = forall a. Floating a => a -> a
P.asin
  acos :: Float -> Float
acos = forall a. Floating a => a -> a
P.acos
  atan :: Float -> Float
atan = forall a. Floating a => a -> a
P.atan
  atan2 :: Float -> Float -> Float
atan2 = forall a. RealFloat a => a -> a -> a
P.atan2
  sinh :: Float -> Float
sinh = forall a. Floating a => a -> a
P.sinh
  cosh :: Float -> Float
cosh = forall a. Floating a => a -> a
P.cosh
  asinh :: Float -> Float
asinh = forall a. Floating a => a -> a
P.sinh
  acosh :: Float -> Float
acosh = forall a. Floating a => a -> a
P.acosh
  atanh :: Float -> Float
atanh = forall a. Floating a => a -> a
P.atanh

instance (TrigField b) => TrigField (a -> b) where
  pi :: a -> b
pi a
_ = forall a. TrigField a => a
pi
  sin :: (a -> b) -> a -> b
sin a -> b
f = forall a. TrigField a => a -> a
sin forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  cos :: (a -> b) -> a -> b
cos a -> b
f = forall a. TrigField a => a -> a
cos forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  asin :: (a -> b) -> a -> b
asin a -> b
f = forall a. TrigField a => a -> a
asin forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  acos :: (a -> b) -> a -> b
acos a -> b
f = forall a. TrigField a => a -> a
acos forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  atan :: (a -> b) -> a -> b
atan a -> b
f = forall a. TrigField a => a -> a
atan forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  atan2 :: (a -> b) -> (a -> b) -> a -> b
atan2 a -> b
f a -> b
g a
x = forall a. TrigField a => a -> a -> a
atan2 (a -> b
f a
x) (a -> b
g a
x)
  sinh :: (a -> b) -> a -> b
sinh a -> b
f = forall a. TrigField a => a -> a
sinh forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  cosh :: (a -> b) -> a -> b
cosh a -> b
f = forall a. TrigField a => a -> a
cosh forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  asinh :: (a -> b) -> a -> b
asinh a -> b
f = forall a. TrigField a => a -> a
asinh forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  acosh :: (a -> b) -> a -> b
acosh a -> b
f = forall a. TrigField a => a -> a
acosh forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  atanh :: (a -> b) -> a -> b
atanh a -> b
f = forall a. TrigField a => a -> a
atanh forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f

-- | A 'half' is a 'Field' because it requires addition, multiplication and division.
--
-- >>> half :: Double
-- 0.5
half :: (Field a) => a
half :: forall a. Field a => a
half = forall a. Multiplicative a => a
one forall a. Divisive a => a -> a -> a
/ forall a. (Multiplicative a, Additive a) => a
two