Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one inspect what more complicated tactics do in Coq step-by-step?

Tags:

coq

coq-tactic

I was trying to go through the famous and wonderful software foundations book but I got to an example where simpl. and reflexivity. just do to much under the covers and are hindering my learning & understanding.

I was going through the following theorem:

Theorem plus_1_neq_0 : forall n : nat,
  beq_nat (n + 1) 0 = false. (* n+1 != 0 *)
Proof.
  intros n.
  destruct n as [| n'].
    -simpl. reflexivity.
    -simpl. reflexivity.
Qed.

what I really want is something that allows me to go through step by step what simpl. and reflexivity. did. Is there something that allows me to do that?


Destruct is suppose to resolve the following issue:

because the first argument to beq_nat (which is just not equal i.e. !=) does a matching but the first input depends on a unknown variable n and the same thing for + so the matching can't do anything, so doing simpl. gets us stuck (for some reason).

which it clearly it must resolve it since Coq later accepts the proof. But if one looks carefully what the second goal is, it seems that the same issue as above is re-introduced:

2 subgoals
______________________________________(1/2)
beq_nat (0 + 1) 0 = false
______________________________________(2/2)
beq_nat (S n' + 1) 0 = false

now we have n' as the first argument for both beq_nat and + again. However, to a novice like me, simpl. miraculously does work this time for some mysterious reason. I obviously read the simpl. documentation but being a novice in this I didn't really know what I was looking for and it was to dense for me to form an understanding of it that was helpful...

Anyway, why does it work here? The reason I am asking is because it would have never occurred to me to use destruct on this example proof, especially cuz of the re ocurrence of n' an unknown variable, and it seems that being able to see what really happened or what was different would be useful. So I thought checking a step-by-step break down of these type of things would be useful (rather than posting a new SO question every other day).


Note I did see this question:

Step by step simplification in coq?

but I couldn't find a way to make it useful for me since it was tailored for that particular example to much. Hopefully my question doesn't become to narrow to my particular example, though it might since the step by step break down might not be possible without knowing how simpl. (or reflexivity.) works already (or at least the above answers to the question above gave me that impression).

like image 296
Charlie Parker Avatar asked Jan 05 '19 00:01

Charlie Parker


People also ask

What are tactics in Coq?

Tactics specify how to transform the proof state of an incomplete proof to eventually generate a complete proof. Proofs can be developed in two basic ways: In forward reasoning, the proof begins by proving simple statements that are then combined to prove the theorem statement as the last step of the proof.

What does Simpl do in Coq?

simpl: Simplifies the goal or hypotheses in the context. unfold: Unfolds the definitions of terms. apply: Uses implications to transform goals and hypotheses. rewrite: Replaces a term with an equivalent term if the equivalence of the terms has already been proven.

What does rewrite do in Coq?

rewrite. If we know two terms are equal we can transform one term into the other using rewrite . While rewrite is similar to subst , it also works when both sides of the equality are terms.

What is a proof tactic?

In this chapter, we describe an alternative approach to constructing proofs, using tactics. A proof term is a representation of a mathematical proof; tactics are commands, or instructions, that describe how to build such a proof.


2 Answers

One way to break the evaluation down is to give an argument to simpl, as suggested in the question you linked. simpl f allows to simplify only the sub-expressions that appear under calls to f. In this case, simpl Nat.add (or simpl plus or simpl "+") simplifies S n' + 1 into S (n' + 1). Then simpl beq_nat turns beq_nat (S (n' + 1)) 0 into false.

As for reflexivity, it can conclude if the two terms under comparison are equal up to simplification, which means that, if I am not mistaken, you can always replace simpl; reflexivity by just reflexivity.

like image 88
eponier Avatar answered Oct 07 '22 23:10

eponier


Reducing this step by step:

beq_nat (S n' + 1) 0 = false

  (* Without the `+` notation, which is purely for pretty-printing: *)

beq_nat (plus (S n') 1) 0 = false

  (* by definition of plus:   plus (S n') 1 = S (plus n' 1) *)

beq_nat (S (plus n' 1)) 0 = false

  (* by definition of `beq_nat`,

     beq_nat (S (plus n' 1)) 0 =
     = match S (plus n' 1) with
       | O => ... (* unreachable *)
       | S m => match 0 with
                | O => false
                | S _ => ...
                end
       end
     = match 0 with
       | O => false
       | S _ => ...
       end
     = false
  *)

false = false
like image 2
Li-yao Xia Avatar answered Oct 08 '22 00:10

Li-yao Xia