holdenlee
holdenlee

Reputation: 959

Rewriting a match in Coq

In Coq, suppose I have a fixpoint function f whose matching definition on (g x), and I want to use a hypothesis in the form (g x = ...) in a proof. The following is a minimal working example (in reality f, g would be more complicated):

Definition g (x:nat) := x.

Fixpoint f (x:nat) := 
  match g x with
    | O => O
    | S y => match x with 
      | O => S O
      | S z => f z
      end 
  end.

Lemma test : forall (x : nat), g x = O -> f x = O.
Proof.
  intros.
  unfold f.
  rewrite H. (*fails*)

The message shows where Coq gets stuck:

(fix f (x0 : nat) : nat :=
   match g x0 with
   | 0 => 0
   | S _ => match x0 with
            | 0 => 1
            | S z0 => f z0
            end
   end) x = 0

Error: Found no subterm matching "g x" in the current goal.

But, the commands unfold f. rewrite H. does not work.

How do I get Coq to unfold f and then use H ?

Upvotes: 7

Views: 2399

Answers (3)

user3284833
user3284833

Reputation:

Here is another solution, but of course for this trivial example. Perhaps will give you some idea. Lemma test2 : forall (x : nat), g x = O -> f x = O.
Proof.
=>intros;
pattern x;
unfold g in H;
rewrite H;
trivial.
Qed.

Upvotes: 0

Olivier Verdier
Olivier Verdier

Reputation: 49146

I'm not sure if this would solve the general problem, but in your particular case (since g is so simple), this works:

Lemma test : forall (x : nat), g x = O -> f x = O.
Proof.
  unfold g.
  intros ? H. rewrite H. reflexivity.
Qed.

Upvotes: 1

jbapple
jbapple

Reputation: 3375

Parameter g: nat -> nat.

(* You could restructure f in one of two ways: *)

(* 1. Use a helper then prove an unrolling lemma: *)

Definition fhelp fhat (x:nat) := 
  match g x with
    | O => O
    | S y => match x with 
      | O => S O
      | S z => fhat z
      end 
  end.

Fixpoint f (x:nat) := fhelp f x.

Lemma funroll : forall x, f x = fhelp f x.
destruct x; simpl; reflexivity.
Qed.

Lemma test : forall (x : nat), g x = O -> f x = O.
Proof.
  intros.
  rewrite funroll.
  unfold fhelp.
  rewrite H.
  reflexivity.
Qed.

(* 2. Use Coq's "Function": *)

Function f2 (x:nat) := 
  match g x with
    | O => O
    | S y => match x with 
      | O => S O
      | S z => f2 z
      end 
  end.

Check f2_equation.

Lemma test2 : forall (x : nat), g x = O -> f2 x = O.
Proof.
  intros.
  rewrite f2_equation.
  rewrite H.
  reflexivity.
Qed.

Upvotes: 5

Related Questions