import Tablet.DecimalGoalVocabularyDefault import Tablet.P9_T1_ActualDispatchOperations import Tablet.num_imp_dec2flt_dec2flt import Tablet.num_imp_dec2flt_dec2flt_with_trace import Tablet.SourceValidByteSlice import Tablet.ModelStrAsBytes import Tablet.ModelSliceFirst import Tablet.SpecialHelperRejection import Tablet.P401_T1_ExactZeroSignificand import Tablet.P401_T1_UnsignedZeroRecord import Tablet.P401_T1_ZeroRounding import Tablet.P401_T1_ComputeZeroPair import Tablet.P13_T1_NativeCorrectOrDefer import Tablet.RoundingDeterminism import Tablet.AssemblyCorrespondence import Tablet.Binary64ApplySign open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P13_T1_ParserSignedZero] theorem P13_T1_ParserSignedZero : forall (s : Str) (x : DecimalLexeme) (f : F64), DecimalGoalVocabularyDefault.sourceString s -> DecimalGoalVocabularyDefault.validNumeric x -> DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes s -> DecimalGoalVocabularyDefault.exactMagnitude x = 0 -> num.imp.dec2flt.dec2flt F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64 F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64 F64.Insts.CoreOpsArithNegF64 s = ok (core.result.Result.Ok f) -> f.val = if DecimalGoalVocabularyDefault.lexicalNegative x then 2 ^ 63 else 0 := by -- BODY intro s x f hsource hvalid hrender hzero hpublic have bind_ok_inv : ∀ {α β : Type} (r : Result α) (g : α → Result β) y, Bind.bind r g = ok y → ∃ z, r = ok z ∧ g z = ok y := by intro α β r g 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 z => exact ⟨z, rfl, hr⟩ obtain ⟨zeroFold, zeroDigits⟩ := P401_T1_ExactZeroSignificand x hzero let fractionBytes : List Nat := match x.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d) let exponentBytes : List Nat := match x.exponent with | none => [] | some (upper, sign, ds) => (if upper then 69 else 101) :: (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++ ds.map (fun d => 48 + d) let numericBody : List Nat := x.integerDigits.map (fun d => 48 + d) ++ fractionBytes ++ exponentBytes have numericHead : ∃ head rest, numericBody = head :: rest ∧ (head = 46 ∨ (48 ≤ head ∧ head < 58)) := by have hv := hvalid dsimp only [DecimalGoalVocabularyDefault] at hv cases hi : x.integerDigits with | nil => cases hf : x.fractionDigits with | none => simp [hi, hf] at hv | some ds => refine ⟨46, ds.map (fun d => 48 + d) ++ exponentBytes, ?_, Or.inl rfl⟩ simp [numericBody, fractionBytes, hi, hf] | cons d ds => refine ⟨48 + d, ds.map (fun d => 48 + d) ++ fractionBytes ++ exponentBytes, ?_, Or.inr ?_⟩ · simp [numericBody, hi] · have hd : d < 10 := hv.1 d (by simp [hi]) omega obtain ⟨head, rest, hhead, hheadRange⟩ := numericHead have hrenderBody : (match x.leadingSign with | none => [] | some negative => [if negative then 45 else 43]) ++ (head :: rest) = DecimalGoalVocabularyDefault.bytes s := by rw [← hhead] simpa only [DecimalGoalVocabularyDefault, numericBody, fractionBytes, exponentBytes, List.append_assoc] using hrender have notSpecial : ∀ nan : Bool, ¬ DecimalGoalVocabularyDefault.specialLiteral (head :: rest) none nan := by intro nan hspecial dsimp only [DecimalGoalVocabularyDefault] at hspecial rcases hspecial with ⟨specialBody, hspecialBody, hcase⟩ simp only [List.nil_append] at hspecialBody rw [← hspecialBody] at hcase cases nan with | false => simp only [Bool.false_eq_true, if_false] at hcase rcases hcase with hcase | hcase <;> simp only [List.forall₂_cons] at hcase <;> omega | true => simp only [if_true, List.forall₂_cons] at hcase omega unfold num.imp.dec2flt.dec2flt at hpublic obtain ⟨out, hout, hpublic⟩ := bind_ok_inv _ _ _ hpublic rcases out with ⟨publicResult, trace⟩ cases hpublic have traced : num.imp.dec2flt.dec2flt_with_trace F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64 F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64 F64.Insts.CoreOpsArithNegF64 s = ok (core.result.Result.Ok f, trace) := hout rcases s with ⟨source, source_bound⟩ cases source with | nil => unfold num.imp.dec2flt.dec2flt_with_trace at traced obtain ⟨body, hbody, traced⟩ := bind_ok_inv _ _ _ traced have hbodyEq : body = (⟨[], source_bound⟩ : Slice Std.U8) := by simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody.symm subst body obtain ⟨firstOption, hfirst, traced⟩ := bind_ok_inv _ _ _ traced have hfirstEq : firstOption = none := by simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm subst firstOption obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced cases traced | cons first tail => unfold num.imp.dec2flt.dec2flt_with_trace at traced obtain ⟨body, hbody, traced⟩ := bind_ok_inv _ _ _ traced have hbodyEq : body = (⟨first :: tail, source_bound⟩ : Slice Std.U8) := by simpa [core.str.Str.as_bytes, ModelStrAsBytes] using hbody.symm subst body obtain ⟨firstOption, hfirst, traced⟩ := bind_ok_inv _ _ _ traced have hfirstEq : firstOption = some first := by simpa [core.slice.Slice.first, ModelSliceFirst] using hfirst.symm subst firstOption obtain ⟨body1, hbody1, traced⟩ := bind_ok_inv _ _ _ traced have hstrip : decide (first = 45#u8) = DecimalGoalVocabularyDefault.lexicalNegative x ∧ DecimalGoalVocabularyDefault.bytes body1 = head :: rest ∧ SourceValidByteSlice body1 := by have hslen := hsource.1 dsimp only [DecimalGoalVocabularyDefault] at hrenderBody simp only [List.map_cons] at hrenderBody cases hsign : x.leadingSign with | none => simp only [hsign, List.nil_append, List.cons.injEq] at hrenderBody have hminus : first ≠ 45#u8 := by intro heq have hv := congrArg UScalar.val heq norm_num at hv omega have hplus : first ≠ 43#u8 := by intro heq have hv := congrArg UScalar.val heq norm_num at hv omega simp only [hminus, hplus, if_false] at hbody1 cases hbody1 refine ⟨?_, ?_, hslen⟩ · simp [DecimalGoalVocabularyDefault, hsign, hminus] · dsimp only [DecimalGoalVocabularyDefault] simp only [List.map_cons] rw [← hrenderBody.1, ← hrenderBody.2] | some negative => cases negative with | false => simp only [hsign, Bool.false_eq_true, if_false, List.cons_append, List.nil_append, List.cons.injEq] at hrenderBody have hfirstPlus : first = 43#u8 := by apply UScalar.eq_of_val_eq norm_num exact hrenderBody.1.symm simp only [hfirstPlus, if_true] at hbody1 simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop] at hbody1 cases hbody1 refine ⟨?_, ?_, ?_⟩ · simp [DecimalGoalVocabularyDefault, hsign, hfirstPlus] · simpa only [DecimalGoalVocabularyDefault] using hrenderBody.2.symm · unfold SourceValidByteSlice simp at hslen ⊢ omega | true => simp only [hsign, if_true, List.cons_append, List.nil_append, List.cons.injEq] at hrenderBody have hfirstMinus : first = 45#u8 := by apply UScalar.eq_of_val_eq norm_num exact hrenderBody.1.symm simp only [hfirstMinus, if_true] at hbody1 simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop] at hbody1 cases hbody1 refine ⟨?_, ?_, ?_⟩ · simp [DecimalGoalVocabularyDefault, hsign, hfirstMinus] · simpa only [DecimalGoalVocabularyDefault] using hrenderBody.2.symm · unfold SourceValidByteSlice simp at hslen ⊢ omega obtain ⟨empty, hempty, traced⟩ := bind_ok_inv _ _ _ traced by_cases emptyTrue : empty = true · simp only [emptyTrue, if_true] at traced obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced cases traced · have emptyFalse : empty = false := Bool.eq_false_of_not_eq_true emptyTrue simp only [emptyFalse] at traced obtain ⟨parsed, hparsed, traced⟩ := bind_ok_inv _ _ _ traced cases parsed with | none => obtain ⟨special, hspecial, traced⟩ := bind_ok_inv _ _ _ traced have hrejection := SpecialHelperRejection body1 (decide (first = 45#u8)) hstrip.2.2 (by rw [hstrip.2.1]; exact notSpecial false) (by rw [hstrip.2.1]; exact notSpecial true) rw [hrejection] at hspecial cases hspecial obtain ⟨pfe, hpfe, traced⟩ := bind_ok_inv _ _ _ traced cases traced | some r => have unsignedRender : DecimalGoalVocabularyDefault.renderNumeric { x with leadingSign := none } = DecimalGoalVocabularyDefault.bytes body1 := by change numericBody = DecimalGoalVocabularyDefault.bytes body1 rw [hhead, hstrip.2.1] obtain ⟨hrzero, hrmany, hrnegative⟩ := P401_T1_UnsignedZeroRecord body1 { x with leadingSign := none } r hstrip.2.2 rfl hvalid unsignedRender zeroDigits hparsed obtain ⟨fast, hfast, traced⟩ := bind_ok_inv _ _ _ traced cases fast with | some value => have hnative := P13_T1_NativeCorrectOrDefer ({ r with negative := decide (first = 45#u8) }) (some value) hfast rcases hnative with hnone | ⟨value', magnitude, hvalue, _, hround, hsigned⟩ · simp at hnone · cases hvalue have hdecimalZero : DecimalGoalVocabularyDefault.decimalMagnitude { r with negative := decide (first = 45#u8) } = 0 := by dsimp only [DecimalGoalVocabularyDefault] rw [hrzero] simp rw [hdecimalZero] at hround have hmzero := RoundingDeterminism 0 magnitude (⟨BitVec.ofNat 64 0⟩ : F64) (le_refl 0) hround P401_T1_ZeroRounding subst magnitude cases traced dsimp only [DecimalGoalVocabularyDefault] at hsigned have signedVal : f.val = 0 + if decide (first = 45#u8) then 2 ^ 63 else 0 := hsigned.2 simpa only [hstrip.1, Nat.zero_add] using signedVal | none => obtain ⟨firstPair, hfirstPair, traced⟩ := bind_ok_inv _ _ _ traced have hrword : r.mantissa = 0#u64 := by apply UScalar.eq_of_val_eq simpa using hrzero rw [hrword] at hfirstPair have hpair := P401_T1_ComputeZeroPair r.exponent firstPair hfirstPair subst firstPair have hnotnegative : ¬ (0#i32 : Std.I32) < 0#i32 := by decide obtain ⟨phase1, hphase1, traced⟩ := bind_ok_inv _ _ _ traced simp only [hrmany, Bool.false_eq_true, if_false] at hphase1 cases hphase1 obtain ⟨phase2, hphase2, traced⟩ := bind_ok_inv _ _ _ traced simp only [Bool.false_eq_true, if_false] at hphase2 cases hphase2 obtain ⟨phase3, hphase3, traced⟩ := bind_ok_inv _ _ _ traced simp only [hnotnegative, if_false] at hphase3 cases hphase3 obtain ⟨i, hi, traced⟩ := bind_ok_inv _ _ _ traced obtain ⟨i1, hi1, traced⟩ := bind_ok_inv _ _ _ traced obtain ⟨i2, hi2, traced⟩ := bind_ok_inv _ _ _ traced obtain ⟨word, hword, traced⟩ := bind_ok_inv _ _ _ traced obtain ⟨assembled, hassembled, traced⟩ := bind_ok_inv _ _ _ traced obtain ⟨canonical, hcanonical, canonicalSpec⟩ := AssemblyCorrespondence { m := 0#u64, p_biased := 0#i32 } rw [hassembled] at hcanonical cases hcanonical have assembledZero : assembled.val = 0 := by change assembled.val = Nat.lor 0 (((0 : Int).emod (2 ^ 64)).toNat * 2 ^ 52 % 2 ^ 64) at canonicalSpec exact canonicalSpec by_cases negative : first = 45#u8 · simp only [negative, if_true] at traced obtain ⟨result, hresult, traced⟩ := bind_ok_inv _ _ _ traced cases traced change ModelF64Neg assembled = ok f at hresult obtain ⟨signed, hsign, hsigned⟩ := Binary64ApplySign true assembled (by rw [assembledZero]; norm_num) simp only [if_true, hresult, bind_tc_ok] at hsign cases hsign have hlexical : DecimalGoalVocabularyDefault.lexicalNegative x = true := by rw [← hstrip.1] simp [negative] dsimp only [DecimalGoalVocabularyDefault] at hsigned simpa only [hlexical, if_true, assembledZero, Nat.zero_add] using hsigned.2 · simp only [negative, if_false] at traced cases traced have hlexical : DecimalGoalVocabularyDefault.lexicalNegative x = false := by rw [← hstrip.1] simp [negative] simpa only [hlexical, Bool.false_eq_true, if_false] using assembledZero end dec2flt_model