Elm Exactly

Theoremexists_aReady

StandardElmMech/Src/AliasOrderProof.lean:76 · tactic proof

Statement

theorem exists_aReady {placed rest : List AliasD} (hp : (placed ++ rest).Perm all)
    (hnd : (all.map (·.1)).Nodup)
    (hacyc : ∀ i < all.length, ¬ Plus all.length (mentionsIdx all) i i) (hne : rest ≠ []) :
    ∃ a ∈ rest, aReady all placed a = true

Proof

by
  have hmemall : ∀ x ∈ rest, x ∈ all := fun x hx => hp.mem_iff.mp (List.mem_append_right _ hx)
  -- the indices of the unplaced aliases
  let idxs : List Nat := (List.range all.length).filter fun j =>
    decide ((all[j]?.map (·.1)) ∈ rest.map (fun x => some x.1))
  have hidx : ∀ x ∈ rest, ∃ i, ∃ hi : i < all.length, all[i] = x ∧ i ∈ idxs := by
    intro x hx
    obtain ⟨i, hi, he⟩ := exists_idx (hmemall x hx)
    refine ⟨i, hi, he, List.mem_filter.mpr ⟨List.mem_range.mpr hi, ?_⟩⟩
    simp only [decide_eq_true_eq, List.getElem?_eq_getElem hi, Option.map_some, he, List.mem_map]
    exact ⟨x, hx, rfl⟩
  obtain ⟨x1, hx1⟩ := List.exists_mem_of_ne_nil rest hne
  obtain ⟨i1, _, _, hi1⟩ := hidx x1 hx1
  have hne' : idxs ≠ [] := fun h => by rw [h] at hi1; simp at hi1
  obtain ⟨i0, hi0, hmin⟩ := exists_min (aCnt all rest) idxs hne'
  obtain ⟨hi0r, hi0p⟩ := List.mem_filter.mp hi0
  have hi0n : i0 < all.length := List.mem_range.mp hi0r
  simp only [decide_eq_true_eq, List.getElem?_eq_getElem hi0n, Option.map_some, List.mem_map,
    Option.some.injEq] at hi0p
  obtain ⟨a0, ha0, hname⟩ := hi0p
  -- a0 and all[i0] have one name, so are one alias
  have heq : all[i0] = a0 := by
    obtain ⟨j, hj, hje⟩ := exists_idx (hmemall a0 ha0)
    have : j = i0 := by
      apply Classical.byContradiction
      intro hji
      exact names_distinct hnd j i0 hj hi0n hji (by rw [hje]; exact hname)
    subst this; exact hje
  refine ⟨a0, ha0, ?_⟩
  unfold aReady
  apply List.all_eq_true.mpr
  intro b hb
  simp only [Bool.or_eq_true, beq_iff_eq, Bool.not_eq_true', List.any_eq_true]
  by_cases hbn : b.1 = a0.1
  · exact .inl (.inl hbn)
  cases hm : mentions all a0.2.2 b.1
  · exact .inl (.inr rfl)
  by_cases hbp : ∃ x ∈ placed, x.1 = b.1
  · exact .inr hbp
  exfalso
  -- b is unplaced
  have hbrest : b ∈ rest := by
    rcases List.mem_append.mp (hp.mem_iff.mpr hb) with h | h
    · exact absurd ⟨b, h, rfl⟩ hbp
    · exact h
  obtain ⟨ib, hib, hibe, hibi⟩ := hidx b hbrest
  have hstep : mentionsIdx all i0 ib = true := by
    simp [mentionsIdx, List.getElem?_eq_getElem hi0n, List.getElem?_eq_getElem hib, heq, hibe, hm]
  have hplus : Plus all.length (mentionsIdx all) i0 ib := .step hi0n hib hstep
  have hmono : ∀ y ∈ List.range all.length,
      (decide ((all[y]?.map (·.1)) ∈ rest.map (fun x => some x.1)) && plusB all.length (mentionsIdx all) ib y) = true →
      (decide ((all[y]?.map (·.1)) ∈ rest.map (fun x => some x.1)) && plusB all.length (mentionsIdx all) i0 y) = true := by
    intro y _ hy
    simp only [Bool.and_eq_true, plusB_iff] at hy ⊢
    exact ⟨hy.1, .trans hplus hy.2⟩
  have hle := len_filter_mono hmono
  have hne2 : aCnt all rest ib ≠ aCnt all rest i0 := by
    intro he
    have := len_filter_eq hmono he ib (List.mem_range.mpr hib)
      (by simp only [Bool.and_eq_true, plusB_iff]; exact ⟨(List.mem_filter.mp hibi).2, hplus⟩)
    simp only [Bool.and_eq_true, plusB_iff] at this
    exact hacyc ib hib this.2
  have := hmin ib hibi
  unfold aCnt at hle hne2 this
  omega

Proved with tactics, which Lean expands into a proof term that its kernel checks.

Theorems it uses

Used by

In StandardElmMech/Src/AliasOrderProof.lean: aliasOrder_valid: the canonical order of aliases is one the book allows.

A Standard Definition of Elm? (Draft version 0.4) © Rupert Smith. The text is licensed under CC BY 4.0: share and adapt it freely, with credit and a link to the licence.