src/HOL/Library/Multiset_Order.thy
author wenzelm
Fri, 27 Nov 2020 11:41:43 +0100
changeset 72740 082200ee003d
parent 67020 c32254ab1901
child 74806 ba59c691b3ee
permissions -rw-r--r--
tuned signature;
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
65546
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
    12
subsection \<open>Alternative Characterizations\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    13
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
    14
context preorder
59813
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 order_mult: "class.order
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    18
  (\<lambda>M N. (M, N) \<in> mult {(x, y). x < y} \<or> M = N)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    19
  (\<lambda>M N. (M, N) \<in> mult {(x, y). x < y})"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    20
  (is "class.order ?le ?less")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    21
proof -
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    22
  have irrefl: "\<And>M :: 'a multiset. \<not> ?less M M"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    23
  proof
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    24
    fix M :: "'a multiset"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    25
    have "trans {(x'::'a, x). x' < x}"
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
    26
      by (rule transI) (blast intro: less_trans)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    27
    moreover
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    28
    assume "(M, M) \<in> mult {(x, y). x < y}"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    29
    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
    30
      \<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
    31
      by (rule mult_implies_one_step)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    32
    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
    33
      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
    34
    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
    35
    have "finite (set_mset K)" by simp
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    36
    moreover note aux2
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
    37
    ultimately have "set_mset K = {}"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    38
      by (induct rule: finite_induct)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    39
       (simp, metis (mono_tags) insert_absorb insert_iff insert_not_empty less_irrefl less_trans)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    40
    with aux1 show False by simp
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    41
  qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    42
  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
    43
    unfolding mult_def by (blast intro: trancl_trans)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    44
  show "class.order ?le ?less"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
    45
    by standard (auto simp add: less_eq_multiset_def irrefl dest: trans)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    46
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    47
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    48
text \<open>The Dershowitz--Manna ordering:\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    49
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    50
definition less_multiset\<^sub>D\<^sub>M where
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    51
  "less_multiset\<^sub>D\<^sub>M M N \<longleftrightarrow>
64587
8355a6e2df79 standardized notation
haftmann
parents: 64418
diff changeset
    52
   (\<exists>X Y. X \<noteq> {#} \<and> X \<subseteq># 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
    53
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    54
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
    55
text \<open>The Huet--Oppen ordering:\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    56
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    57
definition less_multiset\<^sub>H\<^sub>O where
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    58
  "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
    59
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    60
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
    61
  "(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
    62
proof (unfold mult_def, induct rule: trancl_induct)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    63
  case (base P)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    64
  then show ?case
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    65
    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
    66
next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    67
  case (step N P)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    68
  from step(3) have "M \<noteq> N" and
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    69
    **: "\<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
    70
    by (simp_all add: less_multiset\<^sub>H\<^sub>O_def)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    71
  from step(2) obtain M0 a K where
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
    72
    *: "P = add_mset a M0" "N = M0 + K" "a \<notin># K" "\<And>b. b \<in># K \<Longrightarrow> b < a"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    73
    by (blast elim: mult1_lessE)
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
    74
  from \<open>M \<noteq> N\<close> ** *(1,2,3) have "M \<noteq> P" by (force dest: *(4) elim!: less_asym split: if_splits )
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    75
  moreover
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    76
  { 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
    77
    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
    78
      by (auto simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    79
      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
    80
        by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    81
      with * have "count N z \<le> count P z" 
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
    82
        by (auto elim: less_asym intro: count_inI)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    83
      with z have "\<exists>z > a. count M z < count P z" by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    84
  } note count_a = this
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    85
  { fix y
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    86
    assume count_y: "count P y < count M y"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    87
    have "\<exists>x>y. count M x < count P x"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    88
    proof (cases "y = a")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    89
      case True
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    90
      with count_y count_a show ?thesis by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    91
    next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    92
      case False
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    93
      show ?thesis
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    94
      proof (cases "y \<in># K")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    95
        case True
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
    96
        with *(4) have "y < a" by simp
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    97
        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
    98
      next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
    99
        case False
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   100
        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
   101
          by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   102
        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
   103
        show ?thesis
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   104
        proof (cases "z \<in># K")
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   105
          case True
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   106
          with *(4) have "z < a" by simp
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   107
          with z(1) show ?thesis
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   108
            by (cases "count P a \<le> count M a") (auto dest!: count_a intro: less_trans)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   109
        next
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   110
          case False
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   111
          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
   112
            by (auto simp add: not_in_iff)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   113
          with z show ?thesis by auto
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   114
        qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   115
      qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   116
    qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   117
  }
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   118
  ultimately show ?case unfolding less_multiset\<^sub>H\<^sub>O_def by blast
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   119
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   120
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   121
lemma less_multiset\<^sub>D\<^sub>M_imp_mult:
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   122
  "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
   123
proof -
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   124
  assume "less_multiset\<^sub>D\<^sub>M M N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   125
  then obtain X Y where
64587
8355a6e2df79 standardized notation
haftmann
parents: 64418
diff changeset
   126
    "X \<noteq> {#}" and "X \<subseteq># 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
   127
    unfolding less_multiset\<^sub>D\<^sub>M_def by blast
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   128
  then have "(N - X + Y, N - X + X) \<in> mult {(x, y). x < y}"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   129
    by (intro one_step_implies_mult) (auto simp: Bex_def trans_def)
64587
8355a6e2df79 standardized notation
haftmann
parents: 64418
diff changeset
   130
  with \<open>M = N - X + Y\<close> \<open>X \<subseteq># 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
   131
    by (metis subset_mset.diff_add)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   132
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   133
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   134
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
   135
unfolding less_multiset\<^sub>D\<^sub>M_def
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   136
proof (intro iffI exI conjI)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   137
  assume "less_multiset\<^sub>H\<^sub>O M N"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   138
  then obtain z where z: "count M z < count N z"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   139
    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
   140
  define X where "X = N - M"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62430
diff changeset
   141
  define Y where "Y = M - N"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   142
  from z show "X \<noteq> {#}" unfolding X_def by (auto simp: multiset_eq_iff not_less_eq_eq Suc_le_eq)
64587
8355a6e2df79 standardized notation
haftmann
parents: 64418
diff changeset
   143
  from z show "X \<subseteq># N" unfolding X_def by auto
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   144
  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
   145
  show "\<forall>k. k \<in># Y \<longrightarrow> (\<exists>a. a \<in># X \<and> k < a)"
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   146
  proof (intro allI impI)
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   147
    fix k
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   148
    assume "k \<in># Y"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 61424
diff changeset
   149
    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
   150
      by (auto simp add: in_diff_count)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60397
diff changeset
   151
    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
   152
      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
   153
    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
   154
      by (auto simp add: in_diff_count)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   155
  qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   156
qed
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   157
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   158
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
   159
  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
   160
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   161
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
   162
  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
   163
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   164
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
   165
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
   166
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   167
end
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   168
67020
c32254ab1901 added FIXMEs
blanchet
parents: 65546
diff changeset
   169
lemma less_multiset_less_multiset\<^sub>H\<^sub>O: "M < N \<longleftrightarrow> less_multiset\<^sub>H\<^sub>O M N"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   170
  unfolding less_multiset_def mult\<^sub>H\<^sub>O less_multiset\<^sub>H\<^sub>O_def ..
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   171
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   172
lemmas less_multiset\<^sub>D\<^sub>M = mult\<^sub>D\<^sub>M[folded less_multiset_def]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   173
lemmas less_multiset\<^sub>H\<^sub>O = mult\<^sub>H\<^sub>O[folded less_multiset_def]
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   174
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   175
lemma subset_eq_imp_le_multiset:
64587
8355a6e2df79 standardized notation
haftmann
parents: 64418
diff changeset
   176
  shows "M \<subseteq># N \<Longrightarrow> M \<le> N"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   177
  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
   178
  by (simp add: less_le_not_le subseteq_mset_def)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   179
67020
c32254ab1901 added FIXMEs
blanchet
parents: 65546
diff changeset
   180
(* FIXME: "le" should be "less" in this and other names *)
c32254ab1901 added FIXMEs
blanchet
parents: 65546
diff changeset
   181
lemma le_multiset_right_total: "M < add_mset x M"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   182
  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
   183
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   184
lemma less_eq_multiset_empty_left[simp]:
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   185
  shows "{#} \<le> M"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   186
  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
   187
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   188
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
   189
  unfolding less_multiset\<^sub>H\<^sub>O
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   190
  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
   191
67020
c32254ab1901 added FIXMEs
blanchet
parents: 65546
diff changeset
   192
lemma less_eq_multiset_empty_right[simp]: "M \<noteq> {#} \<Longrightarrow> \<not> M \<le> {#}"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   193
  by (metis less_eq_multiset_empty_left antisym)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   194
67020
c32254ab1901 added FIXMEs
blanchet
parents: 65546
diff changeset
   195
(* FIXME: "le" should be "less" in this and other names *)
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   196
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
   197
  by (simp add: less_multiset\<^sub>H\<^sub>O)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   198
67020
c32254ab1901 added FIXMEs
blanchet
parents: 65546
diff changeset
   199
(* FIXME: "le" should be "less" in this and other names *)
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   200
lemma le_multiset_empty_right[simp]: "\<not> M < {#}"
64076
9f089287687b tuning multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
   201
  using subset_mset.le_zero_eq less_multiset\<^sub>D\<^sub>M by blast
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   202
67020
c32254ab1901 added FIXMEs
blanchet
parents: 65546
diff changeset
   203
(* FIXME: "le" should be "less" in this and other names *)
64587
8355a6e2df79 standardized notation
haftmann
parents: 64418
diff changeset
   204
lemma union_le_diff_plus: "P \<subseteq># M \<Longrightarrow> N < P \<Longrightarrow> M - P + N < M"
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   205
  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
   206
63525
f01d1e393f3f more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63410
diff changeset
   207
instantiation multiset :: (preorder) ordered_ab_semigroup_monoid_add_imp_le
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   208
begin
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   209
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   210
lemma less_eq_multiset\<^sub>H\<^sub>O:
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   211
  "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
   212
  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
   213
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   214
instance by standard (auto simp: less_eq_multiset\<^sub>H\<^sub>O)
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   215
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   216
lemma
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   217
  fixes M N :: "'a multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   218
  shows
63525
f01d1e393f3f more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63410
diff changeset
   219
    less_eq_multiset_plus_left: "N \<le> (M + N)" and
f01d1e393f3f more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63410
diff changeset
   220
    less_eq_multiset_plus_right: "M \<le> (M + N)"
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   221
  by simp_all
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   222
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   223
lemma
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   224
  fixes M N :: "'a multiset"
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   225
  shows
63525
f01d1e393f3f more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63410
diff changeset
   226
    le_multiset_plus_left_nonempty: "M \<noteq> {#} \<Longrightarrow> N < M + N" and
f01d1e393f3f more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63410
diff changeset
   227
    le_multiset_plus_right_nonempty: "N \<noteq> {#} \<Longrightarrow> M < M + N"
f01d1e393f3f more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63410
diff changeset
   228
    by simp_all
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   229
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   230
end
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   231
65546
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   232
lemma all_lt_Max_imp_lt_mset: "N \<noteq> {#} \<Longrightarrow> (\<forall>a \<in># M. a < Max (set_mset N)) \<Longrightarrow> M < N"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   233
  by (meson Max_in[OF finite_set_mset] ex_gt_imp_less_multiset set_mset_eq_empty_iff)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   234
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   235
lemma lt_imp_ex_count_lt: "M < N \<Longrightarrow> \<exists>y. count M y < count N y"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   236
  by (meson less_eq_multiset\<^sub>H\<^sub>O less_le_not_le)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   237
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   238
lemma subset_imp_less_mset: "A \<subset># B \<Longrightarrow> A < B"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   239
  by (simp add: order.not_eq_order_implies_strict subset_eq_imp_le_multiset)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   240
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   241
lemma image_mset_strict_mono:
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   242
  assumes
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   243
    mono_f: "\<forall>x \<in> set_mset M. \<forall>y \<in> set_mset N. x < y \<longrightarrow> f x < f y" and
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   244
    less: "M < N"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   245
  shows "image_mset f M < image_mset f N"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   246
proof -
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   247
  obtain Y X where
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   248
    y_nemp: "Y \<noteq> {#}" and y_sub_N: "Y \<subseteq># N" and M_eq: "M = N - Y + X" and
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   249
    ex_y: "\<forall>x. x \<in># X \<longrightarrow> (\<exists>y. y \<in># Y \<and> x < y)"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   250
    using less[unfolded less_multiset\<^sub>D\<^sub>M] by blast
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   251
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   252
  have x_sub_M: "X \<subseteq># M"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   253
    using M_eq by simp
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   254
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   255
  let ?fY = "image_mset f Y"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   256
  let ?fX = "image_mset f X"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   257
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   258
  show ?thesis
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   259
    unfolding less_multiset\<^sub>D\<^sub>M
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   260
  proof (intro exI conjI)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   261
    show "image_mset f M = image_mset f N - ?fY + ?fX"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   262
      using M_eq[THEN arg_cong, of "image_mset f"] y_sub_N
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   263
      by (metis image_mset_Diff image_mset_union)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   264
  next
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   265
    obtain y where y: "\<forall>x. x \<in># X \<longrightarrow> y x \<in># Y \<and> x < y x"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   266
      using ex_y by moura
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   267
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   268
    show "\<forall>fx. fx \<in># ?fX \<longrightarrow> (\<exists>fy. fy \<in># ?fY \<and> fx < fy)"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   269
    proof (intro allI impI)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   270
      fix fx
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   271
      assume "fx \<in># ?fX"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   272
      then obtain x where fx: "fx = f x" and x_in: "x \<in># X"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   273
        by auto
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   274
      hence y_in: "y x \<in># Y" and y_gt: "x < y x"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   275
        using y[rule_format, OF x_in] by blast+
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   276
      hence "f (y x) \<in># ?fY \<and> f x < f (y x)"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   277
        using mono_f y_sub_N x_sub_M x_in
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   278
        by (metis image_eqI in_image_mset mset_subset_eqD)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   279
      thus "\<exists>fy. fy \<in># ?fY \<and> fx < fy"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   280
        unfolding fx by auto
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   281
    qed
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   282
  qed (auto simp: y_nemp y_sub_N image_mset_subseteq_mono)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   283
qed
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   284
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   285
lemma image_mset_mono:
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   286
  assumes
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   287
    mono_f: "\<forall>x \<in> set_mset M. \<forall>y \<in> set_mset N. x < y \<longrightarrow> f x < f y" and
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   288
    less: "M \<le> N"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   289
  shows "image_mset f M \<le> image_mset f N"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   290
  by (metis eq_iff image_mset_strict_mono less less_imp_le mono_f order.not_eq_order_implies_strict)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   291
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   292
lemma mset_lt_single_right_iff[simp]: "M < {#y#} \<longleftrightarrow> (\<forall>x \<in># M. x < y)" for y :: "'a::linorder"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   293
proof (rule iffI)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   294
  assume M_lt_y: "M < {#y#}"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   295
  show "\<forall>x \<in># M. x < y"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   296
  proof
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   297
    fix x
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   298
    assume x_in: "x \<in># M"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   299
    hence M: "M - {#x#} + {#x#} = M"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   300
      by (meson insert_DiffM2)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   301
    hence "\<not> {#x#} < {#y#} \<Longrightarrow> x < y"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   302
      using x_in M_lt_y
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   303
      by (metis diff_single_eq_union le_multiset_empty_left less_add_same_cancel2 mset_le_trans)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   304
    also have "\<not> {#y#} < M"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   305
      using M_lt_y mset_le_not_sym by blast
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   306
    ultimately show "x < y"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   307
      by (metis (no_types) Max_ge all_lt_Max_imp_lt_mset empty_iff finite_set_mset insertE
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   308
        less_le_trans linorder_less_linear mset_le_not_sym set_mset_add_mset_insert
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   309
        set_mset_eq_empty_iff x_in)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   310
  qed
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   311
next
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   312
  assume y_max: "\<forall>x \<in># M. x < y"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   313
  show "M < {#y#}"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   314
    by (rule all_lt_Max_imp_lt_mset) (auto intro!: y_max)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   315
qed
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   316
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   317
lemma mset_le_single_right_iff[simp]:
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   318
  "M \<le> {#y#} \<longleftrightarrow> M = {#y#} \<or> (\<forall>x \<in># M. x < y)" for y :: "'a::linorder"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   319
  by (meson less_eq_multiset_def mset_lt_single_right_iff)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   320
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   321
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   322
subsection \<open>Simprocs\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   323
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   324
lemma mset_le_add_iff1:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   325
  "j \<le> (i::nat) \<Longrightarrow> (repeat_mset i u + m \<le> repeat_mset j u + n) = (repeat_mset (i-j) u + m \<le> n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   326
proof -
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   327
  assume "j \<le> i"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   328
  then have "j + (i - j) = i"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   329
    using le_add_diff_inverse by blast
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   330
  then show ?thesis
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   331
    by (metis (no_types) add_le_cancel_left left_add_mult_distrib_mset)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   332
qed
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   333
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   334
lemma mset_le_add_iff2:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   335
  "i \<le> (j::nat) \<Longrightarrow> (repeat_mset i u + m \<le> repeat_mset j u + n) = (m \<le> repeat_mset (j-i) u + n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   336
proof -
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   337
  assume "i \<le> j"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   338
  then have "i + (j - i) = j"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   339
    using le_add_diff_inverse by blast
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   340
  then show ?thesis
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   341
    by (metis (no_types) add_le_cancel_left left_add_mult_distrib_mset)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   342
qed
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   343
65027
2b8583507891 renaming multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64978
diff changeset
   344
simproc_setup msetless_cancel
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   345
  ("(l::'a::preorder multiset) + m < n" | "(l::'a multiset) < m + n" |
65028
87e003397834 adding simplification patterns to multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65027
diff changeset
   346
   "add_mset a m < n" | "m < add_mset a n" |
87e003397834 adding simplification patterns to multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65027
diff changeset
   347
   "replicate_mset p a < n" | "m < replicate_mset p a" |
87e003397834 adding simplification patterns to multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65027
diff changeset
   348
   "repeat_mset p m < n" | "m < repeat_mset p n") =
65031
52e2c99f3711 use the cancellation simprocs directly
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65028
diff changeset
   349
  \<open>fn phi => Cancel_Simprocs.less_cancel\<close>
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   350
65027
2b8583507891 renaming multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64978
diff changeset
   351
simproc_setup msetle_cancel
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   352
  ("(l::'a::preorder multiset) + m \<le> n" | "(l::'a multiset) \<le> m + n" |
65028
87e003397834 adding simplification patterns to multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65027
diff changeset
   353
   "add_mset a m \<le> n" | "m \<le> add_mset a n" |
87e003397834 adding simplification patterns to multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65027
diff changeset
   354
   "replicate_mset p a \<le> n" | "m \<le> replicate_mset p a" |
87e003397834 adding simplification patterns to multiset simprocs
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65027
diff changeset
   355
   "repeat_mset p m \<le> n" | "m \<le> repeat_mset p n") =
65031
52e2c99f3711 use the cancellation simprocs directly
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 65028
diff changeset
   356
  \<open>fn phi => Cancel_Simprocs.less_eq_cancel\<close>
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   357
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   358
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   359
subsection \<open>Additional facts and instantiations\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63525
diff changeset
   360
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   361
lemma ex_gt_count_imp_le_multiset:
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   362
  "(\<forall>y :: 'a :: order. 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
   363
  unfolding less_multiset\<^sub>H\<^sub>O
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   364
  by (metis count_greater_zero_iff le_imp_less_or_eq less_imp_not_less not_gr_zero union_iff)
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   365
64418
91eae3a1be51 more lemmas
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64076
diff changeset
   366
lemma mset_lt_single_iff[iff]: "{#x#} < {#y#} \<longleftrightarrow> x < y"
91eae3a1be51 more lemmas
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64076
diff changeset
   367
  unfolding less_multiset\<^sub>H\<^sub>O by simp
91eae3a1be51 more lemmas
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64076
diff changeset
   368
91eae3a1be51 more lemmas
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64076
diff changeset
   369
lemma mset_le_single_iff[iff]: "{#x#} \<le> {#y#} \<longleftrightarrow> x \<le> y" for x y :: "'a::order"
91eae3a1be51 more lemmas
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64076
diff changeset
   370
  unfolding less_eq_multiset\<^sub>H\<^sub>O by force
91eae3a1be51 more lemmas
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 64076
diff changeset
   371
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   372
instance multiset :: (linorder) linordered_cancel_ab_semigroup_add
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   373
  by standard (metis less_eq_multiset\<^sub>H\<^sub>O not_less_iff_gr_or_eq)
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   374
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   375
lemma less_eq_multiset_total:
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   376
  fixes M N :: "'a :: linorder multiset"
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   377
  shows "\<not> M \<le> N \<Longrightarrow> N \<le> M"
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   378
  by simp
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   379
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   380
instantiation multiset :: (wellorder) wellorder
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   381
begin
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   382
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   383
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
   384
  unfolding less_multiset_def by (auto intro: wf_mult wf)
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   385
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   386
instance by standard (metis less_multiset_def wf wf_def wf_mult)
59813
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   387
6320064f22bb more multiset theorems
blanchet
parents:
diff changeset
   388
end
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   389
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   390
instantiation multiset :: (preorder) order_bot
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   391
begin
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   392
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   393
definition bot_multiset :: "'a multiset" where "bot_multiset = {#}"
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   394
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   395
instance by standard (simp add: bot_multiset_def)
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   396
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63407
diff changeset
   397
end
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   398
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   399
instance multiset :: (preorder) no_top
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   400
proof standard
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   401
  fix x :: "'a multiset"
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   402
  obtain a :: 'a where True by simp
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   403
  have "x < x + (x + {#a#})"
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   404
    by simp
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   405
  then show "\<exists>y. x < y"
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   406
    by blast
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   407
qed
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   408
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   409
instance multiset :: (preorder) ordered_cancel_comm_monoid_add
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   410
  by standard
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   411
65546
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   412
instantiation multiset :: (linorder) distrib_lattice
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   413
begin
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   414
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   415
definition inf_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   416
  "inf_multiset A B = (if A < B then A else B)"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   417
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   418
definition sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   419
  "sup_multiset A B = (if B > A then B else A)"
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   420
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   421
instance
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   422
  by intro_classes (auto simp: inf_multiset_def sup_multiset_def)
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   423
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
   424
end
65546
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   425
7c58f69451b0 moved lemmas from AFP to Isabelle
blanchet
parents: 65031
diff changeset
   426
end