【发布时间】:2021-03-07 01:39:08
【问题描述】:
为了证明我定义的树高是 3,我卡住了。我在网上找到了二叉树的 coq 代码,但我定义的树不是二叉树。节点包含任意数量。
下面是我的树代码:
Inductive tree (X:Type) : Type :=
| empty : tree X
| branch : X -> list (tree X) -> tree X.
Arguments tree {X}.
Arguments empty {X}.
Arguments branch {X}.
Definition mytree :=
branch 1 [ branch 5 [ branch 2 [empty] ] ; branch 8 [empty] ;
branch 7 [ branch 3 [empty];branch 4 [empty] ] ].
Fixpoint height {X: Type} (node: @tree X) : nat :=
match node with
| empty => 0
| branch _ l => S (match l with
| [] => 0
| h::t => S (height h)
end)
end.
下面是单元测试。我试图证明我的树高是3(如图)
Example test_height:
height mytree = 3.
Proof.
simpl.
在我简化后,它显示:6 = 3: 1 个子目标 ______________________________________(1/1) 6 = 3
我猜是 BC 我之前定义的一个或多个函数是错误的。但我不确定是哪一个,为什么他们错了? 任何人都可以帮助或给我一些提示吗?非常感谢
PS:还有一个关于练习的提示:
提示: 您可能需要使用 let 表达式定义相互递归的函数。
它给出了 let 表达式的工作方式,如下所示: • 命名递归函数(需要关键字修复):
Definition a := let fix f (x:nat) := match x with
| O => O
| S y => f y
end
in f 2.
但我不确定如何在我的练习中使用这个 let。
【问题讨论】:
-
我认为您的
height函数是错误的 - 我不完全确定您要计算什么(不幸的是,它看起来会返回1 + 1 + the height of the first leaf of the root,这不是整棵树的高度)。
标签: coq