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