@@ -8,7 +8,7 @@
9-----------------------------------------------------------------------------
9-----------------------------------------------------------------------------
11−-- Module : Relation.Internal
11+-- Module : LAoP.Relation.Internal
12-- Copyright : (c) Armando Santos 2019-2020
12-- Copyright : (c) Armando Santos 2019-2020
13-- Maintainer : armandoifsantos@gmail.com
13-- Maintainer : armandoifsantos@gmail.com
14-- Stability : experimental
14-- Stability : experimental
@@ -24,7 +24,7 @@
25-----------------------------------------------------------------------------
25-----------------------------------------------------------------------------
27−module Relation.Internal
27+module LAoP.Relation.Internal
28 ( -- | This definition makes use of the fact that 'Void' is
28 ( -- | This definition makes use of the fact that 'Void' is
29 -- isomorphic to 0 and '()' to 1 and captures matrix
29 -- isomorphic to 0 and '()' to 1 and captures matrix
30 -- dimensions as stacks of 'Either's.
30 -- dimensions as stacks of 'Either's.
@@ -45,12 +45,12 @@ module Relation.Internal
47 -- * Auxiliary type families
47 -- * Auxiliary type families
48− FromNat,
48+ I.FromNat,
50− Normalize,
50+ I.Normalize,
52 -- * Matrix construction and conversion
52 -- * Matrix construction and conversion
53− FromLists,
53+ I.FromLists,
54 fromLists,
54 fromLists,
@@ -97,11 +97,12 @@ module Relation.Internal
99import Data.Void
99import Data.Void
100−import qualified Matrix.Internal as I
100+import qualified LAoP.Matrix.Internal as I
101−import Utils
101+import LAoP.Utils
102import Control.DeepSeq
102import Control.DeepSeq
103import qualified Control.Category as C
103import qualified Control.Category as C
104import Data.Coerce
104import Data.Coerce
105import GHC.TypeLits
106import GHC.TypeLits
107-- | Boolean type synonym for working with boolean matrices
108-- | Boolean type synonym for working with boolean matrices
@@ -123,11 +124,14 @@ instance C.Category Relation where
123 (.) = comp
124 (.) = comp
125instance Num (Relation a b) where
126instance 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)
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)
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)
132-- Type alias
136-- Type alias
133type Zero = Void
137type Zero = Void
@@ -181,8 +185,8 @@ fromLists = R . I.fromLists
181-- | Matrix builder function. Constructs a matrix provided with
185-- | Matrix builder function. Constructs a matrix provided with
182-- a construction function.
186-- a construction function.
183matrixBuilder ::
187matrixBuilder ::
184− (I.FromLists e (I.Normalize a) (I.Normalize b), KnownNat (I.Count (I.Normalize a)), KnownNat (I.Count (I.Normalize b))) =>
188+ (I.FromLists Boolean (I.Normalize a) (I.Normalize b), KnownNat (I.Count (I.Normalize a)), KnownNat (I.Count (I.Normalize b))) =>
185− ((Int, Int) -> e) ->
189+ ((Int, Int) -> Boolean) ->
186 Relation a b
190 Relation a b
187matrixBuilder = R . I.matrixBuilder
191matrixBuilder = R . I.matrixBuilder
@@ -221,12 +225,12 @@ fromF' f = R (I.fromFRel' f)
221-- Conversion
225-- Conversion
223-- | Converts a matrix to a list of lists of elements.
227-- | Converts a matrix to a list of lists of elements.
224−toLists :: Relation a b -> [[e]]
228+toLists :: Relation a b -> [[Boolean]]
225−toLists (R m) = R $ I.toLists m
229+toLists (R m) = I.toLists m
227-- | Converts a matrix to a list of elements.
231-- | Converts a matrix to a list of elements.
228−toList :: Relation a b -> [e]
232+toList :: Relation a b -> [Boolean]
229−toList (R m) = R $ I.toList m
233+toList (R m) = I.toList m
231-- Zeros Matrix
235-- Zeros Matrix
@@ -318,47 +322,47 @@ entire :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize
318entire = reflexive . ker
322entire = reflexive . ker
320-- | A 'Relation' @r@ is surjective iff @'reflexive' ('img' r)@
324-- | 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
322surjective = reflexive . img
326surjective = reflexive . img
324−-- | A 'Relation' @r@ is transitive iff @r . r `'sse'` r@
328+-- | A 'Relation' @r@ is transitive iff @(r `'comp'` r) `'sse'` r@
325−transitive :: Relation a b -> Bool
329+transitive :: Relation a a -> Bool
326−transitive r = r . r `sse` r
330+transitive r = (r `comp` r) `sse` r
328-- | A 'Relation' @r@ is symmetric iff @r == r@
332-- | A 'Relation' @r@ is symmetric iff @r == r@
329−symmetric :: Relation a b -> Bool
333+symmetric :: Relation a a -> Bool
330symmetric r = r == r
334symmetric r = r == r
332-- | A 'Relation' @r@ is anti-symmetric iff @(r `'intersection'` 'conv' r) `'sse'` 'identity'@
336-- | 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
334antiSymmetric r = (r `intersection` conv r) `sse` identity
338antiSymmetric r = (r `intersection` conv r) `sse` identity
336-- | A 'Relation' @r@ is irreflexive iff @(r `'intersection'` identity) == 'zeros'@
340-- | 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
338irreflexive r = (r `intersection` identity) == zeros
342irreflexive r = (r `intersection` identity) == zeros
340-- | A 'Relation' @r@ is connected iff @(r `'union'` 'conv' r) == 'ones'@
344-- | 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
342connected r = (r `union` conv r) == ones
346connected r = (r `union` conv r) == ones
344-- | A 'Relation @r@ is a preorder iff @'reflexive' r && 'transitive' r@
348-- | 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
346preorder r = reflexive r && transitive r
350preorder r = reflexive r && transitive r
348-- | A 'Relation @r@ is a partial order iff @'antiSymmetric' r && 'preorder' r@
352-- | 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
350partialOrder r = antiSymmetric r && preorder r
354partialOrder r = antiSymmetric r && preorder r
352-- | A 'Relation @r@ is a linear order iff @'connected' r && 'partialOrder' r@
356-- | 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
354linearOrder r = connected r && partialOrder r
358linearOrder r = connected r && partialOrder r
356-- | A 'Relation @r@ is an equivalence iff @'symmetric' r && 'preorder' r@
360-- | 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
358equivalence r = symmetric r && preorder r
362equivalence r = symmetric r && preorder r
360-- | A 'Relation @r@ is a partial equivalence iff @'partialOrder' r && 'equivalence' r@
364-- | 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
362partialEquivalence r = partialOrder r && equivalence r
366partialEquivalence r = partialOrder r && equivalence r
364-- Relation pretty print
368-- Relation pretty print