Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,58 @@
theory Insertionsort
imports Main
begin
fun insert :: "int ⇒ int list ⇒ int list" where
"insert x [] = [x]"
| "insert x (y#ys) = (if x ≤ y then (x#y#ys) else y#(insert x ys))"
textExample:
lemma "insert 4 [1, 2, 3, 5, 6] = [1, 2, 3, 4, 5, 6]" by(code_simp)
fun insertionsort :: "int list ⇒ int list" where
"insertionsort [] = []"
| "insertionsort (x#xs) = insert x (insertionsort xs)"
lemma "insertionsort [4, 2, 6, 1, 8, 1] = [1, 1, 2, 4, 6, 8]" by(code_simp)
text
Our function behaves the same as the \<^term>sort function of the standard library.
lemma insertionsort: "insertionsort xs = sort xs"
proof(induction xs)
case Nil
show "insertionsort [] = sort []" by simp
next
case (Cons x xs)
textOur \<^const>insert behaves the same as the std libs \<^const>insort.
have "insert a as = insort a as" for a as by(induction as) simp+
with Cons show "insertionsort (x # xs) = sort (x # xs)" by simp
qed
text
Given that we behave the same as the std libs sorting algorithm,
we get the correctness properties for free.
corollary insertionsort_correctness:
"sorted (insertionsort xs)" and
"set (insertionsort xs) = set xs"
using insertionsort by(simp)+
text
The Haskell implementation from
🌐https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#Haskell
also behaves the same. Ultimately, they all return a sorted list.
One exception to the Haskell implementation is that the type signature of
\<^const>foldr in Isabelle is slightly different:
The initial value of the accumulator goes last.
definition rosettacode_haskell_insertionsort :: "int list ⇒ int list" where
"rosettacode_haskell_insertionsort ≡ λxs. foldr insert xs []"
lemma "rosettacode_haskell_insertionsort [4, 2, 6, 1, 8, 1] =
[1, 1, 2, 4, 6, 8]" by(code_simp)
lemma "rosettacode_haskell_insertionsort xs = insertionsort xs"
unfolding rosettacode_haskell_insertionsort_def by(induction xs) simp+
end