import Tablet.DecimalGoalVocabularyDefault import Tablet.Assumptions import Tablet.SourceValidByteSlice import Tablet.NumberHelperDigitScanExact import Tablet.P364_T1_NumberHelperOffsetSourceLength import Tablet.P364_T1_NumberHelperAdjustCountExact import Tablet.ModelSliceSplitFirst import Tablet.ModelOptionBranch import Tablet.ModelOptionFromResidual import Tablet.num_imp_dec2flt_parse_parse_number import Tablet.num_imp_dec2flt_parse_parse_partial_number open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P401_T1_UnsignedZeroRecord] theorem P401_T1_UnsignedZeroRecord (s : Slice Std.U8) (x : DecimalLexeme) (d : num.imp.dec2flt.decimal.Decimal) (hsource : SourceValidByteSlice s) (hsign : x.leadingSign = none) (hvalid : DecimalGoalVocabularyDefault.validNumeric x) (hrender : DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes s) (hzero : ∀ digit ∈ (x.integerDigits ++ match x.fractionDigits with | none => [] | some ds => ds), digit = 0) (hresult : num.imp.dec2flt.parse.parse_number s = ok (some d)) : d.mantissa.val = 0 ∧ d.many_digits = false ∧ d.negative = false := by -- BODY have hpartial : ∃ len : Std.Usize, num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, len)) := by unfold num.imp.dec2flt.parse.parse_number at hresult cases hp : num.imp.dec2flt.parse.parse_partial_number s with | fail err => simp [hp] at hresult | div => simp [hp] at hresult | ok out => cases out with | none => simp [hp] at hresult | some pair => rcases pair with ⟨d', len⟩ simp [hp] at hresult split at hresult · simp_all · simp at hresult rcases hpartial with ⟨len, hpartial⟩ have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y, Bind.bind r f = ok y → ∃ z, r = ok z ∧ f z = ok y := by intro α β r f y h cases r with | fail err => simp [Bind.bind, Aeneas.Std.bind] at h | div => simp [Bind.bind, Aeneas.Std.bind] at h | ok z => exact ⟨z, rfl, h⟩ have hpartialExec := hpartial unfold num.imp.dec2flt.parse.parse_partial_number at hpartialExec obtain ⟨isEmpty, hempty, hafterEmpty⟩ := bind_ok_inv _ _ _ hpartialExec obtain ⟨_, hmassert, hafterAssert⟩ := bind_ok_inv _ _ _ hafterEmpty cases isEmpty <;> simp [massert] at hmassert obtain ⟨scanOut, hscan, hafterScan⟩ := bind_ok_inv _ _ _ hafterAssert have hvalid' := hvalid simp [DecimalGoalVocabularyDefault] at hvalid' rcases hvalid' with ⟨hint, hfrac, hnonempty, hexp⟩ let fracDigits := match x.fractionDigits with | none => [] | some ds => ds have hzeroInt : ∀ digit, digit ∈ x.integerDigits → digit = 0 := by intro digit hd exact hzero digit (List.mem_append_left _ hd) have hzeroFrac : ∀ digit, digit ∈ fracDigits → digit = 0 := by intro digit hd cases hf : x.fractionDigits with | none => simp [fracDigits, hf] at hd | some ds => simp [fracDigits, hf] at hd exact hzero digit (by simpa [fracDigits, hf] using List.mem_append_right x.integerDigits hd) have mapZero : ∀ ds : List Nat, (∀ n, n ∈ ds → n = 0) → ds.map (fun n => 48 + n) = ds.map (fun _ => 48) := by intro ds induction ds with | nil => intro; rfl | cons n ns ih => intro hn have hn0 := hn n List.mem_cons_self simp only [List.map_cons, hn0] exact congrArg (List.cons 48) (ih (by intro m hm; exact hn m (List.mem_cons_of_mem _ hm))) have hrenderNat : (x.integerDigits.map (fun _ => 48) ++ (match x.fractionDigits with | none => [] | some _ => 46 :: fracDigits.map (fun _ => 48))) ++ (match x.exponent with | none => [] | some (upper, sign, ds) => (if upper then 69 else 101) :: (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++ ds.map (fun n => 48 + n)) = s.val.map (fun b => b.val) := by have hvals := hrender simp [DecimalGoalVocabularyDefault, hsign] at hvals rw [mapZero x.integerDigits hzeroInt] at hvals cases hf : x.fractionDigits with | none => simpa [fracDigits, hf, List.append_assoc] using hvals | some ds => have hz := mapZero ds (by intro n hn exact hzero n (by simpa [hf] using List.mem_append_right x.integerDigits hn)) simpa [fracDigits, hf, hz, List.append_assoc] using hvals let intNat := x.integerDigits.map (fun _ => 48) let fracNat := fracDigits.map (fun _ => 48) let dotNat := match x.fractionDigits with | none => [] | some _ => 46 :: fracNat let expNat := match x.exponent with | none => [] | some (upper, sign, ds) => (if upper then 69 else 101) :: (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++ ds.map (fun n => 48 + n) let renderedNat := intNat ++ dotNat ++ expNat have hrenderNat' : renderedNat = s.val.map (fun b => b.val) := by simpa [renderedNat, intNat, dotNat, expNat, fracNat, fracDigits, List.append_assoc] using hrenderNat have hrenderNonempty : renderedNat ≠ [] := by rcases hnonempty with hi | hf · cases hx : x.integerDigits with | nil => simp [hx] at hi | cons a ds => simp [renderedNat, intNat, hx] · cases hx : x.fractionDigits with | none => simp [hx] at hf | some ds => cases hd : ds with | nil => simp [hx, hd] at hf | cons a ds' => simp [renderedNat, intNat, dotNat, fracNat, hx, hd] have hsourceNonempty : s.val ≠ [] := by intro hs apply hrenderNonempty simpa [hs] using hrenderNat' have hrenderSmall : ∀ n, n ∈ renderedNat → n < 256 := by intro n hn have hp : n ∈ intNat ++ dotNat ++ expNat := by simpa [renderedNat] using hn rcases List.mem_append.mp hp with hprefix | he · rcases List.mem_append.mp hprefix with hi | hd · rcases List.mem_map.mp hi with ⟨d, _, rfl⟩ norm_num · cases hf : x.fractionDigits with | none => simp [dotNat, hf] at hd | some ds => simp [dotNat, hf] at hd rcases hd with rfl | hd · norm_num · rcases List.mem_map.mp hd with ⟨d, _, rfl⟩ norm_num · cases hx : x.exponent with | none => simp [expNat, hx] at he | some e => rcases e with ⟨upper, sign, ds⟩ simp [expNat, hx] at he rcases he with hhead | hs · rw [hhead] split <;> norm_num · rcases hs with hsign | hd · cases sign with | none => simp at hsign | some neg => cases neg <;> simp at hsign <;> omega · rcases hd with ⟨d, hdm, heq⟩ have hexp' : ds ≠ [] ∧ ∀ d, d ∈ ds → d < 10 := by simpa [hx] using hexp have hdigit := hexp'.2 d hdm omega let byteOfNat := fun n : Nat => @UScalar.ofNatCore .U8 (n % 256) (by exact Nat.mod_lt _ (by omega)) have mapByteValues : ∀ ns : List Nat, (∀ n, n ∈ ns → n < 256) → List.map (fun b : Std.U8 => b.val) (ns.map byteOfNat) = ns := by intro ns induction ns with | nil => intro; rfl | cons n ns ih => intro hn simp only [List.map_cons, List.map_map, Function.comp_apply] have hlt := hn n List.mem_cons_self have htail := ih (by intro m hm; exact hn m (List.mem_cons_of_mem _ hm)) have hhead : (byteOfNat n).val = n := by simp [byteOfNat, UScalar.ofNatCore_val_eq, Nat.mod_eq_of_lt hlt] rw [hhead] simpa only [List.map_map] using congrArg (List.cons n) htail let intBytes := intNat.map byteOfNat let dotBytes := dotNat.map byteOfNat let expBytes := expNat.map byteOfNat have mapByteInj : Function.Injective (List.map (fun b : Std.U8 => b.val)) := by intro l1 l2 heq induction l1 generalizing l2 with | nil => cases l2 <;> simp at heq ⊢ | cons a xs ih => cases l2 with | nil => simp at heq | cons b bs => simp only [List.map_cons] at heq injection heq with hhead htail have hab : a = b := UScalar.eq_of_val_eq hhead subst b exact congrArg (List.cons a) (ih htail) have hsourceBytes : s.val = intBytes ++ dotBytes ++ expBytes := by apply mapByteInj have hmap : s.val.map (fun b => b.val) = (renderedNat.map byteOfNat).map (fun b => b.val) := by rw [mapByteValues renderedNat hrenderSmall] exact hrenderNat'.symm simpa [intBytes, dotBytes, expBytes, renderedNat, List.map_append, List.append_assoc] using hmap let scanTail := dotBytes ++ expBytes have hscanPartition : s.val = intBytes ++ scanTail := by simpa [scanTail, List.append_assoc] using hsourceBytes have hscanDigits : ∀ b, b ∈ intBytes → 48 ≤ b.val ∧ b.val ≤ 57 := by intro b hb rcases List.mem_map.mp hb with ⟨n, hn, rfl⟩ rcases List.mem_map.mp hn with ⟨d, _, rfl⟩ simp [byteOfNat] have hscanTail : ∀ b rest, scanTail = b :: rest → ¬ (48 ≤ b.val ∧ b.val ≤ 57) := by intro b rest htail cases hf : x.fractionDigits with | some ds => have : b = byteOfNat 46 := by simp [scanTail, dotBytes, dotNat, hf, List.map_append] at htail exact htail.1.symm rw [this] norm_num [byteOfNat] | none => cases he : x.exponent with | none => simp [scanTail, dotBytes, dotNat, expBytes, expNat, hf, he] at htail | some e => rcases e with ⟨upper, sign, ds⟩ have : b = byteOfNat (if upper then 69 else 101) := by simp [scanTail, dotBytes, dotNat, expBytes, expNat, hf, he, List.map_append] at htail exact htail.1.symm rw [this] cases upper <;> norm_num [byteOfNat] have foldZero : ∀ ns : List Nat, (∀ n, n ∈ ns → n = 48) → (ns.map byteOfNat).foldl (fun a b => 10 * a + (b.val - 48)) 0 = 0 := by intro ns induction ns with | nil => intro; rfl | cons n ns ih => intro hn have hn48 := hn n List.mem_cons_self simp only [List.map_cons, List.foldl_cons] have hb : (byteOfNat n).val = 48 := by rw [hn48]; norm_num [byteOfNat] simp [hb, ih (by intro m hm; exact hn m (List.mem_cons_of_mem _ hm))] have hscanFold : intBytes.foldl (fun a b => 10 * a + (b.val - 48)) 0 = 0 := by simpa [intBytes] using foldZero intNat (by intro n hn rcases List.mem_map.mp hn with ⟨v, _, rfl⟩ rfl) let fracBytes := fracNat.map byteOfNat have hfracFold : fracBytes.foldl (fun a b => 10 * a + (b.val - 48)) 0 = 0 := by simpa [fracBytes] using foldZero fracNat (by intro n hn rcases List.mem_map.mp hn with ⟨v, _, rfl⟩ rfl) have hfracBytesZero : ∀ b, b ∈ fracBytes → b.val = 48 := by intro b hb rcases List.mem_map.mp hb with ⟨n, hn, rfl⟩ rcases List.mem_map.mp hn with ⟨v, _, rfl⟩ simp [byteOfNat] have hfracLeDot : fracBytes.length ≤ dotBytes.length := by cases hf : x.fractionDigits with | none => simp [dotBytes, dotNat, fracBytes, fracNat, fracDigits, hf] | some ds => simp [dotBytes, dotNat, fracBytes, fracNat, fracDigits, hf] have hfracSuffixLe : (fracBytes ++ expBytes).length ≤ s.val.length := by calc (fracBytes ++ expBytes).length = fracBytes.length + expBytes.length := by simp _ ≤ dotBytes.length + expBytes.length := Nat.add_le_add_right hfracLeDot _ _ ≤ (intBytes ++ dotBytes ++ expBytes).length := by simp [List.length_append] _ = s.val.length := by rw [hsourceBytes] let fractionSlice : Slice Std.U8 := ⟨fracBytes ++ expBytes, le_trans hfracSuffixLe s.property⟩ have hfracTailNotDigit : ∀ b rest, expBytes = b :: rest → ¬ (48 ≤ b.val ∧ b.val ≤ 57) := by intro b rest he cases hx : x.exponent with | none => simp [expBytes, expNat, hx] at he | some tuple => rcases tuple with ⟨upper, sign, digits⟩ have hhead : List.head? expBytes = some b := by rw [he]; rfl cases upper <;> simp [expBytes, expNat, hx, byteOfNat] at hhead all_goals rw [← hhead] <;> norm_num have hfracByteDigits : ∀ b, b ∈ fracBytes → 48 ≤ b.val ∧ b.val ≤ 57 := by intro b hb rw [hfracBytesZero b hb] norm_num have hfracFoldBound : fracBytes.foldl (fun a b => 10 * a + (b.val - 48)) (0#u64).val ≤ U64.max := by change fracBytes.foldl (fun a b => 10 * a + (b.val - 48)) 0 ≤ U64.max rw [hfracFold] exact Nat.zero_le _ have hfracSource : SourceValidByteSlice fractionSlice := by change fractionSlice.val.length ≤ 9223372036854775807 exact Nat.le_trans hfracSuffixLe hsource obtain ⟨fractionValue, fractionRest, hfractionScan, hfractionRest, hfractionValue⟩ := NumberHelperDigitScanExact fractionSlice 0#u64 fracBytes expBytes (by simp [fractionSlice]) hfracByteDigits hfracTailNotDigit hfracSource hfracFoldBound have hfractionValueZero : fractionValue.val = 0 := by simpa [hfracFold] using hfractionValue have hfoldBound : intBytes.foldl (fun a b => 10 * a + (b.val - 48)) (0#u64).val ≤ U64.max := by simpa [hscanFold] using (Nat.zero_le U64.max) have hscanCert := NumberHelperDigitScanExact s 0#u64 intBytes scanTail hscanPartition hscanDigits hscanTail hsource hfoldBound rcases hscanCert with ⟨scanValue, scanRest, hcert, hrest, hscanValue⟩ have hscanPair : scanOut = (scanRest, scanValue) := by rw [hcert] at hscan injection hscan with hpair exact hpair.symm subst scanOut have hscanValueZero : scanValue.val = 0 := by simpa [hscanFold] using hscanValue have hscanRestEq : scanRest.val = scanTail := hrest let tailSlice : Slice Std.U8 := ⟨scanTail, by rw [← hscanRestEq] exact scanRest.property⟩ have hscanRestSlice : scanRest = tailSlice := (Slice.eq_iff _ _).2 hscanRestEq obtain ⟨nDigits, hoffset, hafterOffset⟩ := bind_ok_inv _ _ _ hafterScan have htailLe : scanRest.val.length ≤ s.val.length := by rw [hscanRestEq, hscanPartition] simp obtain ⟨off, hoff, hoffVal⟩ := P364_T1_NumberHelperOffsetSourceLength s scanRest hsource htailLe have hoffEq : nDigits = off := by rw [hoff] at hoffset injection hoffset with h exact h.symm have hcountEq : nDigits.val = x.integerDigits.length := by have hintByteLen : intBytes.length = x.integerDigits.length := by simp [intBytes, intNat] have hlenPartitionNat : s.val.length = intBytes.length + scanTail.length := by rw [hscanPartition] simp have hlenPartition : (s.val.length : Int) = (intBytes.length : Int) + (scanTail.length : Int) := by exact_mod_cast hlenPartitionNat have hrestLen : (scanRest.val.length : Int) = (scanTail.length : Int) := by exact_mod_cast congrArg List.length hscanRestEq have hoffInt : off.val = (intBytes.length : Int) := by rw [hoffVal, hrestLen, hlenPartition] omega rw [hoffEq, hoffInt, hintByteLen] obtain ⟨dotOpt, hsplit, hafterSplit⟩ := bind_ok_inv _ _ _ hafterOffset have hfracActual : ∀ ds, x.fractionDigits = some ds → dotOpt = some (46#u8, fractionSlice) := by intro ds hf have hdotBytes : dotBytes = 46#u8 :: fracBytes := by simp only [dotBytes, dotNat, hf, List.map_cons, fracBytes] have h46 : byteOfNat 46 = 46#u8 := by apply UScalar.eq_of_val_eq simp [byteOfNat, UScalar.ofNatCore_val_eq] rw [h46] have hshape : scanTail = 46#u8 :: (fracBytes ++ expBytes) := by simp [scanTail, hdotBytes] have hv : scanRest.val = 46#u8 :: (fracBytes ++ expBytes) := by rw [hscanRestEq, hshape] have hs : scanRest = ⟨46#u8 :: (fracBytes ++ expBytes), by rw [← hv] exact scanRest.property⟩ := (Slice.eq_iff _ _).2 hv rw [hs] at hsplit simp [core.slice.Slice.split_first, ModelSliceSplitFirst, fractionSlice] at hsplit exact hsplit.symm obtain ⟨afterSplit, hsplitContinuation, hafterSplitContinuation⟩ := bind_ok_inv _ _ _ hafterSplit rcases afterSplit with ⟨s2, mantissa1, nAfterDot, baseExponent⟩ have hscanValueEq : scanValue = 0#u64 := by apply UScalar.eq_of_val_eq exact hscanValueZero have hfracOutputs : ∀ ds, x.fractionDigits = some ds → s2 = fractionRest ∧ mantissa1 = fractionValue ∧ nAfterDot.val = ds.length ∧ baseExponent.val = -(ds.length : Int) := by intro ds hf have hfracLenDs : fracBytes.length = ds.length := by simp [fracBytes, fracNat, fracDigits, hf, List.length_map] have hrestLe : fractionRest.val.length ≤ fractionSlice.val.length := by rw [hfractionRest] change expBytes.length ≤ (fracBytes ++ expBytes).length simp obtain ⟨fracCount, hoffFrac, hoffFracVal⟩ := P364_T1_NumberHelperOffsetSourceLength fractionSlice fractionRest hfracSource hrestLe have hfracCountVal : fracCount.val = ds.length := by rw [hoffFracVal, hfractionRest] simp [fractionSlice, hfracLenDs] have hnotmin : fracCount.val ≠ IScalar.min .Isize := by rw [hfracCountVal, IScalar.min] norm_num [IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits] have hds : ds.length ≤ s.val.length := by rw [← hfracLenDs] exact Nat.le_trans (by simp) hfracSuffixLe have hsmax : s.val.length ≤ 9223372036854775807 := by simpa [SourceValidByteSlice] using hsource omega obtain ⟨fracNeg, hneg, hnegVal⟩ := Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step fracCount hnotmin) have hnegBounds : IScalar.min .I64 ≤ fracNeg.val ∧ fracNeg.val ≤ IScalar.max .I64 := by rw [hnegVal, hfracCountVal, IScalar.min_IScalarTy_I64_eq, I64.min_eq, IScalar.max_IScalarTy_I64_eq, I64.max_eq] have hds : ds.length ≤ s.val.length := by rw [← hfracLenDs] exact Nat.le_trans (by simp) hfracSuffixLe have hsmax : s.val.length ≤ 9223372036854775807 := by simpa [SourceValidByteSlice] using hsource omega obtain ⟨base, hcast, hbaseVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.cast_inBounds_spec .I64 fracNeg hnegBounds) rw [hfracActual ds hf] at hsplitContinuation dsimp at hsplitContinuation rw [hscanValueEq, hfractionScan] at hsplitContinuation simp only [bind_tc_ok] at hsplitContinuation simp [UScalar.ofNat, UScalar.ofNatCore] at hsplitContinuation rw [hoffFrac] at hsplitContinuation simp only [bind_tc_ok] at hsplitContinuation rw [hneg] at hsplitContinuation simp only [bind_tc_ok] at hsplitContinuation rw [hcast] at hsplitContinuation simp only [bind_tc_ok] at hsplitContinuation simp at hsplitContinuation rcases hsplitContinuation with ⟨h1, h2, h3, h4⟩ exact ⟨h1.symm, h2.symm, by rw [← h3, hfracCountVal], by rw [← h4, hbaseVal, hnegVal, hfracCountVal]⟩ have hnoFractionOutputs : x.fractionDigits = none → s2 = scanRest ∧ mantissa1 = scanValue ∧ nAfterDot = 0#isize ∧ baseExponent = 0#i64 := by intro hf cases he : x.exponent with | none => have hnil : scanTail = [] := by simp [scanTail, dotBytes, dotNat, expBytes, expNat, hf, he] have hv : scanRest.val = [] := by rw [hscanRestEq, hnil] have hs : scanRest = ⟨[], by rw [← hv]; exact scanRest.property⟩ := (Slice.eq_iff _ _).2 hv have hd : dotOpt = none := by rw [hs] at hsplit simp [core.slice.Slice.split_first, ModelSliceSplitFirst] at hsplit exact hsplit.symm rw [hd] at hsplitContinuation simp at hsplitContinuation rcases hsplitContinuation with ⟨h1, h2, h3, h4⟩ exact ⟨h1.symm, h2.symm, h3.symm, h4.symm⟩ | some exponentData => rcases exponentData with ⟨upper, sign, digits⟩ let marker : Std.U8 := if upper then 69#u8 else 101#u8 let signNat := (match sign with | none => [] | some neg => [if neg then 45 else 43]) let rest := signNat.map byteOfNat ++ digits.map (fun n => byteOfNat (48 + n)) have htail : scanTail = marker :: rest := by cases upper <;> simp [scanTail, dotBytes, dotNat, expBytes, expNat, hf, he, marker, rest, signNat, List.map_append, byteOfNat] have hrestLen : rest.length ≤ Usize.max := by have h := scanRest.property rw [hscanRestEq, htail] at h simp at h omega let restSlice : Slice Std.U8 := ⟨rest, hrestLen⟩ have hv : scanRest.val = marker :: rest := by rw [hscanRestEq, htail] have hs : scanRest = ⟨marker :: rest, by rw [← hv]; exact scanRest.property⟩ := (Slice.eq_iff _ _).2 hv have hd : dotOpt = some (marker, restSlice) := by rw [hs] at hsplit simp [core.slice.Slice.split_first, ModelSliceSplitFirst, restSlice] at hsplit exact hsplit.symm rw [hd] at hsplitContinuation cases upper with | false => have htuple : scanRest = s2 ∧ scanValue = mantissa1 ∧ (0#isize : Std.Isize) = nAfterDot ∧ (0#i64 : Std.I64) = baseExponent := by simpa [marker, UScalar.ofNat, UScalar.ofNatCore] using hsplitContinuation rcases htuple with ⟨h1, h2, h3, h4⟩ exact ⟨h1.symm, h2.symm, h3.symm, h4.symm⟩ | true => have htuple : scanRest = s2 ∧ scanValue = mantissa1 ∧ (0#isize : Std.Isize) = nAfterDot ∧ (0#i64 : Std.I64) = baseExponent := by simpa [marker, UScalar.ofNat, UScalar.ofNatCore] using hsplitContinuation rcases htuple with ⟨h1, h2, h3, h4⟩ exact ⟨h1.symm, h2.symm, h3.symm, h4.symm⟩ have hmantissaZero : mantissa1.val = 0 := by cases hf : x.fractionDigits with | none => rw [(hnoFractionOutputs hf).2.1] exact hscanValueZero | some ds => rw [(hfracOutputs ds hf).2.1] exact hfractionValueZero have hpartialFull : num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, len)) := hpartial unfold num.imp.dec2flt.parse.parse_partial_number at hpartialFull simp [hsourceNonempty, hscan, hoffset, hsplit] at hpartialFull rw [hsplitContinuation] at hpartialFull simp only [bind_tc_ok] at hpartialFull obtain ⟨totalDigits, htotal, hpartialAfterCount⟩ := bind_ok_inv _ _ _ hpartialFull have htotalVal : totalDigits.val = nDigits.val + nAfterDot.val := by have heq := IScalar.add_equiv nDigits nAfterDot rw [htotal] at heq exact heq.2.1 let fractionalCount := match x.fractionDigits with | none => 0 | some ds => ds.length let digitCount := x.integerDigits.length + fractionalCount have htotalDigitCount : totalDigits.val = digitCount := by cases hf : x.fractionDigits with | none => have hna := (hnoFractionOutputs hf).2.2.1 rw [hcountEq, hna] at htotalVal simp [digitCount, fractionalCount, hf] at htotalVal ⊢ omega | some ds => have hna := (hfracOutputs ds hf).2.2.1 rw [hcountEq, hna] at htotalVal simp [digitCount, fractionalCount, hf] at htotalVal ⊢ omega have hsourceLength : s.val.length = intBytes.length + dotBytes.length + expBytes.length := by rw [hsourceBytes] simp [List.length_append, Nat.add_assoc] have hintByteLength : intBytes.length = x.integerDigits.length := by simp [intBytes, intNat] have hfractionalByteLength : fractionalCount = fracBytes.length := by cases hf : x.fractionDigits with | none => simp [fractionalCount, fracBytes, fracNat, fracDigits, hf] | some ds => simp [fractionalCount, fracBytes, fracNat, fracDigits, hf] have hdigitCountLe : digitCount ≤ s.val.length := by change x.integerDigits.length + fractionalCount ≤ s.val.length rw [← hintByteLength, hfractionalByteLength] rw [hsourceLength] omega have hdigitCountPos : 0 < digitCount := by cases hf : x.fractionDigits with | none => simp [digitCount, fractionalCount, hf] rcases hnonempty with hi | hfrac · have hp : 0 < x.integerDigits.length := by cases hx : x.integerDigits with | nil => simp [hx] at hi | cons a ds => simp omega · simp [hf] at hfrac | some ds => simp [digitCount, fractionalCount, hf] rcases hnonempty with hi | hfrac · have hp : 0 < x.integerDigits.length := by cases hx : x.integerDigits with | nil => simp [hx] at hi | cons a rest => simp omega · have hp : 0 < ds.length := by cases hd : ds with | nil => have hds : ds ≠ [] := by simpa [hf] using hfrac exact False.elim (hds (by simp [hd])) | cons a rest => simp omega let zeroPrefix := intBytes ++ dotBytes have hbyte48 : byteOfNat 48 = 48#u8 := by apply UScalar.eq_of_val_eq simp [byteOfNat, UScalar.ofNatCore_val_eq] have hintBytesZero : ∀ b, b ∈ intBytes → b = 48#u8 := by intro b hb rcases List.mem_map.mp hb with ⟨n, hn, rfl⟩ rcases List.mem_map.mp hn with ⟨digit, _, rfl⟩ exact hbyte48 have hdotCase : ∀ ds, x.fractionDigits = some ds → dotBytes = 46#u8 :: fracBytes := by intro ds hf simp only [dotBytes, dotNat, hf, List.map_cons, fracBytes] have h46 : byteOfNat 46 = 46#u8 := by apply UScalar.eq_of_val_eq simp [byteOfNat, UScalar.ofNatCore_val_eq] rw [h46] have hprefixChar : ∀ b, b ∈ zeroPrefix → b = 48#u8 ∨ b = 46#u8 := by intro b hb rcases List.mem_append.mp hb with hi | hd · left exact hintBytesZero b hi · cases hf : x.fractionDigits with | none => simp [dotBytes, dotNat, hf] at hd | some ds => rw [hdotCase ds hf] at hd rcases List.mem_cons.mp hd with heq | hfrac · exact Or.inr heq · left have hbval := hfracBytesZero b hfrac apply UScalar.eq_of_val_eq simpa using hbval have countZeros : ∀ bytes : List Std.U8, (∀ b, b ∈ bytes → b = 48#u8) → bytes.count 48#u8 = bytes.length := by intro bytes induction bytes with | nil => intro; rfl | cons b bs ih => intro hall have hb := hall b List.mem_cons_self have htail : ∀ c, c ∈ bs → c = 48#u8 := by intro c hc exact hall c (List.mem_cons_of_mem _ hc) subst b simp [ih htail] have hintCount : intBytes.count 48#u8 = x.integerDigits.length := by rw [countZeros intBytes hintBytesZero, hintByteLength] have hfracCount : fracBytes.count 48#u8 = fracBytes.length := countZeros fracBytes (by intro b hb; apply UScalar.eq_of_val_eq; simpa using hfracBytesZero b hb) have hdotCount : dotBytes.count 48#u8 = fractionalCount := by cases hf : x.fractionDigits with | none => simp [dotBytes, dotNat, fractionalCount, hf] | some ds => rw [hdotCase ds hf] simp [hfracCount, fractionalCount, hf, hfractionalByteLength] have hprefixCount : zeroPrefix.count 48#u8 = digitCount := by change (intBytes ++ dotBytes).count 48#u8 = digitCount rw [List.count_append, hintCount, hdotCount] have hstartAdjustment : s.val = zeroPrefix ++ expBytes := by simpa [zeroPrefix, List.append_assoc] using hsourceBytes have htailAdjustment : expBytes = [] ∨ ∃ b rest, expBytes = b :: rest ∧ b ≠ 48#u8 ∧ b ≠ 46#u8 := by cases ht : expBytes with | nil => exact Or.inl rfl | cons b rest => cases hx : x.exponent with | none => simp [expBytes, expNat, hx] at ht | some tuple => rcases tuple with ⟨upper, sign, digits⟩ have hhead : List.head? expBytes = some b := by rw [ht]; rfl cases upper with | false => have hb : b = byteOfNat 101 := by simpa [expBytes, expNat, hx] using hhead.symm subst b exact Or.inr ⟨byteOfNat 101, rest, rfl, by norm_num [byteOfNat], by norm_num [byteOfNat]⟩ | true => have hb : b = byteOfNat 69 := by simpa [expBytes, expNat, hx] using hhead.symm subst b exact Or.inr ⟨byteOfNat 69, rest, rfl, by norm_num [byteOfNat], by norm_num [byteOfNat]⟩ have hadjustmentExact : ∀ nMore, totalDigits - 19#isize = ok nMore → ¬ (totalDigits.val ≤ 19) → ∃ adjusted, num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ∧ adjusted.val = -19 := by intro nMore hsub hlarge have hsubEquiv := IScalar.sub_equiv totalDigits 19#isize rw [hsub] at hsubEquiv have hnMoreVal : nMore.val = totalDigits.val - 19 := by simpa using hsubEquiv.2.1 have hnMoreNonneg : 0 ≤ nMore.val := by rw [hnMoreVal] omega have hnMoreLe : nMore.val ≤ s.val.length := by rw [hnMoreVal, htotalDigitCount] omega have hadjustLower : IScalar.min .Isize ≤ nMore.val - (zeroPrefix.count 48#u8 : Int) := by rw [hnMoreVal, hprefixCount, htotalDigitCount] simp [IScalar.min, IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits] obtain ⟨adjusted, hadjusted, hadjustedVal⟩ := P364_T1_NumberHelperAdjustCountExact s nMore zeroPrefix expBytes hstartAdjustment hprefixChar htailAdjustment hsource hnMoreNonneg hnMoreLe hadjustLower have hadjustedExact : adjusted.val = -19 := by rw [hprefixCount, ← htotalDigitCount] at hadjustedVal rw [hnMoreVal] at hadjustedVal simp at hadjustedVal omega exact ⟨adjusted, hadjusted, hadjustedExact⟩ have hnoRetry : ∀ nMore adjusted, totalDigits - 19#isize = ok nMore → num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted → ¬ (totalDigits.val ≤ 19) → ¬ (0 < adjusted.val) := by intro nMore adjusted hsub hadjust hlarge hpos obtain ⟨adjustedExact, hadjustedExact, hvalue⟩ := hadjustmentExact nMore hsub hlarge rw [hadjust] at hadjustedExact injection hadjustedExact with hEq subst adjustedExact rw [hvalue] at hpos norm_num at hpos have htotalNonzero : totalDigits ≠ 0#isize := by intro hz simp [hz] at hpartialAfterCount obtain ⟨suffixOpt, hsplitSuffix, hpartialAfterSuffixSplit⟩ := bind_ok_inv _ _ _ (by simpa [htotalNonzero] using hpartialAfterCount) have recordBranchNegative : ∀ (total : Std.Isize) (shortMantissa : Std.U64) (intEnd : Slice Std.U8) (shortExponent retryExponent : Std.I64) (consumed outLen : Std.Usize), (if total.val ≤ 19 then ok (some (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa false false, consumed)) else do let nMore ← total - 19#isize let adjusted ← num.imp.dec2flt.parse.adjust_many_digit_count s nMore if 0 < adjusted.val then let (manyMantissa, manyExponent) ← num.imp.dec2flt.parse.parse_many_digits s intEnd retryExponent ok (some (num.imp.dec2flt.decimal.Decimal.mk manyExponent manyMantissa false true, consumed)) else ok (some (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa false false, consumed))) = ok (some (d, outLen)) → d.negative = false := by intro total shortMantissa intEnd shortExponent retryExponent consumed outLen hresult by_cases hsmall : total.val ≤ 19 · simp [hsmall] at hresult rcases hresult with ⟨rfl, rfl⟩ rfl · simp [hsmall] at hresult obtain ⟨nMore, hsub, hresult⟩ := bind_ok_inv _ _ _ hresult obtain ⟨adjusted, hadjust, hresult⟩ := bind_ok_inv _ _ _ hresult by_cases hpositive : 0 < adjusted.val · simp [hpositive] at hresult obtain ⟨manyResult, hmany, hresult⟩ := bind_ok_inv _ _ _ hresult rcases manyResult with ⟨manyMantissa, manyExponent⟩ rcases hresult with ⟨rfl, rfl⟩ rfl · simp [hpositive] at hresult rcases hresult with ⟨rfl, rfl⟩ rfl have recordBranchZero : ∀ (total : Std.Isize) (shortMantissa : Std.U64) (intEnd : Slice Std.U8) (shortExponent retryExponent : Std.I64) (consumed outLen : Std.Usize), shortMantissa.val = 0 → (∀ nMore adjusted, total - 19#isize = ok nMore → num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted → ¬ (total.val ≤ 19) → ¬ (0 < adjusted.val)) → (if total.val ≤ 19 then ok (some (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa false false, consumed)) else do let nMore ← total - 19#isize let adjusted ← num.imp.dec2flt.parse.adjust_many_digit_count s nMore if 0 < adjusted.val then let (manyMantissa, manyExponent) ← num.imp.dec2flt.parse.parse_many_digits s intEnd retryExponent ok (some (num.imp.dec2flt.decimal.Decimal.mk manyExponent manyMantissa false true, consumed)) else ok (some (num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa false false, consumed))) = ok (some (d, outLen)) → d.mantissa.val = 0 ∧ d.many_digits = false := by intro total shortMantissa intEnd shortExponent retryExponent consumed outLen hshort hretry hresult by_cases hsmall : total.val ≤ 19 · simp [hsmall] at hresult rcases hresult with ⟨rfl, rfl⟩ exact ⟨hshort, rfl⟩ · simp [hsmall] at hresult obtain ⟨nMore, hsub, hresult⟩ := bind_ok_inv _ _ _ hresult obtain ⟨adjusted, hadjust, hresult⟩ := bind_ok_inv _ _ _ hresult by_cases hpositive : 0 < adjusted.val · have hfalse := hretry nMore adjusted hsub hadjust hsmall hpositive contradiction · simp [hpositive] at hresult rcases hresult with ⟨rfl, rfl⟩ exact ⟨hshort, rfl⟩ have hnegative : d.negative = false := by cases suffixOpt with | none => simp at hpartialAfterSuffixSplit obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest exact recordBranchNegative totalDigits mantissa1 scanRest baseExponent 0#i64 outLen len (by simpa using hrest) | some pair => rcases pair with ⟨marker, suffix⟩ by_cases hlower : marker = 101#u8 · subst marker obtain ⟨sciResult, hsci, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit rcases sciResult with ⟨optionExp, sciRest⟩ cases optionExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hrest | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hrest obtain ⟨combined, hsum, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest exact recordBranchNegative totalDigits mantissa1 scanRest combined parsedExp outLen len (by simpa using hrest) · by_cases hupper : marker = 69#u8 · subst marker obtain ⟨sciResult, hsci, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit rcases sciResult with ⟨optionExp, sciRest⟩ cases optionExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hrest | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hrest obtain ⟨combined, hsum, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest exact recordBranchNegative totalDigits mantissa1 scanRest combined parsedExp outLen len (by simpa using hrest) · simp [hlower, hupper] at hpartialAfterSuffixSplit obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest exact recordBranchNegative totalDigits mantissa1 scanRest baseExponent 0#i64 outLen len (by simpa using hrest) have hfields : d.mantissa.val = 0 ∧ d.many_digits = false := by cases suffixOpt with | none => simp at hpartialAfterSuffixSplit obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest apply recordBranchZero totalDigits mantissa1 scanRest baseExponent 0#i64 outLen len · exact hmantissaZero · exact hnoRetry · simpa using hrest | some pair => rcases pair with ⟨marker, suffix⟩ by_cases hlower : marker = 101#u8 · subst marker obtain ⟨sciResult, hsci, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit rcases sciResult with ⟨optionExp, sciRest⟩ cases optionExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hrest | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hrest obtain ⟨combined, hsum, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest apply recordBranchZero totalDigits mantissa1 scanRest combined parsedExp outLen len · exact hmantissaZero · exact hnoRetry · simpa using hrest · by_cases hupper : marker = 69#u8 · subst marker obtain ⟨sciResult, hsci, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit rcases sciResult with ⟨optionExp, sciRest⟩ cases optionExp with | none => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, ModelOptionFromResidual] at hrest | some parsedExp => simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch] at hrest obtain ⟨combined, hsum, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hrest obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest apply recordBranchZero totalDigits mantissa1 scanRest combined parsedExp outLen len · exact hmantissaZero · exact hnoRetry · simpa using hrest · simp [hlower, hupper] at hpartialAfterSuffixSplit obtain ⟨off, hoff, hrest⟩ := bind_ok_inv _ _ _ hpartialAfterSuffixSplit obtain ⟨outLen, hcast, hrest⟩ := bind_ok_inv _ _ _ hrest apply recordBranchZero totalDigits mantissa1 scanRest baseExponent 0#i64 outLen len · exact hmantissaZero · exact hnoRetry · simpa using hrest refine ⟨?_, ?_, hnegative⟩ · exact hfields.1 · exact hfields.2 end dec2flt_model