laop-0.1.0.3

Copyright(c) Armando Santos 2019-2020
Maintainerarmandoifsantos@gmail.com
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

LAoP.Matrix.Internal

Contents

Description

The LAoP discipline generalises relations and functions treating them as Boolean matrices and in turn consider these as arrows.

LAoP is a library for algebraic (inductive) construction and manipulation of matrices in Haskell. See my Msc Thesis for the motivation behind the library, the underlying theory, and implementation details.

This module offers many of the combinators mentioned in the work of Macedo (2012) and Oliveira (2012).

This is an Internal module and it is no supposed to be imported.

Synopsis

Documentation

This definition makes use of the fact that Void is isomorphic to 0 and '()' to 1 and captures matrix dimensions as stacks of Eithers.

There exists two type families that make it easier to write matrix dimensions: FromNat and Count. This approach leads to a very straightforward implementation of LAoP combinators.

Type safe matrix representation

data Matrix e cols rows where Source #

LAoP (Linear Algebra of Programming) Inductive Matrix definition.

Constructors

Empty :: Matrix e Void Void 
One :: e -> Matrix e () () 
Junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows 
Split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b) 
Instances
Num e => Category (Matrix e :: Type -> Type -> Type) Source #

It isn't possible to implement the Category function so it's implementation is undefined. However comp can be and this partial class implementation exists just to make the code more readable.

Please use identity instead.

Instance details

Defined in LAoP.Matrix.Internal

Methods

id :: Matrix e a a #

(.) :: Matrix e b c -> Matrix e a b -> Matrix e a c #

Eq e => Eq (Matrix e cols rows) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

(==) :: Matrix e cols rows -> Matrix e cols rows -> Bool #

(/=) :: Matrix e cols rows -> Matrix e cols rows -> Bool #

Num e => Num (Matrix e cols rows) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

(+) :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows #

(-) :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows #

(*) :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows #

negate :: Matrix e cols rows -> Matrix e cols rows #

abs :: Matrix e cols rows -> Matrix e cols rows #

signum :: Matrix e cols rows -> Matrix e cols rows #

fromInteger :: Integer -> Matrix e cols rows #

Ord e => Ord (Matrix e cols rows) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

compare :: Matrix e cols rows -> Matrix e cols rows -> Ordering #

(<) :: Matrix e cols rows -> Matrix e cols rows -> Bool #

(<=) :: Matrix e cols rows -> Matrix e cols rows -> Bool #

(>) :: Matrix e cols rows -> Matrix e cols rows -> Bool #

(>=) :: Matrix e cols rows -> Matrix e cols rows -> Bool #

max :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows #

min :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows #

Show e => Show (Matrix e cols rows) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

showsPrec :: Int -> Matrix e cols rows -> ShowS #

show :: Matrix e cols rows -> String #

showList :: [Matrix e cols rows] -> ShowS #

NFData e => NFData (Matrix e cols rows) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

rnf :: Matrix e cols rows -> () #

Primitives

empty :: Matrix e Void Void Source #

Empty matrix constructor

one :: e -> Matrix e () () Source #

Unit matrix constructor

junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows Source #

Matrix Junc constructor

split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b) Source #

Matrix Split constructor

Auxiliary type families

type family FromNat (n :: Nat) :: Type where ... Source #

Type family that computes of a given type dimension from a given natural

Thanks to Li-Yao Xia this type family is super fast.

Equations

FromNat 0 = Void 
FromNat 1 = () 
FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2)) 

type family Count (d :: Type) :: Nat where ... Source #

Type family that computes the cardinality of a given type dimension.

It can also count the cardinality of custom types that implement the Generic instance.

Equations

Count (Natural n m) = (m - n) + 1 
Count (List a) = (^) 2 (Count a) 
Count (Either a b) = (+) (Count a) (Count b) 
Count (a, b) = * (Count a) (Count b) 
Count (a -> b) = (^) (Count b) (Count a) 
Count (M1 _ _ f p) = Count (f p) 
Count (K1 _ _ _) = 1 
Count (V1 _) = 0 
Count (U1 _) = 1 
Count ((:*:) a b p) = Count (a p) * Count (b p) 
Count ((:+:) a b p) = Count (a p) + Count (b p) 
Count d = Count (Rep d R) 

type family Normalize (d :: Type) :: Type where ... Source #

Type family that normalizes the representation of a given data structure

Equations

Normalize d = FromNat (Count d) 

Matrix construction and conversion

class FromLists e cols rows Source #

Type class for defining the fromList conversion function.

Given that it is not possible to branch on types at the term level type classes are needed bery much like an inductive definition but on types.

Minimal complete definition

fromLists

Instances
FromLists e () () Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

fromLists :: [[e]] -> Matrix e () () Source #

FromLists e Void Void Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

fromLists :: [[e]] -> Matrix e Void Void Source #

(FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

fromLists :: [[e]] -> Matrix e () (Either a b) Source #

FromLists e () rows => FromLists e () (Either () rows) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

fromLists :: [[e]] -> Matrix e () (Either () rows) Source #

(FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

fromLists :: [[e]] -> Matrix e (Either a b) () Source #

FromLists e cols () => FromLists e (Either () cols) () Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

fromLists :: [[e]] -> Matrix e (Either () cols) () Source #

(FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

fromLists :: [[e]] -> Matrix e (Either a b) (Either c d) Source #

fromLists :: FromLists e cols rows => [[e]] -> Matrix e cols rows Source #

Build a matrix out of a list of list of elements. Throws a runtime error if the dimensions do not match.

toLists :: Matrix e cols rows -> [[e]] Source #

Converts a matrix to a list of lists of elements.

toList :: Matrix e cols rows -> [e] Source #

Converts a matrix to a list of elements.

matrixBuilder :: forall e cols rows. (FromLists e cols rows, CountableDimensions cols rows) => ((Int, Int) -> e) -> Matrix e cols rows Source #

Matrix builder function. Constructs a matrix provided with a construction function.

row :: FromLists e cols () => [e] -> Matrix e cols () Source #

Constructs a row vector matrix

col :: FromLists e () rows => [e] -> Matrix e () rows Source #

Constructs a column vector matrix

zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows Source #

The zero matrix. A matrix wholly filled with zeros.

ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows Source #

The ones matrix. A matrix wholly filled with ones.

Also known as T (Top) matrix.

bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols () Source #

The T (Top) row vector matrix.

constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows Source #

The constant matrix constructor. A matrix wholly filled with a given value.

Misc

Get dimensions

columns :: forall e cols rows. Countable cols => Matrix e cols rows -> Int Source #

Obtain the number of columns.

NOTE: The KnownNat constaint is needed in order to obtain the dimensions in constant time.

TODO: A columns function that does not need the KnownNat constraint in exchange for performance.

rows :: forall e cols rows. Countable rows => Matrix e cols rows -> Int Source #

Obtain the number of rows.

NOTE: The KnownNat constaint is needed in order to obtain the dimensions in constant time.

TODO: A rows function that does not need the KnownNat constraint in exchange for performance.

Matrix Transposition

tr :: Matrix e cols rows -> Matrix e rows cols Source #

Matrix transposition.

Selective operator

select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b Source #

Selective functors select operator equivalent inspired by the ArrowMonad solution presented in the paper.

McCarthy's Conditional

cond :: (cols ~ Normalize cols, Countable cols, FromLists e () cols, FromLists e cols (), FromLists e cols cols, Bounded a, Enum a, Num e, Ord e) => (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows Source #

McCarthy's Conditional expresses probabilistic choice.

Matrix "abiding"

abideJS :: Matrix e cols rows -> Matrix e cols rows Source #

Matrix "abiding" followin the 'Junc'-'Split' abide law.

Law:

Junc (Split a c) (Split b d) == Split (Junc a b) (Junc c d)

abideSJ :: Matrix e cols rows -> Matrix e cols rows Source #

Matrix "abiding" followin the 'Split'-'Junc' abide law.

Split (Junc a b) (Junc c d) == Junc (Split a c) (Split b d)

Biproduct approach

Split

(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b) infixl 2 Source #

Matrix Split constructor

Projections

p1 :: forall e m n. (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m Source #

Biproduct first component projection

p2 :: forall e m n. (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n Source #

Biproduct second component projection

Junc

(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows infixl 3 Source #

Matrix Junc constructor

Injections

i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n) Source #

Biproduct first component injection

i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n) Source #

Biproduct second component injection

Bifunctors

(-|-) :: forall e n k m j. (Num e, CountableDimensions j k, FromLists e k k, FromLists e j k, FromLists e k j, FromLists e j j) => Matrix e n k -> Matrix e m j -> Matrix e (Either n m) (Either k j) infixl 5 Source #

Matrix coproduct functor also known as matrix direct sum.

(><) :: forall e m p n q. (Num e, CountableDimensions m n, CountableDimensions p q, CountableDimensionsN (m, n) (p, q), FromLists e (Normalize (m, n)) m, FromLists e (Normalize (m, n)) n, FromLists e (Normalize (p, q)) p, FromLists e (Normalize (p, q)) q) => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q)) infixl 4 Source #

Matrix product functor also known as kronecker product

Applicative matrix combinators

Note that given the restrictions imposed it is not possible to implement the standard type classes present in standard Haskell. *** Matrix pairing projections

kp1 :: forall e m k. (Num e, CountableDimensions k m, FromLists e (Normalize (m, k)) m, CountableN (m, k)) => Matrix e (Normalize (m, k)) m Source #

Khatri Rao product first component projection matrix.

kp2 :: forall e m k. (Num e, CountableDimensions k m, FromLists e (Normalize (m, k)) k, CountableN (m, k)) => Matrix e (Normalize (m, k)) k Source #

Khatri Rao product second component projection matrix.

Matrix pairing

khatri :: forall e cols a b. (Num e, CountableDimensions a b, CountableN (a, b), FromLists e (Normalize (a, b)) a, FromLists e (Normalize (a, b)) b) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b)) Source #

Khatri Rao Matrix product also known as matrix pairing.

NOTE: That this is not a true categorical product, see for instance:

               | kp1 . khatri a b == a 
khatri a b ==> |
               | kp2 . khatri a b == b

Emphasis on the implication symbol.

Matrix composition and lifting

Arrow matrix combinators

Note that given the restrictions imposed it is not possible to implement the standard type classes present in standard Haskell.

identity :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols Source #

Identity matrix.

comp :: Num e => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows Source #

Matrix composition. Equivalent to matrix-matrix multiplication.

This definition takes advantage of divide-and-conquer and fusion laws from LAoP.

fromF :: forall a b cols rows e. (Liftable e a b, CountableDimensions cols rows, FromLists e rows cols) => (a -> b) -> Matrix e cols rows Source #

Lifts functions to matrices with arbitrary dimensions.

NOTE: Be careful to not ask for a matrix bigger than the cardinality of types a or b allows.

fromF' :: forall a b e. (Liftable e a b, CountableDimensionsN a b, FromListsN e b a) => (a -> b) -> Matrix e (Normalize a) (Normalize b) Source #

Lifts functions to matrices with dimensions matching a and b cardinality's.

Matrix printing

pretty :: (Countable cols, Show e) => Matrix e cols rows -> String Source #

Matrix pretty printer

prettyPrint :: (Countable cols, Show e) => Matrix e cols rows -> IO () Source #

Matrix pretty printer

Other

toBool :: (Num e, Eq e) => e -> Bool Source #

Helper conversion function

fromBool :: Bool -> Natural 0 1 Source #

Helper conversion function

compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows Source #

Matrix relational composition.

divR :: Relation b c -> Relation b a -> Relation a c Source #

Matrix relational right division

divL :: Relation c b -> Relation a b -> Relation a c Source #

Matrix relational left division

divS :: Relation c a -> Relation b a -> Relation c b Source #

Matrix relational symmetric division

fromFRel :: forall a b cols rows. (Liftable Boolean a b, CountableDimensions cols rows, FromLists Boolean rows cols) => (a -> b) -> Relation cols rows Source #

Lifts functions to relations with arbitrary dimensions.

NOTE: Be careful to not ask for a relation bigger than the cardinality of types a or b allows.

fromFRel' :: forall a b. (Liftable Boolean a b, CountableDimensionsN a b, FromLists Boolean (Normalize b) (Normalize a)) => (a -> b) -> Relation (Normalize a) (Normalize b) Source #

Lifts functions to relations with dimensions matching a and b cardinality's.

toRel :: forall a b. (Bounded a, Bounded b, Enum a, Enum b, Eq b, CountableDimensionsN a b, FromListsN Boolean b a) => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b) Source #

Lifts a relation function to a Boolean Matrix

negateM :: Relation cols rows -> Relation cols rows Source #

Relational negation

orM :: Relation cols rows -> Relation cols rows -> Relation cols rows Source #

Relational addition

andM :: Relation cols rows -> Relation cols rows -> Relation cols rows Source #

Relational multiplication

subM :: Relation cols rows -> Relation cols rows -> Relation cols rows Source #

Relational subtraction