Compute if in a decideable prop in Coq
Question
I want to write a function that calculate count of true values of p 0 .. p t in a nat -> prop function.
Section count_sc. Variable p : nat -> Prop. Hypothesis p_dec : forall x : nat, {p x} + {~ p x}. Fixpoint count (x : nat) := match x with | 0 => if p_dec 0 then 1 else 0 | S y => if p_dec x then 1 + count y else count y end. End count_sc. Definition fret (x : nat) := False. Axiom fret_dec : forall x : nat, { fret x } + { ~ fret x }.forall x : nat, count fret fret_dec x = 0forall x : nat, count fret fret_dec x = 0x: natcount fret fret_dec x = 0count fret fret_dec 0 = 0x: nat
IHx: count fret fret_dec x = 0count fret fret_dec (S x) = 0(if fret_dec 0 then 1 else 0) = 0x: nat
IHx: count fret fret_dec x = 0count fret fret_dec (S x) = 0(if fret_dec 0 then 1 else 0) = 0(if fret_dec 0 then 1 else 0) = 0
Before the replace tactic I should proof some goal like this:
(if fret_dec 0 then 1 else 0) = 0
Coq does not automatically compute the value of the if statement. and if I try to replace the fret_dec with it's value, I will get this error:
How I can write count function that I can unfold and use it in theorems?
Answer
You have declared fret_dec as an axiom. But that means it does not have a definition, or implementation in other words. Thus, Coq cannot compute with it.
You can still prove your theorem like so, using the destruct tactic:
forall x : nat, count fret fret_dec x = 0forall x : nat, count fret fret_dec x = 0count fret fret_dec 0 = 0x: nat
IH: count fret fret_dec x = 0count fret fret_dec (S x) = 0count fret fret_dec 0 = 0(if fret_dec 0 then 1 else 0) = 0contra: fret 01 = 00 = 0contradiction.contra: fret 01 = 0reflexivity.0 = 0x: nat
IH: count fret fret_dec x = 0count fret fret_dec (S x) = 0x: nat
IH: count fret fret_dec x = 0(if fret_dec (S x) then S (count fret fret_dec x) else count fret fret_dec x) = 0x: nat
IH: count fret fret_dec x = 0(if fret_dec (S x) then 1 else 0) = 0x: nat
IH: count fret fret_dec x = 0
contra: fret (S x)1 = 0x: nat
IH: count fret fret_dec x = 00 = 0contradiction.x: nat
IH: count fret fret_dec x = 0
contra: fret (S x)1 = 0reflexivity. Qed.x: nat
IH: count fret fret_dec x = 00 = 0
But, in this case it is really easy to provide such a decision procedure instead of postulating it. And this simplifies the proof a lot:
x: nat{fret x} + {~ fret x}now right. Defined.x: nat{fret x} + {~ fret x}forall x : nat, count fret fret_dec' x = 0forall x : nat, count fret fret_dec' x = 00 = 0x: nat
IH: count fret fret_dec' x = 0count fret fret_dec' x = 0reflexivity.0 = 0exact IH. Qed.x: nat
IH: count fret fret_dec' x = 0count fret fret_dec' x = 0