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