import Tablet.DispatchContracts import Tablet.P9_T1_DispatchFrame import Tablet.P9_T1_EmbedComponents import Tablet.P616_T1_SignStrippedClassification 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 import Tablet.ParserExponentConstants open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P616_T1_FrameNormality] theorem P616_T1_FrameNormality : forall C : P9_T1_AbstractComponents, DispatchContracts C -> forall s : Str, DecimalGoalVocabularyDefault.sourceString s -> s.val.length <= 3458764513820540928 -> exists r trace, P9_T1_DispatchFrame (P9_T1_EmbedComponents C) s = ok (r, trace) := by -- BODY intro C hC s hs hlen have hcastOK (p : num.imp.dec2flt.common.BiasedFp) : lift (IScalar.hcast .U64 p.p_biased) = ok (IScalar.hcast .U64 p.p_biased) := by simp [lift, IScalar.hcast] have hshiftOK (i : Std.U64) : ∃ y, i <<< 52#u32 = ok y := by simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_UScalar, UScalar.shiftLeft] exact ⟨_, rfl⟩ have hshiftEq (i : Std.U64) : i <<< 52#u32 = ok ⟨i.bv.shiftLeft 52⟩ := by simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_UScalar, UScalar.shiftLeft] rfl have hbitOr (x y : Std.U64) : lift (x ||| y) = ok (x ||| y) := by simp [lift] have hsub53 : 53#u32 - 1#u32 = ok 52#u32 := by rfl obtain ⟨_, htotal53, hsig52, _, _, _, _, _, _, _, _, _⟩ := ParserExponentConstants have hsigSub : (do let x ← F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS x - 1#u32) = ok 52#u32 := by simp only [Bind.bind, Aeneas.Std.bind] cases h : F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS with | ok x => have hx : x = 53#u32 := by have e := htotal53.symm.trans h injection e with hxx exact hxx.symm subst x rfl | fail e => simp [h] at htotal53 | div => simp [h] at htotal53 rcases s with ⟨xs, hxs⟩ cases xs with | nil => simp [P9_T1_DispatchFrame, P9_T1_EmbedComponents, core.str.Str.as_bytes, ModelStrAsBytes, core.slice.Slice.first, ModelSliceFirst, num.imp.dec2flt.pfe_empty] | cons c tail => by_cases hminus : c = 45#u8 · subst c simp only [P9_T1_DispatchFrame, P9_T1_EmbedComponents, core.str.Str.as_bytes, ModelStrAsBytes, core.slice.Slice.first, ModelSliceFirst, core.slice.Slice.is_empty, Slice.length, Slice.index_SliceIndexRangeFromUsizeSliceInst, core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop, num.imp.dec2flt.pfe_invalid] have htailbound : tail.length ≤ Usize.max := by have h := hxs simp only [List.length_cons] at h omega let body : Slice Std.U8 := ⟨tail, htailbound⟩ by_cases htail : tail = [] · simp [htail] · cases hn : C.parseNumber body with | none => cases hsp : C.parseSpecial body true <;> simp [body, htail, hn, hsp] | some d => cases hnat : C.native { d with negative := true } with | some value => simp [body, htail, hn, hnat] | none => have hlen' : tail.length ≤ 3458764513820540928 := by simp only [List.length_cons] at hlen omega have hvalid : SourceValidByteSlice body := by unfold SourceValidByteSlice change tail.length ≤ 9223372036854775807 exact Nat.le_trans hlen' (by norm_num) have hne : body.val ≠ [] := by simp [body, htail] have hrecord := hC.record_bounds body d hvalid hne hn have haddBound : d.mantissa.val + 1 ≤ U64.max := by rw [U64.max_eq] omega have hadd := UScalar.add_spec (x := d.mantissa) (y := 1#u64) (by simpa using haddBound) obtain ⟨next, hadd, hnext⟩ := Aeneas.Std.WP.spec_imp_exists hadd cases htot : F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS with | ok sig => have hsigVal : sig = 53#u32 := by have e := htotal53.symm.trans htot injection e with he exact he.symm subst sig by_cases hmany : d.many_digits = true · have hnatMany : C.native { d with negative := true, many_digits := true } = none := by simpa [hmany] using hnat by_cases hp : ((C.integer d.exponent d.mantissa).p_biased : Int) ≥ 0 · simp [body, htail, hn, hnatMany, hadd, hmany, hp, htotal53, hsig52, hsub53, num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq] <;> simp only [hnatMany, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] by_cases hmEq : (C.integer d.exponent d.mantissa).m = (C.integer d.exponent next).m · by_cases hpEq : (C.integer d.exponent d.mantissa).p_biased = (C.integer d.exponent next).p_biased · have hpEqInt : (↑(C.integer d.exponent d.mantissa).p_biased : Int) = (↑(C.integer d.exponent next).p_biased : Int) := congrArg IScalar.val hpEq have hnotneg : ¬ ((C.integer d.exponent next).p_biased : Int) < 0 := by rw [← hpEqInt] omega simp [hmEq, hpEq, hpEqInt, hp, hnotneg] · simp [hmEq, hpEq, hp] · simp [hmEq, hp] · have hpNeg : ((C.integer d.exponent d.mantissa).p_biased : Int) < 0 := by omega simp [body, htail, hn, hnatMany, hadd, hmany, hp, hpNeg, htotal53, hsig52, hsub53, num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq] <;> simp only [hnatMany, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] <;> simp · have hmany' : d.many_digits = false := Bool.eq_false_of_not_eq_true hmany have hnatFew : C.native { d with negative := true, many_digits := false } = none := by simpa [hmany'] using hnat simp [body, htail, hn, hnatFew, hmany', hsig52, hsub53] <;> simp only [hnatFew, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] <;> split_ifs <;> simp only [Bind.bind, Aeneas.Std.bind] <;> simp | fail e => simp [htot] at htotal53 | div => simp [htot] at htotal53 · by_cases hplus : c = 43#u8 · subst c simp only [P9_T1_DispatchFrame, P9_T1_EmbedComponents, core.str.Str.as_bytes, ModelStrAsBytes, core.slice.Slice.first, ModelSliceFirst, core.slice.Slice.is_empty, Slice.length, Slice.index_SliceIndexRangeFromUsizeSliceInst, core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop, num.imp.dec2flt.pfe_invalid] have htailbound : tail.length ≤ Usize.max := by have h := hxs simp only [List.length_cons] at h omega let body : Slice Std.U8 := ⟨tail, htailbound⟩ by_cases htail : tail = [] · simp [htail] · cases hn : C.parseNumber body with | none => cases hsp : C.parseSpecial body false <;> simp [body, htail, hn, hsp] | some d => cases hnat : C.native { d with negative := false } with | some value => simp [body, htail, hn, hnat] | none => have hlen' : tail.length ≤ 3458764513820540928 := by simp only [List.length_cons] at hlen omega have hvalid : SourceValidByteSlice body := by unfold SourceValidByteSlice simp [body] omega have hne : body.val ≠ [] := by simp [body, htail] have hrecord := hC.record_bounds body d hvalid hne hn have haddBound : d.mantissa.val + 1 ≤ U64.max := by rw [U64.max_eq] omega have hadd := UScalar.add_spec (x := d.mantissa) (y := 1#u64) (by simpa using haddBound) obtain ⟨next, hadd, hnext⟩ := Aeneas.Std.WP.spec_imp_exists hadd cases htot : F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS with | ok sig => have hsigVal : sig = 53#u32 := by have e := htotal53.symm.trans htot injection e with he exact he.symm subst sig by_cases hmany : d.many_digits = true · have hnatMany : C.native { d with negative := false, many_digits := true } = none := by simpa [hmany] using hnat by_cases hp : ((C.integer d.exponent d.mantissa).p_biased : Int) ≥ 0 · simp [body, htail, hn, hnatMany, hadd, hmany, hp, htotal53, hsig52, hsub53, num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq] <;> simp only [hnatMany, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] by_cases hmEq : (C.integer d.exponent d.mantissa).m = (C.integer d.exponent next).m · by_cases hpEq : (C.integer d.exponent d.mantissa).p_biased = (C.integer d.exponent next).p_biased · have hpEqInt : (↑(C.integer d.exponent d.mantissa).p_biased : Int) = (↑(C.integer d.exponent next).p_biased : Int) := congrArg IScalar.val hpEq have hnotneg : ¬ ((C.integer d.exponent next).p_biased : Int) < 0 := by rw [← hpEqInt] omega simp [hmEq, hpEq, hpEqInt, hp, hnotneg] · simp [hmEq, hpEq, hp] · simp [hmEq, hp] · have hpNeg : ((C.integer d.exponent d.mantissa).p_biased : Int) < 0 := by omega simp [body, htail, hn, hnatMany, hadd, hmany, hp, hpNeg, htotal53, hsig52, hsub53, num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq] <;> simp only [hnatMany, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] <;> simp · have hmany' : d.many_digits = false := Bool.eq_false_of_not_eq_true hmany have hnatFew : C.native { d with negative := false, many_digits := false } = none := by simpa [hmany'] using hnat simp [body, htail, hn, hnatFew, hmany', hsig52, hsub53] <;> simp only [hnatFew, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] <;> split_ifs <;> simp only [Bind.bind, Aeneas.Std.bind] <;> simp | fail e => simp [htot] at htotal53 | div => simp [htot] at htotal53 · simp [P9_T1_DispatchFrame, P9_T1_EmbedComponents, core.str.Str.as_bytes, ModelStrAsBytes, core.slice.Slice.first, ModelSliceFirst, core.slice.Slice.is_empty, Slice.length, core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop, hminus, hplus, num.imp.dec2flt.pfe_invalid] let body : Slice Std.U8 := ⟨c :: tail, hxs⟩ cases hn : C.parseNumber body with | none => cases hsp : C.parseSpecial body false <;> simp [body, hn, hsp] | some d => cases hnat : C.native { d with negative := false } with | some value => simp [body, hn, hnat] | none => have hlen' : (c :: tail).length ≤ 3458764513820540928 := by simp only [List.length_cons] at hlen omega have hvalid : SourceValidByteSlice body := by unfold SourceValidByteSlice change (c :: tail).length ≤ 9223372036854775807 exact Nat.le_trans hlen' (by norm_num) have hne : body.val ≠ [] := by simp [body] have hrecord := hC.record_bounds body d hvalid hne hn have haddBound : d.mantissa.val + 1 ≤ U64.max := by rw [U64.max_eq] omega have hadd := UScalar.add_spec (x := d.mantissa) (y := 1#u64) (by simpa using haddBound) obtain ⟨next, hadd, hnext⟩ := Aeneas.Std.WP.spec_imp_exists hadd cases htot : F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS with | ok sig => have hsigVal : sig = 53#u32 := by have e := htotal53.symm.trans htot injection e with he exact he.symm subst sig by_cases hmany : d.many_digits = true · have hnatMany : C.native { d with negative := false, many_digits := true } = none := by simpa [hmany] using hnat by_cases hp : ((C.integer d.exponent d.mantissa).p_biased : Int) ≥ 0 · simp [body, hn, hnatMany, hadd, hmany, hp, htotal53, hsig52, hsub53, num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq] <;> simp only [hnatMany, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] by_cases hmEq : (C.integer d.exponent d.mantissa).m = (C.integer d.exponent next).m · by_cases hpEq : (C.integer d.exponent d.mantissa).p_biased = (C.integer d.exponent next).p_biased · have hpEqInt : (↑(C.integer d.exponent d.mantissa).p_biased : Int) = (↑(C.integer d.exponent next).p_biased : Int) := congrArg IScalar.val hpEq have hnotneg : ¬ ((C.integer d.exponent next).p_biased : Int) < 0 := by rw [← hpEqInt] omega simp [hmEq, hpEq, hpEqInt, hp, hnotneg] · simp [hmEq, hpEq, hp] · simp [hmEq, hp] <;> simp_all · have hpNeg : ((C.integer d.exponent d.mantissa).p_biased : Int) < 0 := by omega simp [body, hn, hnatMany, hadd, hmany, hp, hpNeg, htotal53, hsig52, hsub53, num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq] <;> simp only [hnatMany, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] <;> simp · have hmany' : d.many_digits = false := Bool.eq_false_of_not_eq_true hmany have hnatFew : C.native { d with negative := false, many_digits := false } = none := by simpa [hmany'] using hnat simp [body, hn, hnatFew, hmany', hsig52, hsub53] <;> simp only [hnatFew, hcastOK, hshiftEq, hbitOr, Bind.bind, Aeneas.Std.bind] <;> split_ifs <;> simp only [Bind.bind, Aeneas.Std.bind] <;> simp | fail e => simp [htot] at htotal53 | div => simp [htot] at htotal53 end dec2flt_model