on master help sign in
Merge pull request #6 from bolt12/renaming

Renaming

commitb14d465b0b2a9b24d05e2eb2a380cfd4afda0bbd authorArmando Santos <armandoifsantos@gmail.com> dateSat 22 Feb 2020, 10:52 (7 years ago) parentecee26df888838
10 files changed+488−401
app/Benchmarks.hs+5−5 app/Examples.hs+14−14 app/Quantum.hs+29 app/Spreadsheets.hs+1−2 src/LAoP/Dist/Internal.hs+2−2 src/LAoP/Matrix/Internal.hs+223−202 src/LAoP/Matrix/Nat.hs+65−45 src/LAoP/Matrix/Type.hs+81−63 src/LAoP/Relation.hs+5−5 src/LAoP/Relation/Internal.hs+63−63
app/Benchmarks.hs+5−5
@@ -28,16 +28,16 @@ selectM2 ::
2828 T.FromListsN e b b,
2929 CountableN b
3030 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
31−selectM2 m y = junc y identity `comp` m
31+selectM2 m y = join y iden `comp` m
3232
3333selectD2 ::
3434 ( FromListsN b b,
3535 CountableN b
3636 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
37−selectD2 (D d) m = D (junc m identity `comp` d)
37+selectD2 (D d) m = D (join m iden `comp` d)
3838
39−-- Composition with identity
40−compId m = comp identity m
39+-- Composition with iden
40+compId m = comp iden m
4141
4242randomDist :: forall a . (Countable a, FromListsN () a) => Gen (Dist a)
4343randomDist = do
@@ -162,7 +162,7 @@ benchmark = defaultMain [
162162 , bench "NF - 200x200" $ nf (comp m31) m32
163163 ],
164164 bgroup "200x200 - RULES" [
165− bench "No rules - 200x200" $ nf (comp m21) identity
165+ bench "No rules - 200x200" $ nf (comp m21) iden
166166 , bench "Rules - 200x200" $ nf compId m21
167167 ] ],
168168 env setupEnv2 $ \ ~(m21, m40, dist, dist2, dl1, dl2) -> bgroup "Matrix vs List - `select`" [
app/Examples.hs+14−14
@@ -32,7 +32,7 @@ firstChoice :: Matrix Double () Outcome
3232firstChoice = col [1/3, 2/3]
3333
3434secondChoice :: Matrix Double Outcome Outcome
35−secondChoice = fromF' switch
35+secondChoice = fromF switch
3636
3737-- Dice sum
3838
@@ -41,7 +41,7 @@ type SS = Natural 1 6 -- Sample Space
4141sumSS :: SS -> SS -> Natural 2 12
4242sumSS = coerceNat (+)
4343
44−sumSSM = fromF' (uncurry sumSS)
44+sumSSM = fromF (uncurry sumSS)
4545
4646condition :: (Int, Int) -> Int -> Int
4747condition (fst, snd) thrd = if fst == snd
@@ -51,7 +51,7 @@ condition (fst, snd) thrd = if fst == snd
5151conditionSS :: (SS, SS) -> SS -> Natural 3 18
5252conditionSS = coerceNat2 condition
5353
54−conditionalThrows = fromF' (uncurry conditionSS) . khatri (khatri die die) die
54+conditionalThrows = fromF (uncurry conditionSS) . kr (kr die die) die
5555
5656die :: Matrix Double () SS
5757die = col $ map (const (1/6)) [nat @1 @6 1 .. nat 6]
@@ -75,15 +75,15 @@ sprinkler = fromLists [[0.6, 0.99], [0.4, 0.01]]
7575grass :: Matrix Double (S, R) G
7676grass = fromLists [[1, 0.2, 0.1, 0.01], [0, 0.8, 0.9, 0.99]]
7777
78−tag f = khatri f identity
78+tag f = kr f iden
7979
8080state g s r = tag g `comp` tag s `comp` r
8181
82−grass_wet :: Matrix Double (G, (S, R)) One
83−grass_wet = row [0,1] . kp1
82+grass_wet :: Matrix Double () (G, (S, R)) -> Matrix Double One One
83+grass_wet state = row [0,1] . fstM . state
8484
8585rainning :: Matrix Double (G, (S, R)) One
86−rainning = row [0,1] . kp2 . kp2
86+rainning = row [0,1] . sndM . sndM
8787
8888-- Alcuin Puzzle
8989
@@ -106,21 +106,21 @@ cross LeftB = RightB
106106cross RightB = LeftB
107107
108108crossR :: R.Relation Bank Bank
109−crossR = R.fromF' cross
109+crossR = R.fromF cross
110110
111111-- | Initial state, everyone in the left bank
112112locationLeft :: Being -> Bank
113113locationLeft _ = LeftB
114114
115115locationLeftR :: R.Relation Being Bank
116−locationLeftR = R.fromF' locationLeft
116+locationLeftR = R.fromF locationLeft
117117
118118-- | Initial state, everyone in the right bank
119119locationRight :: Being -> Bank
120120locationRight _ = RightB
121121
122122locationRightR :: R.Relation Being Bank
123−locationRightR = R.fromF' locationRight
123+locationRightR = R.fromF locationRight
124124
125125-- Properties
126126
@@ -137,7 +137,7 @@ inv :: R.Relation Being Bank -> Bool
137137inv w = (w `R.comp` canEat w) `R.sse` (w `R.comp` farmer)
138138 where
139139 farmer :: R.Relation Being Being
140− farmer = R.fromF' (const Farmer)
140+ farmer = R.fromF (const Farmer)
141141
142142-- Arbitrary state
143143bankState :: Being -> Bank -> Bool
@@ -156,13 +156,13 @@ exec = do
156156 putStrLn "Monty Hall Problem solution:"
157157 prettyPrint (secondChoice . firstChoice)
158158 putStrLn "\n Sum of dices probability:"
159− prettyPrint (sumSSM `comp` khatri die die)
159+ prettyPrint (sumSSM `comp` kr die die)
160160 putStrLn "\n Conditional dice throw:"
161161 prettyPrint conditionalThrows
162162 putStrLn "\n Checking that the last result is indeed a distribution: "
163− prettyPrint (bang . sumSSM . khatri die die)
163+ prettyPrint (bang . sumSSM . kr die die)
164164 putStrLn "\n Probability of grass being wet:"
165− prettyPrint (grass_wet . state grass sprinkler rain)
165+ prettyPrint (grass_wet (state grass sprinkler rain))
166166 putStrLn "\n Probability of rain:"
167167 prettyPrint (rainning . state grass sprinkler rain)
168168 putStrLn "\n Is the arbitrary state a valid state? (Alcuin Puzzle)"
app/Quantum.hsnew file+29
@@ -0,0 +1,29 @@
1+{-# LANGUAGE StandaloneDeriving #-}
2+module Quantum where
3+
4+import LAoP.Matrix.Type
5+import Data.Complex
6+
7+deriving instance Ord a => Ord (Complex a)
8+
9+xor :: (Bool, Bool) -> Bool
10+xor (False, b) = b
11+xor (True, b) = not b
12+
13+ker :: Num e => Matrix e a b -> Matrix e a a
14+ker m = tr m `comp` m
15+
16+cnot :: Matrix (Complex Double) (Bool, Bool) (Bool, Bool)
17+cnot = kr fstM (fromF xor)
18+
19+ccnot :: (Num e, Ord e) => Matrix e ((Bool, Bool), Bool) ((Bool, Bool), Bool)
20+ccnot = kr fstM (fromF f)
21+ where
22+ f = xor . (tp (uncurry (&&)) id)
23+ tp f g (a,b) = (f a, g b)
24+
25+had :: Matrix (Complex Double) Bool Bool
26+had = (1/sqrt 2) .| fromLists [[1, 1], [1, -1]]
27+
28+bell :: Matrix (Complex Double) (Bool, Bool) (Bool, Bool)
29+bell = cnot `comp` (had >< iden)
app/Spreadsheets.hs+1−2
@@ -4,7 +4,6 @@ module Spreadsheets
44 where
55
66import LAoP.Matrix.Type
7−import qualified LAoP.Matrix.Internal as I
87import GHC.Generics
98
109data Student = Student1 | Student2 | Student3 | Student4
@@ -35,7 +34,7 @@ xls :: Matrix Float Question One
3534 -> Matrix Float Question Student
3635 -> Matrix Float Student One
3736 -> Matrix Float (Either Question Results) (Either One Student)
38−xls w m t = junc (split w m) (split zeros r)
37+xls w m t = join (fork w m) (fork zeros r)
3938 where
4039 rExam = m `comp` tr w
4140 rTest = tr t
src/LAoP/Dist/Internal.hs+2−2
@@ -90,7 +90,7 @@ multD ::
9090 FromListsN (a, b) b,
9191 TrivialP a b
9292 ) => Dist a -> Dist b -> Dist (a, b)
93−multD (D a) (D b) = D (khatri a b)
93+multD (D a) (D b) = D (kr a b)
9494
9595-- | Selective instance function
9696selectD ::
@@ -118,7 +118,7 @@ branchD ::
118118 => Dist (Either a b) -> Matrix Prob a c -> Matrix Prob b c -> Dist c
119119branchD x l r = f x `selectD` g l `selectD` r
120120 where
121− f (D m) = D (split (tr i1) (i1 `comp` tr i2) `comp` m)
121+ f (D m) = D (fork (tr i1) (i1 `comp` tr i2) `comp` m)
122122 g m = i2 `comp` m
123123
124124-- | Branch on a Boolean value, skipping unnecessary computations.
src/LAoP/Matrix/Internal.hs+223−202
@@ -52,8 +52,8 @@ module LAoP.Matrix.Internal
5252 -- * Primitives
5353 empty,
5454 one,
55− junc,
56− split,
55+ join,
56+ fork,
5757
5858 -- * Auxiliary type families
5959 FromNat,
@@ -81,6 +81,10 @@ module LAoP.Matrix.Internal
8181 -- ** Matrix Transposition
8282 tr,
8383
84+ -- ** Scalar multiplication/division of matrices
85+ (.|),
86+ (./),
87+
8488 -- ** Selective operator
8589 select,
8690 branch,
@@ -89,19 +93,19 @@ module LAoP.Matrix.Internal
8993 cond,
9094
9195 -- ** Matrix "abiding"
92− abideJS,
93− abideSJ,
96+ abideJF,
97+ abideFJ,
9498
9599 -- ** Zip matrices
96100 zipWithM,
97101
98102 -- * Biproduct approach
99− -- ** Split
103+ -- ** Fork
100104 (===),
101105 -- *** Projections
102106 p1,
103107 p2,
104− -- ** Junc
108+ -- ** Join
105109 (|||),
106110 -- *** Injections
107111 i1,
@@ -115,11 +119,11 @@ module LAoP.Matrix.Internal
115119 -- | Note that given the restrictions imposed it is not possible to
116120 -- implement the standard type classes present in standard Haskell.
117121 -- *** Matrix pairing projections
118− kp1,
119− kp2,
122+ fstM,
123+ sndM,
120124
121125 -- *** Matrix pairing
122− khatri,
126+ kr,
123127
124128 -- * Matrix composition and lifting
125129
@@ -127,10 +131,10 @@ module LAoP.Matrix.Internal
127131
128132 -- | Note that given the restrictions imposed it is not possible to
129133 -- implement the standard type classes present in standard Haskell.
130− identity,
134+ iden,
131135 comp,
132− fromF,
133136 fromF',
137+ fromF,
134138
135139 -- * Matrix printing
136140 pretty,
@@ -171,8 +175,8 @@ import Prelude hiding ((.))
171175data Matrix e cols rows where
172176 Empty :: Matrix e Void Void
173177 One :: e -> Matrix e () ()
174− Junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
175− Split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
178+ Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
179+ Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
176180
177181deriving instance (Show e) => Show (Matrix e cols rows)
178182
@@ -226,24 +230,24 @@ type Trivial a = FromNat (Count a) ~ a
226230-- implementation is 'undefined'. However 'comp' can be and this partial
227231-- class implementation exists just to make the code more readable.
228232--
229−-- Please use 'identity' instead.
233+-- Please use 'iden' instead.
230234instance (Num e) => Category (Matrix e) where
231− id = undefined
235+ id = undefined
232236 (.) = comp
233237
234238instance NFData e => NFData (Matrix e cols rows) where
235− rnf Empty = ()
236− rnf (One e) = rnf e
237− rnf (Junc a b) = rnf a `seq` rnf b
238− rnf (Split a b) = rnf a `seq` rnf b
239+ rnf Empty = ()
240+ rnf (One e) = rnf e
241+ rnf (Join a b) = rnf a `seq` rnf b
242+ rnf (Fork a b) = rnf a `seq` rnf b
239243
240244instance Eq e => Eq (Matrix e cols rows) where
241− Empty == Empty = True
242− (One a) == (One b) = a == b
243− (Junc a b) == (Junc c d) = a == c && b == d
244− (Split a b) == (Split c d) = a == c && b == d
245− x@(Split _ _) == y@(Junc _ _) = x == abideJS y
246− x@(Junc _ _) == y@(Split _ _) = abideJS x == y
245+ Empty == Empty = True
246+ (One a) == (One b) = a == b
247+ (Join a b) == (Join c d) = a == c && b == d
248+ (Fork a b) == (Fork c d) = a == c && b == d
249+ x@(Fork _ _) == y@(Join _ _) = x == abideJF y
250+ x@(Join _ _) == y@(Fork _ _) = abideJF x == y
247251
248252instance Num e => Num (Matrix e cols rows) where
249253
@@ -253,23 +257,23 @@ instance Num e => Num (Matrix e cols rows) where
253257
254258 a * b = zipWithM (*) a b
255259
256− abs Empty = Empty
257− abs (One a) = One (abs a)
258− abs (Junc a b) = Junc (abs a) (abs b)
259− abs (Split a b) = Split (abs a) (abs b)
260+ abs Empty = Empty
261+ abs (One a) = One (abs a)
262+ abs (Join a b) = Join (abs a) (abs b)
263+ abs (Fork a b) = Fork (abs a) (abs b)
260264
261− signum Empty = Empty
262− signum (One a) = One (signum a)
263− signum (Junc a b) = Junc (signum a) (signum b)
264− signum (Split a b) = Split (signum a) (signum b)
265+ signum Empty = Empty
266+ signum (One a) = One (signum a)
267+ signum (Join a b) = Join (signum a) (signum b)
268+ signum (Fork a b) = Fork (signum a) (signum b)
265269
266270instance Ord e => Ord (Matrix e cols rows) where
267− Empty <= Empty = True
268− (One a) <= (One b) = a <= b
269− (Junc a b) <= (Junc c d) = (a <= c) && (b <= d)
270− (Split a b) <= (Split c d) = (a <= c) && (b <= d)
271− x@(Split _ _) <= y@(Junc _ _) = x <= abideJS y
272− x@(Junc _ _) <= y@(Split _ _) = abideJS x <= y
271+ Empty <= Empty = True
272+ (One a) <= (One b) = a <= b
273+ (Join a b) <= (Join c d) = (a <= c) && (b <= d)
274+ (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
275+ x@(Fork _ _) <= y@(Join _ _) = x <= abideJF y
276+ x@(Join _ _) <= y@(Fork _ _) = abideJF x <= y
273277
274278-- Primitives
275279
@@ -281,25 +285,25 @@ empty = Empty
281285one :: e -> Matrix e () ()
282286one = One
283287
284−-- | Matrix 'Junc' constructor
285−junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
286−junc = Junc
288+-- | Matrix 'Join' constructor
289+join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
290+join = Join
287291
288292infixl 3 |||
289293
290−-- | Matrix 'Junc' constructor
294+-- | Matrix 'Join' constructor
291295(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
292−(|||) = Junc
296+(|||) = Join
293297
294−-- | Matrix 'Split' constructor
295−split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
296−split = Split
298+-- | Matrix 'Fork' constructor
299+fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
300+fork = Fork
297301
298302infixl 2 ===
299303
300−-- | Matrix 'Split' constructor
304+-- | Matrix 'Fork' constructor
301305(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
302−(===) = Split
306+(===) = Fork
303307
304308-- Construction
305309
@@ -321,32 +325,32 @@ instance {-# OVERLAPPING #-} FromLists e () () where
321325 fromLists _ = error "Wrong dimensions"
322326
323327instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
324− fromLists [h : t] = Junc (One h) (fromLists [t])
328+ fromLists [h : t] = Join (One h) (fromLists [t])
325329 fromLists _ = error "Wrong dimensions"
326330
327331instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
328− fromLists [l] =
332+ fromLists [l] =
329333 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
330− in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
334+ in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
331335 fromLists _ = error "Wrong dimensions"
332336
333337instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
334− fromLists ([h] : t) = Split (One h) (fromLists t)
338+ fromLists ([h] : t) = Fork (One h) (fromLists t)
335339 fromLists _ = error "Wrong dimensions"
336340
337341instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
338342 fromLists l@([_] : _) =
339343 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
340− in Split (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344+ in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
341345 fromLists _ = error "Wrong dimensions"
342346
343347instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) where
344348 fromLists l@(h : t) =
345349 let lh = length h
346350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
347− condition = all (== lh) (map length t)
351+ condition = all ((== lh) . length) t
348352 in if lh > 0 && condition
349− then Split (fromLists (take rowsC l)) (fromLists (drop rowsC l))
353+ then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
350354 else error "Not all rows have the same length"
351355
352356-- | Matrix builder function. Constructs a matrix provided with
@@ -355,9 +359,7 @@ matrixBuilder ::
355359 forall e cols rows.
356360 ( FromLists e cols rows,
357361 CountableDimensions cols rows
358− ) =>
359− ((Int, Int) -> e) ->
360− Matrix e cols rows
362+ ) => ((Int, Int) -> e) -> Matrix e cols rows
361363matrixBuilder f =
362364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
363365 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
@@ -376,7 +378,7 @@ row = fromLists . (: [])
376378--
377379-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
378380-- types @a@ or @b@ allows.
379−fromF ::
381+fromF' ::
380382 forall a b cols rows e.
381383 ( Liftable e a b,
382384 CountableDimensions cols rows,
@@ -384,7 +386,7 @@ fromF ::
384386 ) =>
385387 (a -> b) ->
386388 Matrix e cols rows
387−fromF f =
389+fromF' f =
388390 let minA = minBound @a
389391 maxA = maxBound @a
390392 minB = minBound @b
@@ -394,8 +396,8 @@ fromF f =
394396 elementsA = take ccols [minA .. maxA]
395397 elementsB = take rrows [minB .. maxB]
396398 combinations = (,) <$> elementsA <*> elementsB
397− combAp = map snd . sort . map (\(a, b) -> if f a == b
398− then ((fromEnum a, fromEnum b), 1)
399+ combAp = map snd . sort . map (\(a, b) -> if f a == b
400+ then ((fromEnum a, fromEnum b), 1)
399401 else ((fromEnum a, fromEnum b), 0)) $ combinations
400402 mList = buildList combAp rrows
401403 in tr $ fromLists mList
@@ -405,7 +407,7 @@ fromF f =
405407
406408-- | Lifts functions to matrices with dimensions matching @a@ and @b@
407409-- cardinality's.
408−fromF' ::
410+fromF ::
409411 forall a b e.
410412 ( Liftable e a b,
411413 CountableDimensionsN a b,
@@ -413,7 +415,7 @@ fromF' ::
413415 ) =>
414416 (a -> b) ->
415417 Matrix e (Normalize a) (Normalize b)
416−fromF' f =
418+fromF f =
417419 let minA = minBound @a
418420 maxA = maxBound @a
419421 minB = minBound @b
@@ -423,8 +425,8 @@ fromF' f =
423425 elementsA = take ccols [minA .. maxA]
424426 elementsB = take rrows [minB .. maxB]
425427 combinations = (,) <$> elementsA <*> elementsB
426− combAp = map snd . sort . map (\(a, b) -> if f a == b
427− then ((fromEnum a, fromEnum b), 1)
428+ combAp = map snd . sort . map (\(a, b) -> if f a == b
429+ then ((fromEnum a, fromEnum b), 1)
428430 else ((fromEnum a, fromEnum b), 0)) $ combinations
429431 mList = buildList combAp rrows
430432 in tr $ fromLists mList
@@ -438,8 +440,8 @@ fromF' f =
438440toLists :: Matrix e cols rows -> [[e]]
439441toLists Empty = []
440442toLists (One e) = [[e]]
441−toLists (Split l r) = toLists l ++ toLists r
442−toLists (Junc l r) = zipWith (++) (toLists l) (toLists r)
443+toLists (Fork l r) = toLists l ++ toLists r
444+toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
443445
444446-- | Converts a matrix to a list of elements.
445447toList :: Matrix e cols rows -> [e]
@@ -474,12 +476,12 @@ bang =
474476 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
475477 in fromLists [take c [1, 1 ..]]
476478
477−-- Identity Matrix
479+-- iden Matrix
478480
479−-- | Identity matrix.
480−identity :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
481−identity = matrixBuilder (bool 0 1 . uncurry (==))
482−{-# NOINLINE identity #-}
481+-- | iden matrix.
482+iden :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
483+iden = matrixBuilder (bool 0 1 . uncurry (==))
484+{-# NOINLINE iden #-}
483485
484486-- Matrix composition (MMM)
485487
@@ -488,32 +490,52 @@ identity = matrixBuilder (bool 0 1 . uncurry (==))
488490-- This definition takes advantage of divide-and-conquer and fusion laws
489491-- from LAoP.
490492comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
491−comp Empty Empty = Empty
492−comp (One a) (One b) = One (a * b)
493−comp (Junc a b) (Split c d) = comp a c + comp b d -- Divide-and-conquer law
494−comp (Split a b) c = Split (comp a c) (comp b c) -- Split fusion law
495−comp c (Junc a b) = Junc (comp c a) (comp c b) -- Junc fusion law
493+comp Empty Empty = Empty
494+comp (One a) (One b) = One (a * b)
495+comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
496+comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
497+comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
496498{-# NOINLINE comp #-}
497499{-# RULES
498− "comp/identity1" forall m. comp m identity = m ;
499− "comp/identity2" forall m. comp identity m = m
500+ "comp/iden1" forall m. comp m iden = m ;
501+ "comp/iden2" forall m. comp iden m = m
500502#-}
501503
504+-- Scalar multiplication of matrices
505+
506+infixl 7 .|
507+-- | Scalar multiplication of matrices.
508+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
509+(.|) _ Empty = Empty
510+(.|) e (One a) = One (e * a)
511+(.|) e (Join a b) = Join (e .| a) (e .| b)
512+(.|) e (Fork a b) = Fork (e .| a) (e .| b)
513+
514+-- Scalar division of matrices
515+
516+infixl 7 ./
517+-- | Scalar multiplication of matrices.
518+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
519+(./) Empty _ = Empty
520+(./) (One a) e = One (a / e)
521+(./) (Join a b) e = Join (a ./ e) (b ./ e)
522+(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
523+
502524-- Projections
503525
504526-- | Biproduct first component projection
505527p1 :: forall e m n. (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
506528p1 =
507− let iden = identity :: Matrix e m m
529+ let iden = iden :: Matrix e m m
508530 zero = zeros :: Matrix e n m
509− in junc iden zero
531+ in join iden zero
510532
511533-- | Biproduct second component projection
512534p2 :: forall e m n. (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
513535p2 =
514− let iden = identity :: Matrix e n n
536+ let iden = iden :: Matrix e n n
515537 zero = zeros :: Matrix e m n
516− in junc zero iden
538+ in join zero iden
517539
518540-- Injections
519541
@@ -564,71 +586,71 @@ infixl 5 -|-
564586 Matrix e n k ->
565587 Matrix e m j ->
566588 Matrix e (Either n m) (Either k j)
567−(-|-) a b = Junc (i1 . a) (i2 . b)
589+(-|-) a b = Join (i1 . a) (i2 . b)
568590
569591-- Khatri Rao Product and projections
570592
571593-- | Khatri Rao product first component projection matrix.
572−kp1 ::
594+fstM ::
573595 forall e m k .
574596 ( Num e,
575597 CountableDimensions k m,
576598 FromLists e (Normalize (m, k)) m,
577599 CountableN (m, k)
578600 ) => Matrix e (Normalize (m, k)) m
579−kp1 = matrixBuilder f
601+fstM = matrixBuilder f
580602 where
581603 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
582604 f (x, y)
583605 | y >= (x * offset) && y <= (x * offset + offset - 1) = 1
584− | otherwise = 0
606+ | otherwise = 0
585607
586608-- | Khatri Rao product second component projection matrix.
587−kp2 ::
609+sndM ::
588610 forall e m k .
589611 ( Num e,
590612 CountableDimensions k m,
591613 FromLists e (Normalize (m, k)) k,
592614 CountableN (m, k)
593615 ) => Matrix e (Normalize (m, k)) k
594−kp2 = matrixBuilder f
616+sndM = matrixBuilder f
595617 where
596618 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
597619 f (x, y)
598620 | x == y || mod (y - x) offset == 0 = 1
599− | otherwise = 0
621+ | otherwise = 0
600622
601623-- | Khatri Rao Matrix product also known as matrix pairing.
602624--
603625-- NOTE: That this is not a true categorical product, see for instance:
604626--
605627-- @
606−-- | kp1 . khatri a b == a
607−-- khatri a b ==> |
608−-- | kp2 . khatri a b == b
628+-- | fstM . kr a b == a
629+-- kr a b ==> |
630+-- | sndM . kr a b == b
609631-- @
610632--
611633-- __Emphasis__ on the implication symbol.
612−khatri ::
613− forall e cols a b.
634+kr ::
635+ forall e cols a b.
614636 ( Num e,
615637 CountableDimensions a b,
616638 CountableN (a, b),
617639 FromLists e (Normalize (a, b)) a,
618640 FromLists e (Normalize (a, b)) b
619641 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
620−khatri a b =
621− let kp1' = kp1 @e @a @b
622− kp2' = kp2 @e @a @b
623− in (tr kp1') . a * (tr kp2') . b
642+kr a b =
643+ let fstM' = fstM @e @a @b
644+ sndM' = sndM @e @a @b
645+ in tr fstM' . a * tr sndM' . b
624646
625647-- Product Bifunctor (Kronecker)
626648
627649infixl 4 ><
628650
629651-- | Matrix product functor also known as kronecker product
630−(><) ::
631− forall e m p n q.
652+(><) ::
653+ forall e m p n q.
632654 ( Num e,
633655 CountableDimensions m n,
634656 CountableDimensions p q,
@@ -640,57 +662,57 @@ infixl 4 ><
640662 )
641663 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
642664(><) a b =
643− let kp1' = kp1 @e @m @n
644− kp2' = kp2 @e @m @n
645− in khatri (a . kp1') (b . kp2')
665+ let fstM' = fstM @e @m @n
666+ sndM' = sndM @e @m @n
667+ in kr (a . fstM') (b . sndM')
646668
647−-- Matrix abide Junc Split
669+-- Matrix abide Join Fork
648670
649−-- | Matrix "abiding" followin the 'Junc'-'Split' abide law.
650−--
671+-- | Matrix "abiding" following the 'Join'-'Fork' exchange law.
672+--
651673-- Law:
652674--
653675-- @
654−-- 'Junc' ('Split' a c) ('Split' b d) == 'Split' ('Junc' a b) ('Junc' c d)
676+-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
655677-- @
656−abideJS :: Matrix e cols rows -> Matrix e cols rows
657−abideJS (Junc (Split a c) (Split b d)) = Split (Junc (abideJS a) (abideJS b)) (Junc (abideJS c) (abideJS d)) -- Junc-Split abide law
658−abideJS Empty = Empty
659−abideJS (One e) = One e
660−abideJS (Junc a b) = Junc (abideJS a) (abideJS b)
661−abideJS (Split a b) = Split (abideJS a) (abideJS b)
678+abideJF :: Matrix e cols rows -> Matrix e cols rows
679+abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
680+abideJF Empty = Empty
681+abideJF (One e) = One e
682+abideJF (Join a b) = Join (abideJF a) (abideJF b)
683+abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
662684
663−-- Matrix abide Split Junc
685+-- Matrix abide Fork Join
664686
665−-- | Matrix "abiding" followin the 'Split'-'Junc' abide law.
666−--
687+-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
688+--
667689-- @
668−-- 'Split' ('Junc' a b) ('Junc' c d) == 'Junc' ('Split' a c) ('Split' b d)
690+-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
669691-- @
670−abideSJ :: Matrix e cols rows -> Matrix e cols rows
671−abideSJ (Split (Junc a b) (Junc c d)) = Junc (Split (abideSJ a) (abideSJ c)) (Split (abideSJ b) (abideSJ d)) -- Split-Junc abide law
672−abideSJ Empty = Empty
673−abideSJ (One e) = One e
674−abideSJ (Junc a b) = Junc (abideSJ a) (abideSJ b)
675−abideSJ (Split a b) = Split (abideSJ a) (abideSJ b)
692+abideFJ :: Matrix e cols rows -> Matrix e cols rows
693+abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
694+abideFJ Empty = Empty
695+abideFJ (One e) = One e
696+abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
697+abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
676698
677699-- Matrix transposition
678700
679701-- | Matrix transposition.
680702tr :: Matrix e cols rows -> Matrix e rows cols
681−tr Empty = Empty
682−tr (One e) = One e
683−tr (Junc a b) = Split (tr a) (tr b)
684−tr (Split a b) = Junc (tr a) (tr b)
703+tr Empty = Empty
704+tr (One e) = One e
705+tr (Join a b) = Fork (tr a) (tr b)
706+tr (Fork a b) = Join (tr a) (tr b)
685707
686708-- Selective 'select' operator
687709
688710-- | Selective functors 'select' operator equivalent inspired by the
689711-- ArrowMonad solution presented in the paper.
690712select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
691−select (Split a b) y = y . a + b -- Divide-and-conquer law
692−select (Junc (Split a c) (Split b d)) y = junc (y . a + c) (y . b + d) -- Pattern matching + DnC law
693−select m y = junc y identity . m
713+select (Fork a b) y = y . a + b -- Divide-and-conquer law
714+select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
715+select m y = join y iden . m
694716
695717branch ::
696718 ( Num e,
@@ -711,7 +733,7 @@ branch x l r = f x `select` g l `select` r
711733 where
712734 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)
713735 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
714− f m = split (tr i1) (i1 . tr i2) . m
736+ f m = fork (tr i1) (i1 . tr i2) . m
715737 g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
716738 g m = i2 . m
717739
@@ -731,9 +753,9 @@ cond ::
731753 )
732754 =>
733755 (a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
734−cond p f g = junc f g . grd p
756+cond p f g = join f g . grd p
735757
736−grd ::
758+grd ::
737759 ( Trivial q,
738760 Countable q,
739761 FromLists e () q,
@@ -746,40 +768,40 @@ grd ::
746768 )
747769 =>
748770 (a -> Bool) -> Matrix e q (Either q q)
749−grd f = split (corr f) (corr (not . f))
771+grd f = fork (corr f) (corr (not . f))
750772
751−corr ::
752− forall e a q .
773+corr ::
774+ forall e a q .
753775 ( Trivial q,
754776 Countable q,
755777 FromLists e () q,
756778 FromLists e q (),
757779 FromLists e q q,
758780 Liftable e a Bool
759− )
781+ )
760782 => (a -> Bool) -> Matrix e q q
761−corr p = let f = fromF p :: Matrix e q ()
762− in khatri f (identity :: Matrix e q q)
783+corr p = let f = fromF' p :: Matrix e q ()
784+ in kr f (iden :: Matrix e q q)
763785
764786-- Pretty print
765787
766788prettyAux :: Show e => [[e]] -> [[e]] -> String
767789prettyAux [] _ = ""
768−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
790+prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
769791 where
770792 v = fmap show m
771793 widest = maximum $ fmap length v
772794 fill str = replicate (widest - length str - 2) ' ' ++ str
773−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
795+prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
774796 where
775− v = fmap show m
776− widest = maximum $ fmap length v
797+ v = fmap show m
798+ widest = maximum $ fmap length v
777799 fill str = replicate (widest - length str - 2) ' ' ++ str
778−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
800+prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
779801 prettyAux t l
780802 where
781− v = fmap show l
782− widest = maximum $ fmap length v
803+ v = fmap show l
804+ widest = maximum $ fmap length v
783805 fill str = replicate (widest - length str - 2) ' ' ++ str
784806
785807-- | Matrix pretty printer
@@ -791,16 +813,16 @@ pretty m = concat
791813 , "└ ", unwords (replicate (columns m) blank), " ┘"
792814 ]
793815 where
794− strings = map show (toList m)
795− widest = maximum $ map length strings
816+ strings = map show (toList m)
817+ widest = maximum $ map length strings
796818 fill str = replicate (widest - length str) ' ' ++ str
797− blank = fill ""
819+ blank = fill ""
798820 safeGet i j m
799821 | i > rows m || j > columns m || i < 1 || j < 1 = Nothing
800822 | otherwise = Just $ unsafeGet i j m (toList m)
801823 unsafeGet i j m l = l !! encode (columns m) (i,j)
802− encode m (i,j) = (i-1)*m + j - 1
803− getElem i j m =
824+ encode m (i,j) = (i-1)*m + j - 1
825+ getElem i j m =
804826 fromMaybe
805827 (error $
806828 "getElem: Trying to get the "
@@ -817,16 +839,16 @@ prettyPrint = putStrLn . pretty
817839
818840-- | Zip two matrices with a given binary function
819841zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
820−zipWithM f Empty Empty = Empty
821−zipWithM f (One a) (One b) = One (f a b)
822−zipWithM f (Junc a b) (Junc c d) = Junc (zipWithM f a c) (zipWithM f b d)
823−zipWithM f (Split a b) (Split c d) = Split (zipWithM f a c) (zipWithM f b d)
824−zipWithM f x@(Split _ _) y@(Junc _ _) = zipWithM f x (abideJS y)
825−zipWithM f x@(Junc _ _) y@(Split _ _) = zipWithM f (abideJS x) y
842+zipWithM _ Empty Empty = Empty
843+zipWithM f (One a) (One b) = One (f a b)
844+zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
845+zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
846+zipWithM f x@(Fork _ _) y@(Join _ _) = zipWithM f x (abideJF y)
847+zipWithM f x@(Join _ _) y@(Fork _ _) = zipWithM f (abideJF x) y
826848
827849-- Relational operators functions
828850
829−type Boolean = Natural 0 1
851+type Boolean = Natural 0 1
830852type Relation a b = Matrix Boolean a b
831853
832854-- | Helper conversion function
@@ -843,53 +865,52 @@ fromBool False = nat 0
843865-- | Relational negation
844866negateM :: Relation cols rows -> Relation cols rows
845867negateM Empty = Empty
846−negateM (One (Nat 0)) = One (Nat 1)
847−negateM (One (Nat 1)) = One (Nat 0)
848−negateM (Junc a b) = Junc (negateM a) (negateM b)
849−negateM (Split a b) = Split (negateM a) (negateM b)
868+negateM (One (Nat p)) = One (Nat (negate p))
869+negateM (Join a b) = Join (negateM a) (negateM b)
870+negateM (Fork a b) = Fork (negateM a) (negateM b)
850871
851872-- | Relational addition
852873orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
853−orM Empty Empty = Empty
854−orM (One a) (One b) = One (fromBool (toBool a || toBool b))
855−orM (Junc a b) (Junc c d) = Junc (orM a c) (orM b d)
856−orM (Split a b) (Split c d) = Split (orM a c) (orM b d)
857−orM x@(Split _ _) y@(Junc _ _) = orM x (abideJS y)
858−orM x@(Junc _ _) y@(Split _ _) = orM (abideJS x) y
874+orM Empty Empty = Empty
875+orM (One a) (One b) = One (fromBool (toBool a || toBool b))
876+orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
877+orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
878+orM x@(Fork _ _) y@(Join _ _) = orM x (abideJF y)
879+orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
859880
860881-- | Relational multiplication
861882andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
862−andM Empty Empty = Empty
863−andM (One a) (One b) = One (fromBool (toBool a && toBool b))
864−andM (Junc a b) (Junc c d) = Junc (andM a c) (andM b d)
865−andM (Split a b) (Split c d) = Split (andM a c) (andM b d)
866−andM x@(Split _ _) y@(Junc _ _) = andM x (abideJS y)
867−andM x@(Junc _ _) y@(Split _ _) = andM (abideJS x) y
883+andM Empty Empty = Empty
884+andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885+andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886+andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
887+andM x@(Fork _ _) y@(Join _ _) = andM x (abideJF y)
888+andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
868889
869890-- | Relational subtraction
870891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
871−subM Empty Empty = Empty
872−subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
873−subM (Junc a b) (Junc c d) = Junc (subM a c) (subM b d)
874−subM (Split a b) (Split c d) = Split (subM a c) (subM b d)
875−subM x@(Split _ _) y@(Junc _ _) = subM x (abideJS y)
876−subM x@(Junc _ _) y@(Split _ _) = subM (abideJS x) y
892+subM Empty Empty = Empty
893+subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
894+subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
895+subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
896+subM x@(Fork _ _) y@(Join _ _) = subM x (abideJF y)
897+subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
877898
878899-- | Matrix relational composition.
879900compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
880−compRel Empty Empty = Empty
881−compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
882−compRel (Junc a b) (Split c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
883−compRel (Split a b) c = Split (compRel a c) (compRel b c) -- Split fusion law
884−compRel c (Junc a b) = Junc (compRel c a) (compRel c b) -- Junc fusion law
901+compRel Empty Empty = Empty
902+compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
903+compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
904+compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
905+compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion law
885906
886907-- | Matrix relational right division
887908divR :: Relation b c -> Relation b a -> Relation a c
888909divR Empty Empty = Empty
889910divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
890−divR (Junc a b) (Junc c d) = andM (divR a c) (divR b d)
891−divR (Split a b) c = Split (divR a c) (divR b c)
892−divR c (Split a b) = Junc (divR c a) (divR c b)
911+divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
912+divR (Fork a b) c = Fork (divR a c) (divR b c)
913+divR c (Fork a b) = Join (divR c a) (divR c b)
893914
894915-- | Matrix relational left division
895916divL :: Relation c b -> Relation a b -> Relation a c
@@ -905,7 +926,7 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
905926--
906927-- NOTE: Be careful to not ask for a relation bigger than the cardinality of
907928-- types @a@ or @b@ allows.
908−fromFRel ::
929+fromFRel' ::
909930 forall a b cols rows.
910931 ( Liftable Boolean a b,
911932 CountableDimensions cols rows,
@@ -913,7 +934,7 @@ fromFRel ::
913934 ) =>
914935 (a -> b) ->
915936 Relation cols rows
916−fromFRel f =
937+fromFRel' f =
917938 let minA = minBound @a
918939 maxA = maxBound @a
919940 minB = minBound @b
@@ -923,8 +944,8 @@ fromFRel f =
923944 elementsA = take ccols [minA .. maxA]
924945 elementsB = take rrows [minB .. maxB]
925946 combinations = (,) <$> elementsA <*> elementsB
926− combAp = map snd . sort . map (\(a, b) -> if f a == b
927− then ((fromEnum a, fromEnum b), nat 1)
947+ combAp = map snd . sort . map (\(a, b) -> if f a == b
948+ then ((fromEnum a, fromEnum b), nat 1)
928949 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
929950 mList = buildList combAp rrows
930951 in tr $ fromLists mList
@@ -934,7 +955,7 @@ fromFRel f =
934955
935956-- | Lifts functions to relations with dimensions matching @a@ and @b@
936957-- cardinality's.
937−fromFRel' ::
958+fromFRel ::
938959 forall a b.
939960 ( Liftable Boolean a b,
940961 CountableDimensionsN a b,
@@ -942,7 +963,7 @@ fromFRel' ::
942963 ) =>
943964 (a -> b) ->
944965 Relation (Normalize a) (Normalize b)
945−fromFRel' f =
966+fromFRel f =
946967 let minA = minBound @a
947968 maxA = maxBound @a
948969 minB = minBound @b
@@ -952,8 +973,8 @@ fromFRel' f =
952973 elementsA = take ccols [minA .. maxA]
953974 elementsB = take rrows [minB .. maxB]
954975 combinations = (,) <$> elementsA <*> elementsB
955− combAp = map snd . sort . map (\(a, b) -> if f a == b
956− then ((fromEnum a, fromEnum b), nat 1)
976+ combAp = map snd . sort . map (\(a, b) -> if f a == b
977+ then ((fromEnum a, fromEnum b), nat 1)
957978 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
958979 mList = buildList combAp rrows
959980 in tr $ fromLists mList
@@ -962,7 +983,7 @@ fromFRel' f =
962983 buildList l r = take r l : buildList (drop r l) r
963984
964985-- | Lifts a relation function to a Boolean Matrix
965−toRel ::
986+toRel ::
966987 forall a b.
967988 ( Bounded a,
968989 Bounded b,
@@ -983,8 +1004,8 @@ toRel f =
9831004 elementsA = take ccols [minA .. maxA]
9841005 elementsB = take rrows [minB .. maxB]
9851006 combinations = (,) <$> elementsA <*> elementsB
986− combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
987− then ((fromEnum a, fromEnum b), nat 1)
1007+ combAp = map snd . sort . map (\(a, b) -> if uncurry f (a, b)
1008+ then ((fromEnum a, fromEnum b), nat 1)
9881009 else ((fromEnum a, fromEnum b), nat 0)) $ combinations
9891010 mList = buildList combAp rrows
9901011 in tr $ fromLists mList
src/LAoP/Matrix/Nat.hs+65−45
@@ -69,8 +69,8 @@ module LAoP.Matrix.Nat
6969 -- * Primitives
7070 empty,
7171 one,
72− junc,
73− split,
72+ join,
73+ fork,
7474
7575 -- * Auxiliary type families
7676 I.FromNat,
@@ -98,6 +98,10 @@ module LAoP.Matrix.Nat
9898 -- ** Matrix Transposition
9999 tr,
100100
101+ -- ** Scalar multiplication/division of matrices
102+ (.|),
103+ (./),
104+
101105 -- ** Selective operator
102106 select,
103107
@@ -105,19 +109,19 @@ module LAoP.Matrix.Nat
105109 cond,
106110
107111 -- ** Matrix "abiding"
108− abideJS,
109− abideSJ,
112+ abideJF,
113+ abideFJ,
110114
111115 -- ** Zip Matrices
112116 zipWithM,
113117
114118 -- * Biproduct approach
115− -- ** Split
119+ -- ** Fork
116120 (===),
117121 -- *** Projections
118122 p1,
119123 p2,
120− -- ** Junc
124+ -- ** Join
121125 (|||),
122126 -- *** Injections
123127 i1,
@@ -131,11 +135,11 @@ module LAoP.Matrix.Nat
131135 -- | Note that given the restrictions imposed it is not possible to
132136 -- implement the standard type classes present in standard Haskell.
133137 -- *** Matrix pairing projections
134− kp1,
135− kp2,
138+ fstM,
139+ sndM,
136140
137141 -- *** Matrix pairing
138− khatri,
142+ kr,
139143
140144 -- * Matrix composition and lifting
141145
@@ -143,10 +147,10 @@ module LAoP.Matrix.Nat
143147
144148 -- | Note that given the restrictions imposed it is not possible to
145149 -- implement the standard type classes present in standard Haskell.
146− identity,
150+ iden,
147151 comp,
148− fromF,
149152 fromF',
153+ fromF,
150154
151155 -- * Matrix printing
152156 pretty,
@@ -179,7 +183,7 @@ type TrivialP a b = I.FromNat (a * b) ~ I.FromNat (I.Count (I.FromNa
179183-- implementation is 'undefined'. However 'comp' can be and this partial
180184-- class implementation exists just to make the code more readable.
181185--
182−-- Please use 'identity' instead.
186+-- Please use 'iden' instead.
183187instance (Num e) => C.Category (Matrix e) where
184188 id = undefined
185189 (.) = comp
@@ -192,12 +196,12 @@ empty = M I.Empty
192196one :: e -> Matrix e 1 1
193197one = M . I.One
194198
195−junc ::
199+join ::
196200 (TrivialE a b) =>
197201 Matrix e a rows ->
198202 Matrix e b rows ->
199203 Matrix e (a + b) rows
200−junc (M a) (M b) = M (I.Junc a b)
204+join (M a) (M b) = M (I.Join a b)
201205
202206infixl 3 |||
203207(|||) ::
@@ -205,14 +209,14 @@ infixl 3 |||
205209 Matrix e a rows ->
206210 Matrix e b rows ->
207211 Matrix e (a + b) rows
208−(|||) = junc
212+(|||) = join
209213
210−split ::
214+fork ::
211215 (TrivialE a b) =>
212216 Matrix e cols a ->
213217 Matrix e cols b ->
214218 Matrix e cols (a + b)
215−split (M a) (M b) = M (I.Split a b)
219+fork (M a) (M b) = M (I.Fork a b)
216220
217221infixl 2 ===
218222(===) ::
@@ -220,7 +224,7 @@ infixl 2 ===
220224 Matrix e cols a ->
221225 Matrix e cols b ->
222226 Matrix e cols (a + b)
223−(===) = split
227+(===) = fork
224228
225229-- Construction
226230
@@ -238,7 +242,7 @@ col = M . I.col
238242row :: (I.FromLists e (I.FromNat cols) ()) => [e] -> Matrix e cols 1
239243row = M . I.row
240244
241−fromF ::
245+fromF' ::
242246 ( Liftable e a b,
243247 CountableN cols,
244248 CountableN rows,
@@ -246,9 +250,9 @@ fromF ::
246250 ) =>
247251 (a -> b) ->
248252 Matrix e cols rows
249−fromF = M . I.fromF
253+fromF' = M . I.fromF'
250254
251−fromF' ::
255+fromF ::
252256 ( Liftable e a b,
253257 CountableNz a,
254258 CountableNz b,
@@ -256,7 +260,7 @@ fromF' ::
256260 ) =>
257261 (a -> b) ->
258262 Matrix e (I.Count a) (I.Count b)
259−fromF' = undefined -- M . I.fromF'
263+fromF = undefined -- M . I.fromF
260264
261265-- Conversion
262266
@@ -296,18 +300,32 @@ bang ::
296300 Matrix e cols 1
297301bang = M I.bang
298302
299−-- Identity Matrix
303+-- iden Matrix
300304
301−identity ::
305+iden ::
302306 (Num e, FromListsN e cols cols, CountableN cols) =>
303307 Matrix e cols cols
304−identity = M I.identity
308+iden = M I.iden
305309
306310-- Matrix composition (MMM)
307311
308312comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
309313comp (M a) (M b) = M (I.comp a b)
310314
315+-- Scalar multiplication of matrices
316+
317+infixl 7 .|
318+-- | Scalar multiplication of matrices.
319+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
320+(.|) e (M m) = M (e I..| m)
321+
322+-- Scalar division of matrices
323+
324+infixl 7 ./
325+-- | Scalar multiplication of matrices.
326+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
327+(./) (M m) e = M (m I../ e)
328+
311329p1 ::
312330 ( Num e,
313331 CountableDimensionsN n m,
@@ -358,9 +376,9 @@ rows (M m) = I.rows m
358376columns :: (CountableN cols) => Matrix e cols rows -> Int
359377columns (M m) = I.columns m
360378
361−-- | Coproduct Bifunctor
362379infixl 5 -|-
363380
381+-- | Coproduct Bifunctor (Direct sum)
364382(-|-) ::
365383 ( Num e,
366384 CountableDimensionsN j k,
@@ -376,8 +394,8 @@ infixl 5 -|-
376394 Matrix e (n + m) (k + j)
377395(-|-) (M a) (M b) = M ((I.-|-) a b)
378396
379−-- | Khatri Rao Product and projections
380−kp1 ::
397+-- | Khatri Rao Product first projection
398+fstM ::
381399 forall e m k .
382400 ( Num e,
383401 CountableDimensionsN m k,
@@ -385,9 +403,10 @@ kp1 ::
385403 FromListsN e (m * k) m,
386404 TrivialP m k
387405 ) => Matrix e (m * k) m
388−kp1 = M (I.kp1 @e @(I.FromNat m) @(I.FromNat k))
406+fstM = M (I.fstM @e @(I.FromNat m) @(I.FromNat k))
389407
390−kp2 ::
408+-- | Khatri Rao Product second projection
409+sndM ::
391410 forall e m k.
392411 ( Num e,
393412 CountableDimensionsN k m,
@@ -395,9 +414,10 @@ kp2 ::
395414 CountableN (m * k),
396415 TrivialP m k
397416 ) => Matrix e (m * k) k
398−kp2 = M (I.kp2 @e @(I.FromNat m) @(I.FromNat k))
417+sndM = M (I.sndM @e @(I.FromNat m) @(I.FromNat k))
399418
400−khatri ::
419+-- | Khatri Rao Product
420+kr ::
401421 forall e cols a b.
402422 ( Num e,
403423 CountableDimensionsN a b,
@@ -406,10 +426,10 @@ khatri ::
406426 FromListsN e (a * b) b,
407427 TrivialP a b
408428 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a * b)
409−khatri a b =
410− let kp1' = kp1 @e @a @b
411− kp2' = kp2 @e @a @b
412− in comp (tr kp1') a * comp (tr kp2') b
429+kr a b =
430+ let fstM' = fstM @e @a @b
431+ sndM' = sndM @e @a @b
432+ in comp (tr fstM') a * comp (tr sndM') b
413433
414434-- | Product Bifunctor (Kronecker)
415435infixl 4 ><
@@ -428,17 +448,17 @@ infixl 4 ><
428448 TrivialP p q
429449 ) => Matrix e m p -> Matrix e n q -> Matrix e (m * n) (p * q)
430450(><) a b =
431− let kp1' = kp1 @e @m @n
432− kp2' = kp2 @e @m @n
433− in khatri (comp a kp1') (comp b kp2')
451+ let fstM' = fstM @e @m @n
452+ sndM' = sndM @e @m @n
453+ in kr (comp a fstM') (comp b sndM')
434454
435−-- | Matrix abide Junc Split
436−abideJS :: Matrix e cols rows -> Matrix e cols rows
437−abideJS (M m) = M (I.abideJS m)
455+-- | Matrix abide Join Fork
456+abideJF :: Matrix e cols rows -> Matrix e cols rows
457+abideJF (M m) = M (I.abideJF m)
438458
439−-- | Matrix abide Split Junc
440−abideSJ :: Matrix e cols rows -> Matrix e cols rows
441−abideSJ (M m) = M (I.abideSJ m)
459+-- | Matrix abide Fork Join
460+abideFJ :: Matrix e cols rows -> Matrix e cols rows
461+abideFJ (M m) = M (I.abideFJ m)
442462
443463-- | Matrix transposition
444464tr :: Matrix e cols rows -> Matrix e rows cols
src/LAoP/Matrix/Type.hs+81−63
@@ -73,8 +73,8 @@ module LAoP.Matrix.Type
7373 -- * Primitives
7474 empty,
7575 one,
76− junc,
77− split,
76+ join,
77+ fork,
7878
7979 -- * Auxiliary type families
8080 I.FromNat,
@@ -118,23 +118,27 @@ module LAoP.Matrix.Type
118118 -- ** Matrix Transposition
119119 tr,
120120
121+ -- ** Scalar multiplication/division of matrices
122+ (.|),
123+ (./),
124+
121125 -- ** McCarthy's Conditional
122126 cond,
123127
124128 -- ** Matrix "abiding"
125− abideJS,
126− abideSJ,
129+ abideJF,
130+ abideFJ,
127131
128132 -- ** Zip Matrices
129133 zipWithM,
130134
131135 -- * Biproduct approach
132− -- ** Split
136+ -- ** Fork
133137 (===),
134138 -- *** Projections
135139 p1,
136140 p2,
137− -- ** Junc
141+ -- ** Join
138142 (|||),
139143 -- *** Injections
140144 i1,
@@ -148,11 +152,11 @@ module LAoP.Matrix.Type
148152 -- | Note that given the restrictions imposed it is not possible to
149153 -- implement the standard type classes present in standard Haskell.
150154 -- *** Matrix pairing projections
151− kp1,
152− kp2,
155+ fstM,
156+ sndM,
153157
154158 -- *** Matrix pairing
155− khatri,
159+ kr,
156160
157161 -- * Matrix composition and lifting
158162
@@ -160,10 +164,10 @@ module LAoP.Matrix.Type
160164
161165 -- | Note that given the restrictions imposed it is not possible to
162166 -- implement the standard type classes present in standard Haskell.
163− identity,
167+ iden,
164168 comp,
165− fromF,
166169 fromF',
170+ fromF,
167171
168172 -- * Relation
169173 toRel,
@@ -202,7 +206,7 @@ type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a,
202206-- implementation is 'undefined'. However 'comp' can be and this partial
203207-- class implementation exists just to make the code more readable.
204208--
205−-- Please use 'identity' instead.
209+-- Please use 'iden' instead.
206210instance (Num e) => C.Category (Matrix e) where
207211 id = undefined
208212 (.) = comp
@@ -216,7 +220,7 @@ bimapM ::
216220 FromListsN e d c,
217221 FromListsN e b a
218222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
219−bimapM f g m = fromF' g `comp` m `comp` tr (fromF' f)
223+bimapM f g m = fromF g `comp` m `comp` tr (fromF f)
220224
221225-- | Zero type alias
222226type Zero = Void
@@ -234,35 +238,35 @@ empty = M I.Empty
234238one :: e -> Matrix e One One
235239one = M . I.One
236240
237−-- | Matrix 'Junc' constructor
238−junc ::
241+-- | Matrix 'Join' constructor
242+join ::
239243 Matrix e a rows ->
240244 Matrix e b rows ->
241245 Matrix e (Either a b) rows
242−junc (M a) (M b) = M (I.Junc a b)
246+join (M a) (M b) = M (I.Join a b)
243247
244248infixl 3 |||
245−-- | Matrix 'Junc' constructor
249+-- | Matrix 'Join' constructor
246250(|||) ::
247251 Matrix e a rows ->
248252 Matrix e b rows ->
249253 Matrix e (Either a b) rows
250−(|||) = junc
254+(|||) = join
251255
252−-- | Matrix 'Split' constructor
253−split ::
256+-- | Matrix 'Fork' constructor
257+fork ::
254258 Matrix e cols a ->
255259 Matrix e cols b ->
256260 Matrix e cols (Either a b)
257−split (M a) (M b) = M (I.Split a b)
261+fork (M a) (M b) = M (I.Fork a b)
258262
259263infixl 2 ===
260−-- | Matrix 'Split' constructor
264+-- | Matrix 'Fork' constructor
261265(===) ::
262266 Matrix e cols a ->
263267 Matrix e cols b ->
264268 Matrix e cols (Either a b)
265−(===) = split
269+(===) = fork
266270
267271-- Functor hierarchy
268272
@@ -274,7 +278,7 @@ fmapM ::
274278 )
275279 =>
276280 (a -> b) -> Matrix e c a -> Matrix e c b
277−fmapM f m = fromF' f `comp` m
281+fmapM f m = fromF f `comp` m
278282
279283-- | Applicative instance equivalent 'unit' function,
280284unitM :: (Num e) => Matrix e () ()
@@ -289,7 +293,7 @@ multM ::
289293 FromListsN e (a, b) b,
290294 TrivialP a b
291295 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
292−multM = khatri
296+multM = kr
293297
294298-- | Monad instance equivalent 'return' function,
295299returnM ::
@@ -336,23 +340,23 @@ row = M . I.row
336340--
337341-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
338342-- types @a@ or @b@ allows.
339−fromF ::
343+fromF' ::
340344 ( Liftable e a b,
341345 CountableDimensionsN cols rows,
342346 FromListsN e rows cols
343347 ) =>
344348 (a -> b) -> Matrix e cols rows
345−fromF = M . I.fromF
349+fromF' = M . I.fromF'
346350
347351-- | Lifts functions to matrices with dimensions matching @a@ and @b@
348352-- cardinality's.
349−fromF' ::
353+fromF ::
350354 ( Liftable e a b,
351355 CountableDimensionsN a b,
352356 FromListsN e b a
353357 ) =>
354358 (a -> b) -> Matrix e a b
355−fromF' = M . I.fromF'
359+fromF = M . I.fromF
356360
357361-- | Lifts relation functions to Boolean Matrix
358362toRel ::
@@ -418,16 +422,16 @@ point ::
418422 CountableN a,
419423 FromListsN e a One
420424 ) => a -> Matrix e One a
421−point = fromF' . const
425+point = fromF . const
422426
423−-- Identity Matrix
427+-- iden Matrix
424428
425−-- | Identity matrix
426−identity ::
429+-- | iden matrix
430+iden ::
427431 (Num e, FromListsN e a a, CountableN a) =>
428432 Matrix e a a
429−identity = M I.identity
430−{-# NOINLINE identity #-}
433+iden = M I.iden
434+{-# NOINLINE iden #-}
431435
432436-- Matrix composition (MMM)
433437
@@ -439,10 +443,24 @@ comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
439443comp (M a) (M b) = M (I.comp a b)
440444{-# NOINLINE comp #-}
441445{-# RULES
442− "comp/identity1" forall m. comp m identity = m ;
443− "comp/identity2" forall m. comp identity m = m
446+ "comp/iden1" forall m. comp m iden = m ;
447+ "comp/iden2" forall m. comp iden m = m
444448#-}
445449
450+-- Scalar multiplication of matrices
451+
452+infixl 7 .|
453+-- | Scalar multiplication of matrices.
454+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
455+(.|) e (M m) = M (e I..| m)
456+
457+-- Scalar division of matrices
458+
459+infixl 7 ./
460+-- | Scalar multiplication of matrices.
461+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
462+(./) (M m) e = M (m I../ e)
463+
446464-- | Biproduct first component projection
447465p1 ::
448466 ( Num e,
@@ -528,7 +546,7 @@ infixl 5 -|-
528546-- Khatri Rao Product and projections
529547
530548-- | Khatri Rao product first component projection matrix.
531−kp1 ::
549+fstM ::
532550 forall e m k .
533551 ( Num e,
534552 CountableDimensionsN m k,
@@ -536,10 +554,10 @@ kp1 ::
536554 FromListsN e (m, k) m,
537555 TrivialP m k
538556 ) => Matrix e (m, k) m
539−kp1 = M (I.kp1 @e @(I.Normalize m) @(I.Normalize k))
557+fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
540558
541559-- | Khatri Rao product second component projection matrix.
542−kp2 ::
560+sndM ::
543561 forall e m k.
544562 ( Num e,
545563 CountableDimensionsN k m,
@@ -547,20 +565,20 @@ kp2 ::
547565 FromListsN e (m, k) k,
548566 TrivialP m k
549567 ) => Matrix e (m, k) k
550−kp2 = M (I.kp2 @e @(I.Normalize m) @(I.Normalize k))
568+sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
551569
552570-- | Khatri Rao Matrix product also known as matrix pairing.
553571--
554572-- NOTE: That this is not a true categorical product, see for instance:
555573--
556574-- @
557−-- | kp1 `comp` khatri a b == a
558−-- khatri a b ==> |
559−-- | kp2 `comp` khatri a b == b
575+-- | fstM `comp` kr a b == a
576+-- kr a b ==> |
577+-- | sndM `comp` kr a b == b
560578-- @
561579--
562580-- __Emphasis__ on the implication symbol.
563−khatri ::
581+kr ::
564582 forall e cols a b.
565583 ( Num e,
566584 CountableDimensionsN a b,
@@ -569,16 +587,16 @@ khatri ::
569587 FromListsN e (a, b) b,
570588 TrivialP a b
571589 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
572−khatri a b =
573− let kp1' = kp1 @e @a @b
574− kp2' = kp2 @e @a @b
575− in comp (tr kp1') a * comp (tr kp2') b
590+kr a b =
591+ let fstM' = fstM @e @a @b
592+ sndM' = sndM @e @a @b
593+ in comp (tr fstM') a * comp (tr sndM') b
576594
577595-- Product Bifunctor (Kronecker)
578596
579597infixl 4 ><
580598
581−-- | Matrix product functor also known as kronecker product
599+-- | Matrix product functor also known as Kronecker product
582600(><) ::
583601 forall e m p n q.
584602 ( Num e,
@@ -593,33 +611,33 @@ infixl 4 ><
593611 TrivialP p q
594612 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
595613(><) a b =
596− let kp1' = kp1 @e @m @n
597− kp2' = kp2 @e @m @n
598− in khatri (comp a kp1') (comp b kp2')
614+ let fstM' = fstM @e @m @n
615+ sndM' = sndM @e @m @n
616+ in kr (comp a fstM') (comp b sndM')
599617
600−-- Matrix abide Junc Split
618+-- Matrix abide Join Fork
601619
602−-- | Matrix "abiding" followin the 'Junc'-'Split' abide law.
620+-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
603621--
604622-- Law:
605623--
606624-- @
607−-- 'Junc' ('Split' a c) ('Split' b d) == 'Split' ('Junc' a b) ('Junc' c d)
625+-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
608626-- @
609−abideJS :: Matrix e cols rows -> Matrix e cols rows
610−abideJS (M m) = M (I.abideJS m)
627+abideJF :: Matrix e cols rows -> Matrix e cols rows
628+abideJF (M m) = M (I.abideJF m)
611629
612−-- Matrix abide Split Junc
630+-- Matrix abide Fork Join
613631
614−-- | Matrix "abiding" followin the 'Split'-'Junc' abide law.
632+-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
615633--
616634-- Law:
617635--
618636-- @
619−-- 'Split' ('Junc' a b) ('Junc' c d) == 'Junc' ('Split' a c) ('Split' b d)
637+-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
620638-- @
621−abideSJ :: Matrix e cols rows -> Matrix e cols rows
622−abideSJ (M m) = M (I.abideSJ m)
639+abideFJ :: Matrix e cols rows -> Matrix e cols rows
640+abideFJ (M m) = M (I.abideFJ m)
623641
624642-- Matrix transposition
625643
src/LAoP/Relation.hs+5−5
@@ -43,9 +43,9 @@ module LAoP.Relation
4343 -- * Primitives
4444 empty,
4545 one,
46− junc,
46+ join,
4747 (|||),
48− split,
48+ fork,
4949 (===),
5050
5151 -- * Auxiliary type families
@@ -108,8 +108,8 @@ module LAoP.Relation
108108 splitR,
109109
110110 -- ** Projections
111− p1,
112− p2,
111+ fstR,
112+ sndR,
113113 -- ** Bifunctor
114114 (><),
115115 -- * Relational coproduct
@@ -148,7 +148,7 @@ module LAoP.Relation
148148 cond,
149149
150150 -- * Relational composition and lifting
151− identity,
151+ iden,
152152 comp,
153153 fromF,
154154 fromF',
src/LAoP/Relation/Internal.hs+63−63
@@ -53,9 +53,9 @@ module LAoP.Relation.Internal
5353 -- * Primitives
5454 empty,
5555 one,
56− junc,
56+ join,
5757 (|||),
58− split,
58+ fork,
5959 (===),
6060
6161 -- * Auxiliary type families
@@ -66,8 +66,8 @@ module LAoP.Relation.Internal
6666 -- * Matrix construction and conversion
6767 I.FromLists,
6868 fromLists,
69− fromF,
7069 fromF',
70+ fromF,
7171 toRel,
7272 toLists,
7373 toList,
@@ -118,8 +118,8 @@ module LAoP.Relation.Internal
118118 splitR,
119119
120120 -- ** Projections
121− p1,
122− p2,
121+ fstR,
122+ sndR,
123123 -- ** Bifunctor
124124 (><),
125125 -- * Relational coproduct
@@ -158,10 +158,10 @@ module LAoP.Relation.Internal
158158 cond,
159159
160160 -- * Relational composition and lifting
161− identity,
161+ iden,
162162 comp,
163− fromF,
164163 fromF',
164+ fromF,
165165
166166 -- ** Relational application
167167 pointAp,
@@ -205,19 +205,19 @@ type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a,
205205-- implementation is 'undefined'. However 'comp' can be and this partial
206206-- class implementation exists just to make the code more readable.
207207--
208−-- Please use 'identity' instead.
208+-- Please use 'iden' instead.
209209instance C.Category Relation where
210210 id = undefined
211211 (.) = comp
212212
213213instance Num (Relation a b) where
214− -- | Matrix addition becomes Boolean matrix disjunction
214+ -- | Matrix addition becomes Boolean matrix disjointion
215215 (R a) + (R b) = R (I.orM a b)
216216
217217 -- | Matrix subtraction becomes Relational subtraction
218218 (R a) - (R b) = R (I.subM a b)
219219
220− -- | Matrix multiplication becomes Boolean matrix conjunction
220+ -- | Matrix multiplication becomes Boolean matrix conjointion
221221 (R a) * (R b) = R (I.andM a b)
222222
223223 -- | Matrix negation becomes Boolean matrix negation
@@ -237,33 +237,33 @@ empty = R I.Empty
237237one :: Boolean -> Relation One One
238238one = R . I.One
239239
240−-- | Boolean Matrix 'Junc' constructor, also known as relational coproduct.
240+-- | Boolean Matrix 'Join' constructor, also known as relational coproduct.
241241--
242242-- See 'eitherR'.
243−junc :: Relation a c -> Relation b c -> Relation (Either a b) c
244−junc (R a) (R b) = R (I.Junc a b)
243+join :: Relation a c -> Relation b c -> Relation (Either a b) c
244+join (R a) (R b) = R (I.Join a b)
245245
246246infixl 3 |||
247−-- | Boolean Matrix 'Junc' constructor
247+-- | Boolean Matrix 'Join' constructor
248248--
249249-- See 'eitherR'.
250250(|||) ::
251251 Relation a c ->
252252 Relation b c ->
253253 Relation (Either a b) c
254−(|||) = junc
254+(|||) = join
255255
256−-- | Boolean Matrix 'Split' constructor, also known as relational product.
257−split :: Relation c a -> Relation c b -> Relation c (Either a b)
258−split (R a) (R b) = R (I.Split a b)
256+-- | Boolean Matrix 'Fork' constructor, also known as relational product.
257+fork :: Relation c a -> Relation c b -> Relation c (Either a b)
258+fork (R a) (R b) = R (I.Fork a b)
259259
260260infixl 2 ===
261−-- | Boolean Matrix 'Split' constructor
261+-- | Boolean Matrix 'Fork' constructor
262262(===) ::
263263 Relation c a ->
264264 Relation c b ->
265265 Relation c (Either a b)
266−(===) = split
266+(===) = fork
267267
268268-- Construction
269269
@@ -283,23 +283,23 @@ relationBuilder = R . I.matrixBuilder
283283--
284284-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
285285-- types @a@ or @b@ allows.
286−fromF ::
286+fromF' ::
287287 ( Liftable a b,
288288 CountableDimensionsN c d,
289289 FromListsN d c
290290 )
291291 => (a -> b) -> Relation c d
292−fromF f = R (I.fromFRel f)
292+fromF' f = R (I.fromFRel' f)
293293
294294-- | Lifts functions to matrices with dimensions matching @a@ and @b@
295295-- cardinality's.
296−fromF' ::
296+fromF ::
297297 ( Liftable a b,
298298 CountableDimensionsN a b,
299299 FromListsN b a
300300 )
301301 => (a -> b) -> Relation a b
302−fromF' f = R (I.fromFRel' f)
302+fromF f = R (I.fromFRel f)
303303
304304-- | Lifts relation functions to 'Relation'
305305toRel ::
@@ -412,18 +412,18 @@ point ::
412412 CountableN a,
413413 FromListsN a One
414414 ) => a -> Relation One a
415−point = fromF' . const
415+point = fromF . const
416416
417−-- Identity Matrix
417+-- iden Matrix
418418
419−-- | Identity matrix
419+-- | iden matrix
420420--
421421-- @
422−-- 'identity' ``comp`` r == r == r ``comp`` 'identity'
422+-- 'iden' ``comp`` r == r == r ``comp`` 'iden'
423423-- @
424−identity ::
424+iden ::
425425 (FromListsN a a, CountableN a) => Relation a a
426−identity = relationBuilder (bool (nat 0) (nat 1) . uncurry (==))
426+iden = relationBuilder (bool (nat 0) (nat 1) . uncurry (==))
427427
428428-- | Relational composition
429429--
@@ -530,7 +530,7 @@ iff r s = r == s
530530
531531-- | Relational intersection
532532--
533−-- Lifts pointwise conjunction.
533+-- Lifts pointwise conjointion.
534534--
535535-- @
536536-- (r ``intersection`` s) ``intersection`` t == r ``intersection`` (s ``intersection`` t)
@@ -541,7 +541,7 @@ intersection a b = a * b
541541
542542-- | Relational union
543543--
544−-- Lifts pointwise disjunction.
544+-- Lifts pointwise disjointion.
545545--
546546-- @
547547-- (r ``union`` s) ``union`` t == r `'union' (s ``union`` t)
@@ -658,13 +658,13 @@ bijection r = injection r && surjection r
658658
659659-- Properties of relations
660660
661−-- | A 'Relation' @r@ is 'reflexive' 'iff' @'identity' ``sse`` r@
661+-- | A 'Relation' @r@ is 'reflexive' 'iff' @'iden' ``sse`` r@
662662reflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
663−reflexive r = identity <= r
663+reflexive r = iden <= r
664664
665−-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'identity'@
665+-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'iden'@
666666coreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
667−coreflexive r = r <= identity
667+coreflexive r = r <= iden
668668
669669-- | A 'Relation' @r@ is 'transitive' 'iff' @(r ``comp`` r) ``sse`` r@
670670transitive :: Relation a a -> Bool
@@ -674,13 +674,13 @@ transitive r = (r `comp` r) `sse` r
674674symmetric :: Relation a a -> Bool
675675symmetric r = r == conv r
676676
677−-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'identity'@
677+-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'iden'@
678678antiSymmetric :: (CountableN a, FromListsN a a) => Relation a a -> Bool
679−antiSymmetric r = (r `intersection` conv r) `sse` identity
679+antiSymmetric r = (r `intersection` conv r) `sse` iden
680680
681−-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'identity') == 'zeros'@
681+-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'iden') == 'zeros'@
682682irreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
683−irreflexive r = (r `intersection` identity) == zeros
683+irreflexive r = (r `intersection` iden) == zeros
684684
685685-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
686686connected :: (CountableN a, FromListsN a a) => Relation a a -> Bool
@@ -718,9 +718,9 @@ difunctional r = r `comp` conv r `comp` r == r
718718-- NOTE: That this is not a true categorical product, see for instance:
719719--
720720-- @
721−-- | 'p1' ``comp`` 'splitR' a b ``sse`` a
721+-- | 'fstR' ``comp`` 'splitR' a b ``sse`` a
722722-- 'splitR' a b <=> |
723−-- | 'p2' ``comp`` 'splitR' a b ``sse`` b
723+-- | 'sndR' ``comp`` 'splitR' a b ``sse`` b
724724-- @
725725--
726726-- __Emphasis__ on the 'sse'.
@@ -742,14 +742,14 @@ splitR ::
742742 TrivialP a b
743743 )
744744 => Relation c a -> Relation c b -> Relation c (a, b)
745−splitR (R f) (R g) = R (I.khatri f g)
745+splitR (R f) (R g) = R (I.kr f g)
746746
747747-- | Relational pairing first component projection
748748--
749749-- @
750−-- 'p1' ``comp`` 'splitR' r s ``sse`` r
750+-- 'fstR' ``comp`` 'splitR' r s ``sse`` r
751751-- @
752−p1 ::
752+fstR ::
753753 forall a b .
754754 ( CountableDimensionsN a b,
755755 CountableN (a, b),
@@ -757,14 +757,14 @@ p1 ::
757757 TrivialP a b
758758 )
759759 => Relation (a, b) a
760−p1 = R (I.kp1 @Boolean @(I.Normalize a) @(I.Normalize b))
760+fstR = R (I.fstM @Boolean @(I.Normalize a) @(I.Normalize b))
761761
762762-- | Relational pairing second component projection
763763--
764764-- @
765−-- 'p2' ``comp`` 'splitR' r s ``sse`` s
765+-- 'sndR' ``comp`` 'splitR' r s ``sse`` s
766766-- @
767−p2 ::
767+sndR ::
768768 forall a b .
769769 ( CountableDimensionsN a b,
770770 CountableN (a, b),
@@ -772,7 +772,7 @@ p2 ::
772772 TrivialP a b
773773 )
774774 => Relation (a, b) b
775−p2 = R (I.kp2 @Boolean @(I.Normalize a) @(I.Normalize b))
775+sndR = R (I.sndM @Boolean @(I.Normalize a) @(I.Normalize b))
776776
777777-- Relational pairing functor
778778
@@ -780,7 +780,7 @@ infixl 4 ><
780780-- | Relational pairing functor
781781--
782782-- @
783−-- r '><' s == 'splitR' (r ``comp`` p1) (s ``comp`` p2)
783+-- r '><' s == 'splitR' (r ``comp`` fstR) (s ``comp`` sndR)
784784-- (r '><' s) ``comp`` (p '><' q) == (r ``comp`` p) '><' (s ``comp`` q)
785785-- @
786786(><) ::
@@ -815,12 +815,12 @@ infixl 4 ><
815815-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
816816-- @
817817eitherR :: Relation a c -> Relation b c -> Relation (Either a b) c
818−eitherR = junc
818+eitherR = join
819819
820820-- | Relational coproduct first component injection
821821--
822822-- @
823−-- 'img' 'i1' ``union`` 'img' 'i2' == 'identity'
823+-- 'img' 'i1' ``union`` 'img' 'i2' == 'iden'
824824-- 'i1' ``comp`` 'i2' = 'zeros'
825825-- @
826826i1 ::
@@ -834,7 +834,7 @@ i1 = R I.i1
834834-- | Relational coproduct second component injection
835835--
836836-- @
837−-- 'img' 'i1' ``union`` 'img' 'i2' == 'identity'
837+-- 'img' 'i1' ``union`` 'img' 'i2' == 'iden'
838838-- 'i1' ``comp`` 'i2' = 'zeros'
839839-- @
840840i2 ::
@@ -883,7 +883,7 @@ trans ::
883883 TrivialP c b
884884 )
885885 => Relation (a, b) c -> Relation a (c, b)
886−trans r = splitR r p2 `comp` conv p1
886+trans r = splitR r sndR `comp` conv fstR
887887
888888-- | Relational 'untrans'
889889--
@@ -904,12 +904,12 @@ untrans ::
904904 TrivialP c b
905905 )
906906 => Relation a (c, b) -> Relation (a, b) c
907−untrans s = p1 `comp` conv (splitR (conv s) p2)
907+untrans s = fstR `comp` conv (splitR (conv s) sndR)
908908
909909-- | Transforms predicate @p@ into a correflexive relation.
910910--
911911-- @
912−-- 'predR' ('const' True) == 'identity'
912+-- 'predR' ('const' True) == 'iden'
913913-- 'predR' ('const' False) == 'zeros'
914914-- @
915915--
@@ -924,14 +924,14 @@ predR ::
924924 FromListsN Bool a
925925 )
926926 => Relation a Bool -> Relation a a
927−predR p = identity `intersection` divisionF (fromF' (const True)) p
927+predR p = iden `intersection` divisionF (fromF (const True)) p
928928
929929-- | Equalizes functions @f@ and @g@.
930930-- That is, @'equalizer' f g@ is the largest coreflexive
931931-- that restricts @g@ so that @f@ and @g@ yield the same outputs.
932932--
933933-- @
934−-- 'equalizer' r r == 'identity'
934+-- 'equalizer' r r == 'iden'
935935-- 'equalizer' ('point' True) ('point' False) == 'zeros'
936936-- @
937937equalizer ::
@@ -939,7 +939,7 @@ equalizer ::
939939 FromListsN a a
940940 )
941941 => Relation a b -> Relation a b -> Relation a a
942−equalizer f g = identity `intersection` divisionF f g
942+equalizer f g = iden `intersection` divisionF f g
943943
944944-- | Relational conditional guard.
945945--
@@ -969,22 +969,22 @@ cond p r s = eitherR r s `comp` guard p
969969-- | Relational domain.
970970--
971971-- For injective relations, 'domain' and 'ker'nel coincide,
972−-- since @'ker' r ``sse`` 'identity'@ in such situations.
972+-- since @'ker' r ``sse`` 'iden'@ in such situations.
973973domain ::
974974 ( CountableN a,
975975 FromListsN a a
976976 ) => Relation a b -> Relation a a
977−domain r = ker r `intersection` identity
977+domain r = ker r `intersection` iden
978978
979979-- | Relational range.
980980--
981981-- For functions, 'range' and 'img' (image) coincide,
982−-- since @'img' f ``sse`` 'identity'@ for any @f@.
982+-- since @'img' f ``sse`` 'iden'@ for any @f@.
983983range ::
984984 ( CountableN b,
985985 FromListsN b b
986986 ) => Relation a b -> Relation b b
987−range r = img r `intersection` identity
987+range r = img r `intersection` iden
988988
989989-- Relation pretty print
990990