src/HOL/Data_Structures/List_Ins_Del.thy
author wenzelm
Sat, 28 Nov 2020 15:15:53 +0100
changeset 72755 8dffbe01a3e1
parent 70631 f14b997da756
permissions -rw-r--r--
support for Scala compile-time positions;
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
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67168
diff changeset
     3
section \<open>List Insertion and Deletion\<close>
61640
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
lemma sorted_Cons_iff:
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    12
  "sorted(x # xs) = ((\<forall>y \<in> set xs. x < y) \<and> sorted xs)"
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    13
by(simp add: sorted_wrt_Cons)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    14
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    15
lemma sorted_snoc_iff:
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    16
  "sorted(xs @ [x]) = (sorted xs \<and> (\<forall>y \<in> set xs. y < x))"
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    17
by(simp add: sorted_wrt_append)
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    18
(*
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67168
diff changeset
    19
text\<open>The above two rules introduce quantifiers. It turns out
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    20
that in practice this is not a problem because of the simplicity of
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    21
the "isin" functions that implement @{const set}. Nevertheless
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 67168
diff changeset
    22
it is possible to avoid the quantifiers with the help of some rewrite rules:\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    23
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    24
lemma sorted_ConsD: "sorted (y # xs) \<Longrightarrow> x \<le> y \<Longrightarrow> x \<notin> set xs"
61692
cb595e12451d removed lemmas that were only needed for old version of isin.
nipkow
parents: 61642
diff changeset
    25
by (auto simp: sorted_Cons_iff)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    26
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    27
lemma sorted_snocD: "sorted (xs @ [y]) \<Longrightarrow> y \<le> x \<Longrightarrow> x \<notin> set xs"
61692
cb595e12451d removed lemmas that were only needed for old version of isin.
nipkow
parents: 61642
diff changeset
    28
by (auto simp: sorted_snoc_iff)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    29
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    30
lemmas isin_simps2 = sorted_lems sorted_ConsD sorted_snocD
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    31
*)
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    32
70629
2bbd945728e2 simplified setup
nipkow
parents: 69597
diff changeset
    33
lemmas isin_simps = sorted_mid_iff' sorted_Cons_iff sorted_snoc_iff
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    34
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    35
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    36
subsection \<open>Inserting into an ordered list without duplicates:\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    37
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    38
fun ins_list :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    39
"ins_list x [] = [x]" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    40
"ins_list x (a#xs) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    41
  (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
    42
70631
f14b997da756 simplified proofs
nipkow
parents: 70629
diff changeset
    43
lemma set_ins_list: "set (ins_list x xs) = set xs \<union> {x}"
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    44
by(induction xs) auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    45
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    46
lemma sorted_ins_list: "sorted xs \<Longrightarrow> sorted(ins_list x xs)"
67168
bea1258d9a27 added lemmas
nipkow
parents: 66441
diff changeset
    47
by(induction xs rule: induct_list012) auto
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    48
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    49
lemma ins_list_sorted: "sorted (xs @ [a]) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    50
  ins_list x (xs @ a # ys) =
61642
nipkow
parents: 61640
diff changeset
    51
  (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
    52
by(induction xs) (auto simp: sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    53
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    54
text\<open>In principle, @{thm ins_list_sorted} suffices, but the following two
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    55
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    56
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    57
corollary ins_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    58
  ins_list x (xs @ a # ys) = xs @ ins_list x (a#ys)"
61642
nipkow
parents: 61640
diff changeset
    59
by(auto simp add: ins_list_sorted)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    60
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    61
corollary ins_list_sorted2: "sorted (xs @ [a]) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    62
  ins_list x (xs @ a # ys) = ins_list x xs @ (a#ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    63
by(auto simp: ins_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    64
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    65
lemmas ins_list_simps = sorted_lems ins_list_sorted1 ins_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    66
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67929
diff changeset
    67
text\<open>Splay trees need two additional \<^const>\<open>ins_list\<close> lemmas:\<close>
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    68
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    69
lemma ins_list_Cons: "sorted (x # xs) \<Longrightarrow> ins_list x xs = x # xs"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    70
by (induction xs) auto
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    71
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    72
lemma ins_list_snoc: "sorted (xs @ [x]) \<Longrightarrow> ins_list x xs = xs @ [x]"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    73
by(induction xs) (auto simp add: sorted_mid_iff2)
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    74
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    75
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    76
subsection \<open>Delete one occurrence of an element from a list:\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    77
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    78
fun del_list :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    79
"del_list x [] = []" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    80
"del_list x (a#xs) = (if x=a then xs else a # del_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    81
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    82
lemma del_list_idem: "x \<notin> set xs \<Longrightarrow> del_list x xs = xs"
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    83
by (induct xs) simp_all
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    84
70631
f14b997da756 simplified proofs
nipkow
parents: 70629
diff changeset
    85
lemma set_del_list:
f14b997da756 simplified proofs
nipkow
parents: 70629
diff changeset
    86
  "sorted xs \<Longrightarrow> set (del_list x xs) = set xs - {x}"
f14b997da756 simplified proofs
nipkow
parents: 70629
diff changeset
    87
by(induct xs) (auto simp: sorted_Cons_iff)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    88
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    89
lemma sorted_del_list: "sorted xs \<Longrightarrow> sorted(del_list x xs)"
67168
bea1258d9a27 added lemmas
nipkow
parents: 66441
diff changeset
    90
apply(induction xs rule: induct_list012)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    91
apply auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    92
by (meson order.strict_trans sorted_Cons_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    93
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    94
lemma del_list_sorted: "sorted (xs @ a # ys) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    95
  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
    96
by(induction xs)
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    97
  (fastforce simp: sorted_lems sorted_Cons_iff intro!: del_list_idem)+
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    98
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    99
text\<open>In principle, @{thm del_list_sorted} suffices, but the following
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   100
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   101
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   102
corollary del_list_sorted1: "sorted (xs @ a # ys) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   103
  del_list x (xs @ a # ys) = xs @ del_list x (a # ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   104
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   105
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   106
corollary del_list_sorted2: "sorted (xs @ a # ys) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   107
  del_list x (xs @ a # ys) = del_list x xs @ a # ys"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   108
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   109
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   110
corollary del_list_sorted3:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   111
  "sorted (xs @ a # ys @ b # zs) \<Longrightarrow> x < b \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   112
  del_list x (xs @ a # ys @ b # zs) = del_list x (xs @ a # ys) @ b # zs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   113
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   114
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   115
corollary del_list_sorted4:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   116
  "sorted (xs @ a # ys @ b # zs @ c # us) \<Longrightarrow> x < c \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   117
  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
   118
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   119
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   120
corollary del_list_sorted5:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   121
  "sorted (xs @ a # ys @ b # zs @ c # us @ d # vs) \<Longrightarrow> x < d \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   122
   del_list x (xs @ a # ys @ b # zs @ c # us @ d # vs) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   123
   del_list x (xs @ a # ys @ b # zs @ c # us) @ d # vs" 
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   124
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   125
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   126
lemmas del_list_simps = sorted_lems
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   127
  del_list_sorted1
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   128
  del_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   129
  del_list_sorted3
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   130
  del_list_sorted4
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   131
  del_list_sorted5
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   132
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67929
diff changeset
   133
text\<open>Splay trees need two additional \<^const>\<open>del_list\<close> lemmas:\<close>
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   134
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   135
lemma del_list_notin_Cons: "sorted (x # xs) \<Longrightarrow> del_list x xs = xs"
66441
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
   136
by(induction xs)(fastforce simp: sorted_Cons_iff)+
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   137
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   138
lemma del_list_sorted_app:
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   139
  "sorted(xs @ [x]) \<Longrightarrow> del_list x (xs @ ys) = xs @ del_list x ys"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   140
by (induction xs) (auto simp: sorted_mid_iff2)
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   141
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   142
end