Problem: merge_sort

Your task is to implement the Merge Sort algorithm and verify its correctness in Lean. Given a list of integers, return a new list containing the same elements sorted in non-decreasing order. Postconditions: (1) Output is sorted. (2) Output is a permutation of input. Preconditions: None. Implementation: Recursive divide-and-conquer. Split list into two halves, recursively sort them, then merge the sorted halves. Note: You should use 'partial' or a termination proof for recursion.


import Mathlib

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

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

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

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

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

-- Proof content
theorem mergeSort_postcond_satisfied (v : List Int) (h_precond : mergeSort_precond v) :
    mergeSort_postcond v (mergeSort v h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof