Debian Codegraph @

src/Util/ZUtil/Peano.v

raw
1 (** * Basic Peano-arithmetic-like properties of ℤ *)
2 From Coq Require Import Lia.
3 From Coq Require Import BinInt.
4 Require Import Crypto.Util.Tactics.BreakMatch.
5 Require Import Crypto.Util.HProp.
6 Require Import Crypto.Util.Decidable.
7
8 Local Open Scope Z_scope.
9 Module Z.
10 (** We need versions of [succ] and [pred] that reduce to [Pos.succ] and [Pos.pred] *)
11 Definition succ' (z : Z) : Z
12 := match z with
13 | 0 => 1
14 | -1 => 0
15 | Z.pos x => Z.pos (Pos.succ x)
16 | Z.neg x => Z.neg (Pos.pred x)
17 end.
18 Definition pred' (z : Z) : Z
19 := match z with
20 | 0 => -1
21 | 1 => 0
22 | Z.pos x => Z.pos (Pos.pred x)
23 | Z.neg x => Z.neg (Pos.succ x)
24 end.
25 Lemma succ'_succ z : Z.succ' z = Z.succ z.
26 Proof.
27 unfold succ'; break_match; try reflexivity.
28 rewrite <- Pos2Z.inj_succ; reflexivity.
29 Qed.
30 Lemma pred'_pred z : Z.pred' z = Z.pred z.
31 Proof.
32 unfold pred'; break_match; try reflexivity.
33 unfold Z.pred; simpl.
34 rewrite Pos.add_1_r; reflexivity.
35 Qed.
36
37 (** [Z.peano_rect] does not provide the [0 <= z] and [z <= 0] hypotheses in
38 [Psucc] and [Ppred]. Define an alternate version for proofs which require
39 that. *)
40 Section rect_strong.
41 Context (P : Z -> Type)
42 (P0 : P 0)
43 (Psucc : forall z, z = 0 \/ 0 < z -> P z -> P (Z.succ' z))
44 (Ppred : forall z, z = 0 \/ z < 0 -> P z -> P (Z.pred' z)).
45
46 Definition peano_rect_strong z : P z
47 := match z return P z with
48 | 0 => P0
49 | Z.pos x => Pos.peano_rect (fun p => P (Z.pos p)) (Psucc _ (or_introl eq_refl) P0) (fun p => Psucc (Z.pos p) (or_intror (Pos2Z.is_pos _))) x
50 | Z.neg x => Pos.peano_rect (fun p => P (Z.neg p)) (Ppred _ (or_introl eq_refl) P0) (fun p => Ppred (Z.neg p) (or_intror (Pos2Z.neg_is_neg _))) x
51 end.
52
53 Local Ltac peano_rect_t :=
54 repeat first [ progress unfold peano_rect_strong
55 | progress simpl
56 | reflexivity
57 | exfalso; lia
58 | rewrite Pos.peano_rect_succ
59 | progress f_equal; []
60 | match goal with
61 | [ |- ?x = ?y :> (_ < _) ]
62 => apply allpath_hprop
63 end ].
64 Lemma peano_rect_strong_base
65 : peano_rect_strong 0 = P0.
66 Proof. reflexivity. Qed.
67 Lemma peano_rect_strong_succ z (pf : z = 0 \/ 0 < z)
68 : peano_rect_strong (Z.succ' z) = Psucc z pf (peano_rect_strong z).
69 Proof.
70 destruct pf; [ subst | destruct z]; peano_rect_t.
71 Qed.
72 Lemma peano_rect_strong_pred z (pf : z = 0 \/ z < 0)
73 : peano_rect_strong (Z.pred' z) = Ppred z pf (peano_rect_strong z).
74 Proof.
75 destruct pf; [ subst | destruct z]; peano_rect_t.
76 Qed.
77 End rect_strong.
78 Section rect.
79 Context (P : Z -> Type)
80 (P0 : P 0)
81 (Psucc : forall z, P z -> P (Z.succ' z))
82 (Ppred : forall z, P z -> P (Z.pred' z)).
83
84 Definition peano_rect z : P z
85 := peano_rect_strong P P0 (fun z pf => Psucc z) (fun z pf => Ppred z) z.
86
87 Lemma peano_rect_base
88 : peano_rect 0 = P0.
89 Proof. reflexivity. Qed.
90 Lemma peano_rect_succ z
91 : 0 <= z -> peano_rect (Z.succ' z) = Psucc z (peano_rect z).
92 Proof.
93 unfold peano_rect; intro; erewrite peano_rect_strong_succ by lia; reflexivity.
94 Qed.
95 Lemma peano_rect_pred z
96 : z <= 0 -> peano_rect (Z.pred' z) = Ppred z (peano_rect z).
97 Proof.
98 unfold peano_rect; intro; erewrite peano_rect_strong_pred by lia; reflexivity.
99 Qed.
100 End rect.
101 End Z.