fixed-vector-hetero-0.3.1.1: Generic heterogeneous vectors

Safe HaskellNone
LanguageHaskell98

Data.Vector.HFixed.Class

Contents

Synopsis

Types and type classes

Peano numbers

data S n :: * -> *

Successor of n

Instances

Arity n => Index Z (S n) 
Arity n => Arity (S n) 
(NatIso k ((-) n 1), (~) * (ToPeano ((-) n 1)) k, (~) * (ToPeano n) (S k), (~) Nat n ((+) 1 ((-) n 1))) => NatIso (S k) n 
HomArity n a => HomArity (S n) a Source 
Index k n => Index (S k) (S n) 
Index n xs => Index (S n) ((:) * x xs) Source 
type HomList k (S n) a = (:) k a (HomList k n a) Source 
type Add (S n) k = S (Add n k) 
type Fn (S n) a b = a -> Fn n a b 
type ValueAt (S n) ((:) * x xs) = ValueAt n xs Source 
type NewElems (S n) ((:) * x xs) a = (:) * x (NewElems n xs a) Source 

data Z :: *

Type level zero

Instances

Arity Z 
NatIso Z 0 
HomArity Z a Source 
Arity n => Index Z (S n) 
Arity xs => Index Z ((:) * x xs) Source 
type Add Z n = n 
type Fn Z a b = b 
type HomList k Z a = [] k Source 
type ValueAt Z ((:) * x xs) = x Source 
type NewElems Z ((:) * x xs) a = (:) * a xs Source 

Isomorphism between Peano numbers and Nats

class ((~) Nat (ToNat a) b, (~) * (ToPeano b) a) => NatIso a b

Isomorphism between two representations of natural numbers

Instances

NatIso Z 0 
(NatIso k ((-) n 1), (~) * (ToPeano ((-) n 1)) k, (~) * (ToPeano n) (S k), (~) Nat n ((+) 1 ((-) n 1))) => NatIso (S k) n 

type family ToPeano b :: *

Convert Nat number to Peano represenation

Equations

ToPeano 0 = Z 
ToPeano n = S (ToPeano ((-) n 1)) 

type family ToNat a :: Nat

Convert Peano number to Nat

Equations

ToNat Z = 0 
ToNat (S k) = (+) 1 (ToNat k) 

N-ary functions

type family Fn as b Source

Type family for N-ary function. Types of function parameters are encoded as the list of types.

Instances

type Fn ([] *) b = b Source 
type Fn ((:) * a as) b = a -> Fn as b Source 

newtype Fun as b Source

Newtype wrapper to work around of type families' lack of injectivity.

Constructors

Fun 

Fields

unFun :: Fn as b
 

Instances

Arity xs => Monad (Fun xs) Source 
Arity xs => Functor (Fun xs) Source 
Arity xs => Applicative (Fun xs) Source 

newtype TFun f as b Source

Newtype wrapper for function where all type parameters have same type constructor. This type is required for writing function which works with monads, appicatives etc.

Constructors

TFun 

Fields

unTFun :: Fn (Wrap f as) b
 

Instances

Arity xs => Monad (TFun * f xs) Source 
Arity xs => Functor (TFun * f xs) Source 
Arity xs => Applicative (TFun * f xs) Source 

funToTFun :: Fun (Wrap f xs) b -> TFun f xs b Source

Cast Fun to equivalent TFun

tfunToFun :: TFun f xs b -> Fun (Wrap f xs) b Source

Cast TFun to equivalent Fun

Type functions

data Proxy t :: k -> *

A concrete, poly-kinded proxy type

Constructors

Proxy 

Instances

Monad (Proxy *) 
Functor (Proxy *) 
Applicative (Proxy *) 
Vector (Proxy *) a 
Bounded (Proxy k s) 
Enum (Proxy k s) 
Eq (Proxy k s) 
Ord (Proxy k s) 
Read (Proxy k s) 
Show (Proxy k s) 
Ix (Proxy k s) 
Generic (Proxy * t) 
Monoid (Proxy k s) 
NFData (Proxy * a)

Since: 1.4.0.0

type Dim (Proxy *) = Z 
type Rep (Proxy k t) = D1 D1Proxy (C1 C1_0Proxy U1) 

type family xs ++ ys :: [α] Source

Concaternation of type level lists.

Instances

type (++) k ([] k) ys = ys Source 
type (++) k ((:) k x xs) ys = (:) k x ((++) k xs ys) Source 

type family Len xs :: * Source

Length of type list expressed as type level naturals from fixed-vector.

Instances

type Len k ([] k) = Z Source 
type Len k ((:) k x xs) = S (Len k xs) Source 

type family Wrap f a :: [β] Source

Wrap every element of list into type constructor

Instances

type Wrap k k1 f ([] k1) = [] k Source 
type Wrap k k1 f ((:) k1 x xs) = (:) k (f x) (Wrap k k1 f xs) Source 

type family HomList n a :: [α] Source

Homogeneous type list with length n and element of type a. It uses type level natural defined in fixed-vector.

Instances

type HomList k Z a = [] k Source 
type HomList k (S n) a = (:) k a (HomList k n a) Source 

Type classes

class Arity (Len xs) => Arity xs where Source

Type class for dealing with N-ary function in generic way. Both accum and apply work with accumulator data types which are polymorphic. So it's only possible to write functions which rearrange elements in vector using plain ADT. It's possible to get around it by using GADT as accumulator (See ArityC and function which use it)

This is also somewhat a kitchen sink module. It contains witnesses which could be used to prove type equalities or to bring instance in scope.

Methods

accum Source

Arguments

:: (forall a as. t (a : as) -> a -> t as)

Step function. Applies element to accumulator.

-> (t `[]` -> b)

Extract value from accumulator.

-> t xs

Initial state.

-> Fn xs b 

Fold over N elements exposed as N-ary function.

apply Source

Arguments

:: (forall a as. t (a : as) -> (a, t as))

Extract value to be applied to function.

-> t xs

Initial state.

-> ContVec xs 

Apply values to N-ary function

applyM Source

Arguments

:: Monad m 
=> (forall a as. t (a : as) -> m (a, t as))

Extract value to be applied to function

-> t xs

Initial state

-> m (ContVec xs) 

Apply value to N-ary function using monadic actions

accumTy :: (forall a as. t (a : as) -> f a -> t as) -> (t `[]` -> b) -> t xs -> Fn (Wrap f xs) b Source

Analog of accum

applyTy :: (forall a as. t (a : as) -> (f a, t as)) -> t xs -> ContVecF xs f Source

Analog of apply which allows to works with vectors which elements are wrapped in the newtype constructor.

arity :: p xs -> Int Source

Size of type list as integer.

witWrapped :: WitWrapped f xs Source

witConcat :: Arity ys => WitConcat xs ys Source

witNestedFun :: WitNestedFun xs ys r Source

witLenWrap :: WitLenWrap f xs Source

Instances

Arity ([] *) Source 
Arity xs => Arity ((:) * x xs) Source 

class Arity xs => ArityC c xs where Source

Declares that every type in list satisfy constraint c

Instances

ArityC c ([] *) Source 
(c x, ArityC c xs) => ArityC c ((:) * x xs) Source 

class Arity (Elems v) => HVector v where Source

Type class for heterogeneous vectors. Instance should specify way to construct and deconstruct itself

Note that this type class is extremely generic. Almost any single constructor data type could be made instance. It could be monomorphic, it could be polymorphic in some or all fields it doesn't matter. Only law instance should obey is:

inspect v construct = v

Default implementation which uses Generic is provided.

Minimal complete definition

Nothing

Associated Types

type Elems v :: [*] Source

Methods

construct :: Fun (Elems v) v Source

Function for constructing vector

inspect :: v -> Fun (Elems v) a -> a Source

Function for deconstruction of vector. It applies vector's elements to N-ary function.

Instances

HVector () Source

Unit is empty heterogeneous vector

HVector (Complex a) Source 
Arity xs => HVector (ContVec xs) Source 
Arity xs => HVector (VecList xs) Source 
Arity xs => HVector (HVec xs) Source 
HVector (a, b) Source 
(Storable a, HomArity n a) => HVector (Vec n a) Source 
(Unbox n a, HomArity n a) => HVector (Vec n a) Source 
(Prim a, HomArity n a) => HVector (Vec n a) Source 
HomArity n a => HVector (Vec n a) Source 
(Arity (Wrap * * f xs), Arity xs) => HVector (HVecF xs f) Source

It's not possible to remove constrain Arity (Wrap f xs) because it's required by superclass and we cannot prove it for all f. witWrapped allow to generate proofs for terms

HVector (a, b, c) Source 
HVector (a, b, c, d) Source 
HVector (a, b, c, d, e) Source 
HVector (a, b, c, d, e, f) Source 
HVector (a, b, c, d, e, f, g) Source 
HVector (a, b, c, d, e, f, g, h) Source 
HVector (a, b, c, d, e, f, g, h, i) Source 
HVector (a, b, c, d, e, f, g, h, i, j) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a') Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a', b') Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a', b', c') Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a', b', c', d') Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a', b', c', d', e') Source 
HVector (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a', b', c', d', e', f') Source 

class Arity (ElemsF v) => HVectorF v where Source

Type class for partially homogeneous vector where every element in the vector have same type constructor. Vector itself is parametrized by that constructor

Associated Types

type ElemsF v :: [*] Source

Elements of the vector without type constructors

Methods

inspectF :: v f -> TFun f (ElemsF v) a -> a Source

constructF :: TFun f (ElemsF v) (v f) Source

Instances

Witnesses

data WitWrapped f xs where Source

Witness that observe fact that if we have instance Arity xs than we have instance Arity (Wrap f xs).

Constructors

WitWrapped :: Arity (Wrap f xs) => WitWrapped f xs 

data WitConcat xs ys where Source

Witness that observe fact that (Arity xs, Arity ys) implies Arity (xs++ys)

Constructors

WitConcat :: Arity (xs ++ ys) => WitConcat xs ys 

data WitNestedFun xs ys r where Source

Observes fact that Fn (xs++ys) r ~ Fn xs (Fn ys r)

Constructors

WitNestedFun :: (Fn (xs ++ ys) r ~ Fn xs (Fn ys r)) => WitNestedFun xs ys r 

data WitLenWrap f xs where Source

Observe fact than Len xs ~ Len (Wrap f xs)

Constructors

WitLenWrap :: (Len xs ~ Len (Wrap f xs)) => WitLenWrap f xs 

data WitWrapIndex f n xs where Source

Proofs for the indexing of wrapped type lists.

Constructors

WitWrapIndex :: (ValueAt n (Wrap f xs) ~ f (ValueAt n xs), Index n (Wrap f xs), Arity (Wrap f xs)) => WitWrapIndex f n xs 

data WitAllInstances c xs where Source

Witness that all elements of type list satisfy predicate c.

Constructors

WitAllInstancesNil :: WitAllInstances c `[]` 
WitAllInstancesCons :: c x => WitAllInstances c xs -> WitAllInstances c (x : xs) 

CPS-encoded vector

newtype ContVec xs Source

CPS-encoded heterogeneous vector.

Constructors

ContVec 

Fields

runContVec :: forall r. Fun xs r -> r
 

Instances

Arity xs => HVector (ContVec xs) Source 
type Elems (ContVec xs) = xs Source 

newtype ContVecF xs f Source

CPS-encoded partially heterogeneous vector.

Constructors

ContVecF (forall r. TFun f xs r -> r) 

Instances

Arity xs => HVectorF (ContVecF * xs) Source 
type ElemsF (ContVecF * xs) = xs Source 

cons :: x -> ContVec xs -> ContVec (x : xs) Source

Cons element to the vector

consF :: f x -> ContVecF xs f -> ContVecF (x : xs) f Source

Cons element to the vector

Interop with homogeneous vectors

class (Arity n, Arity (HomList n a)) => HomArity n a where Source

Conversion between homogeneous and heterogeneous N-ary functions.

Methods

toHeterogeneous :: Fun n a r -> Fun (HomList n a) r Source

Convert n-ary homogeneous function to heterogeneous.

toHomogeneous :: Fun (HomList n a) r -> Fun n a r Source

Convert heterogeneous n-ary function to homogeneous.

Instances

homInspect :: (Vector v a, HomArity (Dim v) a) => v a -> Fun (HomList (Dim v) a) r -> r Source

Default implementation of inspect for homogeneous vector.

homConstruct :: forall v a. (Vector v a, HomArity (Dim v) a) => Fun (HomList (Dim v) a) (v a) Source

Default implementation of construct for homogeneous vector.

Operations of Fun

Primitives for Fun

curryFun :: Fun (x : xs) r -> x -> Fun xs r Source

Apply single parameter to function

uncurryFun :: (x -> Fun xs r) -> Fun (x : xs) r Source

Uncurry N-ary function.

uncurryFun2 :: Arity xs => (x -> y -> Fun xs (Fun ys r)) -> Fun (x : xs) (Fun (y : ys) r) Source

curryMany :: forall xs ys r. Arity xs => Fun (xs ++ ys) r -> Fun xs (Fun ys r) Source

Curry first n arguments of N-ary function.

constFun :: Fun xs r -> Fun (x : xs) r Source

Add one parameter to function which is ignored.

stepFun :: (Fun xs a -> Fun ys b) -> Fun (x : xs) a -> Fun (x : ys) b Source

Transform function but leave outermost parameter untouched.

Primitives for TFun

curryTFun :: TFun f (x : xs) r -> f x -> TFun f xs r Source

Apply single parameter to function

uncurryTFun :: (f x -> TFun f xs r) -> TFun f (x : xs) r Source

Uncurry single parameter

uncurryTFun2 :: (Arity xs, Arity ys) => (f x -> f y -> TFun f xs (TFun f ys r)) -> TFun f (x : xs) (TFun f (y : ys) r) Source

Uncurry two parameters for nested TFun.

shuffleTF :: forall f x xs r. Arity xs => (x -> TFun f xs r) -> TFun f xs (x -> r) Source

Move first argument of function to its result. This function is useful for implementation of lens.

More complicated functions

concatF :: (Arity xs, Arity ys) => (a -> b -> c) -> Fun xs a -> Fun ys b -> Fun (xs ++ ys) c Source

Concatenate n-ary functions. This function combine results of both N-ary functions and merge their parameters into single list.

shuffleF :: forall x xs r. Arity xs => (x -> Fun xs r) -> Fun xs (x -> r) Source

Move first argument of function to its result. This function is useful for implementation of lens.

lensWorkerF :: forall f r x y xs. (Functor f, Arity xs) => (x -> f y) -> Fun (y : xs) r -> Fun (x : xs) (f r) Source

Helper for lens implementation.

class Arity n => Index n xs where Source

Indexing of vectors

Associated Types

type ValueAt n xs :: * Source

Type at position n

type NewElems n xs a :: [*] Source

List of types with n'th element replaced by a.

Methods

getF :: n -> Fun xs (ValueAt n xs) Source

Getter function for vectors

putF :: n -> ValueAt n xs -> Fun xs r -> Fun xs r Source

Putter function. It applies value x to nth parameter of function.

lensF :: (Functor f, v ~ ValueAt n xs) => n -> (v -> f v) -> Fun xs r -> Fun xs (f r) Source

Helper for implementation of lens

lensChF :: Functor f => n -> (ValueAt n xs -> f a) -> Fun (NewElems n xs a) r -> Fun xs (f r) Source

Helper for type-changing lens

witWrapIndex :: WitWrapIndex f n xs Source

Instances

Arity xs => Index Z ((:) * x xs) Source 
Index n xs => Index (S n) ((:) * x xs) Source