@@ -1,4 +1,6 @@
1+{-# LANGUAGE StrictData #-}
1{-# LANGUAGE TypeFamilyDependencies #-}
2{-# LANGUAGE TypeFamilyDependencies #-}
3+{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE AllowAmbiguousTypes #-}
4{-# LANGUAGE AllowAmbiguousTypes #-}
3{-# LANGUAGE DataKinds #-}
5{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE FlexibleContexts #-}
@@ -28,7 +30,7 @@
28-- motivation behind the library, the underlying theory, and implementation details.
30-- motivation behind the library, the underlying theory, and implementation details.
30-- This module offers many of the combinators mentioned in the work of
32-- This module offers many of the combinators mentioned in the work of
31−-- Macedo (2012) and Oliveira (2012).
33+-- Macedo (2012) and Oliveira (2012).
33-- This is an Internal module and it is no supposed to be imported.
35-- This is an Internal module and it is no supposed to be imported.
@@ -41,17 +43,26 @@ module LAoP.Matrix.Internal
42 -- There exists two type families that make it easier to write
44 -- There exists two type families that make it easier to write
43 -- matrix dimensions: 'FromNat' and 'Count'. This approach
45 -- matrix dimensions: 'FromNat' and 'Count'. This approach
44− -- leads to a very straightforward implementation
46+ -- leads to a very straightforward implementation
45− -- of LAoP combinators.
47+ -- of LAoP combinators.
47 -- * Type safe matrix representation
49 -- * Type safe matrix representation
48 Matrix (..),
50 Matrix (..),
52+ -- * Constraint type aliases
50 -- * Primitives
62 -- * Primitives
56 -- * Auxiliary type families
67 -- * Auxiliary type families
@@ -64,6 +75,7 @@ module LAoP.Matrix.Internal
66 matrixBuilder,
77 matrixBuilder,
@@ -74,28 +86,38 @@ module LAoP.Matrix.Internal
75 -- ** Get dimensions
87 -- ** Get dimensions
79 -- ** Matrix Transposition
93 -- ** Matrix Transposition
96+ -- ** Scalar multiplication/division of matrices
82 -- ** Selective operator
100 -- ** Selective operator
85 -- ** McCarthy's Conditional
104 -- ** McCarthy's Conditional
88 -- ** Matrix "abiding"
107 -- ** Matrix "abiding"
89− abideJS,
108+ abideJF,
90− abideSJ,
109+ abideFJ,
92 -- * Biproduct approach
114 -- * Biproduct approach
93− -- ** Split
115+ -- ** Fork
95 -- *** Projections
117 -- *** Projections
98− -- ** Junc
120+ -- ** Join
100 -- *** Injections
122 -- *** Injections
@@ -108,12 +130,13 @@ module LAoP.Matrix.Internal
109 -- | Note that given the restrictions imposed it is not possible to
131 -- | Note that given the restrictions imposed it is not possible to
110 -- implement the standard type classes present in standard Haskell.
132 -- implement the standard type classes present in standard Haskell.
111 -- *** Matrix pairing projections
134 -- *** Matrix pairing projections
115 -- *** Matrix pairing
138 -- *** Matrix pairing
118 -- * Matrix composition and lifting
141 -- * Matrix composition and lifting
@@ -121,10 +144,10 @@ module LAoP.Matrix.Internal
122 -- | Note that given the restrictions imposed it is not possible to
145 -- | Note that given the restrictions imposed it is not possible to
123 -- implement the standard type classes present in standard Haskell.
146 -- implement the standard type classes present in standard Haskell.
129 -- * Matrix printing
152 -- * Matrix printing
@@ -151,21 +174,22 @@ import LAoP.Utils.Internal
151import Data.Bool
174import Data.Bool
152import Data.Kind
175import Data.Kind
153import Data.List
176import Data.List
154import Data.Proxy
178import Data.Proxy
155import Data.Void
180import Data.Void
156import GHC.TypeLits
181import GHC.TypeLits
157import Data.Type.Equality
182import Data.Type.Equality
158import GHC.Generics
183import GHC.Generics
159import Control.DeepSeq
184import Control.DeepSeq
160−import Control.Category
185+import Prelude hiding (id, (.))
161−import Prelude hiding ((.))
186+import qualified Prelude (id, (.))
163-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
188-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
164data Matrix e cols rows where
189data Matrix e cols rows where
165− Empty :: Matrix e Void Void
166 One :: e -> Matrix e () ()
190 One :: e -> Matrix e () ()
167− Junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
191+ Join :: 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)
192+ Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
170deriving instance (Show e) => Show (Matrix e cols rows)
194deriving instance (Show e) => Show (Matrix e cols rows)
@@ -175,7 +199,7 @@ deriving instance (Show e) => Show (Matrix e cols rows)
175-- 'Generic' instance.
199-- 'Generic' instance.
176type family Count (d :: Type) :: Nat where
200type family Count (d :: Type) :: Nat where
177 Count (Natural n m) = (m - n) + 1
201 Count (Natural n m) = (m - n) + 1
178− Count (Powerset a) = (^) (Count a) (Count a)
202+ Count (List a) = (^) 2 (Count a)
179 Count (Either a b) = (+) (Count a) (Count b)
203 Count (Either a b) = (+) (Count a) (Count b)
180 Count (a, b) = (*) (Count a) (Count b)
204 Count (a, b) = (*) (Count a) (Count b)
181 Count (a -> b) = (^) (Count b) (Count a)
205 Count (a -> b) = (^) (Count b) (Count a)
@@ -192,205 +216,197 @@ type family Count (d :: Type) :: Nat where
193-- Thanks to Li-Yao Xia this type family is super fast.
217-- Thanks to Li-Yao Xia this type family is super fast.
194type family FromNat (n :: Nat) :: Type where
218type family FromNat (n :: Nat) :: Type where
196 FromNat 1 = ()
219 FromNat 1 = ()
197 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
220 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
199type family FromNat' (b :: Bool) (m :: Type) :: Type where
222type family FromNat' (b :: Bool) (m :: Type) :: Type where
200− FromNat' 'True m = Either m m
223+ FromNat' 'True m = Either m m
201 FromNat' 'False m = Either () (Either m m)
224 FromNat' 'False m = Either () (Either m m)
203-- | Type family that normalizes the representation of a given data
226-- | Type family that normalizes the representation of a given data
204-- structure
227-- structure
205type family Normalize (d :: Type) :: Type where
228type family Normalize (d :: Type) :: Type where
229+ Normalize (Either a b) = Either (Normalize a) (Normalize b)
230+ Normalize d = FromNat (Count d)
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)
206− Normalize d = FromNat (Count d)
240+type Trivial a = FromNat (Count a) ~ a
208−-- | It isn't possible to implement the 'id' function so it's
242+-- | It is possible to implement a constrained version of the category type
209−-- implementation is 'undefined'. However 'comp' can be and this partial
243+-- class.
210−-- class implementation exists just to make the code more readable.
212−-- Please use 'identity' instead.
213instance (Num e) => Category (Matrix e) where
244instance (Num e) => Category (Matrix e) where
214− id = undefined
245+ type Object (Matrix e) a = (FL a a, Countable a)
215− (.) = comp
247+ (.) = comp
217instance NFData e => NFData (Matrix e cols rows) where
249instance NFData e => NFData (Matrix e cols rows) where
219− rnf (One e) = rnf e
250+ rnf (One e) = rnf e
220− rnf (Junc a b) = rnf a `seq` rnf b
251+ rnf (Join a b) = rnf a `seq` rnf b
221− rnf (Split a b) = rnf a `seq` rnf b
252+ rnf (Fork a b) = rnf a `seq` rnf b
223instance Eq e => Eq (Matrix e cols rows) where
254instance Eq e => Eq (Matrix e cols rows) where
224− Empty == Empty = True
225− (One a) == (One b) = a == b
255+ (One a) == (One b) = a == b
226− (Junc a b) == (Junc c d) = a == c && b == d
256+ (Join a b) == (Join c d) = a == c && b == d
227− (Split a b) == (Split c d) = a == c && b == d
257+ (Fork a b) == (Fork c d) = a == c && b == d
228− x@(Split _ _) == y@(Junc _ _) = x == abideJS y
258+ x@(Fork _ _) == y@(Join _ _) = x == abideJF y
229− x@(Junc _ _) == y@(Split _ _) = abideJS x == y
259+ x@(Join _ _) == y@(Fork _ _) = abideJF x == y
231instance Num e => Num (Matrix e cols rows) where
261instance Num e => Num (Matrix e cols rows) where
262+ a + b = zipWithM (+) a b
264+ a - b = zipWithM (-) a b
266+ a * b = zipWithM (*) a b
268+ abs (One a) = One (abs a)
269+ abs (Join a b) = Join (abs a) (abs b)
270+ abs (Fork a b) = Fork (abs a) (abs b)
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
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
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
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)
259− signum Empty = Empty
260− signum (One a) = One (signum a)
272+ signum (One a) = One (signum a)
261− signum (Junc a b) = Junc (signum a) (signum b)
273+ signum (Join a b) = Join (signum a) (signum b)
262− signum (Split a b) = Split (signum a) (signum b)
274+ signum (Fork a b) = Fork (signum a) (signum b)
264instance Ord e => Ord (Matrix e cols rows) where
276instance Ord e => Ord (Matrix e cols rows) where
265− Empty <= Empty = True
266− (One a) <= (One b) = a <= b
277+ (One a) <= (One b) = a <= b
267− (Junc a b) <= (Junc c d) = (a <= c) && (b <= d)
278+ (Join a b) <= (Join c d) = (a <= c) && (b <= d)
268− (Split a b) <= (Split c d) = (a <= c) && (b <= d)
279+ (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
269− x@(Split _ _) <= y@(Junc _ _) = x <= abideJS y
280+ x@(Fork _ _) <= y@(Join _ _) = x <= abideJF y
270− x@(Junc _ _) <= y@(Split _ _) = abideJS x <= y
281+ x@(Join _ _) <= y@(Fork _ _) = abideJF x <= y
272-- Primitives
283-- Primitives
274−-- | Empty matrix constructor
275−empty :: Matrix e Void Void
278-- | Unit matrix constructor
285-- | Unit matrix constructor
279one :: e -> Matrix e () ()
286one :: e -> Matrix e () ()
282−-- | Matrix 'Junc' constructor
289+-- | Matrix 'Join' constructor
283−junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
290+join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
284−junc = Junc
291+join = Join
286infixl 3 |||
293infixl 3 |||
288−-- | Matrix 'Junc' constructor
295+-- | Matrix 'Join' constructor
289(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
296(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
290−(|||) = Junc
297+(|||) = Join
292−-- | Matrix 'Split' constructor
299+-- | Matrix 'Fork' constructor
293−split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
300+fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
294−split = Split
301+fork = Fork
296infixl 2 ===
303infixl 2 ===
298−-- | Matrix 'Split' constructor
305+-- | Matrix 'Fork' constructor
299(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
306(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
300−(===) = Split
307+(===) = Fork
302-- Construction
309-- Construction
304-- | Type class for defining the 'fromList' conversion function.
311-- | Type class for defining the 'fromList' conversion function.
306-- Given that it is not possible to branch on types at the term level type
313-- Given that it is not possible to branch on types at the term level type
307−-- classes are needed bery much like an inductive definition but on types.
314+-- classes are needed very much like an inductive definition but on types.
308−class FromLists e cols rows where
315+class FromLists cols rows where
309 -- | Build a matrix out of a list of list of elements. Throws a runtime
316 -- | Build a matrix out of a list of list of elements. Throws a runtime
310 -- error if the dimensions do not match.
317 -- error if the dimensions do not match.
311 fromLists :: [[e]] -> Matrix e cols rows
318 fromLists :: [[e]] -> Matrix e cols rows
313−instance FromLists e Void Void where
320+instance FromLists () () where
314− fromLists [] = Empty
315− fromLists _ = error "Wrong dimensions"
317−instance {-# OVERLAPPING #-} FromLists e () () where
318 fromLists [[e]] = One e
321 fromLists [[e]] = One e
319 fromLists _ = error "Wrong dimensions"
322 fromLists _ = error "Wrong dimensions"
321−instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
324+instance (FromLists cols ()) => FromLists (Either () cols) () where
322− fromLists [h : t] = Junc (One h) (fromLists [t])
325+ fromLists [h : t] = Join (One h) (fromLists [t])
323 fromLists _ = error "Wrong dimensions"
326 fromLists _ = error "Wrong dimensions"
328+instance {-# OVERLAPPABLE #-}
325−instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), KnownNat (Count a)) => FromLists e (Either a b) () where
329+ (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
326− fromLists [l] =
330+ fromLists [l] =
327 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
328− in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
332+ in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
329 fromLists _ = error "Wrong dimensions"
333 fromLists _ = error "Wrong dimensions"
331−instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
335+instance (FromLists () rows) => FromLists () (Either () rows) where
332− fromLists ([h] : t) = Split (One h) (fromLists t)
336+ fromLists ([h] : t) = Fork (One h) (fromLists t)
333 fromLists _ = error "Wrong dimensions"
337 fromLists _ = error "Wrong dimensions"
339+instance {-# OVERLAPPABLE #-}
335−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, KnownNat (Count a)) => FromLists e () (Either a b) where
340+ (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
336− fromLists l@([_] : _) =
341+ fromLists l@([_] : _) =
337 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
338− in Split (fromLists (take rowsA l)) (fromLists (drop rowsA l))
343+ in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
339 fromLists _ = error "Wrong dimensions"
344 fromLists _ = error "Wrong dimensions"
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
346+instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347+ => FromLists (Either a b) (Either c d) where
342 fromLists l@(h : t) =
348 fromLists l@(h : t) =
343 let lh = length h
349 let lh = length h
344 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
345− condition = all (== lh) (map length t)
351+ condition = all ((== lh) . length) t
346 in if lh > 0 && condition
352 in if lh > 0 && condition
347− then Split (fromLists (take rowsC l)) (fromLists (drop rowsC l))
353+ then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
348 else error "Not all rows have the same length"
354 else error "Not all rows have the same length"
350-- | Matrix builder function. Constructs a matrix provided with
356-- | Matrix builder function. Constructs a matrix provided with
351−-- a construction function.
357+-- a construction function that operates with indices.
352−matrixBuilder ::
358+matrixBuilder' ::
353 forall e cols rows.
359 forall e cols rows.
354− ( FromLists e cols rows,
360+ ( FL cols rows,
355− KnownNat (Count cols),
361+ CountableDims cols rows
356− KnownNat (Count rows)
358− ((Int, Int) -> e) ->
359− Matrix e cols rows
362+ ) => ((Int, Int) -> e) -> Matrix e cols rows
360−matrixBuilder f =
363+matrixBuilder' f =
361 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
362 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
365 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
363 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
366 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
364 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
367 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
369+-- | Matrix builder function. Constructs a matrix provided with
370+-- a construction function that operates with arbitrary types.
380+ ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
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
387+ buildList r l = take r l : buildList r (drop r l)
366-- | Constructs a column vector matrix
389-- | Constructs a column vector matrix
367−col :: (FromLists e () rows) => [e] -> Matrix e () rows
390+col :: (FL () rows) => [e] -> Matrix e () rows
368col = fromLists . map (: [])
391col = fromLists . map (: [])
370-- | Constructs a row vector matrix
393-- | Constructs a row vector matrix
371−row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
394+row :: (FL cols ()) => [e] -> Matrix e cols ()
372row = fromLists . (: [])
395row = fromLists . (: [])
374-- | Lifts functions to matrices with arbitrary dimensions.
397-- | Lifts functions to matrices with arbitrary dimensions.
376-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
399-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
377-- types @a@ or @b@ allows.
400-- types @a@ or @b@ allows.
378−fromF ::
401+fromF' ::
379 forall a b cols rows e.
402 forall a b cols rows e.
380− ( Bounded a,
403+ ( Liftable e a b,
381− Bounded b,
404+ CountableDims cols rows,
387− KnownNat (Count cols),
388− KnownNat (Count rows),
389− FromLists e rows cols
405+ FL rows cols
391 (a -> b) ->
407 (a -> b) ->
392 Matrix e cols rows
408 Matrix e cols rows
393−fromF f =
409+fromF' f =
394 let minA = minBound @a
410 let minA = minBound @a
395 maxA = maxBound @a
411 maxA = maxBound @a
396 minB = minBound @b
412 minB = minBound @b
@@ -400,8 +416,8 @@ fromF f =
400 elementsA = take ccols [minA .. maxA]
416 elementsA = take ccols [minA .. maxA]
401 elementsB = take rrows [minB .. maxB]
417 elementsB = take rrows [minB .. maxB]
402 combinations = (,) <$> elementsA <*> elementsB
418 combinations = (,) <$> elementsA <*> elementsB
403− combAp = map snd . sort . map (\(a, b) -> if f a == b
419+ combAp = map snd . sort . map (\(a, b) -> if f a == b
404− then ((fromEnum a, fromEnum b), 1)
420+ then ((fromEnum a, fromEnum b), 1)
405 else ((fromEnum a, fromEnum b), 0)) $ combinations
421 else ((fromEnum a, fromEnum b), 0)) $ combinations
406 mList = buildList combAp rrows
422 mList = buildList combAp rrows
407 in tr $ fromLists mList
423 in tr $ fromLists mList
@@ -411,48 +427,23 @@ fromF f =
412-- | Lifts functions to matrices with dimensions matching @a@ and @b@
428-- | Lifts functions to matrices with dimensions matching @a@ and @b@
413-- cardinality's.
429-- cardinality's.
414−fromF' ::
430+fromF ::
415 forall a b e.
431 forall a b e.
416− ( Bounded a,
432+ ( Liftable e a b,
417− Bounded b,
433+ CountableDimsN a b,
423− KnownNat (Count (Normalize a)),
424− KnownNat (Count (Normalize b)),
425− FromLists e (Normalize b) (Normalize a)
427 (a -> b) ->
436 (a -> b) ->
428 Matrix e (Normalize a) (Normalize b)
437 Matrix e (Normalize a) (Normalize b)
429−fromF' f =
438+fromF = fromF'
430− let minA = minBound @a
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
446− buildList l r = take r l : buildList (drop r l) r
448-- Conversion
440-- Conversion
450-- | Converts a matrix to a list of lists of elements.
442-- | Converts a matrix to a list of lists of elements.
451toLists :: Matrix e cols rows -> [[e]]
443toLists :: Matrix e cols rows -> [[e]]
453toLists (One e) = [[e]]
444toLists (One e) = [[e]]
454−toLists (Split l r) = toLists l ++ toLists r
445+toLists (Fork l r) = toLists l ++ toLists r
455−toLists (Junc l r) = zipWith (++) (toLists l) (toLists r)
446+toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
457-- | Converts a matrix to a list of elements.
448-- | Converts a matrix to a list of elements.
458toList :: Matrix e cols rows -> [e]
449toList :: Matrix e cols rows -> [e]
@@ -461,37 +452,38 @@ toList = concat . toLists
461-- Zeros Matrix
452-- Zeros Matrix
463-- | The zero matrix. A matrix wholly filled with zeros.
454-- | The zero matrix. A matrix wholly filled with zeros.
464−zeros :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => Matrix e cols rows
455+zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
465−zeros = matrixBuilder (const 0)
456+zeros = matrixBuilder' (const 0)
467-- Ones Matrix
458-- Ones Matrix
469-- | The ones matrix. A matrix wholly filled with ones.
460-- | The ones matrix. A matrix wholly filled with ones.
471-- Also known as T (Top) matrix.
462-- Also known as T (Top) matrix.
472−ones :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => Matrix e cols rows
463+ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
473−ones = matrixBuilder (const 1)
464+ones = matrixBuilder' (const 1)
475-- Const Matrix
466-- Const Matrix
477-- | The constant matrix constructor. A matrix wholly filled with a given
468-- | The constant matrix constructor. A matrix wholly filled with a given
479−constant :: (Num e, FromLists e cols rows, KnownNat (Count cols), KnownNat (Count rows)) => e -> Matrix e cols rows
470+constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
480−constant e = matrixBuilder (const e)
471+constant e = matrixBuilder' (const e)
482-- Bang Matrix
473-- Bang Matrix
484-- | The T (Top) row vector matrix.
475-- | The T (Top) row vector matrix.
485−bang :: forall e cols. (Num e, Enum e, FromLists e cols (), KnownNat (Count cols)) => Matrix e cols ()
476+bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
487 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
478 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
488 in fromLists [take c [1, 1 ..]]
479 in fromLists [take c [1, 1 ..]]
490−-- Identity Matrix
481+-- iden Matrix
492−-- | Identity matrix.
483+-- | iden matrix.
493−identity :: (Num e, FromLists e cols cols, KnownNat (Count cols)) => Matrix e cols cols
484+iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
494−identity = matrixBuilder (bool 0 1 . uncurry (==))
485+iden = matrixBuilder' (bool 0 1 . uncurry (==))
486+{-# NOINLINE iden #-}
496-- Matrix composition (MMM)
488-- Matrix composition (MMM)
@@ -500,60 +492,90 @@ identity = matrixBuilder (bool 0 1 . uncurry (==))
500-- This definition takes advantage of divide-and-conquer and fusion laws
492-- This definition takes advantage of divide-and-conquer and fusion laws
501-- from LAoP.
493-- from LAoP.
502comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
494comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
503−comp Empty Empty = Empty
504−comp (One a) (One b) = One (a * b)
495+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
496+comp (Join a b) (Fork 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
497+comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
507−comp c (Junc a b) = Junc (comp c a) (comp c b) -- Junc fusion law
498+comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
499+{-# NOINLINE comp #-}
501+ "comp/iden1" forall m. comp m iden = m ;
502+ "comp/iden2" forall m. comp iden m = m
505+-- Scalar multiplication of matrices
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)
514+-- Scalar division of matrices
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)
509-- Projections
523-- Projections
511-- | Biproduct first component projection
525-- | Biproduct first component projection
512−p1 :: forall e m n. (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
526+p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
513−p1 =
527+p1 = join id zeros
514− let iden = identity :: Matrix e m m
515− zero = zeros :: Matrix e n m
518-- | Biproduct second component projection
529-- | Biproduct second component projection
519−p2 :: forall e m n. (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
530+p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
520−p2 =
531+p2 = join zeros id
521− let iden = identity :: Matrix e n n
522− zero = zeros :: Matrix e m n
525-- Injections
533-- Injections
527-- | Biproduct first component injection
535-- | Biproduct first component injection
528−i1 :: (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
536+i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
529i1 = tr p1
537i1 = tr p1
531-- | Biproduct second component injection
539-- | Biproduct second component injection
532−i2 :: (Num e, KnownNat (Count n), KnownNat (Count m), FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
540+i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
533i2 = tr p2
541i2 = tr p2
535-- Dimensions
543-- Dimensions
537-- | Obtain the number of rows.
545-- | Obtain the number of rows.
539−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
547+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
540−-- dimensions in constant time.
548+-- dimensions in constant time. For a version that doesn't require the
541−--
549+-- constraint see 'rows''.
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
550+rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
545rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
551rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
553+-- | Obtain the number of rows in an inefficient manner, but without any
556+-- For a more efficient version see 'rows'.
557+rows' :: Matrix e cols rows -> Int
559+rows' (Join lhs _) = rows' lhs
560+rows' (Fork top bottom) = rows' top + rows' bottom
547-- | Obtain the number of columns.
562-- | Obtain the number of columns.
549−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
550−-- dimensions in constant time.
552−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
564+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
553−-- exchange for performance.
565+-- dimensions in constant time. For a version that doesn't require the
566+-- constraint see 'columns''.
554−columns :: forall e cols rows. (KnownNat (Count cols)) => Matrix e cols rows -> Int
567+columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
555columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
568columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
570+-- | Obtain the number of columns in an inefficient manner, but without any
573+-- For a more efficient version see 'columns'.
574+columns' :: Matrix e cols rows -> Int
576+columns' (Join lhs rhs) = columns' lhs + columns' rhs
577+columns' (Fork top _) = columns' top
557-- Coproduct Bifunctor
579-- Coproduct Bifunctor
559infixl 5 -|-
581infixl 5 -|-
@@ -562,157 +584,172 @@ infixl 5 -|-
563 forall e n k m j.
585 forall e n k m j.
565− KnownNat (Count j),
587+ CountableDims j k,
566− KnownNat (Count k),
588+ FL k k,
567− FromLists e k k,
589+ FL j k,
568− FromLists e j k,
590+ FL k j,
569− FromLists e k j,
591+ FL j j
572 Matrix e n k ->
593 Matrix e n k ->
573 Matrix e m j ->
594 Matrix e m j ->
574 Matrix e (Either n m) (Either k j)
595 Matrix e (Either n m) (Either k j)
575−(-|-) a b = Junc (i1 . a) (i2 . b)
596+(-|-) a b = Join (i1 . a) (i2 . b)
577-- Khatri Rao Product and projections
598-- Khatri Rao Product and projections
579-- | Khatri Rao product first component projection matrix.
600-- | Khatri Rao product first component projection matrix.
581 forall e m k .
602 forall e m k .
583− KnownNat (Count k),
604+ CountableDims k m,
584− FromLists e (FromNat (Count m * Count k)) m,
586− KnownNat (Count (Normalize (m, k)))
605+ FL (Normalize (m, k)) m,
587 ) => Matrix e (Normalize (m, k)) m
607 ) => Matrix e (Normalize (m, k)) m
588−kp1 = matrixBuilder f
608+fstM = matrixBuilder' f
590 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
610 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
592 | y >= (x * offset) && y <= (x * offset + offset - 1) = 1
612 | y >= (x * offset) && y <= (x * offset + offset - 1) = 1
593− | otherwise = 0
613+ | otherwise = 0
595-- | Khatri Rao product second component projection matrix.
615-- | Khatri Rao product second component projection matrix.
597 forall e m k .
617 forall e m k .
599− KnownNat (Count k),
619+ CountableDims k m,
600− FromLists e (FromNat (Count m * Count k)) k,
602− KnownNat (Count (Normalize (m, k)))
620+ FL (Normalize (m, k)) k,
603 ) => Matrix e (Normalize (m, k)) k
622 ) => Matrix e (Normalize (m, k)) k
604−kp2 = matrixBuilder f
623+sndM = matrixBuilder' f
606 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
625 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
608 | x == y || mod (y - x) offset == 0 = 1
627 | x == y || mod (y - x) offset == 0 = 1
609− | otherwise = 0
628+ | otherwise = 0
611-- | Khatri Rao Matrix product also known as matrix pairing.
630-- | Khatri Rao Matrix product also known as matrix pairing.
613-- NOTE: That this is not a true categorical product, see for instance:
632-- NOTE: That this is not a true categorical product, see for instance:
616−-- | kp1 . khatri a b == a
635+-- | fstM . kr a b == a
617−-- khatri a b ==> |
636+-- kr a b ==> |
618−-- | kp2 . khatri a b == b
637+-- | sndM . kr a b == b
621-- __Emphasis__ on the implication symbol.
640-- __Emphasis__ on the implication symbol.
623− forall e cols a b.
642+ forall e cols a b.
625− KnownNat (Count a),
644+ CountableDims a b,
626− KnownNat (Count b),
645+ CountableN (a, b),
627− KnownNat (Count (Normalize (a, b))),
628− FromLists e (Normalize (a, b)) a,
646+ FL (Normalize (a, b)) a,
629− FromLists e (Normalize (a, b)) b
647+ FL (Normalize (a, b)) b
630 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
648 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
631−khatri a b =
649+kr a b =
632− let kp1' = kp1 @e @a @b
650+ let fstM' = fstM @e @a @b
633− kp2' = kp2 @e @a @b
651+ sndM' = sndM @e @a @b
634− in (tr kp1') . a * (tr kp2') . b
652+ in (tr fstM' . a) * (tr sndM' . b)
636-- Product Bifunctor (Kronecker)
654-- Product Bifunctor (Kronecker)
638infixl 4 ><
656infixl 4 ><
640-- | Matrix product functor also known as kronecker product
658-- | Matrix product functor also known as kronecker product
642− forall e m p n q.
660+ forall e m p n q.
644− KnownNat (Count m),
662+ CountableDims m n,
645− KnownNat (Count n),
663+ CountableDims p q,
646− KnownNat (Count p),
664+ CountableDimsN (m, n) (p, q),
648− KnownNat (Count (Normalize (m, n))),
649− FromLists e (Normalize (m, n)) m,
665+ FL (Normalize (m, n)) m,
650− FromLists e (Normalize (m, n)) n,
666+ FL (Normalize (m, n)) n,
651− KnownNat (Count (Normalize (p, q))),
652− FromLists e (Normalize (p, q)) p,
667+ FL (Normalize (p, q)) p,
653− FromLists e (Normalize (p, q)) q
668+ FL (Normalize (p, q)) q
655 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
670 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
656(><) a b =
671(><) a b =
657− let kp1' = kp1 @e @m @n
672+ let fstM' = fstM @e @m @n
658− kp2' = kp2 @e @m @n
673+ sndM' = sndM @e @m @n
659− in khatri (a . kp1') (b . kp2')
674+ in kr (a . fstM') (b . sndM')
661−-- Matrix abide Junc Split
676+-- Matrix abide Join Fork
663−-- | Matrix "abiding" followin the 'Junc'-'Split' abide law.
678+-- | Matrix "abiding" following the 'Join'-'Fork' exchange law.
668−-- 'Junc' ('Split' a c) ('Split' b d) == 'Split' ('Junc' a b) ('Junc' c d)
683+-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
670−abideJS :: Matrix e cols rows -> Matrix e cols rows
685+abideJF :: 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
686+abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
672−abideJS Empty = Empty
673−abideJS (One e) = One e
687+abideJF (One e) = One e
674−abideJS (Junc a b) = Junc (abideJS a) (abideJS b)
688+abideJF (Join a b) = Join (abideJF a) (abideJF b)
675−abideJS (Split a b) = Split (abideJS a) (abideJS b)
689+abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
677−-- Matrix abide Split Junc
691+-- Matrix abide Fork Join
679−-- | Matrix "abiding" followin the 'Split'-'Junc' abide law.
693+-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
682−-- 'Split' ('Junc' a b) ('Junc' c d) == 'Junc' ('Split' a c) ('Split' b d)
696+-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
684−abideSJ :: Matrix e cols rows -> Matrix e cols rows
698+abideFJ :: 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
699+abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
686−abideSJ Empty = Empty
687−abideSJ (One e) = One e
700+abideFJ (One e) = One e
688−abideSJ (Junc a b) = Junc (abideSJ a) (abideSJ b)
701+abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
689−abideSJ (Split a b) = Split (abideSJ a) (abideSJ b)
702+abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
691-- Matrix transposition
704-- Matrix transposition
693-- | Matrix transposition.
706-- | Matrix transposition.
694tr :: Matrix e cols rows -> Matrix e rows cols
707tr :: Matrix e cols rows -> Matrix e rows cols
696−tr (One e) = One e
708+tr (One e) = One e
697−tr (Junc a b) = Split (tr a) (tr b)
709+tr (Join a b) = Fork (tr a) (tr b)
698−tr (Split a b) = Junc (tr a) (tr b)
710+tr (Fork a b) = Join (tr a) (tr b)
700-- Selective 'select' operator
712-- Selective 'select' operator
702-- | Selective functors 'select' operator equivalent inspired by the
714-- | Selective functors 'select' operator equivalent inspired by the
703-- ArrowMonad solution presented in the paper.
715-- 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
724+ CountableDims c (Either b c),
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
735+ => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
705−select m y = junc y identity . m
736+branch x l r = f x `select` g l `select` r
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)
707-- McCarthy's Conditional
744-- McCarthy's Conditional
709-- | McCarthy's Conditional expresses probabilistic choice.
746-- | McCarthy's Conditional expresses probabilistic choice.
711− ( cols ~ FromNat (Count cols),
748+ ( Trivial cols,
712− KnownNat (Count cols),
749+ Countable cols,
713− FromLists e () cols,
750+ FL () cols,
714− FromLists e cols (),
751+ FL cols (),
715− FromLists e cols cols,
752+ FL cols cols,
716 Bounded a,
753 Bounded a,
@@ -720,14 +757,14 @@ cond ::
722 (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
759 (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
723−cond p f g = junc f g . grd p
760+cond p f g = join f g . grd p
726− ( q ~ FromNat (Count q),
763+ ( Trivial q,
727− KnownNat (Count q),
764+ Countable q,
728− FromLists e () q,
765+ FL () q,
729− FromLists e q (),
766+ FL q (),
730− FromLists e q q,
767+ FL q q,
731 Bounded a,
768 Bounded a,
@@ -735,63 +772,86 @@ grd ::
737 (a -> Bool) -> Matrix e q (Either q q)
774 (a -> Bool) -> Matrix e q (Either q q)
738−grd f = split (corr f) (corr (not . f))
775+grd f = fork (corr f) (corr (not . f))
741− forall e a q .
778+ forall e a q .
742− ( q ~ FromNat (Count q),
779+ ( Trivial q,
743− KnownNat (Count q),
780+ Countable q,
744− FromLists e () q,
781+ FL () q,
745− FromLists e q (),
782+ FL q (),
746− FromLists e q q,
783+ FL q q,
747− Bounded a,
784+ Liftable e a Bool
752 => (a -> Bool) -> Matrix e q q
786 => (a -> Bool) -> Matrix e q q
753−corr p = let f = fromF p :: Matrix e q ()
787+corr p = let f = fromF' p :: Matrix e q ()
754− in khatri f (identity :: Matrix e q q)
788+ in kr f (id :: Matrix e q q)
756-- Pretty print
790-- Pretty print
758prettyAux :: Show e => [[e]] -> [[e]] -> String
792prettyAux :: Show e => [[e]] -> [[e]] -> String
759prettyAux [] _ = ""
793prettyAux [] _ = ""
760−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
794+prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
762 v = fmap show m
796 v = fmap show m
763 widest = maximum $ fmap length v
797 widest = maximum $ fmap length v
764 fill str = replicate (widest - length str - 2) ' ' ++ str
798 fill str = replicate (widest - length str - 2) ' ' ++ str
765−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
799+prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
767− v = fmap show m
801+ v = fmap show m
768− widest = maximum $ fmap length v
802+ widest = maximum $ fmap length v
769 fill str = replicate (widest - length str - 2) ' ' ++ str
803 fill str = replicate (widest - length str - 2) ' ' ++ str
770−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
804+prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
771 prettyAux t l
805 prettyAux t l
773− v = fmap show l
807+ v = fmap show l
774− widest = maximum $ fmap length v
808+ widest = maximum $ fmap length v
775 fill str = replicate (widest - length str - 2) ' ' ++ str
809 fill str = replicate (widest - length str - 2) ' ' ++ str
777-- | Matrix pretty printer
811-- | Matrix pretty printer
778−pretty :: (KnownNat (Count cols), Show e) => Matrix e cols rows -> String
812+pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
779−pretty m = "┌ " ++ unwords (replicate (columns m) blank) ++ " ┐\n" ++
814+ [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
780− prettyAux (toLists m) (toLists m) ++
815+ , unlines
816+ [ "│ " ++ unwords (fmap (\j -> fill $ show $ getElem i j m) [1..columns m]) ++ " │" | i <- [1..rows m] ]
781− "└ " ++ unwords (replicate (columns m) blank) ++ " ┘"
817+ , "└ ", unwords (replicate (columns m) blank), " ┘"
783− v = fmap show (toList m)
820+ strings = map show (toList m)
784− widest = maximum $ fmap length v
821+ widest = maximum $ map length strings
785 fill str = replicate (widest - length str) ' ' ++ str
822 fill str = replicate (widest - length str) ' ' ++ str
786− blank = fill ""
823+ blank = fill ""
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
832+ "getElem: Trying to get the "
834+ ++ " element from a "
835+ ++ show (rows m) ++ "x" ++ show (columns m)
788-- | Matrix pretty printer
840-- | Matrix pretty printer
789−prettyPrint :: (KnownNat (Count cols), Show e) => Matrix e cols rows -> IO ()
841+prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
790prettyPrint = putStrLn . pretty
842prettyPrint = putStrLn . pretty
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
792-- Relational operators functions
852-- Relational operators functions
794−type Boolean = Natural 0 1
854+type Boolean = Natural 0 1
795type Relation a b = Matrix Boolean a b
855type Relation a b = Matrix Boolean a b
797-- | Helper conversion function
857-- | Helper conversion function
@@ -807,54 +867,47 @@ fromBool False = nat 0
808-- | Relational negation
868-- | Relational negation
809negateM :: Relation cols rows -> Relation cols rows
869negateM :: Relation cols rows -> Relation cols rows
810−negateM Empty = Empty
811−negateM (One (Nat 0)) = One (Nat 1)
870+negateM (One (Nat p)) = One (Nat (negate p))
812−negateM (One (Nat 1)) = One (Nat 0)
813−negateM (Junc a b) = Junc (negateM a) (negateM b)
871+negateM (Join a b) = Join (negateM a) (negateM b)
814−negateM (Split a b) = Split (negateM a) (negateM b)
872+negateM (Fork a b) = Fork (negateM a) (negateM b)
816-- | Relational addition
874-- | Relational addition
817orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
875orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
818−orM Empty Empty = Empty
819−orM (One a) (One b) = One (fromBool (toBool a || toBool b))
876+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)
877+orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
821−orM (Split a b) (Split c d) = Split (orM a c) (orM b d)
878+orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
822−orM x@(Split _ _) y@(Junc _ _) = orM x (abideJS y)
879+orM x@(Fork _ _) y@(Join _ _) = orM x (abideJF y)
823−orM x@(Junc _ _) y@(Split _ _) = orM (abideJS x) y
880+orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
825-- | Relational multiplication
882-- | Relational multiplication
826andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
883andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
827−andM Empty Empty = Empty
828−andM (One a) (One b) = One (fromBool (toBool a && toBool b))
884+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)
885+andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
830−andM (Split a b) (Split c d) = Split (andM a c) (andM b d)
886+andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
831−andM x@(Split _ _) y@(Junc _ _) = andM x (abideJS y)
887+andM x@(Fork _ _) y@(Join _ _) = andM x (abideJF y)
832−andM x@(Junc _ _) y@(Split _ _) = andM (abideJS x) y
888+andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
834-- | Relational subtraction
890-- | Relational subtraction
835subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
836−subM Empty Empty = Empty
837−subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
892+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)
893+subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
839−subM (Split a b) (Split c d) = Split (subM a c) (subM b d)
894+subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
840−subM x@(Split _ _) y@(Junc _ _) = subM x (abideJS y)
895+subM x@(Fork _ _) y@(Join _ _) = subM x (abideJF y)
841−subM x@(Junc _ _) y@(Split _ _) = subM (abideJS x) y
896+subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
843-- | Matrix relational composition.
898-- | Matrix relational composition.
844compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
899compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
845−compRel Empty Empty = Empty
846−compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
900+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
901+compRel (Join a b) (Fork 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
902+compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
849−compRel c (Junc a b) = Junc (compRel c a) (compRel c b) -- Junc fusion law
903+compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion law
851-- | Matrix relational right division
905-- | Matrix relational right division
852divR :: Relation b c -> Relation b a -> Relation a c
906divR :: Relation b c -> Relation b a -> Relation a c
853−divR Empty Empty = Empty
854divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
907divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
855−divR (Junc a b) (Junc c d) = andM (divR a c) (divR b d)
908+divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
856−divR (Split a b) c = Split (divR a c) (divR b c)
909+divR (Fork a b) c = Fork (divR a c) (divR b c)
857−divR c (Split a b) = Junc (divR c a) (divR c b)
910+divR c (Fork a b) = Join (divR c a) (divR c b)
859-- | Matrix relational left division
912-- | Matrix relational left division
860divL :: Relation c b -> Relation a b -> Relation a c
913divL :: Relation c b -> Relation a b -> Relation a c
@@ -870,97 +923,52 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
871-- NOTE: Be careful to not ask for a relation bigger than the cardinality of
924-- NOTE: Be careful to not ask for a relation bigger than the cardinality of
872-- types @a@ or @b@ allows.
925-- types @a@ or @b@ allows.
873−fromFRel ::
926+fromFRel' ::
874 forall a b cols rows.
927 forall a b cols rows.
875− ( Bounded a,
928+ ( Liftable Boolean a b,
876− Bounded b,
929+ CountableDims cols rows,
880− KnownNat (Count cols),
881− KnownNat (Count rows),
882− FromLists (Natural 0 1) rows cols
930+ FL rows cols
884 (a -> b) ->
932 (a -> b) ->
885 Relation cols rows
933 Relation cols rows
886−fromFRel f =
934+fromFRel' = fromF'
887− let minA = minBound @a
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
903− buildList l r = take r l : buildList (drop r l) r
905-- | Lifts functions to relations with dimensions matching @a@ and @b@
936-- | Lifts functions to relations with dimensions matching @a@ and @b@
906-- cardinality's.
937-- cardinality's.
907−fromFRel' ::
938+fromFRel ::
908 forall a b.
939 forall a b.
909− ( Bounded a,
940+ ( Liftable Boolean a b,
910− Bounded b,
941+ CountableDimsN a b,
914− KnownNat (Count (Normalize a)),
915− KnownNat (Count (Normalize b)),
916− FromLists (Natural 0 1) (Normalize b) (Normalize a)
918 (a -> b) ->
944 (a -> b) ->
919 Relation (Normalize a) (Normalize b)
945 Relation (Normalize a) (Normalize b)
920−fromFRel' f =
946+fromFRel = fromFRel'
921− let minA = minBound @a
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
937− buildList l r = take r l : buildList (drop r l) r
939-- | Lifts a relation function to a Boolean Matrix
948-- | Lifts a relation function to a Boolean Matrix
940−toRel ::
949+toRel ::
941 forall a b.
950 forall a b.
942 ( Bounded a,
951 ( Bounded a,
943 Bounded b,
952 Bounded b,
947− KnownNat (Count (Normalize a)),
956+ CountableDims a b,
948− KnownNat (Count (Normalize b)),
957+ FLN b a
949− FromLists (Natural 0 1) (Normalize b) (Normalize a)
951 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
953 let minA = minBound @a
961 let minA = minBound @a
954 maxA = maxBound @a
962 maxA = maxBound @a
955 minB = minBound @b
963 minB = minBound @b
956 maxB = maxBound @b
964 maxB = maxBound @b
957− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
958− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
959 elementsA = take ccols [minA .. maxA]
967 elementsA = take ccols [minA .. maxA]
960 elementsB = take rrows [minB .. maxB]
968 elementsB = take rrows [minB .. maxB]
961 combinations = (,) <$> elementsA <*> elementsB
969 combinations = (,) <$> elementsA <*> elementsB
962− combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
970+ combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
963− then ((fromEnum a, fromEnum b), nat 1)
971+ then ((fromEnum a, fromEnum b), nat 1)
964 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
972 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
965 mList = buildList combAp rrows
973 mList = buildList combAp rrows
966 in tr $ fromLists mList
974 in tr $ fromLists mList