/
P2_solution.v
213 lines (192 loc) · 4.54 KB
/
P2_solution.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
Set Implicit Arguments.
Require Import Omega.
Require Import Coq.Arith.Max.
Definition LPO := forall f : nat -> bool, (exists x, f x = true) \/ (forall x, f x = false).
Fixpoint true_below(f : nat -> bool)(x : nat) : bool :=
match x with
|0 => f 0
|S y => f x || true_below f y
end.
Lemma true_below_correct1 : forall f x, true_below f x = true -> exists y, f y = true.
Proof.
intros.
induction x.
exists 0.
exact H.
simpl in H.
destruct (f (S x)) eqn:G.
exists (S x); exact G.
apply IHx.
exact H.
Qed.
Lemma f_true_below : forall f x, f x = true -> true_below f x = true.
Proof.
intros.
destruct x.
exact H.
simpl.
rewrite H; reflexivity.
Qed.
Lemma true_below_correct2 : forall f x y, true_below f x = true -> x <= y -> true_below f y = true.
Proof.
intros f x.
induction x.
intros.
induction y.
exact H.
simpl.
rewrite IHy.
destruct (f (S y)); reflexivity.
omega.
induction y.
intros.
inversion H0.
intros.
inversion H0.
rewrite <- H2; exact H.
simpl.
rewrite IHy.
destruct (f (S y)); reflexivity.
exact H.
exact H2.
Qed.
Definition to_nat(f : nat -> bool) : nat -> nat :=
fun x => if f x then 0 else 1.
Definition decr(f : nat -> nat) := forall n, f (S n) <= f n.
Lemma decr_shift : forall y f, decr f -> decr (fun x => f (x + y)).
Proof.
induction y; simpl; intros; intro x.
apply H.
simpl.
apply H.
Qed.
Lemma decr_antitone : forall f, decr f -> forall x y, x <= y -> f y <= f x.
Proof.
intros.
induction y.
inversion H0.
omega.
inversion H0.
omega.
pose (IHy H2).
pose (H y).
omega.
Qed.
Lemma decr_0 : forall f, decr f -> f 0 = 0 -> forall x, f x = 0.
Proof.
intros.
induction x.
exact H0.
pose (G := H x).
omega.
Qed.
Lemma to_nat_decr : forall f, decr (to_nat (true_below f)).
Proof.
intros f x.
unfold to_nat.
simpl.
destruct (f (S x)); simpl; omega.
Qed.
Definition infvalley(f : nat -> nat)(x : nat) := forall y, x <= y -> f y = f x.
Theorem infvalley_LPO : (forall f, decr f -> exists x, infvalley f x) -> LPO.
Proof.
intros H f.
destruct (H _ (to_nat_decr f)).
unfold to_nat,infvalley in H0.
destruct (true_below f x) eqn:G.
left.
exact (true_below_correct1 _ _ G).
right.
intro y.
destruct (f y) eqn:fy.
destruct (true_below f (max x y)) eqn:G1.
pose (G2 := H0 _(le_max_l x y)).
rewrite G1 in G2.
discriminate G2.
pose (G2 := f_true_below _ _ fy).
rewrite (true_below_correct2 _ G2 (le_max_r x y)) in G1.
exact G1.
reflexivity.
Qed.
Definition Slt(f : nat -> nat) : nat -> bool :=
fun x => match lt_dec (f (S x)) (f x) with
|left _ => true
|right _ => false
end.
(*to avoid creating ill-typed terms*)
Lemma obvious : forall f x, f (S x) < f x -> Slt f x = true.
Proof.
intros.
unfold Slt.
destruct (lt_dec (f (S x)) (f x)).
reflexivity.
contradiction.
Qed.
Lemma Slt_correct1 : forall f, decr f -> (forall x, Slt f x = false) -> forall x, f x = f 0.
Proof.
intros.
induction x.
reflexivity.
destruct (lt_dec (f (S x)) (f x)).
pose (G := H0 x).
rewrite (obvious _ _ l) in G.
discriminate G.
pose (G := H x).
omega.
Qed.
Lemma Slt_correct2 : forall f, decr f -> forall y, Slt f y = true -> f (S y) < f 0.
Proof.
intros.
unfold Slt in H0.
destruct (lt_dec (f (S y)) (f y)).
assert (0 <= y).
omega.
pose (decr_antitone H H1).
omega.
discriminate H0.
Qed.
Lemma arith_lemma : forall x y z, x + y <= z -> exists u, z = u + y.
Proof.
intros.
exists (z - y).
omega.
Qed.
Lemma infvalley_shift : forall s f x, infvalley (fun x => f (x + s)) x -> infvalley f (x + s).
Proof.
intros s f x v y Hy.
destruct (arith_lemma _ _ Hy) as [y' Hy'].
rewrite Hy'.
apply v.
omega.
Qed.
Lemma infvalley_aux : LPO -> forall n f, f 0 <= n -> decr f -> exists x, infvalley f x.
Proof.
intros H n; induction n.
intros.
assert (f 0 = 0).
omega.
exists 0.
intros x _.
rewrite H2.
rewrite (decr_0 H1 H2).
reflexivity.
intros.
destruct (H (Slt f)).
destruct H2.
pose (Slt_correct2 H1 _ H2).
assert (f (S x) <= n).
omega.
destruct (IHn _ H3 (decr_shift _ H1)).
exists (x0 + S x).
apply infvalley_shift.
exact H4.
exists 0.
pose (Slt_correct1 H1 H2).
intros y _.
apply e.
Qed.
Theorem LPO_infvalley : LPO -> forall f, decr f -> exists x, infvalley f x.
Proof.
intros H f fd.
exact (infvalley_aux H (le_refl (f 0)) fd).
Qed.