src/HOL/Library/FinFun.thy
author kuncar
Wed May 15 12:10:44 2013 +0200 (2013-05-15)
changeset 51995 07344a4f19db
parent 51124 8fd094d5b7b7
child 52916 5f3faf72b62a
permissions -rw-r--r--
superfluous transfer rule
Andreas@48028
     1
(* Author: Andreas Lochbihler, Uni Karlsruhe *)
Andreas@48028
     2
Andreas@48028
     3
header {* Almost everywhere constant functions *}
Andreas@48028
     4
Andreas@48028
     5
theory FinFun
Andreas@48051
     6
imports Cardinality
Andreas@48028
     7
begin
Andreas@48028
     8
Andreas@48028
     9
text {*
Andreas@48028
    10
  This theory defines functions which are constant except for finitely
Andreas@48028
    11
  many points (FinFun) and introduces a type finfin along with a
Andreas@48028
    12
  number of operators for them. The code generator is set up such that
Andreas@48028
    13
  such functions can be represented as data in the generated code and
Andreas@48028
    14
  all operators are executable.
Andreas@48028
    15
Andreas@48028
    16
  For details, see Formalising FinFuns - Generating Code for Functions as Data by A. Lochbihler in TPHOLs 2009.
Andreas@48028
    17
*}
Andreas@48028
    18
Andreas@48028
    19
Andreas@48028
    20
definition "code_abort" :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a"
Andreas@48028
    21
where [simp, code del]: "code_abort f = f ()"
Andreas@48028
    22
Andreas@48028
    23
code_abort "code_abort"
Andreas@48028
    24
Andreas@48028
    25
hide_const (open) "code_abort"
Andreas@48028
    26
Andreas@48028
    27
subsection {* The @{text "map_default"} operation *}
Andreas@48028
    28
Andreas@48028
    29
definition map_default :: "'b \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
Andreas@48028
    30
where "map_default b f a \<equiv> case f a of None \<Rightarrow> b | Some b' \<Rightarrow> b'"
Andreas@48028
    31
Andreas@48028
    32
lemma map_default_delete [simp]:
Andreas@48028
    33
  "map_default b (f(a := None)) = (map_default b f)(a := b)"
Andreas@48028
    34
by(simp add: map_default_def fun_eq_iff)
Andreas@48028
    35
Andreas@48028
    36
lemma map_default_insert:
Andreas@48028
    37
  "map_default b (f(a \<mapsto> b')) = (map_default b f)(a := b')"
Andreas@48028
    38
by(simp add: map_default_def fun_eq_iff)
Andreas@48028
    39
Andreas@48028
    40
lemma map_default_empty [simp]: "map_default b empty = (\<lambda>a. b)"
Andreas@48028
    41
by(simp add: fun_eq_iff map_default_def)
Andreas@48028
    42
Andreas@48028
    43
lemma map_default_inject:
Andreas@48028
    44
  fixes g g' :: "'a \<rightharpoonup> 'b"
Andreas@48028
    45
  assumes infin_eq: "\<not> finite (UNIV :: 'a set) \<or> b = b'"
Andreas@48028
    46
  and fin: "finite (dom g)" and b: "b \<notin> ran g"
Andreas@48028
    47
  and fin': "finite (dom g')" and b': "b' \<notin> ran g'"
Andreas@48028
    48
  and eq': "map_default b g = map_default b' g'"
Andreas@48028
    49
  shows "b = b'" "g = g'"
Andreas@48028
    50
proof -
Andreas@48028
    51
  from infin_eq show bb': "b = b'"
Andreas@48028
    52
  proof
Andreas@48028
    53
    assume infin: "\<not> finite (UNIV :: 'a set)"
Andreas@48028
    54
    from fin fin' have "finite (dom g \<union> dom g')" by auto
Andreas@48028
    55
    with infin have "UNIV - (dom g \<union> dom g') \<noteq> {}" by(auto dest: finite_subset)
Andreas@48028
    56
    then obtain a where a: "a \<notin> dom g \<union> dom g'" by auto
Andreas@48028
    57
    hence "map_default b g a = b" "map_default b' g' a = b'" by(auto simp add: map_default_def)
Andreas@48028
    58
    with eq' show "b = b'" by simp
Andreas@48028
    59
  qed
Andreas@48028
    60
Andreas@48028
    61
  show "g = g'"
Andreas@48028
    62
  proof
Andreas@48028
    63
    fix x
Andreas@48028
    64
    show "g x = g' x"
Andreas@48028
    65
    proof(cases "g x")
Andreas@48028
    66
      case None
Andreas@48028
    67
      hence "map_default b g x = b" by(simp add: map_default_def)
Andreas@48028
    68
      with bb' eq' have "map_default b' g' x = b'" by simp
Andreas@48028
    69
      with b' have "g' x = None" by(simp add: map_default_def ran_def split: option.split_asm)
Andreas@48028
    70
      with None show ?thesis by simp
Andreas@48028
    71
    next
Andreas@48028
    72
      case (Some c)
Andreas@48028
    73
      with b have cb: "c \<noteq> b" by(auto simp add: ran_def)
Andreas@48028
    74
      moreover from Some have "map_default b g x = c" by(simp add: map_default_def)
Andreas@48028
    75
      with eq' have "map_default b' g' x = c" by simp
Andreas@48028
    76
      ultimately have "g' x = Some c" using b' bb' by(auto simp add: map_default_def split: option.splits)
Andreas@48028
    77
      with Some show ?thesis by simp
Andreas@48028
    78
    qed
Andreas@48028
    79
  qed
Andreas@48028
    80
qed
Andreas@48028
    81
Andreas@48028
    82
subsection {* The finfun type *}
Andreas@48028
    83
Andreas@48028
    84
definition "finfun = {f::'a\<Rightarrow>'b. \<exists>b. finite {a. f a \<noteq> b}}"
Andreas@48028
    85
wenzelm@49834
    86
typedef ('a,'b) finfun  ("(_ =>f /_)" [22, 21] 21) = "finfun :: ('a => 'b) set"
Andreas@48029
    87
  morphisms finfun_apply Abs_finfun
Andreas@48028
    88
proof -
Andreas@48028
    89
  have "\<exists>f. finite {x. f x \<noteq> undefined}"
Andreas@48028
    90
  proof
Andreas@48028
    91
    show "finite {x. (\<lambda>y. undefined) x \<noteq> undefined}" by auto
Andreas@48028
    92
  qed
Andreas@48028
    93
  then show ?thesis unfolding finfun_def by auto
Andreas@48028
    94
qed
Andreas@48028
    95
Andreas@48034
    96
type_notation finfun ("(_ \<Rightarrow>f /_)" [22, 21] 21)
Andreas@48034
    97
Andreas@48028
    98
setup_lifting type_definition_finfun
Andreas@48028
    99
Andreas@48028
   100
lemma fun_upd_finfun: "y(a := b) \<in> finfun \<longleftrightarrow> y \<in> finfun"
Andreas@48028
   101
proof -
Andreas@48028
   102
  { fix b'
Andreas@48028
   103
    have "finite {a'. (y(a := b)) a' \<noteq> b'} = finite {a'. y a' \<noteq> b'}"
Andreas@48028
   104
    proof(cases "b = b'")
Andreas@48028
   105
      case True
Andreas@48028
   106
      hence "{a'. (y(a := b)) a' \<noteq> b'} = {a'. y a' \<noteq> b'} - {a}" by auto
Andreas@48028
   107
      thus ?thesis by simp
Andreas@48028
   108
    next
Andreas@48028
   109
      case False
Andreas@48028
   110
      hence "{a'. (y(a := b)) a' \<noteq> b'} = insert a {a'. y a' \<noteq> b'}" by auto
Andreas@48028
   111
      thus ?thesis by simp
Andreas@48028
   112
    qed }
Andreas@48028
   113
  thus ?thesis unfolding finfun_def by blast
Andreas@48028
   114
qed
Andreas@48028
   115
Andreas@48028
   116
lemma const_finfun: "(\<lambda>x. a) \<in> finfun"
Andreas@48028
   117
by(auto simp add: finfun_def)
Andreas@48028
   118
Andreas@48028
   119
lemma finfun_left_compose:
Andreas@48028
   120
  assumes "y \<in> finfun"
Andreas@48028
   121
  shows "g \<circ> y \<in> finfun"
Andreas@48028
   122
proof -
Andreas@48028
   123
  from assms obtain b where "finite {a. y a \<noteq> b}"
Andreas@48028
   124
    unfolding finfun_def by blast
Andreas@48028
   125
  hence "finite {c. g (y c) \<noteq> g b}"
Andreas@48028
   126
  proof(induct "{a. y a \<noteq> b}" arbitrary: y)
Andreas@48028
   127
    case empty
Andreas@48037
   128
    hence "y = (\<lambda>a. b)" by(auto)
Andreas@48028
   129
    thus ?case by(simp)
Andreas@48028
   130
  next
Andreas@48028
   131
    case (insert x F)
Andreas@48028
   132
    note IH = `\<And>y. F = {a. y a \<noteq> b} \<Longrightarrow> finite {c. g (y c) \<noteq> g b}`
Andreas@48028
   133
    from `insert x F = {a. y a \<noteq> b}` `x \<notin> F`
Andreas@48028
   134
    have F: "F = {a. (y(x := b)) a \<noteq> b}" by(auto)
Andreas@48028
   135
    show ?case
Andreas@48028
   136
    proof(cases "g (y x) = g b")
Andreas@48028
   137
      case True
Andreas@48028
   138
      hence "{c. g ((y(x := b)) c) \<noteq> g b} = {c. g (y c) \<noteq> g b}" by auto
Andreas@48028
   139
      with IH[OF F] show ?thesis by simp
Andreas@48028
   140
    next
Andreas@48028
   141
      case False
Andreas@48028
   142
      hence "{c. g (y c) \<noteq> g b} = insert x {c. g ((y(x := b)) c) \<noteq> g b}" by auto
Andreas@48028
   143
      with IH[OF F] show ?thesis by(simp)
Andreas@48028
   144
    qed
Andreas@48028
   145
  qed
Andreas@48028
   146
  thus ?thesis unfolding finfun_def by auto
Andreas@48028
   147
qed
Andreas@48028
   148
Andreas@48028
   149
lemma assumes "y \<in> finfun"
Andreas@48028
   150
  shows fst_finfun: "fst \<circ> y \<in> finfun"
Andreas@48028
   151
  and snd_finfun: "snd \<circ> y \<in> finfun"
Andreas@48028
   152
proof -
Andreas@48028
   153
  from assms obtain b c where bc: "finite {a. y a \<noteq> (b, c)}"
Andreas@48028
   154
    unfolding finfun_def by auto
Andreas@48028
   155
  have "{a. fst (y a) \<noteq> b} \<subseteq> {a. y a \<noteq> (b, c)}"
Andreas@48028
   156
    and "{a. snd (y a) \<noteq> c} \<subseteq> {a. y a \<noteq> (b, c)}" by auto
Andreas@48028
   157
  hence "finite {a. fst (y a) \<noteq> b}" 
Andreas@48028
   158
    and "finite {a. snd (y a) \<noteq> c}" using bc by(auto intro: finite_subset)
Andreas@48028
   159
  thus "fst \<circ> y \<in> finfun" "snd \<circ> y \<in> finfun"
Andreas@48028
   160
    unfolding finfun_def by auto
Andreas@48028
   161
qed
Andreas@48028
   162
Andreas@48028
   163
lemma map_of_finfun: "map_of xs \<in> finfun"
Andreas@48028
   164
unfolding finfun_def
Andreas@48028
   165
by(induct xs)(auto simp add: Collect_neg_eq Collect_conj_eq Collect_imp_eq intro: finite_subset)
Andreas@48028
   166
Andreas@48028
   167
lemma Diag_finfun: "(\<lambda>x. (f x, g x)) \<in> finfun \<longleftrightarrow> f \<in> finfun \<and> g \<in> finfun"
Andreas@48028
   168
by(auto intro: finite_subset simp add: Collect_neg_eq Collect_imp_eq Collect_conj_eq finfun_def)
Andreas@48028
   169
Andreas@48028
   170
lemma finfun_right_compose:
Andreas@48028
   171
  assumes g: "g \<in> finfun" and inj: "inj f"
Andreas@48028
   172
  shows "g o f \<in> finfun"
Andreas@48028
   173
proof -
Andreas@48028
   174
  from g obtain b where b: "finite {a. g a \<noteq> b}" unfolding finfun_def by blast
Andreas@48028
   175
  moreover have "f ` {a. g (f a) \<noteq> b} \<subseteq> {a. g a \<noteq> b}" by auto
Andreas@48028
   176
  moreover from inj have "inj_on f {a.  g (f a) \<noteq> b}" by(rule subset_inj_on) blast
Andreas@48028
   177
  ultimately have "finite {a. g (f a) \<noteq> b}"
Andreas@48028
   178
    by(blast intro: finite_imageD[where f=f] finite_subset)
Andreas@48028
   179
  thus ?thesis unfolding finfun_def by auto
Andreas@48028
   180
qed
Andreas@48028
   181
Andreas@48028
   182
lemma finfun_curry:
Andreas@48028
   183
  assumes fin: "f \<in> finfun"
Andreas@48028
   184
  shows "curry f \<in> finfun" "curry f a \<in> finfun"
Andreas@48028
   185
proof -
Andreas@48028
   186
  from fin obtain c where c: "finite {ab. f ab \<noteq> c}" unfolding finfun_def by blast
Andreas@48028
   187
  moreover have "{a. \<exists>b. f (a, b) \<noteq> c} = fst ` {ab. f ab \<noteq> c}" by(force)
Andreas@48028
   188
  hence "{a. curry f a \<noteq> (\<lambda>b. c)} = fst ` {ab. f ab \<noteq> c}"
Andreas@48028
   189
    by(auto simp add: curry_def fun_eq_iff)
Andreas@48028
   190
  ultimately have "finite {a. curry f a \<noteq> (\<lambda>b. c)}" by simp
Andreas@48028
   191
  thus "curry f \<in> finfun" unfolding finfun_def by blast
Andreas@48028
   192
  
Andreas@48028
   193
  have "snd ` {ab. f ab \<noteq> c} = {b. \<exists>a. f (a, b) \<noteq> c}" by(force)
Andreas@48028
   194
  hence "{b. f (a, b) \<noteq> c} \<subseteq> snd ` {ab. f ab \<noteq> c}" by auto
Andreas@48028
   195
  hence "finite {b. f (a, b) \<noteq> c}" by(rule finite_subset)(rule finite_imageI[OF c])
Andreas@48028
   196
  thus "curry f a \<in> finfun" unfolding finfun_def by auto
Andreas@48028
   197
qed
Andreas@48028
   198
Andreas@48030
   199
bundle finfun =
Andreas@48030
   200
  fst_finfun[simp] snd_finfun[simp] Abs_finfun_inverse[simp] 
Andreas@48030
   201
  finfun_apply_inverse[simp] Abs_finfun_inject[simp] finfun_apply_inject[simp]
Andreas@48030
   202
  Diag_finfun[simp] finfun_curry[simp]
Andreas@48030
   203
  const_finfun[iff] fun_upd_finfun[iff] finfun_apply[iff] map_of_finfun[iff]
Andreas@48030
   204
  finfun_left_compose[intro] fst_finfun[intro] snd_finfun[intro]
Andreas@48028
   205
Andreas@48028
   206
lemma Abs_finfun_inject_finite:
Andreas@48028
   207
  fixes x y :: "'a \<Rightarrow> 'b"
Andreas@48028
   208
  assumes fin: "finite (UNIV :: 'a set)"
Andreas@48028
   209
  shows "Abs_finfun x = Abs_finfun y \<longleftrightarrow> x = y"
Andreas@48028
   210
proof
Andreas@48028
   211
  assume "Abs_finfun x = Abs_finfun y"
Andreas@48028
   212
  moreover have "x \<in> finfun" "y \<in> finfun" unfolding finfun_def
Andreas@48028
   213
    by(auto intro: finite_subset[OF _ fin])
Andreas@48028
   214
  ultimately show "x = y" by(simp add: Abs_finfun_inject)
Andreas@48028
   215
qed simp
Andreas@48028
   216
Andreas@48028
   217
lemma Abs_finfun_inject_finite_class:
Andreas@48028
   218
  fixes x y :: "('a :: finite) \<Rightarrow> 'b"
Andreas@48028
   219
  shows "Abs_finfun x = Abs_finfun y \<longleftrightarrow> x = y"
Andreas@48028
   220
using finite_UNIV
Andreas@48028
   221
by(simp add: Abs_finfun_inject_finite)
Andreas@48028
   222
Andreas@48028
   223
lemma Abs_finfun_inj_finite:
Andreas@48028
   224
  assumes fin: "finite (UNIV :: 'a set)"
Andreas@48034
   225
  shows "inj (Abs_finfun :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow>f 'b)"
Andreas@48028
   226
proof(rule inj_onI)
Andreas@48028
   227
  fix x y :: "'a \<Rightarrow> 'b"
Andreas@48028
   228
  assume "Abs_finfun x = Abs_finfun y"
Andreas@48028
   229
  moreover have "x \<in> finfun" "y \<in> finfun" unfolding finfun_def
Andreas@48028
   230
    by(auto intro: finite_subset[OF _ fin])
Andreas@48028
   231
  ultimately show "x = y" by(simp add: Abs_finfun_inject)
Andreas@48028
   232
qed
Andreas@48028
   233
Andreas@48028
   234
lemma Abs_finfun_inverse_finite:
Andreas@48028
   235
  fixes x :: "'a \<Rightarrow> 'b"
Andreas@48028
   236
  assumes fin: "finite (UNIV :: 'a set)"
Andreas@48029
   237
  shows "finfun_apply (Abs_finfun x) = x"
Andreas@48030
   238
  including finfun
Andreas@48028
   239
proof -
Andreas@48028
   240
  from fin have "x \<in> finfun"
Andreas@48028
   241
    by(auto simp add: finfun_def intro: finite_subset)
Andreas@48028
   242
  thus ?thesis by simp
Andreas@48028
   243
qed
Andreas@48028
   244
Andreas@48028
   245
lemma Abs_finfun_inverse_finite_class:
Andreas@48028
   246
  fixes x :: "('a :: finite) \<Rightarrow> 'b"
Andreas@48029
   247
  shows "finfun_apply (Abs_finfun x) = x"
Andreas@48028
   248
using finite_UNIV by(simp add: Abs_finfun_inverse_finite)
Andreas@48028
   249
Andreas@48028
   250
lemma finfun_eq_finite_UNIV: "finite (UNIV :: 'a set) \<Longrightarrow> (finfun :: ('a \<Rightarrow> 'b) set) = UNIV"
Andreas@48028
   251
unfolding finfun_def by(auto intro: finite_subset)
Andreas@48028
   252
Andreas@48028
   253
lemma finfun_finite_UNIV_class: "finfun = (UNIV :: ('a :: finite \<Rightarrow> 'b) set)"
Andreas@48028
   254
by(simp add: finfun_eq_finite_UNIV)
Andreas@48028
   255
Andreas@48028
   256
lemma map_default_in_finfun:
Andreas@48028
   257
  assumes fin: "finite (dom f)"
Andreas@48028
   258
  shows "map_default b f \<in> finfun"
Andreas@48028
   259
unfolding finfun_def
Andreas@48028
   260
proof(intro CollectI exI)
Andreas@48028
   261
  from fin show "finite {a. map_default b f a \<noteq> b}"
Andreas@48028
   262
    by(auto simp add: map_default_def dom_def Collect_conj_eq split: option.splits)
Andreas@48028
   263
qed
Andreas@48028
   264
Andreas@48028
   265
lemma finfun_cases_map_default:
Andreas@48028
   266
  obtains b g where "f = Abs_finfun (map_default b g)" "finite (dom g)" "b \<notin> ran g"
Andreas@48028
   267
proof -
Andreas@48028
   268
  obtain y where f: "f = Abs_finfun y" and y: "y \<in> finfun" by(cases f)
Andreas@48028
   269
  from y obtain b where b: "finite {a. y a \<noteq> b}" unfolding finfun_def by auto
Andreas@48028
   270
  let ?g = "(\<lambda>a. if y a = b then None else Some (y a))"
Andreas@48028
   271
  have "map_default b ?g = y" by(simp add: fun_eq_iff map_default_def)
Andreas@48028
   272
  with f have "f = Abs_finfun (map_default b ?g)" by simp
Andreas@48028
   273
  moreover from b have "finite (dom ?g)" by(auto simp add: dom_def)
Andreas@48028
   274
  moreover have "b \<notin> ran ?g" by(auto simp add: ran_def)
Andreas@48028
   275
  ultimately show ?thesis by(rule that)
Andreas@48028
   276
qed
Andreas@48028
   277
Andreas@48028
   278
Andreas@48034
   279
subsection {* Kernel functions for type @{typ "'a \<Rightarrow>f 'b"} *}
Andreas@48028
   280
Andreas@48036
   281
lift_definition finfun_const :: "'b \<Rightarrow> 'a \<Rightarrow>f 'b" ("K$/ _" [0] 1)
Andreas@48028
   282
is "\<lambda> b x. b" by (rule const_finfun)
Andreas@48028
   283
Andreas@48036
   284
lift_definition finfun_update :: "'a \<Rightarrow>f 'b \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow>f 'b" ("_'(_ $:= _')" [1000,0,0] 1000) is "fun_upd"
Andreas@48034
   285
by (simp add: fun_upd_finfun)
Andreas@48028
   286
Andreas@48036
   287
lemma finfun_update_twist: "a \<noteq> a' \<Longrightarrow> f(a $:= b)(a' $:= b') = f(a' $:= b')(a $:= b)"
Andreas@48028
   288
by transfer (simp add: fun_upd_twist)
Andreas@48028
   289
Andreas@48028
   290
lemma finfun_update_twice [simp]:
Andreas@48036
   291
  "f(a $:= b)(a $:= b') = f(a $:= b')"
Andreas@48028
   292
by transfer simp
Andreas@48028
   293
Andreas@48036
   294
lemma finfun_update_const_same: "(K$ b)(a $:= b) = (K$ b)"
Andreas@48028
   295
by transfer (simp add: fun_eq_iff)
Andreas@48028
   296
Andreas@48028
   297
subsection {* Code generator setup *}
Andreas@48028
   298
Andreas@48036
   299
definition finfun_update_code :: "'a \<Rightarrow>f 'b \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow>f 'b"
Andreas@48028
   300
where [simp, code del]: "finfun_update_code = finfun_update"
Andreas@48028
   301
Andreas@48028
   302
code_datatype finfun_const finfun_update_code
Andreas@48028
   303
Andreas@48028
   304
lemma finfun_update_const_code [code]:
Andreas@48036
   305
  "(K$ b)(a $:= b') = (if b = b' then (K$ b) else finfun_update_code (K$ b) a b')"
Andreas@48028
   306
by(simp add: finfun_update_const_same)
Andreas@48028
   307
Andreas@48028
   308
lemma finfun_update_update_code [code]:
Andreas@48036
   309
  "(finfun_update_code f a b)(a' $:= b') = (if a = a' then f(a $:= b') else finfun_update_code (f(a' $:= b')) a b)"
Andreas@48028
   310
by(simp add: finfun_update_twist)
Andreas@48028
   311
Andreas@48028
   312
Andreas@48028
   313
subsection {* Setup for quickcheck *}
Andreas@48028
   314
Andreas@48034
   315
quickcheck_generator finfun constructors: finfun_update_code, "finfun_const :: 'b \<Rightarrow> 'a \<Rightarrow>f 'b"
Andreas@48028
   316
Andreas@48028
   317
subsection {* @{text "finfun_update"} as instance of @{text "comp_fun_commute"} *}
Andreas@48028
   318
Andreas@48036
   319
interpretation finfun_update: comp_fun_commute "\<lambda>a f. f(a :: 'a $:= b')"
Andreas@48030
   320
  including finfun
Andreas@48028
   321
proof
Andreas@48028
   322
  fix a a' :: 'a
Andreas@48036
   323
  show "(\<lambda>f. f(a $:= b')) \<circ> (\<lambda>f. f(a' $:= b')) = (\<lambda>f. f(a' $:= b')) \<circ> (\<lambda>f. f(a $:= b'))"
Andreas@48028
   324
  proof
Andreas@48028
   325
    fix b
Andreas@48029
   326
    have "(finfun_apply b)(a := b', a' := b') = (finfun_apply b)(a' := b', a := b')"
Andreas@48028
   327
      by(cases "a = a'")(auto simp add: fun_upd_twist)
Andreas@48036
   328
    then have "b(a $:= b')(a' $:= b') = b(a' $:= b')(a $:= b')"
Andreas@48028
   329
      by(auto simp add: finfun_update_def fun_upd_twist)
Andreas@48036
   330
    then show "((\<lambda>f. f(a $:= b')) \<circ> (\<lambda>f. f(a' $:= b'))) b = ((\<lambda>f. f(a' $:= b')) \<circ> (\<lambda>f. f(a $:= b'))) b"
Andreas@48028
   331
      by (simp add: fun_eq_iff)
Andreas@48028
   332
  qed
Andreas@48028
   333
qed
Andreas@48028
   334
Andreas@48028
   335
lemma fold_finfun_update_finite_univ:
Andreas@48028
   336
  assumes fin: "finite (UNIV :: 'a set)"
Andreas@48036
   337
  shows "Finite_Set.fold (\<lambda>a f. f(a $:= b')) (K$ b) (UNIV :: 'a set) = (K$ b')"
Andreas@48028
   338
proof -
Andreas@48028
   339
  { fix A :: "'a set"
Andreas@48028
   340
    from fin have "finite A" by(auto intro: finite_subset)
Andreas@48036
   341
    hence "Finite_Set.fold (\<lambda>a f. f(a $:= b')) (K$ b) A = Abs_finfun (\<lambda>a. if a \<in> A then b' else b)"
Andreas@48028
   342
    proof(induct)
Andreas@48028
   343
      case (insert x F)
Andreas@48028
   344
      have "(\<lambda>a. if a = x then b' else (if a \<in> F then b' else b)) = (\<lambda>a. if a = x \<or> a \<in> F then b' else b)"
Andreas@48037
   345
        by(auto)
Andreas@48028
   346
      with insert show ?case
Andreas@48028
   347
        by(simp add: finfun_const_def fun_upd_def)(simp add: finfun_update_def Abs_finfun_inverse_finite[OF fin] fun_upd_def)
Andreas@48028
   348
    qed(simp add: finfun_const_def) }
Andreas@48028
   349
  thus ?thesis by(simp add: finfun_const_def)
Andreas@48028
   350
qed
Andreas@48028
   351
Andreas@48028
   352
Andreas@48028
   353
subsection {* Default value for FinFuns *}
Andreas@48028
   354
Andreas@48028
   355
definition finfun_default_aux :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b"
Andreas@48028
   356
where [code del]: "finfun_default_aux f = (if finite (UNIV :: 'a set) then undefined else THE b. finite {a. f a \<noteq> b})"
Andreas@48028
   357
Andreas@48028
   358
lemma finfun_default_aux_infinite:
Andreas@48028
   359
  fixes f :: "'a \<Rightarrow> 'b"
Andreas@48028
   360
  assumes infin: "\<not> finite (UNIV :: 'a set)"
Andreas@48028
   361
  and fin: "finite {a. f a \<noteq> b}"
Andreas@48028
   362
  shows "finfun_default_aux f = b"
Andreas@48028
   363
proof -
Andreas@48028
   364
  let ?B = "{a. f a \<noteq> b}"
Andreas@48028
   365
  from fin have "(THE b. finite {a. f a \<noteq> b}) = b"
Andreas@48028
   366
  proof(rule the_equality)
Andreas@48028
   367
    fix b'
Andreas@48028
   368
    assume "finite {a. f a \<noteq> b'}" (is "finite ?B'")
Andreas@48028
   369
    with infin fin have "UNIV - (?B' \<union> ?B) \<noteq> {}" by(auto dest: finite_subset)
Andreas@48028
   370
    then obtain a where a: "a \<notin> ?B' \<union> ?B" by auto
Andreas@48028
   371
    thus "b' = b" by auto
Andreas@48028
   372
  qed
Andreas@48028
   373
  thus ?thesis using infin by(simp add: finfun_default_aux_def)
Andreas@48028
   374
qed
Andreas@48028
   375
Andreas@48028
   376
Andreas@48028
   377
lemma finite_finfun_default_aux:
Andreas@48028
   378
  fixes f :: "'a \<Rightarrow> 'b"
Andreas@48028
   379
  assumes fin: "f \<in> finfun"
Andreas@48028
   380
  shows "finite {a. f a \<noteq> finfun_default_aux f}"
Andreas@48028
   381
proof(cases "finite (UNIV :: 'a set)")
Andreas@48028
   382
  case True thus ?thesis using fin
Andreas@48028
   383
    by(auto simp add: finfun_def finfun_default_aux_def intro: finite_subset)
Andreas@48028
   384
next
Andreas@48028
   385
  case False
Andreas@48028
   386
  from fin obtain b where b: "finite {a. f a \<noteq> b}" (is "finite ?B")
Andreas@48028
   387
    unfolding finfun_def by blast
Andreas@48028
   388
  with False show ?thesis by(simp add: finfun_default_aux_infinite)
Andreas@48028
   389
qed
Andreas@48028
   390
Andreas@48028
   391
lemma finfun_default_aux_update_const:
Andreas@48028
   392
  fixes f :: "'a \<Rightarrow> 'b"
Andreas@48028
   393
  assumes fin: "f \<in> finfun"
Andreas@48028
   394
  shows "finfun_default_aux (f(a := b)) = finfun_default_aux f"
Andreas@48028
   395
proof(cases "finite (UNIV :: 'a set)")
Andreas@48028
   396
  case False
Andreas@48028
   397
  from fin obtain b' where b': "finite {a. f a \<noteq> b'}" unfolding finfun_def by blast
Andreas@48028
   398
  hence "finite {a'. (f(a := b)) a' \<noteq> b'}"
Andreas@48028
   399
  proof(cases "b = b' \<and> f a \<noteq> b'") 
Andreas@48028
   400
    case True
Andreas@48028
   401
    hence "{a. f a \<noteq> b'} = insert a {a'. (f(a := b)) a' \<noteq> b'}" by auto
Andreas@48028
   402
    thus ?thesis using b' by simp
Andreas@48028
   403
  next
Andreas@48028
   404
    case False
Andreas@48028
   405
    moreover
Andreas@48028
   406
    { assume "b \<noteq> b'"
Andreas@48028
   407
      hence "{a'. (f(a := b)) a' \<noteq> b'} = insert a {a. f a \<noteq> b'}" by auto
Andreas@48028
   408
      hence ?thesis using b' by simp }
Andreas@48028
   409
    moreover
Andreas@48028
   410
    { assume "b = b'" "f a = b'"
Andreas@48028
   411
      hence "{a'. (f(a := b)) a' \<noteq> b'} = {a. f a \<noteq> b'}" by auto
Andreas@48028
   412
      hence ?thesis using b' by simp }
Andreas@48028
   413
    ultimately show ?thesis by blast
Andreas@48028
   414
  qed
Andreas@48028
   415
  with False b' show ?thesis by(auto simp del: fun_upd_apply simp add: finfun_default_aux_infinite)
Andreas@48028
   416
next
Andreas@48028
   417
  case True thus ?thesis by(simp add: finfun_default_aux_def)
Andreas@48028
   418
qed
Andreas@48028
   419
Andreas@48034
   420
lift_definition finfun_default :: "'a \<Rightarrow>f 'b \<Rightarrow> 'b"
Andreas@48028
   421
is "finfun_default_aux" ..
Andreas@48028
   422
Andreas@48029
   423
lemma finite_finfun_default: "finite {a. finfun_apply f a \<noteq> finfun_default f}"
Andreas@48031
   424
by transfer (erule finite_finfun_default_aux)
Andreas@48028
   425
Andreas@48036
   426
lemma finfun_default_const: "finfun_default ((K$ b) :: 'a \<Rightarrow>f 'b) = (if finite (UNIV :: 'a set) then undefined else b)"
Andreas@48031
   427
by(transfer)(auto simp add: finfun_default_aux_infinite finfun_default_aux_def)
Andreas@48028
   428
Andreas@48028
   429
lemma finfun_default_update_const:
Andreas@48036
   430
  "finfun_default (f(a $:= b)) = finfun_default f"
Andreas@48028
   431
by transfer (simp add: finfun_default_aux_update_const)
Andreas@48028
   432
Andreas@48028
   433
lemma finfun_default_const_code [code]:
Andreas@48070
   434
  "finfun_default ((K$ c) :: 'a :: card_UNIV \<Rightarrow>f 'b) = (if CARD('a) = 0 then c else undefined)"
Andreas@48059
   435
by(simp add: finfun_default_const)
Andreas@48028
   436
Andreas@48028
   437
lemma finfun_default_update_code [code]:
Andreas@48028
   438
  "finfun_default (finfun_update_code f a b) = finfun_default f"
Andreas@48028
   439
by(simp add: finfun_default_update_const)
Andreas@48028
   440
Andreas@48028
   441
subsection {* Recursion combinator and well-formedness conditions *}
Andreas@48028
   442
Andreas@48034
   443
definition finfun_rec :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow>f 'b) \<Rightarrow> 'c"
Andreas@48028
   444
where [code del]:
Andreas@48028
   445
  "finfun_rec cnst upd f \<equiv>
Andreas@48028
   446
   let b = finfun_default f;
Andreas@48028
   447
       g = THE g. f = Abs_finfun (map_default b g) \<and> finite (dom g) \<and> b \<notin> ran g
Andreas@48028
   448
   in Finite_Set.fold (\<lambda>a. upd a (map_default b g a)) (cnst b) (dom g)"
Andreas@48028
   449
Andreas@48028
   450
locale finfun_rec_wf_aux =
Andreas@48028
   451
  fixes cnst :: "'b \<Rightarrow> 'c"
Andreas@48028
   452
  and upd :: "'a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c"
Andreas@48028
   453
  assumes upd_const_same: "upd a b (cnst b) = cnst b"
Andreas@48028
   454
  and upd_commute: "a \<noteq> a' \<Longrightarrow> upd a b (upd a' b' c) = upd a' b' (upd a b c)"
Andreas@48028
   455
  and upd_idemp: "b \<noteq> b' \<Longrightarrow> upd a b'' (upd a b' (cnst b)) = upd a b'' (cnst b)"
Andreas@48028
   456
begin
Andreas@48028
   457
Andreas@48028
   458
Andreas@48028
   459
lemma upd_left_comm: "comp_fun_commute (\<lambda>a. upd a (f a))"
Andreas@48028
   460
by(unfold_locales)(auto intro: upd_commute simp add: fun_eq_iff)
Andreas@48028
   461
Andreas@48028
   462
lemma upd_upd_twice: "upd a b'' (upd a b' (cnst b)) = upd a b'' (cnst b)"
Andreas@48028
   463
by(cases "b \<noteq> b'")(auto simp add: fun_upd_def upd_const_same upd_idemp)
Andreas@48028
   464
Andreas@48028
   465
lemma map_default_update_const:
Andreas@48028
   466
  assumes fin: "finite (dom f)"
Andreas@48028
   467
  and anf: "a \<notin> dom f"
Andreas@48028
   468
  and fg: "f \<subseteq>\<^sub>m g"
Andreas@48028
   469
  shows "upd a d  (Finite_Set.fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f)) =
Andreas@48028
   470
         Finite_Set.fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f)"
Andreas@48028
   471
proof -
Andreas@48028
   472
  let ?upd = "\<lambda>a. upd a (map_default d g a)"
Andreas@48028
   473
  let ?fr = "\<lambda>A. Finite_Set.fold ?upd (cnst d) A"
Andreas@48028
   474
  interpret gwf: comp_fun_commute "?upd" by(rule upd_left_comm)
Andreas@48028
   475
  
Andreas@48028
   476
  from fin anf fg show ?thesis
Andreas@48028
   477
  proof(induct "dom f" arbitrary: f)
Andreas@48028
   478
    case empty
Andreas@48037
   479
    from `{} = dom f` have "f = empty" by(auto simp add: dom_def)
Andreas@48028
   480
    thus ?case by(simp add: finfun_const_def upd_const_same)
Andreas@48028
   481
  next
Andreas@48028
   482
    case (insert a' A)
Andreas@48028
   483
    note IH = `\<And>f.  \<lbrakk> A = dom f; a \<notin> dom f; f \<subseteq>\<^sub>m g \<rbrakk> \<Longrightarrow> upd a d (?fr (dom f)) = ?fr (dom f)`
Andreas@48028
   484
    note fin = `finite A` note anf = `a \<notin> dom f` note a'nA = `a' \<notin> A`
Andreas@48028
   485
    note domf = `insert a' A = dom f` note fg = `f \<subseteq>\<^sub>m g`
Andreas@48028
   486
    
Andreas@48028
   487
    from domf obtain b where b: "f a' = Some b" by auto
Andreas@48028
   488
    let ?f' = "f(a' := None)"
Andreas@48028
   489
    have "upd a d (?fr (insert a' A)) = upd a d (upd a' (map_default d g a') (?fr A))"
Andreas@48028
   490
      by(subst gwf.fold_insert[OF fin a'nA]) rule
Andreas@48028
   491
    also from b fg have "g a' = f a'" by(auto simp add: map_le_def intro: domI dest: bspec)
Andreas@48028
   492
    hence ga': "map_default d g a' = map_default d f a'" by(simp add: map_default_def)
Andreas@48028
   493
    also from anf domf have "a \<noteq> a'" by auto note upd_commute[OF this]
Andreas@48028
   494
    also from domf a'nA anf fg have "a \<notin> dom ?f'" "?f' \<subseteq>\<^sub>m g" and A: "A = dom ?f'" by(auto simp add: ran_def map_le_def)
Andreas@48028
   495
    note A also note IH[OF A `a \<notin> dom ?f'` `?f' \<subseteq>\<^sub>m g`]
Andreas@48028
   496
    also have "upd a' (map_default d f a') (?fr (dom (f(a' := None)))) = ?fr (dom f)"
Andreas@48028
   497
      unfolding domf[symmetric] gwf.fold_insert[OF fin a'nA] ga' unfolding A ..
Andreas@48028
   498
    also have "insert a' (dom ?f') = dom f" using domf by auto
Andreas@48028
   499
    finally show ?case .
Andreas@48028
   500
  qed
Andreas@48028
   501
qed
Andreas@48028
   502
Andreas@48028
   503
lemma map_default_update_twice:
Andreas@48028
   504
  assumes fin: "finite (dom f)"
Andreas@48028
   505
  and anf: "a \<notin> dom f"
Andreas@48028
   506
  and fg: "f \<subseteq>\<^sub>m g"
Andreas@48028
   507
  shows "upd a d'' (upd a d' (Finite_Set.fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f))) =
Andreas@48028
   508
         upd a d'' (Finite_Set.fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f))"
Andreas@48028
   509
proof -
Andreas@48028
   510
  let ?upd = "\<lambda>a. upd a (map_default d g a)"
Andreas@48028
   511
  let ?fr = "\<lambda>A. Finite_Set.fold ?upd (cnst d) A"
Andreas@48028
   512
  interpret gwf: comp_fun_commute "?upd" by(rule upd_left_comm)
Andreas@48028
   513
  
Andreas@48028
   514
  from fin anf fg show ?thesis
Andreas@48028
   515
  proof(induct "dom f" arbitrary: f)
Andreas@48028
   516
    case empty
Andreas@48037
   517
    from `{} = dom f` have "f = empty" by(auto simp add: dom_def)
Andreas@48028
   518
    thus ?case by(auto simp add: finfun_const_def finfun_update_def upd_upd_twice)
Andreas@48028
   519
  next
Andreas@48028
   520
    case (insert a' A)
Andreas@48028
   521
    note IH = `\<And>f. \<lbrakk>A = dom f; a \<notin> dom f; f \<subseteq>\<^sub>m g\<rbrakk> \<Longrightarrow> upd a d'' (upd a d' (?fr (dom f))) = upd a d'' (?fr (dom f))`
Andreas@48028
   522
    note fin = `finite A` note anf = `a \<notin> dom f` note a'nA = `a' \<notin> A`
Andreas@48028
   523
    note domf = `insert a' A = dom f` note fg = `f \<subseteq>\<^sub>m g`
Andreas@48028
   524
    
Andreas@48028
   525
    from domf obtain b where b: "f a' = Some b" by auto
Andreas@48028
   526
    let ?f' = "f(a' := None)"
Andreas@48028
   527
    let ?b' = "case f a' of None \<Rightarrow> d | Some b \<Rightarrow> b"
Andreas@48028
   528
    from domf have "upd a d'' (upd a d' (?fr (dom f))) = upd a d'' (upd a d' (?fr (insert a' A)))" by simp
Andreas@48028
   529
    also note gwf.fold_insert[OF fin a'nA]
Andreas@48028
   530
    also from b fg have "g a' = f a'" by(auto simp add: map_le_def intro: domI dest: bspec)
Andreas@48028
   531
    hence ga': "map_default d g a' = map_default d f a'" by(simp add: map_default_def)
Andreas@48028
   532
    also from anf domf have ana': "a \<noteq> a'" by auto note upd_commute[OF this]
Andreas@48028
   533
    also note upd_commute[OF ana']
Andreas@48028
   534
    also from domf a'nA anf fg have "a \<notin> dom ?f'" "?f' \<subseteq>\<^sub>m g" and A: "A = dom ?f'" by(auto simp add: ran_def map_le_def)
Andreas@48028
   535
    note A also note IH[OF A `a \<notin> dom ?f'` `?f' \<subseteq>\<^sub>m g`]
Andreas@48028
   536
    also note upd_commute[OF ana'[symmetric]] also note ga'[symmetric] also note A[symmetric]
Andreas@48028
   537
    also note gwf.fold_insert[symmetric, OF fin a'nA] also note domf
Andreas@48028
   538
    finally show ?case .
Andreas@48028
   539
  qed
Andreas@48028
   540
qed
Andreas@48028
   541
Andreas@48028
   542
lemma map_default_eq_id [simp]: "map_default d ((\<lambda>a. Some (f a)) |` {a. f a \<noteq> d}) = f"
Andreas@48037
   543
by(auto simp add: map_default_def restrict_map_def)
Andreas@48028
   544
Andreas@48028
   545
lemma finite_rec_cong1:
Andreas@48028
   546
  assumes f: "comp_fun_commute f" and g: "comp_fun_commute g"
Andreas@48028
   547
  and fin: "finite A"
Andreas@48028
   548
  and eq: "\<And>a. a \<in> A \<Longrightarrow> f a = g a"
Andreas@48028
   549
  shows "Finite_Set.fold f z A = Finite_Set.fold g z A"
Andreas@48028
   550
proof -
Andreas@48028
   551
  interpret f: comp_fun_commute f by(rule f)
Andreas@48028
   552
  interpret g: comp_fun_commute g by(rule g)
Andreas@48028
   553
  { fix B
Andreas@48028
   554
    assume BsubA: "B \<subseteq> A"
Andreas@48028
   555
    with fin have "finite B" by(blast intro: finite_subset)
Andreas@48028
   556
    hence "B \<subseteq> A \<Longrightarrow> Finite_Set.fold f z B = Finite_Set.fold g z B"
Andreas@48028
   557
    proof(induct)
Andreas@48028
   558
      case empty thus ?case by simp
Andreas@48028
   559
    next
Andreas@48028
   560
      case (insert a B)
Andreas@48028
   561
      note finB = `finite B` note anB = `a \<notin> B` note sub = `insert a B \<subseteq> A`
Andreas@48028
   562
      note IH = `B \<subseteq> A \<Longrightarrow> Finite_Set.fold f z B = Finite_Set.fold g z B`
Andreas@48028
   563
      from sub anB have BpsubA: "B \<subset> A" and BsubA: "B \<subseteq> A" and aA: "a \<in> A" by auto
Andreas@48028
   564
      from IH[OF BsubA] eq[OF aA] finB anB
Andreas@48028
   565
      show ?case by(auto)
Andreas@48028
   566
    qed
Andreas@48028
   567
    with BsubA have "Finite_Set.fold f z B = Finite_Set.fold g z B" by blast }
Andreas@48028
   568
  thus ?thesis by blast
Andreas@48028
   569
qed
Andreas@48028
   570
Andreas@48028
   571
lemma finfun_rec_upd [simp]:
Andreas@48036
   572
  "finfun_rec cnst upd (f(a' $:= b')) = upd a' b' (finfun_rec cnst upd f)"
Andreas@48030
   573
  including finfun
Andreas@48028
   574
proof -
Andreas@48028
   575
  obtain b where b: "b = finfun_default f" by auto
Andreas@48028
   576
  let ?the = "\<lambda>f g. f = Abs_finfun (map_default b g) \<and> finite (dom g) \<and> b \<notin> ran g"
Andreas@48028
   577
  obtain g where g: "g = The (?the f)" by blast
Andreas@48028
   578
  obtain y where f: "f = Abs_finfun y" and y: "y \<in> finfun" by (cases f)
Andreas@48028
   579
  from f y b have bfin: "finite {a. y a \<noteq> b}" by(simp add: finfun_default_def finite_finfun_default_aux)
Andreas@48028
   580
Andreas@48028
   581
  let ?g = "(\<lambda>a. Some (y a)) |` {a. y a \<noteq> b}"
Andreas@48028
   582
  from bfin have fing: "finite (dom ?g)" by auto
Andreas@48028
   583
  have bran: "b \<notin> ran ?g" by(auto simp add: ran_def restrict_map_def)
Andreas@48028
   584
  have yg: "y = map_default b ?g" by simp
Andreas@48028
   585
  have gg: "g = ?g" unfolding g
Andreas@48028
   586
  proof(rule the_equality)
Andreas@48028
   587
    from f y bfin show "?the f ?g"
Andreas@48028
   588
      by(auto)(simp add: restrict_map_def ran_def split: split_if_asm)
Andreas@48028
   589
  next
Andreas@48028
   590
    fix g'
Andreas@48028
   591
    assume "?the f g'"
Andreas@48028
   592
    hence fin': "finite (dom g')" and ran': "b \<notin> ran g'"
Andreas@48028
   593
      and eq: "Abs_finfun (map_default b ?g) = Abs_finfun (map_default b g')" using f yg by auto
Andreas@48028
   594
    from fin' fing have "map_default b ?g \<in> finfun" "map_default b g' \<in> finfun" by(blast intro: map_default_in_finfun)+
Andreas@48028
   595
    with eq have "map_default b ?g = map_default b g'" by simp
Andreas@48028
   596
    with fing bran fin' ran' show "g' = ?g" by(rule map_default_inject[OF disjI2[OF refl], THEN sym])
Andreas@48028
   597
  qed
Andreas@48028
   598
Andreas@48028
   599
  show ?thesis
Andreas@48028
   600
  proof(cases "b' = b")
Andreas@48028
   601
    case True
Andreas@48028
   602
    note b'b = True
Andreas@48028
   603
Andreas@48028
   604
    let ?g' = "(\<lambda>a. Some ((y(a' := b)) a)) |` {a. (y(a' := b)) a \<noteq> b}"
Andreas@48028
   605
    from bfin b'b have fing': "finite (dom ?g')"
Andreas@48028
   606
      by(auto simp add: Collect_conj_eq Collect_imp_eq intro: finite_subset)
Andreas@48028
   607
    have brang': "b \<notin> ran ?g'" by(auto simp add: ran_def restrict_map_def)
Andreas@48028
   608
Andreas@48028
   609
    let ?b' = "\<lambda>a. case ?g' a of None \<Rightarrow> b | Some b \<Rightarrow> b"
Andreas@48028
   610
    let ?b = "map_default b ?g"
Andreas@48028
   611
    from upd_left_comm upd_left_comm fing'
Andreas@48028
   612
    have "Finite_Set.fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g') = Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g')"
Andreas@48028
   613
      by(rule finite_rec_cong1)(auto simp add: restrict_map_def b'b b map_default_def)
Andreas@48028
   614
    also interpret gwf: comp_fun_commute "\<lambda>a. upd a (?b a)" by(rule upd_left_comm)
Andreas@48028
   615
    have "Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g') = upd a' b' (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g))"
Andreas@48028
   616
    proof(cases "y a' = b")
Andreas@48028
   617
      case True
Andreas@48037
   618
      with b'b have g': "?g' = ?g" by(auto simp add: restrict_map_def)
Andreas@48028
   619
      from True have a'ndomg: "a' \<notin> dom ?g" by auto
Andreas@48028
   620
      from f b'b b show ?thesis unfolding g'
Andreas@48028
   621
        by(subst map_default_update_const[OF fing a'ndomg map_le_refl, symmetric]) simp
Andreas@48028
   622
    next
Andreas@48028
   623
      case False
Andreas@48028
   624
      hence domg: "dom ?g = insert a' (dom ?g')" by auto
Andreas@48028
   625
      from False b'b have a'ndomg': "a' \<notin> dom ?g'" by auto
Andreas@48028
   626
      have "Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (insert a' (dom ?g')) = 
Andreas@48028
   627
            upd a' (?b a') (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'))"
Andreas@48028
   628
        using fing' a'ndomg' unfolding b'b by(rule gwf.fold_insert)
Andreas@48028
   629
      hence "upd a' b (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (insert a' (dom ?g'))) =
Andreas@48028
   630
             upd a' b (upd a' (?b a') (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g')))" by simp
Andreas@48028
   631
      also from b'b have g'leg: "?g' \<subseteq>\<^sub>m ?g" by(auto simp add: restrict_map_def map_le_def)
Andreas@48028
   632
      note map_default_update_twice[OF fing' a'ndomg' this, of b "?b a'" b]
Andreas@48028
   633
      also note map_default_update_const[OF fing' a'ndomg' g'leg, of b]
Andreas@48028
   634
      finally show ?thesis unfolding b'b domg[unfolded b'b] by(rule sym)
Andreas@48028
   635
    qed
Andreas@48036
   636
    also have "The (?the (f(a' $:= b'))) = ?g'"
Andreas@48028
   637
    proof(rule the_equality)
Andreas@48036
   638
      from f y b b'b brang' fing' show "?the (f(a' $:= b')) ?g'"
Andreas@48028
   639
        by(auto simp del: fun_upd_apply simp add: finfun_update_def)
Andreas@48028
   640
    next
Andreas@48028
   641
      fix g'
Andreas@48036
   642
      assume "?the (f(a' $:= b')) g'"
Andreas@48028
   643
      hence fin': "finite (dom g')" and ran': "b \<notin> ran g'"
Andreas@48036
   644
        and eq: "f(a' $:= b') = Abs_finfun (map_default b g')" 
Andreas@48028
   645
        by(auto simp del: fun_upd_apply)
Andreas@48028
   646
      from fin' fing' have "map_default b g' \<in> finfun" "map_default b ?g' \<in> finfun"
Andreas@48028
   647
        by(blast intro: map_default_in_finfun)+
Andreas@48028
   648
      with eq f b'b b have "map_default b ?g' = map_default b g'"
Andreas@48028
   649
        by(simp del: fun_upd_apply add: finfun_update_def)
Andreas@48028
   650
      with fing' brang' fin' ran' show "g' = ?g'"
Andreas@48028
   651
        by(rule map_default_inject[OF disjI2[OF refl], THEN sym])
Andreas@48028
   652
    qed
Andreas@48028
   653
    ultimately show ?thesis unfolding finfun_rec_def Let_def b gg[unfolded g b] using bfin b'b b
Andreas@48028
   654
      by(simp only: finfun_default_update_const map_default_def)
Andreas@48028
   655
  next
Andreas@48028
   656
    case False
Andreas@48028
   657
    note b'b = this
Andreas@48028
   658
    let ?g' = "?g(a' \<mapsto> b')"
Andreas@48028
   659
    let ?b' = "map_default b ?g'"
Andreas@48028
   660
    let ?b = "map_default b ?g"
Andreas@48028
   661
    from fing have fing': "finite (dom ?g')" by auto
Andreas@48028
   662
    from bran b'b have bnrang': "b \<notin> ran ?g'" by(auto simp add: ran_def)
Andreas@48037
   663
    have ffmg': "map_default b ?g' = y(a' := b')" by(auto simp add: map_default_def restrict_map_def)
Andreas@48036
   664
    with f y have f_Abs: "f(a' $:= b') = Abs_finfun (map_default b ?g')" by(auto simp add: finfun_update_def)
Andreas@48036
   665
    have g': "The (?the (f(a' $:= b'))) = ?g'"
Andreas@48028
   666
    proof (rule the_equality)
Andreas@48036
   667
      from fing' bnrang' f_Abs show "?the (f(a' $:= b')) ?g'"
Andreas@48036
   668
        by(auto simp add: finfun_update_def restrict_map_def)
Andreas@48028
   669
    next
Andreas@48036
   670
      fix g' assume "?the (f(a' $:= b')) g'"
Andreas@48036
   671
      hence f': "f(a' $:= b') = Abs_finfun (map_default b g')"
Andreas@48028
   672
        and fin': "finite (dom g')" and brang': "b \<notin> ran g'" by auto
Andreas@48028
   673
      from fing' fin' have "map_default b ?g' \<in> finfun" "map_default b g' \<in> finfun"
Andreas@48028
   674
        by(auto intro: map_default_in_finfun)
Andreas@48028
   675
      with f' f_Abs have "map_default b g' = map_default b ?g'" by simp
Andreas@48028
   676
      with fin' brang' fing' bnrang' show "g' = ?g'"
Andreas@48028
   677
        by(rule map_default_inject[OF disjI2[OF refl]])
Andreas@48028
   678
    qed
Andreas@48028
   679
    have dom: "dom (((\<lambda>a. Some (y a)) |` {a. y a \<noteq> b})(a' \<mapsto> b')) = insert a' (dom ((\<lambda>a. Some (y a)) |` {a. y a \<noteq> b}))"
Andreas@48028
   680
      by auto
Andreas@48028
   681
    show ?thesis
Andreas@48028
   682
    proof(cases "y a' = b")
Andreas@48028
   683
      case True
Andreas@48028
   684
      hence a'ndomg: "a' \<notin> dom ?g" by auto
Andreas@48028
   685
      from f y b'b True have yff: "y = map_default b (?g' |` dom ?g)"
Andreas@48028
   686
        by(auto simp add: restrict_map_def map_default_def intro!: ext)
Andreas@48028
   687
      hence f': "f = Abs_finfun (map_default b (?g' |` dom ?g))" using f by simp
Andreas@48028
   688
      interpret g'wf: comp_fun_commute "\<lambda>a. upd a (?b' a)" by(rule upd_left_comm)
Andreas@48028
   689
      from upd_left_comm upd_left_comm fing
Andreas@48028
   690
      have "Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g) = Finite_Set.fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g)"
Andreas@48028
   691
        by(rule finite_rec_cong1)(auto simp add: restrict_map_def b'b True map_default_def)
Andreas@48028
   692
      thus ?thesis unfolding finfun_rec_def Let_def finfun_default_update_const b[symmetric]
Andreas@48028
   693
        unfolding g' g[symmetric] gg g'wf.fold_insert[OF fing a'ndomg, of "cnst b", folded dom]
Andreas@48028
   694
        by -(rule arg_cong2[where f="upd a'"], simp_all add: map_default_def)
Andreas@48028
   695
    next
Andreas@48028
   696
      case False
Andreas@48028
   697
      hence "insert a' (dom ?g) = dom ?g" by auto
Andreas@48028
   698
      moreover {
Andreas@48028
   699
        let ?g'' = "?g(a' := None)"
Andreas@48028
   700
        let ?b'' = "map_default b ?g''"
Andreas@48028
   701
        from False have domg: "dom ?g = insert a' (dom ?g'')" by auto
Andreas@48028
   702
        from False have a'ndomg'': "a' \<notin> dom ?g''" by auto
Andreas@48028
   703
        have fing'': "finite (dom ?g'')" by(rule finite_subset[OF _ fing]) auto
Andreas@48028
   704
        have bnrang'': "b \<notin> ran ?g''" by(auto simp add: ran_def restrict_map_def)
Andreas@48028
   705
        interpret gwf: comp_fun_commute "\<lambda>a. upd a (?b a)" by(rule upd_left_comm)
Andreas@48028
   706
        interpret g'wf: comp_fun_commute "\<lambda>a. upd a (?b' a)" by(rule upd_left_comm)
Andreas@48028
   707
        have "upd a' b' (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (insert a' (dom ?g''))) =
Andreas@48028
   708
              upd a' b' (upd a' (?b a') (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'')))"
Andreas@48028
   709
          unfolding gwf.fold_insert[OF fing'' a'ndomg''] f ..
Andreas@48028
   710
        also have g''leg: "?g |` dom ?g'' \<subseteq>\<^sub>m ?g" by(auto simp add: map_le_def)
Andreas@48028
   711
        have "dom (?g |` dom ?g'') = dom ?g''" by auto
Andreas@48028
   712
        note map_default_update_twice[where d=b and f = "?g |` dom ?g''" and a=a' and d'="?b a'" and d''=b' and g="?g",
Andreas@48028
   713
                                     unfolded this, OF fing'' a'ndomg'' g''leg]
Andreas@48028
   714
        also have b': "b' = ?b' a'" by(auto simp add: map_default_def)
Andreas@48028
   715
        from upd_left_comm upd_left_comm fing''
Andreas@48036
   716
        have "Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'') =
Andreas@48036
   717
          Finite_Set.fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g'')"
Andreas@48028
   718
          by(rule finite_rec_cong1)(auto simp add: restrict_map_def b'b map_default_def)
Andreas@48028
   719
        with b' have "upd a' b' (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'')) =
Andreas@48028
   720
                     upd a' (?b' a') (Finite_Set.fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g''))" by simp
Andreas@48028
   721
        also note g'wf.fold_insert[OF fing'' a'ndomg'', symmetric]
Andreas@48028
   722
        finally have "upd a' b' (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g)) =
Andreas@48028
   723
                   Finite_Set.fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g)"
Andreas@48028
   724
          unfolding domg . }
Andreas@48028
   725
      ultimately have "Finite_Set.fold (\<lambda>a. upd a (?b' a)) (cnst b) (insert a' (dom ?g)) =
Andreas@48028
   726
                    upd a' b' (Finite_Set.fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g))" by simp
Andreas@48028
   727
      thus ?thesis unfolding finfun_rec_def Let_def finfun_default_update_const b[symmetric] g[symmetric] g' dom[symmetric]
Andreas@48028
   728
        using b'b gg by(simp add: map_default_insert)
Andreas@48028
   729
    qed
Andreas@48028
   730
  qed
Andreas@48028
   731
qed
Andreas@48028
   732
Andreas@48028
   733
end
Andreas@48028
   734
Andreas@48028
   735
locale finfun_rec_wf = finfun_rec_wf_aux + 
Andreas@48028
   736
  assumes const_update_all:
Andreas@48028
   737
  "finite (UNIV :: 'a set) \<Longrightarrow> Finite_Set.fold (\<lambda>a. upd a b') (cnst b) (UNIV :: 'a set) = cnst b'"
Andreas@48028
   738
begin
Andreas@48028
   739
Andreas@48030
   740
lemma finfun_rec_const [simp]: includes finfun shows
Andreas@48036
   741
  "finfun_rec cnst upd (K$ c) = cnst c"
Andreas@48028
   742
proof(cases "finite (UNIV :: 'a set)")
Andreas@48028
   743
  case False
Andreas@48036
   744
  hence "finfun_default ((K$ c) :: 'a \<Rightarrow>f 'b) = c" by(simp add: finfun_default_const)
Andreas@48036
   745
  moreover have "(THE g :: 'a \<rightharpoonup> 'b. (K$ c) = Abs_finfun (map_default c g) \<and> finite (dom g) \<and> c \<notin> ran g) = empty"
Andreas@48028
   746
  proof (rule the_equality)
Andreas@48036
   747
    show "(K$ c) = Abs_finfun (map_default c empty) \<and> finite (dom empty) \<and> c \<notin> ran empty"
Andreas@48028
   748
      by(auto simp add: finfun_const_def)
Andreas@48028
   749
  next
Andreas@48028
   750
    fix g :: "'a \<rightharpoonup> 'b"
Andreas@48036
   751
    assume "(K$ c) = Abs_finfun (map_default c g) \<and> finite (dom g) \<and> c \<notin> ran g"
Andreas@48036
   752
    hence g: "(K$ c) = Abs_finfun (map_default c g)" and fin: "finite (dom g)" and ran: "c \<notin> ran g" by blast+
Andreas@48028
   753
    from g map_default_in_finfun[OF fin, of c] have "map_default c g = (\<lambda>a. c)"
Andreas@48028
   754
      by(simp add: finfun_const_def)
Andreas@48028
   755
    moreover have "map_default c empty = (\<lambda>a. c)" by simp
Andreas@48028
   756
    ultimately show "g = empty" by-(rule map_default_inject[OF disjI2[OF refl] fin ran], auto)
Andreas@48028
   757
  qed
Andreas@48028
   758
  ultimately show ?thesis by(simp add: finfun_rec_def)
Andreas@48028
   759
next
Andreas@48028
   760
  case True
Andreas@48036
   761
  hence default: "finfun_default ((K$ c) :: 'a \<Rightarrow>f 'b) = undefined" by(simp add: finfun_default_const)
Andreas@48036
   762
  let ?the = "\<lambda>g :: 'a \<rightharpoonup> 'b. (K$ c) = Abs_finfun (map_default undefined g) \<and> finite (dom g) \<and> undefined \<notin> ran g"
Andreas@48028
   763
  show ?thesis
Andreas@48028
   764
  proof(cases "c = undefined")
Andreas@48028
   765
    case True
Andreas@48028
   766
    have the: "The ?the = empty"
Andreas@48028
   767
    proof (rule the_equality)
Andreas@48028
   768
      from True show "?the empty" by(auto simp add: finfun_const_def)
Andreas@48028
   769
    next
Andreas@48028
   770
      fix g'
Andreas@48028
   771
      assume "?the g'"
Andreas@48036
   772
      hence fg: "(K$ c) = Abs_finfun (map_default undefined g')"
Andreas@48028
   773
        and fin: "finite (dom g')" and g: "undefined \<notin> ran g'" by simp_all
Andreas@48028
   774
      from fin have "map_default undefined g' \<in> finfun" by(rule map_default_in_finfun)
Andreas@48028
   775
      with fg have "map_default undefined g' = (\<lambda>a. c)"
Andreas@48030
   776
        by(auto simp add: finfun_const_def intro: Abs_finfun_inject[THEN iffD1, symmetric])
Andreas@48028
   777
      with True show "g' = empty"
Andreas@48028
   778
        by -(rule map_default_inject(2)[OF _ fin g], auto)
Andreas@48028
   779
    qed
Andreas@48028
   780
    show ?thesis unfolding finfun_rec_def using `finite UNIV` True
Andreas@48028
   781
      unfolding Let_def the default by(simp)
Andreas@48028
   782
  next
Andreas@48028
   783
    case False
Andreas@48028
   784
    have the: "The ?the = (\<lambda>a :: 'a. Some c)"
Andreas@48028
   785
    proof (rule the_equality)
Andreas@48028
   786
      from False True show "?the (\<lambda>a :: 'a. Some c)"
Andreas@48028
   787
        by(auto simp add: map_default_def [abs_def] finfun_const_def dom_def ran_def)
Andreas@48028
   788
    next
Andreas@48028
   789
      fix g' :: "'a \<rightharpoonup> 'b"
Andreas@48028
   790
      assume "?the g'"
Andreas@48036
   791
      hence fg: "(K$ c) = Abs_finfun (map_default undefined g')"
Andreas@48028
   792
        and fin: "finite (dom g')" and g: "undefined \<notin> ran g'" by simp_all
Andreas@48028
   793
      from fin have "map_default undefined g' \<in> finfun" by(rule map_default_in_finfun)
Andreas@48028
   794
      with fg have "map_default undefined g' = (\<lambda>a. c)"
Andreas@48028
   795
        by(auto simp add: finfun_const_def intro: Abs_finfun_inject[THEN iffD1])
Andreas@48028
   796
      with True False show "g' = (\<lambda>a::'a. Some c)"
Andreas@48028
   797
        by - (rule map_default_inject(2)[OF _ fin g],
Andreas@48028
   798
          auto simp add: dom_def ran_def map_default_def [abs_def])
Andreas@48028
   799
    qed
Andreas@48028
   800
    show ?thesis unfolding finfun_rec_def using True False
Andreas@48028
   801
      unfolding Let_def the default by(simp add: dom_def map_default_def const_update_all)
Andreas@48028
   802
  qed
Andreas@48028
   803
qed
Andreas@48028
   804
Andreas@48028
   805
end
Andreas@48028
   806
Andreas@48028
   807
subsection {* Weak induction rule and case analysis for FinFuns *}
Andreas@48028
   808
Andreas@48028
   809
lemma finfun_weak_induct [consumes 0, case_names const update]:
Andreas@48036
   810
  assumes const: "\<And>b. P (K$ b)"
Andreas@48036
   811
  and update: "\<And>f a b. P f \<Longrightarrow> P (f(a $:= b))"
Andreas@48028
   812
  shows "P x"
Andreas@48030
   813
  including finfun
Andreas@48028
   814
proof(induct x rule: Abs_finfun_induct)
Andreas@48028
   815
  case (Abs_finfun y)
Andreas@48028
   816
  then obtain b where "finite {a. y a \<noteq> b}" unfolding finfun_def by blast
Andreas@48028
   817
  thus ?case using `y \<in> finfun`
Andreas@48028
   818
  proof(induct "{a. y a \<noteq> b}" arbitrary: y rule: finite_induct)
Andreas@48028
   819
    case empty
Andreas@48028
   820
    hence "\<And>a. y a = b" by blast
Andreas@48037
   821
    hence "y = (\<lambda>a. b)" by(auto)
Andreas@48028
   822
    hence "Abs_finfun y = finfun_const b" unfolding finfun_const_def by simp
Andreas@48028
   823
    thus ?case by(simp add: const)
Andreas@48028
   824
  next
Andreas@48028
   825
    case (insert a A)
Andreas@48028
   826
    note IH = `\<And>y. \<lbrakk> A = {a. y a \<noteq> b}; y \<in> finfun  \<rbrakk> \<Longrightarrow> P (Abs_finfun y)`
Andreas@48028
   827
    note y = `y \<in> finfun`
Andreas@48028
   828
    with `insert a A = {a. y a \<noteq> b}` `a \<notin> A`
Andreas@48028
   829
    have "A = {a'. (y(a := b)) a' \<noteq> b}" "y(a := b) \<in> finfun" by auto
Andreas@48028
   830
    from IH[OF this] have "P (finfun_update (Abs_finfun (y(a := b))) a (y a))" by(rule update)
Andreas@48028
   831
    thus ?case using y unfolding finfun_update_def by simp
Andreas@48028
   832
  qed
Andreas@48028
   833
qed
Andreas@48028
   834
Andreas@48028
   835
lemma finfun_exhaust_disj: "(\<exists>b. x = finfun_const b) \<or> (\<exists>f a b. x = finfun_update f a b)"
Andreas@48028
   836
by(induct x rule: finfun_weak_induct) blast+
Andreas@48028
   837
Andreas@48028
   838
lemma finfun_exhaust:
Andreas@48036
   839
  obtains b where "x = (K$ b)"
Andreas@48036
   840
        | f a b where "x = f(a $:= b)"
Andreas@48028
   841
by(atomize_elim)(rule finfun_exhaust_disj)
Andreas@48028
   842
Andreas@48028
   843
lemma finfun_rec_unique:
Andreas@48034
   844
  fixes f :: "'a \<Rightarrow>f 'b \<Rightarrow> 'c"
Andreas@48036
   845
  assumes c: "\<And>c. f (K$ c) = cnst c"
Andreas@48036
   846
  and u: "\<And>g a b. f (g(a $:= b)) = upd g a b (f g)"
Andreas@48036
   847
  and c': "\<And>c. f' (K$ c) = cnst c"
Andreas@48036
   848
  and u': "\<And>g a b. f' (g(a $:= b)) = upd g a b (f' g)"
Andreas@48028
   849
  shows "f = f'"
Andreas@48028
   850
proof
Andreas@48034
   851
  fix g :: "'a \<Rightarrow>f 'b"
Andreas@48028
   852
  show "f g = f' g"
Andreas@48028
   853
    by(induct g rule: finfun_weak_induct)(auto simp add: c u c' u')
Andreas@48028
   854
qed
Andreas@48028
   855
Andreas@48028
   856
Andreas@48028
   857
subsection {* Function application *}
Andreas@48028
   858
Andreas@48035
   859
notation finfun_apply (infixl "$" 999)
Andreas@48028
   860
Andreas@48028
   861
interpretation finfun_apply_aux: finfun_rec_wf_aux "\<lambda>b. b" "\<lambda>a' b c. if (a = a') then b else c"
Andreas@48028
   862
by(unfold_locales) auto
Andreas@48028
   863
Andreas@48028
   864
interpretation finfun_apply: finfun_rec_wf "\<lambda>b. b" "\<lambda>a' b c. if (a = a') then b else c"
Andreas@48028
   865
proof(unfold_locales)
Andreas@48028
   866
  fix b' b :: 'a
Andreas@48028
   867
  assume fin: "finite (UNIV :: 'b set)"
Andreas@48028
   868
  { fix A :: "'b set"
Andreas@48028
   869
    interpret comp_fun_commute "\<lambda>a'. If (a = a') b'" by(rule finfun_apply_aux.upd_left_comm)
Andreas@48028
   870
    from fin have "finite A" by(auto intro: finite_subset)
Andreas@48028
   871
    hence "Finite_Set.fold (\<lambda>a'. If (a = a') b') b A = (if a \<in> A then b' else b)"
Andreas@48028
   872
      by induct auto }
Andreas@48028
   873
  from this[of UNIV] show "Finite_Set.fold (\<lambda>a'. If (a = a') b') b UNIV = b'" by simp
Andreas@48028
   874
qed
Andreas@48028
   875
Andreas@48035
   876
lemma finfun_apply_def: "op $ = (\<lambda>f a. finfun_rec (\<lambda>b. b) (\<lambda>a' b c. if (a = a') then b else c) f)"
Andreas@48029
   877
proof(rule finfun_rec_unique)
Andreas@48036
   878
  fix c show "op $ (K$ c) = (\<lambda>a. c)" by(simp add: finfun_const.rep_eq)
Andreas@48029
   879
next
Andreas@48036
   880
  fix g a b show "op $ g(a $:= b) = (\<lambda>c. if c = a then b else g $ c)"
Andreas@48029
   881
    by(auto simp add: finfun_update_def fun_upd_finfun Abs_finfun_inverse finfun_apply)
Andreas@48029
   882
qed auto
Andreas@48028
   883
Andreas@48036
   884
lemma finfun_upd_apply: "f(a $:= b) $ a' = (if a = a' then b else f $ a')"
Andreas@48035
   885
  and finfun_upd_apply_code [code]: "(finfun_update_code f a b) $ a' = (if a = a' then b else f $ a')"
Andreas@48028
   886
by(simp_all add: finfun_apply_def)
Andreas@48028
   887
Andreas@48036
   888
lemma finfun_const_apply [simp, code]: "(K$ b) $ a = b"
Andreas@48029
   889
by(simp add: finfun_apply_def)
Andreas@48029
   890
Andreas@48028
   891
lemma finfun_upd_apply_same [simp]:
Andreas@48036
   892
  "f(a $:= b) $ a = b"
Andreas@48028
   893
by(simp add: finfun_upd_apply)
Andreas@48028
   894
Andreas@48028
   895
lemma finfun_upd_apply_other [simp]:
Andreas@48036
   896
  "a \<noteq> a' \<Longrightarrow> f(a $:= b) $ a' = f $ a'"
Andreas@48028
   897
by(simp add: finfun_upd_apply)
Andreas@48028
   898
Andreas@48035
   899
lemma finfun_ext: "(\<And>a. f $ a = g $ a) \<Longrightarrow> f = g"
Andreas@48029
   900
by(auto simp add: finfun_apply_inject[symmetric] simp del: finfun_apply_inject)
Andreas@48028
   901
Andreas@48035
   902
lemma expand_finfun_eq: "(f = g) = (op $ f = op $ g)"
Andreas@48028
   903
by(auto intro: finfun_ext)
Andreas@48028
   904
Andreas@48100
   905
lemma finfun_upd_triv [simp]: "f(x $:= f $ x) = f"
Andreas@48100
   906
by(simp add: expand_finfun_eq fun_eq_iff finfun_upd_apply)
Andreas@48100
   907
Andreas@48036
   908
lemma finfun_const_inject [simp]: "(K$ b) = (K$ b') \<equiv> b = b'"
Andreas@48028
   909
by(simp add: expand_finfun_eq fun_eq_iff)
Andreas@48028
   910
Andreas@48028
   911
lemma finfun_const_eq_update:
Andreas@48036
   912
  "((K$ b) = f(a $:= b')) = (b = b' \<and> (\<forall>a'. a \<noteq> a' \<longrightarrow> f $ a' = b))"
Andreas@48028
   913
by(auto simp add: expand_finfun_eq fun_eq_iff finfun_upd_apply)
Andreas@48028
   914
Andreas@48028
   915
subsection {* Function composition *}
Andreas@48028
   916
Andreas@48037
   917
definition finfun_comp :: "('a \<Rightarrow> 'b) \<Rightarrow> 'c \<Rightarrow>f 'a \<Rightarrow> 'c \<Rightarrow>f 'b" (infixr "o$" 55)
Andreas@48037
   918
where [code del]: "g o$ f  = finfun_rec (\<lambda>b. (K$ g b)) (\<lambda>a b c. c(a $:= g b)) f"
Andreas@48037
   919
Andreas@48037
   920
notation (xsymbols) finfun_comp (infixr "\<circ>$" 55)
Andreas@48028
   921
Andreas@48036
   922
interpretation finfun_comp_aux: finfun_rec_wf_aux "(\<lambda>b. (K$ g b))" "(\<lambda>a b c. c(a $:= g b))"
Andreas@48028
   923
by(unfold_locales)(auto simp add: finfun_upd_apply intro: finfun_ext)
Andreas@48028
   924
Andreas@48036
   925
interpretation finfun_comp: finfun_rec_wf "(\<lambda>b. (K$ g b))" "(\<lambda>a b c. c(a $:= g b))"
Andreas@48028
   926
proof
Andreas@48028
   927
  fix b' b :: 'a
Andreas@48028
   928
  assume fin: "finite (UNIV :: 'c set)"
Andreas@48028
   929
  { fix A :: "'c set"
Andreas@48028
   930
    from fin have "finite A" by(auto intro: finite_subset)
Andreas@48036
   931
    hence "Finite_Set.fold (\<lambda>(a :: 'c) c. c(a $:= g b')) (K$ g b) A =
Andreas@48028
   932
      Abs_finfun (\<lambda>a. if a \<in> A then g b' else g b)"
Andreas@48028
   933
      by induct (simp_all add: finfun_const_def, auto simp add: finfun_update_def Abs_finfun_inverse_finite fun_upd_def Abs_finfun_inject_finite fun_eq_iff fin) }
Andreas@48036
   934
  from this[of UNIV] show "Finite_Set.fold (\<lambda>(a :: 'c) c. c(a $:= g b')) (K$ g b) UNIV = (K$ g b')"
Andreas@48028
   935
    by(simp add: finfun_const_def)
Andreas@48028
   936
qed
Andreas@48028
   937
Andreas@48028
   938
lemma finfun_comp_const [simp, code]:
Andreas@48037
   939
  "g \<circ>$ (K$ c) = (K$ g c)"
Andreas@48028
   940
by(simp add: finfun_comp_def)
Andreas@48028
   941
Andreas@48037
   942
lemma finfun_comp_update [simp]: "g \<circ>$ (f(a $:= b)) = (g \<circ>$ f)(a $:= g b)"
Andreas@48037
   943
  and finfun_comp_update_code [code]: 
Andreas@48037
   944
  "g \<circ>$ (finfun_update_code f a b) = finfun_update_code (g \<circ>$ f) a (g b)"
Andreas@48028
   945
by(simp_all add: finfun_comp_def)
Andreas@48028
   946
Andreas@48028
   947
lemma finfun_comp_apply [simp]:
Andreas@48037
   948
  "op $ (g \<circ>$ f) = g \<circ> op $ f"
Andreas@48035
   949
by(induct f rule: finfun_weak_induct)(auto simp add: finfun_upd_apply)
Andreas@48028
   950
Andreas@48037
   951
lemma finfun_comp_comp_collapse [simp]: "f \<circ>$ g \<circ>$ h = (f \<circ> g) \<circ>$ h"
Andreas@48028
   952
by(induct h rule: finfun_weak_induct) simp_all
Andreas@48028
   953
Andreas@48037
   954
lemma finfun_comp_const1 [simp]: "(\<lambda>x. c) \<circ>$ f = (K$ c)"
Andreas@48028
   955
by(induct f rule: finfun_weak_induct)(auto intro: finfun_ext simp add: finfun_upd_apply)
Andreas@48028
   956
Andreas@48037
   957
lemma finfun_comp_id1 [simp]: "(\<lambda>x. x) \<circ>$ f = f" "id \<circ>$ f = f"
Andreas@48028
   958
by(induct f rule: finfun_weak_induct) auto
Andreas@48028
   959
Andreas@48037
   960
lemma finfun_comp_conv_comp: "g \<circ>$ f = Abs_finfun (g \<circ> op $ f)"
Andreas@48030
   961
  including finfun
Andreas@48028
   962
proof -
Andreas@48037
   963
  have "(\<lambda>f. g \<circ>$ f) = (\<lambda>f. Abs_finfun (g \<circ> op $ f))"
Andreas@48028
   964
  proof(rule finfun_rec_unique)
Andreas@48036
   965
    { fix c show "Abs_finfun (g \<circ> op $ (K$ c)) = (K$ g c)"
Andreas@48028
   966
        by(simp add: finfun_comp_def o_def)(simp add: finfun_const_def) }
Andreas@48036
   967
    { fix g' a b show "Abs_finfun (g \<circ> op $ g'(a $:= b)) = (Abs_finfun (g \<circ> op $ g'))(a $:= g b)"
Andreas@48028
   968
      proof -
Andreas@48028
   969
        obtain y where y: "y \<in> finfun" and g': "g' = Abs_finfun y" by(cases g')
Andreas@48035
   970
        moreover hence "(g \<circ> op $ g') \<in> finfun" by(simp add: finfun_left_compose)
Andreas@48035
   971
        moreover have "g \<circ> y(a := b) = (g \<circ> y)(a := g b)" by(auto)
Andreas@48029
   972
        ultimately show ?thesis by(simp add: finfun_comp_def finfun_update_def)
Andreas@48028
   973
      qed }
Andreas@48028
   974
  qed auto
Andreas@48028
   975
  thus ?thesis by(auto simp add: fun_eq_iff)
Andreas@48028
   976
qed
Andreas@48028
   977
Andreas@48037
   978
definition finfun_comp2 :: "'b \<Rightarrow>f 'c \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow>f 'c" (infixr "$o" 55)
Andreas@48037
   979
where [code del]: "g $o f = Abs_finfun (op $ g \<circ> f)"
Andreas@48037
   980
Andreas@48037
   981
notation (xsymbol) finfun_comp2 (infixr "$\<circ>" 55)
Andreas@48028
   982
Andreas@48036
   983
lemma finfun_comp2_const [code, simp]: "finfun_comp2 (K$ c) f = (K$ c)"
Andreas@48030
   984
  including finfun
Andreas@48028
   985
by(simp add: finfun_comp2_def finfun_const_def comp_def)
Andreas@48028
   986
Andreas@48028
   987
lemma finfun_comp2_update:
Andreas@48030
   988
  includes finfun
Andreas@48028
   989
  assumes inj: "inj f"
Andreas@48036
   990
  shows "finfun_comp2 (g(b $:= c)) f = (if b \<in> range f then (finfun_comp2 g f)(inv f b $:= c) else finfun_comp2 g f)"
Andreas@48028
   991
proof(cases "b \<in> range f")
Andreas@48028
   992
  case True
Andreas@48035
   993
  from inj have "\<And>x. (op $ g)(f x := c) \<circ> f = (op $ g \<circ> f)(x := c)" by(auto intro!: ext dest: injD)
Andreas@48028
   994
  with inj True show ?thesis by(auto simp add: finfun_comp2_def finfun_update_def finfun_right_compose)
Andreas@48028
   995
next
Andreas@48028
   996
  case False
Andreas@48035
   997
  hence "(op $ g)(b := c) \<circ> f = op $ g \<circ> f" by(auto simp add: fun_eq_iff)
Andreas@48028
   998
  with False show ?thesis by(auto simp add: finfun_comp2_def finfun_update_def)
Andreas@48028
   999
qed
Andreas@48028
  1000
Andreas@48028
  1001
subsection {* Universal quantification *}
Andreas@48028
  1002
Andreas@48034
  1003
definition finfun_All_except :: "'a list \<Rightarrow> 'a \<Rightarrow>f bool \<Rightarrow> bool"
Andreas@48035
  1004
where [code del]: "finfun_All_except A P \<equiv> \<forall>a. a \<in> set A \<or> P $ a"
Andreas@48028
  1005
Andreas@48036
  1006
lemma finfun_All_except_const: "finfun_All_except A (K$ b) \<longleftrightarrow> b \<or> set A = UNIV"
Andreas@48028
  1007
by(auto simp add: finfun_All_except_def)
Andreas@48028
  1008
Andreas@48028
  1009
lemma finfun_All_except_const_finfun_UNIV_code [code]:
Andreas@48036
  1010
  "finfun_All_except A (K$ b) = (b \<or> is_list_UNIV A)"
Andreas@48028
  1011
by(simp add: finfun_All_except_const is_list_UNIV_iff)
Andreas@48028
  1012
Andreas@48035
  1013
lemma finfun_All_except_update:
Andreas@48036
  1014
  "finfun_All_except A f(a $:= b) = ((a \<in> set A \<or> b) \<and> finfun_All_except (a # A) f)"
Andreas@48028
  1015
by(fastforce simp add: finfun_All_except_def finfun_upd_apply)
Andreas@48028
  1016
Andreas@48028
  1017
lemma finfun_All_except_update_code [code]:
Andreas@48028
  1018
  fixes a :: "'a :: card_UNIV"
Andreas@48028
  1019
  shows "finfun_All_except A (finfun_update_code f a b) = ((a \<in> set A \<or> b) \<and> finfun_All_except (a # A) f)"
Andreas@48028
  1020
by(simp add: finfun_All_except_update)
Andreas@48028
  1021
Andreas@48034
  1022
definition finfun_All :: "'a \<Rightarrow>f bool \<Rightarrow> bool"
Andreas@48028
  1023
where "finfun_All = finfun_All_except []"
Andreas@48028
  1024
Andreas@48036
  1025
lemma finfun_All_const [simp]: "finfun_All (K$ b) = b"
Andreas@48028
  1026
by(simp add: finfun_All_def finfun_All_except_def)
Andreas@48028
  1027
Andreas@48036
  1028
lemma finfun_All_update: "finfun_All f(a $:= b) = (b \<and> finfun_All_except [a] f)"
Andreas@48028
  1029
by(simp add: finfun_All_def finfun_All_except_update)
Andreas@48028
  1030
Andreas@48035
  1031
lemma finfun_All_All: "finfun_All P = All (op $ P)"
Andreas@48028
  1032
by(simp add: finfun_All_def finfun_All_except_def)
Andreas@48028
  1033
Andreas@48028
  1034
Andreas@48034
  1035
definition finfun_Ex :: "'a \<Rightarrow>f bool \<Rightarrow> bool"
Andreas@48037
  1036
where "finfun_Ex P = Not (finfun_All (Not \<circ>$ P))"
Andreas@48028
  1037
Andreas@48035
  1038
lemma finfun_Ex_Ex: "finfun_Ex P = Ex (op $ P)"
Andreas@48028
  1039
unfolding finfun_Ex_def finfun_All_All by simp
Andreas@48028
  1040
Andreas@48036
  1041
lemma finfun_Ex_const [simp]: "finfun_Ex (K$ b) = b"
Andreas@48028
  1042
by(simp add: finfun_Ex_def)
Andreas@48028
  1043
Andreas@48028
  1044
Andreas@48028
  1045
subsection {* A diagonal operator for FinFuns *}
Andreas@48028
  1046
Andreas@48038
  1047
definition finfun_Diag :: "'a \<Rightarrow>f 'b \<Rightarrow> 'a \<Rightarrow>f 'c \<Rightarrow> 'a \<Rightarrow>f ('b \<times> 'c)" ("(1'($_,/ _$'))" [0, 0] 1000)
Andreas@48038
  1048
where [code del]: "($f, g$) = finfun_rec (\<lambda>b. Pair b \<circ>$ g) (\<lambda>a b c. c(a $:= (b, g $ a))) f"
Andreas@48028
  1049
Andreas@48037
  1050
interpretation finfun_Diag_aux: finfun_rec_wf_aux "\<lambda>b. Pair b \<circ>$ g" "\<lambda>a b c. c(a $:= (b, g $ a))"
Andreas@48028
  1051
by(unfold_locales)(simp_all add: expand_finfun_eq fun_eq_iff finfun_upd_apply)
Andreas@48028
  1052
Andreas@48037
  1053
interpretation finfun_Diag: finfun_rec_wf "\<lambda>b. Pair b \<circ>$ g" "\<lambda>a b c. c(a $:= (b, g $ a))"
Andreas@48028
  1054
proof
Andreas@48028
  1055
  fix b' b :: 'a
Andreas@48028
  1056
  assume fin: "finite (UNIV :: 'c set)"
Andreas@48028
  1057
  { fix A :: "'c set"
Andreas@48036
  1058
    interpret comp_fun_commute "\<lambda>a c. c(a $:= (b', g $ a))" by(rule finfun_Diag_aux.upd_left_comm)
Andreas@48028
  1059
    from fin have "finite A" by(auto intro: finite_subset)
Andreas@48037
  1060
    hence "Finite_Set.fold (\<lambda>a c. c(a $:= (b', g $ a))) (Pair b \<circ>$ g) A =
Andreas@48035
  1061
      Abs_finfun (\<lambda>a. (if a \<in> A then b' else b, g $ a))"
Andreas@48028
  1062
      by(induct)(simp_all add: finfun_const_def finfun_comp_conv_comp o_def,
Andreas@48028
  1063
                 auto simp add: finfun_update_def Abs_finfun_inverse_finite fun_upd_def Abs_finfun_inject_finite fun_eq_iff fin) }
Andreas@48037
  1064
  from this[of UNIV] show "Finite_Set.fold (\<lambda>a c. c(a $:= (b', g $ a))) (Pair b \<circ>$ g) UNIV = Pair b' \<circ>$ g"
Andreas@48028
  1065
    by(simp add: finfun_const_def finfun_comp_conv_comp o_def)
Andreas@48028
  1066
qed
Andreas@48028
  1067
Andreas@48038
  1068
lemma finfun_Diag_const1: "($K$ b, g$) = Pair b \<circ>$ g"
Andreas@48028
  1069
by(simp add: finfun_Diag_def)
Andreas@48028
  1070
Andreas@48028
  1071
text {*
Andreas@48038
  1072
  Do not use @{thm finfun_Diag_const1} for the code generator because @{term "Pair b"} is injective, i.e. if @{term g} is free of redundant updates, there is no need to check for redundant updates as is done for @{term "op \<circ>$"}.
Andreas@48028
  1073
*}
Andreas@48028
  1074
Andreas@48028
  1075
lemma finfun_Diag_const_code [code]:
Andreas@48038
  1076
  "($K$ b, K$ c$) = (K$ (b, c))"
Andreas@48038
  1077
  "($K$ b, finfun_update_code g a c$) = finfun_update_code ($K$ b, g$) a (b, c)"
Andreas@48028
  1078
by(simp_all add: finfun_Diag_const1)
Andreas@48028
  1079
Andreas@48038
  1080
lemma finfun_Diag_update1: "($f(a $:= b), g$) = ($f, g$)(a $:= (b, g $ a))"
Andreas@48038
  1081
  and finfun_Diag_update1_code [code]: "($finfun_update_code f a b, g$) = ($f, g$)(a $:= (b, g $ a))"
Andreas@48028
  1082
by(simp_all add: finfun_Diag_def)
Andreas@48028
  1083
Andreas@48038
  1084
lemma finfun_Diag_const2: "($f, K$ c$) = (\<lambda>b. (b, c)) \<circ>$ f"
Andreas@48028
  1085
by(induct f rule: finfun_weak_induct)(auto intro!: finfun_ext simp add: finfun_upd_apply finfun_Diag_const1 finfun_Diag_update1)
Andreas@48028
  1086
Andreas@48038
  1087
lemma finfun_Diag_update2: "($f, g(a $:= c)$) = ($f, g$)(a $:= (f $ a, c))"
Andreas@48028
  1088
by(induct f rule: finfun_weak_induct)(auto intro!: finfun_ext simp add: finfun_upd_apply finfun_Diag_const1 finfun_Diag_update1)
Andreas@48028
  1089
Andreas@48038
  1090
lemma finfun_Diag_const_const [simp]: "($K$ b, K$ c$) = (K$ (b, c))"
Andreas@48028
  1091
by(simp add: finfun_Diag_const1)
Andreas@48028
  1092
Andreas@48028
  1093
lemma finfun_Diag_const_update:
Andreas@48038
  1094
  "($K$ b, g(a $:= c)$) = ($K$ b, g$)(a $:= (b, c))"
Andreas@48028
  1095
by(simp add: finfun_Diag_const1)
Andreas@48028
  1096
Andreas@48028
  1097
lemma finfun_Diag_update_const:
Andreas@48038
  1098
  "($f(a $:= b), K$ c$) = ($f, K$ c$)(a $:= (b, c))"
Andreas@48028
  1099
by(simp add: finfun_Diag_def)
Andreas@48028
  1100
Andreas@48028
  1101
lemma finfun_Diag_update_update:
Andreas@48038
  1102
  "($f(a $:= b), g(a' $:= c)$) = (if a = a' then ($f, g$)(a $:= (b, c)) else ($f, g$)(a $:= (b, g $ a))(a' $:= (f $ a', c)))"
Andreas@48028
  1103
by(auto simp add: finfun_Diag_update1 finfun_Diag_update2)
Andreas@48028
  1104
Andreas@48038
  1105
lemma finfun_Diag_apply [simp]: "op $ ($f, g$) = (\<lambda>x. (f $ x, g $ x))"
Andreas@48035
  1106
by(induct f rule: finfun_weak_induct)(auto simp add: finfun_Diag_const1 finfun_Diag_update1 finfun_upd_apply)
Andreas@48028
  1107
Andreas@48028
  1108
lemma finfun_Diag_conv_Abs_finfun:
Andreas@48038
  1109
  "($f, g$) = Abs_finfun ((\<lambda>x. (f $ x, g $ x)))"
Andreas@48030
  1110
  including finfun
Andreas@48028
  1111
proof -
Andreas@48038
  1112
  have "(\<lambda>f :: 'a \<Rightarrow>f 'b. ($f, g$)) = (\<lambda>f. Abs_finfun ((\<lambda>x. (f $ x, g $ x))))"
Andreas@48028
  1113
  proof(rule finfun_rec_unique)
Andreas@48037
  1114
    { fix c show "Abs_finfun (\<lambda>x. ((K$ c) $ x, g $ x)) = Pair c \<circ>$ g"
Andreas@48029
  1115
        by(simp add: finfun_comp_conv_comp o_def finfun_const_def) }
Andreas@48028
  1116
    { fix g' a b
Andreas@48036
  1117
      show "Abs_finfun (\<lambda>x. (g'(a $:= b) $ x, g $ x)) =
Andreas@48036
  1118
            (Abs_finfun (\<lambda>x. (g' $ x, g $ x)))(a $:= (b, g $ a))"
Andreas@48029
  1119
        by(auto simp add: finfun_update_def fun_eq_iff simp del: fun_upd_apply) simp }
Andreas@48028
  1120
  qed(simp_all add: finfun_Diag_const1 finfun_Diag_update1)
Andreas@48028
  1121
  thus ?thesis by(auto simp add: fun_eq_iff)
Andreas@48028
  1122
qed
Andreas@48028
  1123
Andreas@48038
  1124
lemma finfun_Diag_eq: "($f, g$) = ($f', g'$) \<longleftrightarrow> f = f' \<and> g = g'"
Andreas@48028
  1125
by(auto simp add: expand_finfun_eq fun_eq_iff)
Andreas@48028
  1126
Andreas@48034
  1127
definition finfun_fst :: "'a \<Rightarrow>f ('b \<times> 'c) \<Rightarrow> 'a \<Rightarrow>f 'b"
Andreas@48037
  1128
where [code]: "finfun_fst f = fst \<circ>$ f"
Andreas@48028
  1129
Andreas@48036
  1130
lemma finfun_fst_const: "finfun_fst (K$ bc) = (K$ fst bc)"
Andreas@48028
  1131
by(simp add: finfun_fst_def)
Andreas@48028
  1132
Andreas@48036
  1133
lemma finfun_fst_update: "finfun_fst (f(a $:= bc)) = (finfun_fst f)(a $:= fst bc)"
Andreas@48036
  1134
  and finfun_fst_update_code: "finfun_fst (finfun_update_code f a bc) = (finfun_fst f)(a $:= fst bc)"
Andreas@48028
  1135
by(simp_all add: finfun_fst_def)
Andreas@48028
  1136
Andreas@48037
  1137
lemma finfun_fst_comp_conv: "finfun_fst (f \<circ>$ g) = (fst \<circ> f) \<circ>$ g"
Andreas@48028
  1138
by(simp add: finfun_fst_def)
Andreas@48028
  1139
Andreas@48038
  1140
lemma finfun_fst_conv [simp]: "finfun_fst ($f, g$) = f"
Andreas@48028
  1141
by(induct f rule: finfun_weak_induct)(simp_all add: finfun_Diag_const1 finfun_fst_comp_conv o_def finfun_Diag_update1 finfun_fst_update)
Andreas@48028
  1142
Andreas@48037
  1143
lemma finfun_fst_conv_Abs_finfun: "finfun_fst = (\<lambda>f. Abs_finfun (fst \<circ> op $ f))"
Andreas@48029
  1144
by(simp add: finfun_fst_def [abs_def] finfun_comp_conv_comp)
Andreas@48028
  1145
Andreas@48028
  1146
Andreas@48034
  1147
definition finfun_snd :: "'a \<Rightarrow>f ('b \<times> 'c) \<Rightarrow> 'a \<Rightarrow>f 'c"
Andreas@48037
  1148
where [code]: "finfun_snd f = snd \<circ>$ f"
Andreas@48028
  1149
Andreas@48036
  1150
lemma finfun_snd_const: "finfun_snd (K$ bc) = (K$ snd bc)"
Andreas@48028
  1151
by(simp add: finfun_snd_def)
Andreas@48028
  1152
Andreas@48036
  1153
lemma finfun_snd_update: "finfun_snd (f(a $:= bc)) = (finfun_snd f)(a $:= snd bc)"
Andreas@48036
  1154
  and finfun_snd_update_code [code]: "finfun_snd (finfun_update_code f a bc) = (finfun_snd f)(a $:= snd bc)"
Andreas@48028
  1155
by(simp_all add: finfun_snd_def)
Andreas@48028
  1156
Andreas@48037
  1157
lemma finfun_snd_comp_conv: "finfun_snd (f \<circ>$ g) = (snd \<circ> f) \<circ>$ g"
Andreas@48028
  1158
by(simp add: finfun_snd_def)
Andreas@48028
  1159
Andreas@48038
  1160
lemma finfun_snd_conv [simp]: "finfun_snd ($f, g$) = g"
Andreas@48028
  1161
apply(induct f rule: finfun_weak_induct)
Andreas@48028
  1162
apply(auto simp add: finfun_Diag_const1 finfun_snd_comp_conv o_def finfun_Diag_update1 finfun_snd_update finfun_upd_apply intro: finfun_ext)
Andreas@48028
  1163
done
Andreas@48028
  1164
Andreas@48037
  1165
lemma finfun_snd_conv_Abs_finfun: "finfun_snd = (\<lambda>f. Abs_finfun (snd \<circ> op $ f))"
Andreas@48029
  1166
by(simp add: finfun_snd_def [abs_def] finfun_comp_conv_comp)
Andreas@48028
  1167
Andreas@48038
  1168
lemma finfun_Diag_collapse [simp]: "($finfun_fst f, finfun_snd f$) = f"
Andreas@48028
  1169
by(induct f rule: finfun_weak_induct)(simp_all add: finfun_fst_const finfun_snd_const finfun_fst_update finfun_snd_update finfun_Diag_update_update)
Andreas@48028
  1170
Andreas@48028
  1171
subsection {* Currying for FinFuns *}
Andreas@48028
  1172
Andreas@48034
  1173
definition finfun_curry :: "('a \<times> 'b) \<Rightarrow>f 'c \<Rightarrow> 'a \<Rightarrow>f 'b \<Rightarrow>f 'c"
Andreas@48036
  1174
where [code del]: "finfun_curry = finfun_rec (finfun_const \<circ> finfun_const) (\<lambda>(a, b) c f. f(a $:= (f $ a)(b $:= c)))"
Andreas@48028
  1175
Andreas@48036
  1176
interpretation finfun_curry_aux: finfun_rec_wf_aux "finfun_const \<circ> finfun_const" "\<lambda>(a, b) c f. f(a $:= (f $ a)(b $:= c))"
Andreas@48028
  1177
apply(unfold_locales)
Andreas@48028
  1178
apply(auto simp add: split_def finfun_update_twist finfun_upd_apply split_paired_all finfun_update_const_same)
Andreas@48028
  1179
done
Andreas@48028
  1180
Andreas@48036
  1181
interpretation finfun_curry: finfun_rec_wf "finfun_const \<circ> finfun_const" "\<lambda>(a, b) c f. f(a $:= (f $ a)(b $:= c))"
Andreas@48028
  1182
proof(unfold_locales)
Andreas@48028
  1183
  fix b' b :: 'b
Andreas@48028
  1184
  assume fin: "finite (UNIV :: ('c \<times> 'a) set)"
Andreas@48028
  1185
  hence fin1: "finite (UNIV :: 'c set)" and fin2: "finite (UNIV :: 'a set)"
Andreas@48028
  1186
    unfolding UNIV_Times_UNIV[symmetric]
Andreas@48028
  1187
    by(fastforce dest: finite_cartesian_productD1 finite_cartesian_productD2)+
Andreas@48028
  1188
  note [simp] = Abs_finfun_inverse_finite[OF fin] Abs_finfun_inverse_finite[OF fin1] Abs_finfun_inverse_finite[OF fin2]
Andreas@48028
  1189
  { fix A :: "('c \<times> 'a) set"
Andreas@48036
  1190
    interpret comp_fun_commute "\<lambda>a :: 'c \<times> 'a. (\<lambda>(a, b) c f. f(a $:= (f $ a)(b $:= c))) a b'"
Andreas@48028
  1191
      by(rule finfun_curry_aux.upd_left_comm)
Andreas@48028
  1192
    from fin have "finite A" by(auto intro: finite_subset)
Andreas@48036
  1193
    hence "Finite_Set.fold (\<lambda>a :: 'c \<times> 'a. (\<lambda>(a, b) c f. f(a $:= (f $ a)(b $:= c))) a b') ((finfun_const \<circ> finfun_const) b) A = Abs_finfun (\<lambda>a. Abs_finfun (\<lambda>b''. if (a, b'') \<in> A then b' else b))"
Andreas@48029
  1194
      by induct (simp_all, auto simp add: finfun_update_def finfun_const_def split_def intro!: arg_cong[where f="Abs_finfun"] ext) }
Andreas@48028
  1195
  from this[of UNIV]
Andreas@48036
  1196
  show "Finite_Set.fold (\<lambda>a :: 'c \<times> 'a. (\<lambda>(a, b) c f. f(a $:= (f $ a)(b $:= c))) a b') ((finfun_const \<circ> finfun_const) b) UNIV = (finfun_const \<circ> finfun_const) b'"
Andreas@48028
  1197
    by(simp add: finfun_const_def)
Andreas@48028
  1198
qed
Andreas@48028
  1199
Andreas@48036
  1200
lemma finfun_curry_const [simp, code]: "finfun_curry (K$ c) = (K$ K$ c)"
Andreas@48028
  1201
by(simp add: finfun_curry_def)
Andreas@48028
  1202
Andreas@48028
  1203
lemma finfun_curry_update [simp]:
Andreas@48036
  1204
  "finfun_curry (f((a, b) $:= c)) = (finfun_curry f)(a $:= (finfun_curry f $ a)(b $:= c))"
Andreas@48028
  1205
  and finfun_curry_update_code [code]:
Andreas@48036
  1206
  "finfun_curry (finfun_update_code f (a, b) c) = (finfun_curry f)(a $:= (finfun_curry f $ a)(b $:= c))"
Andreas@48028
  1207
by(simp_all add: finfun_curry_def)
Andreas@48028
  1208
Andreas@48028
  1209
lemma finfun_Abs_finfun_curry: assumes fin: "f \<in> finfun"
Andreas@48028
  1210
  shows "(\<lambda>a. Abs_finfun (curry f a)) \<in> finfun"
Andreas@48030
  1211
  including finfun
Andreas@48028
  1212
proof -
Andreas@48028
  1213
  from fin obtain c where c: "finite {ab. f ab \<noteq> c}" unfolding finfun_def by blast
Andreas@48028
  1214
  have "{a. \<exists>b. f (a, b) \<noteq> c} = fst ` {ab. f ab \<noteq> c}" by(force)
Andreas@48028
  1215
  hence "{a. curry f a \<noteq> (\<lambda>x. c)} = fst ` {ab. f ab \<noteq> c}"
Andreas@48028
  1216
    by(auto simp add: curry_def fun_eq_iff)
Andreas@48036
  1217
  with fin c have "finite {a.  Abs_finfun (curry f a) \<noteq> (K$ c)}"
Andreas@48028
  1218
    by(simp add: finfun_const_def finfun_curry)
Andreas@48028
  1219
  thus ?thesis unfolding finfun_def by auto
Andreas@48028
  1220
qed
Andreas@48028
  1221
Andreas@48028
  1222
lemma finfun_curry_conv_curry:
Andreas@48034
  1223
  fixes f :: "('a \<times> 'b) \<Rightarrow>f 'c"
Andreas@48029
  1224
  shows "finfun_curry f = Abs_finfun (\<lambda>a. Abs_finfun (curry (finfun_apply f) a))"
Andreas@48030
  1225
  including finfun
Andreas@48028
  1226
proof -
Andreas@48034
  1227
  have "finfun_curry = (\<lambda>f :: ('a \<times> 'b) \<Rightarrow>f 'c. Abs_finfun (\<lambda>a. Abs_finfun (curry (finfun_apply f) a)))"
Andreas@48028
  1228
  proof(rule finfun_rec_unique)
Andreas@48036
  1229
    fix c show "finfun_curry (K$ c) = (K$ K$ c)" by simp
Andreas@48034
  1230
    fix f a
Andreas@48036
  1231
    show "finfun_curry (f(a $:= c)) = (finfun_curry f)(fst a $:= (finfun_curry f $ (fst a))(snd a $:= c))"
Andreas@48034
  1232
      by(cases a) simp
Andreas@48036
  1233
    show "Abs_finfun (\<lambda>a. Abs_finfun (curry (finfun_apply (K$ c)) a)) = (K$ K$ c)"
Andreas@48034
  1234
      by(simp add: finfun_curry_def finfun_const_def curry_def)
Andreas@48034
  1235
    fix g b
Andreas@48036
  1236
    show "Abs_finfun (\<lambda>aa. Abs_finfun (curry (op $ g(a $:= b)) aa)) =
Andreas@48036
  1237
      (Abs_finfun (\<lambda>a. Abs_finfun (curry (op $ g) a)))(
Andreas@48036
  1238
      fst a $:= ((Abs_finfun (\<lambda>a. Abs_finfun (curry (op $ g) a))) $ (fst a))(snd a $:= b))"
Andreas@48035
  1239
      by(cases a)(auto intro!: ext arg_cong[where f=Abs_finfun] simp add: finfun_curry_def finfun_update_def finfun_Abs_finfun_curry)
Andreas@48028
  1240
  qed
Andreas@48028
  1241
  thus ?thesis by(auto simp add: fun_eq_iff)
Andreas@48028
  1242
qed
Andreas@48028
  1243
Andreas@48028
  1244
subsection {* Executable equality for FinFuns *}
Andreas@48028
  1245
Andreas@48038
  1246
lemma eq_finfun_All_ext: "(f = g) \<longleftrightarrow> finfun_All ((\<lambda>(x, y). x = y) \<circ>$ ($f, g$))"
Andreas@48028
  1247
by(simp add: expand_finfun_eq fun_eq_iff finfun_All_All o_def)
Andreas@48028
  1248
Andreas@48028
  1249
instantiation finfun :: ("{card_UNIV,equal}",equal) equal begin
Andreas@48038
  1250
definition eq_finfun_def [code]: "HOL.equal f g \<longleftrightarrow> finfun_All ((\<lambda>(x, y). x = y) \<circ>$ ($f, g$))"
Andreas@48028
  1251
instance by(intro_classes)(simp add: eq_finfun_All_ext eq_finfun_def)
Andreas@48028
  1252
end
Andreas@48028
  1253
Andreas@48028
  1254
lemma [code nbe]:
Andreas@48034
  1255
  "HOL.equal (f :: _ \<Rightarrow>f _) f \<longleftrightarrow> True"
Andreas@48028
  1256
  by (fact equal_refl)
Andreas@48028
  1257
Andreas@48028
  1258
subsection {* An operator that explicitly removes all redundant updates in the generated representations *}
Andreas@48028
  1259
Andreas@48034
  1260
definition finfun_clearjunk :: "'a \<Rightarrow>f 'b \<Rightarrow> 'a \<Rightarrow>f 'b"
Andreas@48028
  1261
where [simp, code del]: "finfun_clearjunk = id"
Andreas@48028
  1262
Andreas@48036
  1263
lemma finfun_clearjunk_const [code]: "finfun_clearjunk (K$ b) = (K$ b)"
Andreas@48028
  1264
by simp
Andreas@48028
  1265
Andreas@48036
  1266
lemma finfun_clearjunk_update [code]: 
Andreas@48036
  1267
  "finfun_clearjunk (finfun_update_code f a b) = f(a $:= b)"
Andreas@48028
  1268
by simp
Andreas@48028
  1269
Andreas@48028
  1270
subsection {* The domain of a FinFun as a FinFun *}
Andreas@48028
  1271
Andreas@48034
  1272
definition finfun_dom :: "('a \<Rightarrow>f 'b) \<Rightarrow> ('a \<Rightarrow>f bool)"
Andreas@48035
  1273
where [code del]: "finfun_dom f = Abs_finfun (\<lambda>a. f $ a \<noteq> finfun_default f)"
Andreas@48028
  1274
Andreas@48028
  1275
lemma finfun_dom_const:
Andreas@48036
  1276
  "finfun_dom ((K$ c) :: 'a \<Rightarrow>f 'b) = (K$ finite (UNIV :: 'a set) \<and> c \<noteq> undefined)"
Andreas@48028
  1277
unfolding finfun_dom_def finfun_default_const
Andreas@48028
  1278
by(auto)(simp_all add: finfun_const_def)
Andreas@48028
  1279
Andreas@48028
  1280
text {*
Andreas@48028
  1281
  @{term "finfun_dom" } raises an exception when called on a FinFun whose domain is a finite type. 
Andreas@48028
  1282
  For such FinFuns, the default value (and as such the domain) is undefined.
Andreas@48028
  1283
*}
Andreas@48028
  1284
Andreas@48028
  1285
lemma finfun_dom_const_code [code]:
Andreas@48036
  1286
  "finfun_dom ((K$ c) :: ('a :: card_UNIV) \<Rightarrow>f 'b) = 
Andreas@48059
  1287
   (if CARD('a) = 0 then (K$ False) else FinFun.code_abort (\<lambda>_. finfun_dom (K$ c)))"
Andreas@48059
  1288
by(simp add: finfun_dom_const card_UNIV card_eq_0_iff)
Andreas@48028
  1289
Andreas@48035
  1290
lemma finfun_dom_finfunI: "(\<lambda>a. f $ a \<noteq> finfun_default f) \<in> finfun"
Andreas@48028
  1291
using finite_finfun_default[of f]
Andreas@48029
  1292
by(simp add: finfun_def exI[where x=False])
Andreas@48028
  1293
Andreas@48028
  1294
lemma finfun_dom_update [simp]:
Andreas@48036
  1295
  "finfun_dom (f(a $:= b)) = (finfun_dom f)(a $:= (b \<noteq> finfun_default f))"
Andreas@48030
  1296
including finfun unfolding finfun_dom_def finfun_update_def
Andreas@48029
  1297
apply(simp add: finfun_default_update_const fun_upd_apply finfun_dom_finfunI)
Andreas@48028
  1298
apply(fold finfun_update.rep_eq)
Andreas@48029
  1299
apply(simp add: finfun_upd_apply fun_eq_iff fun_upd_def finfun_default_update_const)
Andreas@48028
  1300
done
Andreas@48028
  1301
Andreas@48028
  1302
lemma finfun_dom_update_code [code]:
Andreas@48028
  1303
  "finfun_dom (finfun_update_code f a b) = finfun_update_code (finfun_dom f) a (b \<noteq> finfun_default f)"
Andreas@48028
  1304
by(simp)
Andreas@48028
  1305
Andreas@48035
  1306
lemma finite_finfun_dom: "finite {x. finfun_dom f $ x}"
Andreas@48028
  1307
proof(induct f rule: finfun_weak_induct)
Andreas@48028
  1308
  case (const b)
Andreas@48028
  1309
  thus ?case
Andreas@48028
  1310
    by (cases "finite (UNIV :: 'a set) \<and> b \<noteq> undefined")
Andreas@48028
  1311
      (auto simp add: finfun_dom_const UNIV_def [symmetric] Set.empty_def [symmetric])
Andreas@48028
  1312
next
Andreas@48028
  1313
  case (update f a b)
Andreas@48036
  1314
  have "{x. finfun_dom f(a $:= b) $ x} =
Andreas@48035
  1315
    (if b = finfun_default f then {x. finfun_dom f $ x} - {a} else insert a {x. finfun_dom f $ x})"
Andreas@48028
  1316
    by (auto simp add: finfun_upd_apply split: split_if_asm)
Andreas@48028
  1317
  thus ?case using update by simp
Andreas@48028
  1318
qed
Andreas@48028
  1319
Andreas@48028
  1320
Andreas@48028
  1321
subsection {* The domain of a FinFun as a sorted list *}
Andreas@48028
  1322
Andreas@48034
  1323
definition finfun_to_list :: "('a :: linorder) \<Rightarrow>f 'b \<Rightarrow> 'a list"
Andreas@48028
  1324
where
Andreas@48035
  1325
  "finfun_to_list f = (THE xs. set xs = {x. finfun_dom f $ x} \<and> sorted xs \<and> distinct xs)"
Andreas@48028
  1326
Andreas@48035
  1327
lemma set_finfun_to_list [simp]: "set (finfun_to_list f) = {x. finfun_dom f $ x}" (is ?thesis1)
Andreas@48028
  1328
  and sorted_finfun_to_list: "sorted (finfun_to_list f)" (is ?thesis2)
Andreas@48028
  1329
  and distinct_finfun_to_list: "distinct (finfun_to_list f)" (is ?thesis3)
Andreas@48028
  1330
proof -
Andreas@48028
  1331
  have "?thesis1 \<and> ?thesis2 \<and> ?thesis3"
Andreas@48028
  1332
    unfolding finfun_to_list_def
Andreas@48028
  1333
    by(rule theI')(rule finite_sorted_distinct_unique finite_finfun_dom)+
Andreas@48028
  1334
  thus ?thesis1 ?thesis2 ?thesis3 by simp_all
Andreas@48028
  1335
qed
Andreas@48028
  1336
Andreas@48036
  1337
lemma finfun_const_False_conv_bot: "op $ (K$ False) = bot"
Andreas@48028
  1338
by auto
Andreas@48028
  1339
Andreas@48036
  1340
lemma finfun_const_True_conv_top: "op $ (K$ True) = top"
Andreas@48028
  1341
by auto
Andreas@48028
  1342
Andreas@48028
  1343
lemma finfun_to_list_const:
Andreas@48036
  1344
  "finfun_to_list ((K$ c) :: ('a :: {linorder} \<Rightarrow>f 'b)) = 
Andreas@48028
  1345
  (if \<not> finite (UNIV :: 'a set) \<or> c = undefined then [] else THE xs. set xs = UNIV \<and> sorted xs \<and> distinct xs)"
Andreas@48028
  1346
by(auto simp add: finfun_to_list_def finfun_const_False_conv_bot finfun_const_True_conv_top finfun_dom_const)
Andreas@48028
  1347
Andreas@48028
  1348
lemma finfun_to_list_const_code [code]:
Andreas@48036
  1349
  "finfun_to_list ((K$ c) :: ('a :: {linorder, card_UNIV} \<Rightarrow>f 'b)) =
Andreas@48059
  1350
   (if CARD('a) = 0 then [] else FinFun.code_abort (\<lambda>_. finfun_to_list ((K$ c) :: ('a \<Rightarrow>f 'b))))"
Andreas@48059
  1351
by(auto simp add: finfun_to_list_const card_UNIV card_eq_0_iff)
Andreas@48028
  1352
Andreas@48028
  1353
lemma remove1_insort_insert_same:
Andreas@48028
  1354
  "x \<notin> set xs \<Longrightarrow> remove1 x (insort_insert x xs) = xs"
Andreas@48028
  1355
by (metis insort_insert_insort remove1_insort)
Andreas@48028
  1356
Andreas@48028
  1357
lemma finfun_dom_conv:
Andreas@48035
  1358
  "finfun_dom f $ x \<longleftrightarrow> f $ x \<noteq> finfun_default f"
Andreas@48028
  1359
by(induct f rule: finfun_weak_induct)(auto simp add: finfun_dom_const finfun_default_const finfun_default_update_const finfun_upd_apply)
Andreas@48028
  1360
Andreas@48028
  1361
lemma finfun_to_list_update:
Andreas@48036
  1362
  "finfun_to_list (f(a $:= b)) = 
Andreas@48028
  1363
  (if b = finfun_default f then List.remove1 a (finfun_to_list f) else List.insort_insert a (finfun_to_list f))"
Andreas@48028
  1364
proof(subst finfun_to_list_def, rule the_equality)
Andreas@48028
  1365
  fix xs
Andreas@48036
  1366
  assume "set xs = {x. finfun_dom f(a $:= b) $ x} \<and> sorted xs \<and> distinct xs"
Andreas@48036
  1367
  hence eq: "set xs = {x. finfun_dom f(a $:= b) $ x}"
Andreas@48028
  1368
    and [simp]: "sorted xs" "distinct xs" by simp_all
Andreas@48028
  1369
  show "xs = (if b = finfun_default f then remove1 a (finfun_to_list f) else insort_insert a (finfun_to_list f))"
Andreas@48028
  1370
  proof(cases "b = finfun_default f")
Andreas@48028
  1371
    case True [simp]
Andreas@48028
  1372
    show ?thesis
Andreas@48035
  1373
    proof(cases "finfun_dom f $ a")
Andreas@48028
  1374
      case True
Andreas@48028
  1375
      have "finfun_to_list f = insort_insert a xs"
Andreas@48028
  1376
        unfolding finfun_to_list_def
Andreas@48028
  1377
      proof(rule the_equality)
Andreas@48028
  1378
        have "set (insort_insert a xs) = insert a (set xs)" by(simp add: set_insort_insert)
Andreas@48028
  1379
        also note eq also
Andreas@48036
  1380
        have "insert a {x. finfun_dom f(a $:= b) $ x} = {x. finfun_dom f $ x}" using True
Andreas@48028
  1381
          by(auto simp add: finfun_upd_apply split: split_if_asm)
Andreas@48035
  1382
        finally show 1: "set (insort_insert a xs) = {x. finfun_dom f $ x} \<and> sorted (insort_insert a xs) \<and> distinct (insort_insert a xs)"
Andreas@48028
  1383
          by(simp add: sorted_insort_insert distinct_insort_insert)
Andreas@48028
  1384
Andreas@48028
  1385
        fix xs'
Andreas@48035
  1386
        assume "set xs' = {x. finfun_dom f $ x} \<and> sorted xs' \<and> distinct xs'"
Andreas@48028
  1387
        thus "xs' = insort_insert a xs" using 1 by(auto dest: sorted_distinct_set_unique)
Andreas@48028
  1388
      qed
Andreas@48028
  1389
      with eq True show ?thesis by(simp add: remove1_insort_insert_same)
Andreas@48028
  1390
    next
Andreas@48028
  1391
      case False
Andreas@48035
  1392
      hence "f $ a = b" by(auto simp add: finfun_dom_conv)
Andreas@48036
  1393
      hence f: "f(a $:= b) = f" by(simp add: expand_finfun_eq fun_eq_iff finfun_upd_apply)
Andreas@48028
  1394
      from eq have "finfun_to_list f = xs" unfolding f finfun_to_list_def
Andreas@48028
  1395
        by(auto elim: sorted_distinct_set_unique intro!: the_equality)
Andreas@48028
  1396
      with eq False show ?thesis unfolding f by(simp add: remove1_idem)
Andreas@48028
  1397
    qed
Andreas@48028
  1398
  next
Andreas@48028
  1399
    case False
Andreas@48028
  1400
    show ?thesis
Andreas@48035
  1401
    proof(cases "finfun_dom f $ a")
Andreas@48028
  1402
      case True
Andreas@48028
  1403
      have "finfun_to_list f = xs"
Andreas@48028
  1404
        unfolding finfun_to_list_def
Andreas@48028
  1405
      proof(rule the_equality)
Andreas@48036
  1406
        have "finfun_dom f = finfun_dom f(a $:= b)" using False True
Andreas@48028
  1407
          by(simp add: expand_finfun_eq fun_eq_iff finfun_upd_apply)
Andreas@48035
  1408
        with eq show 1: "set xs = {x. finfun_dom f $ x} \<and> sorted xs \<and> distinct xs"
Andreas@48028
  1409
          by(simp del: finfun_dom_update)
Andreas@48028
  1410
        
Andreas@48028
  1411
        fix xs'
Andreas@48035
  1412
        assume "set xs' = {x. finfun_dom f $ x} \<and> sorted xs' \<and> distinct xs'"
Andreas@48028
  1413
        thus "xs' = xs" using 1 by(auto elim: sorted_distinct_set_unique)
Andreas@48028
  1414
      qed
Andreas@48028
  1415
      thus ?thesis using False True eq by(simp add: insort_insert_triv)
Andreas@48028
  1416
    next
Andreas@48028
  1417
      case False
Andreas@48028
  1418
      have "finfun_to_list f = remove1 a xs"
Andreas@48028
  1419
        unfolding finfun_to_list_def
Andreas@48028
  1420
      proof(rule the_equality)
Andreas@48028
  1421
        have "set (remove1 a xs) = set xs - {a}" by simp
Andreas@48028
  1422
        also note eq also
Andreas@48036
  1423
        have "{x. finfun_dom f(a $:= b) $ x} - {a} = {x. finfun_dom f $ x}" using False
Andreas@48028
  1424
          by(auto simp add: finfun_upd_apply split: split_if_asm)
Andreas@48035
  1425
        finally show 1: "set (remove1 a xs) = {x. finfun_dom f $ x} \<and> sorted (remove1 a xs) \<and> distinct (remove1 a xs)"
Andreas@48028
  1426
          by(simp add: sorted_remove1)
Andreas@48028
  1427
        
Andreas@48028
  1428
        fix xs'
Andreas@48035
  1429
        assume "set xs' = {x. finfun_dom f $ x} \<and> sorted xs' \<and> distinct xs'"
Andreas@48028
  1430
        thus "xs' = remove1 a xs" using 1 by(blast intro: sorted_distinct_set_unique)
Andreas@48028
  1431
      qed
Andreas@48028
  1432
      thus ?thesis using False eq `b \<noteq> finfun_default f` 
Andreas@48028
  1433
        by (simp add: insort_insert_insort insort_remove1)
Andreas@48028
  1434
    qed
Andreas@48028
  1435
  qed
Andreas@48028
  1436
qed (auto simp add: distinct_finfun_to_list sorted_finfun_to_list sorted_remove1 set_insort_insert sorted_insort_insert distinct_insort_insert finfun_upd_apply split: split_if_asm)
Andreas@48028
  1437
Andreas@48028
  1438
lemma finfun_to_list_update_code [code]:
Andreas@48028
  1439
  "finfun_to_list (finfun_update_code f a b) = 
Andreas@48028
  1440
  (if b = finfun_default f then List.remove1 a (finfun_to_list f) else List.insort_insert a (finfun_to_list f))"
Andreas@48028
  1441
by(simp add: finfun_to_list_update)
Andreas@48028
  1442
Andreas@51124
  1443
text {* More type class instantiations *}
Andreas@51124
  1444
Andreas@51124
  1445
lemma card_eq_1_iff: "card A = 1 \<longleftrightarrow> A \<noteq> {} \<and> (\<forall>x\<in>A. \<forall>y\<in>A. x = y)"
Andreas@51124
  1446
  (is "?lhs \<longleftrightarrow> ?rhs")
Andreas@51124
  1447
proof
Andreas@51124
  1448
  assume ?lhs
Andreas@51124
  1449
  moreover {
Andreas@51124
  1450
    fix x y
Andreas@51124
  1451
    assume A: "x \<in> A" "y \<in> A" and neq: "x \<noteq> y"
Andreas@51124
  1452
    have "finite A" using `?lhs` by(simp add: card_ge_0_finite)
Andreas@51124
  1453
    from neq have "2 = card {x, y}" by simp
Andreas@51124
  1454
    also have "\<dots> \<le> card A" using A `finite A`
Andreas@51124
  1455
      by(auto intro: card_mono)
Andreas@51124
  1456
    finally have False using `?lhs` by simp }
Andreas@51124
  1457
  ultimately show ?rhs by auto
Andreas@51124
  1458
next
Andreas@51124
  1459
  assume ?rhs
Andreas@51124
  1460
  hence "A = {THE x. x \<in> A}"
Andreas@51124
  1461
    by safe (auto intro: theI the_equality[symmetric])
Andreas@51124
  1462
  also have "card \<dots> = 1" by simp
Andreas@51124
  1463
  finally show ?lhs .
Andreas@51124
  1464
qed
Andreas@51124
  1465
Andreas@51124
  1466
lemma card_UNIV_finfun: 
Andreas@51124
  1467
  defines "F == finfun :: ('a \<Rightarrow> 'b) set"
Andreas@51124
  1468
  shows "CARD('a \<Rightarrow>f 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 \<or> CARD('b) = 1 then CARD('b) ^ CARD('a) else 0)"
Andreas@51124
  1469
proof(cases "0 < CARD('a) \<and> 0 < CARD('b) \<or> CARD('b) = 1")
Andreas@51124
  1470
  case True
Andreas@51124
  1471
  from True have "F = UNIV"
Andreas@51124
  1472
  proof
Andreas@51124
  1473
    assume b: "CARD('b) = 1"
Andreas@51124
  1474
    hence "\<forall>x :: 'b. x = undefined"
Andreas@51124
  1475
      by(auto simp add: card_eq_1_iff simp del: One_nat_def)
Andreas@51124
  1476
    thus ?thesis by(auto simp add: finfun_def F_def intro: exI[where x=undefined])
Andreas@51124
  1477
  qed(auto simp add: finfun_def card_gt_0_iff F_def intro: finite_subset[where B=UNIV])
Andreas@51124
  1478
  moreover have "CARD('a \<Rightarrow>f 'b) = card F"
Andreas@51124
  1479
    unfolding type_definition.Abs_image[OF type_definition_finfun, symmetric]
Andreas@51124
  1480
    by(auto intro!: card_image inj_onI simp add: Abs_finfun_inject F_def)
Andreas@51124
  1481
  ultimately show ?thesis by(simp add: card_fun)
Andreas@51124
  1482
next
Andreas@51124
  1483
  case False
Andreas@51124
  1484
  hence infinite: "\<not> (finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set))"
Andreas@51124
  1485
    and b: "CARD('b) \<noteq> 1" by(simp_all add: card_eq_0_iff)
Andreas@51124
  1486
  from b obtain b1 b2 :: 'b where "b1 \<noteq> b2"
Andreas@51124
  1487
    by(auto simp add: card_eq_1_iff simp del: One_nat_def)
Andreas@51124
  1488
  let ?f = "\<lambda>a a' :: 'a. if a = a' then b1 else b2"
Andreas@51124
  1489
  from infinite have "\<not> finite (UNIV :: ('a \<Rightarrow>f 'b) set)"
Andreas@51124
  1490
  proof(rule contrapos_nn[OF _ conjI])
Andreas@51124
  1491
    assume finite: "finite (UNIV :: ('a \<Rightarrow>f 'b) set)"
Andreas@51124
  1492
    hence "finite F"
Andreas@51124
  1493
      unfolding type_definition.Abs_image[OF type_definition_finfun, symmetric] F_def
Andreas@51124
  1494
      by(rule finite_imageD)(auto intro: inj_onI simp add: Abs_finfun_inject)
Andreas@51124
  1495
    hence "finite (range ?f)" 
Andreas@51124
  1496
      by(rule finite_subset[rotated 1])(auto simp add: F_def finfun_def `b1 \<noteq> b2` intro!: exI[where x=b2])
Andreas@51124
  1497
    thus "finite (UNIV :: 'a set)"
Andreas@51124
  1498
      by(rule finite_imageD)(auto intro: inj_onI simp add: fun_eq_iff `b1 \<noteq> b2` split: split_if_asm)
Andreas@51124
  1499
    
Andreas@51124
  1500
    from finite have "finite (range (\<lambda>b. ((K$ b) :: 'a \<Rightarrow>f 'b)))"
Andreas@51124
  1501
      by(rule finite_subset[rotated 1]) simp
Andreas@51124
  1502
    thus "finite (UNIV :: 'b set)"
Andreas@51124
  1503
      by(rule finite_imageD)(auto intro!: inj_onI)
Andreas@51124
  1504
  qed
Andreas@51124
  1505
  with False show ?thesis by simp
Andreas@51124
  1506
qed
Andreas@51124
  1507
Andreas@51124
  1508
lemma finite_UNIV_finfun:
Andreas@51124
  1509
  "finite (UNIV :: ('a \<Rightarrow>f 'b) set) \<longleftrightarrow>
Andreas@51124
  1510
  (finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set) \<or> CARD('b) = 1)"
Andreas@51124
  1511
  (is "?lhs \<longleftrightarrow> ?rhs")
Andreas@51124
  1512
proof -
Andreas@51124
  1513
  have "?lhs \<longleftrightarrow> CARD('a \<Rightarrow>f 'b) > 0" by(simp add: card_gt_0_iff)
Andreas@51124
  1514
  also have "\<dots> \<longleftrightarrow> CARD('a) > 0 \<and> CARD('b) > 0 \<or> CARD('b) = 1"
Andreas@51124
  1515
    by(simp add: card_UNIV_finfun)
Andreas@51124
  1516
  also have "\<dots> = ?rhs" by(simp add: card_gt_0_iff)
Andreas@51124
  1517
  finally show ?thesis .
Andreas@51124
  1518
qed
Andreas@51124
  1519
Andreas@51124
  1520
instantiation finfun :: (finite_UNIV, card_UNIV) finite_UNIV begin
Andreas@51124
  1521
definition "finite_UNIV = Phantom('a \<Rightarrow>f 'b)
Andreas@51124
  1522
  (let cb = of_phantom (card_UNIV :: 'b card_UNIV)
Andreas@51124
  1523
   in cb = 1 \<or> of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> cb \<noteq> 0)"
Andreas@51124
  1524
instance
Andreas@51124
  1525
  by intro_classes (auto simp add: finite_UNIV_finfun_def Let_def card_UNIV finite_UNIV finite_UNIV_finfun card_gt_0_iff)
Andreas@51124
  1526
end
Andreas@51124
  1527
Andreas@51124
  1528
instantiation finfun :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@51124
  1529
definition "card_UNIV = Phantom('a \<Rightarrow>f 'b)
Andreas@51124
  1530
  (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
Andreas@51124
  1531
       cb = of_phantom (card_UNIV :: 'b card_UNIV)
Andreas@51124
  1532
   in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
Andreas@51124
  1533
instance by intro_classes (simp add: card_UNIV_finfun_def card_UNIV Let_def card_UNIV_finfun)
Andreas@51124
  1534
end
Andreas@51124
  1535
Andreas@48041
  1536
text {* Deactivate syntax again. Import theory @{text FinFun_Syntax} to reactivate it again *}
Andreas@48041
  1537
Andreas@48041
  1538
no_type_notation
Andreas@48041
  1539
  finfun ("(_ =>f /_)" [22, 21] 21)
Andreas@48041
  1540
Andreas@48041
  1541
no_type_notation (xsymbols)
Andreas@48041
  1542
  finfun ("(_ \<Rightarrow>f /_)" [22, 21] 21)
Andreas@48041
  1543
Andreas@48041
  1544
no_notation
Andreas@48041
  1545
  finfun_const ("K$/ _" [0] 1) and
Andreas@48041
  1546
  finfun_update ("_'(_ $:= _')" [1000,0,0] 1000) and
Andreas@48041
  1547
  finfun_apply (infixl "$" 999) and
Andreas@48041
  1548
  finfun_comp (infixr "o$" 55) and
Andreas@48041
  1549
  finfun_comp2 (infixr "$o" 55) and
Andreas@48041
  1550
  finfun_Diag ("(1'($_,/ _$'))" [0, 0] 1000)
Andreas@48041
  1551
Andreas@48041
  1552
no_notation (xsymbols) 
Andreas@48041
  1553
  finfun_comp (infixr "\<circ>$" 55) and
Andreas@48041
  1554
  finfun_comp2 (infixr "$\<circ>" 55)
Andreas@48041
  1555
Andreas@48028
  1556
end