merged
authorwenzelm
Tue, 29 May 2018 22:29:32 +0200
changeset 68324 88c07fabd5b4
parent 68313 56c57e91edf9 (diff)
parent 68323 bf7336731981 (current diff)
child 68325 57e4bd1e2e18
child 68330 d7920eb7de54
merged
--- a/src/HOL/Data_Structures/AVL_Set.thy	Tue May 29 22:25:59 2018 +0200
+++ b/src/HOL/Data_Structures/AVL_Set.thy	Tue May 29 22:29:32 2018 +0200
@@ -455,80 +455,93 @@
 
 subsection \<open>Height-Size Relation\<close>
 
-text \<open>By Daniel St\"uwe\<close>
-
-fun fib_tree :: "nat \<Rightarrow> unit avl_tree" where
-"fib_tree 0 = Leaf" |
-"fib_tree (Suc 0) = Node 1 Leaf () Leaf" |
-"fib_tree (Suc(Suc n)) = Node (Suc(Suc(n))) (fib_tree (Suc n)) () (fib_tree n)"
-
-lemma [simp]: "ht (fib_tree h) = h"
-by (induction h rule: "fib_tree.induct") auto
+text \<open>By Daniel St\"uwe, Manuel Eberl and Peter Lammich.\<close>
 
-lemma [simp]: "height (fib_tree h) = h"
-by (induction h rule: "fib_tree.induct") auto
-
-lemma "avl(fib_tree h)"          
-by (induction h rule: "fib_tree.induct") auto
-
-lemma fib_tree_size1: "size1 (fib_tree h) = fib (h+2)"
-by (induction h rule: fib_tree.induct) auto
-
-lemma height_invers[simp]: 
+lemma height_invers: 
   "(height t = 0) = (t = Leaf)"
   "avl t \<Longrightarrow> (height t = Suc h) = (\<exists> l a r . t = Node (Suc h) l a r)"
 by (induction t) auto
 
-lemma fib_Suc_lt: "fib n \<le> fib (Suc n)"
-by (induction n rule: fib.induct) auto
+text \<open>Any AVL tree of height \<open>h\<close> has at least \<open>fib (h+2)\<close> leaves:\<close>
 
-lemma fib_mono: "n \<le> m \<Longrightarrow> fib n \<le> fib m"
-proof (induction n arbitrary: m rule: fib.induct )
-  case (2 m)
-  thus ?case using fib_neq_0_nat[of m] by auto
+lemma avl_fib_bound: "avl t \<Longrightarrow> height t = h \<Longrightarrow> fib (h+2) \<le> size1 t"
+proof (induction h arbitrary: t rule: fib.induct)
+  case 1 thus ?case by (simp add: height_invers)
 next
-  case (3 n m)
-  from 3 obtain m' where "m = Suc (Suc m')"
-    by (metis le_Suc_ex plus_nat.simps(2)) 
-  thus ?case using 3(1)[of "Suc m'"] 3(2)[of m'] 3(3) by auto
-qed simp
-
-lemma size1_fib_tree_mono:
-  assumes "n \<le> m"
-  shows   "size1 (fib_tree n) \<le> size1 (fib_tree m)"
-using fib_tree_size1 fib_mono[OF assms] fib_mono[of "Suc n"] add_le_mono assms by fastforce 
-
-lemma fib_tree_minimal: "avl t \<Longrightarrow> size1 (fib_tree (ht t)) \<le> size1 t"
-proof (induction "ht t" arbitrary: t rule: fib_tree.induct)
-  case (2 t)
-  from 2 obtain l a r where "t = Node (Suc 0) l a r" by (cases t) auto
-  with 2 show ?case by auto
+  case 2 thus ?case by (cases t) (auto simp: height_invers)
 next
-  case (3 h t)
-  note [simp] = 3(3)[symmetric] 
-  from 3 obtain l a r where [simp]: "t = Node (Suc (Suc h)) l a r" by (cases t) auto
-  show ?case proof (cases rule: linorder_cases[of "ht l" "ht r"]) 
-    case equal
-    with 3(3,4) have ht: "ht l = Suc h" "ht r = Suc h" by auto
-    with 3 have "size1 (fib_tree (ht l)) \<le> size1 l" by auto moreover
-    from 3(1)[of r] 3(3,4) ht(2) have "size1 (fib_tree (ht r)) \<le> size1 r" by auto ultimately
-    show ?thesis using ht size1_fib_tree_mono[of h "Suc h"] by auto
-  next
-    case greater
-    with 3(3,4) have ht: "ht l = Suc h"  "ht r = h" by auto
-    from ht 3(1,2,4) have "size1 (fib_tree (Suc h)) \<le> size1 l" by auto moreover
-    from ht 3(1,2,4) have "size1 (fib_tree h) \<le> size1 r" by auto ultimately
-    show ?thesis by auto
-  next
-    case less (* analogously *)
-    with 3 have ht: "ht l = h"  "Suc h = ht r" by auto
-    from ht 3 have "size1 (fib_tree h) \<le> size1 l" by auto moreover
-    from ht 3 have "size1 (fib_tree (Suc h)) \<le> size1 r" by auto ultimately
-    show ?thesis by auto
-  qed
-qed auto
+  case (3 h)
+  from "3.prems" obtain l a r where
+    [simp]: "t = Node (Suc(Suc h)) l a r" "avl l" "avl r"
+    and C: "
+      height r = Suc h \<and> height l = Suc h
+    \<or> height r = Suc h \<and> height l = h
+    \<or> height r = h \<and> height l = Suc h" (is "?C1 \<or> ?C2 \<or> ?C3")
+    by (cases t) (simp, fastforce)
+  {
+    assume ?C1
+    with "3.IH"(1)
+    have "fib (h + 3) \<le> size1 l" "fib (h + 3) \<le> size1 r"
+      by (simp_all add: eval_nat_numeral)
+    hence ?case by (auto simp: eval_nat_numeral)
+  } moreover {
+    assume ?C2
+    hence ?case using "3.IH"(1)[of r] "3.IH"(2)[of l] by auto
+  } moreover {
+    assume ?C3
+    hence ?case using "3.IH"(1)[of l] "3.IH"(2)[of r] by auto
+  } ultimately show ?case using C by blast
+qed
+
+lemma fib_alt_induct [consumes 1, case_names 1 2 rec]:
+  assumes "n > 0" "P 1" "P 2" "\<And>n. n > 0 \<Longrightarrow> P n \<Longrightarrow> P (Suc n) \<Longrightarrow> P (Suc (Suc n))"
+  shows   "P n"
+  using assms(1)
+proof (induction n rule: fib.induct)
+  case (3 n)
+  thus ?case using assms by (cases n) (auto simp: eval_nat_numeral)
+qed (insert assms, auto)
+
+text \<open>An exponential lower bound for @{const fib}:\<close>
 
-theorem avl_size_bound: "avl t \<Longrightarrow> fib(height t + 2) \<le> size1 t" 
-using fib_tree_minimal fib_tree_size1 by fastforce
+lemma fib_lowerbound:
+  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
+  defines "c \<equiv> 1 / \<phi> ^ 2"
+  assumes "n > 0"
+  shows   "real (fib n) \<ge> c * \<phi> ^ n"
+proof -
+  have "\<phi> > 1" by (simp add: \<phi>_def)
+  hence "c > 0" by (simp add: c_def)
+  from \<open>n > 0\<close> show ?thesis
+  proof (induction n rule: fib_alt_induct)
+    case (rec n)
+    have "c * \<phi> ^ Suc (Suc n) = \<phi> ^ 2 * (c * \<phi> ^ n)"
+      by (simp add: field_simps power2_eq_square)
+    also have "\<dots> \<le> (\<phi> + 1) * (c * \<phi> ^ n)"
+      by (rule mult_right_mono) (insert \<open>c > 0\<close>, simp_all add: \<phi>_def power2_eq_square field_simps)
+    also have "\<dots> = c * \<phi> ^ Suc n + c * \<phi> ^ n"
+      by (simp add: field_simps)
+    also have "\<dots> \<le> real (fib (Suc n)) + real (fib n)"
+      by (intro add_mono rec.IH)
+    finally show ?case by simp
+  qed (insert \<open>\<phi> > 1\<close>, simp_all add: c_def power2_eq_square eval_nat_numeral)
+qed
+
+text \<open>The size of an AVL tree is (at least) exponential in its height:\<close>
+
+lemma avl_lowerbound:
+  defines "\<phi> \<equiv> (1 + sqrt 5) / 2"
+  assumes "avl t"
+  shows   "real (size1 t) \<ge> \<phi> ^ (height t)"
+proof -
+  have "\<phi> > 0" by(simp add: \<phi>_def add_pos_nonneg)
+  hence "\<phi> ^ height t = (1 / \<phi> ^ 2) * \<phi> ^ (height t + 2)"
+    by(simp add: field_simps power2_eq_square)
+  also have "\<dots> \<le> real (fib (height t + 2))"
+    using fib_lowerbound[of "height t + 2"] by(simp add: \<phi>_def)
+  also have "\<dots> \<le> real (size1 t)"
+    using avl_fib_bound[of t "height t"] assms by simp
+  finally show ?thesis .
+qed
 
 end
--- a/src/HOL/Library/Fun_Lexorder.thy	Tue May 29 22:25:59 2018 +0200
+++ b/src/HOL/Library/Fun_Lexorder.thy	Tue May 29 22:29:32 2018 +0200
@@ -1,6 +1,6 @@
 (* Author: Florian Haftmann, TU Muenchen *)
 
-section \<open>Lexical order on functions\<close>
+section \<open>Lexicographic order on functions\<close>
 
 theory Fun_Lexorder
 imports Main
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/List_Lexorder.thy	Tue May 29 22:29:32 2018 +0200
@@ -0,0 +1,121 @@
+(*  Title:      HOL/Library/List_Lexorder.thy
+    Author:     Norbert Voelker
+*)
+
+section \<open>Lexicographic order on lists\<close>
+
+theory List_Lexorder
+imports Main
+begin
+
+instantiation list :: (ord) ord
+begin
+
+definition
+  list_less_def: "xs < ys \<longleftrightarrow> (xs, ys) \<in> lexord {(u, v). u < v}"
+
+definition
+  list_le_def: "(xs :: _ list) \<le> ys \<longleftrightarrow> xs < ys \<or> xs = ys"
+
+instance ..
+
+end
+
+instance list :: (order) order
+proof
+  fix xs :: "'a list"
+  show "xs \<le> xs" by (simp add: list_le_def)
+next
+  fix xs ys zs :: "'a list"
+  assume "xs \<le> ys" and "ys \<le> zs"
+  then show "xs \<le> zs"
+    apply (auto simp add: list_le_def list_less_def)
+    apply (rule lexord_trans)
+    apply (auto intro: transI)
+    done
+next
+  fix xs ys :: "'a list"
+  assume "xs \<le> ys" and "ys \<le> xs"
+  then show "xs = ys"
+    apply (auto simp add: list_le_def list_less_def)
+    apply (rule lexord_irreflexive [THEN notE])
+    defer
+    apply (rule lexord_trans)
+    apply (auto intro: transI)
+    done
+next
+  fix xs ys :: "'a list"
+  show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs"
+    apply (auto simp add: list_less_def list_le_def)
+    defer
+    apply (rule lexord_irreflexive [THEN notE])
+    apply auto
+    apply (rule lexord_irreflexive [THEN notE])
+    defer
+    apply (rule lexord_trans)
+    apply (auto intro: transI)
+    done
+qed
+
+instance list :: (linorder) linorder
+proof
+  fix xs ys :: "'a list"
+  have "(xs, ys) \<in> lexord {(u, v). u < v} \<or> xs = ys \<or> (ys, xs) \<in> lexord {(u, v). u < v}"
+    by (rule lexord_linear) auto
+  then show "xs \<le> ys \<or> ys \<le> xs"
+    by (auto simp add: list_le_def list_less_def)
+qed
+
+instantiation list :: (linorder) distrib_lattice
+begin
+
+definition "(inf :: 'a list \<Rightarrow> _) = min"
+
+definition "(sup :: 'a list \<Rightarrow> _) = max"
+
+instance
+  by standard (auto simp add: inf_list_def sup_list_def max_min_distrib2)
+
+end
+
+lemma not_less_Nil [simp]: "\<not> x < []"
+  by (simp add: list_less_def)
+
+lemma Nil_less_Cons [simp]: "[] < a # x"
+  by (simp add: list_less_def)
+
+lemma Cons_less_Cons [simp]: "a # x < b # y \<longleftrightarrow> a < b \<or> a = b \<and> x < y"
+  by (simp add: list_less_def)
+
+lemma le_Nil [simp]: "x \<le> [] \<longleftrightarrow> x = []"
+  unfolding list_le_def by (cases x) auto
+
+lemma Nil_le_Cons [simp]: "[] \<le> x"
+  unfolding list_le_def by (cases x) auto
+
+lemma Cons_le_Cons [simp]: "a # x \<le> b # y \<longleftrightarrow> a < b \<or> a = b \<and> x \<le> y"
+  unfolding list_le_def by auto
+
+instantiation list :: (order) order_bot
+begin
+
+definition "bot = []"
+
+instance
+  by standard (simp add: bot_list_def)
+
+end
+
+lemma less_list_code [code]:
+  "xs < ([]::'a::{equal, order} list) \<longleftrightarrow> False"
+  "[] < (x::'a::{equal, order}) # xs \<longleftrightarrow> True"
+  "(x::'a::{equal, order}) # xs < y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs < ys"
+  by simp_all
+
+lemma less_eq_list_code [code]:
+  "x # xs \<le> ([]::'a::{equal, order} list) \<longleftrightarrow> False"
+  "[] \<le> (xs::'a::{equal, order} list) \<longleftrightarrow> True"
+  "(x::'a::{equal, order}) # xs \<le> y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs \<le> ys"
+  by simp_all
+
+end
--- a/src/HOL/Library/List_lexord.thy	Tue May 29 22:25:59 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,121 +0,0 @@
-(*  Title:      HOL/Library/List_lexord.thy
-    Author:     Norbert Voelker
-*)
-
-section \<open>Lexicographic order on lists\<close>
-
-theory List_lexord
-imports Main
-begin
-
-instantiation list :: (ord) ord
-begin
-
-definition
-  list_less_def: "xs < ys \<longleftrightarrow> (xs, ys) \<in> lexord {(u, v). u < v}"
-
-definition
-  list_le_def: "(xs :: _ list) \<le> ys \<longleftrightarrow> xs < ys \<or> xs = ys"
-
-instance ..
-
-end
-
-instance list :: (order) order
-proof
-  fix xs :: "'a list"
-  show "xs \<le> xs" by (simp add: list_le_def)
-next
-  fix xs ys zs :: "'a list"
-  assume "xs \<le> ys" and "ys \<le> zs"
-  then show "xs \<le> zs"
-    apply (auto simp add: list_le_def list_less_def)
-    apply (rule lexord_trans)
-    apply (auto intro: transI)
-    done
-next
-  fix xs ys :: "'a list"
-  assume "xs \<le> ys" and "ys \<le> xs"
-  then show "xs = ys"
-    apply (auto simp add: list_le_def list_less_def)
-    apply (rule lexord_irreflexive [THEN notE])
-    defer
-    apply (rule lexord_trans)
-    apply (auto intro: transI)
-    done
-next
-  fix xs ys :: "'a list"
-  show "xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs"
-    apply (auto simp add: list_less_def list_le_def)
-    defer
-    apply (rule lexord_irreflexive [THEN notE])
-    apply auto
-    apply (rule lexord_irreflexive [THEN notE])
-    defer
-    apply (rule lexord_trans)
-    apply (auto intro: transI)
-    done
-qed
-
-instance list :: (linorder) linorder
-proof
-  fix xs ys :: "'a list"
-  have "(xs, ys) \<in> lexord {(u, v). u < v} \<or> xs = ys \<or> (ys, xs) \<in> lexord {(u, v). u < v}"
-    by (rule lexord_linear) auto
-  then show "xs \<le> ys \<or> ys \<le> xs"
-    by (auto simp add: list_le_def list_less_def)
-qed
-
-instantiation list :: (linorder) distrib_lattice
-begin
-
-definition "(inf :: 'a list \<Rightarrow> _) = min"
-
-definition "(sup :: 'a list \<Rightarrow> _) = max"
-
-instance
-  by standard (auto simp add: inf_list_def sup_list_def max_min_distrib2)
-
-end
-
-lemma not_less_Nil [simp]: "\<not> x < []"
-  by (simp add: list_less_def)
-
-lemma Nil_less_Cons [simp]: "[] < a # x"
-  by (simp add: list_less_def)
-
-lemma Cons_less_Cons [simp]: "a # x < b # y \<longleftrightarrow> a < b \<or> a = b \<and> x < y"
-  by (simp add: list_less_def)
-
-lemma le_Nil [simp]: "x \<le> [] \<longleftrightarrow> x = []"
-  unfolding list_le_def by (cases x) auto
-
-lemma Nil_le_Cons [simp]: "[] \<le> x"
-  unfolding list_le_def by (cases x) auto
-
-lemma Cons_le_Cons [simp]: "a # x \<le> b # y \<longleftrightarrow> a < b \<or> a = b \<and> x \<le> y"
-  unfolding list_le_def by auto
-
-instantiation list :: (order) order_bot
-begin
-
-definition "bot = []"
-
-instance
-  by standard (simp add: bot_list_def)
-
-end
-
-lemma less_list_code [code]:
-  "xs < ([]::'a::{equal, order} list) \<longleftrightarrow> False"
-  "[] < (x::'a::{equal, order}) # xs \<longleftrightarrow> True"
-  "(x::'a::{equal, order}) # xs < y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs < ys"
-  by simp_all
-
-lemma less_eq_list_code [code]:
-  "x # xs \<le> ([]::'a::{equal, order} list) \<longleftrightarrow> False"
-  "[] \<le> (xs::'a::{equal, order} list) \<longleftrightarrow> True"
-  "(x::'a::{equal, order}) # xs \<le> y # ys \<longleftrightarrow> x < y \<or> x = y \<and> xs \<le> ys"
-  by simp_all
-
-end
--- a/src/HOL/ROOT	Tue May 29 22:25:59 2018 +0200
+++ b/src/HOL/ROOT	Tue May 29 22:29:32 2018 +0200
@@ -29,7 +29,7 @@
     Library
     (*conflicting type class instantiations and dependent applications*)
     Finite_Lattice
-    List_lexord
+    List_Lexorder
     Prefix_Order
     Product_Lexorder
     Product_Order
--- a/src/HOL/ex/Radix_Sort.thy	Tue May 29 22:25:59 2018 +0200
+++ b/src/HOL/ex/Radix_Sort.thy	Tue May 29 22:29:32 2018 +0200
@@ -2,7 +2,7 @@
 
 theory Radix_Sort
 imports
-  "HOL-Library.List_lexord" 
+  "HOL-Library.List_Lexorder" 
   "HOL-Library.Sublist" 
   "HOL-Library.Multiset" 
 begin