src/HOL/Data_Structures/List_Ins_Del.thy
author nipkow
Wed, 18 Nov 2015 08:54:58 +0100
changeset 61696 ce6320b9ef9b
parent 61692 cb595e12451d
child 66441 b9468503742a
permissions -rw-r--r--
moved lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     1
(* Author: Tobias Nipkow *)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     3
section {* List Insertion and Deletion *}
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     4
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     5
theory List_Ins_Del
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     6
imports Sorted_Less
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     7
begin
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     8
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
     9
subsection \<open>Elements in a list\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    10
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    11
fun elems :: "'a list \<Rightarrow> 'a set" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    12
"elems [] = {}" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    13
"elems (x#xs) = Set.insert x (elems xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    14
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    15
lemma elems_app: "elems (xs @ ys) = (elems xs \<union> elems ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    16
by (induction xs) auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    17
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    18
lemma elems_eq_set: "elems xs = set xs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    19
by (induction xs) auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    20
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    21
lemma sorted_Cons_iff:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    22
  "sorted(x # xs) = (sorted xs \<and> (\<forall>y \<in> elems xs. x < y))"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    23
by(simp add: elems_eq_set Sorted_Less.sorted_Cons_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    24
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    25
lemma sorted_snoc_iff:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    26
  "sorted(xs @ [x]) = (sorted xs \<and> (\<forall>y \<in> elems xs. y < x))"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    27
by(simp add: elems_eq_set Sorted_Less.sorted_snoc_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    28
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    29
text{* The above two rules introduce quantifiers. It turns out
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    30
that in practice this is not a problem because of the simplicity of
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    31
the "isin" functions that implement @{const elems}. Nevertheless
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    32
it is possible to avoid the quantifiers with the help of some rewrite rules: *}
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    33
61692
cb595e12451d removed lemmas that were only needed for old version of isin.
nipkow
parents: 61642
diff changeset
    34
lemma sorted_ConsD: "sorted (y # xs) \<Longrightarrow> x \<le> y \<Longrightarrow> x \<notin> elems xs"
cb595e12451d removed lemmas that were only needed for old version of isin.
nipkow
parents: 61642
diff changeset
    35
by (auto simp: sorted_Cons_iff)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    36
61692
cb595e12451d removed lemmas that were only needed for old version of isin.
nipkow
parents: 61642
diff changeset
    37
lemma sorted_snocD: "sorted (xs @ [y]) \<Longrightarrow> y \<le> x \<Longrightarrow> x \<notin> elems xs"
cb595e12451d removed lemmas that were only needed for old version of isin.
nipkow
parents: 61642
diff changeset
    38
by (auto simp: sorted_snoc_iff)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    39
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    40
lemmas elems_simps = sorted_lems elems_app
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    41
lemmas elems_simps1 = elems_simps sorted_Cons_iff sorted_snoc_iff
61692
cb595e12451d removed lemmas that were only needed for old version of isin.
nipkow
parents: 61642
diff changeset
    42
lemmas elems_simps2 = elems_simps sorted_ConsD sorted_snocD
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    43
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    44
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    45
subsection \<open>Inserting into an ordered list without duplicates:\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    46
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    47
fun ins_list :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    48
"ins_list x [] = [x]" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    49
"ins_list x (a#xs) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    50
  (if x < a then x#a#xs else if x=a then a#xs else a # ins_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    51
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    52
lemma set_ins_list: "elems (ins_list x xs) = insert x (elems xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    53
by(induction xs) auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    54
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    55
lemma distinct_if_sorted: "sorted xs \<Longrightarrow> distinct xs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    56
apply(induction xs rule: sorted.induct)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    57
apply auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    58
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    59
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    60
lemma sorted_ins_list: "sorted xs \<Longrightarrow> sorted(ins_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    61
by(induction xs rule: sorted.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    62
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    63
lemma ins_list_sorted: "sorted (xs @ [a]) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    64
  ins_list x (xs @ a # ys) =
61642
nipkow
parents: 61640
diff changeset
    65
  (if x < a then ins_list x xs @ (a#ys) else xs @ ins_list x (a#ys))"
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    66
by(induction xs) (auto simp: sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    67
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    68
text\<open>In principle, @{thm ins_list_sorted} suffices, but the following two
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    69
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    70
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    71
corollary ins_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    72
  ins_list x (xs @ a # ys) = xs @ ins_list x (a#ys)"
61642
nipkow
parents: 61640
diff changeset
    73
by(auto simp add: ins_list_sorted)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    74
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    75
corollary ins_list_sorted2: "sorted (xs @ [a]) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    76
  ins_list x (xs @ a # ys) = ins_list x xs @ (a#ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    77
by(auto simp: ins_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    78
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    79
lemmas ins_list_simps = sorted_lems ins_list_sorted1 ins_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    80
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    81
text\<open>Splay trees need two additional @{const ins_list} lemmas:\<close>
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    82
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    83
lemma ins_list_Cons: "sorted (x # xs) \<Longrightarrow> ins_list x xs = x # xs"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    84
by (induction xs) auto
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    85
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    86
lemma ins_list_snoc: "sorted (xs @ [x]) \<Longrightarrow> ins_list x xs = xs @ [x]"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    87
by(induction xs) (auto simp add: sorted_mid_iff2)
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    88
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    89
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    90
subsection \<open>Delete one occurrence of an element from a list:\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    91
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    92
fun del_list :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    93
"del_list x [] = []" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    94
"del_list x (a#xs) = (if x=a then xs else a # del_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    95
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    96
lemma del_list_idem: "x \<notin> elems xs \<Longrightarrow> del_list x xs = xs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    97
by (induct xs) simp_all
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    98
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    99
lemma elems_del_list_eq:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   100
  "distinct xs \<Longrightarrow> elems (del_list x xs) = elems xs - {x}"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   101
apply(induct xs)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   102
 apply simp
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   103
apply (simp add: elems_eq_set)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   104
apply blast
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   105
done
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   106
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   107
lemma sorted_del_list: "sorted xs \<Longrightarrow> sorted(del_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   108
apply(induction xs rule: sorted.induct)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   109
apply auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   110
by (meson order.strict_trans sorted_Cons_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   111
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   112
lemma del_list_sorted: "sorted (xs @ a # ys) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   113
  del_list x (xs @ a # ys) = (if x < a then del_list x xs @ a # ys else xs @ del_list x (a # ys))"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   114
by(induction xs)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   115
  (fastforce simp: sorted_lems sorted_Cons_iff elems_eq_set intro!: del_list_idem)+
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   116
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   117
text\<open>In principle, @{thm del_list_sorted} suffices, but the following
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   118
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   119
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   120
corollary del_list_sorted1: "sorted (xs @ a # ys) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   121
  del_list x (xs @ a # ys) = xs @ del_list x (a # ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   122
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   123
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   124
corollary del_list_sorted2: "sorted (xs @ a # ys) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   125
  del_list x (xs @ a # ys) = del_list x xs @ a # ys"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   126
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   127
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   128
corollary del_list_sorted3:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   129
  "sorted (xs @ a # ys @ b # zs) \<Longrightarrow> x < b \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   130
  del_list x (xs @ a # ys @ b # zs) = del_list x (xs @ a # ys) @ b # zs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   131
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   132
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   133
corollary del_list_sorted4:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   134
  "sorted (xs @ a # ys @ b # zs @ c # us) \<Longrightarrow> x < c \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   135
  del_list x (xs @ a # ys @ b # zs @ c # us) = del_list x (xs @ a # ys @ b # zs) @ c # us"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   136
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   137
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   138
corollary del_list_sorted5:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   139
  "sorted (xs @ a # ys @ b # zs @ c # us @ d # vs) \<Longrightarrow> x < d \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   140
   del_list x (xs @ a # ys @ b # zs @ c # us @ d # vs) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   141
   del_list x (xs @ a # ys @ b # zs @ c # us) @ d # vs" 
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   142
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   143
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   144
lemmas del_list_simps = sorted_lems
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   145
  del_list_sorted1
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   146
  del_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   147
  del_list_sorted3
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   148
  del_list_sorted4
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   149
  del_list_sorted5
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   150
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   151
text\<open>Splay trees need two additional @{const del_list} lemmas:\<close>
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   152
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   153
lemma del_list_notin_Cons: "sorted (x # xs) \<Longrightarrow> del_list x xs = xs"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   154
by(induction xs)(auto simp: sorted_Cons_iff)
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   155
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   156
lemma del_list_sorted_app:
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   157
  "sorted(xs @ [x]) \<Longrightarrow> del_list x (xs @ ys) = xs @ del_list x ys"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   158
by (induction xs) (auto simp: sorted_mid_iff2)
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   159
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   160
end