import Tablet.DispatchContracts import Tablet.P9_T1_DispatchFrame import Tablet.P9_T1_EmbedComponents import Tablet.P616_T1_SignStrippedClassification import Tablet.P616_T1_RoundingCellConvexity import Tablet.ClassificationDeterminism import Tablet.ModelStrAsBytes import Tablet.ModelSliceFirst import Tablet.ModelF64MantissaDigits import Tablet.ModelU64Shl import Tablet.ModelU64BitOr import Tablet.num_imp_dec2flt_pfe_empty import Tablet.num_imp_dec2flt_pfe_invalid import Tablet.num_imp_traits_Float_SIG_BITS_default import Tablet.num_imp_dec2flt_common_BiasedFp_Insts_CoreCmpPartialEqBiasedFp_eq open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P616_T1_FrameCorrectness] theorem P616_T1_FrameCorrectness : forall C : P9_T1_AbstractComponents, DispatchContracts C -> forall (s : Str) r trace, DecimalGoalVocabularyDefault.sourceString s -> s.val.length <= 3458764513820540928 -> P9_T1_DispatchFrame (P9_T1_EmbedComponents C) s = ok (r, trace) -> ((DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s) none -> exists error, r = core.result.Result.Err error) ∧ (forall sign nan, DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s) (some (Sum.inr (sign, nan))) -> exists f, r = core.result.Result.Ok f ∧ (if nan then DecimalGoalVocabularyDefault.isNan f else DecimalGoalVocabularyDefault.isInfinity (decide (sign = some true)) f)) ∧ (forall x : DecimalLexeme, DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s) (some (Sum.inl x)) -> exists f magnitude, r = core.result.Result.Ok f ∧ DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.accumulatedMagnitude x) magnitude ∧ DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude f)) := by -- BODY classical intro C hc s r trace hs hA hrun have hbytes : ∀ b ∈ DecimalGoalVocabularyDefault.bytes s, b < 256 := by intro b hb dsimp [DecimalGoalVocabularyDefault] at hb ⊢ rcases List.mem_map.mp hb with ⟨u, hu, rfl⟩ exact u.bv.isLt have bind_ok_inv : forall {alpha beta : Type} (x : Result alpha) (g : alpha -> Result beta) y, Bind.bind x g = ok y -> exists z, x = ok z /\ g z = ok y := by intro alpha beta x g y hx cases x with | fail err => simp [Bind.bind, Aeneas.Std.bind] at hx | div => simp [Bind.bind, Aeneas.Std.bind] at hx | ok z => exact ⟨z, rfl, hx⟩ have noUnsignedNumericEmpty : forall x : DecimalLexeme, DecimalGoalVocabularyDefault.validNumeric x -> x.leadingSign = none -> DecimalGoalVocabularyDefault.renderNumeric x = [] -> False := by intro x hv hs hr dsimp only [DecimalGoalVocabularyDefault] at hv hs hr rcases hv with ⟨_, _, hne, _⟩ cases hi : x.integerDigits with | nil => cases hf : x.fractionDigits with | none => simp_all [hi, hf] | some ds => cases ds <;> simp_all [hi, hf] | cons d ds => simp [hi] at hr unfold P9_T1_DispatchFrame at hrun obtain ⟨body, hbody, hrun⟩ := bind_ok_inv _ _ _ hrun have hbody_eq : body = s := by simpa only [core.str.Str.as_bytes, ModelStrAsBytes, Result.ok.injEq] using hbody.symm subst body obtain ⟨head, hhead, hrun⟩ := bind_ok_inv _ _ _ hrun have hhead_eq : 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 => simp only [hlist, List.head?_nil] at hrun obtain ⟨pfe, hpfe, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun constructor · intro _ exact ⟨_, rfl⟩ constructor · intro sign nan hclass dsimp only [DecimalGoalVocabularyDefault] at hclass simp [hlist] at hclass · intro x hclass dsimp only [DecimalGoalVocabularyDefault] at hclass simp [hlist] at hclass rcases hclass with ⟨_, _, hnonempty, _, _, hi, hf, _⟩ cases hfrac : x.fractionDigits <;> simp_all [hfrac] | cons c tail => simp only [hlist, List.head?_cons] at hrun obtain ⟨body1, hbody1, hrun⟩ := bind_ok_inv _ _ _ hrun 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 : body1.val = if c = 45#u8 ∨ c = 43#u8 then tail else s.val := by by_cases hm : c = 45#u8 · simp [hm, hindex] at hbody1 subst body1 simp [Slice.drop, hlist, hm] · by_cases hp : c = 43#u8 · simp [hp, hindex] at hbody1 subst body1 simp [Slice.drop, hlist, hp] · simp only [hm, hp, ite_false, Result.ok.injEq] at hbody1 subst body1 simp [hm, hp] have hbodyval' : body1.val = if s.val.head? = some (45#u8) ∨ s.val.head? = some (43#u8) then s.val.drop 1 else s.val := by simpa [hlist, Slice.drop] using hbodyval have hstrip := P616_T1_SignStrippedClassification s body1 hs hbodyval' have hbodySource : SourceValidByteSlice body1 := hstrip.1 have hbodyLength : body1.val.length <= s.val.length := hstrip.2.1 obtain ⟨empty, hempty, hrun⟩ := bind_ok_inv _ _ _ hrun have hempty_eq : empty = decide (body1.val = []) := by simpa only [core.slice.Slice.is_empty, Slice.length, List.length_eq_zero_iff, Result.ok.injEq] using hempty.symm by_cases hbodyempty : body1.val = [] · simp only [hempty_eq, hbodyempty, decide_true, if_true] at hrun obtain ⟨pfe, hpfe, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun constructor · intro _ exact ⟨_, rfl⟩ constructor · intro sign nan hclass have hsbody := (hstrip.2.2.2.1 sign nan hclass).1 dsimp only [DecimalGoalVocabularyDefault] at hsbody simp [hbodyempty] at hsbody · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } have hnum := hstrip.2.2.1 x hclass have hrender : DecimalGoalVocabularyDefault.renderNumeric y = [] := by dsimp only [DecimalGoalVocabularyDefault] dsimp only [DecimalGoalVocabularyDefault] at hnum simpa [y, hbodyempty] using hnum.2.1 exact (noUnsignedNumericEmpty y hnum.1 rfl hrender).elim · have hempty_false : empty = false := by simp [hempty_eq, hbodyempty] rw [hempty_false] at hrun obtain ⟨parsed, hparsed, hrun⟩ := bind_ok_inv _ _ _ hrun cases parsed with | none => obtain ⟨special, hspecial, hrun⟩ := bind_ok_inv _ _ _ hrun cases special with | none => obtain ⟨pfe, hpfe, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun constructor · intro _ exact ⟨_, rfl⟩ constructor · intro sign nan hclass have hsbody := (hstrip.2.2.2.1 sign nan hclass).1 cases nan with | false => obtain ⟨f, hparse, _⟩ := hc.special_infinity body1 (decide (c = 45#u8)) hbodySource hsbody have hnone : C.parseSpecial body1 (decide (c = 45#u8)) = none := by simpa [P9_T1_EmbedComponents] using hspecial rw [hnone] at hparse cases hparse | true => obtain ⟨f, hparse, _⟩ := hc.special_nan body1 (decide (c = 45#u8)) hbodySource hsbody have hnone : C.parseSpecial body1 (decide (c = 45#u8)) = none := by simpa [P9_T1_EmbedComponents] using hspecial rw [hnone] at hparse cases hparse · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } have hnum := hstrip.2.2.1 x hclass obtain ⟨d, hparse⟩ := hc.number_accept body1 y hbodySource hbodyempty (Nat.le_trans hbodyLength hA) rfl hnum.1 hnum.2.1 have hnone : C.parseNumber body1 = none := by simpa [P9_T1_EmbedComponents] using hparsed rw [hnone] at hparse cases hparse | some value => have hspecialC : C.parseSpecial body1 (decide (c = 45#u8)) = some value := by simpa [P9_T1_EmbedComponents] using hspecial cases hrun constructor · intro hclass have hnoSpecial : ¬ exists nan, DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes body1) none nan := by intro hx obtain ⟨sign, nan, hclass'⟩ := hstrip.2.2.2.2.2 hx exact hclass.2 ⟨sign, nan, hclass'⟩ have hnone := hc.special_reject body1 (decide (c = 45#u8)) hbodySource hnoSpecial rw [hnone] at hspecialC cases hspecialC constructor · intro sign nan hclass have hsdata := hstrip.2.2.2.1 sign nan hclass have hnegative : decide (sign = some true) = decide (c = 45#u8) := by simpa [hlist] using hsdata.2 cases nan with | false => obtain ⟨f, hparse, hinf⟩ := hc.special_infinity body1 (decide (c = 45#u8)) hbodySource hsdata.1 rw [hspecialC] at hparse cases hparse refine ⟨value, rfl, ?_⟩ simpa [hnegative] using hinf | true => obtain ⟨f, hparse, hnan⟩ := hc.special_nan body1 (decide (c = 45#u8)) hbodySource hsdata.1 rw [hspecialC] at hparse cases hparse exact ⟨value, rfl, hnan⟩ · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } have hnum := hstrip.2.2.1 x hclass obtain ⟨d, hparse⟩ := hc.number_accept body1 y hbodySource hbodyempty (Nat.le_trans hbodyLength hA) rfl hnum.1 hnum.2.1 have hnone : C.parseNumber body1 = none := by simpa [P9_T1_EmbedComponents] using hparsed rw [hnone] at hparse cases hparse | some d => have hparse : C.parseNumber body1 = some d := by simpa [P9_T1_EmbedComponents] using hparsed have hnotInvalid : DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s) none -> False := by intro hclass have hnoBodyNum : ¬ exists y : DecimalLexeme, y.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric y ∧ DecimalGoalVocabularyDefault.renderNumeric y = DecimalGoalVocabularyDefault.bytes body1 := by intro hx obtain ⟨x', hx'⟩ := hstrip.2.2.2.2.1 hx exact hclass.1 ⟨x', hx'⟩ have hn := hc.number_reject body1 hbodySource hbodyempty (Nat.le_trans hbodyLength hA) hnoBodyNum simp [hn] at hparse have hnotSpecial : ∀ sign nan, DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s) (some (Sum.inr (sign, nan))) -> False := by intro sign nan hclass have hnoFullNum : ¬ exists x : DecimalLexeme, DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s) (some (Sum.inl x)) := by intro hx obtain ⟨x, hx⟩ := hx have heq := ClassificationDeterminism.1 (DecimalGoalVocabularyDefault.bytes s) (some (Sum.inr (sign, nan))) (some (Sum.inl x)) hbytes hclass hx cases heq have hnoBodyNum : ¬ exists y : DecimalLexeme, y.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric y ∧ DecimalGoalVocabularyDefault.renderNumeric y = DecimalGoalVocabularyDefault.bytes body1 := by intro hx obtain ⟨x, hx⟩ := hstrip.2.2.2.2.1 hx exact hnoFullNum ⟨x, hx⟩ have hn := hc.number_reject body1 hbodySource hbodyempty (Nat.le_trans hbodyLength hA) hnoBodyNum simp [hn] at hparse obtain ⟨fast, hfast, hrun⟩ := bind_ok_inv _ _ _ hrun cases fast with | none => have hnativeNone : C.native { d with negative := decide (c = 45#u8), many_digits := d.many_digits } = none := by change ok (C.native { d with negative := decide (c = 45#u8), many_digits := d.many_digits }) = ok none at hfast injection hfast with hnativeNone obtain ⟨first, hfirst, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨phase1, hphase1, hrun⟩ := bind_ok_inv _ _ _ hrun rcases phase1 with ⟨many1, many2, first1, fp, secondGuard⟩ obtain ⟨phase2, hphase2, hrun⟩ := bind_ok_inv _ _ _ hrun rcases phase2 with ⟨post, secondExp, secondMant, secondResult, secondEqual, forced⟩ obtain ⟨phase3, hphase3, hrun⟩ := bind_ok_inv _ _ _ hrun rcases phase3 with ⟨selected, slowGuard, slowResult⟩ have hfirstC : C.integer d.exponent d.mantissa = first := by change ok (C.integer d.exponent d.mantissa) = ok first at hfirst injection hfirst with hfirstC by_cases hmany : d.many_digits · simp only [hmany, if_pos] at hphase1 cases hphase1 by_cases hfirstNegative : first.p_biased < 0#i32 · have hltVal : first.p_biased.val < 0 := by simpa using hfirstNegative have hfirstBelow : ¬ 0 ≤ first.p_biased.val := by omega simp [hfirstBelow] at hphase2 rcases hphase2 with ⟨hpost, hphase2⟩ subst post simp only [hfirstNegative, if_pos] at hphase3 cases hphase3 obtain ⟨i, hi, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i1, hi1, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i2, hi2, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembledWord, hword, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembly, hassembly, hrun⟩ := bind_ok_inv _ _ _ hrun by_cases hnegative : c = 45#u8 · simp only [hnegative, if_pos] at hrun obtain ⟨result, hnegate, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } let magnitude := C.assemble (C.slow body1) have hnum := hstrip.2.2.1 x hclass have hslow := hc.slow_normal body1 y hbodySource rfl hnum.1 hnum.2.1 have hround := hslow.1 rw [hnum.2.2.1] at hround have hclear := hslow.2 have hassemblyC : C.assemble (C.slow body1) = assembly := by change ok (C.assemble (C.slow body1)) = ok assembly at hassembly injection hassembly with hassemblyC have hnegateC : C.negate assembly = result := by change ok (C.negate assembly) = ok result at hnegate injection hnegate with hnegateC have hresult : result = C.negate magnitude := by calc result = C.negate assembly := hnegateC.symm _ = C.negate magnitude := by rw [← hassemblyC] have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = true := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = true := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude true magnitude (C.negate magnitude) := by change magnitude.val < 2 ^ 63 ∧ (C.negate magnitude).val = magnitude.val + (if true then 2 ^ 63 else 0) rw [hc.negate_all magnitude] constructor · exact hclear · rw [if_pos hclear] simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude (C.negate magnitude) := by rw [hlex]; exact hsign exact ⟨C.negate magnitude, magnitude, congrArg (fun z => core.result.Result.Ok z) hresult, hround, hsignLex⟩ · simp only [hnegative, if_neg] at hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } let magnitude := C.assemble (C.slow body1) have hnum := hstrip.2.2.1 x hclass have hslow := hc.slow_normal body1 y hbodySource rfl hnum.1 hnum.2.1 have hround := hslow.1 rw [hnum.2.2.1] at hround have hclear := hslow.2 have hassemblyC : C.assemble (C.slow body1) = assembly := by change ok (C.assemble (C.slow body1)) = ok assembly at hassembly injection hassembly with hassemblyC have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = false := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = false := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude false magnitude magnitude := by change magnitude.val < 2 ^ 63 ∧ magnitude.val = magnitude.val + (if false then 2 ^ 63 else 0) constructor · exact hclear · simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude magnitude := by rw [hlex] exact hsign have hsignLexAssembly : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude assembly := by rw [← hassemblyC] exact hsignLex exact ⟨assembly, magnitude, rfl, hround, hsignLexAssembly⟩ · have hnotLtVal : ¬ first.p_biased.val < 0 := by simpa using hfirstNegative have hfirstNonneg : 0 ≤ first.p_biased.val := by omega simp [hfirstNonneg] at hphase2 obtain ⟨next, hnext, hphase2⟩ := bind_ok_inv _ _ _ hphase2 obtain ⟨second, hsecond, hphase2⟩ := bind_ok_inv _ _ _ hphase2 obtain ⟨equal, hequal, hphase2⟩ := bind_ok_inv _ _ _ hphase2 cases equal · simp at hphase2 rcases hphase2 with ⟨hpost, hExp, hMant, hSecond, hEqual, hForced⟩ have hpostNegative : post.p_biased < 0#i32 := by rw [← hpost] simp simp only [hpostNegative, if_pos] at hphase3 cases hphase3 obtain ⟨i, hi, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i1, hi1, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i2, hi2, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembledWord, hword, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembly, hassembly, hrun⟩ := bind_ok_inv _ _ _ hrun have hassemblyC : C.assemble (C.slow body1) = assembly := by change ok (C.assemble (C.slow body1)) = ok assembly at hassembly injection hassembly with hassemblyC by_cases hnegative : c = 45#u8 · simp only [hnegative, if_pos] at hrun obtain ⟨result, hnegate, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } let magnitude := C.assemble (C.slow body1) have hnum := hstrip.2.2.1 x hclass have hslow := hc.slow_normal body1 y hbodySource rfl hnum.1 hnum.2.1 have hround := hslow.1 rw [hnum.2.2.1] at hround have hclear := hslow.2 have hnegateC : C.negate assembly = result := by change ok (C.negate assembly) = ok result at hnegate injection hnegate with hnegateC have hresult : result = C.negate magnitude := by calc result = C.negate assembly := hnegateC.symm _ = C.negate magnitude := by rw [← hassemblyC] have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = true := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = true := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude true magnitude (C.negate magnitude) := by change magnitude.val < 2 ^ 63 ∧ (C.negate magnitude).val = magnitude.val + (if true then 2 ^ 63 else 0) rw [hc.negate_all magnitude] constructor · exact hclear · rw [if_pos hclear] simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude (C.negate magnitude) := by rw [hlex]; exact hsign exact ⟨C.negate magnitude, magnitude, congrArg (fun z => core.result.Result.Ok z) hresult, hround, hsignLex⟩ · simp only [hnegative, if_neg] at hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } let magnitude := C.assemble (C.slow body1) have hnum := hstrip.2.2.1 x hclass have hslow := hc.slow_normal body1 y hbodySource rfl hnum.1 hnum.2.1 have hround := hslow.1 rw [hnum.2.2.1] at hround have hclear := hslow.2 have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = false := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = false := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude false magnitude magnitude := by change magnitude.val < 2 ^ 63 ∧ magnitude.val = magnitude.val + (if false then 2 ^ 63 else 0) constructor · exact hclear · simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude magnitude := by rw [hlex] exact hsign have hsignLexAssembly : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude assembly := by rw [← hassemblyC] exact hsignLex exact ⟨assembly, magnitude, rfl, hround, hsignLexAssembly⟩ · simp at hphase2 rcases hphase2 with ⟨hpost, hExp, hMant, hSecond, hEqual, hForced⟩ subst post simp [hfirstNegative] at hphase3 have hselected : selected = first := by exact hphase3.1.symm subst selected obtain ⟨i, hi, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i1, hi1, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i2, hi2, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembledWord, hword, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembly, hassembly, hrun⟩ := bind_ok_inv _ _ _ hrun have hassemblyC : C.assemble first = assembly := by change ok (C.assemble first) = ok assembly at hassembly injection hassembly with hassemblyC have hsecondC : C.integer d.exponent next = second := by change ok (C.integer d.exponent next) = ok second at hsecond injection hsecond with hsecondC have hpair : first = second := by have hm : first.m = second.m := by by_contra h simp [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq, h] at hequal have hp : first.p_biased = second.p_biased := by have hh : decide (first.p_biased = second.p_biased) = true := by simpa [num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq, hm] using hequal simpa using hh have hf := And.intro hm hp rcases hf with ⟨hm, hp⟩ cases first cases second cases hm cases hp rfl have hnextval : next.val = d.mantissa.val + 1 := by have hadd := UScalar.add_equiv d.mantissa (1#u64) rw [hnext] at hadd simpa using hadd.2.1 have hbounds := hc.record_bounds body1 d hbodySource hbodyempty hparse have hfirstNormal := hc.integer_normal d.exponent d.mantissa have hfirstRound : DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.decimalMagnitude d) (C.assemble first) := by have hnot : ¬ (C.integer d.exponent d.mantissa).p_biased.val < 0 := by rw [hfirstC] exact hnotLtVal rcases hfirstNormal with hneg | ⟨hr, hclear⟩ · exact (hnot hneg).elim · simpa only [DecimalGoalVocabularyDefault, hfirstC] using hr have hfirstClear : (C.assemble first).val < 2 ^ 63 := by have hnot : ¬ (C.integer d.exponent d.mantissa).p_biased.val < 0 := by rw [hfirstC] exact hnotLtVal rcases hfirstNormal with hneg | ⟨hr, hclear⟩ · exact (hnot hneg).elim · simpa [hfirstC] using hclear have hsecondNormal := hc.integer_normal d.exponent next have hsecondRound : DecimalGoalVocabularyDefault.roundsMagnitude ((next.val : Real) * (10 : Real) ^ d.exponent.val) (C.assemble second) := by have hnot : ¬ (C.integer d.exponent next).p_biased.val < 0 := by rw [hsecondC, ← hpair] exact hnotLtVal rcases hsecondNormal with hneg | ⟨hr, hclear⟩ · exact (hnot hneg).elim · simpa [hsecondC] using hr have hsecondRound' : DecimalGoalVocabularyDefault.roundsMagnitude ((((d.mantissa.val + 1 : Nat) : Real) * (10 : Real) ^ d.exponent.val)) (C.assemble first) := by rw [← hpair] at hsecondRound rw [hnextval] at hsecondRound exact hsecondRound have hupperRewrite : ((next.val : Real) * (10 : Real) ^ d.exponent.val) = (((d.mantissa.val + 1 : Nat) : Real) * (10 : Real) ^ d.exponent.val) := by rw [hnextval] have hsecondRound'' := hsecondRound rw [hupperRewrite] at hsecondRound'' constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } have hnum := hstrip.2.2.1 x hclass have hacc := hc.record_accumulated body1 y d hbodySource (Nat.le_trans hbodyLength hA) rfl hnum.1 hnum.2.1 hparse have hinterval : DecimalGoalVocabularyDefault.decimalMagnitude d ≤ DecimalGoalVocabularyDefault.accumulatedMagnitude y ∧ DecimalGoalVocabularyDefault.accumulatedMagnitude y < (((d.mantissa.val + 1 : Nat) : Real) * (10 : Real) ^ d.exponent.val) := by simpa [hmany] using hacc have hroundMag := P616_T1_RoundingCellConvexity (DecimalGoalVocabularyDefault.decimalMagnitude d) (((d.mantissa.val + 1 : Nat) : Real) * (10 : Real) ^ d.exponent.val) (DecimalGoalVocabularyDefault.accumulatedMagnitude y) (C.assemble first) hfirstRound hsecondRound' hinterval.1 (le_of_lt hinterval.2) have hround := hroundMag rw [hnum.2.2.1] at hround by_cases hnegative : c = 45#u8 · simp only [hnegative, if_pos] at hrun obtain ⟨result, hnegate, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun have hnegateC : C.negate assembly = result := by change ok (C.negate assembly) = ok result at hnegate injection hnegate with hnegateC have hresult : result = C.negate (C.assemble first) := by calc result = C.negate assembly := hnegateC.symm _ = C.negate (C.assemble first) := by rw [← hassemblyC] have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = true := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = true := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude true (C.assemble first) (C.negate (C.assemble first)) := by change (C.assemble first).val < 2 ^ 63 ∧ (C.negate (C.assemble first)).val = (C.assemble first).val + (if true then 2 ^ 63 else 0) rw [hc.negate_all] constructor · exact hfirstClear · rw [if_pos hfirstClear] simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) (C.assemble first) (C.negate (C.assemble first)) := by rw [hlex] exact hsign exact ⟨C.negate (C.assemble first), C.assemble first, congrArg (fun z => core.result.Result.Ok z) hresult, hround, hsignLex⟩ · simp only [hnegative, if_neg] at hrun cases hrun have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = false := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = false := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude false (C.assemble first) (C.assemble first) := by change (C.assemble first).val < 2 ^ 63 ∧ (C.assemble first).val = (C.assemble first).val + (if false then 2 ^ 63 else 0) constructor · exact hfirstClear · simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) (C.assemble first) assembly := by rw [hlex, ← hassemblyC] exact hsign exact ⟨assembly, C.assemble first, rfl, hround, hsignLex⟩ · simp only [hmany, Bool.false_eq_true, if_false] at hphase1 cases hphase1 simp only [Bool.false_eq_true, if_false] at hphase2 cases hphase2 by_cases hfirstNegative : first.p_biased < 0#i32 · simp only [hfirstNegative, if_pos] at hphase3 cases hphase3 obtain ⟨i, hi, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i1, hi1, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i2, hi2, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembledWord, hword, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembly, hassembly, hrun⟩ := bind_ok_inv _ _ _ hrun by_cases hnegative : c = 45#u8 · simp only [hnegative, if_pos] at hrun obtain ⟨result, hnegate, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } let magnitude := C.assemble (C.slow body1) have hnum := hstrip.2.2.1 x hclass have hslow := hc.slow_normal body1 y hbodySource rfl hnum.1 hnum.2.1 have hround := hslow.1 rw [hnum.2.2.1] at hround have hclear := hslow.2 have hassemblyC : C.assemble (C.slow body1) = assembly := by change ok (C.assemble (C.slow body1)) = ok assembly at hassembly injection hassembly with hassemblyC have hnegateC : C.negate assembly = result := by change ok (C.negate assembly) = ok result at hnegate injection hnegate with hnegateC have hresult : result = C.negate magnitude := by calc result = C.negate assembly := hnegateC.symm _ = C.negate magnitude := by rw [← hassemblyC] have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = true := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = true := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude true magnitude (C.negate magnitude) := by change magnitude.val < 2 ^ 63 ∧ (C.negate magnitude).val = magnitude.val + (if true then 2 ^ 63 else 0) rw [hc.negate_all magnitude] constructor · exact hclear · rw [if_pos hclear] simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude (C.negate magnitude) := by rw [hlex] exact hsign exact ⟨C.negate magnitude, magnitude, congrArg (fun z => core.result.Result.Ok z) hresult, hround, hsignLex⟩ · simp only [hnegative, if_neg] at hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } let magnitude := C.assemble (C.slow body1) have hnum := hstrip.2.2.1 x hclass have hslow := hc.slow_normal body1 y hbodySource rfl hnum.1 hnum.2.1 have hround := hslow.1 rw [hnum.2.2.1] at hround have hclear := hslow.2 have hassemblyC : C.assemble (C.slow body1) = assembly := by change ok (C.assemble (C.slow body1)) = ok assembly at hassembly injection hassembly with hassemblyC have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = false := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = false := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude false magnitude magnitude := by change magnitude.val < 2 ^ 63 ∧ magnitude.val = magnitude.val + (if false then 2 ^ 63 else 0) constructor · exact hclear · simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude magnitude := by rw [hlex] exact hsign have hsignLexAssembly : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude assembly := by rw [← hassemblyC] exact hsignLex exact ⟨assembly, magnitude, rfl, hround, hsignLexAssembly⟩ · simp only [hfirstNegative, if_neg] at hphase3 cases hphase3 obtain ⟨i, hi, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i1, hi1, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨i2, hi2, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembledWord, hword, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨assembly, hassembly, hrun⟩ := bind_ok_inv _ _ _ hrun have hassemblyC : C.assemble first = assembly := by change ok (C.assemble first) = ok assembly at hassembly injection hassembly with hassemblyC by_cases hnegative : c = 45#u8 · simp only [hnegative, if_pos] at hrun obtain ⟨result, hnegate, hrun⟩ := bind_ok_inv _ _ _ hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } have hnum := hstrip.2.2.1 x hclass have hacc := hc.record_accumulated body1 y d hbodySource (Nat.le_trans hbodyLength hA) rfl hnum.1 hnum.2.1 hparse have hdecimal : DecimalGoalVocabularyDefault.decimalMagnitude d = DecimalGoalVocabularyDefault.accumulatedMagnitude y := by simpa [hmany] using hacc have hinteger := hc.integer_normal d.exponent d.mantissa have hroundD : DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.decimalMagnitude d) (C.assemble first) := by have hnonneg : ¬ first.p_biased.val < 0 := by simpa using hfirstNegative rw [← hfirstC] at hnonneg rcases hinteger with hneg | ⟨hr, hclear⟩ · exact (hnonneg hneg).elim · simpa [hfirstC] using hr have hround := hroundD rw [hdecimal, hnum.2.2.1] at hround have hclear : (C.assemble first).val < 2 ^ 63 := by have hnonneg : ¬ first.p_biased.val < 0 := by simpa using hfirstNegative rw [← hfirstC] at hnonneg rcases hinteger with hneg | ⟨hr, hclear⟩ · exact (hnonneg hneg).elim · simpa [hfirstC] using hclear have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = true := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = true := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude true (C.assemble first) (C.negate (C.assemble first)) := by change (C.assemble first).val < 2 ^ 63 ∧ (C.negate (C.assemble first)).val = (C.assemble first).val + (if true then 2 ^ 63 else 0) rw [hc.negate_all] constructor · exact hclear · rw [if_pos hclear] simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) (C.assemble first) (C.negate (C.assemble first)) := by rw [hlex] exact hsign have hresult : result = C.negate (C.assemble first) := by have hnegateC : C.negate assembly = result := by change ok (C.negate assembly) = ok result at hnegate injection hnegate with hnegateC simpa [hassemblyC] using hnegateC.symm exact ⟨C.negate (C.assemble first), C.assemble first, congrArg (fun z => core.result.Result.Ok z) hresult, hround, hsignLex⟩ · simp only [hnegative, if_neg] at hrun cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } have hnum := hstrip.2.2.1 x hclass have hacc := hc.record_accumulated body1 y d hbodySource (Nat.le_trans hbodyLength hA) rfl hnum.1 hnum.2.1 hparse have hdecimal : DecimalGoalVocabularyDefault.decimalMagnitude d = DecimalGoalVocabularyDefault.accumulatedMagnitude y := by simpa [hmany] using hacc have hinteger := hc.integer_normal d.exponent d.mantissa have hroundD : DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.decimalMagnitude d) (C.assemble first) := by have hnonneg : ¬ first.p_biased.val < 0 := by simpa using hfirstNegative rw [← hfirstC] at hnonneg rcases hinteger with hneg | ⟨hr, hclear⟩ · exact (hnonneg hneg).elim · simpa [hfirstC] using hr have hround := hroundD rw [hdecimal, hnum.2.2.1] at hround have hclear : (C.assemble first).val < 2 ^ 63 := by have hnonneg : ¬ first.p_biased.val < 0 := by simpa using hfirstNegative rw [← hfirstC] at hnonneg rcases hinteger with hneg | ⟨hr, hclear⟩ · exact (hnonneg hneg).elim · simpa [hfirstC] using hclear have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = false := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = false := by simp [hlist, hnegative] have hsign : DecimalGoalVocabularyDefault.signedMagnitude false (C.assemble first) (C.assemble first) := by change (C.assemble first).val < 2 ^ 63 ∧ (C.assemble first).val = (C.assemble first).val + (if false then 2 ^ 63 else 0) constructor · exact hclear · simp have hsignLex : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) (C.assemble first) (C.assemble first) := by rw [hlex] exact hsign have hsignLexAssembly : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) (C.assemble first) assembly := by rw [← hassemblyC] exact hsignLex exact ⟨assembly, C.assemble first, rfl, hround, hsignLexAssembly⟩ | some value => have hnative : C.native { d with negative := decide (c = 45#u8), many_digits := d.many_digits } = some value := by change ok (C.native { d with negative := decide (c = 45#u8), many_digits := d.many_digits }) = ok (some value) at hfast injection hfast with hnative cases hrun constructor · intro hclass exact (hnotInvalid hclass).elim constructor · intro sign nan hclass exact (hnotSpecial sign nan hclass).elim · intro x hclass let y : DecimalLexeme := { x with leadingSign := none } let nativeRecord : num.imp.dec2flt.decimal.Decimal := { d with negative := decide (c = 45#u8), many_digits := d.many_digits } have hnum := hstrip.2.2.1 x hclass have hnativeNormal := hc.native_normal nativeRecord value hnative rcases hnativeNormal with ⟨hmd, hrest⟩ rcases hrest with ⟨magnitude, hround, hsigned⟩ have hmany : d.many_digits = false := by change nativeRecord.many_digits = false at hmd simpa [nativeRecord] using hmd have hacc := hc.record_accumulated body1 y d hbodySource (Nat.le_trans hbodyLength hA) rfl hnum.1 hnum.2.1 hparse have hdecimal : DecimalGoalVocabularyDefault.decimalMagnitude d = DecimalGoalVocabularyDefault.accumulatedMagnitude y := by simpa [hmany] using hacc have hroundD : DecimalGoalVocabularyDefault.roundsMagnitude (DecimalGoalVocabularyDefault.decimalMagnitude d) magnitude := by simpa only [DecimalGoalVocabularyDefault] using hround rw [hdecimal, hnum.2.2.1] at hroundD have hlex : DecimalGoalVocabularyDefault.lexicalNegative x = decide (c = 45#u8) := by calc DecimalGoalVocabularyDefault.lexicalNegative x = decide (s.val.head? = some (45#u8)) := hnum.2.2.2.2 _ = decide (c = 45#u8) := by simp [hlist] have hsigned' : DecimalGoalVocabularyDefault.signedMagnitude (DecimalGoalVocabularyDefault.lexicalNegative x) magnitude value := by rw [hlex] simpa only [DecimalGoalVocabularyDefault, nativeRecord] using hsigned exact ⟨value, magnitude, rfl, hroundD, hsigned'⟩ end dec2flt_model