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