src/HOL/Orderings.thy
author haftmann
Mon Nov 13 15:43:06 2006 +0100 (2006-11-13)
changeset 21329 7338206d75f1
parent 21259 63ab016c99ca
child 21383 17e6275e13f5
permissions -rw-r--r--
introduces preorders
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
section {* Abstract orders *}
haftmann@21083
    13
haftmann@21329
    14
subsection {* Order syntax *}
nipkow@15524
    15
haftmann@21194
    16
class ord =
haftmann@20588
    17
  fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
wenzelm@21204
    18
    and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
wenzelm@21204
    19
begin
wenzelm@21204
    20
wenzelm@21204
    21
notation
wenzelm@21204
    22
  less_eq  ("op \<^loc><=")
wenzelm@21259
    23
  less_eq  ("(_/ \<^loc><= _)" [51, 51] 50)
wenzelm@21204
    24
  less  ("op \<^loc><")
wenzelm@21259
    25
  less  ("(_/ \<^loc>< _)"  [51, 51] 50)
wenzelm@21204
    26
wenzelm@21204
    27
notation (xsymbols)
wenzelm@21204
    28
  less_eq  ("op \<^loc>\<le>")
wenzelm@21259
    29
  less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
nipkow@15524
    30
wenzelm@21204
    31
notation (HTML output)
wenzelm@21204
    32
  less_eq  ("op \<^loc>\<le>")
wenzelm@21259
    33
  less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
wenzelm@21204
    34
wenzelm@21204
    35
abbreviation (input)
wenzelm@21204
    36
  greater  (infix "\<^loc>>" 50)
wenzelm@21204
    37
  "x \<^loc>> y \<equiv> y \<^loc>< x"
wenzelm@21204
    38
  greater_eq  (infix "\<^loc>>=" 50)
wenzelm@21204
    39
  "x \<^loc>>= y \<equiv> y \<^loc><= x"
wenzelm@21204
    40
wenzelm@21204
    41
notation (xsymbols)
wenzelm@21259
    42
  greater_eq  (infix "\<^loc>\<ge>" 50)
wenzelm@21204
    43
wenzelm@21204
    44
end
wenzelm@21204
    45
wenzelm@21204
    46
notation
wenzelm@19656
    47
  less_eq  ("op <=")
wenzelm@21259
    48
  less_eq  ("(_/ <= _)" [51, 51] 50)
wenzelm@21204
    49
  less  ("op <")
wenzelm@21259
    50
  less  ("(_/ < _)"  [51, 51] 50)
wenzelm@21204
    51
  
wenzelm@21204
    52
notation (xsymbols)
wenzelm@19656
    53
  less_eq  ("op \<le>")
wenzelm@21259
    54
  less_eq  ("(_/ \<le> _)"  [51, 51] 50)
nipkow@15524
    55
wenzelm@21204
    56
notation (HTML output)
wenzelm@21204
    57
  less_eq  ("op \<le>")
wenzelm@21259
    58
  less_eq  ("(_/ \<le> _)"  [51, 51] 50)
haftmann@20714
    59
wenzelm@19536
    60
abbreviation (input)
wenzelm@21259
    61
  greater  (infix ">" 50)
haftmann@20714
    62
  "x > y \<equiv> y < x"
wenzelm@21259
    63
  greater_eq  (infix ">=" 50)
haftmann@20714
    64
  "x >= y \<equiv> y <= x"
haftmann@20714
    65
  
wenzelm@21204
    66
notation (xsymbols)
wenzelm@21259
    67
  greater_eq  (infix "\<ge>" 50)
nipkow@15524
    68
nipkow@15524
    69
haftmann@21329
    70
subsection {* Quasiorders (preorders) *}
nipkow@15524
    71
haftmann@21329
    72
locale preorder =
haftmann@21216
    73
  fixes below :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "\<sqsubseteq>" 50)
haftmann@21216
    74
  fixes less :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "\<sqsubset>" 50)
haftmann@21216
    75
  assumes refl [iff]: "x \<sqsubseteq> x"
haftmann@21216
    76
  and trans: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> z"
haftmann@21248
    77
  and less_le: "x \<sqsubset> y \<longleftrightarrow> x \<sqsubseteq> y \<and> x \<noteq> y"
haftmann@21248
    78
begin
haftmann@21248
    79
haftmann@21248
    80
abbreviation (input)
haftmann@21248
    81
  greater_eq (infixl "\<sqsupseteq>" 50)
haftmann@21248
    82
  "x \<sqsupseteq> y \<equiv> y \<sqsubseteq> x"
haftmann@21248
    83
haftmann@21248
    84
abbreviation (input)
haftmann@21248
    85
  greater (infixl "\<sqsupset>" 50)
haftmann@21248
    86
  "x \<sqsupset> y \<equiv> y \<sqsubset> x"
haftmann@21248
    87
nipkow@15524
    88
text {* Reflexivity. *}
nipkow@15524
    89
haftmann@21248
    90
lemma eq_refl: "x = y \<Longrightarrow> x \<sqsubseteq> y"
nipkow@15524
    91
    -- {* This form is useful with the classical reasoner. *}
haftmann@21248
    92
  by (erule ssubst) (rule refl)
nipkow@15524
    93
haftmann@21248
    94
lemma less_irrefl [iff]: "\<not> x \<sqsubset> x"
haftmann@21248
    95
  by (simp add: less_le)
nipkow@15524
    96
haftmann@21248
    97
lemma le_less: "x \<sqsubseteq> y \<longleftrightarrow> x \<sqsubset> y \<or> x = y"
nipkow@15524
    98
    -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
haftmann@21248
    99
  by (simp add: less_le) blast
nipkow@15524
   100
haftmann@21248
   101
lemma le_imp_less_or_eq: "x \<sqsubseteq> y \<Longrightarrow> x \<sqsubset> y \<or> x = y"
haftmann@21248
   102
  unfolding less_le by blast
nipkow@15524
   103
haftmann@21248
   104
lemma less_imp_le: "x \<sqsubset> y \<Longrightarrow> x \<sqsubseteq> y"
haftmann@21248
   105
  unfolding less_le by blast
haftmann@21248
   106
haftmann@21329
   107
lemma less_imp_neq: "x \<sqsubset> y \<Longrightarrow> x \<noteq> y"
haftmann@21329
   108
  by (erule contrapos_pn, erule subst, rule less_irrefl)
haftmann@21329
   109
haftmann@21329
   110
haftmann@21329
   111
text {* Useful for simplification, but too risky to include by default. *}
haftmann@21329
   112
haftmann@21329
   113
lemma less_imp_not_eq: "x \<sqsubset> y \<Longrightarrow> (x = y) \<longleftrightarrow> False"
haftmann@21329
   114
  by auto
haftmann@21329
   115
haftmann@21329
   116
lemma less_imp_not_eq2: "x \<sqsubset> y \<Longrightarrow> (y = x) \<longleftrightarrow> False"
haftmann@21329
   117
  by auto
haftmann@21329
   118
haftmann@21329
   119
haftmann@21329
   120
text {* Transitivity rules for calculational reasoning *}
haftmann@21329
   121
haftmann@21329
   122
lemma neq_le_trans: "\<lbrakk> a \<noteq> b; a \<sqsubseteq> b \<rbrakk> \<Longrightarrow> a \<sqsubset> b"
haftmann@21329
   123
  by (simp add: less_le)
haftmann@21329
   124
haftmann@21329
   125
lemma le_neq_trans: "\<lbrakk> a \<sqsubseteq> b; a \<noteq> b \<rbrakk> \<Longrightarrow> a \<sqsubset> b"
haftmann@21329
   126
  by (simp add: less_le)
haftmann@21329
   127
haftmann@21329
   128
end
haftmann@21329
   129
haftmann@21329
   130
haftmann@21329
   131
subsection {* Partial orderings *}
haftmann@21329
   132
haftmann@21329
   133
locale partial_order = preorder + 
haftmann@21329
   134
  assumes antisym: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> x \<Longrightarrow> x = y"
haftmann@21329
   135
haftmann@21329
   136
context partial_order
haftmann@21329
   137
begin
nipkow@15524
   138
nipkow@15524
   139
text {* Asymmetry. *}
nipkow@15524
   140
haftmann@21248
   141
lemma less_not_sym: "x \<sqsubset> y \<Longrightarrow> \<not> (y \<sqsubset> x)"
haftmann@21248
   142
  by (simp add: less_le antisym)
nipkow@15524
   143
haftmann@21248
   144
lemma less_asym: "x \<sqsubset> y \<Longrightarrow> (\<not> P \<Longrightarrow> y \<sqsubset> x) \<Longrightarrow> P"
haftmann@21248
   145
  by (drule less_not_sym, erule contrapos_np) simp
nipkow@15524
   146
haftmann@21248
   147
lemma eq_iff: "x = y \<longleftrightarrow> x \<sqsubseteq> y \<and> y \<sqsubseteq> x"
haftmann@21248
   148
  by (blast intro: antisym)
nipkow@15524
   149
haftmann@21248
   150
lemma antisym_conv: "y \<sqsubseteq> x \<Longrightarrow> x \<sqsubseteq> y \<longleftrightarrow> x = y"
haftmann@21248
   151
  by (blast intro: antisym)
nipkow@15524
   152
haftmann@21248
   153
lemma less_imp_neq: "x \<sqsubset> y \<Longrightarrow> x \<noteq> y"
haftmann@21248
   154
  by (erule contrapos_pn, erule subst, rule less_irrefl)
haftmann@21248
   155
haftmann@21083
   156
nipkow@15524
   157
text {* Transitivity. *}
nipkow@15524
   158
haftmann@21248
   159
lemma less_trans: "\<lbrakk> x \<sqsubset> y; y \<sqsubset> z \<rbrakk> \<Longrightarrow> x \<sqsubset> z"
haftmann@21248
   160
  by (simp add: less_le) (blast intro: trans antisym)
nipkow@15524
   161
haftmann@21248
   162
lemma le_less_trans: "\<lbrakk> x \<sqsubseteq> y; y \<sqsubset> z \<rbrakk> \<Longrightarrow> x \<sqsubset> z"
haftmann@21248
   163
  by (simp add: less_le) (blast intro: trans antisym)
nipkow@15524
   164
haftmann@21248
   165
lemma less_le_trans: "\<lbrakk> x \<sqsubset> y; y \<sqsubseteq> z \<rbrakk> \<Longrightarrow> x \<sqsubset> z"
haftmann@21248
   166
  by (simp add: less_le) (blast intro: trans antisym)
nipkow@15524
   167
nipkow@15524
   168
nipkow@15524
   169
text {* Useful for simplification, but too risky to include by default. *}
nipkow@15524
   170
haftmann@21248
   171
lemma less_imp_not_less: "x \<sqsubset> y \<Longrightarrow> (\<not> y \<sqsubset> x) \<longleftrightarrow> True"
haftmann@21248
   172
  by (blast elim: less_asym)
nipkow@15524
   173
haftmann@21248
   174
lemma less_imp_triv: "x \<sqsubset> y \<Longrightarrow> (y \<sqsubset> x \<longrightarrow> P) \<longleftrightarrow> True"
haftmann@21248
   175
  by (blast elim: less_asym)
nipkow@15524
   176
haftmann@21248
   177
haftmann@21083
   178
text {* Transitivity rules for calculational reasoning *}
nipkow@15524
   179
haftmann@21248
   180
lemma less_asym': "\<lbrakk> a \<sqsubset> b; b \<sqsubset> a \<rbrakk> \<Longrightarrow> P"
haftmann@21248
   181
  by (rule less_asym)
haftmann@21248
   182
haftmann@21248
   183
end
nipkow@15524
   184
haftmann@21329
   185
axclass order < ord
haftmann@21329
   186
  order_refl [iff]: "x <= x"
haftmann@21329
   187
  order_trans: "x <= y ==> y <= z ==> x <= z"
haftmann@21329
   188
  order_antisym: "x <= y ==> y <= x ==> x = y"
haftmann@21329
   189
  order_less_le: "(x < y) = (x <= y & x ~= y)"
nipkow@15524
   190
haftmann@21329
   191
interpretation order:
haftmann@21329
   192
  partial_order ["op \<le> \<Colon> 'a\<Colon>order \<Rightarrow> 'a \<Rightarrow> bool" "op < \<Colon> 'a\<Colon>order \<Rightarrow> 'a \<Rightarrow> bool"]
haftmann@21329
   193
apply unfold_locales
haftmann@21329
   194
apply (rule order_refl)
haftmann@21329
   195
apply (erule (1) order_trans)
haftmann@21329
   196
apply (rule order_less_le)
haftmann@21329
   197
apply (erule (1) order_antisym)
haftmann@21329
   198
done
haftmann@21216
   199
haftmann@21329
   200
haftmann@21329
   201
subsection {* Linear (total) orders *}
haftmann@21329
   202
haftmann@21329
   203
locale linorder = partial_order +
haftmann@21216
   204
  assumes linear: "x \<sqsubseteq> y \<or> y \<sqsubseteq> x"
nipkow@15524
   205
haftmann@21329
   206
context linorder
haftmann@21248
   207
begin
haftmann@21248
   208
haftmann@21248
   209
lemma trichotomy: "x \<sqsubset> y \<or> x = y \<or> y \<sqsubset> x"
haftmann@21248
   210
  unfolding less_le using less_le linear by blast 
haftmann@21248
   211
haftmann@21248
   212
lemma le_less_linear: "x \<sqsubseteq> y \<or> y \<sqsubset> x"
haftmann@21248
   213
  by (simp add: le_less trichotomy)
haftmann@21248
   214
haftmann@21248
   215
lemma le_cases [case_names le ge]:
haftmann@21248
   216
  "\<lbrakk> x \<sqsubseteq> y \<Longrightarrow> P; y \<sqsubseteq> x \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
haftmann@21248
   217
  using linear by blast
haftmann@21248
   218
haftmann@21248
   219
lemma cases [case_names less equal greater]:
haftmann@21248
   220
    "\<lbrakk> x \<sqsubset> y \<Longrightarrow> P; x = y \<Longrightarrow> P; y \<sqsubset> x \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
haftmann@21248
   221
  using trichotomy by blast
haftmann@21248
   222
haftmann@21248
   223
lemma not_less: "\<not> x \<sqsubset> y \<longleftrightarrow> y \<sqsubseteq> x"
haftmann@21248
   224
  apply (simp add: less_le)
haftmann@21248
   225
  using linear apply (blast intro: antisym)
nipkow@15524
   226
  done
nipkow@15524
   227
haftmann@21248
   228
lemma not_le: "\<not> x \<sqsubseteq> y \<longleftrightarrow> y \<sqsubset> x"
haftmann@21248
   229
  apply (simp add: less_le)
haftmann@21248
   230
  using linear apply (blast intro: antisym)
nipkow@15524
   231
  done
nipkow@15524
   232
haftmann@21248
   233
lemma neq_iff: "x \<noteq> y \<longleftrightarrow> x \<sqsubset> y \<or> y \<sqsubset> x"
haftmann@21248
   234
  by (cut_tac x = x and y = y in trichotomy, auto)
nipkow@15524
   235
haftmann@21248
   236
lemma neqE: "\<lbrakk> x \<noteq> y; x \<sqsubset> y \<Longrightarrow> R; y \<sqsubset> x \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
haftmann@21248
   237
  by (simp add: neq_iff) blast
nipkow@15524
   238
haftmann@21248
   239
lemma antisym_conv1: "\<not> x \<sqsubset> y \<Longrightarrow> x \<sqsubseteq> y \<longleftrightarrow> x = y"
haftmann@21248
   240
  by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   241
haftmann@21248
   242
lemma antisym_conv2: "x \<sqsubseteq> y \<Longrightarrow> \<not> x \<sqsubset> y \<longleftrightarrow> x = y"
haftmann@21248
   243
  by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   244
haftmann@21248
   245
lemma antisym_conv3: "\<not> y \<sqsubset> x \<Longrightarrow> \<not> x \<sqsubset> y \<longleftrightarrow> x = y"
haftmann@21248
   246
  by (blast intro: antisym dest: not_less [THEN iffD1])
nipkow@15524
   247
paulson@16796
   248
text{*Replacing the old Nat.leI*}
haftmann@21248
   249
lemma leI: "\<not> x \<sqsubset> y \<Longrightarrow> y \<sqsubseteq> x"
haftmann@21248
   250
  unfolding not_less .
paulson@16796
   251
haftmann@21248
   252
lemma leD: "y \<sqsubseteq> x \<Longrightarrow> \<not> x \<sqsubset> y"
haftmann@21248
   253
  unfolding not_less .
paulson@16796
   254
paulson@16796
   255
(*FIXME inappropriate name (or delete altogether)*)
haftmann@21248
   256
lemma not_leE: "\<not> y \<sqsubseteq> x \<Longrightarrow> x \<sqsubset> y"
haftmann@21248
   257
  unfolding not_le .
haftmann@21248
   258
haftmann@21248
   259
end
haftmann@21248
   260
haftmann@21329
   261
axclass linorder < order
haftmann@21329
   262
  linorder_linear: "x <= y | y <= x"
haftmann@21329
   263
haftmann@21329
   264
interpretation linorder:
haftmann@21329
   265
  linorder ["op \<le> \<Colon> 'a\<Colon>linorder \<Rightarrow> 'a \<Rightarrow> bool" "op < \<Colon> 'a\<Colon>linorder \<Rightarrow> 'a \<Rightarrow> bool"]
haftmann@21329
   266
  by unfold_locales (rule linorder_linear)
haftmann@21329
   267
haftmann@21248
   268
haftmann@21248
   269
subsection {* Name duplicates *}
haftmann@21248
   270
haftmann@21248
   271
lemmas order_eq_refl [where 'b = "?'a::order"] = order.eq_refl
haftmann@21248
   272
lemmas order_less_irrefl [where 'b = "?'a::order"] = order.less_irrefl
haftmann@21248
   273
lemmas order_le_less [where 'b = "?'a::order"] = order.le_less
haftmann@21248
   274
lemmas order_le_imp_less_or_eq [where 'b = "?'a::order"] = order.le_imp_less_or_eq
haftmann@21248
   275
lemmas order_less_imp_le [where 'b = "?'a::order"] = order.less_imp_le
haftmann@21248
   276
lemmas order_less_not_sym [where 'b = "?'a::order"] = order.less_not_sym
haftmann@21248
   277
lemmas order_less_asym [where 'b = "?'a::order"] = order.less_asym
haftmann@21248
   278
lemmas order_eq_iff [where 'b = "?'a::order"] = order.eq_iff
haftmann@21248
   279
lemmas order_antisym_conv [where 'b = "?'a::order"] = order.antisym_conv
haftmann@21248
   280
lemmas less_imp_neq [where 'b = "?'a::order"] = order.less_imp_neq
haftmann@21248
   281
lemmas order_less_trans [where 'b = "?'a::order"] = order.less_trans
haftmann@21248
   282
lemmas order_le_less_trans [where 'b = "?'a::order"] = order.le_less_trans
haftmann@21248
   283
lemmas order_less_le_trans [where 'b = "?'a::order"] = order.less_le_trans
haftmann@21248
   284
lemmas order_less_imp_not_less [where 'b = "?'a::order"] = order.less_imp_not_less
haftmann@21248
   285
lemmas order_less_imp_triv [where 'b = "?'a::order"] = order.less_imp_triv
haftmann@21248
   286
lemmas order_less_imp_not_eq [where 'b = "?'a::order"] = order.less_imp_not_eq
haftmann@21248
   287
lemmas order_less_imp_not_eq2 [where 'b = "?'a::order"] = order.less_imp_not_eq2
haftmann@21248
   288
lemmas order_neq_le_trans [where 'b = "?'a::order"] = order.neq_le_trans
haftmann@21248
   289
lemmas order_le_neq_trans [where 'b = "?'a::order"] = order.le_neq_trans
haftmann@21248
   290
lemmas order_less_asym' [where 'b = "?'a::order"] = order.less_asym'
haftmann@21248
   291
lemmas linorder_less_linear [where 'b = "?'a::linorder"] = linorder.trichotomy
haftmann@21248
   292
lemmas linorder_le_less_linear [where 'b = "?'a::linorder"] = linorder.le_less_linear
haftmann@21248
   293
lemmas linorder_le_cases [where 'b = "?'a::linorder"] = linorder.le_cases
haftmann@21248
   294
lemmas linorder_cases [where 'b = "?'a::linorder"] = linorder.cases
haftmann@21248
   295
lemmas linorder_not_less [where 'b = "?'a::linorder"] = linorder.not_less
haftmann@21248
   296
lemmas linorder_not_le [where 'b = "?'a::linorder"] = linorder.not_le
haftmann@21248
   297
lemmas linorder_neq_iff [where 'b = "?'a::linorder"] = linorder.neq_iff
haftmann@21248
   298
lemmas linorder_neqE [where 'b = "?'a::linorder"] = linorder.neqE
haftmann@21248
   299
lemmas linorder_antisym_conv1 [where 'b = "?'a::linorder"] = linorder.antisym_conv1
haftmann@21248
   300
lemmas linorder_antisym_conv2 [where 'b = "?'a::linorder"] = linorder.antisym_conv2
haftmann@21248
   301
lemmas linorder_antisym_conv3 [where 'b = "?'a::linorder"] = linorder.antisym_conv3
haftmann@21248
   302
lemmas leI [where 'b = "?'a::linorder"] = linorder.leI
haftmann@21248
   303
lemmas leD [where 'b = "?'a::linorder"] = linorder.leD
haftmann@21248
   304
lemmas not_leE [where 'b = "?'a::linorder"] = linorder.not_leE
paulson@16796
   305
haftmann@21083
   306
haftmann@21083
   307
subsection {* Reasoning tools setup *}
haftmann@21083
   308
haftmann@21091
   309
ML {*
haftmann@21091
   310
local
haftmann@21091
   311
haftmann@21091
   312
fun decomp_gen sort thy (Trueprop $ t) =
haftmann@21248
   313
  let
haftmann@21248
   314
    fun of_sort t =
haftmann@21248
   315
      let
haftmann@21248
   316
        val T = type_of t
haftmann@21248
   317
      in
haftmann@21091
   318
        (* exclude numeric types: linear arithmetic subsumes transitivity *)
haftmann@21248
   319
        T <> HOLogic.natT andalso T <> HOLogic.intT
haftmann@21248
   320
          andalso T <> HOLogic.realT andalso Sign.of_sort thy (T, sort)
haftmann@21248
   321
      end;
haftmann@21248
   322
    fun dec (Const ("Not", _) $ t) = (case dec t
haftmann@21248
   323
          of NONE => NONE
haftmann@21248
   324
           | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
haftmann@21248
   325
      | dec (Const ("op =",  _) $ t1 $ t2) =
haftmann@21248
   326
          if of_sort t1
haftmann@21248
   327
          then SOME (t1, "=", t2)
haftmann@21248
   328
          else NONE
haftmann@21248
   329
      | dec (Const ("Orderings.less_eq",  _) $ t1 $ t2) =
haftmann@21248
   330
          if of_sort t1
haftmann@21248
   331
          then SOME (t1, "<=", t2)
haftmann@21248
   332
          else NONE
haftmann@21248
   333
      | dec (Const ("Orderings.less",  _) $ t1 $ t2) =
haftmann@21248
   334
          if of_sort t1
haftmann@21248
   335
          then SOME (t1, "<", t2)
haftmann@21248
   336
          else NONE
haftmann@21248
   337
      | dec _ = NONE;
haftmann@21091
   338
  in dec t end;
haftmann@21091
   339
haftmann@21091
   340
in
haftmann@21091
   341
haftmann@21091
   342
(* The setting up of Quasi_Tac serves as a demo.  Since there is no
haftmann@21091
   343
   class for quasi orders, the tactics Quasi_Tac.trans_tac and
haftmann@21091
   344
   Quasi_Tac.quasi_tac are not of much use. *)
haftmann@21091
   345
haftmann@21248
   346
structure Quasi_Tac = Quasi_Tac_Fun (
haftmann@21248
   347
struct
haftmann@21248
   348
  val le_trans = thm "order_trans";
haftmann@21248
   349
  val le_refl = thm "order_refl";
haftmann@21248
   350
  val eqD1 = thm "order_eq_refl";
haftmann@21248
   351
  val eqD2 = thm "sym" RS thm "order_eq_refl";
haftmann@21248
   352
  val less_reflE = thm "order_less_irrefl" RS thm "notE";
haftmann@21248
   353
  val less_imp_le = thm "order_less_imp_le";
haftmann@21248
   354
  val le_neq_trans = thm "order_le_neq_trans";
haftmann@21248
   355
  val neq_le_trans = thm "order_neq_le_trans";
haftmann@21248
   356
  val less_imp_neq = thm "less_imp_neq";
haftmann@21248
   357
  val decomp_trans = decomp_gen ["Orderings.order"];
haftmann@21248
   358
  val decomp_quasi = decomp_gen ["Orderings.order"];
haftmann@21248
   359
end);
haftmann@21091
   360
haftmann@21091
   361
structure Order_Tac = Order_Tac_Fun (
haftmann@21248
   362
struct
haftmann@21248
   363
  val less_reflE = thm "order_less_irrefl" RS thm "notE";
haftmann@21248
   364
  val le_refl = thm "order_refl";
haftmann@21248
   365
  val less_imp_le = thm "order_less_imp_le";
haftmann@21248
   366
  val not_lessI = thm "linorder_not_less" RS thm "iffD2";
haftmann@21248
   367
  val not_leI = thm "linorder_not_le" RS thm "iffD2";
haftmann@21248
   368
  val not_lessD = thm "linorder_not_less" RS thm "iffD1";
haftmann@21248
   369
  val not_leD = thm "linorder_not_le" RS thm "iffD1";
haftmann@21248
   370
  val eqI = thm "order_antisym";
haftmann@21248
   371
  val eqD1 = thm "order_eq_refl";
haftmann@21248
   372
  val eqD2 = thm "sym" RS thm "order_eq_refl";
haftmann@21248
   373
  val less_trans = thm "order_less_trans";
haftmann@21248
   374
  val less_le_trans = thm "order_less_le_trans";
haftmann@21248
   375
  val le_less_trans = thm "order_le_less_trans";
haftmann@21248
   376
  val le_trans = thm "order_trans";
haftmann@21248
   377
  val le_neq_trans = thm "order_le_neq_trans";
haftmann@21248
   378
  val neq_le_trans = thm "order_neq_le_trans";
haftmann@21248
   379
  val less_imp_neq = thm "less_imp_neq";
haftmann@21248
   380
  val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
haftmann@21248
   381
  val not_sym = thm "not_sym";
haftmann@21248
   382
  val decomp_part = decomp_gen ["Orderings.order"];
haftmann@21248
   383
  val decomp_lin = decomp_gen ["Orderings.linorder"];
haftmann@21248
   384
end);
haftmann@21091
   385
haftmann@21091
   386
end;
haftmann@21091
   387
*}
haftmann@21091
   388
haftmann@21083
   389
setup {*
haftmann@21083
   390
let
haftmann@21083
   391
haftmann@21083
   392
val order_antisym_conv = thm "order_antisym_conv"
haftmann@21083
   393
val linorder_antisym_conv1 = thm "linorder_antisym_conv1"
haftmann@21083
   394
val linorder_antisym_conv2 = thm "linorder_antisym_conv2"
haftmann@21083
   395
val linorder_antisym_conv3 = thm "linorder_antisym_conv3"
haftmann@21083
   396
haftmann@21083
   397
fun prp t thm = (#prop (rep_thm thm) = t);
nipkow@15524
   398
haftmann@21083
   399
fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
haftmann@21083
   400
  let val prems = prems_of_ss ss;
haftmann@21083
   401
      val less = Const("Orderings.less",T);
haftmann@21083
   402
      val t = HOLogic.mk_Trueprop(le $ s $ r);
haftmann@21083
   403
  in case find_first (prp t) prems of
haftmann@21083
   404
       NONE =>
haftmann@21083
   405
         let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
haftmann@21083
   406
         in case find_first (prp t) prems of
haftmann@21083
   407
              NONE => NONE
haftmann@21083
   408
            | SOME thm => SOME(mk_meta_eq(thm RS linorder_antisym_conv1))
haftmann@21083
   409
         end
haftmann@21083
   410
     | SOME thm => SOME(mk_meta_eq(thm RS order_antisym_conv))
haftmann@21083
   411
  end
haftmann@21083
   412
  handle THM _ => NONE;
nipkow@15524
   413
haftmann@21083
   414
fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
haftmann@21083
   415
  let val prems = prems_of_ss ss;
haftmann@21083
   416
      val le = Const("Orderings.less_eq",T);
haftmann@21083
   417
      val t = HOLogic.mk_Trueprop(le $ r $ s);
haftmann@21083
   418
  in case find_first (prp t) prems of
haftmann@21083
   419
       NONE =>
haftmann@21083
   420
         let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
haftmann@21083
   421
         in case find_first (prp t) prems of
haftmann@21083
   422
              NONE => NONE
haftmann@21083
   423
            | SOME thm => SOME(mk_meta_eq(thm RS linorder_antisym_conv3))
haftmann@21083
   424
         end
haftmann@21083
   425
     | SOME thm => SOME(mk_meta_eq(thm RS linorder_antisym_conv2))
haftmann@21083
   426
  end
haftmann@21083
   427
  handle THM _ => NONE;
nipkow@15524
   428
haftmann@21248
   429
fun add_simprocs procs thy =
haftmann@21248
   430
  (Simplifier.change_simpset_of thy (fn ss => ss
haftmann@21248
   431
    addsimprocs (map (fn (name, raw_ts, proc) =>
haftmann@21248
   432
      Simplifier.simproc thy name raw_ts proc)) procs); thy);
haftmann@21248
   433
fun add_solver name tac thy =
haftmann@21248
   434
  (Simplifier.change_simpset_of thy (fn ss => ss addSolver
haftmann@21248
   435
    (mk_solver name (K tac))); thy);
haftmann@21083
   436
haftmann@21083
   437
in
haftmann@21248
   438
  add_simprocs [
haftmann@21248
   439
       ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
haftmann@21248
   440
       ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
haftmann@21248
   441
     ]
haftmann@21248
   442
  #> add_solver "Trans_linear" Order_Tac.linear_tac
haftmann@21248
   443
  #> add_solver "Trans_partial" Order_Tac.partial_tac
haftmann@21248
   444
  (* Adding the transitivity reasoners also as safe solvers showed a slight
haftmann@21248
   445
     speed up, but the reasoning strength appears to be not higher (at least
haftmann@21248
   446
     no breaking of additional proofs in the entire HOL distribution, as
haftmann@21248
   447
     of 5 March 2004, was observed). *)
haftmann@21083
   448
end
haftmann@21083
   449
*}
nipkow@15524
   450
nipkow@15524
   451
haftmann@21083
   452
subsection {* Bounded quantifiers *}
haftmann@21083
   453
haftmann@21083
   454
syntax
wenzelm@21180
   455
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   456
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   457
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   458
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   459
wenzelm@21180
   460
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   461
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   462
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   463
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
haftmann@21083
   464
haftmann@21083
   465
syntax (xsymbols)
wenzelm@21180
   466
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   467
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   468
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   469
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   470
wenzelm@21180
   471
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   472
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   473
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   474
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   475
haftmann@21083
   476
syntax (HOL)
wenzelm@21180
   477
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   478
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   479
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
wenzelm@21180
   480
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
haftmann@21083
   481
haftmann@21083
   482
syntax (HTML output)
wenzelm@21180
   483
  "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   484
  "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   485
  "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   486
  "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
haftmann@21083
   487
wenzelm@21180
   488
  "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   489
  "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
wenzelm@21180
   490
  "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
wenzelm@21180
   491
  "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
haftmann@21083
   492
haftmann@21083
   493
translations
haftmann@21083
   494
  "ALL x<y. P"   =>  "ALL x. x < y \<longrightarrow> P"
haftmann@21083
   495
  "EX x<y. P"    =>  "EX x. x < y \<and> P"
haftmann@21083
   496
  "ALL x<=y. P"  =>  "ALL x. x <= y \<longrightarrow> P"
haftmann@21083
   497
  "EX x<=y. P"   =>  "EX x. x <= y \<and> P"
haftmann@21083
   498
  "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
haftmann@21083
   499
  "EX x>y. P"    =>  "EX x. x > y \<and> P"
haftmann@21083
   500
  "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
haftmann@21083
   501
  "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
haftmann@21083
   502
haftmann@21083
   503
print_translation {*
haftmann@21083
   504
let
wenzelm@21180
   505
  val syntax_name = Sign.const_syntax_name (the_context ());
wenzelm@21180
   506
  val impl = syntax_name "op -->";
wenzelm@21180
   507
  val conj = syntax_name "op &";
wenzelm@21180
   508
  val less = syntax_name "Orderings.less";
wenzelm@21180
   509
  val less_eq = syntax_name "Orderings.less_eq";
wenzelm@21180
   510
wenzelm@21180
   511
  val trans =
wenzelm@21180
   512
   [(("ALL ", impl, less), ("_All_less", "_All_greater")),
wenzelm@21180
   513
    (("ALL ", impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
wenzelm@21180
   514
    (("EX ", conj, less), ("_Ex_less", "_Ex_greater")),
wenzelm@21180
   515
    (("EX ", conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
wenzelm@21180
   516
haftmann@21083
   517
  fun mk v v' c n P =
wenzelm@21180
   518
    if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)
haftmann@21083
   519
    then Syntax.const c $ Syntax.mark_bound v' $ n $ P else raise Match;
wenzelm@21180
   520
wenzelm@21180
   521
  fun tr' q = (q,
wenzelm@21180
   522
    fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
wenzelm@21180
   523
      (case AList.lookup (op =) trans (q, c, d) of
wenzelm@21180
   524
        NONE => raise Match
wenzelm@21180
   525
      | SOME (l, g) =>
wenzelm@21180
   526
          (case (t, u) of
wenzelm@21180
   527
            (Const ("_bound", _) $ Free (v', _), n) => mk v v' l n P
wenzelm@21180
   528
          | (n, Const ("_bound", _) $ Free (v', _)) => mk v v' g n P
wenzelm@21180
   529
          | _ => raise Match))
wenzelm@21180
   530
     | _ => raise Match);
wenzelm@21180
   531
in [tr' "ALL ", tr' "EX "] end
haftmann@21083
   532
*}
haftmann@21083
   533
haftmann@21083
   534
haftmann@21083
   535
subsection {* Transitivity reasoning on decreasing inequalities *}
haftmann@21083
   536
wenzelm@21180
   537
(* FIXME cleanup *)
wenzelm@21180
   538
haftmann@21083
   539
text {* These support proving chains of decreasing inequalities
haftmann@21083
   540
    a >= b >= c ... in Isar proofs. *}
haftmann@21083
   541
haftmann@21083
   542
lemma xt1:
haftmann@21083
   543
  "a = b ==> b > c ==> a > c"
haftmann@21083
   544
  "a > b ==> b = c ==> a > c"
haftmann@21083
   545
  "a = b ==> b >= c ==> a >= c"
haftmann@21083
   546
  "a >= b ==> b = c ==> a >= c"
haftmann@21083
   547
  "(x::'a::order) >= y ==> y >= x ==> x = y"
haftmann@21083
   548
  "(x::'a::order) >= y ==> y >= z ==> x >= z"
haftmann@21083
   549
  "(x::'a::order) > y ==> y >= z ==> x > z"
haftmann@21083
   550
  "(x::'a::order) >= y ==> y > z ==> x > z"
haftmann@21083
   551
  "(a::'a::order) > b ==> b > a ==> ?P"
haftmann@21083
   552
  "(x::'a::order) > y ==> y > z ==> x > z"
haftmann@21083
   553
  "(a::'a::order) >= b ==> a ~= b ==> a > b"
haftmann@21083
   554
  "(a::'a::order) ~= b ==> a >= b ==> a > b"
haftmann@21083
   555
  "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
haftmann@21083
   556
  "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   557
  "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   558
  "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   559
by auto
haftmann@21083
   560
haftmann@21083
   561
lemma xt2:
haftmann@21083
   562
  "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
haftmann@21083
   563
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   564
haftmann@21083
   565
lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
haftmann@21083
   566
    (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
haftmann@21083
   567
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   568
haftmann@21083
   569
lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
haftmann@21083
   570
  (!!x y. x >= y ==> f x >= f y) ==> a > f c"
haftmann@21083
   571
by (subgoal_tac "f b >= f c", force, force)
haftmann@21083
   572
haftmann@21083
   573
lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
haftmann@21083
   574
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   575
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   576
haftmann@21083
   577
lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
haftmann@21083
   578
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   579
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   580
haftmann@21083
   581
lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
haftmann@21083
   582
    (!!x y. x >= y ==> f x >= f y) ==> f a > c"
haftmann@21083
   583
by (subgoal_tac "f a >= f b", force, force)
haftmann@21083
   584
haftmann@21083
   585
lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
haftmann@21083
   586
    (!!x y. x > y ==> f x > f y) ==> a > f c"
haftmann@21083
   587
by (subgoal_tac "f b > f c", force, force)
haftmann@21083
   588
haftmann@21083
   589
lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
haftmann@21083
   590
    (!!x y. x > y ==> f x > f y) ==> f a > c"
haftmann@21083
   591
by (subgoal_tac "f a > f b", force, force)
haftmann@21083
   592
haftmann@21083
   593
lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
haftmann@21083
   594
haftmann@21083
   595
(* 
haftmann@21083
   596
  Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
haftmann@21083
   597
  for the wrong thing in an Isar proof.
haftmann@21083
   598
haftmann@21083
   599
  The extra transitivity rules can be used as follows: 
haftmann@21083
   600
haftmann@21083
   601
lemma "(a::'a::order) > z"
haftmann@21083
   602
proof -
haftmann@21083
   603
  have "a >= b" (is "_ >= ?rhs")
haftmann@21083
   604
    sorry
haftmann@21083
   605
  also have "?rhs >= c" (is "_ >= ?rhs")
haftmann@21083
   606
    sorry
haftmann@21083
   607
  also (xtrans) have "?rhs = d" (is "_ = ?rhs")
haftmann@21083
   608
    sorry
haftmann@21083
   609
  also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
haftmann@21083
   610
    sorry
haftmann@21083
   611
  also (xtrans) have "?rhs > f" (is "_ > ?rhs")
haftmann@21083
   612
    sorry
haftmann@21083
   613
  also (xtrans) have "?rhs > z"
haftmann@21083
   614
    sorry
haftmann@21083
   615
  finally (xtrans) show ?thesis .
haftmann@21083
   616
qed
haftmann@21083
   617
haftmann@21083
   618
  Alternatively, one can use "declare xtrans [trans]" and then
haftmann@21083
   619
  leave out the "(xtrans)" above.
haftmann@21083
   620
*)
haftmann@21083
   621
haftmann@21216
   622
subsection {* Monotonicity, syntactic least value operator and syntactic min/max *}
haftmann@21083
   623
haftmann@21216
   624
locale mono =
haftmann@21216
   625
  fixes f
haftmann@21216
   626
  assumes mono: "A \<le> B \<Longrightarrow> f A \<le> f B"
haftmann@21216
   627
haftmann@21216
   628
lemmas monoI [intro?] = mono.intro
haftmann@21216
   629
  and monoD [dest?] = mono.mono
haftmann@21083
   630
haftmann@21083
   631
constdefs
haftmann@21083
   632
  Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
haftmann@21083
   633
  "Least P == THE x. P x & (ALL y. P y --> x <= y)"
haftmann@21083
   634
    -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
haftmann@21083
   635
haftmann@21083
   636
constdefs
haftmann@21083
   637
  min :: "['a::ord, 'a] => 'a"
haftmann@21083
   638
  "min a b == (if a <= b then a else b)"
haftmann@21083
   639
  max :: "['a::ord, 'a] => 'a"
haftmann@21083
   640
  "max a b == (if a <= b then b else a)"
haftmann@21083
   641
nipkow@15524
   642
end