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