在精益证明的目标中应用功能
Apply function in goal in lean proof
有一个树数据结构和一个flip
方法。我想写一个证明,如果你将 flip
方法应用于一棵树两次,你就会得到初始树。我有一个目标
⊢ flip_mytree (flip_mytree (mytree.branch t_ᾰ t_ᾰ_1 t_ᾰ_2)) = mytree.branch t_ᾰ t_ᾰ_1 t_ᾰ_2
我想用 flip_mytree
的结果替换 flip_mytree (mytree.branch t_ᾰ t_ᾰ_1 t_ᾰ_2)
。我该怎么做?或者我如何将 (mytree.branch a l r) := mytree.branch a (flip_mytree r) (flip_mytree l)
假设从 flip_mytree
函数定义中提取到我的定理上下文中?
我读过 rw
、apply
和 have
战术,但它们在这里似乎毫无用处。
下面是一个完整的例子。
universes u
inductive mytree (A : Type u) : Type u
| leaf : A → mytree
| branch : A → mytree → mytree → mytree
def flip_mytree {A : Type u} : mytree A → mytree A
| t@(mytree.leaf _) := t
| (mytree.branch a l r) := mytree.branch a (flip_mytree r) (flip_mytree l)
theorem flip_flip {A : Type u} {t : mytree A} : flip_mytree (flip_mytree t) = t :=
begin
cases t,
end
我认为你需要做归纳而不是案例才能工作。
但这只需要 induction
和 rw
就可以做到,如下所示
universes u
inductive mytree (A : Type u) : Type u
| leaf : A → mytree
| branch : A → mytree → mytree → mytree
def flip_mytree {A : Type u} : mytree A → mytree A
| t@(mytree.leaf _) := t
| (mytree.branch a l r) := mytree.branch a (flip_mytree r) (flip_mytree l)
theorem flip_flip {A : Type u} {t : mytree A} : flip_mytree (flip_mytree t) = t :=
begin
induction t with l a b₁ b₂ ih₁ ih₂,
rw [flip_mytree],
refl,
rw [flip_mytree, flip_mytree],
rw [ih₁, ih₂],
end
证明它的几种替代方法
theorem flip_flip {A : Type u} : ∀ {t : mytree A}, flip_mytree (flip_mytree t) = t
| t@(mytree.leaf _) := rfl
| (mytree.branch a l r) := by rw [flip_mytree, flip_mytree, flip_flip, flip_flip]
theorem flip_flip' {A : Type u} {t : mytree A} : flip_mytree (flip_mytree t) = t :=
by induction t; simp [*, flip_mytree]
有一个树数据结构和一个flip
方法。我想写一个证明,如果你将 flip
方法应用于一棵树两次,你就会得到初始树。我有一个目标
⊢ flip_mytree (flip_mytree (mytree.branch t_ᾰ t_ᾰ_1 t_ᾰ_2)) = mytree.branch t_ᾰ t_ᾰ_1 t_ᾰ_2
我想用 flip_mytree
的结果替换 flip_mytree (mytree.branch t_ᾰ t_ᾰ_1 t_ᾰ_2)
。我该怎么做?或者我如何将 (mytree.branch a l r) := mytree.branch a (flip_mytree r) (flip_mytree l)
假设从 flip_mytree
函数定义中提取到我的定理上下文中?
我读过 rw
、apply
和 have
战术,但它们在这里似乎毫无用处。
下面是一个完整的例子。
universes u
inductive mytree (A : Type u) : Type u
| leaf : A → mytree
| branch : A → mytree → mytree → mytree
def flip_mytree {A : Type u} : mytree A → mytree A
| t@(mytree.leaf _) := t
| (mytree.branch a l r) := mytree.branch a (flip_mytree r) (flip_mytree l)
theorem flip_flip {A : Type u} {t : mytree A} : flip_mytree (flip_mytree t) = t :=
begin
cases t,
end
我认为你需要做归纳而不是案例才能工作。
但这只需要 induction
和 rw
就可以做到,如下所示
universes u
inductive mytree (A : Type u) : Type u
| leaf : A → mytree
| branch : A → mytree → mytree → mytree
def flip_mytree {A : Type u} : mytree A → mytree A
| t@(mytree.leaf _) := t
| (mytree.branch a l r) := mytree.branch a (flip_mytree r) (flip_mytree l)
theorem flip_flip {A : Type u} {t : mytree A} : flip_mytree (flip_mytree t) = t :=
begin
induction t with l a b₁ b₂ ih₁ ih₂,
rw [flip_mytree],
refl,
rw [flip_mytree, flip_mytree],
rw [ih₁, ih₂],
end
证明它的几种替代方法
theorem flip_flip {A : Type u} : ∀ {t : mytree A}, flip_mytree (flip_mytree t) = t
| t@(mytree.leaf _) := rfl
| (mytree.branch a l r) := by rw [flip_mytree, flip_mytree, flip_flip, flip_flip]
theorem flip_flip' {A : Type u} {t : mytree A} : flip_mytree (flip_mytree t) = t :=
by induction t; simp [*, flip_mytree]