Peano.v 8.56 KB
 Pierre Letouzey committed Jul 10, 2019 1 `````` `````` Pierre Letouzey committed Aug 09, 2019 2 3 4 5 6 7 ``````(** * The Theory of Peano Arithmetic and its Coq model *) (** The NatDed development, Pierre Letouzey, 2019. This file is released under the CC0 License, see the LICENSE file *) Require Import Defs NameProofs Mix Meta Theories PreModels Models. `````` Pierre Letouzey committed Jul 10, 2019 8 9 10 11 ``````Import ListNotations. Local Open Scope bool_scope. Local Open Scope eqb_scope. `````` Pierre Letouzey committed Aug 09, 2019 12 13 14 15 ``````(** The Peano axioms *) Definition PeanoSign := Finite.to_infinite peano_sign. `````` Pierre Letouzey committed Jul 10, 2019 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ``````Definition Zero := Fun "O" []. Definition Succ x := Fun "S" [x]. Notation "x = y" := (Pred "=" [x;y]) : formula_scope. Notation "x + y" := (Fun "+" [x;y]) : formula_scope. Notation "x * y" := (Fun "*" [x;y]) : formula_scope. Module PeanoAx. Local Open Scope formula_scope. Definition ax1 := ∀ (#0 = #0). Definition ax2 := ∀∀ (#1 = #0 -> #0 = #1). Definition ax3 := ∀∀∀ (#2 = #1 /\ #1 = #0 -> #2 = #0). Definition ax4 := ∀∀ (#1 = #0 -> Succ (#1) = Succ (#0)). Definition ax5 := ∀∀∀ (#2 = #1 -> #2 + #0 = #1 + #0). Definition ax6 := ∀∀∀ (#1 = #0 -> #2 + #1 = #2 + #0). Definition ax7 := ∀∀∀ (#2 = #1 -> #2 * #0 = #1 * #0). Definition ax8 := ∀∀∀ (#1 = #0 -> #2 * #1 = #2 * #0). Definition ax9 := ∀ (Zero + #0 = #0). Definition ax10 := ∀∀ (Succ(#1) + #0 = Succ(#1 + #0)). `````` Pierre Letouzey committed Aug 09, 2019 38 ``````Definition ax11 := ∀ (Zero * #0 = Zero). `````` Pierre Letouzey committed Jul 10, 2019 39 40 41 42 43 44 ``````Definition ax12 := ∀∀ (Succ(#1) * #0 = (#1 * #0) + #0). Definition ax13 := ∀∀ (Succ(#1) = Succ(#0) -> #1 = #0). Definition ax14 := ∀ ~(Succ(#0) = Zero). Definition axioms_list := `````` 45 `````` [ ax1; ax2; ax3; ax4; ax5; ax6; ax7; ax8; `````` Pierre Letouzey committed Jul 10, 2019 46 47 `````` ax9; ax10; ax11; ax12; ax13; ax14 ]. `````` Pierre Letouzey committed Aug 09, 2019 48 49 50 ``````(** Beware, [bsubst] is ok below for turning [#0] into [Succ #0], but only since it contains now a [lift] of substituted terms inside quantifiers. `````` Pierre Letouzey committed Jul 10, 2019 51 52 53 `````` And the unconventional [∀] before [A[0]] is to get the right bounded vars (Hack !). *) `````` Pierre Letouzey committed Aug 09, 2019 54 55 56 ``````Definition induction_schema A_x := let A_0 := bsubst 0 Zero A_x in let A_Sx := bsubst 0 (Succ(#0)) A_x in `````` Pierre Letouzey committed Jul 10, 2019 57 `````` nForall `````` Pierre Letouzey committed Aug 09, 2019 58 59 `````` (Nat.pred (level A_x)) (((∀ A_0) /\ (∀ (A_x -> A_Sx))) -> ∀ A_x). `````` Pierre Letouzey committed Jul 10, 2019 60 61 62 63 64 65 `````` Local Close Scope formula_scope. Definition IsAx A := List.In A axioms_list \/ exists B, A = induction_schema B /\ `````` Pierre Letouzey committed Aug 09, 2019 66 `````` check PeanoSign B = true /\ `````` Pierre Letouzey committed Jul 10, 2019 67 68 `````` FClosed B. `````` Pierre Letouzey committed Aug 09, 2019 69 ``````Lemma WfAx A : IsAx A -> Wf PeanoSign A. `````` Pierre Letouzey committed Jul 10, 2019 70 71 72 73 74 75 76 ``````Proof. intros [ IN | (B & -> & HB & HB')]. - apply Wf_iff. unfold axioms_list in IN. simpl in IN. intuition; subst; reflexivity. - repeat split; unfold induction_schema; cbn. + rewrite nForall_check. cbn. `````` Pierre Letouzey committed Aug 09, 2019 77 `````` rewrite !check_bsubst, HB; auto. `````` Pierre Letouzey committed Jul 10, 2019 78 79 80 `````` + red. rewrite nForall_level. cbn. assert (level (bsubst 0 Zero B) <= level B). { apply level_bsubst'. auto. } `````` Pierre Letouzey committed Aug 09, 2019 81 82 `````` assert (level (bsubst 0 (Succ(BVar 0)) B) <= level B). { apply level_bsubst'. auto. } `````` Pierre Letouzey committed Jul 10, 2019 83 84 85 86 87 `````` omega with *. + apply nForall_fclosed. red. cbn. assert (FClosed (bsubst 0 Zero B)). { red. rewrite bsubst_fvars. intro x. rewrite Names.union_spec. cbn. red in HB'. intuition. } `````` Pierre Letouzey committed Aug 09, 2019 88 89 90 91 92 `````` assert (FClosed (bsubst 0 (Succ(BVar 0)) B)). { red. rewrite bsubst_fvars. intro x. rewrite Names.union_spec. cbn - [Names.union]. rewrite Names.union_spec. generalize (HB' x) (@Names.empty_spec x). intuition. } `````` Pierre Letouzey committed Jul 10, 2019 93 94 95 96 97 `````` unfold FClosed in *. intuition. Qed. End PeanoAx. `````` Pierre Letouzey committed Aug 09, 2019 98 ``````Local Open Scope string. `````` 99 100 ``````Local Open Scope formula_scope. `````` Pierre Letouzey committed Jul 10, 2019 101 ``````Definition PeanoTheory := `````` Pierre Letouzey committed Aug 09, 2019 102 `````` {| sign := PeanoSign; `````` Pierre Letouzey committed Jul 10, 2019 103 104 105 `````` IsAxiom := PeanoAx.IsAx; WfAxiom := PeanoAx.WfAx |}. `````` Samuel Ben Hamou committed Jun 08, 2020 106 107 ``````(** Some basic proofs in Peano arithmetics. *) `````` 108 109 ``````Import PeanoAx. `````` Samuel Ben Hamou committed Jun 08, 2020 110 111 112 113 114 ``````Lemma ZeroRight : IsTheorem Intuiti PeanoTheory (∀ (#0 = #0 + Zero)). Proof. unfold IsTheorem. split. + unfold Wf. split; [ auto | split; auto ]. `````` 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 `````` + exists ((PeanoAx.induction_schema (#0 = #0 + Zero))::axioms_list). split. - apply Forall_forall. intros. destruct H. * simpl. unfold IsAx. right. exists (#0 = #0 + Zero). split; [ auto | split ; [ auto | auto ] ]. * simpl. unfold IsAx. left. exact H. - apply R_Imp_e with (A := (nForall (Nat.pred (level (# 0 = # 0 + Zero))) ((∀ bsubst 0 Zero (# 0 = # 0 + Zero)) /\ (∀ # 0 = # 0 + Zero -> bsubst 0 (Succ (# 0)) (# 0 = # 0 + Zero))))). * apply R_Ax. unfold induction_schema. apply in_eq. * simpl. apply R_And_i. cbn. change (Fun "O" []) with Zero. apply R_All_i with (x := "x"). ++ compute. inversion 1. (* ATROCE *) ++ cbn. change (Fun "O" []) with Zero. eapply R_Imp_e. set (hyp := (_ -> _)%form). assert ( sym : Pr Intuiti (hyp::axioms_list ⊢ ∀∀ (#1 = #0 -> #0 = #1))). { apply R_Ax. compute; intuition. } apply R_All_e with (t := Zero + Zero) in sym. cbn in sym. apply R_All_e with (t := Zero) in sym. cbn in sym. exact sym. -- reflexivity. -- reflexivity. -- set (hyp := (_ -> _)%form). change (Fun "O" []) with Zero. change (Zero + Zero = Zero) with (bsubst 0 Zero (Zero + #0 = #0)). apply R_All_e. reflexivity. apply R_Ax. compute; intuition. `````` Samuel Ben Hamou committed Jun 10, 2020 133 `````` ++ cbn. change (Fun "O" []) with Zero. apply R_All_i with (x := "y"). `````` 134 135 `````` -- compute. inversion 1. -- cbn. change (Fun "O" []) with Zero. apply R_Imp_i. set (H1 := FVar _ = _). set (H2 := _ -> _). `````` Samuel Ben Hamou committed Jun 10, 2020 136 137 `````` assert (hyp : Pr Intuiti (H1 :: H2 :: axioms_list ⊢ Fun "S" [FVar "y"] = Fun "S" [FVar "y" + Zero] /\ Fun "S" [FVar "y" + Zero] = Fun "S" [FVar "y"] + Zero)). { apply R_And_i. `````` Samuel Ben Hamou committed Jun 10, 2020 138 139 140 141 `````` + assert (AX4 : Pr Intuiti (H1 :: H2 :: axioms_list ⊢ ax4)). { apply R_Ax. compute; intuition. } apply R_Imp_e with (A := (FVar "y" = FVar "y" + Zero)%form); [ | apply R_Ax; compute; intuition ]. unfold ax4 in AX4. apply R_All_e with (t := FVar "y") in AX4; [ | auto ]. apply R_All_e with (t := FVar "y" + Zero) in AX4; [ | auto ]. cbn in AX4. exact AX4. + apply R_Imp_e with (A := Fun "S" [FVar "y"] + Zero = Fun "S" [FVar "y" + Zero]). - assert (AX2 : Pr Intuiti (H1 :: H2 :: axioms_list ⊢ ax2)). { apply R_Ax. compute; intuition. } unfold ax2 in AX2. apply R_All_e with (t := Fun "S" [FVar "y"] + Zero) in AX2; [ | auto ]. apply R_All_e with (t := Fun "S" [FVar "y" + Zero]) in AX2; [ | auto ]. cbn in AX2. exact AX2. - assert (AX10 : Pr Intuiti (H1 :: H2 :: axioms_list ⊢ ax10)). { apply R_Ax. compute; intuition. } unfold ax10 in AX10. apply R_All_e with (t:= FVar "y") in AX10; [ | auto ]. apply R_All_e with (t := Zero) in AX10; [ | auto ]. cbn in AX10. exact AX10. } `````` Samuel Ben Hamou committed Jun 10, 2020 142 `````` apply R_Imp_e with (A := Fun "S" [FVar "y"] = Fun "S" [FVar "y" + Zero] /\ Fun "S" [FVar "y" + Zero] = Fun "S" [FVar "y"] + Zero). `````` Samuel Ben Hamou committed Jun 10, 2020 143 144 145 `````` ** assert (AX3 : Pr Intuiti (H1 :: H2 :: axioms_list ⊢ ax3)). { apply R_Ax. compute; intuition. } unfold ax3 in AX3. apply R_All_e with (t:= Fun "S" [FVar "y"]) in AX3; [ | auto ]. apply R_All_e with (t := Fun "S" [FVar "y" + Zero]) in AX3; [ | auto ]. apply R_All_e with (t := Fun "S" [FVar "y"] + Zero) in AX3; [ | auto ]. cbn in AX3. exact AX3. ** exact hyp. Qed. `````` Samuel Ben Hamou committed Jun 08, 2020 146 147 148 149 150 `````` Lemma Comm : IsTheorem Intuiti PeanoTheory (∀∀ (#0 + #1 = #1 + #0)). Proof. Admitted. `````` Pierre Letouzey committed Aug 09, 2019 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 ``````(** A Coq model of this Peano theory, based on the [nat] type *) Definition PeanoFuns : modfuns nat := fun f => if f =? "O" then Some (existT _ 0 0) else if f =? "S" then Some (existT _ 1 S) else if f =? "+" then Some (existT _ 2 Nat.add) else if f =? "*" then Some (existT _ 2 Nat.mul) else None. Definition PeanoPreds : modpreds nat := fun p => if p =? "=" then Some (existT _ 2 (@Logic.eq nat)) else None. Lemma PeanoFuns_ok s : funsymbs PeanoSign s = get_arity (PeanoFuns s). Proof. unfold PeanoSign, peano_sign, PeanoFuns. simpl. unfold eqb, eqb_inst_string. repeat (case string_eqb; auto). Qed. Lemma PeanoPreds_ok s : predsymbs PeanoSign s = get_arity (PeanoPreds s). Proof. unfold PeanoSign, peano_sign, PeanoPreds. simpl. unfold eqb, eqb_inst_string. case string_eqb; auto. Qed. Definition PeanoPreModel : PreModel nat PeanoTheory := {| someone := 0; funs := PeanoFuns; preds := PeanoPreds; funsOk := PeanoFuns_ok; predsOk := PeanoPreds_ok |}. Lemma PeanoAxOk A : IsAxiom PeanoTheory A -> forall genv, interp_form PeanoPreModel genv [] A. Proof. unfold PeanoTheory. simpl. unfold PeanoAx.IsAx. intros [IN|(B & -> & CK & CL)]. - compute in IN. intuition; subst; cbn; intros; subst; omega. - intros genv. unfold PeanoAx.induction_schema. apply interp_nforall. intros stk Len. rewrite app_nil_r. cbn. intros (Base,Step). (* The Peano induction emulated by a Coq induction :-) *) induction m. + specialize (Base 0). apply -> interp_form_bsubst_gen in Base; simpl; eauto. + apply Step in IHm. apply -> interp_form_bsubst_gen in IHm; simpl; eauto. now intros [|k]. Qed. Definition PeanoModel : Model nat PeanoTheory := {| pre := PeanoPreModel; AxOk := PeanoAxOk |}.``````