import Tablet.Binary64MagnitudeTarget import Tablet.DecimalLiteral import Tablet.SpecialLiteral import Tablet.num_float_parse_ParseFloatError import Tablet.num_imp_dec2flt_Dec2FltTrace import Tablet.num_imp_dec2flt_common_BiasedFp import Tablet.num_imp_dec2flt_decimal_Decimal open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: DispatchComponentContract] structure DispatchComponentContract where -- BODY parseNumber : Slice Std.U8 → Result (Option num.imp.dec2flt.decimal.Decimal) parseSpecial : Slice Std.U8 → Bool → Result (Option F64) native : num.imp.dec2flt.decimal.Decimal → Result (Option F64) integer : Std.I64 → Std.U64 → Result num.imp.dec2flt.common.BiasedFp slow : Slice Std.U8 → Result num.imp.dec2flt.common.BiasedFp assemble : num.imp.dec2flt.common.BiasedFp → Result F64 dispatch : Str → Result ((core.result.Result F64 num.float_parse.ParseFloatError) × num.imp.dec2flt.Dec2FltTrace F64) numeric_acceptance : ∀ (s : Slice Std.U8) (n : DecimalLiteral), n.bytes = s.val → n.sign = none → s.val ≠ [] → ∃ d, parseNumber s = .ok (some d) numeric_rejection : ∀ (s : Slice Std.U8), s.val ≠ [] → (¬ ∃ n : DecimalLiteral, n.bytes = s.val ∧ n.sign = none) → parseNumber s = .ok none numeric_record : ∀ (s : Slice Std.U8) (d : num.imp.dec2flt.decimal.Decimal), s.val ≠ [] → parseNumber s = .ok (some d) → d.negative = false ∧ d.mantissa.val < 10 ^ 19 ∧ ∃ n : DecimalLiteral, n.bytes = s.val ∧ n.sign = none ∧ (d.many_digits = false → n.accumulatedValue = (d.mantissa.val : Real) * 10 ^ d.exponent.val) ∧ (d.many_digits = true → (d.mantissa.val : Real) * 10 ^ d.exponent.val ≤ n.accumulatedValue ∧ n.accumulatedValue < ((d.mantissa.val + 1 : Nat) : Real) * 10 ^ d.exponent.val) numeric_target_preservation : ∀ (s : Slice Std.U8) (d : num.imp.dec2flt.decimal.Decimal) (n : DecimalLiteral), n.bytes = s.val → n.sign = none → parseNumber s = .ok (some d) → ∀ bits : Nat, Binary64MagnitudeTarget |n.exactValue| bits ↔ Binary64MagnitudeTarget |n.accumulatedValue| bits special_contract : ∀ (s : Slice Std.U8) (negative : Bool), ((∃ k : SpecialLiteral, k.bytes = s.val ∧ k.sign = none ∧ k.isNan = false) → ∃ f, parseSpecial s negative = .ok (some f) ∧ f.val % 2 ^ 63 = 2047 * 2 ^ 52 ∧ f.val / 2 ^ 63 = if negative then 1 else 0) ∧ ((∃ k : SpecialLiteral, k.bytes = s.val ∧ k.sign = none ∧ k.isNan = true) → ∃ f, parseSpecial s negative = .ok (some f) ∧ f.val % 2 ^ 63 / 2 ^ 52 = 2047 ∧ f.val % 2 ^ 52 ≠ 0) ∧ ((¬ ∃ k : SpecialLiteral, k.bytes = s.val ∧ k.sign = none) → parseSpecial s negative = .ok none) native_contract : ∀ (d : num.imp.dec2flt.decimal.Decimal) (o : Option F64), native d = .ok o → o = none ∨ ∃ f, o = some f ∧ d.many_digits = false ∧ Binary64MagnitudeTarget ((d.mantissa.val : Real) * 10 ^ d.exponent.val) (f.val % 2 ^ 63) ∧ f.val / 2 ^ 63 = if d.negative then 1 else 0 integer_contract : ∀ (q : Std.I64) (w : Std.U64) (fp : num.imp.dec2flt.common.BiasedFp), integer q w = .ok fp → fp.p_biased.val < 0 ∨ Binary64MagnitudeTarget ((w.val : Real) * 10 ^ q.val) (fp.m.bv ||| (fp.p_biased.bv.signExtend 64 <<< 52)).toNat slow_contract : ∀ (s : Slice Std.U8) (n : DecimalLiteral) (fp : num.imp.dec2flt.common.BiasedFp), n.bytes = s.val → n.sign = none → slow s = .ok fp → Binary64MagnitudeTarget |n.accumulatedValue| (fp.m.bv ||| (fp.p_biased.bv.signExtend 64 <<< 52)).toNat assembly_contract : ∀ fp : num.imp.dec2flt.common.BiasedFp, assemble fp = .ok (⟨fp.m.bv ||| (fp.p_biased.bv.signExtend 64 <<< 52)⟩ : F64) dispatch_total : ∀ s : Str, ∃ (r : core.result.Result F64 num.float_parse.ParseFloatError) (trace : num.imp.dec2flt.Dec2FltTrace F64), dispatch s = .ok (r, trace) empty_branch : ∀ (s : Str) (r : core.result.Result F64 num.float_parse.ParseFloatError), dispatch s = .ok (r, .EmptyInput) → s.val = [] ∧ ∃ e, r = .Err e sign_only_branch : ∀ (s : Str) (r : core.result.Result F64 num.float_parse.ParseFloatError) (body : Slice Std.U8) (negative : Bool), dispatch s = .ok (r, .SignOnly body negative) → body.val = [] ∧ ((s.val = 45#u8 :: body.val ∧ negative = true) ∨ (s.val = 43#u8 :: body.val ∧ negative = false)) ∧ ∃ e, r = .Err e invalid_branch : ∀ (s : Str) (r : core.result.Result F64 num.float_parse.ParseFloatError) (body : Slice Std.U8) (negative : Bool) (numberResult : Option num.imp.dec2flt.decimal.Decimal) (specialResult : Option F64), dispatch s = .ok (r, .Invalid body negative numberResult specialResult) → body.val ≠ [] ∧ ((s.val = body.val ∧ negative = false) ∨ (s.val = 45#u8 :: body.val ∧ negative = true) ∨ (s.val = 43#u8 :: body.val ∧ negative = false)) ∧ parseNumber body = .ok numberResult ∧ numberResult = none ∧ parseSpecial body negative = .ok specialResult ∧ specialResult = none ∧ ∃ e, r = .Err e special_branch : ∀ (s : Str) (r : core.result.Result F64 num.float_parse.ParseFloatError) (body : Slice Std.U8) (negative : Bool) (numberResult : Option num.imp.dec2flt.decimal.Decimal) (specialResult : Option F64) (value : F64), dispatch s = .ok (r, .Special body negative numberResult specialResult value) → body.val ≠ [] ∧ ((s.val = body.val ∧ negative = false) ∨ (s.val = 45#u8 :: body.val ∧ negative = true) ∨ (s.val = 43#u8 :: body.val ∧ negative = false)) ∧ parseNumber body = .ok numberResult ∧ numberResult = none ∧ parseSpecial body negative = .ok specialResult ∧ specialResult = some value ∧ r = .Ok value native_branch : ∀ (s : Str) (r : core.result.Result F64 num.float_parse.ParseFloatError) (body : Slice Std.U8) (negative : Bool) (numberResult : Option num.imp.dec2flt.decimal.Decimal) (fastInput : num.imp.dec2flt.decimal.Decimal) (fastResult : Option F64) (value : F64), dispatch s = .ok (r, .FastPath body negative numberResult fastInput fastResult value) → body.val ≠ [] ∧ ((s.val = body.val ∧ negative = false) ∨ (s.val = 45#u8 :: body.val ∧ negative = true) ∨ (s.val = 43#u8 :: body.val ∧ negative = false)) ∧ parseNumber body = .ok numberResult ∧ (∃ d, numberResult = some d ∧ fastInput = { d with negative := negative }) ∧ native fastInput = .ok fastResult ∧ fastResult = some value ∧ r = .Ok value assembled_branch : ∀ (s : Str) (r : core.result.Result F64 num.float_parse.ParseFloatError) (a : num.imp.dec2flt.Dec2FltAssemblyTrace F64), dispatch s = .ok (r, .Assembled a) → a.body.val ≠ [] ∧ ((s.val = a.body.val ∧ a.negative = false) ∨ (s.val = 45#u8 :: a.body.val ∧ a.negative = true) ∨ (s.val = 43#u8 :: a.body.val ∧ a.negative = false)) ∧ parseNumber a.body = .ok a.parse_number_result ∧ ∃ d, a.parse_number_result = some d ∧ a.fast_path_input = { d with negative := a.negative } ∧ native a.fast_path_input = .ok a.fast_path_result ∧ a.fast_path_result = none ∧ a.first_compute_exponent = d.exponent ∧ a.first_compute_mantissa = d.mantissa ∧ integer a.first_compute_exponent a.first_compute_mantissa = .ok a.first_compute_result ∧ (a.second_compute_guard = true ↔ d.many_digits = true ∧ 0 ≤ a.first_compute_result.p_biased.val) ∧ (a.second_compute_guard = false → a.second_compute_exponent = none ∧ a.second_compute_mantissa = none ∧ a.second_compute_result = none ∧ a.second_equals_first = none ∧ a.forced_deferral = false ∧ a.post_comparison_pair = a.first_compute_result) ∧ (a.second_compute_guard = true → ∃ (w : Std.U64) (second : num.imp.dec2flt.common.BiasedFp), w.val = d.mantissa.val + 1 ∧ a.second_compute_exponent = some d.exponent ∧ a.second_compute_mantissa = some w ∧ integer d.exponent w = .ok second ∧ a.second_compute_result = some second ∧ ((a.second_equals_first = some true ∧ second = a.first_compute_result ∧ a.forced_deferral = false ∧ a.post_comparison_pair = a.first_compute_result) ∨ (a.second_equals_first = some false ∧ second ≠ a.first_compute_result ∧ a.forced_deferral = true ∧ a.post_comparison_pair.m = a.first_compute_result.m ∧ a.post_comparison_pair.p_biased = (-1)#i32))) ∧ (a.slow_path_guard = true ↔ a.post_comparison_pair.p_biased.val < 0) ∧ (a.slow_path_guard = false → a.slow_path_result = none ∧ a.selected_pair = a.post_comparison_pair) ∧ (a.slow_path_guard = true → ∃ replacement, slow a.body = .ok replacement ∧ a.slow_path_result = some replacement ∧ a.selected_pair = replacement) ∧ assemble a.selected_pair = .ok a.assembly_result ∧ a.assembled_word = a.assembly_result ∧ a.lexical_sign_applied = a.negative ∧ (a.assembly_result.val < 2 ^ 63 → a.result.val % 2 ^ 63 = a.assembly_result.val ∧ a.result.val / 2 ^ 63 = if a.lexical_sign_applied then 1 else 0) ∧ r = .Ok a.result end dec2flt_model