2 include "arithmetics/nat.ma".
3 include "arithmetics/log.ma".
4 (* include "arithmetics/ord.ma". *)
5 include "arithmetics/bigops.ma".
6 include "basics/sets.ma".
7 include "basics/types.ma".
9 (************************************ MAX *************************************)
10 notation "Max_{ ident i < n | p } f"
12 for @{'bigop $n max 0 (λ${ident i}. $p) (λ${ident i}. $f)}.
14 notation "Max_{ ident i < n } f"
16 for @{'bigop $n max 0 (λ${ident i}.true) (λ${ident i}. $f)}.
18 notation "Max_{ ident j ∈ [a,b[ } f"
20 for @{'bigop ($b-$a) max 0 (λ${ident j}.((λ${ident j}.true) (${ident j}+$a)))
21 (λ${ident j}.((λ${ident j}.$f)(${ident j}+$a)))}.
23 notation "Max_{ ident j ∈ [a,b[ | p } f"
25 for @{'bigop ($b-$a) max 0 (λ${ident j}.((λ${ident j}.$p) (${ident j}+$a)))
26 (λ${ident j}.((λ${ident j}.$f)(${ident j}+$a)))}.
28 lemma Max_assoc: ∀a,b,c. max (max a b) c = max a (max b c).
29 #a #b #c normalize cases (true_or_false (leb a b)) #leab >leab normalize
30 [cases (true_or_false (leb b c )) #lebc >lebc normalize
31 [>(le_to_leb_true a c) // @(transitive_le ? b) @leb_true_to_le //
34 |cases (true_or_false (leb b c )) #lebc >lebc normalize //
35 >leab normalize >(not_le_to_leb_false a c) // @lt_to_not_le
36 @(transitive_lt ? b) @not_le_to_lt @leb_false_to_not_le //
40 lemma Max0 : ∀n. max 0 n = n.
43 lemma Max0r : ∀n. max n 0 = n.
44 #n >commutative_max //
48 mk_Aop nat 0 max Max0 Max0r (λa,b,c.sym_eq … (Max_assoc a b c)).
50 definition MaxAC ≝ mk_ACop nat 0 MaxA commutative_max.
52 lemma le_Max: ∀f,p,n,a. a < n → p a = true →
53 f a ≤ Max_{i < n | p i}(f i).
55 >(bigop_diff p ? 0 MaxAC f a n) // @(le_maxl … (le_n ?))
58 lemma Max_le: ∀f,p,n,b.
59 (∀a.a < n → p a = true → f a ≤ b) → Max_{i < n | p i}(f i) ≤ b.
60 #f #p #n elim n #b #H //
61 #b0 #H1 cases (true_or_false (p b)) #Hb
62 [>bigop_Strue [2:@Hb] @to_max [@H1 // | @H #a #ltab #pa @H1 // @le_S //]
63 |>bigop_Sfalse [2:@Hb] @H #a #ltab #pa @H1 // @le_S //
67 (******************************** big O notation ******************************)
69 (* O f g means g ∈ O(f) *)
70 definition O: relation (nat→nat) ≝
71 λf,g. ∃c.∃n0.∀n. n0 ≤ n → g n ≤ c* (f n).
73 lemma O_refl: ∀s. O s s.
74 #s %{1} %{0} #n #_ >commutative_times <times_n_1 @le_n qed.
76 lemma O_trans: ∀s1,s2,s3. O s2 s1 → O s3 s2 → O s3 s1.
77 #s1 #s2 #s3 * #c1 * #n1 #H1 * #c2 * # n2 #H2 %{(c1*c2)}
78 %{(max n1 n2)} #n #Hmax
79 @(transitive_le … (H1 ??)) [@(le_maxl … Hmax)]
80 >associative_times @le_times [//|@H2 @(le_maxr … Hmax)]
83 lemma sub_O_to_O: ∀s1,s2. O s1 ⊆ O s2 → O s2 s1.
86 lemma O_to_sub_O: ∀s1,s2. O s2 s1 → O s1 ⊆ O s2.
87 #s1 #s2 #H #g #Hg @(O_trans … H) // qed.
89 definition sum_f ≝ λf,g:nat→nat.λn.f n + g n.
90 interpretation "function sum" 'plus f g = (sum_f f g).
92 lemma O_plus: ∀f,g,s. O s f → O s g → O s (f+g).
93 #f #g #s * #cf * #nf #Hf * #cg * #ng #Hg
94 %{(cf+cg)} %{(max nf ng)} #n #Hmax normalize
95 >distributive_times_plus_r @le_plus
96 [@Hf @(le_maxl … Hmax) |@Hg @(le_maxr … Hmax) ]
99 lemma O_plus_l: ∀f,s1,s2. O s1 f → O (s1+s2) f.
100 #f #s1 #s2 * #c * #a #Os1f %{c} %{a} #n #lean
101 @(transitive_le … (Os1f n lean)) @le_times //
104 lemma O_absorbl: ∀f,g,s. O s f → O f g → O s (g+f).
105 #f #g #s #Osf #Ofg @(O_plus … Osf) @(O_trans … Osf) //
108 lemma O_absorbr: ∀f,g,s. O s f → O f g → O s (f+g).
109 #f #g #s #Osf #Ofg @(O_plus … Osf) @(O_trans … Osf) //
113 lemma O_ff: ∀f,s. O s f → O s (f+f).
117 lemma O_ext2: ∀f,g,s. O s f → (∀x.f x = g x) → O s g.
118 #f #g #s * #c * #a #Osf #eqfg %{c} %{a} #n #lean <eqfg @Osf //
122 definition not_O ≝ λf,g.∀c,n0.∃n. n0 ≤ n ∧ c* (f n) < g n .
124 (* this is the only classical result *)
125 axiom not_O_def: ∀f,g. ¬ O f g → not_O f g.
127 (*********************************** pairing **********************************)
129 axiom pair: nat →nat →nat.
130 axiom fst : nat → nat.
131 axiom snd : nat → nat.
132 axiom fst_pair: ∀a,b. fst (pair a b) = a.
133 axiom snd_pair: ∀a,b. snd (pair a b) = b.
135 interpretation "abstract pair" 'pair f g = (pair f g).
137 (************************ basic complexity notions ****************************)
139 (* u is the deterministic configuration relation of the universal machine (one
142 axiom u: nat → option nat.
147 | S m ⇒ match u c with
148 [ None ⇒ Some ? c (* halting case *)
153 lemma halt_U: ∀i,n,y. u i = None ? → U i n = Some ? y → y = i.
155 [normalize #H1 destruct |#m normalize >H normalize #H1 destruct //]
158 lemma Some_to_halt: ∀n,i,y. U i n = Some ? y → u y = None ? .
160 [#i #y normalize #H destruct (H)
161 |#m #Hind #i #y normalize
162 cut (u i = None ? ∨ ∃c. u i = Some ? c)
163 [cases (u i) [/2/ | #c %2 /2/ ]]
164 *[#H >H normalize #H1 destruct (H1) // |* #c #H >H normalize @Hind ]
168 lemma monotonici_U: ∀y,n,m,i.
169 U i m = Some ? y → U i (n+m) = Some ? y.
171 [#i normalize #H destruct
172 |#p #Hind #i <plus_n_Sm normalize
173 cut (u i = None ? ∨ ∃c. u i = Some ? c)
174 [cases (u i) [/2/ | #c %2 /2/ ]]
175 *[#H1 >H1 normalize // |* #c #H >H normalize #H1 @Hind //]
179 lemma monotonic_U: ∀i,n,m,y.n ≤m →
180 U i n = Some ? y → U i m = Some ? y.
181 #i #n #m #y #lenm #H >(plus_minus_m_m m n) // @monotonici_U //
184 (* axiom U: nat → nat → option nat. *)
185 (* axiom monotonic_U: ∀i,n,m,y.n ≤m →
186 U i n = Some ? y → U i m = Some ? y. *)
188 lemma unique_U: ∀i,n,m,yn,ym.
189 U i n = Some ? yn → U i m = Some ? ym → yn = ym.
190 #i #n #m #yn #ym #Hn #Hm cases (decidable_le n m)
191 [#lenm lapply (monotonic_U … lenm Hn) >Hm #HS destruct (HS) //
192 |#ltmn lapply (monotonic_U … n … Hm) [@lt_to_le @not_le_to_lt //]
193 >Hn #HS destruct (HS) //
197 definition code_for ≝ λf,i.∀x.
198 ∃n.∀m. n ≤ m → U 〈i,x〉 m = f x.
200 definition terminate ≝ λc,r. ∃y. U c r = Some ? y.
202 interpretation "termination" 'fintersects c r = (terminate c r).
204 definition lang ≝ λi,x.∃r,y. U 〈i,x〉 r = Some ? y ∧ 0 < y.
206 lemma lang_cf :∀f,i,x. code_for f i →
207 lang i x ↔ ∃y.f x = Some ? y ∧ 0 < y.
208 #f #i #x normalize #H %
209 [* #n * #y * #H1 #posy %{y} % //
210 cases (H x) -H #m #H <(H (max n m)) [2:@(le_maxr … n) //]
211 @(monotonic_U … H1) @(le_maxl … m) //
212 |cases (H x) -H #m #Hm * #y #Hy %{m} %{y} >Hm //
216 (******************************* complexity classes ***************************)
218 axiom size: nat → nat.
219 axiom of_size: nat → nat.
221 interpretation "size" 'card n = (size n).
223 axiom size_of_size: ∀n. |of_size n| = n.
224 axiom of_size_max: ∀i,n. |i| = n → i ≤ of_size n.
226 axiom size_fst : ∀n. |fst n| ≤ |n|.
228 definition size_f ≝ λf,n.Max_{i < S (of_size n) | eqb (|i|) n}|(f i)|.
230 lemma size_f_def: ∀f,n. size_f f n =
231 Max_{i < S (of_size n) | eqb (|i|) n}|(f i)|.
235 definition Max_const : ∀f,p,n,a. a < n → p a →
237 Max_{i < n | p n}(f n) = *)
239 lemma size_f_size : ∀f,n. size_f (f ∘ size) n = |(f n)|.
240 #f #n @le_to_le_to_eq
241 [@Max_le #a #lta #Ha normalize >(eqb_true_to_eq … Ha) //
242 |<(size_of_size n) in ⊢ (?%?); >size_f_def
243 @(le_Max (λi.|f (|i|)|) ? (S (of_size n)) (of_size n) ??)
244 [@le_S_S // | @eq_to_eqb_true //]
248 lemma size_f_id : ∀n. size_f (λx.x) n = n.
250 [@Max_le #a #lta #Ha >(eqb_true_to_eq … Ha) //
251 |<(size_of_size n) in ⊢ (?%?); >size_f_def
252 @(le_Max (λi.|i|) ? (S (of_size n)) (of_size n) ??)
253 [@le_S_S // | @eq_to_eqb_true //]
257 lemma size_f_fst : ∀n. size_f fst n ≤ n.
258 #n @Max_le #a #lta #Ha <(eqb_true_to_eq … Ha) //
261 (* definition def ≝ λf:nat → option nat.λx.∃y. f x = Some ? y.*)
263 (* C s i means that the complexity of i is O(s) *)
265 definition C ≝ λs,i.∃c.∃a.∀x.a ≤ |x| → ∃y.
266 U 〈i,x〉 (c*(s(|x|))) = Some ? y.
268 definition CF ≝ λs,f.∃i.code_for f i ∧ C s i.
270 lemma ext_CF : ∀f,g,s. (∀n. f n = g n) → CF s f → CF s g.
271 #f #g #s #Hext * #i * #Hcode #HC %{i} %
272 [#x cases (Hcode x) #a #H %{a} <Hext @H | //]
275 lemma monotonic_CF: ∀s1,s2,f. O s2 s1 → CF s1 f → CF s2 f.
276 #s1 #s2 #f * #c1 * #a #H * #i * #Hcodef #HCs1 %{i} % //
277 cases HCs1 #c2 * #b #H2 %{(c2*c1)} %{(max a b)}
278 #x #Hmax cases (H2 x ?) [2:@(le_maxr … Hmax)] #y #Hy
279 %{y} @(monotonic_U …Hy) >associative_times @le_times // @H @(le_maxl … Hmax)
282 (************************** The diagonal language *****************************)
284 (* the diagonal language used for the hierarchy theorem *)
286 definition diag ≝ λs,i.
287 U 〈fst i,i〉 (s (|i|)) = Some ? 0.
289 lemma equiv_diag: ∀s,i.
290 diag s i ↔ 〈fst i, i〉 ↓ s (|i|) ∧ ¬lang (fst i) i.
292 [whd in ⊢ (%→?); #H % [%{0} //] % * #x * #y *
293 #H1 #Hy cut (0 = y) [@(unique_U … H H1)] #eqy /2/
295 #y0 #H * #H1 @False_ind @H1 -H1 whd %{(s (|i|))} %{(S y0)} % //
299 (* Let us define the characteristic function diag_cf for diag, and prove
302 definition diag_cf ≝ λs,i.
303 match U 〈fst i,i〉 (s (|i|)) with
305 | Some y ⇒ if (eqb y 0) then (Some ? 1) else (Some ? 0)].
307 lemma diag_cf_OK: ∀s,x. diag s x ↔ ∃y.diag_cf s x = Some ? y ∧ 0 < y.
309 [whd in ⊢ (%→?); #H %{1} % // whd in ⊢ (??%?); >H //
310 |* #y * whd in ⊢ (??%?→?→%);
311 cases (U 〈fst x,x〉 (s (|x|))) normalize
313 |#x cases (true_or_false (eqb x 0)) #Hx >Hx
314 [>(eqb_true_to_eq … Hx) //
315 |normalize #H destruct #H @False_ind @(absurd ? H) @lt_to_not_le //
321 lemma diag_spec: ∀s,i. code_for (diag_cf s) i → ∀x. lang i x ↔ diag s x.
322 #s #i #Hcode #x @(iff_trans … (lang_cf … Hcode)) @iff_sym @diag_cf_OK
325 (******************************************************************************)
327 lemma absurd1: ∀P. iff P (¬ P) →False.
328 #P * #H1 #H2 cut (¬P) [% #H2 @(absurd … H2) @H1 //]
329 #H3 @(absurd ?? H3) @H2 @H3
332 (* axiom weak_pad : ∀a,∃a0.∀n. a0 < n → ∃b. |〈a,b〉| = n. *)
333 axiom pad : ∀a,n. |a| < n → ∃b. |〈a,b〉| = n.
335 lemma not_included_ex: ∀s1,s2. not_O s2 s1 → ∀i. C s2 i →
336 ∃b.〈i, 〈i,b〉〉 ↓ s1 (|〈i,b〉|).
337 #s1 #s2 #H #i * #c * #x0 #H1
338 cases (H c (max (S(|i|)) x0)) #x1 * #H2 #H3 cases (pad i x1 ?)
341 [#z >H4 #H5 %{z} @(monotonic_U … H5) @lt_to_le //
348 lemma diag1_not_s1: ∀s1,s2. not_O s2 s1 → ¬ CF s2 (diag_cf s1).
349 #s1 #s2 #H1 % * #i * #Hcode_i #Hs2_i
350 cases (not_included_ex … H1 ? Hs2_i) #b #H2
351 lapply (diag_spec … Hcode_i) #H3
352 @(absurd1 (lang i 〈i,b〉))
353 @(iff_trans … (H3 〈i,b〉))
354 @(iff_trans … (equiv_diag …)) >fst_pair
355 %[* #_ // |#H6 % // ]
358 (******************************************************************************)
359 (* definition sumF ≝ λf,g:nat→nat.λn.f n + g n. *)
361 definition to_Some ≝ λf.λx:nat. Some nat (f x).
363 definition deopt ≝ λn. match n with
367 definition opt_comp ≝ λf,g:nat → option nat. λx.
372 (* axiom CFU: ∀h,g,s. CF s (to_Some h) → CF s (to_Some (of_size ∘ g)) →
373 CF (Slow s) (λx.U (h x) (g x)). *)
375 axiom sU2: nat → nat → nat.
376 axiom sU: nat → nat → nat → nat.
378 (* axiom CFU: CF sU (λx.U (fst x) (snd x)). *)
380 axiom CFU: ∀h,g,f,s1,s2,s3.
381 CF s1 (to_Some h) → CF s2 (to_Some g) → CF s3 (to_Some f) →
382 CF (λx. s1 x + s2 x + s3 x + sU (size_f h x) (size_f g x) (size_f f x))
383 (λx.U 〈h x,g x〉 (|f x|)).
385 axiom monotonic_sU: ∀a1,a2,b1,b2,c1,c2. a1 ≤ a2 → b1 ≤ b2 → c1 ≤c2 →
386 sU a1 b1 c1 ≤ sU a2 b2 c2.
388 definition sU_space ≝ λi,x,r.i+x+r.
389 definition sU_time ≝ λi,x,r.i+x+(i^2)*r*(log 2 r).
392 axiom CF_comp: ∀f,g,s1, s2. CF s1 f → CF s2 g →
393 CF (λx.s2 x + s1 (size (deopt (g x)))) (opt_comp f g).
395 (* axiom CF_comp: ∀f,g,s1, s2. CF s1 f → CF s2 g →
396 CF (s1 ∘ (λx. size (deopt (g x)))) (opt_comp f g). *)
398 axiom CF_comp_strong: ∀f,g,s1,s2. CF s1 f → CF s2 g →
399 CF (s1 ∘ s2) (opt_comp f g). *)
401 definition IF ≝ λb,f,g:nat →option nat. λx.
404 |Some n ⇒ if (eqb n 0) then f x else g x].
406 axiom IF_CF: ∀b,f,g,sb,sf,sg. CF sb b → CF sf f → CF sg g →
407 CF (λn. sb n + sf n + sg n) (IF b f g).
409 lemma diag_cf_def : ∀s.∀i.
411 IF (λi.U (pair (fst i) i) (|of_size (s (|i|))|)) (λi.Some ? 1) (λi.Some ? 0) i.
412 #s #i normalize >size_of_size // qed.
415 axiom CF_pair: ∀f,g,s. CF s (λx.Some ? (f x)) → CF s (λx.Some ? (g x)) →
416 CF s (λx.Some ? (pair (f x) (g x))).
418 axiom CF_fst: ∀f,s. CF s (λx.Some ? (f x)) → CF s (λx.Some ? (fst (f x))).
420 definition minimal ≝ λs. CF s (λn. Some ? n) ∧ ∀c. CF s (λn. Some ? c).
424 axiom le_snd: ∀n. |snd n| ≤ |n|.
425 axiom daemon: ∀P:Prop.P. *)
427 definition constructible ≝ λs. CF s (λx.Some ? (of_size (s (|x|)))).
431 CF s (to_Some fst) → CF s (to_Some (λx.x)) → CF s (to_Some (λx.(s (|x|)))) →
432 CF (λx. s x + s x + s x + sU (size_f fst x) (size_f (λx.x) x) (size_f (λx.(s (|x|))) x))
433 (λx.U 〈fst x,x〉 (|s (|x|)|)).
434 #s #H1 #H2 #H3 @CFU //
438 CF s (to_Some fst) → CF s (to_Some (λx.x)) → CF s (to_Some (λx.(s (|x|)))) →
439 CF (λx. s x + s x + s x + sU (size_f fst x) (size_f (λx.x) x) (|(s x)| ))
440 (λx.U 〈fst x,x〉 (|s (|x|)|)).
441 #s #H1 #H2 #H3 @monotonic_CF [3: @(CFU ??? s s s) @CFU //
444 lemma diag_s: ∀s. minimal s → constructible s →
445 CF (λx.s x + sU x x (s x)) (diag_cf s).
446 #s * #Hs_id #Hs_c #Hs_constr
447 @ext_CF [2: #n @sym_eq @diag_cf_def | skip]
448 @(monotonic_CF ???? (IF_CF (λi:ℕ.U (pair (fst i) i) (|of_size (s (|i|))|))
449 … (λi.s i + s i + s i + (sU (size_f fst i) (size_f (λi.i) i) (size_f (λi.of_size (s (|i|))) i))) … (Hs_c 1) (Hs_c 0) … ))
450 [2: @CFU [@CF_fst // | // |@Hs_constr]
451 |@(O_ext2 (λn:ℕ.s n+sU (size_f fst n) n (s n) + (s n+s n+s n+s n)))
452 [2: #i >size_f_size >size_of_size >size_f_id //]
454 [%{1} %{0} #n #_ >commutative_times <times_n_1 @le_plus //
456 |@O_plus_l @(O_plus … (O_refl s)) @(O_plus … (O_refl s))
457 @(O_plus … (O_refl s)) //
462 (*************************** The hierachy theorem *****************************)
465 theorem hierarchy_theorem_right: ∀s1,s2:nat→nat.
466 O s1 idN → constructible s1 →
467 not_O s2 s1 → ¬ CF s1 ⊆ CF s2.
468 #s1 #s2 #Hs1 #monos1 #H % #H1
469 @(absurd … (CF s2 (diag_cf s1)))
470 [@H1 @diag_s // |@(diag1_not_s1 … H)]
474 theorem hierarchy_theorem_left: ∀s1,s2:nat→nat.
475 O(s1) ⊆ O(s2) → CF s1 ⊆ CF s2.
476 #s1 #s2 #HO #f * #i * #Hcode * #c * #a #Hs1_i %{i} % //
477 cases (sub_O_to_O … HO) -HO #c1 * #b #Hs1s2
478 %{(c*c1)} %{(max a b)} #x #lemax
479 cases (Hs1_i x ?) [2: @(le_maxl …lemax)]
480 #y #Hy %{y} @(monotonic_U … Hy) >associative_times
481 @le_times // @Hs1s2 @(le_maxr … lemax)