Elm Exactly

TheoremsortFields_eq

StandardElmMech/Eff/RoundTrip.lean:192, Lists and their values · tactic proof

With distinct labels, the fields of a record value whose labels are the type's, sorted by label and normalised, are the decoded fields.

Statement

theorem sortFields_eq (fs : PFields) (r : List (String × Val)) (hm : labelsMatch r fs = true)
    (hn : (r.map (·.1)).Nodup) (hinc : fs.labels.Pairwise (· < ·)) :
    decodedFields fs r = sortFields (normFields r)

Proof

by
  simp only [labelsMatch, Bool.and_eq_true, List.all_eq_true, List.contains_iff_mem] at hm
  have hlnd : fs.labels.Nodup := hinc.imp fun {a b} h he => by subst he; exact String.lt_irrefl _ h
  let le : String × Val → String × Val → Bool := fun a b => decide (a.1 ≤ b.1)
  have hkeysL : (decodedFields fs r).map (·.1) = fs.labels := by simp [decodedFields, Function.comp_def]
  have hLnd : ((decodedFields fs r).map (·.1)).Nodup := by rw [hkeysL]; exact hlnd
  have hperm : (normFields r).Perm (decodedFields fs r) := by
    rw [normFields_eq]
    have nd1 : (r.map fun p => (p.1, normVal p.2)).Nodup := by
      have : ((r.map fun p => (p.1, normVal p.2)).map (·.1)).Nodup := by
        simpa [Function.comp_def] using hn
      exact nodup_of_map _ this
    have nd2 : (decodedFields fs r).Nodup := nodup_of_map _ hLnd
    apply (List.perm_ext_iff_of_nodup nd1 nd2).mpr
    intro x
    constructor
    · intro hx
      obtain ⟨⟨l, v⟩, hp, rfl⟩ := List.mem_map.mp hx
      have hl : l ∈ fs.labels := hm.1 l (List.mem_map_of_mem (f := (·.1)) hp)
      simp only [decodedFields, List.mem_map]
      exact ⟨l, hl, by simp [fieldOf_of_mem hn hp]⟩
    · intro hx
      simp only [decodedFields, List.mem_map] at hx
      obtain ⟨l, hl, rfl⟩ := hx
      obtain ⟨v, hv⟩ := mem_keys_fieldOf (hm.2 l hl)
      exact List.mem_map.mpr ⟨(l, v), fieldOf_some_mem hv, by simp [hv]⟩
  unfold sortFields
  apply List.Perm.eq_of_pairwise (le := fun a b => le a b = true)
  · intro a b ha hb hab hba
    have hab' : a.1 ≤ b.1 := of_decide_eq_true hab
    have hba' : b.1 ≤ a.1 := of_decide_eq_true hba
    have hb' : b ∈ decodedFields fs r := ((List.mergeSort_perm _ _).trans hperm).mem_iff.mp hb
    exact keys_nodup_mem_eq hLnd ha hb' (String.le_antisymm hab' hba')
  · have hp : (decodedFields fs r).map (·.1) |>.Pairwise (· < ·) := by rw [hkeysL]; exact hinc
    rw [List.pairwise_map] at hp
    exact hp.imp fun {a b} h => decide_eq_true (String.not_lt.mp (String.lt_asymm h))
  · exact List.pairwise_mergeSort (le := fun a b : String × Val => decide (a.1 ≤ b.1))
      (fun a b c hab hbc => decide_eq_true (String.le_trans (of_decide_eq_true hab) (of_decide_eq_true hbc)))
      (fun a b => by
        rcases String.le_total a.1 b.1 with h | h <;> simp [h])
      _
  · exact hperm.symm.trans (List.mergeSort_perm _ _).symm

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

Theorems it uses

In StandardElmMech/Eff/RoundTrip.lean: The round trip of encoding and decoding (roundtrip), except where A88 says otherwise (NoExc), under the named hypothesis FloatRoundTrip; the exceptions shown to be needed.

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.