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