Search code examples
coqltac

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


Using this definition of a group:

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).

And having proven this theorem:

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

How do I write an Ltac that automates the process of left multiplying a given equality (either the goal itself or a hypothesis) by a given term?

Ideally, using this Ltac in a proof would look like this:

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

Solution

  • Building on the answer given by larsr, you can use Tactic Notations to write

    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.
    

    Using uconstr says "delay typechecking of this term until we plug it into apply". (Other options include constr ("typecheck this at the call site") and open_constr ("typecheck this at the call site and fill in holes with evars").)