[英]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
元素subsetA
為subsetA
,但未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
分支中。 您至少有三個選擇:
使用策略來構建您的cast
功能:
Definition cast (a: A) : option subsetA. destruct (filter a) eqn:prf. - exact (Some (exist _ a prf)). - exact None. Defined.
顯式使用依賴模式匹配(在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.
使用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.