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