@@ -1,6 +1,4 @@
1−{-# LANGUAGE StrictData #-}
21{-# LANGUAGE TypeFamilyDependencies #-}
3−{-# LANGUAGE ConstraintKinds #-}
42{-# LANGUAGE AllowAmbiguousTypes #-}
53{-# LANGUAGE DataKinds #-}
64{-# LANGUAGE FlexibleContexts #-}
@@ -30,7 +28,7 @@
3028-- motivation behind the library, the underlying theory, and implementation details.
3129--
3230-- This module offers many of the combinators mentioned in the work of
33−-- Macedo (2012) and Oliveira (2012).
31+-- Macedo (2012) and Oliveira (2012).
3432--
3533-- This is an Internal module and it is no supposed to be imported.
3634--
@@ -43,26 +41,17 @@ module LAoP.Matrix.Internal
4341 --
4442 -- There exists two type families that make it easier to write
4543 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
47− -- of LAoP combinators.
44+ -- leads to a very straightforward implementation
45+ -- of LAoP combinators.
4846
4947 -- * Type safe matrix representation
5048 Matrix (..),
5149
52− -- * Constraint type aliases
53− Countable,
54− CountableDims,
55− CountableN,
56− CountableDimsN,
57− FL,
58− FLN,
59− Liftable,
60− Trivial,
61−
6250 -- * Primitives
51+ empty,
6352 one,
64− join,
65− fork,
53+ junc,
54+ split,
6655
6756 -- * Auxiliary type families
6857 FromNat,
@@ -75,7 +64,6 @@ module LAoP.Matrix.Internal
7564 toLists,
7665 toList,
7766 matrixBuilder,
78− matrixBuilder',
7967 row,
8068 col,
8169 zeros,
@@ -86,38 +74,28 @@ module LAoP.Matrix.Internal
8674 -- * Misc
8775 -- ** Get dimensions
8876 columns,
89− columns',
9077 rows,
91− rows',
9278
9379 -- ** Matrix Transposition
9480 tr,
9581
96− -- ** Scalar multiplication/division of matrices
97− (.|),
98− (./),
99−
10082 -- ** Selective operator
101− select,
102− branch,
83+ select,
10384
10485 -- ** McCarthy's Conditional
10586 cond,
10687
10788 -- ** Matrix "abiding"
108− abideJF,
109− abideFJ,
110−
111− -- ** Zip matrices
112− zipWithM,
89+ abideJS,
90+ abideSJ,
11391
11492 -- * Biproduct approach
115− -- ** Fork
93+ -- ** Split
11694 (===),
11795 -- *** Projections
11896 p1,
11997 p2,
120− -- ** Join
98+ -- ** Junc
12199 (|||),
122100 -- *** Injections
123101 i1,
@@ -130,13 +108,12 @@ module LAoP.Matrix.Internal
130108
131109 -- | Note that given the restrictions imposed it is not possible to
132110 -- implement the standard type classes present in standard Haskell.
133−
134111 -- *** Matrix pairing projections
135− fstM,
136− sndM,
112+ kp1,
113+ kp2,
137114
138115 -- *** Matrix pairing
139− kr,
116+ khatri,
140117
141118 -- * Matrix composition and lifting
142119
@@ -144,10 +121,10 @@ module LAoP.Matrix.Internal
144121
145122 -- | Note that given the restrictions imposed it is not possible to
146123 -- implement the standard type classes present in standard Haskell.
147− iden,
124+ identity,
148125 comp,
149− fromF',
150126 fromF,
127+ fromF',
151128
152129 -- * Matrix printing
153130 pretty,
@@ -174,22 +151,21 @@ import LAoP.Utils.Internal
174151import Data.Bool
175152import Data.Kind
176153import Data.List
177−import Data.Maybe
178154import Data.Proxy
179−import Data.Typeable
180155import Data.Void
181156import GHC.TypeLits
182157import Data.Type.Equality
183158import GHC.Generics
184159import Control.DeepSeq
185−import Prelude hiding (id, (.))
186−import qualified Prelude (id, (.))
160+import Control.Category
161+import Prelude hiding ((.))
187162
188163-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
189164data Matrix e cols rows where
165+ Empty :: Matrix e Void Void
190166 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)
167+ Junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
168+ Split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
193169
194170deriving instance (Show e) => Show (Matrix e cols rows)
195171
@@ -199,7 +175,7 @@ deriving instance (Show e) => Show (Matrix e cols rows)
199175-- 'Generic' instance.
200176type family Count (d :: Type) :: Nat where
201177 Count (Natural n m) = (m - n) + 1
202− Count (List a) = (^) 2 (Count a)
178+ Count (Powerset a) = (^) (Count a) (Count a)
203179 Count (Either a b) = (+) (Count a) (Count b)
204180 Count (a, b) = (*) (Count a) (Count b)
205181 Count (a -> b) = (^) (Count b) (Count a)
@@ -216,197 +192,205 @@ type family Count (d :: Type) :: Nat where
216192--
217193-- Thanks to Li-Yao Xia this type family is super fast.
218194type family FromNat (n :: Nat) :: Type where
195+ FromNat 0 = Void
219196 FromNat 1 = ()
220197 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
221198
222199type family FromNat' (b :: Bool) (m :: Type) :: Type where
223− FromNat' 'True m = Either m m
200+ FromNat' 'True m = Either m m
224201 FromNat' 'False m = Either () (Either m m)
225202
226203-- | Type family that normalizes the representation of a given data
227204-- structure
228205type family Normalize (d :: Type) :: Type where
229− Normalize (Either a b) = Either (Normalize a) (Normalize b)
230− Normalize d = FromNat (Count d)
231−
232−-- | Constraint type synonyms to keep the type signatures less convoluted
233−type Countable a = KnownNat (Count a)
234−type 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)
239−type 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
241−
242−-- | It is possible to implement a constrained version of the category type
243−-- class.
206+ Normalize d = FromNat (Count d)
207+
208+-- | It isn't possible to implement the 'id' function so it's
209+-- implementation is 'undefined'. However 'comp' can be and this partial
210+-- class implementation exists just to make the code more readable.
211+--
212+-- Please use 'identity' instead.
244213instance (Num e) => Category (Matrix e) where
245− type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
247− (.) = comp
214+ id = undefined
215+ (.) = comp
248216
249217instance 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
218+ rnf Empty = ()
219+ rnf (One e) = rnf e
220+ rnf (Junc a b) = rnf a `seq` rnf b
221+ rnf (Split a b) = rnf a `seq` rnf b
253222
254223instance 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
224+ Empty == Empty = True
225+ (One a) == (One b) = a == b
226+ (Junc a b) == (Junc c d) = a == c && b == d
227+ (Split a b) == (Split c d) = a == c && b == d
228+ x@(Split _ _) == y@(Junc _ _) = x == abideJS y
229+ x@(Junc _ _) == y@(Split _ _) = abideJS x == y
260230
261231instance 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
267−
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)
271232
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)
233+ Empty + Empty = Empty
234+ (One a) + (One b) = One (a + b)
235+ (Junc a b) + (Junc c d) = Junc (a + c) (b + d)
236+ (Split a b) + (Split c d) = Split (a + c) (b + d)
237+ x@(Split _ _) + y@(Junc _ _) = x + abideJS y
238+ x@(Junc _ _) + y@(Split _ _) = abideJS x + y
239+
240+ Empty - Empty = Empty
241+ (One a) - (One b) = One (a - b)
242+ (Junc a b) - (Junc c d) = Junc (a - c) (b - d)
243+ (Split a b) - (Split c d) = Split (a - c) (b - d)
244+ x@(Split _ _) - y@(Junc _ _) = x - abideJS y
245+ x@(Junc _ _) - y@(Split _ _) = abideJS x - y
246+
247+ Empty * Empty = Empty
248+ (One a) * (One b) = One (a * b)
249+ (Junc a b) * (Junc c d) = Junc (a * c) (b * d)
250+ (Split a b) * (Split c d) = Split (a * c) (b * d)
251+ x@(Split _ _) * y@(Junc _ _) = x * abideJS y
252+ x@(Junc _ _) * y@(Split _ _) = abideJS x * y
253+
254+ abs Empty = Empty
255+ abs (One a) = One (abs a)
256+ abs (Junc a b) = Junc (abs a) (abs b)
257+ abs (Split a b) = Split (abs a) (abs b)
258+
259+ signum Empty = Empty
260+ signum (One a) = One (signum a)
261+ signum (Junc a b) = Junc (signum a) (signum b)
262+ signum (Split a b) = Split (signum a) (signum b)
275263
276264instance 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
265+ Empty <= Empty = True
266+ (One a) <= (One b) = a <= b
267+ (Junc a b) <= (Junc c d) = (a <= c) && (b <= d)
268+ (Split a b) <= (Split c d) = (a <= c) && (b <= d)
269+ x@(Split _ _) <= y@(Junc _ _) = x <= abideJS y
270+ x@(Junc _ _) <= y@(Split _ _) = abideJS x <= y
282271
283272-- Primitives
284273
274+-- | Empty matrix constructor
275+empty :: Matrix e Void Void
276+empty = Empty
277+
285278-- | Unit matrix constructor
286279one :: e -> Matrix e () ()
287280one = One
288281
289−-- | Matrix 'Join' constructor
290−join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
291−join = Join
282+-- | Matrix 'Junc' constructor
283+junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
284+junc = Junc
292285
293286infixl 3 |||
294287
295−-- | Matrix 'Join' constructor
288+-- | Matrix 'Junc' constructor
296289(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
297−(|||) = Join
290+(|||) = Junc
298291
299−-- | Matrix 'Fork' constructor
300−fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
301−fork = Fork
292+-- | Matrix 'Split' constructor
293+split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
294+split = Split
302295
303296infixl 2 ===
304297
305−-- | Matrix 'Fork' constructor
298+-- | Matrix 'Split' constructor
306299(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
307−(===) = Fork
300+(===) = Split
308301
309302-- Construction
310303
311304-- | Type class for defining the 'fromList' conversion function.
312305--
313306-- Given that it is not possible to branch on types at the term level type
314−-- classes are needed very much like an inductive definition but on types.
315−class FromLists cols rows where
307+-- classes are needed bery much like an inductive definition but on types.
308+class FromLists e cols rows where
316309 -- | Build a matrix out of a list of list of elements. Throws a runtime
317310 -- error if the dimensions do not match.
318311 fromLists :: [[e]] -> Matrix e cols rows
319312
320−instance FromLists () () where
313+instance FromLists e Void Void where
314+ fromLists [] = Empty
315+ fromLists _ = error "Wrong dimensions"
316+
317+instance {-# OVERLAPPING #-} FromLists e () () where
321318 fromLists [[e]] = One e
322319 fromLists _ = error "Wrong dimensions"
323320
324−instance (FromLists cols ()) => FromLists (Either () cols) () where
325− fromLists [h : t] = Join (One h) (fromLists [t])
321+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
322+ fromLists [h : t] = Junc (One h) (fromLists [t])
326323 fromLists _ = error "Wrong dimensions"
327324
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
330− fromLists [l] =
325+instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), KnownNat (Count a)) => FromLists e (Either a b) () where
326+ fromLists [l] =
331327 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332− in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
328+ in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
333329 fromLists _ = error "Wrong dimensions"
334330
335−instance (FromLists () rows) => FromLists () (Either () rows) where
336− fromLists ([h] : t) = Fork (One h) (fromLists t)
331+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
332+ fromLists ([h] : t) = Split (One h) (fromLists t)
337333 fromLists _ = error "Wrong dimensions"
338334
339−instance {-# OVERLAPPABLE #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341− fromLists l@([_] : _) =
335+instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, KnownNat (Count a)) => FromLists e () (Either a b) where
336+ fromLists l@([_] : _) =
342337 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343− in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
338+ in Split (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344339 fromLists _ = error "Wrong dimensions"
345340
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
341+instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, KnownNat (Count c)) => FromLists e (Either a b) (Either c d) where
348342 fromLists l@(h : t) =
349343 let lh = length h
350344 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
351− condition = all ((== lh) . length) t
345+ condition = all (== lh) (map length t)
352346 in if lh > 0 && condition
353− then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
347+ then Split (fromLists (take rowsC l)) (fromLists (drop rowsC l))
354348 else error "Not all rows have the same length"
355349
356350-- | Matrix builder function. Constructs a matrix provided with
357−-- a construction function that operates with indices.
358−matrixBuilder' ::
351+-- a construction function.
352+matrixBuilder ::
359353 forall e cols rows.
360− ( FL cols rows,
361− CountableDims cols rows
362− ) => ((Int, Int) -> e) -> Matrix e cols rows
363−matrixBuilder' f =
354+ ( FromLists e cols rows,
355+ KnownNat (Count cols),
356+ KnownNat (Count rows)
357+ ) =>
358+ ((Int, Int) -> e) ->
359+ Matrix e cols rows
360+matrixBuilder f =
364361 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
365362 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
366363 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
367364 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
368365
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−
389366-- | Constructs a column vector matrix
390−col :: (FL () rows) => [e] -> Matrix e () rows
367+col :: (FromLists e () rows) => [e] -> Matrix e () rows
391368col = fromLists . map (: [])
392369
393370-- | Constructs a row vector matrix
394−row :: (FL cols ()) => [e] -> Matrix e cols ()
371+row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
395372row = fromLists . (: [])
396373
397374-- | Lifts functions to matrices with arbitrary dimensions.
398375--
399376-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
400377-- types @a@ or @b@ allows.
401−fromF' ::
378+fromF ::
402379 forall a b cols rows e.
403− ( Liftable e a b,
404− CountableDims cols rows,
405− FL rows cols
380+ ( Bounded a,
381+ Bounded b,
382+ Enum a,
383+ Enum b,
384+ Eq b,
385+ Num e,
386+ Ord e,
387+ KnownNat (Count cols),
388+ KnownNat (Count rows),
389+ FromLists e rows cols
406390 ) =>
407391 (a -> b) ->
408392 Matrix e cols rows
409−fromF' f =
393+fromF f =
410394 let minA = minBound @a
411395 maxA = maxBound @a
412396 minB = minBound @b
@@ -416,8 +400,8 @@ fromF' f =
416400 elementsA = take ccols [minA .. maxA]
417401 elementsB = take rrows [minB .. maxB]
418402 combinations = (,) <$> elementsA <*> elementsB
419− combAp = map snd . sort . map (\(a, b) -> if f a == b
420− then ((fromEnum a, fromEnum b), 1)
403+ combAp = map snd . sort . map (\(a, b) -> if f a == b
404+ then ((fromEnum a, fromEnum b), 1)
421405 else ((fromEnum a, fromEnum b), 0)) $ combinations
422406 mList = buildList combAp rrows
423407 in tr $ fromLists mList
@@ -427,23 +411,48 @@ fromF' f =
427411
428412-- | Lifts functions to matrices with dimensions matching @a@ and @b@
429413-- cardinality's.
430−fromF ::
414+fromF' ::
431415 forall a b e.
432− ( Liftable e a b,
433− CountableDimsN a b,
434− FLN b a
416+ ( Bounded a,
417+ Bounded b,
418+ Enum a,
419+ Enum b,
420+ Eq b,
421+ Num e,
422+ Ord e,
423+ KnownNat (Count (Normalize a)),
424+ KnownNat (Count (Normalize b)),
425+ FromLists e (Normalize b) (Normalize a)
435426 ) =>
436427 (a -> b) ->
437428 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
429+fromF' f =
430+ let minA = minBound @a
431+ maxA = maxBound @a
432+ minB = minBound @b
433+ maxB = maxBound @b
434+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
435+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
436+ elementsA = take ccols [minA .. maxA]
437+ elementsB = take rrows [minB .. maxB]
438+ combinations = (,) <$> elementsA <*> elementsB
439+ combAp = map snd . sort . map (\(a, b) -> if f a == b
440+ then ((fromEnum a, fromEnum b), 1)
441+ else ((fromEnum a, fromEnum b), 0)) $ combinations
442+ mList = buildList combAp rrows
443+ in tr $ fromLists mList
444+ where
445+ buildList [] _ = []
446+ buildList l r = take r l : buildList (drop r l) r
439447
440448-- Conversion
441449
442450-- | Converts a matrix to a list of lists of elements.
443451toLists :: Matrix e cols rows -> [[e]]
452+toLists Empty = []
444453toLists (One e) = [[e]]
445−toLists (Fork l r) = toLists l ++ toLists r
446−toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
454+toLists (Split l r) = toLists l ++ toLists r
455+toLists (Junc l r) = zipWith (++) (toLists l) (toLists r)
447456
448457-- | Converts a matrix to a list of elements.
449458toList :: Matrix e cols rows -> [e]
@@ -452,38 +461,37 @@ toList = concat . toLists
452461-- Zeros Matrix
453462
454463-- | 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)
464+zeros :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => Matrix e cols rows
465+zeros = matrixBuilder (const 0)
457466
458467-- Ones Matrix
459468
460469-- | The ones matrix. A matrix wholly filled with ones.
461470--
462471-- 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)
472+ones :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => Matrix e cols rows
473+ones = matrixBuilder (const 1)
465474
466475-- Const Matrix
467476
468477-- | The constant matrix constructor. A matrix wholly filled with a given
469478-- value.
470−constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
471−constant e = matrixBuilder' (const e)
479+constant :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => e -> Matrix e cols rows
480+constant e = matrixBuilder (const e)
472481
473482-- Bang Matrix
474483
475484-- | The T (Top) row vector matrix.
476−bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
485+bang :: forall e cols. (Num e, Enum e, FromLists e cols (), KnownNat (Count cols)) => Matrix e cols ()
477486bang =
478487 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
479488 in fromLists [take c [1, 1 ..]]
480489
481−-- iden Matrix
490+-- Identity Matrix
482491
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 #-}
492+-- | Identity matrix.
493+identity :: (Num e, FromLists e cols cols, KnownNat (Count cols)) => Matrix e cols cols
494+identity = matrixBuilder (bool 0 1 . uncurry (==))
487495
488496-- Matrix composition (MMM)
489497
@@ -492,90 +500,60 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
492500-- This definition takes advantage of divide-and-conquer and fusion laws
493501-- from LAoP.
494502comp :: (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)
503+comp Empty Empty = Empty
504+comp (One a) (One b) = One (a * b)
505+comp (Junc a b) (Split c d) = comp a c + comp b d -- Divide-and-conquer law
506+comp (Split a b) c = Split (comp a c) (comp b c) -- Split fusion law
507+comp c (Junc a b) = Junc (comp c a) (comp c b) -- Junc fusion law
522508
523509-- Projections
524510
525511-- | 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
512+p1 :: forall e m n. (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
513+p1 =
514+ let iden = identity :: Matrix e m m
515+ zero = zeros :: Matrix e n m
516+ in junc iden zero
528517
529518-- | 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
519+p2 :: forall e m n. (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
520+p2 =
521+ let iden = identity :: Matrix e n n
522+ zero = zeros :: Matrix e m n
523+ in junc zero iden
532524
533525-- Injections
534526
535527-- | Biproduct first component injection
536−i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
528+i1 :: (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
537529i1 = tr p1
538530
539531-- | Biproduct second component injection
540−i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
532+i2 :: (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
541533i2 = tr p2
542534
543535-- Dimensions
544536
545537-- | Obtain the number of rows.
546538--
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''.
550−rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551−rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552−
553−-- | Obtain the number of rows in an inefficient manner, but without any
554−-- constraints.
539+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
540+-- dimensions in constant time.
555541--
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
542+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
543+-- exchange for performance.
544+rows :: forall e cols rows. (KnownNat (Count rows)) => Matrix e cols rows -> Int
545+rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
561546
562547-- | Obtain the number of columns.
548+--
549+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
550+-- dimensions in constant time.
563551--
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''.
567−columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
552+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
553+-- exchange for performance.
554+columns :: forall e cols rows. (KnownNat (Count cols)) => Matrix e cols rows -> Int
568555columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569556
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−
579557-- Coproduct Bifunctor
580558
581559infixl 5 -|-
@@ -584,172 +562,157 @@ infixl 5 -|-
584562(-|-) ::
585563 forall e n k m j.
586564 ( Num e,
587− CountableDims j k,
588− FL k k,
589− FL j k,
590− FL k j,
591− FL j j
565+ KnownNat (Count j),
566+ KnownNat (Count k),
567+ FromLists e k k,
568+ FromLists e j k,
569+ FromLists e k j,
570+ FromLists e j j
592571 ) =>
593572 Matrix e n k ->
594573 Matrix e m j ->
595574 Matrix e (Either n m) (Either k j)
596−(-|-) a b = Join (i1 . a) (i2 . b)
575+(-|-) a b = Junc (i1 . a) (i2 . b)
597576
598577-- Khatri Rao Product and projections
599578
600579-- | Khatri Rao product first component projection matrix.
601−fstM ::
580+kp1 ::
602581 forall e m k .
603582 ( Num e,
604− CountableDims k m,
605− FL (Normalize (m, k)) m,
606− CountableN (m, k)
583+ KnownNat (Count k),
584+ FromLists e (FromNat (Count m * Count k)) m,
585+ KnownNat (Count m),
586+ KnownNat (Count (Normalize (m, k)))
607587 ) => Matrix e (Normalize (m, k)) m
608−fstM = matrixBuilder' f
588+kp1 = matrixBuilder f
609589 where
610590 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
611591 f (x, y)
612592 | y >= (x * offset) && y <= (x * offset + offset - 1) = 1
613− | otherwise = 0
593+ | otherwise = 0
614594
615595-- | Khatri Rao product second component projection matrix.
616−sndM ::
596+kp2 ::
617597 forall e m k .
618598 ( Num e,
619− CountableDims k m,
620− FL (Normalize (m, k)) k,
621− CountableN (m, k)
599+ KnownNat (Count k),
600+ FromLists e (FromNat (Count m * Count k)) k,
601+ KnownNat (Count m),
602+ KnownNat (Count (Normalize (m, k)))
622603 ) => Matrix e (Normalize (m, k)) k
623−sndM = matrixBuilder' f
604+kp2 = matrixBuilder f
624605 where
625606 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
626607 f (x, y)
627608 | x == y || mod (y - x) offset == 0 = 1
628− | otherwise = 0
609+ | otherwise = 0
629610
630611-- | Khatri Rao Matrix product also known as matrix pairing.
631612--
632613-- NOTE: That this is not a true categorical product, see for instance:
633−--
614+--
634615-- @
635−-- | fstM . kr a b == a
636−-- kr a b ==> |
637−-- | sndM . kr a b == b
616+-- | kp1 . khatri a b == a
617+-- khatri a b ==> |
618+-- | kp2 . khatri a b == b
638619-- @
639620--
640621-- __Emphasis__ on the implication symbol.
641−kr ::
642− forall e cols a b.
622+khatri ::
623+ forall e cols a b.
643624 ( Num e,
644− CountableDims a b,
645− CountableN (a, b),
646− FL (Normalize (a, b)) a,
647− FL (Normalize (a, b)) b
625+ KnownNat (Count a),
626+ KnownNat (Count b),
627+ KnownNat (Count (Normalize (a, b))),
628+ FromLists e (Normalize (a, b)) a,
629+ FromLists e (Normalize (a, b)) b
648630 ) => 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)
631+khatri a b =
632+ let kp1' = kp1 @e @a @b
633+ kp2' = kp2 @e @a @b
634+ in (tr kp1') . a * (tr kp2') . b
653635
654636-- Product Bifunctor (Kronecker)
655637
656638infixl 4 ><
657639
658640-- | Matrix product functor also known as kronecker product
659−(><) ::
660− forall e m p n q.
641+(><) ::
642+ forall e m p n q.
661643 ( 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− )
644+ KnownNat (Count m),
645+ KnownNat (Count n),
646+ KnownNat (Count p),
647+ KnownNat (Count q),
648+ KnownNat (Count (Normalize (m, n))),
649+ FromLists e (Normalize (m, n)) m,
650+ FromLists e (Normalize (m, n)) n,
651+ KnownNat (Count (Normalize (p, q))),
652+ FromLists e (Normalize (p, q)) p,
653+ FromLists e (Normalize (p, q)) q
654+ )
670655 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671656(><) a b =
672− let fstM' = fstM @e @m @n
673− sndM' = sndM @e @m @n
674− in kr (a . fstM') (b . sndM')
657+ let kp1' = kp1 @e @m @n
658+ kp2' = kp2 @e @m @n
659+ in khatri (a . kp1') (b . kp2')
675660
676−-- Matrix abide Join Fork
661+-- Matrix abide Junc Split
677662
678−-- | Matrix "abiding" following the 'Join'-'Fork' exchange law.
679−--
663+-- | Matrix "abiding" followin the 'Junc'-'Split' abide law.
664+--
680665-- Law:
681666--
682667-- @
683−-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
668+-- 'Junc' ('Split' a c) ('Split' b d) == 'Split' ('Junc' a b) ('Junc' c d)
684669-- @
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)
670+abideJS :: Matrix e cols rows -> Matrix e cols rows
671+abideJS (Junc (Split a c) (Split b d)) = Split (Junc (abideJS a) (abideJS b)) (Junc (abideJS c) (abideJS d)) -- Junc-Split abide law
672+abideJS Empty = Empty
673+abideJS (One e) = One e
674+abideJS (Junc a b) = Junc (abideJS a) (abideJS b)
675+abideJS (Split a b) = Split (abideJS a) (abideJS b)
690676
691−-- Matrix abide Fork Join
677+-- Matrix abide Split Junc
692678
693−-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
694−--
679+-- | Matrix "abiding" followin the 'Split'-'Junc' abide law.
680+--
695681-- @
696−-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
682+-- 'Split' ('Junc' a b) ('Junc' c d) == 'Junc' ('Split' a c) ('Split' b d)
697683-- @
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)
684+abideSJ :: Matrix e cols rows -> Matrix e cols rows
685+abideSJ (Split (Junc a b) (Junc c d)) = Junc (Split (abideSJ a) (abideSJ c)) (Split (abideSJ b) (abideSJ d)) -- Split-Junc abide law
686+abideSJ Empty = Empty
687+abideSJ (One e) = One e
688+abideSJ (Junc a b) = Junc (abideSJ a) (abideSJ b)
689+abideSJ (Split a b) = Split (abideSJ a) (abideSJ b)
703690
704691-- Matrix transposition
705692
706693-- | Matrix transposition.
707694tr :: 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)
695+tr Empty = Empty
696+tr (One e) = One e
697+tr (Junc a b) = Split (tr a) (tr b)
698+tr (Split a b) = Junc (tr a) (tr b)
711699
712700-- Selective 'select' operator
713701
714702-- | Selective functors 'select' operator equivalent inspired by the
715703-- 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
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
704+select :: (Num e, FromLists e b b, KnownNat (Count b)) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
705+select m y = junc y identity . m
743706
744707-- McCarthy's Conditional
745708
746709-- | McCarthy's Conditional expresses probabilistic choice.
747710cond ::
748− ( Trivial cols,
749− Countable cols,
750− FL () cols,
751− FL cols (),
752− FL cols cols,
711+ ( cols ~ FromNat (Count cols),
712+ KnownNat (Count cols),
713+ FromLists e () cols,
714+ FromLists e cols (),
715+ FromLists e cols cols,
753716 Bounded a,
754717 Enum a,
755718 Num e,
@@ -757,14 +720,14 @@ cond ::
757720 )
758721 =>
759722 (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
760−cond p f g = join f g . grd p
761−
762−grd ::
763− ( Trivial q,
764− Countable q,
765− FL () q,
766− FL q (),
767− FL q q,
723+cond p f g = junc f g . grd p
724+
725+grd ::
726+ ( q ~ FromNat (Count q),
727+ KnownNat (Count q),
728+ FromLists e () q,
729+ FromLists e q (),
730+ FromLists e q q,
768731 Bounded a,
769732 Enum a,
770733 Num e,
@@ -772,86 +735,63 @@ grd ::
772735 )
773736 =>
774737 (a -> Bool) -> Matrix e q (Either q q)
775−grd f = fork (corr f) (corr (not . f))
776−
777−corr ::
778− forall e a q .
779− ( Trivial q,
780− Countable q,
781− FL () q,
782− FL q (),
783− FL q q,
784− Liftable e a Bool
785− )
738+grd f = split (corr f) (corr (not . f))
739+
740+corr ::
741+ forall e a q .
742+ ( q ~ FromNat (Count q),
743+ KnownNat (Count q),
744+ FromLists e () q,
745+ FromLists e q (),
746+ FromLists e q q,
747+ Bounded a,
748+ Enum a,
749+ Num e,
750+ Ord e
751+ )
786752 => (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)
753+corr p = let f = fromF p :: Matrix e q ()
754+ in khatri f (identity :: Matrix e q q)
789755
790756-- Pretty print
791757
792758prettyAux :: Show e => [[e]] -> [[e]] -> String
793759prettyAux [] _ = ""
794−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
760+prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
795761 where
796762 v = fmap show m
797763 widest = maximum $ fmap length v
798764 fill str = replicate (widest - length str - 2) ' ' ++ str
799−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
765+prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
800766 where
801− v = fmap show m
802− widest = maximum $ fmap length v
767+ v = fmap show m
768+ widest = maximum $ fmap length v
803769 fill str = replicate (widest - length str - 2) ' ' ++ str
804−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
770+prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
805771 prettyAux t l
806772 where
807− v = fmap show l
808− widest = maximum $ fmap length v
773+ v = fmap show l
774+ widest = maximum $ fmap length v
809775 fill str = replicate (widest - length str - 2) ' ' ++ str
810776
811777-- | 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
778+pretty :: (KnownNat (Count cols), Show e) => Matrix e cols rows -> String
779+pretty m = "┌ " ++ unwords (replicate (columns m) blank) ++ " ┐\n" ++
780+ prettyAux (toLists m) (toLists m) ++
781+ "└ " ++ unwords (replicate (columns m) blank) ++ " ┘"
782+ where
783+ v = fmap show (toList m)
784+ widest = maximum $ fmap length v
822785 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)
786+ blank = fill ""
839787
840788-- | Matrix pretty printer
841−prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
789+prettyPrint :: (KnownNat (Count cols), Show e) => Matrix e cols rows -> IO ()
842790prettyPrint = putStrLn . pretty
843791
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−
852792-- Relational operators functions
853793
854−type Boolean = Natural 0 1
794+type Boolean = Natural 0 1
855795type Relation a b = Matrix Boolean a b
856796
857797-- | Helper conversion function
@@ -867,47 +807,54 @@ fromBool False = nat 0
867807
868808-- | Relational negation
869809negateM :: 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)
810+negateM Empty = Empty
811+negateM (One (Nat 0)) = One (Nat 1)
812+negateM (One (Nat 1)) = One (Nat 0)
813+negateM (Junc a b) = Junc (negateM a) (negateM b)
814+negateM (Split a b) = Split (negateM a) (negateM b)
873815
874816-- | Relational addition
875817orM :: 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
818+orM Empty Empty = Empty
819+orM (One a) (One b) = One (fromBool (toBool a || toBool b))
820+orM (Junc a b) (Junc c d) = Junc (orM a c) (orM b d)
821+orM (Split a b) (Split c d) = Split (orM a c) (orM b d)
822+orM x@(Split _ _) y@(Junc _ _) = orM x (abideJS y)
823+orM x@(Junc _ _) y@(Split _ _) = orM (abideJS x) y
881824
882825-- | Relational multiplication
883826andM :: 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
827+andM Empty Empty = Empty
828+andM (One a) (One b) = One (fromBool (toBool a && toBool b))
829+andM (Junc a b) (Junc c d) = Junc (andM a c) (andM b d)
830+andM (Split a b) (Split c d) = Split (andM a c) (andM b d)
831+andM x@(Split _ _) y@(Junc _ _) = andM x (abideJS y)
832+andM x@(Junc _ _) y@(Split _ _) = andM (abideJS x) y
889833
890834-- | Relational subtraction
891835subM :: 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
836+subM Empty Empty = Empty
837+subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
838+subM (Junc a b) (Junc c d) = Junc (subM a c) (subM b d)
839+subM (Split a b) (Split c d) = Split (subM a c) (subM b d)
840+subM x@(Split _ _) y@(Junc _ _) = subM x (abideJS y)
841+subM x@(Junc _ _) y@(Split _ _) = subM (abideJS x) y
897842
898843-- | Matrix relational composition.
899844compRel :: 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
845+compRel Empty Empty = Empty
846+compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
847+compRel (Junc a b) (Split c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
848+compRel (Split a b) c = Split (compRel a c) (compRel b c) -- Split fusion law
849+compRel c (Junc a b) = Junc (compRel c a) (compRel c b) -- Junc fusion law
904850
905851-- | Matrix relational right division
906852divR :: Relation b c -> Relation b a -> Relation a c
853+divR Empty Empty = Empty
907854divR (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)
855+divR (Junc a b) (Junc c d) = andM (divR a c) (divR b d)
856+divR (Split a b) c = Split (divR a c) (divR b c)
857+divR c (Split a b) = Junc (divR c a) (divR c b)
911858
912859-- | Matrix relational left division
913860divL :: Relation c b -> Relation a b -> Relation a c
@@ -923,52 +870,97 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
923870--
924871-- NOTE: Be careful to not ask for a relation bigger than the cardinality of
925872-- types @a@ or @b@ allows.
926−fromFRel' ::
873+fromFRel ::
927874 forall a b cols rows.
928− ( Liftable Boolean a b,
929− CountableDims cols rows,
930− FL rows cols
875+ ( Bounded a,
876+ Bounded b,
877+ Enum a,
878+ Enum b,
879+ Eq b,
880+ KnownNat (Count cols),
881+ KnownNat (Count rows),
882+ FromLists (Natural 0 1) rows cols
931883 ) =>
932884 (a -> b) ->
933885 Relation cols rows
934−fromFRel' = fromF'
886+fromFRel f =
887+ let minA = minBound @a
888+ maxA = maxBound @a
889+ minB = minBound @b
890+ maxB = maxBound @b
891+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
892+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
893+ elementsA = take ccols [minA .. maxA]
894+ elementsB = take rrows [minB .. maxB]
895+ combinations = (,) <$> elementsA <*> elementsB
896+ combAp = map snd . sort . map (\(a, b) -> if f a == b
897+ then ((fromEnum a, fromEnum b), nat 1)
898+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
899+ mList = buildList combAp rrows
900+ in tr $ fromLists mList
901+ where
902+ buildList [] _ = []
903+ buildList l r = take r l : buildList (drop r l) r
935904
936905-- | Lifts functions to relations with dimensions matching @a@ and @b@
937906-- cardinality's.
938−fromFRel ::
907+fromFRel' ::
939908 forall a b.
940− ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
909+ ( Bounded a,
910+ Bounded b,
911+ Enum a,
912+ Enum b,
913+ Eq b,
914+ KnownNat (Count (Normalize a)),
915+ KnownNat (Count (Normalize b)),
916+ FromLists (Natural 0 1) (Normalize b) (Normalize a)
943917 ) =>
944918 (a -> b) ->
945919 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
920+fromFRel' f =
921+ let minA = minBound @a
922+ maxA = maxBound @a
923+ minB = minBound @b
924+ maxB = maxBound @b
925+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
926+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
927+ elementsA = take ccols [minA .. maxA]
928+ elementsB = take rrows [minB .. maxB]
929+ combinations = (,) <$> elementsA <*> elementsB
930+ combAp = map snd . sort . map (\(a, b) -> if f a == b
931+ then ((fromEnum a, fromEnum b), nat 1)
932+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
933+ mList = buildList combAp rrows
934+ in tr $ fromLists mList
935+ where
936+ buildList [] _ = []
937+ buildList l r = take r l : buildList (drop r l) r
947938
948939-- | Lifts a relation function to a Boolean Matrix
949−toRel ::
940+toRel ::
950941 forall a b.
951942 ( Bounded a,
952943 Bounded b,
953944 Enum a,
954945 Enum b,
955946 Eq b,
956− CountableDims a b,
957− FLN b a
958− )
947+ KnownNat (Count (Normalize a)),
948+ KnownNat (Count (Normalize b)),
949+ FromLists (Natural 0 1) (Normalize b) (Normalize a)
950+ )
959951 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
960952toRel f =
961953 let minA = minBound @a
962954 maxA = maxBound @a
963955 minB = minBound @b
964956 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
957+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
958+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
967959 elementsA = take ccols [minA .. maxA]
968960 elementsB = take rrows [minB .. maxB]
969961 combinations = (,) <$> elementsA <*> elementsB
970− combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
971− then ((fromEnum a, fromEnum b), nat 1)
962+ combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
963+ then ((fromEnum a, fromEnum b), nat 1)
972964 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
973965 mList = buildList combAp rrows
974966 in tr $ fromLists mList