Types 在Coq中,如何构造“sig”类型的元素

Types 在Coq中,如何构造“sig”类型的元素,types,casting,coq,subtype,Types,Casting,Coq,Subtype,使用a型的简单归纳定义: 和子类型定义: (* 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*

使用a型的简单归纳定义:

和子类型定义:

(* 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) }.
当过滤器通过时,我尝试使用此代码将A的元素强制转换为子集,但未能确认它是“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


你能解释一下如何提供这样的类型吗?多谢各位

首先,有一个标准包装器。您可以这样明确地使用它:

Definition subsetA : Set := {a : A | is_true (filter a) }.
或者隐式使用强制机制:

Coercion is_true : bool >-> Sortclass.
Definition subsetA : Set := { a : A | filter a }.
接下来,过滤器a上的非依赖模式匹配不会将过滤器a=true传播到true分支中。您至少有三种选择:

使用策略构建您的演员阵容:

Definition cast (a: A) : option subsetA.
  destruct (filter a) eqn:prf.
  - exact (Some (exist _ a prf)).
  - exact None.
Defined.
使用依赖模式匹配在Stackoverflow或中显式搜索护航模式:

使用计划设施:

Require Import Coq.Program.Program.

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

首先是标准包装器。您可以这样明确地使用它:

Definition subsetA : Set := {a : A | is_true (filter a) }.
或者隐式使用强制机制:

Coercion is_true : bool >-> Sortclass.
Definition subsetA : Set := { a : A | filter a }.
接下来,过滤器a上的非依赖模式匹配不会将过滤器a=true传播到true分支中。您至少有三种选择:

使用策略构建您的演员阵容:

Definition cast (a: A) : option subsetA.
  destruct (filter a) eqn:prf.
  - exact (Some (exist _ a prf)).
  - exact None.
Defined.
使用依赖模式匹配在Stackoverflow或中显式搜索护航模式:

使用计划设施:

Require Import Coq.Program.Program.

Program Definition cast'' (a: A) : option subsetA :=
  match filter a with
  | true => Some (exist _ a _)
  | false => None
  end.
布尔也有真的,布尔也有真的。