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