Theory Hilbert_Choice

theory Hilbert_Choice
imports Wellfounded
(*  Title:      HOL/Hilbert_Choice.thy
    Author:     Lawrence C Paulson, Tobias Nipkow
    Copyright   2001  University of Cambridge

section ‹Hilbert's Epsilon-Operator and the Axiom of Choice›

theory Hilbert_Choice
  imports Wellfounded
  keywords "specification" :: thy_goal

subsection ‹Hilbert's epsilon›

axiomatization Eps :: "('a ⇒ bool) ⇒ 'a"
  where someI: "P x ⟹ P (Eps P)"

syntax (epsilon)
  "_Eps" :: "pttrn ⇒ bool ⇒ 'a"  ("(3ϵ_./ _)" [0, 10] 10)
syntax (input)
  "_Eps" :: "pttrn ⇒ bool ⇒ 'a"  ("(3@ _./ _)" [0, 10] 10)
  "_Eps" :: "pttrn ⇒ bool ⇒ 'a"  ("(3SOME _./ _)" [0, 10] 10)
  "SOME x. P"  "CONST Eps (λx. P)"

print_translation ‹
  [(@{const_syntax Eps}, fn _ => fn [Abs abs] =>
      let val (x, t) = Syntax_Trans.atomic_abs_tr' abs
      in Syntax.const @{syntax_const "_Eps"} $ x $ t end)]
›  ‹to avoid eta-contraction of body›

definition inv_into :: "'a set ⇒ ('a ⇒ 'b) ⇒ ('b ⇒ 'a)" where
"inv_into A f = (λx. SOME y. y ∈ A ∧ f y = x)"

lemma inv_into_def2: "inv_into A f x = (SOME y. y ∈ A ∧ f y = x)"
by(simp add: inv_into_def)

abbreviation inv :: "('a ⇒ 'b) ⇒ ('b ⇒ 'a)" where
"inv ≡ inv_into UNIV"

subsection ‹Hilbert's Epsilon-operator›

text ‹
  Easier to apply than ‹someI› if the witness comes from an
  existential formula.
lemma someI_ex [elim?]: "∃x. P x ⟹ P (SOME x. P x)"
  apply (erule exE)
  apply (erule someI)

text ‹
  Easier to apply than ‹someI› because the conclusion has only one
  occurrence of @{term P}.
lemma someI2: "P a ⟹ (⋀x. P x ⟹ Q x) ⟹ Q (SOME x. P x)"
  by (blast intro: someI)

text ‹
  Easier to apply than ‹someI2› if the witness comes from an
  existential formula.
lemma someI2_ex: "∃a. P a ⟹ (⋀x. P x ⟹ Q x) ⟹ Q (SOME x. P x)"
  by (blast intro: someI2)

lemma someI2_bex: "∃a∈A. P a ⟹ (⋀x. x ∈ A ∧ P x ⟹ Q x) ⟹ Q (SOME x. x ∈ A ∧ P x)"
  by (blast intro: someI2)

lemma some_equality [intro]: "P a ⟹ (⋀x. P x ⟹ x = a) ⟹ (SOME x. P x) = a"
  by (blast intro: someI2)

lemma some1_equality: "∃!x. P x ⟹ P a ⟹ (SOME x. P x) = a"
  by blast

lemma some_eq_ex: "P (SOME x. P x) ⟷ (∃x. P x)"
  by (blast intro: someI)

lemma some_in_eq: "(SOME x. x ∈ A) ∈ A ⟷ A ≠ {}"
  unfolding ex_in_conv[symmetric] by (rule some_eq_ex)

lemma some_eq_trivial [simp]: "(SOME y. y = x) = x"
  by (rule some_equality) (rule refl)

lemma some_sym_eq_trivial [simp]: "(SOME y. x = y) = x"
  apply (rule some_equality)
   apply (rule refl)
  apply (erule sym)

subsection ‹Axiom of Choice, Proved Using the Description Operator›

lemma choice: "∀x. ∃y. Q x y ⟹ ∃f. ∀x. Q x (f x)"
  by (fast elim: someI)

lemma bchoice: "∀x∈S. ∃y. Q x y ⟹ ∃f. ∀x∈S. Q x (f x)"
  by (fast elim: someI)

lemma choice_iff: "(∀x. ∃y. Q x y) ⟷ (∃f. ∀x. Q x (f x))"
  by (fast elim: someI)

lemma choice_iff': "(∀x. P x ⟶ (∃y. Q x y)) ⟷ (∃f. ∀x. P x ⟶ Q x (f x))"
  by (fast elim: someI)

lemma bchoice_iff: "(∀x∈S. ∃y. Q x y) ⟷ (∃f. ∀x∈S. Q x (f x))"
  by (fast elim: someI)

lemma bchoice_iff': "(∀x∈S. P x ⟶ (∃y. Q x y)) ⟷ (∃f. ∀x∈S. P x ⟶ Q x (f x))"
  by (fast elim: someI)

lemma dependent_nat_choice:
  assumes 1: "∃x. P 0 x"
    and 2: "⋀x n. P n x ⟹ ∃y. P (Suc n) y ∧ Q n x y"
  shows "∃f. ∀n. P n (f n) ∧ Q n (f n) (f (Suc n))"
proof (intro exI allI conjI)
  fix n
  define f where "f = rec_nat (SOME x. P 0 x) (λn x. SOME y. P (Suc n) y ∧ Q n x y)"
  then have "P 0 (f 0)" "⋀n. P n (f n) ⟹ P (Suc n) (f (Suc n)) ∧ Q n (f n) (f (Suc n))"
    using someI_ex[OF 1] someI_ex[OF 2] by simp_all
  then show "P n (f n)" "Q n (f n) (f (Suc n))"
    by (induct n) auto

subsection ‹Function Inverse›

lemma inv_def: "inv f = (λy. SOME x. f x = y)"
  by (simp add: inv_into_def)

lemma inv_into_into: "x ∈ f ` A ⟹ inv_into A f x ∈ A"
  by (simp add: inv_into_def) (fast intro: someI2)

lemma inv_identity [simp]: "inv (λa. a) = (λa. a)"
  by (simp add: inv_def)

lemma inv_id [simp]: "inv id = id"
  by (simp add: id_def)

lemma inv_into_f_f [simp]: "inj_on f A ⟹ x ∈ A ⟹ inv_into A f (f x) = x"
  by (simp add: inv_into_def inj_on_def) (blast intro: someI2)

lemma inv_f_f: "inj f ⟹ inv f (f x) = x"
  by simp

lemma f_inv_into_f: "y : f`A ⟹ f (inv_into A f y) = y"
  by (simp add: inv_into_def) (fast intro: someI2)

lemma inv_into_f_eq: "inj_on f A ⟹ x ∈ A ⟹ f x = y ⟹ inv_into A f y = x"
  by (erule subst) (fast intro: inv_into_f_f)

lemma inv_f_eq: "inj f ⟹ f x = y ⟹ inv f y = x"
  by (simp add:inv_into_f_eq)

lemma inj_imp_inv_eq: "inj f ⟹ ∀x. f (g x) = x ⟹ inv f = g"
  by (blast intro: inv_into_f_eq)

text ‹But is it useful?›
lemma inj_transfer:
  assumes inj: "inj f"
    and minor: "⋀y. y ∈ range f ⟹ P (inv f y)"
  shows "P x"
proof -
  have "f x ∈ range f" by auto
  then have "P(inv f (f x))" by (rule minor)
  then show "P x" by (simp add: inv_into_f_f [OF inj])

lemma inj_iff: "inj f ⟷ inv f ∘ f = id"
  by (simp add: o_def fun_eq_iff) (blast intro: inj_on_inverseI inv_into_f_f)

lemma inv_o_cancel[simp]: "inj f ⟹ inv f ∘ f = id"
  by (simp add: inj_iff)

lemma o_inv_o_cancel[simp]: "inj f ⟹ g ∘ inv f ∘ f = g"
  by (simp add: comp_assoc)

lemma inv_into_image_cancel[simp]: "inj_on f A ⟹ S ⊆ A ⟹ inv_into A f ` f ` S = S"
  by (fastforce simp: image_def)

lemma inj_imp_surj_inv: "inj f ⟹ surj (inv f)"
  by (blast intro!: surjI inv_into_f_f)

lemma surj_f_inv_f: "surj f ⟹ f (inv f y) = y"
  by (simp add: f_inv_into_f)

lemma inv_into_injective:
  assumes eq: "inv_into A f x = inv_into A f y"
    and x: "x ∈ f`A"
    and y: "y ∈ f`A"
  shows "x = y"
proof -
  from eq have "f (inv_into A f x) = f (inv_into A f y)"
    by simp
  with x y show ?thesis
    by (simp add: f_inv_into_f)

lemma inj_on_inv_into: "B ⊆ f`A ⟹ inj_on (inv_into A f) B"
  by (blast intro: inj_onI dest: inv_into_injective injD)

lemma bij_betw_inv_into: "bij_betw f A B ⟹ bij_betw (inv_into A f) B A"
  by (auto simp add: bij_betw_def inj_on_inv_into)

lemma surj_imp_inj_inv: "surj f ⟹ inj (inv f)"
  by (simp add: inj_on_inv_into)

lemma surj_iff: "surj f ⟷ f ∘ inv f = id"
  by (auto intro!: surjI simp: surj_f_inv_f fun_eq_iff[where 'b='a])

lemma surj_iff_all: "surj f ⟷ (∀x. f (inv f x) = x)"
  by (simp add: o_def surj_iff fun_eq_iff)

lemma surj_imp_inv_eq: "surj f ⟹ ∀x. g (f x) = x ⟹ inv f = g"
  apply (rule ext)
  apply (drule_tac x = "inv f x" in spec)
  apply (simp add: surj_f_inv_f)

lemma bij_imp_bij_inv: "bij f ⟹ bij (inv f)"
  by (simp add: bij_def inj_imp_surj_inv surj_imp_inj_inv)

lemma inv_equality: "(⋀x. g (f x) = x) ⟹ (⋀y. f (g y) = y) ⟹ inv f = g"
  by (rule ext) (auto simp add: inv_into_def)

lemma inv_inv_eq: "bij f ⟹ inv (inv f) = f"
  by (rule inv_equality) (auto simp add: bij_def surj_f_inv_f)

text ‹
  ‹bij (inv f)› implies little about ‹f›. Consider ‹f :: bool ⇒ bool› such
  that ‹f True = f False = True›. Then it ia consistent with axiom ‹someI›
  that ‹inv f› could be any function at all, including the identity function.
  If ‹inv f = id› then ‹inv f› is a bijection, but ‹inj f›, ‹surj f› and ‹inv
  (inv f) = f› all fail.

lemma inv_into_comp:
  "inj_on f (g ` A) ⟹ inj_on g A ⟹ x ∈ f ` g ` A ⟹
    inv_into A (f ∘ g) x = (inv_into A g ∘ inv_into (g ` A) f) x"
  apply (rule inv_into_f_eq)
    apply (fast intro: comp_inj_on)
   apply (simp add: inv_into_into)
  apply (simp add: f_inv_into_f inv_into_into)

lemma o_inv_distrib: "bij f ⟹ bij g ⟹ inv (f ∘ g) = inv g ∘ inv f"
  by (rule inv_equality) (auto simp add: bij_def surj_f_inv_f)

lemma image_f_inv_f: "surj f ⟹ f ` (inv f ` A) = A"
  by (simp add: surj_f_inv_f image_comp comp_def)

lemma image_inv_f_f: "inj f ⟹ inv f ` (f ` A) = A"
  by simp

lemma bij_image_Collect_eq: "bij f ⟹ f ` Collect P = {y. P (inv f y)}"
  apply auto
   apply (force simp add: bij_is_inj)
  apply (blast intro: bij_is_surj [THEN surj_f_inv_f, symmetric])

lemma bij_vimage_eq_inv_image: "bij f ⟹ f -` A = inv f ` A"
  apply (auto simp add: bij_is_surj [THEN surj_f_inv_f])
  apply (blast intro: bij_is_inj [THEN inv_into_f_f, symmetric])

lemma finite_fun_UNIVD1:
  assumes fin: "finite (UNIV :: ('a ⇒ 'b) set)"
    and card: "card (UNIV :: 'b set) ≠ Suc 0"
  shows "finite (UNIV :: 'a set)"
proof -
  let ?UNIV_b = "UNIV :: 'b set"
  from fin have "finite ?UNIV_b"
    by (rule finite_fun_UNIVD2)
  with card have "card ?UNIV_b ≥ Suc (Suc 0)"
    by (cases "card ?UNIV_b") (auto simp: card_eq_0_iff)
  then have "card ?UNIV_b = Suc (Suc (card ?UNIV_b - Suc (Suc 0)))"
    by simp
  then obtain b1 b2 :: 'b where b1b2: "b1 ≠ b2"
    by (auto simp: card_Suc_eq)
  from fin have fin': "finite (range (λf :: 'a ⇒ 'b. inv f b1))"
    by (rule finite_imageI)
  have "UNIV = range (λf :: 'a ⇒ 'b. inv f b1)"
  proof (rule UNIV_eq_I)
    fix x :: 'a
    from b1b2 have "x = inv (λy. if y = x then b1 else b2) b1"
      by (simp add: inv_into_def)
    then show "x ∈ range (λf::'a ⇒ 'b. inv f b1)"
      by blast
  with fin' show ?thesis
    by simp

text ‹
  Every infinite set contains a countable subset. More precisely we
  show that a set ‹S› is infinite if and only if there exists an
  injective function from the naturals into ‹S›.

  The ``only if'' direction is harder because it requires the
  construction of a sequence of pairwise different elements of an
  infinite set ‹S›. The idea is to construct a sequence of
  non-empty and infinite subsets of ‹S› obtained by successively
  removing elements of ‹S›.

lemma infinite_countable_subset:
  assumes inf: "¬ finite S"
  shows "∃f::nat ⇒ 'a. inj f ∧ range f ⊆ S"
   ‹Courtesy of Stephan Merz›
proof -
  define Sseq where "Sseq = rec_nat S (λn T. T - {SOME e. e ∈ T})"
  define pick where "pick n = (SOME e. e ∈ Sseq n)" for n
  have *: "Sseq n ⊆ S" "¬ finite (Sseq n)" for n
    by (induct n) (auto simp: Sseq_def inf)
  then have **: "⋀n. pick n ∈ Sseq n"
    unfolding pick_def by (subst (asm) finite.simps) (auto simp add: ex_in_conv intro: someI_ex)
  with * have "range pick ⊆ S" by auto
  moreover have "pick n ≠ pick (n + Suc m)" for m n
  proof -
    have "pick n ∉ Sseq (n + Suc m)"
      by (induct m) (auto simp add: Sseq_def pick_def)
    with ** show ?thesis by auto
  then have "inj pick"
    by (intro linorder_injI) (auto simp add: less_iff_Suc_add)
  ultimately show ?thesis by blast

lemma infinite_iff_countable_subset: "¬ finite S ⟷ (∃f::nat ⇒ 'a. inj f ∧ range f ⊆ S)"
   ‹Courtesy of Stephan Merz›
  using finite_imageD finite_subset infinite_UNIV_char_0 infinite_countable_subset by auto

lemma image_inv_into_cancel:
  assumes surj: "f`A = A'"
    and sub: "B' ⊆ A'"
  shows "f `((inv_into A f)`B') = B'"
  using assms
proof (auto simp: f_inv_into_f)
  let ?f' = "inv_into A f"
  fix a'
  assume *: "a' ∈ B'"
  with sub have "a' ∈ A'" by auto
  with surj have "a' = f (?f' a')"
    by (auto simp: f_inv_into_f)
  with * show "a' ∈ f ` (?f' ` B')" by blast

lemma inv_into_inv_into_eq:
  assumes "bij_betw f A A'"
    and a: "a ∈ A"
  shows "inv_into A' (inv_into A f) a = f a"
proof -
  let ?f' = "inv_into A f"
  let ?f'' = "inv_into A' ?f'"
  from assms have *: "bij_betw ?f' A' A"
    by (auto simp: bij_betw_inv_into)
  with a obtain a' where a': "a' ∈ A'" "?f' a' = a"
    unfolding bij_betw_def by force
  with a * have "?f'' a = a'"
    by (auto simp: f_inv_into_f bij_betw_def)
  moreover from assms a' have "f a = a'"
    by (auto simp: bij_betw_def)
  ultimately show "?f'' a = f a" by simp

lemma inj_on_iff_surj:
  assumes "A ≠ {}"
  shows "(∃f. inj_on f A ∧ f ` A ⊆ A') ⟷ (∃g. g ` A' = A)"
proof safe
  fix f
  assume inj: "inj_on f A" and incl: "f ` A ⊆ A'"
  let ?phi = "λa' a. a ∈ A ∧ f a = a'"
  let ?csi = "λa. a ∈ A"
  let ?g = "λa'. if a' ∈ f ` A then (SOME a. ?phi a' a) else (SOME a. ?csi a)"
  have "?g ` A' = A"
    show "?g ` A' ⊆ A"
    proof clarify
      fix a'
      assume *: "a' ∈ A'"
      show "?g a' ∈ A"
      proof (cases "a' ∈ f ` A")
        case True
        then obtain a where "?phi a' a" by blast
        then have "?phi a' (SOME a. ?phi a' a)"
          using someI[of "?phi a'" a] by blast
        with True show ?thesis by auto
        case False
        with assms have "?csi (SOME a. ?csi a)"
          using someI_ex[of ?csi] by blast
        with False show ?thesis by auto
    show "A ⊆ ?g ` A'"
    proof -
      have "?g (f a) = a ∧ f a ∈ A'" if a: "a ∈ A" for a
      proof -
        let ?b = "SOME aa. ?phi (f a) aa"
        from a have "?phi (f a) a" by auto
        then have *: "?phi (f a) ?b"
          using someI[of "?phi(f a)" a] by blast
        then have "?g (f a) = ?b" using a by auto
        moreover from inj * a have "a = ?b"
          by (auto simp add: inj_on_def)
        ultimately have "?g(f a) = a" by simp
        with incl a show ?thesis by auto
      then show ?thesis by force
  then show "∃g. g ` A' = A" by blast
  fix g
  let ?f = "inv_into A' g"
  have "inj_on ?f (g ` A')"
    by (auto simp: inj_on_inv_into)
  moreover have "?f (g a') ∈ A'" if a': "a' ∈ A'" for a'
  proof -
    let ?phi = "λ b'. b' ∈ A' ∧ g b' = g a'"
    from a' have "?phi a'" by auto
    then have "?phi (SOME b'. ?phi b')"
      using someI[of ?phi] by blast
    then show ?thesis by (auto simp: inv_into_def)
  ultimately show "∃f. inj_on f (g ` A') ∧ f ` g ` A' ⊆ A'"
    by auto

lemma Ex_inj_on_UNION_Sigma:
  "∃f. (inj_on f (⋃i ∈ I. A i) ∧ f ` (⋃i ∈ I. A i) ⊆ (SIGMA i : I. A i))"
  let ?phi = "λa i. i ∈ I ∧ a ∈ A i"
  let ?sm = "λa. SOME i. ?phi a i"
  let ?f = "λa. (?sm a, a)"
  have "inj_on ?f (⋃i ∈ I. A i)"
    by (auto simp: inj_on_def)
  have "?sm a ∈ I ∧ a ∈ A(?sm a)" if "i ∈ I" and "a ∈ A i" for i a
    using that someI[of "?phi a" i] by auto
  then have "?f ` (⋃i ∈ I. A i) ⊆ (SIGMA i : I. A i)"
    by auto
  ultimately show "inj_on ?f (⋃i ∈ I. A i) ∧ ?f ` (⋃i ∈ I. A i) ⊆ (SIGMA i : I. A i)"
    by auto

lemma inv_unique_comp:
  assumes fg: "f ∘ g = id"
    and gf: "g ∘ f = id"
  shows "inv f = g"
  using fg gf inv_equality[of g f] by (auto simp add: fun_eq_iff)

subsection ‹Other Consequences of Hilbert's Epsilon›

text ‹Hilbert's Epsilon and the @{term split} Operator›

text ‹Looping simprule!›
lemma split_paired_Eps: "(SOME x. P x) = (SOME (a, b). P (a, b))"
  by simp

lemma Eps_case_prod: "Eps (case_prod P) = (SOME xy. P (fst xy) (snd xy))"
  by (simp add: split_def)

lemma Eps_case_prod_eq [simp]: "(SOME (x', y'). x = x' ∧ y = y') = (x, y)"
  by blast

text ‹A relation is wellfounded iff it has no infinite descending chain.›
lemma wf_iff_no_infinite_down_chain: "wf r ⟷ (∄f. ∀i. (f (Suc i), f i) ∈ r)"
  (is "_ ⟷ ¬ ?ex")
  assume "wf r"
  show "¬ ?ex"
    assume ?ex
    then obtain f where f: "(f (Suc i), f i) ∈ r" for i
      by blast
    from ‹wf r› have minimal: "x ∈ Q ⟹ ∃z∈Q. ∀y. (y, z) ∈ r ⟶ y ∉ Q" for x Q
      by (auto simp: wf_eq_minimal)
    let ?Q = "{w. ∃i. w = f i}"
    fix n
    have "f n ∈ ?Q" by blast
    from minimal [OF this] obtain j where "(y, f j) ∈ r ⟹ y ∉ ?Q" for y by blast
    with this [OF ‹(f (Suc j), f j) ∈ r›] have "f (Suc j) ∉ ?Q" by simp
    then show False by blast
  assume "¬ ?ex"
  then show "wf r"
  proof (rule contrapos_np)
    assume "¬ wf r"
    then obtain Q x where x: "x ∈ Q" and rec: "z ∈ Q ⟹ ∃y. (y, z) ∈ r ∧ y ∈ Q" for z
      by (auto simp add: wf_eq_minimal)
    obtain descend :: "nat ⇒ 'a"
      where descend_0: "descend 0 = x"
        and descend_Suc: "descend (Suc n) = (SOME y. y ∈ Q ∧ (y, descend n) ∈ r)" for n
      by (rule that [of "rec_nat x (λ_ rec. (SOME y. y ∈ Q ∧ (y, rec) ∈ r))"]) simp_all
    have descend_Q: "descend n ∈ Q" for n
    proof (induct n)
      case 0
      with x show ?case by (simp only: descend_0)
      case Suc
      then show ?case by (simp only: descend_Suc) (rule someI2_ex; use rec in blast)
    have "(descend (Suc i), descend i) ∈ r" for i
      by (simp only: descend_Suc) (rule someI2_ex; use descend_Q rec in blast)
    then show "∃f. ∀i. (f (Suc i), f i) ∈ r" by blast

lemma wf_no_infinite_down_chainE:
  assumes "wf r"
  obtains k where "(f (Suc k), f k) ∉ r"
  using assms wf_iff_no_infinite_down_chain[of r] by blast

text ‹A dynamically-scoped fact for TFL›
lemma tfl_some: "∀P x. P x ⟶ P (Eps P)"
  by (blast intro: someI)

subsection ‹An aside: bounded accessible part›

text ‹Finite monotone eventually stable sequences›

lemma finite_mono_remains_stable_implies_strict_prefix:
  fixes f :: "nat ⇒ 'a::order"
  assumes S: "finite (range f)" "mono f"
    and eq: "∀n. f n = f (Suc n) ⟶ f (Suc n) = f (Suc (Suc n))"
  shows "∃N. (∀n≤N. ∀m≤N. m < n ⟶ f m < f n) ∧ (∀n≥N. f N = f n)"
  using assms
proof -
  have "∃n. f n = f (Suc n)"
  proof (rule ccontr)
    assume "¬ ?thesis"
    then have "⋀n. f n ≠ f (Suc n)" by auto
    with ‹mono f› have "⋀n. f n < f (Suc n)"
      by (auto simp: le_less mono_iff_le_Suc)
    with lift_Suc_mono_less_iff[of f] have *: "⋀n m. n < m ⟹ f n < f m"
      by auto
    have "inj f"
    proof (intro injI)
      fix x y
      assume "f x = f y"
      then show "x = y"
        by (cases x y rule: linorder_cases) (auto dest: *)
    with ‹finite (range f)› have "finite (UNIV::nat set)"
      by (rule finite_imageD)
    then show False by simp
  then obtain n where n: "f n = f (Suc n)" ..
  define N where "N = (LEAST n. f n = f (Suc n))"
  have N: "f N = f (Suc N)"
    unfolding N_def using n by (rule LeastI)
  show ?thesis
  proof (intro exI[of _ N] conjI allI impI)
    fix n
    assume "N ≤ n"
    then have "⋀m. N ≤ m ⟹ m ≤ n ⟹ f m = f N"
    proof (induct rule: dec_induct)
      case base
      then show ?case by simp
      case (step n)
      then show ?case
        using eq [rule_format, of "n - 1"] N
        by (cases n) (auto simp add: le_Suc_eq)
    from this[of n] ‹N ≤ n› show "f N = f n" by auto
    fix n m :: nat
    assume "m < n" "n ≤ N"
    then show "f m < f n"
    proof (induct rule: less_Suc_induct)
      case (1 i)
      then have "i < N" by simp
      then have "f i ≠ f (Suc i)"
        unfolding N_def by (rule not_less_Least)
      with ‹mono f› show ?case by (simp add: mono_iff_le_Suc less_le)
      case 2
      then show ?case by simp

lemma finite_mono_strict_prefix_implies_finite_fixpoint:
  fixes f :: "nat ⇒ 'a set"
  assumes S: "⋀i. f i ⊆ S" "finite S"
    and ex: "∃N. (∀n≤N. ∀m≤N. m < n ⟶ f m ⊂ f n) ∧ (∀n≥N. f N = f n)"
  shows "f (card S) = (⋃n. f n)"
proof -
  from ex obtain N where inj: "⋀n m. n ≤ N ⟹ m ≤ N ⟹ m < n ⟹ f m ⊂ f n"
    and eq: "∀n≥N. f N = f n"
    by atomize auto
  have "i ≤ N ⟹ i ≤ card (f i)" for i
  proof (induct i)
    case 0
    then show ?case by simp
    case (Suc i)
    with inj [of "Suc i" i] have "(f i) ⊂ (f (Suc i))" by auto
    moreover have "finite (f (Suc i))" using S by (rule finite_subset)
    ultimately have "card (f i) < card (f (Suc i))" by (intro psubset_card_mono)
    with Suc inj show ?case by auto
  then have "N ≤ card (f N)" by simp
  also have "… ≤ card S" using S by (intro card_mono)
  finally have "f (card S) = f N" using eq by auto
  then show ?thesis
    using eq inj [of N]
    apply auto
    apply (case_tac "n < N")
     apply (auto simp: not_less)

subsection ‹More on injections, bijections, and inverses›

locale bijection =
  fixes f :: "'a ⇒ 'a"
  assumes bij: "bij f"

lemma bij_inv: "bij (inv f)"
  using bij by (rule bij_imp_bij_inv)

lemma surj [simp]: "surj f"
  using bij by (rule bij_is_surj)

lemma inj: "inj f"
  using bij by (rule bij_is_inj)

lemma surj_inv [simp]: "surj (inv f)"
  using inj by (rule inj_imp_surj_inv)

lemma inj_inv: "inj (inv f)"
  using surj by (rule surj_imp_inj_inv)

lemma eqI: "f a = f b ⟹ a = b"
  using inj by (rule injD)

lemma eq_iff [simp]: "f a = f b ⟷ a = b"
  by (auto intro: eqI)

lemma eq_invI: "inv f a = inv f b ⟹ a = b"
  using inj_inv by (rule injD)

lemma eq_inv_iff [simp]: "inv f a = inv f b ⟷ a = b"
  by (auto intro: eq_invI)

lemma inv_left [simp]: "inv f (f a) = a"
  using inj by (simp add: inv_f_eq)

lemma inv_comp_left [simp]: "inv f ∘ f = id"
  by (simp add: fun_eq_iff)

lemma inv_right [simp]: "f (inv f a) = a"
  using surj by (simp add: surj_f_inv_f)

lemma inv_comp_right [simp]: "f ∘ inv f = id"
  by (simp add: fun_eq_iff)

lemma inv_left_eq_iff [simp]: "inv f a = b ⟷ f b = a"
  by auto

lemma inv_right_eq_iff [simp]: "b = inv f a ⟷ f b = a"
  by auto


lemma infinite_imp_bij_betw:
  assumes infinite: "¬ finite A"
  shows "∃h. bij_betw h A (A - {a})"
proof (cases "a ∈ A")
  case False
  then have "A - {a} = A" by blast
  then show ?thesis
    using bij_betw_id[of A] by auto
  case True
  with infinite have "¬ finite (A - {a})" by auto
  with infinite_iff_countable_subset[of "A - {a}"]
  obtain f :: "nat ⇒ 'a" where 1: "inj f" and 2: "f ` UNIV ⊆ A - {a}" by blast
  define g where "g n = (if n = 0 then a else f (Suc n))" for n
  define A' where "A' = g ` UNIV"
  have *: "∀y. f y ≠ a" using 2 by blast
  have 3: "inj_on g UNIV ∧ g ` UNIV ⊆ A ∧ a ∈ g ` UNIV"
    apply (auto simp add: True g_def [abs_def])
     apply (unfold inj_on_def)
     apply (intro ballI impI)
     apply (case_tac "x = 0")
      apply (auto simp add: 2)
  proof -
    fix y
    assume "a = (if y = 0 then a else f (Suc y))"
    then show "y = 0" by (cases "y = 0") (use * in auto)
    fix x y
    assume "f (Suc x) = (if y = 0 then a else f (Suc y))"
    with 1 * show "x = y" by (cases "y = 0") (auto simp: inj_on_def)
    fix n
    from 2 show "f (Suc n) ∈ A" by blast
  then have 4: "bij_betw g UNIV A' ∧ a ∈ A' ∧ A' ⊆ A"
    using inj_on_imp_bij_betw[of g] by (auto simp: A'_def)
  then have 5: "bij_betw (inv g) A' UNIV"
    by (auto simp add: bij_betw_inv_into)
  from 3 obtain n where n: "g n = a" by auto
  have 6: "bij_betw g (UNIV - {n}) (A' - {a})"
    by (rule bij_betw_subset) (use 3 4 n in ‹auto simp: image_set_diff A'_def›)
  define v where "v m = (if m < n then m else Suc m)" for m
  have 7: "bij_betw v UNIV (UNIV - {n})"
  proof (unfold bij_betw_def inj_on_def, intro conjI, clarify)
    fix m1 m2
    assume "v m1 = v m2"
    then show "m1 = m2"
      apply (cases "m1 < n")
       apply (cases "m2 < n")
        apply (auto simp: inj_on_def v_def [abs_def])
      apply (cases "m2 < n")
       apply auto
    show "v ` UNIV = UNIV - {n}"
    proof (auto simp: v_def [abs_def])
      fix m
      assume "m ≠ n"
      assume *: "m ∉ Suc ` {m'. ¬ m' < n}"
      have False if "n ≤ m"
      proof -
        from ‹m ≠ n› that have **: "Suc n ≤ m" by auto
        from Suc_le_D [OF this] obtain m' where m': "m = Suc m'" ..
        with ** have "n ≤ m'" by auto
        with m' * show ?thesis by auto
      then show "m < n" by force
  define h' where "h' = g ∘ v ∘ (inv g)"
  with 5 6 7 have 8: "bij_betw h' A' (A' - {a})"
    by (auto simp add: bij_betw_trans)
  define h where "h b = (if b ∈ A' then h' b else b)" for b
  then have "∀b ∈ A'. h b = h' b" by simp
  with 8 have "bij_betw h  A' (A' - {a})"
    using bij_betw_cong[of A' h] by auto
  have "∀b ∈ A - A'. h b = b" by (auto simp: h_def)
  then have "bij_betw h  (A - A') (A - A')"
    using bij_betw_cong[of "A - A'" h id] bij_betw_id[of "A - A'"] by auto
  from 4 have "(A' ∩ (A - A') = {} ∧ A' ∪ (A - A') = A) ∧
    ((A' - {a}) ∩ (A - A') = {} ∧ (A' - {a}) ∪ (A - A') = A - {a})"
    by blast
  ultimately have "bij_betw h A (A - {a})"
    using bij_betw_combine[of h A' "A' - {a}" "A - A'" "A - A'"] by simp
  then show ?thesis by blast

lemma infinite_imp_bij_betw2:
  assumes "¬ finite A"
  shows "∃h. bij_betw h A (A ∪ {a})"
proof (cases "a ∈ A")
  case True
  then have "A ∪ {a} = A" by blast
  then show ?thesis using bij_betw_id[of A] by auto
  case False
  let ?A' = "A ∪ {a}"
  from False have "A = ?A' - {a}" by blast
  moreover from assms have "¬ finite ?A'" by auto
  ultimately obtain f where "bij_betw f ?A' A"
    using infinite_imp_bij_betw[of ?A' a] by auto
  then have "bij_betw (inv_into ?A' f) A ?A'" by (rule bij_betw_inv_into)
  then show ?thesis by auto

lemma bij_betw_inv_into_left: "bij_betw f A A' ⟹ a ∈ A ⟹ inv_into A f (f a) = a"
  unfolding bij_betw_def by clarify (rule inv_into_f_f)

lemma bij_betw_inv_into_right: "bij_betw f A A' ⟹ a' ∈ A' ⟹ f (inv_into A f a') = a'"
  unfolding bij_betw_def using f_inv_into_f by force

lemma bij_betw_inv_into_subset:
  "bij_betw f A A' ⟹ B ⊆ A ⟹ f ` B = B' ⟹ bij_betw (inv_into A f) B' B"
  by (auto simp: bij_betw_def intro: inj_on_inv_into)

subsection ‹Specification package -- Hilbertized version›

lemma exE_some: "Ex P ⟹ c ≡ Eps P ⟹ P c"
  by (simp only: someI_ex)

ML_file "Tools/choice_specification.ML"