Repository navigation
Expand file tree
/
Copy pathBasic.lean
More file actions
2330 lines (2059 loc) · 100 KB
/
Copy pathBasic.lean
File metadata and controls
2330 lines (2059 loc) · 100 KB
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
import Mathlib.Data.PFun
import Mathlib.Algebra.Order.BigOperators.Group.Finset
import Mathlib.Logic.Equiv.Basic
import Mathlib.Data.Fintype.Option
/-!
# Systems and their wiring
A system `System A B` is a partial function `List A →. B` from input histories to
the next output; it is a deterministic discrete system (`IsDDS`) when it is silent
before the first input and its domain is closed under nonempty prefixes. Source:
Lanzenberger–Maurer, *Coupling of Random Systems*, Definition 5.
## Main definitions
* `System`, `IsDDS`, `DDS`, `SilentAtEmpty`, `PrefixClosed`, `Finite`, `Terminating`
* `Reach`, `trim`, `EqOnReachable` (`≈`): reachable histories and equality on them
* `relabel s f g`: renaming the input and output alphabets
* `interconnect s route inj`: feeding outputs back as inputs along a routing
table, with the relations `Exchange` and `Induces` and the condition `Converges`
* `parAll`, `pair`: parallel composition of a family and of two systems
## Main results
* `interconnect_interconnect`: two nested wirings are one wiring
* `interconnect_relabel`, `relabel_interconnect`: relabeling commutes with wiring
* `parAll_interconnect`: locality of parallel composition under wiring
* `parAll_reindex`, `parAll_flatten`, `parAll_relabel`
* `Finite.terminating`, `Terminating.parAll`, `Terminating.interconnect`,
`Terminating.relabel`
* `converges_pair`, `converges_pair_of_terminating`, `interconnect_no_feedback`
* `trim_behEq`, `trim_prefix_closed`, `IsDDS.trim_eq`
-/
namespace SystemAlgebra
open Classical
/-- A discrete system (LanMau20 Def. 5, printed p. 11): a partial function on histories. -/
abbrev System (A B : Type) := List A →. B
variable {A B C D E X Y M N : Type}
/-! ## Relabeling -/
/-- Rename inputs and outputs. -/
def relabel (s : System X Y) (f : A → X) (g : Y → B) : System A B :=
fun h => (s (h.map f)).map g
lemma relabel_relabel (s : System X Y) (f : A → X) (g : Y → B)
(f' : C → A) (g' : B → D) :
relabel (relabel s f g) f' g' = relabel s (f ∘ f') (g' ∘ g) := by
funext h
simp [relabel, List.map_map, Part.map_map]
lemma relabel_id (s : System X Y) : relabel s id id = s := by
funext h
simp [relabel]
exact Part.map_id' (fun _ => rfl) _
/-- Two systems are equal once their replies have the same members. -/
lemma system_ext {s t : System A B} (h : ∀ u b, b ∈ s u ↔ b ∈ t u) : s = t := by
funext u
exact Part.ext (h u)
/-! ## Predicates on the carrier (spec §1) -/
/-- A reactive system never speaks before it has received an input. -/
def SilentAtEmpty (s : System X Y) : Prop := ¬ (s []).Dom
/-- The domain is closed under nonempty prefixes. -/
def PrefixClosed (s : System X Y) : Prop :=
∀ ⦃l₁ l₂ : List X⦄, l₁ <+: l₂ → l₁ ≠ [] → (s l₂).Dom → (s l₁).Dom
/-- A deterministic discrete system (LanMau20 Def. 5, printed p. 11): silent before
the first input, with a prefix-closed domain. -/
def IsDDS (s : System X Y) : Prop := SilentAtEmpty s ∧ PrefixClosed s
/-- Deterministic discrete systems. -/
abbrev DDS (X Y : Type) := {s : System X Y // IsDDS s}
/-- Silent on all histories longer than `n`. -/
def Finite (n : ℕ) (s : System X Y) : Prop := ∀ h, (s h).Dom → h.length ≤ n
/-- Every defined reply strictly decreases a natural-number measure. -/
def Terminating (s : System X Y) : Prop :=
∃ μ : List X → ℕ, ∀ h x, (s (h ++ [x])).Dom → μ (h ++ [x]) < μ h
/-- T0. -/
lemma Finite.terminating {n : ℕ} {s : System X Y} (hs : Finite n s) : Terminating s :=
⟨fun h => n - h.length, fun h x hd => by
have := hs _ hd
simp at this ⊢
omega⟩
/-- T3. -/
lemma Terminating.relabel {s : System X Y} (hs : Terminating s) (f : A → X) (g : Y → B) :
Terminating (SystemAlgebra.relabel s f g) := by
obtain ⟨μ, hμ⟩ := hs
exact ⟨fun h => μ (h.map f), fun h x hd => by simpa using hμ (h.map f) (f x) (by simpa [SystemAlgebra.relabel] using hd)⟩
/-- Histories all of whose nonempty prefixes are answered. -/
def Reach (s : System X Y) (h : List X) : Prop :=
∀ h' e, h = h' ++ e → h' ≠ [] → (s h').Dom
/-- The reachable part of a system. -/
noncomputable def trim (s : System X Y) : System X Y :=
fun h => if Reach s h then s h else Part.none
lemma trim_eq_of_prefix_eq {s t : System X Y} {h : List X}
(he : ∀ p, p <+: h → s p = t p) : trim s h = trim t h := by
have hr : Reach s h ↔ Reach t h := by
constructor <;> intro hp p e hh hn
· rw [← he p ⟨e, hh.symm⟩]; exact hp p e hh hn
· rw [he p ⟨e, hh.symm⟩]; exact hp p e hh hn
simp only [trim, hr, he h (List.prefix_refl h)]
/-- Behavioural equality: same reachable histories, same replies there. -/
def EqOnReachable (s t : System X Y) : Prop :=
(∀ h, Reach s h ↔ Reach t h) ∧ ∀ h, Reach s h → h ≠ [] → s h = t h
infix:50 " ≈ₛ " => EqOnReachable
/-! ## Wiring by a routing table -/
section Wire
variable (s : System X Y) (route : Y → B ⊕ X) (inj : A → X)
/-- The internal exchange from the internal history `h`: evaluate `s`,
feed internal outputs back, stop at an exposed output or at silence. -/
inductive Exchange : List X → Option B → List X → Prop
| silent (h) : ¬ (s h).Dom → Exchange h none h
| out (h) (y : Y) (b : B) : y ∈ s h → route y = Sum.inl b → Exchange h (some b) h
| feed (h) (y : Y) (x : X) (o h') : y ∈ s h → route y = Sum.inr x →
Exchange (h ++ [x]) o h' → Exchange h o h'
/-- An external history induces the last exposed reply and the internal history
reached afterwards. No exchange happens at the empty history (spec §4). -/
inductive Induces : List A → Option B → List X → Prop
| nil : Induces [] none []
| snoc (u a o₀ H o H') : Induces u o₀ H → Exchange s route (H ++ [inj a]) o H' →
Induces (u ++ [a]) o H'
variable {s route inj}
lemma Exchange.det {h o₁ H₁ o₂ H₂} (l₁ : Exchange s route h o₁ H₁) (l₂ : Exchange s route h o₂ H₂) :
o₁ = o₂ ∧ H₁ = H₂ := by
induction l₁ generalizing o₂ H₂ with
| silent h hd =>
cases l₂ with
| silent => exact ⟨rfl, rfl⟩
| out _ y _ hy => exact (hd (Part.dom_iff_mem.mpr ⟨y, hy⟩)).elim
| feed _ y _ _ _ hy => exact (hd (Part.dom_iff_mem.mpr ⟨y, hy⟩)).elim
| out h y b hy hr =>
cases l₂ with
| silent _ hd => exact (hd (Part.dom_iff_mem.mpr ⟨y, hy⟩)).elim
| out _ y' b' hy' hr' =>
have := Part.mem_unique hy hy'
subst this
rw [hr] at hr'
cases hr'
exact ⟨rfl, rfl⟩
| feed _ y' x' _ _ hy' hr' =>
have := Part.mem_unique hy hy'
subst this
rw [hr] at hr'
cases hr'
| feed h y x o h' hy hr _ ih =>
cases l₂ with
| silent _ hd => exact (hd (Part.dom_iff_mem.mpr ⟨y, hy⟩)).elim
| out _ y' b' hy' hr' =>
have := Part.mem_unique hy hy'
subst this
rw [hr] at hr'
cases hr'
| feed _ y' x' _ _ hy' hr' l =>
have := Part.mem_unique hy hy'
subst this
rw [hr] at hr'
cases hr'
exact ih l
lemma Induces.inv {u o H} (r : Induces s route inj u o H) :
(u = [] ∧ o = none ∧ H = []) ∨
∃ u' a o₀ H₀, u = u' ++ [a] ∧ Induces s route inj u' o₀ H₀ ∧
Exchange s route (H₀ ++ [inj a]) o H := by
cases r with
| nil => exact Or.inl ⟨rfl, rfl, rfl⟩
| snoc u' a o₀ H₀ _ _ r l => exact Or.inr ⟨u', a, o₀, H₀, rfl, r, l⟩
lemma induces_nil_iff {o H} : Induces s route inj [] o H ↔ o = none ∧ H = [] := by
constructor
· intro r
rcases r.inv with ⟨-, h⟩ | ⟨u', a, _, _, hu, _, _⟩
· exact h
· simp at hu
· rintro ⟨rfl, rfl⟩; exact Induces.nil
lemma induces_snoc_iff {u a o H'} :
Induces s route inj (u ++ [a]) o H' ↔
∃ o₀ H, Induces s route inj u o₀ H ∧ Exchange s route (H ++ [inj a]) o H' := by
constructor
· intro r
rcases r.inv with ⟨hu, -⟩ | ⟨u', a', o₀, H, hu, r, l⟩
· simp at hu
· obtain ⟨rfl, h2⟩ := List.append_inj' hu rfl
cases h2
exact ⟨o₀, H, r, l⟩
· rintro ⟨o₀, H, r, l⟩
exact Induces.snoc _ _ _ _ _ _ r l
lemma Induces.det {u o₁ H₁ o₂ H₂} (r₁ : Induces s route inj u o₁ H₁)
(r₂ : Induces s route inj u o₂ H₂) : o₁ = o₂ ∧ H₁ = H₂ := by
induction r₁ generalizing o₂ H₂ with
| nil => obtain ⟨rfl, rfl⟩ := induces_nil_iff.mp r₂; exact ⟨rfl, rfl⟩
| snoc u a o₀ H o H' r l ih =>
obtain ⟨_, K, r', l'⟩ := induces_snoc_iff.mp r₂
obtain ⟨-, rfl⟩ := ih r'
exact l.det l'
variable (s route inj)
/-- The wired system: its reply is the last exposed reply induced by the external history. -/
noncomputable def interconnect : System A B := fun u =>
⟨∃ b H, Induces s route inj u (some b) H, fun h => Classical.choose h⟩
variable {s route inj}
lemma mem_interconnect {u b} : b ∈ interconnect s route inj u ↔ ∃ H, Induces s route inj u (some b) H := by
constructor
· rintro ⟨h, rfl⟩
exact Classical.choose_spec h
· rintro ⟨H, r⟩
refine ⟨⟨b, H, r⟩, ?_⟩
obtain ⟨H', r'⟩ := Classical.choose_spec (⟨b, H, r⟩ : ∃ b H, Induces s route inj u (some b) H)
exact Option.some_inj.mp (r'.det r).1
end Wire
section Basic
variable {s : System X Y} {route : Y → B ⊕ X} {inj : A → X}
lemma Exchange.extends {h o h'} (l : Exchange s route h o h') : ∃ e, h' = h ++ e := by
induction l with
| silent => exact ⟨[], by simp⟩
| out => exact ⟨[], by simp⟩
| feed h y x o h' _ _ _ ih =>
obtain ⟨e, rfl⟩ := ih
exact ⟨x :: e, by simp⟩
lemma Induces.prefix {u e o H} (r : Induces s route inj (u ++ e) o H) :
∃ o' H', Induces s route inj u o' H' := by
induction e using List.reverseRecOn generalizing o H with
| nil => exact ⟨o, H, by simpa using r⟩
| append_singleton e a ih =>
rw [← List.append_assoc] at r
obtain ⟨o₀, H₀, r₀, -⟩ := induces_snoc_iff.mp r
exact ih r₀
lemma mem_of_induces_some {u b H} (r : Induces s route inj u (some b) H) :
b ∈ interconnect s route inj u := mem_interconnect.mpr ⟨H, r⟩
lemma not_dom_of_induces_none {u H} (r : Induces s route inj u none H) :
¬ (interconnect s route inj u).Dom := by
intro d
obtain ⟨H', r'⟩ := mem_interconnect.mp (Part.get_mem d)
cases (r.det r').1
lemma interconnect_eq_some {u b H} (r : Induces s route inj u (some b) H) :
interconnect s route inj u = Part.some b :=
Part.eq_some_iff.mpr (mem_of_induces_some r)
lemma interconnect_eq_none {u H} (r : Induces s route inj u none H) :
interconnect s route inj u = Part.none :=
Part.eq_none_iff'.mpr (not_dom_of_induces_none r)
/-- An exchange uses only the system values on its actual input prefixes. -/
lemma Exchange.congr_of_prefix_eq {t : System X Y} {G o H}
(l : Exchange s route G o H) (he : ∀ p, p <+: H → s p = t p) :
Exchange t route G o H := by
induction l with
| silent G hd => exact Exchange.silent G (by rwa [← he G (List.prefix_refl G)])
| out G y b hy hr =>
exact Exchange.out G y b (by rwa [← he G (List.prefix_refl G)]) hr
| feed G y x o H hy hr l ih =>
obtain ⟨e, hH⟩ := l.extends
exact Exchange.feed G y x o H (by rwa [← he G ⟨x :: e, by rw [hH]; simp⟩])
hr (ih he)
lemma Induces.congr_of_prefix_eq {t : System X Y} {u o H}
(r : Induces s route inj u o H) (he : ∀ p, p <+: H → s p = t p) :
Induces t route inj u o H := by
induction r with
| nil => exact Induces.nil
| snoc u x o₀ H₀ o H r l ih =>
obtain ⟨e, hH⟩ := l.extends
have hp : H₀ <+: H := ⟨inj x :: e, by rw [hH]; simp⟩
exact Induces.snoc _ _ _ _ _ _ (ih (fun p h => he p (h.trans hp)))
(l.congr_of_prefix_eq he)
/-- Prefixes of the exposed history keep prefixes of the same component history. -/
lemma Induces.prefix_history {u e o H} (r : Induces s route inj (u ++ e) o H) :
∃ o' H', Induces s route inj u o' H' ∧ H' <+: H := by
induction e using List.reverseRecOn generalizing o H with
| nil => exact ⟨o, H, by simpa using r, List.prefix_refl H⟩
| append_singleton e a ih =>
rw [← List.append_assoc] at r
obtain ⟨o₀, H₀, r₀, l⟩ := induces_snoc_iff.mp r
obtain ⟨o', H', r', hp⟩ := ih r₀
obtain ⟨e', he'⟩ := l.extends
exact ⟨o', H', r', hp.trans ⟨inj a :: e', by rw [he']; simp⟩⟩
/-- Finite component agreement determines every observed prefix, including silence. -/
lemma Induces.interconnect_eq_on_prefixes {t : System X Y} {u o H}
(r : Induces s route inj u o H) (he : ∀ p, p <+: H → s p = t p) :
∀ p, p <+: u → interconnect s route inj p = interconnect t route inj p := by
rintro p ⟨e, rfl⟩
obtain ⟨o', H', r', hp⟩ := r.prefix_history
have rt := r'.congr_of_prefix_eq (fun q hq => he q (hq.trans hp))
cases o' with
| none => rw [interconnect_eq_none r', interconnect_eq_none rt]
| some y => rw [interconnect_eq_some r', interconnect_eq_some rt]
/-- No internal exchange diverges: every external history induces an internal history. -/
def Converges (s : System X Y) (route : Y → B ⊕ X) (inj : A → X) : Prop :=
∀ u, ∃ o H, Induces s route inj u o H
/-- If every internal exchange stops, the wiring converges. -/
lemma converges_of_exchanges (h : ∀ H, ∃ o H', Exchange s route H o H') :
∀ u, ∃ o H, Induces s route inj u o H := by
intro u
induction u using List.reverseRecOn with
| nil => exact ⟨none, [], Induces.nil⟩
| append_singleton u a ih =>
obtain ⟨o₀, H₀, r⟩ := ih
obtain ⟨o, H, l⟩ := h (H₀ ++ [inj a])
exact ⟨o, H, Induces.snoc _ _ _ _ _ _ r l⟩
end Basic
/-! ## Two nested wirings are one wiring -/
section Vanishing
variable (s : System X Y) (r₁ : Y → M ⊕ X) (i₁ : N → X)
(r₂ : M → B ⊕ N) (i₂ : A → N)
/-- The routing table obtained by closing first `r₁`, then `r₂`. -/
def routeComp : Y → B ⊕ X := fun y =>
match r₁ y with
| Sum.inr x => Sum.inr x
| Sum.inl m =>
match r₂ m with
| Sum.inl b => Sum.inl b
| Sum.inr n => Sum.inr (i₁ n)
variable {s r₁ i₁ r₂ i₂}
local notation "F" => interconnect s r₁ i₁
local notation "RJ" => routeComp r₁ i₁ r₂
/-- What the joint exchange does after the inner exchange has stopped. -/
def Cont (o₁ : Option M) (T : List X) (o : Option B) (T' : List X) : Prop :=
match o₁ with
| none => o = none ∧ T' = T
| some m =>
(∃ b, r₂ m = Sum.inl b ∧ o = some b ∧ T' = T) ∨
(∃ n, r₂ m = Sum.inr n ∧ Exchange s RJ (T ++ [i₁ n]) o T')
lemma joint_of_inner {T₀ o₁ T₁ o T'} (l : Exchange s r₁ T₀ o₁ T₁)
(c : Cont (s := s) (r₁ := r₁) (i₁ := i₁) (r₂ := r₂) o₁ T₁ o T') : Exchange s RJ T₀ o T' := by
induction l with
| silent h hd =>
obtain ⟨rfl, rfl⟩ := c
exact Exchange.silent _ hd
| out h y m hy hr =>
rcases c with ⟨b, hb, rfl, rfl⟩ | ⟨n, hn, l'⟩
· exact Exchange.out _ y b hy (by simp [routeComp, hr, hb])
· exact Exchange.feed _ y _ _ _ hy (by simp [routeComp, hr, hn]) l'
| feed h y x _ _ hy hr _ ih =>
exact Exchange.feed _ y x _ _ hy (by simp [routeComp, hr]) (ih c)
/-- The internal history induced by `K` continues as the inner exchange from `T₀`. -/
def Pending (K : List N) (T₀ : List X) : Prop :=
∀ o₂ T₂, Induces s r₁ i₁ K o₂ T₂ ↔ Exchange s r₁ T₀ o₂ T₂
lemma pending_snoc {K o T n} (r : Induces s r₁ i₁ K o T) :
Pending (s := s) (r₁ := r₁) (i₁ := i₁) (K ++ [n]) (T ++ [i₁ n]) := by
intro o₂ T₂
rw [induces_snoc_iff]
constructor
· rintro ⟨o', T', r', l⟩
obtain ⟨-, rfl⟩ := r.det r'
exact l
· intro l
exact ⟨o, T, r, l⟩
lemma pending_feed {K T₀ y x} (p : Pending (s := s) (r₁ := r₁) (i₁ := i₁) K T₀)
(hy : y ∈ s T₀) (hr : r₁ y = Sum.inr x) :
Pending (s := s) (r₁ := r₁) (i₁ := i₁) K (T₀ ++ [x]) := by
intro o₂ T₂
rw [p]
constructor
· intro l
cases l with
| silent _ hd => exact (hd (Part.dom_iff_mem.mpr ⟨y, hy⟩)).elim
| out _ y' _ hy' hr' =>
rw [Part.mem_unique hy hy', hr'] at hr; cases hr
| feed _ y' x' _ _ hy' hr' l =>
rw [Part.mem_unique hy hy', hr'] at hr; cases hr; exact l
· exact Exchange.feed _ y x _ _ hy hr
lemma outer_of_joint {T₀ o T'} (l : Exchange s RJ T₀ o T') :
∀ K, Pending (s := s) (r₁ := r₁) (i₁ := i₁) K T₀ →
∃ K', Exchange F r₂ K o K' ∧ ∃ o₁, Induces s r₁ i₁ K' o₁ T' := by
induction l with
| silent h hd =>
intro K p
have r := (p none h).mpr (Exchange.silent _ hd)
exact ⟨K, Exchange.silent _ (not_dom_of_induces_none r), none, r⟩
| out h y b hy hr =>
intro K p
unfold routeComp at hr
split at hr
· cases hr
· rename_i m hm
split at hr
· cases hr
have r := (p (some m) h).mpr (Exchange.out _ y m hy hm)
exact ⟨K, Exchange.out _ m b (mem_of_induces_some r) ‹_›, some m, r⟩
· cases hr
| feed h y x o h' hy hr _ ih =>
intro K p
unfold routeComp at hr
split at hr
· rename_i x' hx
cases hr
exact ih K (pending_feed p hy hx)
· rename_i m hm
split at hr
· cases hr
· rename_i n hn
cases hr
have r := (p (some m) h).mpr (Exchange.out _ y m hy hm)
obtain ⟨K', l', rest⟩ := ih (K ++ [n]) (pending_snoc r)
exact ⟨K', Exchange.feed _ m n _ _ (mem_of_induces_some r) hn l', rest⟩
lemma joint_of_outer {K o K'} (l : Exchange F r₂ K o K') :
∀ T₀, Pending (s := s) (r₁ := r₁) (i₁ := i₁) K T₀ →
∀ o₁ T', Induces s r₁ i₁ K' o₁ T' → Exchange s RJ T₀ o T' := by
induction l with
| silent K hd =>
intro T₀ p o₁ T' r
cases o₁ with
| none => exact joint_of_inner ((p _ _).mp r) ⟨rfl, rfl⟩
| some m => exact (hd (Part.dom_iff_mem.mpr ⟨_, mem_of_induces_some r⟩)).elim
| out K m b hm hb =>
intro T₀ p o₁ T' r
obtain ⟨T₁, r₁'⟩ := mem_interconnect.mp hm
obtain ⟨rfl, rfl⟩ := r₁'.det r
exact joint_of_inner ((p _ _).mp r₁') (Or.inl ⟨b, hb, rfl, rfl⟩)
| feed K m n o K' hm hn _ ih =>
intro T₀ p o₁ T' r
obtain ⟨T₁, r₁'⟩ := mem_interconnect.mp hm
exact joint_of_inner ((p _ _).mp r₁')
(Or.inr ⟨n, hn, ih _ (pending_snoc r₁') o₁ T' r⟩)
lemma induces_joint_iff {u o T} :
Induces s RJ (i₁ ∘ i₂) u o T ↔
∃ K, Induces F r₂ i₂ u o K ∧ ∃ o₁, Induces s r₁ i₁ K o₁ T := by
induction u using List.reverseRecOn generalizing o T with
| nil =>
rw [induces_nil_iff]
constructor
· rintro ⟨rfl, rfl⟩
exact ⟨[], Induces.nil, none, Induces.nil⟩
· rintro ⟨K, r, o₁, r'⟩
obtain ⟨rfl, rfl⟩ := induces_nil_iff.mp r
obtain ⟨-, rfl⟩ := induces_nil_iff.mp r'
exact ⟨rfl, rfl⟩
| append_singleton u a ih =>
rw [induces_snoc_iff]
constructor
· rintro ⟨o₀, T₀, r₀, l⟩
obtain ⟨K, rK, o₁, rT⟩ := ih.mp r₀
obtain ⟨K', l', rest⟩ := outer_of_joint l _ (pending_snoc (n := i₂ a) rT)
exact ⟨K', induces_snoc_iff.mpr ⟨o₀, K, rK, l'⟩, rest⟩
· rintro ⟨K', r, o₁, r'⟩
obtain ⟨o₀, K, rK, l⟩ := induces_snoc_iff.mp r
obtain ⟨e, rfl⟩ := l.extends
obtain ⟨o₂, T₀, rT⟩ := Induces.prefix (e := [i₂ a] ++ e) (by simpa using r')
exact ⟨o₀, T₀, ih.mpr ⟨K, rK, o₂, rT⟩,
joint_of_outer l _ (pending_snoc rT) o₁ T r'⟩
/-- **Vanishing.** Closing `r₁` and then `r₂` is the same as closing the
composed routing table at once. No hypothesis is needed. -/
theorem interconnect_interconnect :
interconnect (interconnect s r₁ i₁) r₂ i₂ = interconnect s (routeComp r₁ i₁ r₂) (i₁ ∘ i₂) := by
apply system_ext
intro u b
rw [mem_interconnect, mem_interconnect]
constructor
· rintro ⟨K, r⟩
have hK : ∃ o₁ T, Induces s r₁ i₁ K o₁ T := by
have hd : ∀ {K₀ K'}, Exchange F r₂ K₀ (some b) K' → (F K').Dom := by
intro K₀ K' l
generalize hb : (some b : Option B) = ob at l
induction l with
| silent => cases hb
| out _ m _ hm _ => exact Part.dom_iff_mem.mpr ⟨m, hm⟩
| feed _ _ _ _ _ _ _ _ ih => exact ih hb
have hdK : (F K).Dom := by
induction u using List.reverseRecOn with
| nil => cases (induces_nil_iff.mp r).1
| append_singleton u a _ =>
obtain ⟨_, _, _, l⟩ := induces_snoc_iff.mp r
exact hd l
obtain ⟨T, rT⟩ := mem_interconnect.mp (Part.get_mem hdK)
exact ⟨_, T, rT⟩
obtain ⟨o₁, T, rT⟩ := hK
exact ⟨T, induces_joint_iff.mpr ⟨K, r, o₁, rT⟩⟩
· rintro ⟨T, r⟩
obtain ⟨K, rK, -⟩ := induces_joint_iff.mp r
exact ⟨K, rK⟩
end Vanishing
/-! ## Relabeling and wiring -/
section Naturality
variable (s : System X Y) (route : Y → B ⊕ X) (inj : A → X)
/-- Relabeling the exposed side of a wired system relabels its routing table. -/
lemma relabel_interconnect {A' B' : Type} (f : A' → A) (g : B → B') :
relabel (interconnect s route inj) f g = interconnect s (fun y => (route y).map g id) (inj ∘ f) := by
have hl : ∀ {h o h'}, Exchange s route h o h' →
Exchange s (fun y => (route y).map g id) h (o.map g) h' := by
intro h o h' l
induction l with
| silent h hd => exact Exchange.silent _ hd
| out h y b hy hr => exact Exchange.out _ y (g b) hy (by simp [hr])
| feed h y x _ _ hy hr _ ih => exact Exchange.feed _ y x _ _ hy (by simp [hr]) ih
have hr : ∀ {u o H}, Induces s route inj (u.map f) o H →
Induces s (fun y => (route y).map g id) (inj ∘ f) u (o.map g) H := by
intro u
induction u using List.reverseRecOn with
| nil =>
intro o H r
obtain ⟨rfl, rfl⟩ := induces_nil_iff.mp r
exact Induces.nil
| append_singleton u a ih =>
intro o H r
rw [List.map_append, List.map_singleton] at r
obtain ⟨o₀, H₀, r₀, l⟩ := induces_snoc_iff.mp r
exact Induces.snoc _ _ _ _ _ _ (ih r₀) (hl l)
-- every induced history of the relabeled table comes from one of the original table
have hl' : ∀ {h o' h'}, Exchange s (fun y => (route y).map g id) h o' h' →
∃ o, Exchange s route h o h' ∧ o' = o.map g := by
intro h o' h' l
induction l with
| silent h hd => exact ⟨none, Exchange.silent _ hd, rfl⟩
| out h y b hy hr =>
cases e : route y with
| inl b₀ => rw [e] at hr; cases hr; exact ⟨some b₀, Exchange.out _ y b₀ hy e, rfl⟩
| inr x => rw [e] at hr; cases hr
| feed h y x _ _ hy hr _ ih =>
obtain ⟨o, l, rfl⟩ := ih
cases e : route y with
| inl b₀ => rw [e] at hr; cases hr
| inr x₀ => rw [e] at hr; cases hr; exact ⟨o, Exchange.feed _ y x₀ _ _ hy e l, rfl⟩
have hr' : ∀ {u o' H}, Induces s (fun y => (route y).map g id) (inj ∘ f) u o' H →
∃ o, Induces s route inj (u.map f) o H ∧ o' = o.map g := by
intro u
induction u using List.reverseRecOn with
| nil =>
intro o' H r
obtain ⟨rfl, rfl⟩ := induces_nil_iff.mp r
exact ⟨none, Induces.nil, rfl⟩
| append_singleton u a ih =>
intro o' H r
obtain ⟨o₀, H₀, r₀, l⟩ := induces_snoc_iff.mp r
obtain ⟨o₁, r₁, -⟩ := ih r₀
obtain ⟨o, l', e⟩ := hl' l
refine ⟨o, ?_, e⟩
rw [List.map_append, List.map_singleton]
exact Induces.snoc _ _ _ _ _ _ r₁ l'
apply system_ext
intro u b'
simp only [relabel, Part.mem_map_iff, mem_interconnect]
constructor
· rintro ⟨b, ⟨H, r⟩, rfl⟩
exact ⟨H, hr r⟩
· rintro ⟨H, r⟩
obtain ⟨o, r', e⟩ := hr' r
cases o with
| none => cases e
| some b => cases e; exact ⟨b, ⟨H, r'⟩, rfl⟩
/-- An induced history of a relabeled system maps to one of the system itself. -/
lemma induces_relabel {X' Y' : Type} {s : System X Y} {f : X' → X} {g : Y → Y'}
{route : Y' → B ⊕ X'} {inj : A → X'} {u o H}
(r : Induces (relabel s f g) route inj u o H) :
Induces s (fun y => (route (g y)).map id f) (f ∘ inj) u o (H.map f) := by
have hl : ∀ {h o h'}, Exchange (relabel s f g) route h o h' →
Exchange s (fun y => (route (g y)).map id f) (h.map f) o (h'.map f) := by
intro h o h' l
induction l with
| silent h hd => exact Exchange.silent _ (by simpa [relabel] using hd)
| out h y b hy hr =>
obtain ⟨y₀, hy₀, rfl⟩ := Part.mem_map_iff _ |>.mp hy
exact Exchange.out _ y₀ b hy₀ (by simp [hr])
| feed h y x _ _ hy hr _ ih =>
obtain ⟨y₀, hy₀, rfl⟩ := Part.mem_map_iff _ |>.mp hy
exact Exchange.feed _ y₀ (f x) _ _ hy₀ (by simp [hr]) (by simpa using ih)
induction u using List.reverseRecOn generalizing o H with
| nil =>
obtain ⟨rfl, rfl⟩ := induces_nil_iff.mp r
exact Induces.nil
| append_singleton u a ih =>
obtain ⟨o₀, H₀, r₀, l⟩ := induces_snoc_iff.mp r
exact Induces.snoc _ _ _ _ _ _ (ih r₀) (by simpa using hl l)
lemma Converges.relabel {X' Y' : Type} {s : System X Y} {f : X' → X} {g : Y → Y'}
{route : Y' → B ⊕ X'} {inj : A → X'} (hc : Converges (SystemAlgebra.relabel s f g) route inj) :
Converges s (fun y => (route (g y)).map id f) (f ∘ inj) := fun u => by
obtain ⟨o, H, r⟩ := hc u
exact ⟨o, _, induces_relabel r⟩
/-- Relabeling the inside of a wired system is absorbed by the routing table. -/
lemma interconnect_relabel {X' Y' : Type} (s : System X Y) (f : X' → X) (g : Y → Y')
(route : Y' → B ⊕ X') (inj : A → X') :
interconnect (relabel s f g) route inj =
interconnect s (fun y => (route (g y)).map id f) (f ∘ inj) := by
have hl : ∀ {h o h'}, Exchange (relabel s f g) route h o h' →
Exchange s (fun y => (route (g y)).map id f) (h.map f) o (h'.map f) := by
intro h o h' l
induction l with
| silent h hd => exact Exchange.silent _ (by simpa [relabel] using hd)
| out h y b hy hr =>
obtain ⟨y₀, hy₀, rfl⟩ := Part.mem_map_iff _ |>.mp hy
exact Exchange.out _ y₀ b hy₀ (by simp [hr])
| feed h y x _ _ hy hr _ ih =>
obtain ⟨y₀, hy₀, rfl⟩ := Part.mem_map_iff _ |>.mp hy
exact Exchange.feed _ y₀ (f x) _ _ hy₀ (by simp [hr]) (by simpa using ih)
have hr : ∀ {u o H}, Induces (relabel s f g) route inj u o H →
Induces s (fun y => (route (g y)).map id f) (f ∘ inj) u o (H.map f) := by
intro u
induction u using List.reverseRecOn with
| nil =>
intro o H r
obtain ⟨rfl, rfl⟩ := induces_nil_iff.mp r
exact Induces.nil
| append_singleton u a ih =>
intro o H r
obtain ⟨o₀, H₀, r₀, l⟩ := induces_snoc_iff.mp r
exact Induces.snoc _ _ _ _ _ _ (ih r₀) (by simpa using hl l)
have hl' : ∀ {T o T'}, Exchange s (fun y => (route (g y)).map id f) T o T' →
∀ h, T = h.map f → ∃ h', T' = h'.map f ∧ Exchange (relabel s f g) route h o h' := by
intro T o T' l
induction l with
| silent T hd =>
rintro h rfl
exact ⟨h, rfl, Exchange.silent _ (by simpa [relabel] using hd)⟩
| out T y b hy hr =>
rintro h rfl
refine ⟨h, rfl, Exchange.out _ (g y) b (Part.mem_map_iff _ |>.mpr ⟨y, hy, rfl⟩) ?_⟩
cases e : route (g y) with
| inl b₀ => rw [e] at hr; cases hr; rfl
| inr x => rw [e] at hr; cases hr
| feed T y x _ _ hy hr _ ih =>
rintro h rfl
cases e : route (g y) with
| inl b₀ => rw [e] at hr; cases hr
| inr x₀ =>
rw [e] at hr; cases hr
obtain ⟨h', rfl, l⟩ := ih (h ++ [x₀]) (by simp)
exact ⟨h', rfl, Exchange.feed _ (g y) x₀ _ _
(Part.mem_map_iff _ |>.mpr ⟨y, hy, rfl⟩) e l⟩
have hr' : ∀ {u o T}, Induces s (fun y => (route (g y)).map id f) (f ∘ inj) u o T →
∃ H, T = H.map f ∧ Induces (relabel s f g) route inj u o H := by
intro u
induction u using List.reverseRecOn with
| nil =>
intro o T r
obtain ⟨rfl, rfl⟩ := induces_nil_iff.mp r
exact ⟨[], rfl, Induces.nil⟩
| append_singleton u a ih =>
intro o T r
obtain ⟨o₀, T₀, r₀, l⟩ := induces_snoc_iff.mp r
obtain ⟨H₀, rfl, r₁⟩ := ih r₀
obtain ⟨H, rfl, l'⟩ := hl' l (H₀ ++ [inj a]) (by simp)
exact ⟨H, rfl, Induces.snoc _ _ _ _ _ _ r₁ l'⟩
apply system_ext
intro u b
rw [mem_interconnect, mem_interconnect]
constructor
· rintro ⟨H, r⟩; exact ⟨_, hr r⟩
· rintro ⟨T, r⟩
obtain ⟨H, -, r'⟩ := hr' r
exact ⟨H, r'⟩
end Naturality
/-! ## Wiring without feedback -/
section NoFeedback
variable {s : System X Y} {route : Y → B ⊕ X} {inj : A → X} {g : Y → B}
/-- A routing that feeds nothing back: the wiring is a relabeling. -/
lemma interconnect_no_feedback (hs : SilentAtEmpty s) (hr : ∀ y, route y = .inl (g y)) :
interconnect s route inj = relabel s inj g := by
have ex : ∀ H, ∃ o, Exchange s route H o H := by
intro H
by_cases hd : (s H).Dom
· exact ⟨_, Exchange.out H _ _ (Part.get_mem hd) (hr _)⟩
· exact ⟨none, Exchange.silent H hd⟩
have ind : ∀ u, ∃ o, Induces s route inj u o (u.map inj) := by
intro u
induction u using List.reverseRecOn with
| nil => exact ⟨none, Induces.nil⟩
| append_singleton u a ih =>
obtain ⟨o, r⟩ := ih
obtain ⟨o', l⟩ := ex (u.map inj ++ [inj a])
exact ⟨o', by simpa using Induces.snoc _ _ _ _ _ _ r l⟩
funext u
apply Part.ext
intro b
rw [mem_interconnect]
rcases List.eq_nil_or_concat u with rfl | ⟨u, a, rfl⟩
· simp only [relabel, List.map_nil]
constructor
· rintro ⟨H, r⟩; cases (induces_nil_iff.mp r).1
· intro hb
obtain ⟨y, hy, -⟩ := Part.mem_map_iff _ |>.mp hb
exact (hs (Part.dom_iff_mem.mpr ⟨y, hy⟩)).elim
· rw [List.concat_eq_append]
simp only [relabel, List.map_append, List.map_singleton]
obtain ⟨o₀, r₀⟩ := ind u
constructor
· rintro ⟨H, r⟩
obtain ⟨o₁, H₀, r₁, l⟩ := induces_snoc_iff.mp r
obtain ⟨-, rfl⟩ := r₁.det r₀
cases l with
| out _ y _ hy hb => rw [hr] at hb; cases hb; exact Part.mem_map _ hy
| feed _ y _ _ _ _ hb _ => rw [hr] at hb; cases hb
· intro hb
obtain ⟨y, hy, rfl⟩ := Part.mem_map_iff _ |>.mp hb
exact ⟨_, Induces.snoc _ _ _ _ _ _ r₀ (Exchange.out _ y _ hy (hr y))⟩
end NoFeedback
/-! ## A sufficient condition for convergence -/
section Termination
variable {s : System X Y} {route : Y → B ⊕ X}
/-- Exchanges stop when every continued feedback step decreases a measure. -/
lemma exchanges_of_measure (μ : List X → ℕ)
(hμ : ∀ H y x, y ∈ s H → route y = Sum.inr x → (s (H ++ [x])).Dom → μ (H ++ [x]) < μ H)
(H : List X) : ∃ o H', Exchange s route H o H' := by
induction h : μ H using Nat.strong_induction_on generalizing H with
| _ n ih =>
by_cases hd : (s H).Dom
· obtain ⟨y, hy⟩ := Part.dom_iff_mem.mp hd
cases e : route y with
| inl b => exact ⟨_, _, Exchange.out _ y b hy e⟩
| inr x =>
by_cases hd' : (s (H ++ [x])).Dom
· obtain ⟨o, H', l⟩ := ih _ (h ▸ hμ H y x hy e hd') (H ++ [x]) rfl
exact ⟨o, H', Exchange.feed _ y x _ _ hy e l⟩
· exact ⟨none, _, Exchange.feed _ y x _ _ hy e (Exchange.silent _ hd')⟩
· exact ⟨none, H, Exchange.silent _ hd⟩
/-- T2 (first half): the internal exchanges of a terminating system stop. -/
lemma Terminating.converges {s : System X Y} (hs : Terminating s) (route : Y → B ⊕ X)
(inj : A → X) : Converges s route inj := by
obtain ⟨μ, hμ⟩ := hs
exact converges_of_exchanges (exchanges_of_measure μ (fun H y x _ _ hd => hμ H x hd))
end Termination
/-! ## n-ary parallel composition (spec §3) -/
section Par
variable {K : Type} {A B : K → Type}
/-- The inputs of `h` addressed to component `k`, with the tags removed. -/
noncomputable def restrict (k : K) : List (Σ k, A k) → List (A k)
| [] => []
| x :: h => if e : x.1 = k then (e ▸ x.2) :: restrict k h else restrict k h
@[simp] lemma restrict_nil (k : K) : restrict k ([] : List (Σ k, A k)) = [] := rfl
@[simp] lemma restrict_cons_self (k : K) (a : A k) (h : List (Σ k, A k)) :
restrict k (⟨k, a⟩ :: h) = a :: restrict k h := by
simp [restrict]
@[simp] lemma restrict_cons_ne {k k' : K} (hk : k' ≠ k) (a : A k') (h : List (Σ k, A k)) :
restrict k (⟨k', a⟩ :: h) = restrict k h := by
simp [restrict, hk]
@[simp] lemma restrict_append (k : K) (h h' : List (Σ k, A k)) :
restrict k (h ++ h') = restrict k h ++ restrict k h' := by
induction h with
| nil => rfl
| cons x h ih =>
obtain ⟨k', a⟩ := x
by_cases hk : k' = k
· subst hk; simp [ih]
· simp [hk, ih]
lemma length_restrict_le (k : K) (h : List (Σ k, A k)) : (restrict k h).length ≤ h.length := by
induction h with
| nil => simp
| cons x h ih =>
obtain ⟨k', a⟩ := x
by_cases hk : k' = k
· subst hk; simpa using ih
· simpa [hk] using ih.trans (Nat.le_succ _)
lemma length_restrict_singleton (k : K) (x : Σ k, A k) :
(restrict k [x]).length = if x.1 = k then 1 else 0 := by
obtain ⟨k', a⟩ := x
by_cases hk : k' = k
· subst hk; simp
· simp [hk]
/-- A history has as many inputs as its components together. -/
lemma length_eq_sum_restrict [Fintype K] (h : List (Σ k, A k)) :
h.length = ∑ k, (restrict k h).length := by
classical
induction h with
| nil => simp
| cons x h ih =>
obtain ⟨k', a⟩ := x
have step : ∀ k, (restrict k (⟨k', a⟩ :: h)).length =
(restrict k h).length + if k' = k then 1 else 0 := by
intro k
by_cases hk : k' = k
· subst hk; simp
· simp [hk]
rw [List.length_cons, ih, Finset.sum_congr rfl fun k _ => step k, Finset.sum_add_distrib,
Finset.sum_eq_single k' (fun k _ hk => if_neg (Ne.symm hk)) (by simp), if_pos rfl]
/-- Parallel composition: an input `⟨k, a⟩` is answered by component `k` on
its own restricted history. Undefined on the empty history. -/
noncomputable def parAll (s : ∀ k, System (A k) (B k)) : System (Σ k, A k) (Σ k, B k) :=
fun h =>
match h.getLast? with
| none => Part.none
| some x => (s x.1 (restrict x.1 h)).map (Sigma.mk x.1)
variable (s : ∀ k, System (A k) (B k))
@[simp] lemma parAll_nil : parAll s [] = Part.none := rfl
lemma parAll_snoc (h : List (Σ k, A k)) (k : K) (a : A k) :
parAll s (h ++ [⟨k, a⟩]) = (s k (restrict k h ++ [a])).map (Sigma.mk k) := by
simp [parAll]
lemma parAll_silentAtEmpty : SilentAtEmpty (parAll s) := by
simp [SilentAtEmpty]
/-- Reverse induction on histories, split by the component of the last input. -/
lemma parAll_ext {S : System (Σ k, A k) (Σ k, B k)} {T : System (Σ k, A k) (Σ k, B k)}
(h0 : S [] = T []) (h1 : ∀ h k a, S (h ++ [⟨k, a⟩]) = T (h ++ [⟨k, a⟩])) : S = T := by
funext h
induction h using List.reverseRecOn with
| nil => exact h0
| append_singleton h x _ => exact h1 h x.1 x.2
/-- P3: relabeling distributes over parallel composition. -/
lemma parAll_relabel {A' B' : K → Type} (f : ∀ k, A' k → A k) (g : ∀ k, B k → B' k) :
parAll (fun k => relabel (s k) (f k) (g k)) =
relabel (parAll s) (Sigma.map id f) (Sigma.map id g) := by
have hr : ∀ (k : K) (h : List (Σ k, A' k)),
restrict k (h.map (Sigma.map id f)) = (restrict k h).map (f k) := by
intro k h
induction h with
| nil => rfl
| cons x h ih =>
obtain ⟨k', a⟩ := x
by_cases hk : k' = k
· subst hk; simp [Sigma.map, ih]
· simp [Sigma.map, hk, ih]
apply parAll_ext
· simp [relabel]
· intro h k a
simp [relabel, parAll_snoc, hr, Part.map_map, Sigma.map, Function.comp_def]
/-- Flatten a doubly tagged message. -/
def flatMsg {L : K → Type} {A : ∀ k, L k → Type} (x : Σ k, Σ l, A k l) :
Σ p : Σ k, L k, A p.1 p.2 := ⟨⟨x.1, x.2.1⟩, x.2.2⟩
/-- Nest a message tagged by a pair. -/
def nestMsg {L : K → Type} {A : ∀ k, L k → Type} (y : Σ p : Σ k, L k, A p.1 p.2) :
Σ k, Σ l, A k l := ⟨y.1.1, ⟨y.1.2, y.2⟩⟩
/-- P2: flattening a parallel composition of parallel compositions. -/
lemma parAll_flatten {L : K → Type} {A B : ∀ k, L k → Type}
(s : ∀ k l, System (A k l) (B k l)) :
parAll (fun k => parAll (s k)) =
relabel (parAll fun p : Σ k, L k => s p.1 p.2) flatMsg nestMsg := by
have hr : ∀ (k : K) (l : L k) (h : List (Σ k, Σ l, A k l)),
restrict l (restrict k h) = restrict (⟨k, l⟩ : Σ k, L k) (h.map flatMsg) := by
intro k l h
induction h with
| nil => rfl
| cons x h ih =>
obtain ⟨k', l', a⟩ := x
by_cases hk : k' = k
· subst hk
by_cases hl : l' = l
· subst hl; simp [flatMsg, ih]
· have : (⟨k', l'⟩ : Σ k, L k) ≠ ⟨k', l⟩ := by simpa using hl
simp [flatMsg, hl, this, ih]
· have : (⟨k', l'⟩ : Σ k, L k) ≠ ⟨k, l⟩ := by
intro e; exact hk (congrArg Sigma.fst e)
simp [flatMsg, hk, this, ih]
apply parAll_ext
· simp [relabel]
· intro h k x
obtain ⟨l, a⟩ := x
simp only [relabel, List.map_append, List.map_singleton, parAll_snoc, Part.map_map]
rw [show flatMsg (⟨k, ⟨l, a⟩⟩ : Σ k, Σ l, A k l) = ⟨⟨k, l⟩, a⟩ from rfl, parAll_snoc, ← hr]
rw [Part.map_map]
rfl
/-- P1: reindexing a parallel composition along a bijection of indices. -/
lemma parAll_reindex {K' : Type} (τ : K' ≃ K) :
parAll (fun k' => s (τ k')) =
relabel (parAll s) (Equiv.sigmaCongrLeft τ (β := A))
(Equiv.sigmaCongrLeft τ (β := B)).symm := by
have hr : ∀ (k' : K') (h : List (Σ k', A (τ k'))),
restrict (τ k') (h.map (Equiv.sigmaCongrLeft τ (β := A))) = restrict k' h := by
intro k' h
induction h with
| nil => rfl
| cons x h ih =>
obtain ⟨k'', a⟩ := x
rw [List.map_cons, show (Equiv.sigmaCongrLeft τ (β := A)) ⟨k'', a⟩ = ⟨τ k'', a⟩ from rfl]
by_cases hk : k'' = k'
· subst hk; rw [restrict_cons_self, restrict_cons_self, ih]
· have : τ k'' ≠ τ k' := fun e => hk (τ.injective e)
rw [restrict_cons_ne this, restrict_cons_ne hk, ih]
apply parAll_ext
· simp [relabel]
· intro h k' a
have e : (Equiv.sigmaCongrLeft τ (β := A)) ⟨k', a⟩ = ⟨τ k', a⟩ := rfl
simp only [relabel, List.map_append, List.map_singleton, e, parAll_snoc, hr, Part.map_map]
congr 1
funext b
apply (Equiv.sigmaCongrLeft τ (β := B)).injective
simp
end Par
/-! ## Locality (spec §6, C3) -/
section Locality
variable {K : Type} {A B : K → Type} {A₀ B₀ : Type}
/-- A family with one distinguished component `none`. -/
abbrev optF (Z : Type) (F : K → Type) : Option K → Type
| none => Z
| some k => F k
/-- A family of systems with one distinguished component `none`. -/
def optS {Z W : Type} (s₀ : System Z W) (t : ∀ k, System (A k) (B k)) :
∀ o, System (optF Z A o) (optF W B o)
| none => s₀
| some k => t k
@[simp] lemma restrict_none_cons_none {Z : Type} (z : Z) (h : List (Σ o, optF Z A o)) :
restrict (A := optF Z A) none (⟨none, z⟩ :: h) = z :: restrict none h :=
restrict_cons_self (A := optF Z A) none z h
@[simp] lemma restrict_some_cons_none {Z : Type} (k : K) (z : Z) (h : List (Σ o, optF Z A o)) :
restrict (A := optF Z A) (some k) (⟨none, z⟩ :: h) = restrict (some k) h :=
restrict_cons_ne (A := optF Z A) (k := some k) (k' := none) (by simp) z h
@[simp] lemma restrict_none_cons_some {Z : Type} (k : K) (a : A k) (h : List (Σ o, optF Z A o)) :
restrict (A := optF Z A) none (⟨some k, a⟩ :: h) = restrict none h :=
restrict_cons_ne (A := optF Z A) (k := none) (k' := some k) (by simp) a h
@[simp] lemma restrict_some_cons_some {Z : Type} (k : K) (a : A k) (h : List (Σ o, optF Z A o)) :
restrict (A := optF Z A) (some k) (⟨some k, a⟩ :: h) = a :: restrict (some k) h :=
restrict_cons_self (A := optF Z A) (some k) a h
@[simp] lemma restrict_some_cons_some_ne {Z : Type} {k k' : K} (hk : k' ≠ k) (a : A k')
(h : List (Σ o, optF Z A o)) :
restrict (A := optF Z A) (some k) (⟨some k', a⟩ :: h) = restrict (some k) h :=
restrict_cons_ne (A := optF Z A) (k := some k) (k' := some k') (by simpa using hk) a h