Copyright | (c) Armando Santos 2019-2020 |
---|---|
Maintainer | armandoifsantos@gmail.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
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
- data Matrix e cols rows where
- type Countable a = KnownNat (Count a)
- type CountableDims a b = (Countable a, Countable b)
- type CountableN a = KnownNat (Count (Normalize a))
- type CountableDimsN a b = (CountableN a, CountableN b)
- type FL a b = FromLists a b
- type FLN a b = FromLists (Normalize a) (Normalize b)
- type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
- type Trivial a = FromNat (Count a) ~ a
- 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)
- type family FromNat (n :: Nat) :: Type where ...
- type family Count (d :: Type) :: Nat where ...
- type family Normalize (d :: Type) :: Type where ...
- class FromLists cols rows
- fromLists :: FromLists cols rows => [[e]] -> Matrix e cols rows
- toLists :: Matrix e cols rows -> [[e]]
- toList :: Matrix e cols rows -> [e]
- 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)
- matrixBuilder' :: forall e cols rows. (FL cols rows, CountableDims cols rows) => ((Int, Int) -> e) -> Matrix e cols rows
- row :: FL cols () => [e] -> Matrix e cols ()
- col :: FL () rows => [e] -> Matrix e () rows
- zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
- ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
- bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
- constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
- columns :: forall e cols rows. Countable cols => Matrix e cols rows -> Int
- columns' :: Matrix e cols rows -> Int
- rows :: forall e cols rows. Countable rows => Matrix e cols rows -> Int
- rows' :: Matrix e cols rows -> Int
- tr :: Matrix e cols rows -> Matrix e rows cols
- (.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
- (./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
- select :: (Num e, FL b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
- 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
- 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
- abideJF :: Matrix e cols rows -> Matrix e cols rows
- abideFJ :: Matrix e cols rows -> Matrix e cols rows
- zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
- (===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
- p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
- p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
- (|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
- i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
- i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
- (-|-) :: 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)
- (><) :: 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))
- fstM :: forall e m k. (Num e, CountableDims k m, FL (Normalize (m, k)) m, CountableN (m, k)) => Matrix e (Normalize (m, k)) m
- sndM :: forall e m k. (Num e, CountableDims k m, FL (Normalize (m, k)) k, CountableN (m, k)) => Matrix e (Normalize (m, k)) k
- 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))
- iden :: forall e cols. (Num e, FL cols cols, Countable cols) => Matrix e cols cols
- comp :: Num e => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
- fromF' :: forall a b cols rows e. (Liftable e a b, CountableDims cols rows, FL rows cols) => (a -> b) -> Matrix e cols rows
- fromF :: forall a b e. (Liftable e a b, CountableDimsN a b, FLN b a) => (a -> b) -> Matrix e (Normalize a) (Normalize b)
- pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
- prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
- toBool :: (Num e, Eq e) => e -> Bool
- fromBool :: Bool -> Natural 0 1
- compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
- divR :: Relation b c -> Relation b a -> Relation a c
- divL :: Relation c b -> Relation a b -> Relation a c
- divS :: Relation c a -> Relation b a -> Relation c b
- fromFRel :: forall a b. (Liftable Boolean a b, CountableDimsN a b, FL (Normalize b) (Normalize a)) => (a -> b) -> Relation (Normalize a) (Normalize b)
- fromFRel' :: forall a b cols rows. (Liftable Boolean a b, CountableDims cols rows, FL rows cols) => (a -> b) -> Relation cols rows
- 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)
- negateM :: Relation cols rows -> Relation cols rows
- orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
- andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
- subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
Documentation
This definition makes use of the fact that Void
is
isomorphic to 0 and '()' to 1 and captures matrix
dimensions as stacks of Either
s.
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.
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. |
Eq e => Eq (Matrix e cols rows) Source # | |
Num e => Num (Matrix e cols rows) Source # | |
Defined in LAoP.Matrix.Internal (+) :: 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 # | |
Defined in LAoP.Matrix.Internal 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 # | |
NFData e => NFData (Matrix e cols rows) Source # | |
Defined in LAoP.Matrix.Internal | |
type Object (Matrix e) a Source # | |
Defined in LAoP.Matrix.Internal |
Constraint type aliases
type Countable a = KnownNat (Count a) Source #
Constraint type synonyms to keep the type signatures less convoluted
type CountableDims a b = (Countable a, Countable b) Source #
type CountableDimsN a b = (CountableN a, CountableN b) Source #
Primitives
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.
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.
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
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.
Instances
FromLists () () Source # | |
Defined in LAoP.Matrix.Internal | |
FromLists () rows => FromLists () (Either () rows) Source # | |
(FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) Source # | |
FromLists cols () => FromLists (Either () cols) () Source # | |
(FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () Source # | |
(FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (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.
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.
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' :: 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' :: 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
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"
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
compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows Source #
Matrix relational composition.
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