src/HOL/Library/Tree.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62202 e5bc7cbb0bcc
child 62650 7e6bb43e7217
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
     1 (* Author: Tobias Nipkow *)
     2 
     3 section \<open>Binary Tree\<close>
     4 
     5 theory Tree
     6 imports Main
     7 begin
     8 
     9 datatype 'a tree =
    10   is_Leaf: Leaf ("\<langle>\<rangle>") |
    11   Node (left: "'a tree") (val: 'a) (right: "'a tree") ("(1\<langle>_,/ _,/ _\<rangle>)")
    12   where
    13     "left Leaf = Leaf"
    14   | "right Leaf = Leaf"
    15 datatype_compat tree
    16 
    17 text\<open>Can be seen as counting the number of leaves rather than nodes:\<close>
    18 
    19 definition size1 :: "'a tree \<Rightarrow> nat" where
    20 "size1 t = size t + 1"
    21 
    22 lemma size1_simps[simp]:
    23   "size1 \<langle>\<rangle> = 1"
    24   "size1 \<langle>l, x, r\<rangle> = size1 l + size1 r"
    25 by (simp_all add: size1_def)
    26 
    27 lemma size_0_iff_Leaf: "size t = 0 \<longleftrightarrow> t = Leaf"
    28 by(cases t) auto
    29 
    30 lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)"
    31 by (cases t) auto
    32 
    33 lemma finite_set_tree[simp]: "finite(set_tree t)"
    34 by(induction t) auto
    35 
    36 lemma size_map_tree[simp]: "size (map_tree f t) = size t"
    37 by (induction t) auto
    38 
    39 lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
    40 by (simp add: size1_def)
    41 
    42 
    43 subsection "The Height"
    44 
    45 class height = fixes height :: "'a \<Rightarrow> nat"
    46 
    47 instantiation tree :: (type)height
    48 begin
    49 
    50 fun height_tree :: "'a tree => nat" where
    51 "height Leaf = 0" |
    52 "height (Node t1 a t2) = max (height t1) (height t2) + 1"
    53 
    54 instance ..
    55 
    56 end
    57 
    58 lemma height_map_tree[simp]: "height (map_tree f t) = height t"
    59 by (induction t) auto
    60 
    61 lemma size1_height: "size t + 1 \<le> 2 ^ height (t::'a tree)"
    62 proof(induction t)
    63   case (Node l a r)
    64   show ?case
    65   proof (cases "height l \<le> height r")
    66     case True
    67     have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
    68     also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1))
    69     also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2))
    70     also have "(2::nat) ^ height l \<le> 2 ^ height r" using True by simp
    71     finally show ?thesis using True by (auto simp: max_def mult_2)
    72   next
    73     case False
    74     have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
    75     also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1))
    76     also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2))
    77     also have "(2::nat) ^ height r \<le> 2 ^ height l" using False by simp
    78     finally show ?thesis using False by (auto simp: max_def mult_2)
    79   qed
    80 qed simp
    81 
    82 
    83 subsection "The set of subtrees"
    84 
    85 fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
    86 "subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
    87 "subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"
    88 
    89 lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t"
    90 by (induction t)(auto)
    91 
    92 lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
    93 by (induction t) auto
    94 
    95 lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
    96 by (metis Node_notin_subtrees_if)
    97 
    98 
    99 subsection "List of entries"
   100 
   101 fun preorder :: "'a tree \<Rightarrow> 'a list" where
   102 "preorder \<langle>\<rangle> = []" |
   103 "preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
   104 
   105 fun inorder :: "'a tree \<Rightarrow> 'a list" where
   106 "inorder \<langle>\<rangle> = []" |
   107 "inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
   108 
   109 lemma set_inorder[simp]: "set (inorder t) = set_tree t"
   110 by (induction t) auto
   111 
   112 lemma set_preorder[simp]: "set (preorder t) = set_tree t"
   113 by (induction t) auto
   114 
   115 lemma length_preorder[simp]: "length (preorder t) = size t"
   116 by (induction t) auto
   117 
   118 lemma length_inorder[simp]: "length (inorder t) = size t"
   119 by (induction t) auto
   120 
   121 lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
   122 by (induction t) auto
   123 
   124 lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
   125 by (induction t) auto
   126 
   127 
   128 subsection \<open>Binary Search Tree predicate\<close>
   129 
   130 fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
   131 "bst \<langle>\<rangle> \<longleftrightarrow> True" |
   132 "bst \<langle>l, a, r\<rangle> \<longleftrightarrow> bst l \<and> bst r \<and> (\<forall>x\<in>set_tree l. x < a) \<and> (\<forall>x\<in>set_tree r. a < x)"
   133 
   134 text\<open>In case there are duplicates:\<close>
   135 
   136 fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where
   137 "bst_eq \<langle>\<rangle> \<longleftrightarrow> True" |
   138 "bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow>
   139  bst_eq l \<and> bst_eq r \<and> (\<forall>x\<in>set_tree l. x \<le> a) \<and> (\<forall>x\<in>set_tree r. a \<le> x)"
   140 
   141 lemma (in linorder) bst_eq_if_bst: "bst t \<Longrightarrow> bst_eq t"
   142 by (induction t) (auto)
   143 
   144 lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)"
   145 apply (induction t)
   146  apply(simp)
   147 by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
   148 
   149 lemma (in linorder) distinct_preorder_if_bst: "bst t \<Longrightarrow> distinct (preorder t)"
   150 apply (induction t)
   151  apply simp
   152 apply(fastforce elim: order.asym)
   153 done
   154 
   155 lemma (in linorder) distinct_inorder_if_bst: "bst t \<Longrightarrow> distinct (inorder t)"
   156 apply (induction t)
   157  apply simp
   158 apply(fastforce elim: order.asym)
   159 done
   160 
   161 
   162 subsection "The heap predicate"
   163 
   164 fun heap :: "'a::linorder tree \<Rightarrow> bool" where
   165 "heap Leaf = True" |
   166 "heap (Node l m r) =
   167   (heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))"
   168 
   169 
   170 subsection "Function \<open>mirror\<close>"
   171 
   172 fun mirror :: "'a tree \<Rightarrow> 'a tree" where
   173 "mirror \<langle>\<rangle> = Leaf" |
   174 "mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
   175 
   176 lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
   177 by (induction t) simp_all
   178 
   179 lemma size_mirror[simp]: "size(mirror t) = size t"
   180 by (induction t) simp_all
   181 
   182 lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
   183 by (simp add: size1_def)
   184 
   185 lemma height_mirror[simp]: "height(mirror t) = height t"
   186 by (induction t) simp_all
   187 
   188 lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
   189 by (induction t) simp_all
   190 
   191 lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
   192 by (induction t) simp_all
   193 
   194 lemma mirror_mirror[simp]: "mirror(mirror t) = t"
   195 by (induction t) simp_all
   196 
   197 end