src/HOL/Orderings.thy
author haftmann
Thu May 24 08:37:39 2007 +0200 (2007-05-24)
changeset 23087 ad7244663431
parent 23032 b6cb6a131511
child 23182 01fa88b79ddc
permissions -rw-r--r--
rudimentary class target implementation
nipkow@15524
     1
(*  Title:      HOL/Orderings.thy
nipkow@15524
     2
    ID:         $Id$
nipkow@15524
     3
    Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
nipkow@15524
     4
*)
nipkow@15524
     5
haftmann@21329
     6
header {* Syntactic and abstract orders *}
nipkow@15524
     7
nipkow@15524
     8
theory Orderings
haftmann@22886
     9
imports Code_Generator
nipkow@15524
    10
begin
nipkow@15524
    11
haftmann@21329
    12
subsection {* Order syntax *}
nipkow@15524
    13
haftmann@22473
    14
class ord = type +
wenzelm@21656
    15
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  (infix "\<sqsubseteq>" 50)
wenzelm@21656
    16
    and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  (infix "\<sqsubset>" 50)
wenzelm@21204
    17
begin
wenzelm@21204
    18
wenzelm@21204
    19
notation
wenzelm@21656
    20
  less_eq  ("op \<^loc><=") and
haftmann@21620
    21
  less_eq  ("(_/ \<^loc><= _)" [51, 51] 50) and
wenzelm@21656
    22
  less  ("op \<^loc><") and
wenzelm@21656
    23
  less  ("(_/ \<^loc>< _)"  [51, 51] 50)
haftmann@21620
    24
  
wenzelm@21204
    25
notation (xsymbols)
wenzelm@21404
    26
  less_eq  ("op \<^loc>\<le>") and
wenzelm@21259
    27
  less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
nipkow@15524
    28
wenzelm@21204
    29
notation (HTML output)
wenzelm@21404
    30
  less_eq  ("op \<^loc>\<le>") and
wenzelm@21259
    31
  less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
wenzelm@21204
    32
wenzelm@21204
    33
abbreviation (input)
wenzelm@21656
    34
  greater  (infix "\<^loc>>" 50) where
haftmann@21620
    35
  "x \<^loc>> y \<equiv> y \<^loc>< x"
haftmann@21620
    36
wenzelm@21656
    37
abbreviation (input)
wenzelm@21656
    38
  greater_eq  (infix "\<^loc>>=" 50) where
wenzelm@21656
    39
  "x \<^loc>>= y \<equiv> y \<^loc><= x"
wenzelm@21204
    40
wenzelm@21656
    41
notation (input)
wenzelm@21656
    42
  greater_eq  (infix "\<^loc>\<ge>" 50)
wenzelm@21204
    43
haftmann@22738
    44
text {*
haftmann@22738
    45
  syntactic min/max -- these definitions reach
haftmann@22738
    46
  their usual semantics in class linorder ahead.
haftmann@22738
    47
*}
haftmann@22738
    48
haftmann@22738
    49
definition
haftmann@22738
    50
  min :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@22841
    51
  "min a b = (if a \<^loc>\<le> b then a else b)"
haftmann@22738
    52
haftmann@22738
    53
definition
haftmann@22738
    54
  max :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@22841
    55
  "max a b = (if a \<^loc>\<le> b then b else a)"
haftmann@22738
    56
wenzelm@21204
    57
end
wenzelm@21204
    58
wenzelm@21204
    59
notation
wenzelm@21656
    60
  less_eq  ("op <=") and
haftmann@21620
    61
  less_eq  ("(_/ <= _)" [51, 51] 50) and
wenzelm@21656
    62
  less  ("op <") and
wenzelm@21656
    63
  less  ("(_/ < _)"  [51, 51] 50)
wenzelm@21204
    64
  
wenzelm@21204
    65
notation (xsymbols)
wenzelm@21404
    66
  less_eq  ("op \<le>") and
wenzelm@21259
    67
  less_eq  ("(_/ \<le> _)"  [51, 51] 50)
nipkow@15524
    68
wenzelm@21204
    69
notation (HTML output)
wenzelm@21404
    70
  less_eq  ("op \<le>") and
wenzelm@21259
    71
  less_eq  ("(_/ \<le> _)"  [51, 51] 50)
haftmann@20714
    72
wenzelm@19536
    73
abbreviation (input)
wenzelm@21656
    74
  greater  (infix ">" 50) where
haftmann@21620
    75
  "x > y \<equiv> y < x"
haftmann@21620
    76
wenzelm@21656
    77
abbreviation (input)
wenzelm@21656
    78
  greater_eq  (infix ">=" 50) where
wenzelm@21656
    79
  "x >= y \<equiv> y <= x"
haftmann@21620
    80
wenzelm@21656
    81
notation (input)
wenzelm@21656
    82
  greater_eq  (infix "\<ge>" 50)
nipkow@15524
    83
haftmann@23087
    84
lemmas min_def [code func, code unfold, code inline del] = min_def [folded ord_class.min]
haftmann@23087
    85
lemmas max_def [code func, code unfold, code inline del] = max_def [folded ord_class.max]
haftmann@22738
    86
nipkow@15524
    87
haftmann@22841
    88
subsection {* Partial orders *}
nipkow@15524
    89
haftmann@22841
    90
class order = ord +
haftmann@22316
    91
  assumes less_le: "x \<sqsubset> y \<longleftrightarrow> x \<sqsubseteq> y \<and> x \<noteq> y"
haftmann@22384
    92
  and order_refl [iff]: "x \<sqsubseteq> x"
haftmann@22384
    93
  and order_trans: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> z"
haftmann@22841
    94
  assumes antisym: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> x \<Longrightarrow> x = y"
haftmann@22841
    95
haftmann@21248
    96
begin
haftmann@21248
    97
nipkow@15524
    98
text {* Reflexivity. *}
nipkow@15524
    99
haftmann@22841
   100
lemma eq_refl: "x = y \<Longrightarrow> x \<^loc>\<le> y"
nipkow@15524
   101
    -- {* This form is useful with the classical reasoner. *}
haftmann@22384
   102
  by (erule ssubst) (rule order_refl)
nipkow@15524
   103
haftmann@22841
   104
lemma less_irrefl [iff]: "\<not> x \<^loc>< x"
haftmann@21248
   105
  by (simp add: less_le)
nipkow@15524
   106
haftmann@22841
   107
lemma le_less: "x \<^loc>\<le> y \<longleftrightarrow> x \<^loc>< y \<or> x = y"
nipkow@15524
   108
    -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
haftmann@21248
   109
  by (simp add: less_le) blast
nipkow@15524
   110
haftmann@22841
   111
lemma le_imp_less_or_eq: "x \<^loc>\<le> y \<Longrightarrow> x \<^loc>< y \<or> x = y"
haftmann@21248
   112
  unfolding less_le by blast
nipkow@15524
   113
haftmann@22841
   114
lemma less_imp_le: "x \<^loc>< y \<Longrightarrow> x \<^loc>\<le> y"
haftmann@21248
   115
  unfolding less_le by blast
haftmann@21248
   116
haftmann@22841
   117
lemma less_imp_neq: "x \<^loc>< y \<Longrightarrow> x \<noteq> y"
haftmann@21329
   118
  by (erule contrapos_pn, erule subst, rule less_irrefl)
haftmann@21329
   119
haftmann@21329
   120
haftmann@21329
   121
text {* Useful for simplification, but too risky to include by default. *}
haftmann@21329
   122
haftmann@22841
   123
lemma less_imp_not_eq: "x \<^loc>< y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
haftmann@21329
   124
  by auto
haftmann@21329
   125
haftmann@22841
   126
lemma less_imp_not_eq2: "x \<^loc>< y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
haftmann@21329
   127
  by auto
haftmann@21329
   128
haftmann@21329
   129
haftmann@21329
   130
text {* Transitivity rules for calculational reasoning *}
haftmann@21329
   131
haftmann@22841
   132
lemma neq_le_trans: "a \<noteq> b \<Longrightarrow> a \<^loc>\<le> b \<Longrightarrow> a \<^loc>< b"
haftmann@21329
   133
  by (simp add: less_le)
haftmann@21329
   134
haftmann@22841
   135
lemma le_neq_trans: "a \<^loc>\<le> b \<Longrightarrow> a \<noteq> b \<Longrightarrow> a \<^loc>< b"
haftmann@22841
   136
  by (simp add: less_le)
haftmann@21329
   137
nipkow@15524
   138
nipkow@15524
   139
text {* Asymmetry. *}
nipkow@15524
   140
haftmann@22841
   141
lemma less_not_sym: "x \<^loc>< y \<Longrightarrow> \<not> (y \<^loc>< x)"
haftmann@21248
   142
  by (simp add: less_le antisym)
nipkow@15524
   143
haftmann@22841
   144
lemma less_asym: "x \<^loc>< y \<Longrightarrow> (\<not> P \<Longrightarrow> y \<^loc>< x) \<Longrightarrow> P"
haftmann@21248
   145
  by (drule less_not_sym, erule contrapos_np) simp
nipkow@15524
   146
haftmann@22841
   147
lemma eq_iff: "x = y \<longleftrightarrow> x \<^loc>\<le> y \<and> y \<^loc>\<le> x"
haftmann@21248
   148
  by (blast intro: antisym)
nipkow@15524
   149
haftmann@22841
   150
lemma antisym_conv: "y \<^loc>\<le> x \<Longrightarrow> x \<^loc>\<le> y \<longleftrightarrow> x = y"
haftmann@21248
   151
  by (blast intro: antisym)
nipkow@15524
   152
haftmann@22841
   153
lemma less_imp_neq: "x \<^loc>< y \<Longrightarrow> x \<noteq> y"
haftmann@21248
   154
  by (erule contrapos_pn, erule subst, rule less_irrefl)
haftmann@21248
   155
haftmann@21083
   156
nipkow@15524
   157
text {* Transitivity. *}
nipkow@15524
   158
haftmann@22841
   159
lemma less_trans: "x \<^loc>< y \<Longrightarrow> y \<^loc>< z \<Longrightarrow> x \<^loc>< z"
haftmann@22384
   160
  by (simp add: less_le) (blast intro: order_trans antisym)
nipkow@15524
   161
haftmann@22841
   162
lemma le_less_trans: "x \<^loc>\<le> y \<Longrightarrow> y \<^loc>< z \<Longrightarrow> x \<^loc>< z"
haftmann@22384
   163
  by (simp add: less_le) (blast intro: order_trans antisym)
nipkow@15524
   164
haftmann@22841
   165
lemma less_le_trans: "x \<^loc>< y \<Longrightarrow> y \<^loc>\<le> z \<Longrightarrow> x \<^loc>< z"
haftmann@22384
   166
  by (simp add: less_le) (blast intro: order_trans antisym)
nipkow@15524
   167
nipkow@15524
   168
nipkow@15524
   169
text {* Useful for simplification, but too risky to include by default. *}
nipkow@15524
   170
haftmann@22841
   171
lemma less_imp_not_less: "x \<^loc>< y \<Longrightarrow> (\<not> y \<^loc>< x) \<longleftrightarrow> True"
haftmann@21248
   172
  by (blast elim: less_asym)
nipkow@15524
   173
haftmann@22841
   174
lemma less_imp_triv: "x \<^loc>< y \<Longrightarrow> (y \<^loc>< x \<longrightarrow> P) \<longleftrightarrow> True"
haftmann@21248
   175
  by (blast elim: less_asym)
nipkow@15524
   176
haftmann@21248
   177
haftmann@21083
   178
text {* Transitivity rules for calculational reasoning *}
nipkow@15524
   179
haftmann@22841
   180
lemma less_asym': "a \<^loc>< b \<Longrightarrow> b \<^loc>< a \<Longrightarrow> P"
haftmann@21248
   181
  by (rule less_asym)
haftmann@21248
   182
haftmann@22916
   183
haftmann@22916
   184
text {* Reverse order *}
haftmann@22916
   185
haftmann@22916
   186
lemma order_reverse:
haftmann@23018
   187
  "order (\<lambda>x y. y \<^loc>\<le> x) (\<lambda>x y. y \<^loc>< x)"
haftmann@22916
   188
  by unfold_locales
haftmann@22916
   189
    (simp add: less_le, auto intro: antisym order_trans)
haftmann@22916
   190
haftmann@21248
   191
end
nipkow@15524
   192
haftmann@21329
   193
haftmann@21329
   194
subsection {* Linear (total) orders *}
haftmann@21329
   195
haftmann@22316
   196
class linorder = order +
haftmann@21216
   197
  assumes linear: "x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
haftmann@21248
   198
begin
haftmann@21248
   199
haftmann@22841
   200
lemma less_linear: "x \<^loc>< y \<or> x = y \<or> y \<^loc>< x"
haftmann@21248
   201
  unfolding less_le using less_le linear by blast 
haftmann@21248
   202
haftmann@22841
   203
lemma le_less_linear: "x \<^loc>\<le> y \<or> y \<^loc>< x"
haftmann@21412
   204
  by (simp add: le_less less_linear)
haftmann@21248
   205
haftmann@21248
   206
lemma le_cases [case_names le ge]:
haftmann@22841
   207
  "(x \<^loc>\<le> y \<Longrightarrow> P) \<Longrightarrow> (y \<^loc>\<le> x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@21248
   208
  using linear by blast
haftmann@21248
   209
haftmann@22384
   210
lemma linorder_cases [case_names less equal greater]:
haftmann@22841
   211
    "(x \<^loc>< y \<Longrightarrow> P) \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> (y \<^loc>< x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@21412
   212
  using less_linear by blast
haftmann@21248
   213
haftmann@22841
   214
lemma not_less: "\<not> x \<^loc>< y \<longleftrightarrow> y \<^loc>\<le> x"
haftmann@21248
   215
  apply (simp add: less_le)
haftmann@21248
   216
  using linear apply (blast intro: antisym)
nipkow@15524
   217
  done
nipkow@15524
   218
haftmann@22841
   219
lemma not_le: "\<not> x \<^loc>\<le> y \<longleftrightarrow> y \<^loc>< x"
haftmann@21248
   220
  apply (simp add: less_le)
haftmann@21248
   221
  using linear apply (blast intro: antisym)
nipkow@15524
   222
  done
nipkow@15524
   223
haftmann@22841
   224
lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x \<^loc>< y \<or> y \<^loc>< x"
haftmann@21412
   225
  by (cut_tac x = x and y = y in less_linear, auto)
nipkow@15524
   226
haftmann@22841
   227
lemma neqE: "x \<noteq> y \<Longrightarrow> (x \<^loc>< y \<Longrightarrow> R) \<Longrightarrow> (y \<^loc>< x \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@21248
   228
  by (simp add: neq_iff) blast
nipkow@15524
   229
haftmann@22841
   230
lemma antisym_conv1: "\<not> x \<^loc>< y \<Longrightarrow> x \<^loc>\<le> y \<longleftrightarrow> x = y"
haftmann@21248
   231
  by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   232
haftmann@22841
   233
lemma antisym_conv2: "x \<^loc>\<le> y \<Longrightarrow> \<not> x \<^loc>< y \<longleftrightarrow> x = y"
haftmann@21248
   234
  by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   235
haftmann@22841
   236
lemma antisym_conv3: "\<not> y \<^loc>< x \<Longrightarrow> \<not> x \<^loc>< y \<longleftrightarrow> x = y"
haftmann@21248
   237
  by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   238
paulson@16796
   239
text{*Replacing the old Nat.leI*}
haftmann@22841
   240
lemma leI: "\<not> x \<^loc>< y \<Longrightarrow> y \<^loc>\<le> x"
haftmann@21248
   241
  unfolding not_less .
paulson@16796
   242
haftmann@22841
   243
lemma leD: "y \<^loc>\<le> x \<Longrightarrow> \<not> x \<^loc>< y"
haftmann@21248
   244
  unfolding not_less .
paulson@16796
   245
paulson@16796
   246
(*FIXME inappropriate name (or delete altogether)*)
haftmann@22841
   247
lemma not_leE: "\<not> y \<^loc>\<le> x \<Longrightarrow> x \<^loc>< y"
haftmann@21248
   248
  unfolding not_le .
haftmann@21248
   249
haftmann@22916
   250
haftmann@22916
   251
text {* Reverse order *}
haftmann@22916
   252
haftmann@22916
   253
lemma linorder_reverse:
haftmann@23018
   254
  "linorder (\<lambda>x y. y \<^loc>\<le> x) (\<lambda>x y. y \<^loc>< x)"
haftmann@22916
   255
  by unfold_locales
haftmann@22916
   256
    (simp add: less_le, auto intro: antisym order_trans simp add: linear)
haftmann@22916
   257
haftmann@22916
   258
haftmann@22738
   259
text {* min/max properties *}
haftmann@22384
   260
haftmann@21383
   261
lemma min_le_iff_disj:
haftmann@22841
   262
  "min x y \<^loc>\<le> z \<longleftrightarrow> x \<^loc>\<le> z \<or> y \<^loc>\<le> z"
haftmann@22384
   263
  unfolding min_def using linear by (auto intro: order_trans)
haftmann@21383
   264
haftmann@21383
   265
lemma le_max_iff_disj:
haftmann@22841
   266
  "z \<^loc>\<le> max x y \<longleftrightarrow> z \<^loc>\<le> x \<or> z \<^loc>\<le> y"
haftmann@22384
   267
  unfolding max_def using linear by (auto intro: order_trans)
haftmann@21383
   268
haftmann@21383
   269
lemma min_less_iff_disj:
haftmann@22841
   270
  "min x y \<^loc>< z \<longleftrightarrow> x \<^loc>< z \<or> y \<^loc>< z"
haftmann@21412
   271
  unfolding min_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   272
haftmann@21383
   273
lemma less_max_iff_disj:
haftmann@22841
   274
  "z \<^loc>< max x y \<longleftrightarrow> z \<^loc>< x \<or> z \<^loc>< y"
haftmann@21412
   275
  unfolding max_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   276
haftmann@21383
   277
lemma min_less_iff_conj [simp]:
haftmann@22841
   278
  "z \<^loc>< min x y \<longleftrightarrow> z \<^loc>< x \<and> z \<^loc>< y"
haftmann@21412
   279
  unfolding min_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   280
haftmann@21383
   281
lemma max_less_iff_conj [simp]:
haftmann@22841
   282
  "max x y \<^loc>< z \<longleftrightarrow> x \<^loc>< z \<and> y \<^loc>< z"
haftmann@21412
   283
  unfolding max_def le_less using less_linear by (auto intro: less_trans)
haftmann@21383
   284
haftmann@21383
   285
lemma split_min:
haftmann@22841
   286
  "P (min i j) \<longleftrightarrow> (i \<^loc>\<le> j \<longrightarrow> P i) \<and> (\<not> i \<^loc>\<le> j \<longrightarrow> P j)"
haftmann@21383
   287
  by (simp add: min_def)
haftmann@21383
   288
haftmann@21383
   289
lemma split_max:
haftmann@22841
   290
  "P (max i j) \<longleftrightarrow> (i \<^loc>\<le> j \<longrightarrow> P j) \<and> (\<not> i \<^loc>\<le> j \<longrightarrow> P i)"
haftmann@21383
   291
  by (simp add: max_def)
haftmann@21383
   292
haftmann@21248
   293
end
haftmann@21248
   294
haftmann@22916
   295
subsection {* Name duplicates -- including min/max interpretation *}
haftmann@21248
   296
haftmann@22384
   297
lemmas order_less_le = less_le
haftmann@22841
   298
lemmas order_eq_refl = order_class.eq_refl
haftmann@22841
   299
lemmas order_less_irrefl = order_class.less_irrefl
haftmann@22841
   300
lemmas order_le_less = order_class.le_less
haftmann@22841
   301
lemmas order_le_imp_less_or_eq = order_class.le_imp_less_or_eq
haftmann@22841
   302
lemmas order_less_imp_le = order_class.less_imp_le
haftmann@22841
   303
lemmas order_less_imp_not_eq = order_class.less_imp_not_eq
haftmann@22841
   304
lemmas order_less_imp_not_eq2 = order_class.less_imp_not_eq2
haftmann@22841
   305
lemmas order_neq_le_trans = order_class.neq_le_trans
haftmann@22841
   306
lemmas order_le_neq_trans = order_class.le_neq_trans
haftmann@22316
   307
haftmann@22384
   308
lemmas order_antisym = antisym
haftmann@22316
   309
lemmas order_less_not_sym = order_class.less_not_sym
haftmann@22316
   310
lemmas order_less_asym = order_class.less_asym
haftmann@22316
   311
lemmas order_eq_iff = order_class.eq_iff
haftmann@22316
   312
lemmas order_antisym_conv = order_class.antisym_conv
haftmann@22316
   313
lemmas less_imp_neq = order_class.less_imp_neq
haftmann@22316
   314
lemmas order_less_trans = order_class.less_trans
haftmann@22316
   315
lemmas order_le_less_trans = order_class.le_less_trans
haftmann@22316
   316
lemmas order_less_le_trans = order_class.less_le_trans
haftmann@22316
   317
lemmas order_less_imp_not_less = order_class.less_imp_not_less
haftmann@22316
   318
lemmas order_less_imp_triv = order_class.less_imp_triv
haftmann@22316
   319
lemmas order_less_asym' = order_class.less_asym'
haftmann@22316
   320
haftmann@22384
   321
lemmas linorder_linear = linear
haftmann@22316
   322
lemmas linorder_less_linear = linorder_class.less_linear
haftmann@22316
   323
lemmas linorder_le_less_linear = linorder_class.le_less_linear
haftmann@22316
   324
lemmas linorder_le_cases = linorder_class.le_cases
haftmann@22316
   325
lemmas linorder_not_less = linorder_class.not_less
haftmann@22316
   326
lemmas linorder_not_le = linorder_class.not_le
haftmann@22316
   327
lemmas linorder_neq_iff = linorder_class.neq_iff
haftmann@22316
   328
lemmas linorder_neqE = linorder_class.neqE
haftmann@22316
   329
lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
haftmann@22316
   330
lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
haftmann@22316
   331
lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
haftmann@22316
   332
lemmas leI = linorder_class.leI
haftmann@22316
   333
lemmas leD = linorder_class.leD
haftmann@22316
   334
lemmas not_leE = linorder_class.not_leE
paulson@16796
   335
haftmann@23087
   336
lemmas min_le_iff_disj = linorder_class.min_le_iff_disj [folded ord_class.min]
haftmann@23087
   337
lemmas le_max_iff_disj = linorder_class.le_max_iff_disj [folded ord_class.max]
haftmann@23087
   338
lemmas min_less_iff_disj = linorder_class.min_less_iff_disj [folded ord_class.min]
haftmann@23087
   339
lemmas less_max_iff_disj = linorder_class.less_max_iff_disj [folded ord_class.max]
haftmann@23087
   340
lemmas min_less_iff_conj [simp] = linorder_class.min_less_iff_conj [folded ord_class.min]
haftmann@23087
   341
lemmas max_less_iff_conj [simp] = linorder_class.max_less_iff_conj [folded ord_class.max]
haftmann@23087
   342
lemmas split_min = linorder_class.split_min [folded ord_class.min]
haftmann@23087
   343
lemmas split_max = linorder_class.split_max [folded ord_class.max]
haftmann@22916
   344
haftmann@21083
   345
haftmann@21083
   346
subsection {* Reasoning tools setup *}
haftmann@21083
   347
haftmann@21091
   348
ML {*
haftmann@21091
   349
local
haftmann@21091
   350
haftmann@21091
   351
fun decomp_gen sort thy (Trueprop $ t) =
haftmann@21248
   352
  let
haftmann@21248
   353
    fun of_sort t =
haftmann@21248
   354
      let
haftmann@21248
   355
        val T = type_of t
haftmann@21248
   356
      in
haftmann@21091
   357
        (* exclude numeric types: linear arithmetic subsumes transitivity *)
haftmann@21248
   358
        T <> HOLogic.natT andalso T <> HOLogic.intT
haftmann@21248
   359
          andalso T <> HOLogic.realT andalso Sign.of_sort thy (T, sort)
haftmann@21248
   360
      end;
haftmann@22916
   361
    fun dec (Const (@{const_name Not}, _) $ t) = (case dec t
haftmann@21248
   362
          of NONE => NONE
haftmann@21248
   363
           | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
haftmann@22916
   364
      | dec (Const (@{const_name "op ="},  _) $ t1 $ t2) =
haftmann@21248
   365
          if of_sort t1
haftmann@21248
   366
          then SOME (t1, "=", t2)
haftmann@21248
   367
          else NONE
haftmann@22997
   368
      | dec (Const (@{const_name Orderings.less_eq},  _) $ t1 $ t2) =
haftmann@21248
   369
          if of_sort t1
haftmann@21248
   370
          then SOME (t1, "<=", t2)
haftmann@21248
   371
          else NONE
haftmann@22997
   372
      | dec (Const (@{const_name Orderings.less},  _) $ t1 $ t2) =
haftmann@21248
   373
          if of_sort t1
haftmann@21248
   374
          then SOME (t1, "<", t2)
haftmann@21248
   375
          else NONE
haftmann@21248
   376
      | dec _ = NONE;
haftmann@21091
   377
  in dec t end;
haftmann@21091
   378
haftmann@21091
   379
in
haftmann@21091
   380
haftmann@22841
   381
(* sorry - there is no preorder class
haftmann@21248
   382
structure Quasi_Tac = Quasi_Tac_Fun (
haftmann@21248
   383
struct
haftmann@21248
   384
  val le_trans = thm "order_trans";
haftmann@21248
   385
  val le_refl = thm "order_refl";
haftmann@21248
   386
  val eqD1 = thm "order_eq_refl";
haftmann@21248
   387
  val eqD2 = thm "sym" RS thm "order_eq_refl";
haftmann@21248
   388
  val less_reflE = thm "order_less_irrefl" RS thm "notE";
haftmann@21248
   389
  val less_imp_le = thm "order_less_imp_le";
haftmann@21248
   390
  val le_neq_trans = thm "order_le_neq_trans";
haftmann@21248
   391
  val neq_le_trans = thm "order_neq_le_trans";
haftmann@21248
   392
  val less_imp_neq = thm "less_imp_neq";
haftmann@22738
   393
  val decomp_trans = decomp_gen ["Orderings.preorder"];
haftmann@22738
   394
  val decomp_quasi = decomp_gen ["Orderings.preorder"];
haftmann@22841
   395
end);*)
haftmann@21091
   396
haftmann@21091
   397
structure Order_Tac = Order_Tac_Fun (
haftmann@21248
   398
struct
haftmann@21248
   399
  val less_reflE = thm "order_less_irrefl" RS thm "notE";
haftmann@21248
   400
  val le_refl = thm "order_refl";
haftmann@21248
   401
  val less_imp_le = thm "order_less_imp_le";
haftmann@21248
   402
  val not_lessI = thm "linorder_not_less" RS thm "iffD2";
haftmann@21248
   403
  val not_leI = thm "linorder_not_le" RS thm "iffD2";
haftmann@21248
   404
  val not_lessD = thm "linorder_not_less" RS thm "iffD1";
haftmann@21248
   405
  val not_leD = thm "linorder_not_le" RS thm "iffD1";
haftmann@21248
   406
  val eqI = thm "order_antisym";
haftmann@21248
   407
  val eqD1 = thm "order_eq_refl";
haftmann@21248
   408
  val eqD2 = thm "sym" RS thm "order_eq_refl";
haftmann@21248
   409
  val less_trans = thm "order_less_trans";
haftmann@21248
   410
  val less_le_trans = thm "order_less_le_trans";
haftmann@21248
   411
  val le_less_trans = thm "order_le_less_trans";
haftmann@21248
   412
  val le_trans = thm "order_trans";
haftmann@21248
   413
  val le_neq_trans = thm "order_le_neq_trans";
haftmann@21248
   414
  val neq_le_trans = thm "order_neq_le_trans";
haftmann@21248
   415
  val less_imp_neq = thm "less_imp_neq";
haftmann@21248
   416
  val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
haftmann@21248
   417
  val not_sym = thm "not_sym";
haftmann@21248
   418
  val decomp_part = decomp_gen ["Orderings.order"];
haftmann@21248
   419
  val decomp_lin = decomp_gen ["Orderings.linorder"];
haftmann@21248
   420
end);
haftmann@21091
   421
haftmann@21091
   422
end;
haftmann@21091
   423
*}
haftmann@21091
   424
haftmann@21083
   425
setup {*
haftmann@21083
   426
let
haftmann@21083
   427
haftmann@21083
   428
fun prp t thm = (#prop (rep_thm thm) = t);
nipkow@15524
   429
haftmann@21083
   430
fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
haftmann@21083
   431
  let val prems = prems_of_ss ss;
haftmann@22916
   432
      val less = Const (@{const_name less}, T);
haftmann@21083
   433
      val t = HOLogic.mk_Trueprop(le $ s $ r);
haftmann@21083
   434
  in case find_first (prp t) prems of
haftmann@21083
   435
       NONE =>
haftmann@21083
   436
         let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
haftmann@21083
   437
         in case find_first (prp t) prems of
haftmann@21083
   438
              NONE => NONE
haftmann@22738
   439
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv1}))
haftmann@21083
   440
         end
haftmann@22738
   441
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_antisym_conv}))
haftmann@21083
   442
  end
haftmann@21083
   443
  handle THM _ => NONE;
nipkow@15524
   444
haftmann@21083
   445
fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
haftmann@21083
   446
  let val prems = prems_of_ss ss;
haftmann@22916
   447
      val le = Const (@{const_name less_eq}, T);
haftmann@21083
   448
      val t = HOLogic.mk_Trueprop(le $ r $ s);
haftmann@21083
   449
  in case find_first (prp t) prems of
haftmann@21083
   450
       NONE =>
haftmann@21083
   451
         let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
haftmann@21083
   452
         in case find_first (prp t) prems of
haftmann@21083
   453
              NONE => NONE
haftmann@22738
   454
            | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv3}))
haftmann@21083
   455
         end
haftmann@22738
   456
     | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv2}))
haftmann@21083
   457
  end
haftmann@21083
   458
  handle THM _ => NONE;
nipkow@15524
   459
haftmann@21248
   460
fun add_simprocs procs thy =
haftmann@21248
   461
  (Simplifier.change_simpset_of thy (fn ss => ss
haftmann@21248
   462
    addsimprocs (map (fn (name, raw_ts, proc) =>
haftmann@21248
   463
      Simplifier.simproc thy name raw_ts proc)) procs); thy);
haftmann@21248
   464
fun add_solver name tac thy =
haftmann@21248
   465
  (Simplifier.change_simpset_of thy (fn ss => ss addSolver
haftmann@21248
   466
    (mk_solver name (K tac))); thy);
haftmann@21083
   467
haftmann@21083
   468
in
haftmann@21248
   469
  add_simprocs [
haftmann@21248
   470
       ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
haftmann@21248
   471
       ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
haftmann@21248
   472
     ]
haftmann@21248
   473
  #> add_solver "Trans_linear" Order_Tac.linear_tac
haftmann@21248
   474
  #> add_solver "Trans_partial" Order_Tac.partial_tac
haftmann@21248
   475
  (* Adding the transitivity reasoners also as safe solvers showed a slight
haftmann@21248
   476
     speed up, but the reasoning strength appears to be not higher (at least
haftmann@21248
   477
     no breaking of additional proofs in the entire HOL distribution, as
haftmann@21248
   478
     of 5 March 2004, was observed). *)
haftmann@21083
   479
end
haftmann@21083
   480
*}
nipkow@15524
   481
nipkow@15524
   482
haftmann@21083
   483
subsection {* Bounded quantifiers *}
haftmann@21083
   484
haftmann@21083
   485
syntax
wenzelm@21180
   486
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   487
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   488
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   489
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   490
wenzelm@21180
   491
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   492
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   493
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   494
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   495
haftmann@21083
   496
syntax (xsymbols)
wenzelm@21180
   497
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   498
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   499
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   500
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   501
wenzelm@21180
   502
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   503
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   504
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   505
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   506
haftmann@21083
   507
syntax (HOL)
wenzelm@21180
   508
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   509
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   510
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   511
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   512
haftmann@21083
   513
syntax (HTML output)
wenzelm@21180
   514
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   515
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   516
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   517
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   518
wenzelm@21180
   519
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   520
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   521
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   522
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   523
haftmann@21083
   524
translations
haftmann@21083
   525
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   526
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   527
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   528
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   529
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   530
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   531
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   532
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   533
haftmann@21083
   534
print_translation {*
haftmann@21083
   535
let
haftmann@22916
   536
  val All_binder = Syntax.binder_name @{const_syntax All};
haftmann@22916
   537
  val Ex_binder = Syntax.binder_name @{const_syntax Ex};
wenzelm@22377
   538
  val impl = @{const_syntax "op -->"};
wenzelm@22377
   539
  val conj = @{const_syntax "op &"};
haftmann@22916
   540
  val less = @{const_syntax less};
haftmann@22916
   541
  val less_eq = @{const_syntax less_eq};
wenzelm@21180
   542
wenzelm@21180
   543
  val trans =
wenzelm@21524
   544
   [((All_binder, impl, less), ("_All_less", "_All_greater")),
wenzelm@21524
   545
    ((All_binder, impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
wenzelm@21524
   546
    ((Ex_binder, conj, less), ("_Ex_less", "_Ex_greater")),
wenzelm@21524
   547
    ((Ex_binder, conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
wenzelm@21180
   548
krauss@22344
   549
  fun matches_bound v t = 
krauss@22344
   550
     case t of (Const ("_bound", _) $ Free (v', _)) => (v = v')
krauss@22344
   551
              | _ => false
krauss@22344
   552
  fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false)
krauss@22344
   553
  fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P
wenzelm@21180
   554
wenzelm@21180
   555
  fun tr' q = (q,
wenzelm@21180
   556
    fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@21180
   557
      (case AList.lookup (op =) trans (q, c, d) of
wenzelm@21180
   558
        NONE => raise Match
wenzelm@21180
   559
      | SOME (l, g) =>
krauss@22344
   560
          if matches_bound v t andalso not (contains_var v u) then mk v l u P
krauss@22344
   561
          else if matches_bound v u andalso not (contains_var v t) then mk v g t P
krauss@22344
   562
          else raise Match)
wenzelm@21180
   563
     | _ => raise Match);
wenzelm@21524
   564
in [tr' All_binder, tr' Ex_binder] end
haftmann@21083
   565
*}
haftmann@21083
   566
haftmann@21083
   567
haftmann@21383
   568
subsection {* Transitivity reasoning *}
haftmann@21383
   569
haftmann@21383
   570
lemma ord_le_eq_trans: "a <= b ==> b = c ==> a <= c"
haftmann@21383
   571
  by (rule subst)
haftmann@21383
   572
haftmann@21383
   573
lemma ord_eq_le_trans: "a = b ==> b <= c ==> a <= c"
haftmann@21383
   574
  by (rule ssubst)
haftmann@21383
   575
haftmann@21383
   576
lemma ord_less_eq_trans: "a < b ==> b = c ==> a < c"
haftmann@21383
   577
  by (rule subst)
haftmann@21383
   578
haftmann@21383
   579
lemma ord_eq_less_trans: "a = b ==> b < c ==> a < c"
haftmann@21383
   580
  by (rule ssubst)
haftmann@21383
   581
haftmann@21383
   582
lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
haftmann@21383
   583
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   584
proof -
haftmann@21383
   585
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   586
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   587
  also assume "f b < c"
haftmann@21383
   588
  finally (order_less_trans) show ?thesis .
haftmann@21383
   589
qed
haftmann@21383
   590
haftmann@21383
   591
lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
haftmann@21383
   592
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   593
proof -
haftmann@21383
   594
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   595
  assume "a < f b"
haftmann@21383
   596
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   597
  finally (order_less_trans) show ?thesis .
haftmann@21383
   598
qed
haftmann@21383
   599
haftmann@21383
   600
lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
haftmann@21383
   601
  (!!x y. x <= y ==> f x <= f y) ==> f a < c"
haftmann@21383
   602
proof -
haftmann@21383
   603
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   604
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   605
  also assume "f b < c"
haftmann@21383
   606
  finally (order_le_less_trans) show ?thesis .
haftmann@21383
   607
qed
haftmann@21383
   608
haftmann@21383
   609
lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
haftmann@21383
   610
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   611
proof -
haftmann@21383
   612
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   613
  assume "a <= f b"
haftmann@21383
   614
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   615
  finally (order_le_less_trans) show ?thesis .
haftmann@21383
   616
qed
haftmann@21383
   617
haftmann@21383
   618
lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
haftmann@21383
   619
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   620
proof -
haftmann@21383
   621
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   622
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   623
  also assume "f b <= c"
haftmann@21383
   624
  finally (order_less_le_trans) show ?thesis .
haftmann@21383
   625
qed
haftmann@21383
   626
haftmann@21383
   627
lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
haftmann@21383
   628
  (!!x y. x <= y ==> f x <= f y) ==> a < f c"
haftmann@21383
   629
proof -
haftmann@21383
   630
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   631
  assume "a < f b"
haftmann@21383
   632
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   633
  finally (order_less_le_trans) show ?thesis .
haftmann@21383
   634
qed
haftmann@21383
   635
haftmann@21383
   636
lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
haftmann@21383
   637
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   638
proof -
haftmann@21383
   639
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   640
  assume "a <= f b"
haftmann@21383
   641
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   642
  finally (order_trans) show ?thesis .
haftmann@21383
   643
qed
haftmann@21383
   644
haftmann@21383
   645
lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
haftmann@21383
   646
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   647
proof -
haftmann@21383
   648
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   649
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   650
  also assume "f b <= c"
haftmann@21383
   651
  finally (order_trans) show ?thesis .
haftmann@21383
   652
qed
haftmann@21383
   653
haftmann@21383
   654
lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
haftmann@21383
   655
  (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
haftmann@21383
   656
proof -
haftmann@21383
   657
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   658
  assume "a <= b" hence "f a <= f b" by (rule r)
haftmann@21383
   659
  also assume "f b = c"
haftmann@21383
   660
  finally (ord_le_eq_trans) show ?thesis .
haftmann@21383
   661
qed
haftmann@21383
   662
haftmann@21383
   663
lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
haftmann@21383
   664
  (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
haftmann@21383
   665
proof -
haftmann@21383
   666
  assume r: "!!x y. x <= y ==> f x <= f y"
haftmann@21383
   667
  assume "a = f b"
haftmann@21383
   668
  also assume "b <= c" hence "f b <= f c" by (rule r)
haftmann@21383
   669
  finally (ord_eq_le_trans) show ?thesis .
haftmann@21383
   670
qed
haftmann@21383
   671
haftmann@21383
   672
lemma ord_less_eq_subst: "a < b ==> f b = c ==>
haftmann@21383
   673
  (!!x y. x < y ==> f x < f y) ==> f a < c"
haftmann@21383
   674
proof -
haftmann@21383
   675
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   676
  assume "a < b" hence "f a < f b" by (rule r)
haftmann@21383
   677
  also assume "f b = c"
haftmann@21383
   678
  finally (ord_less_eq_trans) show ?thesis .
haftmann@21383
   679
qed
haftmann@21383
   680
haftmann@21383
   681
lemma ord_eq_less_subst: "a = f b ==> b < c ==>
haftmann@21383
   682
  (!!x y. x < y ==> f x < f y) ==> a < f c"
haftmann@21383
   683
proof -
haftmann@21383
   684
  assume r: "!!x y. x < y ==> f x < f y"
haftmann@21383
   685
  assume "a = f b"
haftmann@21383
   686
  also assume "b < c" hence "f b < f c" by (rule r)
haftmann@21383
   687
  finally (ord_eq_less_trans) show ?thesis .
haftmann@21383
   688
qed
haftmann@21383
   689
haftmann@21383
   690
text {*
haftmann@21383
   691
  Note that this list of rules is in reverse order of priorities.
haftmann@21383
   692
*}
haftmann@21383
   693
haftmann@21383
   694
lemmas order_trans_rules [trans] =
haftmann@21383
   695
  order_less_subst2
haftmann@21383
   696
  order_less_subst1
haftmann@21383
   697
  order_le_less_subst2
haftmann@21383
   698
  order_le_less_subst1
haftmann@21383
   699
  order_less_le_subst2
haftmann@21383
   700
  order_less_le_subst1
haftmann@21383
   701
  order_subst2
haftmann@21383
   702
  order_subst1
haftmann@21383
   703
  ord_le_eq_subst
haftmann@21383
   704
  ord_eq_le_subst
haftmann@21383
   705
  ord_less_eq_subst
haftmann@21383
   706
  ord_eq_less_subst
haftmann@21383
   707
  forw_subst
haftmann@21383
   708
  back_subst
haftmann@21383
   709
  rev_mp
haftmann@21383
   710
  mp
haftmann@21383
   711
  order_neq_le_trans
haftmann@21383
   712
  order_le_neq_trans
haftmann@21383
   713
  order_less_trans
haftmann@21383
   714
  order_less_asym'
haftmann@21383
   715
  order_le_less_trans
haftmann@21383
   716
  order_less_le_trans
haftmann@21383
   717
  order_trans
haftmann@21383
   718
  order_antisym
haftmann@21383
   719
  ord_le_eq_trans
haftmann@21383
   720
  ord_eq_le_trans
haftmann@21383
   721
  ord_less_eq_trans
haftmann@21383
   722
  ord_eq_less_trans
haftmann@21383
   723
  trans
haftmann@21383
   724
haftmann@21083
   725
wenzelm@21180
   726
(* FIXME cleanup *)
wenzelm@21180
   727
haftmann@21083
   728
text {* These support proving chains of decreasing inequalities
haftmann@21083
   729
    a >= b >= c ... in Isar proofs. *}
haftmann@21083
   730
haftmann@21083
   731
lemma xt1:
haftmann@21083
   732
  "a = b ==> b > c ==> a > c"
haftmann@21083
   733
  "a > b ==> b = c ==> a > c"
haftmann@21083
   734
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   735
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   736
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   737
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   738
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   739
  "(x::'a::order) >= y ==> y > z ==> x > z"
haftmann@21083
   740
  "(a::'a::order) > b ==> b > a ==> ?P"
haftmann@21083
   741
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   742
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   743
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
haftmann@21083
   744
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
haftmann@21083
   745
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   746
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   747
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   748
by auto
haftmann@21083
   749
haftmann@21083
   750
lemma xt2:
haftmann@21083
   751
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   752
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   753
haftmann@21083
   754
lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
haftmann@21083
   755
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   756
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   757
haftmann@21083
   758
lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
   759
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
   760
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   761
haftmann@21083
   762
lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
   763
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   764
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   765
haftmann@21083
   766
lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
   767
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   768
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   769
haftmann@21083
   770
lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
   771
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
   772
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   773
haftmann@21083
   774
lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
   775
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   776
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   777
haftmann@21083
   778
lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
   779
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   780
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   781
haftmann@21083
   782
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
   783
haftmann@21083
   784
(* 
haftmann@21083
   785
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
   786
  for the wrong thing in an Isar proof.
haftmann@21083
   787
haftmann@21083
   788
  The extra transitivity rules can be used as follows: 
haftmann@21083
   789
haftmann@21083
   790
lemma "(a::'a::order) > z"
haftmann@21083
   791
proof -
haftmann@21083
   792
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
   793
    sorry
haftmann@21083
   794
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
   795
    sorry
haftmann@21083
   796
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
   797
    sorry
haftmann@21083
   798
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
   799
    sorry
haftmann@21083
   800
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
   801
    sorry
haftmann@21083
   802
  also (xtrans) have "?rhs > z"
haftmann@21083
   803
    sorry
haftmann@21083
   804
  finally (xtrans) show ?thesis .
haftmann@21083
   805
qed
haftmann@21083
   806
haftmann@21083
   807
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
   808
  leave out the "(xtrans)" above.
haftmann@21083
   809
*)
haftmann@21083
   810
haftmann@21546
   811
subsection {* Order on bool *}
haftmann@21546
   812
haftmann@22886
   813
instance bool :: order 
haftmann@21546
   814
  le_bool_def: "P \<le> Q \<equiv> P \<longrightarrow> Q"
haftmann@21546
   815
  less_bool_def: "P < Q \<equiv> P \<le> Q \<and> P \<noteq> Q"
haftmann@22916
   816
  by intro_classes (auto simp add: le_bool_def less_bool_def)
haftmann@21546
   817
haftmann@21546
   818
lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
haftmann@21546
   819
  by (simp add: le_bool_def)
haftmann@21546
   820
haftmann@21546
   821
lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
haftmann@21546
   822
  by (simp add: le_bool_def)
haftmann@21546
   823
haftmann@21546
   824
lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
haftmann@21546
   825
  by (simp add: le_bool_def)
haftmann@21546
   826
haftmann@21546
   827
lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
haftmann@21546
   828
  by (simp add: le_bool_def)
haftmann@21546
   829
haftmann@22348
   830
lemma [code func]:
haftmann@22348
   831
  "False \<le> b \<longleftrightarrow> True"
haftmann@22348
   832
  "True \<le> b \<longleftrightarrow> b"
haftmann@22348
   833
  "False < b \<longleftrightarrow> b"
haftmann@22348
   834
  "True < b \<longleftrightarrow> False"
haftmann@22348
   835
  unfolding le_bool_def less_bool_def by simp_all
haftmann@22348
   836
haftmann@22424
   837
haftmann@21383
   838
subsection {* Monotonicity, syntactic least value operator and min/max *}
haftmann@21083
   839
haftmann@21216
   840
locale mono =
haftmann@21216
   841
  fixes f
haftmann@21216
   842
  assumes mono: "A \<le> B \<Longrightarrow> f A \<le> f B"
haftmann@21216
   843
haftmann@21216
   844
lemmas monoI [intro?] = mono.intro
haftmann@21216
   845
  and monoD [dest?] = mono.mono
haftmann@21083
   846
haftmann@21083
   847
constdefs
haftmann@21083
   848
  Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
haftmann@21083
   849
  "Least P == THE x. P x & (ALL y. P y --> x <= y)"
haftmann@21083
   850
    -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
haftmann@21083
   851
haftmann@21383
   852
lemma LeastI2_order:
haftmann@21383
   853
  "[| P (x::'a::order);
haftmann@21383
   854
      !!y. P y ==> x <= y;
haftmann@21383
   855
      !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
haftmann@21383
   856
   ==> Q (Least P)"
haftmann@21383
   857
  apply (unfold Least_def)
haftmann@21383
   858
  apply (rule theI2)
haftmann@21383
   859
    apply (blast intro: order_antisym)+
haftmann@21383
   860
  done
haftmann@21383
   861
haftmann@21383
   862
lemma Least_equality:
haftmann@21383
   863
    "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
haftmann@21383
   864
  apply (simp add: Least_def)
haftmann@21383
   865
  apply (rule the_equality)
haftmann@21383
   866
  apply (auto intro!: order_antisym)
haftmann@21383
   867
  done
haftmann@21383
   868
haftmann@21383
   869
lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
haftmann@21383
   870
  by (simp add: min_def)
haftmann@21383
   871
haftmann@21383
   872
lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
haftmann@21383
   873
  by (simp add: max_def)
haftmann@21383
   874
haftmann@21383
   875
lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
haftmann@21383
   876
  apply (simp add: min_def)
haftmann@21383
   877
  apply (blast intro: order_antisym)
haftmann@21383
   878
  done
haftmann@21383
   879
haftmann@21383
   880
lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
haftmann@21383
   881
  apply (simp add: max_def)
haftmann@21383
   882
  apply (blast intro: order_antisym)
haftmann@21383
   883
  done
haftmann@21383
   884
haftmann@21383
   885
lemma min_of_mono:
haftmann@21383
   886
    "(!!x y. (f x <= f y) = (x <= y)) ==> min (f m) (f n) = f (min m n)"
haftmann@21383
   887
  by (simp add: min_def)
haftmann@21383
   888
haftmann@21383
   889
lemma max_of_mono:
haftmann@21383
   890
    "(!!x y. (f x <= f y) = (x <= y)) ==> max (f m) (f n) = f (max m n)"
haftmann@21383
   891
  by (simp add: max_def)
haftmann@21383
   892
haftmann@22548
   893
haftmann@22548
   894
subsection {* legacy ML bindings *}
wenzelm@21673
   895
wenzelm@21673
   896
ML {*
haftmann@22548
   897
val monoI = @{thm monoI};
haftmann@22886
   898
*}
wenzelm@21673
   899
nipkow@15524
   900
end