From c58070a4f5fec3c4cd7fa0390fd03e327d179eed Mon Sep 17 00:00:00 2001 From: Garrett Mills Date: Mon, 20 Mar 2023 08:18:56 +0000 Subject: [PATCH] Add 'Factorial.v' --- Factorial.v | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Factorial.v diff --git a/Factorial.v b/Factorial.v new file mode 100644 index 0000000..0669353 --- /dev/null +++ b/Factorial.v @@ -0,0 +1,57 @@ +(* I renamed this from factorial to factorial_rec. *) +Fixpoint factorial_rec (n: nat): nat := + match n with + | O => 1 + | S n' => n * factorial_rec n' + end. + +Eval compute in (factorial_rec 5). + +Inductive factorial : nat -> nat -> Prop := + | fact_base : factorial O 1 + | fact_step : forall n m, factorial n m -> factorial (S n) ((S n) * m). + +Example factorial_5 : factorial 5 120. +Proof. + apply (fact_step 4 24). + apply (fact_step 3 6). + apply (fact_step 2 2). + apply (fact_step 1 1). + apply (fact_step O 1). (* The step ChatGPT missed. *) + apply fact_base. +Qed. + +Lemma factorial_equiv : forall n, factorial n (factorial_rec n). +Proof. + induction n. + - apply fact_base. + - simpl. + apply (fact_step n (factorial_rec n)). + apply IHn. +Qed. + +(* This generated lemma was incorrect. I rewrote the claim manually and generated a new proof. *) +(* Lemma factorial_rec_equiv : forall n, factorial_rec n = factorial n. *) +(* Proof. *) + (* intros n. *) + (* unfold factorial_rec. *) + (* induction n. *) + (* - reflexivity. *) + (* - simpl. *) + (* rewrite IHn. *) + (* apply (fact_step n (factorial n)). *) + (* apply factorial_equiv. *) +(* Qed. *) + +Lemma factorial_rec_equiv : forall n m, factorial_rec n = m -> factorial n m. +Proof. + intros n m H. + rewrite <- H. + clear H. + induction n. + - apply fact_base. + - simpl. + apply (fact_step n (factorial_rec n)). + apply IHn. + (* reflexivity. *) (* This proved (ha) unnecessary. *) +Qed. \ No newline at end of file