src/HOL/Library/More_List.thy
author haftmann
Wed Sep 29 15:28:29 2010 +0200 (2010-09-29)
changeset 39791 a91430778479
parent 39778 9b1814140bcf
child 39921 45f95e4de831
permissions -rw-r--r--
redundancy check: drop trailing Var arguments (avoids eta problems with equations)
haftmann@37025
     1
(*  Author:  Florian Haftmann, TU Muenchen *)
haftmann@37025
     2
haftmann@37025
     3
header {* Operations on lists beyond the standard List theory *}
haftmann@37025
     4
haftmann@37025
     5
theory More_List
haftmann@37025
     6
imports Main
haftmann@37025
     7
begin
haftmann@37025
     8
haftmann@37025
     9
hide_const (open) Finite_Set.fold
haftmann@37025
    10
haftmann@37025
    11
text {* Repairing code generator setup *}
haftmann@37025
    12
haftmann@37025
    13
declare (in lattice) Inf_fin_set_fold [code_unfold del]
haftmann@37025
    14
declare (in lattice) Sup_fin_set_fold [code_unfold del]
haftmann@37025
    15
declare (in linorder) Min_fin_set_fold [code_unfold del]
haftmann@37025
    16
declare (in linorder) Max_fin_set_fold [code_unfold del]
haftmann@37025
    17
declare (in complete_lattice) Inf_set_fold [code_unfold del]
haftmann@37025
    18
declare (in complete_lattice) Sup_set_fold [code_unfold del]
haftmann@37025
    19
haftmann@37025
    20
text {* Fold combinator with canonical argument order *}
haftmann@37025
    21
haftmann@37025
    22
primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@37025
    23
    "fold f [] = id"
haftmann@37025
    24
  | "fold f (x # xs) = fold f xs \<circ> f x"
haftmann@37025
    25
haftmann@37025
    26
lemma foldl_fold:
haftmann@37025
    27
  "foldl f s xs = fold (\<lambda>x s. f s x) xs s"
haftmann@37025
    28
  by (induct xs arbitrary: s) simp_all
haftmann@37025
    29
haftmann@37025
    30
lemma foldr_fold_rev:
haftmann@37025
    31
  "foldr f xs = fold f (rev xs)"
nipkow@39302
    32
  by (simp add: foldr_foldl foldl_fold fun_eq_iff)
haftmann@37025
    33
haftmann@37025
    34
lemma fold_rev_conv [code_unfold]:
haftmann@37025
    35
  "fold f (rev xs) = foldr f xs"
haftmann@37025
    36
  by (simp add: foldr_fold_rev)
haftmann@37025
    37
  
haftmann@37025
    38
lemma fold_cong [fundef_cong, recdef_cong]:
haftmann@37025
    39
  "a = b \<Longrightarrow> xs = ys \<Longrightarrow> (\<And>x. x \<in> set xs \<Longrightarrow> f x = g x)
haftmann@37025
    40
    \<Longrightarrow> fold f xs a = fold g ys b"
haftmann@37025
    41
  by (induct ys arbitrary: a b xs) simp_all
haftmann@37025
    42
haftmann@37025
    43
lemma fold_id:
haftmann@37025
    44
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x = id"
haftmann@37025
    45
  shows "fold f xs = id"
haftmann@37025
    46
  using assms by (induct xs) simp_all
haftmann@37025
    47
haftmann@37025
    48
lemma fold_apply:
haftmann@37025
    49
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> h \<circ> g x = f x \<circ> h"
haftmann@37025
    50
  shows "h \<circ> fold g xs = fold f xs \<circ> h"
nipkow@39302
    51
  using assms by (induct xs) (simp_all add: fun_eq_iff)
haftmann@37025
    52
haftmann@37025
    53
lemma fold_invariant: 
haftmann@37025
    54
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> Q x" and "P s"
haftmann@37025
    55
    and "\<And>x s. Q x \<Longrightarrow> P s \<Longrightarrow> P (f x s)"
haftmann@37025
    56
  shows "P (fold f xs s)"
haftmann@37025
    57
  using assms by (induct xs arbitrary: s) simp_all
haftmann@37025
    58
haftmann@37025
    59
lemma fold_weak_invariant:
haftmann@37025
    60
  assumes "P s"
haftmann@37025
    61
    and "\<And>s x. x \<in> set xs \<Longrightarrow> P s \<Longrightarrow> P (f x s)"
haftmann@37025
    62
  shows "P (fold f xs s)"
haftmann@37025
    63
  using assms by (induct xs arbitrary: s) simp_all
haftmann@37025
    64
haftmann@37025
    65
lemma fold_append [simp]:
haftmann@37025
    66
  "fold f (xs @ ys) = fold f ys \<circ> fold f xs"
haftmann@37025
    67
  by (induct xs) simp_all
haftmann@37025
    68
haftmann@37025
    69
lemma fold_map [code_unfold]:
haftmann@37025
    70
  "fold g (map f xs) = fold (g o f) xs"
haftmann@37025
    71
  by (induct xs) simp_all
haftmann@37025
    72
haftmann@37025
    73
lemma fold_rev:
haftmann@37025
    74
  assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y"
haftmann@37025
    75
  shows "fold f (rev xs) = fold f xs"
haftmann@37025
    76
  using assms by (induct xs) (simp_all del: o_apply add: fold_apply)
haftmann@37025
    77
haftmann@37025
    78
lemma foldr_fold:
haftmann@37025
    79
  assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y"
haftmann@37025
    80
  shows "foldr f xs = fold f xs"
haftmann@37025
    81
  using assms unfolding foldr_fold_rev by (rule fold_rev)
haftmann@37025
    82
haftmann@37025
    83
lemma fold_Cons_rev:
haftmann@37025
    84
  "fold Cons xs = append (rev xs)"
haftmann@37025
    85
  by (induct xs) simp_all
haftmann@37025
    86
haftmann@37025
    87
lemma rev_conv_fold [code]:
haftmann@37025
    88
  "rev xs = fold Cons xs []"
haftmann@37025
    89
  by (simp add: fold_Cons_rev)
haftmann@37025
    90
haftmann@37025
    91
lemma fold_append_concat_rev:
haftmann@37025
    92
  "fold append xss = append (concat (rev xss))"
haftmann@37025
    93
  by (induct xss) simp_all
haftmann@37025
    94
haftmann@37025
    95
lemma concat_conv_foldr [code]:
haftmann@37025
    96
  "concat xss = foldr append xss []"
haftmann@37025
    97
  by (simp add: fold_append_concat_rev foldr_fold_rev)
haftmann@37025
    98
haftmann@37025
    99
lemma fold_plus_listsum_rev:
haftmann@37025
   100
  "fold plus xs = plus (listsum (rev xs))"
haftmann@37025
   101
  by (induct xs) (simp_all add: add.assoc)
haftmann@37025
   102
haftmann@39773
   103
lemma (in monoid_add) listsum_conv_fold [code]:
haftmann@39773
   104
  "listsum xs = fold (\<lambda>x y. y + x) xs 0"
haftmann@39773
   105
  by (auto simp add: listsum_foldl foldl_fold fun_eq_iff)
haftmann@37025
   106
haftmann@39773
   107
lemma (in linorder) sort_key_conv_fold:
haftmann@37025
   108
  assumes "inj_on f (set xs)"
haftmann@37025
   109
  shows "sort_key f xs = fold (insort_key f) xs []"
haftmann@37025
   110
proof -
haftmann@37025
   111
  have "fold (insort_key f) (rev xs) = fold (insort_key f) xs"
haftmann@37025
   112
  proof (rule fold_rev, rule ext)
haftmann@37025
   113
    fix zs
haftmann@37025
   114
    fix x y
haftmann@37025
   115
    assume "x \<in> set xs" "y \<in> set xs"
haftmann@37025
   116
    with assms have *: "f y = f x \<Longrightarrow> y = x" by (auto dest: inj_onD)
haftmann@39773
   117
    have **: "x = y \<longleftrightarrow> y = x" by auto
haftmann@37025
   118
    show "(insort_key f y \<circ> insort_key f x) zs = (insort_key f x \<circ> insort_key f y) zs"
haftmann@39773
   119
      by (induct zs) (auto intro: * simp add: **)
haftmann@37025
   120
  qed
haftmann@37025
   121
  then show ?thesis by (simp add: sort_key_def foldr_fold_rev)
haftmann@37025
   122
qed
haftmann@37025
   123
haftmann@39773
   124
lemma (in linorder) sort_conv_fold:
haftmann@37025
   125
  "sort xs = fold insort xs []"
haftmann@37025
   126
  by (rule sort_key_conv_fold) simp
haftmann@37025
   127
haftmann@37025
   128
text {* @{const Finite_Set.fold} and @{const fold} *}
haftmann@37025
   129
haftmann@37025
   130
lemma (in fun_left_comm) fold_set_remdups:
haftmann@37025
   131
  "Finite_Set.fold f y (set xs) = fold f (remdups xs) y"
haftmann@37025
   132
  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm insert_absorb)
haftmann@37025
   133
haftmann@37025
   134
lemma (in fun_left_comm_idem) fold_set:
haftmann@37025
   135
  "Finite_Set.fold f y (set xs) = fold f xs y"
haftmann@37025
   136
  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm)
haftmann@37025
   137
haftmann@37025
   138
lemma (in ab_semigroup_idem_mult) fold1_set:
haftmann@37025
   139
  assumes "xs \<noteq> []"
haftmann@37025
   140
  shows "Finite_Set.fold1 times (set xs) = fold times (tl xs) (hd xs)"
haftmann@37025
   141
proof -
haftmann@37025
   142
  interpret fun_left_comm_idem times by (fact fun_left_comm_idem)
haftmann@37025
   143
  from assms obtain y ys where xs: "xs = y # ys"
haftmann@37025
   144
    by (cases xs) auto
haftmann@37025
   145
  show ?thesis
haftmann@37025
   146
  proof (cases "set ys = {}")
haftmann@37025
   147
    case True with xs show ?thesis by simp
haftmann@37025
   148
  next
haftmann@37025
   149
    case False
haftmann@37025
   150
    then have "fold1 times (insert y (set ys)) = Finite_Set.fold times y (set ys)"
haftmann@37025
   151
      by (simp only: finite_set fold1_eq_fold_idem)
haftmann@37025
   152
    with xs show ?thesis by (simp add: fold_set mult_commute)
haftmann@37025
   153
  qed
haftmann@37025
   154
qed
haftmann@37025
   155
haftmann@37025
   156
lemma (in lattice) Inf_fin_set_fold:
haftmann@37025
   157
  "Inf_fin (set (x # xs)) = fold inf xs x"
haftmann@37025
   158
proof -
haftmann@37025
   159
  interpret ab_semigroup_idem_mult "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@37025
   160
    by (fact ab_semigroup_idem_mult_inf)
haftmann@37025
   161
  show ?thesis
haftmann@37025
   162
    by (simp add: Inf_fin_def fold1_set del: set.simps)
haftmann@37025
   163
qed
haftmann@37025
   164
haftmann@37025
   165
lemma (in lattice) Inf_fin_set_foldr [code_unfold]:
haftmann@37025
   166
  "Inf_fin (set (x # xs)) = foldr inf xs x"
nipkow@39302
   167
  by (simp add: Inf_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps)
haftmann@37025
   168
haftmann@37025
   169
lemma (in lattice) Sup_fin_set_fold:
haftmann@37025
   170
  "Sup_fin (set (x # xs)) = fold sup xs x"
haftmann@37025
   171
proof -
haftmann@37025
   172
  interpret ab_semigroup_idem_mult "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@37025
   173
    by (fact ab_semigroup_idem_mult_sup)
haftmann@37025
   174
  show ?thesis
haftmann@37025
   175
    by (simp add: Sup_fin_def fold1_set del: set.simps)
haftmann@37025
   176
qed
haftmann@37025
   177
haftmann@37025
   178
lemma (in lattice) Sup_fin_set_foldr [code_unfold]:
haftmann@37025
   179
  "Sup_fin (set (x # xs)) = foldr sup xs x"
nipkow@39302
   180
  by (simp add: Sup_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps)
haftmann@37025
   181
haftmann@37025
   182
lemma (in linorder) Min_fin_set_fold:
haftmann@37025
   183
  "Min (set (x # xs)) = fold min xs x"
haftmann@37025
   184
proof -
haftmann@37025
   185
  interpret ab_semigroup_idem_mult "min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@37025
   186
    by (fact ab_semigroup_idem_mult_min)
haftmann@37025
   187
  show ?thesis
haftmann@37025
   188
    by (simp add: Min_def fold1_set del: set.simps)
haftmann@37025
   189
qed
haftmann@37025
   190
haftmann@37025
   191
lemma (in linorder) Min_fin_set_foldr [code_unfold]:
haftmann@37025
   192
  "Min (set (x # xs)) = foldr min xs x"
nipkow@39302
   193
  by (simp add: Min_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps)
haftmann@37025
   194
haftmann@37025
   195
lemma (in linorder) Max_fin_set_fold:
haftmann@37025
   196
  "Max (set (x # xs)) = fold max xs x"
haftmann@37025
   197
proof -
haftmann@37025
   198
  interpret ab_semigroup_idem_mult "max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@37025
   199
    by (fact ab_semigroup_idem_mult_max)
haftmann@37025
   200
  show ?thesis
haftmann@37025
   201
    by (simp add: Max_def fold1_set del: set.simps)
haftmann@37025
   202
qed
haftmann@37025
   203
haftmann@37025
   204
lemma (in linorder) Max_fin_set_foldr [code_unfold]:
haftmann@37025
   205
  "Max (set (x # xs)) = foldr max xs x"
nipkow@39302
   206
  by (simp add: Max_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps)
haftmann@37025
   207
haftmann@37025
   208
lemma (in complete_lattice) Inf_set_fold:
haftmann@37025
   209
  "Inf (set xs) = fold inf xs top"
haftmann@37025
   210
proof -
haftmann@37025
   211
  interpret fun_left_comm_idem "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@37025
   212
    by (fact fun_left_comm_idem_inf)
haftmann@37025
   213
  show ?thesis by (simp add: Inf_fold_inf fold_set inf_commute)
haftmann@37025
   214
qed
haftmann@37025
   215
haftmann@37025
   216
lemma (in complete_lattice) Inf_set_foldr [code_unfold]:
haftmann@37025
   217
  "Inf (set xs) = foldr inf xs top"
nipkow@39302
   218
  by (simp add: Inf_set_fold ac_simps foldr_fold fun_eq_iff)
haftmann@37025
   219
haftmann@37025
   220
lemma (in complete_lattice) Sup_set_fold:
haftmann@37025
   221
  "Sup (set xs) = fold sup xs bot"
haftmann@37025
   222
proof -
haftmann@37025
   223
  interpret fun_left_comm_idem "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@37025
   224
    by (fact fun_left_comm_idem_sup)
haftmann@37025
   225
  show ?thesis by (simp add: Sup_fold_sup fold_set sup_commute)
haftmann@37025
   226
qed
haftmann@37025
   227
haftmann@37025
   228
lemma (in complete_lattice) Sup_set_foldr [code_unfold]:
haftmann@37025
   229
  "Sup (set xs) = foldr sup xs bot"
nipkow@39302
   230
  by (simp add: Sup_set_fold ac_simps foldr_fold fun_eq_iff)
haftmann@37025
   231
haftmann@37025
   232
lemma (in complete_lattice) INFI_set_fold:
haftmann@37025
   233
  "INFI (set xs) f = fold (inf \<circ> f) xs top"
haftmann@37025
   234
  unfolding INFI_def set_map [symmetric] Inf_set_fold fold_map ..
haftmann@37025
   235
haftmann@37025
   236
lemma (in complete_lattice) SUPR_set_fold:
haftmann@37025
   237
  "SUPR (set xs) f = fold (sup \<circ> f) xs bot"
haftmann@37025
   238
  unfolding SUPR_def set_map [symmetric] Sup_set_fold fold_map ..
haftmann@37025
   239
haftmann@37028
   240
text {* @{text nth_map} *}
haftmann@37025
   241
haftmann@37025
   242
definition nth_map :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@37025
   243
  "nth_map n f xs = (if n < length xs then
haftmann@37025
   244
       take n xs @ [f (xs ! n)] @ drop (Suc n) xs
haftmann@37025
   245
     else xs)"
haftmann@37025
   246
haftmann@37025
   247
lemma nth_map_id:
haftmann@37025
   248
  "n \<ge> length xs \<Longrightarrow> nth_map n f xs = xs"
haftmann@37025
   249
  by (simp add: nth_map_def)
haftmann@37025
   250
haftmann@37025
   251
lemma nth_map_unfold:
haftmann@37025
   252
  "n < length xs \<Longrightarrow> nth_map n f xs = take n xs @ [f (xs ! n)] @ drop (Suc n) xs"
haftmann@37025
   253
  by (simp add: nth_map_def)
haftmann@37025
   254
haftmann@37025
   255
lemma nth_map_Nil [simp]:
haftmann@37025
   256
  "nth_map n f [] = []"
haftmann@37025
   257
  by (simp add: nth_map_def)
haftmann@37025
   258
haftmann@37025
   259
lemma nth_map_zero [simp]:
haftmann@37025
   260
  "nth_map 0 f (x # xs) = f x # xs"
haftmann@37025
   261
  by (simp add: nth_map_def)
haftmann@37025
   262
haftmann@37025
   263
lemma nth_map_Suc [simp]:
haftmann@37025
   264
  "nth_map (Suc n) f (x # xs) = x # nth_map n f xs"
haftmann@37025
   265
  by (simp add: nth_map_def)
haftmann@37025
   266
haftmann@37025
   267
end