on master help sign in
Added constraints type aliases to Matrix.Internal and Matrix.Type
43624a6Armando Santos
src/LAoP/Matrix/Internal.hs+59−86
@@ -1,4 +1,5 @@
1{-# LANGUAGE TypeFamilyDependencies #-}
1{-# LANGUAGE TypeFamilyDependencies #-}
2+{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE AllowAmbiguousTypes #-}
3{-# LANGUAGE AllowAmbiguousTypes #-}
3{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleContexts #-}
@@ -205,6 +206,14 @@ type family FromNat' (b :: Bool) (m :: Type) :: Type where
205type family Normalize (d :: Type) :: Type where
206type family Normalize (d :: Type) :: Type where
206 Normalize d = FromNat (Count d)
207 Normalize d = FromNat (Count d)
207
208
209+-- | Constraint type synonyms to keep the type signatures less convoluted
210+type Countable a = KnownNat (Count a)
211+type CountableN a = KnownNat (Count (Normalize a))
212+type CountableDimensions a b = (Countable a, Countable b)
213+type CountableDimensionsN a b = (CountableN a, CountableN b)
214+type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
215+type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
216+
208-- | It isn't possible to implement the 'id' function so it's
217-- | It isn't possible to implement the 'id' function so it's
209-- implementation is 'undefined'. However 'comp' can be and this partial
218-- implementation is 'undefined'. However 'comp' can be and this partial
210-- class implementation exists just to make the code more readable.
219-- class implementation exists just to make the code more readable.
@@ -322,7 +331,7 @@ instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () col
322 fromLists [h : t] = Junc (One h) (fromLists [t])
331 fromLists [h : t] = Junc (One h) (fromLists [t])
323 fromLists _ = error "Wrong dimensions"
332 fromLists _ = error "Wrong dimensions"
324
333
325−instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), KnownNat (Count a)) => FromLists e (Either a b) () where
334+instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
326 fromLists [l] =
335 fromLists [l] =
327 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
336 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
328 in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
337 in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
@@ -332,13 +341,13 @@ instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either ()
332 fromLists ([h] : t) = Split (One h) (fromLists t)
341 fromLists ([h] : t) = Split (One h) (fromLists t)
333 fromLists _ = error "Wrong dimensions"
342 fromLists _ = error "Wrong dimensions"
334
343
335−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, KnownNat (Count a)) => FromLists e () (Either a b) where
344+instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
336 fromLists l@([_] : _) =
345 fromLists l@([_] : _) =
337 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
346 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
338 in Split (fromLists (take rowsA l)) (fromLists (drop rowsA l))
347 in Split (fromLists (take rowsA l)) (fromLists (drop rowsA l))
339 fromLists _ = error "Wrong dimensions"
348 fromLists _ = error "Wrong dimensions"
340
349
341−instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, KnownNat (Count c)) => FromLists e (Either a b) (Either c d) where
350+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
342 fromLists l@(h : t) =
351 fromLists l@(h : t) =
343 let lh = length h
352 let lh = length h
344 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
353 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -352,8 +361,7 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
352matrixBuilder ::
361matrixBuilder ::
353 forall e cols rows.
362 forall e cols rows.
354 ( FromLists e cols rows,
363 ( FromLists e cols rows,
355− KnownNat (Count cols),
364+ CountableDimensions cols rows
356− KnownNat (Count rows)
357 ) =>
365 ) =>
358 ((Int, Int) -> e) ->
366 ((Int, Int) -> e) ->
359 Matrix e cols rows
367 Matrix e cols rows
@@ -377,15 +385,8 @@ row = fromLists . (: [])
377-- types @a@ or @b@ allows.
385-- types @a@ or @b@ allows.
378fromF ::
386fromF ::
379 forall a b cols rows e.
387 forall a b cols rows e.
380− ( Bounded a,
388+ ( Liftable e a b,
381− Bounded b,
389+ CountableDimensions cols rows,
382− Enum a,
383− Enum b,
384− Eq b,
385− Num e,
386− Ord e,
387− KnownNat (Count cols),
388− KnownNat (Count rows),
389 FromLists e rows cols
390 FromLists e rows cols
390 ) =>
391 ) =>
391 (a -> b) ->
392 (a -> b) ->
@@ -413,16 +414,9 @@ fromF f =
413-- cardinality's.
414-- cardinality's.
414fromF' ::
415fromF' ::
415 forall a b e.
416 forall a b e.
416− ( Bounded a,
417+ ( Liftable e a b,
417− Bounded b,
418+ CountableDimensionsN a b,
418− Enum a,
419+ FromListsN e b a
419− Enum b,
420− Eq b,
421− Num e,
422− Ord e,
423− KnownNat (Count (Normalize a)),
424− KnownNat (Count (Normalize b)),
425− FromLists e (Normalize b) (Normalize a)
426 ) =>
420 ) =>
427 (a -> b) ->
421 (a -> b) ->
428 Matrix e (Normalize a) (Normalize b)
422 Matrix e (Normalize a) (Normalize b)
@@ -461,7 +455,7 @@ toList = concat . toLists
461-- Zeros Matrix
455-- Zeros Matrix
462
456
463-- | The zero matrix. A matrix wholly filled with zeros.
457-- | The zero matrix. A matrix wholly filled with zeros.
464−zeros :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => Matrix e cols rows
458+zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
465zeros = matrixBuilder (const 0)
459zeros = matrixBuilder (const 0)
466
460
467-- Ones Matrix
461-- Ones Matrix
@@ -469,20 +463,20 @@ zeros = matrixBuilder (const 0)
469-- | The ones matrix. A matrix wholly filled with ones.
463-- | The ones matrix. A matrix wholly filled with ones.
470--
464--
471-- Also known as T (Top) matrix.
465-- Also known as T (Top) matrix.
472−ones :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => Matrix e cols rows
466+ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
473ones = matrixBuilder (const 1)
467ones = matrixBuilder (const 1)
474
468
475-- Const Matrix
469-- Const Matrix
476
470
477-- | The constant matrix constructor. A matrix wholly filled with a given
471-- | The constant matrix constructor. A matrix wholly filled with a given
478-- value.
472-- value.
479−constant :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => e -> Matrix e cols rows
473+constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
480constant e = matrixBuilder (const e)
474constant e = matrixBuilder (const e)
481
475
482-- Bang Matrix
476-- Bang Matrix
483
477
484-- | The T (Top) row vector matrix.
478-- | The T (Top) row vector matrix.
485−bang :: forall e cols. (Num e, Enum e, FromLists e cols (), KnownNat (Count cols)) => Matrix e cols ()
479+bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
486bang =
480bang =
487 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
481 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
488 in fromLists [take c [1, 1 ..]]
482 in fromLists [take c [1, 1 ..]]
@@ -490,7 +484,7 @@ bang =
490-- Identity Matrix
484-- Identity Matrix
491
485
492-- | Identity matrix.
486-- | Identity matrix.
493−identity :: (Num e, FromLists e cols cols, KnownNat (Count cols)) => Matrix e cols cols
487+identity :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
494identity = matrixBuilder (bool 0 1 . uncurry (==))
488identity = matrixBuilder (bool 0 1 . uncurry (==))
495
489
496-- Matrix composition (MMM)
490-- Matrix composition (MMM)
@@ -509,14 +503,14 @@ comp c (Junc a b) = Junc (comp c a) (comp c b) -- Junc fusion law
509-- Projections
503-- Projections
510
504
511-- | Biproduct first component projection
505-- | Biproduct first component projection
512−p1 :: forall e m n. (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
506+p1 :: forall e m n. (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
513p1 =
507p1 =
514 let iden = identity :: Matrix e m m
508 let iden = identity :: Matrix e m m
515 zero = zeros :: Matrix e n m
509 zero = zeros :: Matrix e n m
516 in junc iden zero
510 in junc iden zero
517
511
518-- | Biproduct second component projection
512-- | Biproduct second component projection
519−p2 :: forall e m n. (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
513+p2 :: forall e m n. (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
520p2 =
514p2 =
521 let iden = identity :: Matrix e n n
515 let iden = identity :: Matrix e n n
522 zero = zeros :: Matrix e m n
516 zero = zeros :: Matrix e m n
@@ -525,11 +519,11 @@ p2 =
525-- Injections
519-- Injections
526
520
527-- | Biproduct first component injection
521-- | Biproduct first component injection
528−i1 :: (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
522+i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
529i1 = tr p1
523i1 = tr p1
530
524
531-- | Biproduct second component injection
525-- | Biproduct second component injection
532−i2 :: (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
526+i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
533i2 = tr p2
527i2 = tr p2
534
528
535-- Dimensions
529-- Dimensions
@@ -541,7 +535,7 @@ i2 = tr p2
541--
535--
542-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
536-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
543-- exchange for performance.
537-- exchange for performance.
544−rows :: forall e cols rows. (KnownNat (Count rows)) => Matrix e cols rows -> Int
538+rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
545rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
539rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
546
540
547-- | Obtain the number of columns.
541-- | Obtain the number of columns.
@@ -551,7 +545,7 @@ rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
551--
545--
552-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
546-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
553-- exchange for performance.
547-- exchange for performance.
554−columns :: forall e cols rows. (KnownNat (Count cols)) => Matrix e cols rows -> Int
548+columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
555columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
549columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
556
550
557-- Coproduct Bifunctor
551-- Coproduct Bifunctor
@@ -562,8 +556,7 @@ infixl 5 -|-
562(-|-) ::
556(-|-) ::
563 forall e n k m j.
557 forall e n k m j.
564 ( Num e,
558 ( Num e,
565− KnownNat (Count j),
559+ CountableDimensions j k,
566− KnownNat (Count k),
567 FromLists e k k,
560 FromLists e k k,
568 FromLists e j k,
561 FromLists e j k,
569 FromLists e k j,
562 FromLists e k j,
@@ -580,10 +573,9 @@ infixl 5 -|-
580kp1 ::
573kp1 ::
581 forall e m k .
574 forall e m k .
582 ( Num e,
575 ( Num e,
583− KnownNat (Count k),
576+ CountableDimensions k m,
584− FromLists e (FromNat (Count m * Count k)) m,
585− KnownNat (Count m),
586− KnownNat (Count (Normalize (m, k)))
577+ FromLists e (Normalize (m, k)) m,
578+ CountableN (m, k)
587 ) => Matrix e (Normalize (m, k)) m
579 ) => Matrix e (Normalize (m, k)) m
588kp1 = matrixBuilder f
580kp1 = matrixBuilder f
589 where
581 where
@@ -596,10 +588,9 @@ kp1 = matrixBuilder f
596kp2 ::
588kp2 ::
597 forall e m k .
589 forall e m k .
598 ( Num e,
590 ( Num e,
599− KnownNat (Count k),
591+ CountableDimensions k m,
600− FromLists e (FromNat (Count m * Count k)) k,
601− KnownNat (Count m),
602− KnownNat (Count (Normalize (m, k)))
592+ FromLists e (Normalize (m, k)) k,
593+ CountableN (m, k)
603 ) => Matrix e (Normalize (m, k)) k
594 ) => Matrix e (Normalize (m, k)) k
604kp2 = matrixBuilder f
595kp2 = matrixBuilder f
605 where
596 where
@@ -622,9 +613,8 @@ kp2 = matrixBuilder f
622khatri ::
613khatri ::
623 forall e cols a b.
614 forall e cols a b.
624 ( Num e,
615 ( Num e,
625− KnownNat (Count a),
616+ CountableDimensions a b,
626− KnownNat (Count b),
617+ CountableN (a, b),
627− KnownNat (Count (Normalize (a, b))),
628 FromLists e (Normalize (a, b)) a,
618 FromLists e (Normalize (a, b)) a,
629 FromLists e (Normalize (a, b)) b
619 FromLists e (Normalize (a, b)) b
630 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
620 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
@@ -641,14 +631,11 @@ infixl 4 ><
641(><) ::
631(><) ::
642 forall e m p n q.
632 forall e m p n q.
643 ( Num e,
633 ( Num e,
644− KnownNat (Count m),
634+ CountableDimensions m n,
645− KnownNat (Count n),
635+ CountableDimensions p q,
646− KnownNat (Count p),
636+ CountableDimensionsN (m, n) (p, q),
647− KnownNat (Count q),
648− KnownNat (Count (Normalize (m, n))),
649 FromLists e (Normalize (m, n)) m,
637 FromLists e (Normalize (m, n)) m,
650 FromLists e (Normalize (m, n)) n,
638 FromLists e (Normalize (m, n)) n,
651− KnownNat (Count (Normalize (p, q))),
652 FromLists e (Normalize (p, q)) p,
639 FromLists e (Normalize (p, q)) p,
653 FromLists e (Normalize (p, q)) q
640 FromLists e (Normalize (p, q)) q
654 )
641 )
@@ -701,15 +688,15 @@ tr (Split a b) = Junc (tr a) (tr b)
701
688
702-- | Selective functors 'select' operator equivalent inspired by the
689-- | Selective functors 'select' operator equivalent inspired by the
703-- ArrowMonad solution presented in the paper.
690-- ArrowMonad solution presented in the paper.
704−select :: (Num e, FromLists e b b, KnownNat (Count b)) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
691+select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
705select m y = junc y identity . m
692select m y = junc y identity . m
706
693
707-- McCarthy's Conditional
694-- McCarthy's Conditional
708
695
709-- | McCarthy's Conditional expresses probabilistic choice.
696-- | McCarthy's Conditional expresses probabilistic choice.
710cond ::
697cond ::
711− ( cols ~ FromNat (Count cols),
698+ ( cols ~ Normalize cols,
712− KnownNat (Count cols),
699+ Countable cols,
713 FromLists e () cols,
700 FromLists e () cols,
714 FromLists e cols (),
701 FromLists e cols (),
715 FromLists e cols cols,
702 FromLists e cols cols,
@@ -723,8 +710,8 @@ cond ::
723cond p f g = junc f g . grd p
710cond p f g = junc f g . grd p
724
711
725grd ::
712grd ::
726− ( q ~ FromNat (Count q),
713+ ( q ~ Normalize q,
727− KnownNat (Count q),
714+ Countable q,
728 FromLists e () q,
715 FromLists e () q,
729 FromLists e q (),
716 FromLists e q (),
730 FromLists e q q,
717 FromLists e q q,
@@ -739,15 +726,12 @@ grd f = split (corr f) (corr (not . f))
739
726
740corr ::
727corr ::
741 forall e a q .
728 forall e a q .
742− ( q ~ FromNat (Count q),
729+ ( q ~ Normalize q,
743− KnownNat (Count q),
730+ Countable q,
744 FromLists e () q,
731 FromLists e () q,
745 FromLists e q (),
732 FromLists e q (),
746 FromLists e q q,
733 FromLists e q q,
747− Bounded a,
734+ Liftable e a Bool
748− Enum a,
749− Num e,
750− Ord e
751 )
735 )
752 => (a -> Bool) -> Matrix e q q
736 => (a -> Bool) -> Matrix e q q
753corr p = let f = fromF p :: Matrix e q ()
737corr p = let f = fromF p :: Matrix e q ()
@@ -775,7 +759,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
775 fill str = replicate (widest - length str - 2) ' ' ++ str
759 fill str = replicate (widest - length str - 2) ' ' ++ str
776
760
777-- | Matrix pretty printer
761-- | Matrix pretty printer
778−pretty :: (KnownNat (Count cols), Show e) => Matrix e cols rows -> String
762+pretty :: (Countable cols, Show e) => Matrix e cols rows -> String
779pretty m = "┌ " ++ unwords (replicate (columns m) blank) ++ " ┐\n" ++
763pretty m = "┌ " ++ unwords (replicate (columns m) blank) ++ " ┐\n" ++
780 prettyAux (toLists m) (toLists m) ++
764 prettyAux (toLists m) (toLists m) ++
781 "└ " ++ unwords (replicate (columns m) blank) ++ " ┘"
765 "└ " ++ unwords (replicate (columns m) blank) ++ " ┘"
@@ -786,7 +770,7 @@ pretty m = "┌ " ++ unwords (replicate (columns m) blank) ++ " ┐\n" ++
786 blank = fill ""
770 blank = fill ""
787
771
788-- | Matrix pretty printer
772-- | Matrix pretty printer
789−prettyPrint :: (KnownNat (Count cols), Show e) => Matrix e cols rows -> IO ()
773+prettyPrint :: (Countable cols, Show e) => Matrix e cols rows -> IO ()
790prettyPrint = putStrLn . pretty
774prettyPrint = putStrLn . pretty
791
775
792-- Relational operators functions
776-- Relational operators functions
@@ -872,14 +856,9 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
872-- types @a@ or @b@ allows.
856-- types @a@ or @b@ allows.
873fromFRel ::
857fromFRel ::
874 forall a b cols rows.
858 forall a b cols rows.
875− ( Bounded a,
859+ ( Liftable Boolean a b,
876− Bounded b,
860+ CountableDimensions cols rows,
877− Enum a,
878− Enum b,
879− Eq b,
880− KnownNat (Count cols),
881− KnownNat (Count rows),
882− FromLists (Natural 0 1) rows cols
861+ FromLists Boolean rows cols
883 ) =>
862 ) =>
884 (a -> b) ->
863 (a -> b) ->
885 Relation cols rows
864 Relation cols rows
@@ -906,14 +885,9 @@ fromFRel f =
906-- cardinality's.
885-- cardinality's.
907fromFRel' ::
886fromFRel' ::
908 forall a b.
887 forall a b.
909− ( Bounded a,
888+ ( Liftable Boolean a b,
910− Bounded b,
889+ CountableDimensionsN a b,
911− Enum a,
912− Enum b,
913− Eq b,
914− KnownNat (Count (Normalize a)),
915− KnownNat (Count (Normalize b)),
916− FromLists (Natural 0 1) (Normalize b) (Normalize a)
890+ FromLists Boolean (Normalize b) (Normalize a)
917 ) =>
891 ) =>
918 (a -> b) ->
892 (a -> b) ->
919 Relation (Normalize a) (Normalize b)
893 Relation (Normalize a) (Normalize b)
@@ -944,9 +918,8 @@ toRel ::
944 Enum a,
918 Enum a,
945 Enum b,
919 Enum b,
946 Eq b,
920 Eq b,
947− KnownNat (Count (Normalize a)),
921+ CountableDimensionsN a b,
948− KnownNat (Count (Normalize b)),
922+ FromListsN Boolean b a
949− FromLists (Natural 0 1) (Normalize b) (Normalize a)
950 )
923 )
951 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
924 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
952toRel f =
925toRel f =
src/LAoP/Matrix/Type.hs+112−146
@@ -1,3 +1,4 @@
1+{-# LANGUAGE ConstraintKinds #-}
1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DerivingVia #-}
4{-# LANGUAGE DerivingVia #-}
@@ -170,6 +171,17 @@ import qualified LAoP.Matrix.Internal as I
170newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
171newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
171 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.Normalize cols) (I.Normalize rows))
172 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.Normalize cols) (I.Normalize rows))
172
173
174+-- | Constraint type synonyms to keep the type signatures less convoluted
175+type Countable a = KnownNat (I.Count a)
176+type CountableDimensions a b = (Countable a, Countable b)
177+type CountableN a = KnownNat (I.Count (I.Normalize a))
178+type CountableDimensionsN a b = (CountableN a, CountableN b)
179+type FromListsN e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
180+type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
181+type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
182+type TrivialE a b = I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)
183+type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
184+
173-- | It isn't possible to implement the 'id' function so it's
185-- | It isn't possible to implement the 'id' function so it's
174-- implementation is 'undefined'. However 'comp' can be and this partial
186-- implementation is 'undefined'. However 'comp' can be and this partial
175-- class implementation exists just to make the code more readable.
187-- class implementation exists just to make the code more readable.
@@ -197,7 +209,7 @@ one = M . I.One
197
209
198-- | Matrix 'Junc' constructor
210-- | Matrix 'Junc' constructor
199junc ::
211junc ::
200− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
212+ (TrivialE a b) =>
201 Matrix e a rows ->
213 Matrix e a rows ->
202 Matrix e b rows ->
214 Matrix e b rows ->
203 Matrix e (Either a b) rows
215 Matrix e (Either a b) rows
@@ -206,7 +218,7 @@ junc (M a) (M b) = M (I.Junc a b)
206infixl 3 |||
218infixl 3 |||
207-- | Matrix 'Junc' constructor
219-- | Matrix 'Junc' constructor
208(|||) ::
220(|||) ::
209− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
221+ (TrivialE a b) =>
210 Matrix e a rows ->
222 Matrix e a rows ->
211 Matrix e b rows ->
223 Matrix e b rows ->
212 Matrix e (Either a b) rows
224 Matrix e (Either a b) rows
@@ -214,7 +226,7 @@ infixl 3 |||
214
226
215-- | Matrix 'Split' constructor
227-- | Matrix 'Split' constructor
216split ::
228split ::
217− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
229+ (TrivialE a b) =>
218 Matrix e cols a ->
230 Matrix e cols a ->
219 Matrix e cols b ->
231 Matrix e cols b ->
220 Matrix e cols (Either a b)
232 Matrix e cols (Either a b)
@@ -223,7 +235,7 @@ split (M a) (M b) = M (I.Split a b)
223infixl 2 ===
235infixl 2 ===
224-- | Matrix 'Split' constructor
236-- | Matrix 'Split' constructor
225(===) ::
237(===) ::
226− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
238+ (TrivialE a b) =>
227 Matrix e cols a ->
239 Matrix e cols a ->
228 Matrix e cols b ->
240 Matrix e cols b ->
229 Matrix e cols (Either a b)
241 Matrix e cols (Either a b)
@@ -233,16 +245,9 @@ infixl 2 ===
233
245
234-- | Functor instance equivalent function
246-- | Functor instance equivalent function
235fmapM ::
247fmapM ::
236− ( Bounded a,
248+ ( Liftable e a b,
237− Bounded b,
249+ CountableDimensionsN a b,
238− Enum a,
250+ FromListsN e b a
239− Enum b,
240− Eq b,
241− Num e,
242− Ord e,
243− KnownNat (I.Count (I.Normalize a)),
244− KnownNat (I.Count (I.Normalize b)),
245− I.FromLists e (I.Normalize b) (I.Normalize a)
246 )
251 )
247 =>
252 =>
248 (a -> b) -> Matrix e c a -> Matrix e c b
253 (a -> b) -> Matrix e c a -> Matrix e c b
@@ -254,25 +259,23 @@ unitM = one 1
254
259
255-- | Applicative instance equivalent 'unit' function,
260-- | Applicative instance equivalent 'unit' function,
256multM ::
261multM ::
257− ( KnownNat (I.Count (I.Normalize a)),
262+ ( CountableDimensionsN a b,
258− KnownNat (I.Count (I.Normalize b)),
263+ CountableN (a, b),
259− KnownNat (I.Count (I.Normalize (a, b))),
260 Num e,
264 Num e,
261− I.FromLists e (I.Normalize (a, b)) (I.Normalize a),
265+ FromListsN e (a, b) a,
262− I.FromLists e (I.Normalize (a, b)) (I.Normalize b),
266+ FromListsN e (a, b) b,
263− I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
267+ TrivialP a b
264 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
268 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
265−multM a b = khatri a b
269+multM = khatri
266−
267
270
268-- | Monad instance equivalent 'return' function,
271-- | Monad instance equivalent 'return' function,
269returnM ::
272returnM ::
270 forall e a .
273 forall e a .
271− ( Num e,
274+ ( Num e,
272− Enum e,
275+ Enum e,
273− Enum a,
276+ Enum a,
274− I.FromLists e () (I.Normalize a),
277+ FromListsN e () a,
275− KnownNat (I.Count a)
278+ Countable a
276 ) => a -> Matrix e One a
279 ) => a -> Matrix e One a
277returnM a = col l
280returnM a = col l
278 where
281 where
@@ -288,23 +291,22 @@ bindM = flip comp
288
291
289-- | Build a matrix out of a list of list of elements. Throws a runtime
292-- | Build a matrix out of a list of list of elements. Throws a runtime
290-- error if the dimensions do not match.
293-- error if the dimensions do not match.
291−fromLists :: (I.FromLists e (I.Normalize cols) (I.Normalize rows)) => [[e]] -> Matrix e cols rows
294+fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
292fromLists = M . I.fromLists
295fromLists = M . I.fromLists
293
296
294-- | Matrix builder function. Constructs a matrix provided with
297-- | Matrix builder function. Constructs a matrix provided with
295-- a construction function.
298-- a construction function.
296matrixBuilder ::
299matrixBuilder ::
297− (I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
300+ (FromListsN e cols rows, CountableDimensionsN cols rows )
298− ((Int, Int) -> e) ->
299− Matrix e cols rows
301+ => ((Int, Int) -> e) -> Matrix e cols rows
300matrixBuilder = M . I.matrixBuilder
302matrixBuilder = M . I.matrixBuilder
301
303
302-- | Constructs a column vector matrix
304-- | Constructs a column vector matrix
303−col :: (I.FromLists e () (I.Normalize rows)) => [e] -> Matrix e One rows
305+col :: (FromListsN e () rows) => [e] -> Matrix e One rows
304col = M . I.col
306col = M . I.col
305
307
306-- | Constructs a row vector matrix
308-- | Constructs a row vector matrix
307−row :: (I.FromLists e (I.Normalize cols) ()) => [e] -> Matrix e cols One
309+row :: (FromListsN e cols ()) => [e] -> Matrix e cols One
308row = M . I.row
310row = M . I.row
309
311
310-- | Lifts functions to matrices with arbitrary dimensions.
312-- | Lifts functions to matrices with arbitrary dimensions.
@@ -312,49 +314,28 @@ row = M . I.row
312-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
314-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
313-- types @a@ or @b@ allows.
315-- types @a@ or @b@ allows.
314fromF ::
316fromF ::
315− ( Bounded a,
317+ ( Liftable e a b,
316− Bounded b,
318+ CountableDimensionsN cols rows,
317− Enum a,
319+ FromListsN e rows cols
318− Enum b,
319− Eq b,
320− Num e,
321− Ord e,
322− KnownNat (I.Count (I.Normalize cols)),
323− KnownNat (I.Count (I.Normalize rows)),
324− I.FromLists e (I.Normalize rows) (I.Normalize cols)
325 ) =>
320 ) =>
326− (a -> b) ->
327− Matrix e cols rows
321+ (a -> b) -> Matrix e cols rows
328fromF = M . I.fromF
322fromF = M . I.fromF
329
323
330-- | Lifts functions to matrices with dimensions matching @a@ and @b@
324-- | Lifts functions to matrices with dimensions matching @a@ and @b@
331-- cardinality's.
325-- cardinality's.
332fromF' ::
326fromF' ::
333− ( Bounded a,
327+ ( Liftable e a b,
334− Bounded b,
328+ CountableDimensionsN a b,
335− Enum a,
329+ FromListsN e b a
336− Enum b,
337− Eq b,
338− Num e,
339− Ord e,
340− KnownNat (I.Count (I.Normalize a)),
341− KnownNat (I.Count (I.Normalize b)),
342− I.FromLists e (I.Normalize b) (I.Normalize a)
343 ) =>
330 ) =>
344− (a -> b) ->
345− Matrix e a b
331+ (a -> b) -> Matrix e a b
346fromF' = M . I.fromF'
332fromF' = M . I.fromF'
347
333
348-- | Lifts relation functions to Boolean Matrix
334-- | Lifts relation functions to Boolean Matrix
349toRel ::
335toRel ::
350− ( Bounded a,
336+ ( Liftable (Natural 0 1) a b,
351− Bounded b,
337+ CountableDimensionsN a b,
352− Enum a,
338+ FromListsN (Natural 0 1) b a
353− Enum b,
354− Eq b,
355− KnownNat (I.Count (I.Normalize a)),
356− KnownNat (I.Count (I.Normalize b)),
357− I.FromLists (Natural 0 1) (I.Normalize b) (I.Normalize a)
358 ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
339 ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
359toRel = M . I.toRel
340toRel = M . I.toRel
360
341
@@ -372,8 +353,8 @@ toList (M m) = I.toList m
372
353
373-- | The zero matrix. A matrix wholly filled with zeros.
354-- | The zero matrix. A matrix wholly filled with zeros.
374zeros ::
355zeros ::
375− (Num e, I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
356+ (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
376− Matrix e cols rows
357+ => Matrix e cols rows
377zeros = M I.zeros
358zeros = M I.zeros
378
359
379-- Ones Matrix
360-- Ones Matrix
@@ -382,8 +363,8 @@ zeros = M I.zeros
382--
363--
383-- Also known as T (Top) matrix.
364-- Also known as T (Top) matrix.
384ones ::
365ones ::
385− (Num e, I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
366+ (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
386− Matrix e cols rows
367+ => Matrix e cols rows
387ones = M I.ones
368ones = M I.ones
388
369
389-- Const Matrix
370-- Const Matrix
@@ -391,9 +372,8 @@ ones = M I.ones
391-- | The constant matrix constructor. A matrix wholly filled with a given
372-- | The constant matrix constructor. A matrix wholly filled with a given
392-- value.
373-- value.
393constant ::
374constant ::
394− (Num e, I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
375+ (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
395− e ->
396− Matrix e cols rows
376+ => e -> Matrix e cols rows
397constant = M . I.constant
377constant = M . I.constant
398
378
399-- Bang Matrix
379-- Bang Matrix
@@ -401,7 +381,7 @@ constant = M . I.constant
401-- | The T (Top) row vector matrix.
381-- | The T (Top) row vector matrix.
402bang ::
382bang ::
403 forall e cols.
383 forall e cols.
404− (Num e, Enum e, I.FromLists e (I.Normalize cols) (), KnownNat (I.Count (I.Normalize cols))) =>
384+ (Num e, Enum e, FromListsN e cols (), CountableN cols) =>
405 Matrix e cols One
385 Matrix e cols One
406bang = M I.bang
386bang = M I.bang
407
387
@@ -409,7 +389,7 @@ bang = M I.bang
409
389
410-- | Identity matrix
390-- | Identity matrix
411identity ::
391identity ::
412− (Num e, I.FromLists e (I.Normalize a) (I.Normalize a), KnownNat (I.Count (I.Normalize a))) =>
392+ (Num e, FromListsN e a a, CountableN a) =>
413 Matrix e a a
393 Matrix e a a
414identity = M I.identity
394identity = M I.identity
415
395
@@ -425,11 +405,10 @@ comp (M a) (M b) = M (I.comp a b)
425-- | Biproduct first component projection
405-- | Biproduct first component projection
426p1 ::
406p1 ::
427 ( Num e,
407 ( Num e,
428− KnownNat (I.Count (I.Normalize n)),
408+ CountableDimensionsN n m,
429− KnownNat (I.Count (I.Normalize m)),
409+ FromListsN e n m,
430− I.FromLists e (I.Normalize n) (I.Normalize m),
410+ FromListsN e m m,
431− I.FromLists e (I.Normalize m) (I.Normalize m),
411+ TrivialE m n
432− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
433 ) =>
412 ) =>
434 Matrix e (Either m n) m
413 Matrix e (Either m n) m
435p1 = M I.p1
414p1 = M I.p1
@@ -437,11 +416,10 @@ p1 = M I.p1
437-- | Biproduct second component projection
416-- | Biproduct second component projection
438p2 ::
417p2 ::
439 ( Num e,
418 ( Num e,
440− KnownNat (I.Count (I.Normalize n)),
419+ CountableDimensionsN n m,
441− KnownNat (I.Count (I.Normalize m)),
420+ FromListsN e m n,
442− I.FromLists e (I.Normalize m) (I.Normalize n),
421+ FromListsN e n n,
443− I.FromLists e (I.Normalize n) (I.Normalize n),
422+ TrivialE m n
444− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
445 ) =>
423 ) =>
446 Matrix e (Either m n) n
424 Matrix e (Either m n) n
447p2 = M I.p2
425p2 = M I.p2
@@ -451,11 +429,10 @@ p2 = M I.p2
451-- | Biproduct first component injection
429-- | Biproduct first component injection
452i1 ::
430i1 ::
453 ( Num e,
431 ( Num e,
454− KnownNat (I.Count (I.Normalize n)),
432+ CountableDimensionsN n m,
455− KnownNat (I.Count (I.Normalize m)),
433+ FromListsN e n m,
456− I.FromLists e (I.Normalize n) (I.Normalize m),
434+ FromListsN e m m,
457− I.FromLists e (I.Normalize m) (I.Normalize m),
435+ TrivialE m n
458− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
459 ) =>
436 ) =>
460 Matrix e m (Either m n)
437 Matrix e m (Either m n)
461i1 = tr p1
438i1 = tr p1
@@ -463,11 +440,10 @@ i1 = tr p1
463-- | Biproduct second component injection
440-- | Biproduct second component injection
464i2 ::
441i2 ::
465 ( Num e,
442 ( Num e,
466− KnownNat (I.Count (I.Normalize n)),
443+ CountableDimensionsN n m,
467− KnownNat (I.Count (I.Normalize m)),
444+ FromListsN e m n,
468− I.FromLists e (I.Normalize m) (I.Normalize n),
445+ FromListsN e n n,
469− I.FromLists e (I.Normalize n) (I.Normalize n),
446+ TrivialE m n
470− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
471 ) =>
447 ) =>
472 Matrix e n (Either m n)
448 Matrix e n (Either m n)
473i2 = tr p2
449i2 = tr p2
@@ -481,7 +457,7 @@ i2 = tr p2
481--
457--
482-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
458-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
483-- exchange for performance.
459-- exchange for performance.
484−rows :: (KnownNat (I.Count (I.Normalize rows))) => Matrix e cols rows -> Int
460+rows :: (CountableN rows) => Matrix e cols rows -> Int
485rows (M m) = I.rows m
461rows (M m) = I.rows m
486
462
487-- | Obtain the number of columns.
463-- | Obtain the number of columns.
@@ -491,7 +467,7 @@ rows (M m) = I.rows m
491--
467--
492-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
468-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
493-- exchange for performance.
469-- exchange for performance.
494−columns :: (KnownNat (I.Count (I.Normalize cols))) => Matrix e cols rows -> Int
470+columns :: (CountableN cols) => Matrix e cols rows -> Int
495columns (M m) = I.columns m
471columns (M m) = I.columns m
496
472
497-- Coproduct Bifunctor
473-- Coproduct Bifunctor
@@ -501,14 +477,13 @@ infixl 5 -|-
501-- | Matrix coproduct functor also known as matrix direct sum.
477-- | Matrix coproduct functor also known as matrix direct sum.
502(-|-) ::
478(-|-) ::
503 ( Num e,
479 ( Num e,
504− KnownNat (I.Count (I.Normalize j)),
480+ CountableDimensionsN j k,
505− KnownNat (I.Count (I.Normalize k)),
481+ FromListsN e k k,
506− I.FromLists e (I.Normalize k) (I.Normalize k),
482+ FromListsN e j k,
507− I.FromLists e (I.Normalize j) (I.Normalize k),
483+ FromListsN e k j,
508− I.FromLists e (I.Normalize k) (I.Normalize j),
484+ FromListsN e j j,
509− I.FromLists e (I.Normalize j) (I.Normalize j),
485+ TrivialE n m,
510− I.Normalize (Either n m) ~ Either (I.Normalize n) (I.Normalize m),
486+ TrivialE k j
511− I.Normalize (Either k j) ~ Either (I.Normalize k) (I.Normalize j)
512 ) =>
487 ) =>
513 Matrix e n k ->
488 Matrix e n k ->
514 Matrix e m j ->
489 Matrix e m j ->
@@ -521,11 +496,10 @@ infixl 5 -|-
521kp1 ::
496kp1 ::
522 forall e m k .
497 forall e m k .
523 ( Num e,
498 ( Num e,
524− KnownNat (I.Count (I.Normalize m)),
499+ CountableDimensionsN m k,
525− KnownNat (I.Count (I.Normalize k)),
500+ CountableN (m, k),
526− KnownNat (I.Count (I.Normalize (m, k))),
501+ FromListsN e (m, k) m,
527− I.FromLists e (I.Normalize (m, k)) (I.Normalize m),
502+ TrivialP m k
528− I.Normalize (m, k) ~ I.Normalize (I.Normalize m, I.Normalize k)
529 ) => Matrix e (m, k) m
503 ) => Matrix e (m, k) m
530kp1 = M (I.kp1 @e @(I.Normalize m) @(I.Normalize k))
504kp1 = M (I.kp1 @e @(I.Normalize m) @(I.Normalize k))
531
505
@@ -533,11 +507,10 @@ kp1 = M (I.kp1 @e @(I.Normalize m) @(I.Normalize k))
533kp2 ::
507kp2 ::
534 forall e m k.
508 forall e m k.
535 ( Num e,
509 ( Num e,
536− KnownNat (I.Count (I.Normalize k)),
510+ CountableDimensionsN k m,
537− KnownNat (I.Count (I.Normalize m)),
511+ CountableN (m, k),
538− KnownNat (I.Count (I.Normalize (m, k))),
512+ FromListsN e (m, k) k,
539− I.FromLists e (I.Normalize (m, k)) (I.Normalize k),
513+ TrivialP m k
540− I.Normalize (m, k) ~ I.Normalize (I.Normalize m, I.Normalize k)
541 ) => Matrix e (m, k) k
514 ) => Matrix e (m, k) k
542kp2 = M (I.kp2 @e @(I.Normalize m) @(I.Normalize k))
515kp2 = M (I.kp2 @e @(I.Normalize m) @(I.Normalize k))
543
516
@@ -555,12 +528,11 @@ kp2 = M (I.kp2 @e @(I.Normalize m) @(I.Normalize k))
555khatri ::
528khatri ::
556 forall e cols a b.
529 forall e cols a b.
557 ( Num e,
530 ( Num e,
558− KnownNat (I.Count (I.Normalize a)),
531+ CountableDimensionsN a b,
559− KnownNat (I.Count (I.Normalize b)),
532+ CountableN (a, b),
560− KnownNat (I.Count (I.Normalize (a, b))),
533+ FromListsN e (a, b) a,
561− I.FromLists e (I.Normalize (a, b)) (I.Normalize a),
534+ FromListsN e (a, b) b,
562− I.FromLists e (I.Normalize (a, b)) (I.Normalize b),
535+ TrivialP a b
563− I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
564 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
536 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
565khatri a b =
537khatri a b =
566 let kp1' = kp1 @e @a @b
538 let kp1' = kp1 @e @a @b
@@ -575,18 +547,15 @@ infixl 4 ><
575(><) ::
547(><) ::
576 forall e m p n q.
548 forall e m p n q.
577 ( Num e,
549 ( Num e,
578− KnownNat (I.Count (I.Normalize m)),
550+ CountableDimensionsN m n,
579− KnownNat (I.Count (I.Normalize n)),
551+ CountableDimensionsN p q,
580− KnownNat (I.Count (I.Normalize p)),
552+ CountableDimensionsN (m, n) (p, q),
581− KnownNat (I.Count (I.Normalize q)),
553+ FromListsN e (m, n) m,
582− KnownNat (I.Count (I.Normalize (m, n))),
554+ FromListsN e (m, n) n,
583− KnownNat (I.Count (I.Normalize (p, q))),
555+ FromListsN e (p, q) p,
584− I.FromLists e (I.Normalize (m, n)) (I.Normalize m),
556+ FromListsN e (p, q) q,
585− I.FromLists e (I.Normalize (m, n)) (I.Normalize n),
557+ TrivialP m n,
586− I.FromLists e (I.Normalize (p, q)) (I.Normalize p),
558+ TrivialP p q
587− I.FromLists e (I.Normalize (p, q)) (I.Normalize q),
588− I.Normalize (m, n) ~ I.Normalize (I.Normalize m, I.Normalize n),
589− I.Normalize (p, q) ~ I.Normalize (I.Normalize p, I.Normalize q)
590 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
559 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
591(><) a b =
560(><) a b =
592 let kp1' = kp1 @e @m @n
561 let kp1' = kp1 @e @m @n
@@ -629,9 +598,9 @@ tr (M m) = M (I.tr m)
629-- ArrowMonad solution presented in the paper.
598-- ArrowMonad solution presented in the paper.
630selectM ::
599selectM ::
631 ( Num e,
600 ( Num e,
632− I.FromLists e (I.Normalize b) (I.Normalize b),
601+ FromListsN e b b,
633− KnownNat (I.Count (I.Normalize b)),
602+ CountableN b,
634− I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)
603+ TrivialE a b
635 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
604 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
636selectM (M m) (M y) = M (I.select m y)
605selectM (M m) (M y) = M (I.select m y)
637
606
@@ -639,15 +608,12 @@ selectM (M m) (M y) = M (I.select m y)
639
608
640-- | McCarthy's Conditional expresses probabilistic choice.
609-- | McCarthy's Conditional expresses probabilistic choice.
641cond ::
610cond ::
642− ( I.Normalize (I.Normalize a) ~ I.Normalize a,
611+ ( Trivial a,
643− KnownNat (I.Count (I.Normalize a)),
612+ CountableN a,
644− I.FromLists e () (I.Normalize a),
613+ FromListsN e () a,
645− I.FromLists e (I.Normalize a) (),
614+ FromListsN e a (),
646− I.FromLists e (I.Normalize a) (I.Normalize a),
615+ FromListsN e a a,
647− Bounded a,
616+ Liftable e a Bool
648− Enum a,
649− Num e,
650− Ord e
651 )
617 )
652 =>
618 =>
653 (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
619 (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
@@ -656,9 +622,9 @@ cond p (M a) (M b) = M (I.cond p a b)
656-- Pretty print
622-- Pretty print
657
623
658-- | Matrix pretty printer
624-- | Matrix pretty printer
659−pretty :: (KnownNat (I.Count (I.Normalize cols)), Show e) => Matrix e cols rows -> String
625+pretty :: (CountableN cols, Show e) => Matrix e cols rows -> String
660pretty (M m) = I.pretty m
626pretty (M m) = I.pretty m
661
627
662-- | Matrix pretty printer
628-- | Matrix pretty printer
663−prettyPrint :: (KnownNat (I.Count (I.Normalize cols)), Show e) => Matrix e cols rows -> IO ()
629+prettyPrint :: (CountableN cols, Show e) => Matrix e cols rows -> IO ()
664prettyPrint (M m) = I.prettyPrint m
630prettyPrint (M m) = I.prettyPrint m