简体   繁体   English

Coq强制和目标匹配

[英]Coq coercions and goal matching

Assume I have the following setup: 假设我有以下设置:

Inductive exp: Set :=
| CE: nat -> exp.

Inductive adt: exp -> Prop :=
| CA: forall e, adt e.

Coercion nat_to_exp := CE.

Ltac my_tactic := match goal with
| [ |- adt (CE ?N) ] => apply (CA (CE N))
end.

And I try to prove a simple theorem using the custom tactic: 我尝试使用自定义策略证明一个简单的定理:

Theorem silly: adt 0.
Proof.
  my_tactic. (* Error: No matching clauses for match. *)
Abort.

This fails, because the goal is not of the form adt (CE ?N) but of the form adt (nat_to_exp ?N) (This is shown explicitly when using Set Printing Coercions ). 之所以失败,是因为目标不是adt (CE ?N)形式,而是adt (nat_to_exp ?N)形式adt (nat_to_exp ?N)使用Set Printing Coercions adt (nat_to_exp ?N)时会明确显示)。

Trying to prove a slightly different theorem works: 试图证明定理稍有不同:

Theorem silly: adt (CE 0).
Proof.
  my_tactic. (* Success. *)
Qed.

Possible workarounds I know of: 我知道可能的解决方法:

  • Not using coercions. 不使用强制。
  • Unfolding coercions in the tactic (with unfold nat_to_exp ). 在战术中展开强制(使用unfold nat_to_exp )。 This alleviates the problem slightly, but fails as soon as a new coercion is introduced the tactic doesn't know about. 这可以稍微缓解问题,但是一旦引入新的强制措施(该策略不知道)后就会失败。

Ideally, I would like the pattern matching to succeed if the pattern matches after unfolding all definitions (The definitions should not stay unfolded, of course). 理想情况下,如果模式在展开所有定义之后匹配,则我希望模式匹配成功(当然,定义不应该保持展开)。

Is this possible? 这可能吗? If not, are there reasons why it is not possible? 如果没有,有什么原因导致不可能?

You can directly declare the constructor CE as a coercion rather than wrapping it as nat_to_exp like so: 您可以直接将构造函数CE声明为强制,而不是像这样将其包装为nat_to_exp

Coercion CE : nat >-> exp.

The proof then goes through without any issue. 这样证明就毫无问题地通过了。 If you insist on naming your coercion (eg because it's a compound expression rather than a single constructor), you can change your tactics so that it handles non unfolded coercions explicitly: 如果您坚持要命名强制(例如,因为它是一个复合表达式而不是单个构造函数),则可以更改策略,以便它明确处理未展开的强制:

Ltac my_tactic := match goal with
| [ |- adt (CE ?N) ] => apply (CA (CE N))
| [ |- adt (nat_to_exp ?N) ] => apply (CA (CE N))
end.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM