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