Today we'll be doing some in depth proof translation, converting a variety of proofs from day 17's sorting case study.

We'll show four proofs here, followed by some discussion of how to talk about reflected definitions in your informal proofs.

Induction on derivations #1: Permutation_length

Theorem: If $l_1$ is a permutation of $l_2$, then the two lists are the same length.

Proof: By induction on the derivation of $\texttt{Permutation}(l_1, l_2)$.

QED

Induction on derivations #2: monotonic_preserves_sortedness

Theorem: if $f : \mathbb{N} \rightarrow \mathbb{N}$ is monotonic, then mapping $f$ over a list preserves sortedness.

Proof: Suppose we have some monotonic $f$, i.e., $x \le y$ implies $f(x) \le f(y)$. We must show that if $l$ is sorted, then $\texttt{map}(f, l)$ is sorted. We go by induction on the derivation of $\texttt{sorted}(l)$.

QED

Talking about permutations #1: insert_sorted_perm

Theorem: $x::l$ is a permutation of $\texttt{insert_sorted}(x,l)$.

Proof: By induction on $l$.

QED

Talking about permutations #2: sort_perm

Theorem: $l$ is a permutation of $\texttt{sort}(l)$.

Proof: By induction on $l$.

QED

Reflection: leb vs $\le$

The leb_complete and leb_correct lemmas reflect leb into $\le$: leb n m = true iff $n \le m$. Our formal Coq proofs invoke these lemmas to move from the computational world of leb to the propositional world of $\le$.

Informal proofs treat reflection more informally. Once we've established an if-and-only-if relationship like reflection, we'll freely move between the two worlds without invoking leb_iff or any such lemma.

If you're unsure whether or not to invoke a lemma, please just ask!

Counterexamples: bad_function_breaks_sortedness

Definition: let our "bad function" $f : \mathbb{N} \rightarrow \mathbb{N}$ be defined as:

\[ f(n) = \begin{cases} 1 & n \text{ is even} \\ 0 & \text{otherwise} \\ \end{cases} \]

Theorem: $f$ does not preserve sortedness.

Pedantic proof: Suppose for a contradiction that $f$ preserved sortedness, i.e., if $l$ is sorted than $\texttt{map}(f, l)$ is sorted. Fix $l = \texttt{[$2$; $3$]}$, and observe that $l$ is sorted. We find $\texttt{map}(f, l) = \texttt{[$f(2)$; $f(3)$]} = \texttt{[$1$; $0$]}$... which is not sorted. $\unicode{x21af}$

More casual but totally acceptable proof: To see that $f$ doesn't preserve sortedness, we offer a counterexample: $l = \texttt{[$2$; $3$]}$ is sorted, but $\texttt{map}(f, l) = \texttt{[$f(2)$; $f(3)$]} = \texttt{[$1$; $0$]}$ isn't. $\unicode{x21af}$

Stormy weather

It's common to end such proofs by contradiction by ↯. A variety of $\LaTeX$ packages support it, but not MathJax (the JavaScript library that renders this textbook). I've just used a Unicode character, which you can write $\unicode{x21af}$. You can also just write QED.