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