Problem: bubble_sort

Your task is to implement the Bubble Sort algorithm and verify its correctness in Lean. Given a list of integers, return a new list containing the same elements but in non-decreasing order. Postconditions: (1) The output list is sorted. (2) The output list is a permutation of the input list. Preconditions: None (valid for any list which fits in memory). Implementation should use the standard O(N^2) bubble sort logic: repeatedly iterate through the list, swapping adjacent elements if they are in the wrong order, until no swaps are needed.


import Mathlib

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

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

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

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

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

-- Proof content
theorem bubble_sort_postcond_satisfied (v : List Int) (h_precond : bubble_sort_precond v) :
    bubble_sort_postcond v (bubble_sort v h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof