import Determinize.Proof.Traces.CompactReplaynamespace Determinize.Proof.StepTracesopen MeasureTheory ProbabilityTheory Determinize.Spec.Paperopen Determinize.Proof.Paper Symbolic Symbolic.AffineExpropen SymbolicSoundness.TargetSafetyopen Determinize.Proof.Traces (outputGivenTraceAt)noncomputable section/-- A concrete mean step records an empty event and consumes one reduction step. -/theorem exact_succ_mean (depth : Nat) (expression : Expr) (op : Op) (value : ℝ) (continuation : ℝ → Expr) (reduction : reduce expression = .sample (.mean, op) (Measure.dirac value) continuation) : exactMeasure (depth + 1) expression = (exactMeasure depth (continuation value)).map (prepend none) := by rw [exact_succ_sample _ _ _ _ (not_value_of_reduce_sample _ _ _ reduction) reduction, reduce_site reduction] change (Measure.dirac value).bind (fun v ↦ (exactMeasure depth (continuation v)).map (prepend none)) = _ apply Measure.dirac_bind exact (Measure.measurable_map (prepend none) (measurable_prepend.comp (measurable_const.prodMk measurable_id))).comp ((measurable_exactMeasure depth).comp ((MeasurableActionFamily.stepKernel primitiveLaws).sample_continuation_measurable _ _ _ reduction))theorem outputGivenTraceAt_succ_mean (depth : Nat) (expression : Expr) (op : Op) (value : ℝ) (continuation : ℝ → Expr) (reduction : reduce expression = .sample (.mean, op) (Measure.dirac value) continuation) (tape : DrawTrace) : outputGivenTraceAt (depth + 1) expression tape = outputGivenTraceAt depth (continuation value) tape := by rw [outputGivenTraceAt_succ_sampleE depth (not_value_of_reduce_sample _ _ _ reduction) reduction rfl, Measure.dirac_bind (measurable_outputGivenTraceAt_continuation depth reduction tape)]theorem concrete_mean (expression : AffineExpr n) (typed : WellTyped [] expression ty) (actionEq : symbolicReduce expression = .mean op affine general continuation) (environment : Env n) (valid : InDomain op (meanParams op affine general environment)) : reduce (expression.realize environment) = .sample (.mean, op) (Measure.dirac ((meanAffine op affine general).eval environment)) (fun value ↦ (continuation (value, 0)).realize environment) := by have actionTyped := symbolicReduce_wellTyped typed rw [actionEq] at actionTyped obtain ⟨ha, hg, _, _⟩ := SymbolicAction.wellTyped_mean_iff.mp actionTyped rw [← symbolicReduce_realize typed environment, actionEq] simp only [SymbolicAction.realize] rw [primitiveFiber_mean_formula op affine general ha hg environment, ite_eq_left valid]theorem concrete_target_mean (expression : AffineExpr n) (typed : WellTyped [] expression ty) (actionEq : symbolicReduce expression = .mean op affine general continuation) (environment : Env n) (valid : InDomain op (meanParams op affine general environment)) : reduce (expression.realize environment).determinize = .sample (.mean, op) (Measure.dirac ((meanAffine op affine general).eval environment)) (fun value ↦ ((continuation (value, 0)).realize environment).determinize) := by have actionTyped := symbolicReduce_wellTyped typed rw [actionEq] at actionTyped obtain ⟨ha, hg, _, _⟩ := SymbolicAction.wellTyped_mean_iff.mp actionTyped rw [← symbolicReduce_targetRealize typed environment, actionEq] simp only [targetRealize] rw [primitiveFiber_mean_formula op affine general ha hg environment, ite_eq_left valid]theorem actualTraceLaw_mean (depth : Nat) (history : Symbolic.SampleEnv primitiveLaws n) (expression : AffineExpr n) (typed : WellTyped [] expression ty) (actionEq : symbolicReduce expression = .mean op affine general continuation) (valid : ∀ᵐ env ∂history.actualMeasure primitiveLaws, InDomain op (meanParams op affine general env)) : actualTraceLaw (depth + 1) history expression = (actualTraceLaw depth history (continuation (meanAffine op affine general))).map (prepend none) := by have actionTyped := symbolicReduce_wellTyped typed rw [actionEq] at actionTyped rw [actualTraceLaw, actualTraceLaw, map_bind_fun (history.actualMeasure primitiveLaws) (fun env ↦ exactMeasure depth ((continuation (meanAffine op affine general)).realize env)) ((measurable_exactMeasure depth).comp (continuation (meanAffine op affine general)).measurable_realize) (prepend none) (show Measurable (prepend none : Output → Output) from measurable_prepend.comp (measurable_const.prodMk measurable_id))] apply Measure.bind_congr_right filter_upwards [valid] with env valid rw [exact_succ_mean _ _ _ _ _ (concrete_mean expression typed actionEq env valid), ← mean_continuation_realize actionTyped env]theorem targetTraceLaw_mean (depth : Nat) (history : Symbolic.SampleEnv primitiveLaws n) (expression : AffineExpr n) (typed : WellTyped [] expression ty) (actionEq : symbolicReduce expression = .mean op affine general continuation) (valid : InDomain op (meanParams op affine general (history.meanEnvironment primitiveLaws))) : targetTraceLaw (depth + 1) history expression = (targetTraceLaw depth history (continuation (meanAffine op affine general))).map (prepend none) := by have actionTyped := symbolicReduce_wellTyped typed rw [actionEq] at actionTyped rw [targetTraceLaw, exact_succ_mean _ _ _ _ _ (concrete_target_mean expression typed actionEq _ valid), ← mean_continuation_realize actionTyped] rfltheorem targetReplay_mean (depth : Nat) (history : Symbolic.SampleEnv primitiveLaws n) (expression : AffineExpr n) (typed : WellTyped [] expression ty) (actionEq : symbolicReduce expression = .mean op affine general continuation) (valid : InDomain op (meanParams op affine general (history.meanEnvironment primitiveLaws))) (tape : DrawTrace) : outputGivenTraceAt (depth + 1) (expression.realize (history.meanEnvironment primitiveLaws)).determinize tape = outputGivenTraceAt depth ((continuation (meanAffine op affine general)).realize (history.meanEnvironment primitiveLaws)).determinize tape := by have actionTyped := symbolicReduce_wellTyped typed rw [actionEq] at actionTyped rw [outputGivenTraceAt_succ_mean _ _ _ _ _ (concrete_target_mean expression typed actionEq _ valid), ← mean_continuation_realize actionTyped]endend Determinize.Proof.StepTraces