如何编写 Ltac 以将等式两边乘以 Coq 中的组元素

How to write an Ltac to multiply both sides of a equation by a group element in Coq

使用这个组定义:

Structure group :=
  {
    G :> Set;

    id : G;
    op : G -> G -> G;
    inv : G -> G;

    op_assoc_def : forall (x y z : G), op x (op y z) = op (op x y) z;
    op_inv_l : forall (x : G), id = op (inv x) x;
    op_id_l : forall (x : G), x = op id x
  }.

(** Set implicit arguments *)
Arguments id {g}.
Arguments op {g} _ _.
Arguments inv {g} _.

Notation "x # y" := (op x y) (at level 50, left associativity).

并证明了这个定理:

Theorem mult_both_sides (G : group) : forall (a b c : G),
    a = b <-> c # a = c # b.

我如何编写一个 Ltac 来自动执行给定项左乘给定等式(目标本身或假设)的过程?

理想情况下,在证明中使用此 Ltac 应如下所示:

left_mult (arbitrary expression).
left_mult (arbitrary expression) in (hypothesis).

你真的需要一个特定的策略吗?如果你只是用 apply 到这个

Goal forall (G:group) (a b c: G), a = b.
  intros.
  apply (mult_both_sides _ _ _ c).

现在你的目标是

  G0 : group
  a, b, c : G0
  ============================
  c # a = c # b

如果你想修改一个假设H,那么只需要apply ... in H

的基础上,您可以使用 Tactic Notation 编写

Tactic Notation "left_mult" uconstr(arbitrary_expression) :=
  apply (mult_both_sides _ _ _ arbitrary_expression).
Tactic Notation "left_mult" uconstr(arbitrary_expression) "in" hyp(hypothesis) :=
  apply (mult_both_sides _ _ _ arbitrary_expression) in hypothesis.

使用 uconstr 表示 "delay typechecking of this term until we plug it into apply"。 (其他选项包括 constr ("typecheck this at the call site") 和 open_constr ("typecheck this at the call site and fill in holes with evars")。)