簡體   English   中英

如何在Coq中證明證明定義

[英]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賦予的結果fn如果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 請注意,此處說明了此函數的兩個想法(使用matchreturn構造,破壞False的證明以表明分支不可到達): http : //adam.chlipala.net/cpdt/html/Subset。 html

您的開發中存在兩個問題。 一個是您不能在不假設其他公理的情況下使用no_error_in_f在Coq中定義g ,因為Coq不允許從證明中提取計算信息(請在此處查看更多詳細信息)。 另一個問題是您不能在if表達式中使用H ,因為它返回的是Prop而不是bool (有關更多詳細信息,請查看此答案 )。

我找到了一種方法,如果有人感興趣,這是我的解決方案:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM