-
Notifications
You must be signed in to change notification settings - Fork 0
/
bccc_morphism_construction.scala
1747 lines (1608 loc) · 52.2 KB
/
bccc_morphism_construction.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Generator of canonical morphisms in bicartesian closed categories.
*
* This script contains a Scala implementation of a little embedded
* domain-specific language that can build canonical morphisms
* in BCCC's using only domain and codomain types as hints.
*
* For example, this framework can take the following, human readable
* chain of hom-set descriptions
*
* (Hom(D, Exp(C, A)) x Hom(D, Exp(C, B)) x Hom(D, A U B)) ~
* (Hom(D x A, C) x Hom(D x B, C) x Hom(D, A U B)) ~
* (Hom(A x D, C) x Hom(B x D, C) x Hom(D, A U B)) ~
* (Hom(A, Exp(C, D)) x Hom(B, Exp(C, D)) x Hom(D, A U B)) ~
* (Hom(A U B, Exp(C, D)) x Hom(D, A U B)) ~
* (Hom((A U B) x D, C) x Hom(D, A U B)) ~
* (Hom(D x (A U B), C) x Hom(D, A U B)) ~
* (Hom(D, Exp(C, A U B)) x Hom(D, A U B)) ~
* Hom(D, Exp(C, A U B) x (A U B))
*
* and generate the following (rather incomprehensible) explicit
* isomorphism out of it:
*
* (a, b, f) =>
* <λ(ε o <[λ(ε o <a o π_1,π_2> o <π_2,π_1>),
* λ(ε o <b o π_1,π_2> o <π_2,π_1>)] o π_1,π_2> o <π_2,π_1>),f>
*
* Can also be used to extract isomorphisms between objects from isomorphism
* between hom sets (application of the Yoneda-lemma).
*
* @author: Andrey Tyukin
* @date: 2015-12-23
*
*/
/*##############################################################################
Bicartesian closed categories, basic building blocks
##############################################################################*/
sealed trait Obj {
def toTex: String
def toCode: String
def x(other: Obj) = Prod(this, other)
def U(other: Obj) = Coprod(this, other)
def ^(other: Obj) = Exp(this, other)
}
case class AtomObj(name: String) extends Obj {
override def toString = name
def toTex = name
def toCode = name
}
case object Terminal extends Obj {
override def toString = "T"
def toTex = """\mathbb{T}"""
def toCode = """Terminal"""
}
case object Initial extends Obj {
override def toString = "I"
def toTex = """\mathbb{I}"""
def toCode = "Initial"
}
case class Exp(cod: Obj, dom: Obj) extends Obj {
override def toString = "(%s^%s)".format(cod, dom)
def toTex = " ({ %s}^{ %s}) ".format(cod.toTex, dom.toTex)
def toCode = "Exp[%s,%s]".format(cod.toCode, dom.toCode)
}
case class Prod(a: Obj, b: Obj) extends Obj {
override def toString = "(%s x %s)".format(a, b)
def toTex = """ ({ %s}\times{ %s}) """.format(a.toTex, b.toTex)
def toCode = "Prod[%s,%s]".format(a.toCode, b.toCode)
}
case class Coprod(a: Obj, b: Obj) extends Obj {
override def toString = "(%s U %s)".format(a, b)
def toTex = """ ({ %s}\amalg{ %s}) """.format(a.toTex, b.toTex)
def toCode = "Coprod[%s,%s]".format(a.toCode, b.toCode)
}
sealed trait Mor {
def toTex: String
def toCode: String
def dom: Obj
def cod: Obj
def scalaTypeString = "Hom[%s,%s]".format(dom.toCode, cod.toCode)
def o (other: Mor): Mor = Comp(this, other)
}
case class Id(dom: Obj) extends Mor {
def cod = dom
override def toString = "Id_%s".format(dom)
def toTex = """\textrm{Id}_{ %s}""".format(dom.toTex)
def toCode = "Id[%s]".format(dom.toCode)
}
case class Comp(second: Mor, first: Mor) extends Mor {
require(first.cod == second.dom,
"first = " + first + "\n" +
"second = " + second + "\n" +
"first.cod = " + first.cod + "\n" +
"second.dom = " + second.dom
)
def dom = first.dom
def cod = second.cod
override def toString = "%s o %s".format(second, first)
def toTex = """ %s\circ %s """.format(second.toTex, first.toTex)
def toCode = "(\n%s o \n%s)".format(second.toCode, first.toCode)
}
case class InitMor(cod: Obj) extends Mor {
def dom = Initial
override def toString = "!_%s".format(cod)
def toTex = "!_{ %s}".format(cod.toTex)
def toCode = "InitMor[%s]".format(cod.toCode)
}
case class TermMor(dom: Obj) extends Mor {
def cod = Terminal
override def toString = "\u2020_%s".format(dom)
def toTex = """\dagger_{ %s}""".format(dom.toTex)
def toCode = "TermMor[%s]".format(dom.toCode)
}
case class ProdMor(f: Mor, g: Mor) extends Mor {
require(f.dom == g.dom)
def dom = f.dom
def cod = Prod(f.cod, g.cod)
override def toString = "<%s,%s>".format(f, g)
def toTex = """\langle %s,%s\rangle""".format(f.toTex, g.toTex)
def toCode = "ProdMor(%s,%s)".format(f.toCode, g.toCode)
}
case class P1(a: Obj, b: Obj) extends Mor {
def dom = Prod(a, b)
def cod = a
override def toString = "\u03c0_1"
def toTex = """\pi_1^{ %s, %s}""".format(a.toTex, b.toTex)
def toCode = """P1[%s, %s]""".format(a.toCode, b.toCode)
}
case class P2(a: Obj, b: Obj) extends Mor {
def dom = Prod(a, b)
def cod = b
override def toString = "\u03c0_2"
def toTex = """\pi_2^{ %s, %s}""".format(a.toTex, b.toTex)
def toCode = """P2[%s, %s]""".format(a.toCode, b.toCode)
}
case class I1(a: Obj, b: Obj) extends Mor {
def dom = a
def cod = Coprod(a, b)
override def toString = "\u0399_1"
def toTex = """\iota_1^{ %s, %s}""".format(a.toTex, b.toTex)
def toCode = """I1[%s, %s]""".format(a.toCode, b.toCode)
}
case class I2(a: Obj, b: Obj) extends Mor {
def dom = b
def cod = Coprod(a, b)
override def toString = "\u0399_2"
def toTex = """\iota_2^{ %s, %s}""".format(a.toTex, b.toTex)
def toCode = """I2[%s, %s]""".format(a.toCode, b.toCode)
}
case class CoprodMor(f: Mor, g: Mor) extends Mor {
require(f.cod == g.cod)
def dom = Coprod(f.dom, g.dom)
def cod = f.cod
override def toString = "[%s,%s]".format(f, g)
def toTex = """[%s,%s]""".format(f.toTex, g.toTex)
def toCode = "CoprodMor(%s, %s)".format(f.toCode, g.toCode)
}
case class Lambda(f: Mor) extends Mor {
require(f.dom match {
case Prod(_, _) => true
case _ => false
}, "Cannot abstract %s, domain is %s (not a product)".format(f, f.dom) )
private val f_domain_components = { val Prod(a, b) = f.dom; (a, b) }
def dom = f_domain_components._1
def cod = Exp(f.cod, f_domain_components._2)
override def toString = "\u03bb(%s)".format(f)
def toTex = """\lambda( %s )""".format(
// f_domain_components._1.toTex,
// f_domain_components._2.toTex,
// f.cod.toTex,
f.toTex
)
def toCode = """Lambda[%s,%s,%s](%s)""".format(
f_domain_components._1.toCode,
f_domain_components._2.toCode,
f.cod.toCode,
f.toCode
)
}
case class Eval(in: Obj, cod: Obj) extends Mor {
def dom = Prod(Exp(cod, in), in)
override def toString = "\u03b5"
def toTex = """\varepsilon^{ %s,%s}""".format(in.toTex, cod.toTex)
def toCode = """Eval[%s,%s]""".format(in.toCode, cod.toCode)
}
case class VariableMor(name: String, dom: Obj, cod: Obj) extends Mor {
def toTex = name
def toCode = name
override def toString = name
}
/*##############################################################################
Additional useful morphism constructors
##############################################################################*/
def times(f: Mor, g: Mor): Mor = {
ProdMor(f o P1(f.dom, g.dom), g o P2(f.dom, g.dom))
}
def amalg(f: Mor, g: Mor): Mor = {
CoprodMor(I1(f.cod, g.cod) o f, I2(f.cod, g.cod) o g)
}
/*##############################################################################
Some useful isomorphisms
##############################################################################*/
/** A x B ~ B x A */
def swapProd(a: Obj, b: Obj): Mor = ProdMor(P2(a, b), P1(a, b))
/** Hom[X, A x B] ~ Hom[X, A] x Hom[X, B] (inverse to <-,->) */
def splitProd(fg: Mor): (Mor, Mor) = fg.cod match {
case Prod(a, b) => (P1(a, b) o fg, P2(a, b) o fg)
case _ => throw new Exception("Cannot split morphism as <f,g>: " + fg)
}
/** A + B ~ B + A */
def swapCoprod(a: Obj, b: Obj): Mor = CoprodMor(I2(a, b), I1(a, b))
/** Hom[A + B, X] ~ Hom[A, X] x Hom[B, X] (inverse to [-,-]) */
def splitCoprod(fg: Mor): (Mor, Mor) = fg.dom match {
case Coprod(a, b) => (fg o I1(a, b), fg o I2(a, b))
case _ => throw new Exception("Cannot split as [f, g]: " + fg)
}
/** Hom[X, Z^Y] ~ Hom[X x Y, Z] (inverse to lambda) */
def uncurry(f: Mor): Mor = f.cod match {
case Exp(z, y) => {
Eval(y, z) o times(f, Id(y))
}
case _ => {
throw new Exception("Cannot uncurry a morphism, cod not exponential: " + f)
}
}
/** A ~ T x A (add/remove terminal object from left) */
def addTerminalLeft(a: Obj): Mor = ProdMor(TermMor(a), Id(a))
def removeTerminalLeft(a: Obj): Mor = P2(Terminal, a)
/** A ~ A x T (add/remove terminal object from right) */
def addTerminalRight(a: Obj): Mor = ProdMor(Id(a), TermMor(a))
def removeTerminalRight(a: Obj): Mor = P1(a, Terminal)
/*##############################################################################
Variable arrow elimination
##############################################################################*/
def s(xaz: Mor, xzb: Mor): Mor = {
// extract objects from domains/codomains of inputs
val x = xaz.dom
val Exp(z,a) = xaz.cod
val Exp(b,w) = xzb.cod
require(z == w)
// build the X -> B^A morphism
Lambda(Eval(z, b) o ProdMor(xzb o P1(x, a), uncurry(xzb)))
}
def k(x: Obj, c: Mor): Mor = {
val a = c.dom
Lambda(c o P2(Terminal, a)) o TermMor(x)
}
def i(x: Mor): Mor = {
val xo = x.cod
Lambda(P1(xo, Terminal))
}
def abstractProd(xab1: Mor, xab2: Mor): Mor = {
Lambda(ProdMor(uncurry(xab1), uncurry(xab2)))
}
def abstractCoprod(xa1b: Mor, xa2b: Mor): Mor = {
???
}
/**
* Eliminates an arrow `x: T -> X` from a morphism `f: A -> B`, yielding a
* morphism `E_x(f): X -> B^A` such that for each `a: T -> A` it holds:
* `eval o < E_x(f) o x, a> = f o a`.
*/
def abstractVar(x: VariableMor, f: Mor): Mor = f match {
case Comp(a, b) => s(abstractVar(x, a), abstractVar(x, b))
case v: VariableMor if (v == x) => i(x)
case ProdMor(f, g) => abstractProd(abstractVar(x, f), abstractVar(x, g))
case CoprodMor(f, g) => abstractCoprod(abstractVar(x, f), abstractVar(x, g))
case c => k(x.cod, c)
}
/*##############################################################################
Utils
##############################################################################*/
// Bag of stuff...
def sequence[X](list: List[Option[X]]): Option[List[X]] = {
list match {
case Nil => Some(Nil)
case h :: t => for (hVal <- h; tail <- sequence(t)) yield (hVal :: tail)
}
}
/** Finds a permutation such that `b = a o pi`, if uniquely possible.
*/
def extractPermutation[A](a: List[A], b: List[A]): Option[Vector[Int]] = {
val aSet = a.toSet
val bSet = b.toSet
if (aSet != bSet) {
None
} else if (aSet.size != a.size || bSet.size != b.size) {
None
} else {
val a_inv = (a zip Stream.from(0)).toMap
Some((b map a_inv).toVector)
}
}
/**
* Generates all possible "partitions ~ tokenizations" of a list,
* e.g. a list of 6 elements can be cut into 4 parts in the following ways:
*
* *|*|*|***
* *|*|**|**
* *|*|***|*
* *|**|*|**
* *|**|**|*
* *|***|*|*
* **|*|*|**
* **|*|**|*
* **|**|*|*
* ***|*|*|*
*/
def linearPartitions[A](k: Int, l: List[A]): List[List[List[A]]] = {
if (k == 1) List(List(l))
else {
for (
f <- (1 to (l.size - k + 1)).toList;
p <- linearPartitions(k - 1, l.drop(f))
) yield (l.take(f) :: p)
}
}
/**
* Returns some `List[Y]` only if every `x` in `xs` is mapped to some
* element of type `Y`. Does not try to find solutions for elements in
* the tail as soon as `f(x)` fails for at least one `x`.
*/
def findAll[X, Y](xs: List[X])(f: X => Option[Y]): Option[List[Y]] = {
xs match {
case Nil => Some(Nil)
case h :: t => for (
hSolution <- f(h);
tSolutions <- findAll(t)(f)
) yield hSolution :: tSolutions
}
}
/**
* Tries `xs` sequentially until the first `Some[Y]` is produced.
* Returns `Some[Y]` as soon as `f(x)` succeeds for the first time.
*/
def findSome[X, Y](xs: List[X])(f: X => Option[Y]): Option[Y] = {
xs match {
case Nil => None
case x :: tail => f(x) match {
case Some(y) => Some(y)
case None => findSome(tail)(f)
}
}
}
/*##############################################################################
Mappings between (products of) hom-sets
##############################################################################*/
case class Hom(dom: Obj, cod: Obj) {
def x(other: Hom) = HomProd(List(this, other))
def substitute(s: Map[String, Obj]) = Hom(substObj(s, dom), substObj(s, cod))
def toTex = "\\textrm{Hom}[%s,%s]".format(dom.toTex, cod.toTex)
}
case class HomProd(factors: List[Hom]) {
def x (oneMore: Hom) = HomProd(factors ++ List(oneMore))
def substitute(s: Map[String, Obj]) = HomProd(factors.map{_.substitute(s)})
override def toString = factors.mkString(" x ")
def toTex = factors.map{_.toTex}.mkString(" \\times ")
}
/**
* Convert stand-alone Hom-sets into "unary products"
*/
import scala.language.implicitConversions
implicit def unaryHomProd(hom: Hom): HomProd = HomProd(List(hom))
implicit def homProd2homSeq(hp: HomProd): HomSeq = HomSeq(List(hp), Nil)
implicit def hom2homSeq(h: Hom): HomSeq = HomSeq(List(h), Nil)
/**
* A mapping between things like
* `Hom[A_1, B_1] x ... x Hom[A_n, B_n]`.
* It can be interpreted as morphism, or as a natural transformation,
* depending on how many objects one sees as 'fixed', and how many as
* 'variable'.
*/
trait HomMapping {
def dom: HomProd
def cod: HomProd
/**
* Checks whether arguments match the domain.
* Throws errors if not.
*/
def check(arg: List[Mor]): Unit = {
require(arg.size == dom.factors.size,
"#args = %s != %s = #factors".format(arg.size, dom.factors.size)
)
for ((a, Hom(d, c)) <- arg zip dom.factors) {
require(a.dom == d, "Wrong domain: " + a.dom + " <-> " + d + " for\n " +
"HomMapping: " + this + " \n " +
"Morphism: " + a
)
require(a.cod == c, "Wrong codomain: " + a.cod + " <-> " + c + " for\n " +
"HomMapping: " + this + " \n " +
"Morphism: " + a
)
}
}
/**
* Raw version of `apply` that does not perform any type-checks
*/
protected def _apply(args: List[Mor]): List[Mor]
def apply(args: List[Mor]): List[Mor] = { check(args); _apply(args) }
def substitute(s: Map[String, Obj]): HomMapping
def inverse: Option[HomMapping]
def isInvertible = !inverse.isEmpty
/** single-line, no break, no arrow, only representation of this mapping */
def toTex: String
}
/**
* Composition of hom-mappings (apply first, then second, then third, etc...)
*/
case class HomMappingComposition(parts: List[HomMapping])
extends HomMapping {
def dom = parts.head.dom
def cod = parts.last.cod
protected def _apply(arg: List[Mor]): List[Mor] = {
parts.foldLeft(arg){ case (mors, map) => map(mors) }
}
def substitute(s: Map[String, Obj]): HomMappingComposition = {
HomMappingComposition(parts.map(_.substitute(s)))
}
def inverse: Option[HomMapping] = {
for (inverses <- sequence(parts.map(_.inverse)))
yield HomMappingComposition(inverses.reverse)
}
override def toString = parts.mkString(
"HomMappingComposition(\n ",
",\n ",
"\n)"
)
def toTex = throw new UnsupportedOperationException("never used")
def toDiagram(drawInverses: Boolean) = {
val bldr = new StringBuilder
for (p <- parts) {
bldr ++= p.dom.toTex
bldr ++= "\\\\\n"
bldr ++= "\\dTo^{ %s} ".format(p.toTex)
if (drawInverses) {
for (inv <- p.inverse) {
bldr ++= "\\uTo_{ %s}".format(inv.toTex)
}
}
bldr ++= "\\\\\n"
}
bldr ++= parts.last.cod.toTex
bldr.toString
}
}
/**
* A mapping that simply permutates the hom-sets.
* E.g. `Hom[A, B] x Hom[C, D] x Hom[E, F]` can be
* transformed to `Hom[C, D] x Hom[E, F] x Hom[A, B]` with
* the permutation `pi = [1, 2, 0]`.
*/
case class PermutationHomMapping(
permutation: Vector[Int], /* 0-based */
homs: Vector[Hom]
) extends HomMapping {
val dom = HomProd(homs.toList)
val cod = HomProd(permutation.map{i => homs(i)}.toList)
protected def _apply(args: List[Mor]): List[Mor] = {
val vargs = args.toVector
permutation.toList.map{i => vargs(i)}
}
def substitute(s: Map[String, Obj]): PermutationHomMapping = {
PermutationHomMapping(permutation, homs.map{_.substitute(s)})
}
def inverse = {
val p_inv =
(0 until permutation.size).
map{i => (i, permutation(i))}.
sortBy(_._2).
map{_._1}.
toVector
Some(PermutationHomMapping(p_inv, cod.factors.toVector))
}
def toTex = {
val p_inv =
(0 until permutation.size).
map{i => (i, permutation(i))}.
sortBy(_._2).
map{_._1}.
toVector
permutation.mkString("(", ",",")")
}
}
/**
* List of hom-mappings that operate in parallel.
* E.g. if `f: Hom[A, C] x Hom[B, C] -> Hom[A x B, C]` and
* `g: Hom[X, Y] -> Hom[Z, W]`, then their cross product
* will be a hom-mapping from `Hom[A, C] x Hom[B, C] x Hom[X, Y]` to
* `Hom[A x B, C] x Hom[Z, W]`.
*/
case class CrossProdOfHomMappings(ms: List[HomMapping])
extends HomMapping {
def dom = HomProd(ms.flatMap{_.dom.factors})
def cod = HomProd(ms.flatMap{_.cod.factors})
protected def _apply(args: List[Mor]): List[Mor] = {
var remainingArgs = args
var reverseResults: List[Mor] = Nil
for (m <- ms) {
val mArgs = remainingArgs.take(m.dom.factors.size)
remainingArgs = remainingArgs.drop(m.dom.factors.size)
val mResults = m(mArgs)
reverseResults = mResults.reverse ++ reverseResults
}
val results = reverseResults.reverse
results
}
def substitute(s: Map[String, Obj]): HomMapping =
CrossProdOfHomMappings(ms.map{_.substitute(s)})
def inverse: Option[HomMapping] = {
for (inverses <- sequence(ms.map{_.inverse})) yield {
CrossProdOfHomMappings(inverses)
}
}
override def toString = ms.mkString(" x ")
def toTex = {
if (ms.size > 1) ms.map{
x => "(" + x.toTex + ")"
}.mkString(" \\times ")
else ms.map{_.toTex}.mkString(" \\times ")
}
}
/**
* If `f: C -> A` and `g: B -> D`, then `Hom[f, g] : Hom[A, B] -> Hom[C, D]`.
* It simply pre- and post-composes `f` and `g` to morphisms from `Hom[A, B]`.
*/
case class HomBifunctor(
pre: Mor, pre_inv: Option[Mor],
post: Mor, post_inv: Option[Mor]
) extends HomMapping {
def dom = Hom(pre.cod, post.dom)
def cod = Hom(pre.dom, post.cod)
protected def _apply(arg: List[Mor]) = List(post o arg.head o pre)
def substitute(s: Map[String, Obj]) =
HomBifunctor(
substMor(s, pre),
pre_inv.map{ f => substMor(s, f) },
substMor(s, post),
post_inv.map{ f => substMor(s, f) }
)
def inverse =
for (pri <- pre_inv; poi <- post_inv)
yield HomBifunctor(pri, Some(pre), poi, Some(post))
override def toString = "Hom(%s,%s)".format(pre, post)
def toTex = {
(pre, post) match {
case (Id(_), Id(_)) => "\\textrm{Id}"
case (Id(_), f) => f.toTex + " \\circ -"
case (f, Id(_)) => "- \\circ " + f.toTex
case (f, g) => "\\textrm{Hom}[%s,%s]".format(f.toTex, g.toTex)
}
}
}
/*##############################################################################
Bunch of hom-set-mappings that can be used as patterns
##############################################################################*/
case class Prod_UnivProp_mediating(Z: Obj, A: Obj, B: Obj) extends HomMapping {
def dom = Hom(Z, A) x Hom(Z, B)
def cod = Hom(Z, A x B)
protected def _apply(arg: List[Mor]): List[Mor] = {
val f = arg(0)
val g = arg(1)
List(ProdMor(f, g))
}
def substitute(s: Map[String, Obj]): HomMapping = Prod_UnivProp_mediating(
substObj(s, Z),
substObj(s, A),
substObj(s, B)
)
def inverse: Option[HomMapping] = Some(Prod_UnivProp_split(Z, A, B))
def toTex ="\\langle -, - \\rangle"
}
case class Prod_UnivProp_split(Z: Obj, A: Obj, B: Obj) extends HomMapping {
def dom = Hom(Z, A x B)
def cod = Hom(Z, A) x Hom(Z, B)
protected def _apply(arg: List[Mor]): List[Mor] = {
val fg = arg.head
List(P1(A, B) o fg, P2(A, B) o fg)
}
def substitute(s: Map[String, Obj]) = Prod_UnivProp_split(
substObj(s, Z),
substObj(s, A),
substObj(s, B)
)
def inverse: Option[HomMapping] = Some(Prod_UnivProp_mediating(Z, A, B))
def toTex = {
"(\\pi_1^{ %1$s,%2$s} \\circ -, \\pi_2^{ %1$s,%2$s} \\circ -)".
format(A.toTex, B.toTex)
}
}
case class Coprod_UnivProp_mediating(A: Obj, B: Obj, Z: Obj)
extends HomMapping {
def dom = Hom(A, Z) x Hom(B, Z)
def cod = Hom(A U B, Z)
protected def _apply(arg: List[Mor]): List[Mor] = {
val f = arg(0)
val g = arg(1)
List(CoprodMor(f, g))
}
def substitute(s: Map[String, Obj]): HomMapping = Coprod_UnivProp_mediating(
substObj(s, A),
substObj(s, B),
substObj(s, Z)
)
def inverse = Some(Coprod_UnivProp_split(A, B, Z))
def toTex = "[-, -]"
}
case class Coprod_UnivProp_split(A: Obj, B: Obj, Z: Obj) extends HomMapping {
def dom = Hom(A U B, Z)
def cod = Hom(A, Z) x Hom(B, Z)
protected def _apply(arg: List[Mor]): List[Mor] = {
val fg = arg.head
List(fg o I1(A, B), fg o I2(A, B))
}
def substitute(s: Map[String, Obj]) = Coprod_UnivProp_split(
substObj(s, A),
substObj(s, B),
substObj(s, Z)
)
def inverse = Some(Coprod_UnivProp_mediating(A, B, Z))
def toTex = {
"(- \\circ \\iota_1^{ %1$s,%2$s}, - \\circ \\iota_2^{ %1$s,%2$s})".
format(A.toTex, B.toTex)
}
}
case class Exp_UnivProp_curry(A: Obj, B: Obj, Z: Obj) extends HomMapping {
def dom = Hom(A x B, Z)
def cod = Hom(A, Exp(Z, B))
protected def _apply(arg: List[Mor]): List[Mor] = List(Lambda(arg.head))
def substitute(s: Map[String, Obj]) = Exp_UnivProp_curry(
substObj(s, A),
substObj(s, B),
substObj(s, Z)
)
def inverse = Some(Exp_UnivProp_uncurry(A, B, Z))
def toTex = "\\lambda"
}
case class Exp_UnivProp_uncurry(A: Obj, B: Obj, Z: Obj) extends HomMapping {
def dom = Hom(A, Exp(Z, B))
def cod = Hom(A x B, Z)
protected def _apply(arg: List[Mor]) =
List(Eval(B, Z) o times(arg.head, Id(B)))
def substitute(s: Map[String, Obj]) = Exp_UnivProp_uncurry(
substObj(s, A),
substObj(s, B),
substObj(s, Z)
)
def inverse = Some(Exp_UnivProp_curry(A, B, Z))
def toTex = {
"\\textrm{eval}^{ %1$s,%2$s} \\circ (- \\times \\textrm{Id}_{ %1$s})".
format(B.toTex, Z.toTex)
}
}
case class MorphismCompositionHomMapping(A: Obj, B: Obj, C: Obj)
extends HomMapping {
def dom = Hom(A, B) x Hom(B, C)
def cod = Hom(A, C)
protected def _apply(args: List[Mor]) =
List(args(0) o args(1))
def substitute(s: Map[String, Obj]) = MorphismCompositionHomMapping(
substObj(s, A),
substObj(s, B),
substObj(s, C)
)
def inverse = None
def toTex = "- \\circ -"
}
val DefaultPatterns = {
val A = AtomObj("A")
val B = AtomObj("B")
val Z = AtomObj("Z")
List(
Prod_UnivProp_mediating(Z, A, B),
Prod_UnivProp_split(Z, A, B),
Coprod_UnivProp_mediating(A, B, Z),
Coprod_UnivProp_split(A, B, Z),
Exp_UnivProp_curry(A, B, Z),
Exp_UnivProp_uncurry(A, B, Z),
MorphismCompositionHomMapping(A, B, Z)
)
}
/*##############################################################################
Pattern-matching on objects and hom-sets
##############################################################################*/
type Match = Map[String, Obj]
/**
* Merges two matches into a single one, if possible. Fails if both matches
* contain incompatible information.
*/
def mergeMatches(x: Match, y: Match): Option[Match] = {
val keys = x.keySet ++ y.keySet
var res = new scala.collection.mutable.HashMap[String, Obj]
for (k <- keys) {
if (x.contains(k)) {
if (y.contains(k)) {
val xv = x(k)
val yv = y(k)
if (xv == yv) {
res(k) = xv
} else {
// two incompatible values, that's bad
return None
}
} else {
res(k) = x(k)
}
} else if (y.contains(k)) {
res(k) = y(k)
} else {
throw new Error("Either `Map.keySet` or the merging algo has a bug")
}
}
return Some(res.toMap)
}
/**
* Tries to combine all `matches` into a single `Match`. Fails
* if the `Match` instances in input are incompatible.
*/
def mergeMatches(matches: List[Match]): Option[Match] = {
matches match {
case Nil => Some(Map.empty)
case h :: t =>
for (tm <- mergeMatches(t); res <- mergeMatches(h, tm)) yield res
}
}
/**
* Tries to generate an assignment of atomic object names from `pat` to
* sub-objects from which `inst` is built.
*/
def objPatternMatching(inst: Obj, pat: Obj): Option[Match] = {
(inst, pat) match {
case (Prod(a, b), Prod(p, q)) => objPatternMatching_2(a, b, p, q)
case (Coprod(a, b), Coprod(p, q)) => objPatternMatching_2(a, b, p, q)
case (Exp(a, b), Exp(p, q)) => objPatternMatching_2(a, b, p, q)
case (x, AtomObj(name)) => Some(Map(name -> x))
case (Terminal, Terminal) => Some(Map.empty)
case (Initial, Initial) => Some(Map.empty)
case _ => None
}
}
/**
* Tries to match `a` against `p` and `b` against `q`, succeeds only if
* both matches are compatible
*/
def objPatternMatching_2(a: Obj, b: Obj, p: Obj, q: Obj): Option[Match] = {
for {
ma <- objPatternMatching(a, p)
mb <- objPatternMatching(b, q)
mrg <- mergeMatches(ma, mb)
} yield mrg
}
/**
* Attempts to match thingies like `Hom[A_1, B_1] x ... x Hom[A_n, B_n]`
* against each other. Returns some map with suitable substitutions for
* every atomic object that occurs in `pat` in case of success, or `None`
* otherwise.
*/
def homPatternMatching(inst: HomProd, pat: HomProd): Option[Match] = {
val HomProd(insts) = inst
val HomProd(pats) = pat
if (insts.size == pats.size) {
val componentMatches =
for ((Hom(a, b), Hom(p, q)) <- insts zip pats)
yield objPatternMatching_2(a, b, p, q)
// if every component matched, and all their matches were
// compatible, then we have a match
for (
ms <- sequence(componentMatches);
mrg <- mergeMatches(ms)
) yield mrg
} else {
// not even the number of components is the same, no match
None
}
}
/*##############################################################################
Treating objects as variables in more complex objects and morphisms
##############################################################################*/
def substObj(s: Map[String, Obj], obj: Obj): Obj = obj match {
case AtomObj(name) => {
if (s.isDefinedAt(name)) s(name)
else AtomObj(name)
}
case Terminal => Terminal
case Initial => Initial
case Prod(a, b) => Prod(substObj(s,a), substObj(s, b))
case Coprod(a, b) => Coprod(substObj(s, a), substObj(s, b))
case Exp(a, b) => Exp(substObj(s, a), substObj(s, b))
}
def substMor(s: Map[String, Obj], mor: Mor): Mor = mor match {
case Comp(g, f) => Comp(substMor(s, g), substMor(s, f))
case Id(o) => Id(substObj(s, o))
case P1(x, y) => P1(substObj(s, x), substObj(s, y))
case P2(x, y) => P2(substObj(s, x), substObj(s, y))
case I1(x, y) => I1(substObj(s, x), substObj(s, y))
case I2(x, y) => I2(substObj(s, x), substObj(s, y))
case ProdMor(g, f) => ProdMor(substMor(s, g), substMor(s, f))
case CoprodMor(g, f) => CoprodMor(substMor(s, g), substMor(s, f))
case Lambda(f) => Lambda(substMor(s, f))
case Eval(a, b) => Eval(substObj(s, a), substObj(s, b))
case InitMor(o) => InitMor(substObj(s, o))
case TermMor(o) => TermMor(substObj(s, o))
case VariableMor(name, a, b) =>
VariableMor(name, substObj(s, a), substObj(s, b))
}
/*##############################################################################
Morphism searchers
##############################################################################*/
// the idea is this:
// ultimately, we want to find `HomMapping`s automatically.
// - Finding permutations and cross-products-of-hom-mappings are essentially
// combinatoric problems.
// - Finding out whether some universal property is applicable is simple
// Obj-pattern matching
// - Finding the right morphisms for the `HomBifunctor`
// is more difficult (notice that it is the only type of hom-mapping that
// is parameterized by morphisms, not merely objects)
// The raison d'etre of morphism searchers is to find suitable morphisms that
// can be plugged into the `HomBifunctor`.
/**
* An inference rule that tells us what subgoals have to be solved in order
* to solve the final goal, and how to actually compose the final solution out
* of solution of subgoals.
*/
trait MorphismConstructionRule {
/**
* Returns a list of subgoals, if this rule matches the goal.
*/
def subgoals(dom: Obj, cod: Obj): Option[List[(Obj, Obj)]]
/**
* Constructs solution of the goal using solutions of subgoals,
* a solution consists of a morphism from `dom` to `cod`, and an
* optional inverse.
*/
def apply(
dom: Obj,
cod: Obj,
subgoalSolutions: List[(Mor, Option[Mor])]
): (Mor, Option[Mor])
}
/**
* Checks whether variables in `f` can be replaced in such a way that
* they match `cod` and `dom`. The most basic kind of rule, produces no
* subgoals.
*/
case class PatternMorConstrRule(
pattern: Mor,
inverse: Option[Mor]
) extends MorphismConstructionRule {
// NOTE: this is why the interface
// `apply(dom, cod): Option[(List[(Obj, Obj)], List[Mor] => Mor)]`
// actually made sense... We could optimize the double pattern matching
// away there. But this is a minor optimization.
/**
* Returns `Some(Nil)` if the pattern matches, `None` otherwise.
* Degenerated case, produces no subgoals.
*/
def subgoals(dom: Obj, cod: Obj): Option[List[(Obj, Obj)]] = {
for (_ <- objPatternMatching_2(dom, cod, pattern.dom, pattern.cod)) yield {
Nil
}
}
def apply(dom: Obj, cod: Obj, subgoalSolutions: List[(Mor, Option[Mor])]) = {
val s = objPatternMatching_2(dom, cod, pattern.dom, pattern.cod).get
(substMor(s, pattern), for {i <- inverse} yield substMor(s, i))
}
}
/**
* A rule that builds `(f x g)` from `f` and `g`
*/
case object ProdMorConstrRule extends MorphismConstructionRule {
def subgoals(dom: Obj, cod: Obj): Option[List[(Obj, Obj)]] = {
(dom, cod) match {
case (Prod(a, b), Prod(c, d)) => Some(List((a, c), (b, d)))
case _ => None
}
}
def apply(dom: Obj, cod: Obj, subgoalSolutions: List[(Mor, Option[Mor])]) = {
val f = times(subgoalSolutions(0)._1, subgoalSolutions(1)._1)
val fi =
for (ai <- subgoalSolutions(0)._2; bi <- subgoalSolutions(1)._2)
yield times(ai, bi)
(f, fi)
}
}
/**
* Builds `(f U g)` from `f` and `g`
*/
case object CoprodMorConstrRule extends MorphismConstructionRule {
def subgoals(dom: Obj, cod: Obj): Option[List[(Obj, Obj)]] = {
(dom, cod) match {
case (Coprod(a, b), Coprod(c, d)) => Some(List((a, c), (b, d)))
case _ => None
}
}
def apply(dom: Obj, cod: Obj, subgoalSolutions: List[(Mor, Option[Mor])]) = {
val f = amalg(subgoalSolutions(0)._1, subgoalSolutions(1)._1)
val fi =
for (ai <- subgoalSolutions(0)._2; bi <- subgoalSolutions(1)._2)
yield amalg(ai, bi)
(f, fi)
}
}
/**
* Builds `B^A -> D^C` out of `C -> A` and `B -> D`
*/
case object LambdaMorConstrRule extends MorphismConstructionRule {
def subgoals(dom: Obj, cod: Obj): Option[List[(Obj, Obj)]] = {
(dom, cod) match {
case (Exp(b, a), Exp(d, c)) => Some(List((c, a), (b, d)))
case _ => None
}
}
def apply(dom: Obj, cod: Obj, subgoalSolutions: List[(Mor, Option[Mor])]) = {
val Exp(b, a) = dom
val Exp(d, c) = cod
val pre = subgoalSolutions(0)._1
val post = subgoalSolutions(1)._1
val f = Lambda(post o Eval(a, b) o times(Id(dom), pre))
val pre_inv = subgoalSolutions(0)._2
val post_inv = subgoalSolutions(1)._2
val fi =
for (pri <- pre_inv; poi <- post_inv)
yield Lambda(poi o Eval(c, d) o times(Id(cod), pri))
(f, fi)
}