Source lean/Determinize/Proof/Traces/Factorization.lean
import Determinize.Spec.Traces.Semanticsimport Mathlib.Probability.Kernel.MeasurableIntegralimport Mathlib.Probability.Kernel.Composition.MeasureCompProd/-!# Trace factorizationsThe proof-internal form of trace soundness: the source and target joint laws factor over thesource trace law through a Markov kernel and a measurable output function. The corollaries in`Proof/Corollaries.lean` are proved for an arbitrary factorization.`Proof/Traces/ConditionalLaw.lean` identifies the replay kernel with Mathlib’s conditionaldistribution.-/namespace Determinize.Proof.Tracesopen MeasureTheory ProbabilityTheory Determinize.Spec.Paper Determinize.Spec.Tracesopen scoped ProbabilityTheory/-- The canonical real output of a trace-indexed kernel. -/noncomputable def kernelMean (fiber : Kernel Trace ℝ) (trace : Trace) : ℝ :=  ∫ value : ℝ, value ∂fiber trace/-- A factorization of the actual source and target joint laws over the source's own tracelaw: the source output is drawn from the Markov kernel `fiber` indexed by the trace, the targetoutput is the measurable function `output` of the trace, and for almost every trace the fiberis integrable and `output` is its mean. -/def TraceFactorization (source target : Expr) (fiber : Kernel Trace ℝ) (output : Trace → ℝ) :    Prop :=  IsMarkovKernel fiber ∧  Measurable output ∧  traceAndOutputLaw source = traceLaw source ⊗ₘ fiber ∧  traceAndOutputLaw target = (traceLaw source).map (fun trace ↦ (trace, output trace)) ∧  ∀ᵐ trace ∂traceLaw source,    Integrable id (fiber trace) ∧ output trace = ∫ value : ℝ, value ∂fiber trace/-- The output function is determined almost everywhere by the fiber. -/theorem TraceFactorization.canonical {source target : Expr} {fiber : Kernel Trace ℝ}    {output : Trace → ℝ} (factor : TraceFactorization source target fiber output) :    TraceFactorization source target fiber (kernelMean fiber) := by  obtain ⟨markov, _, sourceEq, targetEq, meanEq⟩ := factor  have := markov  refine ⟨markov, (stronglyMeasurable_id.integral_kernel (κ := fiber)).measurable,    sourceEq, targetEq.trans ?_, ?_⟩  · apply Measure.map_congr    filter_upwards [meanEq] with trace good    exact congrArg (fun value ↦ (trace, value)) good.2  · exact meanEq.mono fun _ good ↦ ⟨good.1, rfl⟩/-- Some trace factorization of the source and target joint laws exists. -/def MeanOnTraces (source target : Expr) : Prop :=  ∃ fiber : Kernel Trace ℝ, TraceFactorization source target fiber (kernelMean fiber)end Determinize.Proof.Traces