{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GADTs, StandaloneDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
module OAlg.Structure.Number.Definition
(
Number(..), zFloor
, toDigits, Digits(..)
, toDigitsFinite, fromDigits
, dgsBase, dgsProxy, dgsFrcTake
, Integral(..), primes
, Acyclic(..)
, Fractional
, Measurable(..)
)
where
import qualified Prelude as A
import Data.Proxy
import OAlg.Prelude
import OAlg.Data.TypeLits
import OAlg.Data.Canonical
import OAlg.Structure.Oriented.Definition
import OAlg.Structure.Multiplicative.Definition
import OAlg.Structure.Additive.Definition
import OAlg.Structure.Distributive.Definition
import OAlg.Structure.Ring.Definition
class (Semiring r, Commutative r, Ord r) => Number r where
{-# MINIMAL minusOne, zFloorFraction #-}
minusOne :: Maybe r
signum :: r -> r
signum r
x = case forall r. Semiring r => r
rZero forall a. Ord a => a -> a -> Ordering
`compare` r
x of
Ordering
GT -> forall r. Semiring r => r
rOne
Ordering
EQ -> forall r. Semiring r => r
rZero
Ordering
LT -> r
e where Just r
e = forall r. Number r => Maybe r
minusOne
abs :: r -> r
abs r
x = forall r. Number r => r -> r
signum r
x forall c. Multiplicative c => c -> c -> c
* r
x
floor :: r -> r
floor r
x = forall a. Additive a => N -> a -> a
ntimes (forall a b. Projectible a b => b -> a
prj forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a b. (a, b) -> a
fst forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall r. Number r => r -> (Z, r)
zFloorFraction forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ r
x) (forall r. Number r => r -> r
signum r
x)
fraction :: r -> r
fraction = forall a b. (a, b) -> b
snd forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall r. Number r => r -> (Z, r)
zFloorFraction
zFloorFraction :: r -> (Z,r)
zFloor :: Number r => r -> Z
zFloor :: forall r. Number r => r -> Z
zFloor = forall a b. (a, b) -> a
fstforall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall r. Number r => r -> (Z, r)
zFloorFraction
instance Number N where
minusOne :: Maybe N
minusOne = forall a. Maybe a
Nothing
abs :: N -> N
abs N
n = N
n
signum :: N -> N
signum N
n = if N
n forall a. Eq a => a -> a -> Bool
== N
0 then N
0 else N
1
floor :: N -> N
floor N
n = N
n
fraction :: N -> N
fraction N
_ = N
0
zFloorFraction :: N -> (Z, N)
zFloorFraction N
n = (forall a b. Embeddable a b => a -> b
inj N
n, N
0)
instance Number Integer where
minusOne :: Maybe Integer
minusOne = forall a. a -> Maybe a
Just (-Integer
1)
abs :: Integer -> Integer
abs = forall a. Num a => a -> a
A.abs
signum :: Integer -> Integer
signum = forall a. Num a => a -> a
A.signum
floor :: Integer -> Integer
floor Integer
z = Integer
z
fraction :: Integer -> Integer
fraction Integer
_ = Integer
0
zFloorFraction :: Integer -> (Z, Integer)
zFloorFraction Integer
z = (forall a b. Embeddable a b => a -> b
inj Integer
z,Integer
0)
instance Number Z where
minusOne :: Maybe Z
minusOne = forall a. a -> Maybe a
Just (-Z
1)
abs :: Z -> Z
abs = forall a. Num a => a -> a
A.abs
signum :: Z -> Z
signum = forall a. Num a => a -> a
A.signum
floor :: Z -> Z
floor Z
z = Z
z
fraction :: Z -> Z
fraction Z
_ = Z
0
zFloorFraction :: Z -> (Z, Z)
zFloorFraction Z
z = (Z
z,Z
0)
instance Number Q where
minusOne :: Maybe Q
minusOne = forall a. a -> Maybe a
Just (-Q
1)
abs :: Q -> Q
abs = forall a. Num a => a -> a
A.abs
signum :: Q -> Q
signum = forall a. Num a => a -> a
A.signum
floor :: Q -> Q
floor Q
q = forall a b. (RealFrac a, Integral b) => a -> b
A.floor Q
q Z -> N -> Q
% N
1
zFloorFraction :: Q -> (Z, Q)
zFloorFraction = forall a b. (RealFrac a, Integral b) => a -> (b, a)
A.properFraction
class Number a => Integral a where
{-# MINIMAL divMod #-}
divMod :: a -> a -> (a,a)
div :: a -> a -> a
div a
a a
b = forall a b. (a, b) -> a
fst (forall a. Integral a => a -> a -> (a, a)
divMod a
a a
b)
mod :: a -> a -> a
mod a
a a
b = forall a b. (a, b) -> b
snd (forall a. Integral a => a -> a -> (a, a)
divMod a
a a
b)
instance Integral N where
divMod :: N -> N -> (N, N)
divMod = forall a. Integral a => a -> a -> (a, a)
A.divMod
div :: N -> N -> N
div = forall a. Integral a => a -> a -> a
A.div
mod :: N -> N -> N
mod = forall a. Integral a => a -> a -> a
A.mod
instance Integral Z where
divMod :: Z -> Z -> (Z, Z)
divMod = forall a. Integral a => a -> a -> (a, a)
A.divMod
div :: Z -> Z -> Z
div = forall a. Integral a => a -> a -> a
A.div
mod :: Z -> Z -> Z
mod = forall a. Integral a => a -> a -> a
A.mod
primes :: [N]
primes :: [N]
primes = forall {a}. (Integral a, Num a) => [a] -> [a]
filterPrime [N
2..]
where filterPrime :: [a] -> [a]
filterPrime (a
p:[a]
xs) = a
p forall a. a -> [a] -> [a]
: [a] -> [a]
filterPrime [a
x | a
x <- [a]
xs, a
x forall a. Integral a => a -> a -> a
`mod` a
p forall a. Eq a => a -> a -> Bool
/= a
0]
class (Distributive a, Abelian a, Invertible a) => Acyclic a where
qtimes :: Q -> a -> a
qtimes Q
q a
a = forall a. Abelian a => Z -> a -> a
ztimes Z
z a
a forall c. Multiplicative c => c -> c -> c
* forall c. Invertible c => c -> c
invert (forall a. Additive a => N -> a -> a
ntimes N
n (forall c. Multiplicative c => Point c -> c
one (forall q. Oriented q => q -> Point q
start a
a)))
where z :: Z
z = Q -> Z
numerator Q
q
n :: N
n = Q -> N
denominator Q
q
instance Acyclic () where
qtimes :: Q -> () -> ()
qtimes Q
_ ()
_ = ()
instance Acyclic Q where
qtimes :: Q -> Q -> Q
qtimes = forall c. Multiplicative c => c -> c -> c
(*)
type Fractional r = (Number r, Abelian r, Acyclic r)
class (Entity a, Number r) => Measurable a r where
dist :: a -> a -> r
instance (Number r, Abelian r) => Measurable r r where
dist :: r -> r -> r
dist r
a r
b = forall r. Number r => r -> r
abs (r
b forall a. Abelian a => a -> a -> a
- r
a)
instance Measurable N Z where
dist :: N -> N -> Z
dist N
a N
b = forall a b. Embeddable a b => a -> b
inj forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a. Num a => a -> a
A.abs forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ (N
b forall a. Num a => a -> a -> a
A.- N
a)
data Digits (b :: Nat) r where
Digits :: 2 <= b
=> { forall (b :: Natural) r. Digits b r -> r
dgsSig :: r
, forall (b :: Natural) r. Digits b r -> [N]
dgsFlr :: [N]
, forall (b :: Natural) r. Digits b r -> [N]
dgsFrc :: [N]
}
-> Digits b r
deriving instance Show r => Show (Digits b r)
deriving instance Eq r => Eq (Digits b r)
deriving instance Ord r => Ord (Digits b r)
instance Validable r => Validable (Digits b r) where
valid :: Digits b r -> Statement
valid (Digits r
r [N]
n [N]
m) = [Statement] -> Statement
And [forall a. Validable a => a -> Statement
valid r
r,forall a. Validable a => a -> Statement
valid [N]
n,forall a. Validable a => a -> Statement
valid [N]
m]
instance (KnownNat b, Entity r) => Entity (Digits b r)
dgsProxy :: Digits b r -> Proxy b
dgsProxy :: forall (b :: Natural) r. Digits b r -> Proxy b
dgsProxy Digits b r
_ = forall {k} (t :: k). Proxy t
Proxy
dgsBase :: KnownNat b => Digits b r -> N
dgsBase :: forall (b :: Natural) r. KnownNat b => Digits b r -> N
dgsBase = forall a b. Projectible a b => b -> a
prj forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
natVal forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall (b :: Natural) r. Digits b r -> Proxy b
dgsProxy
dgsFrcTake :: N -> Digits b r -> Digits b r
dgsFrcTake :: forall (b :: Natural) r. N -> Digits b r -> Digits b r
dgsFrcTake N
n (Digits r
r [N]
flr [N]
frc) = forall (b :: Natural) r. (2 <= b) => r -> [N] -> [N] -> Digits b r
Digits r
r [N]
flr (forall a. N -> [a] -> [a]
takeN N
n forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ [N]
frc)
toDigits :: (Number r, KnownNat b, 2 <= b) => r -> Digits b r
toDigits :: forall r (b :: Natural).
(Number r, KnownNat b, 2 <= b) =>
r -> Digits b r
toDigits r
x = Digits b r
res
where res :: Digits b r
res = forall (b :: Natural) r. (2 <= b) => r -> [N] -> [N] -> Digits b r
Digits r
sig (Z -> [N] -> [N]
dgtsFlr Z
z []) (r -> [N]
dgtsFrc r
f)
base :: N
base = forall (b :: Natural) r. KnownNat b => Digits b r -> N
dgsBase Digits b r
res
sig :: r
sig = forall r. Number r => r -> r
signum r
x
(Z
z,r
f) = forall r. Number r => r -> (Z, r)
zFloorFraction forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall r. Number r => r -> r
abs r
x
zbs :: Z
zbs = forall a b. Embeddable a b => a -> b
inj N
base
dgtsFlr :: Z -> [N] -> [N]
dgtsFlr Z
t [N]
ds = if Z
t forall a. Eq a => a -> a -> Bool
== forall r. Semiring r => r
rZero then [N]
ds else Z -> [N] -> [N]
dgtsFlr Z
z' [N]
ds'
where (Z
z',Z
zn) = forall a. Integral a => a -> a -> (a, a)
divMod Z
t Z
zbs
ds' :: [N]
ds' = forall a b. Projectible a b => b -> a
prj Z
zn forall a. a -> [a] -> [a]
: [N]
ds
dgtsFrc :: r -> [N]
dgtsFrc r
frc = if r
frc forall a. Eq a => a -> a -> Bool
== forall r. Semiring r => r
rZero then [] else forall a b. Projectible a b => b -> a
prj Z
flr forall a. a -> [a] -> [a]
: r -> [N]
dgtsFrc r
frc'
where (Z
flr,r
frc') = forall r. Number r => r -> (Z, r)
zFloorFraction (forall a. Additive a => N -> a -> a
ntimes N
base r
frc)
toDigitsFinite :: (Number r, KnownNat b, 2 <= b) => N -> r -> Digits b r
toDigitsFinite :: forall r (b :: Natural).
(Number r, KnownNat b, 2 <= b) =>
N -> r -> Digits b r
toDigitsFinite N
n = forall (b :: Natural) r. N -> Digits b r -> Digits b r
dgsFrcTake N
n forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall r (b :: Natural).
(Number r, KnownNat b, 2 <= b) =>
r -> Digits b r
toDigits
fromDigits :: (Number r, Acyclic r, KnownNat b) => N -> Digits b r -> r
fromDigits :: forall r (b :: Natural).
(Number r, Acyclic r, KnownNat b) =>
N -> Digits b r -> r
fromDigits N
n dgs :: Digits b r
dgs@(Digits r
s [N]
flr [N]
frc)
= r
s forall c. Multiplicative c => c -> c -> c
* (forall r. Number r => r -> [N] -> r -> r
rFlr r
b' [N]
flr forall r. Semiring r => r
rZero forall a. Additive a => a -> a -> a
+ forall r. Number r => r -> r -> [N] -> r -> r
rFrc r
br r
br (forall a. N -> [a] -> [a]
takeN N
n [N]
frc) forall r. Semiring r => r
rZero) where
b :: N
b = forall (b :: Natural) r. KnownNat b => Digits b r -> N
dgsBase Digits b r
dgs
b' :: r
b' = forall a. Additive a => N -> a -> a
ntimes N
b forall r. Semiring r => r
rOne
br :: r
br = forall c. Invertible c => c -> c
invert r
b'
rFlr :: Number r => r -> [N] -> r -> r
rFlr :: forall r. Number r => r -> [N] -> r -> r
rFlr r
_ [] r
r = r
r
rFlr r
x (N
d:[N]
ds) r
r = forall r. Number r => r -> [N] -> r -> r
rFlr r
x [N]
ds (r
x forall c. Multiplicative c => c -> c -> c
* r
r forall a. Additive a => a -> a -> a
+ forall a. Additive a => N -> a -> a
ntimes N
d forall r. Semiring r => r
rOne)
rFrc :: Number r => r -> r -> [N] -> r -> r
rFrc :: forall r. Number r => r -> r -> [N] -> r -> r
rFrc r
_ r
_ [] r
r = r
r
rFrc r
x r
y (N
d:[N]
ds) r
r = forall r. Number r => r -> r -> [N] -> r -> r
rFrc r
x r
z [N]
ds (r
r forall a. Additive a => a -> a -> a
+ r
y forall c. Multiplicative c => c -> c -> c
* forall a. Additive a => N -> a -> a
ntimes N
d forall r. Semiring r => r
rOne) where z :: r
z = r
xforall c. Multiplicative c => c -> c -> c
*r
y