src/HOL/Data_Structures/Leftist_Heap.thy
author nipkow
Wed, 01 Feb 2017 21:09:47 +0100
changeset 64976 1a4cb9403a10
parent 64975 96b66d5c0fc1
child 64977 50f2f10ab576
permissions -rw-r--r--
renaming
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     2
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     3
section \<open>Leftist Heap\<close>
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     4
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     5
theory Leftist_Heap
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     6
imports Tree2 "~~/src/HOL/Library/Multiset" Complex_Main
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     7
begin
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
     8
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
     9
fun mset_tree :: "('a,'b) tree \<Rightarrow> 'a multiset" where
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    10
"mset_tree Leaf = {#}" |
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    11
"mset_tree (Node _ l a r) = {#a#} + mset_tree l + mset_tree r"
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    12
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    13
type_synonym 'a lheap = "('a,nat)tree"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    14
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    15
fun rank :: "'a lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    16
"rank Leaf = 0" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    17
"rank (Node _ _ _ r) = rank r + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    18
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    19
fun rk :: "'a lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    20
"rk Leaf = 0" |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    21
"rk (Node n _ _ _) = n"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    22
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    23
text{* The invariants: *}
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    24
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    25
fun (in linorder) heap :: "('a,'b) tree \<Rightarrow> bool" where
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    26
"heap Leaf = True" |
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    27
"heap (Node _ l m r) =
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    28
  (heap l \<and> heap r \<and> (\<forall>x \<in> set_mset(mset_tree l + mset_tree r). m \<le> x))"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    29
64973
nipkow
parents: 64971
diff changeset
    30
fun ltree :: "'a lheap \<Rightarrow> bool" where
nipkow
parents: 64971
diff changeset
    31
"ltree Leaf = True" |
nipkow
parents: 64971
diff changeset
    32
"ltree (Node n l a r) =
nipkow
parents: 64971
diff changeset
    33
 (n = rank r + 1 \<and> rank l \<ge> rank r \<and> ltree l & ltree r)"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    34
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    35
definition node :: "'a lheap \<Rightarrow> 'a \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    36
"node l a r =
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    37
 (let rl = rk l; rr = rk r
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    38
  in if rl \<ge> rr then Node (rr+1) l a r else Node (rl+1) r a l)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    39
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    40
fun get_min :: "'a lheap \<Rightarrow> 'a" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    41
"get_min(Node n l a r) = a"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    42
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    43
function merge :: "'a::ord lheap \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    44
"merge Leaf t2 = t2" |
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    45
"merge t1 Leaf = t1" |
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    46
"merge (Node n1 l1 a1 r1) (Node n2 l2 a2 r2) =
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    47
   (if a1 \<le> a2 then node l1 a1 (merge r1 (Node n2 l2 a2 r2))
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    48
    else node l2 a2 (merge r2 (Node n1 l1 a1 r1)))"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    49
by pat_completeness auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    50
termination by (relation "measure (%(t1,t2). rank t1 + rank t2)") auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    51
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    52
lemma merge_code: "merge t1 t2 = (case (t1,t2) of
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    53
  (Leaf, _) \<Rightarrow> t2 |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    54
  (_, Leaf) \<Rightarrow> t1 |
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    55
  (Node n1 l1 a1 r1, Node n2 l2 a2 r2) \<Rightarrow>
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    56
    if a1 \<le> a2 then node l1 a1 (merge r1 t2) else node l2 a2 (merge r2 t1))"
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    57
by(induction t1 t2 rule: merge.induct) (simp_all split: tree.split)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    58
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    59
definition insert :: "'a::ord \<Rightarrow> 'a lheap \<Rightarrow> 'a lheap" where
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    60
"insert x t = merge (Node 1 Leaf x Leaf) t"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    61
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    62
fun del_min :: "'a::ord lheap \<Rightarrow> 'a lheap" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    63
"del_min Leaf = Leaf" |
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
    64
"del_min (Node n l x r) = merge l r"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    65
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    66
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    67
subsection "Lemmas"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    68
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    69
declare Let_def [simp]
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    70
64973
nipkow
parents: 64971
diff changeset
    71
lemma rk_eq_rank[simp]: "ltree t \<Longrightarrow> rk t = rank t"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    72
by(cases t) auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    73
64973
nipkow
parents: 64971
diff changeset
    74
lemma ltree_node: "ltree (node l a r) \<longleftrightarrow> ltree l \<and> ltree r"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    75
by(auto simp add: node_def)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    76
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    77
lemma heap_node: "heap (node l a r) \<longleftrightarrow>
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    78
  heap l \<and> heap r \<and> (\<forall>x \<in> set_mset(mset_tree l + mset_tree r). a \<le> x)"
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    79
by(auto simp add: node_def)
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
    80
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    81
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    82
subsection "Functional Correctness"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    83
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    84
locale Priority_Queue =
64971
nipkow
parents: 64969
diff changeset
    85
fixes empty :: "'q"
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
    86
and is_empty :: "'q \<Rightarrow> bool"
64971
nipkow
parents: 64969
diff changeset
    87
and insert :: "'a::linorder \<Rightarrow> 'q \<Rightarrow> 'q"
nipkow
parents: 64969
diff changeset
    88
and get_min :: "'q \<Rightarrow> 'a"
nipkow
parents: 64969
diff changeset
    89
and del_min :: "'q \<Rightarrow> 'q"
nipkow
parents: 64969
diff changeset
    90
and invar :: "'q \<Rightarrow> bool"
nipkow
parents: 64969
diff changeset
    91
and mset :: "'q \<Rightarrow> 'a multiset"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
    92
assumes mset_empty: "mset empty = {#}"
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
    93
and is_empty: "invar q \<Longrightarrow> is_empty q = (mset q = {#})"
64971
nipkow
parents: 64969
diff changeset
    94
and mset_insert: "invar q \<Longrightarrow> mset (insert x q) = mset q + {#x#}"
nipkow
parents: 64969
diff changeset
    95
and mset_del_min: "invar q \<Longrightarrow> mset (del_min q) = mset q - {#get_min q#}"
nipkow
parents: 64969
diff changeset
    96
and get_min: "invar q \<Longrightarrow> q \<noteq> empty \<Longrightarrow>
nipkow
parents: 64969
diff changeset
    97
   get_min q \<in> set_mset(mset q) \<and> (\<forall>x \<in># mset q. get_min q \<le> x)"
nipkow
parents: 64969
diff changeset
    98
and invar_insert: "invar q \<Longrightarrow> invar (insert x q)"
nipkow
parents: 64969
diff changeset
    99
and invar_del_min: "invar q \<Longrightarrow> invar (del_min q)"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   100
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   101
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   102
lemma mset_merge: "mset_tree (merge h1 h2) = mset_tree h1 + mset_tree h2"
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   103
by (induction h1 h2 rule: merge.induct) (auto simp add: node_def ac_simps)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   104
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   105
lemma mset_insert: "mset_tree (insert x t) = mset_tree t + {#x#}"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   106
by (auto simp add: insert_def mset_merge)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   107
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   108
lemma get_min:
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   109
  "heap h \<Longrightarrow> h \<noteq> Leaf \<Longrightarrow>
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   110
   get_min h \<in> set_mset(mset_tree h) \<and> (\<forall>x \<in># mset_tree h. get_min h \<le> x)"
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   111
by (induction h) (auto)
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   112
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   113
lemma mset_del_min: "mset_tree (del_min h) = mset_tree h - {# get_min h #}"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   114
by (cases h) (auto simp: mset_merge)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   115
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   116
lemma ltree_merge: "\<lbrakk> ltree l; ltree r \<rbrakk> \<Longrightarrow> ltree (merge l r)"
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   117
proof(induction l r rule: merge.induct)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   118
  case (3 n1 l1 a1 r1 n2 l2 a2 r2)
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   119
  show ?case (is "ltree(merge ?t1 ?t2)")
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   120
  proof cases
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   121
    assume "a1 \<le> a2"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   122
    hence "ltree (merge ?t1 ?t2) = ltree (node l1 a1 (merge r1 ?t2))" by simp
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   123
    also have "\<dots> = (ltree l1 \<and> ltree(merge r1 ?t2))"
64973
nipkow
parents: 64971
diff changeset
   124
      by(simp add: ltree_node)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   125
    also have "..." using "3.prems" "3.IH"(1)[OF `a1 \<le> a2`] by (simp)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   126
    finally show ?thesis .
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   127
  next (* analogous but automatic *)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   128
    assume "\<not> a1 \<le> a2"
64973
nipkow
parents: 64971
diff changeset
   129
    thus ?thesis using 3 by(simp)(auto simp: ltree_node)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   130
  qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   131
qed simp_all
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   132
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   133
lemma heap_merge: "\<lbrakk> heap l; heap r \<rbrakk> \<Longrightarrow> heap (merge l r)"
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   134
proof(induction l r rule: merge.induct)
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   135
  case 3 thus ?case by(auto simp: heap_node mset_merge ball_Un)
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   136
qed simp_all
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   137
64973
nipkow
parents: 64971
diff changeset
   138
lemma ltree_insert: "ltree t \<Longrightarrow> ltree(insert x t)"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   139
by(simp add: insert_def ltree_merge del: merge.simps split: tree.split)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   140
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   141
lemma heap_insert: "heap t \<Longrightarrow> heap(insert x t)"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   142
by(simp add: insert_def heap_merge del: merge.simps split: tree.split)
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   143
64973
nipkow
parents: 64971
diff changeset
   144
lemma ltree_del_min: "ltree t \<Longrightarrow> ltree(del_min t)"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   145
by(cases t)(auto simp add: ltree_merge simp del: merge.simps)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   146
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   147
lemma heap_del_min: "heap t \<Longrightarrow> heap(del_min t)"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   148
by(cases t)(auto simp add: heap_merge simp del: merge.simps)
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   149
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   150
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   151
interpretation lheap: Priority_Queue
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   152
where empty = Leaf and is_empty = "\<lambda>h. h = Leaf"
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   153
and insert = insert and del_min = del_min
64973
nipkow
parents: 64971
diff changeset
   154
and get_min = get_min and invar = "\<lambda>h. heap h \<and> ltree h"
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   155
and mset = mset_tree
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   156
proof(standard, goal_cases)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   157
  case 1 show ?case by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   158
next
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   159
  case (2 q) show ?case by (cases q) auto
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   160
next
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   161
  case 3 show ?case by(rule mset_insert)
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   162
next
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   163
  case 4 show ?case by(rule mset_del_min)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   164
next
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   165
  case 5 thus ?case by(simp add: get_min)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   166
next
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   167
  case 6 thus ?case by(simp add: heap_insert ltree_insert)
64969
a7ea55c1be52 proper priority queue spec
nipkow
parents: 62706
diff changeset
   168
next
64975
96b66d5c0fc1 added is_empty
nipkow
parents: 64973
diff changeset
   169
  case 7 thus ?case by(simp add: heap_del_min ltree_del_min)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   170
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   171
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   172
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   173
subsection "Complexity"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   174
64973
nipkow
parents: 64971
diff changeset
   175
lemma pow2_rank_size1: "ltree t \<Longrightarrow> 2 ^ rank t \<le> size1 t"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   176
proof(induction t)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   177
  case Leaf show ?case by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   178
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   179
  case (Node n l a r)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   180
  hence "rank r \<le> rank l" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   181
  hence *: "(2::nat) ^ rank r \<le> 2 ^ rank l" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   182
  have "(2::nat) ^ rank \<langle>n, l, a, r\<rangle> = 2 ^ rank r + 2 ^ rank r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   183
    by(simp add: mult_2)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   184
  also have "\<dots> \<le> size1 l + size1 r"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   185
    using Node * by (simp del: power_increasing_iff)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   186
  also have "\<dots> = size1 \<langle>n, l, a, r\<rangle>" by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   187
  finally show ?case .
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   188
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   189
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   190
function t_merge :: "'a::ord lheap \<Rightarrow> 'a lheap \<Rightarrow> nat" where
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   191
"t_merge Leaf t2 = 1" |
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   192
"t_merge t2 Leaf = 1" |
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   193
"t_merge (Node n1 l1 a1 r1) (Node n2 l2 a2 r2) =
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   194
  (if a1 \<le> a2 then 1 + t_merge r1 (Node n2 l2 a2 r2)
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   195
   else 1 + t_merge r2 (Node n1 l1 a1 r1))"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   196
by pat_completeness auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   197
termination by (relation "measure (%(t1,t2). rank t1 + rank t2)") auto
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   198
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   199
definition t_insert :: "'a::ord \<Rightarrow> 'a lheap \<Rightarrow> nat" where
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   200
"t_insert x t = t_merge (Node 1 Leaf x Leaf) t"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   201
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   202
fun t_del_min :: "'a::ord lheap \<Rightarrow> nat" where
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   203
"t_del_min Leaf = 1" |
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   204
"t_del_min (Node n l a r) = t_merge l r"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   205
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   206
lemma t_merge_rank: "t_merge l r \<le> rank l + rank r + 1"
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   207
proof(induction l r rule: merge.induct)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   208
  case 3 thus ?case
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   209
    by(simp)(fastforce split: tree.splits simp del: t_merge.simps)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   210
qed simp_all
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   211
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   212
corollary t_merge_log: assumes "ltree l" "ltree r"
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   213
  shows "t_merge l r \<le> log 2 (size1 l) + log 2 (size1 r) + 1"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   214
using le_log2_of_power[OF pow2_rank_size1[OF assms(1)]]
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   215
  le_log2_of_power[OF pow2_rank_size1[OF assms(2)]] t_merge_rank[of l r]
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   216
by linarith
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   217
64973
nipkow
parents: 64971
diff changeset
   218
corollary t_insert_log: "ltree t \<Longrightarrow> t_insert x t \<le> log 2 (size1 t) + 2"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   219
using t_merge_log[of "Node 1 Leaf x Leaf" t]
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   220
by(simp add: t_insert_def split: tree.split)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   221
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   222
lemma ld_ld_1_less:
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   223
  assumes "x > 0" "y > 0" shows "1 + log 2 x + log 2 y < 2 * log 2 (x+y)"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   224
proof -
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   225
  have 1: "2*x*y < (x+y)^2" using assms
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   226
    by(simp add: numeral_eq_Suc algebra_simps add_pos_pos)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   227
  show ?thesis
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   228
    apply(rule powr_less_cancel_iff[of 2, THEN iffD1])
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   229
     apply simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   230
    using assms 1 by(simp add: powr_add log_powr[symmetric] powr_numeral)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   231
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   232
64973
nipkow
parents: 64971
diff changeset
   233
corollary t_del_min_log: assumes "ltree t"
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   234
  shows "t_del_min t \<le> 2 * log 2 (size1 t) + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   235
proof(cases t)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   236
  case Leaf thus ?thesis using assms by simp
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   237
next
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   238
  case [simp]: (Node _ t1 _ t2)
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   239
  have "t_del_min t = t_merge t1 t2" by simp
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   240
  also have "\<dots> \<le> log 2 (size1 t1) + log 2 (size1 t2) + 1"
64976
1a4cb9403a10 renaming
nipkow
parents: 64975
diff changeset
   241
    using \<open>ltree t\<close> by (auto simp: t_merge_log simp del: t_merge.simps)
62706
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   242
  also have "\<dots> \<le> 2 * log 2 (size1 t) + 1"
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   243
    using ld_ld_1_less[of "size1 t1" "size1 t2"] by (simp)
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   244
  finally show ?thesis .
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   245
qed
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   246
49c6a54ceab6 added Leftist_Heap
nipkow
parents:
diff changeset
   247
end