Problem: insertion_sort

Your task is to implement the Insertion Sort algorithm and verify its correctness in Lean. Given a list of integers, return a sorted permutation of the list. Postconditions: (1) Output is sorted. (2) Output is a permutation of input. Preconditions: None. Implementation: Recursive or iterative insertion sort. Insert each element into the sorted prefix.


import Mathlib

-- Precondition definitions
@[reducible, simp]
def insertionSort_precond (v : List Int) : Prop :=
  -- !benchmark @start precond
  True
  -- !benchmark @end precond

-- !benchmark @start auxcode
-- !benchmark @end auxcode

-- Main function definitions
def insertionSort (v : List Int) (h_precond : insertionSort_precond v) : List Int :=
  -- !benchmark @start code
  sorry
  -- !benchmark @end code

-- Postcondition definitions
@[reducible, simp]
def insertionSort_postcond (v : List Int) (result : List Int) (h_precond : insertionSort_precond v) : Prop :=
  -- !benchmark @start postcond
   List.Sorted (· ≤ ·) result ∧ result.Perm v
  -- !benchmark @end postcond

-- !benchmark @start lemma
-- !benchmark @end lemma

-- Proof content
theorem insertionSort_postcond_satisfied (v : List Int) (h_precond : insertionSort_precond v) :
    insertionSort_postcond v (insertionSort v h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof