src/HOL/Library/Multiset_Order.thy
author wenzelm
Mon, 25 Apr 2016 16:09:26 +0200
changeset 63040 eb4ddd18d635
parent 62430 9527ff088c15
child 63310 caaacf37943f
permissions -rw-r--r--
eliminated old 'def'; tuned comments;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Library/Multiset_Order.thy
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     2
    Author:     Dmitriy Traytel, TU Muenchen
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, Inria, LORIA, MPII
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     4
*)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     5
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
     6
section \<open>More Theorems about the Multiset Order\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     7
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     8
theory Multiset_Order
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
     9
imports Multiset
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    10
begin
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    11
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    12
subsubsection \<open>Alternative characterizations\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    13
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    14
context order
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    15
begin
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    16
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    17
lemma reflp_le: "reflp (op \<le>)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    18
  unfolding reflp_def by simp
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    19
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    20
lemma antisymP_le: "antisymP (op \<le>)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    21
  unfolding antisym_def by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    22
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    23
lemma transp_le: "transp (op \<le>)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    24
  unfolding transp_def by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    25
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    26
lemma irreflp_less: "irreflp (op <)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    27
  unfolding irreflp_def by simp
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    28
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    29
lemma antisymP_less: "antisymP (op <)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    30
  unfolding antisym_def by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    31
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    32
lemma transp_less: "transp (op <)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    33
  unfolding transp_def by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    34
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    35
lemmas le_trans = transp_le[unfolded transp_def, rule_format]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    36
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    37
lemma order_mult: "class.order
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    38
  (\<lambda>M N. (M, N) \<in> mult {(x, y). x < y} \<or> M = N)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    39
  (\<lambda>M N. (M, N) \<in> mult {(x, y). x < y})"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    40
  (is "class.order ?le ?less")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    41
proof -
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    42
  have irrefl: "\<And>M :: 'a multiset. \<not> ?less M M"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    43
  proof
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    44
    fix M :: "'a multiset"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    45
    have "trans {(x'::'a, x). x' < x}"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    46
      by (rule transI) simp
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    47
    moreover
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    48
    assume "(M, M) \<in> mult {(x, y). x < y}"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    49
    ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
    50
      \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    51
      by (rule mult_implies_one_step)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    52
    then obtain I J K where "M = I + J" and "M = I + K"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
    53
      and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})" by blast
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
    54
    then have aux1: "K \<noteq> {#}" and aux2: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
    55
    have "finite (set_mset K)" by simp
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    56
    moreover note aux2
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
    57
    ultimately have "set_mset K = {}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    58
      by (induct rule: finite_induct)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    59
       (simp, metis (mono_tags) insert_absorb insert_iff insert_not_empty less_irrefl less_trans)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    60
    with aux1 show False by simp
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    61
  qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    62
  have trans: "\<And>K M N :: 'a multiset. ?less K M \<Longrightarrow> ?less M N \<Longrightarrow> ?less K N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    63
    unfolding mult_def by (blast intro: trancl_trans)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    64
  show "class.order ?le ?less"
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60502
diff changeset
    65
    by standard (auto simp add: le_multiset_def irrefl dest: trans)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    66
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    67
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    68
text \<open>The Dershowitz--Manna ordering:\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    69
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    70
definition less_multiset\<^sub>D\<^sub>M where
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    71
  "less_multiset\<^sub>D\<^sub>M M N \<longleftrightarrow>
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
    72
   (\<exists>X Y. X \<noteq> {#} \<and> X \<le># N \<and> M = (N - X) + Y \<and> (\<forall>k. k \<in># Y \<longrightarrow> (\<exists>a. a \<in># X \<and> k < a)))"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    73
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    74
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    75
text \<open>The Huet--Oppen ordering:\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    76
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    77
definition less_multiset\<^sub>H\<^sub>O where
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    78
  "less_multiset\<^sub>H\<^sub>O M N \<longleftrightarrow> M \<noteq> N \<and> (\<forall>y. count N y < count M y \<longrightarrow> (\<exists>x. y < x \<and> count M x < count N x))"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    79
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    80
lemma mult_imp_less_multiset\<^sub>H\<^sub>O:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    81
  "(M, N) \<in> mult {(x, y). x < y} \<Longrightarrow> less_multiset\<^sub>H\<^sub>O M N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    82
proof (unfold mult_def, induct rule: trancl_induct)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    83
  case (base P)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    84
  then show ?case
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    85
    by (auto elim!: mult1_lessE simp add: count_eq_zero_iff less_multiset\<^sub>H\<^sub>O_def split: if_splits dest!: Suc_lessD)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    86
next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    87
  case (step N P)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    88
  from step(3) have "M \<noteq> N" and
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    89
    **: "\<And>y. count N y < count M y \<Longrightarrow> (\<exists>x>y. count M x < count N x)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    90
    by (simp_all add: less_multiset\<^sub>H\<^sub>O_def)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    91
  from step(2) obtain M0 a K where
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    92
    *: "P = M0 + {#a#}" "N = M0 + K" "a \<notin># K" "\<And>b. b \<in># K \<Longrightarrow> b < a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    93
    by (blast elim: mult1_lessE)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    94
  from \<open>M \<noteq> N\<close> ** *(1,2,3) have "M \<noteq> P" by (force dest: *(4) split: if_splits)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    95
  moreover
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    96
  { assume "count P a \<le> count M a"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    97
    with \<open>a \<notin># K\<close> have "count N a < count M a" unfolding *(1,2)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    98
      by (auto simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    99
      with ** obtain z where z: "z > a" "count M z < count N z"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   100
        by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   101
      with * have "count N z \<le> count P z" 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   102
        by (force simp add: not_in_iff)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   103
      with z have "\<exists>z > a. count M z < count P z" by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   104
  } note count_a = this
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   105
  { fix y
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   106
    assume count_y: "count P y < count M y"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   107
    have "\<exists>x>y. count M x < count P x"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   108
    proof (cases "y = a")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   109
      case True
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   110
      with count_y count_a show ?thesis by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   111
    next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   112
      case False
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   113
      show ?thesis
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   114
      proof (cases "y \<in># K")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   115
        case True
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   116
        with *(4) have "y < a" by simp
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   117
        then show ?thesis by (cases "count P a \<le> count M a") (auto dest: count_a intro: less_trans)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   118
      next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   119
        case False
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   120
        with \<open>y \<noteq> a\<close> have "count P y = count N y" unfolding *(1,2)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   121
          by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   122
        with count_y ** obtain z where z: "z > y" "count M z < count N z" by auto
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   123
        show ?thesis
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   124
        proof (cases "z \<in># K")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   125
          case True
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   126
          with *(4) have "z < a" by simp
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   127
          with z(1) show ?thesis
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   128
            by (cases "count P a \<le> count M a") (auto dest!: count_a intro: less_trans)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   129
        next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   130
          case False
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   131
          with \<open>a \<notin># K\<close> have "count N z \<le> count P z" unfolding *
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   132
            by (auto simp add: not_in_iff)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   133
          with z show ?thesis by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   134
        qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   135
      qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   136
    qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   137
  }
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   138
  ultimately show ?case unfolding less_multiset\<^sub>H\<^sub>O_def by blast
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   139
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   140
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   141
lemma less_multiset\<^sub>D\<^sub>M_imp_mult:
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   142
  "less_multiset\<^sub>D\<^sub>M M N \<Longrightarrow> (M, N) \<in> mult {(x, y). x < y}"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   143
proof -
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   144
  assume "less_multiset\<^sub>D\<^sub>M M N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   145
  then obtain X Y where
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
   146
    "X \<noteq> {#}" and "X \<le># N" and "M = N - X + Y" and "\<forall>k. k \<in># Y \<longrightarrow> (\<exists>a. a \<in># X \<and> k < a)"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   147
    unfolding less_multiset\<^sub>D\<^sub>M_def by blast
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   148
  then have "(N - X + Y, N - X + X) \<in> mult {(x, y). x < y}"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   149
    by (intro one_step_implies_mult) (auto simp: Bex_def trans_def)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
   150
  with \<open>M = N - X + Y\<close> \<open>X \<le># N\<close> show "(M, N) \<in> mult {(x, y). x < y}"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
   151
    by (metis subset_mset.diff_add)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   152
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   153
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   154
lemma less_multiset\<^sub>H\<^sub>O_imp_less_multiset\<^sub>D\<^sub>M: "less_multiset\<^sub>H\<^sub>O M N \<Longrightarrow> less_multiset\<^sub>D\<^sub>M M N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   155
unfolding less_multiset\<^sub>D\<^sub>M_def
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   156
proof (intro iffI exI conjI)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   157
  assume "less_multiset\<^sub>H\<^sub>O M N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   158
  then obtain z where z: "count M z < count N z"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   159
    unfolding less_multiset\<^sub>H\<^sub>O_def by (auto simp: multiset_eq_iff nat_neq_iff)
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62430
diff changeset
   160
  define X where "X = N - M"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62430
diff changeset
   161
  define Y where "Y = M - N"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   162
  from z show "X \<noteq> {#}" unfolding X_def by (auto simp: multiset_eq_iff not_less_eq_eq Suc_le_eq)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
   163
  from z show "X \<le># N" unfolding X_def by auto
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   164
  show "M = (N - X) + Y" unfolding X_def Y_def multiset_eq_iff count_union count_diff by force
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   165
  show "\<forall>k. k \<in># Y \<longrightarrow> (\<exists>a. a \<in># X \<and> k < a)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   166
  proof (intro allI impI)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   167
    fix k
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   168
    assume "k \<in># Y"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   169
    then have "count N k < count M k" unfolding Y_def
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   170
      by (auto simp add: in_diff_count)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
   171
    with \<open>less_multiset\<^sub>H\<^sub>O M N\<close> obtain a where "k < a" and "count M a < count N a"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   172
      unfolding less_multiset\<^sub>H\<^sub>O_def by blast
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   173
    then show "\<exists>a. a \<in># X \<and> k < a" unfolding X_def
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   174
      by (auto simp add: in_diff_count)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   175
  qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   176
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   177
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   178
lemma mult_less_multiset\<^sub>D\<^sub>M: "(M, N) \<in> mult {(x, y). x < y} \<longleftrightarrow> less_multiset\<^sub>D\<^sub>M M N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   179
  by (metis less_multiset\<^sub>D\<^sub>M_imp_mult less_multiset\<^sub>H\<^sub>O_imp_less_multiset\<^sub>D\<^sub>M mult_imp_less_multiset\<^sub>H\<^sub>O)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   180
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   181
lemma mult_less_multiset\<^sub>H\<^sub>O: "(M, N) \<in> mult {(x, y). x < y} \<longleftrightarrow> less_multiset\<^sub>H\<^sub>O M N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   182
  by (metis less_multiset\<^sub>D\<^sub>M_imp_mult less_multiset\<^sub>H\<^sub>O_imp_less_multiset\<^sub>D\<^sub>M mult_imp_less_multiset\<^sub>H\<^sub>O)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   183
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   184
lemmas mult\<^sub>D\<^sub>M = mult_less_multiset\<^sub>D\<^sub>M[unfolded less_multiset\<^sub>D\<^sub>M_def]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   185
lemmas mult\<^sub>H\<^sub>O = mult_less_multiset\<^sub>H\<^sub>O[unfolded less_multiset\<^sub>H\<^sub>O_def]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   186
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   187
end
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   188
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   189
context linorder
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   190
begin
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   191
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   192
lemma total_le: "total {(a :: 'a, b). a \<le> b}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   193
  unfolding total_on_def by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   194
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   195
lemma total_less: "total {(a :: 'a, b). a < b}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   196
  unfolding total_on_def by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   197
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   198
lemma linorder_mult: "class.linorder
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   199
  (\<lambda>M N. (M, N) \<in> mult {(x, y). x < y} \<or> M = N)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   200
  (\<lambda>M N. (M, N) \<in> mult {(x, y). x < y})"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   201
proof -
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   202
  interpret o: order
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   203
    "(\<lambda>M N. (M, N) \<in> mult {(x, y). x < y} \<or> M = N)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   204
    "(\<lambda>M N. (M, N) \<in> mult {(x, y). x < y})"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   205
    by (rule order_mult)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   206
  show ?thesis by unfold_locales (auto 0 3 simp: mult\<^sub>H\<^sub>O not_less_iff_gr_or_eq)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   207
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   208
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   209
end
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   210
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   211
lemma less_multiset_less_multiset\<^sub>H\<^sub>O:
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   212
  "M #\<subset># N \<longleftrightarrow> less_multiset\<^sub>H\<^sub>O M N"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   213
  unfolding less_multiset_def mult\<^sub>H\<^sub>O less_multiset\<^sub>H\<^sub>O_def ..
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   214
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   215
lemmas less_multiset\<^sub>D\<^sub>M = mult\<^sub>D\<^sub>M[folded less_multiset_def]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   216
lemmas less_multiset\<^sub>H\<^sub>O = mult\<^sub>H\<^sub>O[folded less_multiset_def]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   217
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   218
lemma le_multiset\<^sub>H\<^sub>O:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   219
  fixes M N :: "('a :: linorder) multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   220
  shows "M #\<subseteq># N \<longleftrightarrow> (\<forall>y. count N y < count M y \<longrightarrow> (\<exists>x. y < x \<and> count M x < count N x))"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   221
  by (auto simp: le_multiset_def less_multiset\<^sub>H\<^sub>O)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   222
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   223
lemma wf_less_multiset: "wf {(M :: ('a :: wellorder) multiset, N). M #\<subset># N}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   224
  unfolding less_multiset_def by (auto intro: wf_mult wf)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   225
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   226
lemma order_multiset: "class.order
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   227
  (le_multiset :: ('a :: order) multiset \<Rightarrow> ('a :: order) multiset \<Rightarrow> bool)
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   228
  (less_multiset :: ('a :: order) multiset \<Rightarrow> ('a :: order) multiset \<Rightarrow> bool)"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   229
  by unfold_locales
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   230
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   231
lemma linorder_multiset: "class.linorder
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   232
  (le_multiset :: ('a :: linorder) multiset \<Rightarrow> ('a :: linorder) multiset \<Rightarrow> bool)
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   233
  (less_multiset :: ('a :: linorder) multiset \<Rightarrow> ('a :: linorder) multiset \<Rightarrow> bool)"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   234
  by unfold_locales (fastforce simp add: less_multiset\<^sub>H\<^sub>O le_multiset_def not_less_iff_gr_or_eq)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   235
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   236
interpretation multiset_linorder: linorder
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   237
  "le_multiset :: ('a :: linorder) multiset \<Rightarrow> ('a :: linorder) multiset \<Rightarrow> bool"
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   238
  "less_multiset :: ('a :: linorder) multiset \<Rightarrow> ('a :: linorder) multiset \<Rightarrow> bool"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   239
  by (rule linorder_multiset)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   240
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   241
interpretation multiset_wellorder: wellorder
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   242
  "le_multiset :: ('a :: wellorder) multiset \<Rightarrow> ('a :: wellorder) multiset \<Rightarrow> bool"
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   243
  "less_multiset :: ('a :: wellorder) multiset \<Rightarrow> ('a :: wellorder) multiset \<Rightarrow> bool"
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 61076
diff changeset
   244
  by unfold_locales (blast intro: wf_less_multiset [unfolded wf_def, simplified, rule_format])
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   245
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   246
lemma le_multiset_total:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   247
  fixes M N :: "('a :: linorder) multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   248
  shows "\<not> M #\<subseteq># N \<Longrightarrow> N #\<subseteq># M"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   249
  by (metis multiset_linorder.le_cases)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   250
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   251
lemma less_eq_imp_le_multiset:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   252
  fixes M N :: "('a :: linorder) multiset"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
   253
  shows "M \<le># N \<Longrightarrow> M #\<subseteq># N"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   254
  unfolding le_multiset_def less_multiset\<^sub>H\<^sub>O
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
   255
  by (simp add: less_le_not_le subseteq_mset_def)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   256
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   257
lemma less_multiset_right_total:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   258
  fixes M :: "('a :: linorder) multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   259
  shows "M #\<subset># M + {#undefined#}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   260
  unfolding le_multiset_def less_multiset\<^sub>H\<^sub>O by simp
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   261
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   262
lemma le_multiset_empty_left[simp]:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   263
  fixes M :: "('a :: linorder) multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   264
  shows "{#} #\<subseteq># M"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   265
  by (simp add: less_eq_imp_le_multiset)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   266
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   267
lemma le_multiset_empty_right[simp]:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   268
  fixes M :: "('a :: linorder) multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   269
  shows "M \<noteq> {#} \<Longrightarrow> \<not> M #\<subseteq># {#}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   270
  by (metis le_multiset_empty_left multiset_order.antisym)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   271
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   272
lemma less_multiset_empty_left[simp]:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   273
  fixes M :: "('a :: linorder) multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   274
  shows "M \<noteq> {#} \<Longrightarrow> {#} #\<subset># M"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   275
  by (simp add: less_multiset\<^sub>H\<^sub>O)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   276
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   277
lemma less_multiset_empty_right[simp]:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   278
  fixes M :: "('a :: linorder) multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   279
  shows "\<not> M #\<subset># {#}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   280
  using le_empty less_multiset\<^sub>D\<^sub>M by blast
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   281
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   282
lemma
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   283
  fixes M N :: "('a :: linorder) multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   284
  shows
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   285
    le_multiset_plus_left[simp]: "N #\<subseteq># (M + N)" and
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   286
    le_multiset_plus_right[simp]: "M #\<subseteq># (M + N)"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   287
  using [[metis_verbose = false]] by (metis less_eq_imp_le_multiset mset_le_add_left add.commute)+
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   288
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   289
lemma
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   290
  fixes M N :: "('a :: linorder) multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   291
  shows
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   292
    less_multiset_plus_plus_left_iff[simp]: "M + N #\<subset># M' + N \<longleftrightarrow> M #\<subset># M'" and
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   293
    less_multiset_plus_plus_right_iff[simp]: "M + N #\<subset># M + N' \<longleftrightarrow> N #\<subset># N'"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   294
  unfolding less_multiset\<^sub>H\<^sub>O by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   295
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   296
lemma add_eq_self_empty_iff: "M + N = M \<longleftrightarrow> N = {#}"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   297
  by (metis add.commute add_diff_cancel_right' monoid_add_class.add.left_neutral)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   298
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   299
lemma
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   300
  fixes M N :: "('a :: linorder) multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   301
  shows
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   302
    less_multiset_plus_left_nonempty[simp]: "M \<noteq> {#} \<Longrightarrow> N #\<subset># M + N" and
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59817
diff changeset
   303
    less_multiset_plus_right_nonempty[simp]: "N \<noteq> {#} \<Longrightarrow> M #\<subset># M + N"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   304
  using [[metis_verbose = false]]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   305
  by (metis add.right_neutral less_multiset_empty_left less_multiset_plus_plus_right_iff
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   306
    add.commute)+
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   307
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   308
lemma ex_gt_imp_less_multiset: "(\<exists>y :: 'a :: linorder. y \<in># N \<and> (\<forall>x. x \<in># M \<longrightarrow> x < y)) \<Longrightarrow> M #\<subset># N"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   309
  unfolding less_multiset\<^sub>H\<^sub>O
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   310
  by (metis count_eq_zero_iff count_greater_zero_iff less_le_not_le)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   311
  
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   312
lemma ex_gt_count_imp_less_multiset:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60679
diff changeset
   313
  "(\<forall>y :: 'a :: linorder. y \<in># M + N \<longrightarrow> y \<le> x) \<Longrightarrow> count M x < count N x \<Longrightarrow> M #\<subset># N"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   314
  unfolding less_multiset\<^sub>H\<^sub>O
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   315
  by (metis add_gr_0 count_union mem_Collect_eq not_gr0 not_le not_less_iff_gr_or_eq set_mset_def)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   316
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
   317
lemma union_less_diff_plus: "P \<le># M \<Longrightarrow> N #\<subset># P \<Longrightarrow> M - P + N #\<subset># M"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59958
diff changeset
   318
  by (drule subset_mset.diff_add[symmetric]) (metis union_less_mono2)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   319
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   320
end