简体   繁体   English

如何在Coq中证明证明定义

[英]How to prove a prove definition in Coq

I am currently working with Coq and I am encountering a problem that I don't know how to solve. 我目前正在与Coq合作,遇到一个我不知道如何解决的问题。

Let's say we are working with a given type, I'll take nat for the example, and I want to use a function f that might fail. 假设我们正在处理给定类型,我以nat为例,我想使用可能失败的函数f To compensate the failure, we define f to be of type nat -> option nat . 为了弥补故障,我们将f定义为nat -> option nat

Now I have a given hypothesis H: nat -> bool under which f doesn't fail, and I have even proved the lemma 现在我有一个给定的假设H: nat -> bool在这种情况下f不会失败,我什至证明了引理

Lemma no_error_in_f : forall (n:nat), H n = true -> exists (u:nat), f n = Some u.

I want to define a function g: nat->nat which gives the result of f on n if H n is satisfied, and just gives n otherwise. 我想定义一个函数g: nat->nat赋予的结果fn如果H n得到满足,而只是给n否则。 This function should be well defined, but I don't know how to define it properly. 这个函数应该定义得很好,但是我不知道如何正确定义它。 If I try something naive like Definition g (n:nat) := if H n then fn else n. 如果我尝试类似Definition g (n:nat) := if H n then fn else n.天真方法Definition g (n:nat) := if H n then fn else n. , there is a problem in the typing system. ,则打字系统存在问题。

Does anyone knows how to gather all the elements and tell the system that the definition is legal? 有谁知道如何收集所有元素并告诉系统定义是合法的?

I give here a solution that works with the same hypotheses as the ones given in the question. 我在这里给出一个解决方案,该解决方案与问题中给出的假设相同。

Axiom f : nat -> option nat.
Axiom H : nat -> bool.
Axiom no_error_in_f : forall n,
  H n = true -> exists u, f n = Some u.

Lemma no_error_in_f_bis : forall n,
  H n = true -> f n <> None.
Proof.
  intros. apply no_error_in_f in H0. destruct H0. rewrite H0. discriminate.
Qed.

Definition g n :=
  match H n as b return H n = b -> _ with
  | true => fun H =>
    match f n as f0 return f n = f0 -> _ with
    | Some n0 => fun _ => n0
    | None => fun H0 => match no_error_in_f_bis n H H0 with end
    end eq_refl
  | false => fun _ => n
  end eq_refl.

I use another lemma than no_error_in_f , which is more convenient to prove False . 除了no_error_in_f ,我还使用了另一个引理,这更容易证明False Note that the two ideas of this function (use the return construct of match , destruct a proof of False to show that a branch is not reachable) are explained here: http://adam.chlipala.net/cpdt/html/Subset.html . 请注意,此处说明了此函数的两个想法(使用matchreturn构造,破坏False的证明以表明分支不可到达): http : //adam.chlipala.net/cpdt/html/Subset。 html

There are two problems in your development. 您的开发中存在两个问题。 One is that you cannot use no_error_in_f to define g in Coq without assuming additional axioms, because Coq does not allow to extract computational information from a proof (check here for more details). 一个是您不能在不假设其他公理的情况下使用no_error_in_f在Coq中定义g ,因为Coq不允许从证明中提取计算信息(请在此处查看更多详细信息)。 Another problem is that you can't use H in an if expression, because it returns a Prop instead of a bool (check this answer for more details). 另一个问题是您不能在if表达式中使用H ,因为它返回的是Prop而不是bool (有关更多详细信息,请查看此答案 )。

I have found a way to do this, here is my solution if anyone is interested: 我找到了一种方法,如果有人感兴趣,这是我的解决方案:

Definition g (n:nat) :nat := (match (H n) as a return a = H n -> nat with | true => (fun H_true => (match (fn) as b return b = fn -> nat with | Some u => (fun _ => u) | None => (fun H1 => False_rec _ (no_error_in_f H_true H1)) end) (eq_refl fn)) | false => n end) (eq_refl H n).

For those who would like to know what it means, False_rec take as a second argument a proof of False and certifies that the matching is not possible. 对于那些想知道这意味着什么的人,False_rec将False的证明作为第二个参数,并证明不可能进行匹配。 the term 期限

(match (fn) as b return b = fn -> nat with | Some u => (fun _ => u) | None => (fun H1 => False_rec _ (no_error_in_f H_true H1)) end) (eq_refl fn)) has type fn = f n-> nat and when I apply it to the proof eq_refl (fn) (which is a proof that fn = fn, so is typed fn = fn ), I obtain a nat . (match (fn) as b return b = fn -> nat with | Some u => (fun _ => u) | None => (fun H1 => False_rec _ (no_error_in_f H_true H1)) end) (eq_refl fn))类型为fn = f n-> nat ,当我将其应用于证明eq_refl (fn) (这是fn = fn的证明,因此键入fn = fn )时,我得到了nat This a trick that allows me to obtain H1 which is a proof that fn = None obtained using the reflexevity of equality and the pattern-matching, and that I am goin to use in my proof of False . 这个技巧使我能够获得H1 ,这是使用相等的自反性和模式匹配来获得fn = None的证明,并且可以继续使用False证明。

Same goes for the other match. 其他比赛也一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有类的情况下归纳证明类型相等? - How to prove type equality inductively without classes? 如何在隐式上证明类型相等? - How to prove type equality over an implicit? 如何在haskell中证明类型级列表属性? - How do I prove type-level list properties in haskell? 我如何证明两种类型在Scala中没有子类型关系? - How can I prove that two types have no subtyping relation in Scala? 你如何证明一个函数对于它的类型是唯一的? - How do you prove that a function is unique for its type? 一个人如何编写(和调试)一个依赖于两个参数的应用程序 apd2,并使用它来证明这种 ap 在 agda 中的功能性? - How does one write (and debug) a two arguement dependent application, apd2, and use this to prove functoriality of such ap in agda? 证明泛型类型的上限 - Prove upper type bound for generic types Coq:Universe上的符号或定义作为变量 - Coq: Notation or Definition on a universe as a variable 试图证明一个类型是 `euclidean_semiring` 的一个实例(在 Isabelle 中) - Trying to prove that a type is an instance of `euclidean_semiring` (in Isabelle) 使用 Curry-Howard 对应来证明下一个命题逻辑陈述的正确方法是什么? - What is a correct way to prove the next propositional logic statement using Curry–Howard correspondence?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM