import Tablet.DispatchContracts import Tablet.P9_T1_NormalComponentRefinement import Tablet.P13_T1_NumberHelperAcceptance import Tablet.P13_T1_NumberHelperRejection import Tablet.P13_T1_NumberHelperRecordBounds import Tablet.P13_T1_NumberHelperAccumulatedValue import Tablet.P13_T1_NativeCorrectOrDefer import Tablet.IntegerCorrectOrDefer import Tablet.P13_T1_SlowAccumulatedRounding import Tablet.SpecialHelperInfinity import Tablet.SpecialHelperNan import Tablet.SpecialHelperRejection import Tablet.AssemblyCorrespondence import Tablet.ClassificationDeterminism import Tablet.RoundingTotality import Tablet.P616_T1_NegationWordLaw import Tablet.P616_T1_ExactTransportApplicationsB open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P616_T1_CompatibleCompletion] theorem P616_T1_CompatibleCompletion : exists C : P9_T1_AbstractComponents, DispatchContracts C ∧ P9_T1_NormalComponentRefinement C := by -- BODY classical let observed {α : Type} (r : Result (Option α)) : Option α := match r with | .ok x => x | .fail _ => none | .div => none let observedPair {α : Type} (r : Result α) (fallback : α) : α := match r with | .ok x => x | .fail _ => fallback | .div => fallback have observed_ok_some {α : Type} {r : Result (Option α)} {a : α} (h : r = .ok (some a)) : observed r = some a := by rw [h] have observed_ok_none {α : Type} {r : Result (Option α)} (h : r = .ok none) : observed r = none := by rw [h] have observedPair_ok {α : Type} {r : Result α} {fallback a : α} (h : r = .ok a) : observedPair r fallback = a := by rw [h] let defaultFp : num.imp.dec2flt.common.BiasedFp := ⟨0#u64, (-1)#i32⟩ let chooseSlow (body : Slice Std.U8) : num.imp.dec2flt.common.BiasedFp := if h : ∃ x : DecimalLexeme, x.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric x ∧ DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes body then let x := Classical.choose h let hx := Classical.choose_spec h let hn : 0 ≤ DecimalGoalVocabularyDefault.accumulatedMagnitude x := by dsimp [DecimalGoalVocabularyDefault] positivity let w := Classical.choose (RoundingTotality (DecimalGoalVocabularyDefault.accumulatedMagnitude x) hn) have hw := Classical.choose_spec (RoundingTotality (DecimalGoalVocabularyDefault.accumulatedMagnitude x) hn) ⟨UScalar.ofNatCore w.val (by change w.val < 2 ^ 64 have : w.val ≤ 2047 * 2 ^ 52 := by have hh := hw change 0 ≤ _ ∧ w.val ≤ 2047 * 2 ^ 52 ∧ _ at hh exact hh.2.1 norm_num at this ⊢ omega), 0#i32⟩ else defaultFp let C : P9_T1_AbstractComponents := { parseNumber := fun body => observed (P9_T1_ActualDispatchOperations.parseNumber body) parseSpecial := fun body negative => observed (P9_T1_ActualDispatchOperations.parseSpecial body negative) native := fun d => observed (P9_T1_ActualDispatchOperations.native d) integer := fun q w => observedPair (P9_T1_ActualDispatchOperations.integer q w) defaultFp slow := fun body => observedPair (P9_T1_ActualDispatchOperations.slow body) (chooseSlow body) assemble := fun fp => Classical.choose (AssemblyCorrespondence fp) negate := fun f => Classical.choose (P616_T1_NegationWordLaw f) } refine ⟨C, ?_, ?_⟩ · constructor · intro body x hsource hne hlen hsign hvalid hrender obtain ⟨d, hd⟩ := P13_T1_NumberHelperAcceptance body x hsource hne hlen hsign hvalid hrender refine ⟨d, ?_⟩ change observed (P9_T1_ActualDispatchOperations.parseNumber body) = some d exact observed_ok_some hd · intro body hsource hne hlen hbad have hd := P13_T1_NumberHelperRejection body hsource hne hlen hbad change observed (P9_T1_ActualDispatchOperations.parseNumber body) = none exact observed_ok_none hd · intro body d hsource hne hparse simp only [C] at hparse cases hp : P9_T1_ActualDispatchOperations.parseNumber body with | fail e => simp [observed, hp] at hparse | div => simp [observed, hp] at hparse | ok r => simp [observed, hp] at hparse cases hr : r with | none => simp [hr] at hparse | some d' => have heq : d' = d := Option.some.inj (by simpa [hr] using hparse) subst d exact P13_T1_NumberHelperRecordBounds body d' hsource hne (by simpa [hp, hr]) · intro body x d hsource hlen hsign hvalid hrender hparse simp only [C] at hparse cases hp : P9_T1_ActualDispatchOperations.parseNumber body with | fail e => simp [observed, hp] at hparse | div => simp [observed, hp] at hparse | ok r => simp [observed, hp] at hparse cases hr : r with | none => simp [hr] at hparse | some d' => have heq : d' = d := Option.some.inj (by simpa [hr] using hparse) subst d exact P13_T1_NumberHelperAccumulatedValue body x d' hsource hlen hsign hvalid hrender (by simpa [hp, hr]) · intro body x hsource hlen hsign hvalid hrender target exact (P616_T1_ExactTransportApplicationsB.1 body x hsource hlen hsign hvalid hrender target) · intro body negative hsource hinf obtain ⟨f, hf, hvalue⟩ := SpecialHelperInfinity body negative hinf refine ⟨f, ?_, hvalue⟩ change observed (P9_T1_ActualDispatchOperations.parseSpecial body negative) = some f exact observed_ok_some (by simpa [P9_T1_ActualDispatchOperations] using hf) · intro body negative hsource hnan obtain ⟨f, hf, hvalue⟩ := SpecialHelperNan body negative hnan refine ⟨f, ?_, hvalue⟩ change observed (P9_T1_ActualDispatchOperations.parseSpecial body negative) = some f exact observed_ok_some (by simpa [P9_T1_ActualDispatchOperations] using hf) · intro body negative hsource hbad have hd := SpecialHelperRejection body negative hsource (fun hi => hbad ⟨false, hi⟩) (fun hn => hbad ⟨true, hn⟩) change observed (P9_T1_ActualDispatchOperations.parseSpecial body negative) = none exact observed_ok_none (by simpa [P9_T1_ActualDispatchOperations] using hd) · intro d f hnative simp only [C] at hnative cases hp : P9_T1_ActualDispatchOperations.native d with | fail e => simp [observed, hp] at hnative | div => simp [observed, hp] at hnative | ok r => simp [observed, hp] at hnative cases hr : r with | none => simp [hr] at hnative | some f' => have heq : f' = f := Option.some.inj (by simpa [hr] using hnative) subst f rcases P13_T1_NativeCorrectOrDefer d r (by simpa [hp] ) with hnone | ⟨f, magnitude, hret, hmany, hround, hsign⟩ · simp [hr] at hnone · simp [hr] at hret subst f exact ⟨hmany, ⟨magnitude, hround, hsign⟩⟩ · intro q w dsimp [C, observedPair] cases hp : P9_T1_ActualDispatchOperations.integer q w with | fail e => simp [hp, defaultFp] | div => simp [hp, defaultFp] | ok fp => change (fp.p_biased.val < 0 ∨ (DecimalGoalVocabularyDefault.roundsMagnitude ((w.val : Real) * (10 : Real) ^ q.val) (C.assemble fp) ∧ (C.assemble fp).val < 2 ^ 63)) rcases IntegerCorrectOrDefer q w fp (by simpa [P9_T1_ActualDispatchOperations] using hp) with hdefer | ⟨target, hassembled, hround⟩ · exact Or.inl hdefer · have ha := AssemblyCorrespondence fp let f : F64 := Classical.choose ha have hword : DecimalGoalVocabularyDefault.assembledWord fp f := Classical.choose_spec ha |>.2 have hEq : C.assemble fp = f := by dsimp [C] have hval : target.val = f.val := by change target.val = Nat.lor fp.m.val (((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)) at hassembled change f.val = Nat.lor fp.m.val (((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)) at hword omega have htf : target = f := by apply UScalar.eq_of_val_eq exact hval rw [htf] at hround rw [hEq] refine Or.inr ⟨hround, ?_⟩ have hr := hround change 0 ≤ ((w.val : Real) * (10 : Real) ^ q.val) ∧ f.val ≤ 2047 * 2 ^ 52 ∧ _ at hr have hi : 2047 * 2 ^ 52 < 2 ^ 63 := by norm_num omega · intro body x hsource hsign hvalid hrender -- The actual observation is used when available; otherwise the selected -- canonical rounded fallback has exponent zero. change DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) (C.assemble (observedPair (P9_T1_ActualDispatchOperations.slow body) (chooseSlow body))) ∧ (C.assemble (observedPair (P9_T1_ActualDispatchOperations.slow body) (chooseSlow body))).val < 2 ^ 63 cases hp : P9_T1_ActualDispatchOperations.slow body with | ok fp => simp [observedPair, hp] obtain ⟨magnitude, hword, hround⟩ := P13_T1_SlowAccumulatedRounding body x fp hsource hsign hvalid hrender hp have hEq : C.assemble fp = magnitude := by change Classical.choose (AssemblyCorrespondence fp) = magnitude let f := Classical.choose (AssemblyCorrespondence fp) have hassembled := Classical.choose_spec (AssemblyCorrespondence fp) |>.2 have : f = magnitude := by unfold DecimalGoalVocabularyDefault at hword hassembled dsimp at hword hassembled apply UScalar.eq_of_val_eq exact hassembled.trans hword.symm exact this rw [hEq] have hclear := hround change 0 ≤ DecimalGoalVocabularyDefault.accumulatedMagnitude x ∧ magnitude.val ≤ 2047 * 2 ^ 52 ∧ _ at hclear exact ⟨hround, by omega⟩ | fail e => have hlex : ∃ y : DecimalLexeme, y.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric y ∧ DecimalGoalVocabularyDefault.renderNumeric y = DecimalGoalVocabularyDefault.bytes body := ⟨x, hsign, hvalid, hrender⟩ simp only [observedPair, hp] let y := Classical.choose hlex have hy := Classical.choose_spec hlex have hsame : y = x := by apply (ClassificationDeterminism.2 (DecimalGoalVocabularyDefault.bytes body)) · intro b hb dsimp [DecimalGoalVocabularyDefault] at hb obtain ⟨byte, hmem, rfl⟩ := List.mem_map.mp hb exact byte.bv.isLt · exact ⟨hy.2.1, hy.2.2⟩ · exact ⟨hvalid, hrender⟩ subst y let hn : 0 ≤ DecimalGoalVocabularyDefault.accumulatedMagnitude x := by dsimp [DecimalGoalVocabularyDefault] positivity let w := Classical.choose (RoundingTotality (DecimalGoalVocabularyDefault.accumulatedMagnitude x) hn) have hw := Classical.choose_spec (RoundingTotality (DecimalGoalVocabularyDefault.accumulatedMagnitude x) hn) let fp' := chooseSlow body have hassemble : C.assemble fp' = w := by have ha := AssemblyCorrespondence fp' let f := Classical.choose ha have hword := Classical.choose_spec ha |>.2 have hv : f.val = w.val := by change f.val = Nat.lor fp'.m.val (((fp'.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)) at hword simp only [fp', chooseSlow, dif_pos hlex] at hword have hpzero : (0#i32).val = 0 := by decide rw [hpzero] at hword norm_num at hword rw [hsame] at hword change f.val = w.val at hword exact hword have hf : f = w := by apply UScalar.eq_of_val_eq exact hv change Classical.choose (AssemblyCorrespondence fp') = w exact hf change DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) (C.assemble (chooseSlow body)) ∧ (C.assemble (chooseSlow body)).val < 2 ^ 63 rw [hassemble] have hr := hw change 0 ≤ DecimalGoalVocabularyDefault.accumulatedMagnitude x ∧ w.val ≤ 2047 * 2 ^ 52 ∧ _ at hr have hi : 2047 * 2 ^ 52 < 2 ^ 63 := by norm_num exact ⟨hw, by omega⟩ | div => have hlex : ∃ y : DecimalLexeme, y.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric y ∧ DecimalGoalVocabularyDefault.renderNumeric y = DecimalGoalVocabularyDefault.bytes body := ⟨x, hsign, hvalid, hrender⟩ simp only [observedPair, hp] let y := Classical.choose hlex have hy := Classical.choose_spec hlex have hsame : y = x := by apply (ClassificationDeterminism.2 (DecimalGoalVocabularyDefault.bytes body)) · intro b hb dsimp [DecimalGoalVocabularyDefault] at hb obtain ⟨byte, hmem, rfl⟩ := List.mem_map.mp hb exact byte.bv.isLt · exact ⟨hy.2.1, hy.2.2⟩ · exact ⟨hvalid, hrender⟩ subst y let hn : 0 ≤ DecimalGoalVocabularyDefault.accumulatedMagnitude x := by dsimp [DecimalGoalVocabularyDefault] positivity let w := Classical.choose (RoundingTotality (DecimalGoalVocabularyDefault.accumulatedMagnitude x) hn) have hw := Classical.choose_spec (RoundingTotality (DecimalGoalVocabularyDefault.accumulatedMagnitude x) hn) let fp' := chooseSlow body have hassemble : C.assemble fp' = w := by have ha := AssemblyCorrespondence fp' let f := Classical.choose ha have hword := Classical.choose_spec ha |>.2 have hv : f.val = w.val := by change f.val = Nat.lor fp'.m.val (((fp'.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)) at hword simp only [fp', chooseSlow, dif_pos hlex] at hword have hpzero : (0#i32).val = 0 := by decide rw [hpzero] at hword norm_num at hword rw [hsame] at hword change f.val = w.val at hword exact hword have hf : f = w := by apply UScalar.eq_of_val_eq exact hv change Classical.choose (AssemblyCorrespondence fp') = w exact hf change DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) (C.assemble (chooseSlow body)) ∧ (C.assemble (chooseSlow body)).val < 2 ^ 63 rw [hassemble] have hr := hw change 0 ≤ DecimalGoalVocabularyDefault.accumulatedMagnitude x ∧ w.val ≤ 2047 * 2 ^ 52 ∧ _ at hr have hi : 2047 * 2 ^ 52 < 2 ^ 63 := by norm_num exact ⟨hw, by omega⟩ · intro fp exact Classical.choose_spec (AssemblyCorrespondence fp) |>.2 · intro f exact Classical.choose_spec (P616_T1_NegationWordLaw f) |>.2 · constructor · intro body out hsource hne hnormal simp only [C] cases hp : P9_T1_ActualDispatchOperations.parseNumber body with | ok r => simp [observed, hp] at hnormal ⊢; exact hnormal.symm | fail e => simp [hp] at hnormal | div => simp [hp] at hnormal · intro body hsource hne hlen by_cases hnum : ∃ x : DecimalLexeme, x.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric x ∧ DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes body · obtain ⟨x, hsign, hvalid, hrender⟩ := hnum obtain ⟨d, hd⟩ := P13_T1_NumberHelperAcceptance body x hsource hne hlen hsign hvalid hrender change P9_T1_ActualDispatchOperations.parseNumber body = ok (observed (P9_T1_ActualDispatchOperations.parseNumber body)) rw [hd] · have hd := P13_T1_NumberHelperRejection body hsource hne hlen hnum change P9_T1_ActualDispatchOperations.parseNumber body = ok (observed (P9_T1_ActualDispatchOperations.parseNumber body)) rw [hd] · intro body negative hsource by_cases hinf : DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes body) none false · obtain ⟨f, hf, _⟩ := SpecialHelperInfinity body negative hinf change P9_T1_ActualDispatchOperations.parseSpecial body negative = ok (observed (P9_T1_ActualDispatchOperations.parseSpecial body negative)) have hf' : P9_T1_ActualDispatchOperations.parseSpecial body negative = ok (some f) := by simpa [P9_T1_ActualDispatchOperations] using hf have ho := observed_ok_some (r := P9_T1_ActualDispatchOperations.parseSpecial body negative) (a := f) hf' calc P9_T1_ActualDispatchOperations.parseSpecial body negative = ok (some f) := hf' _ = ok (observed (P9_T1_ActualDispatchOperations.parseSpecial body negative)) := congrArg Result.ok ho.symm · by_cases hnan : DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes body) none true · obtain ⟨f, hf, _⟩ := SpecialHelperNan body negative hnan change P9_T1_ActualDispatchOperations.parseSpecial body negative = ok (observed (P9_T1_ActualDispatchOperations.parseSpecial body negative)) have hf' : P9_T1_ActualDispatchOperations.parseSpecial body negative = ok (some f) := by simpa [P9_T1_ActualDispatchOperations] using hf have ho := observed_ok_some (r := P9_T1_ActualDispatchOperations.parseSpecial body negative) (a := f) hf' calc P9_T1_ActualDispatchOperations.parseSpecial body negative = ok (some f) := hf' _ = ok (observed (P9_T1_ActualDispatchOperations.parseSpecial body negative)) := congrArg Result.ok ho.symm · have hf := SpecialHelperRejection body negative hsource hinf hnan change P9_T1_ActualDispatchOperations.parseSpecial body negative = ok (observed (P9_T1_ActualDispatchOperations.parseSpecial body negative)) have hf' : P9_T1_ActualDispatchOperations.parseSpecial body negative = ok none := by simpa [P9_T1_ActualDispatchOperations] using hf have ho := observed_ok_none (r := P9_T1_ActualDispatchOperations.parseSpecial body negative) hf' calc P9_T1_ActualDispatchOperations.parseSpecial body negative = ok none := hf' _ = ok (observed (P9_T1_ActualDispatchOperations.parseSpecial body negative)) := congrArg Result.ok ho.symm · intro d out hnormal cases hp : P9_T1_ActualDispatchOperations.native d with | ok r => simp [C, observed, hp] at hnormal ⊢; exact hnormal.symm | fail e => simp [hp] at hnormal | div => simp [hp] at hnormal · intro q w out hnormal cases hp : P9_T1_ActualDispatchOperations.integer q w with | ok fp => simp [C, observedPair, hp] at hnormal ⊢; exact hnormal.symm | fail e => simp [hp] at hnormal | div => simp [hp] at hnormal · intro body x out hsource hsign hvalid hrender hnormal cases hp : P9_T1_ActualDispatchOperations.slow body with | ok fp => simp [C, observedPair, hp] at hnormal ⊢; exact hnormal.symm | fail e => simp [hp] at hnormal | div => simp [hp] at hnormal · intro fp dsimp [C] exact Classical.choose_spec (AssemblyCorrespondence fp) |>.1 · intro f dsimp [C] exact Classical.choose_spec (P616_T1_NegationWordLaw f) |>.1 end dec2flt_model