From 749f7dd1feab0e4ccea5e2379e609024a7226fbc Mon Sep 17 00:00:00 2001 From: Marc Ilunga Date: Tue, 24 Nov 2026 12:06:33 +0100 Subject: [PATCH] Add named projections of parallel typed resources --- .../Instances/Algebra.lean | 144 +++++- src/ConstructiveCryptography/Notation.lean | 32 +- src/RandomSystems/Typed/Context.lean | 433 +++++++++++++++++- src/RandomSystems/Typed/README.md | 29 +- src/Tests.lean | 1 + src/Tests/ParallelProjections.lean | 279 +++++++++++ 6 files changed, 894 insertions(+), 24 deletions(-) create mode 100644 src/Tests/ParallelProjections.lean diff --git a/src/ConstructiveCryptography/Instances/Algebra.lean b/src/ConstructiveCryptography/Instances/Algebra.lean index 3b01fad..8622aba 100644 --- a/src/ConstructiveCryptography/Instances/Algebra.lean +++ b/src/ConstructiveCryptography/Instances/Algebra.lean @@ -3,6 +3,7 @@ module public import RandomSystems.Typed.Distinguisher public import ConstructiveCryptography.CryptographicAlgebra.DistinguisherAction public import RandomSystems.Distance.RevealedHCoefficient +public import RandomSystems.Typed.Context public import RandomSystems.Typed.ParallelAttachment public import RandomSystems.Typed.SystemCoherence public import RandomSystems.Typed.Distance @@ -70,6 +71,13 @@ domination law needed by statistical steps of a substitution chain. * `Interface.cc_distance_eq`: the distance is the transcript distance of the random systems, by definition * `Interface.distance_le_of_hCoefficient`: an H-coefficient argument bounds the distance +* `Interface.projectLeft_eq`, `Interface.projectRight_eq`, `Interface.projectLeft_smul_parallel`, + `Interface.projectRight_smul_parallel`, `Interface.projectLeft_smul_sample_parallel`, + `Interface.projectRight_smul_sample_parallel`, `Interface.projectLeft_comp_tensor`, + `Interface.projectLeft_comp_whiskerRight`, `Interface.projectRight_comp_tensor`, + `Interface.projectRight_comp_whiskerLeft`, `Interface.projectLeft_smul_tensor_smul`, + `Interface.projectRight_smul_tensor_smul`: instance-level laws of `TypedConverter.projectLeft` and + `TypedConverter.projectRight` -/ @[expose] public section @@ -107,22 +115,12 @@ lemma parallel_assoc {A B C : Interface} (R : TypedSystem A) (S : TypedSystem B) simp only [← Equiv.prodCongr_symm, List.map_map, Equiv.self_comp_symm, List.map_id] lemma parallel_dummy_left {A : Interface} (R : TypedSystem A) : - ((leftUnitor A).inv • TypedSystem.parallel TypedSystem.dummy R : TypedSystem A) = R := by - apply Subtype.ext - apply RandomSystem.ext - intro h - simp only [leftUnitor, isoOf] - rw [TypedConverter.rename_mass, TypedSystem.parallel_unit_left_mass] - simp only [← Equiv.prodCongr_symm, List.map_map, Equiv.self_comp_symm, List.map_id] + ((leftUnitor A).inv • TypedSystem.parallel TypedSystem.dummy R : TypedSystem A) = R := + TypedConverter.leftUnitor_inv_attach R lemma parallel_dummy_right {A : Interface} (R : TypedSystem A) : - ((rightUnitor A).inv • TypedSystem.parallel R TypedSystem.dummy : TypedSystem A) = R := by - apply Subtype.ext - apply RandomSystem.ext - intro h - simp only [rightUnitor, isoOf] - rw [TypedConverter.rename_mass, TypedSystem.parallel_unit_right_mass] - simp only [← Equiv.prodCongr_symm, List.map_map, Equiv.self_comp_symm, List.map_id] + ((rightUnitor A).inv • TypedSystem.parallel R TypedSystem.dummy : TypedSystem A) = R := + TypedConverter.rightUnitor_inv_attach R lemma parallel_swap {A B : Interface} (R : TypedSystem A) (S : TypedSystem B) : ((swap A B).hom • TypedSystem.parallel S R : TypedSystem (tensor A B)) = @@ -303,4 +301,122 @@ noncomputable instance distinguisherAdvantageDominated rw [cc_distance_eq] exact d.advantage_le_transcriptDistance R S +/-- In the monoidal category of interfaces, `projectLeft A B` is +`(ρ_ A).inv ≫ (𝟙 A ⊗ₘ discard B)`. -/ +lemma projectLeft_eq + -- `A` is the retained outside interface; `B` is the hidden parallel component. + (A B : Interface) : + (TypedConverter.projectLeft A B : A ⟶ A ⊗ B) = + (ρ_ A).inv ≫ (𝟙 A ⊗ₘ TypedConverter.discard B) := rfl + +/-- In the monoidal category of interfaces, `projectRight A B` is +`(λ_ B).inv ≫ (discard A ⊗ₘ 𝟙 B)`. -/ +lemma projectRight_eq + -- `A` is the hidden parallel component; `B` is the retained outside interface. + (A B : Interface) : + (TypedConverter.projectRight A B : B ⟶ A ⊗ B) = + (λ_ B).inv ≫ (TypedConverter.discard A ⊗ₘ 𝟙 B) := rfl + +/-- Projecting the left component of a parallel resource returns `R`. -/ +lemma projectLeft_smul_parallel + -- `R` on `A` is retained; `S` on `B` is discarded. + {A B : Interface} (R : TypedSystem A) (S : TypedSystem B) : + TypedConverter.projectLeft A B • + ConstructiveCryptography.CryptographicAlgebra.parallel R S = R := + TypedConverter.projectLeft_smul_parallel R S + +/-- Projecting the right component of a parallel resource returns `S`. -/ +lemma projectRight_smul_parallel + -- `R` on `A` is discarded; `S` on `B` is retained. + {A B : Interface} (R : TypedSystem A) (S : TypedSystem B) : + TypedConverter.projectRight A B • + ConstructiveCryptography.CryptographicAlgebra.parallel R S = S := + TypedConverter.projectRight_smul_parallel R S + +/-- Projecting the left component of a sampled parallel resource preserves the retained draw +`k ← P` and returns `TypedSystem.sample P R`. -/ +lemma projectLeft_smul_sample_parallel + -- `A`, `B` are the ordered component interfaces; `K` carries the shared initial draw. + {A B : Interface} {K : Type} + -- `P` is the normalized law drawn once before interaction. + (P : Probability.Distribution K) [Probability.Distribution.IsProbability P] + -- `R k` is retained and `S k` is discarded for that draw `k`. + (R : K → TypedSystem A) (S : K → TypedSystem B) : + TypedConverter.projectLeft A B • TypedSystem.sample P (fun k => + ConstructiveCryptography.CryptographicAlgebra.parallel (R k) (S k)) = + TypedSystem.sample P R := + TypedConverter.projectLeft_smul_sample_parallel P R S + +/-- Projecting the right component of a sampled parallel resource preserves the retained draw +`k ← P` and returns `TypedSystem.sample P S`. -/ +lemma projectRight_smul_sample_parallel + -- `A`, `B` are the ordered component interfaces; `K` carries the shared initial draw. + {A B : Interface} {K : Type} + -- `P` is the normalized law drawn once before interaction. + (P : Probability.Distribution K) [Probability.Distribution.IsProbability P] + -- `R k` is discarded and `S k` is retained for that draw `k`. + (R : K → TypedSystem A) (S : K → TypedSystem B) : + TypedConverter.projectRight A B • TypedSystem.sample P (fun k => + ConstructiveCryptography.CryptographicAlgebra.parallel (R k) (S k)) = + TypedSystem.sample P S := + TypedConverter.projectRight_smul_sample_parallel P R S + +/-- Naturality of `projectLeft` under tensor of arrows: +`projectLeft A B ≫ (α ⊗ₘ β) = α ≫ projectLeft A' B'`. -/ +lemma projectLeft_comp_tensor + -- `A`, `A'` are the retained interfaces of `α`; `B`, `B'` are the hidden interfaces of `β`. + {A A' B B' : Interface} (α : A ⟶ A') (β : B ⟶ B') : + (TypedConverter.projectLeft A B : A ⟶ A ⊗ B) ≫ (α ⊗ₘ β) = + α ≫ (TypedConverter.projectLeft A' B' : A' ⟶ A' ⊗ B') := + TypedConverter.projectLeft_comp_parallel α β + +/-- Left projection commutes with a converter `α` on the retained component: +`projectLeft A B ≫ (α ⊗ₘ 𝟙 B) = α ≫ projectLeft A' B`. -/ +lemma projectLeft_comp_whiskerRight + -- `A`, `A'` are the outside and inside interfaces of `α`; `B` is the hidden component. + {A A' B : Interface} (α : A ⟶ A') : + (TypedConverter.projectLeft A B : A ⟶ A ⊗ B) ≫ (α ⊗ₘ 𝟙 B) = + α ≫ (TypedConverter.projectLeft A' B : A' ⟶ A' ⊗ B) := + TypedConverter.projectLeft_comp_parallel_id α + +/-- Naturality of `projectRight` under tensor of arrows: +`projectRight A B ≫ (α ⊗ₘ β) = β ≫ projectRight A' B'`. -/ +lemma projectRight_comp_tensor + -- `A`, `A'` are the hidden interfaces of `α`; `B`, `B'` are the retained interfaces of `β`. + {A A' B B' : Interface} (α : A ⟶ A') (β : B ⟶ B') : + (TypedConverter.projectRight A B : B ⟶ A ⊗ B) ≫ (α ⊗ₘ β) = + β ≫ (TypedConverter.projectRight A' B' : B' ⟶ A' ⊗ B') := + TypedConverter.projectRight_comp_parallel α β + +/-- Right projection commutes with a converter `β` on the retained component: +`projectRight A B ≫ (𝟙 A ⊗ₘ β) = β ≫ projectRight A B'`. -/ +lemma projectRight_comp_whiskerLeft + -- `A` is the hidden component; `B`, `B'` are the outside and inside interfaces of `β`. + {A B B' : Interface} (β : B ⟶ B') : + (TypedConverter.projectRight A B : B ⟶ A ⊗ B) ≫ (𝟙 A ⊗ₘ β) = + β ≫ (TypedConverter.projectRight A B' : B' ⟶ A ⊗ B') := + TypedConverter.projectRight_comp_parallel_id β + +/-- Attaching `α ⊗ₘ β` before `projectLeft A B` equals attaching `α` after `projectLeft A' B'`, +for any (possibly correlated) typed system `T` on `A' ⊗ B'`. -/ +lemma projectLeft_smul_tensor_smul + -- `A`, `A'` are the retained interfaces and `B`, `B'` the hidden interfaces. + {A A' B B' : Interface} + -- `α` acts on the retained side, `β` on the hidden side, and `T` is any joint system. + (α : A ⟶ A') (β : B ⟶ B') (T : TypedSystem (A' ⊗ B')) : + TypedConverter.projectLeft A B • ((α ⊗ₘ β) • T) = + α • (TypedConverter.projectLeft A' B' • T) := + TypedConverter.projectLeft_smul_parallel_smul α β T + +/-- Attaching `α ⊗ₘ β` before `projectRight A B` equals attaching `β` after `projectRight A' B'`, +for any (possibly correlated) typed system `T` on `A' ⊗ B'`. -/ +lemma projectRight_smul_tensor_smul + -- `A`, `A'` are the hidden interfaces and `B`, `B'` the retained interfaces. + {A A' B B' : Interface} + -- `α` acts on the hidden side, `β` on the retained side, and `T` is any joint system. + (α : A ⟶ A') (β : B ⟶ B') (T : TypedSystem (A' ⊗ B')) : + TypedConverter.projectRight A B • ((α ⊗ₘ β) • T) = + β • (TypedConverter.projectRight A' B' • T) := + TypedConverter.projectRight_smul_parallel_smul α β T + end SystemAlgebra.Interface diff --git a/src/ConstructiveCryptography/Notation.lean b/src/ConstructiveCryptography/Notation.lean index f871afd..afa5ca2 100644 --- a/src/ConstructiveCryptography/Notation.lean +++ b/src/ConstructiveCryptography/Notation.lean @@ -17,9 +17,11 @@ instances. This frontend also accepts direct typed systems and converters in `Δ metric or composition. Direct typed operations print in the same notation when the selected instances agree. -`cc_normalize` applies the serial, parallel and sampling laws: +`cc_normalize` applies the serial, parallel, projection and sampling laws: * `(α ⊗ₘ β) • (R ∥ S) = α • R ∥ β • S`; +* `TypedConverter.projectLeft A B • (R ∥ S) = R` and + `TypedConverter.projectRight A B • (R ∥ S) = S`; * `α • sample P R = sample P (fun s => α • R s)` for normalized `P`; * `(⨂ₘ k, α k) • (∥ k, R k) = ∥ k, α k • R k`. @@ -342,6 +344,8 @@ attribute [cc_normalization] TypedConverter.parallel_attach TypedConverter.parallelAll_attach TypedConverter.parallelAllContext_smul + TypedConverter.projectLeft_smul_parallel + TypedConverter.projectRight_smul_parallel -- Hoist samples after structural attachment, so serial composition exposes a component's owner. attribute [cc_normalization 900] TypedConverter.smul_sample @@ -366,6 +370,32 @@ simproc [cc_normalization] tensorSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do unless ← withDefault (isDefEq e lhs) do return .continue return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) } +open Lean Meta Simp in +/-- `projectLeft_smul_parallel` whatever the spelling of the interfaces: the action of +`TypedConverter.projectLeft A B` on `R ∥ S`, with types written through `⊗` or `Interface.tensor`, +returns `R`. -/ +simproc [cc_normalization] projectLeftSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do + let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue + let_expr TypedConverter.projectLeft A B := f | return .continue + let_expr TypedSystem.parallel _ _ R S := x | return .continue + let proof := mkAppN (mkConst ``TypedConverter.projectLeft_smul_parallel) #[A, B, R, S] + let_expr Eq _ lhs rhs := ← inferType proof | return .continue + unless ← withDefault (isDefEq e lhs) do return .continue + return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) } + +open Lean Meta Simp in +/-- `projectRight_smul_parallel` whatever the spelling of the interfaces: the action of +`TypedConverter.projectRight A B` on `R ∥ S`, with types written through `⊗` or `Interface.tensor`, +returns `S`. -/ +simproc [cc_normalization] projectRightSmul (@HSMul.hSMul _ _ _ _ _ _) := fun e => do + let_expr HSMul.hSMul _ _ _ _ f x := e | return .continue + let_expr TypedConverter.projectRight A B := f | return .continue + let_expr TypedSystem.parallel _ _ R S := x | return .continue + let proof := mkAppN (mkConst ``TypedConverter.projectRight_smul_parallel) #[A, B, R, S] + let_expr Eq _ lhs rhs := ← inferType proof | return .continue + unless ← withDefault (isDefEq e lhs) do return .continue + return .visit { expr := rhs, proof? := some (← mkExpectedTypeHint proof (← mkEq e rhs)) } + lemma distance_smul_le {A B : Interface} (α : A ⟶ B) (R S : TypedSystem B) : Δ (α • R) (α • S) ≤ Δ R S := ConstructiveCryptography.CryptographicAlgebra.distance_attach_le diff --git a/src/RandomSystems/Typed/Context.lean b/src/RandomSystems/Typed/Context.lean index 0f4b5c5..c921eba 100644 --- a/src/RandomSystems/Typed/Context.lean +++ b/src/RandomSystems/Typed/Context.lean @@ -2,10 +2,12 @@ module public import RandomSystems.Typed.ParallelAttachment public import RandomSystems.Typed.SystemCoherence +public import RandomSystems.Typed.ConverterCoherence +public import RandomSystems.Typed.Functional public import RandomSystems.Converter.EmptyInterface /-! -# Fixed contexts +# Fixed contexts and parallel projections A typed system `R` on `A` is a typed converter from `A` to the unit interface (`ofSystem`): the PDC of the DDC readings (`resourcePDC`) of the deterministic systems presenting `R`. @@ -13,26 +15,65 @@ Attaching it to the unit system gives `R`. In parallel with the identity it give contexts: attaching `rightContext A S` to `R` is `R ∥ S`, and so is attaching `leftContext R B` to `S`. +Dually, `discard B` exposes the empty unit interface and never queries `B`; every converter with +the unit outside interface equals `discard B` (`eq_discard`). Composing the inverse unit +renamings with `id ⊗ discard` and `discard ⊗ id` defines the ordered projections +`projectLeft A B : A → A ⊗ B` and `projectRight A B : B → A ⊗ B`: + +```text + A ┌───────────────┐ A ⊗ 1 ┌───────────┐ A ⊗ B +────┤ rightUnitor⁻¹ ├───────┤ id A ⊗ ∅ ├─────── (projectLeft A B) + └───────────────┘ └───────────┘ + + B ┌──────────────┐ 1 ⊗ B ┌───────────┐ A ⊗ B +────┤ leftUnitor⁻¹ ├───────┤ ∅ ⊗ id B ├─────── (projectRight A B) + └──────────────┘ └───────────┘ +``` + +Attaching `projectLeft A B` or `projectRight A B` to `R ∥ S` returns `R` or `S` +(`projectLeft_smul_parallel`, `projectRight_smul_parallel`). On a shared initial draw +`sample k ← P; R k ∥ S k`, `smul_sample` preserves that single draw and returns the marginal +system `sample k ← P; R k` or `sample k ← P; S k` (`projectLeft_smul_sample_parallel`, +`projectRight_smul_sample_parallel`). Both projections are natural with respect to parallel +converters (`projectLeft_comp_parallel`, `projectRight_comp_parallel`) and in particular commute +with a converter on the retained component (`projectLeft_comp_parallel_id`, +`projectRight_comp_parallel_id`). + ## Main definitions * `TypedConverter.ofSystem R`: `R` as a typed converter with no inside interfaces +* `TypedConverter.discard B`: the converter exposing the unit interface and ignoring `B` * `TypedConverter.constant R B`: the converter that ignores its inside system and exposes `R` * `TypedConverter.rightContext A S`, `TypedConverter.leftContext R B`: the fixed contexts +* `TypedConverter.projectLeft A B`, `TypedConverter.projectRight A B`: the ordered projections ## Main results * `TypedConverter.attach_ofSystem`, `TypedConverter.attach_constant`: attaching them gives `R` +* `TypedConverter.eq_discard`, `TypedConverter.comp_discard`: uniqueness of the discarding + converter at the unit outside interface * `TypedConverter.attach_rightContext`, `TypedConverter.attach_leftContext`: attaching a context is parallel composition +* `TypedConverter.projectLeft_smul_parallel`, `TypedConverter.projectRight_smul_parallel`: + projecting a parallel system returns the selected component +* `TypedConverter.projectLeft_smul_sample_parallel`, + `TypedConverter.projectRight_smul_sample_parallel`: projecting a sampled parallel system + preserves the retained draw and returns its marginal +* `TypedConverter.projectLeft_comp_parallel`, `TypedConverter.projectRight_comp_parallel`, + `TypedConverter.projectLeft_comp_parallel_id`, `TypedConverter.projectRight_comp_parallel_id`: + naturality of both projections + +## References -Source: Jost, §2.2.2 and Proposition 2.2.3. +1. D. Jost. *On Generalizations of Composable Security*. Doctoral thesis, + Diss. ETH No. 26723, ETH Zurich, 2020. (§2.2.2 and Proposition 2.2.3, pp. 17–18.) -/ @[expose] public section namespace SystemAlgebra -open Classical Probability Interface +open Probability Interface variable {A B : Interface} @@ -77,6 +118,28 @@ lemma attach_ofSystem (R : TypedSystem A) (S : TypedSystem unit) : noncomputable def discard (B : Interface) : TypedConverter unit B := ofDDC (discardSystem B.I B.X B.Y) (discardSystem_isDDCFrom B.domain) +/-- Every typed converter with the unit outside interface equals `discard B`: `unit.domain` admits +no outside query, so every nonempty converter transcript has probability zero. -/ +lemma eq_discard + -- `B` is the inside interface; `α` has the empty unit interface outside. + {B : Interface} (α : TypedConverter unit B) : α = discard B := by + apply PDCBehavior.ext + intro t + rcases List.eq_nil_or_concat t with rfl | ⟨h, ⟨x, y⟩, rfl⟩ + · -- Empty transcripts have probability one in both behaviors. + rw [α.1.mass_nil, (discard B).1.mass_nil] + · -- No input is admitted at the empty outside domain, so nonempty transcripts have mass zero. + rw [List.concat_eq_append, + α.1.mass_eq_zero_of_not_admitted (fun hd => hd.2.2.1) y, + (discard B).1.mass_eq_zero_of_not_admitted (fun hd => hd.2.2.1) y] + +/-- Discarding the output of any typed converter `β` gives `discard C`. -/ +lemma comp_discard + -- `B` is the discarded intermediate interface; `β` connects `B` to `C`. + {B C : Interface} (β : TypedConverter B C) : + comp (discard B) β = discard C := + eq_discard _ + /-- A constant converter: it ignores its inside system and exposes `R`. No efficiency claim is made for this unrestricted mathematical converter. -/ noncomputable def constant (R : TypedSystem A) (B : Interface) : TypedConverter A B := @@ -133,6 +196,370 @@ lemma attach_leftContext (R : TypedSystem A) (S : TypedSystem B) : rw [leftContext, comp_smul, leftUnitor_hom_attach, parallel_attach, identity_attach, attach_ofSystem] +/-! ## Projections of parallel resources -/ + +/-- The inverse right unit renaming drops a right-hand dummy system. -/ +lemma rightUnitor_inv_attach + -- `R` is a typed system on `A`, paired on the right with `TypedSystem.dummy`. + (R : TypedSystem A) : + (rename (A := A) (B := tensor A unit) (Equiv.sumEmpty A.I Empty).symm + (rightUnitAlphabet A.X).symm (rightUnitAlphabet A.Y).symm + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A.X)) + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A.Y)) + (fun h => by + have he := (parallelDomain_unit_right A (h.map (rightUnitAlphabet A.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he) • + TypedSystem.parallel R TypedSystem.dummy : TypedSystem A) = R := by + apply Subtype.ext + apply RandomSystem.ext + intro h + rw [rename_mass, TypedSystem.parallel_unit_right_mass] + simp only [← Equiv.prodCongr_symm, List.map_map, Equiv.self_comp_symm, List.map_id] + +/-- The inverse left unit renaming drops a left-hand dummy system. -/ +lemma leftUnitor_inv_attach + -- `R` is a typed system on `A`, paired on the left with `TypedSystem.dummy`. + (R : TypedSystem A) : + (rename (A := A) (B := tensor unit A) (Equiv.emptySum Empty A.I).symm + (leftUnitAlphabet A.X).symm (leftUnitAlphabet A.Y).symm + (alphabet_symm_label _ _ (leftUnitAlphabet_fst A.X)) + (alphabet_symm_label _ _ (leftUnitAlphabet_fst A.Y)) + (fun h => by + have he := (parallelDomain_unit_left A (h.map (leftUnitAlphabet A.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he) • + TypedSystem.parallel TypedSystem.dummy R : TypedSystem A) = R := by + apply Subtype.ext + apply RandomSystem.ext + intro h + rw [rename_mass, TypedSystem.parallel_unit_left_mass] + simp only [← Equiv.prodCongr_symm, List.map_map, Equiv.self_comp_symm, List.map_id] + +/-- Naturality of the inverse right unit renaming with respect to any typed converter `α`. -/ +lemma comp_rightUnitor_inv + -- `A`, `A'` are the outside and inside interfaces of `α`. + {A A' : Interface} (α : TypedConverter A A') : + comp (rename (A := A) (B := tensor A unit) (Equiv.sumEmpty A.I Empty).symm + (rightUnitAlphabet A.X).symm (rightUnitAlphabet A.Y).symm + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A.X)) + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A.Y)) + (fun h => by + have he := (parallelDomain_unit_right A (h.map (rightUnitAlphabet A.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he)) + (parallel α (TypedConverter.id unit)) = + comp α (rename (A := A') (B := tensor A' unit) (Equiv.sumEmpty A'.I Empty).symm + (rightUnitAlphabet A'.X).symm (rightUnitAlphabet A'.Y).symm + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A'.X)) + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A'.Y)) + (fun h => by + have he := (parallelDomain_unit_right A' (h.map (rightUnitAlphabet A'.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he)) := by + let rHom (C : Interface) : TypedConverter (tensor C unit) C := + rename (Equiv.sumEmpty C.I Empty) (rightUnitAlphabet C.X) (rightUnitAlphabet C.Y) + (rightUnitAlphabet_fst C.X) (rightUnitAlphabet_fst C.Y) (parallelDomain_unit_right C) + let rInv (C : Interface) : TypedConverter C (tensor C unit) := + rename (Equiv.sumEmpty C.I Empty).symm (rightUnitAlphabet C.X).symm + (rightUnitAlphabet C.Y).symm + (alphabet_symm_label _ _ (rightUnitAlphabet_fst C.X)) + (alphabet_symm_label _ _ (rightUnitAlphabet_fst C.Y)) + (fun h => by + have he := (parallelDomain_unit_right C (h.map (rightUnitAlphabet C.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he) + -- The inverse right unit renaming cancels `rHom` on both sides. + have hinvA : comp (rInv A) (rHom A) = TypedConverter.id A := by + rw [rename_comp] + simpa only [Equiv.symm_trans_self] using rename_refl A + have hhomA' : comp (rHom A') (rInv A') = TypedConverter.id (tensor A' unit) := by + rw [rename_comp] + simpa only [Equiv.self_trans_symm] using rename_refl (tensor A' unit) + -- Conjugate `comp_rightUnitor α` by `rInv A` on the left and `rInv A'` on the right. + calc + comp (rInv A) (parallel α (TypedConverter.id unit)) + = comp (rInv A) (comp (parallel α (TypedConverter.id unit)) + (TypedConverter.id (tensor A' unit))) := + congrArg (comp (rInv A)) (PDCBehavior.comp_id _ (tensor A' unit).nonempty_prefix).symm + _ = comp (rInv A) (comp (parallel α (TypedConverter.id unit)) + (comp (rHom A') (rInv A'))) := by + rw [hhomA'] + _ = comp (rInv A) (comp (comp (parallel α (TypedConverter.id unit)) (rHom A')) + (rInv A')) := + congrArg (comp (rInv A)) (PDCBehavior.comp_assoc _ _ _).symm + _ = comp (rInv A) (comp (comp (rHom A) α) (rInv A')) := by + rw [comp_rightUnitor α] + _ = comp (comp (rInv A) (rHom A)) (comp α (rInv A')) := by + simp only [comp, PDCBehavior.comp_assoc] + _ = comp (TypedConverter.id A) (comp α (rInv A')) := by + rw [hinvA] + _ = comp α (rInv A') := + PDCBehavior.id_comp _ A.nonempty_prefix + +/-- Naturality of the inverse left unit renaming with respect to any typed converter `β`. -/ +lemma comp_leftUnitor_inv + -- `B`, `B'` are the outside and inside interfaces of `β`. + {B B' : Interface} (β : TypedConverter B B') : + comp (rename (A := B) (B := tensor unit B) (Equiv.emptySum Empty B.I).symm + (leftUnitAlphabet B.X).symm (leftUnitAlphabet B.Y).symm + (alphabet_symm_label _ _ (leftUnitAlphabet_fst B.X)) + (alphabet_symm_label _ _ (leftUnitAlphabet_fst B.Y)) + (fun h => by + have he := (parallelDomain_unit_left B (h.map (leftUnitAlphabet B.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he)) + (parallel (TypedConverter.id unit) β) = + comp β (rename (A := B') (B := tensor unit B') (Equiv.emptySum Empty B'.I).symm + (leftUnitAlphabet B'.X).symm (leftUnitAlphabet B'.Y).symm + (alphabet_symm_label _ _ (leftUnitAlphabet_fst B'.X)) + (alphabet_symm_label _ _ (leftUnitAlphabet_fst B'.Y)) + (fun h => by + have he := (parallelDomain_unit_left B' (h.map (leftUnitAlphabet B'.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he)) := by + let lHom (C : Interface) : TypedConverter (tensor unit C) C := + rename (Equiv.emptySum Empty C.I) (leftUnitAlphabet C.X) (leftUnitAlphabet C.Y) + (leftUnitAlphabet_fst C.X) (leftUnitAlphabet_fst C.Y) (parallelDomain_unit_left C) + let lInv (C : Interface) : TypedConverter C (tensor unit C) := + rename (Equiv.emptySum Empty C.I).symm (leftUnitAlphabet C.X).symm + (leftUnitAlphabet C.Y).symm + (alphabet_symm_label _ _ (leftUnitAlphabet_fst C.X)) + (alphabet_symm_label _ _ (leftUnitAlphabet_fst C.Y)) + (fun h => by + have he := (parallelDomain_unit_left C (h.map (leftUnitAlphabet C.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he) + -- The inverse left unit renaming cancels `lHom` on both sides. + have hinvB : comp (lInv B) (lHom B) = TypedConverter.id B := by + rw [rename_comp] + simpa only [Equiv.symm_trans_self] using rename_refl B + have hhomB' : comp (lHom B') (lInv B') = TypedConverter.id (tensor unit B') := by + rw [rename_comp] + simpa only [Equiv.self_trans_symm] using rename_refl (tensor unit B') + -- Conjugate `comp_leftUnitor β` by `lInv B` on the left and `lInv B'` on the right. + calc + comp (lInv B) (parallel (TypedConverter.id unit) β) + = comp (lInv B) (comp (parallel (TypedConverter.id unit) β) + (TypedConverter.id (tensor unit B'))) := + congrArg (comp (lInv B)) (PDCBehavior.comp_id _ (tensor unit B').nonempty_prefix).symm + _ = comp (lInv B) (comp (parallel (TypedConverter.id unit) β) + (comp (lHom B') (lInv B'))) := by + rw [hhomB'] + _ = comp (lInv B) (comp (comp (parallel (TypedConverter.id unit) β) (lHom B')) + (lInv B')) := + congrArg (comp (lInv B)) (PDCBehavior.comp_assoc _ _ _).symm + _ = comp (lInv B) (comp (comp (lHom B) β) (lInv B')) := by + rw [comp_leftUnitor β] + _ = comp (comp (lInv B) (lHom B)) (comp β (lInv B')) := by + simp only [comp, PDCBehavior.comp_assoc] + _ = comp (TypedConverter.id B) (comp β (lInv B')) := by + rw [hinvB] + _ = comp β (lInv B') := + PDCBehavior.id_comp _ B.nonempty_prefix + +/-- Left projection of a binary parallel interface: `projectLeft A B : A → A ⊗ B` applies the +inverse right unit renaming outside `id A ⊗ discard B`, forwarding queries on `A` and making +no queries to `B`. + +```text + A ┌───────────────┐ A ⊗ 1 ┌───────────┐ A ⊗ B +────┤ rightUnitor⁻¹ ├───────┤ id A ⊗ ∅ ├─────── + └───────────────┘ └───────────┘ +``` -/ +noncomputable def projectLeft + -- `A` is the retained outside interface; `B` is the hidden parallel component. + (A B : Interface) : TypedConverter A (tensor A B) := + comp + (rename (Equiv.sumEmpty A.I Empty).symm (rightUnitAlphabet A.X).symm + (rightUnitAlphabet A.Y).symm + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A.X)) + (alphabet_symm_label _ _ (rightUnitAlphabet_fst A.Y)) + (fun h => by + have he := (parallelDomain_unit_right A (h.map (rightUnitAlphabet A.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he)) + (parallel (TypedConverter.id A) (discard B)) + +/-- Right projection of a binary parallel interface: `projectRight A B : B → A ⊗ B` applies the +inverse left unit renaming outside `discard A ⊗ id B`, making no queries to `A` and forwarding +queries on `B`. + +```text + B ┌──────────────┐ 1 ⊗ B ┌───────────┐ A ⊗ B +────┤ leftUnitor⁻¹ ├───────┤ ∅ ⊗ id B ├─────── + └──────────────┘ └───────────┘ +``` -/ +noncomputable def projectRight + -- `A` is the hidden parallel component; `B` is the retained outside interface. + (A B : Interface) : TypedConverter B (tensor A B) := + comp + (rename (Equiv.emptySum Empty B.I).symm (leftUnitAlphabet B.X).symm + (leftUnitAlphabet B.Y).symm + (alphabet_symm_label _ _ (leftUnitAlphabet_fst B.X)) + (alphabet_symm_label _ _ (leftUnitAlphabet_fst B.Y)) + (fun h => by + have he := (parallelDomain_unit_left B (h.map (leftUnitAlphabet B.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he)) + (parallel (discard A) (TypedConverter.id B)) + +/-- Projecting the left component of `R ∥ S` returns `R`. -/ +lemma projectLeft_smul_parallel + -- `R` on `A` is retained; `S` on `B` is discarded without being queried. + (R : TypedSystem A) (S : TypedSystem B) : + projectLeft A B • TypedSystem.parallel R S = R := by + -- Attach `id A ⊗ discard B` locally, collapse the unit component to `dummy`, and drop it. + rw [projectLeft, comp_smul, parallel_attach, identity_attach, + TypedSystem.empty_eq (discard B • S), rightUnitor_inv_attach] + +/-- Projecting the right component of `R ∥ S` returns `S`. -/ +lemma projectRight_smul_parallel + -- `R` on `A` is discarded without being queried; `S` on `B` is retained. + (R : TypedSystem A) (S : TypedSystem B) : + projectRight A B • TypedSystem.parallel R S = S := by + -- Attach `discard A ⊗ id B` locally, collapse the unit component to `dummy`, and drop it. + rw [projectRight, comp_smul, parallel_attach, identity_attach, + TypedSystem.empty_eq (discard A • R), leftUnitor_inv_attach] + +/-- Projecting the left component of `sample k ← P; R k ∥ S k` preserves the single retained +draw `k ← P` and returns the marginal system `sample k ← P; R k`. -/ +lemma projectLeft_smul_sample_parallel + -- `K` carries the shared initial sample; `P` is the normalized law drawn once. + {K : Type} (P : Distribution K) [Distribution.IsProbability P] + -- `R k` is retained and `S k` is discarded for that same draw `k`. + (R : K → TypedSystem A) (S : K → TypedSystem B) : + projectLeft A B • TypedSystem.sample P (fun k => TypedSystem.parallel (R k) (S k)) = + TypedSystem.sample P R := by + -- Attachment commutes with the initial draw, then `projectLeft` drops `S k` pointwise in `k`. + rw [smul_sample] + simp only [projectLeft_smul_parallel] + +/-- Projecting the right component of `sample k ← P; R k ∥ S k` preserves the single retained +draw `k ← P` and returns the marginal system `sample k ← P; S k`. -/ +lemma projectRight_smul_sample_parallel + -- `K` carries the shared initial sample; `P` is the normalized law drawn once. + {K : Type} (P : Distribution K) [Distribution.IsProbability P] + -- `R k` is discarded and `S k` is retained for that same draw `k`. + (R : K → TypedSystem A) (S : K → TypedSystem B) : + projectRight A B • TypedSystem.sample P (fun k => TypedSystem.parallel (R k) (S k)) = + TypedSystem.sample P S := by + -- Attachment commutes with the initial draw, then `projectRight` drops `R k` pointwise in `k`. + rw [smul_sample] + simp only [projectRight_smul_parallel] + +/-- Left projection is natural with respect to parallel converters: `α` moves outside `projectLeft`, +and `discard B` absorbs any converter `β` on the hidden component. -/ +lemma projectLeft_comp_parallel + -- `A`, `A'` are the outside and inside interfaces of `α`; + -- `B`, `B'` are the outside and inside interfaces of `β`. + {A A' B B' : Interface} + (α : TypedConverter A A') (β : TypedConverter B B') : + comp (projectLeft A B) (parallel α β) = comp α (projectLeft A' B') := by + let rInv (C : Interface) : TypedConverter C (tensor C unit) := + rename (Equiv.sumEmpty C.I Empty).symm (rightUnitAlphabet C.X).symm + (rightUnitAlphabet C.Y).symm + (alphabet_symm_label _ _ (rightUnitAlphabet_fst C.X)) + (alphabet_symm_label _ _ (rightUnitAlphabet_fst C.Y)) + (fun h => by + have he := (parallelDomain_unit_right C (h.map (rightUnitAlphabet C.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he) + -- Combine `(id A ⊗ discard B) ≫ (α ⊗ β) = α ⊗ discard B'`, then commute `α` past `rInv`. + calc + comp (projectLeft A B) (parallel α β) + = comp (rInv A) (comp (parallel (TypedConverter.id A) (discard B)) (parallel α β)) := + PDCBehavior.comp_assoc _ _ _ + _ = comp (rInv A) (parallel (comp (TypedConverter.id A) α) (comp (discard B) β)) := + congrArg (comp (rInv A)) (PDCBehavior.comp_tensor _ _ _ _) + _ = comp (rInv A) (parallel α (discard B')) := by + rw [show comp (TypedConverter.id A) α = α from PDCBehavior.id_comp α A.nonempty_prefix, + comp_discard β] + _ = comp (rInv A) (parallel (comp α (TypedConverter.id A')) + (comp (TypedConverter.id unit) (discard B'))) := by + rw [show comp α (TypedConverter.id A') = α from PDCBehavior.comp_id α A'.nonempty_prefix, + show comp (TypedConverter.id unit) (discard B') = discard B' from + PDCBehavior.id_comp (discard B') unit.nonempty_prefix] + _ = comp (rInv A) (comp (parallel α (TypedConverter.id unit)) + (parallel (TypedConverter.id A') (discard B'))) := + congrArg (comp (rInv A)) (PDCBehavior.comp_tensor _ _ _ _).symm + _ = comp (comp (rInv A) (parallel α (TypedConverter.id unit))) + (parallel (TypedConverter.id A') (discard B')) := + (PDCBehavior.comp_assoc _ _ _).symm + _ = comp (comp α (rInv A')) (parallel (TypedConverter.id A') (discard B')) := by + rw [comp_rightUnitor_inv α] + _ = comp α (projectLeft A' B') := + PDCBehavior.comp_assoc _ _ _ + +/-- Left projection commutes with a converter `α` on the retained component: +`projectLeft A B ≫ (α ⊗ id B) = α ≫ projectLeft A' B`. -/ +lemma projectLeft_comp_parallel_id + -- `A`, `A'` are the outside and inside interfaces of `α`; `B` is the hidden component. + {A A' B : Interface} (α : TypedConverter A A') : + comp (projectLeft A B) (parallel α (TypedConverter.id B)) = comp α (projectLeft A' B) := + projectLeft_comp_parallel α (TypedConverter.id B) + +/-- Right projection is natural with respect to parallel converters: `β` moves outside +`projectRight`, and `discard A` absorbs any converter `α` on the hidden component. -/ +lemma projectRight_comp_parallel + -- `A`, `A'` are the outside and inside interfaces of `α`; + -- `B`, `B'` are the outside and inside interfaces of `β`. + {A A' B B' : Interface} + (α : TypedConverter A A') (β : TypedConverter B B') : + comp (projectRight A B) (parallel α β) = comp β (projectRight A' B') := by + let lInv (C : Interface) : TypedConverter C (tensor unit C) := + rename (Equiv.emptySum Empty C.I).symm (leftUnitAlphabet C.X).symm + (leftUnitAlphabet C.Y).symm + (alphabet_symm_label _ _ (leftUnitAlphabet_fst C.X)) + (alphabet_symm_label _ _ (leftUnitAlphabet_fst C.Y)) + (fun h => by + have he := (parallelDomain_unit_left C (h.map (leftUnitAlphabet C.X).symm)).symm + simpa only [List.map_map, Equiv.self_comp_symm, List.map_id] using he) + -- Combine `(discard A ⊗ id B) ≫ (α ⊗ β) = discard A' ⊗ β`, then commute `β` past `lInv`. + calc + comp (projectRight A B) (parallel α β) + = comp (lInv B) (comp (parallel (discard A) (TypedConverter.id B)) (parallel α β)) := + PDCBehavior.comp_assoc _ _ _ + _ = comp (lInv B) (parallel (comp (discard A) α) (comp (TypedConverter.id B) β)) := + congrArg (comp (lInv B)) (PDCBehavior.comp_tensor _ _ _ _) + _ = comp (lInv B) (parallel (discard A') β) := by + rw [comp_discard α, + show comp (TypedConverter.id B) β = β from PDCBehavior.id_comp β B.nonempty_prefix] + _ = comp (lInv B) (parallel (comp (TypedConverter.id unit) (discard A')) + (comp β (TypedConverter.id B'))) := by + rw [show comp (TypedConverter.id unit) (discard A') = discard A' from + PDCBehavior.id_comp (discard A') unit.nonempty_prefix, + show comp β (TypedConverter.id B') = β from PDCBehavior.comp_id β B'.nonempty_prefix] + _ = comp (lInv B) (comp (parallel (TypedConverter.id unit) β) + (parallel (discard A') (TypedConverter.id B'))) := + congrArg (comp (lInv B)) (PDCBehavior.comp_tensor _ _ _ _).symm + _ = comp (comp (lInv B) (parallel (TypedConverter.id unit) β)) + (parallel (discard A') (TypedConverter.id B')) := + (PDCBehavior.comp_assoc _ _ _).symm + _ = comp (comp β (lInv B')) (parallel (discard A') (TypedConverter.id B')) := by + rw [comp_leftUnitor_inv β] + _ = comp β (projectRight A' B') := + PDCBehavior.comp_assoc _ _ _ + +/-- Right projection commutes with a converter `β` on the retained component: +`projectRight A B ≫ (id A ⊗ β) = β ≫ projectRight A B'`. -/ +lemma projectRight_comp_parallel_id + -- `A` is the hidden component; `B`, `B'` are the outside and inside interfaces of `β`. + {A B B' : Interface} (β : TypedConverter B B') : + comp (projectRight A B) (parallel (TypedConverter.id A) β) = comp β (projectRight A B') := + projectRight_comp_parallel (TypedConverter.id A) β + +/-- Attaching `α ⊗ β` before `projectLeft A B` equals attaching `α` after `projectLeft A' B'`, +for any (possibly correlated) typed system `T` on `tensor A' B'`. -/ +lemma projectLeft_smul_parallel_smul + -- `A`, `A'` are the retained interfaces and `B`, `B'` the hidden interfaces. + {A A' B B' : Interface} + -- `α` acts on the retained side, `β` on the hidden side, and `T` is any joint system. + (α : TypedConverter A A') (β : TypedConverter B B') + (T : TypedSystem (tensor A' B')) : + projectLeft A B • (parallel α β • T) = α • (projectLeft A' B' • T) := by + rw [← comp_smul, projectLeft_comp_parallel, comp_smul] + +/-- Attaching `α ⊗ β` before `projectRight A B` equals attaching `β` after `projectRight A' B'`, +for any (possibly correlated) typed system `T` on `tensor A' B'`. -/ +lemma projectRight_smul_parallel_smul + -- `A`, `A'` are the hidden interfaces and `B`, `B'` the retained interfaces. + {A A' B B' : Interface} + -- `α` acts on the hidden side, `β` on the retained side, and `T` is any joint system. + (α : TypedConverter A A') (β : TypedConverter B B') + (T : TypedSystem (tensor A' B')) : + projectRight A B • (parallel α β • T) = β • (projectRight A' B' • T) := by + rw [← comp_smul, projectRight_comp_parallel, comp_smul] + end TypedConverter end SystemAlgebra diff --git a/src/RandomSystems/Typed/README.md b/src/RandomSystems/Typed/README.md index 2b3cf57..a407571 100644 --- a/src/RandomSystems/Typed/README.md +++ b/src/RandomSystems/Typed/README.md @@ -111,7 +111,16 @@ exposes a fixed one, and the fixed [right][TypedConverter.rightContext] and [left][TypedConverter.leftContext] **contexts** run a typed system beside: [attaching][TypedConverter.attach_rightContext] $`\mathrm{rightContext}\ A\ S`$ to $`R`$ gives $`R \parallel S`$, and so does [attaching][TypedConverter.attach_leftContext] -$`\mathrm{leftContext}\ R\ B`$ to $`S`$. +$`\mathrm{leftContext}\ R\ B`$ to $`S`$. Dually, the ordered +[left][TypedConverter.projectLeft] and [right][TypedConverter.projectRight] **projections** +combine the inverse unit renamings with `id` and `discard`: +[projecting][TypedConverter.projectLeft_smul_parallel] $`R \parallel S`$ returns $`R`$ or +[$`S`$][TypedConverter.projectRight_smul_parallel], +[preserves a retained shared draw][TypedConverter.projectLeft_smul_sample_parallel] on +$`\mathrm{sample}(P, k \mapsto R_k \parallel S_k)`$ (and +[on the right][TypedConverter.projectRight_smul_sample_parallel]), and is +[natural][TypedConverter.projectLeft_comp_parallel] with respect to parallel converters (and +[on the right][TypedConverter.projectRight_comp_parallel]). A [**source**][TypedSystem.source] answers with fresh independent samples of a law: a memoryless source [3, Definition 3.1] (the sources of [4, Definition 1] may have memory). An @@ -299,11 +308,19 @@ typed system sampling the ideal function, under the fixed-query factorization. [TypedConverter.filter_smul]: Filter.lean#L74 [TypedConverter.filter_comp_filter_smul]: Filter.lean#L128 [RandomSystem.restrict]: ../Distance/Restriction.lean#L64 -[TypedConverter.constant]: Context.lean#L82 -[TypedConverter.rightContext]: Context.lean#L113 -[TypedConverter.leftContext]: Context.lean#L120 -[TypedConverter.attach_rightContext]: Context.lean#L126 -[TypedConverter.attach_leftContext]: Context.lean#L131 +[TypedConverter.constant]: Context.lean#L145 +[TypedConverter.rightContext]: Context.lean#L176 +[TypedConverter.leftContext]: Context.lean#L183 +[TypedConverter.attach_rightContext]: Context.lean#L189 +[TypedConverter.attach_leftContext]: Context.lean#L194 +[TypedConverter.projectLeft]: Context.lean#L362 +[TypedConverter.projectRight]: Context.lean#L384 +[TypedConverter.projectLeft_smul_parallel]: Context.lean#L398 +[TypedConverter.projectRight_smul_parallel]: Context.lean#L407 +[TypedConverter.projectLeft_smul_sample_parallel]: Context.lean#L417 +[TypedConverter.projectRight_smul_sample_parallel]: Context.lean#L430 +[TypedConverter.projectLeft_comp_parallel]: Context.lean#L443 +[TypedConverter.projectRight_comp_parallel]: Context.lean#L493 [TypedSystem.source]: Source.lean#L55 [TypedSystem.ofAutomaton]: Automaton.lean#L77 [TypedSystem.ofAutomaton_map_congr]: Automaton.lean#L98 diff --git a/src/Tests.lean b/src/Tests.lean index 2aa0e7e..ba563fe 100644 --- a/src/Tests.lean +++ b/src/Tests.lean @@ -91,6 +91,7 @@ public import Tests.RandomFunctionCollision public import Tests.RatioCost public import Tests.FilteredRatio public import Tests.CompositeNotation +public import Tests.ParallelProjections /-! Axiom audits per layer and of the Commons library, the checks of the authenticated-encryption example, diff --git a/src/Tests/ParallelProjections.lean b/src/Tests/ParallelProjections.lean new file mode 100644 index 0000000..f3ee387 --- /dev/null +++ b/src/Tests/ParallelProjections.lean @@ -0,0 +1,279 @@ +module + +public import ConstructiveCryptography.Notation +public import Probability.Kernel +public import RandomSystems.Typed.Context +public import RandomSystems.Typed.QueryRouting + +/-! +# Parallel projections, shared randomness, and naturality + +Focused tests for `TypedConverter.projectLeft`, `TypedConverter.projectRight`, and their +`Interface` instance laws on unequal interfaces with unequal query allowances: + +* Projecting `R ∥ S` returns the selected component (`projectLeft_smul_parallel`, + `projectRight_smul_parallel`, and `cc_normalize`), preserving only that component's domain and + query allowance without assuming commutativity. +* Projecting `sample k ← P; R k ∥ S k` preserves the single retained draw `k ← P` and yields + the marginal `sample k ← P; R k` or `sample k ← P; S k` without resampling, verified on + two-query transcripts. +* Both projections are natural with respect to nontrivial converters on the retained component + (and on both components simultaneously), at both the converter level and when attached to a + joint system with retained shared randomness. +-/ + +@[expose] public section + +namespace SystemAlgebra.Tests.ParallelProjections + +open CategoryTheory CategoryTheory.MonoidalCategory Probability +open scoped SystemAlgebra + +/-- Inside left interface: two Boolean ports, Boolean queries and replies, total budget `5`. -/ +abbrev leftInside : Interface := + Interface.queryBudget Bool (fun _ => Bool) (fun _ => Bool) 5 + +/-- Outside left interface: one unit port, Boolean queries and replies, total budget `3`. +Strictly different ports and query allowance from `leftInside` and from both right interfaces. -/ +abbrev leftOutside : Interface := + Interface.queryBudget Unit (fun _ => Bool) (fun _ => Bool) 3 + +/-- Inside right interface: one unit port, unit queries, `Fin 3` replies, total budget `2`. -/ +abbrev rightInside : Interface := + Interface.queryBudget Unit (fun _ => Unit) (fun _ => Fin 3) 2 + +/-- Outside right interface: one unit port, unit queries, Boolean replies, total budget `1`. -/ +abbrev rightOutside : Interface := + Interface.queryBudget Unit (fun _ => Unit) (fun _ => Bool) 1 + +/-- Left component for shared key `k`: on port `i` and query `x`, returns `xor i k`. -/ +noncomputable def leftBranch + -- `k` is the shared Boolean key retained across all queries. + (k : Bool) : TypedSystem leftInside := + TypedSystem.ofFunction (A := leftInside) (fun (i : Bool) (_ : Bool) => xor i k) + +/-- Right component for shared key `k`: returns `1 : Fin 3` when `k = true` and `0` otherwise. -/ +noncomputable def rightBranch + -- `k` is the shared Boolean key retained across all queries. + (k : Bool) : TypedSystem rightInside := + TypedSystem.ofFunction (A := rightInside) (fun (_ : Unit) (_ : Unit) => + if k then (1 : Fin 3) else 0) + +/-- Joint parallel system sharing one uniform Boolean draw `k` across both unequal components: +`sample k ← 𝒰[Bool]; leftBranch k ∥ rightBranch k`. -/ +noncomputable def jointShared : TypedSystem (leftInside ⊗ rightInside) := + sample k ← 𝒰[Bool].1; leftBranch k ∥ rightBranch k + +/-- Nontrivial converter on the left component: routes query `x` on port `()` to port `x` with +query `x`, and negates the Boolean reply; changes both port structure and budget (`3 ≤ 5`). -/ +noncomputable def leftConverter : TypedConverter leftOutside leftInside := + TypedConverter.routing + (fun u => ⟨u.2, u.2⟩) + (fun _ r => !r.2) + (fun _ h => Interface.queryBudget_domain_map _ (show 3 ≤ 5 by omega) h) + +/-- Nontrivial converter on the right component: forwards the unit query and tests whether the +`Fin 3` reply equals `1`; changes both reply alphabet and budget (`1 ≤ 2`). -/ +noncomputable def rightConverter : TypedConverter rightOutside rightInside := + TypedConverter.routing + (fun _ => ⟨(), ()⟩) + (fun _ r => r.2 == (1 : Fin 3)) + (fun _ h => Interface.queryBudget_domain_map _ (show 1 ≤ 2 by omega) h) + +/-! ## Acceptance criterion 1: projecting parallel resources on unequal interfaces -/ + +/-- Left projection of `R ∥ S` returns `R` via the instance law. -/ +example (R : TypedSystem leftInside) (S : TypedSystem rightInside) : + TypedConverter.projectLeft leftInside rightInside • (R ∥ S) = R := + Interface.projectLeft_smul_parallel R S + +/-- Right projection of `R ∥ S` returns `S` via the instance law. -/ +example (R : TypedSystem leftInside) (S : TypedSystem rightInside) : + TypedConverter.projectRight leftInside rightInside • (R ∥ S) = S := + Interface.projectRight_smul_parallel R S + +/-- `cc_normalize` fires on binary projections without extra lemmas. -/ +example (R : TypedSystem leftInside) (S : TypedSystem rightInside) : + TypedConverter.projectLeft leftInside rightInside • (R ∥ S) = R := by + cc_normalize + +example (R : TypedSystem leftInside) (S : TypedSystem rightInside) : + TypedConverter.projectRight leftInside rightInside • (R ∥ S) = S := by + cc_normalize + +/-- The categorical factorization of `projectLeft` and `projectRight` through the unitors and +`discard` holds definitionally. -/ +example : + (TypedConverter.projectLeft leftInside rightInside : + leftInside ⟶ leftInside ⊗ rightInside) = + (ρ_ leftInside).inv ≫ (𝟙 leftInside ⊗ₘ TypedConverter.discard rightInside) := + Interface.projectLeft_eq leftInside rightInside + +example : + (TypedConverter.projectRight leftInside rightInside : + rightInside ⟶ leftInside ⊗ rightInside) = + (λ_ rightInside).inv ≫ (TypedConverter.discard leftInside ⊗ₘ 𝟙 rightInside) := + Interface.projectRight_eq leftInside rightInside + +/-- The retained interface keeps its own query allowance (`5` on the left, `2` on the right), +independent of the discarded component's allowance. -/ +example : leftInside.domain (List.replicate 5 ⟨false, false⟩) := by + refine ⟨List.cons_ne_nil _ _, ?_⟩ + simp only [List.length_replicate, le_refl] + +example : ¬ rightInside.domain (List.replicate 3 ⟨(), ()⟩) := by + intro h + have hb := h.2 + simp only [List.length_replicate] at hb + omega + +/-! ## Acceptance criterion 2: projecting shared randomness without resampling -/ + +/-- Projecting the left component of `jointShared` returns the left marginal on the same draw. -/ +example : + TypedConverter.projectLeft leftInside rightInside • jointShared = + sample k ← 𝒰[Bool].1; leftBranch k := + Interface.projectLeft_smul_sample_parallel 𝒰[Bool].1 leftBranch rightBranch + +/-- Projecting the right component of `jointShared` returns the right marginal on the same draw. -/ +example : + TypedConverter.projectRight leftInside rightInside • jointShared = + sample k ← 𝒰[Bool].1; rightBranch k := + Interface.projectRight_smul_sample_parallel 𝒰[Bool].1 leftBranch rightBranch + +/-- `cc_normalize` hoists the projection under the retained draw and reduces the body. -/ +example : + TypedConverter.projectLeft leftInside rightInside • jointShared = + sample k ← 𝒰[Bool].1; leftBranch k := by + unfold jointShared + cc_normalize + +example : + TypedConverter.projectRight leftInside rightInside • jointShared = + sample k ← 𝒰[Bool].1; rightBranch k := by + unfold jointShared + cc_normalize + +/-- Two repeated queries to the left projection of `jointShared` return `(true, true)` with +probability `1 / 2` (the probability of the single draw `k = true`), not `(1 / 2)² = 1 / 4`. -/ +lemma projectLeft_jointShared_consistent_mass : + (TypedConverter.projectLeft leftInside rightInside • jointShared).1 + [(⟨false, false⟩, ⟨false, true⟩), (⟨false, false⟩, ⟨false, true⟩)] = (1 / 2 : ℝ) := by + rw [show TypedConverter.projectLeft leftInside rightInside • jointShared = + TypedSystem.sample 𝒰[Bool].1 leftBranch from + Interface.projectLeft_smul_sample_parallel 𝒰[Bool].1 leftBranch rightBranch] + -- Evaluate the retained-draw mixture of deterministic functions on the two-query transcript. + unfold leftBranch + rw [TypedSystem.sample_ofFunction_mass_eq] + have hlen : + ([⟨false, false⟩, ⟨false, false⟩] : List (Σ i : Bool, leftInside.X i)).length ≤ 5 := by + norm_num only [List.length_cons, List.length_nil] + simp only [List.map_cons, List.map_nil, hlen, ne_eq, reduceCtorEq, not_false_eq_true, + or_true, true_and, List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, forall_eq, + Sigma.mk.inj_iff, heq_eq_eq, Bool.false_xor, and_self] + -- Only `k = true` produces reply `true` on both queries, contributing its single draw weight. + rw [Distribution.mass_eq_sum] + simp only [Fintype.sum_bool, Distribution.ProbDist.uniform, Distribution.uniform_apply, + Fintype.card_bool, Nat.cast_ofNat, Bool.true_eq_false, ite_true, ite_false, add_zero] + +/-- Conflicting replies `(true, false)` to the same query have probability `0` under the left +projection of `jointShared`, confirming that `k` is retained and never resampled. -/ +lemma projectLeft_jointShared_conflicting_mass : + (TypedConverter.projectLeft leftInside rightInside • jointShared).1 + [(⟨false, false⟩, ⟨false, true⟩), (⟨false, false⟩, ⟨false, false⟩)] = 0 := by + rw [show TypedConverter.projectLeft leftInside rightInside • jointShared = + TypedSystem.sample 𝒰[Bool].1 leftBranch from + Interface.projectLeft_smul_sample_parallel 𝒰[Bool].1 leftBranch rightBranch] + unfold leftBranch + rw [TypedSystem.sample_ofFunction_mass_eq] + simp only [List.map_cons, List.map_nil, List.mem_cons, List.not_mem_nil, or_false, + forall_eq_or_imp, forall_eq, Sigma.mk.inj_iff, heq_eq_eq, true_and, Bool.false_xor] + -- No single Boolean draw `k` satisfies both `true = k` and `false = k`. + rw [Distribution.mass_eq_sum] + simp only [Fintype.sum_bool, Bool.true_eq_false, Bool.false_eq_true, and_false, false_and, + ite_false, add_zero] + +/-! ## Acceptance criterion 3: naturality with nontrivial converters -/ + +/-- Left projection commutes with the nontrivial converter `leftConverter` on the retained side. -/ +example : + (TypedConverter.projectLeft leftOutside rightInside : + leftOutside ⟶ leftOutside ⊗ rightInside) ≫ (leftConverter ⊗ₘ 𝟙 rightInside) = + leftConverter ≫ (TypedConverter.projectLeft leftInside rightInside : + leftInside ⟶ leftInside ⊗ rightInside) := + Interface.projectLeft_comp_whiskerRight leftConverter + +/-- Right projection commutes with the nontrivial converter `rightConverter` on the retained +side. -/ +example : + (TypedConverter.projectRight leftInside rightOutside : + rightOutside ⟶ leftInside ⊗ rightOutside) ≫ (𝟙 leftInside ⊗ₘ rightConverter) = + rightConverter ≫ (TypedConverter.projectRight leftInside rightInside : + rightInside ⟶ leftInside ⊗ rightInside) := + Interface.projectRight_comp_whiskerLeft rightConverter + +/-- Two-sided naturality absorbs a nontrivial converter on the hidden component as well. -/ +example : + (TypedConverter.projectLeft leftOutside rightOutside : + leftOutside ⟶ leftOutside ⊗ rightOutside) ≫ (leftConverter ⊗ₘ rightConverter) = + leftConverter ≫ (TypedConverter.projectLeft leftInside rightInside : + leftInside ⟶ leftInside ⊗ rightInside) := + Interface.projectLeft_comp_tensor leftConverter rightConverter + +example : + (TypedConverter.projectRight leftOutside rightOutside : + rightOutside ⟶ leftOutside ⊗ rightOutside) ≫ (leftConverter ⊗ₘ rightConverter) = + rightConverter ≫ (TypedConverter.projectRight leftInside rightInside : + rightInside ⟶ leftInside ⊗ rightInside) := + Interface.projectRight_comp_tensor leftConverter rightConverter + +/-- Attachment-level naturality on the correlated shared-sample system `jointShared`: applying +`leftConverter ⊗ₘ 𝟙 rightInside` before `projectLeft` equals applying `leftConverter` after it. -/ +lemma projectLeft_naturality_on_jointShared : + TypedConverter.projectLeft leftOutside rightInside • + ((leftConverter ⊗ₘ 𝟙 rightInside) • jointShared) = + leftConverter • (TypedConverter.projectLeft leftInside rightInside • jointShared) := + Interface.projectLeft_smul_tensor_smul leftConverter (𝟙 rightInside) jointShared + +/-- Evaluating `leftConverter` on `leftBranch k` shows it transforms the retained system +nontrivially: query `x` returns `!(xor x k)` on the narrower interface `leftOutside`. -/ +lemma leftConverter_smul_leftBranch (k : Bool) : + leftConverter • leftBranch k = + TypedSystem.ofFunction (A := leftOutside) (fun (_ : Unit) (x : Bool) => !(xor x k)) := by + unfold leftConverter leftBranch + exact TypedConverter.routing_smul_ofFunction _ _ _ _ + +/-- Combining naturality, shared-draw projection, and the converter's functional law yields +the transformed marginal system directly on `leftOutside`. -/ +example : + TypedConverter.projectLeft leftOutside rightInside • + ((leftConverter ⊗ₘ 𝟙 rightInside) • jointShared) = + sample k ← 𝒰[Bool].1; + TypedSystem.ofFunction (A := leftOutside) (fun (_ : Unit) (x : Bool) => !(xor x k)) := by + rw [projectLeft_naturality_on_jointShared] + unfold jointShared + rw [Interface.projectLeft_smul_sample_parallel, TypedConverter.smul_sample] + simp only [leftConverter_smul_leftBranch] + +/-! ## Axiom checks -/ + +#print axioms SystemAlgebra.TypedConverter.eq_discard +#print axioms SystemAlgebra.TypedConverter.comp_discard +#print axioms SystemAlgebra.TypedConverter.projectLeft +#print axioms SystemAlgebra.TypedConverter.projectRight +#print axioms SystemAlgebra.TypedConverter.projectLeft_smul_parallel +#print axioms SystemAlgebra.TypedConverter.projectRight_smul_parallel +#print axioms SystemAlgebra.TypedConverter.projectLeft_smul_sample_parallel +#print axioms SystemAlgebra.TypedConverter.projectRight_smul_sample_parallel +#print axioms SystemAlgebra.TypedConverter.projectLeft_comp_parallel +#print axioms SystemAlgebra.TypedConverter.projectRight_comp_parallel +#print axioms SystemAlgebra.TypedConverter.projectLeft_comp_parallel_id +#print axioms SystemAlgebra.TypedConverter.projectRight_comp_parallel_id +#print axioms SystemAlgebra.Interface.projectLeft_smul_parallel +#print axioms SystemAlgebra.Interface.projectRight_smul_parallel +#print axioms SystemAlgebra.Interface.projectLeft_smul_sample_parallel +#print axioms SystemAlgebra.Interface.projectRight_smul_sample_parallel +#print axioms SystemAlgebra.Interface.projectLeft_comp_whiskerRight +#print axioms SystemAlgebra.Interface.projectRight_comp_whiskerLeft + +end SystemAlgebra.Tests.ParallelProjections