src/HOL/Data_Structures/List_Ins_Del.thy
author nipkow
Mon, 02 Nov 2015 18:35:30 +0100
changeset 61534 a88e07c8d0d5
parent 61231 cc6969542f8d
child 61589 d07d0d5a572b
permissions -rw-r--r--
tuned names and optimized comparison order
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     2
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     3
section {* List Insertion and Deletion *}
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     4
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     5
theory List_Ins_Del
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     6
imports Sorted_Less
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     7
begin
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     8
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     9
subsection \<open>Elements in a list\<close>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    10
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    11
fun elems :: "'a list \<Rightarrow> 'a set" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    12
"elems [] = {}" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    13
"elems (x#xs) = Set.insert x (elems xs)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    14
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    15
lemma elems_app: "elems (xs @ ys) = (elems xs \<union> elems ys)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    16
by (induction xs) auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    17
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    18
lemma elems_eq_set: "elems xs = set xs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    19
by (induction xs) auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    20
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    21
lemma sorted_Cons_iff:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    22
  "sorted(x # xs) = (sorted xs \<and> (\<forall>y \<in> elems xs. x < y))"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    23
by(simp add: elems_eq_set Sorted_Less.sorted_Cons_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    24
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    25
lemma sorted_snoc_iff:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    26
  "sorted(xs @ [x]) = (sorted xs \<and> (\<forall>y \<in> elems xs. y < x))"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    27
by(simp add: elems_eq_set Sorted_Less.sorted_snoc_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    28
61229
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    29
text{* The above two rules introduce quantifiers. It turns out
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    30
that in practice this is not a problem because of the simplicity of
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    31
the "isin" functions that implement @{const elems}. Nevertheless
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    32
it is possible to avoid the quantifiers with the help of some rewrite rules: *}
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    33
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    34
lemma sorted_ConsD: "sorted (y # xs) \<Longrightarrow> x \<in> elems xs \<Longrightarrow> y < x"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    35
by (simp add: sorted_Cons_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    36
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    37
lemma sorted_snocD: "sorted (xs @ [y]) \<Longrightarrow> x \<in> elems xs \<Longrightarrow> x < y"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    38
by (simp add: sorted_snoc_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    39
61229
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    40
lemma sorted_ConsD2: "sorted (y # xs) \<Longrightarrow> x \<le> y \<Longrightarrow> x \<notin> elems xs"
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    41
using leD sorted_ConsD by blast
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    42
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    43
lemma sorted_snocD2: "sorted (xs @ [y]) \<Longrightarrow> y \<le> x \<Longrightarrow> x \<notin> elems xs"
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    44
using leD sorted_snocD by blast
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    45
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    46
lemmas elems_simps = sorted_lems elems_app
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    47
lemmas elems_simps1 = elems_simps sorted_Cons_iff sorted_snoc_iff
0b9c45c4af29 unified isin-proofs
nipkow
parents: 61203
diff changeset
    48
lemmas elems_simps2 = elems_simps sorted_ConsD sorted_snocD sorted_ConsD2 sorted_snocD2
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    49
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    50
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    51
subsection \<open>Inserting into an ordered list without duplicates:\<close>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    52
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    53
fun ins_list :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    54
"ins_list x [] = [x]" |
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    55
"ins_list x (a#xs) =
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    56
  (if x < a then x#a#xs else if x=a then a#xs else a # ins_list x xs)"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    57
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    58
lemma set_ins_list[simp]: "elems (ins_list x xs) = insert x (elems xs)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    59
by(induction xs) auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    60
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    61
lemma distinct_if_sorted: "sorted xs \<Longrightarrow> distinct xs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    62
apply(induction xs rule: sorted.induct)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    63
apply auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    64
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    65
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    66
lemma sorted_ins_list: "sorted xs \<Longrightarrow> sorted(ins_list x xs)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    67
by(induction xs rule: sorted.induct) auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    68
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    69
lemma ins_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    70
  ins_list x (xs @ a # ys) = xs @ ins_list x (a#ys)"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    71
by(induction xs) (auto simp: sorted_lems)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    72
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    73
lemma ins_list_sorted2: "sorted (xs @ [a]) \<Longrightarrow> x < a \<Longrightarrow>
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    74
  ins_list x (xs @ a # ys) = ins_list x xs @ (a#ys)"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    75
by(induction xs) (auto simp: sorted_lems)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    76
61231
nipkow
parents: 61229
diff changeset
    77
lemmas ins_list_simps = sorted_lems ins_list_sorted1 ins_list_sorted2
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    78
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    79
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    80
subsection \<open>Delete one occurrence of an element from a list:\<close>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    81
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    82
fun del_list :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    83
"del_list x [] = []" |
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
    84
"del_list x (a#xs) = (if x=a then xs else a # del_list x xs)"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    85
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    86
lemma del_list_idem: "x \<notin> elems xs \<Longrightarrow> del_list x xs = xs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    87
by (induct xs) simp_all
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    88
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    89
lemma elems_del_list_eq [simp]:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    90
  "distinct xs \<Longrightarrow> elems (del_list x xs) = elems xs - {x}"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    91
apply(induct xs)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    92
 apply simp
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    93
apply (simp add: elems_eq_set)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    94
apply blast
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    95
done
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    96
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    97
lemma sorted_del_list: "sorted xs \<Longrightarrow> sorted(del_list x xs)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    98
apply(induction xs rule: sorted.induct)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    99
apply auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   100
by (meson order.strict_trans sorted_Cons_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   101
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   102
lemma del_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   103
  del_list x (xs @ a # ys) = xs @ del_list x (a # ys)"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   104
by (induction xs) (auto simp: sorted_mid_iff2)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   105
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   106
lemma del_list_sorted2: "sorted (xs @ a # ys) \<Longrightarrow> x < a \<Longrightarrow>
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   107
  del_list x (xs @ a # ys) = del_list x xs @ a # ys"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   108
by (induction xs) (auto simp: sorted_Cons_iff intro!: del_list_idem)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   109
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   110
lemma del_list_sorted3:
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   111
  "sorted (xs @ a # ys @ b # zs) \<Longrightarrow> x < b \<Longrightarrow>
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   112
  del_list x (xs @ a # ys @ b # zs) = del_list x (xs @ a # ys) @ b # zs"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   113
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted2)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   114
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   115
lemma del_list_sorted4:
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   116
  "sorted (xs @ a # ys @ b # zs @ c # us) \<Longrightarrow> x < c \<Longrightarrow>
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   117
  del_list x (xs @ a # ys @ b # zs @ c # us) = del_list x (xs @ a # ys @ b # zs) @ c # us"
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   118
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted3)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   119
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   120
lemma del_list_sorted5:
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   121
  "sorted (xs @ a # ys @ b # zs @ c # us @ d # vs) \<Longrightarrow> x < d \<Longrightarrow>
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   122
   del_list x (xs @ a # ys @ b # zs @ c # us @ d # vs) =
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61231
diff changeset
   123
   del_list x (xs @ a # ys @ b # zs @ c # us) @ d # vs" 
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   124
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted4)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   125
61231
nipkow
parents: 61229
diff changeset
   126
lemmas del_list_simps = sorted_lems
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   127
  del_list_sorted1
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   128
  del_list_sorted2
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   129
  del_list_sorted3
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   130
  del_list_sorted4
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   131
  del_list_sorted5
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   132
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   133
end