src/HOL/Orderings.thy
author haftmann
Thu May 24 08:37:39 2007 +0200 (2007-05-24)
changeset 23087 ad7244663431
parent 23032 b6cb6a131511
child 23182 01fa88b79ddc
permissions -rw-r--r--
rudimentary class target implementation
     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 less_imp_neq = order_class.less_imp_neq
   314 lemmas order_less_trans = order_class.less_trans
   315 lemmas order_le_less_trans = order_class.le_less_trans
   316 lemmas order_less_le_trans = order_class.less_le_trans
   317 lemmas order_less_imp_not_less = order_class.less_imp_not_less
   318 lemmas order_less_imp_triv = order_class.less_imp_triv
   319 lemmas order_less_asym' = order_class.less_asym'
   320 
   321 lemmas linorder_linear = linear
   322 lemmas linorder_less_linear = linorder_class.less_linear
   323 lemmas linorder_le_less_linear = linorder_class.le_less_linear
   324 lemmas linorder_le_cases = linorder_class.le_cases
   325 lemmas linorder_not_less = linorder_class.not_less
   326 lemmas linorder_not_le = linorder_class.not_le
   327 lemmas linorder_neq_iff = linorder_class.neq_iff
   328 lemmas linorder_neqE = linorder_class.neqE
   329 lemmas linorder_antisym_conv1 = linorder_class.antisym_conv1
   330 lemmas linorder_antisym_conv2 = linorder_class.antisym_conv2
   331 lemmas linorder_antisym_conv3 = linorder_class.antisym_conv3
   332 lemmas leI = linorder_class.leI
   333 lemmas leD = linorder_class.leD
   334 lemmas not_leE = linorder_class.not_leE
   335 
   336 lemmas min_le_iff_disj = linorder_class.min_le_iff_disj [folded ord_class.min]
   337 lemmas le_max_iff_disj = linorder_class.le_max_iff_disj [folded ord_class.max]
   338 lemmas min_less_iff_disj = linorder_class.min_less_iff_disj [folded ord_class.min]
   339 lemmas less_max_iff_disj = linorder_class.less_max_iff_disj [folded ord_class.max]
   340 lemmas min_less_iff_conj [simp] = linorder_class.min_less_iff_conj [folded ord_class.min]
   341 lemmas max_less_iff_conj [simp] = linorder_class.max_less_iff_conj [folded ord_class.max]
   342 lemmas split_min = linorder_class.split_min [folded ord_class.min]
   343 lemmas split_max = linorder_class.split_max [folded ord_class.max]
   344 
   345 
   346 subsection {* Reasoning tools setup *}
   347 
   348 ML {*
   349 local
   350 
   351 fun decomp_gen sort thy (Trueprop $ t) =
   352   let
   353     fun of_sort t =
   354       let
   355         val T = type_of t
   356       in
   357         (* exclude numeric types: linear arithmetic subsumes transitivity *)
   358         T <> HOLogic.natT andalso T <> HOLogic.intT
   359           andalso T <> HOLogic.realT andalso Sign.of_sort thy (T, sort)
   360       end;
   361     fun dec (Const (@{const_name Not}, _) $ t) = (case dec t
   362           of NONE => NONE
   363            | SOME (t1, rel, t2) => SOME (t1, "~" ^ rel, t2))
   364       | dec (Const (@{const_name "op ="},  _) $ t1 $ t2) =
   365           if of_sort t1
   366           then SOME (t1, "=", t2)
   367           else NONE
   368       | dec (Const (@{const_name Orderings.less_eq},  _) $ t1 $ t2) =
   369           if of_sort t1
   370           then SOME (t1, "<=", t2)
   371           else NONE
   372       | dec (Const (@{const_name Orderings.less},  _) $ t1 $ t2) =
   373           if of_sort t1
   374           then SOME (t1, "<", t2)
   375           else NONE
   376       | dec _ = NONE;
   377   in dec t end;
   378 
   379 in
   380 
   381 (* sorry - there is no preorder class
   382 structure Quasi_Tac = Quasi_Tac_Fun (
   383 struct
   384   val le_trans = thm "order_trans";
   385   val le_refl = thm "order_refl";
   386   val eqD1 = thm "order_eq_refl";
   387   val eqD2 = thm "sym" RS thm "order_eq_refl";
   388   val less_reflE = thm "order_less_irrefl" RS thm "notE";
   389   val less_imp_le = thm "order_less_imp_le";
   390   val le_neq_trans = thm "order_le_neq_trans";
   391   val neq_le_trans = thm "order_neq_le_trans";
   392   val less_imp_neq = thm "less_imp_neq";
   393   val decomp_trans = decomp_gen ["Orderings.preorder"];
   394   val decomp_quasi = decomp_gen ["Orderings.preorder"];
   395 end);*)
   396 
   397 structure Order_Tac = Order_Tac_Fun (
   398 struct
   399   val less_reflE = thm "order_less_irrefl" RS thm "notE";
   400   val le_refl = thm "order_refl";
   401   val less_imp_le = thm "order_less_imp_le";
   402   val not_lessI = thm "linorder_not_less" RS thm "iffD2";
   403   val not_leI = thm "linorder_not_le" RS thm "iffD2";
   404   val not_lessD = thm "linorder_not_less" RS thm "iffD1";
   405   val not_leD = thm "linorder_not_le" RS thm "iffD1";
   406   val eqI = thm "order_antisym";
   407   val eqD1 = thm "order_eq_refl";
   408   val eqD2 = thm "sym" RS thm "order_eq_refl";
   409   val less_trans = thm "order_less_trans";
   410   val less_le_trans = thm "order_less_le_trans";
   411   val le_less_trans = thm "order_le_less_trans";
   412   val le_trans = thm "order_trans";
   413   val le_neq_trans = thm "order_le_neq_trans";
   414   val neq_le_trans = thm "order_neq_le_trans";
   415   val less_imp_neq = thm "less_imp_neq";
   416   val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
   417   val not_sym = thm "not_sym";
   418   val decomp_part = decomp_gen ["Orderings.order"];
   419   val decomp_lin = decomp_gen ["Orderings.linorder"];
   420 end);
   421 
   422 end;
   423 *}
   424 
   425 setup {*
   426 let
   427 
   428 fun prp t thm = (#prop (rep_thm thm) = t);
   429 
   430 fun prove_antisym_le sg ss ((le as Const(_,T)) $ r $ s) =
   431   let val prems = prems_of_ss ss;
   432       val less = Const (@{const_name less}, T);
   433       val t = HOLogic.mk_Trueprop(le $ s $ r);
   434   in case find_first (prp t) prems of
   435        NONE =>
   436          let val t = HOLogic.mk_Trueprop(HOLogic.Not $ (less $ r $ s))
   437          in case find_first (prp t) prems of
   438               NONE => NONE
   439             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv1}))
   440          end
   441      | SOME thm => SOME(mk_meta_eq(thm RS @{thm order_antisym_conv}))
   442   end
   443   handle THM _ => NONE;
   444 
   445 fun prove_antisym_less sg ss (NotC $ ((less as Const(_,T)) $ r $ s)) =
   446   let val prems = prems_of_ss ss;
   447       val le = Const (@{const_name less_eq}, T);
   448       val t = HOLogic.mk_Trueprop(le $ r $ s);
   449   in case find_first (prp t) prems of
   450        NONE =>
   451          let val t = HOLogic.mk_Trueprop(NotC $ (less $ s $ r))
   452          in case find_first (prp t) prems of
   453               NONE => NONE
   454             | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv3}))
   455          end
   456      | SOME thm => SOME(mk_meta_eq(thm RS @{thm linorder_antisym_conv2}))
   457   end
   458   handle THM _ => NONE;
   459 
   460 fun add_simprocs procs thy =
   461   (Simplifier.change_simpset_of thy (fn ss => ss
   462     addsimprocs (map (fn (name, raw_ts, proc) =>
   463       Simplifier.simproc thy name raw_ts proc)) procs); thy);
   464 fun add_solver name tac thy =
   465   (Simplifier.change_simpset_of thy (fn ss => ss addSolver
   466     (mk_solver name (K tac))); thy);
   467 
   468 in
   469   add_simprocs [
   470        ("antisym le", ["(x::'a::order) <= y"], prove_antisym_le),
   471        ("antisym less", ["~ (x::'a::linorder) < y"], prove_antisym_less)
   472      ]
   473   #> add_solver "Trans_linear" Order_Tac.linear_tac
   474   #> add_solver "Trans_partial" Order_Tac.partial_tac
   475   (* Adding the transitivity reasoners also as safe solvers showed a slight
   476      speed up, but the reasoning strength appears to be not higher (at least
   477      no breaking of additional proofs in the entire HOL distribution, as
   478      of 5 March 2004, was observed). *)
   479 end
   480 *}
   481 
   482 
   483 subsection {* Bounded quantifiers *}
   484 
   485 syntax
   486   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   487   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3EX _<_./ _)"  [0, 0, 10] 10)
   488   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   489   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _<=_./ _)" [0, 0, 10] 10)
   490 
   491   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3ALL _>_./ _)"  [0, 0, 10] 10)
   492   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3EX _>_./ _)"  [0, 0, 10] 10)
   493   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3ALL _>=_./ _)" [0, 0, 10] 10)
   494   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3EX _>=_./ _)" [0, 0, 10] 10)
   495 
   496 syntax (xsymbols)
   497   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   498   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   499   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   500   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   501 
   502   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   503   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   504   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   505   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   506 
   507 syntax (HOL)
   508   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3! _<_./ _)"  [0, 0, 10] 10)
   509   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3? _<_./ _)"  [0, 0, 10] 10)
   510   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3! _<=_./ _)" [0, 0, 10] 10)
   511   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3? _<=_./ _)" [0, 0, 10] 10)
   512 
   513 syntax (HTML output)
   514   "_All_less" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   515   "_Ex_less" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   516   "_All_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   517   "_Ex_less_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   518 
   519   "_All_greater" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_>_./ _)"  [0, 0, 10] 10)
   520   "_Ex_greater" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_>_./ _)"  [0, 0, 10] 10)
   521   "_All_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<forall>_\<ge>_./ _)" [0, 0, 10] 10)
   522   "_Ex_greater_eq" :: "[idt, 'a, bool] => bool"    ("(3\<exists>_\<ge>_./ _)" [0, 0, 10] 10)
   523 
   524 translations
   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   "ALL x>y. P"   =>  "ALL x. x > y \<longrightarrow> P"
   530   "EX x>y. P"    =>  "EX x. x > y \<and> P"
   531   "ALL x>=y. P"  =>  "ALL x. x >= y \<longrightarrow> P"
   532   "EX x>=y. P"   =>  "EX x. x >= y \<and> P"
   533 
   534 print_translation {*
   535 let
   536   val All_binder = Syntax.binder_name @{const_syntax All};
   537   val Ex_binder = Syntax.binder_name @{const_syntax Ex};
   538   val impl = @{const_syntax "op -->"};
   539   val conj = @{const_syntax "op &"};
   540   val less = @{const_syntax less};
   541   val less_eq = @{const_syntax less_eq};
   542 
   543   val trans =
   544    [((All_binder, impl, less), ("_All_less", "_All_greater")),
   545     ((All_binder, impl, less_eq), ("_All_less_eq", "_All_greater_eq")),
   546     ((Ex_binder, conj, less), ("_Ex_less", "_Ex_greater")),
   547     ((Ex_binder, conj, less_eq), ("_Ex_less_eq", "_Ex_greater_eq"))];
   548 
   549   fun matches_bound v t = 
   550      case t of (Const ("_bound", _) $ Free (v', _)) => (v = v')
   551               | _ => false
   552   fun contains_var v = Term.exists_subterm (fn Free (x, _) => x = v | _ => false)
   553   fun mk v c n P = Syntax.const c $ Syntax.mark_bound v $ n $ P
   554 
   555   fun tr' q = (q,
   556     fn [Const ("_bound", _) $ Free (v, _), Const (c, _) $ (Const (d, _) $ t $ u) $ P] =>
   557       (case AList.lookup (op =) trans (q, c, d) of
   558         NONE => raise Match
   559       | SOME (l, g) =>
   560           if matches_bound v t andalso not (contains_var v u) then mk v l u P
   561           else if matches_bound v u andalso not (contains_var v t) then mk v g t P
   562           else raise Match)
   563      | _ => raise Match);
   564 in [tr' All_binder, tr' Ex_binder] end
   565 *}
   566 
   567 
   568 subsection {* Transitivity reasoning *}
   569 
   570 lemma ord_le_eq_trans: "a <= b ==> b = c ==> a <= c"
   571   by (rule subst)
   572 
   573 lemma ord_eq_le_trans: "a = b ==> b <= c ==> a <= c"
   574   by (rule ssubst)
   575 
   576 lemma ord_less_eq_trans: "a < b ==> b = c ==> a < c"
   577   by (rule subst)
   578 
   579 lemma ord_eq_less_trans: "a = b ==> b < c ==> a < c"
   580   by (rule ssubst)
   581 
   582 lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
   583   (!!x y. x < y ==> f x < f y) ==> f a < c"
   584 proof -
   585   assume r: "!!x y. x < y ==> f x < f y"
   586   assume "a < b" hence "f a < f b" by (rule r)
   587   also assume "f b < c"
   588   finally (order_less_trans) show ?thesis .
   589 qed
   590 
   591 lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
   592   (!!x y. x < y ==> f x < f y) ==> a < f c"
   593 proof -
   594   assume r: "!!x y. x < y ==> f x < f y"
   595   assume "a < f b"
   596   also assume "b < c" hence "f b < f c" by (rule r)
   597   finally (order_less_trans) show ?thesis .
   598 qed
   599 
   600 lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
   601   (!!x y. x <= y ==> f x <= f y) ==> f a < c"
   602 proof -
   603   assume r: "!!x y. x <= y ==> f x <= f y"
   604   assume "a <= b" hence "f a <= f b" by (rule r)
   605   also assume "f b < c"
   606   finally (order_le_less_trans) show ?thesis .
   607 qed
   608 
   609 lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
   610   (!!x y. x < y ==> f x < f y) ==> a < f c"
   611 proof -
   612   assume r: "!!x y. x < y ==> f x < f y"
   613   assume "a <= f b"
   614   also assume "b < c" hence "f b < f c" by (rule r)
   615   finally (order_le_less_trans) show ?thesis .
   616 qed
   617 
   618 lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
   619   (!!x y. x < y ==> f x < f y) ==> f a < c"
   620 proof -
   621   assume r: "!!x y. x < y ==> f x < f y"
   622   assume "a < b" hence "f a < f b" by (rule r)
   623   also assume "f b <= c"
   624   finally (order_less_le_trans) show ?thesis .
   625 qed
   626 
   627 lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
   628   (!!x y. x <= y ==> f x <= f y) ==> a < f c"
   629 proof -
   630   assume r: "!!x y. x <= y ==> f x <= f y"
   631   assume "a < f b"
   632   also assume "b <= c" hence "f b <= f c" by (rule r)
   633   finally (order_less_le_trans) show ?thesis .
   634 qed
   635 
   636 lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
   637   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   638 proof -
   639   assume r: "!!x y. x <= y ==> f x <= f y"
   640   assume "a <= f b"
   641   also assume "b <= c" hence "f b <= f c" by (rule r)
   642   finally (order_trans) show ?thesis .
   643 qed
   644 
   645 lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
   646   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
   647 proof -
   648   assume r: "!!x y. x <= y ==> f x <= f y"
   649   assume "a <= b" hence "f a <= f b" by (rule r)
   650   also assume "f b <= c"
   651   finally (order_trans) show ?thesis .
   652 qed
   653 
   654 lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
   655   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
   656 proof -
   657   assume r: "!!x y. x <= y ==> f x <= f y"
   658   assume "a <= b" hence "f a <= f b" by (rule r)
   659   also assume "f b = c"
   660   finally (ord_le_eq_trans) show ?thesis .
   661 qed
   662 
   663 lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
   664   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
   665 proof -
   666   assume r: "!!x y. x <= y ==> f x <= f y"
   667   assume "a = f b"
   668   also assume "b <= c" hence "f b <= f c" by (rule r)
   669   finally (ord_eq_le_trans) show ?thesis .
   670 qed
   671 
   672 lemma ord_less_eq_subst: "a < b ==> f b = c ==>
   673   (!!x y. x < y ==> f x < f y) ==> f a < c"
   674 proof -
   675   assume r: "!!x y. x < y ==> f x < f y"
   676   assume "a < b" hence "f a < f b" by (rule r)
   677   also assume "f b = c"
   678   finally (ord_less_eq_trans) show ?thesis .
   679 qed
   680 
   681 lemma ord_eq_less_subst: "a = f b ==> b < c ==>
   682   (!!x y. x < y ==> f x < f y) ==> a < f c"
   683 proof -
   684   assume r: "!!x y. x < y ==> f x < f y"
   685   assume "a = f b"
   686   also assume "b < c" hence "f b < f c" by (rule r)
   687   finally (ord_eq_less_trans) show ?thesis .
   688 qed
   689 
   690 text {*
   691   Note that this list of rules is in reverse order of priorities.
   692 *}
   693 
   694 lemmas order_trans_rules [trans] =
   695   order_less_subst2
   696   order_less_subst1
   697   order_le_less_subst2
   698   order_le_less_subst1
   699   order_less_le_subst2
   700   order_less_le_subst1
   701   order_subst2
   702   order_subst1
   703   ord_le_eq_subst
   704   ord_eq_le_subst
   705   ord_less_eq_subst
   706   ord_eq_less_subst
   707   forw_subst
   708   back_subst
   709   rev_mp
   710   mp
   711   order_neq_le_trans
   712   order_le_neq_trans
   713   order_less_trans
   714   order_less_asym'
   715   order_le_less_trans
   716   order_less_le_trans
   717   order_trans
   718   order_antisym
   719   ord_le_eq_trans
   720   ord_eq_le_trans
   721   ord_less_eq_trans
   722   ord_eq_less_trans
   723   trans
   724 
   725 
   726 (* FIXME cleanup *)
   727 
   728 text {* These support proving chains of decreasing inequalities
   729     a >= b >= c ... in Isar proofs. *}
   730 
   731 lemma xt1:
   732   "a = b ==> b > c ==> a > c"
   733   "a > b ==> b = c ==> a > c"
   734   "a = b ==> b >= c ==> a >= c"
   735   "a >= b ==> b = c ==> a >= c"
   736   "(x::'a::order) >= y ==> y >= x ==> x = y"
   737   "(x::'a::order) >= y ==> y >= z ==> x >= z"
   738   "(x::'a::order) > y ==> y >= z ==> x > z"
   739   "(x::'a::order) >= y ==> y > z ==> x > z"
   740   "(a::'a::order) > b ==> b > a ==> ?P"
   741   "(x::'a::order) > y ==> y > z ==> x > z"
   742   "(a::'a::order) >= b ==> a ~= b ==> a > b"
   743   "(a::'a::order) ~= b ==> a >= b ==> a > b"
   744   "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
   745   "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
   746   "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   747   "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
   748 by auto
   749 
   750 lemma xt2:
   751   "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   752 by (subgoal_tac "f b >= f c", force, force)
   753 
   754 lemma xt3: "(a::'a::order) >= b ==> (f b::'b::order) >= c ==> 
   755     (!!x y. x >= y ==> f x >= f y) ==> f a >= c"
   756 by (subgoal_tac "f a >= f b", force, force)
   757 
   758 lemma xt4: "(a::'a::order) > f b ==> (b::'b::order) >= c ==>
   759   (!!x y. x >= y ==> f x >= f y) ==> a > f c"
   760 by (subgoal_tac "f b >= f c", force, force)
   761 
   762 lemma xt5: "(a::'a::order) > b ==> (f b::'b::order) >= c==>
   763     (!!x y. x > y ==> f x > f y) ==> f a > c"
   764 by (subgoal_tac "f a > f b", force, force)
   765 
   766 lemma xt6: "(a::'a::order) >= f b ==> b > c ==>
   767     (!!x y. x > y ==> f x > f y) ==> a > f c"
   768 by (subgoal_tac "f b > f c", force, force)
   769 
   770 lemma xt7: "(a::'a::order) >= b ==> (f b::'b::order) > c ==>
   771     (!!x y. x >= y ==> f x >= f y) ==> f a > c"
   772 by (subgoal_tac "f a >= f b", force, force)
   773 
   774 lemma xt8: "(a::'a::order) > f b ==> (b::'b::order) > c ==>
   775     (!!x y. x > y ==> f x > f y) ==> a > f c"
   776 by (subgoal_tac "f b > f c", force, force)
   777 
   778 lemma xt9: "(a::'a::order) > b ==> (f b::'b::order) > c ==>
   779     (!!x y. x > y ==> f x > f y) ==> f a > c"
   780 by (subgoal_tac "f a > f b", force, force)
   781 
   782 lemmas xtrans = xt1 xt2 xt3 xt4 xt5 xt6 xt7 xt8 xt9
   783 
   784 (* 
   785   Since "a >= b" abbreviates "b <= a", the abbreviation "..." stands
   786   for the wrong thing in an Isar proof.
   787 
   788   The extra transitivity rules can be used as follows: 
   789 
   790 lemma "(a::'a::order) > z"
   791 proof -
   792   have "a >= b" (is "_ >= ?rhs")
   793     sorry
   794   also have "?rhs >= c" (is "_ >= ?rhs")
   795     sorry
   796   also (xtrans) have "?rhs = d" (is "_ = ?rhs")
   797     sorry
   798   also (xtrans) have "?rhs >= e" (is "_ >= ?rhs")
   799     sorry
   800   also (xtrans) have "?rhs > f" (is "_ > ?rhs")
   801     sorry
   802   also (xtrans) have "?rhs > z"
   803     sorry
   804   finally (xtrans) show ?thesis .
   805 qed
   806 
   807   Alternatively, one can use "declare xtrans [trans]" and then
   808   leave out the "(xtrans)" above.
   809 *)
   810 
   811 subsection {* Order on bool *}
   812 
   813 instance bool :: order 
   814   le_bool_def: "P \<le> Q \<equiv> P \<longrightarrow> Q"
   815   less_bool_def: "P < Q \<equiv> P \<le> Q \<and> P \<noteq> Q"
   816   by intro_classes (auto simp add: le_bool_def less_bool_def)
   817 
   818 lemma le_boolI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<le> Q"
   819   by (simp add: le_bool_def)
   820 
   821 lemma le_boolI': "P \<longrightarrow> Q \<Longrightarrow> P \<le> Q"
   822   by (simp add: le_bool_def)
   823 
   824 lemma le_boolE: "P \<le> Q \<Longrightarrow> P \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
   825   by (simp add: le_bool_def)
   826 
   827 lemma le_boolD: "P \<le> Q \<Longrightarrow> P \<longrightarrow> Q"
   828   by (simp add: le_bool_def)
   829 
   830 lemma [code func]:
   831   "False \<le> b \<longleftrightarrow> True"
   832   "True \<le> b \<longleftrightarrow> b"
   833   "False < b \<longleftrightarrow> b"
   834   "True < b \<longleftrightarrow> False"
   835   unfolding le_bool_def less_bool_def by simp_all
   836 
   837 
   838 subsection {* Monotonicity, syntactic least value operator and min/max *}
   839 
   840 locale mono =
   841   fixes f
   842   assumes mono: "A \<le> B \<Longrightarrow> f A \<le> f B"
   843 
   844 lemmas monoI [intro?] = mono.intro
   845   and monoD [dest?] = mono.mono
   846 
   847 constdefs
   848   Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
   849   "Least P == THE x. P x & (ALL y. P y --> x <= y)"
   850     -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
   851 
   852 lemma LeastI2_order:
   853   "[| P (x::'a::order);
   854       !!y. P y ==> x <= y;
   855       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
   856    ==> Q (Least P)"
   857   apply (unfold Least_def)
   858   apply (rule theI2)
   859     apply (blast intro: order_antisym)+
   860   done
   861 
   862 lemma Least_equality:
   863     "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
   864   apply (simp add: Least_def)
   865   apply (rule the_equality)
   866   apply (auto intro!: order_antisym)
   867   done
   868 
   869 lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
   870   by (simp add: min_def)
   871 
   872 lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
   873   by (simp add: max_def)
   874 
   875 lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
   876   apply (simp add: min_def)
   877   apply (blast intro: order_antisym)
   878   done
   879 
   880 lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
   881   apply (simp add: max_def)
   882   apply (blast intro: order_antisym)
   883   done
   884 
   885 lemma min_of_mono:
   886     "(!!x y. (f x <= f y) = (x <= y)) ==> min (f m) (f n) = f (min m n)"
   887   by (simp add: min_def)
   888 
   889 lemma max_of_mono:
   890     "(!!x y. (f x <= f y) = (x <= y)) ==> max (f m) (f n) = f (max m n)"
   891   by (simp add: max_def)
   892 
   893 
   894 subsection {* legacy ML bindings *}
   895 
   896 ML {*
   897 val monoI = @{thm monoI};
   898 *}
   899 
   900 end