import Tablet.P661_T1_PrefixRealization import Tablet.P9_T1_DispatchFrame import Tablet.P9_T1_EmbedComponents import Tablet.ModelStrAsBytes import Tablet.ModelSliceFirst import Tablet.DispatchContracts import Tablet.P9_T1_AbstractComponents import Tablet.P9_T1_NormalComponentRefinement import Tablet.P9_T1_ReachedDispatchCall import Tablet.P9_T1_ActualDispatchOperations import Tablet.DecimalGoalVocabularyDefault open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P687_T1_ActualEmbeddedFrameEquality] theorem P687_T1_ActualEmbeddedFrameEquality (C : P9_T1_AbstractComponents) (hc : DispatchContracts C) (hr : P9_T1_NormalComponentRefinement C) (s : Str) (hs : DecimalGoalVocabularyDefault.sourceString s) (hA : s.val.length <= 3458764513820540928) : P9_T1_DispatchFrame P9_T1_ActualDispatchOperations s = P9_T1_DispatchFrame (P9_T1_EmbedComponents C) s := by -- BODY classical have realize := P661_T1_PrefixRealization C hc hr s hs hA have finish {α : Type} (body : Slice Std.U8) (c : Std.U8) (d : num.imp.dec2flt.decimal.Decimal) (selected : num.imp.dec2flt.common.BiasedFp) (reach : P9_T1_ReachedDispatchCall s (.assemble body (c = 45#u8) d selected)) (kn : F64 → F64 → Result α) (kp : F64 → Result α) : (do let magnitude ← P9_T1_ActualDispatchOperations.assemble selected if c = 45#u8 then do let result ← P9_T1_ActualDispatchOperations.negate magnitude kn magnitude result else kp magnitude) = (if c = 45#u8 then kn (C.assemble selected) (C.negate (C.assemble selected)) else kp (C.assemble selected)) := by have ha := realize (.assemble body (c = 45#u8) d selected) reach rw [ha, bind_tc_ok] by_cases hm : c = 45#u8 · have reach' : P9_T1_ReachedDispatchCall s (.assemble body true d selected) := by simpa only [hm, decide_true] using reach have rn := P9_T1_ReachedDispatchCall.negate body d selected (C.assemble selected) reach' ha have hn := (realize (.negate body d selected (C.assemble selected)) rn).2 simp only [hm, if_true, hn, bind_tc_ok] · simp only [hm, if_false] have select_transport {α : Type} (body : Slice Std.U8) (c : Std.U8) (d : num.imp.dec2flt.decimal.Decimal) (post : num.imp.dec2flt.common.BiasedFp) (reach : P9_T1_ReachedDispatchCall s (.post body (c = 45#u8) d post)) (ka ke : (num.imp.dec2flt.common.BiasedFp × Bool × Option num.imp.dec2flt.common.BiasedFp) → Result α) (hk : forall selected guard slow, P9_T1_ReachedDispatchCall s (.assemble body (c = 45#u8) d selected) → ka (selected, guard, slow) = ke (selected, guard, slow)) : Bind.bind ( if post.p_biased < 0#i32 then do let replacement ← P9_T1_ActualDispatchOperations.slow body ok (replacement, true, some replacement) else ok (post, false, none)) ka = Bind.bind ( if post.p_biased < 0#i32 then ok (C.slow body, true, some (C.slow body)) else ok (post, false, none)) ke := by by_cases hp : post.p_biased < 0#i32 · have hval : post.p_biased.val < 0 := by simpa using hp have rs := P9_T1_ReachedDispatchCall.slow body (c = 45#u8) d post reach hval have hslow := (realize (.slow body (c = 45#u8) d post) rs).2.2.2.2 have ra := P9_T1_ReachedDispatchCall.assemble_slow body (c = 45#u8) d post (C.slow body) rs hslow simp only [hp, if_true, hslow, bind_tc_ok] exact hk _ _ _ ra · have hval : 0 <= post.p_biased.val := by have : ¬ post.p_biased.val < 0 := by simpa using hp omega have ra := P9_T1_ReachedDispatchCall.assemble_direct body (c = 45#u8) d post reach hval simp only [hp, if_false, bind_tc_ok] exact hk _ _ _ ra unfold P9_T1_DispatchFrame rw [bind_eq_iff] intro bytes hbytes have heq : bytes = s := by simpa only [core.str.Str.as_bytes, ModelStrAsBytes, Result.ok.injEq] using hbytes.symm subst bytes rw [bind_eq_iff] intro head hhead have heq : head = s.val.head? := by simpa only [core.slice.Slice.first, ModelSliceFirst, Result.ok.injEq] using hhead.symm subst head cases hlist : s.val with | nil => rfl | cons c tail => simp only [List.head?_cons] rw [bind_eq_iff] intro body hbody have hindex : core.slice.index.Slice.index (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s { start := 1#usize } = ok (s.drop 1#usize) := by simp [Slice.index_SliceIndexRangeFromUsizeSliceInst, core.slice.index.SliceIndexRangeFromUsizeSlice.index, hlist] have hbodyval : body.val = if c = 45#u8 ∨ c = 43#u8 then tail else s.val := by by_cases hm : c = 45#u8 · simp [hm, hindex] at hbody subst body simp [Slice.drop, hlist, hm] · by_cases hp : c = 43#u8 · simp [hp, hindex] at hbody subst body simp [Slice.drop, hlist, hp] · simp only [hm, hp, ite_false, Result.ok.injEq] at hbody subst body simp [hm, hp] rw [bind_eq_iff] intro empty hempty have heq : empty = decide (body.val = []) := by simpa only [core.slice.Slice.is_empty, Slice.length, List.length_eq_zero_iff, Result.ok.injEq] using hempty.symm subst empty by_cases hne : body.val = [] · simp only [hne, decide_true, ite_true] · simp only [hne, decide_false, Bool.false_eq_true, ite_false] have rn := P9_T1_ReachedDispatchCall.number c tail body hlist hbodyval hne have hn := (realize (.number body (c = 45#u8)) rn).2.2.2 rw [hn] simp only [P9_T1_EmbedComponents, bind_tc_ok] cases hnum : C.parseNumber body with | none => dsimp only have rn0 : P9_T1_ActualDispatchOperations.parseNumber body = ok none := hn.trans (by rw [hnum]) have rs := P9_T1_ReachedDispatchCall.special body (c = 45#u8) rn rn0 have hsp := (realize (.special body (c = 45#u8)) rs).2.2.2 rw [hsp, bind_tc_ok] | some d => dsimp only have rn1 : P9_T1_ActualDispatchOperations.parseNumber body = ok (some d) := hn.trans (by rw [hnum]) have rnative := P9_T1_ReachedDispatchCall.native body (c = 45#u8) d rn rn1 have hnat := (realize (.native body (c = 45#u8) d) rnative).2.2.2.2.2 rw [hnat, bind_tc_ok] cases hnative : C.native { d with negative := (c = 45#u8) } with | some value => rfl | none => dsimp only have rnat0 : P9_T1_ActualDispatchOperations.native { d with negative := (c = 45#u8) } = ok none := hnat.trans (by rw [hnative]) have rfirst := P9_T1_ReachedDispatchCall.first body (c = 45#u8) d rnative rnat0 have hf := (realize (.first body (c = 45#u8) d) rfirst).2.2.2.2 rw [hf, bind_tc_ok] rw [bind_eq_iff] rintro ⟨many1, many2, first, fp, secondGuard⟩ hstage cases hd : d.many_digits with | false => simp only [hd, Bool.false_eq_true, if_false] at hstage cases hstage simp only [Aeneas.Std.uncurry] simp only [Bool.false_eq_true, if_false] rw [bind_eq_iff] rintro ⟨post, secondExponent, secondMantissa, secondResult, secondEqual, forced⟩ hpoststage cases hpoststage simp only [Aeneas.Std.uncurry] have rpost := P9_T1_ReachedDispatchCall.post_direct body (c = 45#u8) d (C.integer d.exponent d.mantissa) rfirst hf (by simp [hd]) refine select_transport body c d (C.integer d.exponent d.mantissa) rpost _ _ ?_ intro selected guard slow reach simp only [Aeneas.Std.uncurry] repeat' (rw [bind_eq_iff]; intro _ _) exact finish body c d selected reach _ _ | true => simp only [hd, if_true] at hstage cases hstage simp only [Aeneas.Std.uncurry] by_cases hp : (C.integer d.exponent d.mantissa).p_biased >= 0#i32 · simp only [hp, decide_true, if_true] have hval : 0 <= (C.integer d.exponent d.mantissa).p_biased.val := by simpa using hp have ri := P9_T1_ReachedDispatchCall.increment body (c = 45#u8) d (C.integer d.exponent d.mantissa) rfirst hf ⟨hd, hval⟩ obtain ⟨next, hadd, hnext⟩ := (realize (.increment body (c = 45#u8) d (C.integer d.exponent d.mantissa)) ri).2 simp only [bind_assoc_eq, hadd, bind_tc_ok] have rsecond := P9_T1_ReachedDispatchCall.second body (c = 45#u8) d (C.integer d.exponent d.mantissa) next ri hadd have hsecond := (realize (.second body (c = 45#u8) d (C.integer d.exponent d.mantissa) next) rsecond).2 simp only [hsecond, bind_tc_ok] have rcompare := P9_T1_ReachedDispatchCall.compare body (c = 45#u8) d (C.integer d.exponent d.mantissa) (C.integer d.exponent next) next rsecond hsecond have hcompare := realize (.compare body (c = 45#u8) d (C.integer d.exponent d.mantissa) (C.integer d.exponent next)) rcompare simp only [hcompare, bind_tc_ok] rw [bind_eq_iff] rintro ⟨post, forced⟩ hpair simp only [Aeneas.Std.uncurry] have hpost : post = (if decide ((C.integer d.exponent d.mantissa).m = (C.integer d.exponent next).m ∧ (C.integer d.exponent d.mantissa).p_biased = (C.integer d.exponent next).p_biased) then C.integer d.exponent d.mantissa else { C.integer d.exponent d.mantissa with p_biased := (-1)#i32 }) := by generalize heq : decide ((C.integer d.exponent d.mantissa).m = (C.integer d.exponent next).m ∧ (C.integer d.exponent d.mantissa).p_biased = (C.integer d.exponent next).p_biased) = equal at hpair ⊢ cases equal <;> simp only [Bool.false_eq_true, if_false, if_true, Result.ok.injEq, Prod.mk.injEq] at hpair ⊢ <;> exact hpair.1.symm have rpost' := P9_T1_ReachedDispatchCall.post_compared body (c = 45#u8) d (C.integer d.exponent d.mantissa) (C.integer d.exponent next) (decide ((C.integer d.exponent d.mantissa).m = (C.integer d.exponent next).m ∧ (C.integer d.exponent d.mantissa).p_biased = (C.integer d.exponent next).p_biased)) rcompare hcompare have rpost : P9_T1_ReachedDispatchCall s (.post body (c = 45#u8) d post) := hpost ▸ rpost' rw [bind_eq_iff] rintro ⟨post2, secondExponent, secondMantissa, secondResult, secondEqual, forced2⟩ hpoststage cases hpoststage simp only [Aeneas.Std.uncurry] refine select_transport body c d post rpost _ _ ?_ intro selected guard slow reach simp only [Aeneas.Std.uncurry] repeat' (rw [bind_eq_iff]; intro _ _) exact finish body c d selected reach _ _ · simp only [hp, decide_false, Bool.false_eq_true, if_false] rw [bind_eq_iff] rintro ⟨post, secondExponent, secondMantissa, secondResult, secondEqual, forced⟩ hpoststage cases hpoststage simp only [Aeneas.Std.uncurry] have hval : ¬ 0 <= (C.integer d.exponent d.mantissa).p_biased.val := by simpa using hp have rpost := P9_T1_ReachedDispatchCall.post_direct body (c = 45#u8) d (C.integer d.exponent d.mantissa) rfirst hf (by simp only [hd, true_and]; exact hval) refine select_transport body c d (C.integer d.exponent d.mantissa) rpost _ _ ?_ intro selected guard slow reach simp only [Aeneas.Std.uncurry] repeat' (rw [bind_eq_iff]; intro _ _) exact finish body c d selected reach _ _ end dec2flt_model