@@ -1,3 +1,4 @@
1+{-# LANGUAGE ConstraintKinds #-}
1{-# LANGUAGE GADTs #-}
2{-# LANGUAGE GADTs #-}
2{-# LANGUAGE UndecidableInstances #-}
3{-# LANGUAGE UndecidableInstances #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
@@ -39,6 +40,18 @@ module LAoP.Relation.Internal
39 Relation (..),
40 Relation (..),
43+ -- * Constraint type synonyms
47+ CountableDimensionsN,
42 -- * Primitives
55 -- * Primitives
@@ -179,6 +192,18 @@ newtype Relation a b = R (I.Matrix Boolean (I.Normalize a) (I.Normalize b))
180deriving instance (Read (I.Matrix Boolean (I.Normalize a) (I.Normalize b))) => Read (Relation a b)
193deriving instance (Read (I.Matrix Boolean (I.Normalize a) (I.Normalize b))) => Read (Relation a b)
195+-- | 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 a b = I.FromLists Boolean (I.Normalize a) (I.Normalize b)
201+type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Boolean, Ord Boolean)
202+type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
203+type TrivialE a b = I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)
204+type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
205+type TrivialP2 a b = I.Normalize (I.Normalize a, I.Normalize b) ~ I.Normalize (I.Normalize (a, b))
182-- | It isn't possible to implement the 'id' function so it's
207-- | It isn't possible to implement the 'id' function so it's
183-- implementation is 'undefined'. However 'comp' can be and this partial
208-- implementation is 'undefined'. However 'comp' can be and this partial
184-- class implementation exists just to make the code more readable.
209-- class implementation exists just to make the code more readable.
@@ -218,7 +243,7 @@ one = R . I.One
218-- | Boolean Matrix 'Junc' constructor, also known as relational coproduct.
243-- | Boolean Matrix 'Junc' constructor, also known as relational coproduct.
220-- See 'eitherR'.
245-- See 'eitherR'.
221−junc :: (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b))
246+junc :: (TrivialE a b)
222 => Relation a c -> Relation b c -> Relation (Either a b) c
247 => Relation a c -> Relation b c -> Relation (Either a b) c
223junc (R a) (R b) = R (I.Junc a b)
248junc (R a) (R b) = R (I.Junc a b)
@@ -227,21 +252,21 @@ infixl 3 |||
228-- See 'eitherR'.
253-- See 'eitherR'.
230− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
255+ (TrivialE a b) =>
231 Relation a c ->
256 Relation a c ->
232 Relation b c ->
257 Relation b c ->
233 Relation (Either a b) c
258 Relation (Either a b) c
234(|||) = junc
259(|||) = junc
236-- | Boolean Matrix 'Split' constructor, also known as relational product.
261-- | Boolean Matrix 'Split' constructor, also known as relational product.
237−split :: (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b))
262+split :: (TrivialE a b)
238 => Relation c a -> Relation c b -> Relation c (Either a b)
263 => Relation c a -> Relation c b -> Relation c (Either a b)
239split (R a) (R b) = R (I.Split a b)
264split (R a) (R b) = R (I.Split a b)
241infixl 2 ===
266infixl 2 ===
242-- | Boolean Matrix 'Split' constructor
267-- | Boolean Matrix 'Split' constructor
244− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
269+ (TrivialE a b) =>
245 Relation c a ->
270 Relation c a ->
246 Relation c b ->
271 Relation c b ->
247 Relation c (Either a b)
272 Relation c (Either a b)
@@ -251,15 +276,14 @@ infixl 2 ===
252-- | Build a matrix out of a list of list of elements. Throws a runtime
277-- | Build a matrix out of a list of list of elements. Throws a runtime
253-- error if the dimensions do not match.
278-- error if the dimensions do not match.
254−fromLists :: (I.FromLists Boolean (I.Normalize a) (I.Normalize b)) => [[Boolean]] -> Relation a b
279+fromLists :: (FromListsN a b) => [[Boolean]] -> Relation a b
255fromLists = R . I.fromLists
280fromLists = R . I.fromLists
257-- | Matrix builder function. Constructs a matrix provided with
282-- | Matrix builder function. Constructs a matrix provided with
258-- a construction function.
283-- a construction function.
259relationBuilder ::
284relationBuilder ::
260− (I.FromLists Boolean (I.Normalize a) (I.Normalize b), KnownNat (I.Count (I.Normalize a)), KnownNat (I.Count (I.Normalize b))) =>
285+ (FromListsN a b, CountableDimensionsN a b) =>
261− ((Int, Int) -> Boolean) ->
286+ ((Int, Int) -> Boolean) -> Relation a b
263relationBuilder = R . I.matrixBuilder
287relationBuilder = R . I.matrixBuilder
265-- | Lifts functions to matrices with arbitrary dimensions.
289-- | Lifts functions to matrices with arbitrary dimensions.
@@ -267,14 +291,9 @@ relationBuilder = R . I.matrixBuilder
267-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
291-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
268-- types @a@ or @b@ allows.
292-- types @a@ or @b@ allows.
270− ( Bounded a,
294+ ( Liftable a b,
271− Bounded b,
295+ CountableDimensionsN c d,
272− Enum a,
296+ FromListsN d c
275− KnownNat (I.Count (I.Normalize c)),
276− KnownNat (I.Count (I.Normalize d)),
277− I.FromLists Boolean (I.Normalize d) (I.Normalize c)
279 => (a -> b) -> Relation c d
298 => (a -> b) -> Relation c d
280fromF f = R (I.fromFRel f)
299fromF f = R (I.fromFRel f)
@@ -282,44 +301,29 @@ fromF f = R (I.fromFRel f)
282-- | Lifts functions to matrices with dimensions matching @a@ and @b@
301-- | Lifts functions to matrices with dimensions matching @a@ and @b@
283-- cardinality's.
302-- cardinality's.
284fromF' ::
303fromF' ::
285− ( Bounded b,
304+ ( Liftable a b,
286− Bounded a,
305+ CountableDimensionsN a b,
287− Enum a,
306+ FromListsN b a
290− KnownNat (I.Count (I.Normalize a)),
291− KnownNat (I.Count (I.Normalize b)),
292− I.FromLists Boolean (I.Normalize b) (I.Normalize a)
294 => (a -> b) -> Relation a b
308 => (a -> b) -> Relation a b
295fromF' f = R (I.fromFRel' f)
309fromF' f = R (I.fromFRel' f)
297-- | Lifts relation functions to 'Relation'
311-- | Lifts relation functions to 'Relation'
299− ( Bounded b,
313+ ( Liftable a b,
300− Bounded a,
314+ CountableDimensionsN a b,
301− Enum a,
315+ FromListsN b a
304− KnownNat (I.Count (I.Normalize a)),
305− KnownNat (I.Count (I.Normalize b)),
306− I.FromLists Boolean (I.Normalize b) (I.Normalize a)
308− => (a -> b -> Bool) -> Relation a b
317+ => (a -> b -> Bool) -> Relation a b
309toRel = R . I.toRel
318toRel = R . I.toRel
311-- | Lowers a 'Relation' to a function
320-- | Lowers a 'Relation' to a function
312fromRel ::
321fromRel ::
313− ( Bounded a,
322+ ( Liftable a b,
318− Eq b,
324+ CountableDimensionsN a b,
319− KnownNat (I.Count (I.Normalize a)),
325+ FromListsN a One,
320− KnownNat (I.Count (I.Normalize b)),
326+ FromListsN b One
321− I.FromLists Boolean (I.Normalize a) One,
322− I.FromLists Boolean (I.Normalize b) One
324 => Relation a b -> (a -> b -> Bool)
328 => Relation a b -> (a -> b -> Bool)
325fromRel r a b = pointApBool a b r
329fromRel r a b = pointApBool a b r
@@ -344,16 +348,11 @@ toBool r = case toList r of
345-- Maps a relation to a set valued function.
349-- Maps a relation to a set valued function.
347− ( Bounded a,
351+ ( Liftable a b,
352− Eq b,
353+ CountableDimensionsN a b,
353− KnownNat (I.Count (I.Normalize a)),
354+ FromListsN a One,
354− KnownNat (I.Count (I.Normalize b)),
355+ FromListsN b One
355− I.FromLists Boolean (I.Normalize a) One,
356− I.FromLists Boolean (I.Normalize b) One
358 => Relation a b -> (a -> Powerset b)
357 => Relation a b -> (a -> Powerset b)
@@ -365,9 +364,8 @@ belongs ::
365 ( Bounded a,
364 ( Bounded a,
368− KnownNat (I.Count (I.Normalize (Powerset a))),
367+ CountableDimensionsN (Powerset a) a,
369− KnownNat (I.Count (I.Normalize a)),
368+ FromListsN a (Powerset a)
370− I.FromLists Boolean (I.Normalize a) (I.Normalize (Powerset a))
372 => Relation (Powerset a) a
370 => Relation (Powerset a) a
373belongs = toRel (flip elemR)
371belongs = toRel (flip elemR)
@@ -386,7 +384,7 @@ belongs = toRel (flip elemR)
386-- ⊥ ``sse`` R && R ``sse`` T == True
384-- ⊥ ``sse`` R && R ``sse`` T == True
389− (I.FromLists Boolean (I.Normalize a) (I.Normalize b), KnownNat (I.Count (I.Normalize a)), KnownNat (I.Count (I.Normalize b))) =>
387+ (FromListsN a b, CountableDimensionsN a b) =>
390 Relation a b
388 Relation a b
391zeros = relationBuilder (const (nat 0))
389zeros = relationBuilder (const (nat 0))
@@ -401,7 +399,7 @@ zeros = relationBuilder (const (nat 0))
401-- ⊥ ``sse`` R && R ``sse`` T == True
399-- ⊥ ``sse`` R && R ``sse`` T == True
404− (I.FromLists Boolean (I.Normalize a) (I.Normalize b), KnownNat (I.Count (I.Normalize a)), KnownNat (I.Count (I.Normalize b))) =>
402+ (FromListsN a b, CountableDimensionsN a b) =>
405 Relation a b
403 Relation a b
406ones = relationBuilder (const (nat 1))
404ones = relationBuilder (const (nat 1))
@@ -409,7 +407,7 @@ ones = relationBuilder (const (nat 1))
410-- | The T (Top) row vector relation.
408-- | The T (Top) row vector relation.
412− (I.FromLists Boolean (I.Normalize a) One, KnownNat (I.Count (I.Normalize a))) =>
410+ (FromListsN a One, CountableN a) =>
413 Relation a One
411 Relation a One
414bang = ones
412bang = ones
@@ -418,8 +416,8 @@ point ::
418 ( Bounded a,
416 ( Bounded a,
421− KnownNat (I.Count (I.Normalize a)),
419+ CountableN a,
422− I.FromLists Boolean (I.Normalize a) One
420+ FromListsN a One
423 ) => a -> Relation One a
421 ) => a -> Relation One a
424point = fromF' . const
422point = fromF' . const
@@ -431,8 +429,7 @@ point = fromF' . const
431-- 'identity' ``comp`` r == r == r ``comp`` 'identity'
429-- 'identity' ``comp`` r == r == r ``comp`` 'identity'
433identity ::
431identity ::
434− (I.FromLists Boolean (I.Normalize a) (I.Normalize a), KnownNat (I.Count (I.Normalize a))) =>
435− Relation a a
432+ (FromListsN a a, CountableN a) => Relation a a
436identity = relationBuilder (bool (nat 0) (nat 1) . uncurry (==))
433identity = relationBuilder (bool (nat 0) (nat 1) . uncurry (==))
438-- | Relational composition
435-- | Relational composition
@@ -486,8 +483,8 @@ shrunkBy r s = r `intersection` divR s (conv r)
486-- r ``overriddenBy`` r == r
483-- r ``overriddenBy`` r == r
488overriddenBy ::
485overriddenBy ::
489− ( I.FromLists Boolean (I.Normalize b) (I.Normalize b),
486+ ( FromListsN b b,
490− KnownNat (I.Count (I.Normalize b))
487+ CountableN b
491 ) => Relation a b -> Relation a b -> Relation a b
488 ) => Relation a b -> Relation a b -> Relation a b
492overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
489overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
@@ -496,16 +493,11 @@ overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
496-- If @a@ and @b@ are related by 'Relation' @r@
493-- If @a@ and @b@ are related by 'Relation' @r@
497-- then @'pointAp' a b r == 'one' ('nat' 1)@
494-- then @'pointAp' a b r == 'one' ('nat' 1)@
498pointAp ::
495pointAp ::
499− ( Bounded a,
496+ ( Liftable a b,
504− Eq b,
498+ CountableDimensionsN a b,
505− KnownNat (I.Count (I.Normalize a)),
499+ FromListsN a One,
506− KnownNat (I.Count (I.Normalize b)),
500+ FromListsN b One
507− I.FromLists Boolean (I.Normalize a) One,
508− I.FromLists Boolean (I.Normalize b) One
509 ) => a -> b -> Relation a b -> Relation One One
501 ) => a -> b -> Relation a b -> Relation One One
510pointAp a b r = conv (point b) `comp` r `comp` point a
502pointAp a b r = conv (point b) `comp` r `comp` point a
@@ -513,16 +505,11 @@ pointAp a b r = conv (point b) `comp` r `comp` point a
514-- The same as 'pointAp' but converts 'Boolean' to 'Bool'
506-- The same as 'pointAp' but converts 'Boolean' to 'Bool'
515pointApBool ::
507pointApBool ::
516− ( Bounded a,
508+ ( Liftable a b,
521− Eq b,
510+ CountableDimensionsN a b,
522− KnownNat (I.Count (I.Normalize a)),
511+ FromListsN a One,
523− KnownNat (I.Count (I.Normalize b)),
512+ FromListsN b One
524− I.FromLists Boolean (I.Normalize a) One,
525− I.FromLists Boolean (I.Normalize b) One
526 ) => a -> b -> Relation a b -> Bool
513 ) => a -> b -> Relation a b -> Bool
527pointApBool a b r = toBool $ conv (point b) `comp` r `comp` point a
514pointApBool a b r = toBool $ conv (point b) `comp` r `comp` point a
@@ -602,19 +589,19 @@ divisionF f g = conv g `comp` f
602-- Taxonomy of binary relations
589-- Taxonomy of binary relations
604-- | A 'Relation' @r@ is 'simple' 'iff' @'coreflexive' ('img' r)@
591-- | A 'Relation' @r@ is 'simple' 'iff' @'coreflexive' ('img' r)@
605−simple :: (KnownNat (I.Count (I.Normalize b)), I.FromLists Boolean (I.Normalize b) (I.Normalize b)) => Relation a b -> Bool
592+simple :: (CountableN b, FromListsN b b) => Relation a b -> Bool
606simple = coreflexive . img
593simple = coreflexive . img
608-- | A 'Relation' @r@ is 'injective' 'iff' @'coreflexive' ('ker' r)@
595-- | A 'Relation' @r@ is 'injective' 'iff' @'coreflexive' ('ker' r)@
609−injective :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a b -> Bool
596+injective :: (CountableN a, FromListsN a a) => Relation a b -> Bool
610injective = coreflexive . ker
597injective = coreflexive . ker
612-- | A 'Relation' @r@ is 'entire' 'iff' @'reflexive' ('ker' r)@
599-- | A 'Relation' @r@ is 'entire' 'iff' @'reflexive' ('ker' r)@
613−entire :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a b -> Bool
600+entire :: (CountableN a, FromListsN a a) => Relation a b -> Bool
614entire = reflexive . ker
601entire = reflexive . ker
616-- | A 'Relation' @r@ is 'surjective' 'iff' @'reflexive' ('img' r)@
603-- | A 'Relation' @r@ is 'surjective' 'iff' @'reflexive' ('img' r)@
617−surjective :: (KnownNat (I.Count (I.Normalize b)), I.FromLists Boolean (I.Normalize b) (I.Normalize b)) => Relation a b -> Bool
604+surjective :: (CountableN b, FromListsN b b) => Relation a b -> Bool
618surjective = reflexive . img
605surjective = reflexive . img
620-- | A 'Relation' @r@ is a 'function' 'iff' @'simple' r && 'entire' r@
607-- | A 'Relation' @r@ is a 'function' 'iff' @'simple' r && 'entire' r@
@@ -627,68 +614,63 @@ surjective = reflexive . img
627-- r ``comp`` f ``sse`` s == r ``sse`` s ``comp`` f
614-- r ``comp`` f ``sse`` s == r ``sse`` s ``comp`` f
629function ::
616function ::
630− ( KnownNat (I.Count (I.Normalize b)),
617+ ( CountableDimensionsN a b,
631− KnownNat (I.Count (I.Normalize a)),
618+ FromListsN a a,
632− I.FromLists Boolean (I.Normalize a) (I.Normalize a),
619+ FromListsN b b
633− I.FromLists Boolean (I.Normalize b) (I.Normalize b)
635 => Relation a b -> Bool
621 => Relation a b -> Bool
636function r = simple r && entire r
622function r = simple r && entire r
638-- | A 'Relation' @r@ is a 'representation' 'iff' @'injective' r && 'entire' r@
624-- | A 'Relation' @r@ is a 'representation' 'iff' @'injective' r && 'entire' r@
639representation ::
625representation ::
640− ( KnownNat (I.Count (I.Normalize a)),
626+ ( CountableN a,
641− I.FromLists Boolean (I.Normalize a) (I.Normalize a)
627+ FromListsN a a
643 => Relation a b -> Bool
629 => Relation a b -> Bool
644representation r = injective r && entire r
630representation r = injective r && entire r
646-- | A 'Relation' @r@ is an 'abstraction' 'iff' @'surjective' r && 'simple' r@
632-- | A 'Relation' @r@ is an 'abstraction' 'iff' @'surjective' r && 'simple' r@
647abstraction ::
633abstraction ::
648− ( KnownNat (I.Count (I.Normalize b)),
634+ ( CountableN b,
649− I.FromLists Boolean (I.Normalize b) (I.Normalize b)
635+ FromListsN b b
651 => Relation a b -> Bool
637 => Relation a b -> Bool
652abstraction r = surjective r && simple r
638abstraction r = surjective r && simple r
654-- | A 'Relation' @r@ is a 'surjection' 'iff' @'function' r && 'abstraction' r@
640-- | A 'Relation' @r@ is a 'surjection' 'iff' @'function' r && 'abstraction' r@
655surjection ::
641surjection ::
656− ( KnownNat (I.Count (I.Normalize b)),
642+ ( CountableDimensionsN a b,
657− KnownNat (I.Count (I.Normalize a)),
643+ FromListsN a a,
658− I.FromLists Boolean (I.Normalize a) (I.Normalize a),
644+ FromListsN b b
659− I.FromLists Boolean (I.Normalize b) (I.Normalize b)
661 => Relation a b -> Bool
646 => Relation a b -> Bool
662surjection r = function r && abstraction r
647surjection r = function r && abstraction r
664-- | A 'Relation' @r@ is a 'injection' 'iff' @'function' r && 'representation' r@
649-- | A 'Relation' @r@ is a 'injection' 'iff' @'function' r && 'representation' r@
665injection ::
650injection ::
666− ( KnownNat (I.Count (I.Normalize b)),
651+ ( CountableDimensionsN a b,
667− KnownNat (I.Count (I.Normalize a)),
652+ FromListsN a a,
668− I.FromLists Boolean (I.Normalize a) (I.Normalize a),
653+ FromListsN b b
669− I.FromLists Boolean (I.Normalize b) (I.Normalize b)
671 => Relation a b -> Bool
655 => Relation a b -> Bool
672injection r = function r && representation r
656injection r = function r && representation r
674-- | A 'Relation' @r@ is an 'bijection' 'iff' @'injection' r && 'surjection' r@
658-- | A 'Relation' @r@ is an 'bijection' 'iff' @'injection' r && 'surjection' r@
675bijection ::
659bijection ::
676− ( KnownNat (I.Count (I.Normalize b)),
660+ ( CountableDimensionsN a b,
677− KnownNat (I.Count (I.Normalize a)),
661+ FromListsN b b,
678− I.FromLists Boolean (I.Normalize b) (I.Normalize b),
662+ FromListsN a a
679− I.FromLists Boolean (I.Normalize a) (I.Normalize a)
680 ) => Relation a b -> Bool
663 ) => Relation a b -> Bool
681bijection r = injection r && surjection r
664bijection r = injection r && surjection r
684-- Properties of relations
666-- Properties of relations
686-- | A 'Relation' @r@ is 'reflexive' 'iff' @'identity' ``sse`` r@
668-- | A 'Relation' @r@ is 'reflexive' 'iff' @'identity' ``sse`` r@
687−reflexive :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
669+reflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
688reflexive r = identity <= r
670reflexive r = identity <= r
690-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'identity'@
672-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'identity'@
691−coreflexive :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
673+coreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
692coreflexive r = r <= identity
674coreflexive r = r <= identity
694-- | A 'Relation' @r@ is 'transitive' 'iff' @(r ``comp`` r) ``sse`` r@
676-- | A 'Relation' @r@ is 'transitive' 'iff' @(r ``comp`` r) ``sse`` r@
@@ -700,35 +682,35 @@ symmetric :: Relation a a -> Bool
700symmetric r = r == conv r
682symmetric r = r == conv r
702-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'identity'@
684-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'identity'@
703−antiSymmetric :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
685+antiSymmetric :: (CountableN a, FromListsN a a) => Relation a a -> Bool
704antiSymmetric r = (r `intersection` conv r) `sse` identity
686antiSymmetric r = (r `intersection` conv r) `sse` identity
706-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'identity') == 'zeros'@
688-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'identity') == 'zeros'@
707−irreflexive :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
689+irreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
708irreflexive r = (r `intersection` identity) == zeros
690irreflexive r = (r `intersection` identity) == zeros
710-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
692-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
711−connected :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
693+connected :: (CountableN a, FromListsN a a) => Relation a a -> Bool
712connected r = (r `union` conv r) == ones
694connected r = (r `union` conv r) == ones
714-- | A 'Relation' @r@ is a 'preorder' 'iff' @'reflexive' r && 'transitive' r@
696-- | A 'Relation' @r@ is a 'preorder' 'iff' @'reflexive' r && 'transitive' r@
715−preorder :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
697+preorder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
716preorder r = reflexive r && transitive r
698preorder r = reflexive r && transitive r
718-- | A 'Relation' @r@ is a partial-order 'iff' @'antiSymmetric' r && 'preorder' r@
700-- | A 'Relation' @r@ is a partial-order 'iff' @'antiSymmetric' r && 'preorder' r@
719−partialOrder :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
701+partialOrder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
720partialOrder r = antiSymmetric r && preorder r
702partialOrder r = antiSymmetric r && preorder r
722-- | A 'Relation' @r@ is a linear-order 'iff' @'connected' r && 'partialOrder' r@
704-- | A 'Relation' @r@ is a linear-order 'iff' @'connected' r && 'partialOrder' r@
723−linearOrder :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
705+linearOrder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
724linearOrder r = connected r && partialOrder r
706linearOrder r = connected r && partialOrder r
726-- | A 'Relation' @r@ is an 'equivalence' 'iff' @'symmetric' r && 'preorder' r@
708-- | A 'Relation' @r@ is an 'equivalence' 'iff' @'symmetric' r && 'preorder' r@
727−equivalence :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
709+equivalence :: (CountableN a, FromListsN a a) => Relation a a -> Bool
728equivalence r = symmetric r && preorder r
710equivalence r = symmetric r && preorder r
730-- | A 'Relation' @r@ is a partial-equivalence 'iff' @'partialOrder' r && 'equivalence' r@
712-- | A 'Relation' @r@ is a partial-equivalence 'iff' @'partialOrder' r && 'equivalence' r@
731−partialEquivalence :: (KnownNat (I.Count (I.Normalize a)), I.FromLists Boolean (I.Normalize a) (I.Normalize a)) => Relation a a -> Bool
713+partialEquivalence :: (CountableN a, FromListsN a a) => Relation a a -> Bool
732partialEquivalence r = partialOrder r && equivalence r
714partialEquivalence r = partialOrder r && equivalence r
734-- | A 'Relation' @r@ is 'difunctional' or regular wherever
716-- | A 'Relation' @r@ is 'difunctional' or regular wherever
@@ -760,12 +742,11 @@ difunctional r = r `comp` conv r `comp` r == r
760-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
742-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
762splitR ::
744splitR ::
763− ( KnownNat (I.Count (I.Normalize a)),
745+ ( CountableDimensionsN a b,
764− KnownNat (I.Count (I.Normalize b)),
746+ CountableN (a, b),
765− KnownNat (I.Count (I.Normalize (a, b))),
747+ FromListsN (a, b) a,
766− I.FromLists Boolean (I.Normalize (a, b)) (I.Normalize a),
748+ FromListsN (a, b) b,
767− I.FromLists Boolean (I.Normalize (a, b)) (I.Normalize b),
749+ TrivialP a b
768− I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
770 => Relation c a -> Relation c b -> Relation c (a, b)
751 => Relation c a -> Relation c b -> Relation c (a, b)
771splitR (R f) (R g) = R (I.khatri f g)
752splitR (R f) (R g) = R (I.khatri f g)
@@ -777,11 +758,10 @@ splitR (R f) (R g) = R (I.khatri f g)
779 forall a b .
760 forall a b .
780− ( KnownNat (I.Count (I.Normalize a)),
761+ ( CountableDimensionsN a b,
781− KnownNat (I.Count (I.Normalize b)),
762+ CountableN (a, b),
782− KnownNat (I.Count (I.Normalize (a, b))),
763+ FromListsN (a, b) a,
783− I.FromLists Boolean (I.Normalize (a, b)) (I.Normalize a),
764+ TrivialP a b
784− I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
786 => Relation (a, b) a
766 => Relation (a, b) a
787p1 = R (I.kp1 @Boolean @(I.Normalize a) @(I.Normalize b))
767p1 = R (I.kp1 @Boolean @(I.Normalize a) @(I.Normalize b))
@@ -793,11 +773,10 @@ p1 = R (I.kp1 @Boolean @(I.Normalize a) @(I.Normalize b))
795 forall a b .
775 forall a b .
796− ( KnownNat (I.Count (I.Normalize b)),
776+ ( CountableDimensionsN a b,
797− KnownNat (I.Count (I.Normalize a)),
777+ CountableN (a, b),
798− KnownNat (I.Count (I.Normalize (a, b))),
778+ FromListsN (a, b) b,
799− I.FromLists Boolean (I.Normalize (a, b)) (I.Normalize b),
779+ TrivialP a b
800− I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
802 => Relation (a, b) b
781 => Relation (a, b) b
803p2 = R (I.kp2 @Boolean @(I.Normalize a) @(I.Normalize b))
782p2 = R (I.kp2 @Boolean @(I.Normalize a) @(I.Normalize b))
@@ -812,18 +791,15 @@ infixl 4 ><
812-- (r '><' s) ``comp`` (p '><' q) == (r ``comp`` p) '><' (s ``comp`` q)
791-- (r '><' s) ``comp`` (p '><' q) == (r ``comp`` p) '><' (s ``comp`` q)
815− ( KnownNat (I.Count (I.Normalize a)),
794+ ( CountableDimensionsN a b,
816− KnownNat (I.Count (I.Normalize b)),
795+ CountableDimensionsN c d,
817− KnownNat (I.Count (I.Normalize c)),
796+ CountableDimensionsN (a, c) (b, d),
818− KnownNat (I.Count (I.Normalize d)),
797+ FromListsN (a, c) a,
819− KnownNat (I.Count (I.Normalize (a, c))),
798+ FromListsN (a, c) c,
820− KnownNat (I.Count (I.Normalize (b, d))),
799+ FromListsN (b, d) b,
821− I.FromLists Boolean (I.Normalize (a, c)) (I.Normalize a),
800+ FromListsN (b, d) d,
822− I.FromLists Boolean (I.Normalize (a, c)) (I.Normalize c),
801+ TrivialP a c,
823− I.FromLists Boolean (I.Normalize (b, d)) (I.Normalize b),
802+ TrivialP b d
824− I.FromLists Boolean (I.Normalize (b, d)) (I.Normalize d),
825− I.Normalize (a, c) ~ I.Normalize (I.Normalize a, I.Normalize c),
826− I.Normalize (b, d) ~ I.Normalize (I.Normalize b, I.Normalize d)
828 => Relation a b -> Relation c d -> Relation (a, c) (b, d)
804 => Relation a b -> Relation c d -> Relation (a, c) (b, d)
829(><) (R a) (R b) = R ((I.><) a b)
805(><) (R a) (R b) = R ((I.><) a b)
@@ -845,7 +821,7 @@ infixl 4 ><
846-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
822-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
848−eitherR :: (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b))
824+eitherR :: (TrivialE a b)
849 => Relation a c -> Relation b c -> Relation (Either a b) c
825 => Relation a c -> Relation b c -> Relation (Either a b) c
850eitherR = junc
826eitherR = junc
@@ -856,11 +832,10 @@ eitherR = junc
856-- 'i1' ``comp`` 'i2' = 'zeros'
832-- 'i1' ``comp`` 'i2' = 'zeros'
859− ( KnownNat (I.Count (I.Normalize b)),
835+ ( CountableDimensionsN a b,
860− KnownNat (I.Count (I.Normalize a)),
836+ FromListsN b a,
861− I.FromLists Boolean (I.Normalize b) (I.Normalize a),
837+ FromListsN a a,
862− I.FromLists Boolean (I.Normalize a) (I.Normalize a),
838+ TrivialE a b
863− I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)
865 => Relation a (Either a b)
840 => Relation a (Either a b)
866i1 = R I.i1
841i1 = R I.i1
@@ -872,11 +847,10 @@ i1 = R I.i1
872-- 'i1' ``comp`` 'i2' = 'zeros'
847-- 'i1' ``comp`` 'i2' = 'zeros'
875− ( KnownNat (I.Count (I.Normalize b)),
850+ ( CountableDimensionsN a b,
876− KnownNat (I.Count (I.Normalize a)),
851+ FromListsN a b,
877− I.FromLists Boolean (I.Normalize a) (I.Normalize b),
852+ FromListsN b b,
878− I.FromLists Boolean (I.Normalize b) (I.Normalize b),
853+ TrivialE a b
879− I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)
881 => Relation b (Either a b)
855 => Relation b (Either a b)
882i2 = R I.i2
856i2 = R I.i2
@@ -889,14 +863,13 @@ infixl 5 -|-
889-- r '-|-' s == 'eitherR' ('i1' ``comp`` r) ('i2' ``comp`` s)
863-- r '-|-' s == 'eitherR' ('i1' ``comp`` r) ('i2' ``comp`` s)
892− ( KnownNat (I.Count (I.Normalize d)),
866+ ( CountableDimensionsN b d,
893− KnownNat (I.Count (I.Normalize b)),
867+ FromListsN b b,
894− I.FromLists Boolean (I.Normalize b) (I.Normalize b),
868+ FromListsN d b,
895− I.FromLists Boolean (I.Normalize d) (I.Normalize b),
869+ FromListsN b d,
896− I.FromLists Boolean (I.Normalize b) (I.Normalize d),
870+ FromListsN d d,
897− I.FromLists Boolean (I.Normalize d) (I.Normalize d),
871+ TrivialE a c,
898− I.Normalize (Either a c) ~ Either (I.Normalize a) (I.Normalize c),
872+ TrivialE b d
899− I.Normalize (Either b d) ~ Either (I.Normalize b) (I.Normalize d)
901 => Relation a b -> Relation c d -> Relation (Either a c) (Either b d)
874 => Relation a b -> Relation c d -> Relation (Either a c) (Either b d)
902(-|-) (R a) (R b) = R ((I.-|-) a b)
875(-|-) (R a) (R b) = R ((I.-|-) a b)
@@ -909,19 +882,17 @@ infixl 5 -|-
909-- 'trans'/'untrans';
882-- 'trans'/'untrans';
910-- more-over, where each particular attribute is placed (input/output) is irrelevant.
883-- more-over, where each particular attribute is placed (input/output) is irrelevant.
912− ( KnownNat (I.Count (I.Normalize (a, b))),
885+ ( CountableDimensionsN a b,
913− KnownNat (I.Count (I.Normalize a)),
886+ CountableN c,
914− KnownNat (I.Count (I.Normalize (c, b))),
887+ CountableDimensionsN (a, b) (c, b),
915− KnownNat (I.Count (I.Normalize b)),
888+ FromListsN (c, b) c,
916− KnownNat (I.Count (I.Normalize c)),
889+ FromListsN (c, b) b,
917− I.FromLists Boolean (I.Normalize (c, b)) (I.FromNat (I.Count c)),
890+ FromListsN (a, b) a,
918− I.FromLists Boolean (I.Normalize (c, b)) (I.FromNat (I.Count b)),
891+ FromListsN (a, b) b,
919− I.FromLists Boolean (I.Normalize (a, b)) (I.FromNat (I.Count b)),
892+ Trivial (a, b),
920− I.FromLists Boolean (I.Normalize (a, b)) (I.FromNat (I.Count a)),
893+ Trivial (c, b),
921− I.Normalize (a, b) ~ I.Normalize (I.Normalize (a, b)),
894+ TrivialP2 a b,
922− I.Normalize (c, b) ~ I.Normalize (I.Normalize (c, b)),
895+ TrivialP2 c b
923− I.Normalize (I.Normalize a, I.Normalize b) ~ I.FromNat (I.Count (I.Normalize (a, b))),
924− I.Normalize (I.Normalize c, I.Normalize b) ~ I.FromNat (I.Count (I.Normalize (c, b)))
926 => Relation (a, b) c -> Relation a (c, b)
897 => Relation (a, b) c -> Relation a (c, b)
927trans r = splitR r p2 `comp` conv p1
898trans r = splitR r p2 `comp` conv p1
@@ -932,19 +903,17 @@ trans r = splitR r p2 `comp` conv p1
932-- 'trans'/'untrans';
903-- 'trans'/'untrans';
933-- more-over, where each particular attribute is placed (input/output) is irrelevant.
904-- more-over, where each particular attribute is placed (input/output) is irrelevant.
934untrans ::
905untrans ::
935− ( KnownNat (I.Count (I.Normalize (a, b))),
906+ ( CountableDimensionsN a b,
936− KnownNat (I.Count (I.Normalize a)),
907+ CountableN c,
937− KnownNat (I.Count (I.Normalize (c, b))),
908+ CountableDimensionsN (a, b) (c, b),
938− KnownNat (I.Count (I.Normalize b)),
909+ FromListsN (c, b) c,
939− KnownNat (I.Count (I.Normalize c)),
910+ FromListsN (c, b) b,
940− I.FromLists Boolean (I.Normalize (c, b)) (I.FromNat (I.Count c)),
911+ FromListsN (a, b) b,
941− I.FromLists Boolean (I.Normalize (c, b)) (I.FromNat (I.Count b)),
912+ FromListsN (a, b) a,
942− I.FromLists Boolean (I.Normalize (a, b)) (I.FromNat (I.Count b)),
913+ Trivial (a, b),
943− I.FromLists Boolean (I.Normalize (a, b)) (I.FromNat (I.Count a)),
914+ Trivial (c, b),
944− I.Normalize (a, b) ~ I.Normalize (I.Normalize (a, b)),
915+ TrivialP2 a b,
945− I.Normalize (c, b) ~ I.Normalize (I.Normalize (c, b)),
916+ TrivialP2 c b
946− I.Normalize (I.Normalize a, I.Normalize b) ~ I.FromNat (I.Count (I.Normalize (a, b))),
947− I.Normalize (I.Normalize c, I.Normalize b) ~ I.FromNat (I.Count (I.Normalize (c, b)))
949 => Relation a (c, b) -> Relation (a, b) c
918 => Relation a (c, b) -> Relation (a, b) c
950untrans s = p1 `comp` conv (splitR (conv s) p2)
919untrans s = p1 `comp` conv (splitR (conv s) p2)
@@ -962,9 +931,9 @@ untrans s = p1 `comp` conv (splitR (conv s) p2)
963 ( Bounded a,
932 ( Bounded a,
965− KnownNat (I.Count (I.Normalize a)),
934+ CountableN a,
966− I.FromLists Boolean (I.Normalize a) (I.Normalize a),
935+ FromListsN a a,
967− I.FromLists Boolean (I.Normalize Bool) (I.Normalize a)
936+ FromListsN Bool a
969 => (a -> Bool) -> Relation a a
938 => (a -> Bool) -> Relation a a
970predR p = identity `intersection` divisionF (fromF' (const True)) (fromF' p)
939predR p = identity `intersection` divisionF (fromF' (const True)) (fromF' p)
@@ -978,8 +947,8 @@ predR p = identity `intersection` divisionF (fromF' (const True)) (fromF' p)
978-- 'equalizer' ('point' True) ('point' False) == 'zeros'
947-- 'equalizer' ('point' True) ('point' False) == 'zeros'
980equalizer ::
949equalizer ::
981− ( KnownNat (I.Count (I.Normalize a)),
950+ ( CountableN a,
982− I.FromLists Boolean (I.Normalize a) (I.Normalize a)
951+ FromListsN a a
984 => Relation a b -> Relation a b -> Relation a a
953 => Relation a b -> Relation a b -> Relation a a
985equalizer f g = identity `intersection` divisionF f g
954equalizer f g = identity `intersection` divisionF f g
@@ -992,10 +961,10 @@ equalizer f g = identity `intersection` divisionF f g
993 ( Bounded b,
962 ( Bounded b,
995− KnownNat (I.Count (I.Normalize b)),
964+ CountableN b,
996− I.FromLists Boolean (I.Normalize b) (I.Normalize b),
965+ FromListsN b b,
997− I.FromLists Boolean (I.Normalize Bool) (I.Normalize b),
966+ FromListsN Bool b,
998− I.Normalize (Either b b) ~ Either (I.Normalize b) (I.Normalize b)
967+ TrivialE b b
999 ) => (b -> Bool) -> Relation b (Either b b)
968 ) => (b -> Bool) -> Relation b (Either b b)
1000guard p = conv (eitherR (predR p) (predR (not . p)))
969guard p = conv (eitherR (predR p) (predR (not . p)))
@@ -1003,10 +972,10 @@ guard p = conv (eitherR (predR p) (predR (not . p)))
1004 ( Bounded b,
973 ( Bounded b,
1006− KnownNat (I.Count (I.Normalize b)),
975+ CountableN b,
1007− I.FromLists Boolean (I.Normalize b) (I.Normalize b),
976+ FromListsN b b,
1008− I.FromLists Boolean (I.Normalize Bool) (I.Normalize b),
977+ FromListsN Bool b,
1009− I.Normalize (Either b b) ~ Either (I.Normalize b) (I.Normalize b)
978+ TrivialE b b
1011 => (b -> Bool) -> Relation b c -> Relation b c -> Relation b c
980 => (b -> Bool) -> Relation b c -> Relation b c -> Relation b c
1012cond p r s = eitherR r s `comp` guard p
981cond p r s = eitherR r s `comp` guard p
@@ -1016,8 +985,8 @@ cond p r s = eitherR r s `comp` guard p
1016-- For injective relations, 'domain' and 'ker'nel coincide,
985-- For injective relations, 'domain' and 'ker'nel coincide,
1017-- since @'ker' r ``sse`` 'identity'@ in such situations.
986-- since @'ker' r ``sse`` 'identity'@ in such situations.
1018domain ::
987domain ::
1019− ( KnownNat (I.Count (I.Normalize a)),
988+ ( CountableN a,
1020− I.FromLists Boolean (I.Normalize a) (I.Normalize a)
989+ FromListsN a a
1021 ) => Relation a b -> Relation a a
990 ) => Relation a b -> Relation a a
1022domain r = ker r `intersection` identity
991domain r = ker r `intersection` identity
@@ -1026,17 +995,17 @@ domain r = ker r `intersection` identity
1026-- For functions, 'range' and 'img' (image) coincide,
995-- For functions, 'range' and 'img' (image) coincide,
1027-- since @'img' f ``sse`` 'identity'@ for any @f@.
996-- since @'img' f ``sse`` 'identity'@ for any @f@.
1029− ( I.FromLists Boolean (I.Normalize b) (I.Normalize b),
998+ ( CountableN b,
1030− KnownNat (I.Count (I.Normalize b))
999+ FromListsN b b
1031 ) => Relation a b -> Relation b b
1000 ) => Relation a b -> Relation b b
1032range r = img r `intersection` identity
1001range r = img r `intersection` identity
1034-- Relation pretty print
1003-- Relation pretty print
1036-- | Relation pretty printing
1005-- | Relation pretty printing
1037−pretty :: (KnownNat (I.Count (I.Normalize a))) => Relation a b -> String
1006+pretty :: (CountableN a) => Relation a b -> String
1038pretty (R a) = I.pretty a
1007pretty (R a) = I.pretty a
1040-- | Relation pretty printing
1009-- | Relation pretty printing
1041−prettyPrint :: (KnownNat (I.Count (I.Normalize a))) => Relation a b -> IO ()
1010+prettyPrint :: (CountableN a) => Relation a b -> IO ()
1042prettyPrint (R a) = I.prettyPrint a
1011prettyPrint (R a) = I.prettyPrint a