import Tablet.ParserExactRoundingDigitScans import Tablet.ParserExactRoundingAdjustment import Tablet.ParserExactRoundingFastPath import Tablet.ParserExactRoundingOffsets import Tablet.ParserExactRoundingScientific import Tablet.ModelSliceSplitFirst import Tablet.ModelSliceFirst import Tablet.ModelStrAsBytes import Tablet.num_imp_dec2flt_parse_parse_number import Tablet.num_imp_dec2flt_dec2flt open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 2000000 set_option maxRecDepth 1000000 namespace dec2flt_model -- [TABLET NODE: ParserExactRoundingExecution] theorem ParserExactRoundingExecution (s : Str) (hs : s.val = [48#u8, 46#u8] ++ List.replicate 65535 48#u8 ++ [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) : num.imp.dec2flt.dec2flt F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64 F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64 F64.Insts.CoreOpsArithNegF64 s = ok (core.result.Result.Ok (⟨BitVec.ofNat 64 4607182418800017408⟩ : F64)) := by -- BODY have parse_number_exact (body : Slice Std.U8) (hbody : body.val = [48#u8, 46#u8] ++ List.replicate 65535 48#u8 ++ [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) : num.imp.dec2flt.parse.parse_number body = ok (some { exponent := 0#i64, mantissa := 1#u64, negative := false, many_digits := false }) := by have hbodyLen : body.val.length = 65545 := by simp only [hbody, List.length_append, List.length_replicate, List.length_cons, List.length_nil] obtain ⟨point, hscan0, hpoint⟩ := ParserExactRoundingDigitScans.2 body hbody have hpointLen : point.val.length = 65544 := by simp only [hpoint, List.length_cons, List.length_append, List.length_replicate, List.length_nil] obtain ⟨nDigits, hnDigits, hnDigitsVal⟩ := ParserExactRoundingOffsets body point (by omega) (by omega) have hnDigitsEq : nDigits = 1#isize := by apply IScalar.eq_of_val_eq rw [hnDigitsVal, hbodyLen, hpointLen] norm_num subst nDigits let fraction : Slice Std.U8 := ⟨List.replicate 65535 48#u8 ++ [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8], by simp only [List.length_append, List.length_replicate, List.length_cons, List.length_nil] rcases Usize.bounds_eq with q | q <;> norm_num [q, U32.max_eq, U64.max_eq]⟩ have hfraction : fraction.val = List.replicate 65535 48#u8 ++ [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8] := by rfl have hsplitPoint : core.slice.Slice.split_first point = ok (some (46#u8, fraction)) := by let point' : Slice Std.U8 := ⟨46#u8 :: fraction.val, by rw [hfraction] simp only [List.length_cons, List.length_append, List.length_replicate, List.length_nil] rcases Usize.bounds_eq with q | q <;> norm_num [q, U32.max_eq, U64.max_eq]⟩ have hpointEq : point = point' := (Slice.eq_iff _ _).2 (by rw [hpoint, show point'.val = 46#u8 :: fraction.val by rfl, hfraction] rfl) rw [hpointEq] simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst, point'] obtain ⟨expRest, hscan1, hexpRest⟩ := ParserExactRoundingDigitScans.1 fraction hfraction have hfractionLen : fraction.val.length = 65543 := by simp only [hfraction, List.length_append, List.length_replicate, List.length_cons, List.length_nil] have hexpRestLen : expRest.val.length = 7 := by simp [hexpRest] obtain ⟨nAfter, hnAfter, hnAfterVal⟩ := ParserExactRoundingOffsets fraction expRest (by omega) (by omega) have hnAfterEq : nAfter = 65536#isize := by apply IScalar.eq_of_val_eq rw [hnAfterVal, hfractionLen, hexpRestLen] norm_num subst nAfter have hnAfterNotMin : (65536#isize) ≠ IScalar.min .Isize := by rw [IScalar.min] rcases System.Platform.numBits_eq with hbits | hbits <;> simp [IScalarTy.numBits, hbits] obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ := Aeneas.Std.WP.spec_imp_exists (HNeg.hNeg.step (65536#isize) hnAfterNotMin) have hnegAfterEq : negAfter = (-65536)#isize := by apply IScalar.eq_of_val_eq simpa using hnegAfterVal subst negAfter have hcastFractionExponent : lift (IScalar.cast .I64 ((-65536)#isize)) = ok (-65536)#i64 := by have hc := IScalar.cast_inBounds_spec .I64 ((-65536)#isize) (by constructor · rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq] norm_num · rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq] norm_num) obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hc rw [hz] congr 2 apply IScalar.eq_of_val_eq simpa using hzval have haddBounds : IScalar.min .Isize <= (1#isize).val + (65536#isize).val /\ (1#isize).val + (65536#isize).val <= IScalar.max .Isize := by rcases System.Platform.numBits_eq with hbits | hbits <;> simp [IScalar.min, IScalar.max, IScalarTy.numBits, hbits] obtain ⟨totalDigits, haddDigits, htotalDigitsVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec haddBounds.1 haddBounds.2) have htotalDigitsEq : totalDigits = 65537#isize := by apply IScalar.eq_of_val_eq norm_num at htotalDigitsVal simpa using htotalDigitsVal subst totalDigits let exponentDigits : Slice Std.U8 := ⟨[54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8], by rcases Usize.bounds_eq with q | q <;> norm_num [q, U32.max_eq, U64.max_eq]⟩ have hsplitExponent : core.slice.Slice.split_first expRest = ok (some (101#u8, exponentDigits)) := by let expRest' : Slice Std.U8 := ⟨101#u8 :: exponentDigits.val, by rw [← hexpRest] exact expRest.property⟩ have hexpRestEq : expRest = expRest' := (Slice.eq_iff _ _).2 (by rw [hexpRest]) rw [hexpRestEq] simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst, expRest'] have hexponentDigits : exponentDigits.val = [54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8] := by rfl obtain ⟨empty, hscientific, hempty⟩ := ParserExactRoundingScientific exponentDigits hexponentDigits have hexponentAdd : ((-65536)#i64 + 65536#i64) = ok 0#i64 := by have hb : IScalar.min .I64 <= ((-65536)#i64).val + (65536#i64).val /\ ((-65536)#i64).val + (65536#i64).val <= IScalar.max .I64 := by rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq, IScalar.max_IScalarTy_I64_eq, I64.max_eq] norm_num obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.add_spec hb.1 hb.2) rw [hz] congr 1 apply IScalar.eq_of_val_eq simpa using hzval obtain ⟨fullOffset, hfullOffset, hfullOffsetVal⟩ := ParserExactRoundingOffsets body empty (by omega) (by rw [hempty] simp) have hfullOffsetValExact : fullOffset.val = 65545 := by rw [hfullOffsetVal, hbodyLen, hempty] rfl have hfullCastBounds : 0 <= fullOffset.val /\ fullOffset.val <= UScalar.max .Usize := by rw [hfullOffsetValExact] rcases System.Platform.numBits_eq with hbits | hbits <;> simp [UScalar.max, hbits] obtain ⟨fullLen, hcastLen0, hfullLenVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.hcast_inBounds_spec .Usize fullOffset hfullCastBounds) have hfullLenEq : fullLen = Slice.len body := by apply UScalar.eq_of_val_eq rw [hfullOffsetVal] at hfullLenVal simpa [hempty, Slice.len_val] using hfullLenVal subst fullLen have hcastLen : lift (IScalar.hcast .Usize fullOffset) = ok (Slice.len body) := by simpa [Aeneas.Std.lift] using hcastLen0 have htotalNonzero : ¬ (65537#isize).val = 0 := by norm_num have htotalLarge : ¬ (65537#isize).val <= (19#isize).val := by norm_num have hsubBounds : IScalar.min .Isize <= (65537#isize).val - (19#isize).val /\ (65537#isize).val - (19#isize).val <= IScalar.max .Isize := by rcases System.Platform.numBits_eq with hbits | hbits <;> simp [IScalar.min, IScalar.max, IScalarTy.numBits, hbits] obtain ⟨moreDigits, hsubDigits0, hmoreDigitsVal⟩ := Aeneas.Std.WP.spec_imp_exists (IScalar.sub_spec hsubBounds.1 hsubBounds.2) have hmoreDigitsEq : moreDigits = 65518#isize := by apply IScalar.eq_of_val_eq norm_num at hmoreDigitsVal simpa using hmoreDigitsVal subst moreDigits have hadjust := ParserExactRoundingAdjustment body hbody let pointPhase := (match (46#u8 : Std.U8) with | 46#uscalar => ok (expRest, 1#u64, 65536#isize, (-65536)#i64) | _ => ok (point, 0#u64, 0#isize, 0#i64)) have hpointMatch : pointPhase = ok (expRest, 1#u64, 65536#isize, (-65536)#i64) := by rfl have hnotEmpty : core.slice.Slice.is_empty body = ok false := by unfold core.slice.Slice.is_empty rw [show body.length = 65545 by simpa [Slice.length] using hbodyLen] rfl have hnonemptyVal : body.val ≠ [] := by intro h rw [h] at hbodyLen norm_num at hbodyLen have hmassert : massert (¬ body.val = []) = ok () := by simp [hnonemptyVal] have hpartial : num.imp.dec2flt.parse.parse_partial_number body = ok (some ({ exponent := 0#i64, mantissa := 1#u64, negative := false, many_digits := false }, Slice.len body)) := by unfold num.imp.dec2flt.parse.parse_partial_number simp [hnotEmpty, hmassert, hscan0, hnDigits, hsplitPoint, hscan1, hnAfter, hnegAfter, hcastFractionExponent] change (do let x ← pointPhase; _) = _ rw [hpointMatch] simp [haddDigits, hsplitExponent, hscientific, hexponentAdd, hfullOffset, hcastLen, htotalNonzero, htotalLarge, hsubDigits0, hadjust] rw [show core.option.Option.Insts.CoreOpsTry_traitTry.branch (some 65536#i64) = ok (core.ops.control_flow.ControlFlow.Continue 65536#i64) by rfl] change (do let exponent ← (-65536)#i64 + 65536#i64 ok (some (({ exponent, mantissa := 1#u64, negative := false, many_digits := false } : num.imp.dec2flt.decimal.Decimal), Slice.len body))) = _ rw [hexponentAdd] rfl unfold num.imp.dec2flt.parse.parse_number simp [hpartial] have hcompact : s.val = 48#u8 :: 46#u8 :: (List.replicate 65535 48#u8 ++ [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) := by rw [hs] rfl have hslen : s.val.length = 65545 := by simp only [hs, List.length_append, List.length_replicate, List.length_cons, List.length_nil] have hfirst : core.slice.Slice.first s = ok (some 48#u8) := by unfold core.slice.Slice.first ModelSliceFirst rw [hcompact] rfl have hnonempty : s.val ≠ [] := by intro h rw [h] at hslen norm_num at hslen have hnotEmpty : core.slice.Slice.is_empty s = ok false := by unfold core.slice.Slice.is_empty rw [show s.length = 65545 by simpa [Slice.length] using hslen] rfl have hparse := parse_number_exact s hs simp [num.imp.dec2flt.dec2flt, num.imp.dec2flt.dec2flt_with_trace, core.str.Str.as_bytes, ModelStrAsBytes, hfirst, hnotEmpty, hnonempty, hparse, ParserExactRoundingFastPath] end dec2flt_model