{-# LANGUAGE FlexibleContexts #-}
module Prelude.Backprop (
sum,
product,
length,
minimum,
maximum,
traverse,
toList,
mapAccumL,
mapAccumR,
foldr,
foldl',
fmap,
fmapConst,
(<$>),
(<$),
($>),
pure,
liftA2,
liftA3,
fromIntegral,
realToFrac,
round,
fromIntegral',
E.coerce,
) where
import Numeric.Backprop
import qualified Numeric.Backprop.Explicit as E
import qualified Prelude.Backprop.Explicit as E
import Prelude (
Applicative,
Foldable,
Fractional (..),
Functor,
Num (..),
Ord (..),
Traversable,
)
import qualified Prelude as P
sum ::
(Foldable t, Functor t, Backprop (t a), Num a, Reifies s W) =>
BVar s (t a) ->
BVar s a
sum :: forall (t :: * -> *) a s.
(Foldable t, Functor t, Backprop (t a), Num a, Reifies s W) =>
BVar s (t a) -> BVar s a
sum = AddFunc (t a) -> BVar s (t a) -> BVar s a
forall (t :: * -> *) a s.
(Foldable t, Functor t, Num a, Reifies s W) =>
AddFunc (t a) -> BVar s (t a) -> BVar s a
E.sum AddFunc (t a)
forall a. Backprop a => AddFunc a
E.addFunc
{-# INLINE sum #-}
pure ::
(Foldable t, Applicative t, Backprop a, Reifies s W) =>
BVar s a ->
BVar s (t a)
pure :: forall (t :: * -> *) a s.
(Foldable t, Applicative t, Backprop a, Reifies s W) =>
BVar s a -> BVar s (t a)
pure = AddFunc a -> ZeroFunc a -> BVar s a -> BVar s (t a)
forall (t :: * -> *) s a.
(Foldable t, Applicative t, Reifies s W) =>
AddFunc a -> ZeroFunc a -> BVar s a -> BVar s (t a)
E.pure AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE pure #-}
product ::
(Foldable t, Functor t, Backprop (t a), Fractional a, Reifies s W) =>
BVar s (t a) ->
BVar s a
product :: forall (t :: * -> *) a s.
(Foldable t, Functor t, Backprop (t a), Fractional a,
Reifies s W) =>
BVar s (t a) -> BVar s a
product = AddFunc (t a) -> BVar s (t a) -> BVar s a
forall (t :: * -> *) a s.
(Foldable t, Functor t, Fractional a, Reifies s W) =>
AddFunc (t a) -> BVar s (t a) -> BVar s a
E.product AddFunc (t a)
forall a. Backprop a => AddFunc a
E.addFunc
{-# INLINE product #-}
length ::
(Foldable t, Backprop (t a), Num b, Reifies s W) =>
BVar s (t a) ->
BVar s b
length :: forall (t :: * -> *) a b s.
(Foldable t, Backprop (t a), Num b, Reifies s W) =>
BVar s (t a) -> BVar s b
length = AddFunc (t a) -> ZeroFunc (t a) -> BVar s (t a) -> BVar s b
forall (t :: * -> *) b s a.
(Foldable t, Num b, Reifies s W) =>
AddFunc (t a) -> ZeroFunc (t a) -> BVar s (t a) -> BVar s b
E.length AddFunc (t a)
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc (t a)
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE length #-}
minimum ::
(Foldable t, Functor t, Backprop a, Ord a, Backprop (t a), Reifies s W) =>
BVar s (t a) ->
BVar s a
minimum :: forall (t :: * -> *) a s.
(Foldable t, Functor t, Backprop a, Ord a, Backprop (t a),
Reifies s W) =>
BVar s (t a) -> BVar s a
minimum = AddFunc (t a) -> ZeroFunc a -> BVar s (t a) -> BVar s a
forall (t :: * -> *) a s.
(Foldable t, Functor t, Ord a, Reifies s W) =>
AddFunc (t a) -> ZeroFunc a -> BVar s (t a) -> BVar s a
E.minimum AddFunc (t a)
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE minimum #-}
maximum ::
(Foldable t, Functor t, Backprop a, Ord a, Backprop (t a), Reifies s W) =>
BVar s (t a) ->
BVar s a
maximum :: forall (t :: * -> *) a s.
(Foldable t, Functor t, Backprop a, Ord a, Backprop (t a),
Reifies s W) =>
BVar s (t a) -> BVar s a
maximum = AddFunc (t a) -> ZeroFunc a -> BVar s (t a) -> BVar s a
forall (t :: * -> *) a s.
(Foldable t, Functor t, Ord a, Reifies s W) =>
AddFunc (t a) -> ZeroFunc a -> BVar s (t a) -> BVar s a
E.maximum AddFunc (t a)
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE maximum #-}
foldr ::
(Traversable t, Backprop a, Reifies s W) =>
(BVar s a -> BVar s b -> BVar s b) ->
BVar s b ->
BVar s (t a) ->
BVar s b
foldr :: forall (t :: * -> *) a s b.
(Traversable t, Backprop a, Reifies s W) =>
(BVar s a -> BVar s b -> BVar s b)
-> BVar s b -> BVar s (t a) -> BVar s b
foldr = AddFunc a
-> ZeroFunc a
-> (BVar s a -> BVar s b -> BVar s b)
-> BVar s b
-> BVar s (t a)
-> BVar s b
forall (t :: * -> *) s a b.
(Traversable t, Reifies s W) =>
AddFunc a
-> ZeroFunc a
-> (BVar s a -> BVar s b -> BVar s b)
-> BVar s b
-> BVar s (t a)
-> BVar s b
E.foldr AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE foldr #-}
foldl' ::
(Traversable t, Backprop a, Reifies s W) =>
(BVar s b -> BVar s a -> BVar s b) ->
BVar s b ->
BVar s (t a) ->
BVar s b
foldl' :: forall (t :: * -> *) a s b.
(Traversable t, Backprop a, Reifies s W) =>
(BVar s b -> BVar s a -> BVar s b)
-> BVar s b -> BVar s (t a) -> BVar s b
foldl' = AddFunc a
-> ZeroFunc a
-> (BVar s b -> BVar s a -> BVar s b)
-> BVar s b
-> BVar s (t a)
-> BVar s b
forall (t :: * -> *) s a b.
(Traversable t, Reifies s W) =>
AddFunc a
-> ZeroFunc a
-> (BVar s b -> BVar s a -> BVar s b)
-> BVar s b
-> BVar s (t a)
-> BVar s b
E.foldl' AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE foldl' #-}
fmap ::
(Traversable f, Backprop a, Backprop b, Reifies s W) =>
(BVar s a -> BVar s b) ->
BVar s (f a) ->
BVar s (f b)
fmap :: forall (f :: * -> *) a b s.
(Traversable f, Backprop a, Backprop b, Reifies s W) =>
(BVar s a -> BVar s b) -> BVar s (f a) -> BVar s (f b)
fmap = AddFunc a
-> AddFunc b
-> ZeroFunc a
-> ZeroFunc b
-> (BVar s a -> BVar s b)
-> BVar s (f a)
-> BVar s (f b)
forall (f :: * -> *) s a b.
(Traversable f, Reifies s W) =>
AddFunc a
-> AddFunc b
-> ZeroFunc a
-> ZeroFunc b
-> (BVar s a -> BVar s b)
-> BVar s (f a)
-> BVar s (f b)
E.fmap AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc AddFunc b
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc ZeroFunc b
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE fmap #-}
fmapConst ::
(Functor f, Foldable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s b ->
BVar s (f a) ->
BVar s (f b)
fmapConst :: forall (f :: * -> *) b a s.
(Functor f, Foldable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s b -> BVar s (f a) -> BVar s (f b)
fmapConst = AddFunc (f a)
-> AddFunc b
-> ZeroFunc (f a)
-> ZeroFunc b
-> BVar s b
-> BVar s (f a)
-> BVar s (f b)
forall (f :: * -> *) s a b.
(Functor f, Foldable f, Reifies s W) =>
AddFunc (f a)
-> AddFunc b
-> ZeroFunc (f a)
-> ZeroFunc b
-> BVar s b
-> BVar s (f a)
-> BVar s (f b)
E.fmapConst AddFunc (f a)
forall a. Backprop a => AddFunc a
E.addFunc AddFunc b
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc (f a)
forall a. Backprop a => ZeroFunc a
E.zeroFunc ZeroFunc b
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE fmapConst #-}
(<$>) ::
(Traversable f, Backprop a, Backprop b, Reifies s W) =>
(BVar s a -> BVar s b) ->
BVar s (f a) ->
BVar s (f b)
<$> :: forall (f :: * -> *) a b s.
(Traversable f, Backprop a, Backprop b, Reifies s W) =>
(BVar s a -> BVar s b) -> BVar s (f a) -> BVar s (f b)
(<$>) = (BVar s a -> BVar s b) -> BVar s (f a) -> BVar s (f b)
forall (f :: * -> *) a b s.
(Traversable f, Backprop a, Backprop b, Reifies s W) =>
(BVar s a -> BVar s b) -> BVar s (f a) -> BVar s (f b)
fmap
infixl 4 <$>
{-# INLINE (<$>) #-}
(<$) ::
(Traversable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s b ->
BVar s (f a) ->
BVar s (f b)
<$ :: forall (f :: * -> *) b a s.
(Traversable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s b -> BVar s (f a) -> BVar s (f b)
(<$) = BVar s b -> BVar s (f a) -> BVar s (f b)
forall (f :: * -> *) b a s.
(Functor f, Foldable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s b -> BVar s (f a) -> BVar s (f b)
fmapConst
infixl 4 <$
{-# INLINE (<$) #-}
($>) ::
(Traversable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s (f a) ->
BVar s b ->
BVar s (f b)
BVar s (f a)
xs $> :: forall (f :: * -> *) b a s.
(Traversable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s (f a) -> BVar s b -> BVar s (f b)
$> BVar s b
x = BVar s b
x BVar s b -> BVar s (f a) -> BVar s (f b)
forall (f :: * -> *) b a s.
(Traversable f, Backprop b, Backprop (f a), Reifies s W) =>
BVar s b -> BVar s (f a) -> BVar s (f b)
<$ BVar s (f a)
xs
infixl 4 $>
{-# INLINE ($>) #-}
traverse ::
(Traversable t, Applicative f, Foldable f, Backprop a, Backprop b, Backprop (t b), Reifies s W) =>
(BVar s a -> f (BVar s b)) ->
BVar s (t a) ->
BVar s (f (t b))
traverse :: forall (t :: * -> *) (f :: * -> *) a b s.
(Traversable t, Applicative f, Foldable f, Backprop a, Backprop b,
Backprop (t b), Reifies s W) =>
(BVar s a -> f (BVar s b)) -> BVar s (t a) -> BVar s (f (t b))
traverse =
AddFunc a
-> AddFunc b
-> AddFunc (t b)
-> ZeroFunc a
-> ZeroFunc b
-> (BVar s a -> f (BVar s b))
-> BVar s (t a)
-> BVar s (f (t b))
forall (t :: * -> *) (f :: * -> *) s a b.
(Traversable t, Applicative f, Foldable f, Reifies s W) =>
AddFunc a
-> AddFunc b
-> AddFunc (t b)
-> ZeroFunc a
-> ZeroFunc b
-> (BVar s a -> f (BVar s b))
-> BVar s (t a)
-> BVar s (f (t b))
E.traverse
AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc
AddFunc b
forall a. Backprop a => AddFunc a
E.addFunc
AddFunc (t b)
forall a. Backprop a => AddFunc a
E.addFunc
ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
ZeroFunc b
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE traverse #-}
liftA2 ::
( Traversable f
, Applicative f
, Backprop a
, Backprop b
, Backprop c
, Reifies s W
) =>
(BVar s a -> BVar s b -> BVar s c) ->
BVar s (f a) ->
BVar s (f b) ->
BVar s (f c)
liftA2 :: forall (f :: * -> *) a b c s.
(Traversable f, Applicative f, Backprop a, Backprop b, Backprop c,
Reifies s W) =>
(BVar s a -> BVar s b -> BVar s c)
-> BVar s (f a) -> BVar s (f b) -> BVar s (f c)
liftA2 =
AddFunc a
-> AddFunc b
-> AddFunc c
-> ZeroFunc a
-> ZeroFunc b
-> ZeroFunc c
-> (BVar s a -> BVar s b -> BVar s c)
-> BVar s (f a)
-> BVar s (f b)
-> BVar s (f c)
forall (f :: * -> *) s a b c.
(Traversable f, Applicative f, Reifies s W) =>
AddFunc a
-> AddFunc b
-> AddFunc c
-> ZeroFunc a
-> ZeroFunc b
-> ZeroFunc c
-> (BVar s a -> BVar s b -> BVar s c)
-> BVar s (f a)
-> BVar s (f b)
-> BVar s (f c)
E.liftA2
AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc
AddFunc b
forall a. Backprop a => AddFunc a
E.addFunc
AddFunc c
forall a. Backprop a => AddFunc a
E.addFunc
ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
ZeroFunc b
forall a. Backprop a => ZeroFunc a
E.zeroFunc
ZeroFunc c
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE liftA2 #-}
liftA3 ::
( Traversable f
, Applicative f
, Backprop a
, Backprop b
, Backprop c
, Backprop d
, Reifies s W
) =>
(BVar s a -> BVar s b -> BVar s c -> BVar s d) ->
BVar s (f a) ->
BVar s (f b) ->
BVar s (f c) ->
BVar s (f d)
liftA3 :: forall (f :: * -> *) a b c d s.
(Traversable f, Applicative f, Backprop a, Backprop b, Backprop c,
Backprop d, Reifies s W) =>
(BVar s a -> BVar s b -> BVar s c -> BVar s d)
-> BVar s (f a) -> BVar s (f b) -> BVar s (f c) -> BVar s (f d)
liftA3 =
AddFunc a
-> AddFunc b
-> AddFunc c
-> AddFunc d
-> ZeroFunc a
-> ZeroFunc b
-> ZeroFunc c
-> ZeroFunc d
-> (BVar s a -> BVar s b -> BVar s c -> BVar s d)
-> BVar s (f a)
-> BVar s (f b)
-> BVar s (f c)
-> BVar s (f d)
forall (f :: * -> *) s a b c d.
(Traversable f, Applicative f, Reifies s W) =>
AddFunc a
-> AddFunc b
-> AddFunc c
-> AddFunc d
-> ZeroFunc a
-> ZeroFunc b
-> ZeroFunc c
-> ZeroFunc d
-> (BVar s a -> BVar s b -> BVar s c -> BVar s d)
-> BVar s (f a)
-> BVar s (f b)
-> BVar s (f c)
-> BVar s (f d)
E.liftA3
AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc
AddFunc b
forall a. Backprop a => AddFunc a
E.addFunc
AddFunc c
forall a. Backprop a => AddFunc a
E.addFunc
AddFunc d
forall a. Backprop a => AddFunc a
E.addFunc
ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
ZeroFunc b
forall a. Backprop a => ZeroFunc a
E.zeroFunc
ZeroFunc c
forall a. Backprop a => ZeroFunc a
E.zeroFunc
ZeroFunc d
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE liftA3 #-}
fromIntegral ::
(Backprop a, P.Integral a, P.Integral b, Reifies s W) =>
BVar s a ->
BVar s b
fromIntegral :: forall a b s.
(Backprop a, Integral a, Integral b, Reifies s W) =>
BVar s a -> BVar s b
fromIntegral = AddFunc a -> BVar s a -> BVar s b
forall a b s.
(Integral a, Integral b, Reifies s W) =>
AddFunc a -> BVar s a -> BVar s b
E.fromIntegral AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc
{-# INLINE fromIntegral #-}
realToFrac ::
(Backprop a, Fractional a, P.Real a, Fractional b, P.Real b, Reifies s W) =>
BVar s a ->
BVar s b
realToFrac :: forall a b s.
(Backprop a, Fractional a, Real a, Fractional b, Real b,
Reifies s W) =>
BVar s a -> BVar s b
realToFrac = AddFunc a -> BVar s a -> BVar s b
forall a b s.
(Fractional a, Real a, Fractional b, Real b, Reifies s W) =>
AddFunc a -> BVar s a -> BVar s b
E.realToFrac AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc
{-# INLINE realToFrac #-}
round ::
(P.RealFrac a, P.Integral b, Reifies s W) =>
BVar s a ->
BVar s b
round :: forall a b s.
(RealFrac a, Integral b, Reifies s W) =>
BVar s a -> BVar s b
round = AddFunc a -> BVar s a -> BVar s b
forall a b s.
(RealFrac a, Integral b, Reifies s W) =>
AddFunc a -> BVar s a -> BVar s b
E.round AddFunc a
forall a. Num a => AddFunc a
E.afNum
{-# INLINE round #-}
fromIntegral' ::
(P.Integral a, P.RealFrac b, Reifies s W) =>
BVar s a ->
BVar s b
fromIntegral' :: forall a b s.
(Integral a, RealFrac b, Reifies s W) =>
BVar s a -> BVar s b
fromIntegral' = AddFunc a -> BVar s a -> BVar s b
forall a b s.
(Integral a, RealFrac b, Reifies s W) =>
AddFunc a -> BVar s a -> BVar s b
E.fromIntegral' AddFunc a
forall a. Num a => AddFunc a
E.afNum
{-# INLINE fromIntegral' #-}
toList ::
(Traversable t, Backprop a, Reifies s W) =>
BVar s (t a) ->
[BVar s a]
toList :: forall (t :: * -> *) a s.
(Traversable t, Backprop a, Reifies s W) =>
BVar s (t a) -> [BVar s a]
toList = AddFunc a -> ZeroFunc a -> BVar s (t a) -> [BVar s a]
forall (t :: * -> *) s a.
(Traversable t, Reifies s W) =>
AddFunc a -> ZeroFunc a -> BVar s (t a) -> [BVar s a]
E.toList AddFunc a
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc a
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE toList #-}
mapAccumL ::
(Traversable t, Backprop b, Backprop c, Reifies s W) =>
(BVar s a -> BVar s b -> (BVar s a, BVar s c)) ->
BVar s a ->
BVar s (t b) ->
(BVar s a, BVar s (t c))
mapAccumL :: forall (t :: * -> *) b c s a.
(Traversable t, Backprop b, Backprop c, Reifies s W) =>
(BVar s a -> BVar s b -> (BVar s a, BVar s c))
-> BVar s a -> BVar s (t b) -> (BVar s a, BVar s (t c))
mapAccumL = AddFunc b
-> AddFunc c
-> ZeroFunc b
-> ZeroFunc c
-> (BVar s a -> BVar s b -> (BVar s a, BVar s c))
-> BVar s a
-> BVar s (t b)
-> (BVar s a, BVar s (t c))
forall (t :: * -> *) s b c a.
(Traversable t, Reifies s W) =>
AddFunc b
-> AddFunc c
-> ZeroFunc b
-> ZeroFunc c
-> (BVar s a -> BVar s b -> (BVar s a, BVar s c))
-> BVar s a
-> BVar s (t b)
-> (BVar s a, BVar s (t c))
E.mapAccumL AddFunc b
forall a. Backprop a => AddFunc a
E.addFunc AddFunc c
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc b
forall a. Backprop a => ZeroFunc a
E.zeroFunc ZeroFunc c
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE mapAccumL #-}
mapAccumR ::
(Traversable t, Backprop b, Backprop c, Reifies s W) =>
(BVar s a -> BVar s b -> (BVar s a, BVar s c)) ->
BVar s a ->
BVar s (t b) ->
(BVar s a, BVar s (t c))
mapAccumR :: forall (t :: * -> *) b c s a.
(Traversable t, Backprop b, Backprop c, Reifies s W) =>
(BVar s a -> BVar s b -> (BVar s a, BVar s c))
-> BVar s a -> BVar s (t b) -> (BVar s a, BVar s (t c))
mapAccumR = AddFunc b
-> AddFunc c
-> ZeroFunc b
-> ZeroFunc c
-> (BVar s a -> BVar s b -> (BVar s a, BVar s c))
-> BVar s a
-> BVar s (t b)
-> (BVar s a, BVar s (t c))
forall (t :: * -> *) s b c a.
(Traversable t, Reifies s W) =>
AddFunc b
-> AddFunc c
-> ZeroFunc b
-> ZeroFunc c
-> (BVar s a -> BVar s b -> (BVar s a, BVar s c))
-> BVar s a
-> BVar s (t b)
-> (BVar s a, BVar s (t c))
E.mapAccumR AddFunc b
forall a. Backprop a => AddFunc a
E.addFunc AddFunc c
forall a. Backprop a => AddFunc a
E.addFunc ZeroFunc b
forall a. Backprop a => ZeroFunc a
E.zeroFunc ZeroFunc c
forall a. Backprop a => ZeroFunc a
E.zeroFunc
{-# INLINE mapAccumR #-}