在coq中定义多态归纳型树
为了证明我定义的树高是3,我一直在练习。我在网上找到了二叉树coq代码,但我定义的树不是二叉树。该节点包含任意数量的节点 树图片附件: 下面是我的树代码:在coq中定义多态归纳型树,coq,Coq,为了证明我定义的树高是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}. Defi
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(如图所示)
在我简化它之后,它显示为:6=3:
1子目标
______________________________________(1/1)
6=3
我猜我之前定义的一个或多个函数是错误的。但我不确定是哪一个,为什么他们错了?
有人能帮我一下或给我一些提示吗?多谢各位
PS:这项练习还有一个提示:
提示:
您可能需要使用let表达式定义相互递归的函数
下面给出了let表达式的工作原理:
•命名递归函数(需要关键字修复):
但我不知道如何在我的练习中使用此let。您错误地定义了高度函数。您应该使用伪代码定义高度
height empty = 0
height (branch _ children) = 1 + max [height child | child in children]
其中,如果子项为空,则max应返回0。一旦你正确定义了高度函数,你应该能够用自反性来证明这个命题
Fixpoint height {X: Type} (t: tree X) : nat :=
match t with
| emp_tr _ => 0
| node_tr _ l => let fix height_list (ltrees: list (tree X)) :=
match ltrees with
| [] => 0
| htr :: ttr => max (height htr) (height_list ttr)
end
in 1 + height_list l
end.
使用let求解。我认为您的
height
函数是错误的-我不完全确定您试图计算的是什么(不幸的是,它看起来将返回1+1+根的第一片叶子的高度
,而不是整棵树的高度)。
height empty = 0
height (branch _ children) = 1 + max [height child | child in children]
Fixpoint height {X: Type} (t: tree X) : nat :=
match t with
| emp_tr _ => 0
| node_tr _ l => let fix height_list (ltrees: list (tree X)) :=
match ltrees with
| [] => 0
| htr :: ttr => max (height htr) (height_list ttr)
end
in 1 + height_list l
end.