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
- 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)
- type family FromNat (n :: Nat) :: Type where ...
- type family Count (d :: Type) :: Nat where ...
- type family Normalize (d :: Type) :: Type where ...
- class FromLists e cols rows
- fromLists :: FromLists e cols rows => [[e]] -> Matrix e cols rows
- toLists :: Matrix e cols rows -> [[e]]
- toList :: Matrix e cols rows -> [e]
- matrixBuilder :: forall e cols rows. (FromLists e cols rows, CountableDimensions cols rows) => ((Int, Int) -> e) -> Matrix e cols rows
- row :: FromLists e cols () => [e] -> Matrix e cols ()
- col :: FromLists e () rows => [e] -> Matrix e () rows
- zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
- ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
- bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
- constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
- columns :: forall e cols rows. Countable cols => Matrix e cols rows -> Int
- rows :: forall e cols rows. Countable rows => Matrix e cols rows -> Int
- tr :: Matrix e cols rows -> Matrix e rows cols
- select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
- 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
- abideJS :: Matrix e cols rows -> Matrix e cols rows
- abideSJ :: Matrix e cols rows -> Matrix e cols rows
- (===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
- p1 :: forall e m n. (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
- p2 :: forall e m n. (Num e, CountableDimensions n m, FromLists e m n, FromLists e 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, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
- i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
- (-|-) :: 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)
- (><) :: 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))
- 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
- 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
- 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))
- identity :: (Num e, FromLists e 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, CountableDimensions cols rows, FromLists e rows cols) => (a -> b) -> Matrix e cols rows
- fromF' :: forall a b e. (Liftable e a b, CountableDimensionsN a b, FromListsN e b a) => (a -> b) -> Matrix e (Normalize a) (Normalize b)
- pretty :: (Countable cols, Show e) => Matrix e cols rows -> String
- prettyPrint :: (Countable cols, 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 cols rows. (Liftable Boolean a b, CountableDimensions cols rows, FromLists Boolean rows cols) => (a -> b) -> Relation cols rows
- fromFRel' :: forall a b. (Liftable Boolean a b, CountableDimensionsN a b, FromLists Boolean (Normalize b) (Normalize a)) => (a -> b) -> Relation (Normalize a) (Normalize b)
- 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)
- 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.
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 Please use |
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 |
Primitives
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.
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 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.
Instances
FromLists e () () Source # | |
Defined in LAoP.Matrix.Internal | |
FromLists e Void Void Source # | |
(FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) Source # | |
FromLists e () rows => FromLists e () (Either () rows) Source # | |
(FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () Source # | |
FromLists e cols () => FromLists 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 # | |
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.
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.
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
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"
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
prettyPrint :: (Countable cols, 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 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