@@ -1,4 +1,3 @@
1−{-# LANGUAGE StrictData #-}
21{-# LANGUAGE TypeFamilyDependencies #-}
32{-# LANGUAGE ConstraintKinds #-}
43{-# LANGUAGE AllowAmbiguousTypes #-}
@@ -30,7 +29,7 @@
3029-- motivation behind the library, the underlying theory, and implementation details.
3130--
3231-- This module offers many of the combinators mentioned in the work of
33−-- Macedo (2012) and Oliveira (2012).
32+-- Macedo (2012) and Oliveira (2012).
3433--
3534-- This is an Internal module and it is no supposed to be imported.
3635--
@@ -43,26 +42,17 @@ module LAoP.Matrix.Internal
4342 --
4443 -- There exists two type families that make it easier to write
4544 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
47− -- of LAoP combinators.
45+ -- leads to a very straightforward implementation
46+ -- of LAoP combinators.
4847
4948 -- * Type safe matrix representation
5049 Matrix (..),
5150
52− -- * Constraint type aliases
53− Countable,
54− CountableDims,
55− CountableN,
56− CountableDimsN,
57− FL,
58− FLN,
59− Liftable,
60− Trivial,
61−
6251 -- * Primitives
52+ empty,
6353 one,
64− join,
65− fork,
54+ junc,
55+ split,
6656
6757 -- * Auxiliary type families
6858 FromNat,
@@ -75,7 +65,6 @@ module LAoP.Matrix.Internal
7565 toLists,
7666 toList,
7767 matrixBuilder,
78− matrixBuilder',
7968 row,
8069 col,
8170 zeros,
@@ -86,17 +75,11 @@ module LAoP.Matrix.Internal
8675 -- * Misc
8776 -- ** Get dimensions
8877 columns,
89− columns',
9078 rows,
91− rows',
9279
9380 -- ** Matrix Transposition
9481 tr,
9582
96− -- ** Scalar multiplication/division of matrices
97− (.|),
98− (./),
99−
10083 -- ** Selective operator
10184 select,
10285 branch,
@@ -105,19 +88,16 @@ module LAoP.Matrix.Internal
10588 cond,
10689
10790 -- ** Matrix "abiding"
108− abideJF,
109− abideFJ,
110−
111− -- ** Zip matrices
112− zipWithM,
91+ abideJS,
92+ abideSJ,
11393
11494 -- * Biproduct approach
115− -- ** Fork
95+ -- ** Split
11696 (===),
11797 -- *** Projections
11898 p1,
11999 p2,
120− -- ** Join
100+ -- ** Junc
121101 (|||),
122102 -- *** Injections
123103 i1,
@@ -130,13 +110,12 @@ module LAoP.Matrix.Internal
130110
131111 -- | Note that given the restrictions imposed it is not possible to
132112 -- implement the standard type classes present in standard Haskell.
133−
134113 -- *** Matrix pairing projections
135− fstM,
136− sndM,
114+ kp1,
115+ kp2,
137116
138117 -- *** Matrix pairing
139− kr,
118+ khatri,
140119
141120 -- * Matrix composition and lifting
142121
@@ -144,10 +123,10 @@ module LAoP.Matrix.Internal
144123
145124 -- | Note that given the restrictions imposed it is not possible to
146125 -- implement the standard type classes present in standard Haskell.
147− iden,
126+ identity,
148127 comp,
149− fromF',
150128 fromF,
129+ fromF',
151130
152131 -- * Matrix printing
153132 pretty,
@@ -174,22 +153,21 @@ import LAoP.Utils.Internal
174153import Data.Bool
175154import Data.Kind
176155import Data.List
177−import Data.Maybe
178156import Data.Proxy
179−import Data.Typeable
180157import Data.Void
181158import GHC.TypeLits
182159import Data.Type.Equality
183160import GHC.Generics
184161import Control.DeepSeq
185−import Prelude hiding (id, (.))
186−import qualified Prelude (id, (.))
162+import Control.Category
163+import Prelude hiding ((.))
187164
188165-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
189166data Matrix e cols rows where
167+ Empty :: Matrix e Void Void
190168 One :: e -> Matrix e () ()
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)
169+ Junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
170+ Split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
193171
194172deriving instance (Show e) => Show (Matrix e cols rows)
195173
@@ -216,95 +194,122 @@ type family Count (d :: Type) :: Nat where
216194--
217195-- Thanks to Li-Yao Xia this type family is super fast.
218196type family FromNat (n :: Nat) :: Type where
197+ FromNat 0 = Void
219198 FromNat 1 = ()
220199 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
221200
222201type family FromNat' (b :: Bool) (m :: Type) :: Type where
223− FromNat' 'True m = Either m m
202+ FromNat' 'True m = Either m m
224203 FromNat' 'False m = Either () (Either m m)
225204
226205-- | Type family that normalizes the representation of a given data
227206-- structure
228207type family Normalize (d :: Type) :: Type where
229− Normalize (Either a b) = Either (Normalize a) (Normalize b)
230− Normalize d = FromNat (Count d)
208+ Normalize (Either a b) = Either (Normalize a) (Normalize b)
209+ Normalize d = FromNat (Count d)
231210
232211-- | Constraint type synonyms to keep the type signatures less convoluted
233212type Countable a = KnownNat (Count a)
234213type CountableN a = KnownNat (Count (Normalize a))
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)
214+type CountableDimensions a b = (Countable a, Countable b)
215+type CountableDimensionsN a b = (CountableN a, CountableN b)
216+type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
239217type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
240218type Trivial a = FromNat (Count a) ~ a
241219
242−-- | It is possible to implement a constrained version of the category type
243−-- class.
220+-- | It isn't possible to implement the 'id' function so it's
221+-- implementation is 'undefined'. However 'comp' can be and this partial
222+-- class implementation exists just to make the code more readable.
223+--
224+-- Please use 'identity' instead.
244225instance (Num e) => Category (Matrix e) where
245− type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
247− (.) = comp
226+ id = undefined
227+ (.) = comp
248228
249229instance NFData e => NFData (Matrix e cols rows) where
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
230+ rnf Empty = ()
231+ rnf (One e) = rnf e
232+ rnf (Junc a b) = rnf a `seq` rnf b
233+ rnf (Split a b) = rnf a `seq` rnf b
253234
254235instance Eq e => Eq (Matrix e cols rows) where
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
236+ Empty == Empty = True
237+ (One a) == (One b) = a == b
238+ (Junc a b) == (Junc c d) = a == c && b == d
239+ (Split a b) == (Split c d) = a == c && b == d
240+ x@(Split _ _) == y@(Junc _ _) = x == abideJS y
241+ x@(Junc _ _) == y@(Split _ _) = abideJS x == y
260242
261243instance 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
267244
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)
245+ Empty + Empty = Empty
246+ (One a) + (One b) = One (a + b)
247+ (Junc a b) + (Junc c d) = Junc (a + c) (b + d)
248+ (Split a b) + (Split c d) = Split (a + c) (b + d)
249+ x@(Split _ _) + y@(Junc _ _) = x + abideJS y
250+ x@(Junc _ _) + y@(Split _ _) = abideJS x + y
251+
252+ Empty - Empty = Empty
253+ (One a) - (One b) = One (a - b)
254+ (Junc a b) - (Junc c d) = Junc (a - c) (b - d)
255+ (Split a b) - (Split c d) = Split (a - c) (b - d)
256+ x@(Split _ _) - y@(Junc _ _) = x - abideJS y
257+ x@(Junc _ _) - y@(Split _ _) = abideJS x - y
258+
259+ Empty * Empty = Empty
260+ (One a) * (One b) = One (a * b)
261+ (Junc a b) * (Junc c d) = Junc (a * c) (b * d)
262+ (Split a b) * (Split c d) = Split (a * c) (b * d)
263+ x@(Split _ _) * y@(Junc _ _) = x * abideJS y
264+ x@(Junc _ _) * y@(Split _ _) = abideJS x * y
265+
266+ abs Empty = Empty
267+ abs (One a) = One (abs a)
268+ abs (Junc a b) = Junc (abs a) (abs b)
269+ abs (Split a b) = Split (abs a) (abs b)
270+
271+ signum Empty = Empty
272+ signum (One a) = One (signum a)
273+ signum (Junc a b) = Junc (signum a) (signum b)
274+ signum (Split a b) = Split (signum a) (signum b)
275275
276276instance Ord e => Ord (Matrix e cols rows) where
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
277+ Empty <= Empty = True
278+ (One a) <= (One b) = a <= b
279+ (Junc a b) <= (Junc c d) = (a <= c) && (b <= d)
280+ (Split a b) <= (Split c d) = (a <= c) && (b <= d)
281+ x@(Split _ _) <= y@(Junc _ _) = x <= abideJS y
282+ x@(Junc _ _) <= y@(Split _ _) = abideJS x <= y
282283
283284-- Primitives
284285
286+-- | Empty matrix constructor
287+empty :: Matrix e Void Void
288+empty = Empty
289+
285290-- | Unit matrix constructor
286291one :: e -> Matrix e () ()
287292one = One
288293
289−-- | Matrix 'Join' constructor
290−join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
291−join = Join
294+-- | Matrix 'Junc' constructor
295+junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
296+junc = Junc
292297
293298infixl 3 |||
294299
295−-- | Matrix 'Join' constructor
300+-- | Matrix 'Junc' constructor
296301(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
297−(|||) = Join
302+(|||) = Junc
298303
299−-- | Matrix 'Fork' constructor
300−fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
301−fork = Fork
304+-- | Matrix 'Split' constructor
305+split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
306+split = Split
302307
303308infixl 2 ===
304309
305−-- | Matrix 'Fork' constructor
310+-- | Matrix 'Split' constructor
306311(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
307−(===) = Fork
312+(===) = Split
308313
309314-- Construction
310315
@@ -312,101 +317,84 @@ infixl 2 ===
312317--
313318-- Given that it is not possible to branch on types at the term level type
314319-- classes are needed very much like an inductive definition but on types.
315−class FromLists cols rows where
320+class FromLists e cols rows where
316321 -- | Build a matrix out of a list of list of elements. Throws a runtime
317322 -- error if the dimensions do not match.
318323 fromLists :: [[e]] -> Matrix e cols rows
319324
320−instance FromLists () () where
325+instance FromLists e Void Void where
326+ fromLists [] = Empty
327+ fromLists _ = error "Wrong dimensions"
328+
329+instance {-# OVERLAPPING #-} FromLists e () () where
321330 fromLists [[e]] = One e
322331 fromLists _ = error "Wrong dimensions"
323332
324−instance (FromLists cols ()) => FromLists (Either () cols) () where
325− fromLists [h : t] = Join (One h) (fromLists [t])
333+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
334+ fromLists [h : t] = Junc (One h) (fromLists [t])
326335 fromLists _ = error "Wrong dimensions"
327336
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
330− fromLists [l] =
337+instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
338+ fromLists [l] =
331339 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332− in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
340+ in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
333341 fromLists _ = error "Wrong dimensions"
334342
335−instance (FromLists () rows) => FromLists () (Either () rows) where
336− fromLists ([h] : t) = Fork (One h) (fromLists t)
343+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
344+ fromLists ([h] : t) = Split (One h) (fromLists t)
337345 fromLists _ = error "Wrong dimensions"
338346
339−instance {-# OVERLAPPABLE #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341− fromLists l@([_] : _) =
347+instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
348+ fromLists l@([_] : _) =
342349 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343− in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
350+ in Split (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344351 fromLists _ = error "Wrong dimensions"
345352
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
353+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
348354 fromLists l@(h : t) =
349355 let lh = length h
350356 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
351− condition = all ((== lh) . length) t
357+ condition = all (== lh) (map length t)
352358 in if lh > 0 && condition
353− then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
359+ then Split (fromLists (take rowsC l)) (fromLists (drop rowsC l))
354360 else error "Not all rows have the same length"
355361
356362-- | Matrix builder function. Constructs a matrix provided with
357−-- a construction function that operates with indices.
358−matrixBuilder' ::
363+-- a construction function.
364+matrixBuilder ::
359365 forall e cols rows.
360− ( FL cols rows,
361− CountableDims cols rows
362− ) => ((Int, Int) -> e) -> Matrix e cols rows
363−matrixBuilder' f =
366+ ( FromLists e cols rows,
367+ CountableDimensions cols rows
368+ ) =>
369+ ((Int, Int) -> e) ->
370+ Matrix e cols rows
371+matrixBuilder f =
364372 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
365373 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
366374 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
367375 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
368376
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−
389377-- | Constructs a column vector matrix
390−col :: (FL () rows) => [e] -> Matrix e () rows
378+col :: (FromLists e () rows) => [e] -> Matrix e () rows
391379col = fromLists . map (: [])
392380
393381-- | Constructs a row vector matrix
394−row :: (FL cols ()) => [e] -> Matrix e cols ()
382+row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
395383row = fromLists . (: [])
396384
397385-- | Lifts functions to matrices with arbitrary dimensions.
398386--
399387-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
400388-- types @a@ or @b@ allows.
401−fromF' ::
389+fromF ::
402390 forall a b cols rows e.
403391 ( Liftable e a b,
404− CountableDims cols rows,
405− FL rows cols
392+ CountableDimensions cols rows,
393+ FromLists e rows cols
406394 ) =>
407395 (a -> b) ->
408396 Matrix e cols rows
409−fromF' f =
397+fromF f =
410398 let minA = minBound @a
411399 maxA = maxBound @a
412400 minB = minBound @b
@@ -416,8 +404,8 @@ fromF' f =
416404 elementsA = take ccols [minA .. maxA]
417405 elementsB = take rrows [minB .. maxB]
418406 combinations = (,) <$> elementsA <*> elementsB
419− combAp = map snd . sort . map (\(a, b) -> if f a == b
420− then ((fromEnum a, fromEnum b), 1)
407+ combAp = map snd . sort . map (\(a, b) -> if f a == b
408+ then ((fromEnum a, fromEnum b), 1)
421409 else ((fromEnum a, fromEnum b), 0)) $ combinations
422410 mList = buildList combAp rrows
423411 in tr $ fromLists mList
@@ -427,23 +415,41 @@ fromF' f =
427415
428416-- | Lifts functions to matrices with dimensions matching @a@ and @b@
429417-- cardinality's.
430−fromF ::
418+fromF' ::
431419 forall a b e.
432420 ( Liftable e a b,
433− CountableDimsN a b,
434− FLN b a
421+ CountableDimensionsN a b,
422+ FromListsN e b a
435423 ) =>
436424 (a -> b) ->
437425 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
426+fromF' f =
427+ let minA = minBound @a
428+ maxA = maxBound @a
429+ minB = minBound @b
430+ maxB = maxBound @b
431+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
432+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
433+ elementsA = take ccols [minA .. maxA]
434+ elementsB = take rrows [minB .. maxB]
435+ combinations = (,) <$> elementsA <*> elementsB
436+ combAp = map snd . sort . map (\(a, b) -> if f a == b
437+ then ((fromEnum a, fromEnum b), 1)
438+ else ((fromEnum a, fromEnum b), 0)) $ combinations
439+ mList = buildList combAp rrows
440+ in tr $ fromLists mList
441+ where
442+ buildList [] _ = []
443+ buildList l r = take r l : buildList (drop r l) r
439444
440445-- Conversion
441446
442447-- | Converts a matrix to a list of lists of elements.
443448toLists :: Matrix e cols rows -> [[e]]
449+toLists Empty = []
444450toLists (One e) = [[e]]
445−toLists (Fork l r) = toLists l ++ toLists r
446−toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
451+toLists (Split l r) = toLists l ++ toLists r
452+toLists (Junc l r) = zipWith (++) (toLists l) (toLists r)
447453
448454-- | Converts a matrix to a list of elements.
449455toList :: Matrix e cols rows -> [e]
@@ -452,38 +458,37 @@ toList = concat . toLists
452458-- Zeros Matrix
453459
454460-- | The zero matrix. A matrix wholly filled with zeros.
455−zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
456−zeros = matrixBuilder' (const 0)
461+zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
462+zeros = matrixBuilder (const 0)
457463
458464-- Ones Matrix
459465
460466-- | The ones matrix. A matrix wholly filled with ones.
461467--
462468-- Also known as T (Top) matrix.
463−ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
464−ones = matrixBuilder' (const 1)
469+ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
470+ones = matrixBuilder (const 1)
465471
466472-- Const Matrix
467473
468474-- | The constant matrix constructor. A matrix wholly filled with a given
469475-- value.
470−constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
471−constant e = matrixBuilder' (const e)
476+constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
477+constant e = matrixBuilder (const e)
472478
473479-- Bang Matrix
474480
475481-- | The T (Top) row vector matrix.
476−bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
482+bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
477483bang =
478484 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
479485 in fromLists [take c [1, 1 ..]]
480486
481−-- iden Matrix
487+-- Identity Matrix
482488
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 #-}
489+-- | Identity matrix.
490+identity :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
491+identity = matrixBuilder (bool 0 1 . uncurry (==))
487492
488493-- Matrix composition (MMM)
489494
@@ -492,90 +497,60 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
492497-- This definition takes advantage of divide-and-conquer and fusion laws
493498-- from LAoP.
494499comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
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)
500+comp Empty Empty = Empty
501+comp (One a) (One b) = One (a * b)
502+comp (Junc a b) (Split c d) = comp a c + comp b d -- Divide-and-conquer law
503+comp (Split a b) c = Split (comp a c) (comp b c) -- Split fusion law
504+comp c (Junc a b) = Junc (comp c a) (comp c b) -- Junc fusion law
522505
523506-- Projections
524507
525508-- | Biproduct first component projection
526−p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
527−p1 = join id zeros
509+p1 :: forall e m n. (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
510+p1 =
511+ let iden = identity :: Matrix e m m
512+ zero = zeros :: Matrix e n m
513+ in junc iden zero
528514
529515-- | Biproduct second component projection
530−p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
531−p2 = join zeros id
516+p2 :: forall e m n. (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
517+p2 =
518+ let iden = identity :: Matrix e n n
519+ zero = zeros :: Matrix e m n
520+ in junc zero iden
532521
533522-- Injections
534523
535524-- | Biproduct first component injection
536−i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
525+i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
537526i1 = tr p1
538527
539528-- | Biproduct second component injection
540−i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
529+i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
541530i2 = tr p2
542531
543532-- Dimensions
544533
545534-- | Obtain the number of rows.
546535--
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''.
536+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
537+-- dimensions in constant time.
538+--
539+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
540+-- exchange for performance.
550541rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551542rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552543
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−
562544-- | Obtain the number of columns.
545+--
546+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
547+-- dimensions in constant time.
563548--
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''.
549+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
550+-- exchange for performance.
567551columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
568552columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569553
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−
579554-- Coproduct Bifunctor
580555
581556infixl 5 -|-
@@ -584,161 +559,164 @@ infixl 5 -|-
584559(-|-) ::
585560 forall e n k m j.
586561 ( Num e,
587− CountableDims j k,
588− FL k k,
589− FL j k,
590− FL k j,
591− FL j j
562+ CountableDimensions j k,
563+ FromLists e k k,
564+ FromLists e j k,
565+ FromLists e k j,
566+ FromLists e j j
592567 ) =>
593568 Matrix e n k ->
594569 Matrix e m j ->
595570 Matrix e (Either n m) (Either k j)
596−(-|-) a b = Join (i1 . a) (i2 . b)
571+(-|-) a b = Junc (i1 . a) (i2 . b)
597572
598573-- Khatri Rao Product and projections
599574
600575-- | Khatri Rao product first component projection matrix.
601−fstM ::
576+kp1 ::
602577 forall e m k .
603578 ( Num e,
604− CountableDims k m,
605− FL (Normalize (m, k)) m,
579+ CountableDimensions k m,
580+ FromLists e (Normalize (m, k)) m,
606581 CountableN (m, k)
607582 ) => Matrix e (Normalize (m, k)) m
608−fstM = matrixBuilder' f
583+kp1 = matrixBuilder f
609584 where
610585 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
611586 f (x, y)
612587 | y >= (x * offset) && y <= (x * offset + offset - 1) = 1
613− | otherwise = 0
588+ | otherwise = 0
614589
615590-- | Khatri Rao product second component projection matrix.
616−sndM ::
591+kp2 ::
617592 forall e m k .
618593 ( Num e,
619− CountableDims k m,
620− FL (Normalize (m, k)) k,
594+ CountableDimensions k m,
595+ FromLists e (Normalize (m, k)) k,
621596 CountableN (m, k)
622597 ) => Matrix e (Normalize (m, k)) k
623−sndM = matrixBuilder' f
598+kp2 = matrixBuilder f
624599 where
625600 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
626601 f (x, y)
627602 | x == y || mod (y - x) offset == 0 = 1
628− | otherwise = 0
603+ | otherwise = 0
629604
630605-- | Khatri Rao Matrix product also known as matrix pairing.
631606--
632607-- NOTE: That this is not a true categorical product, see for instance:
633−--
608+--
634609-- @
635−-- | fstM . kr a b == a
636−-- kr a b ==> |
637−-- | sndM . kr a b == b
610+-- | kp1 . khatri a b == a
611+-- khatri a b ==> |
612+-- | kp2 . khatri a b == b
638613-- @
639614--
640615-- __Emphasis__ on the implication symbol.
641−kr ::
642− forall e cols a b.
616+khatri ::
617+ forall e cols a b.
643618 ( Num e,
644− CountableDims a b,
619+ CountableDimensions a b,
645620 CountableN (a, b),
646− FL (Normalize (a, b)) a,
647− FL (Normalize (a, b)) b
621+ FromLists e (Normalize (a, b)) a,
622+ FromLists e (Normalize (a, b)) b
648623 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, 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)
624+khatri a b =
625+ let kp1' = kp1 @e @a @b
626+ kp2' = kp2 @e @a @b
627+ in (tr kp1') . a * (tr kp2') . b
653628
654629-- Product Bifunctor (Kronecker)
655630
656631infixl 4 ><
657632
658633-- | Matrix product functor also known as kronecker product
659−(><) ::
660− forall e m p n q.
634+(><) ::
635+ forall e m p n q.
661636 ( Num e,
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− )
637+ CountableDimensions m n,
638+ CountableDimensions p q,
639+ CountableDimensionsN (m, n) (p, q),
640+ FromLists e (Normalize (m, n)) m,
641+ FromLists e (Normalize (m, n)) n,
642+ FromLists e (Normalize (p, q)) p,
643+ FromLists e (Normalize (p, q)) q
644+ )
670645 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671646(><) a b =
672− let fstM' = fstM @e @m @n
673− sndM' = sndM @e @m @n
674− in kr (a . fstM') (b . sndM')
647+ let kp1' = kp1 @e @m @n
648+ kp2' = kp2 @e @m @n
649+ in khatri (a . kp1') (b . kp2')
675650
676−-- Matrix abide Join Fork
651+-- Matrix abide Junc Split
677652
678−-- | Matrix "abiding" following the 'Join'-'Fork' exchange law.
679−--
653+-- | Matrix "abiding" followin the 'Junc'-'Split' abide law.
654+--
680655-- Law:
681656--
682657-- @
683−-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
658+-- 'Junc' ('Split' a c) ('Split' b d) == 'Split' ('Junc' a b) ('Junc' c d)
684659-- @
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)
660+abideJS :: Matrix e cols rows -> Matrix e cols rows
661+abideJS (Junc (Split a c) (Split b d)) = Split (Junc (abideJS a) (abideJS b)) (Junc (abideJS c) (abideJS d)) -- Junc-Split abide law
662+abideJS Empty = Empty
663+abideJS (One e) = One e
664+abideJS (Junc a b) = Junc (abideJS a) (abideJS b)
665+abideJS (Split a b) = Split (abideJS a) (abideJS b)
690666
691−-- Matrix abide Fork Join
667+-- Matrix abide Split Junc
692668
693−-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
694−--
669+-- | Matrix "abiding" followin the 'Split'-'Junc' abide law.
670+--
695671-- @
696−-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
672+-- 'Split' ('Junc' a b) ('Junc' c d) == 'Junc' ('Split' a c) ('Split' b d)
697673-- @
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)
674+abideSJ :: Matrix e cols rows -> Matrix e cols rows
675+abideSJ (Split (Junc a b) (Junc c d)) = Junc (Split (abideSJ a) (abideSJ c)) (Split (abideSJ b) (abideSJ d)) -- Split-Junc abide law
676+abideSJ Empty = Empty
677+abideSJ (One e) = One e
678+abideSJ (Junc a b) = Junc (abideSJ a) (abideSJ b)
679+abideSJ (Split a b) = Split (abideSJ a) (abideSJ b)
703680
704681-- Matrix transposition
705682
706683-- | Matrix transposition.
707684tr :: Matrix e cols rows -> Matrix e rows cols
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)
685+tr Empty = Empty
686+tr (One e) = One e
687+tr (Junc a b) = Split (tr a) (tr b)
688+tr (Split a b) = Junc (tr a) (tr b)
711689
712690-- Selective 'select' operator
713691
714692-- | Selective functors 'select' operator equivalent inspired by the
715693-- ArrowMonad solution presented in the paper.
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
694+select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
695+select (Split a b) y = y . a + b -- Divide-and-conquer law
696+select (Junc (Split a c) (Split b d)) y = junc (y . a + c) (y . b + d) -- Pattern matching + DnC law
697+select m y = junc y identity . m
720698
721−branch ::
699+branch ::
722700 ( 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
701+ CountableDimensions a b,
702+ CountableDimensions c (Either b c),
703+ FromLists e c b,
704+ FromLists e a b,
705+ FromLists e a a,
706+ FromLists e b b,
707+ FromLists e c c,
708+ FromLists e b a,
709+ FromLists e b c,
710+ FromLists e (Either b c) b,
711+ FromLists e (Either b c) c
734712 )
735713 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
736714branch x l r = f x `select` g l `select` r
737715 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)
716+ f :: (Num e, Countable a, CountableDimensions b c, FromLists e a b, FromLists e c b, FromLists e b b, FromLists e b a, FromLists e a a)
739717 => 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)
718+ f m = split (tr i1) (i1 . tr i2) . m
719+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
742720 g m = i2 . m
743721
744722-- McCarthy's Conditional
@@ -747,9 +725,9 @@ branch x l r = f x `select` g l `select` r
747725cond ::
748726 ( Trivial cols,
749727 Countable cols,
750− FL () cols,
751− FL cols (),
752− FL cols cols,
728+ FromLists e () cols,
729+ FromLists e cols (),
730+ FromLists e cols cols,
753731 Bounded a,
754732 Enum a,
755733 Num e,
@@ -757,14 +735,14 @@ cond ::
757735 )
758736 =>
759737 (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
760−cond p f g = join f g . grd p
738+cond p f g = junc f g . grd p
761739
762−grd ::
740+grd ::
763741 ( Trivial q,
764742 Countable q,
765− FL () q,
766− FL q (),
767− FL q q,
743+ FromLists e () q,
744+ FromLists e q (),
745+ FromLists e q q,
768746 Bounded a,
769747 Enum a,
770748 Num e,
@@ -772,86 +750,60 @@ grd ::
772750 )
773751 =>
774752 (a -> Bool) -> Matrix e q (Either q q)
775−grd f = fork (corr f) (corr (not . f))
753+grd f = split (corr f) (corr (not . f))
776754
777−corr ::
778− forall e a q .
755+corr ::
756+ forall e a q .
779757 ( Trivial q,
780758 Countable q,
781− FL () q,
782− FL q (),
783− FL q q,
759+ FromLists e () q,
760+ FromLists e q (),
761+ FromLists e q q,
784762 Liftable e a Bool
785− )
763+ )
786764 => (a -> Bool) -> Matrix e q q
787−corr p = let f = fromF' p :: Matrix e q ()
788− in kr f (id :: Matrix e q q)
765+corr p = let f = fromF p :: Matrix e q ()
766+ in khatri f (identity :: Matrix e q q)
789767
790768-- Pretty print
791769
792770prettyAux :: Show e => [[e]] -> [[e]] -> String
793771prettyAux [] _ = ""
794−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
772+prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
795773 where
796774 v = fmap show m
797775 widest = maximum $ fmap length v
798776 fill str = replicate (widest - length str - 2) ' ' ++ str
799−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
777+prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
800778 where
801− v = fmap show m
802− widest = maximum $ fmap length v
779+ v = fmap show m
780+ widest = maximum $ fmap length v
803781 fill str = replicate (widest - length str - 2) ' ' ++ str
804−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
782+prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
805783 prettyAux t l
806784 where
807− v = fmap show l
808− widest = maximum $ fmap length v
785+ v = fmap show l
786+ widest = maximum $ fmap length v
809787 fill str = replicate (widest - length str - 2) ' ' ++ str
810788
811789-- | Matrix pretty printer
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
790+pretty :: (Countable cols, Show e) => Matrix e cols rows -> String
791+pretty m = "┌ " ++ unwords (replicate (columns m) blank) ++ " ┐\n" ++
792+ prettyAux (toLists m) (toLists m) ++
793+ "└ " ++ unwords (replicate (columns m) blank) ++ " ┘"
794+ where
795+ v = fmap show (toList m)
796+ widest = maximum $ fmap length v
822797 fill str = replicate (widest - length str) ' ' ++ str
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)
798+ blank = fill ""
839799
840800-- | Matrix pretty printer
841−prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
801+prettyPrint :: (Countable cols, Show e) => Matrix e cols rows -> IO ()
842802prettyPrint = putStrLn . pretty
843803
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−
852804-- Relational operators functions
853805
854−type Boolean = Natural 0 1
806+type Boolean = Natural 0 1
855807type Relation a b = Matrix Boolean a b
856808
857809-- | Helper conversion function
@@ -867,47 +819,54 @@ fromBool False = nat 0
867819
868820-- | Relational negation
869821negateM :: Relation cols rows -> Relation cols rows
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)
822+negateM Empty = Empty
823+negateM (One (Nat 0)) = One (Nat 1)
824+negateM (One (Nat 1)) = One (Nat 0)
825+negateM (Junc a b) = Junc (negateM a) (negateM b)
826+negateM (Split a b) = Split (negateM a) (negateM b)
873827
874828-- | Relational addition
875829orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
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
830+orM Empty Empty = Empty
831+orM (One a) (One b) = One (fromBool (toBool a || toBool b))
832+orM (Junc a b) (Junc c d) = Junc (orM a c) (orM b d)
833+orM (Split a b) (Split c d) = Split (orM a c) (orM b d)
834+orM x@(Split _ _) y@(Junc _ _) = orM x (abideJS y)
835+orM x@(Junc _ _) y@(Split _ _) = orM (abideJS x) y
881836
882837-- | Relational multiplication
883838andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
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
839+andM Empty Empty = Empty
840+andM (One a) (One b) = One (fromBool (toBool a && toBool b))
841+andM (Junc a b) (Junc c d) = Junc (andM a c) (andM b d)
842+andM (Split a b) (Split c d) = Split (andM a c) (andM b d)
843+andM x@(Split _ _) y@(Junc _ _) = andM x (abideJS y)
844+andM x@(Junc _ _) y@(Split _ _) = andM (abideJS x) y
889845
890846-- | Relational subtraction
891847subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
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
848+subM Empty Empty = Empty
849+subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
850+subM (Junc a b) (Junc c d) = Junc (subM a c) (subM b d)
851+subM (Split a b) (Split c d) = Split (subM a c) (subM b d)
852+subM x@(Split _ _) y@(Junc _ _) = subM x (abideJS y)
853+subM x@(Junc _ _) y@(Split _ _) = subM (abideJS x) y
897854
898855-- | Matrix relational composition.
899856compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
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
857+compRel Empty Empty = Empty
858+compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
859+compRel (Junc a b) (Split c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
860+compRel (Split a b) c = Split (compRel a c) (compRel b c) -- Split fusion law
861+compRel c (Junc a b) = Junc (compRel c a) (compRel c b) -- Junc fusion law
904862
905863-- | Matrix relational right division
906864divR :: Relation b c -> Relation b a -> Relation a c
865+divR Empty Empty = Empty
907866divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
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)
867+divR (Junc a b) (Junc c d) = andM (divR a c) (divR b d)
868+divR (Split a b) c = Split (divR a c) (divR b c)
869+divR c (Split a b) = Junc (divR c a) (divR c b)
911870
912871-- | Matrix relational left division
913872divL :: Relation c b -> Relation a b -> Relation a c
@@ -923,52 +882,86 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
923882--
924883-- NOTE: Be careful to not ask for a relation bigger than the cardinality of
925884-- types @a@ or @b@ allows.
926−fromFRel' ::
885+fromFRel ::
927886 forall a b cols rows.
928887 ( Liftable Boolean a b,
929− CountableDims cols rows,
930− FL rows cols
888+ CountableDimensions cols rows,
889+ FromLists Boolean rows cols
931890 ) =>
932891 (a -> b) ->
933892 Relation cols rows
934−fromFRel' = fromF'
893+fromFRel f =
894+ let minA = minBound @a
895+ maxA = maxBound @a
896+ minB = minBound @b
897+ maxB = maxBound @b
898+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
899+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
900+ elementsA = take ccols [minA .. maxA]
901+ elementsB = take rrows [minB .. maxB]
902+ combinations = (,) <$> elementsA <*> elementsB
903+ combAp = map snd . sort . map (\(a, b) -> if f a == b
904+ then ((fromEnum a, fromEnum b), nat 1)
905+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
906+ mList = buildList combAp rrows
907+ in tr $ fromLists mList
908+ where
909+ buildList [] _ = []
910+ buildList l r = take r l : buildList (drop r l) r
935911
936912-- | Lifts functions to relations with dimensions matching @a@ and @b@
937913-- cardinality's.
938−fromFRel ::
914+fromFRel' ::
939915 forall a b.
940916 ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
917+ CountableDimensionsN a b,
918+ FromLists Boolean (Normalize b) (Normalize a)
943919 ) =>
944920 (a -> b) ->
945921 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
922+fromFRel' f =
923+ let minA = minBound @a
924+ maxA = maxBound @a
925+ minB = minBound @b
926+ maxB = maxBound @b
927+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
928+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
929+ elementsA = take ccols [minA .. maxA]
930+ elementsB = take rrows [minB .. maxB]
931+ combinations = (,) <$> elementsA <*> elementsB
932+ combAp = map snd . sort . map (\(a, b) -> if f a == b
933+ then ((fromEnum a, fromEnum b), nat 1)
934+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
935+ mList = buildList combAp rrows
936+ in tr $ fromLists mList
937+ where
938+ buildList [] _ = []
939+ buildList l r = take r l : buildList (drop r l) r
947940
948941-- | Lifts a relation function to a Boolean Matrix
949−toRel ::
942+toRel ::
950943 forall a b.
951944 ( Bounded a,
952945 Bounded b,
953946 Enum a,
954947 Enum b,
955948 Eq b,
956− CountableDims a b,
957− FLN b a
958− )
949+ CountableDimensionsN a b,
950+ FromListsN Boolean b a
951+ )
959952 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
960953toRel f =
961954 let minA = minBound @a
962955 maxA = maxBound @a
963956 minB = minBound @b
964957 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
958+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
959+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
967960 elementsA = take ccols [minA .. maxA]
968961 elementsB = take rrows [minB .. maxB]
969962 combinations = (,) <$> elementsA <*> elementsB
970− combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
971− then ((fromEnum a, fromEnum b), nat 1)
963+ combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
964+ then ((fromEnum a, fromEnum b), nat 1)
972965 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
973966 mList = buildList combAp rrows
974967 in tr $ fromLists mList