laop-0.1.1.0: Matrix programming library

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

One :: e -> Matrix e () () 
Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows 
Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b) 
Instances
Num e => Category (Matrix e) Source #

It is possible to implement a constrained version of the category type class.

Instance details

Defined in LAoP.Matrix.Internal

Associated Types

type Object (Matrix e) o :: Constraint Source #

Methods

id :: Object (Matrix e) a => Matrix e a a Source #

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

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 -> () #

type Object (Matrix e) a Source # 
Instance details

Defined in LAoP.Matrix.Internal

type Object (Matrix e) a = (FL a a, Countable a)

Constraint type aliases

type Countable a = KnownNat (Count a) Source #

Constraint type synonyms to keep the type signatures less convoluted

type FL a b = FromLists a b Source #

type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e) Source #

type Trivial a = FromNat (Count a) ~ a Source #

Primitives

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

Unit matrix constructor

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

Matrix Join constructor

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

Matrix Fork 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 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 (Either a b) = Either (Normalize a) (Normalize b) 
Normalize d = FromNat (Count d) 

Matrix construction and conversion

class FromLists 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 very much like an inductive definition but on types.

Minimal complete definition

fromLists

Instances
FromLists () () Source # 
Instance details

Defined in LAoP.Matrix.Internal

Methods

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

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

Defined in LAoP.Matrix.Internal

Methods

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

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

Defined in LAoP.Matrix.Internal

Methods

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

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

Defined in LAoP.Matrix.Internal

Methods

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

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

Defined in LAoP.Matrix.Internal

Methods

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

(FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (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 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 a b. (FLN a b, CountableN b, Enum a, Enum b, Bounded a, Bounded b, Eq a, CountableDimsN a b) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b) Source #

Matrix builder function. Constructs a matrix provided with a construction function that operates with arbitrary types.

matrixBuilder' :: forall e cols rows. (FL cols rows, CountableDims cols rows) => ((Int, Int) -> e) -> Matrix e cols rows Source #

Matrix builder function. Constructs a matrix provided with a construction function that operates with indices.

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

Constructs a row vector matrix

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

Constructs a column vector matrix

zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows Source #

The zero matrix. A matrix wholly filled with zeros.

ones :: (Num e, FL cols rows, CountableDims 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, FL cols (), Countable cols) => Matrix e cols () Source #

The T (Top) row vector matrix.

constant :: (Num e, FL cols rows, CountableDims 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 constraint is needed in order to obtain the dimensions in constant time. For a version that doesn't require the constraint see columns'.

columns' :: Matrix e cols rows -> Int Source #

Obtain the number of columns in an inefficient manner, but without any constraints.

For a more efficient version see columns.

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

Obtain the number of rows.

NOTE: The KnownNat constraint is needed in order to obtain the dimensions in constant time. For a version that doesn't require the constraint see rows'.

rows' :: Matrix e cols rows -> Int Source #

Obtain the number of rows in an inefficient manner, but without any constraints.

For a more efficient version see rows.

Matrix Transposition

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

Matrix transposition.

Scalar multiplication/division of matrices

(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows infixl 7 Source #

Scalar multiplication of matrices.

(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows infixl 7 Source #

Scalar multiplication of matrices.

Selective operator

select :: (Num e, FL 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.

branch :: (Num e, CountableDims a b, CountableDims c (Either b c), FL c b, FL a b, FL a a, FL b b, FL c c, FL b a, FL b c, FL (Either b c) b, FL (Either b c) c) => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c Source #

McCarthy's Conditional

cond :: (Trivial cols, Countable cols, FL () cols, FL cols (), FL 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"

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

Matrix "abiding" following the 'Join'-'Fork' exchange law.

Law:

Join (Fork a c) (Fork b d) == Fork (Join a b) (Join c d)

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

Matrix "abiding" followin the 'Fork'-'Join' abide law.

Fork (Join a b) (Join c d) == Join (Fork a c) (Fork b d)

Zip matrices

zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows Source #

Zip two matrices with a given binary function

Biproduct approach

Fork

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

Matrix Fork constructor

Projections

p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m Source #

Biproduct first component projection

p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n Source #

Biproduct second component projection

Join

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

Matrix Join constructor

Injections

i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n) Source #

Biproduct first component injection

i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n) Source #

Biproduct second component injection

Bifunctors

(-|-) :: forall e n k m j. (Num e, CountableDims j k, FL k k, FL j k, FL k j, FL 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, CountableDims m n, CountableDims p q, CountableDimsN (m, n) (p, q), FL (Normalize (m, n)) m, FL (Normalize (m, n)) n, FL (Normalize (p, q)) p, FL (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

fstM :: forall e m k. (Num e, CountableDims k m, FL (Normalize (m, k)) m, CountableN (m, k)) => Matrix e (Normalize (m, k)) m Source #

Khatri Rao product first component projection matrix.

sndM :: forall e m k. (Num e, CountableDims k m, FL (Normalize (m, k)) k, CountableN (m, k)) => Matrix e (Normalize (m, k)) k Source #

Khatri Rao product second component projection matrix.

Matrix pairing

kr :: forall e cols a b. (Num e, CountableDims a b, CountableN (a, b), FL (Normalize (a, b)) a, FL (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:

           | fstM . kr a b == a
kr a b ==> |
           | sndM . kr 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.

iden :: forall e cols. (Num e, FL cols cols, Countable cols) => Matrix e cols cols Source #

iden 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, CountableDims cols rows, FL 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, CountableDimsN a b, FLN 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 :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String Source #

Matrix pretty printer

prettyPrint :: (CountableDims cols rows, 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. (Liftable Boolean a b, CountableDimsN a b, FL (Normalize b) (Normalize a)) => (a -> b) -> Relation (Normalize a) (Normalize b) Source #

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

fromFRel' :: forall a b cols rows. (Liftable Boolean a b, CountableDims cols rows, FL 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.

toRel :: forall a b. (Bounded a, Bounded b, Enum a, Enum b, Eq b, CountableDimsN a b, FLN 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