src/HOL/Orderings.thy
changeset 25076 a50b36401c61
parent 25062 af5ef0d4d655
child 25103 1ee419a5a30f
equal deleted inserted replaced
25075:8717d93b0fe2 25076:a50b36401c61
    16 class order = ord +
    16 class order = ord +
    17   assumes less_le: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> y"
    17   assumes less_le: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> y"
    18   and order_refl [iff]: "x \<le> x"
    18   and order_refl [iff]: "x \<le> x"
    19   and order_trans: "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
    19   and order_trans: "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
    20   assumes antisym: "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
    20   assumes antisym: "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
    21 
       
    22 begin
    21 begin
    23 
    22 
    24 notation (input)
    23 notation (input)
    25   less_eq (infix "\<sqsubseteq>" 50)
    24   less_eq (infix "\<sqsubseteq>" 50)
    26 and
    25 and
   366 setup Orders.setup
   365 setup Orders.setup
   367 
   366 
   368 
   367 
   369 text {* Declarations to set up transitivity reasoner of partial and linear orders. *}
   368 text {* Declarations to set up transitivity reasoner of partial and linear orders. *}
   370 
   369 
       
   370 context order
       
   371 begin
       
   372 
   371 (* The type constraint on @{term op =} below is necessary since the operation
   373 (* The type constraint on @{term op =} below is necessary since the operation
   372    is not a parameter of the locale. *)
   374    is not a parameter of the locale. *)
   373 lemmas (in order)
   375 
   374   [order add less_reflE: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   376 lemmas
       
   377   [order add less_reflE: order "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool" "op <=" "op <"] =
   375   less_irrefl [THEN notE]
   378   less_irrefl [THEN notE]
   376 lemmas (in order)
   379 lemmas
   377   [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   380   [order add le_refl: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   378   order_refl
   381   order_refl
   379 lemmas (in order)
   382 lemmas
   380   [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   383   [order add less_imp_le: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   381   less_imp_le
   384   less_imp_le
   382 lemmas (in order)
   385 lemmas
   383   [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   386   [order add eqI: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   384   antisym
   387   antisym
   385 lemmas (in order)
   388 lemmas
   386   [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   389   [order add eqD1: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   387   eq_refl
   390   eq_refl
   388 lemmas (in order)
   391 lemmas
   389   [order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   392   [order add eqD2: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   390   sym [THEN eq_refl]
   393   sym [THEN eq_refl]
   391 lemmas (in order)
   394 lemmas
   392   [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   395   [order add less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   393   less_trans
   396   less_trans
   394 lemmas (in order)
   397 lemmas
   395   [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   398   [order add less_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   396   less_le_trans
   399   less_le_trans
   397 lemmas (in order)
   400 lemmas
   398   [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   401   [order add le_less_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   399   le_less_trans
   402   le_less_trans
   400 lemmas (in order)
   403 lemmas
   401   [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   404   [order add le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   402   order_trans
   405   order_trans
   403 lemmas (in order)
   406 lemmas
   404   [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   407   [order add le_neq_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   405   le_neq_trans
   408   le_neq_trans
   406 lemmas (in order)
   409 lemmas
   407   [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   410   [order add neq_le_trans: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   408   neq_le_trans
   411   neq_le_trans
   409 lemmas (in order)
   412 lemmas
   410   [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   413   [order add less_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   411   less_imp_neq
   414   less_imp_neq
   412 lemmas (in order)
   415 lemmas
   413   [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   416   [order add eq_neq_eq_imp_neq: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   414    eq_neq_eq_imp_neq
   417    eq_neq_eq_imp_neq
   415 lemmas (in order)
   418 lemmas
   416   [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   419   [order add not_sym: order "op = :: 'a => 'a => bool" "op <=" "op <"] =
   417   not_sym
   420   not_sym
   418 
   421 
   419 lemmas (in linorder) [order del: order "op = :: 'a => 'a => bool" "op <=" "op <"] = _
   422 end
   420 
   423 
   421 lemmas (in linorder)
   424 context linorder
       
   425 begin
       
   426 
       
   427 lemmas
       
   428   [order del: order "op = :: 'a => 'a => bool" "op <=" "op <"] = _
       
   429 
       
   430 lemmas
   422   [order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   431   [order add less_reflE: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   423   less_irrefl [THEN notE]
   432   less_irrefl [THEN notE]
   424 lemmas (in linorder)
   433 lemmas
   425   [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   434   [order add le_refl: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   426   order_refl
   435   order_refl
   427 lemmas (in linorder)
   436 lemmas
   428   [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   437   [order add less_imp_le: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   429   less_imp_le
   438   less_imp_le
   430 lemmas (in linorder)
   439 lemmas
   431   [order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   440   [order add not_lessI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   432   not_less [THEN iffD2]
   441   not_less [THEN iffD2]
   433 lemmas (in linorder)
   442 lemmas
   434   [order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   443   [order add not_leI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   435   not_le [THEN iffD2]
   444   not_le [THEN iffD2]
   436 lemmas (in linorder)
   445 lemmas
   437   [order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   446   [order add not_lessD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   438   not_less [THEN iffD1]
   447   not_less [THEN iffD1]
   439 lemmas (in linorder)
   448 lemmas
   440   [order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   449   [order add not_leD: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   441   not_le [THEN iffD1]
   450   not_le [THEN iffD1]
   442 lemmas (in linorder)
   451 lemmas
   443   [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   452   [order add eqI: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   444   antisym
   453   antisym
   445 lemmas (in linorder)
   454 lemmas
   446   [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   455   [order add eqD1: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   447   eq_refl
   456   eq_refl
   448 lemmas (in linorder)
   457 lemmas
   449   [order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   458   [order add eqD2: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   450   sym [THEN eq_refl]
   459   sym [THEN eq_refl]
   451 lemmas (in linorder)
   460 lemmas
   452   [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   461   [order add less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   453   less_trans
   462   less_trans
   454 lemmas (in linorder)
   463 lemmas
   455   [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   464   [order add less_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   456   less_le_trans
   465   less_le_trans
   457 lemmas (in linorder)
   466 lemmas
   458   [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   467   [order add le_less_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   459   le_less_trans
   468   le_less_trans
   460 lemmas (in linorder)
   469 lemmas
   461   [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   470   [order add le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   462   order_trans
   471   order_trans
   463 lemmas (in linorder)
   472 lemmas
   464   [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   473   [order add le_neq_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   465   le_neq_trans
   474   le_neq_trans
   466 lemmas (in linorder)
   475 lemmas
   467   [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   476   [order add neq_le_trans: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   468   neq_le_trans
   477   neq_le_trans
   469 lemmas (in linorder)
   478 lemmas
   470   [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   479   [order add less_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   471   less_imp_neq
   480   less_imp_neq
   472 lemmas (in linorder)
   481 lemmas
   473   [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   482   [order add eq_neq_eq_imp_neq: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   474   eq_neq_eq_imp_neq
   483   eq_neq_eq_imp_neq
   475 lemmas (in linorder)
   484 lemmas
   476   [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   485   [order add not_sym: linorder "op = :: 'a => 'a => bool" "op <=" "op <"] =
   477   not_sym
   486   not_sym
       
   487 
       
   488 end
   478 
   489 
   479 
   490 
   480 setup {*
   491 setup {*
   481 let
   492 let
   482 
   493 
   535 
   546 
   536 
   547 
   537 subsection {* Dense orders *}
   548 subsection {* Dense orders *}
   538 
   549 
   539 class dense_linear_order = linorder + 
   550 class dense_linear_order = linorder + 
   540   assumes gt_ex: "\<exists>y. x \<sqsubset> y" 
   551   assumes gt_ex: "\<exists>y. x < y" 
   541   and lt_ex: "\<exists>y. y \<sqsubset> x"
   552   and lt_ex: "\<exists>y. y < x"
   542   and dense: "x \<sqsubset> y \<Longrightarrow> (\<exists>z. x \<sqsubset> z \<and> z \<sqsubset> y)"
   553   and dense: "x < y \<Longrightarrow> (\<exists>z. x < z \<and> z < y)"
   543   (*see further theory Dense_Linear_Order*)
   554   (*see further theory Dense_Linear_Order*)
   544 
   555 begin
   545 
   556 
   546 lemma interval_empty_iff:
   557 lemma interval_empty_iff:
   547   fixes x y z :: "'a\<Colon>dense_linear_order"
   558   "{y. x < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
   548   shows "{y. x < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
       
   549   by (auto dest: dense)
   559   by (auto dest: dense)
       
   560 
       
   561 end
   550 
   562 
   551 subsection {* Name duplicates *}
   563 subsection {* Name duplicates *}
   552 
   564 
   553 lemmas order_less_le = less_le
   565 lemmas order_less_le = less_le
   554 lemmas order_eq_refl = order_class.eq_refl
   566 lemmas order_eq_refl = order_class.eq_refl
   858   "(a::'a::order) ~= b ==> a >= b ==> a > b"
   870   "(a::'a::order) ~= b ==> a >= b ==> a > b"
   859   "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
   871   "a = f b ==> b > c ==> (!!x y. x > y ==> f x > f y) ==> a > f c" 
   860   "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
   872   "a > b ==> f b = c ==> (!!x y. x > y ==> f x > f y) ==> f a > c"
   861   "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   873   "a = f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   862   "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
   874   "a >= b ==> f b = c ==> (!! x y. x >= y ==> f x >= f y) ==> f a >= c"
   863 by auto
   875   by auto
   864 
   876 
   865 lemma xt2:
   877 lemma xt2:
   866   "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   878   "(a::'a::order) >= f b ==> b >= c ==> (!!x y. x >= y ==> f x >= f y) ==> a >= f c"
   867 by (subgoal_tac "f b >= f c", force, force)
   879 by (subgoal_tac "f b >= f c", force, force)
   868 
   880 
  1025   by (rule predicate2D)
  1037   by (rule predicate2D)
  1026 
  1038 
  1027 
  1039 
  1028 subsection {* Monotonicity, least value operator and min/max *}
  1040 subsection {* Monotonicity, least value operator and min/max *}
  1029 
  1041 
  1030 locale mono =
  1042 context order
  1031   fixes f
  1043 begin
  1032   assumes mono: "A \<le> B \<Longrightarrow> f A \<le> f B"
  1044 
  1033 
  1045 definition
  1034 lemmas monoI [intro?] = mono.intro
  1046   mono :: "('a \<Rightarrow> 'b\<Colon>order) \<Rightarrow> bool"
  1035   and monoD [dest?] = mono.mono
  1047 where
       
  1048   "mono f \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> f x \<le> f y)"
       
  1049 
       
  1050 lemma monoI [intro?]:
       
  1051   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
       
  1052   shows "(\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y) \<Longrightarrow> mono f"
       
  1053   unfolding mono_def by iprover
       
  1054 
       
  1055 lemma monoD [dest?]:
       
  1056   fixes f :: "'a \<Rightarrow> 'b\<Colon>order"
       
  1057   shows "mono f \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
       
  1058   unfolding mono_def by iprover
       
  1059 
       
  1060 end
       
  1061 
       
  1062 context linorder
       
  1063 begin
       
  1064 
       
  1065 lemma min_of_mono:
       
  1066   fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
       
  1067   shows "mono f \<Longrightarrow> Orderings.min (f m) (f n) = f (min m n)"
       
  1068   by (auto simp: mono_def Orderings.min_def min_def intro: Orderings.antisym)
       
  1069 
       
  1070 lemma max_of_mono:
       
  1071   fixes f :: "'a \<Rightarrow> 'b\<Colon>linorder"
       
  1072   shows "mono f \<Longrightarrow> Orderings.max (f m) (f n) = f (max m n)"
       
  1073   by (auto simp: mono_def Orderings.max_def max_def intro: Orderings.antisym)
       
  1074 
       
  1075 end
  1036 
  1076 
  1037 lemma LeastI2_order:
  1077 lemma LeastI2_order:
  1038   "[| P (x::'a::order);
  1078   "[| P (x::'a::order);
  1039       !!y. P y ==> x <= y;
  1079       !!y. P y ==> x <= y;
  1040       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
  1080       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
  1075 lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
  1115 lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
  1076 apply (simp add: max_def)
  1116 apply (simp add: max_def)
  1077 apply (blast intro: order_antisym)
  1117 apply (blast intro: order_antisym)
  1078 done
  1118 done
  1079 
  1119 
  1080 lemma min_of_mono:
       
  1081   "(!!x y. (f x <= f y) = (x <= y)) ==> min (f m) (f n) = f (min m n)"
       
  1082 by (simp add: min_def)
       
  1083 
       
  1084 lemma max_of_mono:
       
  1085   "(!!x y. (f x <= f y) = (x <= y)) ==> max (f m) (f n) = f (max m n)"
       
  1086 by (simp add: max_def)
       
  1087 
       
  1088 
       
  1089 subsection {* legacy ML bindings *}
  1120 subsection {* legacy ML bindings *}
  1090 
  1121 
  1091 ML {*
  1122 ML {*
  1092 val monoI = @{thm monoI};
  1123 val monoI = @{thm monoI};
  1093 *}
  1124 *}