| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Data.V.Linear
Contents
Description
This module defines vectors of known length which can hold linear values.
Having a known length matters with linear types, because many common vector operations (like zip) are not total with linear types.
Make these vectors by giving any finite number of arguments to make
and use them with elim:
>>>:set -XLinearTypes>>>:set -XTypeApplications>>>:set -XTypeInType>>>:set -XTypeFamilies>>>import Prelude.Linear>>>import qualified Data.V.Linear as V>>>:{doSomething :: Int %1-> Int %1-> Bool doSomething x y = x + y > 0 :}
>>>:{isTrue :: Bool isTrue = V.elim doSomething (build 4 9) where build :: Int %1-> Int %1-> V.V 2 Int build = V.make :}
A much more expensive library of vectors of known size (including matrices
and tensors of all dimensions) is the linear library on
Hackage (that's linear in the
sense of linear algebra,
rather than linear types).
Synopsis
- data V (n :: Nat) (a :: Type)
- empty :: forall a. V 0 a
- consume :: V 0 a %1 -> ()
- map :: (a %1 -> b) -> V n a %1 -> V n b
- pure :: forall n a. KnownNat n => a -> V n a
- (<*>) :: V n (a %1 -> b) %1 -> V n a %1 -> V n b
- uncons# :: 1 <= n => V n a %1 -> (# a, V (n - 1) a #)
- uncons :: 1 <= n => V n a %1 -> (a, V (n - 1) a)
- class Elim n a b
- elim :: forall (n :: Nat) a b f. (n ~ PeanoToNat (NatToPeano n), Elim (NatToPeano n) a b, IsFunN a b f, f ~ FunN (NatToPeano n) a b, n ~ Arity b f) => f %1 -> V n a %1 -> b
- cons :: forall n a. a %1 -> V (n - 1) a %1 -> V n a
- fromReplicator :: forall n a. KnownNat n => Replicator a %1 -> V n a
- dupV :: forall n a. (KnownNat n, Dupable a) => a %1 -> V n a
- theLength :: forall n. KnownNat n => Int
- class Make m n a
- make :: forall (n :: Nat) a f. (n ~ PeanoToNat (NatToPeano n), Make (NatToPeano n) (NatToPeano n) a, IsFunN a (V n a) f, f ~ FunN (NatToPeano n) a (V n a), n ~ ArityV f) => f
- type family ArityV f where ...
Documentation
data V (n :: Nat) (a :: Type) Source #
represents an immutable sequence of V n an elements of type a
(like a n-tuple), with a linear Applicative instance.
Instances
| Functor (V n) Source # | |
| KnownNat n => Applicative (V n) Source # | |
| Functor (V n) Source # | |
| KnownNat n => Traversable (V n) Source # | |
Defined in Data.V.Linear.Internal.Instances | |
| Eq a => Eq (V n a) Source # | |
| Ord a => Ord (V n a) Source # | |
| (KnownNat n, Consumable a) => Consumable (V n a) Source # | |
Defined in Data.Unrestricted.Linear.Internal.Instances | |
| Consumable (V 0 a) Source # | |
Defined in Data.Unrestricted.Linear.Internal.Instances | |
| (KnownNat n, Dupable a) => Dupable (V n a) Source # | |
uncons# :: 1 <= n => V n a %1 -> (# a, V (n - 1) a #) Source #
Splits the head and tail of the V, returning an unboxed tuple.
uncons :: 1 <= n => V n a %1 -> (a, V (n - 1) a) Source #
Splits the head and tail of the V, returning a boxed tuple.
is used to implement Elim n a belim without recursion
so that we can guarantee that elim will be inlined and unrolled.
Minimal complete definition
elim'
Instances
| Elim 'Z a b Source # | |
Defined in Data.V.Linear.Internal | |
| (1 <= (1 + PeanoToNat n), ((1 + PeanoToNat n) - 1) ~ PeanoToNat n, Elim n a b) => Elim ('S n) a b Source # | |
Defined in Data.V.Linear.Internal | |
elim :: forall (n :: Nat) a b f. (n ~ PeanoToNat (NatToPeano n), Elim (NatToPeano n) a b, IsFunN a b f, f ~ FunN (NatToPeano n) a b, n ~ Arity b f) => f %1 -> V n a %1 -> b Source #
Takes a function of type a %1 -> a %1 -> ... %1 -> a %1 -> b, and
returns a b . The is used to supply all the items of type V n aa
required by the function.
For instance:
elim @1 :: (a %1 -> b) %1 -> V 1 a %1 -> b elim @2 :: (a %1 -> a %1 -> b) %1 -> V 2 a %1 -> b elim @3 :: (a %1 -> a %1 -> a %1 -> b) %1 -> V 3 a %1 -> b
It is not always necessary to give the arity argument. It can be inferred from the function argument.
About the constraints of this function (they won't get in your way):
n ~is just there to help GHC, and will always be provedPeanoToNat(NatToPeanon)provides the actual implementation ofElim(NatToPeanon) a belim; there is an instance of this class for any(n, a, b)indicate the shape ofIsFunNa b f, f ~FunN(NatToPeanon) a b, n ~Arityb ffto the typechecker (see documentation ofIsFunN).
fromReplicator :: forall n a. KnownNat n => Replicator a %1 -> V n a Source #
Creates a V of the specified size by consuming a Replicator.
theLength :: forall n. KnownNat n => Int Source #
Returns the type-level Nat of the context as a term-level integer.
is used to avoid recursion in the implementation of Make m n amake
so that make can be inlined.
Make is solely used in the signature of that function.
Minimal complete definition
make'
Instances
| Make 'Z n a Source # | |
Defined in Data.V.Linear.Internal Methods make' :: (V (PeanoToNat 'Z) a %1 -> V (PeanoToNat n) a) %1 -> FunN 'Z a (V (PeanoToNat n) a) | |
| (((1 + PeanoToNat m) - 1) ~ PeanoToNat m, Make m n a) => Make ('S m) n a Source # | |
Defined in Data.V.Linear.Internal Methods make' :: (V (PeanoToNat ('S m)) a %1 -> V (PeanoToNat n) a) %1 -> FunN ('S m) a (V (PeanoToNat n) a) | |
make :: forall (n :: Nat) a f. (n ~ PeanoToNat (NatToPeano n), Make (NatToPeano n) (NatToPeano n) a, IsFunN a (V n a) f, f ~ FunN (NatToPeano n) a (V n a), n ~ ArityV f) => f Source #
Builds a n-ary constructor for (i.e. a function taking V n an linear
arguments of type a and returning a ).V n a
myV :: V 3 Int myV = make 1 2 3
About the constraints of this function (they won't get in your way):
n ~is just there to help GHC, and will always be provedPeanoToNat(NatToPeanon)provides the actual implementation ofMake(NatToPeanon) (NatToPeanon) amake; there is an instance of this class for any(n, a)indicate the shape to the typechecker ofIsFunNa (Vn a) f, f ~FunN(NatToPeanon) a (Vn a), n ~ArityVff(see documentation ofIsFunN).