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