src/HOL/Data_Structures/Splay_Map.thy
author nipkow
Thu, 07 Jul 2016 18:08:02 +0200
changeset 63411 e051eea34990
parent 61696 ce6320b9ef9b
permissions -rw-r--r--
got rid of class cmp; added height-size proofs by Daniel Stuewe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61525
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     2
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     3
section "Splay Tree Implementation of Maps"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     4
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     5
theory Splay_Map
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     6
imports
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     7
  Splay_Set
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     8
  Map_by_Ordered
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
     9
begin
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    10
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    11
function splay :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    12
"splay x Leaf = Leaf" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    13
"x = fst a \<Longrightarrow> splay x (Node t1 a t2) = Node t1 a t2" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    14
"x = fst a \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node (Node t1 a t2) b t3) = Node t1 a (Node t2 b t3)" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    15
"x < fst a \<Longrightarrow> splay x (Node Leaf a t) = Node Leaf a t" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    16
"x < fst a \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node (Node Leaf a t1) b t2) = Node Leaf a (Node t1 b t2)" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    17
"x < fst a \<Longrightarrow> x < fst b \<Longrightarrow> t1 \<noteq> Leaf \<Longrightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    18
 splay x (Node (Node t1 a t2) b t3) =
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    19
 (case splay x t1 of Node t11 y t12 \<Rightarrow> Node t11 y (Node t12 a (Node t2 b t3)))" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    20
"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node (Node t1 a Leaf) b t2) = Node t1 a (Node Leaf b t2)" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    21
"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow> t2 \<noteq> Leaf \<Longrightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    22
 splay x (Node (Node t1 a t2) b t3) =
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    23
 (case splay x t2 of Node t21 y t22 \<Rightarrow> Node (Node t1 a t21) y (Node t22 b t3))" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    24
"fst a < x \<Longrightarrow> x = fst b \<Longrightarrow> splay x (Node t1 a (Node t2 b t3)) = Node (Node t1 a t2) b t3" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    25
"fst a < x \<Longrightarrow> splay x (Node t a Leaf) = Node t a Leaf" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    26
"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow>  t2 \<noteq> Leaf \<Longrightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    27
 splay x (Node t1 a (Node t2 b t3)) =
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    28
 (case splay x t2 of Node t21 y t22 \<Rightarrow> Node (Node t1 a t21) y (Node t22 b t3))" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    29
"fst a < x \<Longrightarrow> x < fst b \<Longrightarrow> splay x (Node t1 a (Node Leaf b t2)) = Node (Node t1 a Leaf) b t2" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    30
"fst a < x \<Longrightarrow> fst b < x \<Longrightarrow> splay x (Node t1 a (Node t2 b Leaf)) = Node (Node t1 a t2) b Leaf" |
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    31
"fst a < x \<Longrightarrow> fst b < x \<Longrightarrow> t3 \<noteq> Leaf \<Longrightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    32
 splay x (Node t1 a (Node t2 b t3)) =
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    33
 (case splay x t3 of Node t31 y t32 \<Rightarrow> Node (Node (Node t1 a t2) b t31) y t32)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    34
apply(atomize_elim)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    35
apply(auto)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    36
(* 1 subgoal *)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    37
apply (subst (asm) neq_Leaf_iff)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    38
apply(auto)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    39
apply (metis tree.exhaust surj_pair less_linear)+
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    40
done
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    41
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    42
termination splay
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    43
by lexicographic_order
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    44
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 61696
diff changeset
    45
lemma splay_code: "splay (x::_::linorder) t = (case t of Leaf \<Rightarrow> Leaf |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    46
  Node al a ar \<Rightarrow> (case cmp x (fst a) of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    47
    EQ \<Rightarrow> t |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    48
    LT \<Rightarrow> (case al of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    49
      Leaf \<Rightarrow> t |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    50
      Node bl b br \<Rightarrow> (case cmp x (fst b) of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    51
        EQ \<Rightarrow> Node bl b (Node br a ar) |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    52
        LT \<Rightarrow> if bl = Leaf then Node bl b (Node br a ar)
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    53
              else case splay x bl of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    54
                Node bll y blr \<Rightarrow> Node bll y (Node blr b (Node br a ar)) |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    55
        GT \<Rightarrow> if br = Leaf then Node bl b (Node br a ar)
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    56
              else case splay x br of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    57
                Node brl y brr \<Rightarrow> Node (Node bl b brl) y (Node brr a ar))) |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    58
    GT \<Rightarrow> (case ar of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    59
      Leaf \<Rightarrow> t |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    60
      Node bl b br \<Rightarrow> (case cmp x (fst b) of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    61
        EQ \<Rightarrow> Node (Node al a bl) b br |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    62
        LT \<Rightarrow> if bl = Leaf then Node (Node al a bl) b br
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    63
              else case splay x bl of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    64
                Node bll y blr \<Rightarrow> Node (Node al a bll) y (Node blr b br) |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    65
        GT \<Rightarrow> if br=Leaf then Node (Node al a bl) b br
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    66
              else case splay x br of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    67
                Node bll y blr \<Rightarrow> Node (Node (Node al a bl) b bll) y blr))))"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61525
diff changeset
    68
by(auto cong: case_tree_cong split: tree.split)
61525
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    69
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    70
definition lookup :: "('a*'b)tree \<Rightarrow> 'a::linorder \<Rightarrow> 'b option" where "lookup t x =
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    71
  (case splay x t of Leaf \<Rightarrow> None | Node _ (a,b) _ \<Rightarrow> if x=a then Some b else None)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    72
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    73
hide_const (open) insert
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    74
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    75
fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    76
"update x y t =  (if t = Leaf then Node Leaf (x,y) Leaf
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    77
  else case splay x t of
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    78
    Node l a r \<Rightarrow> if x = fst a then Node l (x,y) r
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    79
      else if x < fst a then Node l (x,y) (Node Leaf a r) else Node (Node l a Leaf) (x,y) r)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    80
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    81
definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree \<Rightarrow> ('a*'b) tree" where
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    82
"delete x t = (if t = Leaf then Leaf
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    83
  else case splay x t of Node l a r \<Rightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    84
    if x = fst a
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    85
    then if l = Leaf then r else case splay_max l of Node l' m r' \<Rightarrow> Node l' m r
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    86
    else Node l a r)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    87
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    88
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    89
subsection "Functional Correctness Proofs"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    90
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    91
lemma splay_Leaf_iff: "(splay x t = Leaf) = (t = Leaf)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    92
by(induction x t rule: splay.induct) (auto split: tree.splits)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    93
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    94
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    95
subsubsection "Proofs for lookup"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    96
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    97
lemma splay_map_of_inorder:
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    98
  "splay x t = Node l a r \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
    99
   map_of (inorder t) x = (if x = fst a then Some(snd a) else None)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   100
by(induction x t arbitrary: l a r rule: splay.induct)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   101
  (auto simp: map_of_simps splay_Leaf_iff split: tree.splits)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   102
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   103
lemma lookup_eq:
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   104
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   105
by(auto simp: lookup_def splay_Leaf_iff splay_map_of_inorder split: tree.split)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   106
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   107
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   108
subsubsection "Proofs for update"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   109
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   110
lemma inorder_splay: "inorder(splay x t) = inorder t"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   111
by(induction x t rule: splay.induct)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   112
  (auto simp: neq_Leaf_iff split: tree.split)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   113
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   114
lemma sorted_splay:
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   115
  "sorted1(inorder t) \<Longrightarrow> splay x t = Node l a r \<Longrightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   116
  sorted(map fst (inorder l) @ x # map fst (inorder r))"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   117
unfolding inorder_splay[of x t, symmetric]
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   118
by(induction x t arbitrary: l a r rule: splay.induct)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   119
  (auto simp: sorted_lems sorted_Cons_le sorted_snoc_le splay_Leaf_iff split: tree.splits)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   120
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   121
lemma inorder_update:
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   122
  "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   123
using inorder_splay[of x t, symmetric] sorted_splay[of t x]
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   124
by(auto simp: upd_list_simps upd_list_Cons upd_list_snoc neq_Leaf_iff split: tree.split)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   125
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   126
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   127
subsubsection "Proofs for delete"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   128
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   129
lemma inorder_splay_maxD:
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   130
  "splay_max t = Node l a r \<Longrightarrow> sorted1(inorder t) \<Longrightarrow>
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   131
  inorder l @ [a] = inorder t \<and> r = Leaf"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   132
by(induction t arbitrary: l a r rule: splay_max.induct)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   133
  (auto simp: sorted_lems splay_max_Leaf_iff split: tree.splits if_splits)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   134
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   135
lemma inorder_delete:
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   136
  "sorted1(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   137
using inorder_splay[of x t, symmetric] sorted_splay[of t x]
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   138
by (auto simp: del_list_simps del_list_sorted_app delete_def
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   139
  del_list_notin_Cons inorder_splay_maxD splay_Leaf_iff splay_max_Leaf_iff
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   140
  split: tree.splits)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   141
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   142
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   143
subsubsection "Overall Correctness"
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   144
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   145
interpretation Map_by_Ordered
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   146
where empty = Leaf and lookup = lookup and update = update
61686
e6784939d645 tuned names
nipkow
parents: 61581
diff changeset
   147
and delete = delete and inorder = inorder and inv = "\<lambda>_. True"
61525
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   148
proof (standard, goal_cases)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   149
  case 2 thus ?case by(simp add: lookup_eq)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   150
next
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   151
  case 3 thus ?case by(simp add: inorder_update del: update.simps)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   152
next
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   153
  case 4 thus ?case by(simp add: inorder_delete)
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   154
qed auto
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   155
87244a9cfe40 added splay trees
nipkow
parents:
diff changeset
   156
end