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