@@ -1,3 +1,4 @@
1+{-# LANGUAGE StrictData #-}
12{-# LANGUAGE TypeFamilyDependencies #-}
23{-# LANGUAGE ConstraintKinds #-}
34{-# LANGUAGE AllowAmbiguousTypes #-}
@@ -29,7 +30,7 @@
2930-- motivation behind the library, the underlying theory, and implementation details.
3031--
3132-- This module offers many of the combinators mentioned in the work of
32−-- Macedo (2012) and Oliveira (2012).
33+-- Macedo (2012) and Oliveira (2012).
3334--
3435-- This is an Internal module and it is no supposed to be imported.
3536--
@@ -42,17 +43,26 @@ module LAoP.Matrix.Internal
4243 --
4344 -- There exists two type families that make it easier to write
4445 -- matrix dimensions: 'FromNat' and 'Count'. This approach
45− -- leads to a very straightforward implementation
46− -- of LAoP combinators.
46+ -- leads to a very straightforward implementation
47+ -- of LAoP combinators.
4748
4849 -- * Type safe matrix representation
4950 Matrix (..),
5051
52+ -- * Constraint type aliases
53+ Countable,
54+ CountableDims,
55+ CountableN,
56+ CountableDimsN,
57+ FL,
58+ FLN,
59+ Liftable,
60+ Trivial,
61+
5162 -- * Primitives
52− empty,
5363 one,
54− junc,
55− split,
64+ join,
65+ fork,
5666
5767 -- * Auxiliary type families
5868 FromNat,
@@ -65,6 +75,7 @@ module LAoP.Matrix.Internal
6575 toLists,
6676 toList,
6777 matrixBuilder,
78+ matrixBuilder',
6879 row,
6980 col,
7081 zeros,
@@ -75,28 +86,38 @@ module LAoP.Matrix.Internal
7586 -- * Misc
7687 -- ** Get dimensions
7788 columns,
89+ columns',
7890 rows,
91+ rows',
7992
8093 -- ** Matrix Transposition
8194 tr,
8295
96+ -- ** Scalar multiplication/division of matrices
97+ (.|),
98+ (./),
99+
83100 -- ** Selective operator
84− select,
101+ select,
102+ branch,
85103
86104 -- ** McCarthy's Conditional
87105 cond,
88106
89107 -- ** Matrix "abiding"
90− abideJS,
91− abideSJ,
108+ abideJF,
109+ abideFJ,
110+
111+ -- ** Zip matrices
112+ zipWithM,
92113
93114 -- * Biproduct approach
94− -- ** Split
115+ -- ** Fork
95116 (===),
96117 -- *** Projections
97118 p1,
98119 p2,
99− -- ** Junc
120+ -- ** Join
100121 (|||),
101122 -- *** Injections
102123 i1,
@@ -109,12 +130,13 @@ module LAoP.Matrix.Internal
109130
110131 -- | Note that given the restrictions imposed it is not possible to
111132 -- implement the standard type classes present in standard Haskell.
133+
112134 -- *** Matrix pairing projections
113− kp1,
114− kp2,
135+ fstM,
136+ sndM,
115137
116138 -- *** Matrix pairing
117− khatri,
139+ kr,
118140
119141 -- * Matrix composition and lifting
120142
@@ -122,10 +144,10 @@ module LAoP.Matrix.Internal
122144
123145 -- | Note that given the restrictions imposed it is not possible to
124146 -- implement the standard type classes present in standard Haskell.
125− identity,
147+ iden,
126148 comp,
127− fromF,
128149 fromF',
150+ fromF,
129151
130152 -- * Matrix printing
131153 pretty,
@@ -152,21 +174,22 @@ import LAoP.Utils.Internal
152174import Data.Bool
153175import Data.Kind
154176import Data.List
177+import Data.Maybe
155178import Data.Proxy
179+import Data.Typeable
156180import Data.Void
157181import GHC.TypeLits
158182import Data.Type.Equality
159183import GHC.Generics
160184import Control.DeepSeq
161−import Control.Category
162−import Prelude hiding ((.))
185+import Prelude hiding (id, (.))
186+import qualified Prelude (id, (.))
163187
164188-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
165189data Matrix e cols rows where
166− Empty :: Matrix e Void Void
167190 One :: e -> Matrix e () ()
168− Junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
169− Split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
191+ Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
192+ Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
170193
171194deriving instance (Show e) => Show (Matrix e cols rows)
172195
@@ -176,7 +199,7 @@ deriving instance (Show e) => Show (Matrix e cols rows)
176199-- 'Generic' instance.
177200type family Count (d :: Type) :: Nat where
178201 Count (Natural n m) = (m - n) + 1
179− Count (Powerset a) = (^) (Count a) (Count a)
202+ Count (List a) = (^) 2 (Count a)
180203 Count (Either a b) = (+) (Count a) (Count b)
181204 Count (a, b) = (*) (Count a) (Count b)
182205 Count (a -> b) = (^) (Count b) (Count a)
@@ -193,205 +216,197 @@ type family Count (d :: Type) :: Nat where
193216--
194217-- Thanks to Li-Yao Xia this type family is super fast.
195218type family FromNat (n :: Nat) :: Type where
196− FromNat 0 = Void
197219 FromNat 1 = ()
198220 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
199221
200222type family FromNat' (b :: Bool) (m :: Type) :: Type where
201− FromNat' 'True m = Either m m
223+ FromNat' 'True m = Either m m
202224 FromNat' 'False m = Either () (Either m m)
203225
204226-- | Type family that normalizes the representation of a given data
205227-- structure
206228type family Normalize (d :: Type) :: Type where
207− Normalize d = FromNat (Count d)
229+ Normalize (Either a b) = Either (Normalize a) (Normalize b)
230+ Normalize d = FromNat (Count d)
208231
209232-- | Constraint type synonyms to keep the type signatures less convoluted
210233type Countable a = KnownNat (Count a)
211234type 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)
235+type CountableDims a b = (Countable a, Countable b)
236+type CountableDimsN a b = (CountableN a, CountableN b)
237+type FL a b = FromLists a b
238+type FLN a b = FromLists (Normalize a) (Normalize b)
215239type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
240+type Trivial a = FromNat (Count a) ~ a
216241
217−-- | It isn't possible to implement the 'id' function so it's
218−-- implementation is 'undefined'. However 'comp' can be and this partial
219−-- class implementation exists just to make the code more readable.
220−--
221−-- Please use 'identity' instead.
242+-- | It is possible to implement a constrained version of the category type
243+-- class.
222244instance (Num e) => Category (Matrix e) where
223− id = undefined
224− (.) = comp
245+ type Object (Matrix e) a = (FL a a, Countable a)
246+ id = iden
247+ (.) = comp
225248
226249instance NFData e => NFData (Matrix e cols rows) where
227− rnf Empty = ()
228− rnf (One e) = rnf e
229− rnf (Junc a b) = rnf a `seq` rnf b
230− rnf (Split a b) = rnf a `seq` rnf b
250+ rnf (One e) = rnf e
251+ rnf (Join a b) = rnf a `seq` rnf b
252+ rnf (Fork a b) = rnf a `seq` rnf b
231253
232254instance Eq e => Eq (Matrix e cols rows) where
233− Empty == Empty = True
234− (One a) == (One b) = a == b
235− (Junc a b) == (Junc c d) = a == c && b == d
236− (Split a b) == (Split c d) = a == c && b == d
237− x@(Split _ _) == y@(Junc _ _) = x == abideJS y
238− x@(Junc _ _) == y@(Split _ _) = abideJS x == y
255+ (One a) == (One b) = a == b
256+ (Join a b) == (Join c d) = a == c && b == d
257+ (Fork a b) == (Fork c d) = a == c && b == d
258+ x@(Fork _ _) == y@(Join _ _) = x == abideJF y
259+ x@(Join _ _) == y@(Fork _ _) = abideJF x == y
239260
240261instance Num e => Num (Matrix e cols rows) where
262+ a + b = zipWithM (+) a b
263+
264+ a - b = zipWithM (-) a b
265+
266+ a * b = zipWithM (*) a b
241267
242− Empty + Empty = Empty
243− (One a) + (One b) = One (a + b)
244− (Junc a b) + (Junc c d) = Junc (a + c) (b + d)
245− (Split a b) + (Split c d) = Split (a + c) (b + d)
246− x@(Split _ _) + y@(Junc _ _) = x + abideJS y
247− x@(Junc _ _) + y@(Split _ _) = abideJS x + y
248−
249− Empty - Empty = Empty
250− (One a) - (One b) = One (a - b)
251− (Junc a b) - (Junc c d) = Junc (a - c) (b - d)
252− (Split a b) - (Split c d) = Split (a - c) (b - d)
253− x@(Split _ _) - y@(Junc _ _) = x - abideJS y
254− x@(Junc _ _) - y@(Split _ _) = abideJS x - y
255−
256− Empty * Empty = Empty
257− (One a) * (One b) = One (a * b)
258− (Junc a b) * (Junc c d) = Junc (a * c) (b * d)
259− (Split a b) * (Split c d) = Split (a * c) (b * d)
260− x@(Split _ _) * y@(Junc _ _) = x * abideJS y
261− x@(Junc _ _) * y@(Split _ _) = abideJS x * y
262−
263− abs Empty = Empty
264− abs (One a) = One (abs a)
265− abs (Junc a b) = Junc (abs a) (abs b)
266− abs (Split a b) = Split (abs a) (abs b)
267−
268− signum Empty = Empty
269− signum (One a) = One (signum a)
270− signum (Junc a b) = Junc (signum a) (signum b)
271− signum (Split a b) = Split (signum a) (signum b)
268+ abs (One a) = One (abs a)
269+ abs (Join a b) = Join (abs a) (abs b)
270+ abs (Fork a b) = Fork (abs a) (abs b)
271+
272+ signum (One a) = One (signum a)
273+ signum (Join a b) = Join (signum a) (signum b)
274+ signum (Fork a b) = Fork (signum a) (signum b)
272275
273276instance Ord e => Ord (Matrix e cols rows) where
274− Empty <= Empty = True
275− (One a) <= (One b) = a <= b
276− (Junc a b) <= (Junc c d) = (a <= c) && (b <= d)
277− (Split a b) <= (Split c d) = (a <= c) && (b <= d)
278− x@(Split _ _) <= y@(Junc _ _) = x <= abideJS y
279− x@(Junc _ _) <= y@(Split _ _) = abideJS x <= y
277+ (One a) <= (One b) = a <= b
278+ (Join a b) <= (Join c d) = (a <= c) && (b <= d)
279+ (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
280+ x@(Fork _ _) <= y@(Join _ _) = x <= abideJF y
281+ x@(Join _ _) <= y@(Fork _ _) = abideJF x <= y
280282
281283-- Primitives
282284
283−-- | Empty matrix constructor
284−empty :: Matrix e Void Void
285−empty = Empty
286−
287285-- | Unit matrix constructor
288286one :: e -> Matrix e () ()
289287one = One
290288
291−-- | Matrix 'Junc' constructor
292−junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
293−junc = Junc
289+-- | Matrix 'Join' constructor
290+join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
291+join = Join
294292
295293infixl 3 |||
296294
297−-- | Matrix 'Junc' constructor
295+-- | Matrix 'Join' constructor
298296(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
299−(|||) = Junc
297+(|||) = Join
300298
301−-- | Matrix 'Split' constructor
302−split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
303−split = Split
299+-- | Matrix 'Fork' constructor
300+fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
301+fork = Fork
304302
305303infixl 2 ===
306304
307−-- | Matrix 'Split' constructor
305+-- | Matrix 'Fork' constructor
308306(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
309−(===) = Split
307+(===) = Fork
310308
311309-- Construction
312310
313311-- | Type class for defining the 'fromList' conversion function.
314312--
315313-- Given that it is not possible to branch on types at the term level type
316−-- classes are needed bery much like an inductive definition but on types.
317−class FromLists e cols rows where
314+-- classes are needed very much like an inductive definition but on types.
315+class FromLists cols rows where
318316 -- | Build a matrix out of a list of list of elements. Throws a runtime
319317 -- error if the dimensions do not match.
320318 fromLists :: [[e]] -> Matrix e cols rows
321319
322−instance FromLists e Void Void where
323− fromLists [] = Empty
324− fromLists _ = error "Wrong dimensions"
325−
326−instance {-# OVERLAPPING #-} FromLists e () () where
320+instance FromLists () () where
327321 fromLists [[e]] = One e
328322 fromLists _ = error "Wrong dimensions"
329323
330−instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
331− fromLists [h : t] = Junc (One h) (fromLists [t])
324+instance (FromLists cols ()) => FromLists (Either () cols) () where
325+ fromLists [h : t] = Join (One h) (fromLists [t])
332326 fromLists _ = error "Wrong dimensions"
333327
334−instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
335− fromLists [l] =
328+instance {-# OVERLAPPABLE #-}
329+ (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
330+ fromLists [l] =
336331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
337− in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
332+ in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
338333 fromLists _ = error "Wrong dimensions"
339334
340−instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
341− fromLists ([h] : t) = Split (One h) (fromLists t)
335+instance (FromLists () rows) => FromLists () (Either () rows) where
336+ fromLists ([h] : t) = Fork (One h) (fromLists t)
342337 fromLists _ = error "Wrong dimensions"
343338
344−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
345− fromLists l@([_] : _) =
339+instance {-# OVERLAPPABLE #-}
340+ (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341+ fromLists l@([_] : _) =
346342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
347− in Split (fromLists (take rowsA l)) (fromLists (drop rowsA l))
343+ in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
348344 fromLists _ = error "Wrong dimensions"
349345
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
346+instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347+ => FromLists (Either a b) (Either c d) where
351348 fromLists l@(h : t) =
352349 let lh = length h
353350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
354− condition = all (== lh) (map length t)
351+ condition = all ((== lh) . length) t
355352 in if lh > 0 && condition
356− then Split (fromLists (take rowsC l)) (fromLists (drop rowsC l))
353+ then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
357354 else error "Not all rows have the same length"
358355
359356-- | Matrix builder function. Constructs a matrix provided with
360−-- a construction function.
361−matrixBuilder ::
357+-- a construction function that operates with indices.
358+matrixBuilder' ::
362359 forall e cols rows.
363− ( FromLists e cols rows,
364− CountableDimensions cols rows
365− ) =>
366− ((Int, Int) -> e) ->
367− Matrix e cols rows
368−matrixBuilder f =
360+ ( FL cols rows,
361+ CountableDims cols rows
362+ ) => ((Int, Int) -> e) -> Matrix e cols rows
363+matrixBuilder' f =
369364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
370365 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
371366 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
372367 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
373368
369+-- | Matrix builder function. Constructs a matrix provided with
370+-- a construction function that operates with arbitrary types.
371+matrixBuilder ::
372+ forall e a b.
373+ ( FLN a b,
374+ Enum a,
375+ Enum b,
376+ Bounded a,
377+ Bounded b,
378+ Eq a,
379+ Countable b
380+ ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381+matrixBuilder f =
382+ let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
383+ positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384+ in fromLists . map (map f) . transpose . buildList r $ positions
385+ where
386+ buildList _ [] = []
387+ buildList r l = take r l : buildList r (drop r l)
388+
374389-- | Constructs a column vector matrix
375−col :: (FromLists e () rows) => [e] -> Matrix e () rows
390+col :: (FL () rows) => [e] -> Matrix e () rows
376391col = fromLists . map (: [])
377392
378393-- | Constructs a row vector matrix
379−row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
394+row :: (FL cols ()) => [e] -> Matrix e cols ()
380395row = fromLists . (: [])
381396
382397-- | Lifts functions to matrices with arbitrary dimensions.
383398--
384399-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
385400-- types @a@ or @b@ allows.
386−fromF ::
401+fromF' ::
387402 forall a b cols rows e.
388403 ( Liftable e a b,
389− CountableDimensions cols rows,
390− FromLists e rows cols
404+ CountableDims cols rows,
405+ FL rows cols
391406 ) =>
392407 (a -> b) ->
393408 Matrix e cols rows
394−fromF f =
409+fromF' f =
395410 let minA = minBound @a
396411 maxA = maxBound @a
397412 minB = minBound @b
@@ -401,8 +416,8 @@ fromF f =
401416 elementsA = take ccols [minA .. maxA]
402417 elementsB = take rrows [minB .. maxB]
403418 combinations = (,) <$> elementsA <*> elementsB
404− combAp = map snd . sort . map (\(a, b) -> if f a == b
405− then ((fromEnum a, fromEnum b), 1)
419+ combAp = map snd . sort . map (\(a, b) -> if f a == b
420+ then ((fromEnum a, fromEnum b), 1)
406421 else ((fromEnum a, fromEnum b), 0)) $ combinations
407422 mList = buildList combAp rrows
408423 in tr $ fromLists mList
@@ -412,41 +427,23 @@ fromF f =
412427
413428-- | Lifts functions to matrices with dimensions matching @a@ and @b@
414429-- cardinality's.
415−fromF' ::
430+fromF ::
416431 forall a b e.
417432 ( Liftable e a b,
418− CountableDimensionsN a b,
419− FromListsN e b a
433+ CountableDimsN a b,
434+ FLN b a
420435 ) =>
421436 (a -> b) ->
422437 Matrix e (Normalize a) (Normalize b)
423−fromF' f =
424− let minA = minBound @a
425− maxA = maxBound @a
426− minB = minBound @b
427− maxB = maxBound @b
428− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
429− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
430− elementsA = take ccols [minA .. maxA]
431− elementsB = take rrows [minB .. maxB]
432− combinations = (,) <$> elementsA <*> elementsB
433− combAp = map snd . sort . map (\(a, b) -> if f a == b
434− then ((fromEnum a, fromEnum b), 1)
435− else ((fromEnum a, fromEnum b), 0)) $ combinations
436− mList = buildList combAp rrows
437− in tr $ fromLists mList
438− where
439− buildList [] _ = []
440− buildList l r = take r l : buildList (drop r l) r
438+fromF = fromF'
441439
442440-- Conversion
443441
444442-- | Converts a matrix to a list of lists of elements.
445443toLists :: Matrix e cols rows -> [[e]]
446−toLists Empty = []
447444toLists (One e) = [[e]]
448−toLists (Split l r) = toLists l ++ toLists r
449−toLists (Junc l r) = zipWith (++) (toLists l) (toLists r)
445+toLists (Fork l r) = toLists l ++ toLists r
446+toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
450447
451448-- | Converts a matrix to a list of elements.
452449toList :: Matrix e cols rows -> [e]
@@ -455,37 +452,38 @@ toList = concat . toLists
455452-- Zeros Matrix
456453
457454-- | The zero matrix. A matrix wholly filled with zeros.
458−zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
459−zeros = matrixBuilder (const 0)
455+zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
456+zeros = matrixBuilder' (const 0)
460457
461458-- Ones Matrix
462459
463460-- | The ones matrix. A matrix wholly filled with ones.
464461--
465462-- Also known as T (Top) matrix.
466−ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
467−ones = matrixBuilder (const 1)
463+ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
464+ones = matrixBuilder' (const 1)
468465
469466-- Const Matrix
470467
471468-- | The constant matrix constructor. A matrix wholly filled with a given
472469-- value.
473−constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
474−constant e = matrixBuilder (const e)
470+constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
471+constant e = matrixBuilder' (const e)
475472
476473-- Bang Matrix
477474
478475-- | The T (Top) row vector matrix.
479−bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
476+bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
480477bang =
481478 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
482479 in fromLists [take c [1, 1 ..]]
483480
484−-- Identity Matrix
481+-- iden Matrix
485482
486−-- | Identity matrix.
487−identity :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
488−identity = matrixBuilder (bool 0 1 . uncurry (==))
483+-- | iden matrix.
484+iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
485+iden = matrixBuilder' (bool 0 1 . uncurry (==))
486+{-# NOINLINE iden #-}
489487
490488-- Matrix composition (MMM)
491489
@@ -494,60 +492,90 @@ identity = matrixBuilder (bool 0 1 . uncurry (==))
494492-- This definition takes advantage of divide-and-conquer and fusion laws
495493-- from LAoP.
496494comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
497−comp Empty Empty = Empty
498−comp (One a) (One b) = One (a * b)
499−comp (Junc a b) (Split c d) = comp a c + comp b d -- Divide-and-conquer law
500−comp (Split a b) c = Split (comp a c) (comp b c) -- Split fusion law
501−comp c (Junc a b) = Junc (comp c a) (comp c b) -- Junc fusion law
495+comp (One a) (One b) = One (a * b)
496+comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
497+comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
498+comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
499+{-# NOINLINE comp #-}
500+{-# RULES
501+ "comp/iden1" forall m. comp m iden = m ;
502+ "comp/iden2" forall m. comp iden m = m
503+#-}
504+
505+-- Scalar multiplication of matrices
506+
507+infixl 7 .|
508+-- | Scalar multiplication of matrices.
509+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
510+(.|) e (One a) = One (e * a)
511+(.|) e (Join a b) = Join (e .| a) (e .| b)
512+(.|) e (Fork a b) = Fork (e .| a) (e .| b)
513+
514+-- Scalar division of matrices
515+
516+infixl 7 ./
517+-- | Scalar multiplication of matrices.
518+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
519+(./) (One a) e = One (a / e)
520+(./) (Join a b) e = Join (a ./ e) (b ./ e)
521+(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
502522
503523-- Projections
504524
505525-- | Biproduct first component projection
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
507−p1 =
508− let iden = identity :: Matrix e m m
509− zero = zeros :: Matrix e n m
510− in junc iden zero
526+p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
527+p1 = join id zeros
511528
512529-- | Biproduct second component projection
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
514−p2 =
515− let iden = identity :: Matrix e n n
516− zero = zeros :: Matrix e m n
517− in junc zero iden
530+p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
531+p2 = join zeros id
518532
519533-- Injections
520534
521535-- | Biproduct first component injection
522−i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
536+i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
523537i1 = tr p1
524538
525539-- | Biproduct second component injection
526−i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
540+i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
527541i2 = tr p2
528542
529543-- Dimensions
530544
531545-- | Obtain the number of rows.
532546--
533−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
534−-- dimensions in constant time.
535−--
536−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
537−-- exchange for performance.
547+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
548+-- dimensions in constant time. For a version that doesn't require the
549+-- constraint see 'rows''.
538550rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
539551rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
540552
553+-- | Obtain the number of rows in an inefficient manner, but without any
554+-- constraints.
555+--
556+-- For a more efficient version see 'rows'.
557+rows' :: Matrix e cols rows -> Int
558+rows' (One _) = 1
559+rows' (Join lhs _) = rows' lhs
560+rows' (Fork top bottom) = rows' top + rows' bottom
561+
541562-- | Obtain the number of columns.
542−--
543−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
544−-- dimensions in constant time.
545563--
546−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
547−-- exchange for performance.
564+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
565+-- dimensions in constant time. For a version that doesn't require the
566+-- constraint see 'columns''.
548567columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
549568columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
550569
570+-- | Obtain the number of columns in an inefficient manner, but without any
571+-- constraints.
572+--
573+-- For a more efficient version see 'columns'.
574+columns' :: Matrix e cols rows -> Int
575+columns' (One _) = 1
576+columns' (Join lhs rhs) = columns' lhs + columns' rhs
577+columns' (Fork top _) = columns' top
578+
551579-- Coproduct Bifunctor
552580
553581infixl 5 -|-
@@ -556,150 +584,172 @@ infixl 5 -|-
556584(-|-) ::
557585 forall e n k m j.
558586 ( Num e,
559− CountableDimensions j k,
560− FromLists e k k,
561− FromLists e j k,
562− FromLists e k j,
563− FromLists e j j
587+ CountableDims j k,
588+ FL k k,
589+ FL j k,
590+ FL k j,
591+ FL j j
564592 ) =>
565593 Matrix e n k ->
566594 Matrix e m j ->
567595 Matrix e (Either n m) (Either k j)
568−(-|-) a b = Junc (i1 . a) (i2 . b)
596+(-|-) a b = Join (i1 . a) (i2 . b)
569597
570598-- Khatri Rao Product and projections
571599
572600-- | Khatri Rao product first component projection matrix.
573−kp1 ::
601+fstM ::
574602 forall e m k .
575603 ( Num e,
576− CountableDimensions k m,
577− FromLists e (Normalize (m, k)) m,
604+ CountableDims k m,
605+ FL (Normalize (m, k)) m,
578606 CountableN (m, k)
579607 ) => Matrix e (Normalize (m, k)) m
580−kp1 = matrixBuilder f
608+fstM = matrixBuilder' f
581609 where
582610 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
583611 f (x, y)
584612 | y >= (x * offset) && y <= (x * offset + offset - 1) = 1
585− | otherwise = 0
613+ | otherwise = 0
586614
587615-- | Khatri Rao product second component projection matrix.
588−kp2 ::
616+sndM ::
589617 forall e m k .
590618 ( Num e,
591− CountableDimensions k m,
592− FromLists e (Normalize (m, k)) k,
619+ CountableDims k m,
620+ FL (Normalize (m, k)) k,
593621 CountableN (m, k)
594622 ) => Matrix e (Normalize (m, k)) k
595−kp2 = matrixBuilder f
623+sndM = matrixBuilder' f
596624 where
597625 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
598626 f (x, y)
599627 | x == y || mod (y - x) offset == 0 = 1
600− | otherwise = 0
628+ | otherwise = 0
601629
602630-- | Khatri Rao Matrix product also known as matrix pairing.
603631--
604632-- NOTE: That this is not a true categorical product, see for instance:
605−--
633+--
606634-- @
607−-- | kp1 . khatri a b == a
608−-- khatri a b ==> |
609−-- | kp2 . khatri a b == b
635+-- | fstM . kr a b == a
636+-- kr a b ==> |
637+-- | sndM . kr a b == b
610638-- @
611639--
612640-- __Emphasis__ on the implication symbol.
613−khatri ::
614− forall e cols a b.
641+kr ::
642+ forall e cols a b.
615643 ( Num e,
616− CountableDimensions a b,
644+ CountableDims a b,
617645 CountableN (a, b),
618− FromLists e (Normalize (a, b)) a,
619− FromLists e (Normalize (a, b)) b
646+ FL (Normalize (a, b)) a,
647+ FL (Normalize (a, b)) b
620648 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
621−khatri a b =
622− let kp1' = kp1 @e @a @b
623− kp2' = kp2 @e @a @b
624− in (tr kp1') . a * (tr kp2') . b
649+kr a b =
650+ let fstM' = fstM @e @a @b
651+ sndM' = sndM @e @a @b
652+ in (tr fstM' . a) * (tr sndM' . b)
625653
626654-- Product Bifunctor (Kronecker)
627655
628656infixl 4 ><
629657
630658-- | Matrix product functor also known as kronecker product
631−(><) ::
632− forall e m p n q.
659+(><) ::
660+ forall e m p n q.
633661 ( Num e,
634− CountableDimensions m n,
635− CountableDimensions p q,
636− CountableDimensionsN (m, n) (p, q),
637− FromLists e (Normalize (m, n)) m,
638− FromLists e (Normalize (m, n)) n,
639− FromLists e (Normalize (p, q)) p,
640− FromLists e (Normalize (p, q)) q
641− )
662+ CountableDims m n,
663+ CountableDims p q,
664+ CountableDimsN (m, n) (p, q),
665+ FL (Normalize (m, n)) m,
666+ FL (Normalize (m, n)) n,
667+ FL (Normalize (p, q)) p,
668+ FL (Normalize (p, q)) q
669+ )
642670 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
643671(><) a b =
644− let kp1' = kp1 @e @m @n
645− kp2' = kp2 @e @m @n
646− in khatri (a . kp1') (b . kp2')
672+ let fstM' = fstM @e @m @n
673+ sndM' = sndM @e @m @n
674+ in kr (a . fstM') (b . sndM')
647675
648−-- Matrix abide Junc Split
676+-- Matrix abide Join Fork
649677
650−-- | Matrix "abiding" followin the 'Junc'-'Split' abide law.
651−--
678+-- | Matrix "abiding" following the 'Join'-'Fork' exchange law.
679+--
652680-- Law:
653681--
654682-- @
655−-- 'Junc' ('Split' a c) ('Split' b d) == 'Split' ('Junc' a b) ('Junc' c d)
683+-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
656684-- @
657−abideJS :: Matrix e cols rows -> Matrix e cols rows
658−abideJS (Junc (Split a c) (Split b d)) = Split (Junc (abideJS a) (abideJS b)) (Junc (abideJS c) (abideJS d)) -- Junc-Split abide law
659−abideJS Empty = Empty
660−abideJS (One e) = One e
661−abideJS (Junc a b) = Junc (abideJS a) (abideJS b)
662−abideJS (Split a b) = Split (abideJS a) (abideJS b)
685+abideJF :: Matrix e cols rows -> Matrix e cols rows
686+abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
687+abideJF (One e) = One e
688+abideJF (Join a b) = Join (abideJF a) (abideJF b)
689+abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
663690
664−-- Matrix abide Split Junc
691+-- Matrix abide Fork Join
665692
666−-- | Matrix "abiding" followin the 'Split'-'Junc' abide law.
667−--
693+-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
694+--
668695-- @
669−-- 'Split' ('Junc' a b) ('Junc' c d) == 'Junc' ('Split' a c) ('Split' b d)
696+-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
670697-- @
671−abideSJ :: Matrix e cols rows -> Matrix e cols rows
672−abideSJ (Split (Junc a b) (Junc c d)) = Junc (Split (abideSJ a) (abideSJ c)) (Split (abideSJ b) (abideSJ d)) -- Split-Junc abide law
673−abideSJ Empty = Empty
674−abideSJ (One e) = One e
675−abideSJ (Junc a b) = Junc (abideSJ a) (abideSJ b)
676−abideSJ (Split a b) = Split (abideSJ a) (abideSJ b)
698+abideFJ :: Matrix e cols rows -> Matrix e cols rows
699+abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
700+abideFJ (One e) = One e
701+abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
702+abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
677703
678704-- Matrix transposition
679705
680706-- | Matrix transposition.
681707tr :: Matrix e cols rows -> Matrix e rows cols
682−tr Empty = Empty
683−tr (One e) = One e
684−tr (Junc a b) = Split (tr a) (tr b)
685−tr (Split a b) = Junc (tr a) (tr b)
708+tr (One e) = One e
709+tr (Join a b) = Fork (tr a) (tr b)
710+tr (Fork a b) = Join (tr a) (tr b)
686711
687712-- Selective 'select' operator
688713
689714-- | Selective functors 'select' operator equivalent inspired by the
690715-- ArrowMonad solution presented in the paper.
691−select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
692−select m y = junc y identity . m
716+select :: (Num e, FL b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
717+select (Fork a b) y = y . a + b -- Divide-and-conquer law
718+select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
719+select m y = join y id . m
720+
721+branch ::
722+ ( Num e,
723+ CountableDims a b,
724+ CountableDims c (Either b c),
725+ FL c b,
726+ FL a b,
727+ FL a a,
728+ FL b b,
729+ FL c c,
730+ FL b a,
731+ FL b c,
732+ FL (Either b c) b,
733+ FL (Either b c) c
734+ )
735+ => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
736+branch x l r = f x `select` g l `select` r
737+ where
738+ f :: (Num e, Countable a, CountableDims b c, FL a b, FL c b, FL b b, FL b a, FL a a)
739+ => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
740+ f m = fork (tr i1) (i1 . tr i2) . m
741+ g :: (Num e, CountableDims b c, FL b c, FL c c) => Matrix e a c -> Matrix e a (Either b c)
742+ g m = i2 . m
693743
694744-- McCarthy's Conditional
695745
696746-- | McCarthy's Conditional expresses probabilistic choice.
697747cond ::
698− ( cols ~ Normalize cols,
748+ ( Trivial cols,
699749 Countable cols,
700− FromLists e () cols,
701− FromLists e cols (),
702− FromLists e cols cols,
750+ FL () cols,
751+ FL cols (),
752+ FL cols cols,
703753 Bounded a,
704754 Enum a,
705755 Num e,
@@ -707,14 +757,14 @@ cond ::
707757 )
708758 =>
709759 (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
710−cond p f g = junc f g . grd p
760+cond p f g = join f g . grd p
711761
712−grd ::
713− ( q ~ Normalize q,
762+grd ::
763+ ( Trivial q,
714764 Countable q,
715− FromLists e () q,
716− FromLists e q (),
717− FromLists e q q,
765+ FL () q,
766+ FL q (),
767+ FL q q,
718768 Bounded a,
719769 Enum a,
720770 Num e,
@@ -722,60 +772,86 @@ grd ::
722772 )
723773 =>
724774 (a -> Bool) -> Matrix e q (Either q q)
725−grd f = split (corr f) (corr (not . f))
775+grd f = fork (corr f) (corr (not . f))
726776
727−corr ::
728− forall e a q .
729− ( q ~ Normalize q,
777+corr ::
778+ forall e a q .
779+ ( Trivial q,
730780 Countable q,
731− FromLists e () q,
732− FromLists e q (),
733− FromLists e q q,
781+ FL () q,
782+ FL q (),
783+ FL q q,
734784 Liftable e a Bool
735− )
785+ )
736786 => (a -> Bool) -> Matrix e q q
737−corr p = let f = fromF p :: Matrix e q ()
738− in khatri f (identity :: Matrix e q q)
787+corr p = let f = fromF' p :: Matrix e q ()
788+ in kr f (id :: Matrix e q q)
739789
740790-- Pretty print
741791
742792prettyAux :: Show e => [[e]] -> [[e]] -> String
743793prettyAux [] _ = ""
744−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
794+prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
745795 where
746796 v = fmap show m
747797 widest = maximum $ fmap length v
748798 fill str = replicate (widest - length str - 2) ' ' ++ str
749−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
799+prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
750800 where
751− v = fmap show m
752− widest = maximum $ fmap length v
801+ v = fmap show m
802+ widest = maximum $ fmap length v
753803 fill str = replicate (widest - length str - 2) ' ' ++ str
754−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
804+prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
755805 prettyAux t l
756806 where
757− v = fmap show l
758− widest = maximum $ fmap length v
807+ v = fmap show l
808+ widest = maximum $ fmap length v
759809 fill str = replicate (widest - length str - 2) ' ' ++ str
760810
761811-- | Matrix pretty printer
762−pretty :: (Countable cols, Show e) => Matrix e cols rows -> String
763−pretty m = "┌ " ++ unwords (replicate (columns m) blank) ++ " ┐\n" ++
764− prettyAux (toLists m) (toLists m) ++
765− "└ " ++ unwords (replicate (columns m) blank) ++ " ┘"
766− where
767− v = fmap show (toList m)
768− widest = maximum $ fmap length v
812+pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
813+pretty m = concat
814+ [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
815+ , unlines
816+ [ "│ " ++ unwords (fmap (\j -> fill $ show $ getElem i j m) [1..columns m]) ++ " │" | i <- [1..rows m] ]
817+ , "└ ", unwords (replicate (columns m) blank), " ┘"
818+ ]
819+ where
820+ strings = map show (toList m)
821+ widest = maximum $ map length strings
769822 fill str = replicate (widest - length str) ' ' ++ str
770− blank = fill ""
823+ blank = fill ""
824+ safeGet i j m
825+ | i > rows m || j > columns m || i < 1 || j < 1 = Nothing
826+ | otherwise = Just $ unsafeGet i j m (toList m)
827+ unsafeGet i j m l = l !! encode (columns m) (i,j)
828+ encode m (i,j) = (i-1)*m + j - 1
829+ getElem i j m =
830+ fromMaybe
831+ (error $
832+ "getElem: Trying to get the "
833+ ++ show (i, j)
834+ ++ " element from a "
835+ ++ show (rows m) ++ "x" ++ show (columns m)
836+ ++ " matrix."
837+ )
838+ (safeGet i j m)
771839
772840-- | Matrix pretty printer
773−prettyPrint :: (Countable cols, Show e) => Matrix e cols rows -> IO ()
841+prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
774842prettyPrint = putStrLn . pretty
775843
844+-- | Zip two matrices with a given binary function
845+zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
846+zipWithM f (One a) (One b) = One (f a b)
847+zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
848+zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
849+zipWithM f x@(Fork _ _) y@(Join _ _) = zipWithM f x (abideJF y)
850+zipWithM f x@(Join _ _) y@(Fork _ _) = zipWithM f (abideJF x) y
851+
776852-- Relational operators functions
777853
778−type Boolean = Natural 0 1
854+type Boolean = Natural 0 1
779855type Relation a b = Matrix Boolean a b
780856
781857-- | Helper conversion function
@@ -791,54 +867,47 @@ fromBool False = nat 0
791867
792868-- | Relational negation
793869negateM :: Relation cols rows -> Relation cols rows
794−negateM Empty = Empty
795−negateM (One (Nat 0)) = One (Nat 1)
796−negateM (One (Nat 1)) = One (Nat 0)
797−negateM (Junc a b) = Junc (negateM a) (negateM b)
798−negateM (Split a b) = Split (negateM a) (negateM b)
870+negateM (One (Nat p)) = One (Nat (negate p))
871+negateM (Join a b) = Join (negateM a) (negateM b)
872+negateM (Fork a b) = Fork (negateM a) (negateM b)
799873
800874-- | Relational addition
801875orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
802−orM Empty Empty = Empty
803−orM (One a) (One b) = One (fromBool (toBool a || toBool b))
804−orM (Junc a b) (Junc c d) = Junc (orM a c) (orM b d)
805−orM (Split a b) (Split c d) = Split (orM a c) (orM b d)
806−orM x@(Split _ _) y@(Junc _ _) = orM x (abideJS y)
807−orM x@(Junc _ _) y@(Split _ _) = orM (abideJS x) y
876+orM (One a) (One b) = One (fromBool (toBool a || toBool b))
877+orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
878+orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
879+orM x@(Fork _ _) y@(Join _ _) = orM x (abideJF y)
880+orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
808881
809882-- | Relational multiplication
810883andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
811−andM Empty Empty = Empty
812−andM (One a) (One b) = One (fromBool (toBool a && toBool b))
813−andM (Junc a b) (Junc c d) = Junc (andM a c) (andM b d)
814−andM (Split a b) (Split c d) = Split (andM a c) (andM b d)
815−andM x@(Split _ _) y@(Junc _ _) = andM x (abideJS y)
816−andM x@(Junc _ _) y@(Split _ _) = andM (abideJS x) y
884+andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885+andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886+andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
887+andM x@(Fork _ _) y@(Join _ _) = andM x (abideJF y)
888+andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
817889
818890-- | Relational subtraction
819891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
820−subM Empty Empty = Empty
821−subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
822−subM (Junc a b) (Junc c d) = Junc (subM a c) (subM b d)
823−subM (Split a b) (Split c d) = Split (subM a c) (subM b d)
824−subM x@(Split _ _) y@(Junc _ _) = subM x (abideJS y)
825−subM x@(Junc _ _) y@(Split _ _) = subM (abideJS x) y
892+subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
893+subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
894+subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
895+subM x@(Fork _ _) y@(Join _ _) = subM x (abideJF y)
896+subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
826897
827898-- | Matrix relational composition.
828899compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
829−compRel Empty Empty = Empty
830−compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
831−compRel (Junc a b) (Split c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
832−compRel (Split a b) c = Split (compRel a c) (compRel b c) -- Split fusion law
833−compRel c (Junc a b) = Junc (compRel c a) (compRel c b) -- Junc fusion law
900+compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
901+compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
902+compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
903+compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion law
834904
835905-- | Matrix relational right division
836906divR :: Relation b c -> Relation b a -> Relation a c
837−divR Empty Empty = Empty
838907divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
839−divR (Junc a b) (Junc c d) = andM (divR a c) (divR b d)
840−divR (Split a b) c = Split (divR a c) (divR b c)
841−divR c (Split a b) = Junc (divR c a) (divR c b)
908+divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
909+divR (Fork a b) c = Fork (divR a c) (divR b c)
910+divR c (Fork a b) = Join (divR c a) (divR c b)
842911
843912-- | Matrix relational left division
844913divL :: Relation c b -> Relation a b -> Relation a c
@@ -854,86 +923,52 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
854923--
855924-- NOTE: Be careful to not ask for a relation bigger than the cardinality of
856925-- types @a@ or @b@ allows.
857−fromFRel ::
926+fromFRel' ::
858927 forall a b cols rows.
859928 ( Liftable Boolean a b,
860− CountableDimensions cols rows,
861− FromLists Boolean rows cols
929+ CountableDims cols rows,
930+ FL rows cols
862931 ) =>
863932 (a -> b) ->
864933 Relation cols rows
865−fromFRel f =
866− let minA = minBound @a
867− maxA = maxBound @a
868− minB = minBound @b
869− maxB = maxBound @b
870− ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
871− rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
872− elementsA = take ccols [minA .. maxA]
873− elementsB = take rrows [minB .. maxB]
874− combinations = (,) <$> elementsA <*> elementsB
875− combAp = map snd . sort . map (\(a, b) -> if f a == b
876− then ((fromEnum a, fromEnum b), nat 1)
877− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
878− mList = buildList combAp rrows
879− in tr $ fromLists mList
880− where
881− buildList [] _ = []
882− buildList l r = take r l : buildList (drop r l) r
934+fromFRel' = fromF'
883935
884936-- | Lifts functions to relations with dimensions matching @a@ and @b@
885937-- cardinality's.
886−fromFRel' ::
938+fromFRel ::
887939 forall a b.
888940 ( Liftable Boolean a b,
889− CountableDimensionsN a b,
890− FromLists Boolean (Normalize b) (Normalize a)
941+ CountableDimsN a b,
942+ FLN b a
891943 ) =>
892944 (a -> b) ->
893945 Relation (Normalize a) (Normalize b)
894−fromFRel' f =
895− let minA = minBound @a
896− maxA = maxBound @a
897− minB = minBound @b
898− maxB = maxBound @b
899− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
900− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
901− elementsA = take ccols [minA .. maxA]
902− elementsB = take rrows [minB .. maxB]
903− combinations = (,) <$> elementsA <*> elementsB
904− combAp = map snd . sort . map (\(a, b) -> if f a == b
905− then ((fromEnum a, fromEnum b), nat 1)
906− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
907− mList = buildList combAp rrows
908− in tr $ fromLists mList
909− where
910− buildList [] _ = []
911− buildList l r = take r l : buildList (drop r l) r
946+fromFRel = fromFRel'
912947
913948-- | Lifts a relation function to a Boolean Matrix
914−toRel ::
949+toRel ::
915950 forall a b.
916951 ( Bounded a,
917952 Bounded b,
918953 Enum a,
919954 Enum b,
920955 Eq b,
921− CountableDimensionsN a b,
922− FromListsN Boolean b a
923− )
956+ CountableDims a b,
957+ FLN b a
958+ )
924959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
925960toRel f =
926961 let minA = minBound @a
927962 maxA = maxBound @a
928963 minB = minBound @b
929964 maxB = maxBound @b
930− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
931− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
932967 elementsA = take ccols [minA .. maxA]
933968 elementsB = take rrows [minB .. maxB]
934969 combinations = (,) <$> elementsA <*> elementsB
935− combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
936− then ((fromEnum a, fromEnum b), nat 1)
970+ combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
971+ then ((fromEnum a, fromEnum b), nat 1)
937972 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
938973 mList = buildList combAp rrows
939974 in tr $ fromLists mList