src/HOL/Data_Structures/Tree_Set.thy
author nipkow
Fri, 13 Nov 2015 12:06:50 +0100
changeset 61647 5121b9a57cce
parent 61640 44c9198f210c
child 61651 415e816d3f37
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     1
(* Author: Tobias Nipkow *)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     2
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     3
section {* Tree Implementation of Sets *}
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     4
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     5
theory Tree_Set
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     6
imports
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     7
  "~~/src/HOL/Library/Tree"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     8
  Cmp
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
     9
  Set_by_Ordered
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    10
begin
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    11
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    12
fun isin :: "'a::cmp tree \<Rightarrow> 'a \<Rightarrow> bool" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    13
"isin Leaf x = False" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    14
"isin (Node l a r) x =
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    15
  (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    16
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    17
hide_const (open) insert
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    18
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    19
fun insert :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    20
"insert x Leaf = Node Leaf x Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    21
"insert x (Node l a r) = (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    22
      LT \<Rightarrow> Node (insert x l) a r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    23
      EQ \<Rightarrow> Node l a r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    24
      GT \<Rightarrow> Node l a (insert x r))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    25
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    26
fun del_min :: "'a tree \<Rightarrow> 'a * 'a tree" where
61647
nipkow
parents: 61640
diff changeset
    27
"del_min (Node l a r) = (if l = Leaf then (a,r)
nipkow
parents: 61640
diff changeset
    28
  else let (x,l') = del_min l in (x, Node l' a r))"
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    29
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    30
fun delete :: "'a::cmp \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    31
"delete x Leaf = Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    32
"delete x (Node l a r) = (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    33
  LT \<Rightarrow>  Node (delete x l) a r |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    34
  GT \<Rightarrow>  Node l a (delete x r) |
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    35
  EQ \<Rightarrow> if r = Leaf then l else let (a',r') = del_min r in Node l a' r')"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    36
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    37
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    38
subsection "Functional Correctness Proofs"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    39
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    40
lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    41
by (induction t) (auto simp: elems_simps1)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    42
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    43
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    44
by (induction t) (auto simp: elems_simps2)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    45
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    46
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    47
lemma inorder_insert:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    48
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    49
by(induction t) (auto simp: ins_list_simps)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    50
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    51
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    52
lemma del_minD:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    53
  "del_min t = (x,t') \<Longrightarrow> t \<noteq> Leaf \<Longrightarrow> sorted(inorder t) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    54
   x # inorder t' = inorder t"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    55
by(induction t arbitrary: t' rule: del_min.induct)
61647
nipkow
parents: 61640
diff changeset
    56
  (auto simp: sorted_lems split: prod.splits if_splits)
61640
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    57
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    58
lemma inorder_delete:
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    59
  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    60
by(induction t) (auto simp: del_list_simps del_minD split: prod.splits)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    61
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    62
interpretation Set_by_Ordered
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    63
where empty = Leaf and isin = isin and insert = insert and delete = delete
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    64
and inorder = inorder and inv = "\<lambda>_. True"
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    65
proof (standard, goal_cases)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    66
  case 1 show ?case by simp
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    67
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    68
  case 2 thus ?case by(simp add: isin_set)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    69
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    70
  case 3 thus ?case by(simp add: inorder_insert)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    71
next
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    72
  case 4 thus ?case by(simp add: inorder_delete)
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    73
qed (rule TrueI)+
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    74
44c9198f210c no CRLF
nipkow
parents: 61588
diff changeset
    75
end