on master help sign in
Simplified type constraints aliases
commita3aa4a2b321f20380d32e0f32eccddbbe419dd16 authorArmando Santos <armandoifsantos@gmail.com> dateFri 8 May 2020, 19:35 (6 years ago) parent1e13cba
9 files changed+419−401
.ghc.environment.x86_64-linux-8.8.3+17 app/Benchmarks.hs+7−7 src/LAoP/Dist.hs+2−2 src/LAoP/Dist/Internal.hs+43−43 src/LAoP/Matrix/Internal.hs+86−84 src/LAoP/Matrix/Nat.hs+63−63 src/LAoP/Matrix/Type.hs+80−81 src/LAoP/Relation.hs+3−3 src/LAoP/Relation/Internal.hs+118−118
.ghc.environment.x86_64-linux-8.8.3new file+17
@@ -0,0 +1,17 @@
1+-- This is a GHC environment file written by cabal. This means you can
2+-- run ghc or ghci and get the environment of the project as a whole.
3+-- But you still need to use cabal repl $target to get the environment
4+-- of specific components (libs, exes, tests etc) because each one can
5+-- have its own source dirs, cpp flags etc.
6+--
7+clear-package-db
8+global-package-db
9+package-db /home/bolt/.cabal/store/ghc-8.8.3/package.db
10+package-db dist-docs.Q1TJPL/packagedb/ghc-8.8.3
11+package-id laop-0.1.0.7-inplace
12+package-id base-4.13.0.0
13+package-id ghc-prim-0.5.3
14+package-id rts
15+package-id integer-gmp-1.0.2.0
16+package-id deepseq-1.4.4.0
17+package-id array-0.5.4.0
app/Benchmarks.hs+7−7
@@ -15,8 +15,8 @@ module Benchmarks
1515
1616import LAoP.Dist.Internal
1717import LAoP.Utils
18−import LAoP.Matrix.Type hiding (CountableN, Countable, FromListsN)
19−import qualified LAoP.Matrix.Type as T (FromListsN)
18+import LAoP.Matrix.Type hiding (CountableN, Countable, FLN)
19+import qualified LAoP.Matrix.Type as T (FLN)
2020import Criterion.Main
2121import Control.DeepSeq
2222import Test.QuickCheck
@@ -26,13 +26,13 @@ import Prelude hiding (id, (.))
2626
2727selectM2 ::
2828 ( Num e,
29− T.FromListsN e b b,
29+ T.FLN b b,
3030 CountableN b
3131 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
3232selectM2 m y = join y iden `comp` m
3333
3434selectD2 ::
35− ( FromListsN b b,
35+ ( FLN b b,
3636 CountableN b
3737 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
3838selectD2 (D d) m = D (join m iden `comp` d)
@@ -40,7 +40,7 @@ selectD2 (D d) m = D (join m iden `comp` d)
4040-- Composition with iden
4141compId m = comp iden m
4242
43−randomDist :: forall a . (Countable a, FromListsN () a) => Gen (Dist a)
43+randomDist :: forall a . (Countable a, FLN () a) => Gen (Dist a)
4444randomDist = do
4545 let size = fromInteger (natVal (Proxy :: Proxy (Count a)))
4646 l <- vectorOf size (arbitrary :: Gen Prob)
@@ -49,7 +49,7 @@ randomDist = do
4949 m = fromLists lr
5050 return (D m)
5151
52−randomMatrix :: forall a b . (CountableDimensions a b, FromListsN a b) => Gen (Matrix Prob a b)
52+randomMatrix :: forall a b . (CountableDims a b, FLN a b) => Gen (Matrix Prob a b)
5353randomMatrix = do
5454 let cols = fromInteger (natVal (Proxy :: Proxy (Count a)))
5555 rows = fromInteger (natVal (Proxy :: Proxy (Count b)))
@@ -108,7 +108,7 @@ normalize3 as l = let s = sum l
108108 probs = map (/ s) l
109109 in zip as probs
110110
111−randomDistF :: forall a b . (CountableDimensions a b, CoArbitrary a, Arbitrary b) => Gen (Dist' (a -> b))
111+randomDistF :: forall a b . (CountableDims a b, CoArbitrary a, Arbitrary b) => Gen (Dist' (a -> b))
112112randomDistF = do
113113 let a = fromInteger (natVal (Proxy :: Proxy (Count a)))
114114 b = fromInteger (natVal (Proxy :: Proxy (Count b)))
src/LAoP/Dist.hs+2−2
@@ -39,8 +39,8 @@ module LAoP.Dist
3939 -- * Constraint type synonyms
4040 Countable,
4141 CountableN,
42− CountableDimensionsN,
43− FromListsN,
42+ CountableDimsN,
43+ FLN,
4444 Liftable,
4545 TrivialP,
4646
src/LAoP/Dist/Internal.hs+43−43
@@ -16,8 +16,8 @@ module LAoP.Dist.Internal
1616
1717 Countable,
1818 CountableN,
19− CountableDimensionsN,
20− FromListsN,
19+ CountableDimsN,
20+ FLN,
2121 Liftable,
2222 TrivialP,
2323
@@ -43,7 +43,7 @@ module LAoP.Dist.Internal
4343 )
4444 where
4545
46−import LAoP.Matrix.Type hiding (TrivialP, Countable, CountableDimensions, CountableN, CountableDimensionsN, Liftable, FromListsN)
46+import LAoP.Matrix.Type hiding (TrivialP, Countable, CountableDims, CountableN, CountableDimsN, Liftable, FLN)
4747import Prelude hiding (id, (.))
4848import qualified LAoP.Matrix.Internal as I
4949import LAoP.Utils
@@ -62,18 +62,18 @@ newtype Dist a = D (Matrix Prob () a)
6262 deriving (Show, Num, Eq, Ord, NFData) via (Matrix Prob () a)
6363
6464-- | Constraint type synonyms to keep the type signatures less convoluted
65−type Countable a = KnownNat (I.Count a)
66−type CountableN a = KnownNat (I.Count (I.Normalize a))
67−type CountableDimensionsN a b = (CountableN a, CountableN b)
68−type FromListsN a b = I.FromLists Prob (I.Normalize a) (I.Normalize b)
69−type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Prob, Ord Prob)
70−type TrivialP a b = Normalize (a, b) ~ Normalize (Normalize a, Normalize b)
65+type Countable a = KnownNat (I.Count a)
66+type CountableN a = KnownNat (I.Count (I.Normalize a))
67+type CountableDimsN a b = (CountableN a, CountableN b)
68+type FLN a b = I.FL (I.Normalize a) (I.Normalize b)
69+type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Prob, Ord Prob)
70+type TrivialP a b = Normalize (a, b) ~ Normalize (Normalize a, Normalize b)
7171
7272-- | Functor instance
7373fmapD ::
7474 ( Liftable a b,
75− CountableDimensionsN a b,
76− FromListsN b a
75+ CountableDimsN a b,
76+ FLN b a
7777 )
7878 =>
7979 (a -> b) -> Dist a -> Dist b
@@ -85,17 +85,17 @@ unitD = D (one 1)
8585
8686-- | Applicative/Monoidal instance 'mult' function
8787multD ::
88− ( CountableDimensionsN a b,
88+ ( CountableDimsN a b,
8989 CountableN (a, b),
90− FromListsN (a, b) a,
91− FromListsN (a, b) b,
90+ FLN (a, b) a,
91+ FLN (a, b) b,
9292 TrivialP a b
9393 ) => Dist a -> Dist b -> Dist (a, b)
9494multD (D a) (D b) = D (kr a b)
9595
9696-- | Selective instance function
9797selectD ::
98− ( FromListsN b b,
98+ ( FLN b b,
9999 CountableN b
100100 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
101101selectD (D d) m = D (selectM d m)
@@ -104,17 +104,17 @@ selectD (D d) m = D (selectM d m)
104104-- functions to apply to a given argument;
105105branchD ::
106106 ( Num e,
107− CountableDimensionsN a b,
108− CountableDimensionsN c (Either b c),
109− FromListsN c b,
110− FromListsN a b,
111− FromListsN a a,
112− FromListsN b b,
113− FromListsN c c,
114− FromListsN b a,
115− FromListsN b c,
116− FromListsN (Either b c) b,
117− FromListsN (Either b c) c
107+ CountableDimsN a b,
108+ CountableDimsN c (Either b c),
109+ FLN c b,
110+ FLN a b,
111+ FLN a a,
112+ FLN b b,
113+ FLN c c,
114+ FLN b a,
115+ FLN b c,
116+ FLN (Either b c) b,
117+ FLN (Either b c) c
118118 )
119119 => Dist (Either a b) -> Matrix Prob a c -> Matrix Prob b c -> Dist c
120120branchD x l r = f x `selectD` g l `selectD` r
@@ -124,11 +124,11 @@ branchD x l r = f x `selectD` g l `selectD` r
124124
125125-- | Branch on a Boolean value, skipping unnecessary computations.
126126ifD ::
127− ( CountableDimensionsN a (Either () a),
128− FromListsN a a,
129− FromListsN a (),
130− FromListsN () a,
131− FromListsN (Either () a) a
127+ ( CountableDimsN a (Either () a),
128+ FLN a a,
129+ FLN a (),
130+ FLN () a,
131+ FLN (Either () a) a
132132 )
133133 => Dist Bool -> Dist a -> Dist a -> Dist a
134134ifD x (D t) (D e) = branchD x' t e
@@ -136,7 +136,7 @@ ifD x (D t) (D e) = branchD x' t e
136136 x' = bool (Right ()) (Left ()) `fmapD` x
137137
138138-- | Monad instance 'return' function
139−returnD :: forall a . (Enum a, FromListsN () a, Countable a) => a -> Dist a
139+returnD :: forall a . (Enum a, FLN () a, Countable a) => a -> Dist a
140140returnD a = D (col l)
141141 where
142142 i = fromInteger $ natVal (Proxy :: Proxy (Count a))
@@ -151,7 +151,7 @@ bindD (D d) m = D (m `comp` d)
151151(??) ::
152152 ( Enum a,
153153 Countable a,
154− FromListsN () a
154+ FLN () a
155155 ) => (a -> Bool) -> Dist a -> Prob
156156(??) p d =
157157 let l = toValues d
@@ -161,11 +161,11 @@ bindD (D d) m = D (m `comp` d)
161161-- Distribution Construction
162162
163163-- | Constructs a Bernoulli distribution
164−choose :: (FromListsN () a) => Prob -> Dist a
164+choose :: (FLN () a) => Prob -> Dist a
165165choose prob = D (col [prob, 1 - prob])
166166
167167-- | Creates a distribution given a shape function
168−shape :: (FromListsN () a) => (Prob -> Prob) -> [a] -> Dist a
168+shape :: (FLN () a) => (Prob -> Prob) -> [a] -> Dist a
169169shape _ [] = error "Probability.shape: empty list"
170170shape f xs =
171171 let incr = 1 / fromIntegral (length xs - 1)
@@ -173,23 +173,23 @@ shape f xs =
173173 in fromFreqs (zip xs ps)
174174
175175-- | Constructs a Linear distribution
176−linear :: (FromListsN () a) => [a] -> Dist a
176+linear :: (FLN () a) => [a] -> Dist a
177177linear = shape id
178178
179179-- | Constructs an Uniform distribution
180−uniform :: (FromListsN () a) => [a] -> Dist a
180+uniform :: (FLN () a) => [a] -> Dist a
181181uniform = shape (const 1)
182182
183183-- | Constructs an Negative Exponential distribution
184−negExp :: (FromListsN () a) => [a] -> Dist a
184+negExp :: (FLN () a) => [a] -> Dist a
185185negExp = shape (\x -> exp (-x))
186186
187187-- | Constructs an Normal distribution
188−normal :: (FromListsN () a) => [a] -> Dist a
188+normal :: (FLN () a) => [a] -> Dist a
189189normal = shape (normalCurve 0.5 0.5)
190190
191191-- | Transforms a 'Dist' into a list of pairs.
192−toValues :: forall a . (Enum a, Countable a, FromListsN () a) => Dist a -> [(a, Prob)]
192+toValues :: forall a . (Enum a, Countable a, FLN () a) => Dist a -> [(a, Prob)]
193193toValues (D d) =
194194 let rows = fromInteger (natVal (Proxy :: Proxy (Count a)))
195195 probs = toList d
@@ -197,7 +197,7 @@ toValues (D d) =
197197 in res
198198
199199-- | Pretty a distribution
200−prettyDist :: forall a. (Show a, Enum a, Countable a, FromListsN () a) => Dist a -> String
200+prettyDist :: forall a. (Show a, Enum a, Countable a, FLN () a) => Dist a -> String
201201prettyDist d =
202202 let values = sortBy (\(a, p1) (b, p2) -> compare p2 p1) (toValues @a d)
203203 w = maximum (map (length . show . fst) values)
@@ -209,12 +209,12 @@ prettyDist d =
209209 showR n x = show x ++ " "
210210
211211-- | Pretty Print a distribution
212−prettyPrintDist :: forall a . (Show a, Enum a, Countable a, FromListsN () a) => Dist a -> IO ()
212+prettyPrintDist :: forall a . (Show a, Enum a, Countable a, FLN () a) => Dist a -> IO ()
213213prettyPrintDist = putStrLn . prettyDist @a
214214
215215-- Auxiliary functions
216216
217−fromFreqs :: (FromListsN () a) => [(a,Prob)] -> Dist a
217+fromFreqs :: (FLN () a) => [(a,Prob)] -> Dist a
218218fromFreqs xs = D (col (map (\(x,p) -> p/q) xs))
219219 where q = sum $ map snd xs
220220
src/LAoP/Matrix/Internal.hs+86−84
@@ -51,10 +51,11 @@ module LAoP.Matrix.Internal
5151
5252 -- * Constraint type aliases
5353 Countable,
54− CountableDimensions,
54+ CountableDims,
5555 CountableN,
56− CountableDimensionsN,
57− FromListsN,
56+ CountableDimsN,
57+ FL,
58+ FLN,
5859 Liftable,
5960 Trivial,
6061
@@ -231,16 +232,17 @@ type family Normalize (d :: Type) :: Type where
231232-- | Constraint type synonyms to keep the type signatures less convoluted
232233type Countable a = KnownNat (Count a)
233234type CountableN a = KnownNat (Count (Normalize a))
234−type CountableDimensions a b = (Countable a, Countable b)
235−type CountableDimensionsN a b = (CountableN a, CountableN b)
236−type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
235+type CountableDims a b = (Countable a, Countable b)
236+type CountableDimsN a b = (CountableN a, CountableN b)
237+type FL a b = FromLists a b
238+type FLN a b = FromLists (Normalize a) (Normalize b)
237239type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
238240type Trivial a = FromNat (Count a) ~ a
239241
240242-- | It is possible to implement a constrained version of the category type
241243-- class.
242244instance (Num e) => Category (Matrix e) where
243− type Object (Matrix e) a = (FromLists e a a, Countable a)
245+ type Object (Matrix e) a = (FL a a, Countable a)
244246 id = iden
245247 (.) = comp
246248
@@ -320,40 +322,40 @@ infixl 2 ===
320322--
321323-- Given that it is not possible to branch on types at the term level type
322324-- classes are needed very much like an inductive definition but on types.
323−class FromLists e cols rows where
325+class FromLists cols rows where
324326 -- | Build a matrix out of a list of list of elements. Throws a runtime
325327 -- error if the dimensions do not match.
326328 fromLists :: [[e]] -> Matrix e cols rows
327329
328−instance FromLists e Void Void where
330+instance FromLists Void Void where
329331 fromLists [] = Empty
330332 fromLists _ = error "Wrong dimensions"
331333
332−instance {-# OVERLAPPING #-} FromLists e () () where
334+instance {-# OVERLAPPING #-} FromLists () () where
333335 fromLists [[e]] = One e
334336 fromLists _ = error "Wrong dimensions"
335337
336−instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
338+instance {-# OVERLAPPING #-} (FromLists cols ()) => FromLists (Either () cols) () where
337339 fromLists [h : t] = Join (One h) (fromLists [t])
338340 fromLists _ = error "Wrong dimensions"
339341
340−instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
342+instance {-# OVERLAPPABLE #-} (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
341343 fromLists [l] =
342344 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343345 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
344346 fromLists _ = error "Wrong dimensions"
345347
346−instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
348+instance {-# OVERLAPPING #-} (FromLists () rows) => FromLists () (Either () rows) where
347349 fromLists ([h] : t) = Fork (One h) (fromLists t)
348350 fromLists _ = error "Wrong dimensions"
349351
350−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
352+instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
351353 fromLists l@([_] : _) =
352354 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
353355 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
354356 fromLists _ = error "Wrong dimensions"
355357
356−instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) where
358+instance {-# OVERLAPPABLE #-} (FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (Either a b) (Either c d) where
357359 fromLists l@(h : t) =
358360 let lh = length h
359361 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -366,8 +368,8 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
366368-- a construction function that operates with indices.
367369matrixBuilder' ::
368370 forall e cols rows.
369− ( FromLists e cols rows,
370− CountableDimensions cols rows
371+ ( FL cols rows,
372+ CountableDims cols rows
371373 ) => ((Int, Int) -> e) -> Matrix e cols rows
372374matrixBuilder' f =
373375 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
@@ -379,14 +381,14 @@ matrixBuilder' f =
379381-- a construction function that operates with arbitrary types.
380382matrixBuilder ::
381383 forall e a b.
382− ( FromListsN e a b,
384+ ( FLN a b,
383385 CountableN b,
384386 Enum a,
385387 Enum b,
386388 Bounded a,
387389 Bounded b,
388390 Eq a,
389− CountableDimensionsN a b
391+ CountableDimsN a b
390392 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
391393matrixBuilder f =
392394 let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
@@ -397,11 +399,11 @@ matrixBuilder f =
397399 buildList r l = take r l : buildList r (drop r l)
398400
399401-- | Constructs a column vector matrix
400−col :: (FromLists e () rows) => [e] -> Matrix e () rows
402+col :: (FL () rows) => [e] -> Matrix e () rows
401403col = fromLists . map (: [])
402404
403405-- | Constructs a row vector matrix
404−row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
406+row :: (FL cols ()) => [e] -> Matrix e cols ()
405407row = fromLists . (: [])
406408
407409-- | Lifts functions to matrices with arbitrary dimensions.
@@ -411,8 +413,8 @@ row = fromLists . (: [])
411413fromF' ::
412414 forall a b cols rows e.
413415 ( Liftable e a b,
414− CountableDimensions cols rows,
415− FromLists e rows cols
416+ CountableDims cols rows,
417+ FL rows cols
416418 ) =>
417419 (a -> b) ->
418420 Matrix e cols rows
@@ -440,8 +442,8 @@ fromF' f =
440442fromF ::
441443 forall a b e.
442444 ( Liftable e a b,
443− CountableDimensionsN a b,
444− FromListsN e b a
445+ CountableDimsN a b,
446+ FLN b a
445447 ) =>
446448 (a -> b) ->
447449 Matrix e (Normalize a) (Normalize b)
@@ -480,7 +482,7 @@ toList = concat . toLists
480482-- Zeros Matrix
481483
482484-- | The zero matrix. A matrix wholly filled with zeros.
483−zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
485+zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
484486zeros = matrixBuilder' (const 0)
485487
486488-- Ones Matrix
@@ -488,20 +490,20 @@ zeros = matrixBuilder' (const 0)
488490-- | The ones matrix. A matrix wholly filled with ones.
489491--
490492-- Also known as T (Top) matrix.
491−ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
493+ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
492494ones = matrixBuilder' (const 1)
493495
494496-- Const Matrix
495497
496498-- | The constant matrix constructor. A matrix wholly filled with a given
497499-- value.
498−constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
500+constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
499501constant e = matrixBuilder' (const e)
500502
501503-- Bang Matrix
502504
503505-- | The T (Top) row vector matrix.
504−bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
506+bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
505507bang =
506508 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
507509 in fromLists [take c [1, 1 ..]]
@@ -509,7 +511,7 @@ bang =
509511-- iden Matrix
510512
511513-- | iden matrix.
512−iden :: forall e cols . (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
514+iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
513515iden = matrixBuilder' (bool 0 1 . uncurry (==))
514516{-# NOINLINE iden #-}
515517
@@ -554,21 +556,21 @@ infixl 7 ./
554556-- Projections
555557
556558-- | Biproduct first component projection
557−p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
559+p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
558560p1 = join id zeros
559561
560562-- | Biproduct second component projection
561−p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
563+p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
562564p2 = join zeros id
563565
564566-- Injections
565567
566568-- | Biproduct first component injection
567−i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
569+i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
568570i1 = tr p1
569571
570572-- | Biproduct second component injection
571−i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
573+i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
572574i2 = tr p2
573575
574576-- Dimensions
@@ -601,11 +603,11 @@ infixl 5 -|-
601603(-|-) ::
602604 forall e n k m j.
603605 ( Num e,
604− CountableDimensions j k,
605− FromLists e k k,
606− FromLists e j k,
607− FromLists e k j,
608− FromLists e j j
606+ CountableDims j k,
607+ FL k k,
608+ FL j k,
609+ FL k j,
610+ FL j j
609611 ) =>
610612 Matrix e n k ->
611613 Matrix e m j ->
@@ -618,8 +620,8 @@ infixl 5 -|-
618620fstM ::
619621 forall e m k .
620622 ( Num e,
621− CountableDimensions k m,
622− FromLists e (Normalize (m, k)) m,
623+ CountableDims k m,
624+ FL (Normalize (m, k)) m,
623625 CountableN (m, k)
624626 ) => Matrix e (Normalize (m, k)) m
625627fstM = matrixBuilder' f
@@ -633,8 +635,8 @@ fstM = matrixBuilder' f
633635sndM ::
634636 forall e m k .
635637 ( Num e,
636− CountableDimensions k m,
637− FromLists e (Normalize (m, k)) k,
638+ CountableDims k m,
639+ FL (Normalize (m, k)) k,
638640 CountableN (m, k)
639641 ) => Matrix e (Normalize (m, k)) k
640642sndM = matrixBuilder' f
@@ -658,10 +660,10 @@ sndM = matrixBuilder' f
658660kr ::
659661 forall e cols a b.
660662 ( Num e,
661− CountableDimensions a b,
663+ CountableDims a b,
662664 CountableN (a, b),
663− FromLists e (Normalize (a, b)) a,
664− FromLists e (Normalize (a, b)) b
665+ FL (Normalize (a, b)) a,
666+ FL (Normalize (a, b)) b
665667 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
666668kr a b =
667669 let fstM' = fstM @e @a @b
@@ -676,13 +678,13 @@ infixl 4 ><
676678(><) ::
677679 forall e m p n q.
678680 ( Num e,
679− CountableDimensions m n,
680− CountableDimensions p q,
681− CountableDimensionsN (m, n) (p, q),
682− FromLists e (Normalize (m, n)) m,
683− FromLists e (Normalize (m, n)) n,
684− FromLists e (Normalize (p, q)) p,
685− FromLists e (Normalize (p, q)) q
681+ CountableDims m n,
682+ CountableDims p q,
683+ CountableDimsN (m, n) (p, q),
684+ FL (Normalize (m, n)) m,
685+ FL (Normalize (m, n)) n,
686+ FL (Normalize (p, q)) p,
687+ FL (Normalize (p, q)) q
686688 )
687689 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
688690(><) a b =
@@ -733,32 +735,32 @@ tr (Fork a b) = Join (tr a) (tr b)
733735
734736-- | Selective functors 'select' operator equivalent inspired by the
735737-- ArrowMonad solution presented in the paper.
736−select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
738+select :: (Num e, FL b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
737739select (Fork a b) y = y . a + b -- Divide-and-conquer law
738740select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
739741select m y = join y id . m
740742
741743branch ::
742744 ( Num e,
743− CountableDimensions a b,
744− CountableDimensions c (Either b c),
745− FromLists e c b,
746− FromLists e a b,
747− FromLists e a a,
748− FromLists e b b,
749− FromLists e c c,
750− FromLists e b a,
751− FromLists e b c,
752− FromLists e (Either b c) b,
753− FromLists e (Either b c) c
745+ CountableDims a b,
746+ CountableDims c (Either b c),
747+ FL c b,
748+ FL a b,
749+ FL a a,
750+ FL b b,
751+ FL c c,
752+ FL b a,
753+ FL b c,
754+ FL (Either b c) b,
755+ FL (Either b c) c
754756 )
755757 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
756758branch x l r = f x `select` g l `select` r
757759 where
758− f :: (Num e, Countable a, CountableDimensions b c, FromLists e a b, FromLists e c b, FromLists e b b, FromLists e b a, FromLists e a a)
760+ f :: (Num e, Countable a, CountableDims b c, FL a b, FL c b, FL b b, FL b a, FL a a)
759761 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
760762 f m = fork (tr i1) (i1 . tr i2) . m
761− g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
763+ g :: (Num e, CountableDims b c, FL b c, FL c c) => Matrix e a c -> Matrix e a (Either b c)
762764 g m = i2 . m
763765
764766-- McCarthy's Conditional
@@ -767,9 +769,9 @@ branch x l r = f x `select` g l `select` r
767769cond ::
768770 ( Trivial cols,
769771 Countable cols,
770− FromLists e () cols,
771− FromLists e cols (),
772− FromLists e cols cols,
772+ FL () cols,
773+ FL cols (),
774+ FL cols cols,
773775 Bounded a,
774776 Enum a,
775777 Num e,
@@ -782,9 +784,9 @@ cond p f g = join f g . grd p
782784grd ::
783785 ( Trivial q,
784786 Countable q,
785− FromLists e () q,
786− FromLists e q (),
787− FromLists e q q,
787+ FL () q,
788+ FL q (),
789+ FL q q,
788790 Bounded a,
789791 Enum a,
790792 Num e,
@@ -798,9 +800,9 @@ corr ::
798800 forall e a q .
799801 ( Trivial q,
800802 Countable q,
801− FromLists e () q,
802− FromLists e q (),
803− FromLists e q q,
803+ FL () q,
804+ FL q (),
805+ FL q q,
804806 Liftable e a Bool
805807 )
806808 => (a -> Bool) -> Matrix e q q
@@ -829,7 +831,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
829831 fill str = replicate (widest - length str - 2) ' ' ++ str
830832
831833-- | Matrix pretty printer
832−pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
834+pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
833835pretty m = concat
834836 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
835837 , unlines
@@ -858,7 +860,7 @@ pretty m = concat
858860 (safeGet i j m)
859861
860862-- | Matrix pretty printer
861−prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
863+prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
862864prettyPrint = putStrLn . pretty
863865
864866-- | Zip two matrices with a given binary function
@@ -953,8 +955,8 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
953955fromFRel' ::
954956 forall a b cols rows.
955957 ( Liftable Boolean a b,
956− CountableDimensions cols rows,
957− FromLists Boolean rows cols
958+ CountableDims cols rows,
959+ FL rows cols
958960 ) =>
959961 (a -> b) ->
960962 Relation cols rows
@@ -982,8 +984,8 @@ fromFRel' f =
982984fromFRel ::
983985 forall a b.
984986 ( Liftable Boolean a b,
985− CountableDimensionsN a b,
986− FromLists Boolean (Normalize b) (Normalize a)
987+ CountableDimsN a b,
988+ FL (Normalize b) (Normalize a)
987989 ) =>
988990 (a -> b) ->
989991 Relation (Normalize a) (Normalize b)
@@ -1014,8 +1016,8 @@ toRel ::
10141016 Enum a,
10151017 Enum b,
10161018 Eq b,
1017− CountableDimensionsN a b,
1018− FromListsN Boolean b a
1019+ CountableDimsN a b,
1020+ FLN b a
10191021 )
10201022 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
10211023toRel f =
src/LAoP/Matrix/Nat.hs+63−63
@@ -56,12 +56,12 @@ module LAoP.Matrix.Nat
5656
5757 -- * Constraint type synonyms
5858 Countable,
59− CountableDimensions,
59+ CountableDims,
6060 CountableN,
6161 CountableNz,
62− CountableDimensionsN,
63− FromListsN,
64− FromListsNz,
62+ CountableDimsN,
63+ FLN,
64+ FLNz,
6565 Liftable,
6666 TrivialE,
6767 TrivialP,
@@ -78,7 +78,7 @@ module LAoP.Matrix.Nat
7878 I.Normalize,
7979
8080 -- * Matrix construction and conversion
81− I.FromLists,
81+ I.FL,
8282 fromLists,
8383 toLists,
8484 toList,
@@ -167,16 +167,16 @@ newtype Matrix e (cols :: Nat) (rows :: Nat) = M (I.Matrix e (I.FromNat cols) (I
167167 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.FromNat cols) (I.FromNat rows))
168168
169169-- | Constraint type synonyms to keep the type signatures less convoluted
170−type Countable a = KnownNat (I.Count a)
171−type CountableDimensions a b = (Countable a, Countable b)
172−type CountableN a = KnownNat (I.Count (I.FromNat a))
173−type CountableNz a = KnownNat (I.Count (I.Normalize a))
174−type CountableDimensionsN a b = (CountableN a, CountableN b)
175−type FromListsN e a b = I.FromLists e (I.FromNat a) (I.FromNat b)
176−type FromListsNz e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
177−type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
178−type TrivialE a b = I.FromNat (a + b) ~ Either (I.FromNat a) (I.FromNat b)
179−type TrivialP a b = I.FromNat (a * b) ~ I.FromNat (I.Count (I.FromNat a) * I.Count (I.FromNat b))
170+type Countable a = KnownNat (I.Count a)
171+type CountableDims a b = (Countable a, Countable b)
172+type CountableN a = KnownNat (I.Count (I.FromNat a))
173+type CountableNz a = KnownNat (I.Count (I.Normalize a))
174+type CountableDimsN a b = (CountableN a, CountableN b)
175+type FLN e a b = I.FL (I.FromNat a) (I.FromNat b)
176+type FLNz e a b = I.FL (I.Normalize a) (I.Normalize b)
177+type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
178+type TrivialE a b = I.FromNat (a + b) ~ Either (I.FromNat a) (I.FromNat b)
179+type TrivialP a b = I.FromNat (a * b) ~ I.FromNat (I.Count (I.FromNat a) * I.Count (I.FromNat b))
180180
181181-- Primitives
182182
@@ -218,27 +218,27 @@ infixl 2 ===
218218
219219-- Construction
220220
221−fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
221+fromLists :: (FLN e cols rows) => [[e]] -> Matrix e cols rows
222222fromLists = M . I.fromLists
223223
224224-- | Matrix builder function. Constructs a matrix provided with
225225-- a construction function that operates with indices.
226226matrixBuilder' ::
227− (FromListsN e cols rows, CountableN cols, CountableN rows)
227+ (FLN e cols rows, CountableN cols, CountableN rows)
228228 => ((Int, Int) -> e) -> Matrix e cols rows
229229matrixBuilder' = M . I.matrixBuilder'
230230
231−col :: (I.FromLists e () (I.FromNat rows)) => [e] -> Matrix e 1 rows
231+col :: (I.FL () (I.FromNat rows)) => [e] -> Matrix e 1 rows
232232col = M . I.col
233233
234−row :: (I.FromLists e (I.FromNat cols) ()) => [e] -> Matrix e cols 1
234+row :: (I.FL (I.FromNat cols) ()) => [e] -> Matrix e cols 1
235235row = M . I.row
236236
237237fromF' ::
238238 ( Liftable e a b,
239239 CountableN cols,
240240 CountableN rows,
241− FromListsN e rows cols
241+ FLN e rows cols
242242 ) =>
243243 (a -> b) ->
244244 Matrix e cols rows
@@ -248,7 +248,7 @@ fromF ::
248248 ( Liftable e a b,
249249 CountableNz a,
250250 CountableNz b,
251− FromListsNz e b a
251+ FLNz e b a
252252 ) =>
253253 (a -> b) ->
254254 Matrix e (I.Count a) (I.Count b)
@@ -265,21 +265,21 @@ toList (M m) = I.toList m
265265-- Zeros Matrix
266266
267267zeros ::
268− (Num e, FromListsN e cols rows, CountableN cols, CountableN rows) =>
268+ (Num e, FLN e cols rows, CountableN cols, CountableN rows) =>
269269 Matrix e cols rows
270270zeros = M I.zeros
271271
272272-- Ones Matrix
273273
274274ones ::
275− (Num e, FromListsN e cols rows, CountableN cols, CountableN rows) =>
275+ (Num e, FLN e cols rows, CountableN cols, CountableN rows) =>
276276 Matrix e cols rows
277277ones = M I.ones
278278
279279-- Const Matrix
280280
281281constant ::
282− (Num e, FromListsN e cols rows, CountableN cols, CountableN rows) =>
282+ (Num e, FLN e cols rows, CountableN cols, CountableN rows) =>
283283 e ->
284284 Matrix e cols rows
285285constant = M . I.constant
@@ -288,14 +288,14 @@ constant = M . I.constant
288288
289289bang ::
290290 forall e cols.
291− (Num e, Enum e, I.FromLists e (I.FromNat cols) (), CountableN cols) =>
291+ (Num e, Enum e, I.FL (I.FromNat cols) (), CountableN cols) =>
292292 Matrix e cols 1
293293bang = M I.bang
294294
295295-- iden Matrix
296296
297297iden ::
298− (Num e, FromListsN e cols cols, CountableN cols) =>
298+ (Num e, FLN e cols cols, CountableN cols) =>
299299 Matrix e cols cols
300300iden = M I.iden
301301
@@ -320,9 +320,9 @@ infixl 7 ./
320320
321321p1 ::
322322 ( Num e,
323− CountableDimensionsN n m,
324− FromListsN e n m,
325− FromListsN e m m,
323+ CountableDimsN n m,
324+ FLN e n m,
325+ FLN e m m,
326326 TrivialE m n
327327 ) =>
328328 Matrix e (m + n) m
@@ -330,9 +330,9 @@ p1 = M I.p1
330330
331331p2 ::
332332 ( Num e,
333− CountableDimensionsN n m,
334− FromListsN e m n,
335− FromListsN e n n,
333+ CountableDimsN n m,
334+ FLN e m n,
335+ FLN e n n,
336336 TrivialE m n
337337 ) =>
338338 Matrix e (m + n) n
@@ -342,9 +342,9 @@ p2 = M I.p2
342342
343343i1 ::
344344 ( Num e,
345− CountableDimensionsN n rows,
346− FromListsN e n rows,
347− FromListsN e rows rows,
345+ CountableDimsN n rows,
346+ FLN e n rows,
347+ FLN e rows rows,
348348 TrivialE rows n
349349 ) =>
350350 Matrix e rows (rows + n)
@@ -352,9 +352,9 @@ i1 = tr p1
352352
353353i2 ::
354354 ( Num e,
355− CountableDimensionsN rows m,
356− FromListsN e m rows,
357− FromListsN e rows rows,
355+ CountableDimsN rows m,
356+ FLN e m rows,
357+ FLN e rows rows,
358358 TrivialE m rows
359359 ) =>
360360 Matrix e rows (m + rows)
@@ -373,11 +373,11 @@ infixl 5 -|-
373373-- | Coproduct Bifunctor (Direct sum)
374374(-|-) ::
375375 ( Num e,
376− CountableDimensionsN j k,
377− FromListsN e k k,
378− FromListsN e j k,
379− FromListsN e k j,
380− FromListsN e j j,
376+ CountableDimsN j k,
377+ FLN e k k,
378+ FLN e j k,
379+ FLN e k j,
380+ FLN e j j,
381381 TrivialE n m,
382382 TrivialE k j
383383 ) =>
@@ -390,9 +390,9 @@ infixl 5 -|-
390390fstM ::
391391 forall e m k .
392392 ( Num e,
393− CountableDimensionsN m k,
393+ CountableDimsN m k,
394394 CountableN (m * k),
395− FromListsN e (m * k) m,
395+ FLN e (m * k) m,
396396 TrivialP m k
397397 ) => Matrix e (m * k) m
398398fstM = M (I.fstM @e @(I.FromNat m) @(I.FromNat k))
@@ -401,8 +401,8 @@ fstM = M (I.fstM @e @(I.FromNat m) @(I.FromNat k))
401401sndM ::
402402 forall e m k.
403403 ( Num e,
404− CountableDimensionsN k m,
405− FromListsN e (m * k) k,
404+ CountableDimsN k m,
405+ FLN e (m * k) k,
406406 CountableN (m * k),
407407 TrivialP m k
408408 ) => Matrix e (m * k) k
@@ -412,10 +412,10 @@ sndM = M (I.sndM @e @(I.FromNat m) @(I.FromNat k))
412412kr ::
413413 forall e cols a b.
414414 ( Num e,
415− CountableDimensionsN a b,
415+ CountableDimsN a b,
416416 CountableN (a * b),
417− FromListsN e (a * b) a,
418− FromListsN e (a * b) b,
417+ FLN e (a * b) a,
418+ FLN e (a * b) b,
419419 TrivialP a b
420420 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a * b)
421421kr a b =
@@ -429,13 +429,13 @@ infixl 4 ><
429429(><) ::
430430 forall e m p n q.
431431 ( Num e,
432− CountableDimensionsN m n,
433− CountableDimensionsN p q,
434− CountableDimensionsN (m * n) (p * q),
435− FromListsN e (m * n) m,
436− FromListsN e (m * n) n,
437− FromListsN e (p * q) p,
438− FromListsN e (p * q) q,
432+ CountableDimsN m n,
433+ CountableDimsN p q,
434+ CountableDimsN (m * n) (p * q),
435+ FLN e (m * n) m,
436+ FLN e (m * n) n,
437+ FLN e (p * q) p,
438+ FLN e (p * q) q,
439439 TrivialP m n,
440440 TrivialP p q
441441 ) => Matrix e m p -> Matrix e n q -> Matrix e (m * n) (p * q)
@@ -459,7 +459,7 @@ tr (M m) = M (I.tr m)
459459-- Selective 'select' operator
460460select ::
461461 ( Num e,
462− FromListsN e rows1 rows1,
462+ FLN e rows1 rows1,
463463 CountableN rows1,
464464 I.FromNat rows2 ~ I.FromNat rows1,
465465 I.FromNat cols1 ~ I.FromNat cols2,
@@ -472,9 +472,9 @@ select (M m) (M y) = M (I.select m y)
472472cond ::
473473 ( I.FromNat (I.Count (I.FromNat cols)) ~ I.FromNat cols,
474474 CountableN cols,
475− I.FromLists e () (I.FromNat cols),
476− I.FromLists e (I.FromNat cols) (),
477− FromListsN e cols cols,
475+ I.FL () (I.FromNat cols),
476+ I.FL (I.FromNat cols) (),
477+ FLN e cols cols,
478478 Liftable e a Bool
479479 )
480480 =>
@@ -483,10 +483,10 @@ cond p (M a) (M b) = M (I.cond p a b)
483483
484484-- Pretty print
485485
486−pretty :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> String
486+pretty :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> String
487487pretty (M m) = I.pretty m
488488
489−prettyPrint :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> IO ()
489+prettyPrint :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> IO ()
490490prettyPrint (M m) = I.prettyPrint m
491491
492492-- | Zip two matrices with a given binary function
src/LAoP/Matrix/Type.hs+80−81
@@ -6,7 +6,6 @@
66{-# LANGUAGE FlexibleContexts #-}
77{-# LANGUAGE FlexibleInstances #-}
88{-# LANGUAGE GADTs #-}
9−{-# LANGUAGE KindSignatures #-}
109{-# LANGUAGE MultiParamTypeClasses #-}
1110{-# LANGUAGE NoStarIsType #-}
1211{-# LANGUAGE RankNTypes #-}
@@ -59,10 +58,10 @@ module LAoP.Matrix.Type
5958
6059 -- * Constraint type aliases
6160 Countable,
62− CountableDimensions,
61+ CountableDims,
6362 CountableN,
64− CountableDimensionsN,
65− FromListsN,
63+ CountableDimsN,
64+ FLN,
6665 Liftable,
6766 Trivial,
6867 TrivialP,
@@ -183,7 +182,7 @@ where
183182import Data.Void
184183import Data.Proxy
185184import Data.Kind
186−import GHC.TypeLits
185+import GHC.TypeLits
187186import Control.DeepSeq
188187import LAoP.Utils
189188import qualified LAoP.Matrix.Internal as I
@@ -193,21 +192,21 @@ newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols
193192 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.Normalize cols) (I.Normalize rows))
194193
195194-- | Constraint type synonyms to keep the type signatures less convoluted
196−type Countable a = KnownNat (I.Count a)
197−type CountableDimensions a b = (Countable a, Countable b)
198−type CountableN a = KnownNat (I.Count (I.Normalize a))
199−type CountableDimensionsN a b = (CountableN a, CountableN b)
200−type FromListsN e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
201−type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
202−type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
203−type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
204−type Trivial3 a = I.FromNat (I.Count (I.Normalize (I.Normalize a))) ~ I.Normalize (I.Normalize a)
205−type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
195+type Countable a = KnownNat (I.Count a)
196+type CountableDims a b = (Countable a, Countable b)
197+type CountableN a = KnownNat (I.Count (I.Normalize a))
198+type CountableDimsN a b = (CountableN a, CountableN b)
199+type FLN a b = I.FromLists (I.Normalize a) (I.Normalize b)
200+type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
201+type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
202+type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
203+type Trivial3 a = I.FromNat (I.Count (I.Normalize (I.Normalize a))) ~ I.Normalize (I.Normalize a)
204+type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
206205
207206-- | It is possible to implement a constrained version of the category type
208207-- class.
209208instance (Num e) => Category (Matrix e) where
210− type Object (Matrix e) a = (FromListsN e a a, CountableN a)
209+ type Object (Matrix e) a = (FLN a a, CountableN a)
211210 id = iden
212211 (.) = comp
213212
@@ -215,10 +214,10 @@ instance (Num e) => Category (Matrix e) where
215214bimapM ::
216215 ( Liftable e a b,
217216 Liftable e c d,
218− CountableDimensionsN a c,
219− CountableDimensionsN b d,
220− FromListsN e d c,
221− FromListsN e b a
217+ CountableDimsN a c,
218+ CountableDimsN b d,
219+ FLN d c,
220+ FLN b a
222221 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
223222bimapM f g m = fromF g . m . tr (fromF f)
224223
@@ -273,8 +272,8 @@ infixl 2 ===
273272-- | Functor instance equivalent function
274273fmapM ::
275274 ( Liftable e a b,
276− CountableDimensionsN a b,
277− FromListsN e b a
275+ CountableDimsN a b,
276+ FLN b a
278277 )
279278 =>
280279 (a -> b) -> Matrix e c a -> Matrix e c b
@@ -286,11 +285,11 @@ unitM = one 1
286285
287286-- | Applicative instance equivalent 'unit' function,
288287multM ::
289− ( CountableDimensionsN a b,
288+ ( CountableDimsN a b,
290289 CountableN (a, b),
291290 Num e,
292− FromListsN e (a, b) a,
293− FromListsN e (a, b) b,
291+ FLN (a, b) a,
292+ FLN (a, b) b,
294293 TrivialP a b
295294 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
296295multM = kr
@@ -301,7 +300,7 @@ returnM ::
301300 ( Num e,
302301 Enum e,
303302 Enum a,
304− FromListsN e () a,
303+ FLN () a,
305304 Countable a
306305 ) => a -> Matrix e One a
307306returnM a = col l
@@ -318,35 +317,35 @@ bindM = flip comp
318317
319318-- | Build a matrix out of a list of list of elements. Throws a runtime
320319-- error if the dimensions do not match.
321−fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
320+fromLists :: (FLN cols rows) => [[e]] -> Matrix e cols rows
322321fromLists = M . I.fromLists
323322
324323-- | Matrix builder function. Constructs a matrix provided with
325324-- a construction function that operates with indices.
326325matrixBuilder' ::
327− (FromListsN e cols rows, CountableDimensionsN cols rows )
326+ (FLN cols rows, CountableDimsN cols rows )
328327 => ((Int, Int) -> e) -> Matrix e cols rows
329328matrixBuilder' = M . I.matrixBuilder'
330329
331330-- | Matrix builder function. Constructs a matrix provided with
332331-- a construction function that operates with arbitrary types.
333332matrixBuilder ::
334− ( FromListsN e a b,
333+ ( FLN a b,
335334 Enum a,
336335 Enum b,
337336 Bounded a,
338337 Bounded b,
339338 Eq a,
340− CountableDimensionsN a b
339+ CountableDimsN a b
341340 ) => ((a, b) -> e) -> Matrix e a b
342341matrixBuilder f = M (I.matrixBuilder f)
343342
344343-- | Constructs a column vector matrix
345−col :: (FromListsN e () rows) => [e] -> Matrix e One rows
344+col :: (FLN () rows) => [e] -> Matrix e One rows
346345col = M . I.col
347346
348347-- | Constructs a row vector matrix
349−row :: (FromListsN e cols ()) => [e] -> Matrix e cols One
348+row :: (FLN cols ()) => [e] -> Matrix e cols One
350349row = M . I.row
351350
352351-- | Lifts functions to matrices with arbitrary dimensions.
@@ -355,8 +354,8 @@ row = M . I.row
355354-- types @a@ or @b@ allows.
356355fromF' ::
357356 ( Liftable e a b,
358− CountableDimensionsN cols rows,
359− FromListsN e rows cols
357+ CountableDimsN cols rows,
358+ FLN rows cols
360359 ) =>
361360 (a -> b) -> Matrix e cols rows
362361fromF' = M . I.fromF'
@@ -365,8 +364,8 @@ fromF' = M . I.fromF'
365364-- cardinality's.
366365fromF ::
367366 ( Liftable e a b,
368− CountableDimensionsN a b,
369− FromListsN e b a
367+ CountableDimsN a b,
368+ FLN b a
370369 ) =>
371370 (a -> b) -> Matrix e a b
372371fromF = M . I.fromF
@@ -374,8 +373,8 @@ fromF = M . I.fromF
374373-- | Lifts relation functions to Boolean Matrix
375374toRel ::
376375 ( Liftable (Natural 0 1) a b,
377− CountableDimensionsN a b,
378− FromListsN (Natural 0 1) b a
376+ CountableDimsN a b,
377+ FLN b a
379378 ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
380379toRel = M . I.toRel
381380
@@ -393,7 +392,7 @@ toList (M m) = I.toList m
393392
394393-- | The zero matrix. A matrix wholly filled with zeros.
395394zeros ::
396− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
395+ (Num e, FLN cols rows, CountableDimsN cols rows)
397396 => Matrix e cols rows
398397zeros = M I.zeros
399398
@@ -403,7 +402,7 @@ zeros = M I.zeros
403402--
404403-- Also known as T (Top) matrix.
405404ones ::
406− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
405+ (Num e, FLN cols rows, CountableDimsN cols rows)
407406 => Matrix e cols rows
408407ones = M I.ones
409408
@@ -412,7 +411,7 @@ ones = M I.ones
412411-- | The constant matrix constructor. A matrix wholly filled with a given
413412-- value.
414413constant ::
415− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
414+ (Num e, FLN cols rows, CountableDimsN cols rows)
416415 => e -> Matrix e cols rows
417416constant = M . I.constant
418417
@@ -421,7 +420,7 @@ constant = M . I.constant
421420-- | The T (Top) row vector matrix.
422421bang ::
423422 forall e cols.
424− (Num e, Enum e, FromListsN e cols (), CountableN cols) =>
423+ (Num e, Enum e, FLN cols (), CountableN cols) =>
425424 Matrix e cols One
426425bang = M I.bang
427426
@@ -433,7 +432,7 @@ point ::
433432 Num e,
434433 Ord e,
435434 CountableN a,
436− FromListsN e a One
435+ FLN a One
437436 ) => a -> Matrix e One a
438437point = fromF . const
439438
@@ -441,7 +440,7 @@ point = fromF . const
441440
442441-- | iden matrix
443442iden ::
444− (Num e, FromListsN e a a, CountableN a) =>
443+ (Num e, FLN a a, CountableN a) =>
445444 Matrix e a a
446445iden = M I.iden
447446{-# NOINLINE iden #-}
@@ -477,9 +476,9 @@ infixl 7 ./
477476-- | Biproduct first component projection
478477p1 ::
479478 ( Num e,
480− CountableDimensionsN n m,
481− FromListsN e n m,
482− FromListsN e m m
479+ CountableDimsN n m,
480+ FLN n m,
481+ FLN m m
483482 ) =>
484483 Matrix e (Either m n) m
485484p1 = M I.p1
@@ -487,9 +486,9 @@ p1 = M I.p1
487486-- | Biproduct second component projection
488487p2 ::
489488 ( Num e,
490− CountableDimensionsN n m,
491− FromListsN e m n,
492− FromListsN e n n
489+ CountableDimsN n m,
490+ FLN m n,
491+ FLN n n
493492 ) =>
494493 Matrix e (Either m n) n
495494p2 = M I.p2
@@ -499,9 +498,9 @@ p2 = M I.p2
499498-- | Biproduct first component injection
500499i1 ::
501500 ( Num e,
502− CountableDimensionsN n m,
503− FromListsN e n m,
504− FromListsN e m m
501+ CountableDimsN n m,
502+ FLN n m,
503+ FLN m m
505504 ) =>
506505 Matrix e m (Either m n)
507506i1 = tr p1
@@ -509,9 +508,9 @@ i1 = tr p1
509508-- | Biproduct second component injection
510509i2 ::
511510 ( Num e,
512− CountableDimensionsN n m,
513− FromListsN e m n,
514− FromListsN e n n
511+ CountableDimsN n m,
512+ FLN m n,
513+ FLN n n
515514 ) =>
516515 Matrix e n (Either m n)
517516i2 = tr p2
@@ -545,11 +544,11 @@ infixl 5 -|-
545544-- | Matrix coproduct functor also known as matrix direct sum.
546545(-|-) ::
547546 ( Num e,
548− CountableDimensionsN j k,
549− FromListsN e k k,
550− FromListsN e j k,
551− FromListsN e k j,
552− FromListsN e j j
547+ CountableDimsN j k,
548+ FLN k k,
549+ FLN j k,
550+ FLN k j,
551+ FLN j j
553552 ) =>
554553 Matrix e n k ->
555554 Matrix e m j ->
@@ -562,9 +561,9 @@ infixl 5 -|-
562561fstM ::
563562 forall e m k .
564563 ( Num e,
565− CountableDimensionsN m k,
564+ CountableDimsN m k,
566565 CountableN (m, k),
567− FromListsN e (m, k) m,
566+ FLN (m, k) m,
568567 TrivialP m k
569568 ) => Matrix e (m, k) m
570569fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
@@ -573,9 +572,9 @@ fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
573572sndM ::
574573 forall e m k.
575574 ( Num e,
576− CountableDimensionsN k m,
575+ CountableDimsN k m,
577576 CountableN (m, k),
578− FromListsN e (m, k) k,
577+ FLN (m, k) k,
579578 TrivialP m k
580579 ) => Matrix e (m, k) k
581580sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
@@ -594,10 +593,10 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
594593kr ::
595594 forall e cols a b.
596595 ( Num e,
597− CountableDimensionsN a b,
596+ CountableDimsN a b,
598597 CountableN (a, b),
599− FromListsN e (a, b) a,
600− FromListsN e (a, b) b,
598+ FLN (a, b) a,
599+ FLN (a, b) b,
601600 TrivialP a b
602601 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
603602kr a b =
@@ -613,13 +612,13 @@ infixl 4 ><
613612(><) ::
614613 forall e m p n q.
615614 ( Num e,
616− CountableDimensionsN m n,
617− CountableDimensionsN p q,
618− CountableDimensionsN (m, n) (p, q),
619− FromListsN e (m, n) m,
620− FromListsN e (m, n) n,
621− FromListsN e (p, q) p,
622− FromListsN e (p, q) q,
615+ CountableDimsN m n,
616+ CountableDimsN p q,
617+ CountableDimsN (m, n) (p, q),
618+ FLN (m, n) m,
619+ FLN (m, n) n,
620+ FLN (p, q) p,
621+ FLN (p, q) q,
623622 TrivialP m n,
624623 TrivialP p q
625624 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
@@ -664,7 +663,7 @@ tr (M m) = M (I.tr m)
664663-- ArrowMonad solution presented in the paper.
665664selectM ::
666665 ( Num e,
667− FromListsN e b b,
666+ FLN b b,
668667 CountableN b
669668 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670669selectM (M m) (M y) = M (I.select m y)
@@ -677,9 +676,9 @@ cond ::
677676 Trivial2 a,
678677 Trivial3 a,
679678 CountableN a,
680− FromListsN e () a,
681− FromListsN e a (),
682− FromListsN e a a,
679+ FLN () a,
680+ FLN a (),
681+ FLN a a,
683682 Liftable e a Bool
684683 )
685684 =>
@@ -689,11 +688,11 @@ cond p (M a) (M b) = M (I.cond p a b)
689688-- Pretty print
690689
691690-- | Matrix pretty printer
692−pretty :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> String
691+pretty :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> String
693692pretty (M m) = I.pretty m
694693
695694-- | Matrix pretty printer
696−prettyPrint :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> IO ()
695+prettyPrint :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> IO ()
697696prettyPrint (M m) = I.prettyPrint m
698697
699698-- | Zip two matrices with a given binary function
src/LAoP/Relation.hs+3−3
@@ -32,10 +32,10 @@ module LAoP.Relation
3232
3333 -- * Constraint type synonyms
3434 Countable,
35− CountableDimensions,
35+ CountableDims,
3636 CountableN,
37− CountableDimensionsN,
38− FromListsN,
37+ CountableDimsN,
38+ FLN,
3939 Liftable,
4040 Trivial,
4141 TrivialP,
src/LAoP/Relation/Internal.hs+118−118
@@ -43,10 +43,10 @@ module LAoP.Relation.Internal
4343
4444 -- * Constraint type synonyms
4545 Countable,
46− CountableDimensions,
46+ CountableDims,
4747 CountableN,
48− CountableDimensionsN,
49− FromListsN,
48+ CountableDimsN,
49+ FLN,
5050 Liftable,
5151 Trivial,
5252 TrivialP,
@@ -194,19 +194,19 @@ newtype Relation a b = R (I.Matrix Boolean (I.Normalize a) (I.Normalize b))
194194deriving instance (Read (I.Matrix Boolean (I.Normalize a) (I.Normalize b))) => Read (Relation a b)
195195
196196-- | Constraint type synonyms to keep the type signatures less convoluted
197−type Countable a = KnownNat (I.Count a)
198−type CountableDimensions a b = (Countable a, Countable b)
199−type CountableN a = KnownNat (I.Count (I.Normalize a))
200−type CountableDimensionsN a b = (CountableN a, CountableN b)
201−type FromListsN a b = I.FromLists Boolean (I.Normalize a) (I.Normalize b)
202−type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Boolean, Ord Boolean)
203−type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
204−type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
197+type Countable a = KnownNat (I.Count a)
198+type CountableDims a b = (Countable a, Countable b)
199+type CountableN a = KnownNat (I.Count (I.Normalize a))
200+type CountableDimsN a b = (CountableN a, CountableN b)
201+type FLN a b = I.FromLists (I.Normalize a) (I.Normalize b)
202+type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Boolean, Ord Boolean)
203+type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
204+type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
205205
206206-- | It is possible to implement a constrained version of the category type
207207-- class.
208208instance Category Relation where
209− type Object Relation a = (FromListsN a a, CountableN a)
209+ type Object Relation a = (FLN a a, CountableN a)
210210 id = iden
211211 (.) = comp
212212
@@ -269,26 +269,26 @@ infixl 2 ===
269269
270270-- | Build a matrix out of a list of list of elements. Throws a runtime
271271-- error if the dimensions do not match.
272−fromLists :: (FromListsN a b) => [[Boolean]] -> Relation a b
272+fromLists :: (FLN a b) => [[Boolean]] -> Relation a b
273273fromLists = R . I.fromLists
274274
275275-- | Relation builder function. Constructs a relation provided with
276276-- a construction function that operates with indices.
277277relationBuilder' ::
278− (FromListsN a b, CountableDimensionsN a b) =>
278+ (FLN a b, CountableDimsN a b) =>
279279 ((Int, Int) -> Boolean) -> Relation a b
280280relationBuilder' = R . I.matrixBuilder'
281281
282282-- | Relation builder function. Constructs a relation provided with
283283-- a construction function that operates with arbitrary types.
284284relationBuilder ::
285− ( FromListsN a b,
285+ ( FLN a b,
286286 Enum a,
287287 Enum b,
288288 Bounded a,
289289 Bounded b,
290290 Eq a,
291− CountableDimensionsN a b
291+ CountableDimsN a b
292292 ) => ((a, b) -> Boolean) -> Relation a b
293293relationBuilder = R . I.matrixBuilder
294294
@@ -298,8 +298,8 @@ relationBuilder = R . I.matrixBuilder
298298-- types @a@ or @b@ allows.
299299fromF' ::
300300 ( Liftable a b,
301− CountableDimensionsN c d,
302− FromListsN d c
301+ CountableDimsN c d,
302+ FLN d c
303303 )
304304 => (a -> b) -> Relation c d
305305fromF' f = R (I.fromFRel' f)
@@ -308,8 +308,8 @@ fromF' f = R (I.fromFRel' f)
308308-- cardinality's.
309309fromF ::
310310 ( Liftable a b,
311− CountableDimensionsN a b,
312− FromListsN b a
311+ CountableDimsN a b,
312+ FLN b a
313313 )
314314 => (a -> b) -> Relation a b
315315fromF f = R (I.fromFRel f)
@@ -317,8 +317,8 @@ fromF f = R (I.fromFRel f)
317317-- | Lifts relation functions to 'Relation'
318318toRel ::
319319 ( Liftable a b,
320− CountableDimensionsN a b,
321− FromListsN b a
320+ CountableDimsN a b,
321+ FLN b a
322322 )
323323 => (a -> b -> Bool) -> Relation a b
324324toRel = R . I.toRel
@@ -327,9 +327,9 @@ toRel = R . I.toRel
327327fromRel ::
328328 ( Liftable a b,
329329 Eq a,
330− CountableDimensionsN a b,
331− FromListsN a One,
332− FromListsN b One
330+ CountableDimsN a b,
331+ FLN a One,
332+ FLN b One
333333 )
334334 => Relation a b -> (a -> b -> Bool)
335335fromRel r a b = pointApBool a b r
@@ -356,9 +356,9 @@ toBool r = case toList r of
356356pt ::
357357 ( Liftable a b,
358358 Eq a,
359− CountableDimensionsN a b,
360− FromListsN a One,
361− FromListsN b One
359+ CountableDimsN a b,
360+ FLN a One,
361+ FLN b One
362362 )
363363 => Relation a b -> (a -> List b)
364364pt r a =
@@ -370,8 +370,8 @@ belongs ::
370370 ( Bounded a,
371371 Enum a,
372372 Eq a,
373− CountableDimensionsN (List a) a,
374− FromListsN a (List a)
373+ CountableDimsN (List a) a,
374+ FLN a (List a)
375375 )
376376 => Relation (List a) a
377377belongs = toRel elemR
@@ -390,7 +390,7 @@ belongs = toRel elemR
390390-- ⊥ ``sse`` R && R ``sse`` T == True
391391-- @
392392zeros ::
393− (FromListsN a b, CountableDimensionsN a b) =>
393+ (FLN a b, CountableDimsN a b) =>
394394 Relation a b
395395zeros = relationBuilder' (const (nat 0))
396396
@@ -405,7 +405,7 @@ zeros = relationBuilder' (const (nat 0))
405405-- ⊥ ``sse`` R && R ``sse`` T == True
406406-- @
407407ones ::
408− (FromListsN a b, CountableDimensionsN a b) =>
408+ (FLN a b, CountableDimsN a b) =>
409409 Relation a b
410410ones = relationBuilder' (const (nat 1))
411411
@@ -413,7 +413,7 @@ ones = relationBuilder' (const (nat 1))
413413
414414-- | The T (Top) row vector relation.
415415bang ::
416− (FromListsN a One, CountableN a) =>
416+ (FLN a One, CountableN a) =>
417417 Relation a One
418418bang = ones
419419
@@ -423,7 +423,7 @@ point ::
423423 Enum a,
424424 Eq a,
425425 CountableN a,
426− FromListsN a One
426+ FLN a One
427427 ) => a -> Relation One a
428428point = fromF . const
429429
@@ -435,7 +435,7 @@ point = fromF . const
435435-- 'iden' `.` r == r == r `.` 'iden'
436436-- @
437437iden ::
438− (FromListsN a a, CountableN a) => Relation a a
438+ (FLN a a, CountableN a) => Relation a a
439439iden = relationBuilder' (bool (nat 0) (nat 1) . uncurry (==))
440440
441441-- | Relational composition
@@ -489,7 +489,7 @@ shrunkBy r s = r `intersection` divR s (conv r)
489489-- r ``overriddenBy`` r == r
490490-- @
491491overriddenBy ::
492− ( FromListsN b b,
492+ ( FLN b b,
493493 CountableN b
494494 ) => Relation a b -> Relation a b -> Relation a b
495495overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
@@ -501,9 +501,9 @@ overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
501501pointAp ::
502502 ( Liftable a b,
503503 Eq a,
504− CountableDimensionsN a b,
505− FromListsN a One,
506− FromListsN b One
504+ CountableDimsN a b,
505+ FLN a One,
506+ FLN b One
507507 ) => a -> b -> Relation a b -> Relation One One
508508pointAp a b r = conv (point b) . r . point a
509509
@@ -513,9 +513,9 @@ pointAp a b r = conv (point b) . r . point a
513513pointApBool ::
514514 ( Liftable a b,
515515 Eq a,
516− CountableDimensionsN a b,
517− FromListsN a One,
518− FromListsN b One
516+ CountableDimsN a b,
517+ FLN a One,
518+ FLN b One
519519 ) => a -> b -> Relation a b -> Bool
520520pointApBool a b r = toBool $ conv (point b) . r . point a
521521
@@ -595,19 +595,19 @@ divisionF f g = conv g . f
595595-- Taxonomy of binary relations
596596
597597-- | A 'Relation' @r@ is 'simple' 'iff' @'coreflexive' ('img' r)@
598−simple :: (CountableN b, FromListsN b b) => Relation a b -> Bool
598+simple :: (CountableN b, FLN b b) => Relation a b -> Bool
599599simple = coreflexive . img
600600
601601-- | A 'Relation' @r@ is 'injective' 'iff' @'coreflexive' ('ker' r)@
602−injective :: (CountableN a, FromListsN a a) => Relation a b -> Bool
602+injective :: (CountableN a, FLN a a) => Relation a b -> Bool
603603injective = coreflexive . ker
604604
605605-- | A 'Relation' @r@ is 'entire' 'iff' @'reflexive' ('ker' r)@
606−entire :: (CountableN a, FromListsN a a) => Relation a b -> Bool
606+entire :: (CountableN a, FLN a a) => Relation a b -> Bool
607607entire = reflexive . ker
608608
609609-- | A 'Relation' @r@ is 'surjective' 'iff' @'reflexive' ('img' r)@
610−surjective :: (CountableN b, FromListsN b b) => Relation a b -> Bool
610+surjective :: (CountableN b, FLN b b) => Relation a b -> Bool
611611surjective = reflexive . img
612612
613613-- | A 'Relation' @r@ is a 'function' 'iff' @'simple' r && 'entire' r@
@@ -620,9 +620,9 @@ surjective = reflexive . img
620620-- r `.` f ``sse`` s == r ``sse`` s `.` f
621621-- @
622622function ::
623− ( CountableDimensionsN a b,
624− FromListsN a a,
625− FromListsN b b
623+ ( CountableDimsN a b,
624+ FLN a a,
625+ FLN b b
626626 )
627627 => Relation a b -> Bool
628628function r = simple r && entire r
@@ -630,7 +630,7 @@ function r = simple r && entire r
630630-- | A 'Relation' @r@ is a 'representation' 'iff' @'injective' r && 'entire' r@
631631representation ::
632632 ( CountableN a,
633− FromListsN a a
633+ FLN a a
634634 )
635635 => Relation a b -> Bool
636636representation r = injective r && entire r
@@ -638,45 +638,45 @@ representation r = injective r && entire r
638638-- | A 'Relation' @r@ is an 'abstraction' 'iff' @'surjective' r && 'simple' r@
639639abstraction ::
640640 ( CountableN b,
641− FromListsN b b
641+ FLN b b
642642 )
643643 => Relation a b -> Bool
644644abstraction r = surjective r && simple r
645645
646646-- | A 'Relation' @r@ is a 'surjection' 'iff' @'function' r && 'abstraction' r@
647647surjection ::
648− ( CountableDimensionsN a b,
649− FromListsN a a,
650− FromListsN b b
648+ ( CountableDimsN a b,
649+ FLN a a,
650+ FLN b b
651651 )
652652 => Relation a b -> Bool
653653surjection r = function r && abstraction r
654654
655655-- | A 'Relation' @r@ is a 'injection' 'iff' @'function' r && 'representation' r@
656656injection ::
657− ( CountableDimensionsN a b,
658− FromListsN a a,
659− FromListsN b b
657+ ( CountableDimsN a b,
658+ FLN a a,
659+ FLN b b
660660 )
661661 => Relation a b -> Bool
662662injection r = function r && representation r
663663
664664-- | A 'Relation' @r@ is an 'bijection' 'iff' @'injection' r && 'surjection' r@
665665bijection ::
666− ( CountableDimensionsN a b,
667− FromListsN b b,
668− FromListsN a a
666+ ( CountableDimsN a b,
667+ FLN b b,
668+ FLN a a
669669 ) => Relation a b -> Bool
670670bijection r = injection r && surjection r
671671
672672-- Properties of relations
673673
674674-- | A 'Relation' @r@ is 'reflexive' 'iff' @'id' ``sse`` r@
675−reflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
675+reflexive :: (CountableN a, FLN a a) => Relation a a -> Bool
676676reflexive r = id <= r
677677
678678-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'id'@
679−coreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
679+coreflexive :: (CountableN a, FLN a a) => Relation a a -> Bool
680680coreflexive r = r <= id
681681
682682-- | A 'Relation' @r@ is 'transitive' 'iff' @(r `.` r) ``sse`` r@
@@ -688,35 +688,35 @@ symmetric :: Relation a a -> Bool
688688symmetric r = r == conv r
689689
690690-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'id'@
691−antiSymmetric :: (CountableN a, FromListsN a a) => Relation a a -> Bool
691+antiSymmetric :: (CountableN a, FLN a a) => Relation a a -> Bool
692692antiSymmetric r = (r `intersection` conv r) `sse` id
693693
694694-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'id') == 'zeros'@
695−irreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
695+irreflexive :: (CountableN a, FLN a a) => Relation a a -> Bool
696696irreflexive r = (r `intersection` id) == zeros
697697
698698-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
699−connected :: (CountableN a, FromListsN a a) => Relation a a -> Bool
699+connected :: (CountableN a, FLN a a) => Relation a a -> Bool
700700connected r = (r `union` conv r) == ones
701701
702702-- | A 'Relation' @r@ is a 'preorder' 'iff' @'reflexive' r && 'transitive' r@
703−preorder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
703+preorder :: (CountableN a, FLN a a) => Relation a a -> Bool
704704preorder r = reflexive r && transitive r
705705
706706-- | A 'Relation' @r@ is a partial-order 'iff' @'antiSymmetric' r && 'preorder' r@
707−partialOrder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
707+partialOrder :: (CountableN a, FLN a a) => Relation a a -> Bool
708708partialOrder r = antiSymmetric r && preorder r
709709
710710-- | A 'Relation' @r@ is a linear-order 'iff' @'connected' r && 'partialOrder' r@
711−linearOrder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
711+linearOrder :: (CountableN a, FLN a a) => Relation a a -> Bool
712712linearOrder r = connected r && partialOrder r
713713
714714-- | A 'Relation' @r@ is an 'equivalence' 'iff' @'symmetric' r && 'preorder' r@
715−equivalence :: (CountableN a, FromListsN a a) => Relation a a -> Bool
715+equivalence :: (CountableN a, FLN a a) => Relation a a -> Bool
716716equivalence r = symmetric r && preorder r
717717
718718-- | A 'Relation' @r@ is a partial-equivalence 'iff' @'partialOrder' r && 'equivalence' r@
719−partialEquivalence :: (CountableN a, FromListsN a a) => Relation a a -> Bool
719+partialEquivalence :: (CountableN a, FLN a a) => Relation a a -> Bool
720720partialEquivalence r = partialOrder r && equivalence r
721721
722722-- | A 'Relation' @r@ is 'difunctional' or regular wherever
@@ -748,10 +748,10 @@ difunctional r = r . conv r . r == r
748748-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
749749-- @
750750splitR ::
751− ( CountableDimensionsN a b,
751+ ( CountableDimsN a b,
752752 CountableN (a, b),
753− FromListsN (a, b) a,
754− FromListsN (a, b) b,
753+ FLN (a, b) a,
754+ FLN (a, b) b,
755755 TrivialP a b
756756 )
757757 => Relation c a -> Relation c b -> Relation c (a, b)
@@ -764,9 +764,9 @@ splitR (R f) (R g) = R (I.kr f g)
764764-- @
765765fstR ::
766766 forall a b .
767− ( CountableDimensionsN a b,
767+ ( CountableDimsN a b,
768768 CountableN (a, b),
769− FromListsN (a, b) a,
769+ FLN (a, b) a,
770770 TrivialP a b
771771 )
772772 => Relation (a, b) a
@@ -779,9 +779,9 @@ fstR = R (I.fstM @Boolean @(I.Normalize a) @(I.Normalize b))
779779-- @
780780sndR ::
781781 forall a b .
782− ( CountableDimensionsN a b,
782+ ( CountableDimsN a b,
783783 CountableN (a, b),
784− FromListsN (a, b) b,
784+ FLN (a, b) b,
785785 TrivialP a b
786786 )
787787 => Relation (a, b) b
@@ -797,13 +797,13 @@ infixl 4 ><
797797-- (r '><' s) `.` (p '><' q) == (r `.` p) '><' (s `.` q)
798798-- @
799799(><) ::
800− ( CountableDimensionsN a b,
801− CountableDimensionsN c d,
802− CountableDimensionsN (a, c) (b, d),
803− FromListsN (a, c) a,
804− FromListsN (a, c) c,
805− FromListsN (b, d) b,
806− FromListsN (b, d) d,
800+ ( CountableDimsN a b,
801+ CountableDimsN c d,
802+ CountableDimsN (a, c) (b, d),
803+ FLN (a, c) a,
804+ FLN (a, c) c,
805+ FLN (b, d) b,
806+ FLN (b, d) d,
807807 TrivialP a c,
808808 TrivialP b d
809809 )
@@ -837,9 +837,9 @@ eitherR = join
837837-- 'i1' `.` 'i2' = 'zeros'
838838-- @
839839i1 ::
840− ( CountableDimensionsN a b,
841− FromListsN b a,
842− FromListsN a a
840+ ( CountableDimsN a b,
841+ FLN b a,
842+ FLN a a
843843 )
844844 => Relation a (Either a b)
845845i1 = R I.i1
@@ -851,9 +851,9 @@ i1 = R I.i1
851851-- 'i1' `.` 'i2' = 'zeros'
852852-- @
853853i2 ::
854− ( CountableDimensionsN a b,
855− FromListsN a b,
856− FromListsN b b
854+ ( CountableDimsN a b,
855+ FLN a b,
856+ FLN b b
857857 )
858858 => Relation b (Either a b)
859859i2 = R I.i2
@@ -866,11 +866,11 @@ infixl 5 -|-
866866-- r '-|-' s == 'eitherR' ('i1' `.` r) ('i2' `.` s)
867867-- @
868868(-|-) ::
869− ( CountableDimensionsN b d,
870− FromListsN b b,
871− FromListsN d b,
872− FromListsN b d,
873− FromListsN d d
869+ ( CountableDimsN b d,
870+ FLN b b,
871+ FLN d b,
872+ FLN b d,
873+ FLN d d
874874 )
875875 => Relation a b -> Relation c d -> Relation (Either a c) (Either b d)
876876(-|-) (R a) (R b) = R ((I.-|-) a b)
@@ -883,13 +883,13 @@ infixl 5 -|-
883883-- 'trans'/'untrans';
884884-- more-over, where each particular attribute is placed (input/output) is irrelevant.
885885trans ::
886− ( CountableDimensionsN a b,
886+ ( CountableDimsN a b,
887887 CountableN c,
888− CountableDimensionsN (a, b) (c, b),
889− FromListsN (c, b) c,
890− FromListsN (c, b) b,
891− FromListsN (a, b) a,
892− FromListsN (a, b) b,
888+ CountableDimsN (a, b) (c, b),
889+ FLN (c, b) c,
890+ FLN (c, b) b,
891+ FLN (a, b) a,
892+ FLN (a, b) b,
893893 Trivial (a, b),
894894 Trivial (c, b),
895895 TrivialP a b,
@@ -904,13 +904,13 @@ trans r = splitR r sndR . conv fstR
904904-- 'trans'/'untrans';
905905-- more-over, where each particular attribute is placed (input/output) is irrelevant.
906906untrans ::
907− ( CountableDimensionsN a b,
907+ ( CountableDimsN a b,
908908 CountableN c,
909− CountableDimensionsN (a, b) (c, b),
910− FromListsN (c, b) c,
911− FromListsN (c, b) b,
912− FromListsN (a, b) b,
913− FromListsN (a, b) a,
909+ CountableDimsN (a, b) (c, b),
910+ FLN (c, b) c,
911+ FLN (c, b) b,
912+ FLN (a, b) b,
913+ FLN (a, b) a,
914914 Trivial (a, b),
915915 Trivial (c, b),
916916 TrivialP a b,
@@ -933,8 +933,8 @@ predR ::
933933 ( Bounded a,
934934 Enum a,
935935 CountableN a,
936− FromListsN a a,
937− FromListsN Bool a
936+ FLN a a,
937+ FLN Bool a
938938 )
939939 => Relation a Bool -> Relation a a
940940predR p = id `intersection` divisionF (fromF (const True)) p
@@ -949,7 +949,7 @@ predR p = id `intersection` divisionF (fromF (const True)) p
949949-- @
950950equalizer ::
951951 ( CountableN a,
952− FromListsN a a
952+ FLN a a
953953 )
954954 => Relation a b -> Relation a b -> Relation a a
955955equalizer f g = id `intersection` divisionF f g
@@ -963,8 +963,8 @@ guard ::
963963 ( Bounded b,
964964 Enum b,
965965 CountableN b,
966− FromListsN b b,
967− FromListsN Bool b
966+ FLN b b,
967+ FLN Bool b
968968 ) => Relation b Bool -> Relation b (Either b b)
969969guard p = conv (eitherR (predR p) (predR (negate p)))
970970
@@ -973,8 +973,8 @@ cond ::
973973 ( Bounded b,
974974 Enum b,
975975 CountableN b,
976− FromListsN b b,
977− FromListsN Bool b
976+ FLN b b,
977+ FLN Bool b
978978 )
979979 => Relation b Bool -> Relation b c -> Relation b c -> Relation b c
980980cond p r s = eitherR r s . guard p
@@ -985,7 +985,7 @@ cond p r s = eitherR r s . guard p
985985-- since @'ker' r ``sse`` 'id'@ in such situations.
986986domain ::
987987 ( CountableN a,
988− FromListsN a a
988+ FLN a a
989989 ) => Relation a b -> Relation a a
990990domain r = ker r `intersection` id
991991
@@ -995,16 +995,16 @@ domain r = ker r `intersection` id
995995-- since @'img' f ``sse`` id@ for any @f@.
996996range ::
997997 ( CountableN b,
998− FromListsN b b
998+ FLN b b
999999 ) => Relation a b -> Relation b b
10001000range r = img r `intersection` id
10011001
10021002-- Relation pretty print
10031003
10041004-- | Relation pretty printing
1005−pretty :: (CountableDimensionsN a b) => Relation a b -> String
1005+pretty :: (CountableDimsN a b) => Relation a b -> String
10061006pretty (R a) = I.pretty a
10071007
10081008-- | Relation pretty printing
1009−prettyPrint :: (CountableDimensionsN a b) => Relation a b -> IO ()
1009+prettyPrint :: (CountableDimsN a b) => Relation a b -> IO ()
10101010prettyPrint (R a) = I.prettyPrint a