[英]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
赋予的结果f
上n
如果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 . 请注意,此处说明了此函数的两个想法(使用
match
的return
构造,破坏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.