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