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