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