簡體   English   中英

在Coq中,如何構造'sig'類型的元素

[英]In Coq, How to construct an element of 'sig' type

使用類型A的簡單歸納定義:

Inductive A: Set := mkA : nat-> A.

(*get ID of A*)
Function getId (a: A) : nat := match a with mkA n => n end. 

和子類型定義:

(* filter that test ID of *A* is 0 *)
Function filter (a: A) : bool := if (beq_nat (getId a) 0) then true else false.

(* cast bool to Prop *)
Definition IstrueB (b : bool) : Prop := if b then True else False.

(* subtype of *A* that only interests those who pass the filter *)
Definition subsetA : Set := {a : A | IstrueB (filter a) }.

我嘗試使用此代碼在filter通過時將A元素subsetAsubsetA ,但未subsetA Coq方便,因為它是'sig'類型的元素的有效構造:

Definition cast (a: A) : option subsetA :=
match (filter a) with
 | true => Some (exist _ a (IstrueB (filter a)))
 | false => None
end.

錯誤:

In environment
a : A
The term "IstrueB (filter a)" has type "Prop"
while it is expected to have type "?P a"
(unable to find a well-typed instantiation for "?P": cannot ensure that
"A -> Type" is a subtype of "?X114@{__:=a} -> Prop").

因此,Coq期望類型的實際證明(IstrueB (filter a)) ,但我提供的是類型Prop

您能否說明如何提供這種類型? 謝謝。

首先,有一個標准的is_true包裝器。 您可以像這樣明確地使用它:

Definition subsetA : Set := {a : A | is_true (filter a) }.

或隱式使用強制機制:

Coercion is_true : bool >-> Sortclass.
Definition subsetA : Set := { a : A | filter a }.

接下來,在filter a上進行非依賴性模式運算不會將filter a = true傳播到true分支中。 您至少有三個選擇:

  1. 使用策略來構建您的cast功能:

     Definition cast (a: A) : option subsetA. destruct (filter a) eqn:prf. - exact (Some (exist _ a prf)). - exact None. Defined. 
  2. 顯式使用依賴模式匹配(在Stackoverflow或CDPT中搜索“ convoy模式”):

     Definition cast' (a: A) : option subsetA := match (filter a) as fa return (filter a = fa -> option subsetA) with | true => fun prf => Some (exist _ a prf) | false => fun _ => None end eq_refl. 
  3. 使用Program功能:

     Require Import Coq.Program.Program. Program Definition cast'' (a: A) : option subsetA := match filter a with | true => Some (exist _ a _) | false => None end. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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