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