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