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