src/HOL/Data_Structures/Tree_Set.thy
author nipkow
Sat Apr 21 08:41:42 2018 +0200 (14 months ago)
changeset 68020 6aade817bee5
parent 67965 aaa31cd0caef
child 68431 b294e095f64c
permissions -rw-r--r--
del_min -> split_min
nipkow@61640
     1
(* Author: Tobias Nipkow *)
nipkow@61640
     2
nipkow@63411
     3
section \<open>Unbalanced Tree Implementation of Set\<close>
nipkow@61640
     4
nipkow@61640
     5
theory Tree_Set
nipkow@61640
     6
imports
wenzelm@66453
     7
  "HOL-Library.Tree"
nipkow@61640
     8
  Cmp
nipkow@67965
     9
  Set_Specs
nipkow@61640
    10
begin
nipkow@61640
    11
nipkow@63411
    12
fun isin :: "'a::linorder tree \<Rightarrow> 'a \<Rightarrow> bool" where
nipkow@61640
    13
"isin Leaf x = False" |
nipkow@61640
    14
"isin (Node l a r) x =
nipkow@61678
    15
  (case cmp x a of
nipkow@61678
    16
     LT \<Rightarrow> isin l x |
nipkow@61678
    17
     EQ \<Rightarrow> True |
nipkow@61678
    18
     GT \<Rightarrow> isin r x)"
nipkow@61640
    19
nipkow@61640
    20
hide_const (open) insert
nipkow@61640
    21
nipkow@63411
    22
fun insert :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
nipkow@61640
    23
"insert x Leaf = Node Leaf x Leaf" |
nipkow@61678
    24
"insert x (Node l a r) =
nipkow@61678
    25
  (case cmp x a of
nipkow@61678
    26
     LT \<Rightarrow> Node (insert x l) a r |
nipkow@61678
    27
     EQ \<Rightarrow> Node l a r |
nipkow@61678
    28
     GT \<Rightarrow> Node l a (insert x r))"
nipkow@61640
    29
nipkow@68020
    30
fun split_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
nipkow@68020
    31
"split_min (Node l a r) =
nipkow@68020
    32
  (if l = Leaf then (a,r) else let (x,l') = split_min l in (x, Node l' a r))"
nipkow@61640
    33
nipkow@63411
    34
fun delete :: "'a::linorder \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
nipkow@61640
    35
"delete x Leaf = Leaf" |
nipkow@61678
    36
"delete x (Node l a r) =
nipkow@61678
    37
  (case cmp x a of
nipkow@61678
    38
     LT \<Rightarrow>  Node (delete x l) a r |
nipkow@61678
    39
     GT \<Rightarrow>  Node l a (delete x r) |
nipkow@68020
    40
     EQ \<Rightarrow> if r = Leaf then l else let (a',r') = split_min r in Node l a' r')"
nipkow@61640
    41
nipkow@61640
    42
nipkow@61640
    43
subsection "Functional Correctness Proofs"
nipkow@61640
    44
nipkow@67929
    45
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> set (inorder t))"
nipkow@67929
    46
by (induction t) (auto simp: isin_simps)
nipkow@61640
    47
nipkow@61640
    48
lemma inorder_insert:
nipkow@61640
    49
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61640
    50
by(induction t) (auto simp: ins_list_simps)
nipkow@61640
    51
nipkow@61640
    52
nipkow@68020
    53
lemma split_minD:
nipkow@68020
    54
  "split_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> x # inorder t' = inorder t"
nipkow@68020
    55
by(induction t arbitrary: t' rule: split_min.induct)
nipkow@61647
    56
  (auto simp: sorted_lems split: prod.splits if_splits)
nipkow@61640
    57
nipkow@61640
    58
lemma inorder_delete:
nipkow@61640
    59
  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@68020
    60
by(induction t) (auto simp: del_list_simps split_minD split: prod.splits)
nipkow@61640
    61
nipkow@61640
    62
interpretation Set_by_Ordered
nipkow@61640
    63
where empty = Leaf and isin = isin and insert = insert and delete = delete
nipkow@61640
    64
and inorder = inorder and inv = "\<lambda>_. True"
nipkow@61640
    65
proof (standard, goal_cases)
nipkow@61640
    66
  case 1 show ?case by simp
nipkow@61640
    67
next
nipkow@61640
    68
  case 2 thus ?case by(simp add: isin_set)
nipkow@61640
    69
next
nipkow@61640
    70
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@61640
    71
next
nipkow@61640
    72
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@61640
    73
qed (rule TrueI)+
nipkow@61640
    74
nipkow@61640
    75
end