import Tablet.NumberHelperDigitScanSourceLength import Tablet.NumberHelperOffsetSourceLength import Tablet.NumberHelperAdjustManySourceLength import Tablet.NumberHelperIntegralManyDigitsSourceLength import Tablet.NumberHelperFractionalManyDigitsSourceLength import Tablet.NumberHelperScientificPrefixSafeLength import Tablet.NumberHelperScientificClassificationSourceLength import Tablet.NumberHelperScientificExponentBounds import Tablet.DecimalGoalVocabularyDefault import Tablet.SourceValidByteSlice import Tablet.ModelSliceSplitFirst import Tablet.ModelOptionBranch import Tablet.ModelOptionFromResidual import Tablet.num_imp_dec2flt_parse_parse_partial_number open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 800000 set_option maxRecDepth 10000 namespace dec2flt_model -- [TABLET NODE: NumberHelperPartialNumberSafeLength] theorem NumberHelperPartialNumberSafeLength : forall s : Slice Std.U8, SourceValidByteSlice s -> s.val != [] -> s.val.length <= 9223372036854120459 -> (Not (exists x : DecimalLexeme, x.leadingSign = none /\ DecimalGoalVocabularyDefault.validNumeric x /\ DecimalGoalVocabularyDefault.bytes s = DecimalGoalVocabularyDefault.renderNumeric x)) -> exists o, num.imp.dec2flt.parse.parse_partial_number s = ok o /\ forall (d : num.imp.dec2flt.decimal.Decimal) (r : Std.Usize), o = some (d, r) -> Not (r = Slice.len s) := by -- BODY intro s hsource hne hlen hinvalid have hsourceLen : s.val.length <= 9223372036854775807 := by simp [SourceValidByteSlice] at hsource exact hsource have split_digits : forall bs : List Std.U8, exists digits tail, bs = digits ++ tail /\ (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) /\ (forall b rest, tail = b :: rest -> Not (48 <= b.val /\ b.val <= 57)) := by intro bs induction bs with | nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), (by intro b rest h; cases h)⟩ | cons b bs ih => by_cases hb : 48 <= b.val /\ b.val <= 57 · obtain ⟨digits, tail, hbs, hdigits, htail⟩ := ih refine ⟨b :: digits, tail, ?_, ?_, htail⟩ · simp [hbs] · intro c hc rcases List.mem_cons.mp hc with rfl | hc · exact hb · exact hdigits c hc · exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc), by intro c rest h cases h exact hb⟩ let scientific_sign : List Std.U8 -> Option Bool := fun bs => match bs with | [] => none | b :: _ => if b = 45#u8 then some true else if b = 43#u8 then some false else none let scientific_body : List Std.U8 -> List Std.U8 := fun bs => match bs with | [] => [] | b :: rest => if b = 45#u8 \/ b = 43#u8 then rest else b :: rest have scientific_split : forall bs : List Std.U8, bs = (match scientific_sign bs with | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++ scientific_body bs := by intro bs cases bs with | nil => simp [scientific_sign, scientific_body] | cons b rest => by_cases hm : b = 45#u8 · subst b simp [scientific_sign, scientific_body] · by_cases hp : b = 43#u8 · subst b simp [scientific_sign, scientific_body] · simp [scientific_sign, scientific_body, hm, hp] have scientific_unsigned : forall bs : List Std.U8, scientific_sign bs = none -> forall b rest, scientific_body bs = b :: rest -> b ≠ 45#u8 /\ b ≠ 43#u8 := by intro bs cases bs with | nil => simp [scientific_body] | cons b rest => by_cases hm : b = 45#u8 · subst b simp [scientific_sign] · by_cases hp : b = 43#u8 · subst b simp [scientific_sign, hm] · simp only [scientific_sign, scientific_body, hm, hp, if_false, or_false] intro _ c cs h injection h with hbc _ subst c exact ⟨hm, hp⟩ have render_digits : forall bs : List Std.U8, (forall b, List.Mem b bs -> 48 <= b.val /\ b.val <= 57) -> List.map (fun b : Std.U8 => b.val) bs = List.map (fun d : Nat => 48 + d) (List.map (fun b : Std.U8 => b.val - 48) bs) := by intro bs hbs induction bs with | nil => rfl | cons b bs ih => have hb := hbs b List.mem_cons_self have hrest : forall c, List.Mem c bs -> 48 <= c.val /\ c.val <= 57 := by intro c hc exact hbs c (List.mem_cons_of_mem b hc) simp [ih hrest] omega have complete_spelling : forall (integer : List Std.U8) (fraction : Option (List Std.U8)) (expo : Option (Bool × Option Bool × List Std.U8)), (forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) -> (match fraction with | none => True | some ds => forall b, List.Mem b ds -> 48 <= b.val /\ b.val <= 57) -> (integer != [] \/ match fraction with | none => False | some ds => ds != []) -> (match expo with | none => True | some (_, _, ds) => ds != [] /\ forall b, List.Mem b ds -> 48 <= b.val /\ b.val <= 57) -> s.val = integer ++ (match fraction with | none => [] | some ds => 46#u8 :: ds) ++ (match expo with | none => [] | some (upper, sign, ds) => (if upper then 69#u8 else 101#u8) :: (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ ds) -> False := by intro integer fraction expo hint hfrac hsome hexpo hs apply hinvalid let x : DecimalLexeme := { leadingSign := none integerDigits := integer.map (fun b => b.val - 48) fractionDigits := fraction.map (fun ds => ds.map (fun b => b.val - 48)) exponent := expo.map (fun e => (e.1, e.2.1, e.2.2.map (fun b => b.val - 48))) } refine ⟨x, rfl, ?_, ?_⟩ · simp [DecimalGoalVocabularyDefault, x] constructor · intro d hd have := hint d hd omega constructor · cases fraction with | none => simp | some fs => simp only [Option.map_some] intro d hd obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hd have := hfrac b hb omega constructor · cases fraction <;> simpa using hsome · cases expo with | none => simp | some e => rcases e with ⟨upper, sign, es⟩ simp at hexpo ⊢ refine ⟨hexpo.1, ?_⟩ intro d hd have := hexpo.2 d hd omega · simp [DecimalGoalVocabularyDefault, x] rw [hs] simp only [List.map_append, List.map_cons] have hintRender : List.map ((fun d : Nat => 48 + d) ∘ fun b : Std.U8 => b.val - 48) integer = List.map (fun b : Std.U8 => b.val) integer := by simpa [List.map_map] using (render_digits integer hint).symm rw [hintRender] cases fraction with | none => simp only [Option.map_none] simp at hfrac cases expo with | none => simp | some e => rcases e with ⟨upper, sign, es⟩ simp only [Option.map_some, List.map_map] simp at hexpo ⊢ have hexpRender : List.map ((fun d : Nat => 48 + d) ∘ fun b : Std.U8 => b.val - 48) es = List.map (fun b : Std.U8 => b.val) es := by simpa [List.map_map] using (render_digits es hexpo.2).symm rw [hexpRender] cases upper <;> cases sign with | none => norm_num | some neg => cases neg <;> norm_num | some fs => simp only [Option.map_some, List.map_map] simp at hfrac have hfracRender : List.map ((fun d : Nat => 48 + d) ∘ fun b : Std.U8 => b.val - 48) fs = List.map (fun b : Std.U8 => b.val) fs := by simpa [List.map_map] using (render_digits fs hfrac).symm rw [hfracRender] cases expo with | none => simp | some e => rcases e with ⟨upper, sign, es⟩ simp only [Option.map_some, List.map_map] simp at hexpo ⊢ have hexpRender : List.map ((fun d : Nat => 48 + d) ∘ fun b : Std.U8 => b.val - 48) es = List.map (fun b : Std.U8 => b.val) es := by simpa [List.map_map] using (render_digits es hexpo.2).symm rw [hexpRender] cases upper <;> cases sign with | none => norm_num | some neg => cases neg <;> norm_num obtain ⟨integer, intTailBytes, hsval, hintDigits, hintTail⟩ := split_digits s.val obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ := NumberHelperDigitScanSourceLength s 0#u64 integer intTailBytes hsval hintDigits hintTail hsourceLen obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ := NumberHelperOffsetSourceLength s intRest hsourceLen (by rw [hintRest, hsval] simp) have hnDigitsEq : nDigits.val = integer.length := by rw [hnDigitsVal, hintRest, hsval] simp have haddZero : (nDigits + 0#isize) = ok nDigits := by have hb := IScalar.hBounds nDigits obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec (x := nDigits) (y := 0#isize) (by simp [IScalar.min, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] omega) (by simp [IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega)) rw [hz] congr 2 apply IScalar.eq_of_val_eq simpa using hzval have hnotempty : Not (s.val = []) := by simpa using hne cases htailEq : intTailBytes with | nil => have hintegerNeProp : integer ≠ [] := by intro hi rw [hi, htailEq] at hsval simp at hsval exact hnotempty hsval have hintegerNe : integer != [] := by simpa only [bne_iff_ne] using hintegerNeProp exact False.elim (complete_spelling integer none none hintDigits (by simp) (by left; exact hintegerNe) (by simp) (by simpa [htailEq] using hsval)) | cons c cs => have hintRestVal : intRest.val = c :: cs := by simpa [htailEq] using hintRest have hcsProp : cs.length <= Usize.max := by have hp := intRest.property rw [hintRestVal] at hp simp only [List.length_cons] at hp omega let afterFirst : Slice Std.U8 := ⟨cs, hcsProp⟩ have h46Target : (46#u8 : Std.U8) = 46#uscalar := by apply UScalar.eq_of_val_eq rfl have h101Target : (101#u8 : Std.U8) = 101#uscalar := by apply UScalar.eq_of_val_eq rfl have h69Target : (69#u8 : Std.U8) = 69#uscalar := by apply UScalar.eq_of_val_eq rfl have hsplitInt : core.slice.Slice.split_first intRest = ok (some (c, afterFirst)) := by have hirest : intRest = (⟨c :: cs, by simpa [hintRestVal] using intRest.property⟩ : Slice Std.U8) := (Slice.eq_iff _ _).2 hintRestVal rw [hirest] simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst, afterFirst] by_cases hpoint : c = 46#u8 · subst c obtain ⟨fraction, fracTail, hcs, hfracDigits, hfracTail⟩ := split_digits cs have hafterVal : afterFirst.val = fraction ++ fracTail := by simp [afterFirst, hcs] have hafterLen : afterFirst.val.length <= 9223372036854775807 := by rw [hafterVal] rw [hsval, htailEq, hcs] at hlen simp at hlen rw [List.length_append] omega obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ := NumberHelperDigitScanSourceLength afterFirst mantissa fraction fracTail hafterVal hfracDigits hfracTail hafterLen obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ := NumberHelperOffsetSourceLength afterFirst fracRest hafterLen (by rw [hfracRest, hafterVal] simp) have hnAfterEq : nAfter.val = fraction.length := by rw [hnAfterVal, hfracRest, hafterVal] simp have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by rw [hnAfterEq, IScalar.min] simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ := Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step nAfter hnAfterNotMin) have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\ negAfter.val <= IScalar.max .I64 := by rw [hnegAfterVal, hnAfterEq, IScalar.min_IScalarTy_I64_eq, I64.min_eq, IScalar.max_IScalarTy_I64_eq, I64.max_eq] have hl := hlen rw [hsval, htailEq, hcs] at hl simp at hl constructor <;> omega obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds) have htotalBounds : IScalar.min .Isize <= nDigits.val + nAfter.val /\ nDigits.val + nAfter.val <= IScalar.max .Isize := by rw [hnDigitsEq, hnAfterEq] simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] have hl := hlen rw [hsval, htailEq, hcs] at hl simp at hl constructor <;> omega obtain ⟨totalDigits, htotal, htotalVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec htotalBounds.1 htotalBounds.2) by_cases htotalZero : totalDigits = 0#isize · unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac, hnegAfter, hfracExponent, htotal, htotalZero] norm_num simp only [UScalar.ofNat, UScalar.ofNatCore] simp [htotal, htotalZero] · have htotalPos : 0 < totalDigits.val := by have hb := IScalar.hBounds totalDigits have hz : totalDigits.val ≠ 0 := by intro hv apply htotalZero apply IScalar.eq_of_val_eq simpa using hv rw [htotalVal, hnDigitsEq, hnAfterEq] at hz ⊢ omega have hsome : integer != [] \/ fraction != [] := by simp only [bne_iff_ne] by_contra hnone push_neg at hnone rcases hnone with ⟨hi, hf⟩ subst integer subst fraction rw [htotalVal, hnDigitsEq, hnAfterEq] at htotalPos norm_num at htotalPos cases htailCase : fracTail with | nil => exact False.elim (complete_spelling integer (some fraction) none hintDigits (by simpa using hfracDigits) hsome (by simp) (by rw [hsval, htailEq, hcs, htailCase] simp [List.append_assoc])) | cons q qs => have hqsProp : qs.length <= Usize.max := by have hp := fracRest.property rw [hfracRest, htailCase] at hp simp at hp ⊢ omega let afterFrac : Slice Std.U8 := ⟨qs, hqsProp⟩ have hsplitFrac : core.slice.Slice.split_first fracRest = ok (some (q, afterFrac)) := by have hr : fracRest = (⟨q :: qs, by rw [← htailCase, ← hfracRest] exact fracRest.property⟩ : Slice Std.U8) := (Slice.eq_iff _ _).2 (by simpa [htailCase] using hfracRest) rw [hr] simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst, afterFrac] by_cases hfracMarker : q = 101#u8 \/ q = 69#u8 · obtain ⟨upper, hmarkerByte⟩ : exists upper : Bool, q = if upper then 69#u8 else 101#u8 := by rcases hfracMarker with hq | hq · exact ⟨false, by simpa using hq⟩ · exact ⟨true, by simpa using hq⟩ let sign := scientific_sign qs let expBody := scientific_body qs have hqsSign : qs = (match sign with | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++ expBody := by exact scientific_split qs have hunsigned : sign = none -> forall b rest, expBody = b :: rest -> b ≠ 45#u8 /\ b ≠ 43#u8 := by exact scientific_unsigned qs obtain ⟨expDigits, expTail, hbody, hexpDigits, hexpTail⟩ := split_digits expBody have hsci : afterFrac.val = (match sign with | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++ expDigits ++ expTail := by change qs = _ rw [hqsSign, hbody, List.append_assoc] obtain hnoexp | hexp := NumberHelperScientificClassificationSourceLength afterFrac sign expDigits expTail hexpDigits hexpTail hsci (by intro hd hs b rest ht apply hunsigned hs b rest rw [hbody, hd] simpa using ht) · rcases hnoexp with ⟨_, rest, hscientific⟩ unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac, hnegAfter, hfracExponent, htotal, htotalPos, hsplitFrac] norm_num simp only [UScalar.ofNat, UScalar.ofNatCore] cases upper <;> simp at hmarkerByte <;> subst q <;> simp [htotal, htotalPos, hsplitFrac, h101Target, h69Target, hscientific, core.option.Option.Insts.CoreOpsTry_traitTry.branch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, dec2flt_model.ModelOptionBranch, dec2flt_model.ModelOptionFromResidual] · rcases hexp with ⟨hexpNe, parsedExp, sciRest, hscientific, hsciRest, hexplo, hexphi, hexpshort, hexpnonnegative⟩ cases htailExp : expTail with | nil => exact False.elim (complete_spelling integer (some fraction) (some (upper, sign, expDigits)) hintDigits hfracDigits hsome ⟨hexpNe, hexpDigits⟩ (by rw [hsval, htailEq, hcs, htailCase, hmarkerByte, hqsSign, hbody, htailExp] simp [List.append_assoc])) | cons z zs => obtain ⟨consumed, hoffConsumed, hconsumedVal⟩ := NumberHelperOffsetSourceLength s sciRest hsourceLen (by rw [hsciRest, htailExp] have hl := congrArg List.length hsval rw [htailEq, hcs, htailCase, hqsSign, hbody, htailExp] at hl simp at hl ⊢ omega) have hcastBounds : 0 <= consumed.val /\ consumed.val <= UScalar.max .Usize := by rw [hconsumedVal] have hrestLe : sciRest.val.length <= s.val.length := by rw [hsciRest, htailExp] have hl := congrArg List.length hsval rw [htailEq, hcs, htailCase, hqsSign, hbody, htailExp] at hl simp at hl ⊢ omega simp [UScalar.max, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨len, hcastLen, hlenVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize consumed hcastBounds) have hlenNe : len ≠ Slice.len s := by intro heq have heqv : (len.val : Int) = s.val.length := by have h := congrArg UScalar.val heq simpa [Slice.len_val] using h have hrestPos : 0 < sciRest.val.length := by rw [hsciRest, htailExp] simp have hlenConsumed : (len.val : Int) = consumed.val := by norm_num at hlenVal exact hlenVal rw [hconsumedVal] at hlenConsumed omega have hsuffixLenEq : qs.length = (match sign with | none => [] | some negative => [if negative then 45#u8 else 43#u8]).length + (expDigits.length + expTail.length) := by have hl := congrArg List.length hqsSign rw [hbody] at hl simpa using hl have hcombinedBounds : IScalar.min .I64 <= fracExponent.val + parsedExp.val /\ fracExponent.val + parsedExp.val <= IScalar.max .I64 := by have hparts := hlen rw [hsval, htailEq, hcs, htailCase] at hparts simp only [List.length_append, List.length_cons] at hparts rw [hfracExponentVal, hnegAfterVal, hnAfterEq, IScalar.min_IScalarTy_I64_eq, I64.min_eq, IScalar.max_IScalarTy_I64_eq, I64.max_eq] constructor · by_cases hshort : expDigits.length <= 5 · have hsharp := hexpshort hshort omega · have hq : 6 <= expDigits.length := by omega by_cases hnegative : sign = some true · simp [hnegative] at hsuffixLenEq rw [htailExp] at hsuffixLenEq simp at hsuffixLenEq omega · have hnonnegative := hexpnonnegative hnegative omega · omega obtain ⟨combinedExponent, hcombinedExponent, hcombinedExponentVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec hcombinedBounds.1 hcombinedBounds.2) unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac, hnegAfter, hfracExponent, htotal, htotalPos, hsplitFrac] norm_num simp only [UScalar.ofNat, UScalar.ofNatCore] cases upper <;> simp at hmarkerByte <;> subst q <;> simp [htotal, htotalPos, hsplitFrac, h101Target, h69Target, hscientific, hcombinedExponent, hoffConsumed, hcastLen, core.option.Option.Insts.CoreOpsTry_traitTry.branch, dec2flt_model.ModelOptionBranch, dec2flt_model.ModelOptionFromResidual] all_goals by_cases hsmall : totalDigits.val <= 19 · refine ⟨some ({ exponent := combinedExponent mantissa := mantissa1 negative := false many_digits := false }, len), ?_, ?_⟩ · simp [hsmall] · intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · simp [hsmall] have hsubBounds : IScalar.min .Isize <= totalDigits.val - 19 /\ totalDigits.val - 19 <= IScalar.max .Isize := by have hb := IScalar.hBounds totalDigits simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := totalDigits) (y := 19#isize) (by simpa using hsubBounds.1) (by simpa using hsubBounds.2)) rw [hnMore] simp obtain ⟨adjusted, hadjusted⟩ := NumberHelperAdjustManySourceLength s nMore hsourceLen (by norm_num at hnMoreVal omega) (by norm_num at hnMoreVal rw [hnMoreVal, htotalVal, hnDigitsEq, hnAfterEq] have hl := congrArg List.length hsval rw [htailEq, hcs] at hl simp at hl omega) rw [hadjusted] by_cases hpositive : 0 < adjusted.val · simp [hpositive] obtain ⟨mantissa2, exponent2, hmany⟩ := NumberHelperFractionalManyDigitsSourceLength s intRest parsedExp integer fraction fracTail (by simpa [htailEq, hcs, List.append_assoc] using hsval) (by simpa [htailEq, hcs] using hintRest) hintDigits hfracDigits hfracTail hsourceLen (by have hb := hcombinedBounds.1 rw [hfracExponentVal, hnegAfterVal, hnAfterEq] at hb simpa [IScalar.min_IScalarTy_I64_eq, I64.min_eq] using hb) (by rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq] have hl := hlen rw [hsval, htailEq, hcs] at hl simp at hl omega) rw [hmany] refine ⟨some ({ exponent := exponent2 mantissa := mantissa2 negative := false many_digits := true }, len), rfl, ?_⟩ intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · simp [hpositive] intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · have hq101 : q ≠ 101#u8 := by tauto have hq69 : q ≠ 69#u8 := by tauto obtain ⟨consumed, hoffConsumed, hconsumedVal⟩ := NumberHelperOffsetSourceLength s fracRest hsourceLen (by have hsLenEq := congrArg List.length hsval rw [htailEq, hcs, htailCase] at hsLenEq rw [hfracRest, htailCase] simp at hsLenEq ⊢ omega) have hrestLe : fracRest.val.length <= s.val.length := by have hsLenEq := congrArg List.length hsval rw [htailEq, hcs, htailCase] at hsLenEq rw [hfracRest, htailCase] simp at hsLenEq ⊢ omega have hcastBounds : 0 <= consumed.val /\ consumed.val <= UScalar.max .Usize := by rw [hconsumedVal] simp [UScalar.max, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨len, hcastLen, hlenVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize consumed hcastBounds) have hlenEq : len.val = integer.length + 1 + fraction.length := by norm_num at hlenVal rw [hconsumedVal, hsval, htailEq, hcs, hfracRest, htailCase] at hlenVal simp at hlenVal omega have hlenNe : len ≠ Slice.len s := by intro heq have heqv := congrArg UScalar.val heq simp [Slice.len_val, hlenEq, hsval, htailEq, hcs, htailCase] at heqv omega unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac, hnegAfter, hfracExponent, htotal, htotalPos, hsplitFrac, hq101, hq69, hoffConsumed, hcastLen] norm_num simp only [UScalar.ofNat, UScalar.ofNatCore] simp [htotal, htotalPos, hsplitFrac, hq101, hq69, hoffConsumed, hcastLen] split <;> try contradiction split <;> try contradiction split · rename_i hsmall refine ⟨some ({ exponent := fracExponent mantissa := mantissa1 negative := false many_digits := false }, len), ?_, ?_⟩ · rfl · intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · rename_i hsmall have hsubBounds : IScalar.min .Isize <= totalDigits.val - 19 /\ totalDigits.val - 19 <= IScalar.max .Isize := by have hb := IScalar.hBounds totalDigits simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := totalDigits) (y := 19#isize) (by simpa using hsubBounds.1) (by simpa using hsubBounds.2)) rw [hnMore] simp obtain ⟨adjusted, hadjusted⟩ := NumberHelperAdjustManySourceLength s nMore hsourceLen (by norm_num at hnMoreVal omega) (by norm_num at hnMoreVal rw [hnMoreVal, htotalVal, hnDigitsEq, hnAfterEq] omega) rw [hadjusted] by_cases hpositive : 0 < adjusted.val · simp [hpositive] obtain ⟨mantissa2, exponent2, hmany⟩ := NumberHelperFractionalManyDigitsSourceLength s intRest 0#i64 integer fraction (q :: qs) (by simpa [htailEq, hcs, htailCase, List.append_assoc] using hsval) (by simpa [htailEq, hcs, htailCase] using hintRest) hintDigits hfracDigits (by intro b rest h exact hfracTail b rest (by simpa [htailCase] using h)) hsourceLen (by rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq] norm_num have hl := hlen rw [hsval, htailEq, hcs, htailCase] at hl simp at hl omega) (by rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq] norm_num rw [htotalVal, hnDigitsEq, hnAfterEq] at hsmall omega) rw [hmany] refine ⟨some ({ exponent := exponent2 mantissa := mantissa2 negative := false many_digits := true }, len), rfl, ?_⟩ intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · simp [hpositive] intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · cases c rename_i cbv let c : Std.U8 := @UScalar.mk .U8 cbv have hcbv : cbv ≠ 46 := by intro heq subst cbv exact hpoint (by rfl) have hpointMatch : (match (@UScalar.mk .U8 cbv) with | 46#uscalar => do let (s3, mantissa2) ← num.imp.dec2flt.parse.try_parse_digits afterFirst mantissa let nAfter ← SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from s3 afterFirst let negAfter ← -. nAfter let fracExponent ← lift (IScalar.cast .I64 negAfter) ok (s3, mantissa2, nAfter, fracExponent) | _ => ok (intRest, mantissa, 0#isize, 0#i64)) = ok (intRest, mantissa, 0#isize, 0#i64) := by split <;> try contradiction all_goals rfl by_cases hintegerEmpty : integer = [] · have hnzero : nDigits = 0#isize := by apply IScalar.eq_of_val_eq rw [hnDigitsEq, hintegerEmpty] norm_num have haddZeroZero : (0#isize + 0#isize) = ok 0#isize := by simpa [hnzero] using haddZero unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt] split <;> try contradiction simp [haddZeroZero, hnzero] · have hnpos : 0 < nDigits.val := by rw [hnDigitsEq] have := List.length_pos_of_ne_nil hintegerEmpty omega by_cases hmarker : c = 101#u8 \/ c = 69#u8 · obtain ⟨upper, hupper⟩ : exists upper : Bool, c = if upper then 69#u8 else 101#u8 := by rcases hmarker with hc | hc · exact ⟨false, by simpa using hc⟩ · exact ⟨true, by simpa using hc⟩ have hmarkerByte : (@UScalar.mk .U8 cbv) = if upper then 69#u8 else 101#u8 := by simpa [c] using hupper let sign := scientific_sign cs let expBody := scientific_body cs have hcsSign : cs = (match sign with | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++ expBody := by exact scientific_split cs have hunsigned : sign = none -> forall b rest, expBody = b :: rest -> b ≠ 45#u8 /\ b ≠ 43#u8 := by exact scientific_unsigned cs obtain ⟨expDigits, expTail, hbody, hexpDigits, hexpTail⟩ := split_digits expBody have hsci : afterFirst.val = (match sign with | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++ expDigits ++ expTail := by change cs = _ rw [hcsSign, hbody, List.append_assoc] obtain hnoexp | hexp := NumberHelperScientificClassificationSourceLength afterFirst sign expDigits expTail hexpDigits hexpTail hsci (by intro hd hs b rest ht apply hunsigned hs b rest rw [hbody, hd] simpa using ht) · rcases hnoexp with ⟨_, rest, hscientific⟩ unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt] split <;> try contradiction simp [haddZero, hnpos, hsplitInt] cases upper with | false => have hb := congrArg UScalar.bv hmarkerByte simp at hb subst cbv simp [h101Target, h69Target, hscientific, core.option.Option.Insts.CoreOpsTry_traitTry.branch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, dec2flt_model.ModelOptionBranch, dec2flt_model.ModelOptionFromResidual] | true => have hb := congrArg UScalar.bv hmarkerByte simp at hb subst cbv simp [h101Target, h69Target, hscientific, core.option.Option.Insts.CoreOpsTry_traitTry.branch, core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual, dec2flt_model.ModelOptionBranch, dec2flt_model.ModelOptionFromResidual] · rcases hexp with ⟨hexpNe, parsedExp, sciRest, hscientific, hsciRest, hexplo, hexphi, hexpshort, hexpnonnegative⟩ cases htailExp : expTail with | nil => exact False.elim (complete_spelling integer none (some (upper, sign, expDigits)) hintDigits (by simp) (by left; simpa only [bne_iff_ne] using hintegerEmpty) ⟨hexpNe, hexpDigits⟩ (by rw [hsval, htailEq, hmarkerByte, hcsSign, hbody, htailExp] simp [List.append_assoc])) | cons z zs => obtain ⟨consumed, hoffConsumed, hconsumedVal⟩ := NumberHelperOffsetSourceLength s sciRest hsourceLen (by rw [hsciRest, htailExp] have hl := congrArg List.length hsval rw [htailEq, hcsSign, hbody, htailExp] at hl simp at hl ⊢ omega) have hcastBounds : 0 <= consumed.val /\ consumed.val <= UScalar.max .Usize := by rw [hconsumedVal] have hrestLe : sciRest.val.length <= s.val.length := by rw [hsciRest, htailExp] have hl := congrArg List.length hsval rw [htailEq, hcsSign, hbody, htailExp] at hl simp at hl ⊢ omega simp [UScalar.max, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨len, hcastLen, hlenVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize consumed hcastBounds) have hlenNe : len ≠ Slice.len s := by intro heq have heqv : (len.val : Int) = s.val.length := by have h := congrArg UScalar.val heq simpa [Slice.len_val] using h have hrestPos : 0 < sciRest.val.length := by rw [hsciRest, htailExp] simp have hlenConsumed : (len.val : Int) = consumed.val := by norm_num at hlenVal exact hlenVal rw [hconsumedVal] at hlenConsumed omega have haddBounds : IScalar.min .I64 <= (0#i64).val + parsedExp.val /\ (0#i64).val + parsedExp.val <= IScalar.max .I64 := by rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq, IScalar.max_IScalarTy_I64_eq, I64.max_eq] norm_num omega obtain ⟨combined, haddExp, hcombinedVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec haddBounds.1 haddBounds.2) have hcombined : combined = parsedExp := by apply IScalar.eq_of_val_eq norm_num at hcombinedVal exact hcombinedVal subst combined unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt] split <;> try contradiction simp [haddZero, hnpos, hsplitInt] cases upper <;> have hb := congrArg UScalar.bv hmarkerByte <;> simp at hb <;> subst cbv <;> simp [h101Target, h69Target, hscientific, haddExp, hoffConsumed, hcastLen, core.option.Option.Insts.CoreOpsTry_traitTry.branch, dec2flt_model.ModelOptionBranch, dec2flt_model.ModelOptionFromResidual] all_goals by_cases hsmall : nDigits.val <= 19 · refine ⟨some ({ exponent := parsedExp mantissa := mantissa negative := false many_digits := false }, len), ?_, ?_⟩ · simp [hsmall] · intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · simp [hsmall] have hsubBounds : IScalar.min .Isize <= nDigits.val - 19 /\ nDigits.val - 19 <= IScalar.max .Isize := by have hb := IScalar.hBounds nDigits simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := nDigits) (y := 19#isize) (by simpa using hsubBounds.1) (by simpa using hsubBounds.2)) rw [hnMore] simp obtain ⟨adjusted, hadjusted⟩ := NumberHelperAdjustManySourceLength s nMore hsourceLen (by norm_num at hnMoreVal omega) (by norm_num at hnMoreVal rw [hnMoreVal, hnDigitsEq] omega) rw [hadjusted] by_cases hpositive : 0 < adjusted.val · simp [hpositive] obtain ⟨mantissa2, exponent2, hmany⟩ := NumberHelperIntegralManyDigitsSourceLength s intRest parsedExp nMore adjusted integer intTailBytes hsval hintRest hintDigits hintTail (by intro b rest h rw [htailEq] at h injection h with hbc _ subst b exact hpoint) hsourceLen (by norm_num at hnMoreVal rw [hnMoreVal, hnDigitsEq]) hadjusted hpositive (by rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq] have hl := hlen rw [hsval] at hl simp at hl omega) rw [hmany] refine ⟨some ({ exponent := exponent2 mantissa := mantissa2 negative := false many_digits := true }, len), rfl, ?_⟩ intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · simp [hpositive] intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · have hc101 : c ≠ 101#u8 := by tauto have hc69 : c ≠ 69#u8 := by tauto have hcbv101 : cbv ≠ (101 : BitVec 8) := by intro heq apply hc101 exact congrArg (fun v : BitVec 8 => (@UScalar.mk .U8 v)) heq have hcbv69 : cbv ≠ (69 : BitVec 8) := by intro heq apply hc69 exact congrArg (fun v : BitVec 8 => (@UScalar.mk .U8 v)) heq obtain ⟨consumed, hoffConsumed, hconsumedVal⟩ := NumberHelperOffsetSourceLength s intRest hsourceLen (by rw [hintRest, hsval] simp) have hcastBounds : 0 <= consumed.val /\ consumed.val <= UScalar.max .Usize := by rw [hconsumedVal] simp [UScalar.max, Dec2Flt.configured_x86_64_numBits] omega obtain ⟨len, hcastLen, hlenVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize consumed hcastBounds) have hlenEq : len.val = integer.length := by norm_num at hlenVal rw [hconsumedVal, hintRest, hsval] at hlenVal simp at hlenVal omega have hlenNe : len ≠ Slice.len s := by intro heq have heqv := congrArg UScalar.val heq simp [Slice.len_val, hlenEq, hsval, htailEq] at heqv unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotempty, hscanInt, hoffInt, hsplitInt] split <;> try contradiction simp [haddZero, hnpos, hsplitInt, h101Target, h69Target, hc101, hc69, hcbv101, hcbv69, hoffConsumed, hcastLen] split <;> try contradiction split <;> try contradiction by_cases hsmall : nDigits.val <= 19 · refine ⟨some ({ exponent := 0#i64 mantissa := mantissa negative := false many_digits := false }, len), ?_, ?_⟩ · simp [hsmall] · intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · simp [hsmall] have hsubBounds : IScalar.min .Isize <= nDigits.val - 19 /\ nDigits.val - 19 <= IScalar.max .Isize := by have hb := IScalar.hBounds nDigits simp [IScalar.min, IScalar.max, IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits] at hb ⊢ omega obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec (x := nDigits) (y := 19#isize) (by simpa using hsubBounds.1) (by simpa using hsubBounds.2)) rw [hnMore] simp obtain ⟨adjusted, hadjusted⟩ := NumberHelperAdjustManySourceLength s nMore hsourceLen (by norm_num at hnMoreVal omega) (by norm_num at hnMoreVal rw [hnMoreVal, hnDigitsEq, hsval] simp omega) rw [hadjusted] by_cases hpositive : 0 < adjusted.val · simp [hpositive] obtain ⟨mantissa2, exponent2, hmany⟩ := NumberHelperIntegralManyDigitsSourceLength s intRest 0#i64 nMore adjusted integer (c :: cs) (by rw [htailEq] at hsval simpa [c] using hsval) (by rw [htailEq] at hintRest simpa [c] using hintRest) hintDigits (by intro b rest h apply hintTail b rest rw [htailEq] simpa [c] using h) (by intro b rest h injection h with hbc _ subst b simpa [c] using hpoint) hsourceLen (by norm_num at hnMoreVal rw [hnMoreVal, hnDigitsEq]) hadjusted hpositive (by rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq] norm_num have hl := hlen rw [hsval] at hl simp at hl omega) rw [hmany] refine ⟨some ({ exponent := exponent2 mantissa := mantissa2 negative := false many_digits := true }, len), rfl, ?_⟩ intro d r ho simp only [Option.some.injEq, Prod.mk.injEq] at ho rcases ho with ⟨_, rfl⟩ intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv · simp [hpositive] intro hv apply hlenNe apply UScalar.eq_of_val_eq simpa [Slice.len_val] using hv end dec2flt_model