Coq 当您';重建一场比赛

Coq 当您';重建一场比赛,coq,Coq,我的一般问题是:当我不熟悉我正在使用的类型时,有没有一种简单的方法可以逐步在Coq中建立一个定义 考虑Coq中自然数的一个定义,来自Coq.Narith.BinNat Definition discr n : { p:positive | n = pos p } + { n = 0 }. 现在,对我来说,这个术语看起来有点混乱。假设我试图从定义中提取这个正的p。我的第一次尝试失败: Require Import Coq.Narith.BinNat. Fail Definition NToPos

我的一般问题是:当我不熟悉我正在使用的类型时,有没有一种简单的方法可以逐步在Coq中建立一个定义

考虑Coq中自然数的一个定义,来自
Coq.Narith.BinNat

Definition discr n : { p:positive | n = pos p } + { n = 0 }.
现在,对我来说,这个术语看起来有点混乱。假设我试图从定义中提取这个正的
p
。我的第一次尝试失败:

Require Import Coq.Narith.BinNat.
Fail Definition NToPos (x : N) : positive :=
  match N.discr x with
  | inright HO   => 1 
  | inleft Hpos  => Hpos
  end.
(*
Error:
In environment
x : N
Hpos : {p : positive | x = N.pos p}
The term "Hpos" has type "{p : positive | x = N.pos p}"
while it is expected to have type "positive".
*)
嗯。。。可以现在我知道我的基本误解是符号
{p:positive | x=N.pos p}
,但我从这里该怎么办呢

我的问题是,有没有更好的方法来理解定义,比如
N.discr
?我想我想要的是:

Definition NToPos (x : N) : positive :=
  match N.discr x with
  | inright HO   => 1 
  | inleft Hpos  => (* Please tell me how to further destruct Hpos *)
  end.

我最终通过检查打印N.discr和观察发现了这一点:

N.discr = 
fun n : N =>
match n as n0 return ({p : positive | n0 = N.pos p} + {n0 = 0%N}) with
| 0%N => inright eq_refl
| N.pos p =>
    inleft (exist (fun p0 : positive => N.pos p = N.pos p0) p eq_refl)
end
     : forall n : N, {p : positive | n = N.pos p} + {n = 0%N}
看到我想要的情况是
存在(乐趣p0:positive=>N.posp=N.pos p0)p eq\u refl
。然后,
存在
是关键功能。从这一点上,我能够正确地猜测
inleft(exists p Hpos)
将起作用:

Definition NToPos (x : N) : positive :=
  match N.discr x with
  | inright HO            => 1 
  | inleft (exist p Hpos) => p
  end.

我最终通过检查打印N.discr和观察发现了这一点:

N.discr = 
fun n : N =>
match n as n0 return ({p : positive | n0 = N.pos p} + {n0 = 0%N}) with
| 0%N => inright eq_refl
| N.pos p =>
    inleft (exist (fun p0 : positive => N.pos p = N.pos p0) p eq_refl)
end
     : forall n : N, {p : positive | n = N.pos p} + {n = 0%N}
看到我想要的情况是
存在(乐趣p0:positive=>N.posp=N.pos p0)p eq\u refl
。然后,
存在
是关键功能。从这一点上,我能够正确地猜测
inleft(exists p Hpos)
将起作用:

Definition NToPos (x : N) : positive :=
  match N.discr x with
  | inright HO            => 1 
  | inleft (exist p Hpos) => p
  end.

一般来说,要破译一个符号,你可以问这样的问题

Locate "{ x | p }".
在本例中,这将计算出
{p:positive | n=pos p}
的含义(用(元)变量替换符号的“可替换”部分)。它给

Notation "{ x  |  P }" := sig (fun x => P)
现在可以使用名称
sig
获取更多信息

Print sig.
(*
Inductive sig (A : Type) (P : A -> Prop) : Type :=
  exist : forall x : A,
          P x -> {x : A | P x}
Arguments exist [A]%type_scope _%function_scope
*)
它告诉您需要将
hpo
exist\p hpo
进行匹配(
参数
表示
A
是隐式的,
p
是显式的,但是
p
(作为参数)已被scrutinee的类型修复,必须忽略,其余参数,
x:A
px
,需要命名)

或者

Unset Printing Notations. (* In CoqIDE, you're told to set this from the view menu instead *)
Check N.discr.
(* Shows you that the notation stands for sig *)

然后像以前一样继续。

一般来说,要破译一个符号,你可以问这样的问题

Locate "{ x | p }".
在本例中,这将计算出
{p:positive | n=pos p}
的含义(用(元)变量替换符号的“可替换”部分)。它给

Notation "{ x  |  P }" := sig (fun x => P)
现在可以使用名称
sig
获取更多信息

Print sig.
(*
Inductive sig (A : Type) (P : A -> Prop) : Type :=
  exist : forall x : A,
          P x -> {x : A | P x}
Arguments exist [A]%type_scope _%function_scope
*)
它告诉您需要将
hpo
exist\p hpo
进行匹配(
参数
表示
A
是隐式的,
p
是显式的,但是
p
(作为参数)已被scrutinee的类型修复,必须忽略,其余参数,
x:A
px
,需要命名)

或者

Unset Printing Notations. (* In CoqIDE, you're told to set this from the view menu instead *)
Check N.discr.
(* Shows you that the notation stands for sig *)
然后像以前一样继续。

我认为你应该检查(1)计划定义(2)优化(3)评估(包括
evar
策略)。我认为你应该检查(1)计划定义(2)优化(3)评估(包括
evar
策略)。