{-# LANGUAGE RebindableSyntax #-}
module Algebra.ZeroTestable where

import qualified Algebra.Additive as Additive

import Data.Int  (Int,  Int8,  Int16,  Int32,  Int64,  )
import Data.Word (Word, Word8, Word16, Word32, Word64, )

import Prelude (Integer, Float, Double, )
import NumericPrelude.Base

{- |
Maybe the naming should be according to Algebra.Unit:
Algebra.Zero as module name, and @query@ as method name.
-}
class C a where
   isZero :: a -> Bool

{- |
Checks if a number is the zero element.
This test is not possible for all 'Additive.C' types,
since e.g. a function type does not belong to Eq.
isZero is possible for some types where (==zero) fails
because there is no unique zero.
Examples are
vector (the length of the zero vector is unknown),
physical values (the unit of a zero quantity is unknown),
residue class (the modulus is unknown).
-}
defltIsZero :: (Eq a, Additive.C a) => a -> Bool
defltIsZero :: a -> Bool
defltIsZero = (a
forall a. C a => a
Additive.zeroa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==)


{-* Instances for atomic types -}

instance C Integer where isZero :: Integer -> Bool
isZero = Integer -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Float   where isZero :: Float -> Bool
isZero = Float -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Double  where isZero :: Double -> Bool
isZero = Double -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero

instance C Int     where isZero :: Int -> Bool
isZero = Int -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Int8    where isZero :: Int8 -> Bool
isZero = Int8 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Int16   where isZero :: Int16 -> Bool
isZero = Int16 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Int32   where isZero :: Int32 -> Bool
isZero = Int32 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Int64   where isZero :: Int64 -> Bool
isZero = Int64 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero

instance C Word    where isZero :: Word -> Bool
isZero = Word -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Word8   where isZero :: Word8 -> Bool
isZero = Word8 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Word16  where isZero :: Word16 -> Bool
isZero = Word16 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Word32  where isZero :: Word32 -> Bool
isZero = Word32 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero
instance C Word64  where isZero :: Word64 -> Bool
isZero = Word64 -> Bool
forall a. (Eq a, C a) => a -> Bool
defltIsZero



{-* Instances for composed types -}

instance (C v0, C v1) => C (v0, v1) where
    isZero :: (v0, v1) -> Bool
isZero (v0
x0,v1
x1) = v0 -> Bool
forall a. C a => a -> Bool
isZero v0
x0 Bool -> Bool -> Bool
&& v1 -> Bool
forall a. C a => a -> Bool
isZero v1
x1

instance (C v0, C v1, C v2) => C (v0, v1, v2) where
    isZero :: (v0, v1, v2) -> Bool
isZero (v0
x0,v1
x1,v2
x2) = v0 -> Bool
forall a. C a => a -> Bool
isZero v0
x0 Bool -> Bool -> Bool
&& v1 -> Bool
forall a. C a => a -> Bool
isZero v1
x1 Bool -> Bool -> Bool
&& v2 -> Bool
forall a. C a => a -> Bool
isZero v2
x2


instance (C v) => C [v] where
    isZero :: [v] -> Bool
isZero = (v -> Bool) -> [v] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all v -> Bool
forall a. C a => a -> Bool
isZero