Zulip Chat Archive

Stream: new members

Topic: how to prove `(a.insertionSort).all p = a.all p`


Zihao Zhang (Dec 22 2025 at 08:35):

import Aesop
theorem insertionSort_preserves_all (a : Array Int) (p : Int  Bool) :
    (a.insertionSort).all p = a.all p:= by

Vlad Tsyrklevich (Dec 22 2025 at 10:35):

There is no API at all provided for Array.insertionSort so this seems like you want some basic API. It would be easy if you could e.g. reduce it to another sorting function that does have basic API like in the below:

import Aesop
import Mathlib

theorem Array_insertionSort_eq_List_insertionSort (a : Array Int) :
    a.insertionSort = (a.toList.insertionSort (· < ·)).toArray := by
  sorry

theorem insertionSort_preserves_all (a : Array Int) (p : Int  Bool) :
    (a.insertionSort).all p = a.all p := by
  rw [Array_insertionSort_eq_List_insertionSort]
  rw [List.all_toArray,  Array.all_toList]
  apply List.Perm.all_eq
  exact List.perm_insertionSort (· < ·) a.toList

Wrenna Robson (Dec 22 2025 at 12:20):

Note that Mathlib does have List.insertionSort and that may not be a bad place to start to work out what API is necessary.

Wrenna Robson (Dec 22 2025 at 12:21):

Ah I guess you are saying this! But it would be nice if at least Batteries supported Array.insertionSort.

Jakub Nowak (Dec 22 2025 at 13:14):

Vlad Tsyrklevich said:

There is no API at all provided for Array.insertionSort so this seems like you want some basic API. It would be easy if you could e.g. reduce it to another sorting function that does have basic API like in the below:

import Aesop
import Mathlib

theorem Array_insertionSort_eq_List_insertionSort (a : Array Int) :
    a.insertionSort = (a.toList.insertionSort (· < ·)).toArray := by
  sorry

theorem insertionSort_preserves_all (a : Array Int) (p : Int  Bool) :
    (a.insertionSort).all p = a.all p := by
  rw [Array_insertionSort_eq_List_insertionSort]
  rw [List.all_toArray,  Array.all_toList]
  apply List.Perm.all_eq
  exact List.perm_insertionSort (· < ·) a.toList

I've tried filling in the sorry, but it seems like it's impossible because Array.insertionSort.traverse is private.

Wrenna Robson (Dec 22 2025 at 13:19):

Yeah I think you might have to prove it in Init.Data.Array.InsertionSort

Jakub Nowak (Dec 22 2025 at 13:51):

Wrenna Robson said:

Yeah I think you might have to prove it in Init.Data.Array.InsertionSort

Well, you can't either. Because List.insertionSort is in Mathlib.


Last updated: Feb 28 2026 at 14:05 UTC