evolution

The evolution of a Lean programmer

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
import Mathlib.Order.Lattice

variable [LinearOrder α]

def ins (a : α) : List α  List α
  | [] => [a]
  | x :: xs =>
    if a  x then a :: x :: xs else x :: ins a xs

def List.insSort : List α  List α
  | [] => []
  | x :: xs => ins x xs.insSort

def Sorted : List α  Prop
  | [] | [_] => True
  | x :: x' :: xs => x  x'  Sorted (x' :: xs)

theorem insSorted (a : α) (xs : List α) : Sorted xs  Sorted (ins a xs) := by
  induction xs
  case nil => simp [Sorted, ins]
  case cons x xs ih =>
    cases xs
    case nil =>
      by_cases h₁ : a  x
      · simp [ins, h₁, Sorted]
      · simp only [Sorted, ins, h₁, reduceIte, and_true, forall_const]
        exact le_of_lt <| lt_of_not_ge h₁
    case cons x' t =>
      by_cases h₁ : a  x
      · simp [ins, h₁, Sorted]
      · by_cases h₂ : a  x'
        · simp only [Sorted, ins, h₁, reduceIte, h₂, true_and, and_imp]
          intro h₃ h₄
          constructor
          · exact le_of_lt <| lt_of_not_ge h₁
          · exact h₄
        · simp only [Sorted, ins, h₁, reduceIte, h₂, and_imp]
          intro h₃ h₄
          constructor
          · exact h₃
          · have ih₂ := ih h₄
            simp only [ins, h₂, reduceIte] at ih₂
            exact ih₂

theorem insSortSorted (xs : List α) : Sorted xs.insSort := by
  induction xs
  case nil => simp [List.insSort, Sorted]
  case cons x xs ih =>
    rw [List.insSort]
    exact (insSorted x xs.insSort) ih

theorem insPerm (xs : List α) (a : α) : List.Perm (a :: xs) (ins a xs) := by
  induction xs
  case nil => rw [ins]
  case _ x xs ih =>
    rw [ins]
    by_cases h₁ : a  x
    · simp [h₁]
    · simp only [h₁]
      exact .trans (.swap x a xs) (.cons x ih)

theorem insSortPerm (xs : List α) : List.Perm xs xs.insSort := by
  induction xs
  case nil => rw [List.insSort]
  case cons h t ih =>
    rw [List.insSort]
    exact .trans (.cons h ih) (insPerm t.insSort h)

theorem insSortCorrect (xs : List α) : Sorted xs.insSort  xs.Perm xs.insSort :=
  insSortSorted xs, insSortPerm xs