src/HOL/Library/Permutation.thy
author wenzelm
Thu, 06 May 2004 14:14:18 +0200
changeset 14706 71590b7733b7
parent 11153 950ede59c05a
child 15005 546c8e7e28d4
permissions -rw-r--r--
tuned document;
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
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
14706
71590b7733b7 tuned document;
wenzelm
parents: 11153
diff changeset
     3
    Author:     Lawrence C Paulson and Thomas M Rasmussen
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     4
    Copyright   1995  University of Cambridge
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     5
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     6
TODO: it would be nice to prove (for "multiset", defined on
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     7
HOL/ex/Sorting.thy) xs <~~> ys = (\<forall>x. multiset xs x = multiset ys x)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     8
*)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
     9
14706
71590b7733b7 tuned document;
wenzelm
parents: 11153
diff changeset
    10
header {* Permutations *}
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    11
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    12
theory Permutation = Main:
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    13
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    14
consts
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    15
  perm :: "('a list * 'a list) set"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    16
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    17
syntax
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    18
  "_perm" :: "'a list => 'a list => bool"    ("_ <~~> _"  [50, 50] 50)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    19
translations
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    20
  "x <~~> y" == "(x, y) \<in> perm"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    21
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    22
inductive perm
11153
950ede59c05a Blast bug fix made old proof too slow
paulson
parents: 11054
diff changeset
    23
  intros
950ede59c05a Blast bug fix made old proof too slow
paulson
parents: 11054
diff changeset
    24
    Nil  [intro!]: "[] <~~> []"
950ede59c05a Blast bug fix made old proof too slow
paulson
parents: 11054
diff changeset
    25
    swap [intro!]: "y # x # l <~~> x # y # l"
950ede59c05a Blast bug fix made old proof too slow
paulson
parents: 11054
diff changeset
    26
    Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys"
950ede59c05a Blast bug fix made old proof too slow
paulson
parents: 11054
diff changeset
    27
    trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs"
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    28
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    29
lemma perm_refl [iff]: "l <~~> l"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    30
  apply (induct l)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    31
   apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    32
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    33
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    34
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    35
subsection {* Some examples of rule induction on permutations *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    36
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    37
lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    38
    -- {* the form of the premise lets the induction bind @{term xs} and @{term ys} *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    39
  apply (erule perm.induct)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    40
     apply (simp_all (no_asm_simp))
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    41
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    42
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    43
lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    44
  apply (insert xperm_empty_imp_aux)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    45
  apply blast
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    46
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    47
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    48
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    49
text {*
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    50
  \medskip This more general theorem is easier to understand!
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    51
  *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    52
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    53
lemma perm_length: "xs <~~> ys ==> length xs = length ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    54
  apply (erule perm.induct)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    55
     apply simp_all
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    56
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    57
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    58
lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    59
  apply (drule perm_length)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    60
  apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    61
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    62
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    63
lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    64
  apply (erule perm.induct)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    65
     apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    66
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    67
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    68
lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    69
  apply (erule perm.induct)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    70
     apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    71
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    72
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    73
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    74
subsection {* Ways of making new permutations *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    75
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    76
text {*
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    77
  We can insert the head anywhere in the list.
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    78
*}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    79
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    80
lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    81
  apply (induct xs)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    82
   apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    83
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    84
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    85
lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    86
  apply (induct xs)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    87
    apply simp_all
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    88
  apply (blast intro: perm_append_Cons)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    89
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    90
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    91
lemma perm_append_single: "a # xs <~~> xs @ [a]"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    92
  apply (rule perm.trans)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    93
   prefer 2
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    94
   apply (rule perm_append_swap)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    95
  apply simp
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    96
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    97
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    98
lemma perm_rev: "rev xs <~~> xs"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
    99
  apply (induct xs)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   100
   apply simp_all
11153
950ede59c05a Blast bug fix made old proof too slow
paulson
parents: 11054
diff changeset
   101
  apply (blast intro!: perm_append_single intro: perm_sym)
11054
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   102
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   103
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   104
lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   105
  apply (induct l)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   106
   apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   107
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   108
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   109
lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   110
  apply (blast intro!: perm_append_swap perm_append1)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   111
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   112
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   113
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   114
subsection {* Further results *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   115
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   116
lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   117
  apply (blast intro: perm_empty_imp)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   118
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   119
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   120
lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   121
  apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   122
  apply (erule perm_sym [THEN perm_empty_imp])
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   123
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   124
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   125
lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   126
  apply (erule perm.induct)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   127
     apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   128
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   129
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   130
lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   131
  apply (blast intro: perm_sing_imp)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   132
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   133
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   134
lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   135
  apply (blast dest: perm_sym)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   136
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   137
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   138
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   139
subsection {* Removing elements *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   140
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   141
consts
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   142
  remove :: "'a => 'a list => 'a list"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   143
primrec
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   144
  "remove x [] = []"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   145
  "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   146
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   147
lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   148
  apply (induct ys)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   149
   apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   150
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   151
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   152
lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   153
  apply (induct l)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   154
   apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   155
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   156
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   157
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   158
text {* \medskip Congruence rule *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   159
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   160
lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   161
  apply (erule perm.induct)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   162
     apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   163
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   164
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   165
lemma remove_hd [simp]: "remove z (z # xs) = xs"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   166
  apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   167
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   168
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   169
lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   170
  apply (drule_tac z = z in perm_remove_perm)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   171
  apply auto
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   172
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   173
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   174
lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   175
  apply (blast intro: cons_perm_imp_perm)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   176
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   177
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   178
lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   179
  apply (induct zs rule: rev_induct)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   180
   apply (simp_all (no_asm_use))
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   181
  apply blast
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   182
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   183
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   184
lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   185
  apply (blast intro: append_perm_imp_perm perm_append1)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   186
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   187
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   188
lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   189
  apply (safe intro!: perm_append2)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   190
  apply (rule append_perm_imp_perm)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   191
  apply (rule perm_append_swap [THEN perm.trans])
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   192
    -- {* the previous step helps this @{text blast} call succeed quickly *}
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   193
  apply (blast intro: perm_append_swap)
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   194
  done
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   195
a5404c70982f moved from Induct/ to Library/
wenzelm
parents:
diff changeset
   196
end