gpt4 book ai didi

Coq:列表对的证明

转载 作者:行者123 更新时间:2023-12-04 07:37:33 24 4
gpt4 key购买 nike

我写了这个归纳谓词及其(强)规范的部分证明:

Inductive SumPairs : (nat*nat) -> list (nat*nat) -> Prop :=
| sp_base : SumPairs (0,0) nil
| sp_step : forall (l0:list (nat*nat)) (n0 n1: nat) (y:(nat*nat)), SumPairs (n0,n1) l0 -> SumPairs ((n0+(fst y)),(n1+(snd y))) (cons y l0).

Theorem sumPairs_correct : forall (l:list (nat*nat)), { n: nat | SumPairs (n,n) l }.
Proof.
...
问题是我不认为这个定理是正确的,因为 Coq 不接受类似 {n0 n1: nat | ...} 的东西.有没有办法解决这个问题,还是我想错了?
我认为谓词 SumPairs是正确的,但由于我不确定,这里有一个它应该如何工作的例子:输入 [(1,2),(3,4)] , 预期输出 [3,7]

最佳答案

您可以在结果中放入一对,例如:

Inductive SumPairs : (nat*nat) -> list (nat*nat) -> Prop :=
| sp_base : SumPairs (0,0) nil
| sp_step : forall (l0:list (nat*nat)) (n0 n1: nat) (y:(nat*nat)), SumPairs (n0,n1) l0 -> SumPairs ((n0+(fst y)),(n1+(snd y))) (cons y l0).

Theorem sumPairs_correct : forall (l:list (nat*nat)), { p: nat * nat | SumPairs p l }.
Proof.
intros l.
induction l as [|p l [[x y] IH]].
- exists (0, 0); constructor.
- now exists (x + fst p, y + snd p); constructor.
Qed.
然而,对于这个特定的任务,实际上最好只使用一个普通的函数式程序:
Require Import Coq.Lists.List.

Definition sum_list l := fold_left Nat.add l 0.

Definition sum_pairs l := (sum_list (map fst l), sum_list (map snd l)).
这个定义比第一个版本更容易阅读、理解和修改。请注意,您仍然可以使用 Coq 来推理该函数:
Lemma sum_list_cat l1 l2 : 
sum_pairs (l1 ++ l2) =
(fst (sum_pairs l1) + fst (sum_pairs l2),
snd (sum_pairs l1) + snd (sum_pairs l2)).
(* Exercise! *)

关于Coq:列表对的证明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67665495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com