src/HOL/Combinatorics/Transposition.thy
author haftmann
Wed, 05 May 2021 16:09:02 +0000
changeset 73623 5020054b3a16
child 73648 1bd3463e30b8
permissions -rw-r--r--
tuned theory structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73623
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     1
section \<open>Transposition function\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     2
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     3
theory Transposition
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     4
  imports Main
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     5
begin
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     6
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     7
setup \<open>Context.theory_map (Name_Space.map_naming (Name_Space.qualified_path true \<^binding>\<open>Fun\<close>))\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     8
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
     9
definition swap :: \<open>'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    10
  where \<open>swap a b f = f (a := f b, b:= f a)\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    11
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    12
setup \<open>Context.theory_map (Name_Space.map_naming (Name_Space.parent_path))\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    13
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    14
lemma swap_apply [simp]:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    15
  "Fun.swap a b f a = f b"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    16
  "Fun.swap a b f b = f a"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    17
  "c \<noteq> a \<Longrightarrow> c \<noteq> b \<Longrightarrow> Fun.swap a b f c = f c"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    18
  by (simp_all add: Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    19
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    20
lemma swap_self [simp]: "Fun.swap a a f = f"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    21
  by (simp add: Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    22
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    23
lemma swap_commute: "Fun.swap a b f = Fun.swap b a f"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    24
  by (simp add: fun_upd_def Fun.swap_def fun_eq_iff)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    25
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    26
lemma swap_nilpotent [simp]: "Fun.swap a b (Fun.swap a b f) = f"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    27
  by (rule ext) (simp add: fun_upd_def Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    28
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    29
lemma swap_comp_involutory [simp]: "Fun.swap a b \<circ> Fun.swap a b = id"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    30
  by (rule ext) simp
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    31
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    32
lemma swap_triple:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    33
  assumes "a \<noteq> c" and "b \<noteq> c"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    34
  shows "Fun.swap a b (Fun.swap b c (Fun.swap a b f)) = Fun.swap a c f"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    35
  using assms by (simp add: fun_eq_iff Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    36
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    37
lemma comp_swap: "f \<circ> Fun.swap a b g = Fun.swap a b (f \<circ> g)"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    38
  by (rule ext) (simp add: fun_upd_def Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    39
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    40
lemma swap_image_eq [simp]:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    41
  assumes "a \<in> A" "b \<in> A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    42
  shows "Fun.swap a b f ` A = f ` A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    43
proof -
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    44
  have subset: "\<And>f. Fun.swap a b f ` A \<subseteq> f ` A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    45
    using assms by (auto simp: image_iff Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    46
  then have "Fun.swap a b (Fun.swap a b f) ` A \<subseteq> (Fun.swap a b f) ` A" .
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    47
  with subset[of f] show ?thesis by auto
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    48
qed
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    49
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    50
lemma inj_on_imp_inj_on_swap: "inj_on f A \<Longrightarrow> a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> inj_on (Fun.swap a b f) A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    51
  by (auto simp add: inj_on_def Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    52
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    53
lemma inj_on_swap_iff [simp]:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    54
  assumes A: "a \<in> A" "b \<in> A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    55
  shows "inj_on (Fun.swap a b f) A \<longleftrightarrow> inj_on f A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    56
proof
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    57
  assume "inj_on (Fun.swap a b f) A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    58
  with A have "inj_on (Fun.swap a b (Fun.swap a b f)) A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    59
    by (iprover intro: inj_on_imp_inj_on_swap)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    60
  then show "inj_on f A" by simp
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    61
next
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    62
  assume "inj_on f A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    63
  with A show "inj_on (Fun.swap a b f) A"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    64
    by (iprover intro: inj_on_imp_inj_on_swap)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    65
qed
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    66
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    67
lemma surj_imp_surj_swap: "surj f \<Longrightarrow> surj (Fun.swap a b f)"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    68
  by simp
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    69
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    70
lemma surj_swap_iff [simp]: "surj (Fun.swap a b f) \<longleftrightarrow> surj f"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    71
  by simp
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    72
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    73
lemma bij_betw_swap_iff [simp]: "x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> bij_betw (Fun.swap x y f) A B \<longleftrightarrow> bij_betw f A B"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    74
  by (auto simp: bij_betw_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    75
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    76
lemma bij_swap_iff [simp]: "bij (Fun.swap a b f) \<longleftrightarrow> bij f"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    77
  by simp
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    78
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    79
lemma swap_image:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    80
  \<open>Fun.swap i j f ` A = f ` (A - {i, j}
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    81
    \<union> (if i \<in> A then {j} else {}) \<union> (if j \<in> A then {i} else {}))\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    82
  by (auto simp add: Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    83
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    84
lemma inv_swap_id: "inv (Fun.swap a b id) = Fun.swap a b id"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    85
  by (rule inv_unique_comp) (simp_all add: fun_eq_iff Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    86
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    87
lemma bij_swap_comp:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    88
  assumes "bij p"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    89
  shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    90
  using surj_f_inv_f[OF bij_is_surj[OF \<open>bij p\<close>]]
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    91
  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF \<open>bij p\<close>])
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    92
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    93
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    94
subsection \<open>Transpositions\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    95
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    96
lemma swap_id_eq: "Fun.swap a b id x = (if x = a then b else if x = b then a else x)"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    97
  by (simp add: Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    98
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
    99
lemma swap_unfold:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   100
  \<open>Fun.swap a b p = p \<circ> Fun.swap a b id\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   101
  by (simp add: fun_eq_iff Fun.swap_def)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   102
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   103
lemma swap_id_idempotent [simp]: "Fun.swap a b id \<circ> Fun.swap a b id = id"
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   104
  by (simp flip: swap_unfold)
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   105
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   106
lemma bij_swap_compose_bij:
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   107
  \<open>bij (Fun.swap a b id \<circ> p)\<close> if \<open>bij p\<close>
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   108
  using that by (rule bij_comp) simp
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   109
5020054b3a16 tuned theory structure
haftmann
parents:
diff changeset
   110
end