src/HOL/Data_Structures/Tree_Set.thy
author nipkow
Fri Nov 13 12:06:50 2015 +0100 (2015-11-13)
changeset 61647 5121b9a57cce
parent 61640 44c9198f210c
child 61651 415e816d3f37
permissions -rw-r--r--
tuned
nipkow@61640
     1
(* Author: Tobias Nipkow *)
nipkow@61640
     2
nipkow@61640
     3
section {* Tree Implementation of Sets *}
nipkow@61640
     4
nipkow@61640
     5
theory Tree_Set
nipkow@61640
     6
imports
nipkow@61640
     7
  "~~/src/HOL/Library/Tree"
nipkow@61640
     8
  Cmp
nipkow@61640
     9
  Set_by_Ordered
nipkow@61640
    10
begin
nipkow@61640
    11
nipkow@61640
    12
fun isin :: "'a::cmp tree \<Rightarrow> 'a \<Rightarrow> bool" where
nipkow@61640
    13
"isin Leaf x = False" |
nipkow@61640
    14
"isin (Node l a r) x =
nipkow@61640
    15
  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)"
nipkow@61640
    16
nipkow@61640
    17
hide_const (open) insert
nipkow@61640
    18
nipkow@61640
    19
fun insert :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
nipkow@61640
    20
"insert x Leaf = Node Leaf x Leaf" |
nipkow@61640
    21
"insert x (Node l a r) = (case cmp x a of
nipkow@61640
    22
      LT \<Rightarrow> Node (insert x l) a r |
nipkow@61640
    23
      EQ \<Rightarrow> Node l a r |
nipkow@61640
    24
      GT \<Rightarrow> Node l a (insert x r))"
nipkow@61640
    25
nipkow@61640
    26
fun del_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
nipkow@61647
    27
"del_min (Node l a r) = (if l = Leaf then (a,r)
nipkow@61647
    28
  else let (x,l') = del_min l in (x, Node l' a r))"
nipkow@61640
    29
nipkow@61640
    30
fun delete :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
nipkow@61640
    31
"delete x Leaf = Leaf" |
nipkow@61640
    32
"delete x (Node l a r) = (case cmp x a of
nipkow@61640
    33
  LT \<Rightarrow>  Node (delete x l) a r |
nipkow@61640
    34
  GT \<Rightarrow>  Node l a (delete x r) |
nipkow@61640
    35
  EQ \<Rightarrow> if r = Leaf then l else let (a',r') = del_min r in Node l a' r')"
nipkow@61640
    36
nipkow@61640
    37
nipkow@61640
    38
subsection "Functional Correctness Proofs"
nipkow@61640
    39
nipkow@61640
    40
lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
nipkow@61640
    41
by (induction t) (auto simp: elems_simps1)
nipkow@61640
    42
nipkow@61640
    43
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
nipkow@61640
    44
by (induction t) (auto simp: elems_simps2)
nipkow@61640
    45
nipkow@61640
    46
nipkow@61640
    47
lemma inorder_insert:
nipkow@61640
    48
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61640
    49
by(induction t) (auto simp: ins_list_simps)
nipkow@61640
    50
nipkow@61640
    51
nipkow@61640
    52
lemma del_minD:
nipkow@61640
    53
  "del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> sorted(inorder t) \<Longrightarrow>
nipkow@61640
    54
   x # inorder t' = inorder t"
nipkow@61640
    55
by(induction t arbitrary: t' rule: del_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@61640
    60
by(induction t) (auto simp: del_list_simps del_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