on master help sign in
Refactored Alternative Internal module and Type module
d2fbcb5Armando Santos
src/LAoP/Matrix/Alternative.hs+60−55
@@ -157,7 +157,10 @@ module LAoP.Matrix.Alternative
157 negateM,
157 negateM,
158 orM,
158 orM,
159 andM,
159 andM,
160− subM
160+ subM,
161+
162+ -- * Semantics
163+ Construct
161 )
164 )
162 where
165 where
163
166
@@ -223,8 +226,8 @@ type family Normalize (d :: Type) :: Type where
223
226
224-- | Constraint type synonyms to keep the type signatures less convoluted
227-- | Constraint type synonyms to keep the type signatures less convoluted
225type Countable a = KnownNat (Count a)
228type Countable a = KnownNat (Count a)
226−type CountableN a = KnownNat (Count (Normalize a))
227type CountableDimensions a b = (Countable a, Countable b)
229type CountableDimensions a b = (Countable a, Countable b)
230+type Liftable a b = (Eq b, Constructable a b, Enumerable a)
228type Enumerable a = (Enum a, Bounded a)
231type Enumerable a = (Enum a, Bounded a)
229type ConstructNorm a = (Enum a, Enum (Normalize a))
232type ConstructNorm a = (Enum a, Enum (Normalize a))
230type ConstructableNorm a b = (ConstructNorm a, ConstructNorm b)
233type ConstructableNorm a b = (ConstructNorm a, ConstructNorm b)
@@ -232,15 +235,15 @@ type ConstructN a = Construct (Normalize a)
232type Constructable a b = (Construct a, Construct b)
235type Constructable a b = (Construct a, Construct b)
233type ConstructableN a b = (Construct (Normalize a), Construct (Normalize b))
236type ConstructableN a b = (Construct (Normalize a), Construct (Normalize b))
234
237
235−
236-- | It isn't possible to implement the 'id' function so it's
238-- | It isn't possible to implement the 'id' function so it's
237-- implementation is 'undefined'. However 'comp' can be and this partial
239-- implementation is 'undefined'. However 'comp' can be and this partial
238-- class implementation exists just to make the code more readable.
240-- class implementation exists just to make the code more readable.
239--
241--
240-- Please use 'iden' instead.
242-- Please use 'iden' instead.
241instance (Num e) => Category (Matrix e) where
243instance (Num e) => Category (Matrix e) where
242− id = undefined
244+ type Object (Matrix e) a = Liftable a a
245+ id = iden
243− (.) = comp
246+ (.) = comp
244
247
245instance NFData e => NFData (Matrix e cols rows) where
248instance NFData e => NFData (Matrix e cols rows) where
246 rnf Empty = ()
249 rnf Empty = ()
@@ -314,52 +317,6 @@ infixl 2 ===
314
317
315-- Construction
318-- Construction
316
319
317−-- | Type class for defining the 'fromList' conversion function.
318−--
319−-- Given that it is not possible to branch on types at the term level type
320−-- classes are needed very much like an inductive definition but on types.
321−class FromLists e cols rows where
322− -- | Build a matrix out of a list of list of elements. Throws a runtime
323− -- error if the dimensions do not match.
324− fromLists :: [[e]] -> Matrix e cols rows
325−
326−instance FromLists e Void Void where
327− fromLists [] = Empty
328− fromLists _ = error "Wrong dimensions"
329−
330−instance {-# OVERLAPPING #-} FromLists e () () where
331− fromLists [[e]] = One e
332− fromLists _ = error "Wrong dimensions"
333−
334−instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
335− fromLists [h : t] = Join (One h) (fromLists [t])
336− fromLists _ = error "Wrong dimensions"
337−
338−instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
339− fromLists [l] =
340− let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
341− in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
342− fromLists _ = error "Wrong dimensions"
343−
344−instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
345− fromLists ([h] : t) = Fork (One h) (fromLists t)
346− fromLists _ = error "Wrong dimensions"
347−
348−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
349− fromLists l@([_] : _) =
350− let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
351− in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
352− fromLists _ = error "Wrong dimensions"
353−
354−instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) where
355− fromLists l@(h : t) =
356− let lh = length h
357− rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
358− condition = all ((== lh) . length) t
359− in if lh > 0 && condition
360− then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
361− else error "Not all rows have the same length"
362−
363-- | Constructs a column vector matrix
320-- | Constructs a column vector matrix
364colL :: (FromLists e () rows) => [e] -> Matrix e () rows
321colL :: (FromLists e () rows) => [e] -> Matrix e () rows
365colL = fromLists . map (: [])
322colL = fromLists . map (: [])
@@ -409,13 +366,13 @@ bang :: (Num e, Constructable cols rows, Enumerable cols) => Matrix e cols ()
409bang = ones
366bang = ones
410
367
411-- | Point constant matrix
368-- | Point constant matrix
412−point :: (Num e, Eq a, Construct a) => a -> Matrix e () a
369+point :: (Num e, Liftable () a) => a -> Matrix e () a
413point = fromF . const
370point = fromF . const
414
371
415-- iden Matrix
372-- iden Matrix
416
373
417-- | iden matrix.
374-- | iden matrix.
418−iden :: (Num e, Eq cols, Construct cols, Enumerable cols) => Matrix e cols cols
375+iden :: (Num e, Liftable cols cols) => Matrix e cols cols
419iden = fromF id
376iden = fromF id
420
377
421-- Matrix composition (MMM)
378-- Matrix composition (MMM)
@@ -646,7 +603,7 @@ tr (Fork a b) = Join (tr a) (tr b)
646
603
647-- | Selective functors 'select' operator equivalent inspired by the
604-- | Selective functors 'select' operator equivalent inspired by the
648-- ArrowMonad solution presented in the paper.
605-- ArrowMonad solution presented in the paper.
649−select :: (Num e) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
606+select :: (Num e, Liftable b b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
650select (Fork a b) y = y . a + b -- Divide-and-conquer law
607select (Fork a b) y = y . a + b -- Divide-and-conquer law
651select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
608select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
652select m y = join y id . m
609select m y = join y id . m
@@ -896,7 +853,7 @@ matrixBuilder :: (Num e, Constructable a b, Enumerable a) => ((a, b) -> e) -> Ma
896matrixBuilder f = linearMap $ \v -> Vector $ \b -> dot v $ Vector $ \a -> f (a, b)
853matrixBuilder f = linearMap $ \v -> Vector $ \b -> dot v $ Vector $ \a -> f (a, b)
897
854
898-- | Lifts functions to matrices.
855-- | Lifts functions to matrices.
899−fromF :: (Num e, Eq b, Constructable a b, Enumerable a) => (a -> b) -> Matrix e a b
856+fromF :: (Num e, Liftable a b) => (a -> b) -> Matrix e a b
900fromF f = matrixBuilder (\(a, b) -> if f a == b then 1 else 0)
857fromF f = matrixBuilder (\(a, b) -> if f a == b then 1 else 0)
901
858
902-------------------------------- Deconstruction --------------------------------
859-------------------------------- Deconstruction --------------------------------
@@ -947,3 +904,51 @@ matrixBuilderN f = linearMapN $ \v -> Vector $ \b -> dot v $ Vector $ \a -> f (a
947fromFN :: (Num e, Eq b, ConstructableN a b, ConstructableNorm a b, Enumerable a)
904fromFN :: (Num e, Eq b, ConstructableN a b, ConstructableNorm a b, Enumerable a)
948 => (a -> b) -> Matrix e (Normalize a) (Normalize b)
905 => (a -> b) -> Matrix e (Normalize a) (Normalize b)
949fromFN f = matrixBuilderN (\(a, b) -> if f a == b then 1 else 0)
906fromFN f = matrixBuilderN (\(a, b) -> if f a == b then 1 else 0)
907+
908+------------------------------- Unsafe fromLists matrix construction -------------------------------
909+
910+-- | Type class for defining the 'fromList' conversion function.
911+--
912+-- Given that it is not possible to branch on types at the term level type
913+-- classes are needed very much like an inductive definition but on types.
914+class FromLists e cols rows where
915+ -- | Build a matrix out of a list of list of elements. Throws a runtime
916+ -- error if the dimensions do not match.
917+ fromLists :: [[e]] -> Matrix e cols rows
918+
919+instance FromLists e Void Void where
920+ fromLists [] = Empty
921+ fromLists _ = error "Wrong dimensions"
922+
923+instance {-# OVERLAPPING #-} FromLists e () () where
924+ fromLists [[e]] = One e
925+ fromLists _ = error "Wrong dimensions"
926+
927+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
928+ fromLists [h : t] = Join (One h) (fromLists [t])
929+ fromLists _ = error "Wrong dimensions"
930+
931+instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
932+ fromLists [l] =
933+ let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
934+ in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
935+ fromLists _ = error "Wrong dimensions"
936+
937+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
938+ fromLists ([h] : t) = Fork (One h) (fromLists t)
939+ fromLists _ = error "Wrong dimensions"
940+
941+instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
942+ fromLists l@([_] : _) =
943+ let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
944+ in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
945+ fromLists _ = error "Wrong dimensions"
946+
947+instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) where
948+ fromLists l@(h : t) =
949+ let lh = length h
950+ rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
951+ condition = all ((== lh) . length) t
952+ in if lh > 0 && condition
953+ then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
954+ else error "Not all rows have the same length"
src/LAoP/Matrix/Type.hs+188−194
@@ -87,10 +87,9 @@ module LAoP.Matrix.Type
87 fromLists,
87 fromLists,
88 toLists,
88 toLists,
89 toList,
89 toList,
90− matrixBuilder',
91 matrixBuilder,
90 matrixBuilder,
92− row,
91+ rowL,
93− col,
92+ colL,
94 zeros,
93 zeros,
95 ones,
94 ones,
96 bang,
95 bang,
@@ -125,7 +124,7 @@ module LAoP.Matrix.Type
125 (./),
124 (./),
126
125
127 -- ** McCarthy's Conditional
126 -- ** McCarthy's Conditional
128− cond,
127+ -- cond,
129
128
130 -- ** Matrix "abiding"
129 -- ** Matrix "abiding"
131 abideJF,
130 abideJF,
@@ -168,7 +167,6 @@ module LAoP.Matrix.Type
168 -- implement the standard type classes present in standard Haskell.
167 -- implement the standard type classes present in standard Haskell.
169 iden,
168 iden,
170 comp,
169 comp,
171− fromF',
172 fromF,
170 fromF,
173
171
174 -- * Relation
172 -- * Relation
@@ -181,12 +179,11 @@ module LAoP.Matrix.Type
181where
179where
182
180
183import Data.Void
181import Data.Void
184−import Data.Proxy
185import Data.Kind
182import Data.Kind
186import GHC.TypeLits
183import GHC.TypeLits
187import Control.DeepSeq
184import Control.DeepSeq
188import LAoP.Utils
185import LAoP.Utils
189−import qualified LAoP.Matrix.Internal as I
186+import qualified LAoP.Matrix.Alternative as I
190import Prelude hiding (id, (.))
187import Prelude hiding (id, (.))
191
188
192newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
189newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
@@ -198,27 +195,30 @@ type CountableDimensions a b = (Countable a, Countable b)
198type CountableN a = KnownNat (I.Count (I.Normalize a))
195type CountableN a = KnownNat (I.Count (I.Normalize a))
199type CountableDimensionsN a b = (CountableN a, CountableN b)
196type CountableDimensionsN a b = (CountableN a, CountableN b)
200type FromListsN e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
197type FromListsN e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
201−type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
198+type Liftable a b = (Eq b, Enumerable a, Enum a, EnumN a, Enum b, EnumN b, ConstructableN a b)
202type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
199type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
203type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
200type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
204−type Trivial3 a = I.FromNat (I.Count (I.Normalize (I.Normalize a))) ~ I.Normalize (I.Normalize a)
205type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
201type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
202+type EqN a = Eq (I.Normalize a)
203+type EnumN a = Enum (I.Normalize a)
204+type Enumerable a = (Enum a, Bounded a)
205+type EnumerableN a = (Enum (I.Normalize a), Bounded (I.Normalize a))
206+type ConstructNorm a = (Enum a, Enum (I.Normalize a))
207+type ConstructN a = I.Construct (I.Normalize a)
208+type ConstructableN a b = (I.Construct (I.Normalize a), I.Construct (I.Normalize b))
206
209
207-- | It is possible to implement a constrained version of the category type
210-- | It is possible to implement a constrained version of the category type
208-- class.
211-- class.
209instance (Num e) => Category (Matrix e) where
212instance (Num e) => Category (Matrix e) where
210− type Object (Matrix e) a = (FromListsN e a a, CountableN a)
213+ type Object (Matrix e) a = (Num e, EqN a, ConstructN a, EnumerableN a)
211 id = iden
214 id = iden
212 (.) = comp
215 (.) = comp
213
216
214-- | Bifunctor equivalent function
217-- | Bifunctor equivalent function
215bimapM ::
218bimapM ::
216− ( Liftable e a b,
219+ ( Num e,
217− Liftable e c d,
220+ Liftable a b,
218− CountableDimensionsN a c,
221+ Liftable c d
219− CountableDimensionsN b d,
220− FromListsN e d c,
221− FromListsN e b a
222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
223bimapM f g m = fromF g . m . tr (fromF f)
223bimapM f g m = fromF g . m . tr (fromF f)
224
224
@@ -272,9 +272,8 @@ infixl 2 ===
272
272
273-- | Functor instance equivalent function
273-- | Functor instance equivalent function
274fmapM ::
274fmapM ::
275+ ( Num e,
275− ( Liftable e a b,
276+ Liftable a b
276− CountableDimensionsN a b,
277− FromListsN e b a
278 )
277 )
279 =>
278 =>
280 (a -> b) -> Matrix e c a -> Matrix e c b
279 (a -> b) -> Matrix e c a -> Matrix e c b
@@ -286,29 +285,18 @@ unitM = one 1
286
285
287-- | Applicative instance equivalent 'unit' function,
286-- | Applicative instance equivalent 'unit' function,
288multM ::
287multM ::
289− ( CountableDimensionsN a b,
290− CountableN (a, b),
291− Num e,
288+ ( Num e,
292− FromListsN e (a, b) a,
289+ FstM a b,
293− FromListsN e (a, b) b,
290+ SndM a b
294− TrivialP a b
295 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
291 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
296multM = kr
292multM = kr
297
293
298-- | Monad instance equivalent 'return' function,
294-- | Monad instance equivalent 'return' function,
299−returnM ::
295+returnM ::
300− forall e a .
301 ( Num e,
296 ( Num e,
302− Enum e,
297+ Liftable () a
303− Enum a,
304− FromListsN e () a,
305− Countable a
306 ) => a -> Matrix e One a
298 ) => a -> Matrix e One a
307−returnM a = col l
299+returnM = point
308− where
309− i = fromInteger $ natVal (Proxy :: Proxy (I.Count a))
310− x = fromEnum a
311− l = take x [0,0..] ++ [1] ++ take (i - x - 1) [0,0..]
312
300
313-- | Monad instance equivalent '(>>=)' function,
301-- | Monad instance equivalent '(>>=)' function,
314bindM :: (Num e) => Matrix e a b -> Matrix e b c -> Matrix e a c
302bindM :: (Num e) => Matrix e a b -> Matrix e b c -> Matrix e a c
@@ -321,63 +309,39 @@ bindM = flip comp
321fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
309fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
322fromLists = M . I.fromLists
310fromLists = M . I.fromLists
323
311
324−-- | Matrix builder function. Constructs a matrix provided with
325−-- a construction function that operates with indices.
326−matrixBuilder' ::
327− (FromListsN e cols rows, CountableDimensionsN cols rows )
328− => ((Int, Int) -> e) -> Matrix e cols rows
329−matrixBuilder' = M . I.matrixBuilder'
330−
331-- | Matrix builder function. Constructs a matrix provided with
312-- | Matrix builder function. Constructs a matrix provided with
332-- a construction function that operates with arbitrary types.
313-- a construction function that operates with arbitrary types.
333matrixBuilder ::
314matrixBuilder ::
334− ( FromListsN e a b,
315+ ( Num e,
335− Enum a,
316+ Enumerable a,
336− Enum b,
317+ Enum b,
337− Bounded a,
318+ EnumN a,
338− Bounded b,
319+ EnumN b,
339− Eq a,
320+ ConstructableN a b
340− CountableDimensionsN a b
321+ )
341− ) => ((a, b) -> e) -> Matrix e a b
322+ => ((a, b) -> e) -> Matrix e a b
342−matrixBuilder f = M (I.matrixBuilder f)
323+matrixBuilder = M . I.matrixBuilderN
343
324
344-- | Constructs a column vector matrix
325-- | Constructs a column vector matrix
345−col :: (FromListsN e () rows) => [e] -> Matrix e One rows
326+colL :: (FromListsN e () rows) => [e] -> Matrix e One rows
346−col = M . I.col
327+colL = M . I.colL
347
328
348-- | Constructs a row vector matrix
329-- | Constructs a row vector matrix
349−row :: (FromListsN e cols ()) => [e] -> Matrix e cols One
330+rowL :: (FromListsN e cols ()) => [e] -> Matrix e cols One
350−row = M . I.row
331+rowL = M . I.rowL
351−
352−-- | Lifts functions to matrices with arbitrary dimensions.
353−--
354−-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
355−-- types @a@ or @b@ allows.
356−fromF' ::
357− ( Liftable e a b,
358− CountableDimensionsN cols rows,
359− FromListsN e rows cols
360− ) =>
361− (a -> b) -> Matrix e cols rows
362−fromF' = M . I.fromF'
363
332
364-- | Lifts functions to matrices with dimensions matching @a@ and @b@
333-- | Lifts functions to matrices with dimensions matching @a@ and @b@
365-- cardinality's.
334-- cardinality's.
366fromF ::
335fromF ::
336+ ( Num e,
367− ( Liftable e a b,
337+ Liftable a b
368− CountableDimensionsN a b,
338+ )
369− FromListsN e b a
370− ) =>
371− (a -> b) -> Matrix e a b
339+ => (a -> b) -> Matrix e a b
372−fromF = M . I.fromF
340+fromF = M . I.fromFN
373
341
374-- | Lifts relation functions to Boolean Matrix
342-- | Lifts relation functions to Boolean Matrix
375−toRel ::
376− ( Liftable (Natural 0 1) a b,
377− CountableDimensionsN a b,
378− FromListsN (Natural 0 1) b a
379− ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
343+toRel :: Liftable a b => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
380−toRel = M . I.toRel
344+toRel = M . I.toRelN
381
345
382-- Conversion
346-- Conversion
383
347
@@ -393,8 +357,11 @@ toList (M m) = I.toList m
393
357
394-- | The zero matrix. A matrix wholly filled with zeros.
358-- | The zero matrix. A matrix wholly filled with zeros.
395zeros ::
359zeros ::
396− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
360+ ( Num e,
361+ ConstructableN cols rows,
362+ EnumerableN cols
363+ )
397− => Matrix e cols rows
364+ => Matrix e cols rows
398zeros = M I.zeros
365zeros = M I.zeros
399
366
400-- Ones Matrix
367-- Ones Matrix
@@ -403,8 +370,11 @@ zeros = M I.zeros
403--
370--
404-- Also known as T (Top) matrix.
371-- Also known as T (Top) matrix.
405ones ::
372ones ::
406− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
373+ ( Num e,
374+ ConstructableN cols rows,
375+ EnumerableN cols
376+ )
407− => Matrix e cols rows
377+ => Matrix e cols rows
408ones = M I.ones
378ones = M I.ones
409
379
410-- Const Matrix
380-- Const Matrix
@@ -412,28 +382,28 @@ ones = M I.ones
412-- | The constant matrix constructor. A matrix wholly filled with a given
382-- | The constant matrix constructor. A matrix wholly filled with a given
413-- value.
383-- value.
414constant ::
384constant ::
415− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
385+ ( Num e,
386+ ConstructableN cols rows,
387+ EnumerableN cols
388+ )
416− => e -> Matrix e cols rows
389+ => e -> Matrix e cols rows
417constant = M . I.constant
390constant = M . I.constant
418
391
419-- Bang Matrix
392-- Bang Matrix
420
393
421-- | The T (Top) row vector matrix.
394-- | The T (Top) row vector matrix.
422bang ::
395bang ::
423− forall e cols.
396+ ( Num e,
424− (Num e, Enum e, FromListsN e cols (), CountableN cols) =>
397+ ConstructN cols,
398+ EnumerableN cols
399+ )
425− Matrix e cols One
400+ => Matrix e cols One
426−bang = M I.bang
401+bang = ones
427
402
428-- | Point constant matrix
403-- | Point constant matrix
429−point ::
404+point ::
430− ( Bounded a,
431− Enum a,
432− Eq a,
433− Num e,
405+ ( Num e,
434− Ord e,
406+ Liftable () a
435− CountableN a,
436− FromListsN e a One
437 ) => a -> Matrix e One a
407 ) => a -> Matrix e One a
438point = fromF . const
408point = fromF . const
439
409
@@ -441,8 +411,12 @@ point = fromF . const
441
411
442-- | iden matrix
412-- | iden matrix
443iden ::
413iden ::
444− (Num e, FromListsN e a a, CountableN a) =>
414+ ( Num e,
415+ EqN a,
416+ ConstructN a,
417+ EnumerableN a
418+ )
445− Matrix e a a
419+ => Matrix e a a
446iden = M I.iden
420iden = M I.iden
447{-# NOINLINE iden #-}
421{-# NOINLINE iden #-}
448
422
@@ -476,44 +450,48 @@ infixl 7 ./
476
450
477-- | Biproduct first component projection
451-- | Biproduct first component projection
478p1 ::
452p1 ::
479− ( Num e,
453+ ( Num e,
480− CountableDimensionsN n m,
454+ EqN m,
481− FromListsN e n m,
455+ ConstructableN m n,
482− FromListsN e m m
456+ EnumerableN m,
457+ EnumerableN n
483− ) =>
458+ )
484− Matrix e (Either m n) m
459+ => Matrix e (Either m n) m
485p1 = M I.p1
460p1 = M I.p1
486
461
487-- | Biproduct second component projection
462-- | Biproduct second component projection
488p2 ::
463p2 ::
489− ( Num e,
464+ ( Num e,
490− CountableDimensionsN n m,
465+ EqN n,
491− FromListsN e m n,
466+ ConstructableN m n,
492− FromListsN e n n
467+ EnumerableN m,
468+ EnumerableN n
493− ) =>
469+ )
494− Matrix e (Either m n) n
470+ => Matrix e (Either m n) n
495p2 = M I.p2
471p2 = M I.p2
496
472
497-- Injections
473-- Injections
498
474
499-- | Biproduct first component injection
475-- | Biproduct first component injection
500i1 ::
476i1 ::
501− ( Num e,
477+ ( Num e,
502− CountableDimensionsN n m,
478+ EqN m,
503− FromListsN e n m,
479+ ConstructableN m n,
504− FromListsN e m m
480+ EnumerableN m,
481+ EnumerableN n
505− ) =>
482+ )
506− Matrix e m (Either m n)
483+ => Matrix e m (Either m n)
507i1 = tr p1
484i1 = tr p1
508
485
509-- | Biproduct second component injection
486-- | Biproduct second component injection
510i2 ::
487i2 ::
511− ( Num e,
488+ ( Num e,
512− CountableDimensionsN n m,
489+ EqN n,
513− FromListsN e m n,
490+ ConstructableN m n,
514− FromListsN e n n
491+ EnumerableN m,
492+ EnumerableN n
515− ) =>
493+ )
516− Matrix e n (Either m n)
494+ => Matrix e n (Either m n)
517i2 = tr p2
495i2 = tr p2
518
496
519-- Dimensions
497-- Dimensions
@@ -525,7 +503,7 @@ i2 = tr p2
525--
503--
526-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
504-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
527-- exchange for performance.
505-- exchange for performance.
528−rows :: (CountableN rows) => Matrix e cols rows -> Int
506+rows :: CountableN rows => Matrix e cols rows -> Int
529rows (M m) = I.rows m
507rows (M m) = I.rows m
530
508
531-- | Obtain the number of columns.
509-- | Obtain the number of columns.
@@ -535,7 +513,7 @@ rows (M m) = I.rows m
535--
513--
536-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
514-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
537-- exchange for performance.
515-- exchange for performance.
538−columns :: (CountableN cols) => Matrix e cols rows -> Int
516+columns :: CountableN cols => Matrix e cols rows -> Int
539columns (M m) = I.columns m
517columns (M m) = I.columns m
540
518
541-- Coproduct Bifunctor
519-- Coproduct Bifunctor
@@ -544,41 +522,53 @@ infixl 5 -|-
544
522
545-- | Matrix coproduct functor also known as matrix direct sum.
523-- | Matrix coproduct functor also known as matrix direct sum.
546(-|-) ::
524(-|-) ::
547− ( Num e,
525+ ( Num e,
548− CountableDimensionsN j k,
526+ EqN k,
549− FromListsN e k k,
527+ EqN j,
550− FromListsN e j k,
528+ ConstructableN k j,
551− FromListsN e k j,
529+ EnumerableN k,
552− FromListsN e j j
530+ EnumerableN j
553− ) =>
554− Matrix e n k ->
555− Matrix e m j ->
556− Matrix e (Either n m) (Either k j)
531+ ) => Matrix e n k -> Matrix e m j -> Matrix e (Either n m) (Either k j)
557(-|-) (M a) (M b) = M ((I.-|-) a b)
532(-|-) (M a) (M b) = M ((I.-|-) a b)
558
533
559-- Khatri Rao Product and projections
534-- Khatri Rao Product and projections
560
535
561−-- | Khatri Rao product first component projection matrix.
536+type FstM a b =
562−fstM ::
537+ ( EqN a,
563− forall e m k .
538+ ConstructableN (I.Normalize a) (I.Normalize (a, b)),
564− ( Num e,
539+ ConstructN (I.Normalize a, I.Normalize b),
565− CountableDimensionsN m k,
540+ EnumerableN a,
566− CountableN (m, k),
541+ EnumerableN b,
567− FromListsN e (m, k) m,
542+ EnumN (I.Normalize a, I.Normalize b),
568− TrivialP m k
543+ Trivial2 a,
569− ) => Matrix e (m, k) m
544+ TrivialP a b
570−fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
545+ )
571
546
547+-- | Khatri Rao product first component projection matrix.
548+fstM ::
549+ forall e a b .
550+ ( Num e,
551+ FstM a b
552+ ) => Matrix e (a, b) a
553+fstM = M (I.fstM @e @(I.Normalize a) @(I.Normalize b))
554+
555+type SndM a b =
556+ ( EqN b,
557+ ConstructableN (I.Normalize b) (I.Normalize (a, b)),
558+ ConstructN (I.Normalize a, I.Normalize b),
559+ EnumerableN a,
560+ EnumerableN b,
561+ EnumN (I.Normalize a, I.Normalize b),
562+ Trivial2 b,
563+ TrivialP a b
564+ )
572-- | Khatri Rao product second component projection matrix.
565-- | Khatri Rao product second component projection matrix.
573−sndM ::
566+sndM ::
574− forall e m k.
567+ forall e a b.
575− ( Num e,
568+ ( Num e,
576− CountableDimensionsN k m,
569+ SndM a b
577− CountableN (m, k),
578− FromListsN e (m, k) k,
579− TrivialP m k
580− ) => Matrix e (m, k) k
570+ ) => Matrix e (a, b) b
581−sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
571+sndM = M (I.sndM @e @(I.Normalize a) @(I.Normalize b))
582
572
583-- | Khatri Rao Matrix product also known as matrix pairing.
573-- | Khatri Rao Matrix product also known as matrix pairing.
584--
574--
@@ -592,14 +582,12 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
592--
582--
593-- __Emphasis__ on the implication symbol.
583-- __Emphasis__ on the implication symbol.
594kr ::
584kr ::
595− forall e cols a b.
585+ forall e cols a b.
596− ( Num e,
586+ ( Num e,
597− CountableDimensionsN a b,
587+ FstM a b,
598− CountableN (a, b),
588+ SndM a b
599− FromListsN e (a, b) a,
589+ )
600− FromListsN e (a, b) b,
601− TrivialP a b
602− ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
590+ => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
603kr a b =
591kr a b =
604 let fstM' = fstM @e @a @b
592 let fstM' = fstM @e @a @b
605 sndM' = sndM @e @a @b
593 sndM' = sndM @e @a @b
@@ -608,21 +596,38 @@ kr a b =
608-- Product Bifunctor (Kronecker)
596-- Product Bifunctor (Kronecker)
609
597
610infixl 4 ><
598infixl 4 ><
599+type Kronecker m n p q =
600+ ( EqN (I.Normalize m),
601+ EqN (I.Normalize n),
602+ EqN (I.Normalize p),
603+ EqN (I.Normalize q),
604+ ConstructableN (I.Normalize m) (I.Normalize n),
605+ ConstructableN (I.Normalize p) (I.Normalize q),
606+ ConstructableN (I.Normalize m, I.Normalize n) (I.Normalize p, I.Normalize q),
607+ ConstructableN (I.Normalize (I.Normalize m, I.Normalize n)) (I.Normalize (I.Normalize p, I.Normalize q)),
608+ FstM m n,
609+ SndM m n,
610+ EnumerableN (I.Normalize m),
611+ EnumerableN (I.Normalize n),
612+ EnumerableN (I.Normalize p),
613+ EnumerableN (I.Normalize q),
614+ EnumerableN (I.Normalize m, I.Normalize n),
615+ EnumerableN (I.Normalize p, I.Normalize q),
616+ Trivial2 m,
617+ Trivial2 n,
618+ Trivial2 p,
619+ Trivial2 q,
620+ TrivialP p q,
621+ TrivialP m m
622+ )
611
623
612-- | Matrix product functor also known as Kronecker product
624-- | Matrix product functor also known as Kronecker product
613(><) ::
625(><) ::
614− forall e m p n q.
626+ forall e m p n q.
615− ( Num e,
627+ ( Num e,
616− CountableDimensionsN m n,
628+ Kronecker m n p q
617− CountableDimensionsN p q,
629+ )
618− CountableDimensionsN (m, n) (p, q),
619− FromListsN e (m, n) m,
620− FromListsN e (m, n) n,
621− FromListsN e (p, q) p,
622− FromListsN e (p, q) q,
623− TrivialP m n,
624− TrivialP p q
625− ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
630+ => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
626(><) a b =
631(><) a b =
627 let fstM' = fstM @e @m @n
632 let fstM' = fstM @e @m @n
628 sndM' = sndM @e @m @n
633 sndM' = sndM @e @m @n
@@ -662,34 +667,23 @@ tr (M m) = M (I.tr m)
662
667
663-- | Selective functors 'select' operator equivalent inspired by the
668-- | Selective functors 'select' operator equivalent inspired by the
664-- ArrowMonad solution presented in the paper.
669-- ArrowMonad solution presented in the paper.
665−selectM ::
666− ( Num e,
667− FromListsN e b b,
668− CountableN b
669− ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670+selectM :: (Num e, EqN b, ConstructN b, EnumerableN b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670selectM (M m) (M y) = M (I.select m y)
671selectM (M m) (M y) = M (I.select m y)
671
672
672-- McCarthy's Conditional
673-- McCarthy's Conditional
673
674
674-- | McCarthy's Conditional expresses probabilistic choice.
675-- | McCarthy's Conditional expresses probabilistic choice.
675−cond ::
676+-- cond ::
676− ( Trivial a,
677+-- (
677− Trivial2 a,
678− Trivial3 a,
679− CountableN a,
680− FromListsN e () a,
681− FromListsN e a (),
682− FromListsN e a a,
683− Liftable e a Bool
684− )
678+-- )
685− =>
679+-- =>
686− (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
680+-- (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
687−cond p (M a) (M b) = M (I.cond p a b)
681+-- cond p (M a) (M b) =
688
682
689-- Pretty print
683-- Pretty print
690
684
691-- | Matrix pretty printer
685-- | Matrix pretty printer
692−pretty :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> String
686+pretty :: (Show e, CountableDimensionsN cols rows) => Matrix e cols rows -> String
693pretty (M m) = I.pretty m
687pretty (M m) = I.pretty m
694
688
695-- | Matrix pretty printer
689-- | Matrix pretty printer