src/HOL/Library/Multiset_Order.thy
author blanchet
Thu, 07 Jul 2016 09:24:03 +0200
changeset 63409 3f3223b90239
parent 63407 89dd1345a04f
child 63410 9789ccc2a477
permissions -rw-r--r--
moved lemmas and locales around (with minor incompatibilities)
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
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
    12
subsection \<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"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
    65
    by standard (auto simp add: less_eq_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:
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   212
  "M < 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
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   218
lemma subset_eq_imp_le_multiset:
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   219
  shows "M \<le># N \<Longrightarrow> M \<le> N"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   220
  unfolding less_eq_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
   221
  by (simp add: less_le_not_le subseteq_mset_def)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   222
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   223
lemma le_multiset_right_total:
63407
89dd1345a04f leverage new 'order' type class instantiation in multiset
blanchet
parents: 63388
diff changeset
   224
  shows "M < M + {#x#}"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   225
  unfolding less_eq_multiset_def less_multiset\<^sub>H\<^sub>O by simp
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   226
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   227
lemma less_eq_multiset_empty_left[simp]:
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   228
  shows "{#} \<le> M"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   229
  by (simp add: subset_eq_imp_le_multiset)
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   230
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   231
lemma add_eq_self_empty_iff: "M + N = M \<longleftrightarrow> N = {#}"
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   232
  by (rule cancel_comm_monoid_add_class.add_cancel_left_right)
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   233
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   234
lemma ex_gt_imp_less_multiset: "(\<exists>y. y \<in># N \<and> (\<forall>x. x \<in># M \<longrightarrow> x < y)) \<Longrightarrow> M < N"
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   235
  unfolding less_multiset\<^sub>H\<^sub>O
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   236
  by (metis count_eq_zero_iff count_greater_zero_iff less_le_not_le)
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   237
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   238
lemma less_eq_multiset_empty_right[simp]:
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   239
  "M \<noteq> {#} \<Longrightarrow> \<not> M \<le> {#}"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   240
  by (metis less_eq_multiset_empty_left antisym)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   241
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   242
lemma le_multiset_empty_left[simp]: "M \<noteq> {#} \<Longrightarrow> {#} < M"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   243
  by (simp add: less_multiset\<^sub>H\<^sub>O)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   244
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   245
lemma le_multiset_empty_right[simp]: "\<not> M < {#}"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63040
diff changeset
   246
  using subset_eq_empty less_multiset\<^sub>D\<^sub>M by blast
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   247
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   248
lemma union_le_diff_plus: "P \<le># M \<Longrightarrow> N < P \<Longrightarrow> M - P + N < M"
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   249
  by (drule subset_mset.diff_add[symmetric]) (metis union_le_mono2)
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   250
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   251
instantiation multiset :: (linorder) linorder
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   252
begin
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   253
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   254
lemma less_eq_multiset\<^sub>H\<^sub>O:
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   255
  "M \<le> N \<longleftrightarrow> (\<forall>y. count N y < count M y \<longrightarrow> (\<exists>x. y < x \<and> count M x < count N x))"
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   256
  by (auto simp: less_eq_multiset_def less_multiset\<^sub>H\<^sub>O)
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   257
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   258
instance by standard (metis less_eq_multiset\<^sub>H\<^sub>O not_less_iff_gr_or_eq)
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   259
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   260
lemma less_eq_multiset_total:
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   261
  fixes M N :: "'a multiset"
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   262
  shows "\<not> M \<le> N \<Longrightarrow> N \<le> M"
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   263
  by simp
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   264
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   265
lemma
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   266
  fixes M N :: "'a multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   267
  shows
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   268
    less_eq_multiset_plus_left[simp]: "N \<le> (M + N)" and
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   269
    less_eq_multiset_plus_right[simp]: "M \<le> (M + N)"
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   270
  using [[metis_verbose = false]]
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   271
  by (metis subset_eq_imp_le_multiset mset_subset_eq_add_left add.commute)+
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   272
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   273
lemma
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   274
  fixes M N :: "'a multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   275
  shows
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   276
    le_multiset_plus_plus_left_iff[simp]: "M + N < M' + N \<longleftrightarrow> M < M'" and
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   277
    le_multiset_plus_plus_right_iff[simp]: "M + N < M + N' \<longleftrightarrow> N < N'"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   278
  unfolding less_multiset\<^sub>H\<^sub>O by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   279
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   280
lemma
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   281
  fixes M N :: "'a multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   282
  shows
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   283
    le_multiset_plus_left_nonempty[simp]: "M \<noteq> {#} \<Longrightarrow> N < M + N" and
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   284
    le_multiset_plus_right_nonempty[simp]: "N \<noteq> {#} \<Longrightarrow> M < M + N"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   285
  using [[metis_verbose = false]]
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   286
  by (metis add.right_neutral le_multiset_empty_left le_multiset_plus_plus_right_iff add.commute)+
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   287
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   288
lemma ex_gt_count_imp_le_multiset:
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   289
  "(\<forall>y :: 'a :: linorder. y \<in># M + N \<longrightarrow> y \<le> x) \<Longrightarrow> count M x < count N x \<Longrightarrow> M < N"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   290
  unfolding less_multiset\<^sub>H\<^sub>O
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   291
  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
   292
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   293
end
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   294
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   295
instantiation multiset :: (wellorder) wellorder
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   296
begin
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   297
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   298
lemma wf_less_multiset: "wf {(M :: 'a multiset, N). M < N}"
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   299
  unfolding less_multiset_def by (auto intro: wf_mult wf)
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   300
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   301
instance by standard (metis less_multiset_def wf wf_def wf_mult)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   302
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   303
end
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   304
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   305
end