{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module ZkFold.Base.Algebra.Basic.VectorSpace where
import Control.Monad.State.Strict
import Data.Functor.Rep
import Data.Kind (Type)
import GHC.Generics hiding (Rep)
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), div, divMod, length, mod, negate, product, replicate,
sum, (/), (^))
import ZkFold.Base.Algebra.Basic.Class
class VectorSpace a v where
type Basis a v :: Type
tabulateV :: (Basis a v -> a) -> v a
indexV :: v a -> Basis a v -> a
addV :: (AdditiveSemigroup a, VectorSpace a v) => v a -> v a -> v a
addV :: forall a (v :: Type -> Type).
(AdditiveSemigroup a, VectorSpace a v) =>
v a -> v a -> v a
addV = (a -> a -> a) -> v a -> v a -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(a -> a -> a) -> v a -> v a -> v a
zipWithV a -> a -> a
forall a. AdditiveSemigroup a => a -> a -> a
(+)
zeroV :: (AdditiveMonoid a, VectorSpace a v) => v a
zeroV :: forall a (v :: Type -> Type).
(AdditiveMonoid a, VectorSpace a v) =>
v a
zeroV = a -> v a
forall a (v :: Type -> Type). VectorSpace a v => a -> v a
pureV a
forall a. AdditiveMonoid a => a
zero
subtractV :: (AdditiveGroup a, VectorSpace a v) => v a -> v a -> v a
subtractV :: forall a (v :: Type -> Type).
(AdditiveGroup a, VectorSpace a v) =>
v a -> v a -> v a
subtractV = (a -> a -> a) -> v a -> v a -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(a -> a -> a) -> v a -> v a -> v a
zipWithV (-)
negateV :: (AdditiveGroup a, VectorSpace a v) => v a -> v a
negateV :: forall a (v :: Type -> Type).
(AdditiveGroup a, VectorSpace a v) =>
v a -> v a
negateV = (a -> a) -> v a -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(a -> a) -> v a -> v a
mapV a -> a
forall a. AdditiveGroup a => a -> a
negate
scaleV :: (MultiplicativeSemigroup a, VectorSpace a v) => a -> v a -> v a
scaleV :: forall a (v :: Type -> Type).
(MultiplicativeSemigroup a, VectorSpace a v) =>
a -> v a -> v a
scaleV a
c = (a -> a) -> v a -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(a -> a) -> v a -> v a
mapV (a
c a -> a -> a
forall a. MultiplicativeSemigroup a => a -> a -> a
*)
basisV :: (Semiring a, VectorSpace a v, Eq (Basis a v)) => Basis a v -> v a
basisV :: forall a (v :: Type -> Type).
(Semiring a, VectorSpace a v, Eq (Basis a v)) =>
Basis a v -> v a
basisV Basis a v
i = (Basis a v -> a) -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV ((Basis a v -> a) -> v a) -> (Basis a v -> a) -> v a
forall a b. (a -> b) -> a -> b
$ \Basis a v
j -> if Basis a v
i Basis a v -> Basis a v -> Bool
forall a. Eq a => a -> a -> Bool
== Basis a v
j then a
forall a. MultiplicativeMonoid a => a
one else a
forall a. AdditiveMonoid a => a
zero
dotV :: (Semiring a, VectorSpace a v, Foldable v) => v a -> v a -> a
v a
v dotV :: forall a (v :: Type -> Type).
(Semiring a, VectorSpace a v, Foldable v) =>
v a -> v a -> a
`dotV` v a
w = v a -> a
forall (t :: Type -> Type) a.
(Foldable t, AdditiveMonoid a) =>
t a -> a
sum ((a -> a -> a) -> v a -> v a -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(a -> a -> a) -> v a -> v a -> v a
zipWithV a -> a -> a
forall a. MultiplicativeSemigroup a => a -> a -> a
(*) v a
v v a
w)
mapV :: VectorSpace a v => (a -> a) -> v a -> v a
mapV :: forall a (v :: Type -> Type).
VectorSpace a v =>
(a -> a) -> v a -> v a
mapV a -> a
f = (Basis a v -> a) -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV ((Basis a v -> a) -> v a) -> (v a -> Basis a v -> a) -> v a -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> (Basis a v -> a) -> Basis a v -> a
forall a b. (a -> b) -> (Basis a v -> a) -> Basis a v -> b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
f ((Basis a v -> a) -> Basis a v -> a)
-> (v a -> Basis a v -> a) -> v a -> Basis a v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> Basis a v -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV
pureV :: VectorSpace a v => a -> v a
pureV :: forall a (v :: Type -> Type). VectorSpace a v => a -> v a
pureV = (Basis a v -> a) -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV ((Basis a v -> a) -> v a) -> (a -> Basis a v -> a) -> a -> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Basis a v -> a
forall a b. a -> b -> a
const
zipWithV :: VectorSpace a v => (a -> a -> a) -> v a -> v a -> v a
zipWithV :: forall a (v :: Type -> Type).
VectorSpace a v =>
(a -> a -> a) -> v a -> v a -> v a
zipWithV a -> a -> a
f v a
as v a
bs = (Basis a v -> a) -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV ((Basis a v -> a) -> v a) -> (Basis a v -> a) -> v a
forall a b. (a -> b) -> a -> b
$ \Basis a v
k ->
a -> a -> a
f (v a -> Basis a v -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV v a
as Basis a v
k) (v a -> Basis a v -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV v a
bs Basis a v
k)
dimV
:: forall a v. (Functor v, Foldable v, VectorSpace a v)
=> Natural
dimV :: forall a (v :: Type -> Type).
(Functor v, Foldable v, VectorSpace a v) =>
Natural
dimV = v Natural -> Natural
forall (t :: Type -> Type) a.
(Foldable t, AdditiveMonoid a) =>
t a -> a
sum ((a -> Natural) -> v a -> v Natural
forall a b. (a -> b) -> v a -> v b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
_ -> Natural
1) (forall a (v :: Type -> Type). VectorSpace a v => a -> v a
pureV @a @v a
forall {a}. a
err))
where
err :: a
err = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"ZkFold.Base.Algebra.Basic.VectorSpace.dimV impossible"
fromListV
:: forall a v. (AdditiveMonoid a, Traversable v, VectorSpace a v)
=> [a] -> v a
fromListV :: forall a (v :: Type -> Type).
(AdditiveMonoid a, Traversable v, VectorSpace a v) =>
[a] -> v a
fromListV = State [a] (v a) -> [a] -> v a
forall s a. State s a -> s -> a
evalState ((a -> StateT [a] Identity a) -> v a -> State [a] (v a)
forall (t :: Type -> Type) (f :: Type -> Type) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> v a -> f (v b)
traverse a -> StateT [a] Identity a
forall {m :: Type -> Type} {b} {p}.
(MonadState [b] m, AdditiveMonoid b) =>
p -> m b
listState (forall a (v :: Type -> Type). VectorSpace a v => a -> v a
pureV @a @v a
forall {a}. a
err))
where
err :: a
err = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"ZkFold.Base.Algebra.Basic.VectorSpace.fromList impossible"
listState :: p -> m b
listState p
_ = m [b]
forall s (m :: Type -> Type). MonadState s m => m s
get m [b] -> ([b] -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
[] -> b -> m b
forall a. a -> m a
forall (m :: Type -> Type) a. Monad m => a -> m a
return b
forall a. AdditiveMonoid a => a
zero
b
x:[b]
xs -> [b] -> m ()
forall s (m :: Type -> Type). MonadState s m => s -> m ()
put [b]
xs m () -> m b -> m b
forall a b. m a -> m b -> m b
forall (m :: Type -> Type) a b. Monad m => m a -> m b -> m b
>> b -> m b
forall a. a -> m a
forall (m :: Type -> Type) a. Monad m => a -> m a
return b
x
iterateV
:: forall a v x. (Traversable v, VectorSpace a v)
=> (x -> x) -> x -> v x
iterateV :: forall a (v :: Type -> Type) x.
(Traversable v, VectorSpace a v) =>
(x -> x) -> x -> v x
iterateV x -> x
f = State x (v x) -> x -> v x
forall s a. State s a -> s -> a
evalState ((a -> StateT x Identity x) -> v a -> State x (v x)
forall (t :: Type -> Type) (f :: Type -> Type) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> v a -> f (v b)
traverse a -> StateT x Identity x
iteration (forall a (v :: Type -> Type). VectorSpace a v => a -> v a
pureV @a @v a
forall {a}. a
err))
where
err :: a
err = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"ZkFold.Base.Algebra.Basic.VectorSpace.iterateV impossible"
iteration :: a -> StateT x Identity x
iteration a
_ = do
x
x <- StateT x Identity x
forall s (m :: Type -> Type). MonadState s m => m s
get
x -> StateT x Identity ()
forall s (m :: Type -> Type). MonadState s m => s -> m ()
put (x -> x
f x
x)
x -> StateT x Identity x
forall a. a -> StateT x Identity a
forall (m :: Type -> Type) a. Monad m => a -> m a
return x
x
newtype Representably v (a :: Type) = Representably
{ forall (v :: Type -> Type) a. Representably v a -> v a
runRepresentably :: v a }
instance Representable v => VectorSpace a (Representably v) where
type Basis a (Representably v) = Rep v
tabulateV :: (Basis a (Representably v) -> a) -> Representably v a
tabulateV = v a -> Representably v a
forall (v :: Type -> Type) a. v a -> Representably v a
Representably (v a -> Representably v a)
-> ((Rep v -> a) -> v a) -> (Rep v -> a) -> Representably v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rep v -> a) -> v a
forall a. (Rep v -> a) -> v a
forall (f :: Type -> Type) a.
Representable f =>
(Rep f -> a) -> f a
tabulate
indexV :: Representably v a -> Basis a (Representably v) -> a
indexV = v a -> Rep v -> a
forall a. v a -> Rep v -> a
forall (f :: Type -> Type) a. Representable f => f a -> Rep f -> a
index (v a -> Rep v -> a)
-> (Representably v a -> v a) -> Representably v a -> Rep v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Representably v a -> v a
forall (v :: Type -> Type) a. Representably v a -> v a
runRepresentably
instance (Generic1 v, VectorSpace a (Rep1 v))
=> VectorSpace a (Generically1 v) where
type Basis a (Generically1 v) = Basis a (Rep1 v)
indexV :: Generically1 v a -> Basis a (Generically1 v) -> a
indexV (Generically1 v a
v) Basis a (Generically1 v)
i = Rep1 v a -> Basis a (Rep1 v) -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV (v a -> Rep1 v a
forall a. v a -> Rep1 v a
forall k (f :: k -> Type) (a :: k). Generic1 f => f a -> Rep1 f a
from1 v a
v) Basis a (Rep1 v)
Basis a (Generically1 v)
i
tabulateV :: (Basis a (Generically1 v) -> a) -> Generically1 v a
tabulateV Basis a (Generically1 v) -> a
f = v a -> Generically1 v a
forall {k} (f :: k -> Type) (a :: k). f a -> Generically1 f a
Generically1 (Rep1 v a -> v a
forall a. Rep1 v a -> v a
forall k (f :: k -> Type) (a :: k). Generic1 f => Rep1 f a -> f a
to1 ((Basis a (Rep1 v) -> a) -> Rep1 v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV Basis a (Rep1 v) -> a
Basis a (Generically1 v) -> a
f))
instance VectorSpace a v => VectorSpace a (M1 i c v) where
type Basis a (M1 i c v) = Basis a v
indexV :: M1 i c v a -> Basis a (M1 i c v) -> a
indexV (M1 v a
v) = v a -> Basis a v -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV v a
v
tabulateV :: (Basis a (M1 i c v) -> a) -> M1 i c v a
tabulateV Basis a (M1 i c v) -> a
f = v a -> M1 i c v a
forall k i (c :: Meta) (f :: k -> Type) (p :: k). f p -> M1 i c f p
M1 ((Basis a v -> a) -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV Basis a v -> a
Basis a (M1 i c v) -> a
f)
deriving via Representably U1 instance VectorSpace a U1
deriving via Representably Par1 instance VectorSpace a Par1
instance (VectorSpace a v, VectorSpace a u)
=> VectorSpace a (v :*: u) where
type Basis a (v :*: u) = Either (Basis a v) (Basis a u)
tabulateV :: (Basis a (v :*: u) -> a) -> (:*:) v u a
tabulateV Basis a (v :*: u) -> a
f = (Basis a v -> a) -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV (Either (Basis a v) (Basis a u) -> a
Basis a (v :*: u) -> a
f (Either (Basis a v) (Basis a u) -> a)
-> (Basis a v -> Either (Basis a v) (Basis a u)) -> Basis a v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Basis a v -> Either (Basis a v) (Basis a u)
forall a b. a -> Either a b
Left) v a -> u a -> (:*:) v u a
forall k (f :: k -> Type) (g :: k -> Type) (p :: k).
f p -> g p -> (:*:) f g p
:*: (Basis a u -> a) -> u a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV (Either (Basis a v) (Basis a u) -> a
Basis a (v :*: u) -> a
f (Either (Basis a v) (Basis a u) -> a)
-> (Basis a u -> Either (Basis a v) (Basis a u)) -> Basis a u -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Basis a u -> Either (Basis a v) (Basis a u)
forall a b. b -> Either a b
Right)
indexV :: (:*:) v u a -> Basis a (v :*: u) -> a
indexV (v a
a :*: u a
_) (Left Basis a v
i) = v a -> Basis a v -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV v a
a Basis a v
i
indexV (v a
_ :*: u a
b) (Right Basis a u
j) = u a -> Basis a u -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV u a
b Basis a u
j
instance (Representable u, VectorSpace a v)
=> VectorSpace a (u :.: v) where
type Basis a (u :.: v) = (Rep u, Basis a v)
tabulateV :: (Basis a (u :.: v) -> a) -> (:.:) u v a
tabulateV = u (v a) -> (:.:) u v a
forall k2 k1 (f :: k2 -> Type) (g :: k1 -> k2) (p :: k1).
f (g p) -> (:.:) f g p
Comp1 (u (v a) -> (:.:) u v a)
-> (((Rep u, Basis a v) -> a) -> u (v a))
-> ((Rep u, Basis a v) -> a)
-> (:.:) u v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rep u -> v a) -> u (v a)
forall a. (Rep u -> a) -> u a
forall (f :: Type -> Type) a.
Representable f =>
(Rep f -> a) -> f a
tabulate ((Rep u -> v a) -> u (v a))
-> (((Rep u, Basis a v) -> a) -> Rep u -> v a)
-> ((Rep u, Basis a v) -> a)
-> u (v a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Basis a v -> a) -> v a)
-> (Rep u -> Basis a v -> a) -> Rep u -> v a
forall a b. (a -> b) -> (Rep u -> a) -> Rep u -> b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Basis a v -> a) -> v a
forall a (v :: Type -> Type).
VectorSpace a v =>
(Basis a v -> a) -> v a
tabulateV ((Rep u -> Basis a v -> a) -> Rep u -> v a)
-> (((Rep u, Basis a v) -> a) -> Rep u -> Basis a v -> a)
-> ((Rep u, Basis a v) -> a)
-> Rep u
-> v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Rep u, Basis a v) -> a) -> Rep u -> Basis a v -> a
forall a b c. ((a, b) -> c) -> a -> b -> c
curry
indexV :: (:.:) u v a -> Basis a (u :.: v) -> a
indexV (Comp1 u (v a)
fg) (Rep u
i, Basis a v
j) = v a -> Basis a v -> a
forall a (v :: Type -> Type).
VectorSpace a v =>
v a -> Basis a v -> a
indexV (u (v a) -> Rep u -> v a
forall a. u a -> Rep u -> a
forall (f :: Type -> Type) a. Representable f => f a -> Rep f -> a
index u (v a)
fg Rep u
i) Basis a v
j
class
( VectorSpace a (InputSpace a f)
, VectorSpace a (OutputSpace a f)
) => FunctionSpace a f where
uncurryV :: f -> InputSpace a f a -> OutputSpace a f a
curryV :: (InputSpace a f a -> OutputSpace a f a) -> f
type family InputSpace a f where
InputSpace a (x a -> f) = x :*: InputSpace a f
InputSpace a (y a) = U1
type family OutputSpace a f where
OutputSpace a (x a -> f) = OutputSpace a f
OutputSpace a (y a) = y
instance {-# OVERLAPPABLE #-}
( VectorSpace a y
, OutputSpace a (y a) ~ y
, InputSpace a (y a) ~ U1
) => FunctionSpace a (y a) where
uncurryV :: y a -> InputSpace a (y a) a -> OutputSpace a (y a) a
uncurryV y a
f InputSpace a (y a) a
_ = y a
OutputSpace a (y a) a
f
curryV :: (InputSpace a (y a) a -> OutputSpace a (y a) a) -> y a
curryV InputSpace a (y a) a -> OutputSpace a (y a) a
k = InputSpace a (y a) a -> OutputSpace a (y a) a
k U1 a
InputSpace a (y a) a
forall k (p :: k). U1 p
U1
instance {-# OVERLAPPING #-}
( VectorSpace a x
, OutputSpace a (x a -> f) ~ OutputSpace a f
, InputSpace a (x a -> f) ~ x :*: InputSpace a f
, FunctionSpace a f
) => FunctionSpace a (x a -> f) where
uncurryV :: (x a -> f)
-> InputSpace a (x a -> f) a -> OutputSpace a (x a -> f) a
uncurryV x a -> f
f (x a
i :*: InputSpace a f a
j) = f -> InputSpace a f a -> OutputSpace a f a
forall a f.
FunctionSpace a f =>
f -> InputSpace a f a -> OutputSpace a f a
uncurryV (x a -> f
f x a
i) InputSpace a f a
j
curryV :: (InputSpace a (x a -> f) a -> OutputSpace a (x a -> f) a)
-> x a -> f
curryV InputSpace a (x a -> f) a -> OutputSpace a (x a -> f) a
k x a
x = (InputSpace a f a -> OutputSpace a f a) -> f
forall a f.
FunctionSpace a f =>
(InputSpace a f a -> OutputSpace a f a) -> f
curryV ((:*:) x (InputSpace a f) a -> OutputSpace a f a
InputSpace a (x a -> f) a -> OutputSpace a (x a -> f) a
k ((:*:) x (InputSpace a f) a -> OutputSpace a f a)
-> (InputSpace a f a -> (:*:) x (InputSpace a f) a)
-> InputSpace a f a
-> OutputSpace a f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x a -> InputSpace a f a -> (:*:) x (InputSpace a f) a
forall k (f :: k -> Type) (g :: k -> Type) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) x a
x)
composeFunctions
:: ( FunctionSpace a g
, FunctionSpace a f
, OutputSpace a f ~ InputSpace a g
)
=> g -> f -> InputSpace a f a -> OutputSpace a g a
composeFunctions :: forall a g f.
(FunctionSpace a g, FunctionSpace a f,
OutputSpace a f ~ InputSpace a g) =>
g -> f -> InputSpace a f a -> OutputSpace a g a
composeFunctions g
g f
f = g -> InputSpace a g a -> OutputSpace a g a
forall a f.
FunctionSpace a f =>
f -> InputSpace a f a -> OutputSpace a f a
uncurryV g
g (InputSpace a g a -> OutputSpace a g a)
-> (InputSpace a f a -> InputSpace a g a)
-> InputSpace a f a
-> OutputSpace a g a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f -> InputSpace a f a -> OutputSpace a f a
forall a f.
FunctionSpace a f =>
f -> InputSpace a f a -> OutputSpace a f a
uncurryV f
f