src/HOL/Orderings.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66936 cf8d8fc23891
child 67091 1393c2340eec
permissions -rw-r--r--
more robust sorted_entries;
haftmann@28685
     1
(*  Title:      HOL/Orderings.thy
nipkow@15524
     2
    Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
nipkow@15524
     3
*)
nipkow@15524
     4
wenzelm@60758
     5
section \<open>Abstract orderings\<close>
nipkow@15524
     6
nipkow@15524
     7
theory Orderings
haftmann@35301
     8
imports HOL
wenzelm@46950
     9
keywords "print_orders" :: diag
nipkow@15524
    10
begin
nipkow@15524
    11
wenzelm@48891
    12
ML_file "~~/src/Provers/order.ML"
wenzelm@48891
    13
ML_file "~~/src/Provers/quasi.ML"  (* FIXME unused? *)
wenzelm@48891
    14
wenzelm@60758
    15
subsection \<open>Abstract ordering\<close>
haftmann@51487
    16
haftmann@51487
    17
locale ordering =
haftmann@63290
    18
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<^bold>\<le>" 50)
haftmann@63290
    19
   and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<^bold><" 50)
haftmann@63290
    20
  assumes strict_iff_order: "a \<^bold>< b \<longleftrightarrow> a \<^bold>\<le> b \<and> a \<noteq> b"
haftmann@63290
    21
  assumes refl: "a \<^bold>\<le> a" \<comment> \<open>not \<open>iff\<close>: makes problems due to multiple (dual) interpretations\<close>
haftmann@63290
    22
    and antisym: "a \<^bold>\<le> b \<Longrightarrow> b \<^bold>\<le> a \<Longrightarrow> a = b"
haftmann@63290
    23
    and trans: "a \<^bold>\<le> b \<Longrightarrow> b \<^bold>\<le> c \<Longrightarrow> a \<^bold>\<le> c"
haftmann@51487
    24
begin
haftmann@51487
    25
haftmann@51487
    26
lemma strict_implies_order:
haftmann@63290
    27
  "a \<^bold>< b \<Longrightarrow> a \<^bold>\<le> b"
haftmann@51487
    28
  by (simp add: strict_iff_order)
haftmann@51487
    29
haftmann@51487
    30
lemma strict_implies_not_eq:
haftmann@63290
    31
  "a \<^bold>< b \<Longrightarrow> a \<noteq> b"
haftmann@51487
    32
  by (simp add: strict_iff_order)
haftmann@51487
    33
haftmann@51487
    34
lemma not_eq_order_implies_strict:
haftmann@63290
    35
  "a \<noteq> b \<Longrightarrow> a \<^bold>\<le> b \<Longrightarrow> a \<^bold>< b"
haftmann@51487
    36
  by (simp add: strict_iff_order)
haftmann@51487
    37
haftmann@51487
    38
lemma order_iff_strict:
haftmann@63290
    39
  "a \<^bold>\<le> b \<longleftrightarrow> a \<^bold>< b \<or> a = b"
haftmann@51487
    40
  by (auto simp add: strict_iff_order refl)
haftmann@51487
    41
wenzelm@61799
    42
lemma irrefl: \<comment> \<open>not \<open>iff\<close>: makes problems due to multiple (dual) interpretations\<close>
haftmann@63290
    43
  "\<not> a \<^bold>< a"
haftmann@51487
    44
  by (simp add: strict_iff_order)
haftmann@51487
    45
haftmann@51487
    46
lemma asym:
haftmann@63290
    47
  "a \<^bold>< b \<Longrightarrow> b \<^bold>< a \<Longrightarrow> False"
haftmann@51487
    48
  by (auto simp add: strict_iff_order intro: antisym)
haftmann@51487
    49
haftmann@51487
    50
lemma strict_trans1:
haftmann@63290
    51
  "a \<^bold>\<le> b \<Longrightarrow> b \<^bold>< c \<Longrightarrow> a \<^bold>< c"
haftmann@51487
    52
  by (auto simp add: strict_iff_order intro: trans antisym)
haftmann@51487
    53
haftmann@51487
    54
lemma strict_trans2:
haftmann@63290
    55
  "a \<^bold>< b \<Longrightarrow> b \<^bold>\<le> c \<Longrightarrow> a \<^bold>< c"
haftmann@51487
    56
  by (auto simp add: strict_iff_order intro: trans antisym)
haftmann@51487
    57
haftmann@51487
    58
lemma strict_trans:
haftmann@63290
    59
  "a \<^bold>< b \<Longrightarrow> b \<^bold>< c \<Longrightarrow> a \<^bold>< c"
haftmann@51487
    60
  by (auto intro: strict_trans1 strict_implies_order)
haftmann@51487
    61
haftmann@51487
    62
end
haftmann@51487
    63
haftmann@63819
    64
text \<open>Alternative introduction rule with bias towards strict order\<close>
haftmann@63819
    65
haftmann@63819
    66
lemma ordering_strictI:
haftmann@63819
    67
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
    68
    and less (infix "\<^bold><" 50)
haftmann@63819
    69
  assumes less_eq_less: "\<And>a b. a \<^bold>\<le> b \<longleftrightarrow> a \<^bold>< b \<or> a = b"
haftmann@63819
    70
    assumes asym: "\<And>a b. a \<^bold>< b \<Longrightarrow> \<not> b \<^bold>< a"
haftmann@63819
    71
  assumes irrefl: "\<And>a. \<not> a \<^bold>< a"
haftmann@63819
    72
  assumes trans: "\<And>a b c. a \<^bold>< b \<Longrightarrow> b \<^bold>< c \<Longrightarrow> a \<^bold>< c"
haftmann@63819
    73
  shows "ordering less_eq less"
haftmann@63819
    74
proof
haftmann@63819
    75
  fix a b
haftmann@63819
    76
  show "a \<^bold>< b \<longleftrightarrow> a \<^bold>\<le> b \<and> a \<noteq> b"
haftmann@63819
    77
    by (auto simp add: less_eq_less asym irrefl)
haftmann@63819
    78
next
haftmann@63819
    79
  fix a
haftmann@63819
    80
  show "a \<^bold>\<le> a"
haftmann@63819
    81
    by (auto simp add: less_eq_less)
haftmann@63819
    82
next
haftmann@63819
    83
  fix a b c
haftmann@63819
    84
  assume "a \<^bold>\<le> b" and "b \<^bold>\<le> c" then show "a \<^bold>\<le> c"
haftmann@63819
    85
    by (auto simp add: less_eq_less intro: trans)
haftmann@63819
    86
next
haftmann@63819
    87
  fix a b
haftmann@63819
    88
  assume "a \<^bold>\<le> b" and "b \<^bold>\<le> a" then show "a = b"
haftmann@63819
    89
    by (auto simp add: less_eq_less asym)
haftmann@63819
    90
qed
haftmann@63819
    91
haftmann@63819
    92
lemma ordering_dualI:
haftmann@63819
    93
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
    94
    and less (infix "\<^bold><" 50)
haftmann@63819
    95
  assumes "ordering (\<lambda>a b. b \<^bold>\<le> a) (\<lambda>a b. b \<^bold>< a)"
haftmann@63819
    96
  shows "ordering less_eq less"
haftmann@63819
    97
proof -
haftmann@63819
    98
  from assms interpret ordering "\<lambda>a b. b \<^bold>\<le> a" "\<lambda>a b. b \<^bold>< a" .
haftmann@63819
    99
  show ?thesis
haftmann@63819
   100
    by standard (auto simp: strict_iff_order refl intro: antisym trans)
haftmann@63819
   101
qed
haftmann@63819
   102
haftmann@51487
   103
locale ordering_top = ordering +
haftmann@63290
   104
  fixes top :: "'a"  ("\<^bold>\<top>")
haftmann@63290
   105
  assumes extremum [simp]: "a \<^bold>\<le> \<^bold>\<top>"
haftmann@51487
   106
begin
haftmann@51487
   107
haftmann@51487
   108
lemma extremum_uniqueI:
haftmann@63290
   109
  "\<^bold>\<top> \<^bold>\<le> a \<Longrightarrow> a = \<^bold>\<top>"
haftmann@51487
   110
  by (rule antisym) auto
haftmann@51487
   111
haftmann@51487
   112
lemma extremum_unique:
haftmann@63290
   113
  "\<^bold>\<top> \<^bold>\<le> a \<longleftrightarrow> a = \<^bold>\<top>"
haftmann@51487
   114
  by (auto intro: antisym)
haftmann@51487
   115
haftmann@51487
   116
lemma extremum_strict [simp]:
haftmann@63290
   117
  "\<not> (\<^bold>\<top> \<^bold>< a)"
haftmann@51487
   118
  using extremum [of a] by (auto simp add: order_iff_strict intro: asym irrefl)
haftmann@51487
   119
haftmann@51487
   120
lemma not_eq_extremum:
haftmann@63290
   121
  "a \<noteq> \<^bold>\<top> \<longleftrightarrow> a \<^bold>< \<^bold>\<top>"
haftmann@51487
   122
  by (auto simp add: order_iff_strict intro: not_eq_order_implies_strict extremum)
haftmann@51487
   123
lp15@61824
   124
end
haftmann@51487
   125
haftmann@51487
   126
wenzelm@60758
   127
subsection \<open>Syntactic orders\<close>
haftmann@35092
   128
haftmann@35092
   129
class ord =
haftmann@35092
   130
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@35092
   131
    and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@35092
   132
begin
haftmann@35092
   133
haftmann@35092
   134
notation
wenzelm@61955
   135
  less_eq  ("op \<le>") and
wenzelm@61955
   136
  less_eq  ("(_/ \<le> _)"  [51, 51] 50) and
haftmann@35092
   137
  less  ("op <") and
haftmann@35092
   138
  less  ("(_/ < _)"  [51, 51] 50)
lp15@61824
   139
wenzelm@61955
   140
abbreviation (input)
wenzelm@61955
   141
  greater_eq  (infix "\<ge>" 50)
wenzelm@61955
   142
  where "x \<ge> y \<equiv> y \<le> x"
haftmann@35092
   143
haftmann@35092
   144
abbreviation (input)
wenzelm@61955
   145
  greater  (infix ">" 50)
wenzelm@61955
   146
  where "x > y \<equiv> y < x"
wenzelm@61955
   147
wenzelm@61955
   148
notation (ASCII)
wenzelm@61955
   149
  less_eq  ("op <=") and
wenzelm@61955
   150
  less_eq  ("(_/ <= _)" [51, 51] 50)
haftmann@35092
   151
haftmann@35092
   152
notation (input)
wenzelm@61955
   153
  greater_eq  (infix ">=" 50)
haftmann@35092
   154
haftmann@35092
   155
end
haftmann@35092
   156
haftmann@35092
   157
wenzelm@60758
   158
subsection \<open>Quasi orders\<close>
nipkow@15524
   159
haftmann@27682
   160
class preorder = ord +
haftmann@27682
   161
  assumes less_le_not_le: "x < y \<longleftrightarrow> x \<le> y \<and> \<not> (y \<le> x)"
haftmann@25062
   162
  and order_refl [iff]: "x \<le> x"
haftmann@25062
   163
  and order_trans: "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
haftmann@21248
   164
begin
haftmann@21248
   165
wenzelm@60758
   166
text \<open>Reflexivity.\<close>
nipkow@15524
   167
haftmann@25062
   168
lemma eq_refl: "x = y \<Longrightarrow> x \<le> y"
wenzelm@61799
   169
    \<comment> \<open>This form is useful with the classical reasoner.\<close>
nipkow@23212
   170
by (erule ssubst) (rule order_refl)
nipkow@15524
   171
haftmann@25062
   172
lemma less_irrefl [iff]: "\<not> x < x"
haftmann@27682
   173
by (simp add: less_le_not_le)
haftmann@27682
   174
haftmann@27682
   175
lemma less_imp_le: "x < y \<Longrightarrow> x \<le> y"
wenzelm@63172
   176
by (simp add: less_le_not_le)
haftmann@27682
   177
haftmann@27682
   178
wenzelm@60758
   179
text \<open>Asymmetry.\<close>
haftmann@27682
   180
haftmann@27682
   181
lemma less_not_sym: "x < y \<Longrightarrow> \<not> (y < x)"
haftmann@27682
   182
by (simp add: less_le_not_le)
haftmann@27682
   183
haftmann@27682
   184
lemma less_asym: "x < y \<Longrightarrow> (\<not> P \<Longrightarrow> y < x) \<Longrightarrow> P"
haftmann@27682
   185
by (drule less_not_sym, erule contrapos_np) simp
haftmann@27682
   186
haftmann@27682
   187
wenzelm@60758
   188
text \<open>Transitivity.\<close>
haftmann@27682
   189
haftmann@27682
   190
lemma less_trans: "x < y \<Longrightarrow> y < z \<Longrightarrow> x < z"
lp15@61824
   191
by (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   192
haftmann@27682
   193
lemma le_less_trans: "x \<le> y \<Longrightarrow> y < z \<Longrightarrow> x < z"
lp15@61824
   194
by (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   195
haftmann@27682
   196
lemma less_le_trans: "x < y \<Longrightarrow> y \<le> z \<Longrightarrow> x < z"
lp15@61824
   197
by (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   198
haftmann@27682
   199
wenzelm@60758
   200
text \<open>Useful for simplification, but too risky to include by default.\<close>
haftmann@27682
   201
haftmann@27682
   202
lemma less_imp_not_less: "x < y \<Longrightarrow> (\<not> y < x) \<longleftrightarrow> True"
haftmann@27682
   203
by (blast elim: less_asym)
haftmann@27682
   204
haftmann@27682
   205
lemma less_imp_triv: "x < y \<Longrightarrow> (y < x \<longrightarrow> P) \<longleftrightarrow> True"
haftmann@27682
   206
by (blast elim: less_asym)
haftmann@27682
   207
haftmann@27682
   208
wenzelm@60758
   209
text \<open>Transitivity rules for calculational reasoning\<close>
haftmann@27682
   210
haftmann@27682
   211
lemma less_asym': "a < b \<Longrightarrow> b < a \<Longrightarrow> P"
haftmann@27682
   212
by (rule less_asym)
haftmann@27682
   213
haftmann@27682
   214
wenzelm@60758
   215
text \<open>Dual order\<close>
haftmann@27682
   216
haftmann@27682
   217
lemma dual_preorder:
haftmann@36635
   218
  "class.preorder (op \<ge>) (op >)"
haftmann@63819
   219
  by standard (auto simp add: less_le_not_le intro: order_trans)
haftmann@27682
   220
haftmann@27682
   221
end
haftmann@27682
   222
haftmann@27682
   223
wenzelm@60758
   224
subsection \<open>Partial orders\<close>
haftmann@27682
   225
haftmann@27682
   226
class order = preorder +
haftmann@27682
   227
  assumes antisym: "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
haftmann@27682
   228
begin
haftmann@27682
   229
haftmann@51487
   230
lemma less_le: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> y"
haftmann@51487
   231
  by (auto simp add: less_le_not_le intro: antisym)
haftmann@51487
   232
haftmann@63819
   233
sublocale order: ordering less_eq less + dual_order: ordering greater_eq greater
haftmann@63819
   234
proof -
haftmann@63819
   235
  interpret ordering less_eq less
haftmann@63819
   236
    by standard (auto intro: antisym order_trans simp add: less_le)
haftmann@63819
   237
  show "ordering less_eq less"
haftmann@63819
   238
    by (fact ordering_axioms)
haftmann@63819
   239
  then show "ordering greater_eq greater"
haftmann@63819
   240
    by (rule ordering_dualI)
haftmann@63819
   241
qed
haftmann@51487
   242
wenzelm@60758
   243
text \<open>Reflexivity.\<close>
nipkow@15524
   244
haftmann@25062
   245
lemma le_less: "x \<le> y \<longleftrightarrow> x < y \<or> x = y"
wenzelm@61799
   246
    \<comment> \<open>NOT suitable for iff, since it can cause PROOF FAILED.\<close>
haftmann@51546
   247
by (fact order.order_iff_strict)
nipkow@15524
   248
haftmann@25062
   249
lemma le_imp_less_or_eq: "x \<le> y \<Longrightarrow> x < y \<or> x = y"
wenzelm@63172
   250
by (simp add: less_le)
nipkow@15524
   251
haftmann@21329
   252
wenzelm@60758
   253
text \<open>Useful for simplification, but too risky to include by default.\<close>
haftmann@21329
   254
haftmann@25062
   255
lemma less_imp_not_eq: "x < y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
nipkow@23212
   256
by auto
haftmann@21329
   257
haftmann@25062
   258
lemma less_imp_not_eq2: "x < y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
nipkow@23212
   259
by auto
haftmann@21329
   260
haftmann@21329
   261
wenzelm@60758
   262
text \<open>Transitivity rules for calculational reasoning\<close>
haftmann@21329
   263
haftmann@25062
   264
lemma neq_le_trans: "a \<noteq> b \<Longrightarrow> a \<le> b \<Longrightarrow> a < b"
haftmann@51546
   265
by (fact order.not_eq_order_implies_strict)
haftmann@21329
   266
haftmann@25062
   267
lemma le_neq_trans: "a \<le> b \<Longrightarrow> a \<noteq> b \<Longrightarrow> a < b"
haftmann@51546
   268
by (rule order.not_eq_order_implies_strict)
haftmann@21329
   269
nipkow@15524
   270
wenzelm@60758
   271
text \<open>Asymmetry.\<close>
nipkow@15524
   272
haftmann@25062
   273
lemma eq_iff: "x = y \<longleftrightarrow> x \<le> y \<and> y \<le> x"
nipkow@23212
   274
by (blast intro: antisym)
nipkow@15524
   275
haftmann@25062
   276
lemma antisym_conv: "y \<le> x \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
nipkow@23212
   277
by (blast intro: antisym)
nipkow@15524
   278
haftmann@25062
   279
lemma less_imp_neq: "x < y \<Longrightarrow> x \<noteq> y"
haftmann@51546
   280
by (fact order.strict_implies_not_eq)
haftmann@21248
   281
haftmann@21083
   282
wenzelm@60758
   283
text \<open>Least value operator\<close>
haftmann@27107
   284
haftmann@27299
   285
definition (in ord)
haftmann@27107
   286
  Least :: "('a \<Rightarrow> bool) \<Rightarrow> 'a" (binder "LEAST " 10) where
haftmann@27107
   287
  "Least P = (THE x. P x \<and> (\<forall>y. P y \<longrightarrow> x \<le> y))"
haftmann@27107
   288
haftmann@27107
   289
lemma Least_equality:
haftmann@27107
   290
  assumes "P x"
haftmann@27107
   291
    and "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27107
   292
  shows "Least P = x"
haftmann@27107
   293
unfolding Least_def by (rule the_equality)
haftmann@27107
   294
  (blast intro: assms antisym)+
haftmann@27107
   295
haftmann@27107
   296
lemma LeastI2_order:
haftmann@27107
   297
  assumes "P x"
haftmann@27107
   298
    and "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27107
   299
    and "\<And>x. P x \<Longrightarrow> \<forall>y. P y \<longrightarrow> x \<le> y \<Longrightarrow> Q x"
haftmann@27107
   300
  shows "Q (Least P)"
haftmann@27107
   301
unfolding Least_def by (rule theI2)
haftmann@27107
   302
  (blast intro: assms antisym)+
haftmann@27107
   303
nipkow@65963
   304
text \<open>Greatest value operator\<close>
nipkow@65963
   305
nipkow@65963
   306
definition Greatest :: "('a \<Rightarrow> bool) \<Rightarrow> 'a" (binder "GREATEST " 10) where
nipkow@65963
   307
"Greatest P = (THE x. P x \<and> (\<forall>y. P y \<longrightarrow> x \<ge> y))"
nipkow@65963
   308
nipkow@65963
   309
lemma GreatestI2_order:
nipkow@65963
   310
  "\<lbrakk> P x;
nipkow@65963
   311
    \<And>y. P y \<Longrightarrow> x \<ge> y;
nipkow@65963
   312
    \<And>x. \<lbrakk> P x; \<forall>y. P y \<longrightarrow> x \<ge> y \<rbrakk> \<Longrightarrow> Q x \<rbrakk>
nipkow@65963
   313
  \<Longrightarrow> Q (Greatest P)"
nipkow@65963
   314
unfolding Greatest_def
nipkow@65963
   315
by (rule theI2) (blast intro: antisym)+
nipkow@65963
   316
nipkow@65963
   317
lemma Greatest_equality:
nipkow@65963
   318
  "\<lbrakk> P x;  \<And>y. P y \<Longrightarrow> x \<ge> y \<rbrakk> \<Longrightarrow> Greatest P = x"
nipkow@65963
   319
unfolding Greatest_def
nipkow@65963
   320
by (rule the_equality) (blast intro: antisym)+
nipkow@65963
   321
haftmann@21248
   322
end
nipkow@15524
   323
haftmann@63819
   324
lemma ordering_orderI:
haftmann@63819
   325
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
   326
    and less (infix "\<^bold><" 50)
haftmann@63819
   327
  assumes "ordering less_eq less"
haftmann@63819
   328
  shows "class.order less_eq less"
haftmann@63819
   329
proof -
haftmann@63819
   330
  from assms interpret ordering less_eq less .
haftmann@63819
   331
  show ?thesis
haftmann@63819
   332
    by standard (auto intro: antisym trans simp add: refl strict_iff_order)
haftmann@63819
   333
qed
haftmann@56545
   334
haftmann@56545
   335
lemma order_strictI:
haftmann@56545
   336
  fixes less (infix "\<sqsubset>" 50)
haftmann@56545
   337
    and less_eq (infix "\<sqsubseteq>" 50)
haftmann@63819
   338
  assumes "\<And>a b. a \<sqsubseteq> b \<longleftrightarrow> a \<sqsubset> b \<or> a = b"
haftmann@63819
   339
    assumes "\<And>a b. a \<sqsubset> b \<Longrightarrow> \<not> b \<sqsubset> a"
haftmann@63819
   340
  assumes "\<And>a. \<not> a \<sqsubset> a"
haftmann@63819
   341
  assumes "\<And>a b c. a \<sqsubset> b \<Longrightarrow> b \<sqsubset> c \<Longrightarrow> a \<sqsubset> c"
haftmann@56545
   342
  shows "class.order less_eq less"
haftmann@63819
   343
  by (rule ordering_orderI) (rule ordering_strictI, (fact assms)+)
haftmann@63819
   344
haftmann@63819
   345
context order
haftmann@63819
   346
begin
haftmann@63819
   347
haftmann@63819
   348
text \<open>Dual order\<close>
haftmann@63819
   349
haftmann@63819
   350
lemma dual_order:
haftmann@63819
   351
  "class.order (op \<ge>) (op >)"
haftmann@63819
   352
  using dual_order.ordering_axioms by (rule ordering_orderI)
haftmann@63819
   353
haftmann@63819
   354
end
haftmann@56545
   355
haftmann@56545
   356
wenzelm@60758
   357
subsection \<open>Linear (total) orders\<close>
haftmann@21329
   358
haftmann@22316
   359
class linorder = order +
haftmann@25207
   360
  assumes linear: "x \<le> y \<or> y \<le> x"
haftmann@21248
   361
begin
haftmann@21248
   362
haftmann@25062
   363
lemma less_linear: "x < y \<or> x = y \<or> y < x"
nipkow@23212
   364
unfolding less_le using less_le linear by blast
haftmann@21248
   365
haftmann@25062
   366
lemma le_less_linear: "x \<le> y \<or> y < x"
nipkow@23212
   367
by (simp add: le_less less_linear)
haftmann@21248
   368
haftmann@21248
   369
lemma le_cases [case_names le ge]:
haftmann@25062
   370
  "(x \<le> y \<Longrightarrow> P) \<Longrightarrow> (y \<le> x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   371
using linear by blast
haftmann@21248
   372
lp15@61762
   373
lemma (in linorder) le_cases3:
lp15@61762
   374
  "\<lbrakk>\<lbrakk>x \<le> y; y \<le> z\<rbrakk> \<Longrightarrow> P; \<lbrakk>y \<le> x; x \<le> z\<rbrakk> \<Longrightarrow> P; \<lbrakk>x \<le> z; z \<le> y\<rbrakk> \<Longrightarrow> P;
lp15@61762
   375
    \<lbrakk>z \<le> y; y \<le> x\<rbrakk> \<Longrightarrow> P; \<lbrakk>y \<le> z; z \<le> x\<rbrakk> \<Longrightarrow> P; \<lbrakk>z \<le> x; x \<le> y\<rbrakk> \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
lp15@61762
   376
by (blast intro: le_cases)
lp15@61762
   377
haftmann@22384
   378
lemma linorder_cases [case_names less equal greater]:
haftmann@25062
   379
  "(x < y \<Longrightarrow> P) \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> (y < x \<Longrightarrow> P) \<Longrightarrow> P"
nipkow@23212
   380
using less_linear by blast
haftmann@21248
   381
hoelzl@57447
   382
lemma linorder_wlog[case_names le sym]:
hoelzl@57447
   383
  "(\<And>a b. a \<le> b \<Longrightarrow> P a b) \<Longrightarrow> (\<And>a b. P b a \<Longrightarrow> P a b) \<Longrightarrow> P a b"
hoelzl@57447
   384
  by (cases rule: le_cases[of a b]) blast+
hoelzl@57447
   385
haftmann@25062
   386
lemma not_less: "\<not> x < y \<longleftrightarrow> y \<le> x"
nipkow@23212
   387
apply (simp add: less_le)
nipkow@23212
   388
using linear apply (blast intro: antisym)
nipkow@23212
   389
done
nipkow@23212
   390
nipkow@23212
   391
lemma not_less_iff_gr_or_eq:
haftmann@25062
   392
 "\<not>(x < y) \<longleftrightarrow> (x > y | x = y)"
nipkow@23212
   393
apply(simp add:not_less le_less)
nipkow@23212
   394
apply blast
nipkow@23212
   395
done
nipkow@15524
   396
haftmann@25062
   397
lemma not_le: "\<not> x \<le> y \<longleftrightarrow> y < x"
nipkow@23212
   398
apply (simp add: less_le)
nipkow@23212
   399
using linear apply (blast intro: antisym)
nipkow@23212
   400
done
nipkow@15524
   401
haftmann@25062
   402
lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x < y \<or> y < x"
nipkow@23212
   403
by (cut_tac x = x and y = y in less_linear, auto)
nipkow@15524
   404
haftmann@25062
   405
lemma neqE: "x \<noteq> y \<Longrightarrow> (x < y \<Longrightarrow> R) \<Longrightarrow> (y < x \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23212
   406
by (simp add: neq_iff) blast
nipkow@15524
   407
haftmann@25062
   408
lemma antisym_conv1: "\<not> x < y \<Longrightarrow> x \<le> y \<longleftrightarrow> x = y"
nipkow@23212
   409
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   410
haftmann@25062
   411
lemma antisym_conv2: "x \<le> y \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
nipkow@23212
   412
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   413
haftmann@25062
   414
lemma antisym_conv3: "\<not> y < x \<Longrightarrow> \<not> x < y \<longleftrightarrow> x = y"
nipkow@23212
   415
by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   416
haftmann@25062
   417
lemma leI: "\<not> x < y \<Longrightarrow> y \<le> x"
nipkow@23212
   418
unfolding not_less .
paulson@16796
   419
haftmann@25062
   420
lemma leD: "y \<le> x \<Longrightarrow> \<not> x < y"
nipkow@23212
   421
unfolding not_less .
paulson@16796
   422
lp15@61824
   423
lemma not_le_imp_less: "\<not> y \<le> x \<Longrightarrow> x < y"
nipkow@23212
   424
unfolding not_le .
haftmann@21248
   425
lp15@64758
   426
lemma linorder_less_wlog[case_names less refl sym]:
lp15@64758
   427
     "\<lbrakk>\<And>a b. a < b \<Longrightarrow> P a b;  \<And>a. P a a;  \<And>a b. P b a \<Longrightarrow> P a b\<rbrakk> \<Longrightarrow> P a b"
lp15@64758
   428
  using antisym_conv3 by blast
lp15@64758
   429
wenzelm@60758
   430
text \<open>Dual order\<close>
haftmann@22916
   431
haftmann@26014
   432
lemma dual_linorder:
haftmann@36635
   433
  "class.linorder (op \<ge>) (op >)"
haftmann@36635
   434
by (rule class.linorder.intro, rule dual_order) (unfold_locales, rule linear)
haftmann@22916
   435
haftmann@21248
   436
end
haftmann@21248
   437
haftmann@23948
   438
wenzelm@60758
   439
text \<open>Alternative introduction rule with bias towards strict order\<close>
haftmann@56545
   440
haftmann@56545
   441
lemma linorder_strictI:
haftmann@63819
   442
  fixes less_eq (infix "\<^bold>\<le>" 50)
haftmann@63819
   443
    and less (infix "\<^bold><" 50)
haftmann@56545
   444
  assumes "class.order less_eq less"
haftmann@63819
   445
  assumes trichotomy: "\<And>a b. a \<^bold>< b \<or> a = b \<or> b \<^bold>< a"
haftmann@56545
   446
  shows "class.linorder less_eq less"
haftmann@56545
   447
proof -
haftmann@56545
   448
  interpret order less_eq less
wenzelm@60758
   449
    by (fact \<open>class.order less_eq less\<close>)
haftmann@56545
   450
  show ?thesis
haftmann@56545
   451
  proof
haftmann@56545
   452
    fix a b
haftmann@63819
   453
    show "a \<^bold>\<le> b \<or> b \<^bold>\<le> a"
haftmann@56545
   454
      using trichotomy by (auto simp add: le_less)
haftmann@56545
   455
  qed
haftmann@56545
   456
qed
haftmann@56545
   457
haftmann@56545
   458
wenzelm@60758
   459
subsection \<open>Reasoning tools setup\<close>
haftmann@21083
   460
wenzelm@60758
   461
ML \<open>
ballarin@24641
   462
signature ORDERS =
ballarin@24641
   463
sig
ballarin@24641
   464
  val print_structures: Proof.context -> unit
wenzelm@32215
   465
  val order_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58826
   466
  val add_struct: string * term list -> string -> attribute
wenzelm@58826
   467
  val del_struct: string * term list -> attribute
ballarin@24641
   468
end;
haftmann@21091
   469
ballarin@24641
   470
structure Orders: ORDERS =
haftmann@21248
   471
struct
ballarin@24641
   472
wenzelm@56508
   473
(* context data *)
ballarin@24641
   474
ballarin@24641
   475
fun struct_eq ((s1: string, ts1), (s2, ts2)) =
wenzelm@56508
   476
  s1 = s2 andalso eq_list (op aconv) (ts1, ts2);
ballarin@24641
   477
wenzelm@33519
   478
structure Data = Generic_Data
ballarin@24641
   479
(
ballarin@24641
   480
  type T = ((string * term list) * Order_Tac.less_arith) list;
ballarin@24641
   481
    (* Order structures:
ballarin@24641
   482
       identifier of the structure, list of operations and record of theorems
ballarin@24641
   483
       needed to set up the transitivity reasoner,
ballarin@24641
   484
       identifier and operations identify the structure uniquely. *)
ballarin@24641
   485
  val empty = [];
ballarin@24641
   486
  val extend = I;
wenzelm@33519
   487
  fun merge data = AList.join struct_eq (K fst) data;
ballarin@24641
   488
);
ballarin@24641
   489
ballarin@24641
   490
fun print_structures ctxt =
ballarin@24641
   491
  let
ballarin@24641
   492
    val structs = Data.get (Context.Proof ctxt);
ballarin@24641
   493
    fun pretty_term t = Pretty.block
wenzelm@24920
   494
      [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.brk 1,
ballarin@24641
   495
        Pretty.str "::", Pretty.brk 1,
wenzelm@24920
   496
        Pretty.quote (Syntax.pretty_typ ctxt (type_of t))];
ballarin@24641
   497
    fun pretty_struct ((s, ts), _) = Pretty.block
ballarin@24641
   498
      [Pretty.str s, Pretty.str ":", Pretty.brk 1,
ballarin@24641
   499
       Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
ballarin@24641
   500
  in
wenzelm@51579
   501
    Pretty.writeln (Pretty.big_list "order structures:" (map pretty_struct structs))
ballarin@24641
   502
  end;
ballarin@24641
   503
wenzelm@56508
   504
val _ =
wenzelm@59936
   505
  Outer_Syntax.command @{command_keyword print_orders}
wenzelm@56508
   506
    "print order structures available to transitivity reasoner"
wenzelm@60097
   507
    (Scan.succeed (Toplevel.keep (print_structures o Toplevel.context_of)));
haftmann@21091
   508
wenzelm@56508
   509
wenzelm@56508
   510
(* tactics *)
wenzelm@56508
   511
wenzelm@56508
   512
fun struct_tac ((s, ops), thms) ctxt facts =
ballarin@24641
   513
  let
wenzelm@56508
   514
    val [eq, le, less] = ops;
berghofe@30107
   515
    fun decomp thy (@{const Trueprop} $ t) =
wenzelm@56508
   516
          let
wenzelm@56508
   517
            fun excluded t =
wenzelm@56508
   518
              (* exclude numeric types: linear arithmetic subsumes transitivity *)
wenzelm@56508
   519
              let val T = type_of t
wenzelm@56508
   520
              in
wenzelm@56508
   521
                T = HOLogic.natT orelse T = HOLogic.intT orelse T = HOLogic.realT
wenzelm@56508
   522
              end;
wenzelm@56508
   523
            fun rel (bin_op $ t1 $ t2) =
wenzelm@56508
   524
                  if excluded t1 then NONE
wenzelm@56508
   525
                  else if Pattern.matches thy (eq, bin_op) then SOME (t1, "=", t2)
wenzelm@56508
   526
                  else if Pattern.matches thy (le, bin_op) then SOME (t1, "<=", t2)
wenzelm@56508
   527
                  else if Pattern.matches thy (less, bin_op) then SOME (t1, "<", t2)
wenzelm@56508
   528
                  else NONE
wenzelm@56508
   529
              | rel _ = NONE;
wenzelm@56508
   530
            fun dec (Const (@{const_name Not}, _) $ t) =
wenzelm@56508
   531
                  (case rel t of NONE =>
wenzelm@56508
   532
                    NONE
wenzelm@56508
   533
                  | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
wenzelm@56508
   534
              | dec x = rel x;
wenzelm@56508
   535
          in dec t end
wenzelm@56508
   536
      | decomp _ _ = NONE;
ballarin@24641
   537
  in
wenzelm@56508
   538
    (case s of
wenzelm@56508
   539
      "order" => Order_Tac.partial_tac decomp thms ctxt facts
wenzelm@56508
   540
    | "linorder" => Order_Tac.linear_tac decomp thms ctxt facts
wenzelm@56508
   541
    | _ => error ("Unknown order kind " ^ quote s ^ " encountered in transitivity reasoner"))
ballarin@24641
   542
  end
ballarin@24641
   543
wenzelm@56508
   544
fun order_tac ctxt facts =
wenzelm@56508
   545
  FIRST' (map (fn s => CHANGED o struct_tac s ctxt facts) (Data.get (Context.Proof ctxt)));
ballarin@24641
   546
ballarin@24641
   547
wenzelm@56508
   548
(* attributes *)
ballarin@24641
   549
wenzelm@58826
   550
fun add_struct s tag =
ballarin@24641
   551
  Thm.declaration_attribute
ballarin@24641
   552
    (fn thm => Data.map (AList.map_default struct_eq (s, Order_Tac.empty TrueI) (Order_Tac.update tag thm)));
ballarin@24641
   553
fun del_struct s =
ballarin@24641
   554
  Thm.declaration_attribute
ballarin@24641
   555
    (fn _ => Data.map (AList.delete struct_eq s));
ballarin@24641
   556
haftmann@21091
   557
end;
wenzelm@60758
   558
\<close>
haftmann@21091
   559
wenzelm@60758
   560
attribute_setup order = \<open>
wenzelm@58826
   561
  Scan.lift ((Args.add -- Args.name >> (fn (_, s) => SOME s) || Args.del >> K NONE) --|
wenzelm@58826
   562
    Args.colon (* FIXME || Scan.succeed true *) ) -- Scan.lift Args.name --
wenzelm@58826
   563
    Scan.repeat Args.term
wenzelm@58826
   564
    >> (fn ((SOME tag, n), ts) => Orders.add_struct (n, ts) tag
wenzelm@58826
   565
         | ((NONE, n), ts) => Orders.del_struct (n, ts))
wenzelm@60758
   566
\<close> "theorems controlling transitivity reasoner"
wenzelm@58826
   567
wenzelm@60758
   568
method_setup order = \<open>
wenzelm@47432
   569
  Scan.succeed (fn ctxt => SIMPLE_METHOD' (Orders.order_tac ctxt []))
wenzelm@60758
   570
\<close> "transitivity reasoner"
ballarin@24641
   571
ballarin@24641
   572
wenzelm@60758
   573
text \<open>Declarations to set up transitivity reasoner of partial and linear orders.\<close>
ballarin@24641
   574
haftmann@25076
   575
context order
haftmann@25076
   576
begin
haftmann@25076
   577
ballarin@24641
   578
(* The type constraint on @{term op =} below is necessary since the operation
ballarin@24641
   579
   is not a parameter of the locale. *)
haftmann@25076
   580
haftmann@27689
   581
declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"]
lp15@61824
   582
haftmann@27689
   583
declare order_refl  [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   584
haftmann@27689
   585
declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   586
haftmann@27689
   587
declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   588
haftmann@27689
   589
declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   590
haftmann@27689
   591
declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   592
haftmann@27689
   593
declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   594
haftmann@27689
   595
declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
lp15@61824
   596
haftmann@27689
   597
declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   598
haftmann@27689
   599
declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   600
haftmann@27689
   601
declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   602
haftmann@27689
   603
declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   604
haftmann@27689
   605
declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   606
haftmann@27689
   607
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   608
haftmann@27689
   609
declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   610
haftmann@25076
   611
end
haftmann@25076
   612
haftmann@25076
   613
context linorder
haftmann@25076
   614
begin
ballarin@24641
   615
haftmann@27689
   616
declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]]
haftmann@27689
   617
haftmann@27689
   618
declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   619
haftmann@27689
   620
declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   621
haftmann@27689
   622
declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   623
haftmann@27689
   624
declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   625
haftmann@27689
   626
declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   627
haftmann@27689
   628
declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   629
haftmann@27689
   630
declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   631
haftmann@27689
   632
declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   633
haftmann@27689
   634
declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@25076
   635
haftmann@27689
   636
declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   637
haftmann@27689
   638
declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   639
haftmann@27689
   640
declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   641
haftmann@27689
   642
declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   643
haftmann@27689
   644
declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   645
haftmann@27689
   646
declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   647
haftmann@27689
   648
declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   649
haftmann@27689
   650
declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   651
haftmann@27689
   652
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   653
haftmann@27689
   654
declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   655
haftmann@25076
   656
end
haftmann@25076
   657
wenzelm@60758
   658
setup \<open>
wenzelm@56509
   659
  map_theory_simpset (fn ctxt0 => ctxt0 addSolver
wenzelm@56509
   660
    mk_solver "Transitivity" (fn ctxt => Orders.order_tac ctxt (Simplifier.prems_of ctxt)))
wenzelm@56509
   661
  (*Adding the transitivity reasoners also as safe solvers showed a slight
wenzelm@56509
   662
    speed up, but the reasoning strength appears to be not higher (at least
wenzelm@56509
   663
    no breaking of additional proofs in the entire HOL distribution, as
wenzelm@56509
   664
    of 5 March 2004, was observed).*)
wenzelm@60758
   665
\<close>
nipkow@15524
   666
wenzelm@60758
   667
ML \<open>
wenzelm@56509
   668
local
wenzelm@56509
   669
  fun prp t thm = Thm.prop_of thm = t;  (* FIXME proper aconv!? *)
wenzelm@56509
   670
in
nipkow@15524
   671
wenzelm@56509
   672
fun antisym_le_simproc ctxt ct =
wenzelm@59582
   673
  (case Thm.term_of ct of
wenzelm@56509
   674
    (le as Const (_, T)) $ r $ s =>
wenzelm@56509
   675
     (let
wenzelm@56509
   676
        val prems = Simplifier.prems_of ctxt;
wenzelm@56509
   677
        val less = Const (@{const_name less}, T);
wenzelm@56509
   678
        val t = HOLogic.mk_Trueprop(le $ s $ r);
wenzelm@56509
   679
      in
wenzelm@56509
   680
        (case find_first (prp t) prems of
wenzelm@56509
   681
          NONE =>
wenzelm@56509
   682
            let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s)) in
wenzelm@56509
   683
              (case find_first (prp t) prems of
wenzelm@56509
   684
                NONE => NONE
wenzelm@56509
   685
              | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1})))
wenzelm@56509
   686
             end
wenzelm@56509
   687
         | SOME thm => SOME (mk_meta_eq (thm RS @{thm order_class.antisym_conv})))
wenzelm@56509
   688
      end handle THM _ => NONE)
wenzelm@56509
   689
  | _ => NONE);
nipkow@15524
   690
wenzelm@56509
   691
fun antisym_less_simproc ctxt ct =
wenzelm@59582
   692
  (case Thm.term_of ct of
wenzelm@56509
   693
    NotC $ ((less as Const(_,T)) $ r $ s) =>
wenzelm@56509
   694
     (let
wenzelm@56509
   695
       val prems = Simplifier.prems_of ctxt;
wenzelm@56509
   696
       val le = Const (@{const_name less_eq}, T);
wenzelm@56509
   697
       val t = HOLogic.mk_Trueprop(le $ r $ s);
wenzelm@56509
   698
      in
wenzelm@56509
   699
        (case find_first (prp t) prems of
wenzelm@56509
   700
          NONE =>
wenzelm@56509
   701
            let val t = HOLogic.mk_Trueprop (NotC $ (less $ s $ r)) in
wenzelm@56509
   702
              (case find_first (prp t) prems of
wenzelm@56509
   703
                NONE => NONE
wenzelm@56509
   704
              | SOME thm => SOME (mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3})))
wenzelm@56509
   705
            end
wenzelm@56509
   706
        | SOME thm => SOME (mk_meta_eq (thm RS @{thm linorder_class.antisym_conv2})))
wenzelm@56509
   707
      end handle THM _ => NONE)
wenzelm@56509
   708
  | _ => NONE);
haftmann@21083
   709
wenzelm@56509
   710
end;
wenzelm@60758
   711
\<close>
nipkow@15524
   712
wenzelm@56509
   713
simproc_setup antisym_le ("(x::'a::order) \<le> y") = "K antisym_le_simproc"
wenzelm@56509
   714
simproc_setup antisym_less ("\<not> (x::'a::linorder) < y") = "K antisym_less_simproc"
wenzelm@56509
   715
nipkow@15524
   716
wenzelm@60758
   717
subsection \<open>Bounded quantifiers\<close>
haftmann@21083
   718
wenzelm@61955
   719
syntax (ASCII)
wenzelm@21180
   720
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   721
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   722
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   723
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   724
wenzelm@21180
   725
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   726
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   727
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   728
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   729
wenzelm@61955
   730
syntax
wenzelm@21180
   731
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   732
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   733
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   734
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   735
wenzelm@21180
   736
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   737
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   738
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   739
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   740
wenzelm@62521
   741
syntax (input)
wenzelm@21180
   742
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   743
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   744
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   745
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   746
haftmann@21083
   747
translations
haftmann@21083
   748
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   749
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   750
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   751
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   752
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   753
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   754
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   755
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   756
wenzelm@60758
   757
print_translation \<open>
haftmann@21083
   758
let
wenzelm@42287
   759
  val All_binder = Mixfix.binder_name @{const_syntax All};
wenzelm@42287
   760
  val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
haftmann@38786
   761
  val impl = @{const_syntax HOL.implies};
haftmann@38795
   762
  val conj = @{const_syntax HOL.conj};
haftmann@22916
   763
  val less = @{const_syntax less};
haftmann@22916
   764
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   765
wenzelm@21180
   766
  val trans =
wenzelm@35115
   767
   [((All_binder, impl, less),
wenzelm@35115
   768
    (@{syntax_const "_All_less"}, @{syntax_const "_All_greater"})),
wenzelm@35115
   769
    ((All_binder, impl, less_eq),
wenzelm@35115
   770
    (@{syntax_const "_All_less_eq"}, @{syntax_const "_All_greater_eq"})),
wenzelm@35115
   771
    ((Ex_binder, conj, less),
wenzelm@35115
   772
    (@{syntax_const "_Ex_less"}, @{syntax_const "_Ex_greater"})),
wenzelm@35115
   773
    ((Ex_binder, conj, less_eq),
wenzelm@35115
   774
    (@{syntax_const "_Ex_less_eq"}, @{syntax_const "_Ex_greater_eq"}))];
wenzelm@21180
   775
wenzelm@35115
   776
  fun matches_bound v t =
wenzelm@35115
   777
    (case t of
wenzelm@35364
   778
      Const (@{syntax_const "_bound"}, _) $ Free (v', _) => v = v'
wenzelm@35115
   779
    | _ => false);
wenzelm@35115
   780
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false);
wenzelm@49660
   781
  fun mk x c n P = Syntax.const c $ Syntax_Trans.mark_bound_body x $ n $ P;
wenzelm@21180
   782
wenzelm@52143
   783
  fun tr' q = (q, fn _ =>
wenzelm@52143
   784
    (fn [Const (@{syntax_const "_bound"}, _) $ Free (v, T),
wenzelm@35364
   785
        Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@35115
   786
        (case AList.lookup (op =) trans (q, c, d) of
wenzelm@35115
   787
          NONE => raise Match
wenzelm@35115
   788
        | SOME (l, g) =>
wenzelm@49660
   789
            if matches_bound v t andalso not (contains_var v u) then mk (v, T) l u P
wenzelm@49660
   790
            else if matches_bound v u andalso not (contains_var v t) then mk (v, T) g t P
wenzelm@35115
   791
            else raise Match)
wenzelm@52143
   792
      | _ => raise Match));
wenzelm@21524
   793
in [tr' All_binder, tr' Ex_binder] end
wenzelm@60758
   794
\<close>
haftmann@21083
   795
haftmann@21083
   796
wenzelm@60758
   797
subsection \<open>Transitivity reasoning\<close>
haftmann@21383
   798
haftmann@25193
   799
context ord
haftmann@25193
   800
begin
haftmann@21383
   801
haftmann@25193
   802
lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
haftmann@25193
   803
  by (rule subst)
haftmann@21383
   804
haftmann@25193
   805
lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
haftmann@25193
   806
  by (rule ssubst)
haftmann@21383
   807
haftmann@25193
   808
lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
haftmann@25193
   809
  by (rule subst)
haftmann@25193
   810
haftmann@25193
   811
lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
haftmann@25193
   812
  by (rule ssubst)
haftmann@25193
   813
haftmann@25193
   814
end
haftmann@21383
   815
haftmann@21383
   816
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   817
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   818
proof -
haftmann@21383
   819
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   820
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   821
  also assume "f b < c"
haftmann@34250
   822
  finally (less_trans) show ?thesis .
haftmann@21383
   823
qed
haftmann@21383
   824
haftmann@21383
   825
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   826
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   827
proof -
haftmann@21383
   828
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   829
  assume "a < f b"
haftmann@21383
   830
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   831
  finally (less_trans) show ?thesis .
haftmann@21383
   832
qed
haftmann@21383
   833
haftmann@21383
   834
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   835
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   836
proof -
haftmann@21383
   837
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   838
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   839
  also assume "f b < c"
haftmann@34250
   840
  finally (le_less_trans) show ?thesis .
haftmann@21383
   841
qed
haftmann@21383
   842
haftmann@21383
   843
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   844
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   845
proof -
haftmann@21383
   846
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   847
  assume "a <= f b"
haftmann@21383
   848
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   849
  finally (le_less_trans) show ?thesis .
haftmann@21383
   850
qed
haftmann@21383
   851
haftmann@21383
   852
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   853
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   854
proof -
haftmann@21383
   855
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   856
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   857
  also assume "f b <= c"
haftmann@34250
   858
  finally (less_le_trans) show ?thesis .
haftmann@21383
   859
qed
haftmann@21383
   860
haftmann@21383
   861
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   862
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   863
proof -
haftmann@21383
   864
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   865
  assume "a < f b"
haftmann@21383
   866
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@34250
   867
  finally (less_le_trans) show ?thesis .
haftmann@21383
   868
qed
haftmann@21383
   869
haftmann@21383
   870
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   871
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   872
proof -
haftmann@21383
   873
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   874
  assume "a <= f b"
haftmann@21383
   875
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   876
  finally (order_trans) show ?thesis .
haftmann@21383
   877
qed
haftmann@21383
   878
haftmann@21383
   879
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   880
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   881
proof -
haftmann@21383
   882
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   883
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   884
  also assume "f b <= c"
haftmann@21383
   885
  finally (order_trans) show ?thesis .
haftmann@21383
   886
qed
haftmann@21383
   887
haftmann@21383
   888
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   889
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   890
proof -
haftmann@21383
   891
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   892
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   893
  also assume "f b = c"
haftmann@21383
   894
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   895
qed
haftmann@21383
   896
haftmann@21383
   897
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   898
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   899
proof -
haftmann@21383
   900
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   901
  assume "a = f b"
haftmann@21383
   902
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   903
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   904
qed
haftmann@21383
   905
haftmann@21383
   906
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   907
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   908
proof -
haftmann@21383
   909
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   910
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   911
  also assume "f b = c"
haftmann@21383
   912
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   913
qed
haftmann@21383
   914
haftmann@21383
   915
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   916
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   917
proof -
haftmann@21383
   918
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   919
  assume "a = f b"
haftmann@21383
   920
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   921
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   922
qed
haftmann@21383
   923
wenzelm@60758
   924
text \<open>
haftmann@21383
   925
  Note that this list of rules is in reverse order of priorities.
wenzelm@60758
   926
\<close>
haftmann@21383
   927
haftmann@27682
   928
lemmas [trans] =
haftmann@21383
   929
  order_less_subst2
haftmann@21383
   930
  order_less_subst1
haftmann@21383
   931
  order_le_less_subst2
haftmann@21383
   932
  order_le_less_subst1
haftmann@21383
   933
  order_less_le_subst2
haftmann@21383
   934
  order_less_le_subst1
haftmann@21383
   935
  order_subst2
haftmann@21383
   936
  order_subst1
haftmann@21383
   937
  ord_le_eq_subst
haftmann@21383
   938
  ord_eq_le_subst
haftmann@21383
   939
  ord_less_eq_subst
haftmann@21383
   940
  ord_eq_less_subst
haftmann@21383
   941
  forw_subst
haftmann@21383
   942
  back_subst
haftmann@21383
   943
  rev_mp
haftmann@21383
   944
  mp
haftmann@27682
   945
haftmann@27682
   946
lemmas (in order) [trans] =
haftmann@27682
   947
  neq_le_trans
haftmann@27682
   948
  le_neq_trans
haftmann@27682
   949
haftmann@27682
   950
lemmas (in preorder) [trans] =
haftmann@27682
   951
  less_trans
haftmann@27682
   952
  less_asym'
haftmann@27682
   953
  le_less_trans
haftmann@27682
   954
  less_le_trans
haftmann@21383
   955
  order_trans
haftmann@27682
   956
haftmann@27682
   957
lemmas (in order) [trans] =
haftmann@27682
   958
  antisym
haftmann@27682
   959
haftmann@27682
   960
lemmas (in ord) [trans] =
haftmann@27682
   961
  ord_le_eq_trans
haftmann@27682
   962
  ord_eq_le_trans
haftmann@27682
   963
  ord_less_eq_trans
haftmann@27682
   964
  ord_eq_less_trans
haftmann@27682
   965
haftmann@27682
   966
lemmas [trans] =
haftmann@27682
   967
  trans
haftmann@27682
   968
haftmann@27682
   969
lemmas order_trans_rules =
haftmann@27682
   970
  order_less_subst2
haftmann@27682
   971
  order_less_subst1
haftmann@27682
   972
  order_le_less_subst2
haftmann@27682
   973
  order_le_less_subst1
haftmann@27682
   974
  order_less_le_subst2
haftmann@27682
   975
  order_less_le_subst1
haftmann@27682
   976
  order_subst2
haftmann@27682
   977
  order_subst1
haftmann@27682
   978
  ord_le_eq_subst
haftmann@27682
   979
  ord_eq_le_subst
haftmann@27682
   980
  ord_less_eq_subst
haftmann@27682
   981
  ord_eq_less_subst
haftmann@27682
   982
  forw_subst
haftmann@27682
   983
  back_subst
haftmann@27682
   984
  rev_mp
haftmann@27682
   985
  mp
haftmann@27682
   986
  neq_le_trans
haftmann@27682
   987
  le_neq_trans
haftmann@27682
   988
  less_trans
haftmann@27682
   989
  less_asym'
haftmann@27682
   990
  le_less_trans
haftmann@27682
   991
  less_le_trans
haftmann@27682
   992
  order_trans
haftmann@27682
   993
  antisym
haftmann@21383
   994
  ord_le_eq_trans
haftmann@21383
   995
  ord_eq_le_trans
haftmann@21383
   996
  ord_less_eq_trans
haftmann@21383
   997
  ord_eq_less_trans
haftmann@21383
   998
  trans
haftmann@21383
   999
wenzelm@60758
  1000
text \<open>These support proving chains of decreasing inequalities
wenzelm@60758
  1001
    a >= b >= c ... in Isar proofs.\<close>
haftmann@21083
  1002
blanchet@45221
  1003
lemma xt1 [no_atp]:
haftmann@21083
  1004
  "a = b ==> b > c ==> a > c"
haftmann@21083
  1005
  "a > b ==> b = c ==> a > c"
haftmann@21083
  1006
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
  1007
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
  1008
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
  1009
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
  1010
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
  1011
  "(x::'a::order) >= y ==> y > z ==> x > z"
wenzelm@23417
  1012
  "(a::'a::order) > b ==> b > a ==> P"
haftmann@21083
  1013
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
  1014
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
  1015
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
lp15@61824
  1016
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
  1017
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1018
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
  1019
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@25076
  1020
  by auto
haftmann@21083
  1021
blanchet@45221
  1022
lemma xt2 [no_atp]:
haftmann@21083
  1023
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
  1024
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
  1025
blanchet@45221
  1026
lemma xt3 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==>
haftmann@21083
  1027
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
  1028
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
  1029
blanchet@45221
  1030
lemma xt4 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
  1031
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
  1032
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
  1033
blanchet@45221
  1034
lemma xt5 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
  1035
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1036
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
  1037
blanchet@45221
  1038
lemma xt6 [no_atp]: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
  1039
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
  1040
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
  1041
blanchet@45221
  1042
lemma xt7 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
  1043
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
  1044
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
  1045
blanchet@45221
  1046
lemma xt8 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
  1047
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
  1048
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
  1049
blanchet@45221
  1050
lemma xt9 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
  1051
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
  1052
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
  1053
blanchet@54147
  1054
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
  1055
lp15@61824
  1056
(*
haftmann@21083
  1057
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
  1058
  for the wrong thing in an Isar proof.
haftmann@21083
  1059
lp15@61824
  1060
  The extra transitivity rules can be used as follows:
haftmann@21083
  1061
haftmann@21083
  1062
lemma "(a::'a::order) > z"
haftmann@21083
  1063
proof -
haftmann@21083
  1064
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
  1065
    sorry
haftmann@21083
  1066
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
  1067
    sorry
haftmann@21083
  1068
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
  1069
    sorry
haftmann@21083
  1070
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
  1071
    sorry
haftmann@21083
  1072
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
  1073
    sorry
haftmann@21083
  1074
  also (xtrans) have "?rhs > z"
haftmann@21083
  1075
    sorry
haftmann@21083
  1076
  finally (xtrans) show ?thesis .
haftmann@21083
  1077
qed
haftmann@21083
  1078
haftmann@21083
  1079
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
  1080
  leave out the "(xtrans)" above.
haftmann@21083
  1081
*)
haftmann@21083
  1082
haftmann@23881
  1083
wenzelm@60758
  1084
subsection \<open>Monotonicity\<close>
haftmann@21083
  1085
haftmann@25076
  1086
context order
haftmann@25076
  1087
begin
haftmann@25076
  1088
wenzelm@61076
  1089
definition mono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
haftmann@25076
  1090
  "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
haftmann@25076
  1091
haftmann@25076
  1092
lemma monoI [intro?]:
wenzelm@61076
  1093
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@25076
  1094
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
haftmann@25076
  1095
  unfolding mono_def by iprover
haftmann@21216
  1096
haftmann@25076
  1097
lemma monoD [dest?]:
wenzelm@61076
  1098
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@25076
  1099
  shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
haftmann@25076
  1100
  unfolding mono_def by iprover
haftmann@25076
  1101
haftmann@51263
  1102
lemma monoE:
wenzelm@61076
  1103
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@51263
  1104
  assumes "mono f"
haftmann@51263
  1105
  assumes "x \<le> y"
haftmann@51263
  1106
  obtains "f x \<le> f y"
haftmann@51263
  1107
proof
haftmann@51263
  1108
  from assms show "f x \<le> f y" by (simp add: mono_def)
haftmann@51263
  1109
qed
haftmann@51263
  1110
wenzelm@61076
  1111
definition antimono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
hoelzl@56020
  1112
  "antimono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<ge> f y)"
hoelzl@56020
  1113
hoelzl@56020
  1114
lemma antimonoI [intro?]:
wenzelm@61076
  1115
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1116
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<ge> f y) \<Longrightarrow> antimono f"
hoelzl@56020
  1117
  unfolding antimono_def by iprover
hoelzl@56020
  1118
hoelzl@56020
  1119
lemma antimonoD [dest?]:
wenzelm@61076
  1120
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1121
  shows "antimono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<ge> f y"
hoelzl@56020
  1122
  unfolding antimono_def by iprover
hoelzl@56020
  1123
hoelzl@56020
  1124
lemma antimonoE:
wenzelm@61076
  1125
  fixes f :: "'a \<Rightarrow> 'b::order"
hoelzl@56020
  1126
  assumes "antimono f"
hoelzl@56020
  1127
  assumes "x \<le> y"
hoelzl@56020
  1128
  obtains "f x \<ge> f y"
hoelzl@56020
  1129
proof
hoelzl@56020
  1130
  from assms show "f x \<ge> f y" by (simp add: antimono_def)
hoelzl@56020
  1131
qed
hoelzl@56020
  1132
wenzelm@61076
  1133
definition strict_mono :: "('a \<Rightarrow> 'b::order) \<Rightarrow> bool" where
haftmann@30298
  1134
  "strict_mono f \<longleftrightarrow> (\<forall>x y. x < y \<longrightarrow> f x < f y)"
haftmann@30298
  1135
haftmann@30298
  1136
lemma strict_monoI [intro?]:
haftmann@30298
  1137
  assumes "\<And>x y. x < y \<Longrightarrow> f x < f y"
haftmann@30298
  1138
  shows "strict_mono f"
haftmann@30298
  1139
  using assms unfolding strict_mono_def by auto
haftmann@30298
  1140
haftmann@30298
  1141
lemma strict_monoD [dest?]:
haftmann@30298
  1142
  "strict_mono f \<Longrightarrow> x < y \<Longrightarrow> f x < f y"
haftmann@30298
  1143
  unfolding strict_mono_def by auto
haftmann@30298
  1144
haftmann@30298
  1145
lemma strict_mono_mono [dest?]:
haftmann@30298
  1146
  assumes "strict_mono f"
haftmann@30298
  1147
  shows "mono f"
haftmann@30298
  1148
proof (rule monoI)
haftmann@30298
  1149
  fix x y
haftmann@30298
  1150
  assume "x \<le> y"
haftmann@30298
  1151
  show "f x \<le> f y"
haftmann@30298
  1152
  proof (cases "x = y")
haftmann@30298
  1153
    case True then show ?thesis by simp
haftmann@30298
  1154
  next
wenzelm@60758
  1155
    case False with \<open>x \<le> y\<close> have "x < y" by simp
haftmann@30298
  1156
    with assms strict_monoD have "f x < f y" by auto
haftmann@30298
  1157
    then show ?thesis by simp
haftmann@30298
  1158
  qed
haftmann@30298
  1159
qed
haftmann@30298
  1160
haftmann@25076
  1161
end
haftmann@25076
  1162
haftmann@25076
  1163
context linorder
haftmann@25076
  1164
begin
haftmann@25076
  1165
haftmann@51263
  1166
lemma mono_invE:
wenzelm@61076
  1167
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@51263
  1168
  assumes "mono f"
haftmann@51263
  1169
  assumes "f x < f y"
haftmann@51263
  1170
  obtains "x \<le> y"
haftmann@51263
  1171
proof
haftmann@51263
  1172
  show "x \<le> y"
haftmann@51263
  1173
  proof (rule ccontr)
haftmann@51263
  1174
    assume "\<not> x \<le> y"
haftmann@51263
  1175
    then have "y \<le> x" by simp
wenzelm@60758
  1176
    with \<open>mono f\<close> obtain "f y \<le> f x" by (rule monoE)
wenzelm@60758
  1177
    with \<open>f x < f y\<close> show False by simp
haftmann@51263
  1178
  qed
haftmann@51263
  1179
qed
haftmann@51263
  1180
haftmann@66936
  1181
lemma mono_strict_invE:
haftmann@66936
  1182
  fixes f :: "'a \<Rightarrow> 'b::order"
haftmann@66936
  1183
  assumes "mono f"
haftmann@66936
  1184
  assumes "f x < f y"
haftmann@66936
  1185
  obtains "x < y"
haftmann@66936
  1186
proof
haftmann@66936
  1187
  show "x < y"
haftmann@66936
  1188
  proof (rule ccontr)
haftmann@66936
  1189
    assume "\<not> x < y"
haftmann@66936
  1190
    then have "y \<le> x" by simp
haftmann@66936
  1191
    with \<open>mono f\<close> obtain "f y \<le> f x" by (rule monoE)
haftmann@66936
  1192
    with \<open>f x < f y\<close> show False by simp
haftmann@66936
  1193
  qed
haftmann@66936
  1194
qed
haftmann@66936
  1195
haftmann@30298
  1196
lemma strict_mono_eq:
haftmann@30298
  1197
  assumes "strict_mono f"
haftmann@30298
  1198
  shows "f x = f y \<longleftrightarrow> x = y"
haftmann@30298
  1199
proof
haftmann@30298
  1200
  assume "f x = f y"
haftmann@30298
  1201
  show "x = y" proof (cases x y rule: linorder_cases)
haftmann@30298
  1202
    case less with assms strict_monoD have "f x < f y" by auto
wenzelm@60758
  1203
    with \<open>f x = f y\<close> show ?thesis by simp
haftmann@30298
  1204
  next
haftmann@30298
  1205
    case equal then show ?thesis .
haftmann@30298
  1206
  next
haftmann@30298
  1207
    case greater with assms strict_monoD have "f y < f x" by auto
wenzelm@60758
  1208
    with \<open>f x = f y\<close> show ?thesis by simp
haftmann@30298
  1209
  qed
haftmann@30298
  1210
qed simp
haftmann@30298
  1211
haftmann@30298
  1212
lemma strict_mono_less_eq:
haftmann@30298
  1213
  assumes "strict_mono f"
haftmann@30298
  1214
  shows "f x \<le> f y \<longleftrightarrow> x \<le> y"
haftmann@30298
  1215
proof
haftmann@30298
  1216
  assume "x \<le> y"
haftmann@30298
  1217
  with assms strict_mono_mono monoD show "f x \<le> f y" by auto
haftmann@30298
  1218
next
haftmann@30298
  1219
  assume "f x \<le> f y"
haftmann@30298
  1220
  show "x \<le> y" proof (rule ccontr)
haftmann@30298
  1221
    assume "\<not> x \<le> y" then have "y < x" by simp
haftmann@30298
  1222
    with assms strict_monoD have "f y < f x" by auto
wenzelm@60758
  1223
    with \<open>f x \<le> f y\<close> show False by simp
haftmann@30298
  1224
  qed
haftmann@30298
  1225
qed
lp15@61824
  1226
haftmann@30298
  1227
lemma strict_mono_less:
haftmann@30298
  1228
  assumes "strict_mono f"
haftmann@30298
  1229
  shows "f x < f y \<longleftrightarrow> x < y"
haftmann@30298
  1230
  using assms
haftmann@30298
  1231
    by (auto simp add: less_le Orderings.less_le strict_mono_eq strict_mono_less_eq)
haftmann@30298
  1232
haftmann@54860
  1233
end
haftmann@54860
  1234
haftmann@54860
  1235
wenzelm@60758
  1236
subsection \<open>min and max -- fundamental\<close>
haftmann@54860
  1237
haftmann@54860
  1238
definition (in ord) min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@54860
  1239
  "min a b = (if a \<le> b then a else b)"
haftmann@54860
  1240
haftmann@54860
  1241
definition (in ord) max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@54860
  1242
  "max a b = (if a \<le> b then b else a)"
haftmann@54860
  1243
noschinl@45931
  1244
lemma min_absorb1: "x \<le> y \<Longrightarrow> min x y = x"
haftmann@54861
  1245
  by (simp add: min_def)
haftmann@21383
  1246
haftmann@54857
  1247
lemma max_absorb2: "x \<le> y \<Longrightarrow> max x y = y"
haftmann@54861
  1248
  by (simp add: max_def)
haftmann@21383
  1249
wenzelm@61076
  1250
lemma min_absorb2: "(y::'a::order) \<le> x \<Longrightarrow> min x y = y"
haftmann@54861
  1251
  by (simp add:min_def)
noschinl@45893
  1252
wenzelm@61076
  1253
lemma max_absorb1: "(y::'a::order) \<le> x \<Longrightarrow> max x y = x"
haftmann@54861
  1254
  by (simp add: max_def)
noschinl@45893
  1255
Andreas@61630
  1256
lemma max_min_same [simp]:
Andreas@61630
  1257
  fixes x y :: "'a :: linorder"
Andreas@61630
  1258
  shows "max x (min x y) = x" "max (min x y) x = x" "max (min x y) y = y" "max y (min x y) = y"
Andreas@61630
  1259
by(auto simp add: max_def min_def)
noschinl@45893
  1260
haftmann@66936
  1261
wenzelm@60758
  1262
subsection \<open>(Unique) top and bottom elements\<close>
haftmann@28685
  1263
haftmann@52729
  1264
class bot =
haftmann@43853
  1265
  fixes bot :: 'a ("\<bottom>")
haftmann@52729
  1266
haftmann@52729
  1267
class order_bot = order + bot +
haftmann@51487
  1268
  assumes bot_least: "\<bottom> \<le> a"
haftmann@54868
  1269
begin
haftmann@51487
  1270
wenzelm@61605
  1271
sublocale bot: ordering_top greater_eq greater bot
wenzelm@61169
  1272
  by standard (fact bot_least)
haftmann@51487
  1273
haftmann@43853
  1274
lemma le_bot:
haftmann@43853
  1275
  "a \<le> \<bottom> \<Longrightarrow> a = \<bottom>"
haftmann@51487
  1276
  by (fact bot.extremum_uniqueI)
haftmann@43853
  1277
haftmann@43816
  1278
lemma bot_unique:
haftmann@43853
  1279
  "a \<le> \<bottom> \<longleftrightarrow> a = \<bottom>"
haftmann@51487
  1280
  by (fact bot.extremum_unique)
haftmann@43853
  1281
haftmann@51487
  1282
lemma not_less_bot:
haftmann@51487
  1283
  "\<not> a < \<bottom>"
haftmann@51487
  1284
  by (fact bot.extremum_strict)
haftmann@43816
  1285
haftmann@43814
  1286
lemma bot_less:
haftmann@43853
  1287
  "a \<noteq> \<bottom> \<longleftrightarrow> \<bottom> < a"
haftmann@51487
  1288
  by (fact bot.not_eq_extremum)
haftmann@43814
  1289
haftmann@43814
  1290
end
haftmann@41082
  1291
haftmann@52729
  1292
class top =
haftmann@43853
  1293
  fixes top :: 'a ("\<top>")
haftmann@52729
  1294
haftmann@52729
  1295
class order_top = order + top +
haftmann@51487
  1296
  assumes top_greatest: "a \<le> \<top>"
haftmann@54868
  1297
begin
haftmann@51487
  1298
wenzelm@61605
  1299
sublocale top: ordering_top less_eq less top
wenzelm@61169
  1300
  by standard (fact top_greatest)
haftmann@51487
  1301
haftmann@43853
  1302
lemma top_le:
haftmann@43853
  1303
  "\<top> \<le> a \<Longrightarrow> a = \<top>"
haftmann@51487
  1304
  by (fact top.extremum_uniqueI)
haftmann@43853
  1305
haftmann@43816
  1306
lemma top_unique:
haftmann@43853
  1307
  "\<top> \<le> a \<longleftrightarrow> a = \<top>"
haftmann@51487
  1308
  by (fact top.extremum_unique)
haftmann@43853
  1309
haftmann@51487
  1310
lemma not_top_less:
haftmann@51487
  1311
  "\<not> \<top> < a"
haftmann@51487
  1312
  by (fact top.extremum_strict)
haftmann@43816
  1313
haftmann@43814
  1314
lemma less_top:
haftmann@43853
  1315
  "a \<noteq> \<top> \<longleftrightarrow> a < \<top>"
haftmann@51487
  1316
  by (fact top.not_eq_extremum)
haftmann@43814
  1317
haftmann@43814
  1318
end
haftmann@28685
  1319
haftmann@28685
  1320
wenzelm@60758
  1321
subsection \<open>Dense orders\<close>
haftmann@27823
  1322
hoelzl@53216
  1323
class dense_order = order +
hoelzl@51329
  1324
  assumes dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
hoelzl@51329
  1325
hoelzl@53216
  1326
class dense_linorder = linorder + dense_order
hoelzl@35579
  1327
begin
haftmann@27823
  1328
hoelzl@35579
  1329
lemma dense_le:
hoelzl@35579
  1330
  fixes y z :: 'a
hoelzl@35579
  1331
  assumes "\<And>x. x < y \<Longrightarrow> x \<le> z"
hoelzl@35579
  1332
  shows "y \<le> z"
hoelzl@35579
  1333
proof (rule ccontr)
hoelzl@35579
  1334
  assume "\<not> ?thesis"
hoelzl@35579
  1335
  hence "z < y" by simp
hoelzl@35579
  1336
  from dense[OF this]
hoelzl@35579
  1337
  obtain x where "x < y" and "z < x" by safe
wenzelm@60758
  1338
  moreover have "x \<le> z" using assms[OF \<open>x < y\<close>] .
hoelzl@35579
  1339
  ultimately show False by auto
hoelzl@35579
  1340
qed
hoelzl@35579
  1341
hoelzl@35579
  1342
lemma dense_le_bounded:
hoelzl@35579
  1343
  fixes x y z :: 'a
hoelzl@35579
  1344
  assumes "x < y"
hoelzl@35579
  1345
  assumes *: "\<And>w. \<lbrakk> x < w ; w < y \<rbrakk> \<Longrightarrow> w \<le> z"
hoelzl@35579
  1346
  shows "y \<le> z"
hoelzl@35579
  1347
proof (rule dense_le)
hoelzl@35579
  1348
  fix w assume "w < y"
wenzelm@60758
  1349
  from dense[OF \<open>x < y\<close>] obtain u where "x < u" "u < y" by safe
hoelzl@35579
  1350
  from linear[of u w]
hoelzl@35579
  1351
  show "w \<le> z"
hoelzl@35579
  1352
  proof (rule disjE)
hoelzl@35579
  1353
    assume "u \<le> w"
wenzelm@60758
  1354
    from less_le_trans[OF \<open>x < u\<close> \<open>u \<le> w\<close>] \<open>w < y\<close>
hoelzl@35579
  1355
    show "w \<le> z" by (rule *)
hoelzl@35579
  1356
  next
hoelzl@35579
  1357
    assume "w \<le> u"
wenzelm@60758
  1358
    from \<open>w \<le> u\<close> *[OF \<open>x < u\<close> \<open>u < y\<close>]
hoelzl@35579
  1359
    show "w \<le> z" by (rule order_trans)
hoelzl@35579
  1360
  qed
hoelzl@35579
  1361
qed
hoelzl@35579
  1362
hoelzl@51329
  1363
lemma dense_ge:
hoelzl@51329
  1364
  fixes y z :: 'a
hoelzl@51329
  1365
  assumes "\<And>x. z < x \<Longrightarrow> y \<le> x"
hoelzl@51329
  1366
  shows "y \<le> z"
hoelzl@51329
  1367
proof (rule ccontr)
hoelzl@51329
  1368
  assume "\<not> ?thesis"
hoelzl@51329
  1369
  hence "z < y" by simp
hoelzl@51329
  1370
  from dense[OF this]
hoelzl@51329
  1371
  obtain x where "x < y" and "z < x" by safe
wenzelm@60758
  1372
  moreover have "y \<le> x" using assms[OF \<open>z < x\<close>] .
hoelzl@51329
  1373
  ultimately show False by auto
hoelzl@51329
  1374
qed
hoelzl@51329
  1375
hoelzl@51329
  1376
lemma dense_ge_bounded:
hoelzl@51329
  1377
  fixes x y z :: 'a
hoelzl@51329
  1378
  assumes "z < x"
hoelzl@51329
  1379
  assumes *: "\<And>w. \<lbrakk> z < w ; w < x \<rbrakk> \<Longrightarrow> y \<le> w"
hoelzl@51329
  1380
  shows "y \<le> z"
hoelzl@51329
  1381
proof (rule dense_ge)
hoelzl@51329
  1382
  fix w assume "z < w"
wenzelm@60758
  1383
  from dense[OF \<open>z < x\<close>] obtain u where "z < u" "u < x" by safe
hoelzl@51329
  1384
  from linear[of u w]
hoelzl@51329
  1385
  show "y \<le> w"
hoelzl@51329
  1386
  proof (rule disjE)
hoelzl@51329
  1387
    assume "w \<le> u"
wenzelm@60758
  1388
    from \<open>z < w\<close> le_less_trans[OF \<open>w \<le> u\<close> \<open>u < x\<close>]
hoelzl@51329
  1389
    show "y \<le> w" by (rule *)
hoelzl@51329
  1390
  next
hoelzl@51329
  1391
    assume "u \<le> w"
wenzelm@60758
  1392
    from *[OF \<open>z < u\<close> \<open>u < x\<close>] \<open>u \<le> w\<close>
hoelzl@51329
  1393
    show "y \<le> w" by (rule order_trans)
hoelzl@51329
  1394
  qed
hoelzl@51329
  1395
qed
hoelzl@51329
  1396
hoelzl@35579
  1397
end
haftmann@27823
  1398
lp15@61824
  1399
class no_top = order +
hoelzl@51329
  1400
  assumes gt_ex: "\<exists>y. x < y"
hoelzl@51329
  1401
lp15@61824
  1402
class no_bot = order +
hoelzl@51329
  1403
  assumes lt_ex: "\<exists>y. y < x"
hoelzl@51329
  1404
hoelzl@53216
  1405
class unbounded_dense_linorder = dense_linorder + no_top + no_bot
hoelzl@51329
  1406
haftmann@51546
  1407
wenzelm@60758
  1408
subsection \<open>Wellorders\<close>
haftmann@27823
  1409
haftmann@27823
  1410
class wellorder = linorder +
haftmann@27823
  1411
  assumes less_induct [case_names less]: "(\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P a"
haftmann@27823
  1412
begin
haftmann@27823
  1413
haftmann@27823
  1414
lemma wellorder_Least_lemma:
haftmann@27823
  1415
  fixes k :: 'a
haftmann@27823
  1416
  assumes "P k"
haftmann@34250
  1417
  shows LeastI: "P (LEAST x. P x)" and Least_le: "(LEAST x. P x) \<le> k"
haftmann@27823
  1418
proof -
haftmann@27823
  1419
  have "P (LEAST x. P x) \<and> (LEAST x. P x) \<le> k"
haftmann@27823
  1420
  using assms proof (induct k rule: less_induct)
haftmann@27823
  1421
    case (less x) then have "P x" by simp
haftmann@27823
  1422
    show ?case proof (rule classical)
haftmann@27823
  1423
      assume assm: "\<not> (P (LEAST a. P a) \<and> (LEAST a. P a) \<le> x)"
haftmann@27823
  1424
      have "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27823
  1425
      proof (rule classical)
haftmann@27823
  1426
        fix y
hoelzl@38705
  1427
        assume "P y" and "\<not> x \<le> y"
haftmann@27823
  1428
        with less have "P (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1429
          by (auto simp add: not_le)
haftmann@27823
  1430
        with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1431
          by auto
haftmann@27823
  1432
        then show "x \<le> y" by auto
haftmann@27823
  1433
      qed
wenzelm@60758
  1434
      with \<open>P x\<close> have Least: "(LEAST a. P a) = x"
haftmann@27823
  1435
        by (rule Least_equality)
wenzelm@60758
  1436
      with \<open>P x\<close> show ?thesis by simp
haftmann@27823
  1437
    qed
haftmann@27823
  1438
  qed
haftmann@27823
  1439
  then show "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k" by auto
haftmann@27823
  1440
qed
haftmann@27823
  1441
wenzelm@61799
  1442
\<comment> "The following 3 lemmas are due to Brian Huffman"
haftmann@27823
  1443
lemma LeastI_ex: "\<exists>x. P x \<Longrightarrow> P (Least P)"
haftmann@27823
  1444
  by (erule exE) (erule LeastI)
haftmann@27823
  1445
haftmann@27823
  1446
lemma LeastI2:
haftmann@27823
  1447
  "P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1448
  by (blast intro: LeastI)
haftmann@27823
  1449
haftmann@27823
  1450
lemma LeastI2_ex:
haftmann@27823
  1451
  "\<exists>a. P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1452
  by (blast intro: LeastI_ex)
haftmann@27823
  1453
hoelzl@38705
  1454
lemma LeastI2_wellorder:
hoelzl@38705
  1455
  assumes "P a"
hoelzl@38705
  1456
  and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
hoelzl@38705
  1457
  shows "Q (Least P)"
hoelzl@38705
  1458
proof (rule LeastI2_order)
wenzelm@60758
  1459
  show "P (Least P)" using \<open>P a\<close> by (rule LeastI)
hoelzl@38705
  1460
next
hoelzl@38705
  1461
  fix y assume "P y" thus "Least P \<le> y" by (rule Least_le)
hoelzl@38705
  1462
next
hoelzl@38705
  1463
  fix x assume "P x" "\<forall>y. P y \<longrightarrow> x \<le> y" thus "Q x" by (rule assms(2))
hoelzl@38705
  1464
qed
hoelzl@38705
  1465
lp15@61699
  1466
lemma LeastI2_wellorder_ex:
lp15@61699
  1467
  assumes "\<exists>x. P x"
lp15@61699
  1468
  and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
lp15@61699
  1469
  shows "Q (Least P)"
lp15@61699
  1470
using assms by clarify (blast intro!: LeastI2_wellorder)
lp15@61699
  1471
haftmann@27823
  1472
lemma not_less_Least: "k < (LEAST x. P x) \<Longrightarrow> \<not> P k"
lp15@61699
  1473
apply (simp add: not_le [symmetric])
haftmann@27823
  1474
apply (erule contrapos_nn)
haftmann@27823
  1475
apply (erule Least_le)
haftmann@27823
  1476
done
haftmann@27823
  1477
lp15@64287
  1478
lemma exists_least_iff: "(\<exists>n. P n) \<longleftrightarrow> (\<exists>n. P n \<and> (\<forall>m < n. \<not> P m))" (is "?lhs \<longleftrightarrow> ?rhs")
lp15@64287
  1479
proof
lp15@64287
  1480
  assume ?rhs thus ?lhs by blast
lp15@64287
  1481
next
lp15@64287
  1482
  assume H: ?lhs then obtain n where n: "P n" by blast
lp15@64287
  1483
  let ?x = "Least P"
lp15@64287
  1484
  { fix m assume m: "m < ?x"
lp15@64287
  1485
    from not_less_Least[OF m] have "\<not> P m" . }
lp15@64287
  1486
  with LeastI_ex[OF H] show ?rhs by blast
lp15@64287
  1487
qed
lp15@64287
  1488
hoelzl@38705
  1489
end
haftmann@27823
  1490
haftmann@28685
  1491
wenzelm@60758
  1492
subsection \<open>Order on @{typ bool}\<close>
haftmann@28685
  1493
haftmann@52729
  1494
instantiation bool :: "{order_bot, order_top, linorder}"
haftmann@28685
  1495
begin
haftmann@28685
  1496
haftmann@28685
  1497
definition
haftmann@41080
  1498
  le_bool_def [simp]: "P \<le> Q \<longleftrightarrow> P \<longrightarrow> Q"
haftmann@28685
  1499
haftmann@28685
  1500
definition
wenzelm@61076
  1501
  [simp]: "(P::bool) < Q \<longleftrightarrow> \<not> P \<and> Q"
haftmann@28685
  1502
haftmann@28685
  1503
definition
haftmann@46631
  1504
  [simp]: "\<bottom> \<longleftrightarrow> False"
haftmann@28685
  1505
haftmann@28685
  1506
definition
haftmann@46631
  1507
  [simp]: "\<top> \<longleftrightarrow> True"
haftmann@28685
  1508
haftmann@28685
  1509
instance proof
haftmann@41080
  1510
qed auto
haftmann@28685
  1511
nipkow@15524
  1512
end
haftmann@28685
  1513
haftmann@28685
  1514
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
haftmann@41080
  1515
  by simp
haftmann@28685
  1516
haftmann@28685
  1517
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
haftmann@41080
  1518
  by simp
haftmann@28685
  1519
haftmann@28685
  1520
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@41080
  1521
  by simp
haftmann@28685
  1522
haftmann@28685
  1523
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
haftmann@41080
  1524
  by simp
haftmann@32899
  1525
haftmann@46631
  1526
lemma bot_boolE: "\<bottom> \<Longrightarrow> P"
haftmann@41080
  1527
  by simp
haftmann@32899
  1528
haftmann@46631
  1529
lemma top_boolI: \<top>
haftmann@41080
  1530
  by simp
haftmann@28685
  1531
haftmann@28685
  1532
lemma [code]:
haftmann@28685
  1533
  "False \<le> b \<longleftrightarrow> True"
haftmann@28685
  1534
  "True \<le> b \<longleftrightarrow> b"
haftmann@28685
  1535
  "False < b \<longleftrightarrow> b"
haftmann@28685
  1536
  "True < b \<longleftrightarrow> False"
haftmann@41080
  1537
  by simp_all
haftmann@28685
  1538
haftmann@28685
  1539
wenzelm@60758
  1540
subsection \<open>Order on @{typ "_ \<Rightarrow> _"}\<close>
haftmann@28685
  1541
haftmann@28685
  1542
instantiation "fun" :: (type, ord) ord
haftmann@28685
  1543
begin
haftmann@28685
  1544
haftmann@28685
  1545
definition
haftmann@37767
  1546
  le_fun_def: "f \<le> g \<longleftrightarrow> (\<forall>x. f x \<le> g x)"
haftmann@28685
  1547
haftmann@28685
  1548
definition
wenzelm@61076
  1549
  "(f::'a \<Rightarrow> 'b) < g \<longleftrightarrow> f \<le> g \<and> \<not> (g \<le> f)"
haftmann@28685
  1550
haftmann@28685
  1551
instance ..
haftmann@28685
  1552
haftmann@28685
  1553
end
haftmann@28685
  1554
haftmann@28685
  1555
instance "fun" :: (type, preorder) preorder proof
haftmann@28685
  1556
qed (auto simp add: le_fun_def less_fun_def
huffman@44921
  1557
  intro: order_trans antisym)
haftmann@28685
  1558
haftmann@28685
  1559
instance "fun" :: (type, order) order proof
huffman@44921
  1560
qed (auto simp add: le_fun_def intro: antisym)
haftmann@28685
  1561
haftmann@41082
  1562
instantiation "fun" :: (type, bot) bot
haftmann@41082
  1563
begin
haftmann@41082
  1564
haftmann@41082
  1565
definition
haftmann@46631
  1566
  "\<bottom> = (\<lambda>x. \<bottom>)"
haftmann@41082
  1567
haftmann@52729
  1568
instance ..
haftmann@52729
  1569
haftmann@52729
  1570
end
haftmann@52729
  1571
haftmann@52729
  1572
instantiation "fun" :: (type, order_bot) order_bot
haftmann@52729
  1573
begin
haftmann@52729
  1574
haftmann@49769
  1575
lemma bot_apply [simp, code]:
haftmann@46631
  1576
  "\<bottom> x = \<bottom>"
haftmann@41082
  1577
  by (simp add: bot_fun_def)
haftmann@41082
  1578
haftmann@41082
  1579
instance proof
noschinl@46884
  1580
qed (simp add: le_fun_def)
haftmann@41082
  1581
haftmann@41082
  1582
end
haftmann@41082
  1583
haftmann@28685
  1584
instantiation "fun" :: (type, top) top
haftmann@28685
  1585
begin
haftmann@28685
  1586
haftmann@28685
  1587
definition
haftmann@46631
  1588
  [no_atp]: "\<top> = (\<lambda>x. \<top>)"
haftmann@28685
  1589
haftmann@52729
  1590
instance ..
haftmann@52729
  1591
haftmann@52729
  1592
end
haftmann@52729
  1593
haftmann@52729
  1594
instantiation "fun" :: (type, order_top) order_top
haftmann@52729
  1595
begin
haftmann@52729
  1596
haftmann@49769
  1597
lemma top_apply [simp, code]:
haftmann@46631
  1598
  "\<top> x = \<top>"
haftmann@41080
  1599
  by (simp add: top_fun_def)
haftmann@41080
  1600
haftmann@28685
  1601
instance proof
noschinl@46884
  1602
qed (simp add: le_fun_def)
haftmann@28685
  1603
haftmann@28685
  1604
end
haftmann@28685
  1605
haftmann@28685
  1606
lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
haftmann@28685
  1607
  unfolding le_fun_def by simp
haftmann@28685
  1608
haftmann@28685
  1609
lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@28685
  1610
  unfolding le_fun_def by simp
haftmann@28685
  1611
haftmann@28685
  1612
lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
haftmann@54860
  1613
  by (rule le_funE)
haftmann@28685
  1614
hoelzl@59000
  1615
lemma mono_compose: "mono Q \<Longrightarrow> mono (\<lambda>i x. Q i (f x))"
hoelzl@59000
  1616
  unfolding mono_def le_fun_def by auto
hoelzl@59000
  1617
haftmann@34250
  1618
wenzelm@60758
  1619
subsection \<open>Order on unary and binary predicates\<close>
haftmann@46631
  1620
haftmann@46631
  1621
lemma predicate1I:
haftmann@46631
  1622
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
haftmann@46631
  1623
  shows "P \<le> Q"
haftmann@46631
  1624
  apply (rule le_funI)
haftmann@46631
  1625
  apply (rule le_boolI)
haftmann@46631
  1626
  apply (rule PQ)
haftmann@46631
  1627
  apply assumption
haftmann@46631
  1628
  done
haftmann@46631
  1629
haftmann@46631
  1630
lemma predicate1D:
haftmann@46631
  1631
  "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
haftmann@46631
  1632
  apply (erule le_funE)
haftmann@46631
  1633
  apply (erule le_boolE)
haftmann@46631
  1634
  apply assumption+
haftmann@46631
  1635
  done
haftmann@46631
  1636
haftmann@46631
  1637
lemma rev_predicate1D:
haftmann@46631
  1638
  "P x \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x"
haftmann@46631
  1639
  by (rule predicate1D)
haftmann@46631
  1640
haftmann@46631
  1641
lemma predicate2I:
haftmann@46631
  1642
  assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
haftmann@46631
  1643
  shows "P \<le> Q"
haftmann@46631
  1644
  apply (rule le_funI)+
haftmann@46631
  1645
  apply (rule le_boolI)
haftmann@46631
  1646
  apply (rule PQ)
haftmann@46631
  1647
  apply assumption
haftmann@46631
  1648
  done
haftmann@46631
  1649
haftmann@46631
  1650
lemma predicate2D:
haftmann@46631
  1651
  "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
haftmann@46631
  1652
  apply (erule le_funE)+
haftmann@46631
  1653
  apply (erule le_boolE)
haftmann@46631
  1654
  apply assumption+
haftmann@46631
  1655
  done
haftmann@46631
  1656
haftmann@46631
  1657
lemma rev_predicate2D:
haftmann@46631
  1658
  "P x y \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x y"
haftmann@46631
  1659
  by (rule predicate2D)
haftmann@46631
  1660
haftmann@46631
  1661
lemma bot1E [no_atp]: "\<bottom> x \<Longrightarrow> P"
haftmann@46631
  1662
  by (simp add: bot_fun_def)
haftmann@46631
  1663
haftmann@46631
  1664
lemma bot2E: "\<bottom> x y \<Longrightarrow> P"
haftmann@46631
  1665
  by (simp add: bot_fun_def)
haftmann@46631
  1666
haftmann@46631
  1667
lemma top1I: "\<top> x"
haftmann@46631
  1668
  by (simp add: top_fun_def)
haftmann@46631
  1669
haftmann@46631
  1670
lemma top2I: "\<top> x y"
haftmann@46631
  1671
  by (simp add: top_fun_def)
haftmann@46631
  1672
haftmann@46631
  1673
wenzelm@60758
  1674
subsection \<open>Name duplicates\<close>
haftmann@34250
  1675
haftmann@34250
  1676
lemmas order_eq_refl = preorder_class.eq_refl
haftmann@34250
  1677
lemmas order_less_irrefl = preorder_class.less_irrefl
haftmann@34250
  1678
lemmas order_less_imp_le = preorder_class.less_imp_le
haftmann@34250
  1679
lemmas order_less_not_sym = preorder_class.less_not_sym
haftmann@34250
  1680
lemmas order_less_asym = preorder_class.less_asym
haftmann@34250
  1681
lemmas order_less_trans = preorder_class.less_trans
haftmann@34250
  1682
lemmas order_le_less_trans = preorder_class.le_less_trans
haftmann@34250
  1683
lemmas order_less_le_trans = preorder_class.less_le_trans
haftmann@34250
  1684
lemmas order_less_imp_not_less = preorder_class.less_imp_not_less
haftmann@34250
  1685
lemmas order_less_imp_triv = preorder_class.less_imp_triv
haftmann@34250
  1686
lemmas order_less_asym' = preorder_class.less_asym'
haftmann@34250
  1687
haftmann@34250
  1688
lemmas order_less_le = order_class.less_le
haftmann@34250
  1689
lemmas order_le_less = order_class.le_less
haftmann@34250
  1690
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@34250
  1691
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@34250
  1692
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@34250
  1693
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@34250
  1694
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@34250
  1695
lemmas order_antisym = order_class.antisym
haftmann@34250
  1696
lemmas order_eq_iff = order_class.eq_iff
haftmann@34250
  1697
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@34250
  1698
haftmann@34250
  1699
lemmas linorder_linear = linorder_class.linear
haftmann@34250
  1700
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@34250
  1701
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@34250
  1702
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@34250
  1703
lemmas linorder_not_less = linorder_class.not_less
haftmann@34250
  1704
lemmas linorder_not_le = linorder_class.not_le
haftmann@34250
  1705
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@34250
  1706
lemmas linorder_neqE = linorder_class.neqE
haftmann@34250
  1707
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@34250
  1708
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@34250
  1709
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@34250
  1710
haftmann@28685
  1711
end