- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 Program Fixpoint
定义以下函数或Function
在 Coq 中:
Require Import Coq.Lists.List.
Import ListNotations.
Require Import Coq.Program.Wf.
Require Import Recdef.
Inductive Tree := Node : nat -> list Tree -> Tree.
Fixpoint height (t : Tree) : nat :=
match t with
| Node x ts => S (fold_right Nat.max 0 (map height ts))
end.
Program Fixpoint mapTree (f : nat -> nat) (t : Tree) {measure (height t)} : Tree :=
match t with
Node x ts => Node (f x) (map (fun t => mapTree f t) ts)
end.
Next Obligation.
不幸的是,此时我有举证义务height t < height (Node x ts)
却不知道t
是 ts
的成员.
与 Function
类似而不是Program Fixpoint
,仅此Function
检测到问题并中止定义:
Error:
the term fun t : Tree => mapTree f t can not contain a recursive call to mapTree
我希望获得 In t ts → height t < height (Node x ts)
的证明义务.
有没有一种方法可以在不涉及重构函数定义的情况下实现这一点? (例如,我知道需要在此处内联 map
的定义的解决方法 - 我想避免这些。)
为了证明这一期望的合理性,让我展示一下当我在 Isabelle 中使用 function
做同样的事情时会发生什么。命令,(据我所知)与 Coq 的 Function
相关命令:
theory Tree imports Main begin
datatype Tree = Node nat "Tree list"
fun height where
"height (Node _ ts) = Suc (foldr max (map height ts) 0)"
function mapTree where
"mapTree f (Node x ts) = Node (f x) (map (λ t. mapTree f t) ts)"
by pat_completeness auto
termination
proof (relation "measure (λ(f,t). height t)")
show "wf (measure (λ(f, t). height t))" by auto
next
fix f :: "nat ⇒ nat" and x :: nat and ts :: "Tree list" and t
assume "t ∈ set ts"
thus "((f, t), (f, Node x ts)) ∈ measure (λ(f, t). height t)"
by (induction ts) auto
qed
在终止证明中,我得到假设 t ∈ set ts
.
请注意,Isabelle 在这里不需要手动终止证明,并且以下定义就可以正常工作:
fun mapTree where
"mapTree f (Node x ts) = Node (f x) (map (λ t. mapTree f t) ts)"
这有效是因为 map
函数具有以下形式的“同余引理”
xs = ys ⟹ (⋀x. x ∈ set ys ⟹ f x = g x) ⟹ map f xs = map g ys
function
命令用来查明终止证明只需要考虑t ∈ set ts
..
如果这样的引理不可用,例如因为我定义了
definition "map' = map"
并在 mapTree
中使用它,我得到了与 Coq 中相同的不可证明的证明义务。我可以通过声明 map'
的同余引理使其再次工作。 ,例如使用
declare map_cong[folded map'_def,fundef_cong]
最佳答案
在这种情况下,您实际上不需要具有充分普遍性的有根据的递归:
Require Import Coq.Lists.List.
Set Implicit Arguments.
Inductive tree := Node : nat -> list tree -> tree.
Fixpoint map_tree (f : nat -> nat) (t : tree) : tree :=
match t with
| Node x ts => Node (f x) (map (fun t => map_tree f t) ts)
end.
Coq 能够自行计算出对 map_tree
的递归调用是在严格的子项上执行的。然而,证明这个函数的任何事情都很困难,因为为 tree
生成的归纳原理没有用:
tree_ind :
forall P : tree -> Prop,
(forall (n : nat) (l : list tree), P (Node n l)) ->
forall t : tree, P t
这本质上与您之前描述的问题相同。幸运的是,我们可以通过用证明项证明我们自己的归纳原理来解决这个问题。
Require Import Coq.Lists.List.
Import ListNotations.
Unset Elimination Schemes.
Inductive tree := Node : nat -> list tree -> tree.
Set Elimination Schemes.
Fixpoint tree_ind
(P : tree -> Prop)
(IH : forall (n : nat) (ts : list tree),
fold_right (fun t => and (P t)) True ts ->
P (Node n ts))
(t : tree) : P t :=
match t with
| Node n ts =>
let fix loop ts :=
match ts return fold_right (fun t' => and (P t')) True ts with
| [] => I
| t' :: ts' => conj (tree_ind P IH t') (loop ts')
end in
IH n ts (loop ts)
end.
Fixpoint map_tree (f : nat -> nat) (t : tree) : tree :=
match t with
| Node x ts => Node (f x) (map (fun t => map_tree f t) ts)
end.
Unset Elimination Schemes
命令阻止 Coq 为 tree
生成其默认(且无用)归纳原理。归纳假设上 fold_right
的出现简单地表示谓词 P
对出现在 ts
中的每棵树 t'
成立>.
这是一个可以使用归纳原理证明的陈述:
Lemma map_tree_comp f g t :
map_tree f (map_tree g t) = map_tree (fun n => f (g n)) t.
Proof.
induction t as [n ts IH]; simpl; f_equal.
induction ts as [|t' ts' IHts]; try easy.
simpl in *.
destruct IH as [IHt' IHts'].
specialize (IHts IHts').
now rewrite IHt', <- IHts.
Qed.
关于recursion - 嵌套递归和 `Program Fixpoint` 或 `Function`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46838928/
我认为这个问题说明了一切,但我有一个使用 .net 安装工具包的应用程序(在 vs.2005 中),并且用户问我是否可以将它安装在 c:\Program Files\ProgramName 而不是C:
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是 Stephen Wolfram 的忠实粉丝,但他绝对是一个不怕自吹自擂的人。在许多引用资料中,他将 Mathematica 颂扬为一种不同的符号编程范式。我不是 Mathematica 用户。
我现在正在使用 Squeak4.1 学习 SmallTalk。我使用 Squeak by Example 作为教程,在这里我反驳了一个 delema,“Morphic 是由...开发的,用于自编程语言
Wikipedia有话要说: Total functional programming (also known as strong functional programming, to be cont
在阅读 Paul Graham's Essays 时, 我对 Lisp 越来越好奇了。 在this article ,他提到最强大的功能之一是您可以编写可以编写其他程序的程序。 我无法在他的网站或其他
我知道 functional programming 有几个定义。 .我认为这是一个模糊的类别。我个人的定义是接近' referential transparency '。 这个问题不是“函数式编程的
我注意到许多顶尖大学都开设了类(class),在这些类(class)中,学生将学习与计算机图形学相关的 CS 专业科目。可悲的是,这是我的大学没有提供的东西,我真的很想在 future 几年的某个时候
我正在安装100%托管代码的.NET(C#)应用程序。安装程序(InnoSetup)始终希望将应用程序安装到Vista x64中的“Program Files(x86)”文件夹中,我认为这是因为安装程
假设在 C 中,我们有以下结构: struct MyData { char key1[20]; long key2; ... /* some data */ }; 本质上,除
这个问题已经有答案了: When should I use ampersand with scanf() (3 个回答) 已关闭 6 年前。 所以我在python3中有这个“程序”,它添加了3个字符串
我编写了一个包含 self 更新程序的 Java 应用程序。自更新程序从 Web 服务器加载新的程序版本并替换应用程序文件。如果安装了应用程序,这将完美地工作,例如在用户主目录中,如果它安装在 C:\
注意:标记为社区维基。 是否有一个很好的分析为什么可视化编程语言仍然没有起飞?这些天我们仍在 80x25 文本窗口中“线性”编码;而我们表示的概念(数据结构、算法)似乎可以更直观地表示出来。 最佳答案
我一直在阅读Code Complete 2 .由于我不是以英语为母语的人,因此我需要一些时间才能理解某些陈述。我希望你描述作者在他的书中所做的这两个陈述之间的区别: You should progra
我在为我的 tomcat 设置 CLASSPATH 时遇到了这个问题。我需要在 tomcat 的 CLASSPATH 中引用我的 2 个安装。其中一个位于 C:\Program Files\Postg
这个问题已经有答案了: How can I lock a file using java (if possible) (8 个回答) 已关闭 6 年前。 我有 2-3 个程序可以修改文件,但如果有一个
我 checkout Reading stdout from one program in another program却没有找到我要找的答案 我是 Linux 的新手,我正在使用 Python 中
我有一个程序可以打印出通过或失败。我想检测卡在那里的程序并回显“超时” 我写了这样一个脚本: #!/bin/bash echo -n 'test' && timeout 5 ./mytest | gr
我非常清楚函数式编程技术和命令式编程技术之间的区别。但是现在有一种普遍的趋势是谈论“函数式语言”,这确实让我感到困惑。 当然,像 Haskell 这样的一些语言比 C 等其他语言更欢迎函数式编程。但即
请求:每个进程需要计算自己的组到所有点的距离。我的代码如下: #include stdio.h #include stdlib.h #include math.h #include string.h
我是一名优秀的程序员,十分优秀!