src/HOL/Orderings.thy
author wenzelm
Fri Mar 16 18:20:12 2012 +0100 (2012-03-16)
changeset 46961 5c6955f487e5
parent 46950 d0181abdbdac
child 46976 80123a220219
permissions -rw-r--r--
outer syntax command definitions based on formal command_spec derived from theory header declarations;
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
wenzelm@32215
    10
uses
wenzelm@32215
    11
  "~~/src/Provers/order.ML"
wenzelm@32215
    12
  "~~/src/Provers/quasi.ML"  (* FIXME unused? *)
nipkow@15524
    13
begin
nipkow@15524
    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
ballarin@24641
   315
  val 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
ballarin@24641
   417
ballarin@24641
   418
(** Setup **)
ballarin@24641
   419
wenzelm@24867
   420
val setup =
wenzelm@32215
   421
  Method.setup @{binding order} (Scan.succeed (fn ctxt => SIMPLE_METHOD' (order_tac ctxt [])))
wenzelm@30722
   422
    "transitivity reasoner" #>
wenzelm@30722
   423
  attrib_setup;
haftmann@21091
   424
haftmann@21091
   425
end;
ballarin@24641
   426
haftmann@21091
   427
*}
haftmann@21091
   428
ballarin@24641
   429
setup Orders.setup
ballarin@24641
   430
ballarin@24641
   431
ballarin@24641
   432
text {* Declarations to set up transitivity reasoner of partial and linear orders. *}
ballarin@24641
   433
haftmann@25076
   434
context order
haftmann@25076
   435
begin
haftmann@25076
   436
ballarin@24641
   437
(* The type constraint on @{term op =} below is necessary since the operation
ballarin@24641
   438
   is not a parameter of the locale. *)
haftmann@25076
   439
haftmann@27689
   440
declare less_irrefl [THEN notE, order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"]
haftmann@27689
   441
  
haftmann@27689
   442
declare order_refl  [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   443
  
haftmann@27689
   444
declare less_imp_le [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   445
  
haftmann@27689
   446
declare antisym [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   447
haftmann@27689
   448
declare eq_refl [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   449
haftmann@27689
   450
declare sym [THEN eq_refl, order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   451
haftmann@27689
   452
declare less_trans [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   453
  
haftmann@27689
   454
declare less_le_trans [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   455
  
haftmann@27689
   456
declare le_less_trans [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   457
haftmann@27689
   458
declare order_trans [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   459
haftmann@27689
   460
declare le_neq_trans [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   461
haftmann@27689
   462
declare neq_le_trans [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   463
haftmann@27689
   464
declare less_imp_neq [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   465
haftmann@27689
   466
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   467
haftmann@27689
   468
declare not_sym [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   469
haftmann@25076
   470
end
haftmann@25076
   471
haftmann@25076
   472
context linorder
haftmann@25076
   473
begin
ballarin@24641
   474
haftmann@27689
   475
declare [[order del: order "op = :: 'a => 'a => bool" "op <=" "op <"]]
haftmann@27689
   476
haftmann@27689
   477
declare less_irrefl [THEN notE, order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   478
haftmann@27689
   479
declare order_refl [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   480
haftmann@27689
   481
declare less_imp_le [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   482
haftmann@27689
   483
declare not_less [THEN iffD2, order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   484
haftmann@27689
   485
declare not_le [THEN iffD2, order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   486
haftmann@27689
   487
declare not_less [THEN iffD1, order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   488
haftmann@27689
   489
declare not_le [THEN iffD1, order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   490
haftmann@27689
   491
declare antisym [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   492
haftmann@27689
   493
declare eq_refl [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@25076
   494
haftmann@27689
   495
declare sym [THEN eq_refl, order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   496
haftmann@27689
   497
declare less_trans [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   498
haftmann@27689
   499
declare less_le_trans [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   500
haftmann@27689
   501
declare le_less_trans [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   502
haftmann@27689
   503
declare order_trans [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   504
haftmann@27689
   505
declare le_neq_trans [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   506
haftmann@27689
   507
declare neq_le_trans [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   508
haftmann@27689
   509
declare less_imp_neq [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   510
haftmann@27689
   511
declare eq_neq_eq_imp_neq [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
haftmann@27689
   512
haftmann@27689
   513
declare not_sym [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"]
ballarin@24641
   514
haftmann@25076
   515
end
haftmann@25076
   516
ballarin@24641
   517
haftmann@21083
   518
setup {*
haftmann@21083
   519
let
haftmann@21083
   520
wenzelm@44058
   521
fun prp t thm = Thm.prop_of thm = t;  (* FIXME aconv!? *)
nipkow@15524
   522
haftmann@21083
   523
fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
wenzelm@43597
   524
  let val prems = Simplifier.prems_of ss;
haftmann@22916
   525
      val less = Const (@{const_name less}, T);
haftmann@21083
   526
      val t = HOLogic.mk_Trueprop(le $ s $ r);
haftmann@21083
   527
  in case find_first (prp t) prems of
haftmann@21083
   528
       NONE =>
haftmann@21083
   529
         let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
haftmann@21083
   530
         in case find_first (prp t) prems of
haftmann@21083
   531
              NONE => NONE
haftmann@24422
   532
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv1}))
haftmann@21083
   533
         end
haftmann@24422
   534
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_class.antisym_conv}))
haftmann@21083
   535
  end
haftmann@21083
   536
  handle THM _ => NONE;
nipkow@15524
   537
haftmann@21083
   538
fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
wenzelm@43597
   539
  let val prems = Simplifier.prems_of ss;
haftmann@22916
   540
      val le = Const (@{const_name less_eq}, T);
haftmann@21083
   541
      val t = HOLogic.mk_Trueprop(le $ r $ s);
haftmann@21083
   542
  in case find_first (prp t) prems of
haftmann@21083
   543
       NONE =>
haftmann@21083
   544
         let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
haftmann@21083
   545
         in case find_first (prp t) prems of
haftmann@21083
   546
              NONE => NONE
haftmann@24422
   547
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv3}))
haftmann@21083
   548
         end
haftmann@24422
   549
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_class.antisym_conv2}))
haftmann@21083
   550
  end
haftmann@21083
   551
  handle THM _ => NONE;
nipkow@15524
   552
haftmann@21248
   553
fun add_simprocs procs thy =
wenzelm@42795
   554
  Simplifier.map_simpset_global (fn ss => ss
haftmann@21248
   555
    addsimprocs (map (fn (name, raw_ts, proc) =>
wenzelm@38715
   556
      Simplifier.simproc_global thy name raw_ts proc) procs)) thy;
wenzelm@42795
   557
wenzelm@26496
   558
fun add_solver name tac =
wenzelm@42795
   559
  Simplifier.map_simpset_global (fn ss => ss addSolver
wenzelm@43597
   560
    mk_solver name (fn ss => tac (Simplifier.the_context ss) (Simplifier.prems_of ss)));
haftmann@21083
   561
haftmann@21083
   562
in
haftmann@21248
   563
  add_simprocs [
haftmann@21248
   564
       ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
haftmann@21248
   565
       ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
haftmann@21248
   566
     ]
ballarin@24641
   567
  #> add_solver "Transitivity" Orders.order_tac
haftmann@21248
   568
  (* Adding the transitivity reasoners also as safe solvers showed a slight
haftmann@21248
   569
     speed up, but the reasoning strength appears to be not higher (at least
haftmann@21248
   570
     no breaking of additional proofs in the entire HOL distribution, as
haftmann@21248
   571
     of 5 March 2004, was observed). *)
haftmann@21083
   572
end
haftmann@21083
   573
*}
nipkow@15524
   574
nipkow@15524
   575
haftmann@21083
   576
subsection {* Bounded quantifiers *}
haftmann@21083
   577
haftmann@21083
   578
syntax
wenzelm@21180
   579
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   580
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   581
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   582
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   583
wenzelm@21180
   584
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   585
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   586
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   587
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   588
haftmann@21083
   589
syntax (xsymbols)
wenzelm@21180
   590
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   591
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   592
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   593
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   594
wenzelm@21180
   595
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   596
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   597
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   598
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   599
haftmann@21083
   600
syntax (HOL)
wenzelm@21180
   601
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   602
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   603
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   604
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   605
haftmann@21083
   606
syntax (HTML output)
wenzelm@21180
   607
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   608
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   609
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   610
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   611
wenzelm@21180
   612
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   613
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   614
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   615
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   616
haftmann@21083
   617
translations
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
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   623
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   624
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   625
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   626
haftmann@21083
   627
print_translation {*
haftmann@21083
   628
let
wenzelm@42287
   629
  val All_binder = Mixfix.binder_name @{const_syntax All};
wenzelm@42287
   630
  val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
haftmann@38786
   631
  val impl = @{const_syntax HOL.implies};
haftmann@38795
   632
  val conj = @{const_syntax HOL.conj};
haftmann@22916
   633
  val less = @{const_syntax less};
haftmann@22916
   634
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   635
wenzelm@21180
   636
  val trans =
wenzelm@35115
   637
   [((All_binder, impl, less),
wenzelm@35115
   638
    (@{syntax_const "_All_less"}, @{syntax_const "_All_greater"})),
wenzelm@35115
   639
    ((All_binder, impl, less_eq),
wenzelm@35115
   640
    (@{syntax_const "_All_less_eq"}, @{syntax_const "_All_greater_eq"})),
wenzelm@35115
   641
    ((Ex_binder, conj, less),
wenzelm@35115
   642
    (@{syntax_const "_Ex_less"}, @{syntax_const "_Ex_greater"})),
wenzelm@35115
   643
    ((Ex_binder, conj, less_eq),
wenzelm@35115
   644
    (@{syntax_const "_Ex_less_eq"}, @{syntax_const "_Ex_greater_eq"}))];
wenzelm@21180
   645
wenzelm@35115
   646
  fun matches_bound v t =
wenzelm@35115
   647
    (case t of
wenzelm@35364
   648
      Const (@{syntax_const "_bound"}, _) $ Free (v', _) => v = v'
wenzelm@35115
   649
    | _ => false);
wenzelm@35115
   650
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false);
wenzelm@42284
   651
  fun mk v c n P = Syntax.const c $ Syntax_Trans.mark_bound v $ n $ P;
wenzelm@21180
   652
wenzelm@21180
   653
  fun tr' q = (q,
wenzelm@35364
   654
    fn [Const (@{syntax_const "_bound"}, _) $ Free (v, _),
wenzelm@35364
   655
        Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@35115
   656
        (case AList.lookup (op =) trans (q, c, d) of
wenzelm@35115
   657
          NONE => raise Match
wenzelm@35115
   658
        | SOME (l, g) =>
wenzelm@35115
   659
            if matches_bound v t andalso not (contains_var v u) then mk v l u P
wenzelm@35115
   660
            else if matches_bound v u andalso not (contains_var v t) then mk v g t P
wenzelm@35115
   661
            else raise Match)
wenzelm@21180
   662
     | _ => raise Match);
wenzelm@21524
   663
in [tr' All_binder, tr' Ex_binder] end
haftmann@21083
   664
*}
haftmann@21083
   665
haftmann@21083
   666
haftmann@21383
   667
subsection {* Transitivity reasoning *}
haftmann@21383
   668
haftmann@25193
   669
context ord
haftmann@25193
   670
begin
haftmann@21383
   671
haftmann@25193
   672
lemma ord_le_eq_trans: "a \<le> b \<Longrightarrow> b = c \<Longrightarrow> a \<le> c"
haftmann@25193
   673
  by (rule subst)
haftmann@21383
   674
haftmann@25193
   675
lemma ord_eq_le_trans: "a = b \<Longrightarrow> b \<le> c \<Longrightarrow> a \<le> c"
haftmann@25193
   676
  by (rule ssubst)
haftmann@21383
   677
haftmann@25193
   678
lemma ord_less_eq_trans: "a < b \<Longrightarrow> b = c \<Longrightarrow> a < c"
haftmann@25193
   679
  by (rule subst)
haftmann@25193
   680
haftmann@25193
   681
lemma ord_eq_less_trans: "a = b \<Longrightarrow> b < c \<Longrightarrow> a < c"
haftmann@25193
   682
  by (rule ssubst)
haftmann@25193
   683
haftmann@25193
   684
end
haftmann@21383
   685
haftmann@21383
   686
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   687
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   688
proof -
haftmann@21383
   689
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   690
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   691
  also assume "f b < c"
haftmann@34250
   692
  finally (less_trans) show ?thesis .
haftmann@21383
   693
qed
haftmann@21383
   694
haftmann@21383
   695
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   696
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   697
proof -
haftmann@21383
   698
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   699
  assume "a < f b"
haftmann@21383
   700
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   701
  finally (less_trans) show ?thesis .
haftmann@21383
   702
qed
haftmann@21383
   703
haftmann@21383
   704
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   705
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   706
proof -
haftmann@21383
   707
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   708
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   709
  also assume "f b < c"
haftmann@34250
   710
  finally (le_less_trans) show ?thesis .
haftmann@21383
   711
qed
haftmann@21383
   712
haftmann@21383
   713
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   714
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   715
proof -
haftmann@21383
   716
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   717
  assume "a <= f b"
haftmann@21383
   718
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@34250
   719
  finally (le_less_trans) show ?thesis .
haftmann@21383
   720
qed
haftmann@21383
   721
haftmann@21383
   722
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   723
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   724
proof -
haftmann@21383
   725
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   726
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   727
  also assume "f b <= c"
haftmann@34250
   728
  finally (less_le_trans) show ?thesis .
haftmann@21383
   729
qed
haftmann@21383
   730
haftmann@21383
   731
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   732
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   733
proof -
haftmann@21383
   734
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   735
  assume "a < f b"
haftmann@21383
   736
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@34250
   737
  finally (less_le_trans) show ?thesis .
haftmann@21383
   738
qed
haftmann@21383
   739
haftmann@21383
   740
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   741
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   742
proof -
haftmann@21383
   743
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   744
  assume "a <= f b"
haftmann@21383
   745
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   746
  finally (order_trans) show ?thesis .
haftmann@21383
   747
qed
haftmann@21383
   748
haftmann@21383
   749
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   750
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   751
proof -
haftmann@21383
   752
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   753
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   754
  also assume "f b <= c"
haftmann@21383
   755
  finally (order_trans) show ?thesis .
haftmann@21383
   756
qed
haftmann@21383
   757
haftmann@21383
   758
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   759
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   760
proof -
haftmann@21383
   761
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   762
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   763
  also assume "f b = c"
haftmann@21383
   764
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   765
qed
haftmann@21383
   766
haftmann@21383
   767
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   768
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   769
proof -
haftmann@21383
   770
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   771
  assume "a = f b"
haftmann@21383
   772
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   773
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   774
qed
haftmann@21383
   775
haftmann@21383
   776
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   777
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   778
proof -
haftmann@21383
   779
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   780
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   781
  also assume "f b = c"
haftmann@21383
   782
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   783
qed
haftmann@21383
   784
haftmann@21383
   785
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   786
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   787
proof -
haftmann@21383
   788
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   789
  assume "a = f b"
haftmann@21383
   790
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   791
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   792
qed
haftmann@21383
   793
haftmann@21383
   794
text {*
haftmann@21383
   795
  Note that this list of rules is in reverse order of priorities.
haftmann@21383
   796
*}
haftmann@21383
   797
haftmann@27682
   798
lemmas [trans] =
haftmann@21383
   799
  order_less_subst2
haftmann@21383
   800
  order_less_subst1
haftmann@21383
   801
  order_le_less_subst2
haftmann@21383
   802
  order_le_less_subst1
haftmann@21383
   803
  order_less_le_subst2
haftmann@21383
   804
  order_less_le_subst1
haftmann@21383
   805
  order_subst2
haftmann@21383
   806
  order_subst1
haftmann@21383
   807
  ord_le_eq_subst
haftmann@21383
   808
  ord_eq_le_subst
haftmann@21383
   809
  ord_less_eq_subst
haftmann@21383
   810
  ord_eq_less_subst
haftmann@21383
   811
  forw_subst
haftmann@21383
   812
  back_subst
haftmann@21383
   813
  rev_mp
haftmann@21383
   814
  mp
haftmann@27682
   815
haftmann@27682
   816
lemmas (in order) [trans] =
haftmann@27682
   817
  neq_le_trans
haftmann@27682
   818
  le_neq_trans
haftmann@27682
   819
haftmann@27682
   820
lemmas (in preorder) [trans] =
haftmann@27682
   821
  less_trans
haftmann@27682
   822
  less_asym'
haftmann@27682
   823
  le_less_trans
haftmann@27682
   824
  less_le_trans
haftmann@21383
   825
  order_trans
haftmann@27682
   826
haftmann@27682
   827
lemmas (in order) [trans] =
haftmann@27682
   828
  antisym
haftmann@27682
   829
haftmann@27682
   830
lemmas (in ord) [trans] =
haftmann@27682
   831
  ord_le_eq_trans
haftmann@27682
   832
  ord_eq_le_trans
haftmann@27682
   833
  ord_less_eq_trans
haftmann@27682
   834
  ord_eq_less_trans
haftmann@27682
   835
haftmann@27682
   836
lemmas [trans] =
haftmann@27682
   837
  trans
haftmann@27682
   838
haftmann@27682
   839
lemmas order_trans_rules =
haftmann@27682
   840
  order_less_subst2
haftmann@27682
   841
  order_less_subst1
haftmann@27682
   842
  order_le_less_subst2
haftmann@27682
   843
  order_le_less_subst1
haftmann@27682
   844
  order_less_le_subst2
haftmann@27682
   845
  order_less_le_subst1
haftmann@27682
   846
  order_subst2
haftmann@27682
   847
  order_subst1
haftmann@27682
   848
  ord_le_eq_subst
haftmann@27682
   849
  ord_eq_le_subst
haftmann@27682
   850
  ord_less_eq_subst
haftmann@27682
   851
  ord_eq_less_subst
haftmann@27682
   852
  forw_subst
haftmann@27682
   853
  back_subst
haftmann@27682
   854
  rev_mp
haftmann@27682
   855
  mp
haftmann@27682
   856
  neq_le_trans
haftmann@27682
   857
  le_neq_trans
haftmann@27682
   858
  less_trans
haftmann@27682
   859
  less_asym'
haftmann@27682
   860
  le_less_trans
haftmann@27682
   861
  less_le_trans
haftmann@27682
   862
  order_trans
haftmann@27682
   863
  antisym
haftmann@21383
   864
  ord_le_eq_trans
haftmann@21383
   865
  ord_eq_le_trans
haftmann@21383
   866
  ord_less_eq_trans
haftmann@21383
   867
  ord_eq_less_trans
haftmann@21383
   868
  trans
haftmann@21383
   869
haftmann@21083
   870
text {* These support proving chains of decreasing inequalities
haftmann@21083
   871
    a >= b >= c ... in Isar proofs. *}
haftmann@21083
   872
blanchet@45221
   873
lemma xt1 [no_atp]:
haftmann@21083
   874
  "a = b ==> b > c ==> a > c"
haftmann@21083
   875
  "a > b ==> b = c ==> a > c"
haftmann@21083
   876
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   877
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   878
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   879
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   880
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   881
  "(x::'a::order) >= y ==> y > z ==> x > z"
wenzelm@23417
   882
  "(a::'a::order) > b ==> b > a ==> P"
haftmann@21083
   883
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   884
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   885
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
haftmann@21083
   886
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
haftmann@21083
   887
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   888
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   889
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@25076
   890
  by auto
haftmann@21083
   891
blanchet@45221
   892
lemma xt2 [no_atp]:
haftmann@21083
   893
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   894
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   895
blanchet@45221
   896
lemma xt3 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==>
haftmann@21083
   897
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   898
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   899
blanchet@45221
   900
lemma xt4 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
   901
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
   902
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   903
blanchet@45221
   904
lemma xt5 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
   905
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   906
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   907
blanchet@45221
   908
lemma xt6 [no_atp]: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
   909
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   910
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   911
blanchet@45221
   912
lemma xt7 [no_atp]: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
   913
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
   914
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   915
blanchet@45221
   916
lemma xt8 [no_atp]: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
   917
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   918
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   919
blanchet@45221
   920
lemma xt9 [no_atp]: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
   921
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   922
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   923
blanchet@45221
   924
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9 [no_atp]
haftmann@21083
   925
haftmann@21083
   926
(* 
haftmann@21083
   927
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
   928
  for the wrong thing in an Isar proof.
haftmann@21083
   929
haftmann@21083
   930
  The extra transitivity rules can be used as follows: 
haftmann@21083
   931
haftmann@21083
   932
lemma "(a::'a::order) > z"
haftmann@21083
   933
proof -
haftmann@21083
   934
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
   935
    sorry
haftmann@21083
   936
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
   937
    sorry
haftmann@21083
   938
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
   939
    sorry
haftmann@21083
   940
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
   941
    sorry
haftmann@21083
   942
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
   943
    sorry
haftmann@21083
   944
  also (xtrans) have "?rhs > z"
haftmann@21083
   945
    sorry
haftmann@21083
   946
  finally (xtrans) show ?thesis .
haftmann@21083
   947
qed
haftmann@21083
   948
haftmann@21083
   949
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
   950
  leave out the "(xtrans)" above.
haftmann@21083
   951
*)
haftmann@21083
   952
haftmann@23881
   953
haftmann@23881
   954
subsection {* Monotonicity, least value operator and min/max *}
haftmann@21083
   955
haftmann@25076
   956
context order
haftmann@25076
   957
begin
haftmann@25076
   958
haftmann@30298
   959
definition mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool" where
haftmann@25076
   960
  "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
haftmann@25076
   961
haftmann@25076
   962
lemma monoI [intro?]:
haftmann@25076
   963
  fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
haftmann@25076
   964
  shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
haftmann@25076
   965
  unfolding mono_def by iprover
haftmann@21216
   966
haftmann@25076
   967
lemma monoD [dest?]:
haftmann@25076
   968
  fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
haftmann@25076
   969
  shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
haftmann@25076
   970
  unfolding mono_def by iprover
haftmann@25076
   971
haftmann@30298
   972
definition strict_mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool" where
haftmann@30298
   973
  "strict_mono f \<longleftrightarrow> (\<forall>x y. x < y \<longrightarrow> f x < f y)"
haftmann@30298
   974
haftmann@30298
   975
lemma strict_monoI [intro?]:
haftmann@30298
   976
  assumes "\<And>x y. x < y \<Longrightarrow> f x < f y"
haftmann@30298
   977
  shows "strict_mono f"
haftmann@30298
   978
  using assms unfolding strict_mono_def by auto
haftmann@30298
   979
haftmann@30298
   980
lemma strict_monoD [dest?]:
haftmann@30298
   981
  "strict_mono f \<Longrightarrow> x < y \<Longrightarrow> f x < f y"
haftmann@30298
   982
  unfolding strict_mono_def by auto
haftmann@30298
   983
haftmann@30298
   984
lemma strict_mono_mono [dest?]:
haftmann@30298
   985
  assumes "strict_mono f"
haftmann@30298
   986
  shows "mono f"
haftmann@30298
   987
proof (rule monoI)
haftmann@30298
   988
  fix x y
haftmann@30298
   989
  assume "x \<le> y"
haftmann@30298
   990
  show "f x \<le> f y"
haftmann@30298
   991
  proof (cases "x = y")
haftmann@30298
   992
    case True then show ?thesis by simp
haftmann@30298
   993
  next
haftmann@30298
   994
    case False with `x \<le> y` have "x < y" by simp
haftmann@30298
   995
    with assms strict_monoD have "f x < f y" by auto
haftmann@30298
   996
    then show ?thesis by simp
haftmann@30298
   997
  qed
haftmann@30298
   998
qed
haftmann@30298
   999
haftmann@25076
  1000
end
haftmann@25076
  1001
haftmann@25076
  1002
context linorder
haftmann@25076
  1003
begin
haftmann@25076
  1004
haftmann@30298
  1005
lemma strict_mono_eq:
haftmann@30298
  1006
  assumes "strict_mono f"
haftmann@30298
  1007
  shows "f x = f y \<longleftrightarrow> x = y"
haftmann@30298
  1008
proof
haftmann@30298
  1009
  assume "f x = f y"
haftmann@30298
  1010
  show "x = y" proof (cases x y rule: linorder_cases)
haftmann@30298
  1011
    case less with assms strict_monoD have "f x < f y" by auto
haftmann@30298
  1012
    with `f x = f y` show ?thesis by simp
haftmann@30298
  1013
  next
haftmann@30298
  1014
    case equal then show ?thesis .
haftmann@30298
  1015
  next
haftmann@30298
  1016
    case greater with assms strict_monoD have "f y < f x" by auto
haftmann@30298
  1017
    with `f x = f y` show ?thesis by simp
haftmann@30298
  1018
  qed
haftmann@30298
  1019
qed simp
haftmann@30298
  1020
haftmann@30298
  1021
lemma strict_mono_less_eq:
haftmann@30298
  1022
  assumes "strict_mono f"
haftmann@30298
  1023
  shows "f x \<le> f y \<longleftrightarrow> x \<le> y"
haftmann@30298
  1024
proof
haftmann@30298
  1025
  assume "x \<le> y"
haftmann@30298
  1026
  with assms strict_mono_mono monoD show "f x \<le> f y" by auto
haftmann@30298
  1027
next
haftmann@30298
  1028
  assume "f x \<le> f y"
haftmann@30298
  1029
  show "x \<le> y" proof (rule ccontr)
haftmann@30298
  1030
    assume "\<not> x \<le> y" then have "y < x" by simp
haftmann@30298
  1031
    with assms strict_monoD have "f y < f x" by auto
haftmann@30298
  1032
    with `f x \<le> f y` show False by simp
haftmann@30298
  1033
  qed
haftmann@30298
  1034
qed
haftmann@30298
  1035
  
haftmann@30298
  1036
lemma strict_mono_less:
haftmann@30298
  1037
  assumes "strict_mono f"
haftmann@30298
  1038
  shows "f x < f y \<longleftrightarrow> x < y"
haftmann@30298
  1039
  using assms
haftmann@30298
  1040
    by (auto simp add: less_le Orderings.less_le strict_mono_eq strict_mono_less_eq)
haftmann@30298
  1041
haftmann@25076
  1042
lemma min_of_mono:
haftmann@25076
  1043
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
wenzelm@25377
  1044
  shows "mono f \<Longrightarrow> min (f m) (f n) = f (min m n)"
haftmann@25076
  1045
  by (auto simp: mono_def Orderings.min_def min_def intro: Orderings.antisym)
haftmann@25076
  1046
haftmann@25076
  1047
lemma max_of_mono:
haftmann@25076
  1048
  fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
wenzelm@25377
  1049
  shows "mono f \<Longrightarrow> max (f m) (f n) = f (max m n)"
haftmann@25076
  1050
  by (auto simp: mono_def Orderings.max_def max_def intro: Orderings.antisym)
haftmann@25076
  1051
haftmann@25076
  1052
end
haftmann@21083
  1053
noschinl@45931
  1054
lemma min_absorb1: "x \<le> y \<Longrightarrow> min x y = x"
nipkow@23212
  1055
by (simp add: min_def)
haftmann@21383
  1056
noschinl@45931
  1057
lemma max_absorb2: "x \<le> y ==> max x y = y"
nipkow@23212
  1058
by (simp add: max_def)
haftmann@21383
  1059
noschinl@45931
  1060
lemma min_absorb2: "(y\<Colon>'a\<Colon>order) \<le> x \<Longrightarrow> min x y = y"
noschinl@45931
  1061
by (simp add:min_def)
noschinl@45893
  1062
noschinl@45931
  1063
lemma max_absorb1: "(y\<Colon>'a\<Colon>order) \<le> x \<Longrightarrow> max x y = x"
noschinl@45893
  1064
by (simp add: max_def)
noschinl@45893
  1065
noschinl@45893
  1066
haftmann@21383
  1067
haftmann@43813
  1068
subsection {* (Unique) top and bottom elements *}
haftmann@28685
  1069
haftmann@43813
  1070
class bot = order +
haftmann@43853
  1071
  fixes bot :: 'a ("\<bottom>")
haftmann@43853
  1072
  assumes bot_least [simp]: "\<bottom> \<le> a"
haftmann@43814
  1073
begin
haftmann@43814
  1074
haftmann@43853
  1075
lemma le_bot:
haftmann@43853
  1076
  "a \<le> \<bottom> \<Longrightarrow> a = \<bottom>"
haftmann@43853
  1077
  by (auto intro: antisym)
haftmann@43853
  1078
haftmann@43816
  1079
lemma bot_unique:
haftmann@43853
  1080
  "a \<le> \<bottom> \<longleftrightarrow> a = \<bottom>"
haftmann@43853
  1081
  by (auto intro: antisym)
haftmann@43853
  1082
haftmann@43853
  1083
lemma not_less_bot [simp]:
haftmann@43853
  1084
  "\<not> (a < \<bottom>)"
haftmann@43853
  1085
  using bot_least [of a] by (auto simp: le_less)
haftmann@43816
  1086
haftmann@43814
  1087
lemma bot_less:
haftmann@43853
  1088
  "a \<noteq> \<bottom> \<longleftrightarrow> \<bottom> < a"
haftmann@43814
  1089
  by (auto simp add: less_le_not_le intro!: antisym)
haftmann@43814
  1090
haftmann@43814
  1091
end
haftmann@41082
  1092
haftmann@43813
  1093
class top = order +
haftmann@43853
  1094
  fixes top :: 'a ("\<top>")
haftmann@43853
  1095
  assumes top_greatest [simp]: "a \<le> \<top>"
haftmann@43814
  1096
begin
haftmann@43814
  1097
haftmann@43853
  1098
lemma top_le:
haftmann@43853
  1099
  "\<top> \<le> a \<Longrightarrow> a = \<top>"
haftmann@43853
  1100
  by (rule antisym) auto
haftmann@43853
  1101
haftmann@43816
  1102
lemma top_unique:
haftmann@43853
  1103
  "\<top> \<le> a \<longleftrightarrow> a = \<top>"
haftmann@43853
  1104
  by (auto intro: antisym)
haftmann@43853
  1105
haftmann@43853
  1106
lemma not_top_less [simp]: "\<not> (\<top> < a)"
haftmann@43853
  1107
  using top_greatest [of a] by (auto simp: le_less)
haftmann@43816
  1108
haftmann@43814
  1109
lemma less_top:
haftmann@43853
  1110
  "a \<noteq> \<top> \<longleftrightarrow> a < \<top>"
haftmann@43814
  1111
  by (auto simp add: less_le_not_le intro!: antisym)
haftmann@43814
  1112
haftmann@43814
  1113
end
haftmann@28685
  1114
haftmann@28685
  1115
haftmann@27823
  1116
subsection {* Dense orders *}
haftmann@27823
  1117
haftmann@35028
  1118
class dense_linorder = linorder + 
haftmann@27823
  1119
  assumes gt_ex: "\<exists>y. x < y" 
haftmann@27823
  1120
  and lt_ex: "\<exists>y. y < x"
haftmann@27823
  1121
  and dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
hoelzl@35579
  1122
begin
haftmann@27823
  1123
hoelzl@35579
  1124
lemma dense_le:
hoelzl@35579
  1125
  fixes y z :: 'a
hoelzl@35579
  1126
  assumes "\<And>x. x < y \<Longrightarrow> x \<le> z"
hoelzl@35579
  1127
  shows "y \<le> z"
hoelzl@35579
  1128
proof (rule ccontr)
hoelzl@35579
  1129
  assume "\<not> ?thesis"
hoelzl@35579
  1130
  hence "z < y" by simp
hoelzl@35579
  1131
  from dense[OF this]
hoelzl@35579
  1132
  obtain x where "x < y" and "z < x" by safe
hoelzl@35579
  1133
  moreover have "x \<le> z" using assms[OF `x < y`] .
hoelzl@35579
  1134
  ultimately show False by auto
hoelzl@35579
  1135
qed
hoelzl@35579
  1136
hoelzl@35579
  1137
lemma dense_le_bounded:
hoelzl@35579
  1138
  fixes x y z :: 'a
hoelzl@35579
  1139
  assumes "x < y"
hoelzl@35579
  1140
  assumes *: "\<And>w. \<lbrakk> x < w ; w < y \<rbrakk> \<Longrightarrow> w \<le> z"
hoelzl@35579
  1141
  shows "y \<le> z"
hoelzl@35579
  1142
proof (rule dense_le)
hoelzl@35579
  1143
  fix w assume "w < y"
hoelzl@35579
  1144
  from dense[OF `x < y`] obtain u where "x < u" "u < y" by safe
hoelzl@35579
  1145
  from linear[of u w]
hoelzl@35579
  1146
  show "w \<le> z"
hoelzl@35579
  1147
  proof (rule disjE)
hoelzl@35579
  1148
    assume "u \<le> w"
hoelzl@35579
  1149
    from less_le_trans[OF `x < u` `u \<le> w`] `w < y`
hoelzl@35579
  1150
    show "w \<le> z" by (rule *)
hoelzl@35579
  1151
  next
hoelzl@35579
  1152
    assume "w \<le> u"
hoelzl@35579
  1153
    from `w \<le> u` *[OF `x < u` `u < y`]
hoelzl@35579
  1154
    show "w \<le> z" by (rule order_trans)
hoelzl@35579
  1155
  qed
hoelzl@35579
  1156
qed
hoelzl@35579
  1157
hoelzl@35579
  1158
end
haftmann@27823
  1159
haftmann@27823
  1160
subsection {* Wellorders *}
haftmann@27823
  1161
haftmann@27823
  1162
class wellorder = linorder +
haftmann@27823
  1163
  assumes less_induct [case_names less]: "(\<And>x. (\<And>y. y < x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P a"
haftmann@27823
  1164
begin
haftmann@27823
  1165
haftmann@27823
  1166
lemma wellorder_Least_lemma:
haftmann@27823
  1167
  fixes k :: 'a
haftmann@27823
  1168
  assumes "P k"
haftmann@34250
  1169
  shows LeastI: "P (LEAST x. P x)" and Least_le: "(LEAST x. P x) \<le> k"
haftmann@27823
  1170
proof -
haftmann@27823
  1171
  have "P (LEAST x. P x) \<and> (LEAST x. P x) \<le> k"
haftmann@27823
  1172
  using assms proof (induct k rule: less_induct)
haftmann@27823
  1173
    case (less x) then have "P x" by simp
haftmann@27823
  1174
    show ?case proof (rule classical)
haftmann@27823
  1175
      assume assm: "\<not> (P (LEAST a. P a) \<and> (LEAST a. P a) \<le> x)"
haftmann@27823
  1176
      have "\<And>y. P y \<Longrightarrow> x \<le> y"
haftmann@27823
  1177
      proof (rule classical)
haftmann@27823
  1178
        fix y
hoelzl@38705
  1179
        assume "P y" and "\<not> x \<le> y"
haftmann@27823
  1180
        with less have "P (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1181
          by (auto simp add: not_le)
haftmann@27823
  1182
        with assm have "x < (LEAST a. P a)" and "(LEAST a. P a) \<le> y"
haftmann@27823
  1183
          by auto
haftmann@27823
  1184
        then show "x \<le> y" by auto
haftmann@27823
  1185
      qed
haftmann@27823
  1186
      with `P x` have Least: "(LEAST a. P a) = x"
haftmann@27823
  1187
        by (rule Least_equality)
haftmann@27823
  1188
      with `P x` show ?thesis by simp
haftmann@27823
  1189
    qed
haftmann@27823
  1190
  qed
haftmann@27823
  1191
  then show "P (LEAST x. P x)" and "(LEAST x. P x) \<le> k" by auto
haftmann@27823
  1192
qed
haftmann@27823
  1193
haftmann@27823
  1194
-- "The following 3 lemmas are due to Brian Huffman"
haftmann@27823
  1195
lemma LeastI_ex: "\<exists>x. P x \<Longrightarrow> P (Least P)"
haftmann@27823
  1196
  by (erule exE) (erule LeastI)
haftmann@27823
  1197
haftmann@27823
  1198
lemma LeastI2:
haftmann@27823
  1199
  "P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1200
  by (blast intro: LeastI)
haftmann@27823
  1201
haftmann@27823
  1202
lemma LeastI2_ex:
haftmann@27823
  1203
  "\<exists>a. P a \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> Q (Least P)"
haftmann@27823
  1204
  by (blast intro: LeastI_ex)
haftmann@27823
  1205
hoelzl@38705
  1206
lemma LeastI2_wellorder:
hoelzl@38705
  1207
  assumes "P a"
hoelzl@38705
  1208
  and "\<And>a. \<lbrakk> P a; \<forall>b. P b \<longrightarrow> a \<le> b \<rbrakk> \<Longrightarrow> Q a"
hoelzl@38705
  1209
  shows "Q (Least P)"
hoelzl@38705
  1210
proof (rule LeastI2_order)
hoelzl@38705
  1211
  show "P (Least P)" using `P a` by (rule LeastI)
hoelzl@38705
  1212
next
hoelzl@38705
  1213
  fix y assume "P y" thus "Least P \<le> y" by (rule Least_le)
hoelzl@38705
  1214
next
hoelzl@38705
  1215
  fix x assume "P x" "\<forall>y. P y \<longrightarrow> x \<le> y" thus "Q x" by (rule assms(2))
hoelzl@38705
  1216
qed
hoelzl@38705
  1217
haftmann@27823
  1218
lemma not_less_Least: "k < (LEAST x. P x) \<Longrightarrow> \<not> P k"
haftmann@27823
  1219
apply (simp (no_asm_use) add: not_le [symmetric])
haftmann@27823
  1220
apply (erule contrapos_nn)
haftmann@27823
  1221
apply (erule Least_le)
haftmann@27823
  1222
done
haftmann@27823
  1223
hoelzl@38705
  1224
end
haftmann@27823
  1225
haftmann@28685
  1226
haftmann@46631
  1227
subsection {* Order on @{typ bool} *}
haftmann@28685
  1228
huffman@45262
  1229
instantiation bool :: "{bot, top, linorder}"
haftmann@28685
  1230
begin
haftmann@28685
  1231
haftmann@28685
  1232
definition
haftmann@41080
  1233
  le_bool_def [simp]: "P \<le> Q \<longleftrightarrow> P \<longrightarrow> Q"
haftmann@28685
  1234
haftmann@28685
  1235
definition
haftmann@41080
  1236
  [simp]: "(P\<Colon>bool) < Q \<longleftrightarrow> \<not> P \<and> Q"
haftmann@28685
  1237
haftmann@28685
  1238
definition
haftmann@46631
  1239
  [simp]: "\<bottom> \<longleftrightarrow> False"
haftmann@28685
  1240
haftmann@28685
  1241
definition
haftmann@46631
  1242
  [simp]: "\<top> \<longleftrightarrow> True"
haftmann@28685
  1243
haftmann@28685
  1244
instance proof
haftmann@41080
  1245
qed auto
haftmann@28685
  1246
nipkow@15524
  1247
end
haftmann@28685
  1248
haftmann@28685
  1249
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
haftmann@41080
  1250
  by simp
haftmann@28685
  1251
haftmann@28685
  1252
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
haftmann@41080
  1253
  by simp
haftmann@28685
  1254
haftmann@28685
  1255
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@41080
  1256
  by simp
haftmann@28685
  1257
haftmann@28685
  1258
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
haftmann@41080
  1259
  by simp
haftmann@32899
  1260
haftmann@46631
  1261
lemma bot_boolE: "\<bottom> \<Longrightarrow> P"
haftmann@41080
  1262
  by simp
haftmann@32899
  1263
haftmann@46631
  1264
lemma top_boolI: \<top>
haftmann@41080
  1265
  by simp
haftmann@28685
  1266
haftmann@28685
  1267
lemma [code]:
haftmann@28685
  1268
  "False \<le> b \<longleftrightarrow> True"
haftmann@28685
  1269
  "True \<le> b \<longleftrightarrow> b"
haftmann@28685
  1270
  "False < b \<longleftrightarrow> b"
haftmann@28685
  1271
  "True < b \<longleftrightarrow> False"
haftmann@41080
  1272
  by simp_all
haftmann@28685
  1273
haftmann@28685
  1274
haftmann@46631
  1275
subsection {* Order on @{typ "_ \<Rightarrow> _"} *}
haftmann@28685
  1276
haftmann@28685
  1277
instantiation "fun" :: (type, ord) ord
haftmann@28685
  1278
begin
haftmann@28685
  1279
haftmann@28685
  1280
definition
haftmann@37767
  1281
  le_fun_def: "f \<le> g \<longleftrightarrow> (\<forall>x. f x \<le> g x)"
haftmann@28685
  1282
haftmann@28685
  1283
definition
haftmann@41080
  1284
  "(f\<Colon>'a \<Rightarrow> 'b) < g \<longleftrightarrow> f \<le> g \<and> \<not> (g \<le> f)"
haftmann@28685
  1285
haftmann@28685
  1286
instance ..
haftmann@28685
  1287
haftmann@28685
  1288
end
haftmann@28685
  1289
haftmann@28685
  1290
instance "fun" :: (type, preorder) preorder proof
haftmann@28685
  1291
qed (auto simp add: le_fun_def less_fun_def
huffman@44921
  1292
  intro: order_trans antisym)
haftmann@28685
  1293
haftmann@28685
  1294
instance "fun" :: (type, order) order proof
huffman@44921
  1295
qed (auto simp add: le_fun_def intro: antisym)
haftmann@28685
  1296
haftmann@41082
  1297
instantiation "fun" :: (type, bot) bot
haftmann@41082
  1298
begin
haftmann@41082
  1299
haftmann@41082
  1300
definition
haftmann@46631
  1301
  "\<bottom> = (\<lambda>x. \<bottom>)"
haftmann@41082
  1302
noschinl@46882
  1303
lemma bot_apply [simp] (* CANDIDATE [code] *):
haftmann@46631
  1304
  "\<bottom> x = \<bottom>"
haftmann@41082
  1305
  by (simp add: bot_fun_def)
haftmann@41082
  1306
haftmann@41082
  1307
instance proof
noschinl@46884
  1308
qed (simp add: le_fun_def)
haftmann@41082
  1309
haftmann@41082
  1310
end
haftmann@41082
  1311
haftmann@28685
  1312
instantiation "fun" :: (type, top) top
haftmann@28685
  1313
begin
haftmann@28685
  1314
haftmann@28685
  1315
definition
haftmann@46631
  1316
  [no_atp]: "\<top> = (\<lambda>x. \<top>)"
haftmann@41075
  1317
declare top_fun_def_raw [no_atp]
haftmann@28685
  1318
noschinl@46882
  1319
lemma top_apply [simp] (* CANDIDATE [code] *):
haftmann@46631
  1320
  "\<top> x = \<top>"
haftmann@41080
  1321
  by (simp add: top_fun_def)
haftmann@41080
  1322
haftmann@28685
  1323
instance proof
noschinl@46884
  1324
qed (simp add: le_fun_def)
haftmann@28685
  1325
haftmann@28685
  1326
end
haftmann@28685
  1327
haftmann@28685
  1328
lemma le_funI: "(\<And>x. f x \<le> g x) \<Longrightarrow> f \<le> g"
haftmann@28685
  1329
  unfolding le_fun_def by simp
haftmann@28685
  1330
haftmann@28685
  1331
lemma le_funE: "f \<le> g \<Longrightarrow> (f x \<le> g x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@28685
  1332
  unfolding le_fun_def by simp
haftmann@28685
  1333
haftmann@28685
  1334
lemma le_funD: "f \<le> g \<Longrightarrow> f x \<le> g x"
haftmann@28685
  1335
  unfolding le_fun_def by simp
haftmann@28685
  1336
haftmann@34250
  1337
haftmann@46631
  1338
subsection {* Order on unary and binary predicates *}
haftmann@46631
  1339
haftmann@46631
  1340
lemma predicate1I:
haftmann@46631
  1341
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
haftmann@46631
  1342
  shows "P \<le> Q"
haftmann@46631
  1343
  apply (rule le_funI)
haftmann@46631
  1344
  apply (rule le_boolI)
haftmann@46631
  1345
  apply (rule PQ)
haftmann@46631
  1346
  apply assumption
haftmann@46631
  1347
  done
haftmann@46631
  1348
haftmann@46631
  1349
lemma predicate1D:
haftmann@46631
  1350
  "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
haftmann@46631
  1351
  apply (erule le_funE)
haftmann@46631
  1352
  apply (erule le_boolE)
haftmann@46631
  1353
  apply assumption+
haftmann@46631
  1354
  done
haftmann@46631
  1355
haftmann@46631
  1356
lemma rev_predicate1D:
haftmann@46631
  1357
  "P x \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x"
haftmann@46631
  1358
  by (rule predicate1D)
haftmann@46631
  1359
haftmann@46631
  1360
lemma predicate2I:
haftmann@46631
  1361
  assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
haftmann@46631
  1362
  shows "P \<le> Q"
haftmann@46631
  1363
  apply (rule le_funI)+
haftmann@46631
  1364
  apply (rule le_boolI)
haftmann@46631
  1365
  apply (rule PQ)
haftmann@46631
  1366
  apply assumption
haftmann@46631
  1367
  done
haftmann@46631
  1368
haftmann@46631
  1369
lemma predicate2D:
haftmann@46631
  1370
  "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
haftmann@46631
  1371
  apply (erule le_funE)+
haftmann@46631
  1372
  apply (erule le_boolE)
haftmann@46631
  1373
  apply assumption+
haftmann@46631
  1374
  done
haftmann@46631
  1375
haftmann@46631
  1376
lemma rev_predicate2D:
haftmann@46631
  1377
  "P x y \<Longrightarrow> P \<le> Q \<Longrightarrow> Q x y"
haftmann@46631
  1378
  by (rule predicate2D)
haftmann@46631
  1379
haftmann@46631
  1380
lemma bot1E [no_atp]: "\<bottom> x \<Longrightarrow> P"
haftmann@46631
  1381
  by (simp add: bot_fun_def)
haftmann@46631
  1382
haftmann@46631
  1383
lemma bot2E: "\<bottom> x y \<Longrightarrow> P"
haftmann@46631
  1384
  by (simp add: bot_fun_def)
haftmann@46631
  1385
haftmann@46631
  1386
lemma top1I: "\<top> x"
haftmann@46631
  1387
  by (simp add: top_fun_def)
haftmann@46631
  1388
haftmann@46631
  1389
lemma top2I: "\<top> x y"
haftmann@46631
  1390
  by (simp add: top_fun_def)
haftmann@46631
  1391
haftmann@46631
  1392
haftmann@34250
  1393
subsection {* Name duplicates *}
haftmann@34250
  1394
haftmann@34250
  1395
lemmas order_eq_refl = preorder_class.eq_refl
haftmann@34250
  1396
lemmas order_less_irrefl = preorder_class.less_irrefl
haftmann@34250
  1397
lemmas order_less_imp_le = preorder_class.less_imp_le
haftmann@34250
  1398
lemmas order_less_not_sym = preorder_class.less_not_sym
haftmann@34250
  1399
lemmas order_less_asym = preorder_class.less_asym
haftmann@34250
  1400
lemmas order_less_trans = preorder_class.less_trans
haftmann@34250
  1401
lemmas order_le_less_trans = preorder_class.le_less_trans
haftmann@34250
  1402
lemmas order_less_le_trans = preorder_class.less_le_trans
haftmann@34250
  1403
lemmas order_less_imp_not_less = preorder_class.less_imp_not_less
haftmann@34250
  1404
lemmas order_less_imp_triv = preorder_class.less_imp_triv
haftmann@34250
  1405
lemmas order_less_asym' = preorder_class.less_asym'
haftmann@34250
  1406
haftmann@34250
  1407
lemmas order_less_le = order_class.less_le
haftmann@34250
  1408
lemmas order_le_less = order_class.le_less
haftmann@34250
  1409
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@34250
  1410
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@34250
  1411
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@34250
  1412
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@34250
  1413
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@34250
  1414
lemmas order_antisym = order_class.antisym
haftmann@34250
  1415
lemmas order_eq_iff = order_class.eq_iff
haftmann@34250
  1416
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@34250
  1417
haftmann@34250
  1418
lemmas linorder_linear = linorder_class.linear
haftmann@34250
  1419
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@34250
  1420
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@34250
  1421
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@34250
  1422
lemmas linorder_not_less = linorder_class.not_less
haftmann@34250
  1423
lemmas linorder_not_le = linorder_class.not_le
haftmann@34250
  1424
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@34250
  1425
lemmas linorder_neqE = linorder_class.neqE
haftmann@34250
  1426
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@34250
  1427
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@34250
  1428
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@34250
  1429
haftmann@28685
  1430
end