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