@@ -8,7 +8,7 @@
88
99-----------------------------------------------------------------------------
1010-- |
11−-- Module : Relation.Internal
11+-- Module : LAoP.Relation.Internal
1212-- Copyright : (c) Armando Santos 2019-2020
1313-- Maintainer : armandoifsantos@gmail.com
1414-- Stability : experimental
@@ -24,7 +24,7 @@
2424--
2525-----------------------------------------------------------------------------
2626
27−module Relation.Internal
27+module LAoP.Relation.Internal
2828 ( -- | This definition makes use of the fact that 'Void' is
2929 -- isomorphic to 0 and '()' to 1 and captures matrix
3030 -- dimensions as stacks of 'Either's.
@@ -45,12 +45,12 @@ module Relation.Internal
4545 split,
4646
4747 -- * Auxiliary type families
48− FromNat,
49− Count,
50− Normalize,
48+ I.FromNat,
49+ I.Count,
50+ I.Normalize,
5151
5252 -- * Matrix construction and conversion
53− FromLists,
53+ I.FromLists,
5454 fromLists,
5555 toLists,
5656 toList,
@@ -97,11 +97,12 @@ module Relation.Internal
9797 where
9898
9999import Data.Void
100−import qualified Matrix.Internal as I
101−import Utils
100+import qualified LAoP.Matrix.Internal as I
101+import LAoP.Utils
102102import Control.DeepSeq
103103import qualified Control.Category as C
104104import Data.Coerce
105+import Data.Bool
105106import GHC.TypeLits
106107
107108-- | Boolean type synonym for working with boolean matrices
@@ -123,11 +124,14 @@ instance C.Category Relation where
123124 (.) = comp
124125
125126instance Num (Relation a b) where
126− (R a) + (R b) = R (I.orM a b) --^ Matrix addition becomes Boolean matrix disjunction
127+ -- | Matrix addition becomes Boolean matrix disjunction
128+ (R a) + (R b) = R (I.orM a b)
127129
128− (R a) - (R b) = R (I.subM a b) --^ Matrix subtraction becomes Relational subtraction
130+ -- | Matrix subtraction becomes Relational subtraction
131+ (R a) - (R b) = R (I.subM a b)
129132
130− (R a) * (R b) = R (I.andM a b) --^ Matrix multiplication becomes Boolean matrix conjunction
133+ -- | Matrix multiplication becomes Boolean matrix conjunction
134+ (R a) * (R b) = R (I.andM a b)
131135
132136-- Type alias
133137type Zero = Void
@@ -181,8 +185,8 @@ fromLists = R . I.fromLists
181185-- | Matrix builder function. Constructs a matrix provided with
182186-- a construction function.
183187matrixBuilder ::
184− (I.FromLists e (I.Normalize a) (I.Normalize b), KnownNat (I.Count (I.Normalize a)), KnownNat (I.Count (I.Normalize b))) =>
185− ((Int, Int) -> e) ->
188+ (I.FromLists Boolean (I.Normalize a) (I.Normalize b), KnownNat (I.Count (I.Normalize a)), KnownNat (I.Count (I.Normalize b))) =>
189+ ((Int, Int) -> Boolean) ->
186190 Relation a b
187191matrixBuilder = R . I.matrixBuilder
188192
@@ -221,12 +225,12 @@ fromF' f = R (I.fromFRel' f)
221225-- Conversion
222226
223227-- | Converts a matrix to a list of lists of elements.
224−toLists :: Relation a b -> [[e]]
225−toLists (R m) = R $ I.toLists m
228+toLists :: Relation a b -> [[Boolean]]
229+toLists (R m) = I.toLists m
226230
227231-- | Converts a matrix to a list of elements.
228−toList :: Relation a b -> [e]
229−toList (R m) = R $ I.toList m
232+toList :: Relation a b -> [Boolean]
233+toList (R m) = I.toList m
230234
231235-- Zeros Matrix
232236
@@ -318,47 +322,47 @@ entire :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize
318322entire = reflexive . ker
319323
320324-- | A 'Relation' @r@ is surjective iff @'reflexive' ('img' r)@
321−surjective :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a b -> Bool
325+surjective :: (KnownNat (I.Count (I.Normalize b)), I.FromLists Boolean (I.Normalize b) (I.Normalize b)) => Relation a b -> Bool
322326surjective = reflexive . img
323327
324−-- | A 'Relation' @r@ is transitive iff @r . r `'sse'` r@
325−transitive :: Relation a b -> Bool
326−transitive r = r . r `sse` r
328+-- | A 'Relation' @r@ is transitive iff @(r `'comp'` r) `'sse'` r@
329+transitive :: Relation a a -> Bool
330+transitive r = (r `comp` r) `sse` r
327331
328332-- | A 'Relation' @r@ is symmetric iff @r == r@
329−symmetric :: Relation a b -> Bool
333+symmetric :: Relation a a -> Bool
330334symmetric r = r == r
331335
332336-- | A 'Relation' @r@ is anti-symmetric iff @(r `'intersection'` 'conv' r) `'sse'` 'identity'@
333−antiSymmetric :: Relation a b -> Bool
337+antiSymmetric :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
334338antiSymmetric r = (r `intersection` conv r) `sse` identity
335339
336340-- | A 'Relation' @r@ is irreflexive iff @(r `'intersection'` identity) == 'zeros'@
337−irreflexive :: Relation a b -> Bool
341+irreflexive :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
338342irreflexive r = (r `intersection` identity) == zeros
339343
340344-- | A 'Relation' @r@ is connected iff @(r `'union'` 'conv' r) == 'ones'@
341−connected :: () => Relation a b -> Bool
345+connected :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
342346connected r = (r `union` conv r) == ones
343347
344348-- | A 'Relation @r@ is a preorder iff @'reflexive' r && 'transitive' r@
345−preorder :: () => Relation a b -> Bool
349+preorder :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
346350preorder r = reflexive r && transitive r
347351
348352-- | A 'Relation @r@ is a partial order iff @'antiSymmetric' r && 'preorder' r@
349−partialOrder :: () => Relation a b -> Bool
353+partialOrder :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
350354partialOrder r = antiSymmetric r && preorder r
351355
352356-- | A 'Relation @r@ is a linear order iff @'connected' r && 'partialOrder' r@
353−linearOrder :: () => Relation a b -> Bool
357+linearOrder :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
354358linearOrder r = connected r && partialOrder r
355359
356360-- | A 'Relation @r@ is an equivalence iff @'symmetric' r && 'preorder' r@
357−equivalence :: () => Relation a b -> Bool
361+equivalence :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
358362equivalence r = symmetric r && preorder r
359363
360364-- | A 'Relation @r@ is a partial equivalence iff @'partialOrder' r && 'equivalence' r@
361−partialEquivalence :: () => Relation a b -> Bool
365+partialEquivalence :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
362366partialEquivalence r = partialOrder r && equivalence r
363367
364368-- Relation pretty print