In Lean4, the function
reverse : List α → List α
is defined such that (reverse xs) is the list obtained by reversing the order of elements in xs, such that the first element becomes the last, the second element becomes the second to last, and so on, resulting in a new list with the same elements but in the opposite sequence. For example,
reverse [3,2,5,1] = [1,5,2,3]
Its definition is
def reverseAux : List α → List α → List α
| [], ys => ys
| x::xs, ys => reverseAux xs (x::ys)
def reverse (xs : List α) : List α :=
reverseAux xs []
The following lemmas characterize its behavior:
reverseAux_nil : reverseAux [] ys = ys
reverseAux_cons : reverseAux (x::xs) ys = reverseAux xs (x::ys)
An alternative definition is
def reverse2 : List α → List α
| [] => []
| (x :: xs) => reverse2 xs ++ [x]
Prove that the two definitions are equivalent; that is,
To do this, complete the following Lean4 theory:
import Mathlib.Data.List.Basic
open List
variable {α : Type}
variable (xs : List α)
example : reverse xs = reverse2 xs :=
by sorry
Read more…