1.1 --- a/src/HOL/Library/Fin_Fun.thy Mon Oct 26 09:03:57 2009 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,1599 +0,0 @@
1.4 -
1.5 -(* Author: Andreas Lochbihler, Uni Karlsruhe *)
1.6 -
1.7 -header {* Almost everywhere constant functions *}
1.8 -
1.9 -theory Fin_Fun
1.10 -imports Main Infinite_Set Enum
1.11 -begin
1.12 -
1.13 -text {*
1.14 - This theory defines functions which are constant except for finitely
1.15 - many points (FinFun) and introduces a type finfin along with a
1.16 - number of operators for them. The code generator is set up such that
1.17 - such functions can be represented as data in the generated code and
1.18 - all operators are executable.
1.19 -
1.20 - For details, see Formalising FinFuns - Generating Code for Functions as Data by A. Lochbihler in TPHOLs 2009.
1.21 -*}
1.22 -
1.23 -
1.24 -subsection {* The @{text "map_default"} operation *}
1.25 -
1.26 -definition map_default :: "'b \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
1.27 -where "map_default b f a \<equiv> case f a of None \<Rightarrow> b | Some b' \<Rightarrow> b'"
1.28 -
1.29 -lemma map_default_delete [simp]:
1.30 - "map_default b (f(a := None)) = (map_default b f)(a := b)"
1.31 -by(simp add: map_default_def expand_fun_eq)
1.32 -
1.33 -lemma map_default_insert:
1.34 - "map_default b (f(a \<mapsto> b')) = (map_default b f)(a := b')"
1.35 -by(simp add: map_default_def expand_fun_eq)
1.36 -
1.37 -lemma map_default_empty [simp]: "map_default b empty = (\<lambda>a. b)"
1.38 -by(simp add: expand_fun_eq map_default_def)
1.39 -
1.40 -lemma map_default_inject:
1.41 - fixes g g' :: "'a \<rightharpoonup> 'b"
1.42 - assumes infin_eq: "\<not> finite (UNIV :: 'a set) \<or> b = b'"
1.43 - and fin: "finite (dom g)" and b: "b \<notin> ran g"
1.44 - and fin': "finite (dom g')" and b': "b' \<notin> ran g'"
1.45 - and eq': "map_default b g = map_default b' g'"
1.46 - shows "b = b'" "g = g'"
1.47 -proof -
1.48 - from infin_eq show bb': "b = b'"
1.49 - proof
1.50 - assume infin: "\<not> finite (UNIV :: 'a set)"
1.51 - from fin fin' have "finite (dom g \<union> dom g')" by auto
1.52 - with infin have "UNIV - (dom g \<union> dom g') \<noteq> {}" by(auto dest: finite_subset)
1.53 - then obtain a where a: "a \<notin> dom g \<union> dom g'" by auto
1.54 - hence "map_default b g a = b" "map_default b' g' a = b'" by(auto simp add: map_default_def)
1.55 - with eq' show "b = b'" by simp
1.56 - qed
1.57 -
1.58 - show "g = g'"
1.59 - proof
1.60 - fix x
1.61 - show "g x = g' x"
1.62 - proof(cases "g x")
1.63 - case None
1.64 - hence "map_default b g x = b" by(simp add: map_default_def)
1.65 - with bb' eq' have "map_default b' g' x = b'" by simp
1.66 - with b' have "g' x = None" by(simp add: map_default_def ran_def split: option.split_asm)
1.67 - with None show ?thesis by simp
1.68 - next
1.69 - case (Some c)
1.70 - with b have cb: "c \<noteq> b" by(auto simp add: ran_def)
1.71 - moreover from Some have "map_default b g x = c" by(simp add: map_default_def)
1.72 - with eq' have "map_default b' g' x = c" by simp
1.73 - ultimately have "g' x = Some c" using b' bb' by(auto simp add: map_default_def split: option.splits)
1.74 - with Some show ?thesis by simp
1.75 - qed
1.76 - qed
1.77 -qed
1.78 -
1.79 -subsection {* The finfun type *}
1.80 -
1.81 -typedef ('a,'b) finfun = "{f::'a\<Rightarrow>'b. \<exists>b. finite {a. f a \<noteq> b}}"
1.82 -proof -
1.83 - have "\<exists>f. finite {x. f x \<noteq> undefined}"
1.84 - proof
1.85 - show "finite {x. (\<lambda>y. undefined) x \<noteq> undefined}" by auto
1.86 - qed
1.87 - then show ?thesis by auto
1.88 -qed
1.89 -
1.90 -syntax
1.91 - "finfun" :: "type \<Rightarrow> type \<Rightarrow> type" ("(_ \<Rightarrow>\<^isub>f /_)" [22, 21] 21)
1.92 -
1.93 -lemma fun_upd_finfun: "y(a := b) \<in> finfun \<longleftrightarrow> y \<in> finfun"
1.94 -proof -
1.95 - { fix b'
1.96 - have "finite {a'. (y(a := b)) a' \<noteq> b'} = finite {a'. y a' \<noteq> b'}"
1.97 - proof(cases "b = b'")
1.98 - case True
1.99 - hence "{a'. (y(a := b)) a' \<noteq> b'} = {a'. y a' \<noteq> b'} - {a}" by auto
1.100 - thus ?thesis by simp
1.101 - next
1.102 - case False
1.103 - hence "{a'. (y(a := b)) a' \<noteq> b'} = insert a {a'. y a' \<noteq> b'}" by auto
1.104 - thus ?thesis by simp
1.105 - qed }
1.106 - thus ?thesis unfolding finfun_def by blast
1.107 -qed
1.108 -
1.109 -lemma const_finfun: "(\<lambda>x. a) \<in> finfun"
1.110 -by(auto simp add: finfun_def)
1.111 -
1.112 -lemma finfun_left_compose:
1.113 - assumes "y \<in> finfun"
1.114 - shows "g \<circ> y \<in> finfun"
1.115 -proof -
1.116 - from assms obtain b where "finite {a. y a \<noteq> b}"
1.117 - unfolding finfun_def by blast
1.118 - hence "finite {c. g (y c) \<noteq> g b}"
1.119 - proof(induct x\<equiv>"{a. y a \<noteq> b}" arbitrary: y)
1.120 - case empty
1.121 - hence "y = (\<lambda>a. b)" by(auto intro: ext)
1.122 - thus ?case by(simp)
1.123 - next
1.124 - case (insert x F)
1.125 - note IH = `\<And>y. F = {a. y a \<noteq> b} \<Longrightarrow> finite {c. g (y c) \<noteq> g b}`
1.126 - from `insert x F = {a. y a \<noteq> b}` `x \<notin> F`
1.127 - have F: "F = {a. (y(x := b)) a \<noteq> b}" by(auto)
1.128 - show ?case
1.129 - proof(cases "g (y x) = g b")
1.130 - case True
1.131 - hence "{c. g ((y(x := b)) c) \<noteq> g b} = {c. g (y c) \<noteq> g b}" by auto
1.132 - with IH[OF F] show ?thesis by simp
1.133 - next
1.134 - case False
1.135 - hence "{c. g (y c) \<noteq> g b} = insert x {c. g ((y(x := b)) c) \<noteq> g b}" by auto
1.136 - with IH[OF F] show ?thesis by(simp)
1.137 - qed
1.138 - qed
1.139 - thus ?thesis unfolding finfun_def by auto
1.140 -qed
1.141 -
1.142 -lemma assumes "y \<in> finfun"
1.143 - shows fst_finfun: "fst \<circ> y \<in> finfun"
1.144 - and snd_finfun: "snd \<circ> y \<in> finfun"
1.145 -proof -
1.146 - from assms obtain b c where bc: "finite {a. y a \<noteq> (b, c)}"
1.147 - unfolding finfun_def by auto
1.148 - have "{a. fst (y a) \<noteq> b} \<subseteq> {a. y a \<noteq> (b, c)}"
1.149 - and "{a. snd (y a) \<noteq> c} \<subseteq> {a. y a \<noteq> (b, c)}" by auto
1.150 - hence "finite {a. fst (y a) \<noteq> b}"
1.151 - and "finite {a. snd (y a) \<noteq> c}" using bc by(auto intro: finite_subset)
1.152 - thus "fst \<circ> y \<in> finfun" "snd \<circ> y \<in> finfun"
1.153 - unfolding finfun_def by auto
1.154 -qed
1.155 -
1.156 -lemma map_of_finfun: "map_of xs \<in> finfun"
1.157 -unfolding finfun_def
1.158 -by(induct xs)(auto simp add: Collect_neg_eq Collect_conj_eq Collect_imp_eq intro: finite_subset)
1.159 -
1.160 -lemma Diag_finfun: "(\<lambda>x. (f x, g x)) \<in> finfun \<longleftrightarrow> f \<in> finfun \<and> g \<in> finfun"
1.161 -by(auto intro: finite_subset simp add: Collect_neg_eq Collect_imp_eq Collect_conj_eq finfun_def)
1.162 -
1.163 -lemma finfun_right_compose:
1.164 - assumes g: "g \<in> finfun" and inj: "inj f"
1.165 - shows "g o f \<in> finfun"
1.166 -proof -
1.167 - from g obtain b where b: "finite {a. g a \<noteq> b}" unfolding finfun_def by blast
1.168 - moreover have "f ` {a. g (f a) \<noteq> b} \<subseteq> {a. g a \<noteq> b}" by auto
1.169 - moreover from inj have "inj_on f {a. g (f a) \<noteq> b}" by(rule subset_inj_on) blast
1.170 - ultimately have "finite {a. g (f a) \<noteq> b}"
1.171 - by(blast intro: finite_imageD[where f=f] finite_subset)
1.172 - thus ?thesis unfolding finfun_def by auto
1.173 -qed
1.174 -
1.175 -lemma finfun_curry:
1.176 - assumes fin: "f \<in> finfun"
1.177 - shows "curry f \<in> finfun" "curry f a \<in> finfun"
1.178 -proof -
1.179 - from fin obtain c where c: "finite {ab. f ab \<noteq> c}" unfolding finfun_def by blast
1.180 - moreover have "{a. \<exists>b. f (a, b) \<noteq> c} = fst ` {ab. f ab \<noteq> c}" by(force)
1.181 - hence "{a. curry f a \<noteq> (\<lambda>b. c)} = fst ` {ab. f ab \<noteq> c}"
1.182 - by(auto simp add: curry_def expand_fun_eq)
1.183 - ultimately have "finite {a. curry f a \<noteq> (\<lambda>b. c)}" by simp
1.184 - thus "curry f \<in> finfun" unfolding finfun_def by blast
1.185 -
1.186 - have "snd ` {ab. f ab \<noteq> c} = {b. \<exists>a. f (a, b) \<noteq> c}" by(force)
1.187 - hence "{b. f (a, b) \<noteq> c} \<subseteq> snd ` {ab. f ab \<noteq> c}" by auto
1.188 - hence "finite {b. f (a, b) \<noteq> c}" by(rule finite_subset)(rule finite_imageI[OF c])
1.189 - thus "curry f a \<in> finfun" unfolding finfun_def by auto
1.190 -qed
1.191 -
1.192 -lemmas finfun_simp =
1.193 - fst_finfun snd_finfun Abs_finfun_inverse Rep_finfun_inverse Abs_finfun_inject Rep_finfun_inject Diag_finfun finfun_curry
1.194 -lemmas finfun_iff = const_finfun fun_upd_finfun Rep_finfun map_of_finfun
1.195 -lemmas finfun_intro = finfun_left_compose fst_finfun snd_finfun
1.196 -
1.197 -lemma Abs_finfun_inject_finite:
1.198 - fixes x y :: "'a \<Rightarrow> 'b"
1.199 - assumes fin: "finite (UNIV :: 'a set)"
1.200 - shows "Abs_finfun x = Abs_finfun y \<longleftrightarrow> x = y"
1.201 -proof
1.202 - assume "Abs_finfun x = Abs_finfun y"
1.203 - moreover have "x \<in> finfun" "y \<in> finfun" unfolding finfun_def
1.204 - by(auto intro: finite_subset[OF _ fin])
1.205 - ultimately show "x = y" by(simp add: Abs_finfun_inject)
1.206 -qed simp
1.207 -
1.208 -lemma Abs_finfun_inject_finite_class:
1.209 - fixes x y :: "('a :: finite) \<Rightarrow> 'b"
1.210 - shows "Abs_finfun x = Abs_finfun y \<longleftrightarrow> x = y"
1.211 -using finite_UNIV
1.212 -by(simp add: Abs_finfun_inject_finite)
1.213 -
1.214 -lemma Abs_finfun_inj_finite:
1.215 - assumes fin: "finite (UNIV :: 'a set)"
1.216 - shows "inj (Abs_finfun :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'b)"
1.217 -proof(rule inj_onI)
1.218 - fix x y :: "'a \<Rightarrow> 'b"
1.219 - assume "Abs_finfun x = Abs_finfun y"
1.220 - moreover have "x \<in> finfun" "y \<in> finfun" unfolding finfun_def
1.221 - by(auto intro: finite_subset[OF _ fin])
1.222 - ultimately show "x = y" by(simp add: Abs_finfun_inject)
1.223 -qed
1.224 -
1.225 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.226 -
1.227 -lemma Abs_finfun_inverse_finite:
1.228 - fixes x :: "'a \<Rightarrow> 'b"
1.229 - assumes fin: "finite (UNIV :: 'a set)"
1.230 - shows "Rep_finfun (Abs_finfun x) = x"
1.231 -proof -
1.232 - from fin have "x \<in> finfun"
1.233 - by(auto simp add: finfun_def intro: finite_subset)
1.234 - thus ?thesis by simp
1.235 -qed
1.236 -
1.237 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.238 -
1.239 -lemma Abs_finfun_inverse_finite_class:
1.240 - fixes x :: "('a :: finite) \<Rightarrow> 'b"
1.241 - shows "Rep_finfun (Abs_finfun x) = x"
1.242 -using finite_UNIV by(simp add: Abs_finfun_inverse_finite)
1.243 -
1.244 -lemma finfun_eq_finite_UNIV: "finite (UNIV :: 'a set) \<Longrightarrow> (finfun :: ('a \<Rightarrow> 'b) set) = UNIV"
1.245 -unfolding finfun_def by(auto intro: finite_subset)
1.246 -
1.247 -lemma finfun_finite_UNIV_class: "finfun = (UNIV :: ('a :: finite \<Rightarrow> 'b) set)"
1.248 -by(simp add: finfun_eq_finite_UNIV)
1.249 -
1.250 -lemma map_default_in_finfun:
1.251 - assumes fin: "finite (dom f)"
1.252 - shows "map_default b f \<in> finfun"
1.253 -unfolding finfun_def
1.254 -proof(intro CollectI exI)
1.255 - from fin show "finite {a. map_default b f a \<noteq> b}"
1.256 - by(auto simp add: map_default_def dom_def Collect_conj_eq split: option.splits)
1.257 -qed
1.258 -
1.259 -lemma finfun_cases_map_default:
1.260 - obtains b g where "f = Abs_finfun (map_default b g)" "finite (dom g)" "b \<notin> ran g"
1.261 -proof -
1.262 - obtain y where f: "f = Abs_finfun y" and y: "y \<in> finfun" by(cases f)
1.263 - from y obtain b where b: "finite {a. y a \<noteq> b}" unfolding finfun_def by auto
1.264 - let ?g = "(\<lambda>a. if y a = b then None else Some (y a))"
1.265 - have "map_default b ?g = y" by(simp add: expand_fun_eq map_default_def)
1.266 - with f have "f = Abs_finfun (map_default b ?g)" by simp
1.267 - moreover from b have "finite (dom ?g)" by(auto simp add: dom_def)
1.268 - moreover have "b \<notin> ran ?g" by(auto simp add: ran_def)
1.269 - ultimately show ?thesis by(rule that)
1.270 -qed
1.271 -
1.272 -
1.273 -subsection {* Kernel functions for type @{typ "'a \<Rightarrow>\<^isub>f 'b"} *}
1.274 -
1.275 -definition finfun_const :: "'b \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'b" ("\<lambda>\<^isup>f/ _" [0] 1)
1.276 -where [code del]: "(\<lambda>\<^isup>f b) = Abs_finfun (\<lambda>x. b)"
1.277 -
1.278 -definition finfun_update :: "'a \<Rightarrow>\<^isub>f 'b \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'b" ("_'(\<^sup>f/ _ := _')" [1000,0,0] 1000)
1.279 -where [code del]: "f(\<^sup>fa := b) = Abs_finfun ((Rep_finfun f)(a := b))"
1.280 -
1.281 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.282 -
1.283 -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)"
1.284 -by(simp add: finfun_update_def fun_upd_twist)
1.285 -
1.286 -lemma finfun_update_twice [simp]:
1.287 - "finfun_update (finfun_update f a b) a b' = finfun_update f a b'"
1.288 -by(simp add: finfun_update_def)
1.289 -
1.290 -lemma finfun_update_const_same: "(\<lambda>\<^isup>f b)(\<^sup>f a := b) = (\<lambda>\<^isup>f b)"
1.291 -by(simp add: finfun_update_def finfun_const_def expand_fun_eq)
1.292 -
1.293 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.294 -
1.295 -subsection {* Code generator setup *}
1.296 -
1.297 -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)
1.298 -where [simp, code del]: "finfun_update_code = finfun_update"
1.299 -
1.300 -code_datatype finfun_const finfun_update_code
1.301 -
1.302 -lemma finfun_update_const_code [code]:
1.303 - "(\<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')"
1.304 -by(simp add: finfun_update_const_same)
1.305 -
1.306 -lemma finfun_update_update_code [code]:
1.307 - "(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)"
1.308 -by(simp add: finfun_update_twist)
1.309 -
1.310 -
1.311 -subsection {* Setup for quickcheck *}
1.312 -
1.313 -notation fcomp (infixl "o>" 60)
1.314 -notation scomp (infixl "o\<rightarrow>" 60)
1.315 -
1.316 -definition (in term_syntax) valtermify_finfun_const ::
1.317 - "'b\<Colon>typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> ('a\<Colon>typerep \<Rightarrow>\<^isub>f 'b) \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
1.318 - "valtermify_finfun_const y = Code_Evaluation.valtermify finfun_const {\<cdot>} y"
1.319 -
1.320 -definition (in term_syntax) valtermify_finfun_update_code ::
1.321 - "'a\<Colon>typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> 'b\<Colon>typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> ('a \<Rightarrow>\<^isub>f 'b) \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> ('a \<Rightarrow>\<^isub>f 'b) \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
1.322 - "valtermify_finfun_update_code x y f = Code_Evaluation.valtermify finfun_update_code {\<cdot>} f {\<cdot>} x {\<cdot>} y"
1.323 -
1.324 -instantiation finfun :: (random, random) random
1.325 -begin
1.326 -
1.327 -primrec random_finfun_aux :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<Rightarrow>\<^isub>f 'b \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed" where
1.328 - "random_finfun_aux 0 j = Quickcheck.collapse (Random.select_weight
1.329 - [(1, Quickcheck.random j o\<rightarrow> (\<lambda>y. Pair (valtermify_finfun_const y)))])"
1.330 - | "random_finfun_aux (Suc_code_numeral i) j = Quickcheck.collapse (Random.select_weight
1.331 - [(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))))),
1.332 - (1, Quickcheck.random j o\<rightarrow> (\<lambda>y. Pair (valtermify_finfun_const y)))])"
1.333 -
1.334 -definition
1.335 - "Quickcheck.random i = random_finfun_aux i i"
1.336 -
1.337 -instance ..
1.338 -
1.339 -end
1.340 -
1.341 -lemma random_finfun_aux_code [code]:
1.342 - "random_finfun_aux i j = Quickcheck.collapse (Random.select_weight
1.343 - [(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))))),
1.344 - (1, Quickcheck.random j o\<rightarrow> (\<lambda>y. Pair (valtermify_finfun_const y)))])"
1.345 - apply (cases i rule: code_numeral.exhaust)
1.346 - apply (simp_all only: random_finfun_aux.simps code_numeral_zero_minus_one Suc_code_numeral_minus_one)
1.347 - apply (subst select_weight_cons_zero) apply (simp only:)
1.348 - done
1.349 -
1.350 -no_notation fcomp (infixl "o>" 60)
1.351 -no_notation scomp (infixl "o\<rightarrow>" 60)
1.352 -
1.353 -
1.354 -subsection {* @{text "finfun_update"} as instance of @{text "fun_left_comm"} *}
1.355 -
1.356 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.357 -
1.358 -interpretation finfun_update: fun_left_comm "\<lambda>a f. f(\<^sup>f a :: 'a := b')"
1.359 -proof
1.360 - fix a' a :: 'a
1.361 - fix b
1.362 - have "(Rep_finfun b)(a := b', a' := b') = (Rep_finfun b)(a' := b', a := b')"
1.363 - by(cases "a = a'")(auto simp add: fun_upd_twist)
1.364 - thus "b(\<^sup>f a := b')(\<^sup>f a' := b') = b(\<^sup>f a' := b')(\<^sup>f a := b')"
1.365 - by(auto simp add: finfun_update_def fun_upd_twist)
1.366 -qed
1.367 -
1.368 -lemma fold_finfun_update_finite_univ:
1.369 - assumes fin: "finite (UNIV :: 'a set)"
1.370 - shows "fold (\<lambda>a f. f(\<^sup>f a := b')) (\<lambda>\<^isup>f b) (UNIV :: 'a set) = (\<lambda>\<^isup>f b')"
1.371 -proof -
1.372 - { fix A :: "'a set"
1.373 - from fin have "finite A" by(auto intro: finite_subset)
1.374 - 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)"
1.375 - proof(induct)
1.376 - case (insert x F)
1.377 - 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)"
1.378 - by(auto intro: ext)
1.379 - with insert show ?case
1.380 - by(simp add: finfun_const_def fun_upd_def)(simp add: finfun_update_def Abs_finfun_inverse_finite[OF fin] fun_upd_def)
1.381 - qed(simp add: finfun_const_def) }
1.382 - thus ?thesis by(simp add: finfun_const_def)
1.383 -qed
1.384 -
1.385 -
1.386 -subsection {* Default value for FinFuns *}
1.387 -
1.388 -definition finfun_default_aux :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b"
1.389 -where [code del]: "finfun_default_aux f = (if finite (UNIV :: 'a set) then undefined else THE b. finite {a. f a \<noteq> b})"
1.390 -
1.391 -lemma finfun_default_aux_infinite:
1.392 - fixes f :: "'a \<Rightarrow> 'b"
1.393 - assumes infin: "infinite (UNIV :: 'a set)"
1.394 - and fin: "finite {a. f a \<noteq> b}"
1.395 - shows "finfun_default_aux f = b"
1.396 -proof -
1.397 - let ?B = "{a. f a \<noteq> b}"
1.398 - from fin have "(THE b. finite {a. f a \<noteq> b}) = b"
1.399 - proof(rule the_equality)
1.400 - fix b'
1.401 - assume "finite {a. f a \<noteq> b'}" (is "finite ?B'")
1.402 - with infin fin have "UNIV - (?B' \<union> ?B) \<noteq> {}" by(auto dest: finite_subset)
1.403 - then obtain a where a: "a \<notin> ?B' \<union> ?B" by auto
1.404 - thus "b' = b" by auto
1.405 - qed
1.406 - thus ?thesis using infin by(simp add: finfun_default_aux_def)
1.407 -qed
1.408 -
1.409 -
1.410 -lemma finite_finfun_default_aux:
1.411 - fixes f :: "'a \<Rightarrow> 'b"
1.412 - assumes fin: "f \<in> finfun"
1.413 - shows "finite {a. f a \<noteq> finfun_default_aux f}"
1.414 -proof(cases "finite (UNIV :: 'a set)")
1.415 - case True thus ?thesis using fin
1.416 - by(auto simp add: finfun_def finfun_default_aux_def intro: finite_subset)
1.417 -next
1.418 - case False
1.419 - from fin obtain b where b: "finite {a. f a \<noteq> b}" (is "finite ?B")
1.420 - unfolding finfun_def by blast
1.421 - with False show ?thesis by(simp add: finfun_default_aux_infinite)
1.422 -qed
1.423 -
1.424 -lemma finfun_default_aux_update_const:
1.425 - fixes f :: "'a \<Rightarrow> 'b"
1.426 - assumes fin: "f \<in> finfun"
1.427 - shows "finfun_default_aux (f(a := b)) = finfun_default_aux f"
1.428 -proof(cases "finite (UNIV :: 'a set)")
1.429 - case False
1.430 - from fin obtain b' where b': "finite {a. f a \<noteq> b'}" unfolding finfun_def by blast
1.431 - hence "finite {a'. (f(a := b)) a' \<noteq> b'}"
1.432 - proof(cases "b = b' \<and> f a \<noteq> b'")
1.433 - case True
1.434 - hence "{a. f a \<noteq> b'} = insert a {a'. (f(a := b)) a' \<noteq> b'}" by auto
1.435 - thus ?thesis using b' by simp
1.436 - next
1.437 - case False
1.438 - moreover
1.439 - { assume "b \<noteq> b'"
1.440 - hence "{a'. (f(a := b)) a' \<noteq> b'} = insert a {a. f a \<noteq> b'}" by auto
1.441 - hence ?thesis using b' by simp }
1.442 - moreover
1.443 - { assume "b = b'" "f a = b'"
1.444 - hence "{a'. (f(a := b)) a' \<noteq> b'} = {a. f a \<noteq> b'}" by auto
1.445 - hence ?thesis using b' by simp }
1.446 - ultimately show ?thesis by blast
1.447 - qed
1.448 - with False b' show ?thesis by(auto simp del: fun_upd_apply simp add: finfun_default_aux_infinite)
1.449 -next
1.450 - case True thus ?thesis by(simp add: finfun_default_aux_def)
1.451 -qed
1.452 -
1.453 -definition finfun_default :: "'a \<Rightarrow>\<^isub>f 'b \<Rightarrow> 'b"
1.454 - where [code del]: "finfun_default f = finfun_default_aux (Rep_finfun f)"
1.455 -
1.456 -lemma finite_finfun_default: "finite {a. Rep_finfun f a \<noteq> finfun_default f}"
1.457 -unfolding finfun_default_def by(simp add: finite_finfun_default_aux)
1.458 -
1.459 -lemma finfun_default_const: "finfun_default ((\<lambda>\<^isup>f b) :: 'a \<Rightarrow>\<^isub>f 'b) = (if finite (UNIV :: 'a set) then undefined else b)"
1.460 -apply(auto simp add: finfun_default_def finfun_const_def finfun_default_aux_infinite)
1.461 -apply(simp add: finfun_default_aux_def)
1.462 -done
1.463 -
1.464 -lemma finfun_default_update_const:
1.465 - "finfun_default (f(\<^sup>f a := b)) = finfun_default f"
1.466 -unfolding finfun_default_def finfun_update_def
1.467 -by(simp add: finfun_default_aux_update_const)
1.468 -
1.469 -subsection {* Recursion combinator and well-formedness conditions *}
1.470 -
1.471 -definition finfun_rec :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow>\<^isub>f 'b) \<Rightarrow> 'c"
1.472 -where [code del]:
1.473 - "finfun_rec cnst upd f \<equiv>
1.474 - let b = finfun_default f;
1.475 - g = THE g. f = Abs_finfun (map_default b g) \<and> finite (dom g) \<and> b \<notin> ran g
1.476 - in fold (\<lambda>a. upd a (map_default b g a)) (cnst b) (dom g)"
1.477 -
1.478 -locale finfun_rec_wf_aux =
1.479 - fixes cnst :: "'b \<Rightarrow> 'c"
1.480 - and upd :: "'a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c"
1.481 - assumes upd_const_same: "upd a b (cnst b) = cnst b"
1.482 - and upd_commute: "a \<noteq> a' \<Longrightarrow> upd a b (upd a' b' c) = upd a' b' (upd a b c)"
1.483 - and upd_idemp: "b \<noteq> b' \<Longrightarrow> upd a b'' (upd a b' (cnst b)) = upd a b'' (cnst b)"
1.484 -begin
1.485 -
1.486 -
1.487 -lemma upd_left_comm: "fun_left_comm (\<lambda>a. upd a (f a))"
1.488 -by(unfold_locales)(auto intro: upd_commute)
1.489 -
1.490 -lemma upd_upd_twice: "upd a b'' (upd a b' (cnst b)) = upd a b'' (cnst b)"
1.491 -by(cases "b \<noteq> b'")(auto simp add: fun_upd_def upd_const_same upd_idemp)
1.492 -
1.493 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.494 -
1.495 -lemma map_default_update_const:
1.496 - assumes fin: "finite (dom f)"
1.497 - and anf: "a \<notin> dom f"
1.498 - and fg: "f \<subseteq>\<^sub>m g"
1.499 - shows "upd a d (fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f)) =
1.500 - fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f)"
1.501 -proof -
1.502 - let ?upd = "\<lambda>a. upd a (map_default d g a)"
1.503 - let ?fr = "\<lambda>A. fold ?upd (cnst d) A"
1.504 - interpret gwf: fun_left_comm "?upd" by(rule upd_left_comm)
1.505 -
1.506 - from fin anf fg show ?thesis
1.507 - proof(induct A\<equiv>"dom f" arbitrary: f)
1.508 - case empty
1.509 - from `{} = dom f` have "f = empty" by(auto simp add: dom_def intro: ext)
1.510 - thus ?case by(simp add: finfun_const_def upd_const_same)
1.511 - next
1.512 - case (insert a' A)
1.513 - 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)`
1.514 - note fin = `finite A` note anf = `a \<notin> dom f` note a'nA = `a' \<notin> A`
1.515 - note domf = `insert a' A = dom f` note fg = `f \<subseteq>\<^sub>m g`
1.516 -
1.517 - from domf obtain b where b: "f a' = Some b" by auto
1.518 - let ?f' = "f(a' := None)"
1.519 - have "upd a d (?fr (insert a' A)) = upd a d (upd a' (map_default d g a') (?fr A))"
1.520 - by(subst gwf.fold_insert[OF fin a'nA]) rule
1.521 - also from b fg have "g a' = f a'" by(auto simp add: map_le_def intro: domI dest: bspec)
1.522 - hence ga': "map_default d g a' = map_default d f a'" by(simp add: map_default_def)
1.523 - also from anf domf have "a \<noteq> a'" by auto note upd_commute[OF this]
1.524 - 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)
1.525 - note A also note IH[OF `a \<notin> dom ?f'` `?f' \<subseteq>\<^sub>m g` A]
1.526 - also have "upd a' (map_default d f a') (?fr (dom (f(a' := None)))) = ?fr (dom f)"
1.527 - unfolding domf[symmetric] gwf.fold_insert[OF fin a'nA] ga' unfolding A ..
1.528 - also have "insert a' (dom ?f') = dom f" using domf by auto
1.529 - finally show ?case .
1.530 - qed
1.531 -qed
1.532 -
1.533 -lemma map_default_update_twice:
1.534 - assumes fin: "finite (dom f)"
1.535 - and anf: "a \<notin> dom f"
1.536 - and fg: "f \<subseteq>\<^sub>m g"
1.537 - shows "upd a d'' (upd a d' (fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f))) =
1.538 - upd a d'' (fold (\<lambda>a. upd a (map_default d g a)) (cnst d) (dom f))"
1.539 -proof -
1.540 - let ?upd = "\<lambda>a. upd a (map_default d g a)"
1.541 - let ?fr = "\<lambda>A. fold ?upd (cnst d) A"
1.542 - interpret gwf: fun_left_comm "?upd" by(rule upd_left_comm)
1.543 -
1.544 - from fin anf fg show ?thesis
1.545 - proof(induct A\<equiv>"dom f" arbitrary: f)
1.546 - case empty
1.547 - from `{} = dom f` have "f = empty" by(auto simp add: dom_def intro: ext)
1.548 - thus ?case by(auto simp add: finfun_const_def finfun_update_def upd_upd_twice)
1.549 - next
1.550 - case (insert a' A)
1.551 - 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))`
1.552 - note fin = `finite A` note anf = `a \<notin> dom f` note a'nA = `a' \<notin> A`
1.553 - note domf = `insert a' A = dom f` note fg = `f \<subseteq>\<^sub>m g`
1.554 -
1.555 - from domf obtain b where b: "f a' = Some b" by auto
1.556 - let ?f' = "f(a' := None)"
1.557 - let ?b' = "case f a' of None \<Rightarrow> d | Some b \<Rightarrow> b"
1.558 - from domf have "upd a d'' (upd a d' (?fr (dom f))) = upd a d'' (upd a d' (?fr (insert a' A)))" by simp
1.559 - also note gwf.fold_insert[OF fin a'nA]
1.560 - also from b fg have "g a' = f a'" by(auto simp add: map_le_def intro: domI dest: bspec)
1.561 - hence ga': "map_default d g a' = map_default d f a'" by(simp add: map_default_def)
1.562 - also from anf domf have ana': "a \<noteq> a'" by auto note upd_commute[OF this]
1.563 - also note upd_commute[OF ana']
1.564 - 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)
1.565 - note A also note IH[OF `a \<notin> dom ?f'` `?f' \<subseteq>\<^sub>m g` A]
1.566 - also note upd_commute[OF ana'[symmetric]] also note ga'[symmetric] also note A[symmetric]
1.567 - also note gwf.fold_insert[symmetric, OF fin a'nA] also note domf
1.568 - finally show ?case .
1.569 - qed
1.570 -qed
1.571 -
1.572 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.573 -
1.574 -lemma map_default_eq_id [simp]: "map_default d ((\<lambda>a. Some (f a)) |` {a. f a \<noteq> d}) = f"
1.575 -by(auto simp add: map_default_def restrict_map_def intro: ext)
1.576 -
1.577 -lemma finite_rec_cong1:
1.578 - assumes f: "fun_left_comm f" and g: "fun_left_comm g"
1.579 - and fin: "finite A"
1.580 - and eq: "\<And>a. a \<in> A \<Longrightarrow> f a = g a"
1.581 - shows "fold f z A = fold g z A"
1.582 -proof -
1.583 - interpret f: fun_left_comm f by(rule f)
1.584 - interpret g: fun_left_comm g by(rule g)
1.585 - { fix B
1.586 - assume BsubA: "B \<subseteq> A"
1.587 - with fin have "finite B" by(blast intro: finite_subset)
1.588 - hence "B \<subseteq> A \<Longrightarrow> fold f z B = fold g z B"
1.589 - proof(induct)
1.590 - case empty thus ?case by simp
1.591 - next
1.592 - case (insert a B)
1.593 - note finB = `finite B` note anB = `a \<notin> B` note sub = `insert a B \<subseteq> A`
1.594 - note IH = `B \<subseteq> A \<Longrightarrow> fold f z B = fold g z B`
1.595 - from sub anB have BpsubA: "B \<subset> A" and BsubA: "B \<subseteq> A" and aA: "a \<in> A" by auto
1.596 - from IH[OF BsubA] eq[OF aA] finB anB
1.597 - show ?case by(auto)
1.598 - qed
1.599 - with BsubA have "fold f z B = fold g z B" by blast }
1.600 - thus ?thesis by blast
1.601 -qed
1.602 -
1.603 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.604 -
1.605 -lemma finfun_rec_upd [simp]:
1.606 - "finfun_rec cnst upd (f(\<^sup>f a' := b')) = upd a' b' (finfun_rec cnst upd f)"
1.607 -proof -
1.608 - obtain b where b: "b = finfun_default f" by auto
1.609 - let ?the = "\<lambda>f g. f = Abs_finfun (map_default b g) \<and> finite (dom g) \<and> b \<notin> ran g"
1.610 - obtain g where g: "g = The (?the f)" by blast
1.611 - obtain y where f: "f = Abs_finfun y" and y: "y \<in> finfun" by (cases f)
1.612 - from f y b have bfin: "finite {a. y a \<noteq> b}" by(simp add: finfun_default_def finite_finfun_default_aux)
1.613 -
1.614 - let ?g = "(\<lambda>a. Some (y a)) |` {a. y a \<noteq> b}"
1.615 - from bfin have fing: "finite (dom ?g)" by auto
1.616 - have bran: "b \<notin> ran ?g" by(auto simp add: ran_def restrict_map_def)
1.617 - have yg: "y = map_default b ?g" by simp
1.618 - have gg: "g = ?g" unfolding g
1.619 - proof(rule the_equality)
1.620 - from f y bfin show "?the f ?g"
1.621 - by(auto)(simp add: restrict_map_def ran_def split: split_if_asm)
1.622 - next
1.623 - fix g'
1.624 - assume "?the f g'"
1.625 - hence fin': "finite (dom g')" and ran': "b \<notin> ran g'"
1.626 - and eq: "Abs_finfun (map_default b ?g) = Abs_finfun (map_default b g')" using f yg by auto
1.627 - from fin' fing have "map_default b ?g \<in> finfun" "map_default b g' \<in> finfun" by(blast intro: map_default_in_finfun)+
1.628 - with eq have "map_default b ?g = map_default b g'" by simp
1.629 - with fing bran fin' ran' show "g' = ?g" by(rule map_default_inject[OF disjI2[OF refl], THEN sym])
1.630 - qed
1.631 -
1.632 - show ?thesis
1.633 - proof(cases "b' = b")
1.634 - case True
1.635 - note b'b = True
1.636 -
1.637 - let ?g' = "(\<lambda>a. Some ((y(a' := b)) a)) |` {a. (y(a' := b)) a \<noteq> b}"
1.638 - from bfin b'b have fing': "finite (dom ?g')"
1.639 - by(auto simp add: Collect_conj_eq Collect_imp_eq intro: finite_subset)
1.640 - have brang': "b \<notin> ran ?g'" by(auto simp add: ran_def restrict_map_def)
1.641 -
1.642 - let ?b' = "\<lambda>a. case ?g' a of None \<Rightarrow> b | Some b \<Rightarrow> b"
1.643 - let ?b = "map_default b ?g"
1.644 - from upd_left_comm upd_left_comm fing'
1.645 - have "fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g') = fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g')"
1.646 - by(rule finite_rec_cong1)(auto simp add: restrict_map_def b'b b map_default_def)
1.647 - also interpret gwf: fun_left_comm "\<lambda>a. upd a (?b a)" by(rule upd_left_comm)
1.648 - 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))"
1.649 - proof(cases "y a' = b")
1.650 - case True
1.651 - with b'b have g': "?g' = ?g" by(auto simp add: restrict_map_def intro: ext)
1.652 - from True have a'ndomg: "a' \<notin> dom ?g" by auto
1.653 - from f b'b b show ?thesis unfolding g'
1.654 - by(subst map_default_update_const[OF fing a'ndomg map_le_refl, symmetric]) simp
1.655 - next
1.656 - case False
1.657 - hence domg: "dom ?g = insert a' (dom ?g')" by auto
1.658 - from False b'b have a'ndomg': "a' \<notin> dom ?g'" by auto
1.659 - have "fold (\<lambda>a. upd a (?b a)) (cnst b) (insert a' (dom ?g')) =
1.660 - upd a' (?b a') (fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'))"
1.661 - using fing' a'ndomg' unfolding b'b by(rule gwf.fold_insert)
1.662 - hence "upd a' b (fold (\<lambda>a. upd a (?b a)) (cnst b) (insert a' (dom ?g'))) =
1.663 - upd a' b (upd a' (?b a') (fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g')))" by simp
1.664 - also from b'b have g'leg: "?g' \<subseteq>\<^sub>m ?g" by(auto simp add: restrict_map_def map_le_def)
1.665 - note map_default_update_twice[OF fing' a'ndomg' this, of b "?b a'" b]
1.666 - also note map_default_update_const[OF fing' a'ndomg' g'leg, of b]
1.667 - finally show ?thesis unfolding b'b domg[unfolded b'b] by(rule sym)
1.668 - qed
1.669 - also have "The (?the (f(\<^sup>f a' := b'))) = ?g'"
1.670 - proof(rule the_equality)
1.671 - from f y b b'b brang' fing' show "?the (f(\<^sup>f a' := b')) ?g'"
1.672 - by(auto simp del: fun_upd_apply simp add: finfun_update_def)
1.673 - next
1.674 - fix g'
1.675 - assume "?the (f(\<^sup>f a' := b')) g'"
1.676 - hence fin': "finite (dom g')" and ran': "b \<notin> ran g'"
1.677 - and eq: "f(\<^sup>f a' := b') = Abs_finfun (map_default b g')"
1.678 - by(auto simp del: fun_upd_apply)
1.679 - from fin' fing' have "map_default b g' \<in> finfun" "map_default b ?g' \<in> finfun"
1.680 - by(blast intro: map_default_in_finfun)+
1.681 - with eq f b'b b have "map_default b ?g' = map_default b g'"
1.682 - by(simp del: fun_upd_apply add: finfun_update_def)
1.683 - with fing' brang' fin' ran' show "g' = ?g'"
1.684 - by(rule map_default_inject[OF disjI2[OF refl], THEN sym])
1.685 - qed
1.686 - ultimately show ?thesis unfolding finfun_rec_def Let_def b gg[unfolded g b] using bfin b'b b
1.687 - by(simp only: finfun_default_update_const map_default_def)
1.688 - next
1.689 - case False
1.690 - note b'b = this
1.691 - let ?g' = "?g(a' \<mapsto> b')"
1.692 - let ?b' = "map_default b ?g'"
1.693 - let ?b = "map_default b ?g"
1.694 - from fing have fing': "finite (dom ?g')" by auto
1.695 - from bran b'b have bnrang': "b \<notin> ran ?g'" by(auto simp add: ran_def)
1.696 - have ffmg': "map_default b ?g' = y(a' := b')" by(auto intro: ext simp add: map_default_def restrict_map_def)
1.697 - with f y have f_Abs: "f(\<^sup>f a' := b') = Abs_finfun (map_default b ?g')" by(auto simp add: finfun_update_def)
1.698 - have g': "The (?the (f(\<^sup>f a' := b'))) = ?g'"
1.699 - proof
1.700 - from fing' bnrang' f_Abs show "?the (f(\<^sup>f a' := b')) ?g'" by(auto simp add: finfun_update_def restrict_map_def)
1.701 - next
1.702 - fix g' assume "?the (f(\<^sup>f a' := b')) g'"
1.703 - hence f': "f(\<^sup>f a' := b') = Abs_finfun (map_default b g')"
1.704 - and fin': "finite (dom g')" and brang': "b \<notin> ran g'" by auto
1.705 - from fing' fin' have "map_default b ?g' \<in> finfun" "map_default b g' \<in> finfun"
1.706 - by(auto intro: map_default_in_finfun)
1.707 - with f' f_Abs have "map_default b g' = map_default b ?g'" by simp
1.708 - with fin' brang' fing' bnrang' show "g' = ?g'"
1.709 - by(rule map_default_inject[OF disjI2[OF refl]])
1.710 - qed
1.711 - 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}))"
1.712 - by auto
1.713 - show ?thesis
1.714 - proof(cases "y a' = b")
1.715 - case True
1.716 - hence a'ndomg: "a' \<notin> dom ?g" by auto
1.717 - from f y b'b True have yff: "y = map_default b (?g' |` dom ?g)"
1.718 - by(auto simp add: restrict_map_def map_default_def intro!: ext)
1.719 - hence f': "f = Abs_finfun (map_default b (?g' |` dom ?g))" using f by simp
1.720 - interpret g'wf: fun_left_comm "\<lambda>a. upd a (?b' a)" by(rule upd_left_comm)
1.721 - from upd_left_comm upd_left_comm fing
1.722 - have "fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g) = fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g)"
1.723 - by(rule finite_rec_cong1)(auto simp add: restrict_map_def b'b True map_default_def)
1.724 - thus ?thesis unfolding finfun_rec_def Let_def finfun_default_update_const b[symmetric]
1.725 - unfolding g' g[symmetric] gg g'wf.fold_insert[OF fing a'ndomg, of "cnst b", folded dom]
1.726 - by -(rule arg_cong2[where f="upd a'"], simp_all add: map_default_def)
1.727 - next
1.728 - case False
1.729 - hence "insert a' (dom ?g) = dom ?g" by auto
1.730 - moreover {
1.731 - let ?g'' = "?g(a' := None)"
1.732 - let ?b'' = "map_default b ?g''"
1.733 - from False have domg: "dom ?g = insert a' (dom ?g'')" by auto
1.734 - from False have a'ndomg'': "a' \<notin> dom ?g''" by auto
1.735 - have fing'': "finite (dom ?g'')" by(rule finite_subset[OF _ fing]) auto
1.736 - have bnrang'': "b \<notin> ran ?g''" by(auto simp add: ran_def restrict_map_def)
1.737 - interpret gwf: fun_left_comm "\<lambda>a. upd a (?b a)" by(rule upd_left_comm)
1.738 - interpret g'wf: fun_left_comm "\<lambda>a. upd a (?b' a)" by(rule upd_left_comm)
1.739 - have "upd a' b' (fold (\<lambda>a. upd a (?b a)) (cnst b) (insert a' (dom ?g''))) =
1.740 - upd a' b' (upd a' (?b a') (fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'')))"
1.741 - unfolding gwf.fold_insert[OF fing'' a'ndomg''] f ..
1.742 - also have g''leg: "?g |` dom ?g'' \<subseteq>\<^sub>m ?g" by(auto simp add: map_le_def)
1.743 - have "dom (?g |` dom ?g'') = dom ?g''" by auto
1.744 - 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",
1.745 - unfolded this, OF fing'' a'ndomg'' g''leg]
1.746 - also have b': "b' = ?b' a'" by(auto simp add: map_default_def)
1.747 - from upd_left_comm upd_left_comm fing''
1.748 - have "fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'') = fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g'')"
1.749 - by(rule finite_rec_cong1)(auto simp add: restrict_map_def b'b map_default_def)
1.750 - with b' have "upd a' b' (fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g'')) =
1.751 - upd a' (?b' a') (fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g''))" by simp
1.752 - also note g'wf.fold_insert[OF fing'' a'ndomg'', symmetric]
1.753 - finally have "upd a' b' (fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g)) =
1.754 - fold (\<lambda>a. upd a (?b' a)) (cnst b) (dom ?g)"
1.755 - unfolding domg . }
1.756 - ultimately have "fold (\<lambda>a. upd a (?b' a)) (cnst b) (insert a' (dom ?g)) =
1.757 - upd a' b' (fold (\<lambda>a. upd a (?b a)) (cnst b) (dom ?g))" by simp
1.758 - thus ?thesis unfolding finfun_rec_def Let_def finfun_default_update_const b[symmetric] g[symmetric] g' dom[symmetric]
1.759 - using b'b gg by(simp add: map_default_insert)
1.760 - qed
1.761 - qed
1.762 -qed
1.763 -
1.764 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.765 -
1.766 -end
1.767 -
1.768 -locale finfun_rec_wf = finfun_rec_wf_aux +
1.769 - assumes const_update_all:
1.770 - "finite (UNIV :: 'a set) \<Longrightarrow> fold (\<lambda>a. upd a b') (cnst b) (UNIV :: 'a set) = cnst b'"
1.771 -begin
1.772 -
1.773 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.774 -
1.775 -lemma finfun_rec_const [simp]:
1.776 - "finfun_rec cnst upd (\<lambda>\<^isup>f c) = cnst c"
1.777 -proof(cases "finite (UNIV :: 'a set)")
1.778 - case False
1.779 - hence "finfun_default ((\<lambda>\<^isup>f c) :: 'a \<Rightarrow>\<^isub>f 'b) = c" by(simp add: finfun_default_const)
1.780 - 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"
1.781 - proof
1.782 - show "(\<lambda>\<^isup>f c) = Abs_finfun (map_default c empty) \<and> finite (dom empty) \<and> c \<notin> ran empty"
1.783 - by(auto simp add: finfun_const_def)
1.784 - next
1.785 - fix g :: "'a \<rightharpoonup> 'b"
1.786 - assume "(\<lambda>\<^isup>f c) = Abs_finfun (map_default c g) \<and> finite (dom g) \<and> c \<notin> ran g"
1.787 - 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+
1.788 - from g map_default_in_finfun[OF fin, of c] have "map_default c g = (\<lambda>a. c)"
1.789 - by(simp add: finfun_const_def)
1.790 - moreover have "map_default c empty = (\<lambda>a. c)" by simp
1.791 - ultimately show "g = empty" by-(rule map_default_inject[OF disjI2[OF refl] fin ran], auto)
1.792 - qed
1.793 - ultimately show ?thesis by(simp add: finfun_rec_def)
1.794 -next
1.795 - case True
1.796 - hence default: "finfun_default ((\<lambda>\<^isup>f c) :: 'a \<Rightarrow>\<^isub>f 'b) = undefined" by(simp add: finfun_default_const)
1.797 - let ?the = "\<lambda>g :: 'a \<rightharpoonup> 'b. (\<lambda>\<^isup>f c) = Abs_finfun (map_default undefined g) \<and> finite (dom g) \<and> undefined \<notin> ran g"
1.798 - show ?thesis
1.799 - proof(cases "c = undefined")
1.800 - case True
1.801 - have the: "The ?the = empty"
1.802 - proof
1.803 - from True show "?the empty" by(auto simp add: finfun_const_def)
1.804 - next
1.805 - fix g'
1.806 - assume "?the g'"
1.807 - hence fg: "(\<lambda>\<^isup>f c) = Abs_finfun (map_default undefined g')"
1.808 - and fin: "finite (dom g')" and g: "undefined \<notin> ran g'" by simp_all
1.809 - from fin have "map_default undefined g' \<in> finfun" by(rule map_default_in_finfun)
1.810 - with fg have "map_default undefined g' = (\<lambda>a. c)"
1.811 - by(auto simp add: finfun_const_def intro: Abs_finfun_inject[THEN iffD1])
1.812 - with True show "g' = empty"
1.813 - by -(rule map_default_inject(2)[OF _ fin g], auto)
1.814 - qed
1.815 - show ?thesis unfolding finfun_rec_def using `finite UNIV` True
1.816 - unfolding Let_def the default by(simp)
1.817 - next
1.818 - case False
1.819 - have the: "The ?the = (\<lambda>a :: 'a. Some c)"
1.820 - proof
1.821 - from False True show "?the (\<lambda>a :: 'a. Some c)"
1.822 - by(auto simp add: map_default_def_raw finfun_const_def dom_def ran_def)
1.823 - next
1.824 - fix g' :: "'a \<rightharpoonup> 'b"
1.825 - assume "?the g'"
1.826 - hence fg: "(\<lambda>\<^isup>f c) = Abs_finfun (map_default undefined g')"
1.827 - and fin: "finite (dom g')" and g: "undefined \<notin> ran g'" by simp_all
1.828 - from fin have "map_default undefined g' \<in> finfun" by(rule map_default_in_finfun)
1.829 - with fg have "map_default undefined g' = (\<lambda>a. c)"
1.830 - by(auto simp add: finfun_const_def intro: Abs_finfun_inject[THEN iffD1])
1.831 - with True False show "g' = (\<lambda>a::'a. Some c)"
1.832 - by -(rule map_default_inject(2)[OF _ fin g], auto simp add: dom_def ran_def map_default_def_raw)
1.833 - qed
1.834 - show ?thesis unfolding finfun_rec_def using True False
1.835 - unfolding Let_def the default by(simp add: dom_def map_default_def const_update_all)
1.836 - qed
1.837 -qed
1.838 -
1.839 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.840 -
1.841 -end
1.842 -
1.843 -subsection {* Weak induction rule and case analysis for FinFuns *}
1.844 -
1.845 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.846 -
1.847 -lemma finfun_weak_induct [consumes 0, case_names const update]:
1.848 - assumes const: "\<And>b. P (\<lambda>\<^isup>f b)"
1.849 - and update: "\<And>f a b. P f \<Longrightarrow> P (f(\<^sup>f a := b))"
1.850 - shows "P x"
1.851 -proof(induct x rule: Abs_finfun_induct)
1.852 - case (Abs_finfun y)
1.853 - then obtain b where "finite {a. y a \<noteq> b}" unfolding finfun_def by blast
1.854 - thus ?case using `y \<in> finfun`
1.855 - proof(induct x\<equiv>"{a. y a \<noteq> b}" arbitrary: y rule: finite_induct)
1.856 - case empty
1.857 - hence "\<And>a. y a = b" by blast
1.858 - hence "y = (\<lambda>a. b)" by(auto intro: ext)
1.859 - hence "Abs_finfun y = finfun_const b" unfolding finfun_const_def by simp
1.860 - thus ?case by(simp add: const)
1.861 - next
1.862 - case (insert a A)
1.863 - note IH = `\<And>y. \<lbrakk> y \<in> finfun; A = {a. y a \<noteq> b} \<rbrakk> \<Longrightarrow> P (Abs_finfun y)`
1.864 - note y = `y \<in> finfun`
1.865 - with `insert a A = {a. y a \<noteq> b}` `a \<notin> A`
1.866 - have "y(a := b) \<in> finfun" "A = {a'. (y(a := b)) a' \<noteq> b}" by auto
1.867 - from IH[OF this] have "P (finfun_update (Abs_finfun (y(a := b))) a (y a))" by(rule update)
1.868 - thus ?case using y unfolding finfun_update_def by simp
1.869 - qed
1.870 -qed
1.871 -
1.872 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.873 -
1.874 -lemma finfun_exhaust_disj: "(\<exists>b. x = finfun_const b) \<or> (\<exists>f a b. x = finfun_update f a b)"
1.875 -by(induct x rule: finfun_weak_induct) blast+
1.876 -
1.877 -lemma finfun_exhaust:
1.878 - obtains b where "x = (\<lambda>\<^isup>f b)"
1.879 - | f a b where "x = f(\<^sup>f a := b)"
1.880 -by(atomize_elim)(rule finfun_exhaust_disj)
1.881 -
1.882 -lemma finfun_rec_unique:
1.883 - fixes f :: "'a \<Rightarrow>\<^isub>f 'b \<Rightarrow> 'c"
1.884 - assumes c: "\<And>c. f (\<lambda>\<^isup>f c) = cnst c"
1.885 - and u: "\<And>g a b. f (g(\<^sup>f a := b)) = upd g a b (f g)"
1.886 - and c': "\<And>c. f' (\<lambda>\<^isup>f c) = cnst c"
1.887 - and u': "\<And>g a b. f' (g(\<^sup>f a := b)) = upd g a b (f' g)"
1.888 - shows "f = f'"
1.889 -proof
1.890 - fix g :: "'a \<Rightarrow>\<^isub>f 'b"
1.891 - show "f g = f' g"
1.892 - by(induct g rule: finfun_weak_induct)(auto simp add: c u c' u')
1.893 -qed
1.894 -
1.895 -
1.896 -subsection {* Function application *}
1.897 -
1.898 -definition finfun_apply :: "'a \<Rightarrow>\<^isub>f 'b \<Rightarrow> 'a \<Rightarrow> 'b" ("_\<^sub>f" [1000] 1000)
1.899 -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)"
1.900 -
1.901 -interpretation finfun_apply_aux: finfun_rec_wf_aux "\<lambda>b. b" "\<lambda>a' b c. if (a = a') then b else c"
1.902 -by(unfold_locales) auto
1.903 -
1.904 -interpretation finfun_apply: finfun_rec_wf "\<lambda>b. b" "\<lambda>a' b c. if (a = a') then b else c"
1.905 -proof(unfold_locales)
1.906 - fix b' b :: 'a
1.907 - assume fin: "finite (UNIV :: 'b set)"
1.908 - { fix A :: "'b set"
1.909 - interpret fun_left_comm "\<lambda>a'. If (a = a') b'" by(rule finfun_apply_aux.upd_left_comm)
1.910 - from fin have "finite A" by(auto intro: finite_subset)
1.911 - hence "fold (\<lambda>a'. If (a = a') b') b A = (if a \<in> A then b' else b)"
1.912 - by induct auto }
1.913 - from this[of UNIV] show "fold (\<lambda>a'. If (a = a') b') b UNIV = b'" by simp
1.914 -qed
1.915 -
1.916 -lemma finfun_const_apply [simp, code]: "(\<lambda>\<^isup>f b)\<^sub>f a = b"
1.917 -by(simp add: finfun_apply_def)
1.918 -
1.919 -lemma finfun_upd_apply: "f(\<^sup>fa := b)\<^sub>f a' = (if a = a' then b else f\<^sub>f a')"
1.920 - 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')"
1.921 -by(simp_all add: finfun_apply_def)
1.922 -
1.923 -lemma finfun_upd_apply_same [simp]:
1.924 - "f(\<^sup>fa := b)\<^sub>f a = b"
1.925 -by(simp add: finfun_upd_apply)
1.926 -
1.927 -lemma finfun_upd_apply_other [simp]:
1.928 - "a \<noteq> a' \<Longrightarrow> f(\<^sup>fa := b)\<^sub>f a' = f\<^sub>f a'"
1.929 -by(simp add: finfun_upd_apply)
1.930 -
1.931 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.932 -
1.933 -lemma finfun_apply_Rep_finfun:
1.934 - "finfun_apply = Rep_finfun"
1.935 -proof(rule finfun_rec_unique)
1.936 - fix c show "Rep_finfun (\<lambda>\<^isup>f c) = (\<lambda>a. c)" by(auto simp add: finfun_const_def)
1.937 -next
1.938 - fix g a b show "Rep_finfun g(\<^sup>f a := b) = (\<lambda>c. if c = a then b else Rep_finfun g c)"
1.939 - by(auto simp add: finfun_update_def fun_upd_finfun Abs_finfun_inverse Rep_finfun intro: ext)
1.940 -qed(auto intro: ext)
1.941 -
1.942 -lemma finfun_ext: "(\<And>a. f\<^sub>f a = g\<^sub>f a) \<Longrightarrow> f = g"
1.943 -by(auto simp add: finfun_apply_Rep_finfun Rep_finfun_inject[symmetric] simp del: Rep_finfun_inject intro: ext)
1.944 -
1.945 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.946 -
1.947 -lemma expand_finfun_eq: "(f = g) = (f\<^sub>f = g\<^sub>f)"
1.948 -by(auto intro: finfun_ext)
1.949 -
1.950 -lemma finfun_const_inject [simp]: "(\<lambda>\<^isup>f b) = (\<lambda>\<^isup>f b') \<equiv> b = b'"
1.951 -by(simp add: expand_finfun_eq expand_fun_eq)
1.952 -
1.953 -lemma finfun_const_eq_update:
1.954 - "((\<lambda>\<^isup>f b) = f(\<^sup>f a := b')) = (b = b' \<and> (\<forall>a'. a \<noteq> a' \<longrightarrow> f\<^sub>f a' = b))"
1.955 -by(auto simp add: expand_finfun_eq expand_fun_eq finfun_upd_apply)
1.956 -
1.957 -subsection {* Function composition *}
1.958 -
1.959 -definition finfun_comp :: "('a \<Rightarrow> 'b) \<Rightarrow> 'c \<Rightarrow>\<^isub>f 'a \<Rightarrow> 'c \<Rightarrow>\<^isub>f 'b" (infixr "\<circ>\<^isub>f" 55)
1.960 -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"
1.961 -
1.962 -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))"
1.963 -by(unfold_locales)(auto simp add: finfun_upd_apply intro: finfun_ext)
1.964 -
1.965 -interpretation finfun_comp: finfun_rec_wf "(\<lambda>b. (\<lambda>\<^isup>f g b))" "(\<lambda>a b c. c(\<^sup>f a := g b))"
1.966 -proof
1.967 - fix b' b :: 'a
1.968 - assume fin: "finite (UNIV :: 'c set)"
1.969 - { fix A :: "'c set"
1.970 - from fin have "finite A" by(auto intro: finite_subset)
1.971 - hence "fold (\<lambda>(a :: 'c) c. c(\<^sup>f a := g b')) (\<lambda>\<^isup>f g b) A =
1.972 - Abs_finfun (\<lambda>a. if a \<in> A then g b' else g b)"
1.973 - 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) }
1.974 - 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')"
1.975 - by(simp add: finfun_const_def)
1.976 -qed
1.977 -
1.978 -lemma finfun_comp_const [simp, code]:
1.979 - "g \<circ>\<^isub>f (\<lambda>\<^isup>f c) = (\<lambda>\<^isup>f g c)"
1.980 -by(simp add: finfun_comp_def)
1.981 -
1.982 -lemma finfun_comp_update [simp]: "g \<circ>\<^isub>f (f(\<^sup>f a := b)) = (g \<circ>\<^isub>f f)(\<^sup>f a := g b)"
1.983 - 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)"
1.984 -by(simp_all add: finfun_comp_def)
1.985 -
1.986 -lemma finfun_comp_apply [simp]:
1.987 - "(g \<circ>\<^isub>f f)\<^sub>f = g \<circ> f\<^sub>f"
1.988 -by(induct f rule: finfun_weak_induct)(auto simp add: finfun_upd_apply intro: ext)
1.989 -
1.990 -lemma finfun_comp_comp_collapse [simp]: "f \<circ>\<^isub>f g \<circ>\<^isub>f h = (f o g) \<circ>\<^isub>f h"
1.991 -by(induct h rule: finfun_weak_induct) simp_all
1.992 -
1.993 -lemma finfun_comp_const1 [simp]: "(\<lambda>x. c) \<circ>\<^isub>f f = (\<lambda>\<^isup>f c)"
1.994 -by(induct f rule: finfun_weak_induct)(auto intro: finfun_ext simp add: finfun_upd_apply)
1.995 -
1.996 -lemma finfun_comp_id1 [simp]: "(\<lambda>x. x) \<circ>\<^isub>f f = f" "id \<circ>\<^isub>f f = f"
1.997 -by(induct f rule: finfun_weak_induct) auto
1.998 -
1.999 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.1000 -
1.1001 -lemma finfun_comp_conv_comp: "g \<circ>\<^isub>f f = Abs_finfun (g \<circ> finfun_apply f)"
1.1002 -proof -
1.1003 - have "(\<lambda>f. g \<circ>\<^isub>f f) = (\<lambda>f. Abs_finfun (g \<circ> finfun_apply f))"
1.1004 - proof(rule finfun_rec_unique)
1.1005 - { fix c show "Abs_finfun (g \<circ> (\<lambda>\<^isup>f c)\<^sub>f) = (\<lambda>\<^isup>f g c)"
1.1006 - by(simp add: finfun_comp_def o_def)(simp add: finfun_const_def) }
1.1007 - { 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)"
1.1008 - proof -
1.1009 - obtain y where y: "y \<in> finfun" and g': "g' = Abs_finfun y" by(cases g')
1.1010 - moreover hence "(g \<circ> g'\<^sub>f) \<in> finfun" by(simp add: finfun_apply_Rep_finfun finfun_left_compose)
1.1011 - moreover have "g \<circ> y(a := b) = (g \<circ> y)(a := g b)" by(auto intro: ext)
1.1012 - ultimately show ?thesis by(simp add: finfun_comp_def finfun_update_def finfun_apply_Rep_finfun)
1.1013 - qed }
1.1014 - qed auto
1.1015 - thus ?thesis by(auto simp add: expand_fun_eq)
1.1016 -qed
1.1017 -
1.1018 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.1019 -
1.1020 -
1.1021 -
1.1022 -definition finfun_comp2 :: "'b \<Rightarrow>\<^isub>f 'c \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'c" (infixr "\<^sub>f\<circ>" 55)
1.1023 -where [code del]: "finfun_comp2 g f = Abs_finfun (Rep_finfun g \<circ> f)"
1.1024 -
1.1025 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.1026 -
1.1027 -lemma finfun_comp2_const [code, simp]: "finfun_comp2 (\<lambda>\<^isup>f c) f = (\<lambda>\<^isup>f c)"
1.1028 -by(simp add: finfun_comp2_def finfun_const_def comp_def)
1.1029 -
1.1030 -lemma finfun_comp2_update:
1.1031 - assumes inj: "inj f"
1.1032 - 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)"
1.1033 -proof(cases "b \<in> range f")
1.1034 - case True
1.1035 - 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)
1.1036 - with inj True show ?thesis by(auto simp add: finfun_comp2_def finfun_update_def finfun_right_compose)
1.1037 -next
1.1038 - case False
1.1039 - hence "(Rep_finfun g)(b := c) \<circ> f = Rep_finfun g \<circ> f" by(auto simp add: expand_fun_eq)
1.1040 - with False show ?thesis by(auto simp add: finfun_comp2_def finfun_update_def)
1.1041 -qed
1.1042 -
1.1043 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.1044 -
1.1045 -subsection {* A type class for computing the cardinality of a type's universe *}
1.1046 -
1.1047 -class card_UNIV =
1.1048 - fixes card_UNIV :: "'a itself \<Rightarrow> nat"
1.1049 - assumes card_UNIV: "card_UNIV x = card (UNIV :: 'a set)"
1.1050 -begin
1.1051 -
1.1052 -lemma card_UNIV_neq_0_finite_UNIV:
1.1053 - "card_UNIV x \<noteq> 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
1.1054 -by(simp add: card_UNIV card_eq_0_iff)
1.1055 -
1.1056 -lemma card_UNIV_ge_0_finite_UNIV:
1.1057 - "card_UNIV x > 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
1.1058 -by(auto simp add: card_UNIV intro: card_ge_0_finite finite_UNIV_card_ge_0)
1.1059 -
1.1060 -lemma card_UNIV_eq_0_infinite_UNIV:
1.1061 - "card_UNIV x = 0 \<longleftrightarrow> infinite (UNIV :: 'a set)"
1.1062 -by(simp add: card_UNIV card_eq_0_iff)
1.1063 -
1.1064 -definition is_list_UNIV :: "'a list \<Rightarrow> bool"
1.1065 -where "is_list_UNIV xs = (let c = card_UNIV (TYPE('a)) in if c = 0 then False else size (remdups xs) = c)"
1.1066 -
1.1067 -lemma is_list_UNIV_iff:
1.1068 - fixes xs :: "'a list"
1.1069 - shows "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
1.1070 -proof
1.1071 - assume "is_list_UNIV xs"
1.1072 - hence c: "card_UNIV (TYPE('a)) > 0" and xs: "size (remdups xs) = card_UNIV (TYPE('a))"
1.1073 - unfolding is_list_UNIV_def by(simp_all add: Let_def split: split_if_asm)
1.1074 - from c have fin: "finite (UNIV :: 'a set)" by(auto simp add: card_UNIV_ge_0_finite_UNIV)
1.1075 - have "card (set (remdups xs)) = size (remdups xs)" by(subst distinct_card) auto
1.1076 - also note set_remdups
1.1077 - finally show "set xs = UNIV" using fin unfolding xs card_UNIV by-(rule card_eq_UNIV_imp_eq_UNIV)
1.1078 -next
1.1079 - assume xs: "set xs = UNIV"
1.1080 - from finite_set[of xs] have fin: "finite (UNIV :: 'a set)" unfolding xs .
1.1081 - hence "card_UNIV (TYPE ('a)) \<noteq> 0" unfolding card_UNIV_neq_0_finite_UNIV .
1.1082 - moreover have "size (remdups xs) = card (set (remdups xs))"
1.1083 - by(subst distinct_card) auto
1.1084 - ultimately show "is_list_UNIV xs" using xs by(simp add: is_list_UNIV_def Let_def card_UNIV)
1.1085 -qed
1.1086 -
1.1087 -lemma card_UNIV_eq_0_is_list_UNIV_False:
1.1088 - assumes cU0: "card_UNIV x = 0"
1.1089 - shows "is_list_UNIV = (\<lambda>xs. False)"
1.1090 -proof(rule ext)
1.1091 - fix xs :: "'a list"
1.1092 - from cU0 have "infinite (UNIV :: 'a set)"
1.1093 - by(auto simp only: card_UNIV_eq_0_infinite_UNIV)
1.1094 - moreover have "finite (set xs)" by(rule finite_set)
1.1095 - ultimately have "(UNIV :: 'a set) \<noteq> set xs" by(auto simp del: finite_set)
1.1096 - thus "is_list_UNIV xs = False" unfolding is_list_UNIV_iff by simp
1.1097 -qed
1.1098 -
1.1099 -end
1.1100 -
1.1101 -subsection {* Instantiations for @{text "card_UNIV"} *}
1.1102 -
1.1103 -subsubsection {* @{typ "nat"} *}
1.1104 -
1.1105 -instantiation nat :: card_UNIV begin
1.1106 -
1.1107 -definition card_UNIV_nat_def:
1.1108 - "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
1.1109 -
1.1110 -instance proof
1.1111 - fix x :: "nat itself"
1.1112 - show "card_UNIV x = card (UNIV :: nat set)"
1.1113 - unfolding card_UNIV_nat_def by simp
1.1114 -qed
1.1115 -
1.1116 -end
1.1117 -
1.1118 -subsubsection {* @{typ "int"} *}
1.1119 -
1.1120 -instantiation int :: card_UNIV begin
1.1121 -
1.1122 -definition card_UNIV_int_def:
1.1123 - "card_UNIV_class.card_UNIV = (\<lambda>a :: int itself. 0)"
1.1124 -
1.1125 -instance proof
1.1126 - fix x :: "int itself"
1.1127 - show "card_UNIV x = card (UNIV :: int set)"
1.1128 - unfolding card_UNIV_int_def by simp
1.1129 -qed
1.1130 -
1.1131 -end
1.1132 -
1.1133 -subsubsection {* @{typ "'a list"} *}
1.1134 -
1.1135 -instantiation list :: (type) card_UNIV begin
1.1136 -
1.1137 -definition card_UNIV_list_def:
1.1138 - "card_UNIV_class.card_UNIV = (\<lambda>a :: 'a list itself. 0)"
1.1139 -
1.1140 -instance proof
1.1141 - fix x :: "'a list itself"
1.1142 - show "card_UNIV x = card (UNIV :: 'a list set)"
1.1143 - unfolding card_UNIV_list_def by(simp add: infinite_UNIV_listI)
1.1144 -qed
1.1145 -
1.1146 -end
1.1147 -
1.1148 -subsubsection {* @{typ "unit"} *}
1.1149 -
1.1150 -lemma card_UNIV_unit: "card (UNIV :: unit set) = 1"
1.1151 - unfolding UNIV_unit by simp
1.1152 -
1.1153 -instantiation unit :: card_UNIV begin
1.1154 -
1.1155 -definition card_UNIV_unit_def:
1.1156 - "card_UNIV_class.card_UNIV = (\<lambda>a :: unit itself. 1)"
1.1157 -
1.1158 -instance proof
1.1159 - fix x :: "unit itself"
1.1160 - show "card_UNIV x = card (UNIV :: unit set)"
1.1161 - by(simp add: card_UNIV_unit_def card_UNIV_unit)
1.1162 -qed
1.1163 -
1.1164 -end
1.1165 -
1.1166 -subsubsection {* @{typ "bool"} *}
1.1167 -
1.1168 -lemma card_UNIV_bool: "card (UNIV :: bool set) = 2"
1.1169 - unfolding UNIV_bool by simp
1.1170 -
1.1171 -instantiation bool :: card_UNIV begin
1.1172 -
1.1173 -definition card_UNIV_bool_def:
1.1174 - "card_UNIV_class.card_UNIV = (\<lambda>a :: bool itself. 2)"
1.1175 -
1.1176 -instance proof
1.1177 - fix x :: "bool itself"
1.1178 - show "card_UNIV x = card (UNIV :: bool set)"
1.1179 - by(simp add: card_UNIV_bool_def card_UNIV_bool)
1.1180 -qed
1.1181 -
1.1182 -end
1.1183 -
1.1184 -subsubsection {* @{typ "char"} *}
1.1185 -
1.1186 -lemma card_UNIV_char: "card (UNIV :: char set) = 256"
1.1187 -proof -
1.1188 - from enum_distinct
1.1189 - have "card (set (enum :: char list)) = length (enum :: char list)"
1.1190 - by - (rule distinct_card)
1.1191 - also have "set enum = (UNIV :: char set)" by auto
1.1192 - also note enum_chars
1.1193 - finally show ?thesis by (simp add: chars_def)
1.1194 -qed
1.1195 -
1.1196 -instantiation char :: card_UNIV begin
1.1197 -
1.1198 -definition card_UNIV_char_def:
1.1199 - "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
1.1200 -
1.1201 -instance proof
1.1202 - fix x :: "char itself"
1.1203 - show "card_UNIV x = card (UNIV :: char set)"
1.1204 - by(simp add: card_UNIV_char_def card_UNIV_char)
1.1205 -qed
1.1206 -
1.1207 -end
1.1208 -
1.1209 -subsubsection {* @{typ "'a \<times> 'b"} *}
1.1210 -
1.1211 -instantiation * :: (card_UNIV, card_UNIV) card_UNIV begin
1.1212 -
1.1213 -definition card_UNIV_product_def:
1.1214 - "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
1.1215 -
1.1216 -instance proof
1.1217 - fix x :: "('a \<times> 'b) itself"
1.1218 - show "card_UNIV x = card (UNIV :: ('a \<times> 'b) set)"
1.1219 - by(simp add: card_UNIV_product_def card_UNIV UNIV_Times_UNIV[symmetric] card_cartesian_product del: UNIV_Times_UNIV)
1.1220 -qed
1.1221 -
1.1222 -end
1.1223 -
1.1224 -subsubsection {* @{typ "'a + 'b"} *}
1.1225 -
1.1226 -instantiation "+" :: (card_UNIV, card_UNIV) card_UNIV begin
1.1227 -
1.1228 -definition card_UNIV_sum_def:
1.1229 - "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a + 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
1.1230 - in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
1.1231 -
1.1232 -instance proof
1.1233 - fix x :: "('a + 'b) itself"
1.1234 - show "card_UNIV x = card (UNIV :: ('a + 'b) set)"
1.1235 - 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)
1.1236 -qed
1.1237 -
1.1238 -end
1.1239 -
1.1240 -subsubsection {* @{typ "'a \<Rightarrow> 'b"} *}
1.1241 -
1.1242 -instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
1.1243 -
1.1244 -definition card_UNIV_fun_def:
1.1245 - "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
1.1246 - in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
1.1247 -
1.1248 -instance proof
1.1249 - fix x :: "('a \<Rightarrow> 'b) itself"
1.1250 -
1.1251 - { assume "0 < card (UNIV :: 'a set)"
1.1252 - and "0 < card (UNIV :: 'b set)"
1.1253 - hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
1.1254 - by(simp_all only: card_ge_0_finite)
1.1255 - from finite_distinct_list[OF finb] obtain bs
1.1256 - where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
1.1257 - from finite_distinct_list[OF fina] obtain as
1.1258 - where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
1.1259 - have cb: "card (UNIV :: 'b set) = length bs"
1.1260 - unfolding bs[symmetric] distinct_card[OF distb] ..
1.1261 - have ca: "card (UNIV :: 'a set) = length as"
1.1262 - unfolding as[symmetric] distinct_card[OF dista] ..
1.1263 - let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (n_lists (length as) bs)"
1.1264 - have "UNIV = set ?xs"
1.1265 - proof(rule UNIV_eq_I)
1.1266 - fix f :: "'a \<Rightarrow> 'b"
1.1267 - from as have "f = the \<circ> map_of (zip as (map f as))"
1.1268 - by(auto simp add: map_of_zip_map intro: ext)
1.1269 - thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
1.1270 - qed
1.1271 - moreover have "distinct ?xs" unfolding distinct_map
1.1272 - proof(intro conjI distinct_n_lists distb inj_onI)
1.1273 - fix xs ys :: "'b list"
1.1274 - assume xs: "xs \<in> set (n_lists (length as) bs)"
1.1275 - and ys: "ys \<in> set (n_lists (length as) bs)"
1.1276 - and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
1.1277 - from xs ys have [simp]: "length xs = length as" "length ys = length as"
1.1278 - by(simp_all add: length_n_lists_elem)
1.1279 - have "map_of (zip as xs) = map_of (zip as ys)"
1.1280 - proof
1.1281 - fix x
1.1282 - from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
1.1283 - by(simp_all add: map_of_zip_is_Some[symmetric])
1.1284 - with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
1.1285 - by(auto dest: fun_cong[where x=x])
1.1286 - qed
1.1287 - with dista show "xs = ys" by(simp add: map_of_zip_inject)
1.1288 - qed
1.1289 - hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
1.1290 - moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
1.1291 - ultimately have "card (UNIV :: ('a \<Rightarrow> 'b) set) = card (UNIV :: 'b set) ^ card (UNIV :: 'a set)"
1.1292 - using cb ca by simp }
1.1293 - moreover {
1.1294 - assume cb: "card (UNIV :: 'b set) = Suc 0"
1.1295 - then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
1.1296 - have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
1.1297 - proof(rule UNIV_eq_I)
1.1298 - fix x :: "'a \<Rightarrow> 'b"
1.1299 - { fix y
1.1300 - have "x y \<in> UNIV" ..
1.1301 - hence "x y = b" unfolding b by simp }
1.1302 - thus "x \<in> {\<lambda>x. b}" by(auto intro: ext)
1.1303 - qed
1.1304 - have "card (UNIV :: ('a \<Rightarrow> 'b) set) = Suc 0" unfolding eq by simp }
1.1305 - ultimately show "card_UNIV x = card (UNIV :: ('a \<Rightarrow> 'b) set)"
1.1306 - unfolding card_UNIV_fun_def card_UNIV Let_def
1.1307 - by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
1.1308 -qed
1.1309 -
1.1310 -end
1.1311 -
1.1312 -subsubsection {* @{typ "'a option"} *}
1.1313 -
1.1314 -instantiation option :: (card_UNIV) card_UNIV
1.1315 -begin
1.1316 -
1.1317 -definition card_UNIV_option_def:
1.1318 - "card_UNIV_class.card_UNIV = (\<lambda>a :: 'a option itself. let c = card_UNIV (TYPE('a))
1.1319 - in if c \<noteq> 0 then Suc c else 0)"
1.1320 -
1.1321 -instance proof
1.1322 - fix x :: "'a option itself"
1.1323 - show "card_UNIV x = card (UNIV :: 'a option set)"
1.1324 - unfolding UNIV_option_conv
1.1325 - by(auto simp add: card_UNIV_option_def card_UNIV card_eq_0_iff Let_def intro: inj_Some dest: finite_imageD)
1.1326 - (subst card_insert_disjoint, auto simp add: card_eq_0_iff card_image inj_Some intro: finite_imageI card_ge_0_finite)
1.1327 -qed
1.1328 -
1.1329 -end
1.1330 -
1.1331 -
1.1332 -subsection {* Universal quantification *}
1.1333 -
1.1334 -definition finfun_All_except :: "'a list \<Rightarrow> 'a \<Rightarrow>\<^isub>f bool \<Rightarrow> bool"
1.1335 -where [code del]: "finfun_All_except A P \<equiv> \<forall>a. a \<in> set A \<or> P\<^sub>f a"
1.1336 -
1.1337 -lemma finfun_All_except_const: "finfun_All_except A (\<lambda>\<^isup>f b) \<longleftrightarrow> b \<or> set A = UNIV"
1.1338 -by(auto simp add: finfun_All_except_def)
1.1339 -
1.1340 -lemma finfun_All_except_const_finfun_UNIV_code [code]:
1.1341 - "finfun_All_except A (\<lambda>\<^isup>f b) = (b \<or> is_list_UNIV A)"
1.1342 -by(simp add: finfun_All_except_const is_list_UNIV_iff)
1.1343 -
1.1344 -lemma finfun_All_except_update:
1.1345 - "finfun_All_except A f(\<^sup>f a := b) = ((a \<in> set A \<or> b) \<and> finfun_All_except (a # A) f)"
1.1346 -by(fastsimp simp add: finfun_All_except_def finfun_upd_apply)
1.1347 -
1.1348 -lemma finfun_All_except_update_code [code]:
1.1349 - fixes a :: "'a :: card_UNIV"
1.1350 - shows "finfun_All_except A (finfun_update_code f a b) = ((a \<in> set A \<or> b) \<and> finfun_All_except (a # A) f)"
1.1351 -by(simp add: finfun_All_except_update)
1.1352 -
1.1353 -definition finfun_All :: "'a \<Rightarrow>\<^isub>f bool \<Rightarrow> bool"
1.1354 -where "finfun_All = finfun_All_except []"
1.1355 -
1.1356 -lemma finfun_All_const [simp]: "finfun_All (\<lambda>\<^isup>f b) = b"
1.1357 -by(simp add: finfun_All_def finfun_All_except_def)
1.1358 -
1.1359 -lemma finfun_All_update: "finfun_All f(\<^sup>f a := b) = (b \<and> finfun_All_except [a] f)"
1.1360 -by(simp add: finfun_All_def finfun_All_except_update)
1.1361 -
1.1362 -lemma finfun_All_All: "finfun_All P = All P\<^sub>f"
1.1363 -by(simp add: finfun_All_def finfun_All_except_def)
1.1364 -
1.1365 -
1.1366 -definition finfun_Ex :: "'a \<Rightarrow>\<^isub>f bool \<Rightarrow> bool"
1.1367 -where "finfun_Ex P = Not (finfun_All (Not \<circ>\<^isub>f P))"
1.1368 -
1.1369 -lemma finfun_Ex_Ex: "finfun_Ex P = Ex P\<^sub>f"
1.1370 -unfolding finfun_Ex_def finfun_All_All by simp
1.1371 -
1.1372 -lemma finfun_Ex_const [simp]: "finfun_Ex (\<lambda>\<^isup>f b) = b"
1.1373 -by(simp add: finfun_Ex_def)
1.1374 -
1.1375 -
1.1376 -subsection {* A diagonal operator for FinFuns *}
1.1377 -
1.1378 -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)
1.1379 -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"
1.1380 -
1.1381 -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))"
1.1382 -by(unfold_locales)(simp_all add: expand_finfun_eq expand_fun_eq finfun_upd_apply)
1.1383 -
1.1384 -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))"
1.1385 -proof
1.1386 - fix b' b :: 'a
1.1387 - assume fin: "finite (UNIV :: 'c set)"
1.1388 - { fix A :: "'c set"
1.1389 - interpret fun_left_comm "\<lambda>a c. c(\<^sup>f a := (b', g\<^sub>f a))" by(rule finfun_Diag_aux.upd_left_comm)
1.1390 - from fin have "finite A" by(auto intro: finite_subset)
1.1391 - hence "fold (\<lambda>a c. c(\<^sup>f a := (b', g\<^sub>f a))) (Pair b \<circ>\<^isub>f g) A =
1.1392 - Abs_finfun (\<lambda>a. (if a \<in> A then b' else b, g\<^sub>f a))"
1.1393 - by(induct)(simp_all add: finfun_const_def finfun_comp_conv_comp o_def,
1.1394 - auto simp add: finfun_update_def Abs_finfun_inverse_finite fun_upd_def Abs_finfun_inject_finite expand_fun_eq fin) }
1.1395 - 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"
1.1396 - by(simp add: finfun_const_def finfun_comp_conv_comp o_def)
1.1397 -qed
1.1398 -
1.1399 -lemma finfun_Diag_const1: "(\<lambda>\<^isup>f b, g)\<^sup>f = Pair b \<circ>\<^isub>f g"
1.1400 -by(simp add: finfun_Diag_def)
1.1401 -
1.1402 -text {*
1.1403 - 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"}.
1.1404 -*}
1.1405 -
1.1406 -lemma finfun_Diag_const_code [code]:
1.1407 - "(\<lambda>\<^isup>f b, \<lambda>\<^isup>f c)\<^sup>f = (\<lambda>\<^isup>f (b, c))"
1.1408 - "(\<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))"
1.1409 -by(simp_all add: finfun_Diag_const1)
1.1410 -
1.1411 -lemma finfun_Diag_update1: "(f(\<^sup>f a := b), g)\<^sup>f = (f, g)\<^sup>f(\<^sup>f a := (b, g\<^sub>f a))"
1.1412 - 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))"
1.1413 -by(simp_all add: finfun_Diag_def)
1.1414 -
1.1415 -lemma finfun_Diag_const2: "(f, \<lambda>\<^isup>f c)\<^sup>f = (\<lambda>b. (b, c)) \<circ>\<^isub>f f"
1.1416 -by(induct f rule: finfun_weak_induct)(auto intro!: finfun_ext simp add: finfun_upd_apply finfun_Diag_const1 finfun_Diag_update1)
1.1417 -
1.1418 -lemma finfun_Diag_update2: "(f, g(\<^sup>f a := c))\<^sup>f = (f, g)\<^sup>f(\<^sup>f a := (f\<^sub>f a, c))"
1.1419 -by(induct f rule: finfun_weak_induct)(auto intro!: finfun_ext simp add: finfun_upd_apply finfun_Diag_const1 finfun_Diag_update1)
1.1420 -
1.1421 -lemma finfun_Diag_const_const [simp]: "(\<lambda>\<^isup>f b, \<lambda>\<^isup>f c)\<^sup>f = (\<lambda>\<^isup>f (b, c))"
1.1422 -by(simp add: finfun_Diag_const1)
1.1423 -
1.1424 -lemma finfun_Diag_const_update:
1.1425 - "(\<lambda>\<^isup>f b, g(\<^sup>f a := c))\<^sup>f = (\<lambda>\<^isup>f b, g)\<^sup>f(\<^sup>f a := (b, c))"
1.1426 -by(simp add: finfun_Diag_const1)
1.1427 -
1.1428 -lemma finfun_Diag_update_const:
1.1429 - "(f(\<^sup>f a := b), \<lambda>\<^isup>f c)\<^sup>f = (f, \<lambda>\<^isup>f c)\<^sup>f(\<^sup>f a := (b, c))"
1.1430 -by(simp add: finfun_Diag_def)
1.1431 -
1.1432 -lemma finfun_Diag_update_update:
1.1433 - "(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)))"
1.1434 -by(auto simp add: finfun_Diag_update1 finfun_Diag_update2)
1.1435 -
1.1436 -lemma finfun_Diag_apply [simp]: "(f, g)\<^sup>f\<^sub>f = (\<lambda>x. (f\<^sub>f x, g\<^sub>f x))"
1.1437 -by(induct f rule: finfun_weak_induct)(auto simp add: finfun_Diag_const1 finfun_Diag_update1 finfun_upd_apply intro: ext)
1.1438 -
1.1439 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.1440 -
1.1441 -lemma finfun_Diag_conv_Abs_finfun:
1.1442 - "(f, g)\<^sup>f = Abs_finfun ((\<lambda>x. (Rep_finfun f x, Rep_finfun g x)))"
1.1443 -proof -
1.1444 - 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))))"
1.1445 - proof(rule finfun_rec_unique)
1.1446 - { fix c show "Abs_finfun (\<lambda>x. (Rep_finfun (\<lambda>\<^isup>f c) x, Rep_finfun g x)) = Pair c \<circ>\<^isub>f g"
1.1447 - by(simp add: finfun_comp_conv_comp finfun_apply_Rep_finfun o_def finfun_const_def) }
1.1448 - { fix g' a b
1.1449 - show "Abs_finfun (\<lambda>x. (Rep_finfun g'(\<^sup>f a := b) x, Rep_finfun g x)) =
1.1450 - (Abs_finfun (\<lambda>x. (Rep_finfun g' x, Rep_finfun g x)))(\<^sup>f a := (b, g\<^sub>f a))"
1.1451 - by(auto simp add: finfun_update_def expand_fun_eq finfun_apply_Rep_finfun simp del: fun_upd_apply) simp }
1.1452 - qed(simp_all add: finfun_Diag_const1 finfun_Diag_update1)
1.1453 - thus ?thesis by(auto simp add: expand_fun_eq)
1.1454 -qed
1.1455 -
1.1456 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.1457 -
1.1458 -lemma finfun_Diag_eq: "(f, g)\<^sup>f = (f', g')\<^sup>f \<longleftrightarrow> f = f' \<and> g = g'"
1.1459 -by(auto simp add: expand_finfun_eq expand_fun_eq)
1.1460 -
1.1461 -definition finfun_fst :: "'a \<Rightarrow>\<^isub>f ('b \<times> 'c) \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'b"
1.1462 -where [code]: "finfun_fst f = fst \<circ>\<^isub>f f"
1.1463 -
1.1464 -lemma finfun_fst_const: "finfun_fst (\<lambda>\<^isup>f bc) = (\<lambda>\<^isup>f fst bc)"
1.1465 -by(simp add: finfun_fst_def)
1.1466 -
1.1467 -lemma finfun_fst_update: "finfun_fst (f(\<^sup>f a := bc)) = (finfun_fst f)(\<^sup>f a := fst bc)"
1.1468 - and finfun_fst_update_code: "finfun_fst (finfun_update_code f a bc) = (finfun_fst f)(\<^sup>f a := fst bc)"
1.1469 -by(simp_all add: finfun_fst_def)
1.1470 -
1.1471 -lemma finfun_fst_comp_conv: "finfun_fst (f \<circ>\<^isub>f g) = (fst \<circ> f) \<circ>\<^isub>f g"
1.1472 -by(simp add: finfun_fst_def)
1.1473 -
1.1474 -lemma finfun_fst_conv [simp]: "finfun_fst (f, g)\<^sup>f = f"
1.1475 -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)
1.1476 -
1.1477 -lemma finfun_fst_conv_Abs_finfun: "finfun_fst = (\<lambda>f. Abs_finfun (fst o Rep_finfun f))"
1.1478 -by(simp add: finfun_fst_def_raw finfun_comp_conv_comp finfun_apply_Rep_finfun)
1.1479 -
1.1480 -
1.1481 -definition finfun_snd :: "'a \<Rightarrow>\<^isub>f ('b \<times> 'c) \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'c"
1.1482 -where [code]: "finfun_snd f = snd \<circ>\<^isub>f f"
1.1483 -
1.1484 -lemma finfun_snd_const: "finfun_snd (\<lambda>\<^isup>f bc) = (\<lambda>\<^isup>f snd bc)"
1.1485 -by(simp add: finfun_snd_def)
1.1486 -
1.1487 -lemma finfun_snd_update: "finfun_snd (f(\<^sup>f a := bc)) = (finfun_snd f)(\<^sup>f a := snd bc)"
1.1488 - and finfun_snd_update_code [code]: "finfun_snd (finfun_update_code f a bc) = (finfun_snd f)(\<^sup>f a := snd bc)"
1.1489 -by(simp_all add: finfun_snd_def)
1.1490 -
1.1491 -lemma finfun_snd_comp_conv: "finfun_snd (f \<circ>\<^isub>f g) = (snd \<circ> f) \<circ>\<^isub>f g"
1.1492 -by(simp add: finfun_snd_def)
1.1493 -
1.1494 -lemma finfun_snd_conv [simp]: "finfun_snd (f, g)\<^sup>f = g"
1.1495 -apply(induct f rule: finfun_weak_induct)
1.1496 -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)
1.1497 -done
1.1498 -
1.1499 -lemma finfun_snd_conv_Abs_finfun: "finfun_snd = (\<lambda>f. Abs_finfun (snd o Rep_finfun f))"
1.1500 -by(simp add: finfun_snd_def_raw finfun_comp_conv_comp finfun_apply_Rep_finfun)
1.1501 -
1.1502 -lemma finfun_Diag_collapse [simp]: "(finfun_fst f, finfun_snd f)\<^sup>f = f"
1.1503 -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)
1.1504 -
1.1505 -subsection {* Currying for FinFuns *}
1.1506 -
1.1507 -definition finfun_curry :: "('a \<times> 'b) \<Rightarrow>\<^isub>f 'c \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'b \<Rightarrow>\<^isub>f 'c"
1.1508 -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)))"
1.1509 -
1.1510 -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))"
1.1511 -apply(unfold_locales)
1.1512 -apply(auto simp add: split_def finfun_update_twist finfun_upd_apply split_paired_all finfun_update_const_same)
1.1513 -done
1.1514 -
1.1515 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.1516 -
1.1517 -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))"
1.1518 -proof(unfold_locales)
1.1519 - fix b' b :: 'b
1.1520 - assume fin: "finite (UNIV :: ('c \<times> 'a) set)"
1.1521 - hence fin1: "finite (UNIV :: 'c set)" and fin2: "finite (UNIV :: 'a set)"
1.1522 - unfolding UNIV_Times_UNIV[symmetric]
1.1523 - by(fastsimp dest: finite_cartesian_productD1 finite_cartesian_productD2)+
1.1524 - note [simp] = Abs_finfun_inverse_finite[OF fin] Abs_finfun_inverse_finite[OF fin1] Abs_finfun_inverse_finite[OF fin2]
1.1525 - { fix A :: "('c \<times> 'a) set"
1.1526 - 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'"
1.1527 - by(rule finfun_curry_aux.upd_left_comm)
1.1528 - from fin have "finite A" by(auto intro: finite_subset)
1.1529 - 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))"
1.1530 - 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) }
1.1531 - from this[of UNIV]
1.1532 - 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'"
1.1533 - by(simp add: finfun_const_def)
1.1534 -qed
1.1535 -
1.1536 -declare finfun_simp [simp del] finfun_iff [iff del] finfun_intro [rule del]
1.1537 -
1.1538 -lemma finfun_curry_const [simp, code]: "finfun_curry (\<lambda>\<^isup>f c) = (\<lambda>\<^isup>f \<lambda>\<^isup>f c)"
1.1539 -by(simp add: finfun_curry_def)
1.1540 -
1.1541 -lemma finfun_curry_update [simp]:
1.1542 - "finfun_curry (f(\<^sup>f (a, b) := c)) = (finfun_curry f)(\<^sup>f a := ((finfun_curry f)\<^sub>f a)(\<^sup>f b := c))"
1.1543 - and finfun_curry_update_code [code]:
1.1544 - "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))"
1.1545 -by(simp_all add: finfun_curry_def)
1.1546 -
1.1547 -declare finfun_simp [simp] finfun_iff [iff] finfun_intro [intro]
1.1548 -
1.1549 -lemma finfun_Abs_finfun_curry: assumes fin: "f \<in> finfun"
1.1550 - shows "(\<lambda>a. Abs_finfun (curry f a)) \<in> finfun"
1.1551 -proof -
1.1552 - from fin obtain c where c: "finite {ab. f ab \<noteq> c}" unfolding finfun_def by blast
1.1553 - have "{a. \<exists>b. f (a, b) \<noteq> c} = fst ` {ab. f ab \<noteq> c}" by(force)
1.1554 - hence "{a. curry f a \<noteq> (\<lambda>x. c)} = fst ` {ab. f ab \<noteq> c}"
1.1555 - by(auto simp add: curry_def expand_fun_eq)
1.1556 - with fin c have "finite {a. Abs_finfun (curry f a) \<noteq> (\<lambda>\<^isup>f c)}"
1.1557 - by(simp add: finfun_const_def finfun_curry)
1.1558 - thus ?thesis unfolding finfun_def by auto
1.1559 -qed
1.1560 -
1.1561 -lemma finfun_curry_conv_curry:
1.1562 - fixes f :: "('a \<times> 'b) \<Rightarrow>\<^isub>f 'c"
1.1563 - shows "finfun_curry f = Abs_finfun (\<lambda>a. Abs_finfun (curry (Rep_finfun f) a))"
1.1564 -proof -
1.1565 - have "finfun_curry = (\<lambda>f :: ('a \<times> 'b) \<Rightarrow>\<^isub>f 'c. Abs_finfun (\<lambda>a. Abs_finfun (curry (Rep_finfun f) a)))"
1.1566 - proof(rule finfun_rec_unique)
1.1567 - { fix c show "finfun_curry (\<lambda>\<^isup>f c) = (\<lambda>\<^isup>f \<lambda>\<^isup>f c)" by simp }
1.1568 - { 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))"
1.1569 - by(cases a) simp }
1.1570 - { fix c show "Abs_finfun (\<lambda>a. Abs_finfun (curry (Rep_finfun (\<lambda>\<^isup>f c)) a)) = (\<lambda>\<^isup>f \<lambda>\<^isup>f c)"
1.1571 - by(simp add: finfun_curry_def finfun_const_def curry_def) }
1.1572 - { fix g a b
1.1573 - show "Abs_finfun (\<lambda>aa. Abs_finfun (curry (Rep_finfun g(\<^sup>f a := b)) aa)) =
1.1574 - (Abs_finfun (\<lambda>a. Abs_finfun (curry (Rep_finfun g) a)))(\<^sup>f
1.1575 - fst a := ((Abs_finfun (\<lambda>a. Abs_finfun (curry (Rep_finfun g) a)))\<^sub>f (fst a))(\<^sup>f snd a := b))"
1.1576 - 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) }
1.1577 - qed
1.1578 - thus ?thesis by(auto simp add: expand_fun_eq)
1.1579 -qed
1.1580 -
1.1581 -subsection {* Executable equality for FinFuns *}
1.1582 -
1.1583 -lemma eq_finfun_All_ext: "(f = g) \<longleftrightarrow> finfun_All ((\<lambda>(x, y). x = y) \<circ>\<^isub>f (f, g)\<^sup>f)"
1.1584 -by(simp add: expand_finfun_eq expand_fun_eq finfun_All_All o_def)
1.1585 -
1.1586 -instantiation finfun :: ("{card_UNIV,eq}",eq) eq begin
1.1587 -definition eq_finfun_def: "eq_class.eq f g \<longleftrightarrow> finfun_All ((\<lambda>(x, y). x = y) \<circ>\<^isub>f (f, g)\<^sup>f)"
1.1588 -instance by(intro_classes)(simp add: eq_finfun_All_ext eq_finfun_def)
1.1589 -end
1.1590 -
1.1591 -subsection {* Operator that explicitly removes all redundant updates in the generated representations *}
1.1592 -
1.1593 -definition finfun_clearjunk :: "'a \<Rightarrow>\<^isub>f 'b \<Rightarrow> 'a \<Rightarrow>\<^isub>f 'b"
1.1594 -where [simp, code del]: "finfun_clearjunk = id"
1.1595 -
1.1596 -lemma finfun_clearjunk_const [code]: "finfun_clearjunk (\<lambda>\<^isup>f b) = (\<lambda>\<^isup>f b)"
1.1597 -by simp
1.1598 -
1.1599 -lemma finfun_clearjunk_update [code]: "finfun_clearjunk (finfun_update_code f a b) = f(\<^sup>f a := b)"
1.1600 -by simp
1.1601 -
1.1602 -end
1.1603 \ No newline at end of file
2.1 --- a/src/HOL/Library/Library.thy Mon Oct 26 09:03:57 2009 +0100
2.2 +++ b/src/HOL/Library/Library.thy Mon Oct 26 11:19:24 2009 +0100
2.3 @@ -20,7 +20,6 @@
2.4 Enum
2.5 Eval_Witness
2.6 Executable_Set
2.7 - Fin_Fun
2.8 Float
2.9 Formal_Power_Series
2.10 Fraction_Field