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