Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Finite-law interface and binary machine primitives

Definition
InfoLossLAR_RuntimeBundle_01

by BoomShaKaLaaaKa · Nov 14, 2026 · Mathlib 0df444a (Lean v4.33.1)

affine-transportinfo-loss-lar-runtimestoc-2027

The fixed-family transport law is expressed as a probability law on the platform finite space, with independent fair-bit padding and the corresponding public parameter data. Binary machine programs implement composition, list framing, integer arithmetic, Gaussian approximations, activation probabilities, and the body kernel. Their encodings expose an actual finite computation on input strings and random bits.

Definition code












import Definitions.Def_InfoLossLAR_NativeSupplement_01





















import Definitions.Def_InfoLossLAR_WorstCase_Family


import Definitions.Def_QuantumMaxCutFiniteNumericRuntime
































open _root_.InfoLossLARNativeSupplements.h7fd26e43562bbe30.AFT.BinaryArithmetic.RationalBits renaming exists_code → AFT.BinaryArithmetic.RationalBits.exists_code
open _root_.InfoLossLARNativeSupplements.h7fd26e43562bbe30.AFT.BinaryArithmetic.RationalBits renaming exists_code → _p2mNativeSupplement0
open _root_.InfoLossLARNativeSupplements.hbc5d2d237f3bc4c8.AFT.BinaryArithmetic renaming bits_value → AFT.BinaryArithmetic.bits_value
open _root_.InfoLossLARNativeSupplements.hbc5d2d237f3bc4c8.AFT.BinaryArithmetic renaming bits_value → _p2mNativeSupplement1
open _root_.InfoLossLARNativeSupplements.h5a8ca90c605f85fc.AFT.CanonicalMoments renaming independent_embedding_expectation_general → AFT.CanonicalMoments.independent_embedding_expectation_general
open _root_.InfoLossLARNativeSupplements.h5a8ca90c605f85fc.AFT.CanonicalMoments renaming independent_embedding_expectation_general → _p2mNativeSupplement2
open _root_.InfoLossLARNativeSupplements.hf808c8dbbd952db7.AFT.CanonicalMoments renaming independent_equiv_expectation_general → AFT.CanonicalMoments.independent_equiv_expectation_general
open _root_.InfoLossLARNativeSupplements.hf808c8dbbd952db7.AFT.CanonicalMoments renaming independent_equiv_expectation_general → _p2mNativeSupplement3
open _root_.InfoLossLARNativeSupplements.h854528c9e123c6f3.AFT.RationalNumerics.HeightBound renaming div → AFT.RationalNumerics.HeightBound.div
open _root_.InfoLossLARNativeSupplements.h854528c9e123c6f3.AFT.RationalNumerics.HeightBound renaming div → _p2mNativeSupplement4
open _root_.InfoLossLARNativeSupplements.ha562531658b7642e.AFT.RationalNumerics.HeightBound renaming inv → AFT.RationalNumerics.HeightBound.inv
open _root_.InfoLossLARNativeSupplements.ha562531658b7642e.AFT.RationalNumerics.HeightBound renaming inv → _p2mNativeSupplement5
open _root_.InfoLossLARNativeSupplements.h4f5add37bf40c2da.AFT.RationalNumerics.HeightBound renaming mono → AFT.RationalNumerics.HeightBound.mono
open _root_.InfoLossLARNativeSupplements.h4f5add37bf40c2da.AFT.RationalNumerics.HeightBound renaming mono → _p2mNativeSupplement6
open _root_.InfoLossLARNativeSupplements.hec331a1b1191da2d.AFT.RationalNumerics.HeightBound renaming mul → AFT.RationalNumerics.HeightBound.mul
open _root_.InfoLossLARNativeSupplements.hec331a1b1191da2d.AFT.RationalNumerics.HeightBound renaming mul → _p2mNativeSupplement7
open _root_.InfoLossLARNativeSupplements.hca9f3ecea512145f.AFT.RationalNumerics.HeightBound renaming of_int → AFT.RationalNumerics.HeightBound.of_int
open _root_.InfoLossLARNativeSupplements.hca9f3ecea512145f.AFT.RationalNumerics.HeightBound renaming of_int → _p2mNativeSupplement8
open _root_.InfoLossLARNativeSupplements.h5b3dd70c728d6348.AFT.RationalNumerics.HeightBound renaming of_nat → AFT.RationalNumerics.HeightBound.of_nat
open _root_.InfoLossLARNativeSupplements.h5b3dd70c728d6348.AFT.RationalNumerics.HeightBound renaming of_nat → _p2mNativeSupplement9
open _root_.InfoLossLARNativeSupplements.h8fac5110874b5cd9.AFT.RationalNumerics.HeightBound renaming one → AFT.RationalNumerics.HeightBound.one
open _root_.InfoLossLARNativeSupplements.h8fac5110874b5cd9.AFT.RationalNumerics.HeightBound renaming one → _p2mNativeSupplement10
open _root_.InfoLossLARNativeSupplements.h90032182500c2e77.AFT.RationalNumerics.HeightBound renaming pow → AFT.RationalNumerics.HeightBound.pow
open _root_.InfoLossLARNativeSupplements.h90032182500c2e77.AFT.RationalNumerics.HeightBound renaming pow → _p2mNativeSupplement11
open _root_.InfoLossLARNativeSupplements.h6381918e2bfc67e4.AFT.RationalNumerics renaming arctanSum_error → AFT.RationalNumerics.arctanSum_error
open _root_.InfoLossLARNativeSupplements.h6381918e2bfc67e4.AFT.RationalNumerics renaming arctanSum_error → _p2mNativeSupplement12
open _root_.InfoLossLARNativeSupplements.h3497cf28d345cc71.AFT.RationalNumerics renaming arctan_term_bound → AFT.RationalNumerics.arctan_term_bound
open _root_.InfoLossLARNativeSupplements.h3497cf28d345cc71.AFT.RationalNumerics renaming arctan_term_bound → _p2mNativeSupplement13
open _root_.InfoLossLARNativeSupplements.had468f01f375d910.AFT.RationalNumerics renaming boundedRootList_nodup → AFT.RationalNumerics.boundedRootList_nodup
open _root_.InfoLossLARNativeSupplements.had468f01f375d910.AFT.RationalNumerics renaming boundedRootList_nodup → _p2mNativeSupplement14
open _root_.InfoLossLARNativeSupplements.h41ae6c8d47a51d34.AFT.RationalNumerics renaming commonDenominator_le → AFT.RationalNumerics.commonDenominator_le
open _root_.InfoLossLARNativeSupplements.h41ae6c8d47a51d34.AFT.RationalNumerics renaming commonDenominator_le → _p2mNativeSupplement15
open _root_.InfoLossLARNativeSupplements.h5f0d1be1226eed7f.AFT.RationalNumerics renaming commonDenominator_pos → AFT.RationalNumerics.commonDenominator_pos
open _root_.InfoLossLARNativeSupplements.h5f0d1be1226eed7f.AFT.RationalNumerics renaming commonDenominator_pos → _p2mNativeSupplement16
open _root_.InfoLossLARNativeSupplements.h1076ff82614707e5.AFT.RationalNumerics renaming commonNumerator_abs_le → AFT.RationalNumerics.commonNumerator_abs_le
open _root_.InfoLossLARNativeSupplements.h1076ff82614707e5.AFT.RationalNumerics renaming commonNumerator_abs_le → _p2mNativeSupplement17
open _root_.InfoLossLARNativeSupplements.h3827d5d7d51242c6.AFT.RationalNumerics renaming commonNumerator_cast → AFT.RationalNumerics.commonNumerator_cast
open _root_.InfoLossLARNativeSupplements.h3827d5d7d51242c6.AFT.RationalNumerics renaming commonNumerator_cast → _p2mNativeSupplement18
open _root_.InfoLossLARNativeSupplements.h4b0b2c49d14fd209.AFT.RationalNumerics renaming commonNumerator_div → AFT.RationalNumerics.commonNumerator_div
open _root_.InfoLossLARNativeSupplements.h4b0b2c49d14fd209.AFT.RationalNumerics renaming commonNumerator_div → _p2mNativeSupplement19
open _root_.InfoLossLARNativeSupplements.h2882c93186ab7cf8.AFT.RationalNumerics renaming denominator_dvd_common → AFT.RationalNumerics.denominator_dvd_common
open _root_.InfoLossLARNativeSupplements.h2882c93186ab7cf8.AFT.RationalNumerics renaming denominator_dvd_common → _p2mNativeSupplement20
open _root_.InfoLossLARNativeSupplements.h05d99dd2bef78a64.AFT.RationalNumerics renaming integerLinearIteration_abs_le → AFT.RationalNumerics.integerLinearIteration_abs_le
open _root_.InfoLossLARNativeSupplements.h05d99dd2bef78a64.AFT.RationalNumerics renaming integerLinearIteration_abs_le → _p2mNativeSupplement21
open _root_.InfoLossLARNativeSupplements.hc6ad092b2f522781.AFT.RationalNumerics renaming integerLinearIteration_exact → AFT.RationalNumerics.integerLinearIteration_exact
open _root_.InfoLossLARNativeSupplements.hc6ad092b2f522781.AFT.RationalNumerics renaming integerLinearIteration_exact → _p2mNativeSupplement22
open _root_.InfoLossLARNativeSupplements.hdcd54a4fdf95f258.AFT.RationalNumerics renaming integerLinearIteration_height → AFT.RationalNumerics.integerLinearIteration_height
open _root_.InfoLossLARNativeSupplements.hdcd54a4fdf95f258.AFT.RationalNumerics renaming integerLinearIteration_height → _p2mNativeSupplement23
open _root_.InfoLossLARNativeSupplements.he7db6c4ee6224eee.AFT.RationalNumerics renaming linearIteration_height → AFT.RationalNumerics.linearIteration_height
open _root_.InfoLossLARNativeSupplements.he7db6c4ee6224eee.AFT.RationalNumerics renaming linearIteration_height → _p2mNativeSupplement24
open _root_.InfoLossLARNativeSupplements.h18af7218e3ae5082.AFT.RationalNumerics renaming mem_boundedRootList → AFT.RationalNumerics.mem_boundedRootList
open _root_.InfoLossLARNativeSupplements.h18af7218e3ae5082.AFT.RationalNumerics renaming mem_boundedRootList → _p2mNativeSupplement25
open _root_.InfoLossLARNativeSupplements.h878260dd8e6c9a6e.AFT.RationalNumerics renaming piApprox_error → AFT.RationalNumerics.piApprox_error
open _root_.InfoLossLARNativeSupplements.h878260dd8e6c9a6e.AFT.RationalNumerics renaming piApprox_error → _p2mNativeSupplement26
open _root_.InfoLossLARNativeSupplements.h91d71a61f0497f7b.AFT.RationalNumerics renaming piApprox_le_five → AFT.RationalNumerics.piApprox_le_five
open _root_.InfoLossLARNativeSupplements.h91d71a61f0497f7b.AFT.RationalNumerics renaming piApprox_le_five → _p2mNativeSupplement27
open _root_.InfoLossLARNativeSupplements.hb0db73987b7b1eff.AFT.V7Calibration renaming actualPublicBodyChoice_nonempty → AFT.V7Calibration.actualPublicBodyChoice_nonempty
open _root_.InfoLossLARNativeSupplements.hb0db73987b7b1eff.AFT.V7Calibration renaming actualPublicBodyChoice_nonempty → _p2mNativeSupplement28
open _root_.InfoLossLARNativeSupplements.hb6587a05bd2df660.AFT.V7Calibration renaming actualPublicPrimaryChoice_nonempty → AFT.V7Calibration.actualPublicPrimaryChoice_nonempty
open _root_.InfoLossLARNativeSupplements.hb6587a05bd2df660.AFT.V7Calibration renaming actualPublicPrimaryChoice_nonempty → _p2mNativeSupplement29
open _root_.InfoLossLARNativeSupplements.he83cfe7a7db39d44.CurrentAFT.BinaryFiniteProduct renaming current_value → CurrentAFT.BinaryFiniteProduct.current_value
open _root_.InfoLossLARNativeSupplements.he83cfe7a7db39d44.CurrentAFT.BinaryFiniteProduct renaming current_value → _p2mNativeSupplement30
open _root_.InfoLossLARNativeSupplements.hf109288ce2bff41a.CurrentAFT.BinaryFullResponseCounts renaming current_sizes → CurrentAFT.BinaryFullResponseCounts.current_sizes
open _root_.InfoLossLARNativeSupplements.hf109288ce2bff41a.CurrentAFT.BinaryFullResponseCounts renaming current_sizes → _p2mNativeSupplement31
open _root_.InfoLossLARNativeSupplements.hab90893a8a9293a2.CurrentAFT.BinaryListSum renaming current_value → CurrentAFT.BinaryListSum.current_value
open _root_.InfoLossLARNativeSupplements.hab90893a8a9293a2.CurrentAFT.BinaryListSum renaming current_value → _p2mNativeSupplement32
open _root_.InfoLossLARNativeSupplements.hb7b15c9a79e648be.CurrentAFT.BinaryRootMetadataSerializer renaming actualChoice_ordinary → CurrentAFT.BinaryRootMetadataSerializer.actualChoice_ordinary
open _root_.InfoLossLARNativeSupplements.hb7b15c9a79e648be.CurrentAFT.BinaryRootMetadataSerializer renaming actualChoice_ordinary → _p2mNativeSupplement33
open _root_.InfoLossLARNativeSupplements.h9bd8eaff2c880c78.CurrentAFT.BinaryRootMetadataSerializer renaming actualChoice_primary → CurrentAFT.BinaryRootMetadataSerializer.actualChoice_primary
open _root_.InfoLossLARNativeSupplements.h9bd8eaff2c880c78.CurrentAFT.BinaryRootMetadataSerializer renaming actualChoice_primary → _p2mNativeSupplement34
open _root_.InfoLossLARNativeSupplements.ha802267fba3ae37a.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_input → CurrentAFT.BinaryRootMetadataSerializer.candidate_input
open _root_.InfoLossLARNativeSupplements.ha802267fba3ae37a.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_input → _p2mNativeSupplement35
open _root_.InfoLossLARNativeSupplements.h1ccc0b12a2e9b2e7.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_kept → CurrentAFT.BinaryRootMetadataSerializer.candidate_kept
open _root_.InfoLossLARNativeSupplements.h1ccc0b12a2e9b2e7.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_kept → _p2mNativeSupplement36
open _root_.InfoLossLARNativeSupplements.hf63ab620f3c57a9b.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_ordinary → CurrentAFT.BinaryRootMetadataSerializer.candidate_ordinary
open _root_.InfoLossLARNativeSupplements.hf63ab620f3c57a9b.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_ordinary → _p2mNativeSupplement37
open _root_.InfoLossLARNativeSupplements.h8b1af35cb5128d1e.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_primary → CurrentAFT.BinaryRootMetadataSerializer.candidate_primary
open _root_.InfoLossLARNativeSupplements.h8b1af35cb5128d1e.CurrentAFT.BinaryRootMetadataSerializer renaming candidate_primary → _p2mNativeSupplement38
open _root_.InfoLossLARNativeSupplements.hc002c1f58373bc9e.CurrentAFT.BinaryRootMetadataSerializer renaming classify_numbering → CurrentAFT.BinaryRootMetadataSerializer.classify_numbering
open _root_.InfoLossLARNativeSupplements.hc002c1f58373bc9e.CurrentAFT.BinaryRootMetadataSerializer renaming classify_numbering → _p2mNativeSupplement39
open _root_.InfoLossLARNativeSupplements.h0654708b8f7abd41.CurrentAFT.BinaryRootMetadataSerializer renaming current_castMetadata → CurrentAFT.BinaryRootMetadataSerializer.current_castMetadata
open _root_.InfoLossLARNativeSupplements.h0654708b8f7abd41.CurrentAFT.BinaryRootMetadataSerializer renaming current_castMetadata → _p2mNativeSupplement40
open _root_.InfoLossLARNativeSupplements.h62365cd168bf9746.CurrentAFT.BinaryRootMetadataSerializer renaming current_cast_tags → CurrentAFT.BinaryRootMetadataSerializer.current_cast_tags
open _root_.InfoLossLARNativeSupplements.h62365cd168bf9746.CurrentAFT.BinaryRootMetadataSerializer renaming current_cast_tags → _p2mNativeSupplement41
open _root_.InfoLossLARNativeSupplements.hd04a074a03468451.CurrentAFT.BinaryRootMetadataSerializer renaming current_congr_counts → CurrentAFT.BinaryRootMetadataSerializer.current_congr_counts
open _root_.InfoLossLARNativeSupplements.hd04a074a03468451.CurrentAFT.BinaryRootMetadataSerializer renaming current_congr_counts → _p2mNativeSupplement42
open _root_.InfoLossLARNativeSupplements.h92779a9b15e1ad74.CurrentAFT.BinaryRootMetadataSerializer renaming current_kept_le → CurrentAFT.BinaryRootMetadataSerializer.current_kept_le
open _root_.InfoLossLARNativeSupplements.h92779a9b15e1ad74.CurrentAFT.BinaryRootMetadataSerializer renaming current_kept_le → _p2mNativeSupplement43
open _root_.InfoLossLARNativeSupplements.h5d3e6518d488d268.CurrentAFT.BinaryRootMetadataSerializer renaming current_metadata_actual → CurrentAFT.BinaryRootMetadataSerializer.current_metadata_actual
open _root_.InfoLossLARNativeSupplements.h5d3e6518d488d268.CurrentAFT.BinaryRootMetadataSerializer renaming current_metadata_actual → _p2mNativeSupplement44
open _root_.InfoLossLARNativeSupplements.h6da04ab1cb4f0ae9.CurrentAFT.BinaryRootMetadataSerializer renaming current_native_counts → CurrentAFT.BinaryRootMetadataSerializer.current_native_counts
open _root_.InfoLossLARNativeSupplements.h6da04ab1cb4f0ae9.CurrentAFT.BinaryRootMetadataSerializer renaming current_native_counts → _p2mNativeSupplement45
open _root_.InfoLossLARNativeSupplements.h0034013fd46e34b6.CurrentAFT.BinaryRootMetadataSerializer renaming current_rational_actual → CurrentAFT.BinaryRootMetadataSerializer.current_rational_actual
open _root_.InfoLossLARNativeSupplements.h0034013fd46e34b6.CurrentAFT.BinaryRootMetadataSerializer renaming current_rational_actual → _p2mNativeSupplement46
open _root_.InfoLossLARNativeSupplements.he41810aeef644de7.CurrentAFT.BinaryRootMetadataSerializer renaming current_rational_native → CurrentAFT.BinaryRootMetadataSerializer.current_rational_native
open _root_.InfoLossLARNativeSupplements.he41810aeef644de7.CurrentAFT.BinaryRootMetadataSerializer renaming current_rational_native → _p2mNativeSupplement47
open _root_.InfoLossLARNativeSupplements.hdf21f91932116c35.CurrentAFT.BinaryRootMetadataSerializer renaming current_role → CurrentAFT.BinaryRootMetadataSerializer.current_role
open _root_.InfoLossLARNativeSupplements.hdf21f91932116c35.CurrentAFT.BinaryRootMetadataSerializer renaming current_role → _p2mNativeSupplement48
open _root_.InfoLossLARNativeSupplements.h2f02b0da80d0da15.CurrentAFT.BinaryRootMetadataSerializer renaming current_role_actual → CurrentAFT.BinaryRootMetadataSerializer.current_role_actual
open _root_.InfoLossLARNativeSupplements.h2f02b0da80d0da15.CurrentAFT.BinaryRootMetadataSerializer renaming current_role_actual → _p2mNativeSupplement49
open _root_.InfoLossLARNativeSupplements.hf05bd272bb1b56e1.CurrentAFT.BinaryRootMetadataSerializer renaming current_tagSlot → CurrentAFT.BinaryRootMetadataSerializer.current_tagSlot
open _root_.InfoLossLARNativeSupplements.hf05bd272bb1b56e1.CurrentAFT.BinaryRootMetadataSerializer renaming current_tagSlot → _p2mNativeSupplement50
open _root_.InfoLossLARNativeSupplements.h99dea6e0b2cb92cd.CurrentAFT.BinaryRootMetadataSerializer renaming current_tagSlot_actual → CurrentAFT.BinaryRootMetadataSerializer.current_tagSlot_actual
open _root_.InfoLossLARNativeSupplements.h99dea6e0b2cb92cd.CurrentAFT.BinaryRootMetadataSerializer renaming current_tagSlot_actual → _p2mNativeSupplement51
open _root_.InfoLossLARNativeSupplements.h5aa51497de94248a.CurrentAFT.BinaryRootMetadataSerializer renaming find_value → CurrentAFT.BinaryRootMetadataSerializer.find_value
open _root_.InfoLossLARNativeSupplements.h5aa51497de94248a.CurrentAFT.BinaryRootMetadataSerializer renaming find_value → _p2mNativeSupplement52
open _root_.InfoLossLARNativeSupplements.he1ba053a04ebe416.CurrentAFT.BinaryRootMetadataSerializer renaming inputOrdering_symm_kept → CurrentAFT.BinaryRootMetadataSerializer.inputOrdering_symm_kept
open _root_.InfoLossLARNativeSupplements.he1ba053a04ebe416.CurrentAFT.BinaryRootMetadataSerializer renaming inputOrdering_symm_kept → _p2mNativeSupplement53
open _root_.InfoLossLARNativeSupplements.hb19b481a4ab916e9.CurrentAFT.BinaryRootMetadataSerializer renaming inputOrdering_symm_ordinary → CurrentAFT.BinaryRootMetadataSerializer.inputOrdering_symm_ordinary
open _root_.InfoLossLARNativeSupplements.hb19b481a4ab916e9.CurrentAFT.BinaryRootMetadataSerializer renaming inputOrdering_symm_ordinary → _p2mNativeSupplement54
open _root_.InfoLossLARNativeSupplements.h925bf1f311c5581d.CurrentAFT.BinaryRootMetadataSerializer renaming inputOrdering_symm_primary → CurrentAFT.BinaryRootMetadataSerializer.inputOrdering_symm_primary
open _root_.InfoLossLARNativeSupplements.h925bf1f311c5581d.CurrentAFT.BinaryRootMetadataSerializer renaming inputOrdering_symm_primary → _p2mNativeSupplement55
open _root_.InfoLossLARNativeSupplements.h28bdeb915df6f31b.CurrentAFT.BinaryRootMetadataSerializer renaming keptOrdering_number → CurrentAFT.BinaryRootMetadataSerializer.keptOrdering_number
open _root_.InfoLossLARNativeSupplements.h28bdeb915df6f31b.CurrentAFT.BinaryRootMetadataSerializer renaming keptOrdering_number → _p2mNativeSupplement56
open _root_.InfoLossLARNativeSupplements.hd98ece77ce245c62.CurrentAFT.BinaryRootMetadataSerializer renaming kept_length_le → CurrentAFT.BinaryRootMetadataSerializer.kept_length_le
open _root_.InfoLossLARNativeSupplements.hd98ece77ce245c62.CurrentAFT.BinaryRootMetadataSerializer renaming kept_length_le → _p2mNativeSupplement57
open _root_.InfoLossLARNativeSupplements.h0c3b1f75a36dc0a1.CurrentAFT.BinaryRootMetadataSerializer renaming matchesBlock_input → CurrentAFT.BinaryRootMetadataSerializer.matchesBlock_input
open _root_.InfoLossLARNativeSupplements.h0c3b1f75a36dc0a1.CurrentAFT.BinaryRootMetadataSerializer renaming matchesBlock_input → _p2mNativeSupplement58
open _root_.InfoLossLARNativeSupplements.h3614eab28d16c434.CurrentAFT.BinaryRootMetadataSerializer renaming matchesBlock_value → CurrentAFT.BinaryRootMetadataSerializer.matchesBlock_value
open _root_.InfoLossLARNativeSupplements.h3614eab28d16c434.CurrentAFT.BinaryRootMetadataSerializer renaming matchesBlock_value → _p2mNativeSupplement59
open _root_.InfoLossLARNativeSupplements.h7211ad64e277ecd6.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_get → CurrentAFT.BinaryRootMetadataSerializer.nativeTag_get
open _root_.InfoLossLARNativeSupplements.h7211ad64e277ecd6.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_get → _p2mNativeSupplement60
open _root_.InfoLossLARNativeSupplements.h5bfd14958fa45ca1.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_increasing → CurrentAFT.BinaryRootMetadataSerializer.nativeTag_increasing
open _root_.InfoLossLARNativeSupplements.h5bfd14958fa45ca1.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_increasing → _p2mNativeSupplement61
open _root_.InfoLossLARNativeSupplements.h830e34787bfcc36e.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_membership → CurrentAFT.BinaryRootMetadataSerializer.nativeTag_membership
open _root_.InfoLossLARNativeSupplements.h830e34787bfcc36e.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_membership → _p2mNativeSupplement62
open _root_.InfoLossLARNativeSupplements.hdf4e90e4faeda9bb.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_tagsInput → CurrentAFT.BinaryRootMetadataSerializer.nativeTag_tagsInput
open _root_.InfoLossLARNativeSupplements.hdf4e90e4faeda9bb.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_tagsInput → _p2mNativeSupplement63
open _root_.InfoLossLARNativeSupplements.h0eb6479555712d22.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_tagsInput_vector → CurrentAFT.BinaryRootMetadataSerializer.nativeTag_tagsInput_vector
open _root_.InfoLossLARNativeSupplements.h0eb6479555712d22.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_tagsInput_vector → _p2mNativeSupplement64
open _root_.InfoLossLARNativeSupplements.he3484ce79e618bef.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_value → CurrentAFT.BinaryRootMetadataSerializer.nativeTag_value
open _root_.InfoLossLARNativeSupplements.he3484ce79e618bef.CurrentAFT.BinaryRootMetadataSerializer renaming nativeTag_value → _p2mNativeSupplement65
open _root_.InfoLossLARNativeSupplements.hb10c61056b84e123.CurrentAFT.BinaryRootMetadataSerializer renaming position_input → CurrentAFT.BinaryRootMetadataSerializer.position_input
open _root_.InfoLossLARNativeSupplements.hb10c61056b84e123.CurrentAFT.BinaryRootMetadataSerializer renaming position_input → _p2mNativeSupplement66
open _root_.InfoLossLARNativeSupplements.h717398dca61c08fb.CurrentAFT.BinaryRootMetadataSerializer renaming rational_current_cast_tags → CurrentAFT.BinaryRootMetadataSerializer.rational_current_cast_tags
open _root_.InfoLossLARNativeSupplements.h717398dca61c08fb.CurrentAFT.BinaryRootMetadataSerializer renaming rational_current_cast_tags → _p2mNativeSupplement67
open _root_.InfoLossLARNativeSupplements.h5c1679d60747a3c8.CurrentAFT.BinaryRootMetadataSerializer renaming retainedInput_at_group → CurrentAFT.BinaryRootMetadataSerializer.retainedInput_at_group
open _root_.InfoLossLARNativeSupplements.h5c1679d60747a3c8.CurrentAFT.BinaryRootMetadataSerializer renaming retainedInput_at_group → _p2mNativeSupplement68
open _root_.InfoLossLARNativeSupplements.hd10b40ce4e5fb3ce.CurrentAFT.BinaryRootMetadataSerializer renaming retainedInput_get → CurrentAFT.BinaryRootMetadataSerializer.retainedInput_get
open _root_.InfoLossLARNativeSupplements.hd10b40ce4e5fb3ce.CurrentAFT.BinaryRootMetadataSerializer renaming retainedInput_get → _p2mNativeSupplement69
open _root_.InfoLossLARNativeSupplements.h790debae9bc7d744.CurrentAFT.BinaryRootMetadataSerializer renaming role_input → CurrentAFT.BinaryRootMetadataSerializer.role_input
open _root_.InfoLossLARNativeSupplements.h790debae9bc7d744.CurrentAFT.BinaryRootMetadataSerializer renaming role_input → _p2mNativeSupplement70
open _root_.InfoLossLARNativeSupplements.h36433fb6e771176d.CurrentAFT.BinaryRootMetadataSerializer renaming role_kept → CurrentAFT.BinaryRootMetadataSerializer.role_kept
open _root_.InfoLossLARNativeSupplements.h36433fb6e771176d.CurrentAFT.BinaryRootMetadataSerializer renaming role_kept → _p2mNativeSupplement71
open _root_.InfoLossLARNativeSupplements.h1c0aacbfbcab259c.CurrentAFT.BinaryRootMetadataSerializer renaming role_ordinary → CurrentAFT.BinaryRootMetadataSerializer.role_ordinary
open _root_.InfoLossLARNativeSupplements.h1c0aacbfbcab259c.CurrentAFT.BinaryRootMetadataSerializer renaming role_ordinary → _p2mNativeSupplement72
open _root_.InfoLossLARNativeSupplements.had28d56c66c172c2.CurrentAFT.BinaryRootMetadataSerializer renaming role_primary → CurrentAFT.BinaryRootMetadataSerializer.role_primary
open _root_.InfoLossLARNativeSupplements.had28d56c66c172c2.CurrentAFT.BinaryRootMetadataSerializer renaming role_primary → _p2mNativeSupplement73
open _root_.InfoLossLARNativeSupplements.hf7b606d7b6c71037.CurrentAFT.BinaryRootMetadataSerializer renaming splitIndex_value → CurrentAFT.BinaryRootMetadataSerializer.splitIndex_value
open _root_.InfoLossLARNativeSupplements.hf7b606d7b6c71037.CurrentAFT.BinaryRootMetadataSerializer renaming splitIndex_value → _p2mNativeSupplement74
open _root_.InfoLossLARNativeSupplements.hc171b658125977dd.CurrentAFT.BinaryRootMetadataSerializer renaming tagBlocks_card → CurrentAFT.BinaryRootMetadataSerializer.tagBlocks_card
open _root_.InfoLossLARNativeSupplements.hc171b658125977dd.CurrentAFT.BinaryRootMetadataSerializer renaming tagBlocks_card → _p2mNativeSupplement75
open _root_.InfoLossLARNativeSupplements.h864990220cd05a68.CurrentAFT.BinaryRootMetadataSerializer renaming tagSlot_input → CurrentAFT.BinaryRootMetadataSerializer.tagSlot_input
open _root_.InfoLossLARNativeSupplements.h864990220cd05a68.CurrentAFT.BinaryRootMetadataSerializer renaming tagSlot_input → _p2mNativeSupplement76
open _root_.InfoLossLARNativeSupplements.he99bee3d04267632.CurrentAFT.BinaryRootMetadataSerializer renaming tagsInput_get → CurrentAFT.BinaryRootMetadataSerializer.tagsInput_get
open _root_.InfoLossLARNativeSupplements.he99bee3d04267632.CurrentAFT.BinaryRootMetadataSerializer renaming tagsInput_get → _p2mNativeSupplement77
open _root_.InfoLossLARNativeSupplements.h15d8c2848429780a.CurrentAFT.BinaryRootMetadataSerializer renaming vector_count_cast_get → CurrentAFT.BinaryRootMetadataSerializer.vector_count_cast_get
open _root_.InfoLossLARNativeSupplements.h15d8c2848429780a.CurrentAFT.BinaryRootMetadataSerializer renaming vector_count_cast_get → _p2mNativeSupplement78
open _root_.InfoLossLARNativeSupplements.h6033ffbe1c08cc96.CurrentAFT.BinaryRootMetadataSerializer renaming vector_length_cast_get → CurrentAFT.BinaryRootMetadataSerializer.vector_length_cast_get
open _root_.InfoLossLARNativeSupplements.h6033ffbe1c08cc96.CurrentAFT.BinaryRootMetadataSerializer renaming vector_length_cast_get → _p2mNativeSupplement79
open _root_.InfoLossLARNativeSupplements.he438d921f6936ff8.CurrentAFT.BinaryRootMetadataSerializer renaming vector_mem_toList → CurrentAFT.BinaryRootMetadataSerializer.vector_mem_toList
open _root_.InfoLossLARNativeSupplements.he438d921f6936ff8.CurrentAFT.BinaryRootMetadataSerializer renaming vector_mem_toList → _p2mNativeSupplement80
open _root_.InfoLossLARNativeSupplements.he37fcb2fedaaad11.CurrentAFT.BinarySparseCycleEvaluation renaming current_tuple_value → CurrentAFT.BinarySparseCycleEvaluation.current_tuple_value
open _root_.InfoLossLARNativeSupplements.he37fcb2fedaaad11.CurrentAFT.BinarySparseCycleEvaluation renaming current_tuple_value → _p2mNativeSupplement81
open _root_.InfoLossLARNativeSupplements.h42f327202f6ffc34.CurrentAFT.BinarySparseCycleEvaluation renaming rootEntry_value → CurrentAFT.BinarySparseCycleEvaluation.rootEntry_value
open _root_.InfoLossLARNativeSupplements.h42f327202f6ffc34.CurrentAFT.BinarySparseCycleEvaluation renaming rootEntry_value → _p2mNativeSupplement82
open _root_.InfoLossLARNativeSupplements.h046841627a1cee9b.CurrentAFT.BinarySparseCycleEvaluation renaming roots_value → CurrentAFT.BinarySparseCycleEvaluation.roots_value
open _root_.InfoLossLARNativeSupplements.h046841627a1cee9b.CurrentAFT.BinarySparseCycleEvaluation renaming roots_value → _p2mNativeSupplement83
open _root_.InfoLossLARNativeSupplements.hd0df78f75d4d893f.CurrentAFT.BinarySparseCycleEvaluation renaming term_value → CurrentAFT.BinarySparseCycleEvaluation.term_value
open _root_.InfoLossLARNativeSupplements.hd0df78f75d4d893f.CurrentAFT.BinarySparseCycleEvaluation renaming term_value → _p2mNativeSupplement84
open _root_.InfoLossLARNativeSupplements.hf4f5fd4dcb0d678c.CurrentAFT.BinarySparseCycleEvaluation renaming weightEntry_value → CurrentAFT.BinarySparseCycleEvaluation.weightEntry_value
open _root_.InfoLossLARNativeSupplements.hf4f5fd4dcb0d678c.CurrentAFT.BinarySparseCycleEvaluation renaming weightEntry_value → _p2mNativeSupplement85
open _root_.InfoLossLARNativeSupplements.haf9b5363dbfc3f4c.CurrentAFT.BinarySparseCycleSemantic renaming coordinates_apply → CurrentAFT.BinarySparseCycleSemantic.coordinates_apply
open _root_.InfoLossLARNativeSupplements.haf9b5363dbfc3f4c.CurrentAFT.BinarySparseCycleSemantic renaming coordinates_apply → _p2mNativeSupplement86
open _root_.InfoLossLARNativeSupplements.hb39aff037bdc4326.CurrentAFT.BinarySparseCycleSemantic renaming current_value → CurrentAFT.BinarySparseCycleSemantic.current_value
open _root_.InfoLossLARNativeSupplements.hb39aff037bdc4326.CurrentAFT.BinarySparseCycleSemantic renaming current_value → _p2mNativeSupplement87
open _root_.InfoLossLARNativeSupplements.h0cda298b0874d5b6.CurrentAFT.BinarySparseCycleSemantic renaming product_coordinates → CurrentAFT.BinarySparseCycleSemantic.product_coordinates
open _root_.InfoLossLARNativeSupplements.h0cda298b0874d5b6.CurrentAFT.BinarySparseCycleSemantic renaming product_coordinates → _p2mNativeSupplement88
open _root_.InfoLossLARNativeSupplements.h10d530ee15bdc77f.CurrentAFT.BinarySparseCycleSemantic renaming roots_coordinates → CurrentAFT.BinarySparseCycleSemantic.roots_coordinates
open _root_.InfoLossLARNativeSupplements.h10d530ee15bdc77f.CurrentAFT.BinarySparseCycleSemantic renaming roots_coordinates → _p2mNativeSupplement89
open _root_.InfoLossLARNativeSupplements.h5cfbe96a887c7fbe.CurrentAFT.BinaryTouchedCycle renaming candidates_length → CurrentAFT.BinaryTouchedCycle.candidates_length
open _root_.InfoLossLARNativeSupplements.h5cfbe96a887c7fbe.CurrentAFT.BinaryTouchedCycle renaming candidates_length → _p2mNativeSupplement90
open _root_.InfoLossLARNativeSupplements.hbcb1352f59b47e7a.CurrentAFT.BinaryTouchedCycle renaming prepare_length → CurrentAFT.BinaryTouchedCycle.prepare_length
open _root_.InfoLossLARNativeSupplements.hbcb1352f59b47e7a.CurrentAFT.BinaryTouchedCycle renaming prepare_length → _p2mNativeSupplement91
open _root_.InfoLossLARNativeSupplements.h7b66ba8c3614b749.CurrentAFT.BinaryTouchedCycle renaming unique_length → CurrentAFT.BinaryTouchedCycle.unique_length
open _root_.InfoLossLARNativeSupplements.h7b66ba8c3614b749.CurrentAFT.BinaryTouchedCycle renaming unique_length → _p2mNativeSupplement92
open _root_.InfoLossLARNativeSupplements.h8bc15ae79e4624be.CurrentAFT.BinaryTouchedCycleBuffer renaming lookup_value → CurrentAFT.BinaryTouchedCycleBuffer.lookup_value
open _root_.InfoLossLARNativeSupplements.h8bc15ae79e4624be.CurrentAFT.BinaryTouchedCycleBuffer renaming lookup_value → _p2mNativeSupplement93
open _root_.InfoLossLARNativeSupplements.hf362d6b0d865f5c6.CurrentAFT.BinaryTouchedCycleRanks renaming positions_value → CurrentAFT.BinaryTouchedCycleRanks.positions_value
open _root_.InfoLossLARNativeSupplements.hf362d6b0d865f5c6.CurrentAFT.BinaryTouchedCycleRanks renaming positions_value → _p2mNativeSupplement94
open _root_.InfoLossLARNativeSupplements.h4cdf4f6ad2d19d90.CurrentAFT.BinaryTupleSum renaming current_value → CurrentAFT.BinaryTupleSum.current_value
open _root_.InfoLossLARNativeSupplements.h4cdf4f6ad2d19d90.CurrentAFT.BinaryTupleSum renaming current_value → _p2mNativeSupplement95
open _root_.InfoLossLARNativeSupplements.ha8fcea89e723dc23.CurrentAFT.BinaryTupleSum renaming get_ofFn_fun → CurrentAFT.BinaryTupleSum.get_ofFn_fun
open _root_.InfoLossLARNativeSupplements.ha8fcea89e723dc23.CurrentAFT.BinaryTupleSum renaming get_ofFn_fun → _p2mNativeSupplement96
open _root_.InfoLossLARNativeSupplements.ha84cbfc4268e8d40.CurrentAFT.BinaryTupleSum renaming prepend_get → CurrentAFT.BinaryTupleSum.prepend_get
open _root_.InfoLossLARNativeSupplements.ha84cbfc4268e8d40.CurrentAFT.BinaryTupleSum renaming prepend_get → _p2mNativeSupplement97
open _root_.InfoLossLARNativeSupplements.hcca3d782e3229b7c.CurrentAFT.BinaryTupleSum renaming prepend_value → CurrentAFT.BinaryTupleSum.prepend_value
open _root_.InfoLossLARNativeSupplements.hcca3d782e3229b7c.CurrentAFT.BinaryTupleSum renaming prepend_value → _p2mNativeSupplement98
open _root_.InfoLossLARNativeSupplements.h78d75bb492074482.CurrentAFT.CountedBoundedRootTags.Tag renaming card → CurrentAFT.CountedBoundedRootTags.Tag.card
open _root_.InfoLossLARNativeSupplements.h78d75bb492074482.CurrentAFT.CountedBoundedRootTags.Tag renaming card → _p2mNativeSupplement99
open _root_.InfoLossLARNativeSupplements.h9568a61082f27bb9.CurrentAFT.CountedBoundedRootTags.Tag renaming nodup → CurrentAFT.CountedBoundedRootTags.Tag.nodup
open _root_.InfoLossLARNativeSupplements.h9568a61082f27bb9.CurrentAFT.CountedBoundedRootTags.Tag renaming nodup → _p2mNativeSupplement100
open _root_.InfoLossLARNativeSupplements.hf6e7f4bd634f80a9.CurrentAFT.CountedBoundedRootTags.Tag renaming read_value → CurrentAFT.CountedBoundedRootTags.Tag.read_value
open _root_.InfoLossLARNativeSupplements.hf6e7f4bd634f80a9.CurrentAFT.CountedBoundedRootTags.Tag renaming read_value → _p2mNativeSupplement101
open _root_.InfoLossLARNativeSupplements.hd05ef333dfb7baf7.CurrentAFT.CountedBoundedRootTags.Tag renaming store_value → CurrentAFT.CountedBoundedRootTags.Tag.store_value
open _root_.InfoLossLARNativeSupplements.hd05ef333dfb7baf7.CurrentAFT.CountedBoundedRootTags.Tag renaming store_value → _p2mNativeSupplement102
open _root_.InfoLossLARNativeSupplements.h8bf8720ebb55498f.CurrentAFT.CountedBoundedRootTags.Tag renaming vector_increasing → CurrentAFT.CountedBoundedRootTags.Tag.vector_increasing
open _root_.InfoLossLARNativeSupplements.h8bf8720ebb55498f.CurrentAFT.CountedBoundedRootTags.Tag renaming vector_increasing → _p2mNativeSupplement103
open _root_.InfoLossLARNativeSupplements.h5fde173c8469afa6.CurrentAFT.CountedBoundedRootTags renaming current_sets → CurrentAFT.CountedBoundedRootTags.current_sets
open _root_.InfoLossLARNativeSupplements.h5fde173c8469afa6.CurrentAFT.CountedBoundedRootTags renaming current_sets → _p2mNativeSupplement104
open _root_.InfoLossLARNativeSupplements.he8f344a3055fc54a.CurrentAFT.CountedBoundedRootTags renaming entry_sets → CurrentAFT.CountedBoundedRootTags.entry_sets
open _root_.InfoLossLARNativeSupplements.he8f344a3055fc54a.CurrentAFT.CountedBoundedRootTags renaming entry_sets → _p2mNativeSupplement105
open _root_.InfoLossLARNativeSupplements.h2c50018e32584610.CurrentAFT.CountedBoundedRootTags renaming guarded_value → CurrentAFT.CountedBoundedRootTags.guarded_value
open _root_.InfoLossLARNativeSupplements.h2c50018e32584610.CurrentAFT.CountedBoundedRootTags renaming guarded_value → _p2mNativeSupplement106
open _root_.InfoLossLARNativeSupplements.h4aa23a1b871c5bc6.CurrentAFT.CountedBoundedRootTags renaming sum_value → CurrentAFT.CountedBoundedRootTags.sum_value
open _root_.InfoLossLARNativeSupplements.h4aa23a1b871c5bc6.CurrentAFT.CountedBoundedRootTags renaming sum_value → _p2mNativeSupplement107
open _root_.InfoLossLARNativeSupplements.h5a83c4211de0d052.CurrentAFT.CountedBoundedRootTags renaming sum_value_equiv → CurrentAFT.CountedBoundedRootTags.sum_value_equiv
open _root_.InfoLossLARNativeSupplements.h5a83c4211de0d052.CurrentAFT.CountedBoundedRootTags renaming sum_value_equiv → _p2mNativeSupplement108
open _root_.InfoLossLARNativeSupplements.h857ff8dbe55756b8.CurrentAFT.CurrentCycleEntryProgram renaming bodyCount_eq → CurrentAFT.CurrentCycleEntryProgram.bodyCount_eq
open _root_.InfoLossLARNativeSupplements.h857ff8dbe55756b8.CurrentAFT.CurrentCycleEntryProgram renaming bodyCount_eq → _p2mNativeSupplement109
open _root_.InfoLossLARNativeSupplements.h4b24203afd73c049.CurrentAFT.CurrentCycleEntryProgram renaming roots_mod_iff → CurrentAFT.CurrentCycleEntryProgram.roots_mod_iff
open _root_.InfoLossLARNativeSupplements.h4b24203afd73c049.CurrentAFT.CurrentCycleEntryProgram renaming roots_mod_iff → _p2mNativeSupplement110
open _root_.InfoLossLARNativeSupplements.h187f69822989eb63.CurrentAFT.CurrentCycleEntryProgram renaming selectedEntry_cast → CurrentAFT.CurrentCycleEntryProgram.selectedEntry_cast
open _root_.InfoLossLARNativeSupplements.h187f69822989eb63.CurrentAFT.CurrentCycleEntryProgram renaming selectedEntry_cast → _p2mNativeSupplement111
open _root_.InfoLossLARNativeSupplements.h9180bab9fc132623.CurrentAFT.CurrentCycleEntryProgram renaming selected_probability_source → CurrentAFT.CurrentCycleEntryProgram.selected_probability_source
open _root_.InfoLossLARNativeSupplements.h9180bab9fc132623.CurrentAFT.CurrentCycleEntryProgram renaming selected_probability_source → _p2mNativeSupplement112
open _root_.InfoLossLARNativeSupplements.hf7c0550335227b9c.CurrentAFT.CurrentGaussianBits renaming ratio_bounds → CurrentAFT.CurrentGaussianBits.ratio_bounds
open _root_.InfoLossLARNativeSupplements.hf7c0550335227b9c.CurrentAFT.CurrentGaussianBits renaming ratio_bounds → _p2mNativeSupplement113
open _root_.InfoLossLARNativeSupplements.ha029c925a85a9044.CurrentAFT.CurrentPublicShiftProgram renaming encode_value → CurrentAFT.CurrentPublicShiftProgram.encode_value
open _root_.InfoLossLARNativeSupplements.ha029c925a85a9044.CurrentAFT.CurrentPublicShiftProgram renaming encode_value → _p2mNativeSupplement114
open _root_.InfoLossLARNativeSupplements.h4ae04e3f1cb5b359.CurrentAFT.CurrentRootSlotIndex renaming tagPosition_eq → CurrentAFT.CurrentRootSlotIndex.tagPosition_eq
open _root_.InfoLossLARNativeSupplements.h4ae04e3f1cb5b359.CurrentAFT.CurrentRootSlotIndex renaming tagPosition_eq → _p2mNativeSupplement115
open _root_.InfoLossLARNativeSupplements.h20ee486b3d1dffd8.CurrentAFT.CyclePatternProgram renaming averagedEvaluate_cast → CurrentAFT.CyclePatternProgram.averagedEvaluate_cast
open _root_.InfoLossLARNativeSupplements.h20ee486b3d1dffd8.CurrentAFT.CyclePatternProgram renaming averagedEvaluate_cast → _p2mNativeSupplement116
open _root_.InfoLossLARNativeSupplements.hb37dd6f3dcea3b7d.CurrentAFT.CyclePatternProgram renaming evaluate_local → CurrentAFT.CyclePatternProgram.evaluate_local
open _root_.InfoLossLARNativeSupplements.hb37dd6f3dcea3b7d.CurrentAFT.CyclePatternProgram renaming evaluate_local → _p2mNativeSupplement117
open _root_.InfoLossLARNativeSupplements.h689ba464633d8cff.CurrentAFT.CyclePatternProgram renaming evaluate_semantics → CurrentAFT.CyclePatternProgram.evaluate_semantics
open _root_.InfoLossLARNativeSupplements.h689ba464633d8cff.CurrentAFT.CyclePatternProgram renaming evaluate_semantics → _p2mNativeSupplement118
open _root_.InfoLossLARNativeSupplements.hd02155c30f7ca58d.CurrentAFT.CyclePatternProgram renaming gaussianEvaluate_eq_windowEvaluate → CurrentAFT.CyclePatternProgram.gaussianEvaluate_eq_windowEvaluate
open _root_.InfoLossLARNativeSupplements.hd02155c30f7ca58d.CurrentAFT.CyclePatternProgram renaming gaussianEvaluate_eq_windowEvaluate → _p2mNativeSupplement119
open _root_.InfoLossLARNativeSupplements.hc532f8ed89f571a1.CurrentAFT.CyclePatternProgram renaming gaussianEvaluate_semantics → CurrentAFT.CyclePatternProgram.gaussianEvaluate_semantics
open _root_.InfoLossLARNativeSupplements.hc532f8ed89f571a1.CurrentAFT.CyclePatternProgram renaming gaussianEvaluate_semantics → _p2mNativeSupplement120
open _root_.InfoLossLARNativeSupplements.h3e1ece29cbe1b4d4.CurrentAFT.CyclePatternProgram renaming restricted_expectation → CurrentAFT.CyclePatternProgram.restricted_expectation
open _root_.InfoLossLARNativeSupplements.h3e1ece29cbe1b4d4.CurrentAFT.CyclePatternProgram renaming restricted_expectation → _p2mNativeSupplement121
open _root_.InfoLossLARNativeSupplements.h33993643e1712ab8.CurrentAFT.CycleRootExpansion renaming current_body_two_le → CurrentAFT.CycleRootExpansion.current_body_two_le
open _root_.InfoLossLARNativeSupplements.h33993643e1712ab8.CurrentAFT.CycleRootExpansion renaming current_body_two_le → _p2mNativeSupplement122
open _root_.InfoLossLARNativeSupplements.hd99e5f9e75bbb7e1.CurrentAFT.CycleSelections renaming averagedSelectedLaw_coordinate → CurrentAFT.CycleSelections.averagedSelectedLaw_coordinate
open _root_.InfoLossLARNativeSupplements.hd99e5f9e75bbb7e1.CurrentAFT.CycleSelections renaming averagedSelectedLaw_coordinate → _p2mNativeSupplement123
open _root_.InfoLossLARNativeSupplements.h788b7dba49e3cc6b.CurrentAFT.CycleSelections renaming selectedLaw_coordinate → CurrentAFT.CycleSelections.selectedLaw_coordinate
open _root_.InfoLossLARNativeSupplements.h788b7dba49e3cc6b.CurrentAFT.CycleSelections renaming selectedLaw_coordinate → _p2mNativeSupplement124
open _root_.InfoLossLARNativeSupplements.h8eafa7c1234de921.CurrentAFT.FullResponseEnumeration renaming mem_boundedSubsets → CurrentAFT.FullResponseEnumeration.mem_boundedSubsets
open _root_.InfoLossLARNativeSupplements.h8eafa7c1234de921.CurrentAFT.FullResponseEnumeration renaming mem_boundedSubsets → _p2mNativeSupplement125
open _root_.InfoLossLARNativeSupplements.h4d94229af05aca84.CurrentAFT.Parameters renaming modulus_eq_find → CurrentAFT.Parameters.modulus_eq_find
open _root_.InfoLossLARNativeSupplements.h4d94229af05aca84.CurrentAFT.Parameters renaming modulus_eq_find → _p2mNativeSupplement126
open _root_.InfoLossLARNativeSupplements.h7c8d9f04458b8eff.CurrentAFT.ProtectedCycle renaming cycleLaw_marginal → CurrentAFT.ProtectedCycle.cycleLaw_marginal
open _root_.InfoLossLARNativeSupplements.h7c8d9f04458b8eff.CurrentAFT.ProtectedCycle renaming cycleLaw_marginal → _p2mNativeSupplement127
open _root_.InfoLossLARNativeSupplements.ha0baf4dfdfb7707f.CurrentAFT.ProtectedCycle renaming cycleLaw_marginal_of_two_le → CurrentAFT.ProtectedCycle.cycleLaw_marginal_of_two_le
open _root_.InfoLossLARNativeSupplements.ha0baf4dfdfb7707f.CurrentAFT.ProtectedCycle renaming cycleLaw_marginal_of_two_le → _p2mNativeSupplement128
open _root_.InfoLossLARNativeSupplements.h94f62847a0770621.CurrentAFT.ProtectedCycle renaming independent_pair_law → CurrentAFT.ProtectedCycle.independent_pair_law
open _root_.InfoLossLARNativeSupplements.h94f62847a0770621.CurrentAFT.ProtectedCycle renaming independent_pair_law → _p2mNativeSupplement129
open _root_.InfoLossLARNativeSupplements.h2021c53cebcb97f7.CurrentAFT.RationalScalarFamilyAverage renaming current_eq_window_sum → CurrentAFT.RationalScalarFamilyAverage.current_eq_window_sum
open _root_.InfoLossLARNativeSupplements.h2021c53cebcb97f7.CurrentAFT.RationalScalarFamilyAverage renaming current_eq_window_sum → _p2mNativeSupplement130
open _root_.InfoLossLARNativeSupplements.h6473fbae786c301e.CurrentAFT.RootSignedMassProgram renaming referenceEntry_cast → CurrentAFT.RootSignedMassProgram.referenceEntry_cast
open _root_.InfoLossLARNativeSupplements.h6473fbae786c301e.CurrentAFT.RootSignedMassProgram renaming referenceEntry_cast → _p2mNativeSupplement131
open _root_.InfoLossLARNativeSupplements.h4c23fa31c2a53a0c.CurrentAFT.RootSignedMassProgram renaming singleton_probability → CurrentAFT.RootSignedMassProgram.singleton_probability
open _root_.InfoLossLARNativeSupplements.h4c23fa31c2a53a0c.CurrentAFT.RootSignedMassProgram renaming singleton_probability → _p2mNativeSupplement132
open _root_.InfoLossLARNativeSupplements.hb738544899c1bf50.CurrentAFT.TensorPublicProjection renaming independent_decision_expectation → CurrentAFT.TensorPublicProjection.independent_decision_expectation
open _root_.InfoLossLARNativeSupplements.hb738544899c1bf50.CurrentAFT.TensorPublicProjection renaming independent_decision_expectation → _p2mNativeSupplement133
open _root_.InfoLossLARNativeSupplements.hf2382f7ddc8e222e.AFT.RationalNumerics renaming natAbs_finset_sum_le → AFT.RationalNumerics.natAbs_finset_sum_le
open _root_.InfoLossLARNativeSupplements.hf2382f7ddc8e222e.AFT.RationalNumerics renaming natAbs_finset_sum_le → _p2mNativeSupplement134
open _root_.InfoLossLARNativeSupplements.h707562ec4bd11ae8.AFT.RationalNumerics.HeightBound renaming reduced_le → AFT.RationalNumerics.HeightBound.reduced_le
open _root_.InfoLossLARNativeSupplements.h707562ec4bd11ae8.AFT.RationalNumerics.HeightBound renaming reduced_le → _p2mNativeSupplement135
open _root_.InfoLossLARNativeSupplements.hd70d55bab6f2346d.CurrentAFT.CountedBoundedRootTags renaming bounded_sum → CurrentAFT.CountedBoundedRootTags.bounded_sum
open _root_.InfoLossLARNativeSupplements.hd70d55bab6f2346d.CurrentAFT.CountedBoundedRootTags renaming bounded_sum → _p2mNativeSupplement136
open _root_.InfoLossLARNativeSupplements.hb84db39a2a2ae627.CurrentAFT.CountedBoundedRootTags renaming flatMap_attach → CurrentAFT.CountedBoundedRootTags.flatMap_attach
open _root_.InfoLossLARNativeSupplements.hb84db39a2a2ae627.CurrentAFT.CountedBoundedRootTags renaming flatMap_attach → _p2mNativeSupplement137
open _root_.InfoLossLARNativeSupplements.he91e90e23380c2d0.CurrentAFT.JointTaggedPhysicalFock renaming law_nonempty → CurrentAFT.JointTaggedPhysicalFock.law_nonempty
open _root_.InfoLossLARNativeSupplements.he91e90e23380c2d0.CurrentAFT.JointTaggedPhysicalFock renaming law_nonempty → _p2mNativeSupplement138

section












































namespace AFT
end AFT
namespace AFT.FiniteDist
end AFT.FiniteDist
namespace AFT.FiniteDist.mk
end AFT.FiniteDist.mk
namespace AFT.FiniteDist.uniform
end AFT.FiniteDist.uniform
namespace AFT.canonicalEndpointEquiv
end AFT.canonicalEndpointEquiv
namespace AFT.endpointIntegralPreimage
end AFT.endpointIntegralPreimage
namespace AFT.endpointLift
end AFT.endpointLift
namespace AFT.endpointProjection
end AFT.endpointProjection
namespace AFT.integerLabel
end AFT.integerLabel
namespace AFT.liftCoordinates
end AFT.liftCoordinates
namespace AFT.pathIncidence
end AFT.pathIncidence
namespace AFT.reduceCoordinates
end AFT.reduceCoordinates
namespace AFT.uniformZeroFiber
end AFT.uniformZeroFiber
namespace AFT.zeroFiberPullback
end AFT.zeroFiberPullback
namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.CurrentZeroFiber
end CurrentAFT.CurrentZeroFiber
namespace CurrentAFT.CurrentZeroFiber.label
end CurrentAFT.CurrentZeroFiber.label
namespace CurrentAFT.CurrentZeroFiber.pullback
end CurrentAFT.CurrentZeroFiber.pullback
namespace CurrentAFT.CurrentZeroFiber.rawPullback
end CurrentAFT.CurrentZeroFiber.rawPullback
namespace CurrentAFT.CurrentZeroFiber.uniformFiber
end CurrentAFT.CurrentZeroFiber.uniformFiber
namespace InfoLossLAR
end InfoLossLAR
namespace InfoLossLAR.WorstCase
end InfoLossLAR.WorstCase
namespace InfoLossLAR.WorstCase.Integration
end InfoLossLAR.WorstCase.Integration
namespace InfoLossLAR.WorstCase.TDef63
end InfoLossLAR.WorstCase.TDef63
namespace InfoLossLAR.WorstCase.unifOn
end InfoLossLAR.WorstCase.unifOn
namespace InfoLossLAR.WorstCase.zeroFiber63
end InfoLossLAR.WorstCase.zeroFiber63



set_option autoImplicit false
set_option maxHeartbeats 2000000
set_option maxRecDepth 10000
set_option backward.isDefEq.respectTransparency false
attribute [local irreducible] CurrentAFT.Parameters.base CurrentAFT.Parameters.bodySize
  CurrentAFT.Parameters.dimension CurrentAFT.Parameters.legCount

open scoped BigOperators Classical

namespace InfoLossLAR.WorstCase.Integration

open AFT CurrentAFT.CurrentAffineGeometry

local instance _root_.InfoLossLAR.WorstCase.Integration.instNeZeroNatModulus_integration9_9 (security : ℕ) : NeZero (_root_.CurrentAFT.Parameters.modulus security) :=
  ⟨(_root_.CurrentAFT.Parameters.modulus_spec security).2.ne_zero⟩





end InfoLossLAR.WorstCase.Integration

end

section















































namespace AFT
end AFT
namespace AFT.FiniteDist
end AFT.FiniteDist
namespace AFT.FiniteDist.TVBound
end AFT.FiniteDist.TVBound
namespace AFT.FiniteDist.totalVariation
end AFT.FiniteDist.totalVariation
namespace AFT.GaussianSource
end AFT.GaussianSource
namespace AFT.GaussianSource.independentCoordinates
end AFT.GaussianSource.independentCoordinates
namespace AFT.GaussianSource.independentCoordinates.match_1
end AFT.GaussianSource.independentCoordinates.match_1
namespace AFT.GaussianSource.integerGaussian
end AFT.GaussianSource.integerGaussian
namespace AFT.GaussianSource.latticeGaussian
end AFT.GaussianSource.latticeGaussian
namespace AFT.GaussianSource.latticeWeight
end AFT.GaussianSource.latticeWeight
namespace AFT.GaussianSource.residueGaussian
end AFT.GaussianSource.residueGaussian
namespace AFT.GaussianSource.weight
end AFT.GaussianSource.weight
namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.CurrentHiddenSourceTransport
end CurrentAFT.CurrentHiddenSourceTransport
namespace CurrentAFT.CurrentSourceLayout
end CurrentAFT.CurrentSourceLayout
namespace CurrentAFT.CurrentSourceLayout.splitWord
end CurrentAFT.CurrentSourceLayout.splitWord
namespace InfoLossLAR
end InfoLossLAR
namespace InfoLossLAR.WorstCase
end InfoLossLAR.WorstCase
namespace InfoLossLAR.WorstCase.Integration
end InfoLossLAR.WorstCase.Integration
namespace InfoLossLAR.WorstCase.avg
end InfoLossLAR.WorstCase.avg
namespace InfoLossLAR.WorstCase.evalAffine
end InfoLossLAR.WorstCase.evalAffine
namespace InfoLossLAR.WorstCase.matModq
end InfoLossLAR.WorstCase.matModq
namespace InfoLossLAR.WorstCase.modq
end InfoLossLAR.WorstCase.modq
namespace InfoLossLAR.WorstCase.transportLaw
end InfoLossLAR.WorstCase.transportLaw




set_option autoImplicit false
set_option backward.isDefEq.respectTransparency false
set_option maxHeartbeats 1000000

open scoped BigOperators Classical

namespace InfoLossLAR.WorstCase.Integration
open AFT GaussianSource
open CurrentAFT.CurrentSourceProtection

local instance currentModulusNeZero (security : ℕ) : NeZero (_root_.CurrentAFT.Parameters.modulus security) :=
  ⟨(_root_.CurrentAFT.Parameters.modulus_spec security).2.ne_zero⟩





end InfoLossLAR.WorstCase.Integration

end

section




































set_option autoImplicit false

namespace Integration9_9.FairBitPadding
open InfoLossLAR.WorstCase





/-- Splitting a flat random tape into its prefix and suffix. -/
def splitEquiv (n extra : ℕ) :
    (Fin (n+extra) → Bool) ≃ (Fin n → Bool) × (Fin extra → Bool) :=
  (Equiv.arrowCongr finSumFinEquiv.symm (Equiv.refl Bool)).trans
    (Equiv.sumArrowEquivProdArrow (Fin n) (Fin extra) Bool)











end Integration9_9.FairBitPadding

end

section


































































namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.ActualConstructiveSampler
end CurrentAFT.ActualConstructiveSampler




set_option autoImplicit false
set_option backward.isDefEq.respectTransparency false
set_option maxHeartbeats 2000000
set_option maxRecDepth 10000

namespace InfoLossLAR.WorstCase.Integration
open AFT CurrentAFT
open scoped BigOperators Classical

attribute [local irreducible] CurrentAFT.Parameters.base CurrentAFT.Parameters.bodySize CurrentAFT.Parameters.dimension CurrentAFT.Parameters.legCount

local instance actualSamplerModulusNeZero (security : ℕ) : NeZero (_root_.CurrentAFT.Parameters.modulus security) :=
  ⟨(_root_.CurrentAFT.Parameters.modulus_spec security).2.ne_zero⟩

/-- The literal integer affine pair emitted by the proved finite-bit program. -/
noncomputable def actualSampler (order : ℕ) (data : _root_.CurrentAFT.BinaryFullResponseData.Data order)
    (security : ℕ) (word : Fin (_root_.CurrentAFT.PublicBitTapes.current security).bits → Fin 2) :
    Matrix (Fin (_root_.CurrentAFT.Parameters.dimension security)) (Fin (_root_.CurrentAFT.Parameters.dimension security)) ℤ ×
      (Fin (_root_.CurrentAFT.Parameters.dimension security) → ℤ) :=
  (_root_.CurrentAFT.CurrentBinaryAffineEmitter.matrix security (_root_.CurrentAFT.ActualConstructiveSampler.run security order data word),
    _root_.CurrentAFT.CurrentBinaryAffineEmitter.offset security (_root_.CurrentAFT.ActualConstructiveSampler.run security order data word))





/-- The program consumes a prefix of the specified polynomial Boolean tape. -/
noncomputable def paddedActualSampler (order : ℕ) (data : _root_.CurrentAFT.BinaryFullResponseData.Data order)
    (m : Polynomial ℕ) (hbits : ∀ security, (_root_.CurrentAFT.PublicBitTapes.current security).bits ≤ m.eval security)
    (security : ℕ) (tape : Fin (m.eval security) → Bool) :
    Matrix (Fin (_root_.CurrentAFT.Parameters.dimension security)) (Fin (_root_.CurrentAFT.Parameters.dimension security)) ℤ ×
      (Fin (_root_.CurrentAFT.Parameters.dimension security) → ℤ) :=
  _root_.InfoLossLAR.WorstCase.Integration.actualSampler order data security (fun i => finTwoEquiv.symm (tape (Fin.castLE (hbits security) i)))





end InfoLossLAR.WorstCase.Integration

end

section




































namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.Parameters
end CurrentAFT.Parameters
namespace CurrentAFT.Parameters.base
end CurrentAFT.Parameters.base
namespace CurrentAFT.Parameters.bodySize
end CurrentAFT.Parameters.bodySize
namespace CurrentAFT.Parameters.calibrationOrder
end CurrentAFT.Parameters.calibrationOrder
namespace CurrentAFT.Parameters.dimension
end CurrentAFT.Parameters.dimension
namespace CurrentAFT.Parameters.leastPrimeWitness
end CurrentAFT.Parameters.leastPrimeWitness
namespace CurrentAFT.Parameters.legCount
end CurrentAFT.Parameters.legCount
namespace CurrentAFT.Parameters.primeThreshold
end CurrentAFT.Parameters.primeThreshold
namespace CurrentAFT.Parameters.scale
end CurrentAFT.Parameters.scale



set_option autoImplicit false

namespace InfoLossLAR.WorstCase.Integration

/-- The local and platform parameter families have the same base. -/
theorem local_base_eq (lam : ℕ) : _root_.CurrentAFT.Parameters.base lam = famL lam := rfl

/-- The local checked least-prime representation is exactly the platform modulus. -/
theorem local_modulus_eq (lam : ℕ) : _root_.CurrentAFT.Parameters.modulus lam = famq lam := by
  rw [_root_.InfoLossLARNativeSupplements.h4d94229af05aca84.CurrentAFT.Parameters.modulus_eq_find]
  simp only [_root_.CurrentAFT.Parameters.primeThreshold, _root_.CurrentAFT.Parameters.scale,
    _root_.CurrentAFT.Parameters.base, famq, famL, ← pow_mul]

/-- Both encodings use the same actual block dimension, including the two leg pools. -/
theorem local_dimension_eq (lam : ℕ) : _root_.CurrentAFT.Parameters.dimension lam = famd lam := by
  simp only [_root_.CurrentAFT.Parameters.dimension, _root_.CurrentAFT.Parameters.bodySize_eq,
    _root_.CurrentAFT.Parameters.legCount_eq, _root_.InfoLossLAR.WorstCase.Integration.local_modulus_eq, _root_.InfoLossLAR.WorstCase.Integration.local_base_eq, famd]



end InfoLossLAR.WorstCase.Integration

end

section





































































set_option autoImplicit false
set_option backward.isDefEq.respectTransparency false
set_option maxHeartbeats 1000000
set_option maxRecDepth 10000

namespace InfoLossLAR.WorstCase.Integration
open scoped BigOperators Classical

/-- Relabel both matrix indices and the offset by the same exact dimension equality. -/
def reindexAffine {Ω : Type*} {d d' : ℕ} (hd : d = d')
    (sampler : Ω → Matrix (Fin d) (Fin d) ℤ × (Fin d → ℤ)) :
    Ω → Matrix (Fin d') (Fin d') ℤ × (Fin d' → ℤ) :=
  fun tape =>
    (fun i j => (sampler tape).1 (Fin.cast hd.symm i) (Fin.cast hd.symm j),
      fun i => (sampler tape).2 (Fin.cast hd.symm i))





/-- The same actual sampler, with its integer output indexed by the platform family dimension. -/
noncomputable def fixedFamilyActualSampler (order : ℕ) (data : _root_.CurrentAFT.BinaryFullResponseData.Data order)
    (m : Polynomial ℕ)
    (hbits : ∀ security, (_root_.CurrentAFT.PublicBitTapes.current security).bits ≤ m.eval security)
    (security : ℕ) :
    (Fin (m.eval security) → Bool) →
      Matrix (Fin (famd security)) (Fin (famd security)) ℤ × (Fin (famd security) → ℤ) :=
  _root_.InfoLossLAR.WorstCase.Integration.reindexAffine (_root_.InfoLossLAR.WorstCase.Integration.local_dimension_eq security) (_root_.InfoLossLAR.WorstCase.Integration.paddedActualSampler order data m hbits security)

local instance fixedFamilySamplerLocalModulusNeZero (security : ℕ) :
    NeZero (_root_.CurrentAFT.Parameters.modulus security) :=
  ⟨(_root_.CurrentAFT.Parameters.modulus_spec security).2.ne_zero⟩





end InfoLossLAR.WorstCase.Integration

end

section










































set_option autoImplicit false

namespace Integration9_9.TM2OutputLength
open Turing StateTransition

variable {K Λ σ : Type} {Γ : K → Type} [Fintype K] [DecidableEq K]

/-- Largest stack length; unlike a total-size bound it needs no common stack alphabet. -/
def stackSize (S : ∀ k, List (Γ k)) : ℕ := Finset.univ.sup fun k => (S k).length

omit [DecidableEq K] in
theorem length_le_stackSize (S : ∀ k, List (Γ k)) (k : K) :
    (S k).length ≤ _root_.Integration9_9.TM2OutputLength.stackSize S :=
  by
    unfold _root_.Integration9_9.TM2OutputLength.stackSize
    exact Finset.le_sup (f := fun j => (S j).length) (Finset.mem_univ k)

theorem stackSize_update_le (S : ∀ k, List (Γ k)) (k : K) (v : List (Γ k)) (n : ℕ)
    (hS : _root_.Integration9_9.TM2OutputLength.stackSize S ≤ n) (hv : v.length ≤ n) :
    _root_.Integration9_9.TM2OutputLength.stackSize (Function.update S k v) ≤ n := by
  apply Finset.sup_le
  intro j hj
  by_cases h : j = k
  · subst j
    simpa using hv
  · simpa [Function.update_of_ne h] using (_root_.Integration9_9.TM2OutputLength.length_le_stackSize S j).trans hS

/-- The maximum number of pushes along one branch of a single TM2 statement. -/
def pushBound : TM2.Stmt Γ Λ σ → ℕ
  | .push _ _ q => pushBound q + 1
  | .peek _ _ q => pushBound q
  | .pop _ _ q => pushBound q
  | .load _ q => pushBound q
  | .branch _ q r => max (pushBound q) (pushBound r)
  | .goto _ => 0
  | .halt => 0

theorem stepAux_stackSize_le (q : TM2.Stmt Γ Λ σ) (v : σ) (S : ∀ k, List (Γ k)) :
    _root_.Integration9_9.TM2OutputLength.stackSize (TM2.stepAux q v S).stk ≤ _root_.Integration9_9.TM2OutputLength.stackSize S + _root_.Integration9_9.TM2OutputLength.pushBound q := by
  induction q generalizing v S with
  | push k f q ih =>
    have h := ih v (Function.update S k (f v :: S k))
    have hu : _root_.Integration9_9.TM2OutputLength.stackSize (Function.update S k (f v :: S k)) ≤ _root_.Integration9_9.TM2OutputLength.stackSize S + 1 :=
      _root_.Integration9_9.TM2OutputLength.stackSize_update_le S k _ _ (Nat.le_add_right _ _) (by
        simpa using Nat.succ_le_succ (_root_.Integration9_9.TM2OutputLength.length_le_stackSize S k))
    simpa only [TM2.stepAux, _root_.Integration9_9.TM2OutputLength.pushBound] using h.trans (by omega)
  | peek k f q ih => simpa only [TM2.stepAux, _root_.Integration9_9.TM2OutputLength.pushBound] using ih (f v (S k).head?) S
  | pop k f q ih =>
    have h := ih (f v (S k).head?) (Function.update S k (S k).tail)
    have hu : _root_.Integration9_9.TM2OutputLength.stackSize (Function.update S k (S k).tail) ≤ _root_.Integration9_9.TM2OutputLength.stackSize S :=
      _root_.Integration9_9.TM2OutputLength.stackSize_update_le S k _ _ (le_refl _) (by
        simp only [List.length_tail]
        exact (Nat.sub_le _ _).trans (_root_.Integration9_9.TM2OutputLength.length_le_stackSize S k))
    simpa only [TM2.stepAux, _root_.Integration9_9.TM2OutputLength.pushBound] using h.trans (Nat.add_le_add_right hu _)
  | load f q ih => simpa only [TM2.stepAux, _root_.Integration9_9.TM2OutputLength.pushBound] using ih (f v) S
  | branch f q r iq ir =>
    cases h : f v with
    | false => simpa only [TM2.stepAux, h, Bool.cond_false, _root_.Integration9_9.TM2OutputLength.pushBound] using
        (ir v S).trans (Nat.add_le_add_left (le_max_right _ _) _)
    | true => simpa only [TM2.stepAux, h, Bool.cond_true, _root_.Integration9_9.TM2OutputLength.pushBound] using
        (iq v S).trans (Nat.add_le_add_left (le_max_left _ _) _)
  | goto f => simp [TM2.stepAux, _root_.Integration9_9.TM2OutputLength.pushBound]
  | halt => simp [TM2.stepAux, _root_.Integration9_9.TM2OutputLength.pushBound]

variable [Fintype Λ]

/-- A constant depending only on the finite machine, not on its input. -/
def programPushBound (M : Λ → TM2.Stmt Γ Λ σ) : ℕ :=
  Finset.univ.sup fun l => _root_.Integration9_9.TM2OutputLength.pushBound (M l)

theorem step_stackSize_le (M : Λ → TM2.Stmt Γ Λ σ) (a b : TM2.Cfg Γ Λ σ)
    (h : TM2.step M a = some b) :
    _root_.Integration9_9.TM2OutputLength.stackSize b.stk ≤ _root_.Integration9_9.TM2OutputLength.stackSize a.stk + _root_.Integration9_9.TM2OutputLength.programPushBound M := by
  rcases a with ⟨l,v,S⟩
  cases l with
  | none => simp [TM2.step] at h
  | some l =>
    simp only [TM2.step, Option.some.injEq] at h
    subst b
    have hp : _root_.Integration9_9.TM2OutputLength.pushBound (M l) ≤ _root_.Integration9_9.TM2OutputLength.programPushBound M := by
      unfold _root_.Integration9_9.TM2OutputLength.programPushBound
      exact Finset.le_sup (f := fun j => _root_.Integration9_9.TM2OutputLength.pushBound (M j)) (Finset.mem_univ l)
    exact (_root_.Integration9_9.TM2OutputLength.stepAux_stackSize_le (M l) v S).trans (Nat.add_le_add_left hp _)

/-- Abstract accumulation of a one-step size bound through a bounded execution. -/
theorem iterate_size_bound {C : Type} (step : C → Option C) (size : C → ℕ) (cost : ℕ)
    (hstep : ∀ a b, step a = some b → size b ≤ size a + cost) :
    ∀ n a b, ((fun x : Option C => x.bind step)^[n]) (some a) = some b →
      size b ≤ size a + cost * n := by
  intro n
  induction n with
  | zero =>
    intro a b h
    simp only [Function.iterate_zero, id_eq, Option.some.injEq] at h
    subst b
    simp
  | succ n ih =>
    intro a b h
    rw [Function.iterate_succ_apply] at h
    cases hs : step a with
    | none =>
      have hn : ∀ k, ((fun x : Option C => x.bind step)^[k]) none = none := by
        intro k
        induction k with
        | zero => rfl
        | succ k hk => simpa only [Function.iterate_succ_apply, Option.bind_none] using hk
      simp only [Option.bind_some, hs, hn] at h
      contradiction
    | some a' =>
      simp only [Option.bind_some, hs] at h
      have hi := ih a' b h
      have hlocal := hstep a a' hs
      calc
        size b ≤ size a' + cost * n := hi
        _ ≤ size a + cost * (n+1) := by rw [Nat.mul_add, Nat.mul_one]; omega

theorem evalsToInTime_stackSize_le (M : Λ → TM2.Stmt Γ Λ σ)
    (a b : TM2.Cfg Γ Λ σ) (m : ℕ)
    (h : EvalsToInTime (TM2.step M) a (some b) m) :
    _root_.Integration9_9.TM2OutputLength.stackSize b.stk ≤ _root_.Integration9_9.TM2OutputLength.stackSize a.stk + _root_.Integration9_9.TM2OutputLength.programPushBound M * m := by
  have hs := _root_.Integration9_9.TM2OutputLength.iterate_size_bound (TM2.step M) (fun c => _root_.Integration9_9.TM2OutputLength.stackSize c.stk)
    (_root_.Integration9_9.TM2OutputLength.programPushBound M) (_root_.Integration9_9.TM2OutputLength.step_stackSize_le M) h.steps a b h.evals_in_steps
  exact hs.trans (Nat.add_le_add_left (Nat.mul_le_mul_left _ h.steps_le_m) _)

local instance tmStacksFintype (tm : FinTM2) : Fintype tm.K := tm.kFin
local instance tmLabelsFintype (tm : FinTM2) : Fintype tm.Λ := tm.ΛFin

/-- The initialized machine has no stack longer than its input. -/
theorem initList_stackSize_le (tm : FinTM2) (input : List (tm.Γ tm.k₀)) :
    _root_.Integration9_9.TM2OutputLength.stackSize (initList tm input).stk ≤ input.length := by
  apply Finset.sup_le
  intro k hk
  by_cases h : k = tm.k₀
  · subst k
    simp [initList]
  · simp [initList, h]

/-- A machine cannot emit more symbols than it initially receives plus its
fixed per-step push capacity times the allowed number of steps. -/
theorem output_length_le (tm : FinTM2) (input : List (tm.Γ tm.k₀))
    (output : List (tm.Γ tm.k₁)) (time : ℕ)
    (h : TM2OutputsInTime tm input (some output) time) :
    output.length ≤ input.length + _root_.Integration9_9.TM2OutputLength.programPushBound tm.m * time := by
  have hs := _root_.Integration9_9.TM2OutputLength.evalsToInTime_stackSize_le tm.m (initList tm input) (haltList tm output) time h
  calc
    output.length = ((haltList tm output).stk tm.k₁).length := by simp [haltList]
    _ ≤ _root_.Integration9_9.TM2OutputLength.stackSize (haltList tm output).stk := _root_.Integration9_9.TM2OutputLength.length_le_stackSize _ _
    _ ≤ _root_.Integration9_9.TM2OutputLength.stackSize (initList tm input).stk + _root_.Integration9_9.TM2OutputLength.programPushBound tm.m * time := hs
    _ ≤ input.length + _root_.Integration9_9.TM2OutputLength.programPushBound tm.m * time :=
      Nat.add_le_add_right (_root_.Integration9_9.TM2OutputLength.initList_stackSize_le tm input) _



end Integration9_9.TM2OutputLength

end

section








































set_option autoImplicit false
set_option maxHeartbeats 1600000

/-! Typed stack embedding and exact bounded-run simulation for actual TM2 programs. -/
namespace Integration9_9.TM2Simulation
open Turing StateTransition Function

/-- A finite-control lens. This contains only finite-state operations when the
source and target control types are finite; it cannot inspect a whole stack. -/
structure ControlLens (σ τ : Type) where
  read : τ → σ
  write : τ → σ → τ
  read_write : ∀ t s, read (write t s) = s
  write_read : ∀ t, write t (read t) = t
  write_write : ∀ t s r, write (write t s) r = write t r

namespace ControlLens
variable {σ τ : Type}
def fst : _root_.Integration9_9.TM2Simulation.ControlLens σ (σ × τ) where
  read := Prod.fst
  write t s := (s,t.2)
  read_write _ _ := rfl
  write_read _ := rfl
  write_write _ _ _ := rfl

def snd : _root_.Integration9_9.TM2Simulation.ControlLens τ (σ × τ) where
  read := Prod.snd
  write t s := (t.1,s)
  read_write _ _ := rfl
  write_read _ := rfl
  write_write _ _ _ := rfl
end ControlLens

variable {K J Λ L σ τ : Type} {Γ : K → Type} {Δ : J → Type}
variable [DecidableEq K] [DecidableEq J]

/-- Translate every concrete stack action to an injectively embedded stack.
Halting may either halt the containing machine or jump to a continuation. -/
def statement (i : K → J) (e : ∀ k, Γ k ≃ Δ (i k))
    (v : _root_.Integration9_9.TM2Simulation.ControlLens σ τ) (label : Λ → L) (finish : Option L) :
    TM2.Stmt Γ Λ σ → TM2.Stmt Δ L τ
  | .push k f q => .push (i k) (fun t => e k (f (v.read t))) (statement i e v label finish q)
  | .peek k f q => .peek (i k) (fun t a => v.write t (f (v.read t) (a.map (e k).symm)))
      (statement i e v label finish q)
  | .pop k f q => .pop (i k) (fun t a => v.write t (f (v.read t) (a.map (e k).symm)))
      (statement i e v label finish q)
  | .load f q => .load (fun t => v.write t (f (v.read t))) (statement i e v label finish q)
  | .branch f q r => .branch (fun t => f (v.read t))
      (statement i e v label finish q) (statement i e v label finish r)
  | .goto f => .goto (fun t => label (f (v.read t)))
  | .halt => match finish with | none => .halt | some l => .goto (fun _ => l)

/-- The source stacks are encoded, while stacks outside their image are framed. -/
def StacksRel (i : K → J) (e : ∀ k, Γ k ≃ Δ (i k))
    (S : ∀ k, List (Γ k)) (T : ∀ j, List (Δ j)) : Prop :=
  ∀ k, T (i k) = (S k).map (e k)

theorem stacksRel_update (i : K → J) (hi : Injective i) (e : ∀ k, Γ k ≃ Δ (i k))
    {S : ∀ k, List (Γ k)} {T : ∀ j, List (Δ j)} (h : _root_.Integration9_9.TM2Simulation.StacksRel i e S T)
    (k : K) (x : List (Γ k)) :
    _root_.Integration9_9.TM2Simulation.StacksRel i e (update S k x) (update T (i k) (x.map (e k))) := by
  intro j
  by_cases hj : j = k
  · subst j
    simp
  · have hij : i j ≠ i k := fun hh => hj (hi hh)
    simpa [update_of_ne hj, update_of_ne hij] using h j

theorem stepAux_simulation (i : K → J) (hi : Injective i)
    (e : ∀ k, Γ k ≃ Δ (i k)) (v : _root_.Integration9_9.TM2Simulation.ControlLens σ τ) (label : Λ → L) (finish : Option L)
    (q : TM2.Stmt Γ Λ σ) (s : σ) (t : τ) (S : ∀ k, List (Γ k)) (T : ∀ j, List (Δ j))
    (hv : v.read t = s) (hS : _root_.Integration9_9.TM2Simulation.StacksRel i e S T) :
    let a := TM2.stepAux q s S
    let b := TM2.stepAux (_root_.Integration9_9.TM2Simulation.statement i e v label finish q) t T
    b.l = a.l.elim finish (fun l => some (label l)) ∧
      b.var = v.write t a.var ∧ _root_.Integration9_9.TM2Simulation.StacksRel i e a.stk b.stk ∧
      ∀ j, (∀ k, i k ≠ j) → b.stk j = T j := by
  induction q generalizing s t S T with
  | push k f q ih =>
    simp only [_root_.Integration9_9.TM2Simulation.statement, TM2.stepAux, hv]
    have hu := _root_.Integration9_9.TM2Simulation.stacksRel_update i hi e hS k (f s :: S k)
    simp only [List.map_cons, ← hS k] at hu
    obtain ⟨hl,hv',hs,hfr⟩ := ih s t _ _ hv hu
    refine ⟨hl,hv',hs,?_⟩
    intro j hj
    rw [hfr j hj, update_of_ne (Ne.symm (hj k))]
  | peek k f q ih =>
    have hh : (T (i k)).head?.map (e k).symm = (S k).head? := by
      rw [hS k, List.head?_map, Option.map_map]
      simp
    simp only [_root_.Integration9_9.TM2Simulation.statement, TM2.stepAux, hv, hh]
    obtain ⟨hl,hv',hs,hfr⟩ := ih _ _ S T (v.read_write _ _) hS
    exact ⟨hl, hv'.trans (v.write_write _ _ _),hs,hfr⟩
  | pop k f q ih =>
    have hh : (T (i k)).head?.map (e k).symm = (S k).head? := by
      rw [hS k, List.head?_map, Option.map_map]
      simp
    have hu := _root_.Integration9_9.TM2Simulation.stacksRel_update i hi e hS k (S k).tail
    rw [List.map_tail, ← hS k] at hu
    simp only [_root_.Integration9_9.TM2Simulation.statement, TM2.stepAux, hv, hh]
    obtain ⟨hl,hv',hs,hfr⟩ := ih _ _ _ _ (v.read_write _ _) hu
    refine ⟨hl,hv'.trans (v.write_write _ _ _),hs,?_⟩
    intro j hj
    rw [hfr j hj, update_of_ne (Ne.symm (hj k))]
  | load f q ih =>
    simp only [_root_.Integration9_9.TM2Simulation.statement, TM2.stepAux, hv]
    obtain ⟨hl,hv',hs,hfr⟩ := ih _ _ S T (v.read_write _ _) hS
    exact ⟨hl,hv'.trans (v.write_write _ _ _),hs,hfr⟩
  | branch f q r iq ir =>
    cases hf : f s <;> simp only [_root_.Integration9_9.TM2Simulation.statement, TM2.stepAux, hv, hf, Bool.cond_false, Bool.cond_true]
    · exact ir s t S T hv hS
    · exact iq s t S T hv hS
  | goto f =>
    simp only [_root_.Integration9_9.TM2Simulation.statement, TM2.stepAux, Option.elim_some, hv]
    exact ⟨trivial,by simpa only [hv] using (v.write_read t).symm,hS,fun _ _ => trivial⟩
  | halt =>
    cases finish <;> simp only [_root_.Integration9_9.TM2Simulation.statement, TM2.stepAux, Option.elim_none]
    all_goals exact ⟨trivial,by simpa only [hv] using (v.write_read t).symm,hS,fun _ _ => trivial⟩

/-- Simulation of successful transitions needs no assertion about what happens
after a source machine halts. This permits a halt to become a continuation. -/
theorem iterate_simulation {A B : Type} (f : A → Option A) (g : B → Option B)
    (R : A → B → Prop)
    (hstep : ∀ a a' b, f a = some a' → R a b → ∃ b', g b = some b' ∧ R a' b') :
    ∀ n a a' b, ((fun c : Option A => c.bind f)^[n]) (some a) = some a' → R a b →
      ∃ b', ((fun c : Option B => c.bind g)^[n]) (some b) = some b' ∧ R a' b' := by
  intro n
  induction n with
  | zero =>
    intro a a' b hn hr
    simp only [iterate_zero, id_eq, Option.some.injEq] at hn
    subst a'
    exact ⟨b,rfl,hr⟩
  | succ n ih =>
    intro a a' b hn hr
    rw [iterate_succ_apply] at hn
    cases hf : f a with
    | none =>
      have hnone : ∀ m, ((fun c : Option A => c.bind f)^[m]) none = none := by
        intro m
        induction m with
        | zero => rfl
        | succ m hm => simpa only [iterate_succ_apply, Option.bind_none] using hm
      simp only [Option.bind_some, hf, hnone] at hn
      contradiction
    | some a1 =>
      simp only [Option.bind_some, hf] at hn
      obtain ⟨b1,hb1,hr1⟩ := hstep a a1 b hf hr
      obtain ⟨b',hb',hr'⟩ := ih a1 a' b1 hn hr1
      refine ⟨b',?_,hr'⟩
      simpa only [iterate_succ_apply, Option.bind_some, hb1] using hb'

theorem evalsToInTime_simulation {A B : Type} (f : A → Option A) (g : B → Option B)
    (R : A → B → Prop)
    (hstep : ∀ a a' b, f a = some a' → R a b → ∃ b', g b = some b' ∧ R a' b')
    (a a' : A) (b : B) (m : ℕ) (h : EvalsToInTime f a (some a') m) (hr : R a b) :
    ∃ b', Nonempty (EvalsToInTime g b (some b') m) ∧ R a' b' := by
  obtain ⟨b',hb',hr'⟩ := _root_.Integration9_9.TM2Simulation.iterate_simulation f g R hstep h.steps a a' b h.evals_in_steps hr
  exact ⟨b',⟨⟨⟨h.steps,hb'⟩,h.steps_le_m⟩⟩,hr'⟩

/-- A stack store containing a list on just one typed stack. -/
def singleStack (k : K) (x : List (Γ k)) (j : K) : List (Γ j) :=
  if h : j = k then by rw [h]; exact x else []

@[simp] theorem singleStack_same (k : K) (x : List (Γ k)) :
    _root_.Integration9_9.TM2Simulation.singleStack k x k = x := by simp [_root_.Integration9_9.TM2Simulation.singleStack]







omit [DecidableEq K] [DecidableEq J] in
theorem stacksRel_ext (i : K → J) (e : ∀ k, Γ k ≃ Δ (i k))
    (S : ∀ k, List (Γ k)) (T U : ∀ j, List (Δ j))
    (hT : _root_.Integration9_9.TM2Simulation.StacksRel i e S T) (hU : _root_.Integration9_9.TM2Simulation.StacksRel i e S U)
    (hTf : ∀ j, (∀ k, i k ≠ j) → T j = [])
    (hUf : ∀ j, (∀ k, i k ≠ j) → U j = []) : T = U := by
  classical
  funext j
  by_cases h : ∃ k, i k = j
  · obtain ⟨k,rfl⟩ := h
    exact (hT k).trans (hU k).symm
  · have hn : ∀ k, i k ≠ j := by simpa only [not_exists] using h
    exact (hTf j hn).trans (hUf j hn).symm

/-- A whole configuration embedding, with empty unused stacks. The anchor fixes
the other component of the finite control state throughout one machine phase. -/
def ConfigRel (i : K → J) (e : ∀ k, Γ k ≃ Δ (i k)) (v : _root_.Integration9_9.TM2Simulation.ControlLens σ τ)
    (label : Λ → L) (finish : Option L) (anchor : τ)
    (a : TM2.Cfg Γ Λ σ) (b : TM2.Cfg Δ L τ) : Prop :=
  b.l = a.l.elim finish (fun l => some (label l)) ∧
  b.var = v.write anchor a.var ∧ _root_.Integration9_9.TM2Simulation.StacksRel i e a.stk b.stk ∧
  ∀ j, (∀ k, i k ≠ j) → b.stk j = []

theorem configuration_step (i : K → J) (hi : Injective i)
    (e : ∀ k, Γ k ≃ Δ (i k)) (v : _root_.Integration9_9.TM2Simulation.ControlLens σ τ) (label : Λ → L) (finish : Option L)
    (M : Λ → TM2.Stmt Γ Λ σ) (N : L → TM2.Stmt Δ L τ)
    (hN : ∀ l, N (label l) = _root_.Integration9_9.TM2Simulation.statement i e v label finish (M l)) (anchor : τ)
    (a a' : TM2.Cfg Γ Λ σ) (b : TM2.Cfg Δ L τ)
    (ha : TM2.step M a = some a') (hr : _root_.Integration9_9.TM2Simulation.ConfigRel i e v label finish anchor a b) :
    ∃ b', TM2.step N b = some b' ∧ _root_.Integration9_9.TM2Simulation.ConfigRel i e v label finish anchor a' b' := by
  rcases a with ⟨al,av,as⟩
  cases al with
  | none => simp [TM2.step] at ha
  | some l =>
    simp only [TM2.step, Option.some.injEq] at ha
    subst a'
    obtain ⟨hl,hv,hs,hfr⟩ := hr
    have hb : b.l = some (label l) := hl
    have hvread : v.read b.var = av := by rw [hv,v.read_write]
    obtain ⟨hl',hv',hs',hfr'⟩ := _root_.Integration9_9.TM2Simulation.stepAux_simulation i hi e v label finish
      (M l) av b.var as b.stk hvread hs
    refine ⟨TM2.stepAux (_root_.Integration9_9.TM2Simulation.statement i e v label finish (M l)) b.var b.stk,?_,
      hl',?_,hs',?_⟩
    · cases b with
      | mk bl bv bs =>
        change bl = some (label l) at hb
        subst bl
        simp only [TM2.step,hN]
    · rw [hv',hv,v.write_write]
    · intro j hj
      exact (hfr' j hj).trans (hfr j hj)

theorem bounded_configuration_simulation (i : K → J) (hi : Injective i)
    (e : ∀ k, Γ k ≃ Δ (i k)) (v : _root_.Integration9_9.TM2Simulation.ControlLens σ τ) (label : Λ → L) (finish : Option L)
    (M : Λ → TM2.Stmt Γ Λ σ) (N : L → TM2.Stmt Δ L τ)
    (hN : ∀ l, N (label l) = _root_.Integration9_9.TM2Simulation.statement i e v label finish (M l)) (anchor : τ)
    (a a' : TM2.Cfg Γ Λ σ) (b : TM2.Cfg Δ L τ) (m : ℕ)
    (h : EvalsToInTime (TM2.step M) a (some a') m)
    (hr : _root_.Integration9_9.TM2Simulation.ConfigRel i e v label finish anchor a b) :
    ∃ b', Nonempty (EvalsToInTime (TM2.step N) b (some b') m) ∧
      _root_.Integration9_9.TM2Simulation.ConfigRel i e v label finish anchor a' b' :=
  _root_.Integration9_9.TM2Simulation.evalsToInTime_simulation (TM2.step M) (TM2.step N)
    (_root_.Integration9_9.TM2Simulation.ConfigRel i e v label finish anchor)
    (_root_.Integration9_9.TM2Simulation.configuration_step i hi e v label finish M N hN anchor) a a' b m h hr

end Integration9_9.TM2Simulation

end

section








































set_option autoImplicit false
set_option maxHeartbeats 2000000
set_option backward.isDefEq.respectTransparency false

/-! Sequential composition of actual finite TM2 machines by sharing the
first output stack and second input stack. No whole-list primitive is used. -/
namespace Integration9_9.TM2Composition
open Turing StateTransition Function TM2Simulation

local instance stackFinite (tm : FinTM2) : Fintype tm.K := tm.kFin
local instance labelFinite (tm : FinTM2) : Fintype tm.Λ := tm.ΛFin
local instance stateFinite (tm : FinTM2) : Fintype tm.σ := tm.σFin
local instance inputFinite (tm : FinTM2) : Fintype (tm.Γ tm.k₀) := tm.Γk₀Fin

abbrev Extra (b : FinTM2) := {k : b.K // k ≠ b.k₀}
abbrev Stack (a b : FinTM2) := a.K ⊕ _root_.Integration9_9.TM2Composition.Extra b

def Alphabet (a b : FinTM2) : _root_.Integration9_9.TM2Composition.Stack a b → Type
  | .inl k => a.Γ k
  | .inr k => b.Γ k.val

def leftStack (a b : FinTM2) (k : a.K) : _root_.Integration9_9.TM2Composition.Stack a b := .inl k

def leftSymbol (a b : FinTM2) (k : a.K) : a.Γ k ≃ _root_.Integration9_9.TM2Composition.Alphabet a b (_root_.Integration9_9.TM2Composition.leftStack a b k) :=
  Equiv.refl _

@[simp] theorem leftSymbol_map (a b : FinTM2) (k : a.K) (x : List (a.Γ k)) :
    x.map (_root_.Integration9_9.TM2Composition.leftSymbol a b k) = x := by
  change x.map id = x
  exact List.map_id x

def rightStack (a b : FinTM2) (k : b.K) : _root_.Integration9_9.TM2Composition.Stack a b :=
  if h : k = b.k₀ then .inl a.k₁ else .inr ⟨k,h⟩

@[simp] theorem rightStack_input (a b : FinTM2) : _root_.Integration9_9.TM2Composition.rightStack a b b.k₀ = .inl a.k₁ := by
  simp [_root_.Integration9_9.TM2Composition.rightStack]

theorem rightStack_injective (a b : FinTM2) : Injective (_root_.Integration9_9.TM2Composition.rightStack a b) := by
  intro k j h
  by_cases hk : k = b.k₀ <;> by_cases hj : j = b.k₀
  · exact hk.trans hj.symm
  · simp [_root_.Integration9_9.TM2Composition.rightStack,hk,hj] at h
  · simp [_root_.Integration9_9.TM2Composition.rightStack,hk,hj] at h
  · simpa [_root_.Integration9_9.TM2Composition.rightStack,hk,hj,Subtype.ext_iff] using h

/-- The sole alphabet conversion occurs at the shared interface stack. -/
def rightSymbol (a b : FinTM2) (e : b.Γ b.k₀ ≃ a.Γ a.k₁) (k : b.K) :
    b.Γ k ≃ _root_.Integration9_9.TM2Composition.Alphabet a b (_root_.Integration9_9.TM2Composition.rightStack a b k) := by
  unfold _root_.Integration9_9.TM2Composition.rightStack
  split_ifs with hk
  · subst k; exact e
  · exact Equiv.refl _

/-- The first halt is a jump to the second program. Its stack is already the
second program's input stack, so this introduces no copying loop. -/
def program (a b : FinTM2) (e : b.Γ b.k₀ ≃ a.Γ a.k₁) :
    a.Λ ⊕ b.Λ → TM2.Stmt (_root_.Integration9_9.TM2Composition.Alphabet a b) (a.Λ ⊕ b.Λ) (a.σ × b.σ)
  | .inl l => _root_.Integration9_9.TM2Simulation.statement (_root_.Integration9_9.TM2Composition.leftStack a b) (_root_.Integration9_9.TM2Composition.leftSymbol a b) _root_.Integration9_9.TM2Simulation.ControlLens.fst
      Sum.inl (some (.inr b.main)) (a.m l)
  | .inr l => _root_.Integration9_9.TM2Simulation.statement (_root_.Integration9_9.TM2Composition.rightStack a b) (_root_.Integration9_9.TM2Composition.rightSymbol a b e) _root_.Integration9_9.TM2Simulation.ControlLens.snd
      Sum.inr none (b.m l)

def machine (a b : FinTM2) (e : b.Γ b.k₀ ≃ a.Γ a.k₁) : FinTM2 where
  K := _root_.Integration9_9.TM2Composition.Stack a b
  k₀ := .inl a.k₀
  k₁ := _root_.Integration9_9.TM2Composition.rightStack a b b.k₁
  Γ := _root_.Integration9_9.TM2Composition.Alphabet a b
  Λ := a.Λ ⊕ b.Λ
  main := .inl a.main
  σ := a.σ × b.σ
  initialState := (a.initialState,b.initialState)
  m := _root_.Integration9_9.TM2Composition.program a b e
  Γk₀Fin := a.Γk₀Fin

/-- An exact canonical configuration, with a single nonempty stack. -/
def canonical (a b : FinTM2) (l : Option (a.Λ ⊕ b.Λ)) (s : a.σ × b.σ)
    (k : _root_.Integration9_9.TM2Composition.Stack a b) (x : List (_root_.Integration9_9.TM2Composition.Alphabet a b k)) :
    TM2.Cfg (_root_.Integration9_9.TM2Composition.Alphabet a b) (a.Λ ⊕ b.Λ) (a.σ × b.σ) :=
  ⟨l,s,_root_.Integration9_9.TM2Simulation.singleStack k x⟩

/-- A configuration relation uniquely specifies canonical stack contents. -/
theorem configRel_unique {K J Λ L σ τ : Type} {Γ : K → Type} {Δ : J → Type}
    [DecidableEq K] [DecidableEq J] (i : K → J) (e : ∀ k, Γ k ≃ Δ (i k))
    (v : _root_.Integration9_9.TM2Simulation.ControlLens σ τ) (label : Λ → L) (finish : Option L) (anchor : τ)
    (a : TM2.Cfg Γ Λ σ) (b c : TM2.Cfg Δ L τ)
    (hb : _root_.Integration9_9.TM2Simulation.ConfigRel i e v label finish anchor a b)
    (hc : _root_.Integration9_9.TM2Simulation.ConfigRel i e v label finish anchor a c) : b = c := by
  obtain ⟨hbl,hbv,hbs,hbf⟩ := hb
  obtain ⟨hcl,hcv,hcs,hcf⟩ := hc
  have hl := hbl.trans hcl.symm
  have hv := hbv.trans hcv.symm
  have hs := _root_.Integration9_9.TM2Simulation.stacksRel_ext i e a.stk b.stk c.stk hbs hcs hbf hcf
  cases b; cases c
  simp only [TM2.Cfg.mk.injEq] at *
  exact ⟨hl,hv,hs⟩





theorem left_initial (a b : FinTM2) (e : b.Γ b.k₀ ≃ a.Γ a.k₁)
    (x : List (a.Γ a.k₀)) :
    _root_.Integration9_9.TM2Simulation.ConfigRel (_root_.Integration9_9.TM2Composition.leftStack a b) (_root_.Integration9_9.TM2Composition.leftSymbol a b) _root_.Integration9_9.TM2Simulation.ControlLens.fst Sum.inl
      (some (.inr b.main)) (a.initialState,b.initialState)
      (initList a x) (initList (_root_.Integration9_9.TM2Composition.machine a b e) x) := by
  refine ⟨rfl,rfl,?_,?_⟩
  · intro k
    by_cases hk : k = a.k₀
    · subst k; simp [initList,_root_.Integration9_9.TM2Composition.machine,_root_.Integration9_9.TM2Composition.leftStack,_root_.Integration9_9.TM2Composition.leftSymbol,_root_.Integration9_9.TM2Composition.Alphabet]
    · simp [initList,_root_.Integration9_9.TM2Composition.machine,_root_.Integration9_9.TM2Composition.leftStack,_root_.Integration9_9.TM2Composition.leftSymbol,_root_.Integration9_9.TM2Composition.Alphabet,hk]
  · intro j hj
    cases j with
    | inl k => exact (hj k rfl).elim
    | inr k => simp [initList,_root_.Integration9_9.TM2Composition.machine]

/-- Interface between the two phases, expressed in the first output alphabet. -/
def middle (a b : FinTM2) (x : List (a.Γ a.k₁)) :
    TM2.Cfg (_root_.Integration9_9.TM2Composition.Alphabet a b) (a.Λ ⊕ b.Λ) (a.σ × b.σ) :=
  _root_.Integration9_9.TM2Composition.canonical a b (some (.inr b.main)) (a.initialState,b.initialState) (.inl a.k₁) x

@[simp] theorem middle_stacks (a b : FinTM2) (x : List (a.Γ a.k₁)) :
    (_root_.Integration9_9.TM2Composition.middle a b x).stk = _root_.Integration9_9.TM2Simulation.singleStack (.inl a.k₁) x := rfl

theorem left_terminal (a b : FinTM2) (x : List (a.Γ a.k₁)) :
    _root_.Integration9_9.TM2Simulation.ConfigRel (_root_.Integration9_9.TM2Composition.leftStack a b) (_root_.Integration9_9.TM2Composition.leftSymbol a b) _root_.Integration9_9.TM2Simulation.ControlLens.fst Sum.inl
      (some (.inr b.main)) (a.initialState,b.initialState)
      (haltList a x) (_root_.Integration9_9.TM2Composition.middle a b x) := by
  refine ⟨rfl,rfl,?_,?_⟩
  · intro k
    by_cases hk : k = a.k₁
    · subst k; simp [haltList,_root_.Integration9_9.TM2Composition.middle,_root_.Integration9_9.TM2Composition.canonical,_root_.Integration9_9.TM2Simulation.singleStack,_root_.Integration9_9.TM2Composition.leftStack,_root_.Integration9_9.TM2Composition.leftSymbol,_root_.Integration9_9.TM2Composition.Alphabet]
    · simp [haltList,_root_.Integration9_9.TM2Composition.middle,_root_.Integration9_9.TM2Composition.canonical,_root_.Integration9_9.TM2Simulation.singleStack,_root_.Integration9_9.TM2Composition.leftStack,_root_.Integration9_9.TM2Composition.leftSymbol,_root_.Integration9_9.TM2Composition.Alphabet,hk]
  · intro j hj
    cases j with
    | inl k => exact (hj k rfl).elim
    | inr k => simp [_root_.Integration9_9.TM2Composition.middle,_root_.Integration9_9.TM2Composition.canonical,_root_.Integration9_9.TM2Simulation.singleStack]

 theorem _root_.Integration9_9.TM2Composition.map_cast_equiv {A B C : Type} (h : B = C) (e : A ≃ B) (x : List A) :
    cast (congrArg List h) (x.map e) =
      x.map (fun a => (cast (congrArg (fun D => A ≃ D) h) e : A ≃ C) a) := by
  subst C
  rfl

theorem right_initial (a b : FinTM2) (e : b.Γ b.k₀ ≃ a.Γ a.k₁)
    (x : List (b.Γ b.k₀)) :
    _root_.Integration9_9.TM2Simulation.ConfigRel (_root_.Integration9_9.TM2Composition.rightStack a b) (_root_.Integration9_9.TM2Composition.rightSymbol a b e) _root_.Integration9_9.TM2Simulation.ControlLens.snd Sum.inr
      none (a.initialState,b.initialState)
      (initList b x) (_root_.Integration9_9.TM2Composition.middle a b (x.map e)) := by
  refine ⟨rfl,rfl,?_,?_⟩
  · intro k
    by_cases hk : k = b.k₀
    · subst k
      simp only [initList,_root_.Integration9_9.TM2Composition.middle,_root_.Integration9_9.TM2Composition.canonical,_root_.Integration9_9.TM2Simulation.singleStack,_root_.Integration9_9.TM2Composition.rightSymbol,_root_.Integration9_9.TM2Composition.rightStack,_root_.Integration9_9.TM2Composition.Alphabet,
        dite_true,eq_self]
      exact _root_.Integration9_9.TM2Composition.map_cast_equiv (by simp) e x
    · simp [initList,_root_.Integration9_9.TM2Composition.middle,_root_.Integration9_9.TM2Composition.canonical,_root_.Integration9_9.TM2Simulation.singleStack,_root_.Integration9_9.TM2Composition.rightSymbol,_root_.Integration9_9.TM2Composition.rightStack,_root_.Integration9_9.TM2Composition.Alphabet,hk]
  · intro j hj
    cases j with
    | inl k =>
      by_cases hk : k = a.k₁
      · subst k
        exact (hj b.k₀ (_root_.Integration9_9.TM2Composition.rightStack_input a b)).elim
      · simp [_root_.Integration9_9.TM2Composition.middle,_root_.Integration9_9.TM2Composition.canonical,_root_.Integration9_9.TM2Simulation.singleStack,hk]
    | inr k =>
      exact (hj k.val (by simp [_root_.Integration9_9.TM2Composition.rightStack,k.property])).elim

theorem right_terminal (a b : FinTM2) (e : b.Γ b.k₀ ≃ a.Γ a.k₁)
    (x : List (b.Γ b.k₁)) :
    _root_.Integration9_9.TM2Simulation.ConfigRel (_root_.Integration9_9.TM2Composition.rightStack a b) (_root_.Integration9_9.TM2Composition.rightSymbol a b e) _root_.Integration9_9.TM2Simulation.ControlLens.snd Sum.inr
      none (a.initialState,b.initialState)
      (haltList b x) (haltList (_root_.Integration9_9.TM2Composition.machine a b e) (x.map (_root_.Integration9_9.TM2Composition.rightSymbol a b e b.k₁))) := by
  refine ⟨rfl,rfl,?_,?_⟩
  · intro k
    by_cases hk : k = b.k₁
    · subst k; simp [haltList,_root_.Integration9_9.TM2Composition.machine]
    · have hi : _root_.Integration9_9.TM2Composition.rightStack a b k ≠ _root_.Integration9_9.TM2Composition.rightStack a b b.k₁ :=
        fun h => hk (_root_.Integration9_9.TM2Composition.rightStack_injective a b h)
      simp [haltList,_root_.Integration9_9.TM2Composition.machine,hk,hi]
  · intro j hj
    have hi := Ne.symm (hj b.k₁)
    simp [haltList,_root_.Integration9_9.TM2Composition.machine,hi]

/-- Exact sequential running time for the shared-stack composite. The two
interface words coincide after the fixed letterwise alphabet conversion. -/
theorem outputsInTime (a b : FinTM2) (e : b.Γ b.k₀ ≃ a.Γ a.k₁)
    (input : List (a.Γ a.k₀)) (interface : List (b.Γ b.k₀))
    (output : List (b.Γ b.k₁)) (m n : ℕ)
    (ha : TM2OutputsInTime a input (some (interface.map e)) m)
    (hb : TM2OutputsInTime b interface (some output) n) :
    Nonempty (TM2OutputsInTime (_root_.Integration9_9.TM2Composition.machine a b e) input
      (some (output.map (_root_.Integration9_9.TM2Composition.rightSymbol a b e b.k₁))) (n+m)) := by
  obtain ⟨c,⟨hc⟩,hr⟩ := _root_.Integration9_9.TM2Simulation.bounded_configuration_simulation
    (_root_.Integration9_9.TM2Composition.leftStack a b) Sum.inl_injective (_root_.Integration9_9.TM2Composition.leftSymbol a b) _root_.Integration9_9.TM2Simulation.ControlLens.fst Sum.inl
    (some (.inr b.main)) a.m (_root_.Integration9_9.TM2Composition.program a b e) (fun _ => rfl)
    (a.initialState,b.initialState) (initList a input) (haltList a (interface.map e))
    (initList (_root_.Integration9_9.TM2Composition.machine a b e) input) m ha (_root_.Integration9_9.TM2Composition.left_initial a b e input)
  have heq : c = _root_.Integration9_9.TM2Composition.middle a b (interface.map e) :=
    _root_.Integration9_9.TM2Composition.configRel_unique _ _ _ _ _ _ _ _ _ hr (_root_.Integration9_9.TM2Composition.left_terminal a b (interface.map e))
  subst c
  obtain ⟨d,⟨hd⟩,hr'⟩ := _root_.Integration9_9.TM2Simulation.bounded_configuration_simulation
    (_root_.Integration9_9.TM2Composition.rightStack a b) (_root_.Integration9_9.TM2Composition.rightStack_injective a b) (_root_.Integration9_9.TM2Composition.rightSymbol a b e) _root_.Integration9_9.TM2Simulation.ControlLens.snd Sum.inr
    none b.m (_root_.Integration9_9.TM2Composition.program a b e) (fun _ => rfl)
    (a.initialState,b.initialState) (initList b interface) (haltList b output)
    (_root_.Integration9_9.TM2Composition.middle a b (interface.map e)) n hb (_root_.Integration9_9.TM2Composition.right_initial a b e interface)
  have heq : d = haltList (_root_.Integration9_9.TM2Composition.machine a b e) (output.map (_root_.Integration9_9.TM2Composition.rightSymbol a b e b.k₁)) :=
    _root_.Integration9_9.TM2Composition.configRel_unique _ _ _ _ _ _ _ _ _ hr' (_root_.Integration9_9.TM2Composition.right_terminal a b e output)
  subst d
  exact ⟨EvalsToInTime.trans (_root_.Integration9_9.TM2Composition.machine a b e).step m n _ _ _ hc hd⟩

/-- Evaluation of a natural-coefficient time polynomial is monotone. -/
theorem polynomial_eval_monotone (p : Polynomial ℕ) : Monotone p.eval := by
  intro x y hxy
  simp only [Polynomial.eval_eq_sum,Polynomial.sum_def]
  apply Finset.sum_le_sum
  intro i hi
  exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_left hxy i)

/-- A concrete witness for closure of Mathlib's exact TM2 polynomial-time model
under composition. The machine uses a shared typed interface stack. -/
noncomputable def computableInPolyTime
    {α β γ αΓ βΓ γΓ : Type} {ea : α → List αΓ} {eb : β → List βΓ}
    {ec : γ → List γΓ} {f : α → β} {g : β → γ}
    (h1 : TM2ComputableInPolyTime ea eb f) (h2 : TM2ComputableInPolyTime eb ec g) :
    TM2ComputableInPolyTime ea ec (g ∘ f) := by
  let e : h2.tm.Γ h2.tm.k₀ ≃ h1.tm.Γ h1.tm.k₁ :=
    h2.inputAlphabet.trans h1.outputAlphabet.symm
  let size : Polynomial ℕ := Polynomial.X +
    Polynomial.C (_root_.Integration9_9.TM2OutputLength.programPushBound h1.tm.m) * h1.time
  refine {
    tm := _root_.Integration9_9.TM2Composition.machine h1.tm h2.tm e
    inputAlphabet := h1.inputAlphabet
    outputAlphabet := (_root_.Integration9_9.TM2Composition.rightSymbol h1.tm h2.tm e h2.tm.k₁).symm.trans h2.outputAlphabet
    time := h1.time + h2.time.comp size
    outputsFun := ?_
  }
  intro x
  have hconnect : ((eb (f x)).map h2.inputAlphabet.invFun).map e =
      (eb (f x)).map h1.outputAlphabet.invFun := by
    simp [e,List.map_map,Equiv.coe_trans,Function.comp_def]
  have hrun := _root_.Integration9_9.TM2Composition.outputsInTime h1.tm h2.tm e
    ((ea x).map h1.inputAlphabet.invFun) ((eb (f x)).map h2.inputAlphabet.invFun)
    ((ec (g (f x))).map h2.outputAlphabet.invFun)
    (h1.time.eval (ea x).length) (h2.time.eval (eb (f x)).length)
    (by simpa only [hconnect] using h1.outputsFun x) (h2.outputsFun (f x))
  have hlen := _root_.Integration9_9.TM2OutputLength.output_length_le h1.tm
    ((ea x).map h1.inputAlphabet.invFun) ((eb (f x)).map h1.outputAlphabet.invFun)
    (h1.time.eval (ea x).length) (h1.outputsFun x)
  simp only [List.length_map] at hlen
  have hsize : (eb (f x)).length ≤ size.eval (ea x).length := by
    simpa only [size,Polynomial.eval_add,Polynomial.eval_X,Polynomial.eval_mul,
      Polynomial.eval_C] using hlen
  have htime : h2.time.eval (eb (f x)).length + h1.time.eval (ea x).length ≤
      (h1.time + h2.time.comp size).eval (ea x).length := by
    rw [Polynomial.eval_add,Polynomial.eval_comp]
    exact (Nat.add_le_add_right (_root_.Integration9_9.TM2Composition.polynomial_eval_monotone h2.time hsize) _).trans_eq
      (Nat.add_comm _ _)
  let run := Classical.choice hrun
  have result : TM2OutputsInTime (_root_.Integration9_9.TM2Composition.machine h1.tm h2.tm e)
      ((ea x).map h1.inputAlphabet.invFun)
      (some (((ec (g (f x))).map h2.outputAlphabet.invFun).map
        (_root_.Integration9_9.TM2Composition.rightSymbol h1.tm h2.tm e h2.tm.k₁)))
      ((h1.time+h2.time.comp size).eval (ea x).length) :=
    ⟨run.toEvalsTo,run.steps_le_m.trans htime⟩
  convert result using 1
  simp only [List.map_map,Equiv.coe_trans,Equiv.invFun_as_coe,
      Equiv.symm_trans,Equiv.symm_symm,Function.comp_def,Function.comp_apply]
  rfl

/-- The missing Mathlib closure theorem, proved without importing its
`proof_wanted` declaration or treating a reported work count as execution time. -/
theorem comp
    {α β γ αΓ βΓ γΓ : Type} {ea : α → List αΓ} {eb : β → List βΓ}
    {ec : γ → List γΓ} {f : α → β} {g : β → γ}
    (h1 : TM2ComputableInPolyTime ea eb f) (h2 : TM2ComputableInPolyTime eb ec g) :
    Nonempty (TM2ComputableInPolyTime ea ec (g ∘ f)) :=
  ⟨_root_.Integration9_9.TM2Composition.computableInPolyTime h1 h2⟩

end Integration9_9.TM2Composition

end

section








































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.Result
end AFT.BinaryArithmetic.Result
namespace AFT.BinaryArithmetic.Result.mk
end AFT.BinaryArithmetic.Result.mk
namespace AFT.BinaryArithmetic.add
end AFT.BinaryArithmetic.add
namespace AFT.BinaryArithmetic.add.match_1
end AFT.BinaryArithmetic.add.match_1
namespace AFT.BinaryArithmetic.multiply
end AFT.BinaryArithmetic.multiply
namespace AFT.BinaryArithmetic.multiply.match_1
end AFT.BinaryArithmetic.multiply.match_1
namespace AFT.BinaryArithmetic.value
end AFT.BinaryArithmetic.value
namespace AFT.BinaryArithmetic.value.match_1
end AFT.BinaryArithmetic.value.match_1



set_option autoImplicit false
set_option maxHeartbeats 1800000
set_option backward.isDefEq.respectTransparency false

/-! A literal finite TM2 ripple adder for the actual Boolean-list arithmetic.
The input consists of paired little-endian digits, with false padding on the
shorter operand. Each transition reads one pair and stores one sum bit. -/
namespace Integration9_9.TM2BinaryAddition
open Turing StateTransition Function
open AFT.BinaryArithmetic

abbrev Digit := Bool × Bool

inductive Stack | input | buffer | output
  deriving DecidableEq, Fintype



















def padPairs : List Bool → List Bool → List _root_.Integration9_9.TM2BinaryAddition.Digit
  | [],[] => []
  | a::as,[] => (a,false)::padPairs as []
  | [],b::bs => (false,b)::padPairs [] bs
  | a::as,b::bs => (a,b)::padPairs as bs



























end Integration9_9.TM2BinaryAddition

end

section











































set_option autoImplicit false
set_option maxHeartbeats 2400000
set_option backward.isDefEq.respectTransparency false

namespace Integration9_9.TM2AdditionFrontend
open Turing StateTransition Function
open TM2BinaryAddition (Digit padPairs)

/-- Self-delimiting raw digit lists; unlike `Nat.bits`, high zero bits are retained. -/
def marked (xs : List Bool) : List Bool := xs.flatMap (fun b => [true,b]) ++ [false]


/-- Total parsing also assigns deterministic outputs to malformed strings. -/
def readDigits : List Bool → List Bool × List Bool
  | true::bit::tail => let r := readDigits tail; (bit::r.1,r.2)
  | false::tail => ([],tail)
  | _ => ([],[])





inductive Stack | input | leftRev | rightRev | left | right | pairRev | output
  deriving DecidableEq, Fintype

def Alphabet : _root_.Integration9_9.TM2AdditionFrontend.Stack → Type
  | .pairRev | .output => _root_.Integration9_9.TM2BinaryAddition.Digit
  | _ => Bool

inductive Label | read (side : Bool) | drain | revLeft | revRight | zip | revOut
  deriving DecidableEq, Fintype

abbrev Control := Option Bool × Option Bool × Option _root_.Integration9_9.TM2BinaryAddition.Digit
def initial : _root_.Integration9_9.TM2AdditionFrontend.Control := (none,none,none)

structure Data where
  input : List Bool := []
  leftRev : List Bool := []
  rightRev : List Bool := []
  left : List Bool := []
  right : List Bool := []
  pairRev : List _root_.Integration9_9.TM2BinaryAddition.Digit := []
  output : List _root_.Integration9_9.TM2BinaryAddition.Digit := []









/-- Read a marker and the following bit using two genuine stack pops. -/
def readInto (target : _root_.Integration9_9.TM2AdditionFrontend.Stack) (put : Bool → _root_.Integration9_9.TM2AdditionFrontend.Alphabet target) (again next : _root_.Integration9_9.TM2AdditionFrontend.Label) :
    TM2.Stmt _root_.Integration9_9.TM2AdditionFrontend.Alphabet _root_.Integration9_9.TM2AdditionFrontend.Label _root_.Integration9_9.TM2AdditionFrontend.Control :=
  .pop .input (fun _ bit => (bit,none,none))
    (.branch (fun s => s.1.getD false)
      (.pop .input (fun _ bit => (bit,none,none))
        (.branch (fun s => s.1.isSome)
          (.push target (fun s => put (s.1.getD false))
            (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => again))))
          (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => next)))))
      (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => next))))

def reverseBool (source target : _root_.Integration9_9.TM2AdditionFrontend.Stack) (get : _root_.Integration9_9.TM2AdditionFrontend.Alphabet source → Bool)
    (put : Bool → _root_.Integration9_9.TM2AdditionFrontend.Alphabet target) (again next : _root_.Integration9_9.TM2AdditionFrontend.Label) : TM2.Stmt _root_.Integration9_9.TM2AdditionFrontend.Alphabet _root_.Integration9_9.TM2AdditionFrontend.Label _root_.Integration9_9.TM2AdditionFrontend.Control :=
  .pop source (fun _ bit => (bit.map get,none,none))
    (.branch (fun s => s.1.isSome)
      (.push target (fun s => put (s.1.getD false))
        (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => again))))
      (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => next))))

def program : _root_.Integration9_9.TM2AdditionFrontend.Label → TM2.Stmt _root_.Integration9_9.TM2AdditionFrontend.Alphabet _root_.Integration9_9.TM2AdditionFrontend.Label _root_.Integration9_9.TM2AdditionFrontend.Control
  | .read false => _root_.Integration9_9.TM2AdditionFrontend.readInto .leftRev id (.read false) (.read true)
  | .read true => _root_.Integration9_9.TM2AdditionFrontend.readInto .rightRev id (.read true) .drain
  | .drain => .pop .input (fun _ bit => (bit,none,none))
      (.branch (fun s => s.1.isSome)
        (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => .drain)))
        (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => .revLeft))))
  | .revLeft => _root_.Integration9_9.TM2AdditionFrontend.reverseBool .leftRev .left id id .revLeft .revRight
  | .revRight => _root_.Integration9_9.TM2AdditionFrontend.reverseBool .rightRev .right id id .revRight .zip
  | .zip => .pop .left (fun _ bit => (bit,none,none))
      (.pop .right (fun s bit => (s.1,bit,none))
        (.branch (fun s => s.1.isSome || s.2.1.isSome)
          (.push .pairRev (fun s => (s.1.getD false,s.2.1.getD false))
            (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => .zip))))
          (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => .revOut)))))
  | .revOut => .pop .pairRev (fun _ digit => (none,none,digit))
      (.branch (fun s => s.2.2.isSome)
        (.push .output (fun s => s.2.2.getD (false,false))
          (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) (.goto (fun _ => .revOut))))
        (.load (fun _ => _root_.Integration9_9.TM2AdditionFrontend.initial) .halt))

def machine : FinTM2 where
  K := _root_.Integration9_9.TM2AdditionFrontend.Stack
  k₀ := .input
  k₁ := .output
  Γ := _root_.Integration9_9.TM2AdditionFrontend.Alphabet
  Λ := _root_.Integration9_9.TM2AdditionFrontend.Label
  main := .read false
  σ := _root_.Integration9_9.TM2AdditionFrontend.Control
  initialState := _root_.Integration9_9.TM2AdditionFrontend.initial
  m := _root_.Integration9_9.TM2AdditionFrontend.program
  Γk₀Fin := inferInstanceAs (Fintype Bool)































abbrev tick (q : Option _root_.Integration9_9.TM2AdditionFrontend.machine.Cfg) : Option _root_.Integration9_9.TM2AdditionFrontend.machine.Cfg := q.bind _root_.Integration9_9.TM2AdditionFrontend.machine.step

@[simp] theorem tick_some (q : _root_.Integration9_9.TM2AdditionFrontend.machine.Cfg) : _root_.Integration9_9.TM2AdditionFrontend.tick (some q) = _root_.Integration9_9.TM2AdditionFrontend.machine.step q := rfl



















/-- Canonical digit codes coincide exactly with the platform's natural-number code. -/
theorem marked_natBits (n : ℕ) : _root_.Integration9_9.TM2AdditionFrontend.marked (Nat.bits n) = InfoLossLAR.WorstCase.encB n := rfl



























end Integration9_9.TM2AdditionFrontend

end

section









































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.Difference
end AFT.BinaryArithmetic.Difference
namespace AFT.BinaryArithmetic.Difference.mk
end AFT.BinaryArithmetic.Difference.mk
namespace AFT.BinaryArithmetic.subtract
end AFT.BinaryArithmetic.subtract
namespace AFT.BinaryArithmetic.subtract.match_1
end AFT.BinaryArithmetic.subtract.match_1



set_option autoImplicit false
set_option maxHeartbeats 3200000
set_option backward.isDefEq.respectTransparency false

namespace Integration9_9.TM2PairUnframe
open Turing StateTransition Function
open AFT.BinaryArithmetic
open TM2AdditionFrontend (marked readDigits)




inductive Stack | input | counter | rev | outRev | output
  deriving DecidableEq, Fintype
inductive Label
  | read (side : Bool) | revPrefix (side : Bool) | check (side : Bool)
  | dec (side : Bool) | revDec (side : Bool) | consume (side : Bool)
  | clearCounter (side : Bool) | clearRev (side : Bool) | finish (side : Bool)
  | drain | revOut
  deriving DecidableEq, Fintype
abbrev Control := Bool × Option Bool
def initial : _root_.Integration9_9.TM2PairUnframe.Control := (true,none)

structure Data where
  input : List Bool := []
  counter : List Bool := []
  rev : List Bool := []
  outRev : List Bool := []
  output : List Bool := []



def next (side : Bool) : _root_.Integration9_9.TM2PairUnframe.Label := if side then .drain else .read true

def reverseLoop (src dst : _root_.Integration9_9.TM2PairUnframe.Stack) (again next : _root_.Integration9_9.TM2PairUnframe.Label) :
    TM2.Stmt (fun _ : _root_.Integration9_9.TM2PairUnframe.Stack => Bool) _root_.Integration9_9.TM2PairUnframe.Label _root_.Integration9_9.TM2PairUnframe.Control :=
  .pop src (fun _ b => (true,b))
    (.branch (fun s => s.2.isSome)
      (.push dst (fun s => s.2.getD false)
        (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => again))))
      (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => next))))

def clearLoop (src : _root_.Integration9_9.TM2PairUnframe.Stack) (again next : _root_.Integration9_9.TM2PairUnframe.Label) :
    TM2.Stmt (fun _ : _root_.Integration9_9.TM2PairUnframe.Stack => Bool) _root_.Integration9_9.TM2PairUnframe.Label _root_.Integration9_9.TM2PairUnframe.Control :=
  .pop src (fun _ b => (true,b))
    (.branch (fun s => s.2.isSome)
      (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => again)))
      (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => next))))

def program : _root_.Integration9_9.TM2PairUnframe.Label → TM2.Stmt (fun _ : _root_.Integration9_9.TM2PairUnframe.Stack => Bool) _root_.Integration9_9.TM2PairUnframe.Label _root_.Integration9_9.TM2PairUnframe.Control
  | .read side => .pop .input (fun _ b => (true,b))
      (.branch (fun s => s.2.getD false)
        (.pop .input (fun _ b => (true,b))
          (.branch (fun s => s.2.isSome)
            (.push .rev (fun s => s.2.getD false)
              (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .read side))))
            (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .revPrefix side)))))
        (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .revPrefix side))))
  | .revPrefix side => _root_.Integration9_9.TM2PairUnframe.reverseLoop .rev .counter (.revPrefix side) (.check side)
  | .check side => .peek .input (fun _ b => (true,b))
      (.branch (fun s => s.2.isSome)
        (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .dec side)))
        (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .clearCounter side))))
  | .dec side => .pop .counter (fun s b => (s.1,b))
      (.branch (fun s => s.2.isSome)
        (.push .rev (fun s => _root_.AFT.BinaryArithmetic.sumBit (s.2.getD false) false s.1)
          (.load (fun s => (_root_.AFT.BinaryArithmetic.borrowBit (s.2.getD false) false s.1,none))
            (.goto (fun _ => .dec side))))
        (.branch Prod.fst
          (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .clearRev side)))
          (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .revDec side)))))
  | .revDec side => _root_.Integration9_9.TM2PairUnframe.reverseLoop .rev .counter (.revDec side) (.consume side)
  | .consume side => .pop .input (fun _ b => (true,b))
      (.branch (fun s => s.2.isSome)
        (.push .outRev (fun _ => true) (.push .outRev (fun s => s.2.getD false)
          (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .check side)))))
        (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .clearCounter side))))
  | .clearCounter side => _root_.Integration9_9.TM2PairUnframe.clearLoop .counter (.clearCounter side) (.clearRev side)
  | .clearRev side => _root_.Integration9_9.TM2PairUnframe.clearLoop .rev (.clearRev side) (.finish side)
  | .finish side => .push .outRev (fun _ => false)
      (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => _root_.Integration9_9.TM2PairUnframe.next side)))
  | .drain => _root_.Integration9_9.TM2PairUnframe.clearLoop .input .drain .revOut
  | .revOut => .pop .outRev (fun _ b => (true,b))
      (.branch (fun s => s.2.isSome)
        (.push .output (fun s => s.2.getD false)
          (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) (.goto (fun _ => .revOut))))
        (.load (fun _ => _root_.Integration9_9.TM2PairUnframe.initial) .halt))

def machine : FinTM2 where
  K := _root_.Integration9_9.TM2PairUnframe.Stack
  k₀ := .input
  k₁ := .output
  Γ _ := Bool
  Λ := _root_.Integration9_9.TM2PairUnframe.Label
  main := .read false
  σ := _root_.Integration9_9.TM2PairUnframe.Control
  initialState := _root_.Integration9_9.TM2PairUnframe.initial
  m := _root_.Integration9_9.TM2PairUnframe.program



abbrev tick (q : Option _root_.Integration9_9.TM2PairUnframe.machine.Cfg) := q.bind _root_.Integration9_9.TM2PairUnframe.machine.step
@[simp] theorem tick_some (q : _root_.Integration9_9.TM2PairUnframe.machine.Cfg) : _root_.Integration9_9.TM2PairUnframe.tick (some q) = _root_.Integration9_9.TM2PairUnframe.machine.step q := rfl










































































































end Integration9_9.TM2PairUnframe

end

section










































set_option autoImplicit false
set_option maxHeartbeats 2000000
set_option backward.isDefEq.respectTransparency false

/-! The sampler's subtraction-borrow comparisons executed by a literal finite
TM2. Both borrow directions and a zero flag are scanned in one pass. -/
namespace Integration9_9.TM2BinaryComparison
open Turing StateTransition Function
open AFT.BinaryArithmetic
open TM2BinaryAddition (Digit padPairs)

structure Flags where
  forward : Bool
  backward : Bool
  nonzero : Bool
  deriving Fintype, DecidableEq





inductive Stack | input | output
  deriving Fintype, DecidableEq





























def anyBit (xs : List Bool) : Bool := xs.any id





















































end Integration9_9.TM2BinaryComparison

end

section










































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.normalize
end AFT.BinaryArithmetic.normalize
namespace AFT.BinaryArithmetic.normalize.match_1
end AFT.BinaryArithmetic.normalize.match_1




set_option autoImplicit false
set_option maxHeartbeats 1800000
set_option backward.isDefEq.respectTransparency false

namespace Integration9_9.TM2Canonical
open Turing StateTransition Function
open AFT.BinaryArithmetic















inductive Stack | input | buffer | output
  deriving Fintype, DecidableEq
inductive Label | reverse | trim | copy
  deriving Fintype, DecidableEq







def program : _root_.Integration9_9.TM2Canonical.Label → TM2.Stmt (fun _ : _root_.Integration9_9.TM2Canonical.Stack => Bool) _root_.Integration9_9.TM2Canonical.Label (Option Bool)
  | .reverse => .pop .input (fun _ b => b)
      (.branch Option.isSome
        (.push .buffer (fun b => b.getD false) (.load (fun _ => none) (.goto (fun _ => .reverse))))
        (.load (fun _ => none) (.goto (fun _ => .trim))))
  | .trim => .pop .buffer (fun _ b => b)
      (.branch Option.isSome
        (.branch (fun b => b.getD false)
          (.push .output (fun _ => true) (.load (fun _ => none) (.goto (fun _ => .copy))))
          (.load (fun _ => none) (.goto (fun _ => .trim))))
        (.load (fun _ => none) .halt))
  | .copy => .pop .buffer (fun _ b => b)
      (.branch Option.isSome
        (.push .output (fun b => b.getD false) (.load (fun _ => none) (.goto (fun _ => .copy))))
        (.load (fun _ => none) .halt))

def machine : FinTM2 where
  K := _root_.Integration9_9.TM2Canonical.Stack
  k₀ := .input
  k₁ := .output
  Γ _ := Bool
  Λ := _root_.Integration9_9.TM2Canonical.Label
  main := .reverse
  σ := Option Bool
  initialState := none
  m := _root_.Integration9_9.TM2Canonical.program



abbrev tick (q : Option _root_.Integration9_9.TM2Canonical.machine.Cfg) := q.bind _root_.Integration9_9.TM2Canonical.machine.step
@[simp] theorem tick_some (q : _root_.Integration9_9.TM2Canonical.machine.Cfg) : _root_.Integration9_9.TM2Canonical.tick (some q) = _root_.Integration9_9.TM2Canonical.machine.step q := rfl































end Integration9_9.TM2Canonical

end

section











































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.RationalBits
end AFT.BinaryArithmetic.RationalBits
namespace AFT.BinaryArithmetic.RationalBits.decode
end AFT.BinaryArithmetic.RationalBits.decode
namespace AFT.BinaryArithmetic.RationalBits.mk
end AFT.BinaryArithmetic.RationalBits.mk
namespace AFT.BinaryArithmetic.RationalResult
end AFT.BinaryArithmetic.RationalResult
namespace AFT.BinaryArithmetic.RationalResult.mk
end AFT.BinaryArithmetic.RationalResult.mk
namespace AFT.BinaryArithmetic.reduce
end AFT.BinaryArithmetic.reduce
namespace AFT.BinaryArithmetic.signedValue
end AFT.BinaryArithmetic.signedValue




set_option autoImplicit false
set_option maxHeartbeats 2000000
set_option backward.isDefEq.respectTransparency false

/-! Literal Boolean-list encoders for the platform's natural/integer output
format. The sign of a zero magnitude is always encoded as false. -/
namespace Integration9_9.TM2OutputEncoding
open Turing StateTransition Function
open AFT.BinaryArithmetic
open TM2AdditionFrontend (marked)
open TM2BinaryComparison (anyBit)

def addSign (withSign sign nonzero : Bool) (xs : List Bool) : List Bool :=
  if withSign then (sign && nonzero)::xs else xs

def render (withSign sign : Bool) (xs : List Bool) : List Bool :=
  _root_.Integration9_9.TM2OutputEncoding.addSign withSign sign (_root_.Integration9_9.TM2BinaryComparison.anyBit xs) (_root_.Integration9_9.TM2AdditionFrontend.marked xs)

inductive Stack | input | buffer | output
  deriving Fintype, DecidableEq















































end Integration9_9.TM2OutputEncoding

end

section























































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.IntegerBits
end AFT.BinaryArithmetic.IntegerBits
namespace AFT.BinaryArithmetic.IntegerBits.decode
end AFT.BinaryArithmetic.IntegerBits.decode
namespace AFT.BinaryArithmetic.IntegerBits.mk
end AFT.BinaryArithmetic.IntegerBits.mk
namespace AFT.BinaryArithmetic.IntegerBits.multiply
end AFT.BinaryArithmetic.IntegerBits.multiply
namespace AFT.BinaryArithmetic.IntegerBits.subtract
end AFT.BinaryArithmetic.IntegerBits.subtract
namespace AFT.BinaryArithmetic.IntegerBits.sumFin
end AFT.BinaryArithmetic.IntegerBits.sumFin
namespace AFT.BinaryArithmetic.IntegerBits.sumFin.match_1
end AFT.BinaryArithmetic.IntegerBits.sumFin.match_1
namespace AFT.BinaryArithmetic.IntegerBits.zero
end AFT.BinaryArithmetic.IntegerBits.zero



set_option autoImplicit false
set_option maxHeartbeats 2400000
set_option backward.isDefEq.respectTransparency false

/-! Dynamic integer output encoding. Input is a sign bit followed by arbitrary
little-endian magnitude digits. The sign is read by the machine, high zeros
are removed, and negative zero receives the platform's false sign. -/
namespace Integration9_9.TM2DynamicIntegerEncoding
open Turing StateTransition Function
open AFT.BinaryArithmetic
open TM2AdditionFrontend (marked)
open TM2OutputEncoding (render)

inductive Stack | input | buffer | output
  deriving Fintype, DecidableEq
inductive Label | header | reverse | trim | copy
  deriving Fintype, DecidableEq
abbrev Control := Bool × Option Bool

def initial (sign : Bool) : _root_.Integration9_9.TM2DynamicIntegerEncoding.Control := (sign,none)






/-- The optional sign flip is a fixed gate applied to the runtime sign bit.
Canonicalization and rendering each use only individual stack operations. -/
def program (flip : Bool) : _root_.Integration9_9.TM2DynamicIntegerEncoding.Label → TM2.Stmt (fun _ : _root_.Integration9_9.TM2DynamicIntegerEncoding.Stack => Bool) _root_.Integration9_9.TM2DynamicIntegerEncoding.Label _root_.Integration9_9.TM2DynamicIntegerEncoding.Control
  | .header => .pop .input (fun _ b => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial (b.getD false ^^ flip)) (.goto (fun _ => .reverse))
  | .reverse => .pop .input (fun s b => (s.1,b))
      (.branch (fun s => s.2.isSome)
        (.push .buffer (fun s => s.2.getD false)
          (.load (fun s => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial s.1) (.goto (fun _ => .reverse))))
        (.load (fun s => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial s.1) (.goto (fun _ => .trim))))
  | .trim => .pop .buffer (fun s b => (s.1,b))
      (.branch (fun s => s.2.isSome)
        (.branch (fun s => s.2.getD false)
          (.push .output (fun _ => false)
            (.push .output (fun _ => true)
              (.push .output (fun _ => true)
                (.load (fun s => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial s.1) (.goto (fun _ => .copy))))))
          (.load (fun s => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial s.1) (.goto (fun _ => .trim))))
        (.push .output (fun _ => false)
          (.push .output (fun _ => false) (.load (fun _ => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial false) .halt))))
  | .copy => .pop .buffer (fun s b => (s.1,b))
      (.branch (fun s => s.2.isSome)
        (.push .output (fun s => s.2.getD false)
          (.push .output (fun _ => true)
            (.load (fun s => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial s.1) (.goto (fun _ => .copy)))))
        (.push .output Prod.fst (.load (fun _ => _root_.Integration9_9.TM2DynamicIntegerEncoding.initial false) .halt)))

def machine (flip : Bool) : FinTM2 where
  K := _root_.Integration9_9.TM2DynamicIntegerEncoding.Stack
  k₀ := .input
  k₁ := .output
  Γ _ := Bool
  Λ := _root_.Integration9_9.TM2DynamicIntegerEncoding.Label
  main := .header
  σ := _root_.Integration9_9.TM2DynamicIntegerEncoding.Control
  initialState := _root_.Integration9_9.TM2DynamicIntegerEncoding.initial false
  m := _root_.Integration9_9.TM2DynamicIntegerEncoding.program flip



abbrev tick (flip : Bool) (q : Option (_root_.Integration9_9.TM2DynamicIntegerEncoding.machine flip).Cfg) := q.bind (_root_.Integration9_9.TM2DynamicIntegerEncoding.machine flip).step
@[simp] theorem tick_some (flip : Bool) (q : (_root_.Integration9_9.TM2DynamicIntegerEncoding.machine flip).Cfg) :
    _root_.Integration9_9.TM2DynamicIntegerEncoding.tick flip (some q) = (_root_.Integration9_9.TM2DynamicIntegerEncoding.machine flip).step q := rfl









































end Integration9_9.TM2DynamicIntegerEncoding

end

section
























































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.RationalBits
end AFT.BinaryArithmetic.RationalBits
namespace AFT.BinaryArithmetic.RationalBits.chartAllowedTerm
end AFT.BinaryArithmetic.RationalBits.chartAllowedTerm
namespace AFT.BinaryArithmetic.RationalBits.chartConditional
end AFT.BinaryArithmetic.RationalBits.chartConditional
namespace AFT.BinaryArithmetic.RationalBits.chartConditionalTerm
end AFT.BinaryArithmetic.RationalBits.chartConditionalTerm
namespace AFT.BinaryArithmetic.RationalBits.chartExpectation
end AFT.BinaryArithmetic.RationalBits.chartExpectation
namespace AFT.BinaryArithmetic.RationalBits.chartFeature
end AFT.BinaryArithmetic.RationalBits.chartFeature
namespace AFT.BinaryArithmetic.RationalBits.chartProjection
end AFT.BinaryArithmetic.RationalBits.chartProjection
namespace AFT.BinaryArithmetic.RationalBits.chartProjectionTerm
end AFT.BinaryArithmetic.RationalBits.chartProjectionTerm
namespace AFT.BinaryArithmetic.RationalBits.difference
end AFT.BinaryArithmetic.RationalBits.difference
namespace AFT.BinaryArithmetic.RationalBits.factorial
end AFT.BinaryArithmetic.RationalBits.factorial
namespace AFT.BinaryArithmetic.RationalBits.factorialWorkStep
end AFT.BinaryArithmetic.RationalBits.factorialWorkStep
namespace AFT.BinaryArithmetic.RationalBits.half
end AFT.BinaryArithmetic.RationalBits.half
namespace AFT.BinaryArithmetic.RationalBits.ofCounter
end AFT.BinaryArithmetic.RationalBits.ofCounter
namespace AFT.BinaryArithmetic.RationalBits.power
end AFT.BinaryArithmetic.RationalBits.power
namespace AFT.BinaryArithmetic.RationalBits.power.match_1
end AFT.BinaryArithmetic.RationalBits.power.match_1
namespace AFT.BinaryArithmetic.RationalBits.quotient
end AFT.BinaryArithmetic.RationalBits.quotient
namespace AFT.BinaryArithmetic.RationalBits.sumRange
end AFT.BinaryArithmetic.RationalBits.sumRange
namespace AFT.BinaryArithmetic.RationalBits.third
end AFT.BinaryArithmetic.RationalBits.third





set_option autoImplicit false
set_option maxHeartbeats 3000000
set_option maxRecDepth 10000
set_option synthInstance.maxSize 1024
set_option backward.isDefEq.respectTransparency false

/-! Actual reuse of the platform's finite Boolean-machine arithmetic runtime.
Raw native integer/rational fields are explicitly encoded and parsed by verified
programs. Arithmetic certificates use the native operations' proved value laws;
no statement identifies an arbitrary record work field with machine time. -/
namespace Integration9_9.TM2NativeArithmetic
open AFT.BinaryArithmetic
open OAI.MetricKMedian.Binary







theorem word_value (xs : List Bool) : Word.value xs = _root_.AFT.BinaryArithmetic.value xs := by
  induction xs with
  | nil => rfl
  | cons bit tail ih => simp only [Word.value,_root_.AFT.BinaryArithmetic.value,ih]

noncomputable def wordNatural : PolyProgram id Nat.bits _root_.AFT.BinaryArithmetic.value :=
  PolyProgram.toBits.congr _root_.AFT.BinaryArithmetic.value _root_.Integration9_9.TM2NativeArithmetic.word_value































theorem quote_eq_marked (xs : List Bool) : quote xs = _root_.Integration9_9.TM2AdditionFrontend.marked xs := by
  induction xs with
  | nil => rfl
  | cons bit tail ih => simp [quote,_root_.Integration9_9.TM2AdditionFrontend.marked,ih]

/-- Actual library programs translate canonical numeric results to the target
platform output format. These are machine compositions, not encoding axioms. -/
noncomputable def platformNatural : PolyProgram Nat.bits id InfoLossLAR.WorstCase.encB :=
  PolyProgram.quote.recode Nat.bits (fun _ => rfl) (fun n =>
    (_root_.Integration9_9.TM2NativeArithmetic.quote_eq_marked n.bits).trans (_root_.Integration9_9.TM2AdditionFrontend.marked_natBits n))

noncomputable def platformInteger : PolyProgram Signed.encode id InfoLossLAR.WorstCase.encI := by
  let sign : PolyProgram Signed.encode id (fun z => [Signed.sign z]) :=
    PolyProgram.intSign.recode id (fun _ => rfl) (fun _ => rfl)
  let magnitude := PolyProgram.intAbs.comp _root_.Integration9_9.TM2NativeArithmetic.platformNatural
  exact ((sign.fanout magnitude).comp PolyProgram.append).congr _ (fun z => by
    simp only [Function.comp_apply,Signed.sign,InfoLossLAR.WorstCase.encI,List.singleton_append])



/-- AFT's proved reduced-height bounds supply the actual state-encoding length
bounds required by PolyProgram.listFold and PolyProgram.loop. -/
theorem runtimeHeight {q : ℚ} {L : ℕ} (h : _root_.AFT.RationalNumerics.HeightBound q L) : HasHeight q L := by
  constructor
  · rw [← Int.natCast_natAbs]
    exact_mod_cast h.1
  · exact_mod_cast h.2

theorem ratioLength {q : ℚ} {L : ℕ} (h : _root_.AFT.RationalNumerics.HeightBound q L) :
    (Ratio.encode q).length ≤ 3*L+10 := Ratio.length_height (_root_.Integration9_9.TM2NativeArithmetic.runtimeHeight h)



end Integration9_9.TM2NativeArithmetic

end

section
























































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.RationalBits
end AFT.BinaryArithmetic.RationalBits
namespace AFT.BinaryArithmetic.RationalBits.arctan
end AFT.BinaryArithmetic.RationalBits.arctan
namespace AFT.BinaryArithmetic.RationalBits.arctanTerm
end AFT.BinaryArithmetic.RationalBits.arctanTerm
namespace AFT.BinaryArithmetic.RationalBits.pi
end AFT.BinaryArithmetic.RationalBits.pi
namespace AFT.BinaryArithmetic.RationalBits.piWork
end AFT.BinaryArithmetic.RationalBits.piWork
namespace AFT.BinaryArithmetic.RationalBits.seriesStepWork
end AFT.BinaryArithmetic.RationalBits.seriesStepWork
namespace AFT.BinaryArithmetic.RationalBits.seriesTermWork
end AFT.BinaryArithmetic.RationalBits.seriesTermWork
namespace AFT.BinaryArithmetic.RationalBits.taylor
end AFT.BinaryArithmetic.RationalBits.taylor
namespace AFT.BinaryArithmetic.RationalBits.taylorTerm
end AFT.BinaryArithmetic.RationalBits.taylorTerm
namespace AFT.BinaryArithmetic.RationalBits.termBits_bounds
end AFT.BinaryArithmetic.RationalBits.termBits_bounds



set_option autoImplicit false
set_option maxHeartbeats 4000000
set_option maxRecDepth 10000
set_option synthInstance.maxSize 1024
set_option backward.isDefEq.respectTransparency false

/-! Actual polynomial-time programs for the unary-controlled finite arithmetic
used in the Gaussian reference tables: powers, factorials, Taylor and arctangent
sums, and the native rational pi approximation. -/
namespace Integration9_9.TM2NativeFiniteSeries
open AFT.RationalNumerics AFT.BinaryArithmetic
open OAI.MetricKMedian.Binary
open OAI.MetricKMedian.Binary.Automatic
open scoped BigOperators

abbrev unitCode : Unit → List Bool := fun _ => []
abbrev ticksCode : List Unit → List Bool := encodeList _root_.Integration9_9.TM2NativeFiniteSeries.unitCode
abbrev inputCode : ℚ × List Unit → List Bool := encodePair Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode
abbrev termCode : (ℚ × List Unit) × ℕ → List Bool := encodePair _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Nat.bits

def powerValue (x : ℚ × List Unit) : ℚ := x.1^x.2.length

theorem product_repeated (q : ℚ) (xs : List Unit) : (xs.map (fun _ => q)).prod=q^xs.length := by
  induction xs with
  | nil => simp
  | cons u xs ih => simp only [List.map_cons,List.prod_cons,List.length_cons,ih,pow_succ]; ring

noncomputable def powerProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.powerValue := by
  let repeated : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.inputCode (encodeList Ratio.encode) (fun x : ℚ × List Unit => x.2.map (fun _ => x.1)) := by
    change PolyProgram Automatic.enc Automatic.enc _
    exact (show Poly (fun x : ℚ × List Unit => x.2.map (fun _ => x.1)) by fun_prop).program
  exact (repeated.comp PolyProgram.ratProd).congr _ (fun x => _root_.Integration9_9.TM2NativeFiniteSeries.product_repeated x.1 x.2)

def factorialValue (xs : List Unit) : ℚ := (xs.length.factorial : ℚ)

 theorem _root_.Integration9_9.TM2NativeFiniteSeries.factorial_range (n : ℕ) :
    ((List.range n).map (fun i => ((i+1 : ℕ) : ℚ))).prod = (n.factorial : ℚ) := by
  induction n with
  | zero => simp
  | succ n ih =>
    simp only [List.range_succ,List.map_append,List.prod_append,ih,List.map_singleton,List.prod_cons,List.prod_nil,mul_one,
      Nat.factorial_succ,Nat.cast_mul]
    ring

noncomputable def factorialProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.factorialValue := by
  let successor := (((PolyProgram.identity Nat.bits).fanout (PolyProgram.constant Nat.bits Nat.bits 1)).comp
    PolyProgram.natAdd).comp PolyProgram.ratOfNat
  exact (((PolyProgram.listIndices _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ()).comp (successor.listMap 0)).comp PolyProgram.ratProd).congr _
    (fun xs => by simpa only [Function.comp_def,id_eq,_root_.Integration9_9.TM2NativeFiniteSeries.factorialValue] using _root_.Integration9_9.TM2NativeFiniteSeries.factorial_range xs.length)





def takeTicks (x : (ℚ × List Unit) × ℕ) : List Unit := x.1.2.take x.2

def cappedTaylor (x : (ℚ × List Unit) × ℕ) : ℚ := _root_.Integration9_9.TM2NativeFiniteSeries.powerValue (x.1.1,_root_.Integration9_9.TM2NativeFiniteSeries.takeTicks x)/_root_.Integration9_9.TM2NativeFiniteSeries.factorialValue (_root_.Integration9_9.TM2NativeFiniteSeries.takeTicks x)
def degree (xs : List Unit) : List Unit := xs++xs++[()]
def cappedArctan (x : (ℚ × List Unit) × ℕ) : ℚ :=
  _root_.Integration9_9.TM2NativeFiniteSeries.powerValue (-1,_root_.Integration9_9.TM2NativeFiniteSeries.takeTicks x)*_root_.Integration9_9.TM2NativeFiniteSeries.powerValue (x.1.1,_root_.Integration9_9.TM2NativeFiniteSeries.degree (_root_.Integration9_9.TM2NativeFiniteSeries.takeTicks x))/((_root_.Integration9_9.TM2NativeFiniteSeries.degree (_root_.Integration9_9.TM2NativeFiniteSeries.takeTicks x)).length : ℚ)

 noncomputable def _root_.Integration9_9.TM2NativeFiniteSeries.takeTicksProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.termCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeFiniteSeries.takeTicks := by
  let context := PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Nat.bits
  let index := PolyProgram.snd _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Nat.bits
  let ticks := context.comp (PolyProgram.snd Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  exact ((ticks.fanout index).comp (PolyProgram.listTake _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())).congr _ (fun _ => rfl)

noncomputable def taylorTermProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.termCode Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.cappedTaylor := by
  let argument := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Nat.bits).comp (PolyProgram.fst Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  let numerator := (argument.fanout _root_.Integration9_9.TM2NativeFiniteSeries.takeTicksProgram).comp _root_.Integration9_9.TM2NativeFiniteSeries.powerProgram
  let denominator := _root_.Integration9_9.TM2NativeFiniteSeries.takeTicksProgram.comp _root_.Integration9_9.TM2NativeFiniteSeries.factorialProgram
  exact ((numerator.fanout denominator).comp PolyProgram.ratDiv).congr _ (fun _ => rfl)

noncomputable def arctanTermProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.termCode Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.cappedArctan := by
  let argument := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Nat.bits).comp (PolyProgram.fst Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  let double := (_root_.Integration9_9.TM2NativeFiniteSeries.takeTicksProgram.fanout _root_.Integration9_9.TM2NativeFiniteSeries.takeTicksProgram).comp (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let exponent := (double.fanout (PolyProgram.constant _root_.Integration9_9.TM2NativeFiniteSeries.termCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode [()])).comp (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let sign := ((PolyProgram.constant _root_.Integration9_9.TM2NativeFiniteSeries.termCode Ratio.encode (-1)).fanout _root_.Integration9_9.TM2NativeFiniteSeries.takeTicksProgram).comp _root_.Integration9_9.TM2NativeFiniteSeries.powerProgram
  let power := (argument.fanout exponent).comp _root_.Integration9_9.TM2NativeFiniteSeries.powerProgram
  let denominator := (exponent.comp (PolyProgram.listLength _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())).comp PolyProgram.ratOfNat
  exact ((((sign.fanout power).comp PolyProgram.ratMul).fanout denominator).comp PolyProgram.ratDiv).congr _ (fun _ => rfl)

theorem cappedTaylor_eq (q : ℚ) (xs : List Unit) (i : ℕ) (hi : i ≤ xs.length) :
    _root_.Integration9_9.TM2NativeFiniteSeries.cappedTaylor ((q,xs),i) = q^i/(i.factorial : ℚ) := by
  simp only [_root_.Integration9_9.TM2NativeFiniteSeries.cappedTaylor,_root_.Integration9_9.TM2NativeFiniteSeries.takeTicks,_root_.Integration9_9.TM2NativeFiniteSeries.powerValue,_root_.Integration9_9.TM2NativeFiniteSeries.factorialValue,List.length_take,Nat.min_eq_left hi]

theorem cappedArctan_eq (q : ℚ) (xs : List Unit) (i : ℕ) (hi : i ≤ xs.length) :
    _root_.Integration9_9.TM2NativeFiniteSeries.cappedArctan ((q,xs),i) = (-1 : ℚ)^i*q^(2*i+1)/(2*i+1 : ℕ) := by
  simp only [_root_.Integration9_9.TM2NativeFiniteSeries.cappedArctan,_root_.Integration9_9.TM2NativeFiniteSeries.takeTicks,_root_.Integration9_9.TM2NativeFiniteSeries.powerValue,_root_.Integration9_9.TM2NativeFiniteSeries.degree,List.length_take,Nat.min_eq_left hi,
    List.length_append,List.length_singleton,two_mul]

 theorem _root_.Integration9_9.TM2NativeFiniteSeries.sum_range (f : ℕ → ℚ) (n : ℕ) : ((List.range n).map f).sum = ∑ i∈Finset.range n,f i := by
  induction n with
  | zero => simp
  | succ n ih => simp only [List.range_succ,List.map_append,List.sum_append,ih,List.map_singleton,List.sum_cons,List.sum_nil,add_zero,Finset.sum_range_succ]

noncomputable def taylorProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Ratio.encode (fun x => _root_.AFT.RationalNumerics.expSum x.1 x.2.length) := by
  let indices := (PolyProgram.snd Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.listIndices _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let terms := ((PolyProgram.identity _root_.Integration9_9.TM2NativeFiniteSeries.inputCode).fanout indices).comp (_root_.Integration9_9.TM2NativeFiniteSeries.taylorTermProgram.listMapWith 0 (0 : ℚ))
  exact (terms.comp PolyProgram.ratSum).congr _ (fun x => by
    simp only [Function.comp_apply,id_eq]
    have he : (List.range x.2.length).map (fun i => _root_.Integration9_9.TM2NativeFiniteSeries.cappedTaylor (x,i)) =
        (List.range x.2.length).map (fun i => x.1^i/(i.factorial : ℚ)) := by
      apply List.map_congr_left
      intro i hi
      exact _root_.Integration9_9.TM2NativeFiniteSeries.cappedTaylor_eq x.1 x.2 i (List.mem_range.mp hi).le
    rw [he,_root_.Integration9_9.TM2NativeFiniteSeries.sum_range]
    rfl)

noncomputable def arctanProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.inputCode Ratio.encode (fun x => _root_.AFT.RationalNumerics.arctanSum x.1 x.2.length) := by
  let indices := (PolyProgram.snd Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.listIndices _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let terms := ((PolyProgram.identity _root_.Integration9_9.TM2NativeFiniteSeries.inputCode).fanout indices).comp (_root_.Integration9_9.TM2NativeFiniteSeries.arctanTermProgram.listMapWith 0 (0 : ℚ))
  exact (terms.comp PolyProgram.ratSum).congr _ (fun x => by
    simp only [Function.comp_apply,id_eq]
    have he : (List.range x.2.length).map (fun i => _root_.Integration9_9.TM2NativeFiniteSeries.cappedArctan (x,i)) =
        (List.range x.2.length).map (fun i => (-1 : ℚ)^i*x.1^(2*i+1)/(2*i+1 : ℕ)) := by
      apply List.map_congr_left
      intro i hi
      exact _root_.Integration9_9.TM2NativeFiniteSeries.cappedArctan_eq x.1 x.2 i (List.mem_range.mp hi).le
    rw [he,_root_.Integration9_9.TM2NativeFiniteSeries.sum_range]
    rfl)





noncomputable def piProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode Ratio.encode (fun xs => _root_.AFT.RationalNumerics.piApprox xs.length) := by
  let terms := ((PolyProgram.identity _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).fanout (PolyProgram.constant _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode [(),(),()])).comp
    (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let first := ((PolyProgram.constant _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode Ratio.encode (1/2)).fanout terms).comp _root_.Integration9_9.TM2NativeFiniteSeries.arctanProgram
  let second := ((PolyProgram.constant _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode Ratio.encode (1/3)).fanout terms).comp _root_.Integration9_9.TM2NativeFiniteSeries.arctanProgram
  let sum := (first.fanout second).comp PolyProgram.ratAdd
  exact (((PolyProgram.constant _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode Ratio.encode 4).fanout sum).comp PolyProgram.ratMul).congr _ (fun xs => by
    simp only [Function.comp_apply,id_eq,List.length_append,List.length_cons,List.length_nil,_root_.AFT.RationalNumerics.piApprox])



end Integration9_9.TM2NativeFiniteSeries

end

section
























































namespace AFT
end AFT
namespace AFT.BinaryArithmetic
end AFT.BinaryArithmetic
namespace AFT.BinaryArithmetic.RationalBits
end AFT.BinaryArithmetic.RationalBits
namespace AFT.BinaryArithmetic.RationalBits.gaussian
end AFT.BinaryArithmetic.RationalBits.gaussian
namespace AFT.BinaryArithmetic.RationalBits.gaussianFromRatio
end AFT.BinaryArithmetic.RationalBits.gaussianFromRatio
namespace AFT.BinaryArithmetic.RationalBits.gaussianRatioWork
end AFT.BinaryArithmetic.RationalBits.gaussianRatioWork
namespace AFT.BinaryArithmetic.RationalBits.gaussianWork
end AFT.BinaryArithmetic.RationalBits.gaussianWork



set_option autoImplicit false
set_option maxHeartbeats 500000
set_option maxRecDepth 10000
set_option synthInstance.maxSize 1024
set_option backward.isDefEq.respectTransparency false

/-! Actual rational ceiling and bounded-control exponential/Gaussian machines.
The extra unary buffers are real input data. Every control count is clipped to
its available buffer, so the programs have polynomial time on all typed inputs;
exact native values follow from numerical bounds on the actual Gaussian ratio. -/
namespace Integration9_9.TM2NativeGaussian
open AFT.RationalNumerics AFT.BinaryArithmetic
open OAI.MetricKMedian.Binary
open OAI.MetricKMedian.Binary.Automatic
open TM2NativeFiniteSeries
attribute [local irreducible] expSum arctanSum expNegScaled expNegApprox piApprox
  RationalBits.exponential RationalBits.gaussianFromRatio

 theorem _root_.Integration9_9.TM2NativeGaussian.ceil_fraction (num den : ℕ) (hd : 0 < den) :
    ⌈(num : ℚ)/(den : ℚ)⌉₊=if num%den=0 then num/den else num/den+1 := by
  have hdq : (0 : ℚ)<den := by exact_mod_cast hd
  have hb : num/den*den+num%den=num := by nlinarith [Nat.div_add_mod num den]
  have hr := Nat.mod_lt num hd
  split <;> rename_i hz
  · have he : (num : ℚ)/(den : ℚ)=(num/den : ℕ) := by
      apply (div_eq_iff hdq.ne').mpr
      have hn : num=num/den*den := by omega
      exact_mod_cast hn
    rw [he,Nat.ceil_natCast]
  · apply (Nat.ceil_eq_iff (Nat.succ_ne_zero (num/den))).mpr
    constructor
    · apply (lt_div_iff₀ hdq).mpr
      have hn : num/den*den<num := by omega
      exact_mod_cast hn
    · apply (div_le_iff₀ hdq).mpr
      have hn : num≤ (num/den+1)*den := by nlinarith
      exact_mod_cast hn

def ceilingValue (q : ℚ) : ℕ :=
  if q<0 then 0 else if q.num.natAbs%q.den=0 then q.num.natAbs/q.den else q.num.natAbs/q.den+1

theorem ceilingValue_eq (q : ℚ) : _root_.Integration9_9.TM2NativeGaussian.ceilingValue q = ⌈q⌉₊ := by
  by_cases hq : q<0
  · simp only [_root_.Integration9_9.TM2NativeGaussian.ceilingValue,hq,ite_true,Nat.ceil_eq_zero.mpr hq.le]
  · have hnum : (q.num.natAbs : ℚ) = q.num := by
      simpa only [Int.cast_natCast] using congrArg (fun z : ℤ => (z : ℚ))
        (Int.natAbs_of_nonneg (Rat.num_nonneg.mpr (le_of_not_gt hq)))
    have he : (q.num.natAbs : ℚ)/(q.den : ℚ)=q := by rw [hnum,Rat.num_div_den]
    simpa only [_root_.Integration9_9.TM2NativeGaussian.ceilingValue,hq,ite_false,he] using (_root_.Integration9_9.TM2NativeGaussian.ceil_fraction q.num.natAbs q.den q.den_pos).symm

@[fun_prop] theorem ceilingValue_poly : Poly _root_.Integration9_9.TM2NativeGaussian.ceilingValue := by
  have hn : Poly (fun q : ℚ => q.num.natAbs) := ⟨PolyProgram.ratNum.comp PolyProgram.intAbs⟩
  have hd : Poly (fun q : ℚ => q.den) := ⟨PolyProgram.ratDen⟩
  have div : Poly (fun q : ℚ => q.num.natAbs/q.den) := ⟨(hn.program.fanout hd.program).comp PolyProgram.natDiv⟩
  have rem : Poly (fun q : ℚ => q.num.natAbs%q.den) := ⟨(hn.program.fanout hd.program).comp PolyProgram.natMod⟩
  unfold _root_.Integration9_9.TM2NativeGaussian.ceilingValue
  fun_prop

noncomputable def ceilingProgram : PolyProgram Ratio.encode Nat.bits (fun q => ⌈q⌉₊) :=
  _root_.Integration9_9.TM2NativeGaussian.ceilingValue_poly.program.congr _ _root_.Integration9_9.TM2NativeGaussian.ceilingValue_eq

@[fun_prop] theorem ceiling_poly : Poly (fun q : ℚ => ⌈q⌉₊) := ⟨_root_.Integration9_9.TM2NativeGaussian.ceilingProgram⟩

def clipped (q : ℚ) : ℚ := if q<0 then 0 else q
@[fun_prop] theorem clipped_poly : Poly _root_.Integration9_9.TM2NativeGaussian.clipped := by unfold _root_.Integration9_9.TM2NativeGaussian.clipped;fun_prop

theorem clipped_eq (q : ℚ) : _root_.Integration9_9.TM2NativeGaussian.clipped q=max q 0 := by
  by_cases h : q<0
  · simp [_root_.Integration9_9.TM2NativeGaussian.clipped,h,max_eq_right h.le]
  · simp [_root_.Integration9_9.TM2NativeGaussian.clipped,h,max_eq_left (le_of_not_gt h)]

@[fun_prop] theorem expScale_poly : Poly _root_.AFT.RationalNumerics.expScale := by
  have h : Poly (fun q : ℚ => 2*⌈_root_.Integration9_9.TM2NativeGaussian.clipped q⌉₊+1) := by fun_prop
  change Poly (fun q : ℚ => 2*⌈max q 0⌉₊+1)
  simpa only [_root_.Integration9_9.TM2NativeGaussian.clipped_eq] using h

/-- Argument, requested precision ticks, and an actual scale-cap buffer. -/
abbrev Request := (ℚ × List Unit) × List Unit
abbrev requestCode : _root_.Integration9_9.TM2NativeGaussian.Request → List Bool := encodePair _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode

def scaleTicks (x : _root_.Integration9_9.TM2NativeGaussian.Request) : List Unit := x.2.take (_root_.AFT.RationalNumerics.expScale x.1.1)

def exponentialCapped (x : _root_.Integration9_9.TM2NativeGaussian.Request) : ℚ :=
  _root_.AFT.RationalNumerics.expNegScaled (max x.1.1 0) (_root_.Integration9_9.TM2NativeGaussian.scaleTicks x).length x.1.2.length

 noncomputable def _root_.Integration9_9.TM2NativeGaussian.scaleTicksProgram : PolyProgram _root_.Integration9_9.TM2NativeGaussian.requestCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeGaussian.scaleTicks := by
  let argument := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.fst Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  let count := argument.comp _root_.Integration9_9.TM2NativeGaussian.expScale_poly.program
  exact (((PolyProgram.snd _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).fanout count).comp (PolyProgram.listTake _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())).congr _ (fun _ => rfl)

noncomputable def exponentialProgram : PolyProgram _root_.Integration9_9.TM2NativeGaussian.requestCode Ratio.encode _root_.Integration9_9.TM2NativeGaussian.exponentialCapped := by
  let argument := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.fst Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  let bits := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.snd Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  let scale := (_root_.Integration9_9.TM2NativeGaussian.scaleTicksProgram.comp (PolyProgram.listLength _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())).comp PolyProgram.ratOfNat
  let reduced := ((argument.comp _root_.Integration9_9.TM2NativeGaussian.clipped_poly.program).fanout scale).comp PolyProgram.ratDiv
  let count := ((bits.fanout _root_.Integration9_9.TM2NativeGaussian.scaleTicksProgram).comp (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ()))
  let terms := (count.fanout (PolyProgram.constant _root_.Integration9_9.TM2NativeGaussian.requestCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode [()])).comp (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let polynomial := (reduced.fanout terms).comp _root_.Integration9_9.TM2NativeFiniteSeries.taylorProgram
  exact (((polynomial.comp PolyProgram.ratInv).fanout _root_.Integration9_9.TM2NativeGaussian.scaleTicksProgram).comp _root_.Integration9_9.TM2NativeFiniteSeries.powerProgram).congr _ (fun x => by
    simp only [Function.comp_apply,_root_.Integration9_9.TM2NativeGaussian.exponentialCapped,_root_.AFT.RationalNumerics.expNegScaled,_root_.Integration9_9.TM2NativeFiniteSeries.powerValue,_root_.Integration9_9.TM2NativeGaussian.clipped_eq,
      List.length_append,List.length_singleton])

theorem exponentialCapped_eq (x : _root_.Integration9_9.TM2NativeGaussian.Request) (h : _root_.AFT.RationalNumerics.expScale x.1.1 ≤ x.2.length) :
    _root_.Integration9_9.TM2NativeGaussian.exponentialCapped x=_root_.AFT.RationalNumerics.expNegApprox x.1.1 x.1.2.length := by
  simp only [_root_.Integration9_9.TM2NativeGaussian.exponentialCapped,_root_.Integration9_9.TM2NativeGaussian.scaleTicks,List.length_take,Nat.min_eq_left h,_root_.AFT.RationalNumerics.expNegApprox]

/-- Eleven actual copies of the ratio budget, plus eleven constant ticks. -/
def scaleBudget (xs : List Unit) : List Unit := (List.replicate 11 ()).flatMap (fun _ => ()::xs)
@[fun_prop] theorem scaleBudget_poly : Poly _root_.Integration9_9.TM2NativeGaussian.scaleBudget := by unfold _root_.Integration9_9.TM2NativeGaussian.scaleBudget;fun_prop

theorem scaleBudget_length (xs : List Unit) : (_root_.Integration9_9.TM2NativeGaussian.scaleBudget xs).length=11*(xs.length+1) := by
  simp [_root_.Integration9_9.TM2NativeGaussian.scaleBudget]
  omega

def guardTicks (x : _root_.Integration9_9.TM2NativeGaussian.Request) : List Unit := x.2.take ⌈x.1.1⌉₊
def piTicks (x : _root_.Integration9_9.TM2NativeGaussian.Request) : List Unit := x.1.2++_root_.Integration9_9.TM2NativeGaussian.guardTicks x++[()]
def gaussianArgument (x : _root_.Integration9_9.TM2NativeGaussian.Request) : ℚ := _root_.AFT.RationalNumerics.piApprox (_root_.Integration9_9.TM2NativeGaussian.piTicks x).length*x.1.1

def gaussianCapped (x : _root_.Integration9_9.TM2NativeGaussian.Request) : ℚ :=
  _root_.Integration9_9.TM2NativeGaussian.exponentialCapped ((_root_.Integration9_9.TM2NativeGaussian.gaussianArgument x,x.1.2++[()]),_root_.Integration9_9.TM2NativeGaussian.scaleBudget x.2)

 noncomputable def _root_.Integration9_9.TM2NativeGaussian.guardTicksProgram : PolyProgram _root_.Integration9_9.TM2NativeGaussian.requestCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeGaussian.guardTicks := by
  let argument := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.fst Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  exact (((PolyProgram.snd _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).fanout (argument.comp _root_.Integration9_9.TM2NativeGaussian.ceilingProgram)).comp (PolyProgram.listTake _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())).congr _ (fun _ => rfl)

noncomputable def gaussianProgram : PolyProgram _root_.Integration9_9.TM2NativeGaussian.requestCode Ratio.encode _root_.Integration9_9.TM2NativeGaussian.gaussianCapped := by
  let argument := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.fst Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  let bits := (PolyProgram.fst _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp (PolyProgram.snd Ratio.encode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode)
  let count := (bits.fanout _root_.Integration9_9.TM2NativeGaussian.guardTicksProgram).comp (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let piCount := (count.fanout (PolyProgram.constant _root_.Integration9_9.TM2NativeGaussian.requestCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode [()])).comp (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let gaussianArg := ((piCount.comp _root_.Integration9_9.TM2NativeFiniteSeries.piProgram).fanout argument).comp PolyProgram.ratMul
  let precision := (bits.fanout (PolyProgram.constant _root_.Integration9_9.TM2NativeGaussian.requestCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode [()])).comp (PolyProgram.listAppend _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  let budget := (PolyProgram.snd _root_.Integration9_9.TM2NativeFiniteSeries.inputCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode).comp _root_.Integration9_9.TM2NativeGaussian.scaleBudget_poly.program
  exact ((((gaussianArg.fanout precision).fanout budget).comp _root_.Integration9_9.TM2NativeGaussian.exponentialProgram)).congr _ (fun _ => rfl)

theorem gaussianCapped_eq (x : _root_.Integration9_9.TM2NativeGaussian.Request) (h0 : 0≤x.1.1) (hr : x.1.1≤(x.2.length : ℚ)) :
    _root_.Integration9_9.TM2NativeGaussian.gaussianCapped x=_root_.AFT.RationalNumerics.expNegApprox (_root_.AFT.RationalNumerics.piApprox (x.1.2.length+⌈x.1.1⌉₊+1)*x.1.1) (x.1.2.length+1) := by
  have hg : ⌈x.1.1⌉₊≤x.2.length := Nat.ceil_le.mpr hr
  have he : (_root_.Integration9_9.TM2NativeGaussian.piTicks x).length=x.1.2.length+⌈x.1.1⌉₊+1 := by
    simp only [_root_.Integration9_9.TM2NativeGaussian.piTicks,_root_.Integration9_9.TM2NativeGaussian.guardTicks,List.length_append,List.length_take,Nat.min_eq_left hg,List.length_singleton]
  have hp := mul_le_mul_of_nonneg_right (_root_.InfoLossLARNativeSupplements.h91d71a61f0497f7b.AFT.RationalNumerics.piApprox_le_five (_root_.Integration9_9.TM2NativeGaussian.piTicks x).length) h0
  have ha : max (_root_.Integration9_9.TM2NativeGaussian.gaussianArgument x) 0 ≤ ((5*x.2.length : ℕ) : ℚ) := by
    apply max_le
    · dsimp only [_root_.Integration9_9.TM2NativeGaussian.gaussianArgument]
      have hm := mul_le_mul_of_nonneg_left hr (by norm_num : (0 : ℚ)≤5)
      exact hp.trans (by simpa only [Nat.cast_mul,Nat.cast_ofNat] using hm)
    · positivity
  have hc : ⌈max (_root_.Integration9_9.TM2NativeGaussian.gaussianArgument x) 0⌉₊≤5*x.2.length := Nat.ceil_le.mpr ha
  have hs : _root_.AFT.RationalNumerics.expScale (_root_.Integration9_9.TM2NativeGaussian.gaussianArgument x)≤(_root_.Integration9_9.TM2NativeGaussian.scaleBudget x.2).length := by
    rw [_root_.Integration9_9.TM2NativeGaussian.scaleBudget_length]
    unfold _root_.AFT.RationalNumerics.expScale
    omega
  rw [_root_.Integration9_9.TM2NativeGaussian.gaussianCapped,_root_.Integration9_9.TM2NativeGaussian.exponentialCapped_eq ((_root_.Integration9_9.TM2NativeGaussian.gaussianArgument x,x.1.2++[()]),_root_.Integration9_9.TM2NativeGaussian.scaleBudget x.2) hs]
  simp only [_root_.Integration9_9.TM2NativeGaussian.gaussianArgument,he,List.length_append,List.length_singleton]









end Integration9_9.TM2NativeGaussian

end

section
























































namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.CurrentGaussianBits
end CurrentAFT.CurrentGaussianBits
namespace CurrentAFT.CurrentGaussianBits.budget
end CurrentAFT.CurrentGaussianBits.budget
namespace CurrentAFT.CurrentGaussianBits.table
end CurrentAFT.CurrentGaussianBits.table
namespace CurrentAFT.CurrentGaussianBits.tableWork
end CurrentAFT.CurrentGaussianBits.tableWork
namespace CurrentAFT.CurrentGaussianBits.widthCode
end CurrentAFT.CurrentGaussianBits.widthCode
namespace CurrentAFT.CurrentGaussianBits.widthWork
end CurrentAFT.CurrentGaussianBits.widthWork
namespace CurrentAFT.CurrentGaussianBits.workPolynomial
end CurrentAFT.CurrentGaussianBits.workPolynomial
namespace CurrentAFT.WindowGaussianSampler
end CurrentAFT.WindowGaussianSampler
namespace CurrentAFT.WindowGaussianSampler.squaredWidth
end CurrentAFT.WindowGaussianSampler.squaredWidth



set_option autoImplicit false
set_option maxHeartbeats 1000000
set_option maxRecDepth 10000
set_option synthInstance.maxSize 1024
set_option backward.isDefEq.respectTransparency false

/-! Full current Gaussian window tables generated from actual unary security,
cutoff, and precision buffers. The executable cutoff is clipped to the square
of security+2. No externally supplied probability table or runtime bound is
needed. Exact normalization is performed by verified rational machines. -/
namespace Integration9_9.TM2NativeGaussianWindow
open AFT AFT.RationalNumerics AFT.BinaryArithmetic
open AFT.GaussianSource AFT.FiniteConvolutionProgram
open OAI.MetricKMedian.Binary OAI.MetricKMedian.Binary.Automatic
open TM2NativeFiniteSeries TM2NativeGaussian
open scoped BigOperators
attribute [local irreducible] expSum arctanSum expNegScaled expNegApprox piApprox gaussianApprox

abbrev Request := (List Unit × List Unit) × List Unit
abbrev requestCode : _root_.Integration9_9.TM2NativeGaussianWindow.Request → List Bool := Automatic.enc

def baseTicks (xs : List Unit) : List Unit := ()::()::xs
def squareTicks (xs : List Unit) : List Unit := (_root_.Integration9_9.TM2NativeGaussianWindow.baseTicks xs).flatMap (fun _ => _root_.Integration9_9.TM2NativeGaussianWindow.baseTicks xs)
@[fun_prop] theorem baseTicks_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.baseTicks := by unfold _root_.Integration9_9.TM2NativeGaussianWindow.baseTicks;fun_prop
@[fun_prop] theorem squareTicks_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.squareTicks := by unfold _root_.Integration9_9.TM2NativeGaussianWindow.squareTicks;fun_prop

theorem squareTicks_length (xs : List Unit) : (_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks xs).length=(xs.length+2)^2 := by
  simp only [_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks,List.length_flatMap,_root_.Integration9_9.TM2NativeGaussianWindow.baseTicks,List.length_cons,List.map_const',List.sum_replicate]
  ring

def cutoffTicks (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : List Unit := x.1.2.take (_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1).length

def indexTicks (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : List Unit := _root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks x++_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks x++[()]
def precisionTicks (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : List Unit := x.2++_root_.Integration9_9.TM2NativeGaussianWindow.indexTicks x++[()]
def width (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : ℚ := ((x.1.1.length+2 : ℕ) : ℚ)^2

def ratio (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request × ℕ) : ℚ :=
  ((x.2 : ℚ)-((_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks x.1).length : ℚ)) *
  ((x.2 : ℚ)-((_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks x.1).length : ℚ)) / _root_.Integration9_9.TM2NativeGaussianWindow.width x.1

noncomputable def cutoffProgram : PolyProgram _root_.Integration9_9.TM2NativeGaussianWindow.requestCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks := by
  let cut : PolyProgram _root_.Integration9_9.TM2NativeGaussianWindow.requestCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request => x.1.2) := (show Poly (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request => x.1.2) by fun_prop).program
  let budget : PolyProgram _root_.Integration9_9.TM2NativeGaussianWindow.requestCode Nat.bits (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request => (_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1).length) :=
    (show Poly (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request => (_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1).length) by fun_prop).program
  exact ((cut.fanout budget).comp (PolyProgram.listTake _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())).congr _ (fun _ => rfl)

@[fun_prop] theorem cutoffTicks_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks := ⟨_root_.Integration9_9.TM2NativeGaussianWindow.cutoffProgram⟩
@[fun_prop] theorem indexTicks_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.indexTicks := by unfold _root_.Integration9_9.TM2NativeGaussianWindow.indexTicks;fun_prop
@[fun_prop] theorem precisionTicks_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks := by unfold _root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks;fun_prop
@[fun_prop] theorem width_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.width := by
  have h : Poly (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request => ((x.1.1.length+2 : ℕ) : ℚ)*((x.1.1.length+2 : ℕ) : ℚ)) := by fun_prop
  change Poly (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request => ((x.1.1.length+2 : ℕ) : ℚ)^2)
  simpa only [pow_two] using h
@[fun_prop] theorem ratio_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.ratio := by unfold _root_.Integration9_9.TM2NativeGaussianWindow.ratio;fun_prop

def pointValue (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request × ℕ) : ℚ :=
  _root_.Integration9_9.TM2NativeGaussian.gaussianCapped ((_root_.Integration9_9.TM2NativeGaussianWindow.ratio x,_root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks x.1),_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1.1)

noncomputable def pointProgram : PolyProgram (encodePair _root_.Integration9_9.TM2NativeGaussianWindow.requestCode Nat.bits) Ratio.encode _root_.Integration9_9.TM2NativeGaussianWindow.pointValue := by
  let init : PolyProgram (encodePair _root_.Integration9_9.TM2NativeGaussianWindow.requestCode Nat.bits) _root_.Integration9_9.TM2NativeGaussian.requestCode
      (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request × ℕ => ((_root_.Integration9_9.TM2NativeGaussianWindow.ratio x,_root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks x.1),_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1.1)) :=
    (show Poly (fun x : _root_.Integration9_9.TM2NativeGaussianWindow.Request × ℕ => ((_root_.Integration9_9.TM2NativeGaussianWindow.ratio x,_root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks x.1),_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1.1)) by fun_prop).program
  exact init.comp _root_.Integration9_9.TM2NativeGaussian.gaussianProgram

def raw (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : List ℚ := (List.range (_root_.Integration9_9.TM2NativeGaussianWindow.indexTicks x).length).map (fun i => _root_.Integration9_9.TM2NativeGaussianWindow.pointValue (x,i))

noncomputable def rawProgram : PolyProgram _root_.Integration9_9.TM2NativeGaussianWindow.requestCode (encodeList Ratio.encode) _root_.Integration9_9.TM2NativeGaussianWindow.raw := by
  let indices := _root_.Integration9_9.TM2NativeGaussianWindow.indexTicks_poly.program.comp (PolyProgram.listIndices _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  exact ((PolyProgram.identity _root_.Integration9_9.TM2NativeGaussianWindow.requestCode).fanout indices).comp (_root_.Integration9_9.TM2NativeGaussianWindow.pointProgram.listMapWith 0 (0 : ℚ))

def normalize (xs : List ℚ) : List ℚ := xs.map (fun q => q/xs.sum)

noncomputable def normalizeProgram : PolyProgram (encodeList Ratio.encode) (encodeList Ratio.encode) _root_.Integration9_9.TM2NativeGaussianWindow.normalize := by
  let divide : PolyProgram (encodePair Ratio.encode Ratio.encode) Ratio.encode (fun x : ℚ×ℚ => x.2/x.1) :=
    ((PolyProgram.exchange Ratio.encode Ratio.encode).comp PolyProgram.ratDiv)
  exact (PolyProgram.ratSum.fanout (PolyProgram.identity (encodeList Ratio.encode))).comp (divide.listMapWith 0 0)

def current (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : List ℚ := _root_.Integration9_9.TM2NativeGaussianWindow.normalize (_root_.Integration9_9.TM2NativeGaussianWindow.raw x)
noncomputable def currentProgram : PolyProgram _root_.Integration9_9.TM2NativeGaussianWindow.requestCode (encodeList Ratio.encode) _root_.Integration9_9.TM2NativeGaussianWindow.current := _root_.Integration9_9.TM2NativeGaussianWindow.rawProgram.comp _root_.Integration9_9.TM2NativeGaussianWindow.normalizeProgram

def cutoff (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : ℕ := min x.1.2.length ((x.1.1.length+2)^2)

theorem cutoffTicks_length (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : (_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks x).length=_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x := by
  simp only [_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks,_root_.Integration9_9.TM2NativeGaussianWindow.cutoff,List.length_take,_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks_length,Nat.min_comm]

theorem indexTicks_length (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : (_root_.Integration9_9.TM2NativeGaussianWindow.indexTicks x).length=2*_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x+1 := by
  simp only [_root_.Integration9_9.TM2NativeGaussianWindow.indexTicks,List.length_append,List.length_singleton,_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks_length,two_mul]

theorem precisionTicks_length (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : (_root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks x).length=_root_.AFT.RationalNumerics.windowApproxPrecision (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) x.2.length := by
  simp only [_root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks,List.length_append,List.length_singleton,_root_.Integration9_9.TM2NativeGaussianWindow.indexTicks_length,_root_.AFT.RationalNumerics.windowApproxPrecision]

 theorem _root_.Integration9_9.TM2NativeGaussianWindow.ratio_eq (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) (i : Fin (2*_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x+1)) :
    _root_.Integration9_9.TM2NativeGaussianWindow.ratio (x,i.val)=_root_.AFT.RationalNumerics.gaussianRatio ((x.1.1.length+2 : ℚ)^2) (_root_.AFT.FiniteConvolutionProgram.windowIndexEquiv (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) i).val := by
  simp only [_root_.Integration9_9.TM2NativeGaussianWindow.ratio,_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks_length,_root_.AFT.RationalNumerics.gaussianRatio,_root_.AFT.FiniteConvolutionProgram.windowIndexEquiv,Equiv.coe_fn_mk,_root_.Integration9_9.TM2NativeGaussianWindow.width,Int.cast_sub,Int.cast_natCast,
    Nat.cast_add,Nat.cast_ofNat,pow_two]

theorem pointValue_eq (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) (i : Fin (2*_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x+1)) :
    _root_.Integration9_9.TM2NativeGaussianWindow.pointValue (x,i.val)=_root_.AFT.RationalNumerics.windowApproxRaw ((x.1.1.length+2 : ℚ)^2) (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) x.2.length (_root_.AFT.FiniteConvolutionProgram.windowIndexEquiv (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) i) := by
  have hc : _root_.Integration9_9.TM2NativeGaussianWindow.cutoff x≤(x.1.1.length+2)^2 := Nat.min_le_right _ _
  have hr := _root_.InfoLossLARNativeSupplements.hf7c0550335227b9c.CurrentAFT.CurrentGaussianBits.ratio_bounds x.1.1.length (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) hc (_root_.AFT.FiniteConvolutionProgram.windowIndexEquiv (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) i)
  have h0 : 0≤_root_.Integration9_9.TM2NativeGaussianWindow.ratio (x,i.val) := by rw [_root_.Integration9_9.TM2NativeGaussianWindow.ratio_eq];exact hr.1
  have hb : _root_.Integration9_9.TM2NativeGaussianWindow.ratio (x,i.val)≤((_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1).length : ℚ) := by
    rw [_root_.Integration9_9.TM2NativeGaussianWindow.ratio_eq,_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks_length]
    simpa only [Nat.cast_pow,Nat.cast_add,Nat.cast_ofNat] using hr.2
  unfold _root_.Integration9_9.TM2NativeGaussianWindow.pointValue
  rw [_root_.Integration9_9.TM2NativeGaussian.gaussianCapped_eq ((_root_.Integration9_9.TM2NativeGaussianWindow.ratio (x,i.val),_root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks x),_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1.1) h0 hb]
  rw [_root_.Integration9_9.TM2NativeGaussianWindow.ratio_eq,_root_.Integration9_9.TM2NativeGaussianWindow.precisionTicks_length]
  simp only [_root_.AFT.RationalNumerics.windowApproxRaw,_root_.AFT.RationalNumerics.gaussianApprox,_root_.AFT.RationalNumerics.gaussianGuard]

theorem raw_eq (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : _root_.Integration9_9.TM2NativeGaussianWindow.raw x=List.ofFn (fun i : Fin (2*_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x+1) =>
    _root_.AFT.RationalNumerics.windowApproxRaw ((x.1.1.length+2 : ℚ)^2) (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) x.2.length (_root_.AFT.FiniteConvolutionProgram.windowIndexEquiv (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) i)) := by
  apply List.ext_getElem
  · simp only [_root_.Integration9_9.TM2NativeGaussianWindow.raw,List.length_map,List.length_range,List.length_ofFn,_root_.Integration9_9.TM2NativeGaussianWindow.indexTicks_length]
  · intro i h1 h2
    simp only [_root_.Integration9_9.TM2NativeGaussianWindow.raw,List.getElem_map,List.getElem_range,List.getElem_ofFn]
    exact _root_.Integration9_9.TM2NativeGaussianWindow.pointValue_eq x ⟨i,by simpa only [List.length_ofFn] using h2⟩

theorem current_eq (x : _root_.Integration9_9.TM2NativeGaussianWindow.Request) : _root_.Integration9_9.TM2NativeGaussianWindow.current x=List.ofFn (fun i : Fin (2*_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x+1) =>
    ((_root_.CurrentAFT.CurrentGaussianBits.table x.1.1.length (_root_.Integration9_9.TM2NativeGaussianWindow.cutoff x) x.2.length).data.get i).decode) := by
  simp only [_root_.Integration9_9.TM2NativeGaussianWindow.current,_root_.Integration9_9.TM2NativeGaussianWindow.normalize,_root_.Integration9_9.TM2NativeGaussianWindow.raw_eq,List.sum_ofFn,List.map_ofFn,Function.comp_def]
  apply congrArg List.ofFn
  funext i
  rw [_root_.CurrentAFT.CurrentGaussianBits.table_value]
  rfl



end Integration9_9.TM2NativeGaussianWindow

end

section
























































namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.BinaryActivationProgram
end CurrentAFT.BinaryActivationProgram
namespace CurrentAFT.BinaryActivationProgram.current
end CurrentAFT.BinaryActivationProgram.current
namespace CurrentAFT.BinaryActivationProgram.currentHeight
end CurrentAFT.BinaryActivationProgram.currentHeight
namespace CurrentAFT.BinaryActivationProgram.currentHeightPolynomial
end CurrentAFT.BinaryActivationProgram.currentHeightPolynomial
namespace CurrentAFT.BinaryActivationProgram.currentWork
end CurrentAFT.BinaryActivationProgram.currentWork
namespace CurrentAFT.BinaryActivationProgram.currentWorkPolynomial
end CurrentAFT.BinaryActivationProgram.currentWorkPolynomial
namespace CurrentAFT.BinaryActivationProgram.cutoff
end CurrentAFT.BinaryActivationProgram.cutoff
namespace CurrentAFT.BinaryActivationProgram.cutoffWork
end CurrentAFT.BinaryActivationProgram.cutoffWork
namespace CurrentAFT.BinaryActivationProgram.cutoffWorkPolynomial
end CurrentAFT.BinaryActivationProgram.cutoffWorkPolynomial



set_option autoImplicit false
set_option maxHeartbeats 1000000
set_option maxRecDepth 10000
set_option synthInstance.maxSize 1024
set_option backward.isDefEq.respectTransparency false

/-! The actual current activation entry, generated from unary security and
precision. Its Gaussian table, zero-coordinate lookup, and half factor are
all computed by verified finite Boolean-stack machines. -/
namespace Integration9_9.TM2NativeActivation
open AFT AFT.RationalNumerics AFT.BinaryArithmetic
open AFT.GaussianSource AFT.FiniteConvolutionProgram
open OAI.MetricKMedian.Binary OAI.MetricKMedian.Binary.Automatic
open TM2NativeFiniteSeries

abbrev Request := List Unit × List Unit
abbrev requestCode : _root_.Integration9_9.TM2NativeActivation.Request → List Bool := encodePair _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode

def windowRequest (x : _root_.Integration9_9.TM2NativeActivation.Request) : _root_.Integration9_9.TM2NativeGaussianWindow.Request :=
  ((x.1,(_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks x.1).tail),x.2)

@[fun_prop] theorem windowRequest_poly : Poly _root_.Integration9_9.TM2NativeActivation.windowRequest := by unfold _root_.Integration9_9.TM2NativeActivation.windowRequest;fun_prop

@[fun_prop] theorem windowCurrent_poly : Poly _root_.Integration9_9.TM2NativeGaussianWindow.current :=
  ⟨_root_.Integration9_9.TM2NativeGaussianWindow.currentProgram⟩

def current (x : _root_.Integration9_9.TM2NativeActivation.Request) : ℚ :=
  (1/2 : ℚ)*(_root_.Integration9_9.TM2NativeGaussianWindow.current (_root_.Integration9_9.TM2NativeActivation.windowRequest x))[
    (_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks (_root_.Integration9_9.TM2NativeActivation.windowRequest x)).length]?.getD 0

noncomputable def currentProgram : PolyProgram _root_.Integration9_9.TM2NativeActivation.requestCode Ratio.encode _root_.Integration9_9.TM2NativeActivation.current := by
  change PolyProgram Automatic.enc Automatic.enc _
  exact (show Poly _root_.Integration9_9.TM2NativeActivation.current by unfold _root_.Integration9_9.TM2NativeActivation.current;fun_prop).program

 theorem _root_.Integration9_9.TM2NativeActivation.cutoff_eq (x : _root_.Integration9_9.TM2NativeActivation.Request) :
    _root_.Integration9_9.TM2NativeGaussianWindow.cutoff (_root_.Integration9_9.TM2NativeActivation.windowRequest x)=x.1.length.add 2 ^ 2-1 := by
  simp only [_root_.Integration9_9.TM2NativeGaussianWindow.cutoff,_root_.Integration9_9.TM2NativeActivation.windowRequest,List.length_tail,_root_.Integration9_9.TM2NativeGaussianWindow.squareTicks_length]
  exact Nat.min_eq_left (Nat.sub_le _ _)

theorem current_value (x : _root_.Integration9_9.TM2NativeActivation.Request) :
    _root_.Integration9_9.TM2NativeActivation.current x=(_root_.CurrentAFT.BinaryActivationProgram.current x.1.length x.2.length).data.decode := by
  let c := _root_.Integration9_9.TM2NativeGaussianWindow.cutoff (_root_.Integration9_9.TM2NativeActivation.windowRequest x)
  have hi : c<2*c+1 := by omega
  have hc : c=_root_.CurrentAFT.Parameters.sourceCutoff x.1.length-1 := by
    dsimp only [c]
    rw [_root_.Integration9_9.TM2NativeActivation.cutoff_eq]
    simp only [_root_.CurrentAFT.Parameters.sourceCutoff,_root_.CurrentAFT.Parameters.base]
  have hlookup :
      (_root_.Integration9_9.TM2NativeGaussianWindow.current (_root_.Integration9_9.TM2NativeActivation.windowRequest x))[c]?.getD 0 =
      ((_root_.CurrentAFT.CurrentGaussianBits.table x.1.length c x.2.length).data.get ⟨c,hi⟩).decode := by
    rw [_root_.Integration9_9.TM2NativeGaussianWindow.current_eq]
    change (List.ofFn (fun i : Fin (2*c+1) =>
      ((_root_.CurrentAFT.CurrentGaussianBits.table x.1.length c x.2.length).data.get i).decode))[c]?.getD 0 = _
    simp only [List.getElem?_ofFn,hi,↓reduceDIte,Option.getD_some]
  change (1/2 : ℚ)*(_root_.Integration9_9.TM2NativeGaussianWindow.current (_root_.Integration9_9.TM2NativeActivation.windowRequest x))[
    (_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks (_root_.Integration9_9.TM2NativeActivation.windowRequest x)).length]?.getD 0 = _
  rw [_root_.Integration9_9.TM2NativeGaussianWindow.cutoffTicks_length]
  change (1/2 : ℚ)*(_root_.Integration9_9.TM2NativeGaussianWindow.current (_root_.Integration9_9.TM2NativeActivation.windowRequest x))[c]?.getD 0 = _
  rw [hlookup,_root_.CurrentAFT.CurrentGaussianBits.table_value,_root_.AFT.RationalNumerics.windowGaussianWeights,_root_.CurrentAFT.BinaryActivationProgram.zero_index,
    _root_.CurrentAFT.BinaryActivationProgram.current_value,hc]
  rfl



end Integration9_9.TM2NativeActivation

end

section


















































namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.PublicParameterProgram
end CurrentAFT.PublicParameterProgram
namespace CurrentAFT.PublicParameterProgram.checkedPower
end CurrentAFT.PublicParameterProgram.checkedPower
namespace CurrentAFT.PublicParameterProgram.checkedProduct
end CurrentAFT.PublicParameterProgram.checkedProduct
namespace CurrentAFT.PublicParameterProgram.checkedScan
end CurrentAFT.PublicParameterProgram.checkedScan
namespace CurrentAFT.PublicParameterProgram.primeTest
end CurrentAFT.PublicParameterProgram.primeTest
namespace CurrentAFT.PublicParameterProgram.testWork
end CurrentAFT.PublicParameterProgram.testWork
namespace CurrentAFT.PublicParameterProgram.workBound
end CurrentAFT.PublicParameterProgram.workBound
namespace CurrentAFT.PublicParameterProgram.workPolynomial
end CurrentAFT.PublicParameterProgram.workPolynomial



set_option autoImplicit false
set_option maxHeartbeats 2000000
set_option maxRecDepth 4096
set_option backward.isDefEq.respectTransparency false

namespace Integration9_9.TM2NativePrime
open OAI.MetricKMedian.Binary
open OAI.MetricKMedian.Binary.Automatic
open CurrentAFT
open scoped BigOperators

/-- A fixed-degree polynomial unary budget generated from the actual input tape. -/
def powerSlots : ℕ → List Unit → List Unit
  | 0, _ => [()]
  | k+1, xs => xs.flatMap (fun _ => powerSlots k xs)

@[fun_prop] theorem powerSlots_poly (k : ℕ) : Poly (_root_.Integration9_9.TM2NativePrime.powerSlots k) := by
  induction k with
  | zero => exact OAI.MetricKMedian.Binary.Automatic.Poly.const [()]
  | succ k ih =>
    change Poly (fun xs : List Unit => xs.flatMap (fun _ => _root_.Integration9_9.TM2NativePrime.powerSlots k xs))
    apply Poly.list_flatMap
    · exact Poly.comp ih Poly.fst
    · exact Poly.identity

theorem powerSlots_length (k : ℕ) (xs : List Unit) : (_root_.Integration9_9.TM2NativePrime.powerSlots k xs).length = xs.length ^ k := by
  induction k with
  | zero => rfl
  | succ k ih =>
    simp [_root_.Integration9_9.TM2NativePrime.powerSlots, List.length_flatMap, ih, List.map_const, List.sum_replicate, pow_succ, Nat.mul_comm]

attribute [local irreducible] powerSlots

@[fun_prop] theorem nat_mod_poly : Poly (fun x : ℕ × ℕ => x.1 % x.2) := ⟨PolyProgram.natMod⟩

/-- Test all possible proper divisors using an explicit unary budget. -/
def primeTrial (x : List Unit × ℕ) : Bool :=
  decide (2 ≤ x.2) && ((List.range x.1.length).filter
    (fun d => decide (2 ≤ d) && decide (d < x.2) && decide (x.2 % d = 0))).isEmpty

@[fun_prop] theorem primeTrial_poly : Poly _root_.Integration9_9.TM2NativePrime.primeTrial := by
  unfold _root_.Integration9_9.TM2NativePrime.primeTrial
  have htest : Poly (fun x : (List Unit × ℕ) × ℕ =>
      decide (2 ≤ x.2) && decide (x.2 < x.1.2) && decide (x.1.2 % x.2 = 0)) := by
    fun_prop
  have hmod : Poly (fun x : List Unit × ℕ =>
      (List.range x.1.length).filter
        (fun d => decide (2 ≤ d) && decide (d < x.2) && decide (x.2 % d = 0))) := by
    apply Poly.list_filter htest
    exact Poly.comp (f := fun xs : List Unit => List.range xs.length)
      (g := fun x : List Unit × ℕ => x.1) (Poly.list_indices (α := Unit)) Poly.fst
  have h : Poly (fun x : List Unit × ℕ =>
      decide (2 ≤ x.2) && ((List.range x.1.length).filter
        (fun d => decide (2 ≤ d) && decide (d < x.2) && decide (x.2 % d = 0))).isEmpty) := by
    fun_prop
  exact h

theorem primeTrial_correct (xs : List Unit) (n : ℕ) (hn : n ≤ xs.length) :
    _root_.Integration9_9.TM2NativePrime.primeTrial (xs,n) = true ↔ n.Prime := by
  simp only [_root_.Integration9_9.TM2NativePrime.primeTrial, Bool.and_eq_true, decide_eq_true_eq, Bool.not_eq_true_eq_eq_false,
    List.isEmpty_iff, List.filter_eq_nil_iff, Bool.and_eq_false_iff, decide_eq_false_iff_not, List.mem_range]
  rw [Nat.prime_def_lt']
  constructor
  · rintro ⟨h2,ht⟩
    refine ⟨h2, ?_⟩
    intro d hd hdn hdiv
    have hh := ht d (hdn.trans_le hn)
    have hm := Nat.mod_eq_zero_of_dvd hdiv
    simp [hd, hdn, hm] at hh
  · rintro ⟨h2,ht⟩
    refine ⟨h2, ?_⟩
    intro d hd
    by_cases hd2 : 2 ≤ d
    · by_cases hdn : d < n
      · have hm : n % d ≠ 0 := fun h => ht d hd2 hdn (Nat.dvd_of_mod_eq_zero h)
        simp [hd2,hdn,hm]
      · simp [hdn]
    · simp [hd2]

/-- Fixed repetitions are built by repeated doubling rather than unfolding a huge constant list. -/
def doubleSlots : ℕ → List Unit → List Unit
  | 0, xs => xs
  | k + 1, xs => doubleSlots k xs ++ doubleSlots k xs

@[fun_prop] theorem doubleSlots_poly (k : ℕ) : Poly (_root_.Integration9_9.TM2NativePrime.doubleSlots k) := by
  induction k with
  | zero => exact Poly.identity
  | succ k ih =>
    change Poly (fun xs => _root_.Integration9_9.TM2NativePrime.doubleSlots k xs ++ _root_.Integration9_9.TM2NativePrime.doubleSlots k xs)
    fun_prop

theorem doubleSlots_length (k : ℕ) (xs : List Unit) : (_root_.Integration9_9.TM2NativePrime.doubleSlots k xs).length = 2 ^ k * xs.length := by
  induction k with
  | zero => simp [_root_.Integration9_9.TM2NativePrime.doubleSlots]
  | succ k ih =>
    simp only [_root_.Integration9_9.TM2NativePrime.doubleSlots, List.length_append, ih, pow_succ]
    ring

attribute [local irreducible] doubleSlots

/-- The family threshold is materialized from unary security input by a fixed polynomial program. -/
def thresholdSlots (xs : List Unit) : List Unit :=
  _root_.Integration9_9.TM2NativePrime.doubleSlots 16 (_root_.Integration9_9.TM2NativePrime.powerSlots 64 (() :: () :: xs))

@[fun_prop] theorem thresholdSlots_poly : Poly _root_.Integration9_9.TM2NativePrime.thresholdSlots := by
  unfold _root_.Integration9_9.TM2NativePrime.thresholdSlots
  exact Poly.comp (f := _root_.Integration9_9.TM2NativePrime.doubleSlots 16) (g := fun xs => _root_.Integration9_9.TM2NativePrime.powerSlots 64 (() :: () :: xs))
    (_root_.Integration9_9.TM2NativePrime.doubleSlots_poly 16) (Poly.comp (f := _root_.Integration9_9.TM2NativePrime.powerSlots 64) (g := fun xs : List Unit => () :: () :: xs)
      (_root_.Integration9_9.TM2NativePrime.powerSlots_poly 64) (by fun_prop))

theorem thresholdSlots_length (xs : List Unit) :
    (_root_.Integration9_9.TM2NativePrime.thresholdSlots xs).length = _root_.CurrentAFT.Parameters.primeThreshold xs.length := by
  rw [_root_.CurrentAFT.Parameters.primeThreshold_eq]
  simp only [_root_.Integration9_9.TM2NativePrime.thresholdSlots, _root_.Integration9_9.TM2NativePrime.doubleSlots_length, _root_.Integration9_9.TM2NativePrime.powerSlots_length, List.length_cons,
    _root_.CurrentAFT.Parameters.base, show (2 : ℕ) ^ 16 = 65536 by norm_num]

attribute [local irreducible] thresholdSlots

/-- Bertrand's bound provides a genuine polynomial-sized unary search budget. -/
def upperSlots (xs : List Unit) : List Unit := _root_.Integration9_9.TM2NativePrime.thresholdSlots xs ++ _root_.Integration9_9.TM2NativePrime.thresholdSlots xs ++ [()]

@[fun_prop] theorem upperSlots_poly : Poly _root_.Integration9_9.TM2NativePrime.upperSlots := by unfold _root_.Integration9_9.TM2NativePrime.upperSlots; fun_prop

theorem upperSlots_length (xs : List Unit) :
    (_root_.Integration9_9.TM2NativePrime.upperSlots xs).length = 2 * _root_.CurrentAFT.Parameters.primeThreshold xs.length + 1 := by
  simp only [_root_.Integration9_9.TM2NativePrime.upperSlots, List.length_append, List.length_singleton, _root_.Integration9_9.TM2NativePrime.thresholdSlots_length]
  omega

def candidateTest (x : List Unit × ℕ) : Bool :=
  decide ((_root_.Integration9_9.TM2NativePrime.thresholdSlots x.1).length ≤ x.2) && _root_.Integration9_9.TM2NativePrime.primeTrial (_root_.Integration9_9.TM2NativePrime.upperSlots x.1, x.2)

@[fun_prop] theorem candidateTest_poly : Poly _root_.Integration9_9.TM2NativePrime.candidateTest := by unfold _root_.Integration9_9.TM2NativePrime.candidateTest; fun_prop

theorem candidateTest_correct (xs : List Unit) (n : ℕ) (hn : n ≤ (_root_.Integration9_9.TM2NativePrime.upperSlots xs).length) :
    _root_.Integration9_9.TM2NativePrime.candidateTest (xs,n) = true ↔ _root_.CurrentAFT.Parameters.primeThreshold xs.length ≤ n ∧ n.Prime := by
  simp only [_root_.Integration9_9.TM2NativePrime.candidateTest, Bool.and_eq_true, decide_eq_true_eq, _root_.Integration9_9.TM2NativePrime.thresholdSlots_length,
    _root_.Integration9_9.TM2NativePrime.primeTrial_correct (_root_.Integration9_9.TM2NativePrime.upperSlots xs) n hn]

/-- A total bounded divisor scan with no binary-value-to-unary expansion. -/
def modulus (xs : List Unit) : ℕ :=
  (List.range (_root_.Integration9_9.TM2NativePrime.upperSlots xs).length).findIdx (fun n => _root_.Integration9_9.TM2NativePrime.candidateTest (xs,n))

@[fun_prop] theorem modulus_poly : Poly _root_.Integration9_9.TM2NativePrime.modulus := by
  unfold _root_.Integration9_9.TM2NativePrime.modulus
  apply Poly.list_findIdx _root_.Integration9_9.TM2NativePrime.candidateTest_poly
  exact Poly.comp (f := fun xs : List Unit => List.range xs.length)
    (g := _root_.Integration9_9.TM2NativePrime.upperSlots) (Poly.list_indices (α := Unit)) _root_.Integration9_9.TM2NativePrime.upperSlots_poly

theorem modulus_value (xs : List Unit) : _root_.Integration9_9.TM2NativePrime.modulus xs = _root_.CurrentAFT.Parameters.modulus xs.length := by
  have hm : _root_.CurrentAFT.Parameters.modulus xs.length < (List.range (_root_.Integration9_9.TM2NativePrime.upperSlots xs).length).length := by
    rw [List.length_range, _root_.Integration9_9.TM2NativePrime.upperSlots_length]
    exact Nat.lt_succ_of_le (_root_.CurrentAFT.Parameters.modulus_upper xs.length)
  unfold _root_.Integration9_9.TM2NativePrime.modulus
  apply (List.findIdx_eq hm).2
  constructor
  · simp only [List.getElem_range]
    exact (_root_.Integration9_9.TM2NativePrime.candidateTest_correct xs _ (by simpa only [List.length_range] using hm.le)).2
      (_root_.CurrentAFT.Parameters.modulus_spec xs.length)
  · intro j hj
    simp only [List.getElem_range]
    apply Bool.eq_false_iff.mpr
    intro h
    have hb : j ≤ (_root_.Integration9_9.TM2NativePrime.upperSlots xs).length := by
      have hh := hj.trans hm
      simpa only [List.length_range] using hh.le
    obtain ⟨hc,hp⟩ := (_root_.Integration9_9.TM2NativePrime.candidateTest_correct xs j hb).1 h
    exact Nat.not_le_of_gt hj (_root_.CurrentAFT.Parameters.modulus_least xs.length j hc hp)

/-- The real TM2 output agrees exactly with the native sampler's public prime generator. -/
theorem modulus_native (xs : List Unit) : _root_.Integration9_9.TM2NativePrime.modulus xs = (_root_.CurrentAFT.PublicParameterProgram.modulus xs.length).count := by
  rw [_root_.Integration9_9.TM2NativePrime.modulus_value, _root_.CurrentAFT.PublicParameterProgram.modulus_value]

noncomputable def modulusProgram : PolyProgram (encodeList (fun _ : Unit => [])) Nat.bits _root_.Integration9_9.TM2NativePrime.modulus :=
  _root_.Integration9_9.TM2NativePrime.modulus_poly.program

end Integration9_9.TM2NativePrime

end

section
























































namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.BinaryTrimmedPair
end CurrentAFT.BinaryTrimmedPair
namespace CurrentAFT.BinaryTrimmedPair.pairShift
end CurrentAFT.BinaryTrimmedPair.pairShift
namespace CurrentAFT.BinaryTrimmedPair.pairWork
end CurrentAFT.BinaryTrimmedPair.pairWork
namespace CurrentAFT.BinaryTrimmedPair.pairWorkPolynomial
end CurrentAFT.BinaryTrimmedPair.pairWorkPolynomial



set_option autoImplicit false
set_option maxHeartbeats 1000000
set_option maxRecDepth 10000
set_option synthInstance.maxSize 1024
set_option backward.isDefEq.respectTransparency false

/-! Actual signed endpoint and trimmed-pair probability machines. All integer
comparisons, endpoint arithmetic, clipping, and rational inverses execute on
binary encoded operands. These kernels are used inside the Gaussian-weighted
reference-table loops. -/
namespace Integration9_9.TM2NativeSliceProbability
open AFT AFT.RationalNumerics AFT.BinaryArithmetic AFT.V7Calibration
open OAI.MetricKMedian.Binary OAI.MetricKMedian.Binary.Automatic

@[fun_prop] theorem int_add_poly : Poly (fun x : ℤ×ℤ => x.1+x.2) := ⟨PolyProgram.intAdd⟩
@[fun_prop] theorem int_sub_poly : Poly (fun x : ℤ×ℤ => x.1-x.2) := ⟨PolyProgram.intSub⟩
@[fun_prop] theorem int_mul_poly : Poly (fun x : ℤ×ℤ => x.1*x.2) := ⟨PolyProgram.intMul⟩
@[fun_prop] theorem int_neg_poly : Poly (fun z : ℤ => -z) := ⟨PolyProgram.intNeg⟩
@[fun_prop] theorem int_nat_poly : Poly (fun n : ℕ => (n : ℤ)) := ⟨PolyProgram.intOfNat⟩
@[fun_prop] theorem int_lt_poly : Poly (fun x : ℤ×ℤ => decide (x.1<x.2)) := ⟨PolyProgram.intLT⟩
@[fun_prop] theorem int_eq_poly : Poly (fun x : ℤ×ℤ => decide (x.1=x.2)) := ⟨PolyProgram.intEQ⟩

@[fun_prop] theorem int_le_poly : Poly (fun x : ℤ×ℤ => decide (x.1≤x.2)) := by
  exact ⟨(((PolyProgram.exchange Signed.encode Signed.encode).comp PolyProgram.intLT).comp PolyProgram.boolNot).congr _ (fun x => by
    simp only [Function.comp_apply]
    by_cases h : x.1≤x.2
    · simp [h,not_lt.mpr h]
    · simp [h,lt_of_not_ge h])⟩

@[fun_prop] theorem int_lt_comp {α : Type} [Coding α] {f g : α→ℤ}
    (hf : Poly f) (hg : Poly g) : Poly (fun x => decide (f x<g x)) := Poly.comp _root_.Integration9_9.TM2NativeSliceProbability.int_lt_poly (Poly.pair hf hg)
@[fun_prop] theorem int_le_comp {α : Type} [Coding α] {f g : α→ℤ}
    (hf : Poly f) (hg : Poly g) : Poly (fun x => decide (f x≤g x)) := Poly.comp _root_.Integration9_9.TM2NativeSliceProbability.int_le_poly (Poly.pair hf hg)
@[fun_prop] theorem int_eq_comp {α : Type} [Coding α] {f g : α→ℤ}
    (hf : Poly f) (hg : Poly g) : Poly (fun x => decide (f x=g x)) := Poly.comp _root_.Integration9_9.TM2NativeSliceProbability.int_eq_poly (Poly.pair hf hg)
@[fun_prop] theorem decide_and_poly {α : Type} [Coding α] {p q : α→Prop} [DecidablePred p] [DecidablePred q]
    (hp : Poly (fun x => decide (p x))) (hq : Poly (fun x => decide (q x))) :
    Poly (fun x => decide (p x ∧ q x)) := by
  simpa only [Bool.decide_and] using Poly.comp Poly.bool_and (Poly.pair hp hq)
@[fun_prop] theorem rat_inv_poly : Poly (fun q : ℚ => q⁻¹) := ⟨PolyProgram.ratInv⟩

noncomputable def naturalProgram : PolyProgram Signed.encode Nat.bits Int.toNat := by
  let p := PolyProgram.ite PolyProgram.intSign PolyProgram.intAbs (PolyProgram.constant Signed.encode Nat.bits 0)
  exact p.congr _ (fun z => by
    simp only [Signed.sign,decide_eq_true_eq]
    split_ifs with h
    · exact (Int.toNat_eq_zero.mpr h.le).symm
    · exact_mod_cast (Int.natAbs_of_nonneg (le_of_not_gt h)).trans (Int.toNat_of_nonneg (le_of_not_gt h)).symm)

@[fun_prop] theorem int_toNat_poly : Poly Int.toNat := ⟨_root_.Integration9_9.TM2NativeSliceProbability.naturalProgram⟩
@[fun_prop] theorem int_abs_poly : Poly (fun z : ℤ => |z|) :=
  ⟨(PolyProgram.intAbs.comp PolyProgram.intOfNat).congr _ Int.natCast_natAbs⟩

@[fun_prop] theorem int_max_zero_poly : Poly (fun z : ℤ => max z 0) := by
  have h : Poly (fun z : ℤ => if z<0 then 0 else z) := by fun_prop
  convert h using 1
  funext z
  split_ifs with hz
  · exact max_eq_right hz.le
  · exact max_eq_left (le_of_not_gt hz)

@[fun_prop] theorem int_min_zero_poly : Poly (fun z : ℤ => min z 0) := by
  have h : Poly (fun z : ℤ => if z<0 then z else 0) := by fun_prop
  convert h using 1
  funext z
  split_ifs with hz
  · exact min_eq_left hz.le
  · exact min_eq_right (le_of_not_gt hz)

abbrev IntervalRequest := (ℤ×ℕ)×ℤ


def interval (x : _root_.Integration9_9.TM2NativeSliceProbability.IntervalRequest) : ℚ := _root_.AFT.V7Calibration.intervalProbabilityProgram x.1.1 x.1.2 x.2
@[fun_prop] theorem interval_poly : Poly _root_.Integration9_9.TM2NativeSliceProbability.interval := by unfold _root_.Integration9_9.TM2NativeSliceProbability.interval _root_.AFT.V7Calibration.intervalProbabilityProgram;fun_prop


abbrev SliceRequest := (ℤ×ℤ)×(ℤ×ℤ)


def slice (x : _root_.Integration9_9.TM2NativeSliceProbability.SliceRequest) : ℚ := _root_.AFT.V7Calibration.sliceProbabilityProgram x.1.1 x.1.2 x.2.1 x.2.2
@[fun_prop] theorem slice_poly : Poly _root_.Integration9_9.TM2NativeSliceProbability.slice := by unfold _root_.Integration9_9.TM2NativeSliceProbability.slice _root_.AFT.V7Calibration.sliceProbabilityProgram;fun_prop


abbrev PairRequest := (ℕ×ℤ)×((ℤ×ℤ)×(ℕ×(ℕ×ℕ)))


def pair (x : _root_.Integration9_9.TM2NativeSliceProbability.PairRequest) : ℚ :=
  _root_.AFT.V7Calibration.averagedPairProgram x.1.1 x.1.2 x.2.1.1 x.2.1.2 x.2.2.1 x.2.2.2.1 x.2.2.2.2

@[fun_prop] theorem pair_poly : Poly _root_.Integration9_9.TM2NativeSliceProbability.pair := by
  have hinterval : Poly (fun x : _root_.Integration9_9.TM2NativeSliceProbability.PairRequest => _root_.Integration9_9.TM2NativeSliceProbability.interval (((x.2.1.1,x.2.2.1),
    ((x.2.2.2.1 : ℤ)+x.2.2.2.2-((x.1.1 : ℤ)-1))+x.2.1.2))) := by fun_prop
  have hslice : Poly (fun x : _root_.Integration9_9.TM2NativeSliceProbability.PairRequest => _root_.Integration9_9.TM2NativeSliceProbability.slice ((x.1.1,x.1.2),
    ((x.2.2.2.1 : ℤ)+x.2.2.2.2-((x.1.1 : ℤ)-1)),x.2.2.2.1)) := by fun_prop
  change Poly (fun x : _root_.Integration9_9.TM2NativeSliceProbability.PairRequest => _root_.Integration9_9.TM2NativeSliceProbability.interval (((x.2.1.1,x.2.2.1),
    ((x.2.2.2.1 : ℤ)+x.2.2.2.2-((x.1.1 : ℤ)-1))+x.2.1.2)) *
    _root_.Integration9_9.TM2NativeSliceProbability.slice ((x.1.1,x.1.2),((x.2.2.2.1 : ℤ)+x.2.2.2.2-((x.1.1 : ℤ)-1)),x.2.2.2.1))
  fun_prop










end Integration9_9.TM2NativeSliceProbability

end

section




























































namespace CurrentAFT
end CurrentAFT
namespace CurrentAFT.BinaryBodyKernel
end CurrentAFT.BinaryBodyKernel
namespace CurrentAFT.BinaryBodyKernel.baselineWork
end CurrentAFT.BinaryBodyKernel.baselineWork
namespace CurrentAFT.BinaryBodyKernel.baselineWorkPolynomial
end CurrentAFT.BinaryBodyKernel.baselineWorkPolynomial
namespace CurrentAFT.BinaryBodyKernel.sliceWorkPolynomial
end CurrentAFT.BinaryBodyKernel.sliceWorkPolynomial
namespace CurrentAFT.BinaryBodyKernel.translated
end CurrentAFT.BinaryBodyKernel.translated
namespace CurrentAFT.BinaryBodyKernel.translatedWork
end CurrentAFT.BinaryBodyKernel.translatedWork
namespace CurrentAFT.BinaryBodyKernel.translatedWorkPolynomial
end CurrentAFT.BinaryBodyKernel.translatedWorkPolynomial





set_option autoImplicit false
set_option maxHeartbeats 1200000
set_option maxRecDepth 10000
set_option synthInstance.maxSize 1024
set_option backward.isDefEq.respectTransparency false

/-! Genuine Euclidean residues of signed binary integers, followed by the
actual safe-body palette sum. Palette iteration uses a supplied unary buffer;
the current prime's palette buffer is itself generated from the security tape. -/
namespace Integration9_9.TM2NativeBodyKernel
open AFT AFT.RationalNumerics AFT.BinaryArithmetic
open OAI.MetricKMedian.Binary OAI.MetricKMedian.Binary.Automatic
open TM2NativeFiniteSeries TM2NativeSliceProbability
open scoped BigOperators
attribute [local irreducible] TM2NativePrime.modulus TM2NativePrime.upperSlots TM2NativePrime.powerSlots
  TM2NativePrime.thresholdSlots

@[fun_prop] theorem int_natAbs_poly : Poly Int.natAbs := ⟨PolyProgram.intAbs⟩

def emodNatural (x : ℤ×ℕ) : ℕ :=
  let r := x.1.natAbs%x.2
  if x.1<0 then if r=0 then 0 else x.2-r else r

@[fun_prop] theorem emodNatural_poly : Poly _root_.Integration9_9.TM2NativeBodyKernel.emodNatural := by unfold _root_.Integration9_9.TM2NativeBodyKernel.emodNatural;fun_prop

theorem emodNatural_eq (z : ℤ) (q : ℕ) : _root_.Integration9_9.TM2NativeBodyKernel.emodNatural (z,q)=(z%(q : ℤ)).toNat := by
  by_cases hz : z<0
  · simp only [_root_.Integration9_9.TM2NativeBodyKernel.emodNatural,hz,ite_true]
    conv_rhs => rw [Int.eq_neg_natAbs_of_nonpos hz.le,Int.neg_emod]
    simp only [Int.natAbs_natCast,←Int.natCast_mod]
    by_cases hd : q∣z.natAbs
    · have hi : (q : ℤ)∣(z.natAbs : ℤ) := by exact_mod_cast hd
      simp only [hi,ite_true,Int.toNat_zero,Nat.mod_eq_zero_of_dvd hd]
    · have hi : ¬(q : ℤ)∣(z.natAbs : ℤ) := by exact_mod_cast hd
      have hr : z.natAbs%q≠0 := fun h => hd (Nat.dvd_of_mod_eq_zero h)
      simp only [hi,ite_false,hr,Int.toNat_sub]
  · simp only [_root_.Integration9_9.TM2NativeBodyKernel.emodNatural,hz,ite_false]
    conv_rhs => rw [←Int.natAbs_of_nonneg (le_of_not_gt hz),←Int.natCast_mod]
    rfl



abbrev Context := (List Unit×ℕ)×(ℤ×ℕ)
abbrev contextCode : _root_.Integration9_9.TM2NativeBodyKernel.Context→List Bool := Automatic.enc

def term (x : _root_.Integration9_9.TM2NativeBodyKernel.Context×ℕ) : ℚ :=
  if _root_.Integration9_9.TM2NativeBodyKernel.emodNatural ((x.2 : ℤ)+x.1.2.1,x.1.1.1.length)=x.1.2.2 then
    _root_.Integration9_9.TM2NativeSliceProbability.slice (((x.1.1.1.length : ℤ),(x.1.1.2 : ℤ)),((0 : ℤ),(x.2 : ℤ))) else 0
@[fun_prop] theorem term_poly : Poly _root_.Integration9_9.TM2NativeBodyKernel.term := by unfold _root_.Integration9_9.TM2NativeBodyKernel.term;fun_prop

def current (x : _root_.Integration9_9.TM2NativeBodyKernel.Context) : ℚ := ((List.range x.1.1.length).map (fun i => _root_.Integration9_9.TM2NativeBodyKernel.term (x,i))).sum
noncomputable def currentProgram : PolyProgram _root_.Integration9_9.TM2NativeBodyKernel.contextCode Ratio.encode _root_.Integration9_9.TM2NativeBodyKernel.current := by
  let colors : PolyProgram _root_.Integration9_9.TM2NativeBodyKernel.contextCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode (fun x : _root_.Integration9_9.TM2NativeBodyKernel.Context => x.1.1) :=
    (show Poly (fun x : _root_.Integration9_9.TM2NativeBodyKernel.Context => x.1.1) by fun_prop).program
  let indices := colors.comp (PolyProgram.listIndices _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ())
  exact ((((PolyProgram.identity _root_.Integration9_9.TM2NativeBodyKernel.contextCode).fanout indices).comp (_root_.Integration9_9.TM2NativeBodyKernel.term_poly.program.listMapWith 0 (0 : ℚ))).comp PolyProgram.ratSum)

 theorem _root_.Integration9_9.TM2NativeBodyKernel.sum_range (f : ℕ→ℚ) (n : ℕ) : ((List.range n).map f).sum=∑i : Fin n,f i := by
  rw [Fin.sum_univ_eq_sum_range]
  induction n with
  | zero => simp
  | succ n ih => simp only [List.range_succ,List.map_append,List.sum_append,ih,List.map_singleton,List.sum_cons,List.sum_nil,add_zero,Finset.sum_range_succ]

theorem current_value (x : _root_.Integration9_9.TM2NativeBodyKernel.Context) : _root_.Integration9_9.TM2NativeBodyKernel.current x=
    _root_.CurrentAFT.RationalProposalProgram.bodyKernel x.1.1.length x.1.2 x.2.1 x.2.2 := by
  rw [_root_.Integration9_9.TM2NativeBodyKernel.current,_root_.Integration9_9.TM2NativeBodyKernel.sum_range]
  unfold _root_.CurrentAFT.RationalProposalProgram.bodyKernel
  apply Finset.sum_congr rfl
  intro baseline _
  have hq : (x.1.1.length : ℤ)≠0 := by
    have hb:=baseline.isLt
    exact_mod_cast (show x.1.1.length≠0 by omega)
  have hn := Int.emod_nonneg ((baseline.val : ℤ)+x.2.1) hq
  have he : (_root_.Integration9_9.TM2NativeBodyKernel.emodNatural ((baseline.val : ℤ)+x.2.1,x.1.1.length) : ℤ)=
      ((baseline.val : ℤ)+x.2.1)%(x.1.1.length : ℤ) := by
    rw [_root_.Integration9_9.TM2NativeBodyKernel.emodNatural_eq,Int.toNat_of_nonneg hn]
  have hm : _root_.Integration9_9.TM2NativeBodyKernel.emodNatural ((baseline.val : ℤ)+x.2.1,x.1.1.length)=x.2.2 ↔
      ((baseline.val : ℤ)+x.2.1)%(x.1.1.length : ℤ)=(x.2.2 : ℤ) := by
    rw [←he,Int.natCast_inj]
  simp only [_root_.Integration9_9.TM2NativeBodyKernel.term,hm,_root_.Integration9_9.TM2NativeSliceProbability.slice,_root_.CurrentAFT.RationalProposalProgram.safeEntry]






/-- Actual palette buffer for the actual current prime, bounded before taking. -/
def paletteTicks (xs : List Unit) : List Unit := (_root_.Integration9_9.TM2NativePrime.upperSlots xs).take (_root_.Integration9_9.TM2NativePrime.modulus xs)
noncomputable def paletteTicksProgram : PolyProgram _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeFiniteSeries.ticksCode _root_.Integration9_9.TM2NativeBodyKernel.paletteTicks := by
  change PolyProgram Automatic.enc Automatic.enc (fun xs : List Unit =>
    (_root_.Integration9_9.TM2NativePrime.upperSlots xs).take (_root_.Integration9_9.TM2NativePrime.modulus xs))
  have hi : Poly (fun xs : List Unit => (_root_.Integration9_9.TM2NativePrime.upperSlots xs,_root_.Integration9_9.TM2NativePrime.modulus xs)) := by fun_prop
  have ht : Poly (fun x : List Unit×ℕ => x.1.take x.2) := ⟨PolyProgram.listTake _root_.Integration9_9.TM2NativeFiniteSeries.unitCode ()⟩
  exact (Poly.comp (f := fun x : List Unit×ℕ => x.1.take x.2)
    (g := fun xs : List Unit => (_root_.Integration9_9.TM2NativePrime.upperSlots xs,_root_.Integration9_9.TM2NativePrime.modulus xs)) ht hi).program
@[fun_prop] theorem paletteTicks_poly : Poly _root_.Integration9_9.TM2NativeBodyKernel.paletteTicks := ⟨_root_.Integration9_9.TM2NativeBodyKernel.paletteTicksProgram⟩

theorem paletteTicks_length (xs : List Unit) : (_root_.Integration9_9.TM2NativeBodyKernel.paletteTicks xs).length=_root_.CurrentAFT.Parameters.modulus xs.length := by
  simp only [_root_.Integration9_9.TM2NativeBodyKernel.paletteTicks,List.length_take,_root_.Integration9_9.TM2NativePrime.modulus_value,_root_.Integration9_9.TM2NativePrime.upperSlots_length]
  apply Nat.min_eq_left
  exact Nat.le_succ_of_le (_root_.CurrentAFT.Parameters.modulus_upper xs.length)

end Integration9_9.TM2NativeBodyKernel

end
Source
Companion Lean implementation for the fixed-family Gaussian affine transport theorem. The source modules and their SHA-256 hashes are recorded in the checked extraction manifest. This exact generated file has SHA-256 dbb3c3ca2947702b40282eed380c81e114b9aca6b626a4f452c35f2c824a1a7d. Source modules: Integration9_9_CurrentZeroFiber, Integration9_9_CurrentAffineLaw, Integration9_9_FairBitPadding, Integration9_9_ActualSamplerLaw, Integration9_9_Parameters, Integration9_9_FixedFamilySamplerLaw, Integration9_9_TM2OutputLength, Integration9_9_TM2Simulation, Integration9_9_TM2Composition, Integration9_9_TM2BinaryAddition, Integration9_9_TM2AdditionFrontend, Integration9_9_TM2PairUnframe, Integration9_9_TM2BinaryComparison, Integration9_9_TM2Canonical, Integration9_9_TM2OutputEncoding, Integration9_9_TM2DynamicIntegerEncoding, Integration9_9_TM2NativeArithmetic, Integration9_9_TM2NativeFiniteSeries, Integration9_9_TM2NativeGaussian, Integration9_9_TM2NativeGaussianWindow, Integration9_9_TM2NativeActivation, Integration9_9_TM2NativePrime, Integration9_9_TM2NativeSliceProbability, Integration9_9_TM2NativeBodyKernel. Alternate sparse native dependencies preserve the original non-import body bytes and all public types; this header-only packaging is recorded in sparse_live/manifest.json. Current source SHA256 81cb9c1fbe604ed21a36a63555f91d514c774609dc29da68f714b061bb4a704f. Native10/11 split fallback keeps every original scope-closed data body unchanged and retains the canonical module names as import facades. Current source SHA256 81cb9c1fbe604ed21a36a63555f91d514c774609dc29da68f714b061bb4a704f.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me