on master help sign in
Renamed abides and khatri
commit1a1cf28e1d2afde4bec8a48227ccb7be7354830a authorArmando Santos <armandoifsantos@gmail.com> dateFri 21 Feb 2020, 21:29 (7 years ago) parent399c102
8 files changed+166−165
app/Examples.hs+4−4 app/Quantum.hs+6−3 app/Spreadsheets.hs−1 src/LAoP/Dist/Internal.hs+1−1 src/LAoP/Matrix/Internal.hs+123−126 src/LAoP/Matrix/Nat.hs+16−14 src/LAoP/Matrix/Type.hs+15−15 src/LAoP/Relation/Internal.hs+1−1
app/Examples.hs+4−4
@@ -51,7 +51,7 @@ condition (fst, snd) thrd = if fst == snd
5151conditionSS :: (SS, SS) -> SS -> Natural 3 18
5252conditionSS = coerceNat2 condition
5353
54−conditionalThrows = fromF (uncurry conditionSS) . khatri (khatri die die) die
54+conditionalThrows = fromF (uncurry conditionSS) . kr (kr die die) die
5555
5656die :: Matrix Double () SS
5757die = col $ map (const (1/6)) [nat @1 @6 1 .. nat 6]
@@ -75,7 +75,7 @@ sprinkler = fromLists [[0.6, 0.99], [0.4, 0.01]]
7575grass :: Matrix Double (S, R) G
7676grass = fromLists [[1, 0.2, 0.1, 0.01], [0, 0.8, 0.9, 0.99]]
7777
78−tag f = khatri f iden
78+tag f = kr f iden
7979
8080state g s r = tag g `comp` tag s `comp` r
8181
@@ -156,11 +156,11 @@ exec = do
156156 putStrLn "Monty Hall Problem solution:"
157157 prettyPrint (secondChoice . firstChoice)
158158 putStrLn "\n Sum of dices probability:"
159− prettyPrint (sumSSM `comp` khatri die die)
159+ prettyPrint (sumSSM `comp` kr die die)
160160 putStrLn "\n Conditional dice throw:"
161161 prettyPrint conditionalThrows
162162 putStrLn "\n Checking that the last result is indeed a distribution: "
163− prettyPrint (bang . sumSSM . khatri die die)
163+ prettyPrint (bang . sumSSM . kr die die)
164164 putStrLn "\n Probability of grass being wet:"
165165 prettyPrint (grass_wet (state grass sprinkler rain))
166166 putStrLn "\n Probability of rain:"
app/Quantum.hs+6−3
@@ -14,10 +14,10 @@ ker :: Num e => Matrix e a b -> Matrix e a a
1414ker m = tr m `comp` m
1515
1616cnot :: Matrix (Complex Double) (Bool, Bool) (Bool, Bool)
17−cnot = khatri fstM (fromF xor)
17+cnot = kr fstM (fromF xor)
1818
19−ccnot :: Matrix (Complex Double) ((Bool, Bool), Bool) ((Bool, Bool), Bool)
20−ccnot = khatri fstM (fromF f)
19+ccnot :: (Num e, Ord e) => Matrix e ((Bool, Bool), Bool) ((Bool, Bool), Bool)
20+ccnot = kr fstM (fromF f)
2121 where
2222 f = xor . (tp (uncurry (&&)) id)
2323 tp f g (a,b) = (f a, g b)
@@ -26,3 +26,6 @@ had :: Matrix (Complex Double) Bool Bool
2626had = fromLists l
2727 where
2828 l = map (map (*(1/sqrt 2))) [[1, 1], [1, -1]]
29+
30+bell :: Matrix (Complex Double) (Bool, Bool) (Bool, Bool)
31+bell = cnot `comp` (had >< iden)
app/Spreadsheets.hs−1
@@ -4,7 +4,6 @@ module Spreadsheets
44 where
55
66import LAoP.Matrix.Type
7−import qualified LAoP.Matrix.Internal as I
87import GHC.Generics
98
109data Student = Student1 | Student2 | Student3 | Student4
src/LAoP/Dist/Internal.hs+1−1
@@ -90,7 +90,7 @@ multD ::
9090 FromListsN (a, b) b,
9191 TrivialP a b
9292 ) => Dist a -> Dist b -> Dist (a, b)
93−multD (D a) (D b) = D (khatri a b)
93+multD (D a) (D b) = D (kr a b)
9494
9595-- | Selective instance function
9696selectD ::
src/LAoP/Matrix/Internal.hs+123−126
@@ -89,8 +89,8 @@ module LAoP.Matrix.Internal
8989 cond,
9090
9191 -- ** Matrix "abiding"
92− abideJS,
93− abideSJ,
92+ abideJF,
93+ abideFJ,
9494
9595 -- ** Zip matrices
9696 zipWithM,
@@ -119,7 +119,7 @@ module LAoP.Matrix.Internal
119119 sndM,
120120
121121 -- *** Matrix pairing
122− khatri,
122+ kr,
123123
124124 -- * Matrix composition and lifting
125125
@@ -228,22 +228,22 @@ type Trivial a = FromNat (Count a) ~ a
228228--
229229-- Please use 'iden' instead.
230230instance (Num e) => Category (Matrix e) where
231− id = undefined
231+ id = undefined
232232 (.) = comp
233233
234234instance NFData e => NFData (Matrix e cols rows) where
235− rnf Empty = ()
236− rnf (One e) = rnf e
235+ rnf Empty = ()
236+ rnf (One e) = rnf e
237237 rnf (Join a b) = rnf a `seq` rnf b
238238 rnf (Fork a b) = rnf a `seq` rnf b
239239
240240instance Eq e => Eq (Matrix e cols rows) where
241− Empty == Empty = True
242− (One a) == (One b) = a == b
243− (Join a b) == (Join c d) = a == c && b == d
244− (Fork a b) == (Fork c d) = a == c && b == d
245− x@(Fork _ _) == y@(Join _ _) = x == abideJS y
246− x@(Join _ _) == y@(Fork _ _) = abideJS x == y
241+ Empty == Empty = True
242+ (One a) == (One b) = a == b
243+ (Join a b) == (Join c d) = a == c && b == d
244+ (Fork a b) == (Fork c d) = a == c && b == d
245+ x@(Fork _ _) == y@(Join _ _) = x == abideJF y
246+ x@(Join _ _) == y@(Fork _ _) = abideJF x == y
247247
248248instance Num e => Num (Matrix e cols rows) where
249249
@@ -253,23 +253,23 @@ instance Num e => Num (Matrix e cols rows) where
253253
254254 a * b = zipWithM (*) a b
255255
256− abs Empty = Empty
257− abs (One a) = One (abs a)
258− abs (Join a b) = Join (abs a) (abs b)
256+ abs Empty = Empty
257+ abs (One a) = One (abs a)
258+ abs (Join a b) = Join (abs a) (abs b)
259259 abs (Fork a b) = Fork (abs a) (abs b)
260260
261− signum Empty = Empty
262− signum (One a) = One (signum a)
263− signum (Join a b) = Join (signum a) (signum b)
261+ signum Empty = Empty
262+ signum (One a) = One (signum a)
263+ signum (Join a b) = Join (signum a) (signum b)
264264 signum (Fork a b) = Fork (signum a) (signum b)
265265
266266instance Ord e => Ord (Matrix e cols rows) where
267− Empty <= Empty = True
268− (One a) <= (One b) = a <= b
269− (Join a b) <= (Join c d) = (a <= c) && (b <= d)
270− (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
271− x@(Fork _ _) <= y@(Join _ _) = x <= abideJS y
272− x@(Join _ _) <= y@(Fork _ _) = abideJS x <= y
267+ Empty <= Empty = True
268+ (One a) <= (One b) = a <= b
269+ (Join a b) <= (Join c d) = (a <= c) && (b <= d)
270+ (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
271+ x@(Fork _ _) <= y@(Join _ _) = x <= abideJF y
272+ x@(Join _ _) <= y@(Fork _ _) = abideJF x <= y
273273
274274-- Primitives
275275
@@ -325,7 +325,7 @@ instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () col
325325 fromLists _ = error "Wrong dimensions"
326326
327327instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
328− fromLists [l] =
328+ fromLists [l] =
329329 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
330330 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
331331 fromLists _ = error "Wrong dimensions"
@@ -355,9 +355,7 @@ matrixBuilder ::
355355 forall e cols rows.
356356 ( FromLists e cols rows,
357357 CountableDimensions cols rows
358− ) =>
359− ((Int, Int) -> e) ->
360− Matrix e cols rows
358+ ) => ((Int, Int) -> e) -> Matrix e cols rows
361359matrixBuilder f =
362360 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
363361 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
@@ -394,8 +392,8 @@ fromF' f =
394392 elementsA = take ccols [minA .. maxA]
395393 elementsB = take rrows [minB .. maxB]
396394 combinations = (,) <$> elementsA <*> elementsB
397− combAp = map snd . sort . map (\(a, b) -> if f a == b
398− then ((fromEnum a, fromEnum b), 1)
395+ combAp = map snd . sort . map (\(a, b) -> if f a == b
396+ then ((fromEnum a, fromEnum b), 1)
399397 else ((fromEnum a, fromEnum b), 0)) $ combinations
400398 mList = buildList combAp rrows
401399 in tr $ fromLists mList
@@ -423,8 +421,8 @@ fromF f =
423421 elementsA = take ccols [minA .. maxA]
424422 elementsB = take rrows [minB .. maxB]
425423 combinations = (,) <$> elementsA <*> elementsB
426− combAp = map snd . sort . map (\(a, b) -> if f a == b
427− then ((fromEnum a, fromEnum b), 1)
424+ combAp = map snd . sort . map (\(a, b) -> if f a == b
425+ then ((fromEnum a, fromEnum b), 1)
428426 else ((fromEnum a, fromEnum b), 0)) $ combinations
429427 mList = buildList combAp rrows
430428 in tr $ fromLists mList
@@ -488,11 +486,11 @@ iden = matrixBuilder (bool 0 1 . uncurry (==))
488486-- This definition takes advantage of divide-and-conquer and fusion laws
489487-- from LAoP.
490488comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
491−comp Empty Empty = Empty
492−comp (One a) (One b) = One (a * b)
489+comp Empty Empty = Empty
490+comp (One a) (One b) = One (a * b)
493491comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
494492comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
495−comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
493+comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
496494{-# NOINLINE comp #-}
497495{-# RULES
498496 "comp/iden1" forall m. comp m iden = m ;
@@ -569,7 +567,7 @@ infixl 5 -|-
569567-- Khatri Rao Product and projections
570568
571569-- | Khatri Rao product first component projection matrix.
572−fstM ::
570+fstM ::
573571 forall e m k .
574572 ( Num e,
575573 CountableDimensions k m,
@@ -581,10 +579,10 @@ fstM = matrixBuilder f
581579 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
582580 f (x, y)
583581 | y >= (x * offset) && y <= (x * offset + offset - 1) = 1
584− | otherwise = 0
582+ | otherwise = 0
585583
586584-- | Khatri Rao product second component projection matrix.
587−sndM ::
585+sndM ::
588586 forall e m k .
589587 ( Num e,
590588 CountableDimensions k m,
@@ -596,39 +594,39 @@ sndM = matrixBuilder f
596594 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
597595 f (x, y)
598596 | x == y || mod (y - x) offset == 0 = 1
599− | otherwise = 0
597+ | otherwise = 0
600598
601599-- | Khatri Rao Matrix product also known as matrix pairing.
602600--
603601-- NOTE: That this is not a true categorical product, see for instance:
604602--
605603-- @
606−-- | fstM . khatri a b == a
607−-- khatri a b ==> |
608−-- | sndM . khatri a b == b
604+-- | fstM . kr a b == a
605+-- kr a b ==> |
606+-- | sndM . kr a b == b
609607-- @
610608--
611609-- __Emphasis__ on the implication symbol.
612−khatri ::
613− forall e cols a b.
610+kr ::
611+ forall e cols a b.
614612 ( Num e,
615613 CountableDimensions a b,
616614 CountableN (a, b),
617615 FromLists e (Normalize (a, b)) a,
618616 FromLists e (Normalize (a, b)) b
619617 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
620−khatri a b =
618+kr a b =
621619 let fstM' = fstM @e @a @b
622620 sndM' = sndM @e @a @b
623− in (tr fstM') . a * (tr sndM') . b
621+ in tr fstM' . a * tr sndM' . b
624622
625623-- Product Bifunctor (Kronecker)
626624
627625infixl 4 ><
628626
629627-- | Matrix product functor also known as kronecker product
630−(><) ::
631− forall e m p n q.
628+(><) ::
629+ forall e m p n q.
632630 ( Num e,
633631 CountableDimensions m n,
634632 CountableDimensions p q,
@@ -642,45 +640,45 @@ infixl 4 ><
642640(><) a b =
643641 let fstM' = fstM @e @m @n
644642 sndM' = sndM @e @m @n
645− in khatri (a . fstM') (b . sndM')
643+ in kr (a . fstM') (b . sndM')
646644
647645-- Matrix abide Join Fork
648646
649−-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
650−--
647+-- | Matrix "abiding" following the 'Join'-'Fork' exchange law.
648+--
651649-- Law:
652650--
653651-- @
654652-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
655653-- @
656−abideJS :: Matrix e cols rows -> Matrix e cols rows
657−abideJS (Join (Fork a c) (Fork b d)) = Fork (Join (abideJS a) (abideJS b)) (Join (abideJS c) (abideJS d)) -- Join-Fork abide law
658−abideJS Empty = Empty
659−abideJS (One e) = One e
660−abideJS (Join a b) = Join (abideJS a) (abideJS b)
661−abideJS (Fork a b) = Fork (abideJS a) (abideJS b)
654+abideJF :: Matrix e cols rows -> Matrix e cols rows
655+abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
656+abideJF Empty = Empty
657+abideJF (One e) = One e
658+abideJF (Join a b) = Join (abideJF a) (abideJF b)
659+abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
662660
663661-- Matrix abide Fork Join
664662
665663-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
666−--
664+--
667665-- @
668666-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
669667-- @
670−abideSJ :: Matrix e cols rows -> Matrix e cols rows
671−abideSJ (Fork (Join a b) (Join c d)) = Join (Fork (abideSJ a) (abideSJ c)) (Fork (abideSJ b) (abideSJ d)) -- Fork-Join abide law
672−abideSJ Empty = Empty
673−abideSJ (One e) = One e
674−abideSJ (Join a b) = Join (abideSJ a) (abideSJ b)
675−abideSJ (Fork a b) = Fork (abideSJ a) (abideSJ b)
668+abideFJ :: Matrix e cols rows -> Matrix e cols rows
669+abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
670+abideFJ Empty = Empty
671+abideFJ (One e) = One e
672+abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
673+abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
676674
677675-- Matrix transposition
678676
679677-- | Matrix transposition.
680678tr :: Matrix e cols rows -> Matrix e rows cols
681−tr Empty = Empty
682−tr (One e) = One e
683−tr (Join a b) = Fork (tr a) (tr b)
679+tr Empty = Empty
680+tr (One e) = One e
681+tr (Join a b) = Fork (tr a) (tr b)
684682tr (Fork a b) = Join (tr a) (tr b)
685683
686684-- Selective 'select' operator
@@ -688,9 +686,9 @@ tr (Fork a b) = Join (tr a) (tr b)
688686-- | Selective functors 'select' operator equivalent inspired by the
689687-- ArrowMonad solution presented in the paper.
690688select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
691−select (Fork a b) y = y . a + b -- Divide-and-conquer law
689+select (Fork a b) y = y . a + b -- Divide-and-conquer law
692690select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
693−select m y = join y iden . m
691+select m y = join y iden . m
694692
695693branch ::
696694 ( Num e,
@@ -733,7 +731,7 @@ cond ::
733731 (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
734732cond p f g = join f g . grd p
735733
736−grd ::
734+grd ::
737735 ( Trivial q,
738736 Countable q,
739737 FromLists e () q,
@@ -748,38 +746,38 @@ grd ::
748746 (a -> Bool) -> Matrix e q (Either q q)
749747grd f = fork (corr f) (corr (not . f))
750748
751−corr ::
752− forall e a q .
749+corr ::
750+ forall e a q .
753751 ( Trivial q,
754752 Countable q,
755753 FromLists e () q,
756754 FromLists e q (),
757755 FromLists e q q,
758756 Liftable e a Bool
759− )
757+ )
760758 => (a -> Bool) -> Matrix e q q
761759corr p = let f = fromF' p :: Matrix e q ()
762− in khatri f (iden :: Matrix e q q)
760+ in kr f (iden :: Matrix e q q)
763761
764762-- Pretty print
765763
766764prettyAux :: Show e => [[e]] -> [[e]] -> String
767765prettyAux [] _ = ""
768−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
766+prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
769767 where
770768 v = fmap show m
771769 widest = maximum $ fmap length v
772770 fill str = replicate (widest - length str - 2) ' ' ++ str
773−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
771+prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
774772 where
775− v = fmap show m
776− widest = maximum $ fmap length v
773+ v = fmap show m
774+ widest = maximum $ fmap length v
777775 fill str = replicate (widest - length str - 2) ' ' ++ str
778−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
776+prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
779777 prettyAux t l
780778 where
781− v = fmap show l
782− widest = maximum $ fmap length v
779+ v = fmap show l
780+ widest = maximum $ fmap length v
783781 fill str = replicate (widest - length str - 2) ' ' ++ str
784782
785783-- | Matrix pretty printer
@@ -791,16 +789,16 @@ pretty m = concat
791789 , "└ ", unwords (replicate (columns m) blank), " ┘"
792790 ]
793791 where
794− strings = map show (toList m)
795− widest = maximum $ map length strings
792+ strings = map show (toList m)
793+ widest = maximum $ map length strings
796794 fill str = replicate (widest - length str) ' ' ++ str
797− blank = fill ""
795+ blank = fill ""
798796 safeGet i j m
799797 | i > rows m || j > columns m || i < 1 || j < 1 = Nothing
800798 | otherwise = Just $ unsafeGet i j m (toList m)
801799 unsafeGet i j m l = l !! encode (columns m) (i,j)
802− encode m (i,j) = (i-1)*m + j - 1
803− getElem i j m =
800+ encode m (i,j) = (i-1)*m + j - 1
801+ getElem i j m =
804802 fromMaybe
805803 (error $
806804 "getElem: Trying to get the "
@@ -817,16 +815,16 @@ prettyPrint = putStrLn . pretty
817815
818816-- | Zip two matrices with a given binary function
819817zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
820−zipWithM f Empty Empty = Empty
821−zipWithM f (One a) (One b) = One (f a b)
822−zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
823−zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
824−zipWithM f x@(Fork _ _) y@(Join _ _) = zipWithM f x (abideJS y)
825−zipWithM f x@(Join _ _) y@(Fork _ _) = zipWithM f (abideJS x) y
818+zipWithM _ Empty Empty = Empty
819+zipWithM f (One a) (One b) = One (f a b)
820+zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
821+zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
822+zipWithM f x@(Fork _ _) y@(Join _ _) = zipWithM f x (abideJF y)
823+zipWithM f x@(Join _ _) y@(Fork _ _) = zipWithM f (abideJF x) y
826824
827825-- Relational operators functions
828826
829−type Boolean = Natural 0 1
827+type Boolean = Natural 0 1
830828type Relation a b = Matrix Boolean a b
831829
832830-- | Helper conversion function
@@ -843,53 +841,52 @@ fromBool False = nat 0
843841-- | Relational negation
844842negateM :: Relation cols rows -> Relation cols rows
845843negateM Empty = Empty
846−negateM (One (Nat 0)) = One (Nat 1)
847−negateM (One (Nat 1)) = One (Nat 0)
844+negateM (One (Nat p)) = One (Nat (negate p))
848845negateM (Join a b) = Join (negateM a) (negateM b)
849−negateM (Fork a b) = Fork (negateM a) (negateM b)
846+negateM (Fork a b) = Fork (negateM a) (negateM b)
850847
851848-- | Relational addition
852849orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
853−orM Empty Empty = Empty
854−orM (One a) (One b) = One (fromBool (toBool a || toBool b))
855−orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
856−orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
857−orM x@(Fork _ _) y@(Join _ _) = orM x (abideJS y)
858−orM x@(Join _ _) y@(Fork _ _) = orM (abideJS x) y
850+orM Empty Empty = Empty
851+orM (One a) (One b) = One (fromBool (toBool a || toBool b))
852+orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
853+orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
854+orM x@(Fork _ _) y@(Join _ _) = orM x (abideJF y)
855+orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
859856
860857-- | Relational multiplication
861858andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
862−andM Empty Empty = Empty
863−andM (One a) (One b) = One (fromBool (toBool a && toBool b))
864−andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
865−andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
866−andM x@(Fork _ _) y@(Join _ _) = andM x (abideJS y)
867−andM x@(Join _ _) y@(Fork _ _) = andM (abideJS x) y
859+andM Empty Empty = Empty
860+andM (One a) (One b) = One (fromBool (toBool a && toBool b))
861+andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
862+andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
863+andM x@(Fork _ _) y@(Join _ _) = andM x (abideJF y)
864+andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
868865
869866-- | Relational subtraction
870867subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
871−subM Empty Empty = Empty
872−subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
873−subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
874−subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
875−subM x@(Fork _ _) y@(Join _ _) = subM x (abideJS y)
876−subM x@(Join _ _) y@(Fork _ _) = subM (abideJS x) y
868+subM Empty Empty = Empty
869+subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
870+subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
871+subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
872+subM x@(Fork _ _) y@(Join _ _) = subM x (abideJF y)
873+subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
877874
878875-- | Matrix relational composition.
879876compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
880−compRel Empty Empty = Empty
881−compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
877+compRel Empty Empty = Empty
878+compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
882879compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
883880compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
884−compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion law
881+compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion law
885882
886883-- | Matrix relational right division
887884divR :: Relation b c -> Relation b a -> Relation a c
888885divR Empty Empty = Empty
889886divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
890887divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
891−divR (Fork a b) c = Fork (divR a c) (divR b c)
892−divR c (Fork a b) = Join (divR c a) (divR c b)
888+divR (Fork a b) c = Fork (divR a c) (divR b c)
889+divR c (Fork a b) = Join (divR c a) (divR c b)
893890
894891-- | Matrix relational left division
895892divL :: Relation c b -> Relation a b -> Relation a c
@@ -923,8 +920,8 @@ fromFRel' f =
923920 elementsA = take ccols [minA .. maxA]
924921 elementsB = take rrows [minB .. maxB]
925922 combinations = (,) <$> elementsA <*> elementsB
926− combAp = map snd . sort . map (\(a, b) -> if f a == b
927− then ((fromEnum a, fromEnum b), nat 1)
923+ combAp = map snd . sort . map (\(a, b) -> if f a == b
924+ then ((fromEnum a, fromEnum b), nat 1)
928925 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
929926 mList = buildList combAp rrows
930927 in tr $ fromLists mList
@@ -952,8 +949,8 @@ fromFRel f =
952949 elementsA = take ccols [minA .. maxA]
953950 elementsB = take rrows [minB .. maxB]
954951 combinations = (,) <$> elementsA <*> elementsB
955− combAp = map snd . sort . map (\(a, b) -> if f a == b
956− then ((fromEnum a, fromEnum b), nat 1)
952+ combAp = map snd . sort . map (\(a, b) -> if f a == b
953+ then ((fromEnum a, fromEnum b), nat 1)
957954 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
958955 mList = buildList combAp rrows
959956 in tr $ fromLists mList
@@ -962,7 +959,7 @@ fromFRel f =
962959 buildList l r = take r l : buildList (drop r l) r
963960
964961-- | Lifts a relation function to a Boolean Matrix
965−toRel ::
962+toRel ::
966963 forall a b.
967964 ( Bounded a,
968965 Bounded b,
@@ -983,8 +980,8 @@ toRel f =
983980 elementsA = take ccols [minA .. maxA]
984981 elementsB = take rrows [minB .. maxB]
985982 combinations = (,) <$> elementsA <*> elementsB
986− combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
987− then ((fromEnum a, fromEnum b), nat 1)
983+ combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
984+ then ((fromEnum a, fromEnum b), nat 1)
988985 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
989986 mList = buildList combAp rrows
990987 in tr $ fromLists mList
src/LAoP/Matrix/Nat.hs+16−14
@@ -105,8 +105,8 @@ module LAoP.Matrix.Nat
105105 cond,
106106
107107 -- ** Matrix "abiding"
108− abideJS,
109− abideSJ,
108+ abideJF,
109+ abideFJ,
110110
111111 -- ** Zip Matrices
112112 zipWithM,
@@ -135,7 +135,7 @@ module LAoP.Matrix.Nat
135135 sndM,
136136
137137 -- *** Matrix pairing
138− khatri,
138+ kr,
139139
140140 -- * Matrix composition and lifting
141141
@@ -358,9 +358,9 @@ rows (M m) = I.rows m
358358columns :: (CountableN cols) => Matrix e cols rows -> Int
359359columns (M m) = I.columns m
360360
361−-- | Coproduct Bifunctor
362361infixl 5 -|-
363362
363+-- | Coproduct Bifunctor (Direct sum)
364364(-|-) ::
365365 ( Num e,
366366 CountableDimensionsN j k,
@@ -376,8 +376,8 @@ infixl 5 -|-
376376 Matrix e (n + m) (k + j)
377377(-|-) (M a) (M b) = M ((I.-|-) a b)
378378
379−-- | Khatri Rao Product and projections
380−fstM ::
379+-- | Khatri Rao Product first projection
380+fstM ::
381381 forall e m k .
382382 ( Num e,
383383 CountableDimensionsN m k,
@@ -387,7 +387,8 @@ fstM ::
387387 ) => Matrix e (m * k) m
388388fstM = M (I.fstM @e @(I.FromNat m) @(I.FromNat k))
389389
390−sndM ::
390+-- | Khatri Rao Product second projection
391+sndM ::
391392 forall e m k.
392393 ( Num e,
393394 CountableDimensionsN k m,
@@ -397,7 +398,8 @@ sndM ::
397398 ) => Matrix e (m * k) k
398399sndM = M (I.sndM @e @(I.FromNat m) @(I.FromNat k))
399400
400−khatri ::
401+-- | Khatri Rao Product
402+kr ::
401403 forall e cols a b.
402404 ( Num e,
403405 CountableDimensionsN a b,
@@ -406,7 +408,7 @@ khatri ::
406408 FromListsN e (a * b) b,
407409 TrivialP a b
408410 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a * b)
409−khatri a b =
411+kr a b =
410412 let fstM' = fstM @e @a @b
411413 sndM' = sndM @e @a @b
412414 in comp (tr fstM') a * comp (tr sndM') b
@@ -430,15 +432,15 @@ infixl 4 ><
430432(><) a b =
431433 let fstM' = fstM @e @m @n
432434 sndM' = sndM @e @m @n
433− in khatri (comp a fstM') (comp b sndM')
435+ in kr (comp a fstM') (comp b sndM')
434436
435437-- | Matrix abide Join Fork
436−abideJS :: Matrix e cols rows -> Matrix e cols rows
437−abideJS (M m) = M (I.abideJS m)
438+abideJF :: Matrix e cols rows -> Matrix e cols rows
439+abideJF (M m) = M (I.abideJF m)
438440
439441-- | Matrix abide Fork Join
440−abideSJ :: Matrix e cols rows -> Matrix e cols rows
441−abideSJ (M m) = M (I.abideSJ m)
442+abideFJ :: Matrix e cols rows -> Matrix e cols rows
443+abideFJ (M m) = M (I.abideFJ m)
442444
443445-- | Matrix transposition
444446tr :: Matrix e cols rows -> Matrix e rows cols
src/LAoP/Matrix/Type.hs+15−15
@@ -122,8 +122,8 @@ module LAoP.Matrix.Type
122122 cond,
123123
124124 -- ** Matrix "abiding"
125− abideJS,
126− abideSJ,
125+ abideJF,
126+ abideFJ,
127127
128128 -- ** Zip Matrices
129129 zipWithM,
@@ -152,7 +152,7 @@ module LAoP.Matrix.Type
152152 sndM,
153153
154154 -- *** Matrix pairing
155− khatri,
155+ kr,
156156
157157 -- * Matrix composition and lifting
158158
@@ -289,7 +289,7 @@ multM ::
289289 FromListsN e (a, b) b,
290290 TrivialP a b
291291 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
292−multM = khatri
292+multM = kr
293293
294294-- | Monad instance equivalent 'return' function,
295295returnM ::
@@ -554,13 +554,13 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
554554-- NOTE: That this is not a true categorical product, see for instance:
555555--
556556-- @
557−-- | fstM `comp` khatri a b == a
558−-- khatri a b ==> |
559−-- | sndM `comp` khatri a b == b
557+-- | fstM `comp` kr a b == a
558+-- kr a b ==> |
559+-- | sndM `comp` kr a b == b
560560-- @
561561--
562562-- __Emphasis__ on the implication symbol.
563−khatri ::
563+kr ::
564564 forall e cols a b.
565565 ( Num e,
566566 CountableDimensionsN a b,
@@ -569,7 +569,7 @@ khatri ::
569569 FromListsN e (a, b) b,
570570 TrivialP a b
571571 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
572−khatri a b =
572+kr a b =
573573 let fstM' = fstM @e @a @b
574574 sndM' = sndM @e @a @b
575575 in comp (tr fstM') a * comp (tr sndM') b
@@ -578,7 +578,7 @@ khatri a b =
578578
579579infixl 4 ><
580580
581−-- | Matrix product functor also known as kronecker product
581+-- | Matrix product functor also known as Kronecker product
582582(><) ::
583583 forall e m p n q.
584584 ( Num e,
@@ -595,7 +595,7 @@ infixl 4 ><
595595(><) a b =
596596 let fstM' = fstM @e @m @n
597597 sndM' = sndM @e @m @n
598− in khatri (comp a fstM') (comp b sndM')
598+ in kr (comp a fstM') (comp b sndM')
599599
600600-- Matrix abide Join Fork
601601
@@ -606,8 +606,8 @@ infixl 4 ><
606606-- @
607607-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
608608-- @
609−abideJS :: Matrix e cols rows -> Matrix e cols rows
610−abideJS (M m) = M (I.abideJS m)
609+abideJF :: Matrix e cols rows -> Matrix e cols rows
610+abideJF (M m) = M (I.abideJF m)
611611
612612-- Matrix abide Fork Join
613613
@@ -618,8 +618,8 @@ abideJS (M m) = M (I.abideJS m)
618618-- @
619619-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
620620-- @
621−abideSJ :: Matrix e cols rows -> Matrix e cols rows
622−abideSJ (M m) = M (I.abideSJ m)
621+abideFJ :: Matrix e cols rows -> Matrix e cols rows
622+abideFJ (M m) = M (I.abideFJ m)
623623
624624-- Matrix transposition
625625
src/LAoP/Relation/Internal.hs+1−1
@@ -742,7 +742,7 @@ splitR ::
742742 TrivialP a b
743743 )
744744 => Relation c a -> Relation c b -> Relation c (a, b)
745−splitR (R f) (R g) = R (I.khatri f g)
745+splitR (R f) (R g) = R (I.kr f g)
746746
747747-- | Relational pairing first component projection
748748--