Copyright | (c) Justin Le 2018 |
---|---|
License | BSD3 |
Maintainer | justin@jle.im |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Canonical strict tuples (and unit) with Num
instances for usage with
backprop. This is here to solve the problem of orphan instances in
libraries and potential mismatched tuple types.
If you are writing a library that needs to export BVar
s of tuples,
consider using the tuples in this module so that your library can have
easy interoperability with other libraries using backprop.
Because of API decisions, backprop
and gradBP
only work with things
with Num
instances. However, this disallows default Prelude
tuples
(without orphan instances from packages like
NumInstances).
Until tuples have Num
instances in base, this module is intended to
be a workaround for situations where:
This comes up often in cases where:
- A function wants to return more than one value (
BVar
s (T2
a b) - You want to uncurry a
BVar
function to use withbackprop
andgradBP
. - You want to use the useful
Prism
s automatically generated by the lens library, which use tuples for multiple-constructor fields.
Only 2-tuples and 3-tuples are provided. Any more and you should probably be using your own custom product types, with instances automatically generated from something like one-liner-instances.
Lenses into the fields are provided, but they also work with _1
, _2
,
and _3
from Lens.Micro. However, note that these are incompatible
with _1
, _2
, and _3
from Control.Lens.
You can "construct" a
with functions like
BVar
s (T2
a b)isoVar
.
Since: 0.1.1.0
- data T0 = T0
- data T2 a b = T2 !a !b
- t2Tup :: T2 a b -> (a, b)
- tupT2 :: (a, b) -> T2 a b
- uncurryT2 :: (a -> b -> c) -> T2 a b -> c
- curryT2 :: (T2 a b -> c) -> a -> b -> c
- t2_1 :: Lens (T2 a b) (T2 a' b) a a'
- t2_2 :: Lens (T2 a b) (T2 a b') b b'
- data T3 a b c = T3 !a !b !c
- t3Tup :: T3 a b c -> (a, b, c)
- tupT3 :: (a, b, c) -> T3 a b c
- t3_1 :: Lens (T3 a b c) (T3 a' b c) a a'
- t3_2 :: Lens (T3 a b c) (T3 a b' c) b b'
- t3_3 :: Lens (T3 a b c) (T3 a b c') c c'
- uncurryT3 :: (a -> b -> c -> d) -> T3 a b c -> d
- curryT3 :: (T3 a b c -> d) -> a -> b -> c -> d
- data T :: [Type] -> Type where
- indexT :: Index as a -> T as -> a
- tOnly :: T '[a] -> a
- onlyT :: a -> T '[a]
- tSplit :: Length as -> T (as ++ bs) -> (T as, T bs)
- tAppend :: T as -> T bs -> T (as ++ bs)
- tProd :: T as -> Tuple as
- prodT :: Tuple as -> T as
- tIx :: Index as a -> Lens' (T as) a
- tHead :: Lens (T (a ': as)) (T (b ': as)) a b
- tTail :: Lens (T (a ': as)) (T (a ': bs)) (T as) (T bs)
- tTake :: forall as bs cs. Length as -> Lens (T (as ++ bs)) (T (cs ++ bs)) (T as) (T cs)
- tDrop :: forall as bs cs. Length as -> Lens (T (as ++ bs)) (T (as ++ cs)) (T bs) (T cs)
- constT :: forall c as. ListC (c <$> as) => (forall a. c a => a) -> Length as -> T as
- mapT :: forall c as. ListC (c <$> as) => (forall a. c a => a -> a) -> T as -> T as
- zipT :: forall c as. ListC (c <$> as) => (forall a. c a => a -> a -> a) -> T as -> T as -> T as
Zero-tuples (unit)
Unit ('()') with Num
, Fractional
, and Floating
instances.
Be aware that the methods in its numerical instances are all non-strict:
_ + _ =T0
negate
_ =T0
fromIntegral
_ =T0
Since: 0.1.4.0
Eq T0 Source # | |
Floating T0 Source # | |
Fractional T0 Source # | |
Data T0 Source # | |
Num T0 Source # | |
Ord T0 Source # | |
Read T0 Source # | |
Show T0 Source # | |
Generic T0 Source # | |
Semigroup T0 Source # | |
Monoid T0 Source # | |
Binary T0 Source # | Since: 0.1.5.1 |
NFData T0 Source # | |
Random T0 Source # | Since: 0.1.5.2 |
type Rep T0 Source # | |
Two-tuples
Strict 2-tuple with Num
, Fractional
, and Floating
instances.
Since: 0.1.1.0
T2 !a !b |
Bifunctor T2 Source # | |
Functor (T2 a) Source # | |
(Eq b, Eq a) => Eq (T2 a b) Source # | |
(Floating a, Floating b) => Floating (T2 a b) Source # | |
(Fractional a, Fractional b) => Fractional (T2 a b) Source # | |
(Data b, Data a) => Data (T2 a b) Source # | |
(Num a, Num b) => Num (T2 a b) Source # | |
(Ord b, Ord a) => Ord (T2 a b) Source # | |
(Read b, Read a) => Read (T2 a b) Source # | |
(Show b, Show a) => Show (T2 a b) Source # | |
Generic (T2 a b) Source # | |
(Semigroup a, Semigroup b) => Semigroup (T2 a b) Source # | |
(Semigroup a, Semigroup b, Monoid a, Monoid b) => Monoid (T2 a b) Source # | |
(Binary a, Binary b) => Binary (T2 a b) Source # | Since: 0.1.5.1 |
(NFData a, NFData b) => NFData (T2 a b) Source # | |
(Random a, Random b) => Random (T2 a b) Source # | Since: 0.1.5.2 |
Field1 (T2 a b) (T2 a' b) a a' Source # | |
Field2 (T2 a b) (T2 a b') b b' Source # | |
type Rep (T2 a b) Source # | |
Conversions
If using lens, the two conversion functions can be chained with prisms and traversals and other optics using:
iso
tupT2
t2Tup
::Iso'
(a, b) (T2
a b)
Consumption
uncurryT2 :: (a -> b -> c) -> T2 a b -> c Source #
Uncurry a function to take in a T2
of its arguments
Since: 0.1.2.0
curryT2 :: (T2 a b -> c) -> a -> b -> c Source #
Curry a function taking a T2
of its arguments
Since: 0.1.2.0
Lenses
t2_1 :: Lens (T2 a b) (T2 a' b) a a' Source #
Lens into the first field of a T2
. Also exported as _1
from
Lens.Micro.
t2_2 :: Lens (T2 a b) (T2 a b') b b' Source #
Lens into the second field of a T2
. Also exported as _2
from
Lens.Micro.
Three-tuples
Strict 3-tuple with a Num
, Fractional
, and Floating
instances.
Since: 0.1.1.0
T3 !a !b !c |
Conversions
If using lens, the two conversion functions can be chained with prisms and traversals and other optics using:
iso
tupT3
t2Tup
::Iso'
(a, b, c) (T3
a b c)
Lenses
t3_1 :: Lens (T3 a b c) (T3 a' b c) a a' Source #
Lens into the first field of a T3
. Also exported as _1
from
Lens.Micro.
t3_2 :: Lens (T3 a b c) (T3 a b' c) b b' Source #
Lens into the second field of a T3
. Also exported as _2
from
Lens.Micro.
t3_3 :: Lens (T3 a b c) (T3 a b c') c c' Source #
Lens into the third field of a T3
. Also exported as _3
from
Lens.Micro.
Consumption
uncurryT3 :: (a -> b -> c -> d) -> T3 a b c -> d Source #
Uncurry a function to take in a T3
of its arguments
Since: 0.1.2.0
curryT3 :: (T3 a b c -> d) -> a -> b -> c -> d Source #
Curry a function taking a T3
of its arguments
Since: 0.1.2.0
N-Tuples
data T :: [Type] -> Type where Source #
Strict inductive N-tuple with a Num
, Fractional
, and Floating
instances.
It is basically "yet another HList", like the one found in Data.Type.Product and many other locations on the haskell ecosystem. Because it's inductively defined, it has O(n) random indexing, but is efficient for zipping and mapping and other sequential consumption patterns.
It is provided because of its Num
instance, making it useful for
backproup. Will be obsolete when Product
gets
numerical instances.
Since: 0.1.5.0
Conversions
If using lens, the two conversion functions can be chained with prisms and traversals and other optics using:
iso
onlyT
tOnly
::Iso'
a (T '[a])
Lenses
tTail :: Lens (T (a ': as)) (T (a ': bs)) (T as) (T bs) Source #
Lens into the tail of a T
Since: 0.1.5.0
Internal Utility
constT :: forall c as. ListC (c <$> as) => (forall a. c a => a) -> Length as -> T as Source #
Initialize a T
with a Rank-N value. Mostly used internally, but
provided in case useful.
Must be used with TypeApplications to provide the Rank-N constraint.
Since: 0.1.5.0