import Tablet.P435_T1_ByteValueBlocks import Tablet.NumberHelperSplitExponentSuffix import Tablet.DecimalGoalVocabularyDefault open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P435_T1_RenderedDigitBlocks] theorem P435_T1_RenderedDigitBlocks (bytes remainder : List Std.U8) (x : DecimalLexeme) (hsign : x.leadingSign = none) (hvalid : DecimalGoalVocabularyDefault.validNumeric x) (hrender : bytes.map (fun b => b.val) = DecimalGoalVocabularyDefault.renderNumeric x ++ remainder.map (fun b => b.val)) : exists integer fraction exponent : List Std.U8, bytes = integer ++ (match x.fractionDigits with | none => [] | some _ => 46#u8 :: fraction) ++ (match x.exponent with | none => [] | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) :: (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ exponent) ++ remainder /\ integer.map (fun b => b.val) = x.integerDigits.map (fun d => 48 + d) /\ fraction.map (fun b => b.val) = (match x.fractionDigits with | none => [] | some ds => ds).map (fun d => 48 + d) /\ exponent.map (fun b => b.val) = (match x.exponent with | none => [] | some (_, _, ds) => ds).map (fun d => 48 + d) /\ (forall b, b ∈ integer -> 48 <= b.val /\ b.val <= 57) /\ (forall b, b ∈ fraction -> 48 <= b.val /\ b.val <= 57) /\ (forall b, b ∈ exponent -> 48 <= b.val /\ b.val <= 57) := by -- BODY let fv : List Nat := match x.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d) let ev : 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) have hmap : bytes.map (fun b => b.val) = x.integerDigits.map (fun d => 48 + d) ++ (fv ++ (ev ++ remainder.map (fun b => b.val))) := by dsimp [DecimalGoalVocabularyDefault] at hrender simpa [hsign, fv, ev, List.append_assoc] using hrender obtain ⟨p, hp, hi, ht, _⟩ := P435_T1_ByteValueBlocks bytes (x.integerDigits.map (fun d => 48 + d)) (fv ++ (ev ++ remainder.map (fun b => b.val))) hmap obtain ⟨q, hq, hf, he, _⟩ := P435_T1_ByteValueBlocks p.2 fv (ev ++ remainder.map (fun b => b.val)) ht obtain ⟨r, hr, hev, hrem, hunique⟩ := P435_T1_ByteValueBlocks q.2 ev (remainder.map (fun b => b.val)) he have hremEq : remainder = r.2 := (hunique r.1 remainder hev rfl).2 obtain ⟨fraction, hfraction, hfm⟩ : exists fraction : List Std.U8, q.1 = (match x.fractionDigits with | none => [] | some _ => 46#u8 :: fraction) /\ fraction.map (fun b => b.val) = (match x.fractionDigits with | none => [] | some ds => ds).map (fun d => 48 + d) := by cases hx : x.fractionDigits with | none => have hnil : q.1 = [] := by simpa [fv, hx] using hf exact ⟨[], by simp [hx, hnil]⟩ | some ds => have hf' : q.1.map (fun b => b.val) = 46 :: ds.map (fun d => 48 + d) := by simpa [fv, hx] using hf obtain ⟨b, bs, hs, hb, hbs⟩ := List.map_eq_cons_iff.mp hf' have hb' : b = 46#u8 := UScalar.eq_of_val_eq (by simpa using hb) exact ⟨bs, by simpa [hx, hb'] using hs, by simpa [hx] using hbs⟩ obtain ⟨exponent, hexponent, hem⟩ : exists exponent : List Std.U8, r.1 = (match x.exponent with | none => [] | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) :: (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ exponent) /\ exponent.map (fun b => b.val) = (match x.exponent with | none => [] | some (_, _, ds) => ds).map (fun d => 48 + d) := by cases hx : x.exponent with | none => have hnil : r.1 = [] := by simpa [ev, hx] using hev exact ⟨[], by simp [hx, hnil]⟩ | some tuple => rcases tuple with ⟨upper, sign, ds⟩ have he' : r.1.map (fun b => b.val) = (if upper then 69 else 101) :: ((match sign with | none => [] | some neg => [if neg then 45 else 43]) ++ ds.map (fun d => 48 + d)) := by simpa [ev, hx] using hev obtain ⟨b, bs, hs, hb, hbs⟩ := List.map_eq_cons_iff.mp he' have hb' : b = (if upper then 69#u8 else 101#u8) := by apply UScalar.eq_of_val_eq cases upper <;> simpa using hb obtain ⟨es, hbs', hm⟩ := NumberHelperSplitExponentSuffix sign bs ds hbs refine ⟨es, ?_, by simpa [hx] using hm⟩ cases sign with | none => simpa [hx, hb', hbs'] using hs | some negative => cases negative <;> simpa [hx, hb', hbs'] using hs have ranges : forall (bs : List Std.U8) (ds : List Nat), bs.map (fun b => b.val) = ds.map (fun d => 48 + d) -> (forall d, d ∈ ds -> d < 10) -> forall b, b ∈ bs -> 48 <= b.val /\ b.val <= 57 := by intro bs ds hm hd b hb have hbmap : b.val ∈ bs.map (fun c => c.val) := List.mem_map_of_mem hb rw [hm] at hbmap obtain ⟨d, hdm, heq⟩ := List.mem_map.mp hbmap have := hd d hdm omega have hv := hvalid dsimp [DecimalGoalVocabularyDefault] at hv rcases hv with ⟨hints, hfracs, hnonempty, hexps⟩ refine ⟨p.1, fraction, exponent, ?_, hi, hfm, hem, ranges p.1 x.integerDigits hi hints, ?_, ?_⟩ · rw [hp, hq, hr, hfraction, hexponent, ← hremEq] simp only [List.append_assoc] · apply ranges fraction _ hfm cases hx : x.fractionDigits <;> simpa [hx] using hfracs · apply ranges exponent _ hem cases hx : x.exponent with | none => simp [hx] | some tuple => rcases tuple with ⟨upper, sign, ds⟩ have hexps' : ds ≠ [] /\ (forall d, d ∈ ds -> d < 10) := by simpa [hx] using hexps exact hexps'.2 end dec2flt_model