[英]How to prove a prove definition in Coq
我目前正在与Coq合作,遇到一个我不知道如何解决的问题。
假设我们正在处理给定类型,我以nat
为例,我想使用可能失败的函数f
。 为了弥补故障,我们将f
定义为nat -> option nat
。
现在我有一个给定的假设H: nat -> bool
在这种情况下f不会失败,我什至证明了引理
Lemma no_error_in_f : forall (n:nat), H n = true -> exists (u:nat), f n = Some u.
我想定义一个函数g: nat->nat
赋予的结果f
上n
如果H n
得到满足,而只是给n
否则。 这个函数应该定义得很好,但是我不知道如何正确定义它。 如果我尝试类似Definition g (n:nat) := if H n then fn else n.
天真方法Definition g (n:nat) := if H n then fn else n.
,则打字系统存在问题。
有谁知道如何收集所有元素并告诉系统定义是合法的?
我在这里给出一个解决方案,该解决方案与问题中给出的假设相同。
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.
除了no_error_in_f
,我还使用了另一个引理,这更容易证明False
。 请注意,此处说明了此函数的两个想法(使用match
的return
构造,破坏False
的证明以表明分支不可到达): http : //adam.chlipala.net/cpdt/html/Subset。 html 。
我找到了一种方法,如果有人感兴趣,这是我的解决方案:
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).
对于那些想知道这意味着什么的人,False_rec将False的证明作为第二个参数,并证明不可能进行匹配。 期限
(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
。 这个技巧使我能够获得H1
,这是使用相等的自反性和模式匹配来获得fn = None
的证明,并且可以继续使用False
证明。
其他比赛也一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.