src/HOL/Library/List_Permutation.thy
author haftmann
Tue, 23 Feb 2021 20:41:48 +0000
changeset 73297 beaff25452d2
parent 70680 src/HOL/Library/Permutation.thy@b8cd7ea34e33
child 73300 c52c5a5bf4e6
permissions -rw-r--r--
more specific name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73297
beaff25452d2 more specific name
haftmann
parents: 70680
diff changeset
     1
(*  Title:      HOL/Library/List_Permutation.thy
15005
546c8e7e28d4 Norbert Voelker
paulson
parents: 14706
diff changeset
     2
    Author:     Lawrence C Paulson and Thomas M Rasmussen and Norbert Voelker
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     3
*)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     4
73297
beaff25452d2 more specific name
haftmann
parents: 70680
diff changeset
     5
section \<open>Permuted Lists\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     6
73297
beaff25452d2 more specific name
haftmann
parents: 70680
diff changeset
     7
theory List_Permutation
51542
738598beeb26 tuned imports;
wenzelm
parents: 50037
diff changeset
     8
imports Multiset
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
     9
begin
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    10
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
    11
inductive perm :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixr \<open><~~>\<close> 50)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    12
where
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    13
  Nil [intro!]: "[] <~~> []"
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    14
| swap [intro!]: "y # x # l <~~> x # y # l"
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    15
| Cons [intro!]: "xs <~~> ys \<Longrightarrow> z # xs <~~> z # ys"
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    16
| trans [intro]: "xs <~~> ys \<Longrightarrow> ys <~~> zs \<Longrightarrow> xs <~~> zs"
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    17
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    18
proposition perm_refl [iff]: "l <~~> l"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    19
  by (induct l) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    20
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    21
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    22
subsection \<open>Some examples of rule induction on permutations\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    23
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
    24
proposition perm_empty_imp: "[] <~~> ys \<Longrightarrow> ys = []"
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
    25
  by (induction "[] :: 'a list" ys pred: perm) simp_all
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    26
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    27
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    28
text \<open>\medskip This more general theorem is easier to understand!\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    29
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    30
proposition perm_length: "xs <~~> ys \<Longrightarrow> length xs = length ys"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
    31
  by (induct pred: perm) simp_all
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    32
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    33
proposition perm_sym: "xs <~~> ys \<Longrightarrow> ys <~~> xs"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
    34
  by (induct pred: perm) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    35
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    36
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    37
subsection \<open>Ways of making new permutations\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    38
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    39
text \<open>We can insert the head anywhere in the list.\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    40
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    41
proposition perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    42
  by (induct xs) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    43
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    44
proposition perm_append_swap: "xs @ ys <~~> ys @ xs"
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    45
  by (induct xs) (auto intro: perm_append_Cons)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    46
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    47
proposition perm_append_single: "a # xs <~~> xs @ [a]"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    48
  by (rule perm.trans [OF _ perm_append_swap]) simp
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    49
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    50
proposition perm_rev: "rev xs <~~> xs"
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    51
  by (induct xs) (auto intro!: perm_append_single intro: perm_sym)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    52
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    53
proposition perm_append1: "xs <~~> ys \<Longrightarrow> l @ xs <~~> l @ ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    54
  by (induct l) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    55
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    56
proposition perm_append2: "xs <~~> ys \<Longrightarrow> xs @ l <~~> ys @ l"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    57
  by (blast intro!: perm_append_swap perm_append1)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    58
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    59
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    60
subsection \<open>Further results\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    61
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    62
proposition perm_empty [iff]: "[] <~~> xs \<longleftrightarrow> xs = []"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    63
  by (blast intro: perm_empty_imp)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    64
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    65
proposition perm_empty2 [iff]: "xs <~~> [] \<longleftrightarrow> xs = []"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
    66
  using perm_sym by auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    67
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    68
proposition perm_sing_imp: "ys <~~> xs \<Longrightarrow> xs = [y] \<Longrightarrow> ys = [y]"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
    69
  by (induct pred: perm) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    70
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    71
proposition perm_sing_eq [iff]: "ys <~~> [y] \<longleftrightarrow> ys = [y]"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    72
  by (blast intro: perm_sing_imp)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    73
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    74
proposition perm_sing_eq2 [iff]: "[y] <~~> ys \<longleftrightarrow> ys = [y]"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    75
  by (blast dest: perm_sym)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    76
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    77
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    78
subsection \<open>Removing elements\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    79
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    80
proposition perm_remove: "x \<in> set ys \<Longrightarrow> ys <~~> x # remove1 x ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    81
  by (induct ys) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    82
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    83
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    84
text \<open>\medskip Congruence rule\<close>
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    85
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    86
proposition perm_remove_perm: "xs <~~> ys \<Longrightarrow> remove1 z xs <~~> remove1 z ys"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
    87
  by (induct pred: perm) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    88
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    89
proposition remove_hd [simp]: "remove1 z (z # xs) = xs"
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 15005
diff changeset
    90
  by auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    91
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    92
proposition cons_perm_imp_perm: "z # xs <~~> z # ys \<Longrightarrow> xs <~~> ys"
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63310
diff changeset
    93
  by (drule perm_remove_perm [where z = z]) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    94
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    95
proposition cons_perm_eq [iff]: "z#xs <~~> z#ys \<longleftrightarrow> xs <~~> ys"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
    96
  by (meson cons_perm_imp_perm perm.Cons)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    97
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
    98
proposition append_perm_imp_perm: "zs @ xs <~~> zs @ ys \<Longrightarrow> xs <~~> ys"
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    99
  by (induct zs arbitrary: xs ys rule: rev_induct) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   100
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   101
proposition perm_append1_eq [iff]: "zs @ xs <~~> zs @ ys \<longleftrightarrow> xs <~~> ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
   102
  by (blast intro: append_perm_imp_perm perm_append1)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   103
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   104
proposition perm_append2_eq [iff]: "xs @ zs <~~> ys @ zs \<longleftrightarrow> xs <~~> ys"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   105
  by (meson perm.trans perm_append1_eq perm_append_swap)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   106
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   107
theorem mset_eq_perm: "mset xs = mset ys \<longleftrightarrow> xs <~~> ys"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   108
proof
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   109
  assume "mset xs = mset ys"
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   110
  then show "xs <~~> ys"
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   111
  proof (induction xs arbitrary: ys)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   112
    case (Cons x xs)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   113
    then have "x \<in> set ys"
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   114
      using mset_eq_setD by fastforce
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   115
    then show ?case
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   116
      by (metis Cons.IH Cons.prems mset_remove1 perm.Cons perm.trans perm_remove perm_sym remove_hd)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   117
  qed auto
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   118
next
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   119
  assume "xs <~~> ys"
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   120
  then show "mset xs = mset ys"
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   121
    by induction (simp_all add: union_ac)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   122
qed
15005
546c8e7e28d4 Norbert Voelker
paulson
parents: 14706
diff changeset
   123
64587
8355a6e2df79 standardized notation
haftmann
parents: 63649
diff changeset
   124
proposition mset_le_perm_append: "mset xs \<subseteq># mset ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   125
  apply (rule iffI)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   126
  apply (metis mset_append mset_eq_perm mset_subset_eq_exists_conv surjD surj_mset)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   127
  by (metis mset_append mset_eq_perm mset_subset_eq_exists_conv)
15005
546c8e7e28d4 Norbert Voelker
paulson
parents: 14706
diff changeset
   128
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   129
proposition perm_set_eq: "xs <~~> ys \<Longrightarrow> set xs = set ys"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60502
diff changeset
   130
  by (metis mset_eq_perm mset_eq_setD)
25277
95128fcdd7e8 added lemmas
nipkow
parents: 23755
diff changeset
   131
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   132
proposition perm_distinct_iff: "xs <~~> ys \<Longrightarrow> distinct xs = distinct ys"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   133
  by (metis card_distinct distinct_card perm_length perm_set_eq)
25277
95128fcdd7e8 added lemmas
nipkow
parents: 23755
diff changeset
   134
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   135
theorem eq_set_perm_remdups: "set xs = set ys \<Longrightarrow> remdups xs <~~> remdups ys"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   136
proof (induction xs arbitrary: ys rule: length_induct)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   137
  case (1 xs)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   138
  show ?case
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   139
  proof (cases "remdups xs")
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   140
    case Nil
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   141
    with "1.prems" show ?thesis
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   142
      using "1.prems" by auto
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   143
  next
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   144
    case (Cons x us)
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   145
    then have "x \<in> set (remdups ys)"
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   146
      using "1.prems" set_remdups by fastforce
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   147
    then show ?thesis
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   148
      using "1.prems" mset_eq_perm set_eq_iff_mset_remdups_eq by blast
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   149
  qed
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   150
qed 
25287
094dab519ff5 added lemmas
nipkow
parents: 25277
diff changeset
   151
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   152
proposition perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> set x = set y"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   153
  by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
25287
094dab519ff5 added lemmas
nipkow
parents: 25277
diff changeset
   154
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   155
theorem permutation_Ex_bij:
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   156
  assumes "xs <~~> ys"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   157
  shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))"
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   158
  using assms
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   159
proof induct
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   160
  case Nil
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   161
  then show ?case
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   162
    unfolding bij_betw_def by simp
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   163
next
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   164
  case (swap y x l)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   165
  show ?case
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   166
  proof (intro exI[of _ "Fun.swap 0 1 id"] conjI allI impI)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   167
    show "bij_betw (Fun.swap 0 1 id) {..<length (y # x # l)} {..<length (x # y # l)}"
50037
f2a32197a33a tuned proofs
bulwahn
parents: 44890
diff changeset
   168
      by (auto simp: bij_betw_def)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   169
    fix i
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   170
    assume "i < length (y # x # l)"
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   171
    show "(y # x # l) ! i = (x # y # l) ! (Fun.swap 0 1 id) i"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   172
      by (cases i) (auto simp: Fun.swap_def gr0_conv_Suc)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   173
  qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   174
next
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   175
  case (Cons xs ys z)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   176
  then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   177
    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   178
    by blast
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   179
  let ?f = "\<lambda>i. case i of Suc n \<Rightarrow> Suc (f n) | 0 \<Rightarrow> 0"
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   180
  show ?case
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   181
  proof (intro exI[of _ ?f] allI conjI impI)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   182
    have *: "{..<length (z#xs)} = {0} \<union> Suc ` {..<length xs}"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   183
            "{..<length (z#ys)} = {0} \<union> Suc ` {..<length ys}"
39078
39f8f6d1eb74 Fixes lemma names
hoelzl
parents: 39075
diff changeset
   184
      by (simp_all add: lessThan_Suc_eq_insert_0)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   185
    show "bij_betw ?f {..<length (z#xs)} {..<length (z#ys)}"
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   186
      unfolding *
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   187
    proof (rule bij_betw_combine)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   188
      show "bij_betw ?f (Suc ` {..<length xs}) (Suc ` {..<length ys})"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   189
        using bij unfolding bij_betw_def
56154
f0a927235162 more complete set of lemmas wrt. image and composition
haftmann
parents: 55584
diff changeset
   190
        by (auto intro!: inj_onI imageI dest: inj_onD simp: image_comp comp_def)
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   191
    qed (auto simp: bij_betw_def)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   192
    fix i
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   193
    assume "i < length (z # xs)"
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   194
    then show "(z # xs) ! i = (z # ys) ! (?f i)"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   195
      using perm by (cases i) auto
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   196
  qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   197
next
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   198
  case (trans xs ys zs)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   199
  then obtain f g
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   200
    where bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   201
    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" "\<forall>i<length ys. ys ! i = zs ! (g i)"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   202
    by blast
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   203
  show ?case
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   204
  proof (intro exI[of _ "g \<circ> f"] conjI allI impI)
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   205
    show "bij_betw (g \<circ> f) {..<length xs} {..<length zs}"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   206
      using bij by (rule bij_betw_trans)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   207
    fix i
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   208
    assume "i < length xs"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   209
    with bij have "f i < length ys"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   210
      unfolding bij_betw_def by force
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
   211
    with \<open>i < length xs\<close> show "xs ! i = zs ! (g \<circ> f) i"
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   212
      using trans(1,3)[THEN perm_length] perm by auto
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   213
  qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   214
qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   215
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   216
proposition perm_finite: "finite {B. B <~~> A}"
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   217
proof (rule finite_subset[where B="{xs. set xs \<subseteq> set A \<and> length xs \<le> length A}"])
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   218
 show "finite {xs. set xs \<subseteq> set A \<and> length xs \<le> length A}"
70680
b8cd7ea34e33 tidied up some massive ugliness
paulson <lp15@cam.ac.uk>
parents: 69597
diff changeset
   219
   using finite_lists_length_le by blast
61699
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   220
next
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   221
 show "{B. B <~~> A} \<subseteq> {xs. set xs \<subseteq> set A \<and> length xs \<le> length A}"
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   222
   by (clarsimp simp add: perm_length perm_set_eq)
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   223
qed
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   224
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   225
proposition perm_swap:
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   226
    assumes "i < length xs" "j < length xs"
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   227
    shows "xs[i := xs ! j, j := xs ! i] <~~> xs"
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   228
  using assms by (simp add: mset_eq_perm[symmetric] mset_swap)
a81dc5c4d6a9 New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents: 61585
diff changeset
   229
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   230
end