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