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