@@ -27,7 +27,7 @@
28module LAoP.Relation.Internal
28module LAoP.Relation.Internal
29 ( -- | This definition makes use of the fact that 'Void' is
29 ( -- | This definition makes use of the fact that 'Void' is
30− -- isomorphic to 0 and '()' to 1 and captures matrix
30+ -- isomorphic to 0 and 'One' to 1 and captures matrix
31 -- dimensions as stacks of 'Either's.
31 -- dimensions as stacks of 'Either's.
33 -- There exists two type families that make it easier to write
33 -- There exists two type families that make it easier to write
@@ -60,10 +60,13 @@ module LAoP.Relation.Internal
63 relationBuilder,
65 relationBuilder,
68 -- * Relational operations
71 -- * Relational operations
@@ -132,6 +135,9 @@ module LAoP.Relation.Internal
138+ -- ** Relational application
135 -- * Matrix printing
141 -- * Matrix printing
137 prettyPrint
143 prettyPrint
@@ -140,7 +146,7 @@ module LAoP.Relation.Internal
141import Data.Void
147import Data.Void
142import qualified LAoP.Matrix.Internal as I
148import qualified LAoP.Matrix.Internal as I
143−import LAoP.Utils
149+import LAoP.Utils.Internal
144import Control.DeepSeq
150import Control.DeepSeq
145import qualified Control.Category as C
151import qualified Control.Category as C
146import Data.Bool
152import Data.Bool
@@ -174,6 +180,8 @@ instance Num (Relation a b) where
174 -- | Matrix multiplication becomes Boolean matrix conjunction
180 -- | Matrix multiplication becomes Boolean matrix conjunction
175 (R a) * (R b) = R (I.andM a b)
181 (R a) * (R b) = R (I.andM a b)
183+ negate (R a) = R (I.negateM a)
177-- Type alias
185-- Type alias
178type Zero = Void
186type Zero = Void
179type One = ()
187type One = ()
@@ -289,6 +297,30 @@ toLists (R m) = I.toLists m
289toList :: Relation a b -> [Boolean]
297toList :: Relation a b -> [Boolean]
290toList (R m) = I.toList m
298toList (R m) = I.toList m
300+-- | Converts a well typed @'Relation'@ to @'Bool'@.
301+toBool :: Relation One One -> Bool
302+toBool r = case toList r of
314+ KnownNat (I.Count (I.Normalize a)),
315+ KnownNat (I.Count (I.Normalize b)),
316+ I.FromLists Boolean (I.Normalize a) One,
317+ I.FromLists Boolean (I.Normalize b) One
319+ => Relation a b -> (a -> [b])
321+ let lb = [minBound .. maxBound]
322+ in [ b | b <- lb, toBool (pointAp a b r) ]
292-- Zeros Matrix
324-- Zeros Matrix
294-- | The zero matrix. A matrix wholly filled with zeros.
326-- | The zero matrix. A matrix wholly filled with zeros.
@@ -311,11 +343,20 @@ ones = relationBuilder (const (nat 1))
312-- | The T (Top) row vector matrix.
344-- | The T (Top) row vector matrix.
315− (I.FromLists Boolean (I.Normalize a) (), KnownNat (I.Count (I.Normalize a))) =>
346+ (I.FromLists Boolean (I.Normalize a) One, KnownNat (I.Count (I.Normalize a))) =>
316 Relation a One
347 Relation a One
317bang = ones
348bang = ones
350+-- | Point constant relation
355+ KnownNat (I.Count (I.Normalize a)),
356+ I.FromLists Boolean (I.Normalize a) One
357+ ) => a -> Relation One a
358+point a = fromF' (const a)
319-- Identity Matrix
360-- Identity Matrix
321-- | Identity matrix
362-- | Identity matrix
@@ -325,9 +366,24 @@ identity ::
325identity = relationBuilder (bool (nat 0) (nat 1) . uncurry (==))
366identity = relationBuilder (bool (nat 0) (nat 1) . uncurry (==))
327-- | Relational composition
368-- | Relational composition
328−comp :: forall a b c . Relation b c -> Relation a b -> Relation a c
369+comp :: Relation b c -> Relation a b -> Relation a c
329comp (R a) (R b) = R (I.compRel a b)
370comp (R a) (R b) = R (I.compRel a b)
372+-- | Relational application
380+ KnownNat (I.Count (I.Normalize a)),
381+ KnownNat (I.Count (I.Normalize b)),
382+ I.FromLists Boolean (I.Normalize a) One,
383+ I.FromLists Boolean (I.Normalize b) One
384+ ) => a -> b -> Relation a b -> Relation One One
385+pointAp a b r = conv (point b) `comp` r `comp` point a
331-- | Relational converse
387-- | Relational converse
332conv :: Relation a b -> Relation b a
388conv :: Relation a b -> Relation b a
333conv (R a) = R (I.tr a)
389conv (R a) = R (I.tr a)
@@ -337,12 +393,12 @@ sse :: Relation a b -> Relation a b -> Bool
337sse a b = a <= b
393sse a b = a <= b
339-- | Relational implication (the same as @'sse'@)
395-- | Relational implication (the same as @'sse'@)
340−implies :: Relation a b -> Relation a b -> Bool
396+implies :: Relation a b -> Relation a b -> Relation a b
341−implies = sse
397+implies r s = negate r `union` s
343-- | Relational bi-implication
399-- | Relational bi-implication
344iff :: Relation a b -> Relation a b -> Bool
400iff :: Relation a b -> Relation a b -> Bool
345−iff r s = (r `implies` s) && (s `implies` r)
401+iff r s = r == s
347-- | Relational intersection
403-- | Relational intersection