2 include "arithmetics/nat.ma".
3 include "arithmetics/log.ma".
4 include "arithmetics/bigops.ma".
5 include "arithmetics/bounded_quantifiers.ma".
6 include "arithmetics/pidgeon_hole.ma".
7 include "basics/sets.ma".
8 include "basics/types.ma".
9 include "basics/core_notation/card_1.ma".
11 (************************************ MAX *************************************)
12 notation "Max_{ ident i < n | p } f"
14 for @{'bigop $n max 0 (λ${ident i}. $p) (λ${ident i}. $f)}.
16 notation "Max_{ ident i < n } f"
18 for @{'bigop $n max 0 (λ${ident i}.true) (λ${ident i}. $f)}.
20 notation "Max_{ ident j ∈ [a,b[ } f"
22 for @{'bigop ($b-$a) max 0 (λ${ident j}.((λ${ident j}.true) (${ident j}+$a)))
23 (λ${ident j}.((λ${ident j}.$f)(${ident j}+$a)))}.
25 notation "Max_{ ident j ∈ [a,b[ | p } f"
27 for @{'bigop ($b-$a) max 0 (λ${ident j}.((λ${ident j}.$p) (${ident j}+$a)))
28 (λ${ident j}.((λ${ident j}.$f)(${ident j}+$a)))}.
30 lemma Max_assoc: ∀a,b,c. max (max a b) c = max a (max b c).
31 #a #b #c normalize cases (true_or_false (leb a b)) #leab >leab normalize
32 [cases (true_or_false (leb b c )) #lebc >lebc normalize
33 [>(le_to_leb_true a c) // @(transitive_le ? b) @leb_true_to_le //
36 |cases (true_or_false (leb b c )) #lebc >lebc normalize //
37 >leab normalize >(not_le_to_leb_false a c) // @lt_to_not_le
38 @(transitive_lt ? b) @not_le_to_lt @leb_false_to_not_le //
42 lemma Max0 : ∀n. max 0 n = n.
45 lemma Max0r : ∀n. max n 0 = n.
46 #n >commutative_max //
49 alias id "max" = "cic:/matita/arithmetics/nat/max#def:2".
50 alias id "mk_Aop" = "cic:/matita/arithmetics/bigops/Aop#con:0:1:2".
52 mk_Aop nat 0 max Max0 Max0r (λa,b,c.sym_eq … (Max_assoc a b c)).
54 definition MaxAC ≝ mk_ACop nat 0 MaxA commutative_max.
56 lemma le_Max: ∀f,p,n,a. a < n → p a = true →
57 f a ≤ Max_{i < n | p i}(f i).
59 >(bigop_diff p ? 0 MaxAC f a n) // @(le_maxl … (le_n ?))
62 lemma Max_le: ∀f,p,n,b.
63 (∀a.a < n → p a = true → f a ≤ b) → Max_{i < n | p i}(f i) ≤ b.
64 #f #p #n elim n #b #H //
65 #b0 #H1 cases (true_or_false (p b)) #Hb
66 [>bigop_Strue [2:@Hb] @to_max [@H1 // | @H #a #ltab #pa @H1 // @le_S //]
67 |>bigop_Sfalse [2:@Hb] @H #a #ltab #pa @H1 // @le_S //
71 (******************************** big O notation ******************************)
73 (* O f g means g ∈ O(f) *)
74 definition O: relation (nat→nat) ≝
75 λf,g. ∃c.∃n0.∀n. n0 ≤ n → g n ≤ c* (f n).
77 lemma O_refl: ∀s. O s s.
78 #s %{1} %{0} #n #_ >commutative_times <times_n_1 @le_n qed.
80 lemma O_trans: ∀s1,s2,s3. O s2 s1 → O s3 s2 → O s3 s1.
81 #s1 #s2 #s3 * #c1 * #n1 #H1 * #c2 * # n2 #H2 %{(c1*c2)}
82 %{(max n1 n2)} #n #Hmax
83 @(transitive_le … (H1 ??)) [@(le_maxl … Hmax)]
84 >associative_times @le_times [//|@H2 @(le_maxr … Hmax)]
87 lemma sub_O_to_O: ∀s1,s2. O s1 ⊆ O s2 → O s2 s1.
90 lemma O_to_sub_O: ∀s1,s2. O s2 s1 → O s1 ⊆ O s2.
91 #s1 #s2 #H #g #Hg @(O_trans … H) // qed.
93 definition sum_f ≝ λf,g:nat→nat.λn.f n + g n.
94 interpretation "function sum" 'plus f g = (sum_f f g).
96 lemma O_plus: ∀f,g,s. O s f → O s g → O s (f+g).
97 #f #g #s * #cf * #nf #Hf * #cg * #ng #Hg
98 %{(cf+cg)} %{(max nf ng)} #n #Hmax normalize
99 >distributive_times_plus_r @le_plus
100 [@Hf @(le_maxl … Hmax) |@Hg @(le_maxr … Hmax) ]
103 lemma O_plus_l: ∀f,s1,s2. O s1 f → O (s1+s2) f.
104 #f #s1 #s2 * #c * #a #Os1f %{c} %{a} #n #lean
105 @(transitive_le … (Os1f n lean)) @le_times //
108 lemma O_plus_r: ∀f,s1,s2. O s2 f → O (s1+s2) f.
109 #f #s1 #s2 * #c * #a #Os1f %{c} %{a} #n #lean
110 @(transitive_le … (Os1f n lean)) @le_times //
113 lemma O_absorbl: ∀f,g,s. O s f → O f g → O s (g+f).
114 #f #g #s #Osf #Ofg @(O_plus … Osf) @(O_trans … Osf) //
117 lemma O_absorbr: ∀f,g,s. O s f → O f g → O s (f+g).
118 #f #g #s #Osf #Ofg @(O_plus … Osf) @(O_trans … Osf) //
121 lemma O_ext2: ∀f,g,s. O s f → (∀x.f x = g x) → O s g.
122 #f #g #s * #c * #a #Osf #eqfg %{c} %{a} #n #lean <eqfg @Osf //
125 definition not_O ≝ λf,g.∀c,n0.∃n. n0 ≤ n ∧ c* (f n) < g n .
127 (******************************* small O notation *****************************)
129 (* o f g means g ∈ o(f) *)
130 definition o: relation (nat→nat) ≝
131 λf,g.∀c.∃n0.∀n. n0 ≤ n → c * (g n) < f n.
133 lemma o_irrefl: ∀s. ¬ o s s.
134 #s % #oss cases (oss 1) #n0 #H @(absurd ? (le_n (s n0)))
135 @lt_to_not_le >(times_n_1 (s n0)) in ⊢ (?%?); >commutative_times @H //
138 lemma o_trans: ∀s1,s2,s3. o s2 s1 → o s3 s2 → o s3 s1.
139 #s1 #s2 #s3 #H1 #H2 #c cases (H1 c) #n1 -H1 #H1 cases (H2 1) #n2 -H2 #H2
140 %{(max n1 n2)} #n #Hmax
141 @(transitive_lt … (H1 ??)) [@(le_maxl … Hmax)]
142 >(times_n_1 (s2 n)) in ⊢ (?%?); >commutative_times @H2 @(le_maxr … Hmax)
146 (*********************************** pairing **********************************)
148 axiom pair: nat →nat →nat.
149 axiom fst : nat → nat.
150 axiom snd : nat → nat.
151 axiom fst_pair: ∀a,b. fst (pair a b) = a.
152 axiom snd_pair: ∀a,b. snd (pair a b) = b.
154 interpretation "abstract pair" 'pair f g = (pair f g).
156 (************************ basic complexity notions ****************************)
158 axiom U: nat → nat → nat → option nat.
160 axiom monotonic_U: ∀i,x,n,m,y.n ≤m →
161 U i x n = Some ? y → U i x m = Some ? y.
163 lemma unique_U: ∀i,x,n,m,yn,ym.
164 U i x n = Some ? yn → U i x m = Some ? ym → yn = ym.
165 #i #x #n #m #yn #ym #Hn #Hm cases (decidable_le n m)
166 [#lenm lapply (monotonic_U … lenm Hn) >Hm #HS destruct (HS) //
167 |#ltmn lapply (monotonic_U … n … Hm) [@lt_to_le @not_le_to_lt //]
168 >Hn #HS destruct (HS) //
172 definition code_for ≝ λf,i.∀x.
173 ∃n.∀m. n ≤ m → U i x m = f x.
175 definition terminate ≝ λi,x,r. ∃y. U i x r = Some ? y.
176 notation "{i ⊙ x} ↓ r" with precedence 60 for @{terminate $i $x $r}.
178 definition lang ≝ λi,x.∃r,y. U i x r = Some ? y ∧ 0 < y.
180 lemma lang_cf :∀f,i,x. code_for f i →
181 lang i x ↔ ∃y.f x = Some ? y ∧ 0 < y.
182 #f #i #x normalize #H %
183 [* #n * #y * #H1 #posy %{y} % //
184 cases (H x) -H #m #H <(H (max n m)) [2:@(le_maxr … n) //]
185 @(monotonic_U … H1) @(le_maxl … m) //
186 |cases (H x) -H #m #Hm * #y #Hy %{m} %{y} >Hm //
190 (******************************* complexity classes ***************************)
192 axiom size: nat → nat.
193 axiom of_size: nat → nat.
195 interpretation "size" 'card n = (size n).
197 axiom size_of_size: ∀n. |of_size n| = n.
198 axiom monotonic_size: monotonic ? le size.
200 axiom of_size_max: ∀i,n. |i| = n → i ≤ of_size n.
202 axiom size_fst : ∀n. |fst n| ≤ |n|.
204 definition size_f ≝ λf,n.Max_{i < S (of_size n) | eqb (|i|) n}|(f i)|.
206 lemma size_f_def: ∀f,n. size_f f n =
207 Max_{i < S (of_size n) | eqb (|i|) n}|(f i)|.
210 lemma size_f_size : ∀f,n. size_f (f ∘ size) n = |(f n)|.
211 #f #n @le_to_le_to_eq
212 [@Max_le #a #lta #Ha normalize >(eqb_true_to_eq … Ha) //
213 |<(size_of_size n) in ⊢ (?%?); >size_f_def
214 @(le_Max (λi.|f (|i|)|) ? (S (of_size n)) (of_size n) ??)
215 [@le_S_S // | @eq_to_eqb_true //]
219 lemma size_f_id : ∀n. size_f (λx.x) n = n.
221 [@Max_le #a #lta #Ha >(eqb_true_to_eq … Ha) //
222 |<(size_of_size n) in ⊢ (?%?); >size_f_def
223 @(le_Max (λi.|i|) ? (S (of_size n)) (of_size n) ??)
224 [@le_S_S // | @eq_to_eqb_true //]
228 lemma size_f_fst : ∀n. size_f fst n ≤ n.
229 #n @Max_le #a #lta #Ha <(eqb_true_to_eq … Ha) //
232 (* C s i means that the complexity of i is O(s) *)
234 definition C ≝ λs,i.∃c.∃a.∀x.a ≤ |x| → {i ⊙ x} ↓ (c*(s(|x|))).
236 definition CF ≝ λs,f.∃i.code_for f i ∧ C s i.
238 lemma ext_CF : ∀f,g,s. (∀n. f n = g n) → CF s f → CF s g.
239 #f #g #s #Hext * #i * #Hcode #HC %{i} %
240 [#x cases (Hcode x) #a #H %{a} <Hext @H | //]
243 lemma monotonic_CF: ∀s1,s2,f. O s2 s1 → CF s1 f → CF s2 f.
244 #s1 #s2 #f * #c1 * #a #H * #i * #Hcodef #HCs1 %{i} % //
245 cases HCs1 #c2 * #b #H2 %{(c2*c1)} %{(max a b)}
246 #x #Hmax cases (H2 x ?) [2:@(le_maxr … Hmax)] #y #Hy
247 %{y} @(monotonic_U …Hy) >associative_times @le_times // @H @(le_maxl … Hmax)
250 (*********************** The hierachy theorem (left) **************************)
252 theorem hierarchy_theorem_left: ∀s1,s2:nat→nat.
253 O(s1) ⊆ O(s2) → CF s1 ⊆ CF s2.
254 #s1 #s2 #HO #f * #i * #Hcode * #c * #a #Hs1_i %{i} % //
255 cases (sub_O_to_O … HO) -HO #c1 * #b #Hs1s2
256 %{(c*c1)} %{(max a b)} #x #lemax
257 cases (Hs1_i x ?) [2: @(le_maxl …lemax)]
258 #y #Hy %{y} @(monotonic_U … Hy) >associative_times
259 @le_times // @Hs1s2 @(le_maxr … lemax)
262 (************************** The diagonal language *****************************)
264 (* the diagonal language used for the hierarchy theorem *)
266 definition diag ≝ λs,i.
267 U (fst i) i (s (|i|)) = Some ? 0.
269 lemma equiv_diag: ∀s,i.
270 diag s i ↔ {fst i ⊙ i} ↓ s(|i|) ∧ ¬lang (fst i) i.
272 [whd in ⊢ (%→?); #H % [%{0} //] % * #x * #y *
273 #H1 #Hy cut (0 = y) [@(unique_U … H H1)] #eqy /2/
275 #y0 #H * #H1 @False_ind @H1 -H1 whd %{(s (|i|))} %{(S y0)} % //
279 (* Let us define the characteristic function diag_cf for diag, and prove
282 definition diag_cf ≝ λs,i.
283 match U (fst i) i (s (|i|)) with
285 | Some y ⇒ if (eqb y 0) then (Some ? 1) else (Some ? 0)].
287 lemma diag_cf_OK: ∀s,x. diag s x ↔ ∃y.diag_cf s x = Some ? y ∧ 0 < y.
289 [whd in ⊢ (%→?); #H %{1} % // whd in ⊢ (??%?); >H //
290 |* #y * whd in ⊢ (??%?→?→%);
291 cases (U (fst x) x (s (|x|))) normalize
293 |#x cases (true_or_false (eqb x 0)) #Hx >Hx
294 [>(eqb_true_to_eq … Hx) //
295 |normalize #H destruct #H @False_ind @(absurd ? H) @lt_to_not_le //
301 lemma diag_spec: ∀s,i. code_for (diag_cf s) i → ∀x. lang i x ↔ diag s x.
302 #s #i #Hcode #x @(iff_trans … (lang_cf … Hcode)) @iff_sym @diag_cf_OK
305 (******************************************************************************)
307 lemma absurd1: ∀P. iff P (¬ P) →False.
308 #P * #H1 #H2 cut (¬P) [% #H2 @(absurd … H2) @H1 //]
309 #H3 @(absurd ?? H3) @H2 @H3
312 let rec f_img (f:nat →nat) n on n ≝
315 |S m ⇒ f m::f_img f m
318 (* a few lemma to prove injective_to_exists. This is a general result; a nice
319 example of the pidgeon hole pricniple *)
321 lemma f_img_to_exists:
322 ∀f.∀n,a. a ∈ f_img f n → ∃b. b < n ∧ f b = a.
323 #f #n #a elim n normalize [@False_ind]
325 [#Ha %{m} /2/ |#H cases(Hind H) #b * #Hb #Ha %{b} % // @le_S //]
328 lemma length_f_img: ∀f,n. |f_img f n| = n.
329 #f #n elim n // normalize //
332 lemma unique_f_img: ∀f,n. injective … f → unique ? (f_img f n).
333 #f #n #Hinj elim n normalize //
334 #m #Hind % // % #H lapply(f_img_to_exists …H) * #b * #ltbm
335 #eqbm @(absurd … ltbm) @le_to_not_lt >(Hinj… eqbm) //
338 lemma injective_to_exists: ∀f. injective nat nat f →
339 ∀n.(∀i.i < n → f i < n) → ∀a. a < n → ∃b. b<n ∧ f b = a.
340 #f #finj #n #H1 #a #ltan @(f_img_to_exists f n a)
341 @(eq_length_to_mem_all … (length_f_img …) (unique_f_img …finj …) …ltan)
342 #x #Hx cases(f_img_to_exists … Hx) #b * #ltbn #eqx <eqx @H1 //
345 lemma weak_pad1 :∀n,a.∃b. n ≤ 〈a,b〉.
347 cut (∀i.decidable (〈a,i〉 < n))
349 #Hdec cases(decidable_forall (λb. 〈a,b〉 < n) Hdec n)
350 [#H cut (∀i. i < n → ∃b. b < n ∧ 〈a,b〉 = i)
351 [@(injective_to_exists … H) //]
352 #Hcut %{n} @not_lt_to_le % #Han
353 lapply(Hcut ? Han) * #x * #Hx #Hx2
354 cut (x = n) [//] #Hxn >Hxn in Hx; /2 by absurd/
355 |#H lapply(not_forall_to_exists … Hdec H)
356 * #b * #H1 #H2 %{b} @not_lt_to_le @H2
360 lemma pad : ∀n,a. ∃b. n ≤ |〈a,b〉|.
361 #n #a cases (weak_pad1 (of_size n) a) #b #Hb
362 %{b} <(size_of_size n) @monotonic_size //
365 lemma o_to_ex: ∀s1,s2. o s1 s2 → ∀i. C s2 i →
366 ∃b.{i ⊙ 〈i,b〉} ↓ s1 (|〈i,b〉|).
367 #s1 #s2 #H #i * #c * #x0 #H1
368 cases (H c) #n0 #H2 cases (pad (max x0 n0) i) #b #Hmax
369 %{b} cases (H1 〈i,b〉 ?)
370 [#z #H3 %{z} @(monotonic_U … H3) @lt_to_le @H2
376 lemma diag1_not_s1: ∀s1,s2. o s1 s2 → ¬ CF s2 (diag_cf s1).
377 #s1 #s2 #H1 % * #i * #Hcode_i #Hs2_i
378 cases (o_to_ex … H1 ? Hs2_i) #b #H2
379 lapply (diag_spec … Hcode_i) #H3
380 @(absurd1 (lang i 〈i,b〉))
381 @(iff_trans … (H3 〈i,b〉))
382 @(iff_trans … (equiv_diag …)) >fst_pair
383 %[* #_ // |#H6 % // ]
386 (******************************************************************************)
388 definition to_Some ≝ λf.λx:nat. Some nat (f x).
390 definition deopt ≝ λn. match n with
394 definition opt_comp ≝ λf,g:nat → option nat. λx.
399 axiom sU2: nat → nat → nat.
400 axiom sU: nat → nat → nat → nat.
402 axiom CFU_new: ∀h,g,f,s.
403 CF s (to_Some h) → CF s (to_Some g) → CF s (to_Some f) →
404 O s (λx. sU (size_f h x) (size_f g x) (size_f f x)) →
405 CF s (λx.U (h x) (g x) (|f x|)).
407 lemma CFU: ∀h,g,f,s1,s2,s3.
408 CF s1 (to_Some h) → CF s2 (to_Some g) → CF s3 (to_Some f) →
409 CF (λx. s1 x + s2 x + s3 x + sU (size_f h x) (size_f g x) (size_f f x))
410 (λx.U (h x) (g x) (|f x|)).
411 #h #g #f #s1 #s2 #s3 #Hh #Hg #Hf @CFU_new
412 [@(monotonic_CF … Hh) @O_plus_l @O_plus_l @O_plus_l //
413 |@(monotonic_CF … Hg) @O_plus_l @O_plus_l @O_plus_r //
414 |@(monotonic_CF … Hf) @O_plus_l @O_plus_r //
419 axiom monotonic_sU: ∀a1,a2,b1,b2,c1,c2. a1 ≤ a2 → b1 ≤ b2 → c1 ≤c2 →
420 sU a1 b1 c1 ≤ sU a2 b2 c2.
422 axiom superlinear_sU: ∀i,x,r. r ≤ sU i x r.
425 definition sU_space ≝ λi,x,r.i+x+r.
426 definition sU_time ≝ λi,x,r.i+x+(i^2)*r*(log 2 r).
428 definition IF ≝ λb,f,g:nat →option nat. λx.
431 |Some n ⇒ if (eqb n 0) then f x else g x].
433 axiom IF_CF_new: ∀b,f,g,s. CF s b → CF s f → CF s g → CF s (IF b f g).
435 lemma IF_CF: ∀b,f,g,sb,sf,sg. CF sb b → CF sf f → CF sg g →
436 CF (λn. sb n + sf n + sg n) (IF b f g).
437 #b #f #g #sb #sf #sg #Hb #Hf #Hg @IF_CF_new
438 [@(monotonic_CF … Hb) @O_plus_l @O_plus_l //
439 |@(monotonic_CF … Hf) @O_plus_l @O_plus_r //
440 |@(monotonic_CF … Hg) @O_plus_r //
444 lemma diag_cf_def : ∀s.∀i.
446 IF (λi.U (fst i) i (|of_size (s (|i|))|)) (λi.Some ? 1) (λi.Some ? 0) i.
447 #s #i normalize >size_of_size // qed.
450 axiom CF_pair: ∀f,g,s. CF s (λx.Some ? (f x)) → CF s (λx.Some ? (g x)) →
451 CF s (λx.Some ? (pair (f x) (g x))).
453 axiom CF_fst: ∀f,s. CF s (λx.Some ? (f x)) → CF s (λx.Some ? (fst (f x))).
455 definition sufficiently_large ≝ λs. CF s (λn. Some ? n) ∧ ∀c. CF s (λn. Some ? c).
457 definition constructible ≝ λs. CF s (λx.Some ? (of_size (s (|x|)))).
459 lemma diag_s: ∀s. sufficiently_large s → constructible s →
460 CF (λx.sU x x (s x)) (diag_cf s).
461 #s * #Hs_id #Hs_c #Hs_constr
462 cut (O (λx:ℕ.sU x x (s x)) s) [%{1} %{0} #n //]
463 #O_sU_s @ext_CF [2: #n @sym_eq @diag_cf_def | skip]
464 @IF_CF_new [2,3:@(monotonic_CF … (Hs_c ?)) // ]
466 [@CF_fst @(monotonic_CF … Hs_id) //
467 |@(monotonic_CF … Hs_id) //
468 |@(monotonic_CF … Hs_constr) //
469 |%{1} %{0} #n #_ >commutative_times <times_n_1
470 @monotonic_sU // >size_f_size >size_of_size //