The tree corresponding to
can be represented by the term
Node 3 (Node 2 (Leaf 1) (Leaf 5)) (Leaf 4)
using the datatype defined by
inductive Tree' (α : Type) : Type
| Leaf : α → Tree' α
| Node : α → Tree' α → Tree' α → Tree' α
The mirror image of the previous tree is
and the list obtained by flattening it (traversing it in infix order) is
The definition of the function that calculates the mirror image is
def mirror : Tree' α → Tree' α
| Leaf x => Leaf x
| Node x l r => Node x (mirror r) (mirror l)
and the one that flattens the tree is
def flatten : Tree' α → List α
| Leaf x => [x]
| Node x l r => (flatten l) ++ [x] ++ (flatten r)
Prove that
flatten (mirror a) = reverse (flatten a)
To do this, complete the following Lean4 theory:
import Mathlib.Tactic
open List
variable {α : Type}
set_option pp.fieldNotation false
inductive Tree' (α : Type) : Type
| Leaf : α → Tree' α
| Node : α → Tree' α → Tree' α → Tree' α
namespace Tree'
variable (a l r : Tree' α)
variable (x : α)
def mirror : Tree' α → Tree' α
| Leaf x => Leaf x
| Node x l r => Node x (mirror r) (mirror l)
def flatten : Tree' α → List α
| Leaf x => [x]
| Node x l r => (flatten l) ++ [x] ++ (flatten r)
example :
flatten (mirror a) = reverse (flatten a) :=
by sorry
Read more…