DecimalSeqRenderedSplit

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Helper · Exact rendered split at the last nonzero digit

Let I,F,A be arbitrary finite lists of natural numbers and let q be a natural number. Define
M(L)=L.\operatorname{map}(n\mapsto 48+n).
Suppose I\mathbin{++}F=A\mathbin{++}[0]^q, where A is nonempty and its last entry is nonzero. Then the natural-number list
M(I)\mathbin{++}[46]\mathbin{++}M(F)
splits as X\mathbin{++}Y. More precisely, X=X_0\mathbin{++}[b] for some list X_0 and natural number b with b\ne48 and b\ne46; every entry of Y equals either 48 or 46; and the number of entries of Y equal to 48 is exactly q.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.Preamble

set_option autoImplicit false

namespace dec2flt_model

private theorem suffix_of_append_eq (I F A : List Nat) (q : Nat)
    (h : I ++ F = A ++ List.replicate q 0) (hq : q <= F.length) :
    F = F.take (F.length - q) ++ List.replicate q 0 := by
  have hlen := congrArg List.length h
  simp only [List.length_append, List.length_replicate] at hlen
  have hdrop := congrArg (List.drop (I.length + (F.length - q))) h
  have hn : I.length + (F.length - q) = A.length := by omega
  rw [hn] at hdrop
  simp only [List.drop_append_of_le_length (le_refl A.length),
    List.drop_length, List.nil_append] at hdrop
  have hleft : List.drop (I.length + (F.length - q)) (I ++ F) =
      F.drop (F.length - q) := by simp
  rw [← hn, hleft] at hdrop
  rw [← hdrop, List.take_append_drop]

private theorem prefix_of_append_eq (I F A : List Nat) (q : Nat)
    (h : I ++ F = A ++ List.replicate q 0) (hq : F.length < q) :
    exists B, I = B ++ List.replicate (q - F.length) 0 /\
      F = List.replicate F.length 0 := by
  have hlen := congrArg List.length h
  simp only [List.length_append, List.length_replicate] at hlen
  have hdrop := congrArg (List.drop A.length) h
  simp only [List.drop_append_of_le_length (le_refl A.length),
    List.drop_length, List.nil_append] at hdrop
  have hA : A.length < I.length := by omega
  have hdropLeft : List.drop A.length (I ++ F) =
      I.drop A.length ++ F := List.drop_append_of_le_length (by omega)
  rw [hdropLeft] at hdrop
  have hremLen : (I.drop A.length).length = q - F.length := by
    simp
    omega
  have hsplit : List.replicate q 0 =
      List.replicate (q - F.length) 0 ++ List.replicate F.length 0 := by
    rw [← List.replicate_add]
    congr
    omega
  rw [hsplit] at hdrop
  have hfirst : I.drop A.length = List.replicate (q - F.length) 0 := by
    have ht := congrArg (List.take (q - F.length)) hdrop
    simpa [hremLen] using ht
  have hF : F = List.replicate F.length 0 := by
    have hd := congrArg (List.drop (q - F.length)) hdrop
    simp [hremLen] at hd
    exact hd
  refine ⟨I.take A.length, ?_, hF⟩
  rw [← hfirst]
  exact (List.take_append_drop A.length I).symm

private theorem mapped_terminal (A : List Nat) (hA : A ≠ [])
    (hlast : A.getLast? ≠ some 0) :
    exists pre b, A.map (fun n => 48 + n) = pre ++ [b] /\
      b ≠ 48 /\ b ≠ 46 := by
  let last := A.getLast hA
  refine ⟨A.dropLast.map (fun n => 48 + n), 48 + last, ?_, ?_, ?_⟩
  · have ht := congrArg (List.map (fun n => 48 + n))
      (List.dropLast_append_getLast hA)
    simpa [last, List.map_append] using ht.symm
  · intro heq
    apply hlast
    rw [List.getLast?_eq_getLast hA]
    have : last = 0 := by omega
    simp [last, this]
  · omega

-- [TABLET NODE: DecimalSeqRenderedSplit]
theorem DecimalSeqRenderedSplit (I F A : List Nat) (q : Nat)
    (h : I ++ F = A ++ List.replicate q 0)
    (hA : A ≠ []) (hlast : A.getLast? ≠ some 0) :
    exists prefx suffix : List Nat,
      I.map (fun n => 48 + n) ++ 46 :: F.map (fun n => 48 + n) =
        prefx ++ suffix /\
      (exists pre b, prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46) /\
      (forall b, b ∈ suffix -> b = 48 \/ b = 46) /\
      suffix.count 48 = q := by
Proof

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

First suppose q\leq |F|. Taking the final q positions in I\mathbin{++}F=A\mathbin{++}[0]^q shows F=F_0\mathbin{++}[0]^q, with |F_0|=|F|-q.
If q<|F|, right cancellation gives I\mathbin{++}F_0=A. Choose X=M(I)\mathbin{++}[46]\mathbin{++}M(F_0) and let Y=[48]^q. The last entry of F_0 is the last entry of A and is nonzero, so the last value of X, which is 48 plus that entry, is neither 48 nor 46. If q=|F|, right cancellation instead gives I=A; choose X=M(A) and Y=[46]\mathbin{++}[48]^q. The final value of X is again 48 plus the nonzero last entry of A, so it is neither 48 nor 46.
It remains to consider |F|<q. Comparing lengths and dropping the prefix of length |A| shows that I=B\mathbin{++}[0]^{q-|F|} and F=[0]^{|F|} for some B. Substitution in the original equality and right cancellation give B=A. Choose X=M(A) and let Y=[48]^{q-|F|}\mathbin{++}[46]\mathbin{++}[48]^{|F|}. Its count of the natural value 48 is (q-|F|)+|F|=q, while X ends in 48 plus the nonzero last entry of A, which is neither 48 nor 46. All three cases give the claimed concatenation and natural-value properties.

Lean proof


  obtain ⟨pre, b, hmapA, hb48, hb46⟩ := mapped_terminal A hA hlast
  by_cases hq : q <= F.length
  · have hF := suffix_of_append_eq I F A q h hq
    by_cases heq : q = F.length
    · have hFzero : F = List.replicate q 0 := by simpa [heq] using hF
      have hI : I = A := by
        rw [hFzero] at h
        exact List.append_cancel_right h
      refine ⟨A.map (fun n => 48 + n),
        46 :: List.replicate q 48, ?_, ?_, ?_, ?_⟩
      · rw [hI, hFzero]
        simp
      · exact ⟨pre, b, hmapA, hb48, hb46⟩
      · intro c hc
        simp at hc
        rcases hc with rfl | hc
        · exact Or.inr rfl
        · exact Or.inl hc.2
      · simp
    · have hqlt : q < F.length := by omega
      let B := F.take (F.length - q)
      have hBne : B ≠ [] := by
        have hlenB : B.length = F.length - q := by simp [B]
        intro hnil
        simp [hnil] at hlenB
        omega
      have hpre : I ++ B = A := by
        have hh : (I ++ B) ++ List.replicate q 0 =
            A ++ List.replicate q 0 := by
          rw [hF] at h
          simpa [B, List.append_assoc] using h
        exact List.append_cancel_right hh
      have hBlast : B.getLast? ≠ some 0 := by
        intro hzero
        apply hlast
        rw [← hpre]
        simpa [hBne] using hzero
      obtain ⟨preB, bB, hmapB, hbB48, hbB46⟩ :=
        mapped_terminal B hBne hBlast
      refine ⟨I.map (fun n => 48 + n) ++
          46 :: B.map (fun n => 48 + n), List.replicate q 48,
        ?_, ?_, ?_, ?_⟩
      · rw [hF]
        simp [B, List.map_append, List.append_assoc]
      · refine ⟨I.map (fun n => 48 + n) ++ 46 :: preB,
          bB, ?_, hbB48, hbB46⟩
        rw [hmapB]
        simp [List.append_assoc]
      · intro c hc
        exact Or.inl (List.mem_replicate.mp hc).2
      · simp
  · have hqgt : F.length < q := by omega
    obtain ⟨B, hI, hF⟩ := prefix_of_append_eq I F A q h hqgt
    have hB : B = A := by
      have hadd : q - F.length + F.length = q := by omega
      have hh : B ++ List.replicate q 0 = A ++ List.replicate q 0 := by
        rw [hI, hF] at h
        simpa [List.append_assoc, ← List.replicate_add, hadd] using h
      exact List.append_cancel_right hh
    let suffix := List.replicate (q - F.length) 48 ++
      46 :: List.replicate F.length 48
    refine ⟨A.map (fun n => 48 + n), suffix, ?_, ?_, ?_, ?_⟩
    · rw [hI, hF, hB]
      simp [suffix, List.map_append, List.append_assoc]
    · exact ⟨pre, b, hmapA, hb48, hb46⟩
    · intro c hc
      simp [suffix] at hc
      rcases hc with hc | rfl | hc
      · exact Or.inl hc.2
      · exact Or.inr rfl
      · exact Or.inl hc.2
    · simp [suffix]
      omega

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Preamble

set_option autoImplicit false

namespace dec2flt_model

private theorem suffix_of_append_eq (I F A : List Nat) (q : Nat)
    (h : I ++ F = A ++ List.replicate q 0) (hq : q <= F.length) :
    F = F.take (F.length - q) ++ List.replicate q 0 := by
  have hlen := congrArg List.length h
  simp only [List.length_append, List.length_replicate] at hlen
  have hdrop := congrArg (List.drop (I.length + (F.length - q))) h
  have hn : I.length + (F.length - q) = A.length := by omega
  rw [hn] at hdrop
  simp only [List.drop_append_of_le_length (le_refl A.length),
    List.drop_length, List.nil_append] at hdrop
  have hleft : List.drop (I.length + (F.length - q)) (I ++ F) =
      F.drop (F.length - q) := by simp
  rw [← hn, hleft] at hdrop
  rw [← hdrop, List.take_append_drop]

private theorem prefix_of_append_eq (I F A : List Nat) (q : Nat)
    (h : I ++ F = A ++ List.replicate q 0) (hq : F.length < q) :
    exists B, I = B ++ List.replicate (q - F.length) 0 /\
      F = List.replicate F.length 0 := by
  have hlen := congrArg List.length h
  simp only [List.length_append, List.length_replicate] at hlen
  have hdrop := congrArg (List.drop A.length) h
  simp only [List.drop_append_of_le_length (le_refl A.length),
    List.drop_length, List.nil_append] at hdrop
  have hA : A.length < I.length := by omega
  have hdropLeft : List.drop A.length (I ++ F) =
      I.drop A.length ++ F := List.drop_append_of_le_length (by omega)
  rw [hdropLeft] at hdrop
  have hremLen : (I.drop A.length).length = q - F.length := by
    simp
    omega
  have hsplit : List.replicate q 0 =
      List.replicate (q - F.length) 0 ++ List.replicate F.length 0 := by
    rw [← List.replicate_add]
    congr
    omega
  rw [hsplit] at hdrop
  have hfirst : I.drop A.length = List.replicate (q - F.length) 0 := by
    have ht := congrArg (List.take (q - F.length)) hdrop
    simpa [hremLen] using ht
  have hF : F = List.replicate F.length 0 := by
    have hd := congrArg (List.drop (q - F.length)) hdrop
    simp [hremLen] at hd
    exact hd
  refine ⟨I.take A.length, ?_, hF⟩
  rw [← hfirst]
  exact (List.take_append_drop A.length I).symm

private theorem mapped_terminal (A : List Nat) (hA : A ≠ [])
    (hlast : A.getLast? ≠ some 0) :
    exists pre b, A.map (fun n => 48 + n) = pre ++ [b] /\
      b ≠ 48 /\ b ≠ 46 := by
  let last := A.getLast hA
  refine ⟨A.dropLast.map (fun n => 48 + n), 48 + last, ?_, ?_, ?_⟩
  · have ht := congrArg (List.map (fun n => 48 + n))
      (List.dropLast_append_getLast hA)
    simpa [last, List.map_append] using ht.symm
  · intro heq
    apply hlast
    rw [List.getLast?_eq_getLast hA]
    have : last = 0 := by omega
    simp [last, this]
  · omega

-- [TABLET NODE: DecimalSeqRenderedSplit]
theorem DecimalSeqRenderedSplit (I F A : List Nat) (q : Nat)
    (h : I ++ F = A ++ List.replicate q 0)
    (hA : A ≠ []) (hlast : A.getLast? ≠ some 0) :
    exists prefx suffix : List Nat,
      I.map (fun n => 48 + n) ++ 46 :: F.map (fun n => 48 + n) =
        prefx ++ suffix /\
      (exists pre b, prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46) /\
      (forall b, b ∈ suffix -> b = 48 \/ b = 46) /\
      suffix.count 48 = q := by
-- BODY
  obtain ⟨pre, b, hmapA, hb48, hb46⟩ := mapped_terminal A hA hlast
  by_cases hq : q <= F.length
  · have hF := suffix_of_append_eq I F A q h hq
    by_cases heq : q = F.length
    · have hFzero : F = List.replicate q 0 := by simpa [heq] using hF
      have hI : I = A := by
        rw [hFzero] at h
        exact List.append_cancel_right h
      refine ⟨A.map (fun n => 48 + n),
        46 :: List.replicate q 48, ?_, ?_, ?_, ?_⟩
      · rw [hI, hFzero]
        simp
      · exact ⟨pre, b, hmapA, hb48, hb46⟩
      · intro c hc
        simp at hc
        rcases hc with rfl | hc
        · exact Or.inr rfl
        · exact Or.inl hc.2
      · simp
    · have hqlt : q < F.length := by omega
      let B := F.take (F.length - q)
      have hBne : B ≠ [] := by
        have hlenB : B.length = F.length - q := by simp [B]
        intro hnil
        simp [hnil] at hlenB
        omega
      have hpre : I ++ B = A := by
        have hh : (I ++ B) ++ List.replicate q 0 =
            A ++ List.replicate q 0 := by
          rw [hF] at h
          simpa [B, List.append_assoc] using h
        exact List.append_cancel_right hh
      have hBlast : B.getLast? ≠ some 0 := by
        intro hzero
        apply hlast
        rw [← hpre]
        simpa [hBne] using hzero
      obtain ⟨preB, bB, hmapB, hbB48, hbB46⟩ :=
        mapped_terminal B hBne hBlast
      refine ⟨I.map (fun n => 48 + n) ++
          46 :: B.map (fun n => 48 + n), List.replicate q 48,
        ?_, ?_, ?_, ?_⟩
      · rw [hF]
        simp [B, List.map_append, List.append_assoc]
      · refine ⟨I.map (fun n => 48 + n) ++ 46 :: preB,
          bB, ?_, hbB48, hbB46⟩
        rw [hmapB]
        simp [List.append_assoc]
      · intro c hc
        exact Or.inl (List.mem_replicate.mp hc).2
      · simp
  · have hqgt : F.length < q := by omega
    obtain ⟨B, hI, hF⟩ := prefix_of_append_eq I F A q h hqgt
    have hB : B = A := by
      have hadd : q - F.length + F.length = q := by omega
      have hh : B ++ List.replicate q 0 = A ++ List.replicate q 0 := by
        rw [hI, hF] at h
        simpa [List.append_assoc, ← List.replicate_add, hadd] using h
      exact List.append_cancel_right hh
    let suffix := List.replicate (q - F.length) 48 ++
      46 :: List.replicate F.length 48
    refine ⟨A.map (fun n => 48 + n), suffix, ?_, ?_, ?_, ?_⟩
    · rw [hI, hF, hB]
      simp [suffix, List.map_append, List.append_assoc]
    · exact ⟨pre, b, hmapA, hb48, hb46⟩
    · intro c hc
      simp [suffix] at hc
      rcases hc with hc | rfl | hc
      · exact Or.inl hc.2
      · exact Or.inr rfl
      · exact Or.inl hc.2
    · simp [suffix]
      omega

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (44)
More program dependencies (11)
Standard-library dependencies (30)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
Technical checking records
Local closure summary
{
  "node": "DecimalSeqRenderedSplit",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "7e9be69167ef682121b1a193cd79e4691fd33d46b33a4e39de5a24891c078aec",
  "active_statement_hash": "d6a6e1655a1cbaa8de65d1332b207b84fd79fc6a9f2d8654dfa286166463ecf3",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-169",
  "boundary_theorems": {},
  "strict_theorem_deps": {},
  "strict_definition_deps": {}
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:95e6493c36792a63c7a08772b17159442a53bcc27e441e1580daf46939fc519f",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}