import Tablet.ParserExponentConstants import Tablet.ParserSlowLoopZeroResult import Tablet.ParserSlowLoopInfinityResult import Tablet.ParserSlowLoopMinimumExponent open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: ParserSlowExponentRange] theorem ParserSlowExponentRange (s : Slice U8) (b : num.imp.dec2flt.common.BiasedFp) (h : num.imp.dec2flt.slow.parse_long_mantissa F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s = ok b) : 0 ≤ b.p_biased.val ∧ b.p_biased.val ≤ 2047 := by -- BODY have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y, Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by intro α β r f y hr cases r with | fail err => simp [Bind.bind, Aeneas.Std.bind] at hr | div => simp [Bind.bind, Aeneas.Std.bind] at hr | ok x => exact ⟨x, rfl, hr⟩ rcases ParserExponentConstants with ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, hmin, hsmall, hlarge, hroundmin, hroundmax⟩ have hzero : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 = ok { m := 0#u64, p_biased := 0#i32 } := by rfl have hinfFp : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 = ok { m := 0#u64, p_biased := 2047#i32 } := by rfl unfold num.imp.dec2flt.slow.parse_long_mantissa at h rw [hzero, hinf] at h simp only [bind_tc_ok] at h rw [hinfFp] at h simp only [bind_tc_ok] at h obtain ⟨d, hd, h⟩ := bind_ok_inv _ _ _ h by_cases hempty : d.num_digits = 0#usize · simp only [hempty, if_true] at h cases h norm_num simp only [hempty, if_false] at h by_cases hsmallp : d.decimal_point < (-324)#i32 · simp only [hsmallp, if_true] at h cases h norm_num simp only [hsmallp, if_false] at h by_cases hlargep : d.decimal_point ≥ 310#i32 · simp only [hlargep, if_true] at h cases h norm_num simp only [hlargep, if_false] at h obtain ⟨out0, hout0, h⟩ := bind_ok_inv _ _ _ h rcases out0 with ⟨i1, i2, t, a, exp2, lr0⟩ have hlr0 := ParserSlowLoopZeroResult F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 ({ m := 0#u64, p_biased := 0#i32 } : num.imp.dec2flt.common.BiasedFp) d.num_digits d.decimal_point d.truncated d.digits 0#i32 (i1, i2, t, a, exp2, lr0) hout0 cases lr0 with | some result => rcases hlr0 with hnone | hsome · contradiction · simp only [Option.some.injEq] at hsome subst result cases h norm_num | none => obtain ⟨out1, hout1, h⟩ := bind_ok_inv _ _ _ h rcases out1 with ⟨i3, i4, t1, a1, exp21, lr1⟩ have hlr1 := ParserSlowLoopInfinityResult F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 ({ m := 0#u64, p_biased := 2047#i32 } : num.imp.dec2flt.common.BiasedFp) i1 i2 t a exp2 (i3, i4, t1, a1, exp21, lr1) hout1 cases lr1 with | some result => rcases hlr1 with hnone | hsome · contradiction · simp only [Option.some.injEq] at hsome subst result cases h norm_num | none => obtain ⟨exp22, hexp22, h⟩ := bind_ok_inv _ _ _ h obtain ⟨out2, hout2, h⟩ := bind_ok_inv _ _ _ h rcases out2 with ⟨d1, exp23⟩ have hexp23 : -1022 ≤ exp23.val := ParserSlowLoopMinimumExponent _ _ _ hout2 change (do let i5 ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.EXP_MIN _) = ok b at h rw [hmin] at h simp only [bind_tc_ok] at h obtain ⟨i6, hi6, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i7, hi7, h⟩ := bind_ok_inv _ _ _ h have hi6v : i6.val = exp23.val - (-1022) := by have he := IScalar.sub_equiv exp23 ((-1022)#i32) rw [hi6] at he exact he.2.1 have hi7v : i7.val = i6.val + 1 := by have he := IScalar.add_equiv i6 (1#i32) rw [hi7] at he exact he.2.1 by_cases hsat : i7 ≥ 2047#i32 · simp only [hsat, if_true] at h cases h norm_num · simp only [hsat, if_false] at h change (do let i8 ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_BITS _) = ok b at h rw [hsig] at h simp only [lift, bind_tc_ok] at h obtain ⟨i9, hi9, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i10, hi10, h⟩ := bind_ok_inv _ _ _ h obtain ⟨d2, hd2, h⟩ := bind_ok_inv _ _ _ h obtain ⟨mantissa, hmantissa, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i11, hi11, h⟩ := bind_ok_inv _ _ _ h by_cases hcarry : d2 ≥ i11 · simp only [hcarry, if_true] at h obtain ⟨d3, hd3, h⟩ := bind_ok_inv _ _ _ h obtain ⟨exp24, hexp24, h⟩ := bind_ok_inv _ _ _ h obtain ⟨mantissa1, hmantissa1, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i13, hi13, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h by_cases hsat2 : i14 ≥ 2047#i32 · simp only [hsat2, if_true] at h cases h norm_num · simp only [hsat2, if_false] at h obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i16, hi16, h⟩ := bind_ok_inv _ _ _ h by_cases hlow : mantissa1 < i16 · simp only [hlow, if_true] at h obtain ⟨power21, hpower21, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h cases h change 0 ≤ power21.val ∧ power21.val ≤ 2047 have hexp24v : exp24.val = exp23.val + 1 := by have he := IScalar.add_equiv exp23 (1#i32) rw [hexp24] at he exact he.2.1 have hi13v : i13.val = exp24.val - (-1022) := by have he := IScalar.sub_equiv exp24 ((-1022)#i32) rw [hi13] at he exact he.2.1 have hi14v : i14.val = i13.val + 1 := by have he := IScalar.add_equiv i13 (1#i32) rw [hi14] at he exact he.2.1 have hi15v : i15.val = exp24.val - (-1022) := by have he := IScalar.sub_equiv exp24 ((-1022)#i32) rw [hi15] at he exact he.2.1 have hpower2v : power2.val = i15.val + 1 := by have he := IScalar.add_equiv i15 (1#i32) rw [hpower2] at he exact he.2.1 have hpower21v : power21.val = power2.val - 1 := by have he := IScalar.sub_equiv power2 (1#i32) rw [hpower21] at he exact he.2.1 change ¬ 2047 ≤ i14.val at hsat2 constructor <;> omega · simp only [hlow, if_false, bind_tc_ok] at h obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h cases h change 0 ≤ power2.val ∧ power2.val ≤ 2047 have hexp24v : exp24.val = exp23.val + 1 := by have he := IScalar.add_equiv exp23 (1#i32) rw [hexp24] at he exact he.2.1 have hi13v : i13.val = exp24.val - (-1022) := by have he := IScalar.sub_equiv exp24 ((-1022)#i32) rw [hi13] at he exact he.2.1 have hi14v : i14.val = i13.val + 1 := by have he := IScalar.add_equiv i13 (1#i32) rw [hi14] at he exact he.2.1 have hi15v : i15.val = exp24.val - (-1022) := by have he := IScalar.sub_equiv exp24 ((-1022)#i32) rw [hi15] at he exact he.2.1 have hpower2v : power2.val = i15.val + 1 := by have he := IScalar.add_equiv i15 (1#i32) rw [hpower2] at he exact he.2.1 change ¬ 2047 ≤ i14.val at hsat2 constructor <;> omega · simp only [hcarry, if_false] at h obtain ⟨i13, hi13, h⟩ := bind_ok_inv _ _ _ h obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h by_cases hlow : d2 < i14 · simp only [hlow, if_true] at h obtain ⟨power21, hpower21, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i16, hi16, h⟩ := bind_ok_inv _ _ _ h cases h change 0 ≤ power21.val ∧ power21.val ≤ 2047 have hi13v : i13.val = exp23.val - (-1022) := by have he := IScalar.sub_equiv exp23 ((-1022)#i32) rw [hi13] at he exact he.2.1 have hpower2v : power2.val = i13.val + 1 := by have he := IScalar.add_equiv i13 (1#i32) rw [hpower2] at he exact he.2.1 have hpower21v : power21.val = power2.val - 1 := by have he := IScalar.sub_equiv power2 (1#i32) rw [hpower21] at he exact he.2.1 change ¬ 2047 ≤ i7.val at hsat constructor <;> omega · simp only [hlow, if_false, bind_tc_ok] at h obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h obtain ⟨i16, hi16, h⟩ := bind_ok_inv _ _ _ h cases h change 0 ≤ power2.val ∧ power2.val ≤ 2047 have hi13v : i13.val = exp23.val - (-1022) := by have he := IScalar.sub_equiv exp23 ((-1022)#i32) rw [hi13] at he exact he.2.1 have hpower2v : power2.val = i13.val + 1 := by have he := IScalar.add_equiv i13 (1#i32) rw [hpower2] at he exact he.2.1 change ¬ 2047 ≤ i7.val at hsat constructor <;> omega end dec2flt_model