src/HOL/Library/Dense_Linear_Order.thy
 author haftmann Mon Jul 07 08:47:17 2008 +0200 (2008-07-07) changeset 27487 c8a6ce181805 parent 27368 9f90ac19e32b child 27825 12254665fc41 permissions -rw-r--r--
absolute imports of HOL/*.thy theories
```     1 (*
```
```     2     ID:         \$Id\$
```
```     3     Author:     Amine Chaieb, TU Muenchen
```
```     4 *)
```
```     5
```
```     6 header {* Dense linear order without endpoints
```
```     7   and a quantifier elimination procedure in Ferrante and Rackoff style *}
```
```     8
```
```     9 theory Dense_Linear_Order
```
```    10 imports Plain "~~/src/HOL/Presburger"
```
```    11 uses
```
```    12   "~~/src/HOL/Tools/Qelim/qelim.ML"
```
```    13   "~~/src/HOL/Tools/Qelim/langford_data.ML"
```
```    14   "~~/src/HOL/Tools/Qelim/ferrante_rackoff_data.ML"
```
```    15   ("~~/src/HOL/Tools/Qelim/langford.ML")
```
```    16   ("~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML")
```
```    17 begin
```
```    18
```
```    19 setup Langford_Data.setup
```
```    20 setup Ferrante_Rackoff_Data.setup
```
```    21
```
```    22 context linorder
```
```    23 begin
```
```    24
```
```    25 lemma less_not_permute: "\<not> (x < y \<and> y < x)" by (simp add: not_less linear)
```
```    26
```
```    27 lemma gather_simps:
```
```    28   shows
```
```    29   "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> x < u \<and> P x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> (insert u U). x < y) \<and> P x)"
```
```    30   and "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> l < x \<and> P x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> (insert l L). y < x) \<and> (\<forall>y \<in> U. x < y) \<and> P x)"
```
```    31   "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> x < u) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> (insert u U). x < y))"
```
```    32   and "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> l < x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> (insert l L). y < x) \<and> (\<forall>y \<in> U. x < y))"  by auto
```
```    33
```
```    34 lemma
```
```    35   gather_start: "(\<exists>x. P x) \<equiv> (\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y\<in> {}. x < y) \<and> P x)"
```
```    36   by simp
```
```    37
```
```    38 text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"}*}
```
```    39 lemma minf_lt:  "\<exists>z . \<forall>x. x < z \<longrightarrow> (x < t \<longleftrightarrow> True)" by auto
```
```    40 lemma minf_gt: "\<exists>z . \<forall>x. x < z \<longrightarrow>  (t < x \<longleftrightarrow>  False)"
```
```    41   by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
```
```    42
```
```    43 lemma minf_le: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<le> t \<longleftrightarrow> True)" by (auto simp add: less_le)
```
```    44 lemma minf_ge: "\<exists>z. \<forall>x. x < z \<longrightarrow> (t \<le> x \<longleftrightarrow> False)"
```
```    45   by (auto simp add: less_le not_less not_le)
```
```    46 lemma minf_eq: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
```
```    47 lemma minf_neq: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
```
```    48 lemma minf_P: "\<exists>z. \<forall>x. x < z \<longrightarrow> (P \<longleftrightarrow> P)" by blast
```
```    49
```
```    50 text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"}*}
```
```    51 lemma pinf_gt:  "\<exists>z . \<forall>x. z < x \<longrightarrow> (t < x \<longleftrightarrow> True)" by auto
```
```    52 lemma pinf_lt: "\<exists>z . \<forall>x. z < x \<longrightarrow>  (x < t \<longleftrightarrow>  False)"
```
```    53   by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
```
```    54
```
```    55 lemma pinf_ge: "\<exists>z. \<forall>x. z < x \<longrightarrow> (t \<le> x \<longleftrightarrow> True)" by (auto simp add: less_le)
```
```    56 lemma pinf_le: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<le> t \<longleftrightarrow> False)"
```
```    57   by (auto simp add: less_le not_less not_le)
```
```    58 lemma pinf_eq: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
```
```    59 lemma pinf_neq: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
```
```    60 lemma pinf_P: "\<exists>z. \<forall>x. z < x \<longrightarrow> (P \<longleftrightarrow> P)" by blast
```
```    61
```
```    62 lemma nmi_lt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x < t \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    63 lemma nmi_gt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> t < x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)"
```
```    64   by (auto simp add: le_less)
```
```    65 lemma  nmi_le: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x\<le> t \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    66 lemma  nmi_ge: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> t\<le> x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    67 lemma  nmi_eq: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and>  x = t \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    68 lemma  nmi_neq: "t \<in> U \<Longrightarrow>\<forall>x. \<not>True \<and> x \<noteq> t \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    69 lemma  nmi_P: "\<forall> x. ~P \<and> P \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    70 lemma  nmi_conj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x) ;
```
```    71   \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
```
```    72   \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    73 lemma  nmi_disj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x) ;
```
```    74   \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
```
```    75   \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
```
```    76
```
```    77 lemma  npi_lt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and>  x < t \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by (auto simp add: le_less)
```
```    78 lemma  npi_gt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> t < x \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
```
```    79 lemma  npi_le: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and>  x \<le> t \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
```
```    80 lemma  npi_ge: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> t \<le> x \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
```
```    81 lemma  npi_eq: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and>  x = t \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
```
```    82 lemma  npi_neq: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x \<noteq> t \<longrightarrow>  (\<exists> u\<in> U. x \<le> u )" by auto
```
```    83 lemma  npi_P: "\<forall> x. ~P \<and> P \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
```
```    84 lemma  npi_conj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow>  (\<exists> u\<in> U. x \<le> u) ;  \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)\<rbrakk>
```
```    85   \<Longrightarrow>  \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
```
```    86 lemma  npi_disj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow>  (\<exists> u\<in> U. x \<le> u) ; \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)\<rbrakk>
```
```    87   \<Longrightarrow> \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
```
```    88
```
```    89 lemma lin_dense_lt: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t < u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x < t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y < t)"
```
```    90 proof(clarsimp)
```
```    91   fix x l u y  assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x"
```
```    92     and xu: "x<u"  and px: "x < t" and ly: "l<y" and yu:"y < u"
```
```    93   from tU noU ly yu have tny: "t\<noteq>y" by auto
```
```    94   {assume H: "t < y"
```
```    95     from less_trans[OF lx px] less_trans[OF H yu]
```
```    96     have "l < t \<and> t < u"  by simp
```
```    97     with tU noU have "False" by auto}
```
```    98   hence "\<not> t < y"  by auto hence "y \<le> t" by (simp add: not_less)
```
```    99   thus "y < t" using tny by (simp add: less_le)
```
```   100 qed
```
```   101
```
```   102 lemma lin_dense_gt: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l < x \<and> x < u \<and> t < x \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> t < y)"
```
```   103 proof(clarsimp)
```
```   104   fix x l u y
```
```   105   assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
```
```   106   and px: "t < x" and ly: "l<y" and yu:"y < u"
```
```   107   from tU noU ly yu have tny: "t\<noteq>y" by auto
```
```   108   {assume H: "y< t"
```
```   109     from less_trans[OF ly H] less_trans[OF px xu] have "l < t \<and> t < u" by simp
```
```   110     with tU noU have "False" by auto}
```
```   111   hence "\<not> y<t"  by auto hence "t \<le> y" by (auto simp add: not_less)
```
```   112   thus "t < y" using tny by (simp add:less_le)
```
```   113 qed
```
```   114
```
```   115 lemma lin_dense_le: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x \<le> t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y\<le> t)"
```
```   116 proof(clarsimp)
```
```   117   fix x l u y
```
```   118   assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
```
```   119   and px: "x \<le> t" and ly: "l<y" and yu:"y < u"
```
```   120   from tU noU ly yu have tny: "t\<noteq>y" by auto
```
```   121   {assume H: "t < y"
```
```   122     from less_le_trans[OF lx px] less_trans[OF H yu]
```
```   123     have "l < t \<and> t < u" by simp
```
```   124     with tU noU have "False" by auto}
```
```   125   hence "\<not> t < y"  by auto thus "y \<le> t" by (simp add: not_less)
```
```   126 qed
```
```   127
```
```   128 lemma lin_dense_ge: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> t \<le> x \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> t \<le> y)"
```
```   129 proof(clarsimp)
```
```   130   fix x l u y
```
```   131   assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
```
```   132   and px: "t \<le> x" and ly: "l<y" and yu:"y < u"
```
```   133   from tU noU ly yu have tny: "t\<noteq>y" by auto
```
```   134   {assume H: "y< t"
```
```   135     from less_trans[OF ly H] le_less_trans[OF px xu]
```
```   136     have "l < t \<and> t < u" by simp
```
```   137     with tU noU have "False" by auto}
```
```   138   hence "\<not> y<t"  by auto thus "t \<le> y" by (simp add: not_less)
```
```   139 qed
```
```   140 lemma lin_dense_eq: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x = t   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y= t)"  by auto
```
```   141 lemma lin_dense_neq: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x \<noteq> t   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y\<noteq> t)"  by auto
```
```   142 lemma lin_dense_P: "\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P)"  by auto
```
```   143
```
```   144 lemma lin_dense_conj:
```
```   145   "\<lbrakk>\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P1 x
```
```   146   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
```
```   147   \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
```
```   148   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
```
```   149   \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> (P1 x \<and> P2 x)
```
```   150   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<and> P2 y))"
```
```   151   by blast
```
```   152 lemma lin_dense_disj:
```
```   153   "\<lbrakk>\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P1 x
```
```   154   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
```
```   155   \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
```
```   156   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
```
```   157   \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> (P1 x \<or> P2 x)
```
```   158   \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<or> P2 y))"
```
```   159   by blast
```
```   160
```
```   161 lemma npmibnd: "\<lbrakk>\<forall>x. \<not> MP \<and> P x \<longrightarrow> (\<exists> u\<in> U. u \<le> x); \<forall>x. \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)\<rbrakk>
```
```   162   \<Longrightarrow> \<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<le> x \<and> x \<le> u')"
```
```   163 by auto
```
```   164
```
```   165 lemma finite_set_intervals:
```
```   166   assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
```
```   167   and uinS: "u \<in> S" and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
```
```   168   shows "\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a \<le> x \<and> x \<le> b \<and> P x"
```
```   169 proof-
```
```   170   let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
```
```   171   let ?xM = "{y. y\<in> S \<and> x \<le> y}"
```
```   172   let ?a = "Max ?Mx"
```
```   173   let ?b = "Min ?xM"
```
```   174   have MxS: "?Mx \<subseteq> S" by blast
```
```   175   hence fMx: "finite ?Mx" using fS finite_subset by auto
```
```   176   from lx linS have linMx: "l \<in> ?Mx" by blast
```
```   177   hence Mxne: "?Mx \<noteq> {}" by blast
```
```   178   have xMS: "?xM \<subseteq> S" by blast
```
```   179   hence fxM: "finite ?xM" using fS finite_subset by auto
```
```   180   from xu uinS have linxM: "u \<in> ?xM" by blast
```
```   181   hence xMne: "?xM \<noteq> {}" by blast
```
```   182   have ax:"?a \<le> x" using Mxne fMx by auto
```
```   183   have xb:"x \<le> ?b" using xMne fxM by auto
```
```   184   have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
```
```   185   have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
```
```   186   have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
```
```   187   proof(clarsimp)
```
```   188     fix y   assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
```
```   189     from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by (auto simp add: linear)
```
```   190     moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by (simp add: not_le[symmetric])}
```
```   191     moreover {assume "y \<in> ?xM" hence "?b \<le> y" using xMne fxM by auto with yb have "False" by (simp add: not_le[symmetric])}
```
```   192     ultimately show "False" by blast
```
```   193   qed
```
```   194   from ainS binS noy ax xb px show ?thesis by blast
```
```   195 qed
```
```   196
```
```   197 lemma finite_set_intervals2:
```
```   198   assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
```
```   199   and uinS: "u \<in> S" and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
```
```   200   shows "(\<exists> s\<in> S. P s) \<or> (\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a < x \<and> x < b \<and> P x)"
```
```   201 proof-
```
```   202   from finite_set_intervals[where P="P", OF px lx xu linS uinS fS lS Su]
```
```   203   obtain a and b where
```
```   204     as: "a\<in> S" and bs: "b\<in> S" and noS:"\<forall>y. a < y \<and> y < b \<longrightarrow> y \<notin> S"
```
```   205     and axb: "a \<le> x \<and> x \<le> b \<and> P x"  by auto
```
```   206   from axb have "x= a \<or> x= b \<or> (a < x \<and> x < b)" by (auto simp add: le_less)
```
```   207   thus ?thesis using px as bs noS by blast
```
```   208 qed
```
```   209
```
```   210 end
```
```   211
```
```   212 section {* The classical QE after Langford for dense linear orders *}
```
```   213
```
```   214 context dense_linear_order
```
```   215 begin
```
```   216
```
```   217 lemma dlo_qe_bnds:
```
```   218   assumes ne: "L \<noteq> {}" and neU: "U \<noteq> {}" and fL: "finite L" and fU: "finite U"
```
```   219   shows "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y)) \<equiv> (\<forall> l \<in> L. \<forall>u \<in> U. l < u)"
```
```   220 proof (simp only: atomize_eq, rule iffI)
```
```   221   assume H: "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)"
```
```   222   then obtain x where xL: "\<forall>y\<in>L. y < x" and xU: "\<forall>y\<in>U. x < y" by blast
```
```   223   {fix l u assume l: "l \<in> L" and u: "u \<in> U"
```
```   224     have "l < x" using xL l by blast
```
```   225     also have "x < u" using xU u by blast
```
```   226     finally (less_trans) have "l < u" .}
```
```   227   thus "\<forall>l\<in>L. \<forall>u\<in>U. l < u" by blast
```
```   228 next
```
```   229   assume H: "\<forall>l\<in>L. \<forall>u\<in>U. l < u"
```
```   230   let ?ML = "Max L"
```
```   231   let ?MU = "Min U"
```
```   232   from fL ne have th1: "?ML \<in> L" and th1': "\<forall>l\<in>L. l \<le> ?ML" by auto
```
```   233   from fU neU have th2: "?MU \<in> U" and th2': "\<forall>u\<in>U. ?MU \<le> u" by auto
```
```   234   from th1 th2 H have "?ML < ?MU" by auto
```
```   235   with dense obtain w where th3: "?ML < w" and th4: "w < ?MU" by blast
```
```   236   from th3 th1' have "\<forall>l \<in> L. l < w" by auto
```
```   237   moreover from th4 th2' have "\<forall>u \<in> U. w < u" by auto
```
```   238   ultimately show "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)" by auto
```
```   239 qed
```
```   240
```
```   241 lemma dlo_qe_noub:
```
```   242   assumes ne: "L \<noteq> {}" and fL: "finite L"
```
```   243   shows "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> {}. x < y)) \<equiv> True"
```
```   244 proof(simp add: atomize_eq)
```
```   245   from gt_ex[of "Max L"] obtain M where M: "Max L < M" by blast
```
```   246   from ne fL have "\<forall>x \<in> L. x \<le> Max L" by simp
```
```   247   with M have "\<forall>x\<in>L. x < M" by (auto intro: le_less_trans)
```
```   248   thus "\<exists>x. \<forall>y\<in>L. y < x" by blast
```
```   249 qed
```
```   250
```
```   251 lemma dlo_qe_nolb:
```
```   252   assumes ne: "U \<noteq> {}" and fU: "finite U"
```
```   253   shows "(\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y \<in> U. x < y)) \<equiv> True"
```
```   254 proof(simp add: atomize_eq)
```
```   255   from lt_ex[of "Min U"] obtain M where M: "M < Min U" by blast
```
```   256   from ne fU have "\<forall>x \<in> U. Min U \<le> x" by simp
```
```   257   with M have "\<forall>x\<in>U. M < x" by (auto intro: less_le_trans)
```
```   258   thus "\<exists>x. \<forall>y\<in>U. x < y" by blast
```
```   259 qed
```
```   260
```
```   261 lemma exists_neq: "\<exists>(x::'a). x \<noteq> t" "\<exists>(x::'a). t \<noteq> x"
```
```   262   using gt_ex[of t] by auto
```
```   263
```
```   264 lemmas dlo_simps = order_refl less_irrefl not_less not_le exists_neq
```
```   265   le_less neq_iff linear less_not_permute
```
```   266
```
```   267 lemma axiom: "dense_linear_order (op \<le>) (op <)" by (rule dense_linear_order_axioms)
```
```   268 lemma atoms:
```
```   269   includes meta_term_syntax
```
```   270   shows "TERM (less :: 'a \<Rightarrow> _)"
```
```   271     and "TERM (less_eq :: 'a \<Rightarrow> _)"
```
```   272     and "TERM (op = :: 'a \<Rightarrow> _)" .
```
```   273
```
```   274 declare axiom[langford qe: dlo_qe_bnds dlo_qe_nolb dlo_qe_noub gather: gather_start gather_simps atoms: atoms]
```
```   275 declare dlo_simps[langfordsimp]
```
```   276
```
```   277 end
```
```   278
```
```   279 (* FIXME: Move to HOL -- together with the conj_aci_rule in langford.ML *)
```
```   280 lemma dnf:
```
```   281   "(P & (Q | R)) = ((P&Q) | (P&R))"
```
```   282   "((Q | R) & P) = ((Q&P) | (R&P))"
```
```   283   by blast+
```
```   284
```
```   285 lemmas weak_dnf_simps = simp_thms dnf
```
```   286
```
```   287 lemma nnf_simps:
```
```   288     "(\<not>(P \<and> Q)) = (\<not>P \<or> \<not>Q)" "(\<not>(P \<or> Q)) = (\<not>P \<and> \<not>Q)" "(P \<longrightarrow> Q) = (\<not>P \<or> Q)"
```
```   289     "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not> \<not>(P)) = P"
```
```   290   by blast+
```
```   291
```
```   292 lemma ex_distrib: "(\<exists>x. P x \<or> Q x) \<longleftrightarrow> ((\<exists>x. P x) \<or> (\<exists>x. Q x))" by blast
```
```   293
```
```   294 lemmas dnf_simps = weak_dnf_simps nnf_simps ex_distrib
```
```   295
```
```   296 use "~~/src/HOL/Tools/Qelim/langford.ML"
```
```   297 method_setup dlo = {*
```
```   298   Method.ctxt_args (Method.SIMPLE_METHOD' o LangfordQE.dlo_tac)
```
```   299 *} "Langford's algorithm for quantifier elimination in dense linear orders"
```
```   300
```
```   301
```
```   302 section {* Contructive dense linear orders yield QE for linear arithmetic over ordered Fields -- see @{text "Arith_Tools.thy"} *}
```
```   303
```
```   304 text {* Linear order without upper bounds *}
```
```   305
```
```   306 locale linorder_stupid_syntax = linorder
```
```   307 begin
```
```   308 notation
```
```   309   less_eq  ("op \<sqsubseteq>") and
```
```   310   less_eq  ("(_/ \<sqsubseteq> _)" [51, 51] 50) and
```
```   311   less  ("op \<sqsubset>") and
```
```   312   less  ("(_/ \<sqsubset> _)"  [51, 51] 50)
```
```   313
```
```   314 end
```
```   315
```
```   316 locale linorder_no_ub = linorder_stupid_syntax +
```
```   317   assumes gt_ex: "\<exists>y. less x y"
```
```   318 begin
```
```   319 lemma ge_ex: "\<exists>y. x \<sqsubseteq> y" using gt_ex by auto
```
```   320
```
```   321 text {* Theorems for @{text "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"} *}
```
```   322 lemma pinf_conj:
```
```   323   assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
```
```   324   and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
```
```   325   shows "\<exists>z. \<forall>x. z \<sqsubset>  x \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
```
```   326 proof-
```
```   327   from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
```
```   328      and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
```
```   329   from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
```
```   330   from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
```
```   331   {fix x assume H: "z \<sqsubset> x"
```
```   332     from less_trans[OF zz1 H] less_trans[OF zz2 H]
```
```   333     have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')"  using z1 zz1 z2 zz2 by auto
```
```   334   }
```
```   335   thus ?thesis by blast
```
```   336 qed
```
```   337
```
```   338 lemma pinf_disj:
```
```   339   assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
```
```   340   and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
```
```   341   shows "\<exists>z. \<forall>x. z \<sqsubset>  x \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
```
```   342 proof-
```
```   343   from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
```
```   344      and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
```
```   345   from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
```
```   346   from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
```
```   347   {fix x assume H: "z \<sqsubset> x"
```
```   348     from less_trans[OF zz1 H] less_trans[OF zz2 H]
```
```   349     have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')"  using z1 zz1 z2 zz2 by auto
```
```   350   }
```
```   351   thus ?thesis by blast
```
```   352 qed
```
```   353
```
```   354 lemma pinf_ex: assumes ex:"\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P1)" and p1: P1 shows "\<exists> x. P x"
```
```   355 proof-
```
```   356   from ex obtain z where z: "\<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
```
```   357   from gt_ex obtain x where x: "z \<sqsubset> x" by blast
```
```   358   from z x p1 show ?thesis by blast
```
```   359 qed
```
```   360
```
```   361 end
```
```   362
```
```   363 text {* Linear order without upper bounds *}
```
```   364
```
```   365 locale linorder_no_lb = linorder_stupid_syntax +
```
```   366   assumes lt_ex: "\<exists>y. less y x"
```
```   367 begin
```
```   368 lemma le_ex: "\<exists>y. y \<sqsubseteq> x" using lt_ex by auto
```
```   369
```
```   370
```
```   371 text {* Theorems for @{text "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"} *}
```
```   372 lemma minf_conj:
```
```   373   assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
```
```   374   and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
```
```   375   shows "\<exists>z. \<forall>x. x \<sqsubset>  z \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
```
```   376 proof-
```
```   377   from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"and z2: "\<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
```
```   378   from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
```
```   379   from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
```
```   380   {fix x assume H: "x \<sqsubset> z"
```
```   381     from less_trans[OF H zz1] less_trans[OF H zz2]
```
```   382     have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')"  using z1 zz1 z2 zz2 by auto
```
```   383   }
```
```   384   thus ?thesis by blast
```
```   385 qed
```
```   386
```
```   387 lemma minf_disj:
```
```   388   assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
```
```   389   and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
```
```   390   shows "\<exists>z. \<forall>x. x \<sqsubset>  z \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
```
```   391 proof-
```
```   392   from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"and z2: "\<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
```
```   393   from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
```
```   394   from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
```
```   395   {fix x assume H: "x \<sqsubset> z"
```
```   396     from less_trans[OF H zz1] less_trans[OF H zz2]
```
```   397     have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')"  using z1 zz1 z2 zz2 by auto
```
```   398   }
```
```   399   thus ?thesis by blast
```
```   400 qed
```
```   401
```
```   402 lemma minf_ex: assumes ex:"\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P1)" and p1: P1 shows "\<exists> x. P x"
```
```   403 proof-
```
```   404   from ex obtain z where z: "\<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
```
```   405   from lt_ex obtain x where x: "x \<sqsubset> z" by blast
```
```   406   from z x p1 show ?thesis by blast
```
```   407 qed
```
```   408
```
```   409 end
```
```   410
```
```   411
```
```   412 locale constr_dense_linear_order = linorder_no_lb + linorder_no_ub +
```
```   413   fixes between
```
```   414   assumes between_less: "less x y \<Longrightarrow> less x (between x y) \<and> less (between x y) y"
```
```   415      and  between_same: "between x x = x"
```
```   416
```
```   417 interpretation  constr_dense_linear_order < dense_linear_order
```
```   418   apply unfold_locales
```
```   419   using gt_ex lt_ex between_less
```
```   420     by (auto, rule_tac x="between x y" in exI, simp)
```
```   421
```
```   422 context  constr_dense_linear_order
```
```   423 begin
```
```   424
```
```   425 lemma rinf_U:
```
```   426   assumes fU: "finite U"
```
```   427   and lin_dense: "\<forall>x l u. (\<forall> t. l \<sqsubset> t \<and> t\<sqsubset> u \<longrightarrow> t \<notin> U) \<and> l\<sqsubset> x \<and> x \<sqsubset> u \<and> P x
```
```   428   \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
```
```   429   and nmpiU: "\<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u')"
```
```   430   and nmi: "\<not> MP"  and npi: "\<not> PP"  and ex: "\<exists> x.  P x"
```
```   431   shows "\<exists> u\<in> U. \<exists> u' \<in> U. P (between u u')"
```
```   432 proof-
```
```   433   from ex obtain x where px: "P x" by blast
```
```   434   from px nmi npi nmpiU have "\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u'" by auto
```
```   435   then obtain u and u' where uU:"u\<in> U" and uU': "u' \<in> U" and ux:"u \<sqsubseteq> x" and xu':"x \<sqsubseteq> u'" by auto
```
```   436   from uU have Une: "U \<noteq> {}" by auto
```
```   437   term "linorder.Min less_eq"
```
```   438   let ?l = "linorder.Min less_eq U"
```
```   439   let ?u = "linorder.Max less_eq U"
```
```   440   have linM: "?l \<in> U" using fU Une by simp
```
```   441   have uinM: "?u \<in> U" using fU Une by simp
```
```   442   have lM: "\<forall> t\<in> U. ?l \<sqsubseteq> t" using Une fU by auto
```
```   443   have Mu: "\<forall> t\<in> U. t \<sqsubseteq> ?u" using Une fU by auto
```
```   444   have th:"?l \<sqsubseteq> u" using uU Une lM by auto
```
```   445   from order_trans[OF th ux] have lx: "?l \<sqsubseteq> x" .
```
```   446   have th: "u' \<sqsubseteq> ?u" using uU' Une Mu by simp
```
```   447   from order_trans[OF xu' th] have xu: "x \<sqsubseteq> ?u" .
```
```   448   from finite_set_intervals2[where P="P",OF px lx xu linM uinM fU lM Mu]
```
```   449   have "(\<exists> s\<in> U. P s) \<or>
```
```   450       (\<exists> t1\<in> U. \<exists> t2 \<in> U. (\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U) \<and> t1 \<sqsubset> x \<and> x \<sqsubset> t2 \<and> P x)" .
```
```   451   moreover { fix u assume um: "u\<in>U" and pu: "P u"
```
```   452     have "between u u = u" by (simp add: between_same)
```
```   453     with um pu have "P (between u u)" by simp
```
```   454     with um have ?thesis by blast}
```
```   455   moreover{
```
```   456     assume "\<exists> t1\<in> U. \<exists> t2 \<in> U. (\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U) \<and> t1 \<sqsubset> x \<and> x \<sqsubset> t2 \<and> P x"
```
```   457       then obtain t1 and t2 where t1M: "t1 \<in> U" and t2M: "t2\<in> U"
```
```   458         and noM: "\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U" and t1x: "t1 \<sqsubset> x" and xt2: "x \<sqsubset> t2" and px: "P x"
```
```   459         by blast
```
```   460       from less_trans[OF t1x xt2] have t1t2: "t1 \<sqsubset> t2" .
```
```   461       let ?u = "between t1 t2"
```
```   462       from between_less t1t2 have t1lu: "t1 \<sqsubset> ?u" and ut2: "?u \<sqsubset> t2" by auto
```
```   463       from lin_dense noM t1x xt2 px t1lu ut2 have "P ?u" by blast
```
```   464       with t1M t2M have ?thesis by blast}
```
```   465     ultimately show ?thesis by blast
```
```   466   qed
```
```   467
```
```   468 theorem fr_eq:
```
```   469   assumes fU: "finite U"
```
```   470   and lin_dense: "\<forall>x l u. (\<forall> t. l \<sqsubset> t \<and> t\<sqsubset> u \<longrightarrow> t \<notin> U) \<and> l\<sqsubset> x \<and> x \<sqsubset> u \<and> P x
```
```   471    \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
```
```   472   and nmibnd: "\<forall>x. \<not> MP \<and> P x \<longrightarrow> (\<exists> u\<in> U. u \<sqsubseteq> x)"
```
```   473   and npibnd: "\<forall>x. \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. x \<sqsubseteq> u)"
```
```   474   and mi: "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x = MP)"  and pi: "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x = PP)"
```
```   475   shows "(\<exists> x. P x) \<equiv> (MP \<or> PP \<or> (\<exists> u \<in> U. \<exists> u'\<in> U. P (between u u')))"
```
```   476   (is "_ \<equiv> (_ \<or> _ \<or> ?F)" is "?E \<equiv> ?D")
```
```   477 proof-
```
```   478  {
```
```   479    assume px: "\<exists> x. P x"
```
```   480    have "MP \<or> PP \<or> (\<not> MP \<and> \<not> PP)" by blast
```
```   481    moreover {assume "MP \<or> PP" hence "?D" by blast}
```
```   482    moreover {assume nmi: "\<not> MP" and npi: "\<not> PP"
```
```   483      from npmibnd[OF nmibnd npibnd]
```
```   484      have nmpiU: "\<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u')" .
```
```   485      from rinf_U[OF fU lin_dense nmpiU nmi npi px] have "?D" by blast}
```
```   486    ultimately have "?D" by blast}
```
```   487  moreover
```
```   488  { assume "?D"
```
```   489    moreover {assume m:"MP" from minf_ex[OF mi m] have "?E" .}
```
```   490    moreover {assume p: "PP" from pinf_ex[OF pi p] have "?E" . }
```
```   491    moreover {assume f:"?F" hence "?E" by blast}
```
```   492    ultimately have "?E" by blast}
```
```   493  ultimately have "?E = ?D" by blast thus "?E \<equiv> ?D" by simp
```
```   494 qed
```
```   495
```
```   496 lemmas minf_thms = minf_conj minf_disj minf_eq minf_neq minf_lt minf_le minf_gt minf_ge minf_P
```
```   497 lemmas pinf_thms = pinf_conj pinf_disj pinf_eq pinf_neq pinf_lt pinf_le pinf_gt pinf_ge pinf_P
```
```   498
```
```   499 lemmas nmi_thms = nmi_conj nmi_disj nmi_eq nmi_neq nmi_lt nmi_le nmi_gt nmi_ge nmi_P
```
```   500 lemmas npi_thms = npi_conj npi_disj npi_eq npi_neq npi_lt npi_le npi_gt npi_ge npi_P
```
```   501 lemmas lin_dense_thms = lin_dense_conj lin_dense_disj lin_dense_eq lin_dense_neq lin_dense_lt lin_dense_le lin_dense_gt lin_dense_ge lin_dense_P
```
```   502
```
```   503 lemma ferrack_axiom: "constr_dense_linear_order less_eq less between"
```
```   504   by (rule constr_dense_linear_order_axioms)
```
```   505 lemma atoms:
```
```   506   includes meta_term_syntax
```
```   507   shows "TERM (less :: 'a \<Rightarrow> _)"
```
```   508     and "TERM (less_eq :: 'a \<Rightarrow> _)"
```
```   509     and "TERM (op = :: 'a \<Rightarrow> _)" .
```
```   510
```
```   511 declare ferrack_axiom [ferrack minf: minf_thms pinf: pinf_thms
```
```   512     nmi: nmi_thms npi: npi_thms lindense:
```
```   513     lin_dense_thms qe: fr_eq atoms: atoms]
```
```   514
```
```   515 declaration {*
```
```   516 let
```
```   517 fun simps phi = map (Morphism.thm phi) [@{thm "not_less"}, @{thm "not_le"}]
```
```   518 fun generic_whatis phi =
```
```   519  let
```
```   520   val [lt, le] = map (Morphism.term phi) [@{term "op \<sqsubset>"}, @{term "op \<sqsubseteq>"}]
```
```   521   fun h x t =
```
```   522    case term_of t of
```
```   523      Const("op =", _)\$y\$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
```
```   524                             else Ferrante_Rackoff_Data.Nox
```
```   525    | @{term "Not"}\$(Const("op =", _)\$y\$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
```
```   526                             else Ferrante_Rackoff_Data.Nox
```
```   527    | b\$y\$z => if Term.could_unify (b, lt) then
```
```   528                  if term_of x aconv y then Ferrante_Rackoff_Data.Lt
```
```   529                  else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
```
```   530                  else Ferrante_Rackoff_Data.Nox
```
```   531              else if Term.could_unify (b, le) then
```
```   532                  if term_of x aconv y then Ferrante_Rackoff_Data.Le
```
```   533                  else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
```
```   534                  else Ferrante_Rackoff_Data.Nox
```
```   535              else Ferrante_Rackoff_Data.Nox
```
```   536    | _ => Ferrante_Rackoff_Data.Nox
```
```   537  in h end
```
```   538  fun ss phi = HOL_ss addsimps (simps phi)
```
```   539 in
```
```   540  Ferrante_Rackoff_Data.funs  @{thm "ferrack_axiom"}
```
```   541   {isolate_conv = K (K (K Thm.reflexive)), whatis = generic_whatis, simpset = ss}
```
```   542 end
```
```   543 *}
```
```   544
```
```   545 end
```
```   546
```
```   547 use "~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML"
```
```   548
```
```   549 method_setup ferrack = {*
```
```   550   Method.ctxt_args (Method.SIMPLE_METHOD' o FerranteRackoff.dlo_tac)
```
```   551 *} "Ferrante and Rackoff's algorithm for quantifier elimination in dense linear orders"
```
```   552
```
```   553 subsection {* Ferrante and Rackoff algorithm over ordered fields *}
```
```   554
```
```   555 lemma neg_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x < 0) == (x > 0))"
```
```   556 proof-
```
```   557   assume H: "c < 0"
```
```   558   have "c*x < 0 = (0/c < x)" by (simp only: neg_divide_less_eq[OF H] ring_simps)
```
```   559   also have "\<dots> = (0 < x)" by simp
```
```   560   finally show  "(c*x < 0) == (x > 0)" by simp
```
```   561 qed
```
```   562
```
```   563 lemma pos_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x < 0) == (x < 0))"
```
```   564 proof-
```
```   565   assume H: "c > 0"
```
```   566   hence "c*x < 0 = (0/c > x)" by (simp only: pos_less_divide_eq[OF H] ring_simps)
```
```   567   also have "\<dots> = (0 > x)" by simp
```
```   568   finally show  "(c*x < 0) == (x < 0)" by simp
```
```   569 qed
```
```   570
```
```   571 lemma neg_prod_sum_lt: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t< 0) == (x > (- 1/c)*t))"
```
```   572 proof-
```
```   573   assume H: "c < 0"
```
```   574   have "c*x + t< 0 = (c*x < -t)" by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
```
```   575   also have "\<dots> = (-t/c < x)" by (simp only: neg_divide_less_eq[OF H] ring_simps)
```
```   576   also have "\<dots> = ((- 1/c)*t < x)" by simp
```
```   577   finally show  "(c*x + t < 0) == (x > (- 1/c)*t)" by simp
```
```   578 qed
```
```   579
```
```   580 lemma pos_prod_sum_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t < 0) == (x < (- 1/c)*t))"
```
```   581 proof-
```
```   582   assume H: "c > 0"
```
```   583   have "c*x + t< 0 = (c*x < -t)"  by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
```
```   584   also have "\<dots> = (-t/c > x)" by (simp only: pos_less_divide_eq[OF H] ring_simps)
```
```   585   also have "\<dots> = ((- 1/c)*t > x)" by simp
```
```   586   finally show  "(c*x + t < 0) == (x < (- 1/c)*t)" by simp
```
```   587 qed
```
```   588
```
```   589 lemma sum_lt:"((x::'a::pordered_ab_group_add) + t < 0) == (x < - t)"
```
```   590   using less_diff_eq[where a= x and b=t and c=0] by simp
```
```   591
```
```   592 lemma neg_prod_le:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x <= 0) == (x >= 0))"
```
```   593 proof-
```
```   594   assume H: "c < 0"
```
```   595   have "c*x <= 0 = (0/c <= x)" by (simp only: neg_divide_le_eq[OF H] ring_simps)
```
```   596   also have "\<dots> = (0 <= x)" by simp
```
```   597   finally show  "(c*x <= 0) == (x >= 0)" by simp
```
```   598 qed
```
```   599
```
```   600 lemma pos_prod_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x <= 0) == (x <= 0))"
```
```   601 proof-
```
```   602   assume H: "c > 0"
```
```   603   hence "c*x <= 0 = (0/c >= x)" by (simp only: pos_le_divide_eq[OF H] ring_simps)
```
```   604   also have "\<dots> = (0 >= x)" by simp
```
```   605   finally show  "(c*x <= 0) == (x <= 0)" by simp
```
```   606 qed
```
```   607
```
```   608 lemma neg_prod_sum_le: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t <= 0) == (x >= (- 1/c)*t))"
```
```   609 proof-
```
```   610   assume H: "c < 0"
```
```   611   have "c*x + t <= 0 = (c*x <= -t)"  by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
```
```   612   also have "\<dots> = (-t/c <= x)" by (simp only: neg_divide_le_eq[OF H] ring_simps)
```
```   613   also have "\<dots> = ((- 1/c)*t <= x)" by simp
```
```   614   finally show  "(c*x + t <= 0) == (x >= (- 1/c)*t)" by simp
```
```   615 qed
```
```   616
```
```   617 lemma pos_prod_sum_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t <= 0) == (x <= (- 1/c)*t))"
```
```   618 proof-
```
```   619   assume H: "c > 0"
```
```   620   have "c*x + t <= 0 = (c*x <= -t)" by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
```
```   621   also have "\<dots> = (-t/c >= x)" by (simp only: pos_le_divide_eq[OF H] ring_simps)
```
```   622   also have "\<dots> = ((- 1/c)*t >= x)" by simp
```
```   623   finally show  "(c*x + t <= 0) == (x <= (- 1/c)*t)" by simp
```
```   624 qed
```
```   625
```
```   626 lemma sum_le:"((x::'a::pordered_ab_group_add) + t <= 0) == (x <= - t)"
```
```   627   using le_diff_eq[where a= x and b=t and c=0] by simp
```
```   628
```
```   629 lemma nz_prod_eq:"(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x = 0) == (x = 0))" by simp
```
```   630 lemma nz_prod_sum_eq: "(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x + t = 0) == (x = (- 1/c)*t))"
```
```   631 proof-
```
```   632   assume H: "c \<noteq> 0"
```
```   633   have "c*x + t = 0 = (c*x = -t)" by (subst eq_iff_diff_eq_0 [of "c*x" "-t"], simp)
```
```   634   also have "\<dots> = (x = -t/c)" by (simp only: nonzero_eq_divide_eq[OF H] ring_simps)
```
```   635   finally show  "(c*x + t = 0) == (x = (- 1/c)*t)" by simp
```
```   636 qed
```
```   637 lemma sum_eq:"((x::'a::pordered_ab_group_add) + t = 0) == (x = - t)"
```
```   638   using eq_diff_eq[where a= x and b=t and c=0] by simp
```
```   639
```
```   640
```
```   641 interpretation class_ordered_field_dense_linear_order: constr_dense_linear_order
```
```   642  ["op <=" "op <"
```
```   643    "\<lambda> x y. 1/2 * ((x::'a::{ordered_field,recpower,number_ring}) + y)"]
```
```   644 proof (unfold_locales, dlo, dlo, auto)
```
```   645   fix x y::'a assume lt: "x < y"
```
```   646   from  less_half_sum[OF lt] show "x < (x + y) /2" by simp
```
```   647 next
```
```   648   fix x y::'a assume lt: "x < y"
```
```   649   from  gt_half_sum[OF lt] show "(x + y) /2 < y" by simp
```
```   650 qed
```
```   651
```
```   652 declaration{*
```
```   653 let
```
```   654 fun earlier [] x y = false
```
```   655         | earlier (h::t) x y =
```
```   656     if h aconvc y then false else if h aconvc x then true else earlier t x y;
```
```   657
```
```   658 fun dest_frac ct = case term_of ct of
```
```   659    Const (@{const_name "HOL.divide"},_) \$ a \$ b=>
```
```   660     Rat.rat_of_quotient (snd (HOLogic.dest_number a), snd (HOLogic.dest_number b))
```
```   661  | t => Rat.rat_of_int (snd (HOLogic.dest_number t))
```
```   662
```
```   663 fun mk_frac phi cT x =
```
```   664  let val (a, b) = Rat.quotient_of_rat x
```
```   665  in if b = 1 then Numeral.mk_cnumber cT a
```
```   666     else Thm.capply
```
```   667          (Thm.capply (Drule.cterm_rule (instantiate' [SOME cT] []) @{cpat "op /"})
```
```   668                      (Numeral.mk_cnumber cT a))
```
```   669          (Numeral.mk_cnumber cT b)
```
```   670  end
```
```   671
```
```   672 fun whatis x ct = case term_of ct of
```
```   673   Const(@{const_name "HOL.plus"}, _)\$(Const(@{const_name "HOL.times"},_)\$_\$y)\$_ =>
```
```   674      if y aconv term_of x then ("c*x+t",[(funpow 2 Thm.dest_arg1) ct, Thm.dest_arg ct])
```
```   675      else ("Nox",[])
```
```   676 | Const(@{const_name "HOL.plus"}, _)\$y\$_ =>
```
```   677      if y aconv term_of x then ("x+t",[Thm.dest_arg ct])
```
```   678      else ("Nox",[])
```
```   679 | Const(@{const_name "HOL.times"}, _)\$_\$y =>
```
```   680      if y aconv term_of x then ("c*x",[Thm.dest_arg1 ct])
```
```   681      else ("Nox",[])
```
```   682 | t => if t aconv term_of x then ("x",[]) else ("Nox",[]);
```
```   683
```
```   684 fun xnormalize_conv ctxt [] ct = reflexive ct
```
```   685 | xnormalize_conv ctxt (vs as (x::_)) ct =
```
```   686    case term_of ct of
```
```   687    Const(@{const_name HOL.less},_)\$_\$Const(@{const_name "HOL.zero"},_) =>
```
```   688     (case whatis x (Thm.dest_arg1 ct) of
```
```   689     ("c*x+t",[c,t]) =>
```
```   690        let
```
```   691         val cr = dest_frac c
```
```   692         val clt = Thm.dest_fun2 ct
```
```   693         val cz = Thm.dest_arg ct
```
```   694         val neg = cr </ Rat.zero
```
```   695         val cthp = Simplifier.rewrite (local_simpset_of ctxt)
```
```   696                (Thm.capply @{cterm "Trueprop"}
```
```   697                   (if neg then Thm.capply (Thm.capply clt c) cz
```
```   698                     else Thm.capply (Thm.capply clt cz) c))
```
```   699         val cth = equal_elim (symmetric cthp) TrueI
```
```   700         val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x,t])
```
```   701              (if neg then @{thm neg_prod_sum_lt} else @{thm pos_prod_sum_lt})) cth
```
```   702         val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
```
```   703                    (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
```
```   704       in rth end
```
```   705     | ("x+t",[t]) =>
```
```   706        let
```
```   707         val T = ctyp_of_term x
```
```   708         val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_lt"}
```
```   709         val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
```
```   710               (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
```
```   711        in  rth end
```
```   712     | ("c*x",[c]) =>
```
```   713        let
```
```   714         val cr = dest_frac c
```
```   715         val clt = Thm.dest_fun2 ct
```
```   716         val cz = Thm.dest_arg ct
```
```   717         val neg = cr </ Rat.zero
```
```   718         val cthp = Simplifier.rewrite (local_simpset_of ctxt)
```
```   719                (Thm.capply @{cterm "Trueprop"}
```
```   720                   (if neg then Thm.capply (Thm.capply clt c) cz
```
```   721                     else Thm.capply (Thm.capply clt cz) c))
```
```   722         val cth = equal_elim (symmetric cthp) TrueI
```
```   723         val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
```
```   724              (if neg then @{thm neg_prod_lt} else @{thm pos_prod_lt})) cth
```
```   725         val rth = th
```
```   726       in rth end
```
```   727     | _ => reflexive ct)
```
```   728
```
```   729
```
```   730 |  Const(@{const_name HOL.less_eq},_)\$_\$Const(@{const_name "HOL.zero"},_) =>
```
```   731    (case whatis x (Thm.dest_arg1 ct) of
```
```   732     ("c*x+t",[c,t]) =>
```
```   733        let
```
```   734         val T = ctyp_of_term x
```
```   735         val cr = dest_frac c
```
```   736         val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
```
```   737         val cz = Thm.dest_arg ct
```
```   738         val neg = cr </ Rat.zero
```
```   739         val cthp = Simplifier.rewrite (local_simpset_of ctxt)
```
```   740                (Thm.capply @{cterm "Trueprop"}
```
```   741                   (if neg then Thm.capply (Thm.capply clt c) cz
```
```   742                     else Thm.capply (Thm.capply clt cz) c))
```
```   743         val cth = equal_elim (symmetric cthp) TrueI
```
```   744         val th = implies_elim (instantiate' [SOME T] (map SOME [c,x,t])
```
```   745              (if neg then @{thm neg_prod_sum_le} else @{thm pos_prod_sum_le})) cth
```
```   746         val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
```
```   747                    (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
```
```   748       in rth end
```
```   749     | ("x+t",[t]) =>
```
```   750        let
```
```   751         val T = ctyp_of_term x
```
```   752         val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_le"}
```
```   753         val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
```
```   754               (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
```
```   755        in  rth end
```
```   756     | ("c*x",[c]) =>
```
```   757        let
```
```   758         val T = ctyp_of_term x
```
```   759         val cr = dest_frac c
```
```   760         val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
```
```   761         val cz = Thm.dest_arg ct
```
```   762         val neg = cr </ Rat.zero
```
```   763         val cthp = Simplifier.rewrite (local_simpset_of ctxt)
```
```   764                (Thm.capply @{cterm "Trueprop"}
```
```   765                   (if neg then Thm.capply (Thm.capply clt c) cz
```
```   766                     else Thm.capply (Thm.capply clt cz) c))
```
```   767         val cth = equal_elim (symmetric cthp) TrueI
```
```   768         val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
```
```   769              (if neg then @{thm neg_prod_le} else @{thm pos_prod_le})) cth
```
```   770         val rth = th
```
```   771       in rth end
```
```   772     | _ => reflexive ct)
```
```   773
```
```   774 |  Const("op =",_)\$_\$Const(@{const_name "HOL.zero"},_) =>
```
```   775    (case whatis x (Thm.dest_arg1 ct) of
```
```   776     ("c*x+t",[c,t]) =>
```
```   777        let
```
```   778         val T = ctyp_of_term x
```
```   779         val cr = dest_frac c
```
```   780         val ceq = Thm.dest_fun2 ct
```
```   781         val cz = Thm.dest_arg ct
```
```   782         val cthp = Simplifier.rewrite (local_simpset_of ctxt)
```
```   783             (Thm.capply @{cterm "Trueprop"}
```
```   784              (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
```
```   785         val cth = equal_elim (symmetric cthp) TrueI
```
```   786         val th = implies_elim
```
```   787                  (instantiate' [SOME T] (map SOME [c,x,t]) @{thm nz_prod_sum_eq}) cth
```
```   788         val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
```
```   789                    (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
```
```   790       in rth end
```
```   791     | ("x+t",[t]) =>
```
```   792        let
```
```   793         val T = ctyp_of_term x
```
```   794         val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_eq"}
```
```   795         val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
```
```   796               (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
```
```   797        in  rth end
```
```   798     | ("c*x",[c]) =>
```
```   799        let
```
```   800         val T = ctyp_of_term x
```
```   801         val cr = dest_frac c
```
```   802         val ceq = Thm.dest_fun2 ct
```
```   803         val cz = Thm.dest_arg ct
```
```   804         val cthp = Simplifier.rewrite (local_simpset_of ctxt)
```
```   805             (Thm.capply @{cterm "Trueprop"}
```
```   806              (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
```
```   807         val cth = equal_elim (symmetric cthp) TrueI
```
```   808         val rth = implies_elim
```
```   809                  (instantiate' [SOME T] (map SOME [c,x]) @{thm nz_prod_eq}) cth
```
```   810       in rth end
```
```   811     | _ => reflexive ct);
```
```   812
```
```   813 local
```
```   814   val less_iff_diff_less_0 = mk_meta_eq @{thm "less_iff_diff_less_0"}
```
```   815   val le_iff_diff_le_0 = mk_meta_eq @{thm "le_iff_diff_le_0"}
```
```   816   val eq_iff_diff_eq_0 = mk_meta_eq @{thm "eq_iff_diff_eq_0"}
```
```   817 in
```
```   818 fun field_isolate_conv phi ctxt vs ct = case term_of ct of
```
```   819   Const(@{const_name HOL.less},_)\$a\$b =>
```
```   820    let val (ca,cb) = Thm.dest_binop ct
```
```   821        val T = ctyp_of_term ca
```
```   822        val th = instantiate' [SOME T] [SOME ca, SOME cb] less_iff_diff_less_0
```
```   823        val nth = Conv.fconv_rule
```
```   824          (Conv.arg_conv (Conv.arg1_conv
```
```   825               (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
```
```   826        val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
```
```   827    in rth end
```
```   828 | Const(@{const_name HOL.less_eq},_)\$a\$b =>
```
```   829    let val (ca,cb) = Thm.dest_binop ct
```
```   830        val T = ctyp_of_term ca
```
```   831        val th = instantiate' [SOME T] [SOME ca, SOME cb] le_iff_diff_le_0
```
```   832        val nth = Conv.fconv_rule
```
```   833          (Conv.arg_conv (Conv.arg1_conv
```
```   834               (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
```
```   835        val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
```
```   836    in rth end
```
```   837
```
```   838 | Const("op =",_)\$a\$b =>
```
```   839    let val (ca,cb) = Thm.dest_binop ct
```
```   840        val T = ctyp_of_term ca
```
```   841        val th = instantiate' [SOME T] [SOME ca, SOME cb] eq_iff_diff_eq_0
```
```   842        val nth = Conv.fconv_rule
```
```   843          (Conv.arg_conv (Conv.arg1_conv
```
```   844               (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
```
```   845        val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
```
```   846    in rth end
```
```   847 | @{term "Not"} \$(Const("op =",_)\$a\$b) => Conv.arg_conv (field_isolate_conv phi ctxt vs) ct
```
```   848 | _ => reflexive ct
```
```   849 end;
```
```   850
```
```   851 fun classfield_whatis phi =
```
```   852  let
```
```   853   fun h x t =
```
```   854    case term_of t of
```
```   855      Const("op =", _)\$y\$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
```
```   856                             else Ferrante_Rackoff_Data.Nox
```
```   857    | @{term "Not"}\$(Const("op =", _)\$y\$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
```
```   858                             else Ferrante_Rackoff_Data.Nox
```
```   859    | Const(@{const_name HOL.less},_)\$y\$z =>
```
```   860        if term_of x aconv y then Ferrante_Rackoff_Data.Lt
```
```   861         else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
```
```   862         else Ferrante_Rackoff_Data.Nox
```
```   863    | Const (@{const_name HOL.less_eq},_)\$y\$z =>
```
```   864          if term_of x aconv y then Ferrante_Rackoff_Data.Le
```
```   865          else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
```
```   866          else Ferrante_Rackoff_Data.Nox
```
```   867    | _ => Ferrante_Rackoff_Data.Nox
```
```   868  in h end;
```
```   869 fun class_field_ss phi =
```
```   870    HOL_basic_ss addsimps ([@{thm "linorder_not_less"}, @{thm "linorder_not_le"}])
```
```   871    addsplits [@{thm "abs_split"},@{thm "split_max"}, @{thm "split_min"}]
```
```   872
```
```   873 in
```
```   874 Ferrante_Rackoff_Data.funs @{thm "class_ordered_field_dense_linear_order.ferrack_axiom"}
```
```   875   {isolate_conv = field_isolate_conv, whatis = classfield_whatis, simpset = class_field_ss}
```
```   876 end
```
```   877 *}
```
```   878
```
```   879
```
```   880 end
```