May*_*erg 9 coq ackermann totality
我试图在Coq中定义Ackermann-Peters函数,我收到一条我不明白的错误消息.正如你所看到的,我把a, bAckermann 的论据打包成一对ab; 我提供了一个定义参数的排序函数的排序.然后我使用Function表单来定义Ackermann本身,为它提供ab参数的排序函数.
Require Import Recdef.
Definition ack_ordering (ab1 ab2 : nat * nat) :=
match (ab1, ab2) with
|((a1, b1), (a2, b2)) =>
(a1 > a2) \/ ((a1 = a2) /\ (b1 > b2))
end.
Function ack (ab : nat * nat) {wf ack_ordering} : nat :=
match ab with
| (0, b) => b + 1
| (a, 0) => ack (a-1, 1)
| (a, b) => ack (a-1, ack (a, b-1))
end.
Run Code Online (Sandbox Code Playgroud)
我得到的是以下错误消息:
错误:没有这样的部分变量或假设:
ack.
我不确定是什么困扰Coq,但是在互联网上搜索,我发现使用通过排序或度量定义的递归函数可能存在问题,其中递归调用发生在匹配中.然而,使用突起fst和snd和一个if-then-else产生一个不同的错误消息.有人可以建议如何在Coq中定义Ackermann吗?
似乎Function无法解决此问题。但是,它的表亲Program Fixpoint可以。
让我们定义一些首先处理有根据的引理:
Require Import Coq.Program.Wf.
Require Import Coq.Arith.Arith.
Definition lexicographic_ordering (ab1 ab2 : nat * nat) : Prop :=
match ab1, ab2 with
| (a1, b1), (a2, b2) =>
(a1 < a2) \/ ((a1 = a2) /\ (b1 < b2))
end.
(* this is defined in stdlib, but unfortunately it is opaque *)
Lemma lt_wf_ind :
forall n (P:nat -> Prop), (forall n, (forall m, m < n -> P m) -> P n) -> P n.
Proof. intro p; intros; elim (lt_wf p); auto with arith. Defined.
(* this is defined in stdlib, but unfortunately it is opaque too *)
Lemma lt_wf_double_ind :
forall P:nat -> nat -> Prop,
(forall n m,
(forall p (q:nat), p < n -> P p q) ->
(forall p, p < m -> P n p) -> P n m) -> forall n m, P n m.
Proof.
intros P Hrec p. pattern p. apply lt_wf_ind.
intros n H q. pattern q. apply lt_wf_ind. auto.
Defined.
Lemma lexicographic_ordering_wf : well_founded lexicographic_ordering.
Proof.
intros (a, b); pattern a, b; apply lt_wf_double_ind.
intros m n H1 H2.
constructor; intros (m', n') [G | [-> G]].
- now apply H1.
- now apply H2.
Defined.
Run Code Online (Sandbox Code Playgroud)
现在我们可以定义Ackermann-Péter函数:
Program Fixpoint ack (ab : nat * nat) {wf lexicographic_ordering ab} : nat :=
match ab with
| (0, b) => b + 1
| (S a, 0) => ack (a, 1)
| (S a, S b) => ack (a, ack (S a, b))
end.
Next Obligation.
inversion Heq_ab; subst. left; auto. Defined.
Next Obligation.
apply lexicographic_ordering_wf. Defined.
Run Code Online (Sandbox Code Playgroud)
一些简单的测试表明我们可以使用ack以下方法进行计算:
Example test1 : ack (1, 2) = 4 := eq_refl.
Example test2 : ack (3, 4) = 125 := eq_refl. (* this may take several seconds *)
Run Code Online (Sandbox Code Playgroud)
使用M. Sozeau和C. Mangin 的Equations插件,可以通过以下方式定义函数:
From Equations Require Import Equations Subterm.
Equations ack (p : nat * nat) : nat :=
ack p by rec p (lexprod _ _ lt lt) :=
ack (pair 0 n) := n + 1;
ack (pair (S m) 0) := ack (m, 1);
ack (pair (S m) (S n)) := ack (m, ack (S m, n)).
Run Code Online (Sandbox Code Playgroud)
不幸的是,( , )由于问题#81,无法对使用该符号。该代码是从等式的测试套件采取:ack.v。