src/HOL/Library/Permutation.thy
author wenzelm
Tue, 29 Apr 2014 22:50:55 +0200
changeset 56796 9f84219715a7
parent 56154 f0a927235162
child 57816 d8bbb97689d3
permissions -rw-r--r--
tuned proofs;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Library/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
14706
71590b7733b7 tuned document;
wenzelm
parents: 11153
diff changeset
     5
header {* Permutations *}
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     6
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
     7
theory 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
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    11
inductive perm :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  ("_ <~~> _"  [50, 50] 50)  (* FIXME proper infix, without ambiguity!? *)
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
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    18
lemma 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
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    22
subsection {* Some examples of rule induction on permutations *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    23
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    24
lemma xperm_empty_imp: "[] <~~> ys \<Longrightarrow> ys = []"
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
    25
  by (induct xs == "[] :: '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
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
    28
text {* \medskip This more general theorem is easier to understand! *}
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    29
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    30
lemma 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
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    33
lemma perm_empty_imp: "[] <~~> xs \<Longrightarrow> xs = []"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    34
  by (drule perm_length) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    35
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    36
lemma perm_sym: "xs <~~> ys \<Longrightarrow> ys <~~> xs"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
    37
  by (induct pred: perm) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    38
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    39
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    40
subsection {* Ways of making new permutations *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    41
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
    42
text {* We can insert the head anywhere in the list. *}
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    43
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    44
lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    45
  by (induct xs) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    46
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    47
lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    48
  apply (induct xs)
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    49
    apply simp_all
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    50
  apply (blast intro: perm_append_Cons)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    51
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    52
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    53
lemma perm_append_single: "a # xs <~~> xs @ [a]"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    54
  by (rule perm.trans [OF _ perm_append_swap]) simp
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    55
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    56
lemma perm_rev: "rev xs <~~> xs"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    57
  apply (induct xs)
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    58
   apply simp_all
11153
950ede59c05a Blast bug fix made old proof too slow
paulson
parents: 11054
diff changeset
    59
  apply (blast intro!: perm_append_single intro: perm_sym)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    60
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    61
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    62
lemma perm_append1: "xs <~~> ys \<Longrightarrow> l @ xs <~~> l @ ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    63
  by (induct l) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    64
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    65
lemma perm_append2: "xs <~~> ys \<Longrightarrow> xs @ l <~~> ys @ l"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    66
  by (blast intro!: perm_append_swap perm_append1)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    67
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    68
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    69
subsection {* Further results *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    70
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
    71
lemma perm_empty [iff]: "[] <~~> xs \<longleftrightarrow> xs = []"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    72
  by (blast intro: perm_empty_imp)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    73
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
    74
lemma perm_empty2 [iff]: "xs <~~> [] \<longleftrightarrow> xs = []"
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    75
  apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    76
  apply (erule perm_sym [THEN perm_empty_imp])
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    77
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    78
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    79
lemma perm_sing_imp: "ys <~~> xs \<Longrightarrow> xs = [y] \<Longrightarrow> ys = [y]"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
    80
  by (induct pred: perm) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    81
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
    82
lemma perm_sing_eq [iff]: "ys <~~> [y] \<longleftrightarrow> ys = [y]"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    83
  by (blast intro: perm_sing_imp)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    84
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
    85
lemma perm_sing_eq2 [iff]: "[y] <~~> ys \<longleftrightarrow> ys = [y]"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    86
  by (blast dest: perm_sym)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    87
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    88
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    89
subsection {* Removing elements *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    90
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    91
lemma perm_remove: "x \<in> set ys \<Longrightarrow> ys <~~> x # remove1 x ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
    92
  by (induct ys) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    93
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    94
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    95
text {* \medskip Congruence rule *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    96
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
    97
lemma perm_remove_perm: "xs <~~> ys \<Longrightarrow> remove1 z xs <~~> remove1 z ys"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
    98
  by (induct pred: perm) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    99
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 35272
diff changeset
   100
lemma remove_hd [simp]: "remove1 z (z # xs) = xs"
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 15005
diff changeset
   101
  by auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   102
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   103
lemma cons_perm_imp_perm: "z # xs <~~> z # ys \<Longrightarrow> xs <~~> ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
   104
  by (drule_tac z = z in perm_remove_perm) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   105
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   106
lemma cons_perm_eq [iff]: "z#xs <~~> z#ys \<longleftrightarrow> xs <~~> ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
   107
  by (blast intro: cons_perm_imp_perm)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   108
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   109
lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys \<Longrightarrow> xs <~~> ys"
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   110
  by (induct zs arbitrary: xs ys rule: rev_induct) auto
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   111
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   112
lemma perm_append1_eq [iff]: "zs @ xs <~~> zs @ ys \<longleftrightarrow> xs <~~> ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
   113
  by (blast intro: append_perm_imp_perm perm_append1)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   114
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   115
lemma perm_append2_eq [iff]: "xs @ zs <~~> ys @ zs \<longleftrightarrow> xs <~~> ys"
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   116
  apply (safe intro!: perm_append2)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   117
  apply (rule append_perm_imp_perm)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   118
  apply (rule perm_append_swap [THEN perm.trans])
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   119
    -- {* the previous step helps this @{text blast} call succeed quickly *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   120
  apply (blast intro: perm_append_swap)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   121
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   122
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   123
lemma multiset_of_eq_perm: "multiset_of xs = multiset_of ys \<longleftrightarrow> xs <~~> ys"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
   124
  apply (rule iffI)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   125
  apply (erule_tac [2] perm.induct)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   126
  apply (simp_all add: union_ac)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   127
  apply (erule rev_mp)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   128
  apply (rule_tac x=ys in spec)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   129
  apply (induct_tac xs)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   130
  apply auto
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   131
  apply (erule_tac x = "remove1 a x" in allE)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   132
  apply (drule sym)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   133
  apply simp
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
   134
  apply (subgoal_tac "a \<in> set x")
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   135
  apply (drule_tac z = a in perm.Cons)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   136
  apply (erule perm.trans)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   137
  apply (rule perm_sym)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   138
  apply (erule perm_remove)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   139
  apply (drule_tac f=set_of in arg_cong)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   140
  apply simp
15005
546c8e7e28d4 Norbert Voelker
paulson
parents: 14706
diff changeset
   141
  done
546c8e7e28d4 Norbert Voelker
paulson
parents: 14706
diff changeset
   142
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   143
lemma multiset_of_le_perm_append: "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)"
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 15140
diff changeset
   144
  apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   145
  apply (insert surj_multiset_of)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   146
  apply (drule surjD)
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 15005
diff changeset
   147
  apply (blast intro: sym)+
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 15005
diff changeset
   148
  done
15005
546c8e7e28d4 Norbert Voelker
paulson
parents: 14706
diff changeset
   149
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   150
lemma perm_set_eq: "xs <~~> ys \<Longrightarrow> set xs = set ys"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   151
  by (metis multiset_of_eq_perm multiset_of_eq_setD)
25277
95128fcdd7e8 added lemmas
nipkow
parents: 23755
diff changeset
   152
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   153
lemma perm_distinct_iff: "xs <~~> ys \<Longrightarrow> distinct xs = distinct ys"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   154
  apply (induct pred: perm)
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   155
     apply simp_all
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 40122
diff changeset
   156
   apply fastforce
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   157
  apply (metis perm_set_eq)
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   158
  done
25277
95128fcdd7e8 added lemmas
nipkow
parents: 23755
diff changeset
   159
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   160
lemma eq_set_perm_remdups: "set xs = set ys \<Longrightarrow> remdups xs <~~> remdups ys"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   161
  apply (induct xs arbitrary: ys rule: length_induct)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   162
  apply (case_tac "remdups xs")
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   163
   apply simp_all
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   164
  apply (subgoal_tac "a \<in> set (remdups ys)")
55584
a879f14b6f95 merged 'List.set' with BNF-generated 'set'
blanchet
parents: 53238
diff changeset
   165
   prefer 2 apply (metis set_simps(2) insert_iff set_remdups)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   166
  apply (drule split_list) apply (elim exE conjE)
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   167
  apply (drule_tac x = list in spec) apply (erule impE) prefer 2
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   168
   apply (drule_tac x = "ysa @ zs" in spec) apply (erule impE) prefer 2
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   169
    apply simp
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   170
    apply (subgoal_tac "a # list <~~> a # ysa @ zs")
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   171
     apply (metis Cons_eq_appendI perm_append_Cons trans)
40122
1d8ad2ff3e01 dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
haftmann
parents: 39916
diff changeset
   172
    apply (metis Cons Cons_eq_appendI distinct.simps(2)
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   173
      distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   174
   apply (subgoal_tac "set (a # list) =
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   175
      set (ysa @ a # zs) \<and> distinct (a # list) \<and> distinct (ysa @ a # zs)")
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 40122
diff changeset
   176
    apply (fastforce simp add: insert_ident)
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   177
   apply (metis distinct_remdups set_remdups)
30742
3e89ac3905b9 tuned notoriously slow metis proof
haftmann
parents: 30738
diff changeset
   178
   apply (subgoal_tac "length (remdups xs) < Suc (length xs)")
3e89ac3905b9 tuned notoriously slow metis proof
haftmann
parents: 30738
diff changeset
   179
   apply simp
3e89ac3905b9 tuned notoriously slow metis proof
haftmann
parents: 30738
diff changeset
   180
   apply (subgoal_tac "length (remdups xs) \<le> length xs")
3e89ac3905b9 tuned notoriously slow metis proof
haftmann
parents: 30738
diff changeset
   181
   apply simp
3e89ac3905b9 tuned notoriously slow metis proof
haftmann
parents: 30738
diff changeset
   182
   apply (rule length_remdups_leq)
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   183
  done
25287
094dab519ff5 added lemmas
nipkow
parents: 25277
diff changeset
   184
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   185
lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> set x = set y"
25379
12bcf37252b1 tuned proofs;
wenzelm
parents: 25287
diff changeset
   186
  by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
25287
094dab519ff5 added lemmas
nipkow
parents: 25277
diff changeset
   187
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   188
lemma permutation_Ex_bij:
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   189
  assumes "xs <~~> ys"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   190
  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
   191
  using assms
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   192
proof induct
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   193
  case Nil
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   194
  then show ?case
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   195
    unfolding bij_betw_def by simp
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   196
next
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   197
  case (swap y x l)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   198
  show ?case
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   199
  proof (intro exI[of _ "Fun.swap 0 1 id"] conjI allI impI)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   200
    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
   201
      by (auto simp: bij_betw_def)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   202
    fix i
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   203
    assume "i < length (y # x # l)"
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   204
    show "(y # x # l) ! i = (x # y # l) ! (Fun.swap 0 1 id) i"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   205
      by (cases i) (auto simp: Fun.swap_def gr0_conv_Suc)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   206
  qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   207
next
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   208
  case (Cons xs ys z)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   209
  then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   210
    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   211
    by blast
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   212
  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
   213
  show ?case
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   214
  proof (intro exI[of _ ?f] allI conjI impI)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   215
    have *: "{..<length (z#xs)} = {0} \<union> Suc ` {..<length xs}"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   216
            "{..<length (z#ys)} = {0} \<union> Suc ` {..<length ys}"
39078
39f8f6d1eb74 Fixes lemma names
hoelzl
parents: 39075
diff changeset
   217
      by (simp_all add: lessThan_Suc_eq_insert_0)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   218
    show "bij_betw ?f {..<length (z#xs)} {..<length (z#ys)}"
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   219
      unfolding *
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   220
    proof (rule bij_betw_combine)
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   221
      show "bij_betw ?f (Suc ` {..<length xs}) (Suc ` {..<length ys})"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   222
        using bij unfolding bij_betw_def
56154
f0a927235162 more complete set of lemmas wrt. image and composition
haftmann
parents: 55584
diff changeset
   223
        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
   224
    qed (auto simp: bij_betw_def)
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   225
    fix i
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   226
    assume "i < length (z # xs)"
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   227
    then show "(z # xs) ! i = (z # ys) ! (?f i)"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   228
      using perm by (cases i) auto
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   229
  qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   230
next
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   231
  case (trans xs ys zs)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   232
  then obtain f g
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   233
    where bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   234
    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
   235
    by blast
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   236
  show ?case
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   237
  proof (intro exI[of _ "g \<circ> f"] conjI allI impI)
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   238
    show "bij_betw (g \<circ> f) {..<length xs} {..<length zs}"
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   239
      using bij by (rule bij_betw_trans)
56796
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   240
    fix i
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   241
    assume "i < length xs"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   242
    with bij have "f i < length ys"
9f84219715a7 tuned proofs;
wenzelm
parents: 56154
diff changeset
   243
      unfolding bij_betw_def by force
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   244
    with `i < length xs` show "xs ! i = zs ! (g \<circ> f) i"
53238
01ef0a103fc9 tuned proofs;
wenzelm
parents: 51542
diff changeset
   245
      using trans(1,3)[THEN perm_length] perm by auto
39075
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   246
  qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   247
qed
a18e5946d63c Permutation implies bij function
hoelzl
parents: 36903
diff changeset
   248
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   249
end