src/HOL/Data_Structures/List_Ins_Del.thy
author wenzelm
Sat, 05 Jan 2019 17:24:33 +0100
changeset 69597 ff784d5a5bfb
parent 67929 30486b96274d
child 70629 2bbd945728e2
permissions -rw-r--r--
isabelle update -u control_cartouches;
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
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    33
lemmas isin_simps = sorted_lems 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
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    43
lemma set_ins_list: "set (ins_list x xs) = insert x (set xs)"
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 distinct_if_sorted: "sorted xs \<Longrightarrow> distinct xs"
67168
bea1258d9a27 added lemmas
nipkow
parents: 66441
diff changeset
    47
apply(induction xs rule: induct_list012)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    48
apply auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    49
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    50
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    51
lemma sorted_ins_list: "sorted xs \<Longrightarrow> sorted(ins_list x xs)"
67168
bea1258d9a27 added lemmas
nipkow
parents: 66441
diff changeset
    52
by(induction xs rule: induct_list012) auto
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    53
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    54
lemma ins_list_sorted: "sorted (xs @ [a]) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    55
  ins_list x (xs @ a # ys) =
61642
nipkow
parents: 61640
diff changeset
    56
  (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
    57
by(induction xs) (auto simp: sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    58
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    59
text\<open>In principle, @{thm ins_list_sorted} suffices, but the following two
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    60
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    61
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    62
corollary ins_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    63
  ins_list x (xs @ a # ys) = xs @ ins_list x (a#ys)"
61642
nipkow
parents: 61640
diff changeset
    64
by(auto simp add: ins_list_sorted)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    65
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    66
corollary ins_list_sorted2: "sorted (xs @ [a]) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    67
  ins_list x (xs @ a # ys) = ins_list x xs @ (a#ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    68
by(auto simp: ins_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    69
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    70
lemmas ins_list_simps = sorted_lems ins_list_sorted1 ins_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    71
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67929
diff changeset
    72
text\<open>Splay trees need two additional \<^const>\<open>ins_list\<close> lemmas:\<close>
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    73
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    74
lemma ins_list_Cons: "sorted (x # xs) \<Longrightarrow> ins_list x xs = x # xs"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    75
by (induction xs) auto
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    76
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    77
lemma ins_list_snoc: "sorted (xs @ [x]) \<Longrightarrow> ins_list x xs = xs @ [x]"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    78
by(induction xs) (auto simp add: sorted_mid_iff2)
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
    79
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    80
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    81
subsection \<open>Delete one occurrence of an element from a list:\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    82
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    83
fun del_list :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    84
"del_list x [] = []" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    85
"del_list x (a#xs) = (if x=a then xs else a # del_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    86
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    87
lemma del_list_idem: "x \<notin> set xs \<Longrightarrow> del_list x xs = xs"
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    88
by (induct xs) simp_all
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    89
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    90
lemma set_del_list_eq:
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    91
  "distinct xs \<Longrightarrow> set (del_list x xs) = set xs - {x}"
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
    92
by(induct xs) auto
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    93
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    94
lemma sorted_del_list: "sorted xs \<Longrightarrow> sorted(del_list x xs)"
67168
bea1258d9a27 added lemmas
nipkow
parents: 66441
diff changeset
    95
apply(induction xs rule: induct_list012)
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    96
apply auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    97
by (meson order.strict_trans sorted_Cons_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    98
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    99
lemma del_list_sorted: "sorted (xs @ a # ys) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   100
  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
   101
by(induction xs)
67929
30486b96274d eliminated "elems"
nipkow
parents: 67406
diff changeset
   102
  (fastforce simp: sorted_lems sorted_Cons_iff intro!: del_list_idem)+
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   103
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   104
text\<open>In principle, @{thm del_list_sorted} suffices, but the following
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   105
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   106
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   107
corollary del_list_sorted1: "sorted (xs @ a # ys) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   108
  del_list x (xs @ a # ys) = xs @ del_list x (a # ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   109
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   110
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   111
corollary del_list_sorted2: "sorted (xs @ a # ys) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   112
  del_list x (xs @ a # ys) = del_list x xs @ a # ys"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   113
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   114
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   115
corollary del_list_sorted3:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   116
  "sorted (xs @ a # ys @ b # zs) \<Longrightarrow> x < b \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   117
  del_list x (xs @ a # ys @ b # zs) = del_list x (xs @ a # ys) @ b # zs"
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_sorted4:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   121
  "sorted (xs @ a # ys @ b # zs @ c # us) \<Longrightarrow> x < c \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   122
  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
   123
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   124
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   125
corollary del_list_sorted5:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   126
  "sorted (xs @ a # ys @ b # zs @ c # us @ d # vs) \<Longrightarrow> x < d \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   127
   del_list x (xs @ a # ys @ b # zs @ c # us @ d # vs) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   128
   del_list x (xs @ a # ys @ b # zs @ c # us) @ d # vs" 
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   129
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   130
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   131
lemmas del_list_simps = sorted_lems
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   132
  del_list_sorted1
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   133
  del_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   134
  del_list_sorted3
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   135
  del_list_sorted4
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   136
  del_list_sorted5
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   137
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67929
diff changeset
   138
text\<open>Splay trees need two additional \<^const>\<open>del_list\<close> lemmas:\<close>
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   139
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   140
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
   141
by(induction xs)(fastforce simp: sorted_Cons_iff)+
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   142
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   143
lemma del_list_sorted_app:
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   144
  "sorted(xs @ [x]) \<Longrightarrow> del_list x (xs @ ys) = xs @ del_list x ys"
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   145
by (induction xs) (auto simp: sorted_mid_iff2)
ce6320b9ef9b moved lemmas
nipkow
parents: 61692
diff changeset
   146
61640
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   147
end