Naperian-0.1.0.1: Naperian Functors for APL-like programming

Safe HaskellNone
LanguageHaskell2010

Naperian

Synopsis

Documentation

data Finite :: Nat -> Type where Source #

The finite set of type-bounded Naturals. A value of type Fin n has exactly n inhabitants, the natural numbers from [0..n-1].

Constructors

Fin :: Int -> Finite n 

Instances

Eq (Finite a) Source # 

Methods

(==) :: Finite a -> Finite a -> Bool #

(/=) :: Finite a -> Finite a -> Bool #

Show (Finite a) Source # 

Methods

showsPrec :: Int -> Finite a -> ShowS #

show :: Finite a -> String #

showList :: [Finite a] -> ShowS #

finite :: forall n. KnownNat n => Int -> Maybe (Finite n) Source #

Create a type-bounded finite number Fin n from a runtime integer, bounded to a statically known limit. If the input value x > n, then Nothing is returned. Otherwise, returns Just (x :: Fin n).

azipWith :: Applicative f => (a -> b -> c) -> f a -> f b -> f c Source #

"Applicative zipping".

showVector :: [String] -> String Source #

Format a vector to make it look nice.

data Pair a Source #

The cartesian product of a, equivalent to (a, a).

Constructors

Pair a a 

Instances

Functor Pair Source # 

Methods

fmap :: (a -> b) -> Pair a -> Pair b #

(<$) :: a -> Pair b -> Pair a #

Applicative Pair Source # 

Methods

pure :: a -> Pair a #

(<*>) :: Pair (a -> b) -> Pair a -> Pair b #

liftA2 :: (a -> b -> c) -> Pair a -> Pair b -> Pair c #

(*>) :: Pair a -> Pair b -> Pair b #

(<*) :: Pair a -> Pair b -> Pair a #

Foldable Pair Source # 

Methods

fold :: Monoid m => Pair m -> m #

foldMap :: Monoid m => (a -> m) -> Pair a -> m #

foldr :: (a -> b -> b) -> b -> Pair a -> b #

foldr' :: (a -> b -> b) -> b -> Pair a -> b #

foldl :: (b -> a -> b) -> b -> Pair a -> b #

foldl' :: (b -> a -> b) -> b -> Pair a -> b #

foldr1 :: (a -> a -> a) -> Pair a -> a #

foldl1 :: (a -> a -> a) -> Pair a -> a #

toList :: Pair a -> [a] #

null :: Pair a -> Bool #

length :: Pair a -> Int #

elem :: Eq a => a -> Pair a -> Bool #

maximum :: Ord a => Pair a -> a #

minimum :: Ord a => Pair a -> a #

sum :: Num a => Pair a -> a #

product :: Num a => Pair a -> a #

Traversable Pair Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Pair a -> f (Pair b) #

sequenceA :: Applicative f => Pair (f a) -> f (Pair a) #

mapM :: Monad m => (a -> m b) -> Pair a -> m (Pair b) #

sequence :: Monad m => Pair (m a) -> m (Pair a) #

Dimension Pair Source # 

Methods

size :: Pair a -> Int Source #

Naperian Pair Source # 

Associated Types

type Log (Pair :: * -> *) :: * Source #

Methods

lookup :: Pair a -> Log Pair -> a Source #

tabulate :: (Log Pair -> a) -> Pair a Source #

positions :: Pair (Log Pair) Source #

Eq a => Eq (Pair a) Source # 

Methods

(==) :: Pair a -> Pair a -> Bool #

(/=) :: Pair a -> Pair a -> Bool #

Ord a => Ord (Pair a) Source # 

Methods

compare :: Pair a -> Pair a -> Ordering #

(<) :: Pair a -> Pair a -> Bool #

(<=) :: Pair a -> Pair a -> Bool #

(>) :: Pair a -> Pair a -> Bool #

(>=) :: Pair a -> Pair a -> Bool #

max :: Pair a -> Pair a -> Pair a #

min :: Pair a -> Pair a -> Pair a #

Show a => Show (Pair a) Source # 

Methods

showsPrec :: Int -> Pair a -> ShowS #

show :: Pair a -> String #

showList :: [Pair a] -> ShowS #

type Log Pair Source # 
type Log Pair = Bool

newtype Vector (n :: Nat) a Source #

Constructors

Vector (Vector a) 

Instances

Functor (Vector n) Source # 

Methods

fmap :: (a -> b) -> Vector n a -> Vector n b #

(<$) :: a -> Vector n b -> Vector n a #

KnownNat n => Applicative (Vector n) Source # 

Methods

pure :: a -> Vector n a #

(<*>) :: Vector n (a -> b) -> Vector n a -> Vector n b #

liftA2 :: (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c #

(*>) :: Vector n a -> Vector n b -> Vector n b #

(<*) :: Vector n a -> Vector n b -> Vector n a #

Foldable (Vector n) Source # 

Methods

fold :: Monoid m => Vector n m -> m #

foldMap :: Monoid m => (a -> m) -> Vector n a -> m #

foldr :: (a -> b -> b) -> b -> Vector n a -> b #

foldr' :: (a -> b -> b) -> b -> Vector n a -> b #

foldl :: (b -> a -> b) -> b -> Vector n a -> b #

foldl' :: (b -> a -> b) -> b -> Vector n a -> b #

foldr1 :: (a -> a -> a) -> Vector n a -> a #

foldl1 :: (a -> a -> a) -> Vector n a -> a #

toList :: Vector n a -> [a] #

null :: Vector n a -> Bool #

length :: Vector n a -> Int #

elem :: Eq a => a -> Vector n a -> Bool #

maximum :: Ord a => Vector n a -> a #

minimum :: Ord a => Vector n a -> a #

sum :: Num a => Vector n a -> a #

product :: Num a => Vector n a -> a #

Traversable (Vector n) Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Vector n a -> f (Vector n b) #

sequenceA :: Applicative f => Vector n (f a) -> f (Vector n a) #

mapM :: Monad m => (a -> m b) -> Vector n a -> m (Vector n b) #

sequence :: Monad m => Vector n (m a) -> m (Vector n a) #

KnownNat n => Dimension (Vector n) Source # 

Methods

size :: Vector n a -> Int Source #

KnownNat n => Naperian (Vector n) Source # 

Associated Types

type Log (Vector n :: * -> *) :: * Source #

Methods

lookup :: Vector n a -> Log (Vector n) -> a Source #

tabulate :: (Log (Vector n) -> a) -> Vector n a Source #

positions :: Vector n (Log (Vector n)) Source #

(KnownNat n, Traversable (Vector n)) => IsList (Vector n a) Source # 

Associated Types

type Item (Vector n a) :: * #

Methods

fromList :: [Item (Vector n a)] -> Vector n a #

fromListN :: Int -> [Item (Vector n a)] -> Vector n a #

toList :: Vector n a -> [Item (Vector n a)] #

Eq a => Eq (Vector n a) Source # 

Methods

(==) :: Vector n a -> Vector n a -> Bool #

(/=) :: Vector n a -> Vector n a -> Bool #

Ord a => Ord (Vector n a) Source # 

Methods

compare :: Vector n a -> Vector n a -> Ordering #

(<) :: Vector n a -> Vector n a -> Bool #

(<=) :: Vector n a -> Vector n a -> Bool #

(>) :: Vector n a -> Vector n a -> Bool #

(>=) :: Vector n a -> Vector n a -> Bool #

max :: Vector n a -> Vector n a -> Vector n a #

min :: Vector n a -> Vector n a -> Vector n a #

Show a => Show (Vector n a) Source # 

Methods

showsPrec :: Int -> Vector n a -> ShowS #

show :: Vector n a -> String #

showList :: [Vector n a] -> ShowS #

type Log (Vector n) Source # 
type Log (Vector n) = Finite n
type Item (Vector n a) Source # 
type Item (Vector n a) = a

tail :: Vector (n + 1) a -> Vector n a Source #

fromList :: forall n a. KnownNat n => [a] -> Maybe (Vector n a) Source #

zipWith :: (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c Source #

length :: forall n a. KnownNat n => Vector n a -> Int Source #

replicate :: forall n a. KnownNat n => a -> Vector n a Source #

index :: Vector n a -> Finite n -> a Source #

viota :: forall n. KnownNat n => Vector n (Finite n) Source #

class Functor f => Naperian f where Source #

Naperian functors.

Minimal complete definition

lookup, (tabulate | positions)

Associated Types

type Log f Source #

The "logarithm" of f. This type represents the input you use to look up values inside f a. For example, if you have a list [a], and you want to look up a value, then you use an Int to index into the list. In this case, Log [a] = Int. If you have a type-bounded Vector Vector (n :: Nat) a, then Log (Vector n) is the range of integers [0..n-1] (represented here as Finite n.)

Methods

lookup :: f a -> Log f -> a Source #

Look up an element a inside f a. If you read this function type in english, it says "if you give me an f a, then I will give you a function, so you can look up the elements of f a and get back an a"

tabulate :: (Log f -> a) -> f a Source #

Tabulate a Naperian. This creates f a values by mapping the logarithm of f onto every "position" inside f a

positions :: f (Log f) Source #

Find every position in the "space" of the Naperian f.

Instances

Naperian Pair Source # 

Associated Types

type Log (Pair :: * -> *) :: * Source #

Methods

lookup :: Pair a -> Log Pair -> a Source #

tabulate :: (Log Pair -> a) -> Pair a Source #

positions :: Pair (Log Pair) Source #

KnownNat n => Naperian (Vector n) Source # 

Associated Types

type Log (Vector n :: * -> *) :: * Source #

Methods

lookup :: Vector n a -> Log (Vector n) -> a Source #

tabulate :: (Log (Vector n) -> a) -> Vector n a Source #

positions :: Vector n (Log (Vector n)) Source #

transpose :: (Naperian f, Naperian g) => f (g a) -> g (f a) Source #

The transposition of two Naperian functors f and g.

class (Applicative f, Naperian f, Traversable f) => Dimension f where Source #

Methods

size :: f a -> Int Source #

Instances

Dimension Pair Source # 

Methods

size :: Pair a -> Int Source #

KnownNat n => Dimension (Vector n) Source # 

Methods

size :: Vector n a -> Int Source #

inner :: (Num a, Dimension f) => f a -> f a -> a Source #

matrix :: (Num a, Dimension f, Dimension g, Dimension h) => f (g a) -> g (h a) -> f (h a) Source #

data Hyper :: [Type -> Type] -> Type -> Type where Source #

Arbitrary-rank Hypercuboids, parameterized over their dimension.

Constructors

Scalar :: a -> Hyper '[] a 
Prism :: (Dimension f, Shapely fs) => Hyper fs (f a) -> Hyper (f ': fs) a 

Instances

Functor (Hyper fs) Source # 

Methods

fmap :: (a -> b) -> Hyper fs a -> Hyper fs b #

(<$) :: a -> Hyper fs b -> Hyper fs a #

Shapely fs => Applicative (Hyper fs) Source # 

Methods

pure :: a -> Hyper fs a #

(<*>) :: Hyper fs (a -> b) -> Hyper fs a -> Hyper fs b #

liftA2 :: (a -> b -> c) -> Hyper fs a -> Hyper fs b -> Hyper fs c #

(*>) :: Hyper fs a -> Hyper fs b -> Hyper fs b #

(<*) :: Hyper fs a -> Hyper fs b -> Hyper fs a #

Show a => Show (Hyper fs a) Source # 

Methods

showsPrec :: Int -> Hyper fs a -> ShowS #

show :: Hyper fs a -> String #

showList :: [Hyper fs a] -> ShowS #

point :: Hyper '[] a -> a Source #

crystal :: Hyper (f ': fs) a -> Hyper fs (f a) Source #

class Shapely fs where Source #

Minimal complete definition

hreplicate, hsize

Methods

hreplicate :: a -> Hyper fs a Source #

hsize :: Hyper fs a -> Int Source #

Instances

Shapely ([] (Type -> Type)) Source # 

Methods

hreplicate :: a -> Hyper [Type -> Type] a Source #

hsize :: Hyper [Type -> Type] a -> Int Source #

(Dimension f, Shapely fs) => Shapely ((:) (* -> *) f fs) Source # 

Methods

hreplicate :: a -> Hyper (((* -> *) ': f) fs) a Source #

hsize :: Hyper (((* -> *) ': f) fs) a -> Int Source #

hzipWith :: (a -> b -> c) -> Hyper fs a -> Hyper fs b -> Hyper fs c Source #

first :: Shapely fs => Hyper fs a -> a Source #

transposeH :: Hyper (f ': (g ': fs)) a -> Hyper (g ': (f ': fs)) a Source #

Generalized transposition over arbitrary-rank hypercuboids.

foldrH :: (a -> a -> a) -> a -> Hyper (f ': fs) a -> Hyper fs a Source #

Fold over a single dimension of a Hypercuboid.

unary :: Shapely fs => (a -> b) -> Hyper fs a -> Hyper fs b Source #

Lift an unary function from values to hypercuboids of values.

binary :: (Compatible fs gs, Max fs gs ~ hs, Alignable fs hs, Alignable gs hs) => (a -> b -> c) -> Hyper fs a -> Hyper gs b -> Hyper hs c Source #

Lift a binary function from values to two sets of hypercuboids, which can be aligned properly.

up :: (Shapely fs, Dimension f) => Hyper fs a -> Hyper (f ': fs) a Source #

innerH :: (Max fs gs ~ (f ': hs), Alignable fs (f ': hs), Alignable gs (f ': hs), Compatible fs gs, Num a) => Hyper fs a -> Hyper gs a -> Hyper hs a Source #

Generalized, rank-polymorphic inner product.

matrixH :: (Num a, Dimension f, Dimension g, Dimension h) => Hyper '[g, f] a -> Hyper '[h, g] a -> Hyper '[h, f] a Source #

Generalized, rank-polymorphic matrix product.

class (Shapely fs, Shapely gs) => Alignable fs gs where Source #

Minimal complete definition

align

Methods

align :: Hyper fs a -> Hyper gs a Source #

Instances

Alignable ([] (Type -> Type)) ([] (Type -> Type)) Source # 

Methods

align :: Hyper [Type -> Type] a -> Hyper [Type -> Type] a Source #

(Dimension f, Shapely fs) => Alignable ([] (Type -> Type)) ((:) (* -> *) f fs) Source # 

Methods

align :: Hyper [Type -> Type] a -> Hyper (((* -> *) ': f) fs) a Source #

(Dimension f, Alignable fs gs) => Alignable ((:) (* -> *) f fs) ((:) (* -> *) f gs) Source # 

Methods

align :: Hyper (((* -> *) ': f) fs) a -> Hyper (((* -> *) ': f) gs) a Source #

type family Max (fs :: [Type -> Type]) (gs :: [Type -> Type]) :: [Type -> Type] where ... Source #

Equations

Max '[] '[] = '[] 
Max '[] (f ': gs) = f ': gs 
Max (f ': fs) '[] = f ': fs 
Max (f ': fs) (f ': gs) = f ': Max fs gs 

type family Compatible (fs :: [Type -> Type]) (gs :: [Type -> Type]) :: Constraint where ... Source #

Equations

Compatible '[] '[] = () 
Compatible '[] (f ': gs) = () 
Compatible (f ': fs) '[] = () 
Compatible (f ': fs) (f ': gs) = Compatible fs gs 
Compatible a b = TypeError ((Text "Mismatched dimensions!" :$$: ((Text "The dimension " :<>: ShowType a) :<>: Text " can't be aligned with")) :$$: (Text "the dimension " :<>: ShowType b)) 

elements :: Shapely fs => Hyper fs a -> [a] Source #

data Flat fs a where Source #

Constructors

Flat :: Shapely fs => Vector a -> Flat fs a 

Instances

Functor (Flat fs) Source # 

Methods

fmap :: (a -> b) -> Flat fs a -> Flat fs b #

(<$) :: a -> Flat fs b -> Flat fs a #

Show a => Show (Flat fs a) Source # 

Methods

showsPrec :: Int -> Flat fs a -> ShowS #

show :: Flat fs a -> String #

showList :: [Flat fs a] -> ShowS #

flatten :: Shapely fs => Hyper fs a -> Flat fs a Source #

data Sparse fs a where Source #

Constructors

Sparse :: Shapely fs => a -> IntMap a -> Sparse fs a 

unsparse :: forall fs a. Shapely fs => Sparse fs a -> Flat fs a Source #

type Matrix n m v = Vector n (Vector m v) Source #