src/HOL/Data_Structures/Heaps.thy
author nipkow
Sat, 28 Dec 2024 18:03:41 +0100
changeset 81680 88feb0047d7c
parent 72688 8cb82e7f1743
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72688
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     2
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     3
section \<open>Heaps\<close>
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     4
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     5
theory Heaps
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     6
imports
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     7
  "HOL-Library.Tree_Multiset"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     8
  Priority_Queue_Specs
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
     9
begin
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    10
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    11
text \<open>Heap = priority queue on trees:\<close>
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    12
    
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    13
locale Heap =
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    14
fixes insert :: "('a::linorder) \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    15
and  del_min :: "'a tree \<Rightarrow> 'a tree"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    16
assumes mset_insert: "heap q \<Longrightarrow> mset_tree (insert x q) = {#x#} + mset_tree q"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    17
and    mset_del_min: "\<lbrakk> heap q;  q \<noteq> Leaf \<rbrakk> \<Longrightarrow> mset_tree (del_min q) = mset_tree q - {#value q#}"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    18
and     heap_insert: "heap q \<Longrightarrow> heap(insert x q)"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    19
and    heap_del_min: "heap q \<Longrightarrow> heap(del_min q)"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    20
begin
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    21
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    22
definition empty :: "'a tree" where
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    23
"empty = Leaf"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    24
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    25
fun is_empty :: "'a tree \<Rightarrow> bool" where
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    26
"is_empty t = (t = Leaf)"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    27
81680
nipkow
parents: 72688
diff changeset
    28
fun get_min :: "'a tree \<Rightarrow> 'a" where
nipkow
parents: 72688
diff changeset
    29
"get_min (Node l a r) = a"
nipkow
parents: 72688
diff changeset
    30
72688
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    31
sublocale Priority_Queue where empty = empty and is_empty = is_empty and insert = insert
81680
nipkow
parents: 72688
diff changeset
    32
and get_min = get_min and del_min = del_min and invar = heap and mset = mset_tree
72688
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    33
proof (standard, goal_cases)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    34
  case 1 thus ?case by (simp add: empty_def)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    35
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    36
  case 2 thus ?case by(auto)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    37
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    38
  case 3 thus ?case by(simp add: mset_insert)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    39
next
81680
nipkow
parents: 72688
diff changeset
    40
  case 4 thus ?case by(auto simp add: mset_del_min neq_Leaf_iff)
72688
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    41
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    42
  case 5 thus ?case by(auto simp: neq_Leaf_iff Min_insert2 simp del: Un_iff)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    43
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    44
  case 6 thus ?case by(simp add: empty_def)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    45
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    46
  case 7 thus ?case by(simp add: heap_insert)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    47
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    48
  case 8 thus ?case by(simp add: heap_del_min)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    49
qed
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    50
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    51
end
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    52
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    53
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    54
text \<open>Once you have \<open>merge\<close>, \<open>insert\<close> and \<open>del_min\<close> are easy:\<close>
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    55
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    56
locale Heap_Merge =
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    57
fixes merge :: "'a::linorder tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    58
assumes mset_merge: "\<lbrakk> heap q1; heap q2 \<rbrakk> \<Longrightarrow> mset_tree (merge q1 q2) = mset_tree q1 + mset_tree q2"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    59
and invar_merge: "\<lbrakk> heap q1; heap q2 \<rbrakk> \<Longrightarrow> heap (merge q1 q2)"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    60
begin
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    61
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    62
fun insert :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    63
"insert x t = merge (Node Leaf x Leaf) t"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    64
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    65
fun del_min :: "'a tree \<Rightarrow> 'a tree" where
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    66
"del_min Leaf = Leaf" |
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    67
"del_min (Node l x r) = merge l r"
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    68
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    69
interpretation Heap insert del_min
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    70
proof(standard, goal_cases)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    71
  case 1 thus ?case by(simp add:mset_merge)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    72
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    73
  case (2 q) thus ?case by(cases q)(auto simp: mset_merge)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    74
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    75
  case 3 thus ?case by (simp add: invar_merge)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    76
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    77
  case (4 q) thus ?case by (cases q)(auto simp: invar_merge)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    78
qed
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    79
81680
nipkow
parents: 72688
diff changeset
    80
lemmas local_empty_def = local.empty_def
nipkow
parents: 72688
diff changeset
    81
lemmas local_get_min_def = local.get_min.simps
nipkow
parents: 72688
diff changeset
    82
72688
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    83
sublocale PQM: Priority_Queue_Merge where empty = empty and is_empty = is_empty and insert = insert
81680
nipkow
parents: 72688
diff changeset
    84
and get_min = get_min and del_min = del_min and invar = heap and mset = mset_tree and merge = merge
72688
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    85
proof(standard, goal_cases)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    86
  case 1 thus ?case by (simp add: mset_merge)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    87
next
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    88
  case 2 thus ?case by (simp add: invar_merge)
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    89
qed
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    90
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    91
end
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    92
8cb82e7f1743 proper defn of a heap
nipkow
parents:
diff changeset
    93
end