Coq 为什么构造器在这里要花这么长时间?
考虑以下代码:Coq 为什么构造器在这里要花这么长时间?,coq,Coq,考虑以下代码: Inductive Even : nat -> Prop := | EO : Even O | ESS : forall n, Even n -> Even (S (S n)). Fixpoint is_even_prop (n : nat) : Prop := match n with | O => True | S O => False | S (S n) => is_even_prop n end. Theorem is
Inductive Even : nat -> Prop :=
| EO : Even O
| ESS : forall n, Even n -> Even (S (S n)).
Fixpoint is_even_prop (n : nat) : Prop :=
match n with
| O => True
| S O => False
| S (S n) => is_even_prop n
end.
Theorem is_even_prop_correct : forall n, is_even_prop n -> Even n.
Admitted.
Example Even_5000 : Even 5000.
Proof.
apply is_even_prop_correct.
Time constructor. (* ~0.45 secs *)
Undo.
Time (constructor 1). (* ~0.25 secs *)
Undo.
(* The documentation for constructor says that "constructor 1"
should be the same thing as doing this: *)
Time (apply I). (* ~0 secs *)
Undo.
(* Apparently, if there's only one applicable constructor,
reflexivity falls back on constructor and consequently
takes as much time as that tactic: *)
Time reflexivity. (* Around ~0.45 secs also *)
Undo.
(* If we manually reduce before calling constructor things are
faster, if we use the right reduction strategy: *)
Time (cbv; constructor). (* ~0 secs *)
Undo.
Time (cbn; constructor). (* ~0.5 secs *)
Qed.
Theorem is_even_prop_correct_fast : forall n, is_even_prop n = True -> Even n.
Admitted.
Example Even_5000_fast : Even 5000.
Proof.
apply is_even_prop_correct_fast.
(* Everything here is essentially 0 secs: *)
Time constructor.
Undo.
Time reflexivity.
Undo.
Time (apply eq_refl). Qed.
我只是想看看你是否可以在Prop
而不是Set
中进行反射,我偶然发现了这一点。我的问题不是如何正确地进行反射,我只是想知道为什么与第二种情况相比,第一种情况下的构造函数
速度如此之慢。(可能与第二种情况下,构造函数
可以立即看到(没有任何减少)构造函数必须是eq\u refl
有关?但之后它仍然必须减少…)
另外,在试图弄清楚构造函数在做什么时,我注意到文档中没有说明该策略将使用哪种缩减策略。这种省略是故意的吗?如果你特别想要一种缩减策略,你应该明确地说你想要哪种缩减策略(否则实现可以自由选择任何缩减策略)?简短回答:它花时间试图弄清楚你的目标是哪个归纳族的一部分(两次,在
构造函数的情况下),使用hnf
长一点的回答:做一点源潜水,看起来像,而。依次调用Tacmach.New.pf\u应用Tacred。将\u减少到\u量化的\u ind
以确定归纳类型以计数构造函数,然后依次调用每个可能的构造函数的策略.constructor\u tac
。对于True
,由于只有一个构造函数,因此建议constructor
的时间大约是constructor 1
的两倍;我猜时间因此花在减少到\u量化的\u ind
上Tacred。依次将调用减少到量化的ind
,调用依次调用hnf\u constr
。事实上,timehnf
和timeconstructor 1
看起来差不多。此外,Time构造函数
在手动hnf
之后是即时的。我不确定hnf内部使用的策略是什么。文档遗漏几乎肯定不是故意的(至少,无论当前的策略是什么,我认为都应该出现在脚注中,所以请随意报告错误),但我不清楚构造器
在确定您的目标是什么归纳族时使用的缩减策略是否应该是构造器
规范的一部分,以澄清我的构造器
的缩减策略评论:我不是说必须将缩减策略添加到文件/规格;相反,我想知道(在手册中)是否有关于一般使用的策略的说明。可能的情况是,手册中的某个地方说“如果没有另外说明,cbv将在任何地方使用”,“您不能依赖正在使用的任何特定的减少策略,相反,使用哪种策略取决于实施细节,并根据具体情况(有时是动态的)”决定,或者诸如此类。。。。因为有时这些信息似乎很有用(例如,在这里),但同时过度依赖这些低级细节可能会导致非常脆弱的证明脚本。