是否有可能将统一错误转化为 Coq 中的目标?
Is it possible to turn unification errors into goals in Coq?
我一直在为 Coq 中的过程微积分进行形式化 (repository here), and constantly find myself trying to apply a function which fails because of equivalent, but syntactically different, subterms. This often happens because of manipulation of de Bruijn variables。由于统一失败,我通常会事先明确地替换行为不当的子项,然后应用我需要的函数。一个简单的代码作为我的意思的例子:
Require Import Lia.
Goal
forall P: nat -> Prop,
(forall a b c, P (a + (b + c))) ->
forall a b c, P (b + c + a).
Proof.
intros.
(* Unification fails here. *)
Fail apply H.
(* Replace misbehaving subterms explictly. *)
replace (b + c + a) with (a + (b + c)).
- (* Now application succeeds. *)
apply H.
- (* Show now they were the same thing. *)
lia.
Qed.
所以,我的问题是:有没有一种策略,或者是否可以用 ltac 编写一个,它类似于应用,但将统一错误转化为额外的平等目标而不是失败?
applys_eq
来自 Programming Language Foundations' LibTactics 将实现这一点。来自文档(截至本书的 6.1 版):
applys_eq H
helps proving a goal of the form P x1 .. xN
from an [sic] hypothesis H
that concludes P y1 .. yN
, where the arguments xi
and yi
may or may not be convertible. Equalities are produced for all arguments that don't unify.
The tactic invokes equates
on all arguments, then calls applys K
, and attempts reflexivity on the side equalities.
我一直在为 Coq 中的过程微积分进行形式化 (repository here), and constantly find myself trying to apply a function which fails because of equivalent, but syntactically different, subterms. This often happens because of manipulation of de Bruijn variables。由于统一失败,我通常会事先明确地替换行为不当的子项,然后应用我需要的函数。一个简单的代码作为我的意思的例子:
Require Import Lia.
Goal
forall P: nat -> Prop,
(forall a b c, P (a + (b + c))) ->
forall a b c, P (b + c + a).
Proof.
intros.
(* Unification fails here. *)
Fail apply H.
(* Replace misbehaving subterms explictly. *)
replace (b + c + a) with (a + (b + c)).
- (* Now application succeeds. *)
apply H.
- (* Show now they were the same thing. *)
lia.
Qed.
所以,我的问题是:有没有一种策略,或者是否可以用 ltac 编写一个,它类似于应用,但将统一错误转化为额外的平等目标而不是失败?
applys_eq
来自 Programming Language Foundations' LibTactics 将实现这一点。来自文档(截至本书的 6.1 版):
applys_eq H
helps proving a goal of the formP x1 .. xN
from an [sic] hypothesisH
that concludesP y1 .. yN
, where the argumentsxi
andyi
may or may not be convertible. Equalities are produced for all arguments that don't unify.The tactic invokes
equates
on all arguments, then callsapplys K
, and attempts reflexivity on the side equalities.