src/HOL/Data_Structures/List_Ins_Del.thy
author nipkow
Wed, 11 Nov 2015 18:32:26 +0100
changeset 61640 44c9198f210c
parent 61637 bfa0b481a2d9
child 61642 40ca618e1b2d
permissions -rw-r--r--
no CRLF
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
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    34
lemma sorted_ConsD: "sorted (y # xs) \<Longrightarrow> x \<in> elems xs \<Longrightarrow> y < x"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    35
by (simp add: sorted_Cons_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    36
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    37
lemma sorted_snocD: "sorted (xs @ [y]) \<Longrightarrow> x \<in> elems xs \<Longrightarrow> x < y"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    38
by (simp add: sorted_snoc_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    39
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    40
lemma sorted_ConsD2: "sorted (y # xs) \<Longrightarrow> x \<le> y \<Longrightarrow> x \<notin> elems xs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    41
using leD sorted_ConsD by blast
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    42
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    43
lemma sorted_snocD2: "sorted (xs @ [y]) \<Longrightarrow> y \<le> x \<Longrightarrow> x \<notin> elems xs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    44
using leD sorted_snocD by blast
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    45
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    46
lemmas elems_simps = sorted_lems elems_app
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    47
lemmas elems_simps1 = elems_simps sorted_Cons_iff sorted_snoc_iff
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    48
lemmas elems_simps2 = elems_simps sorted_ConsD sorted_snocD sorted_ConsD2 sorted_snocD2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    49
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    50
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    51
subsection \<open>Inserting into an ordered list without duplicates:\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    52
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    53
fun ins_list :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    54
"ins_list x [] = [x]" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    55
"ins_list x (a#xs) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    56
  (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
    57
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    58
lemma set_ins_list: "elems (ins_list x xs) = insert x (elems xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    59
by(induction xs) auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    60
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    61
lemma distinct_if_sorted: "sorted xs \<Longrightarrow> distinct xs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    62
apply(induction xs rule: sorted.induct)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    63
apply auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    64
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    65
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    66
lemma sorted_ins_list: "sorted xs \<Longrightarrow> sorted(ins_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    67
by(induction xs rule: sorted.induct) auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    68
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    69
lemma ins_list_sorted: "sorted (xs @ [a]) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    70
  ins_list x (xs @ a # ys) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    71
  (if a \<le> x then xs @ ins_list x (a#ys) else ins_list x xs @ (a#ys))"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    72
by(induction xs) (auto simp: sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    73
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    74
text\<open>In principle, @{thm ins_list_sorted} suffices, but the following two
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    75
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    76
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    77
corollary ins_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    78
  ins_list x (xs @ a # ys) = xs @ ins_list x (a#ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    79
by(simp add: ins_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    80
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    81
corollary ins_list_sorted2: "sorted (xs @ [a]) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    82
  ins_list x (xs @ a # ys) = ins_list x xs @ (a#ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    83
by(auto simp: ins_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    84
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    85
lemmas ins_list_simps = sorted_lems ins_list_sorted1 ins_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    86
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    87
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    88
subsection \<open>Delete one occurrence of an element from a list:\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    89
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    90
fun del_list :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    91
"del_list x [] = []" |
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    92
"del_list x (a#xs) = (if x=a then xs else a # del_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    93
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    94
lemma del_list_idem: "x \<notin> elems xs \<Longrightarrow> del_list x xs = xs"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    95
by (induct xs) simp_all
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    96
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    97
lemma elems_del_list_eq:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    98
  "distinct xs \<Longrightarrow> elems (del_list x xs) = elems xs - {x}"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
    99
apply(induct xs)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   100
 apply simp
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   101
apply (simp add: elems_eq_set)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   102
apply blast
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   103
done
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   104
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   105
lemma sorted_del_list: "sorted xs \<Longrightarrow> sorted(del_list x xs)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   106
apply(induction xs rule: sorted.induct)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   107
apply auto
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   108
by (meson order.strict_trans sorted_Cons_iff)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   109
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   110
lemma del_list_sorted: "sorted (xs @ a # ys) \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   111
  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
   112
by(induction xs)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   113
  (fastforce simp: sorted_lems sorted_Cons_iff elems_eq_set intro!: del_list_idem)+
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   114
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   115
text\<open>In principle, @{thm del_list_sorted} suffices, but the following
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   116
corollaries speed up proofs.\<close>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   117
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   118
corollary del_list_sorted1: "sorted (xs @ a # ys) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   119
  del_list x (xs @ a # ys) = xs @ del_list x (a # ys)"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   120
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   121
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   122
corollary del_list_sorted2: "sorted (xs @ a # ys) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   123
  del_list x (xs @ a # ys) = del_list x xs @ a # ys"
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   124
by (auto simp: del_list_sorted)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   125
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   126
corollary del_list_sorted3:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   127
  "sorted (xs @ a # ys @ b # zs) \<Longrightarrow> x < b \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   128
  del_list x (xs @ a # ys @ b # zs) = del_list x (xs @ a # ys) @ b # zs"
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
corollary del_list_sorted4:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   132
  "sorted (xs @ a # ys @ b # zs @ c # us) \<Longrightarrow> x < c \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   133
  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
   134
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   135
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   136
corollary del_list_sorted5:
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   137
  "sorted (xs @ a # ys @ b # zs @ c # us @ d # vs) \<Longrightarrow> x < d \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   138
   del_list x (xs @ a # ys @ b # zs @ c # us @ d # vs) =
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   139
   del_list x (xs @ a # ys @ b # zs @ c # us) @ d # vs" 
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   140
by (auto simp: del_list_sorted sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   141
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   142
lemmas del_list_simps = sorted_lems
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   143
  del_list_sorted1
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   144
  del_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   145
  del_list_sorted3
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   146
  del_list_sorted4
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   147
  del_list_sorted5
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   148
44c9198f210c no CRLF
nipkow
parents: 61637
diff changeset
   149
end