src/HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy
author haftmann
Sun Nov 11 19:24:01 2012 +0100 (2012-11-11)
changeset 50045 2214bc566f88
parent 49962 a8cc904a6820
child 50282 fe4d4bb9f4c2
permissions -rw-r--r--
modernized, simplified and compacted oracle and proof method glue code;
corrected slips with poly.Pw and Orderings.less(_eq)
chaieb@33152
     1
(*  Title:      HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy
chaieb@33152
     2
    Author:     Amine Chaieb
chaieb@33152
     3
*)
chaieb@33152
     4
chaieb@33152
     5
header{* A formalization of Ferrante and Rackoff's procedure with polynomial parameters, see Paper in CALCULEMUS 2008 *}
chaieb@33152
     6
chaieb@33152
     7
theory Parametric_Ferrante_Rackoff
nipkow@41849
     8
imports Reflected_Multivariate_Polynomial Dense_Linear_Order DP_Library
krauss@44013
     9
  "~~/src/HOL/Library/Efficient_Nat" "~~/src/HOL/Library/Old_Recdef"
chaieb@33152
    10
begin
chaieb@33152
    11
chaieb@33152
    12
subsection {* Terms *}
chaieb@33152
    13
chaieb@33152
    14
datatype tm = CP poly | Bound nat | Add tm tm | Mul poly tm 
chaieb@33152
    15
  | Neg tm | Sub tm tm | CNP nat poly tm
chaieb@33152
    16
  (* A size for poly to make inductive proofs simpler*)
chaieb@33152
    17
haftmann@39246
    18
primrec tmsize :: "tm \<Rightarrow> nat" where
chaieb@33152
    19
  "tmsize (CP c) = polysize c"
haftmann@39246
    20
| "tmsize (Bound n) = 1"
haftmann@39246
    21
| "tmsize (Neg a) = 1 + tmsize a"
haftmann@39246
    22
| "tmsize (Add a b) = 1 + tmsize a + tmsize b"
haftmann@39246
    23
| "tmsize (Sub a b) = 3 + tmsize a + tmsize b"
haftmann@39246
    24
| "tmsize (Mul c a) = 1 + polysize c + tmsize a"
haftmann@39246
    25
| "tmsize (CNP n c a) = 3 + polysize c + tmsize a "
chaieb@33152
    26
chaieb@33152
    27
  (* Semantics of terms tm *)
huffman@47108
    28
primrec Itm :: "'a::{field_char_0, field_inverse_zero} list \<Rightarrow> 'a list \<Rightarrow> tm \<Rightarrow> 'a" where
chaieb@33152
    29
  "Itm vs bs (CP c) = (Ipoly vs c)"
haftmann@39246
    30
| "Itm vs bs (Bound n) = bs!n"
haftmann@39246
    31
| "Itm vs bs (Neg a) = -(Itm vs bs a)"
haftmann@39246
    32
| "Itm vs bs (Add a b) = Itm vs bs a + Itm vs bs b"
haftmann@39246
    33
| "Itm vs bs (Sub a b) = Itm vs bs a - Itm vs bs b"
haftmann@39246
    34
| "Itm vs bs (Mul c a) = (Ipoly vs c) * Itm vs bs a"
haftmann@39246
    35
| "Itm vs bs (CNP n c t) = (Ipoly vs c)*(bs!n) + Itm vs bs t"   
chaieb@33152
    36
chaieb@33152
    37
chaieb@33152
    38
fun allpolys:: "(poly \<Rightarrow> bool) \<Rightarrow> tm \<Rightarrow> bool"  where
chaieb@33152
    39
  "allpolys P (CP c) = P c"
chaieb@33152
    40
| "allpolys P (CNP n c p) = (P c \<and> allpolys P p)"
chaieb@33152
    41
| "allpolys P (Mul c p) = (P c \<and> allpolys P p)"
chaieb@33152
    42
| "allpolys P (Neg p) = allpolys P p"
chaieb@33152
    43
| "allpolys P (Add p q) = (allpolys P p \<and> allpolys P q)"
chaieb@33152
    44
| "allpolys P (Sub p q) = (allpolys P p \<and> allpolys P q)"
chaieb@33152
    45
| "allpolys P p = True"
chaieb@33152
    46
haftmann@39246
    47
primrec tmboundslt:: "nat \<Rightarrow> tm \<Rightarrow> bool" where
chaieb@33152
    48
  "tmboundslt n (CP c) = True"
haftmann@39246
    49
| "tmboundslt n (Bound m) = (m < n)"
haftmann@39246
    50
| "tmboundslt n (CNP m c a) = (m < n \<and> tmboundslt n a)"
haftmann@39246
    51
| "tmboundslt n (Neg a) = tmboundslt n a"
haftmann@39246
    52
| "tmboundslt n (Add a b) = (tmboundslt n a \<and> tmboundslt n b)"
haftmann@39246
    53
| "tmboundslt n (Sub a b) = (tmboundslt n a \<and> tmboundslt n b)" 
haftmann@39246
    54
| "tmboundslt n (Mul i a) = tmboundslt n a"
haftmann@39246
    55
haftmann@39246
    56
primrec tmbound0:: "tm \<Rightarrow> bool" (* a tm is INDEPENDENT of Bound 0 *) where
chaieb@33152
    57
  "tmbound0 (CP c) = True"
haftmann@39246
    58
| "tmbound0 (Bound n) = (n>0)"
haftmann@39246
    59
| "tmbound0 (CNP n c a) = (n\<noteq>0 \<and> tmbound0 a)"
haftmann@39246
    60
| "tmbound0 (Neg a) = tmbound0 a"
haftmann@39246
    61
| "tmbound0 (Add a b) = (tmbound0 a \<and> tmbound0 b)"
haftmann@39246
    62
| "tmbound0 (Sub a b) = (tmbound0 a \<and> tmbound0 b)" 
haftmann@39246
    63
| "tmbound0 (Mul i a) = tmbound0 a"
chaieb@33152
    64
lemma tmbound0_I:
chaieb@33152
    65
  assumes nb: "tmbound0 a"
chaieb@33152
    66
  shows "Itm vs (b#bs) a = Itm vs (b'#bs) a"
chaieb@33152
    67
using nb
nipkow@41842
    68
by (induct a rule: tm.induct,auto)
chaieb@33152
    69
haftmann@39246
    70
primrec tmbound:: "nat \<Rightarrow> tm \<Rightarrow> bool" (* a tm is INDEPENDENT of Bound n *) where
chaieb@33152
    71
  "tmbound n (CP c) = True"
haftmann@39246
    72
| "tmbound n (Bound m) = (n \<noteq> m)"
haftmann@39246
    73
| "tmbound n (CNP m c a) = (n\<noteq>m \<and> tmbound n a)"
haftmann@39246
    74
| "tmbound n (Neg a) = tmbound n a"
haftmann@39246
    75
| "tmbound n (Add a b) = (tmbound n a \<and> tmbound n b)"
haftmann@39246
    76
| "tmbound n (Sub a b) = (tmbound n a \<and> tmbound n b)" 
haftmann@39246
    77
| "tmbound n (Mul i a) = tmbound n a"
chaieb@33152
    78
lemma tmbound0_tmbound_iff: "tmbound 0 t = tmbound0 t" by (induct t, auto)
chaieb@33152
    79
chaieb@33152
    80
lemma tmbound_I: 
chaieb@33152
    81
  assumes bnd: "tmboundslt (length bs) t" and nb: "tmbound n t" and le: "n \<le> length bs"
chaieb@33152
    82
  shows "Itm vs (bs[n:=x]) t = Itm vs bs t"
chaieb@33152
    83
  using nb le bnd
haftmann@39246
    84
  by (induct t rule: tm.induct , auto)
haftmann@39246
    85
krauss@41821
    86
fun decrtm0:: "tm \<Rightarrow> tm" where
chaieb@33152
    87
  "decrtm0 (Bound n) = Bound (n - 1)"
krauss@41821
    88
| "decrtm0 (Neg a) = Neg (decrtm0 a)"
krauss@41821
    89
| "decrtm0 (Add a b) = Add (decrtm0 a) (decrtm0 b)"
krauss@41821
    90
| "decrtm0 (Sub a b) = Sub (decrtm0 a) (decrtm0 b)"
krauss@41821
    91
| "decrtm0 (Mul c a) = Mul c (decrtm0 a)"
krauss@41821
    92
| "decrtm0 (CNP n c a) = CNP (n - 1) c (decrtm0 a)"
krauss@41821
    93
| "decrtm0 a = a"
haftmann@39246
    94
krauss@41821
    95
fun incrtm0:: "tm \<Rightarrow> tm" where
chaieb@33152
    96
  "incrtm0 (Bound n) = Bound (n + 1)"
krauss@41821
    97
| "incrtm0 (Neg a) = Neg (incrtm0 a)"
krauss@41821
    98
| "incrtm0 (Add a b) = Add (incrtm0 a) (incrtm0 b)"
krauss@41821
    99
| "incrtm0 (Sub a b) = Sub (incrtm0 a) (incrtm0 b)"
krauss@41821
   100
| "incrtm0 (Mul c a) = Mul c (incrtm0 a)"
krauss@41821
   101
| "incrtm0 (CNP n c a) = CNP (n + 1) c (incrtm0 a)"
krauss@41821
   102
| "incrtm0 a = a"
haftmann@39246
   103
chaieb@33152
   104
lemma decrtm0: assumes nb: "tmbound0 t"
chaieb@33152
   105
  shows "Itm vs (x#bs) t = Itm vs bs (decrtm0 t)"
nipkow@41842
   106
  using nb by (induct t rule: decrtm0.induct, simp_all)
haftmann@39246
   107
chaieb@33152
   108
lemma incrtm0: "Itm vs (x#bs) (incrtm0 t) = Itm vs bs t"
nipkow@41842
   109
  by (induct t rule: decrtm0.induct, simp_all)
chaieb@33152
   110
haftmann@39246
   111
primrec decrtm:: "nat \<Rightarrow> tm \<Rightarrow> tm" where
chaieb@33152
   112
  "decrtm m (CP c) = (CP c)"
haftmann@39246
   113
| "decrtm m (Bound n) = (if n < m then Bound n else Bound (n - 1))"
haftmann@39246
   114
| "decrtm m (Neg a) = Neg (decrtm m a)"
haftmann@39246
   115
| "decrtm m (Add a b) = Add (decrtm m a) (decrtm m b)"
haftmann@39246
   116
| "decrtm m (Sub a b) = Sub (decrtm m a) (decrtm m b)"
haftmann@39246
   117
| "decrtm m (Mul c a) = Mul c (decrtm m a)"
haftmann@39246
   118
| "decrtm m (CNP n c a) = (if n < m then CNP n c (decrtm m a) else CNP (n - 1) c (decrtm m a))"
chaieb@33152
   119
haftmann@39246
   120
primrec removen:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
chaieb@33152
   121
  "removen n [] = []"
haftmann@39246
   122
| "removen n (x#xs) = (if n=0 then xs else (x#(removen (n - 1) xs)))"
chaieb@33152
   123
chaieb@33152
   124
lemma removen_same: "n \<ge> length xs \<Longrightarrow> removen n xs = xs"
chaieb@33152
   125
  by (induct xs arbitrary: n, auto)
chaieb@33152
   126
chaieb@33152
   127
lemma nth_length_exceeds: "n \<ge> length xs \<Longrightarrow> xs!n = []!(n - length xs)"
chaieb@33152
   128
  by (induct xs arbitrary: n, auto)
chaieb@33152
   129
chaieb@33152
   130
lemma removen_length: "length (removen n xs) = (if n \<ge> length xs then length xs else length xs - 1)"
chaieb@33152
   131
  by (induct xs arbitrary: n, auto)
chaieb@33152
   132
lemma removen_nth: "(removen n xs)!m = (if n \<ge> length xs then xs!m 
chaieb@33152
   133
  else if m < n then xs!m else if m \<le> length xs then xs!(Suc m) else []!(m - (length xs - 1)))"
chaieb@33152
   134
proof(induct xs arbitrary: n m)
chaieb@33152
   135
  case Nil thus ?case by simp
chaieb@33152
   136
next
chaieb@33152
   137
  case (Cons x xs n m)
chaieb@33152
   138
  {assume nxs: "n \<ge> length (x#xs)" hence ?case using removen_same[OF nxs] by simp}
chaieb@33152
   139
  moreover
chaieb@33152
   140
  {assume nxs: "\<not> (n \<ge> length (x#xs))" 
wenzelm@41807
   141
    {assume mln: "m < n" hence ?case using Cons by (cases m, auto)}
chaieb@33152
   142
    moreover
chaieb@33152
   143
    {assume mln: "\<not> (m < n)" 
wenzelm@41807
   144
      {assume mxs: "m \<le> length (x#xs)" hence ?case using Cons by (cases m, auto)}
chaieb@33152
   145
      moreover
chaieb@33152
   146
      {assume mxs: "\<not> (m \<le> length (x#xs))" 
wenzelm@33268
   147
        have th: "length (removen n (x#xs)) = length xs" 
wenzelm@33268
   148
          using removen_length[where n="n" and xs="x#xs"] nxs by simp
wenzelm@33268
   149
        with mxs have mxs':"m \<ge> length (removen n (x#xs))" by auto
wenzelm@33268
   150
        hence "(removen n (x#xs))!m = [] ! (m - length xs)" 
wenzelm@33268
   151
          using th nth_length_exceeds[OF mxs'] by auto
wenzelm@33268
   152
        hence th: "(removen n (x#xs))!m = [] ! (m - (length (x#xs) - 1))" 
wenzelm@33268
   153
          by auto
wenzelm@33268
   154
        hence ?case using nxs mln mxs by auto }
chaieb@33152
   155
      ultimately have ?case by blast
chaieb@33152
   156
    }
chaieb@33152
   157
    ultimately have ?case by blast
wenzelm@41807
   158
  } ultimately show ?case by blast
chaieb@33152
   159
qed
chaieb@33152
   160
chaieb@33152
   161
lemma decrtm: assumes bnd: "tmboundslt (length bs) t" and nb: "tmbound m t" 
chaieb@33152
   162
  and nle: "m \<le> length bs" 
chaieb@33152
   163
  shows "Itm vs (removen m bs) (decrtm m t) = Itm vs bs t"
wenzelm@41807
   164
  using bnd nb nle by (induct t rule: tm.induct) (auto simp add: removen_nth)
chaieb@33152
   165
haftmann@39246
   166
primrec tmsubst0:: "tm \<Rightarrow> tm \<Rightarrow> tm" where
chaieb@33152
   167
  "tmsubst0 t (CP c) = CP c"
haftmann@39246
   168
| "tmsubst0 t (Bound n) = (if n=0 then t else Bound n)"
haftmann@39246
   169
| "tmsubst0 t (CNP n c a) = (if n=0 then Add (Mul c t) (tmsubst0 t a) else CNP n c (tmsubst0 t a))"
haftmann@39246
   170
| "tmsubst0 t (Neg a) = Neg (tmsubst0 t a)"
haftmann@39246
   171
| "tmsubst0 t (Add a b) = Add (tmsubst0 t a) (tmsubst0 t b)"
haftmann@39246
   172
| "tmsubst0 t (Sub a b) = Sub (tmsubst0 t a) (tmsubst0 t b)" 
haftmann@39246
   173
| "tmsubst0 t (Mul i a) = Mul i (tmsubst0 t a)"
chaieb@33152
   174
lemma tmsubst0:
chaieb@33152
   175
  shows "Itm vs (x#bs) (tmsubst0 t a) = Itm vs ((Itm vs (x#bs) t)#bs) a"
nipkow@41842
   176
  by (induct a rule: tm.induct) auto
chaieb@33152
   177
chaieb@33152
   178
lemma tmsubst0_nb: "tmbound0 t \<Longrightarrow> tmbound0 (tmsubst0 t a)"
nipkow@41842
   179
  by (induct a rule: tm.induct) auto
chaieb@33152
   180
haftmann@39246
   181
primrec tmsubst:: "nat \<Rightarrow> tm \<Rightarrow> tm \<Rightarrow> tm" where
chaieb@33152
   182
  "tmsubst n t (CP c) = CP c"
haftmann@39246
   183
| "tmsubst n t (Bound m) = (if n=m then t else Bound m)"
haftmann@39246
   184
| "tmsubst n t (CNP m c a) = (if n=m then Add (Mul c t) (tmsubst n t a) 
chaieb@33152
   185
             else CNP m c (tmsubst n t a))"
haftmann@39246
   186
| "tmsubst n t (Neg a) = Neg (tmsubst n t a)"
haftmann@39246
   187
| "tmsubst n t (Add a b) = Add (tmsubst n t a) (tmsubst n t b)"
haftmann@39246
   188
| "tmsubst n t (Sub a b) = Sub (tmsubst n t a) (tmsubst n t b)" 
haftmann@39246
   189
| "tmsubst n t (Mul i a) = Mul i (tmsubst n t a)"
chaieb@33152
   190
chaieb@33152
   191
lemma tmsubst: assumes nb: "tmboundslt (length bs) a" and nlt: "n \<le> length bs"
chaieb@33152
   192
  shows "Itm vs bs (tmsubst n t a) = Itm vs (bs[n:= Itm vs bs t]) a"
chaieb@33152
   193
using nb nlt
nipkow@41842
   194
by (induct a rule: tm.induct,auto)
chaieb@33152
   195
chaieb@33152
   196
lemma tmsubst_nb0: assumes tnb: "tmbound0 t"
chaieb@33152
   197
shows "tmbound0 (tmsubst 0 t a)"
chaieb@33152
   198
using tnb
haftmann@39246
   199
by (induct a rule: tm.induct, auto)
chaieb@33152
   200
chaieb@33152
   201
lemma tmsubst_nb: assumes tnb: "tmbound m t"
chaieb@33152
   202
shows "tmbound m (tmsubst m t a)"
chaieb@33152
   203
using tnb
haftmann@39246
   204
by (induct a rule: tm.induct, auto)
chaieb@33152
   205
lemma incrtm0_tmbound: "tmbound n t \<Longrightarrow> tmbound (Suc n) (incrtm0 t)"
chaieb@33152
   206
  by (induct t, auto)
chaieb@33152
   207
  (* Simplification *)
chaieb@33152
   208
chaieb@33152
   209
consts
chaieb@33152
   210
  tmadd:: "tm \<times> tm \<Rightarrow> tm"
chaieb@33152
   211
recdef tmadd "measure (\<lambda> (t,s). size t + size s)"
chaieb@33152
   212
  "tmadd (CNP n1 c1 r1,CNP n2 c2 r2) =
chaieb@33152
   213
  (if n1=n2 then 
chaieb@33152
   214
  (let c = c1 +\<^sub>p c2
chaieb@33152
   215
  in if c = 0\<^sub>p then tmadd(r1,r2) else CNP n1 c (tmadd (r1,r2)))
chaieb@33152
   216
  else if n1 \<le> n2 then (CNP n1 c1 (tmadd (r1,CNP n2 c2 r2))) 
chaieb@33152
   217
  else (CNP n2 c2 (tmadd (CNP n1 c1 r1,r2))))"
chaieb@33152
   218
  "tmadd (CNP n1 c1 r1,t) = CNP n1 c1 (tmadd (r1, t))"  
chaieb@33152
   219
  "tmadd (t,CNP n2 c2 r2) = CNP n2 c2 (tmadd (t,r2))" 
chaieb@33152
   220
  "tmadd (CP b1, CP b2) = CP (b1 +\<^sub>p b2)"
chaieb@33152
   221
  "tmadd (a,b) = Add a b"
chaieb@33152
   222
chaieb@33152
   223
lemma tmadd[simp]: "Itm vs bs (tmadd (t,s)) = Itm vs bs (Add t s)"
chaieb@33152
   224
apply (induct t s rule: tmadd.induct, simp_all add: Let_def)
chaieb@33152
   225
apply (case_tac "c1 +\<^sub>p c2 = 0\<^sub>p",case_tac "n1 \<le> n2", simp_all)
haftmann@36348
   226
apply (case_tac "n1 = n2", simp_all add: field_simps)
webertj@49962
   227
apply (simp only: distrib_left[symmetric]) 
chaieb@33152
   228
by (auto simp del: polyadd simp add: polyadd[symmetric])
chaieb@33152
   229
chaieb@33152
   230
lemma tmadd_nb0[simp]: "\<lbrakk> tmbound0 t ; tmbound0 s\<rbrakk> \<Longrightarrow> tmbound0 (tmadd (t,s))"
chaieb@33152
   231
by (induct t s rule: tmadd.induct, auto simp add: Let_def)
chaieb@33152
   232
chaieb@33152
   233
lemma tmadd_nb[simp]: "\<lbrakk> tmbound n t ; tmbound n s\<rbrakk> \<Longrightarrow> tmbound n (tmadd (t,s))"
chaieb@33152
   234
by (induct t s rule: tmadd.induct, auto simp add: Let_def)
chaieb@33152
   235
lemma tmadd_blt[simp]: "\<lbrakk>tmboundslt n t ; tmboundslt n s\<rbrakk> \<Longrightarrow> tmboundslt n (tmadd (t,s))"
chaieb@33152
   236
by (induct t s rule: tmadd.induct, auto simp add: Let_def)
chaieb@33152
   237
chaieb@33152
   238
lemma tmadd_allpolys_npoly[simp]: "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly s \<Longrightarrow> allpolys isnpoly (tmadd(t,s))" by (induct t s rule: tmadd.induct, simp_all add: Let_def polyadd_norm)
chaieb@33152
   239
krauss@41821
   240
fun tmmul:: "tm \<Rightarrow> poly \<Rightarrow> tm" where
chaieb@33152
   241
  "tmmul (CP j) = (\<lambda> i. CP (i *\<^sub>p j))"
krauss@41821
   242
| "tmmul (CNP n c a) = (\<lambda> i. CNP n (i *\<^sub>p c) (tmmul a i))"
krauss@41821
   243
| "tmmul t = (\<lambda> i. Mul i t)"
chaieb@33152
   244
chaieb@33152
   245
lemma tmmul[simp]: "Itm vs bs (tmmul t i) = Itm vs bs (Mul i t)"
haftmann@36348
   246
by (induct t arbitrary: i rule: tmmul.induct, simp_all add: field_simps)
chaieb@33152
   247
chaieb@33152
   248
lemma tmmul_nb0[simp]: "tmbound0 t \<Longrightarrow> tmbound0 (tmmul t i)"
chaieb@33152
   249
by (induct t arbitrary: i rule: tmmul.induct, auto )
chaieb@33152
   250
chaieb@33152
   251
lemma tmmul_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (tmmul t i)"
chaieb@33152
   252
by (induct t arbitrary: n rule: tmmul.induct, auto )
chaieb@33152
   253
lemma tmmul_blt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (tmmul t i)"
chaieb@33152
   254
by (induct t arbitrary: i rule: tmmul.induct, auto simp add: Let_def)
chaieb@33152
   255
chaieb@33152
   256
lemma tmmul_allpolys_npoly[simp]: 
haftmann@36409
   257
  assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   258
  shows "allpolys isnpoly t \<Longrightarrow> isnpoly c \<Longrightarrow> allpolys isnpoly (tmmul t c)" by (induct t rule: tmmul.induct, simp_all add: Let_def polymul_norm)
chaieb@33152
   259
haftmann@35416
   260
definition tmneg :: "tm \<Rightarrow> tm" where
chaieb@33152
   261
  "tmneg t \<equiv> tmmul t (C (- 1,1))"
chaieb@33152
   262
haftmann@35416
   263
definition tmsub :: "tm \<Rightarrow> tm \<Rightarrow> tm" where
chaieb@33152
   264
  "tmsub s t \<equiv> (if s = t then CP 0\<^sub>p else tmadd (s,tmneg t))"
chaieb@33152
   265
chaieb@33152
   266
lemma tmneg[simp]: "Itm vs bs (tmneg t) = Itm vs bs (Neg t)"
chaieb@33152
   267
using tmneg_def[of t] 
chaieb@33152
   268
apply simp
chaieb@33152
   269
done
chaieb@33152
   270
chaieb@33152
   271
lemma tmneg_nb0[simp]: "tmbound0 t \<Longrightarrow> tmbound0 (tmneg t)"
chaieb@33152
   272
using tmneg_def by simp
chaieb@33152
   273
chaieb@33152
   274
lemma tmneg_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (tmneg t)"
chaieb@33152
   275
using tmneg_def by simp
chaieb@33152
   276
lemma tmneg_blt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (tmneg t)"
chaieb@33152
   277
using tmneg_def by simp
chaieb@33152
   278
lemma [simp]: "isnpoly (C (-1,1))" unfolding isnpoly_def by simp
chaieb@33152
   279
lemma tmneg_allpolys_npoly[simp]: 
haftmann@36409
   280
  assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   281
  shows "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly (tmneg t)" 
chaieb@33152
   282
  unfolding tmneg_def by auto
chaieb@33152
   283
chaieb@33152
   284
lemma tmsub[simp]: "Itm vs bs (tmsub a b) = Itm vs bs (Sub a b)"
chaieb@33152
   285
using tmsub_def by simp
chaieb@33152
   286
chaieb@33152
   287
lemma tmsub_nb0[simp]: "\<lbrakk> tmbound0 t ; tmbound0 s\<rbrakk> \<Longrightarrow> tmbound0 (tmsub t s)"
chaieb@33152
   288
using tmsub_def by simp
chaieb@33152
   289
lemma tmsub_nb[simp]: "\<lbrakk> tmbound n t ; tmbound n s\<rbrakk> \<Longrightarrow> tmbound n (tmsub t s)"
chaieb@33152
   290
using tmsub_def by simp
chaieb@33152
   291
lemma tmsub_blt[simp]: "\<lbrakk>tmboundslt n t ; tmboundslt n s\<rbrakk> \<Longrightarrow> tmboundslt n (tmsub t s )"
chaieb@33152
   292
using tmsub_def by simp
chaieb@33152
   293
lemma tmsub_allpolys_npoly[simp]: 
haftmann@36409
   294
  assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   295
  shows "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly s \<Longrightarrow> allpolys isnpoly (tmsub t s)" 
chaieb@33152
   296
  unfolding tmsub_def by (simp add: isnpoly_def)
chaieb@33152
   297
krauss@41821
   298
fun simptm:: "tm \<Rightarrow> tm" where
chaieb@33152
   299
  "simptm (CP j) = CP (polynate j)"
krauss@41821
   300
| "simptm (Bound n) = CNP n 1\<^sub>p (CP 0\<^sub>p)"
krauss@41821
   301
| "simptm (Neg t) = tmneg (simptm t)"
krauss@41821
   302
| "simptm (Add t s) = tmadd (simptm t,simptm s)"
krauss@41821
   303
| "simptm (Sub t s) = tmsub (simptm t) (simptm s)"
krauss@41821
   304
| "simptm (Mul i t) = (let i' = polynate i in if i' = 0\<^sub>p then CP 0\<^sub>p else tmmul (simptm t) i')"
krauss@41821
   305
| "simptm (CNP n c t) = (let c' = polynate c in if c' = 0\<^sub>p then simptm t else tmadd (CNP n c' (CP 0\<^sub>p ), simptm t))"
chaieb@33152
   306
chaieb@33152
   307
lemma polynate_stupid: 
haftmann@36409
   308
  assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
huffman@45499
   309
  shows "polynate t = 0\<^sub>p \<Longrightarrow> Ipoly bs t = (0::'a)"
chaieb@33152
   310
apply (subst polynate[symmetric])
chaieb@33152
   311
apply simp
chaieb@33152
   312
done
chaieb@33152
   313
chaieb@33152
   314
lemma simptm_ci[simp]: "Itm vs bs (simptm t) = Itm vs bs t"
chaieb@33152
   315
by (induct t rule: simptm.induct, auto simp add: tmneg tmadd tmsub tmmul Let_def polynate_stupid) 
chaieb@33152
   316
chaieb@33152
   317
lemma simptm_tmbound0[simp]: 
chaieb@33152
   318
  "tmbound0 t \<Longrightarrow> tmbound0 (simptm t)"
chaieb@33152
   319
by (induct t rule: simptm.induct, auto simp add: Let_def)
chaieb@33152
   320
chaieb@33152
   321
lemma simptm_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (simptm t)"
chaieb@33152
   322
by (induct t rule: simptm.induct, auto simp add: Let_def)
chaieb@33152
   323
lemma simptm_nlt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (simptm t)"
chaieb@33152
   324
by (induct t rule: simptm.induct, auto simp add: Let_def)
chaieb@33152
   325
chaieb@33152
   326
lemma [simp]: "isnpoly 0\<^sub>p" and [simp]: "isnpoly (C(1,1))" 
chaieb@33152
   327
  by (simp_all add: isnpoly_def)
chaieb@33152
   328
lemma simptm_allpolys_npoly[simp]: 
haftmann@36409
   329
  assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   330
  shows "allpolys isnpoly (simptm p)"
chaieb@33152
   331
  by (induct p rule: simptm.induct, auto simp add: Let_def)
chaieb@33152
   332
krauss@41822
   333
declare let_cong[fundef_cong del]
krauss@41822
   334
krauss@41822
   335
fun split0 :: "tm \<Rightarrow> (poly \<times> tm)" where
chaieb@33152
   336
  "split0 (Bound 0) = (1\<^sub>p, CP 0\<^sub>p)"
krauss@41822
   337
| "split0 (CNP 0 c t) = (let (c',t') = split0 t in (c +\<^sub>p c',t'))"
krauss@41822
   338
| "split0 (Neg t) = (let (c,t') = split0 t in (~\<^sub>p c,Neg t'))"
krauss@41822
   339
| "split0 (CNP n c t) = (let (c',t') = split0 t in (c',CNP n c t'))"
krauss@41822
   340
| "split0 (Add s t) = (let (c1,s') = split0 s ; (c2,t') = split0 t in (c1 +\<^sub>p c2, Add s' t'))"
krauss@41822
   341
| "split0 (Sub s t) = (let (c1,s') = split0 s ; (c2,t') = split0 t in (c1 -\<^sub>p c2, Sub s' t'))"
krauss@41822
   342
| "split0 (Mul c t) = (let (c',t') = split0 t in (c *\<^sub>p c', Mul c t'))"
krauss@41822
   343
| "split0 t = (0\<^sub>p, t)"
krauss@41822
   344
krauss@41822
   345
declare let_cong[fundef_cong]
chaieb@33152
   346
chaieb@33152
   347
lemma split0_stupid[simp]: "\<exists>x y. (x,y) = split0 p"
chaieb@33152
   348
  apply (rule exI[where x="fst (split0 p)"])
chaieb@33152
   349
  apply (rule exI[where x="snd (split0 p)"])
chaieb@33152
   350
  by simp
chaieb@33152
   351
chaieb@33152
   352
lemma split0:
chaieb@33152
   353
  "tmbound 0 (snd (split0 t)) \<and> (Itm vs bs (CNP 0 (fst (split0 t)) (snd (split0 t))) = Itm vs bs t)"
chaieb@33152
   354
  apply (induct t rule: split0.induct)
chaieb@33152
   355
  apply simp
haftmann@36348
   356
  apply (simp add: Let_def split_def field_simps)
haftmann@36348
   357
  apply (simp add: Let_def split_def field_simps)
haftmann@36348
   358
  apply (simp add: Let_def split_def field_simps)
haftmann@36348
   359
  apply (simp add: Let_def split_def field_simps)
haftmann@36348
   360
  apply (simp add: Let_def split_def field_simps)
webertj@49962
   361
  apply (simp add: Let_def split_def mult_assoc distrib_left[symmetric])
haftmann@36348
   362
  apply (simp add: Let_def split_def field_simps)
haftmann@36348
   363
  apply (simp add: Let_def split_def field_simps)
chaieb@33152
   364
  done
chaieb@33152
   365
chaieb@33152
   366
lemma split0_ci: "split0 t = (c',t') \<Longrightarrow> Itm vs bs t = Itm vs bs (CNP 0 c' t')"
chaieb@33152
   367
proof-
chaieb@33152
   368
  fix c' t'
chaieb@33152
   369
  assume "split0 t = (c', t')" hence "c' = fst (split0 t)" and "t' = snd (split0 t)" by auto
chaieb@33152
   370
  with split0[where t="t" and bs="bs"] show "Itm vs bs t = Itm vs bs (CNP 0 c' t')" by simp
chaieb@33152
   371
qed
chaieb@33152
   372
chaieb@33152
   373
lemma split0_nb0: 
haftmann@36409
   374
  assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   375
  shows "split0 t = (c',t') \<Longrightarrow>  tmbound 0 t'"
chaieb@33152
   376
proof-
chaieb@33152
   377
  fix c' t'
chaieb@33152
   378
  assume "split0 t = (c', t')" hence "c' = fst (split0 t)" and "t' = snd (split0 t)" by auto
chaieb@33152
   379
  with conjunct1[OF split0[where t="t"]] show "tmbound 0 t'" by simp
chaieb@33152
   380
qed
chaieb@33152
   381
haftmann@36409
   382
lemma split0_nb0'[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   383
  shows "tmbound0 (snd (split0 t))"
chaieb@33152
   384
  using split0_nb0[of t "fst (split0 t)" "snd (split0 t)"] by (simp add: tmbound0_tmbound_iff)
chaieb@33152
   385
chaieb@33152
   386
chaieb@33152
   387
lemma split0_nb: assumes nb:"tmbound n t" shows "tmbound n (snd (split0 t))"
chaieb@33152
   388
  using nb by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
chaieb@33152
   389
chaieb@33152
   390
lemma split0_blt: assumes nb:"tmboundslt n t" shows "tmboundslt n (snd (split0 t))"
chaieb@33152
   391
  using nb by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
chaieb@33152
   392
chaieb@33152
   393
lemma tmbound_split0: "tmbound 0 t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0"
chaieb@33152
   394
 by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
chaieb@33152
   395
chaieb@33152
   396
lemma tmboundslt_split0: "tmboundslt n t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0 \<or> n > 0"
chaieb@33152
   397
by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
chaieb@33152
   398
chaieb@33152
   399
lemma tmboundslt0_split0: "tmboundslt 0 t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0"
chaieb@33152
   400
 by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
chaieb@33152
   401
chaieb@33152
   402
lemma allpolys_split0: "allpolys isnpoly p \<Longrightarrow> allpolys isnpoly (snd (split0 p))"
chaieb@33152
   403
by (induct p rule: split0.induct, auto simp  add: isnpoly_def Let_def split_def split0_stupid)
chaieb@33152
   404
haftmann@36409
   405
lemma isnpoly_fst_split0:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   406
  shows 
chaieb@33152
   407
  "allpolys isnpoly p \<Longrightarrow> isnpoly (fst (split0 p))"
chaieb@33152
   408
  by (induct p rule: split0.induct, 
chaieb@33152
   409
    auto simp  add: polyadd_norm polysub_norm polyneg_norm polymul_norm 
chaieb@33152
   410
    Let_def split_def split0_stupid)
chaieb@33152
   411
chaieb@33152
   412
subsection{* Formulae *}
chaieb@33152
   413
chaieb@33152
   414
datatype fm  =  T| F| Le tm | Lt tm | Eq tm | NEq tm|
chaieb@33152
   415
  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
chaieb@33152
   416
chaieb@33152
   417
chaieb@33152
   418
  (* A size for fm *)
krauss@41822
   419
fun fmsize :: "fm \<Rightarrow> nat" where
chaieb@33152
   420
  "fmsize (NOT p) = 1 + fmsize p"
krauss@41822
   421
| "fmsize (And p q) = 1 + fmsize p + fmsize q"
krauss@41822
   422
| "fmsize (Or p q) = 1 + fmsize p + fmsize q"
krauss@41822
   423
| "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
krauss@41822
   424
| "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
krauss@41822
   425
| "fmsize (E p) = 1 + fmsize p"
krauss@41822
   426
| "fmsize (A p) = 4+ fmsize p"
krauss@41822
   427
| "fmsize p = 1"
chaieb@33152
   428
  (* several lemmas about fmsize *)
krauss@41822
   429
lemma fmsize_pos[termination_simp]: "fmsize p > 0"        
chaieb@33152
   430
by (induct p rule: fmsize.induct) simp_all
chaieb@33152
   431
chaieb@33152
   432
  (* Semantics of formulae (fm) *)
huffman@47108
   433
primrec Ifm ::"'a::{linordered_field_inverse_zero} list \<Rightarrow> 'a list \<Rightarrow> fm \<Rightarrow> bool" where
chaieb@33152
   434
  "Ifm vs bs T = True"
haftmann@39246
   435
| "Ifm vs bs F = False"
haftmann@39246
   436
| "Ifm vs bs (Lt a) = (Itm vs bs a < 0)"
haftmann@39246
   437
| "Ifm vs bs (Le a) = (Itm vs bs a \<le> 0)"
haftmann@39246
   438
| "Ifm vs bs (Eq a) = (Itm vs bs a = 0)"
haftmann@39246
   439
| "Ifm vs bs (NEq a) = (Itm vs bs a \<noteq> 0)"
haftmann@39246
   440
| "Ifm vs bs (NOT p) = (\<not> (Ifm vs bs p))"
haftmann@39246
   441
| "Ifm vs bs (And p q) = (Ifm vs bs p \<and> Ifm vs bs q)"
haftmann@39246
   442
| "Ifm vs bs (Or p q) = (Ifm vs bs p \<or> Ifm vs bs q)"
haftmann@39246
   443
| "Ifm vs bs (Imp p q) = ((Ifm vs bs p) \<longrightarrow> (Ifm vs bs q))"
haftmann@39246
   444
| "Ifm vs bs (Iff p q) = (Ifm vs bs p = Ifm vs bs q)"
haftmann@39246
   445
| "Ifm vs bs (E p) = (\<exists> x. Ifm vs (x#bs) p)"
haftmann@39246
   446
| "Ifm vs bs (A p) = (\<forall> x. Ifm vs (x#bs) p)"
chaieb@33152
   447
krauss@41822
   448
fun not:: "fm \<Rightarrow> fm" where
chaieb@33152
   449
  "not (NOT (NOT p)) = not p"
krauss@41822
   450
| "not (NOT p) = p"
krauss@41822
   451
| "not T = F"
krauss@41822
   452
| "not F = T"
krauss@41822
   453
| "not (Lt t) = Le (tmneg t)"
krauss@41822
   454
| "not (Le t) = Lt (tmneg t)"
krauss@41822
   455
| "not (Eq t) = NEq t"
krauss@41822
   456
| "not (NEq t) = Eq t"
krauss@41822
   457
| "not p = NOT p"
chaieb@33152
   458
lemma not[simp]: "Ifm vs bs (not p) = Ifm vs bs (NOT p)"
chaieb@33152
   459
by (induct p rule: not.induct) auto
chaieb@33152
   460
haftmann@35416
   461
definition conj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   462
  "conj p q \<equiv> (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else 
chaieb@33152
   463
   if p = q then p else And p q)"
chaieb@33152
   464
lemma conj[simp]: "Ifm vs bs (conj p q) = Ifm vs bs (And p q)"
chaieb@33152
   465
by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
chaieb@33152
   466
haftmann@35416
   467
definition disj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   468
  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
chaieb@33152
   469
       else if p=q then p else Or p q)"
chaieb@33152
   470
chaieb@33152
   471
lemma disj[simp]: "Ifm vs bs (disj p q) = Ifm vs bs (Or p q)"
chaieb@33152
   472
by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
chaieb@33152
   473
haftmann@35416
   474
definition imp :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   475
  "imp p q \<equiv> (if (p = F \<or> q=T \<or> p=q) then T else if p=T then q else if q=F then not p 
chaieb@33152
   476
    else Imp p q)"
chaieb@33152
   477
lemma imp[simp]: "Ifm vs bs (imp p q) = Ifm vs bs (Imp p q)"
chaieb@33152
   478
by (cases "p=F \<or> q=T",simp_all add: imp_def) 
chaieb@33152
   479
haftmann@35416
   480
definition iff :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   481
  "iff p q \<equiv> (if (p = q) then T else if (p = NOT q \<or> NOT p = q) then F else 
chaieb@33152
   482
       if p=F then not q else if q=F then not p else if p=T then q else if q=T then p else 
chaieb@33152
   483
  Iff p q)"
chaieb@33152
   484
lemma iff[simp]: "Ifm vs bs (iff p q) = Ifm vs bs (Iff p q)"
chaieb@33152
   485
  by (unfold iff_def,cases "p=q", simp,cases "p=NOT q", simp) (cases "NOT p= q", auto)
krauss@41822
   486
chaieb@33152
   487
  (* Quantifier freeness *)
krauss@41822
   488
fun qfree:: "fm \<Rightarrow> bool" where
chaieb@33152
   489
  "qfree (E p) = False"
krauss@41822
   490
| "qfree (A p) = False"
krauss@41822
   491
| "qfree (NOT p) = qfree p" 
krauss@41822
   492
| "qfree (And p q) = (qfree p \<and> qfree q)" 
krauss@41822
   493
| "qfree (Or  p q) = (qfree p \<and> qfree q)" 
krauss@41822
   494
| "qfree (Imp p q) = (qfree p \<and> qfree q)" 
krauss@41822
   495
| "qfree (Iff p q) = (qfree p \<and> qfree q)"
krauss@41822
   496
| "qfree p = True"
chaieb@33152
   497
chaieb@33152
   498
  (* Boundedness and substitution *)
chaieb@33152
   499
haftmann@39246
   500
primrec boundslt :: "nat \<Rightarrow> fm \<Rightarrow> bool" where
chaieb@33152
   501
  "boundslt n T = True"
haftmann@39246
   502
| "boundslt n F = True"
haftmann@39246
   503
| "boundslt n (Lt t) = (tmboundslt n t)"
haftmann@39246
   504
| "boundslt n (Le t) = (tmboundslt n t)"
haftmann@39246
   505
| "boundslt n (Eq t) = (tmboundslt n t)"
haftmann@39246
   506
| "boundslt n (NEq t) = (tmboundslt n t)"
haftmann@39246
   507
| "boundslt n (NOT p) = boundslt n p"
haftmann@39246
   508
| "boundslt n (And p q) = (boundslt n p \<and> boundslt n q)"
haftmann@39246
   509
| "boundslt n (Or p q) = (boundslt n p \<and> boundslt n q)"
haftmann@39246
   510
| "boundslt n (Imp p q) = ((boundslt n p) \<and> (boundslt n q))"
haftmann@39246
   511
| "boundslt n (Iff p q) = (boundslt n p \<and> boundslt n q)"
haftmann@39246
   512
| "boundslt n (E p) = boundslt (Suc n) p"
haftmann@39246
   513
| "boundslt n (A p) = boundslt (Suc n) p"
chaieb@33152
   514
krauss@41822
   515
fun bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *) where
chaieb@33152
   516
  "bound0 T = True"
krauss@41822
   517
| "bound0 F = True"
krauss@41822
   518
| "bound0 (Lt a) = tmbound0 a"
krauss@41822
   519
| "bound0 (Le a) = tmbound0 a"
krauss@41822
   520
| "bound0 (Eq a) = tmbound0 a"
krauss@41822
   521
| "bound0 (NEq a) = tmbound0 a"
krauss@41822
   522
| "bound0 (NOT p) = bound0 p"
krauss@41822
   523
| "bound0 (And p q) = (bound0 p \<and> bound0 q)"
krauss@41822
   524
| "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
krauss@41822
   525
| "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
krauss@41822
   526
| "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
krauss@41822
   527
| "bound0 p = False"
chaieb@33152
   528
lemma bound0_I:
chaieb@33152
   529
  assumes bp: "bound0 p"
chaieb@33152
   530
  shows "Ifm vs (b#bs) p = Ifm vs (b'#bs) p"
chaieb@33152
   531
using bp tmbound0_I[where b="b" and bs="bs" and b'="b'"]
nipkow@41842
   532
by (induct p rule: bound0.induct,auto)
chaieb@33152
   533
haftmann@39246
   534
primrec bound:: "nat \<Rightarrow> fm \<Rightarrow> bool" (* A Formula is independent of Bound n *) where
chaieb@33152
   535
  "bound m T = True"
haftmann@39246
   536
| "bound m F = True"
haftmann@39246
   537
| "bound m (Lt t) = tmbound m t"
haftmann@39246
   538
| "bound m (Le t) = tmbound m t"
haftmann@39246
   539
| "bound m (Eq t) = tmbound m t"
haftmann@39246
   540
| "bound m (NEq t) = tmbound m t"
haftmann@39246
   541
| "bound m (NOT p) = bound m p"
haftmann@39246
   542
| "bound m (And p q) = (bound m p \<and> bound m q)"
haftmann@39246
   543
| "bound m (Or p q) = (bound m p \<and> bound m q)"
haftmann@39246
   544
| "bound m (Imp p q) = ((bound m p) \<and> (bound m q))"
haftmann@39246
   545
| "bound m (Iff p q) = (bound m p \<and> bound m q)"
haftmann@39246
   546
| "bound m (E p) = bound (Suc m) p"
haftmann@39246
   547
| "bound m (A p) = bound (Suc m) p"
chaieb@33152
   548
chaieb@33152
   549
lemma bound_I:
chaieb@33152
   550
  assumes bnd: "boundslt (length bs) p" and nb: "bound n p" and le: "n \<le> length bs"
chaieb@33152
   551
  shows "Ifm vs (bs[n:=x]) p = Ifm vs bs p"
chaieb@33152
   552
  using bnd nb le tmbound_I[where bs=bs and vs = vs]
haftmann@39246
   553
proof(induct p arbitrary: bs n rule: fm.induct)
chaieb@33152
   554
  case (E p bs n) 
chaieb@33152
   555
  {fix y
wenzelm@41807
   556
    from E have bnd: "boundslt (length (y#bs)) p" 
chaieb@33152
   557
      and nb: "bound (Suc n) p" and le: "Suc n \<le> length (y#bs)" by simp+
chaieb@33152
   558
    from E.hyps[OF bnd nb le tmbound_I] have "Ifm vs ((y#bs)[Suc n:=x]) p = Ifm vs (y#bs) p" .   }
chaieb@33152
   559
  thus ?case by simp 
chaieb@33152
   560
next
chaieb@33152
   561
  case (A p bs n) {fix y
wenzelm@41807
   562
    from A have bnd: "boundslt (length (y#bs)) p" 
chaieb@33152
   563
      and nb: "bound (Suc n) p" and le: "Suc n \<le> length (y#bs)" by simp+
chaieb@33152
   564
    from A.hyps[OF bnd nb le tmbound_I] have "Ifm vs ((y#bs)[Suc n:=x]) p = Ifm vs (y#bs) p" .   }
chaieb@33152
   565
  thus ?case by simp 
chaieb@33152
   566
qed auto
chaieb@33152
   567
krauss@41822
   568
fun decr0 :: "fm \<Rightarrow> fm" where
chaieb@33152
   569
  "decr0 (Lt a) = Lt (decrtm0 a)"
krauss@41822
   570
| "decr0 (Le a) = Le (decrtm0 a)"
krauss@41822
   571
| "decr0 (Eq a) = Eq (decrtm0 a)"
krauss@41822
   572
| "decr0 (NEq a) = NEq (decrtm0 a)"
krauss@41822
   573
| "decr0 (NOT p) = NOT (decr0 p)" 
krauss@41822
   574
| "decr0 (And p q) = conj (decr0 p) (decr0 q)"
krauss@41822
   575
| "decr0 (Or p q) = disj (decr0 p) (decr0 q)"
krauss@41822
   576
| "decr0 (Imp p q) = imp (decr0 p) (decr0 q)"
krauss@41822
   577
| "decr0 (Iff p q) = iff (decr0 p) (decr0 q)"
krauss@41822
   578
| "decr0 p = p"
chaieb@33152
   579
chaieb@33152
   580
lemma decr0: assumes nb: "bound0 p"
chaieb@33152
   581
  shows "Ifm vs (x#bs) p = Ifm vs bs (decr0 p)"
chaieb@33152
   582
  using nb 
chaieb@33152
   583
  by (induct p rule: decr0.induct, simp_all add: decrtm0)
chaieb@33152
   584
haftmann@39246
   585
primrec decr :: "nat \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   586
  "decr m T = T"
haftmann@39246
   587
| "decr m F = F"
haftmann@39246
   588
| "decr m (Lt t) = (Lt (decrtm m t))"
haftmann@39246
   589
| "decr m (Le t) = (Le (decrtm m t))"
haftmann@39246
   590
| "decr m (Eq t) = (Eq (decrtm m t))"
haftmann@39246
   591
| "decr m (NEq t) = (NEq (decrtm m t))"
haftmann@39246
   592
| "decr m (NOT p) = NOT (decr m p)" 
haftmann@39246
   593
| "decr m (And p q) = conj (decr m p) (decr m q)"
haftmann@39246
   594
| "decr m (Or p q) = disj (decr m p) (decr m q)"
haftmann@39246
   595
| "decr m (Imp p q) = imp (decr m p) (decr m q)"
haftmann@39246
   596
| "decr m (Iff p q) = iff (decr m p) (decr m q)"
haftmann@39246
   597
| "decr m (E p) = E (decr (Suc m) p)"
haftmann@39246
   598
| "decr m (A p) = A (decr (Suc m) p)"
chaieb@33152
   599
chaieb@33152
   600
lemma decr: assumes  bnd: "boundslt (length bs) p" and nb: "bound m p" 
chaieb@33152
   601
  and nle: "m < length bs" 
chaieb@33152
   602
  shows "Ifm vs (removen m bs) (decr m p) = Ifm vs bs p"
chaieb@33152
   603
  using bnd nb nle
haftmann@39246
   604
proof(induct p arbitrary: bs m rule: fm.induct)
chaieb@33152
   605
  case (E p bs m) 
chaieb@33152
   606
  {fix x
wenzelm@41807
   607
    from E have bnd: "boundslt (length (x#bs)) p" and nb: "bound (Suc m) p" 
chaieb@33152
   608
  and nle: "Suc m < length (x#bs)" by auto
wenzelm@41807
   609
    from E(1)[OF bnd nb nle] have "Ifm vs (removen (Suc m) (x#bs)) (decr (Suc m) p) = Ifm vs (x#bs) p".
chaieb@33152
   610
  } thus ?case by auto 
chaieb@33152
   611
next
chaieb@33152
   612
  case (A p bs m)  
chaieb@33152
   613
  {fix x
wenzelm@41807
   614
    from A have bnd: "boundslt (length (x#bs)) p" and nb: "bound (Suc m) p" 
chaieb@33152
   615
  and nle: "Suc m < length (x#bs)" by auto
wenzelm@41807
   616
    from A(1)[OF bnd nb nle] have "Ifm vs (removen (Suc m) (x#bs)) (decr (Suc m) p) = Ifm vs (x#bs) p".
chaieb@33152
   617
  } thus ?case by auto
chaieb@33152
   618
qed (auto simp add: decrtm removen_nth)
chaieb@33152
   619
haftmann@39246
   620
primrec subst0:: "tm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   621
  "subst0 t T = T"
haftmann@39246
   622
| "subst0 t F = F"
haftmann@39246
   623
| "subst0 t (Lt a) = Lt (tmsubst0 t a)"
haftmann@39246
   624
| "subst0 t (Le a) = Le (tmsubst0 t a)"
haftmann@39246
   625
| "subst0 t (Eq a) = Eq (tmsubst0 t a)"
haftmann@39246
   626
| "subst0 t (NEq a) = NEq (tmsubst0 t a)"
haftmann@39246
   627
| "subst0 t (NOT p) = NOT (subst0 t p)"
haftmann@39246
   628
| "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
haftmann@39246
   629
| "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
haftmann@39246
   630
| "subst0 t (Imp p q) = Imp (subst0 t p)  (subst0 t q)"
haftmann@39246
   631
| "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
haftmann@39246
   632
| "subst0 t (E p) = E p"
haftmann@39246
   633
| "subst0 t (A p) = A p"
chaieb@33152
   634
chaieb@33152
   635
lemma subst0: assumes qf: "qfree p"
chaieb@33152
   636
  shows "Ifm vs (x#bs) (subst0 t p) = Ifm vs ((Itm vs (x#bs) t)#bs) p"
chaieb@33152
   637
using qf tmsubst0[where x="x" and bs="bs" and t="t"]
haftmann@39246
   638
by (induct p rule: fm.induct, auto)
chaieb@33152
   639
chaieb@33152
   640
lemma subst0_nb:
chaieb@33152
   641
  assumes bp: "tmbound0 t" and qf: "qfree p"
chaieb@33152
   642
  shows "bound0 (subst0 t p)"
chaieb@33152
   643
using qf tmsubst0_nb[OF bp] bp
haftmann@39246
   644
by (induct p rule: fm.induct, auto)
chaieb@33152
   645
haftmann@39246
   646
primrec subst:: "nat \<Rightarrow> tm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   647
  "subst n t T = T"
haftmann@39246
   648
| "subst n t F = F"
haftmann@39246
   649
| "subst n t (Lt a) = Lt (tmsubst n t a)"
haftmann@39246
   650
| "subst n t (Le a) = Le (tmsubst n t a)"
haftmann@39246
   651
| "subst n t (Eq a) = Eq (tmsubst n t a)"
haftmann@39246
   652
| "subst n t (NEq a) = NEq (tmsubst n t a)"
haftmann@39246
   653
| "subst n t (NOT p) = NOT (subst n t p)"
haftmann@39246
   654
| "subst n t (And p q) = And (subst n t p) (subst n t q)"
haftmann@39246
   655
| "subst n t (Or p q) = Or (subst n t p) (subst n t q)"
haftmann@39246
   656
| "subst n t (Imp p q) = Imp (subst n t p)  (subst n t q)"
haftmann@39246
   657
| "subst n t (Iff p q) = Iff (subst n t p) (subst n t q)"
haftmann@39246
   658
| "subst n t (E p) = E (subst (Suc n) (incrtm0 t) p)"
haftmann@39246
   659
| "subst n t (A p) = A (subst (Suc n) (incrtm0 t) p)"
chaieb@33152
   660
chaieb@33152
   661
lemma subst: assumes nb: "boundslt (length bs) p" and nlm: "n \<le> length bs"
chaieb@33152
   662
  shows "Ifm vs bs (subst n t p) = Ifm vs (bs[n:= Itm vs bs t]) p"
chaieb@33152
   663
  using nb nlm
haftmann@39246
   664
proof (induct p arbitrary: bs n t rule: fm.induct)
chaieb@33152
   665
  case (E p bs n) 
chaieb@33152
   666
  {fix x 
wenzelm@41807
   667
    from E have bn: "boundslt (length (x#bs)) p" by simp 
wenzelm@41807
   668
    from E have nlm: "Suc n \<le> length (x#bs)" by simp
wenzelm@41807
   669
    from E(1)[OF bn nlm] have "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs ((x#bs)[Suc n:= Itm vs (x#bs) (incrtm0 t)]) p" by simp 
chaieb@33152
   670
    hence "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs (x#bs[n:= Itm vs bs t]) p"
chaieb@33152
   671
    by (simp add: incrtm0[where x="x" and bs="bs" and t="t"]) }  
chaieb@33152
   672
thus ?case by simp 
chaieb@33152
   673
next
chaieb@33152
   674
  case (A p bs n)   
chaieb@33152
   675
  {fix x 
wenzelm@41807
   676
    from A have bn: "boundslt (length (x#bs)) p" by simp 
wenzelm@41807
   677
    from A have nlm: "Suc n \<le> length (x#bs)" by simp
wenzelm@41807
   678
    from A(1)[OF bn nlm] have "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs ((x#bs)[Suc n:= Itm vs (x#bs) (incrtm0 t)]) p" by simp 
chaieb@33152
   679
    hence "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs (x#bs[n:= Itm vs bs t]) p"
chaieb@33152
   680
    by (simp add: incrtm0[where x="x" and bs="bs" and t="t"]) }  
chaieb@33152
   681
thus ?case by simp 
chaieb@33152
   682
qed(auto simp add: tmsubst)
chaieb@33152
   683
chaieb@33152
   684
lemma subst_nb: assumes tnb: "tmbound m t"
chaieb@33152
   685
shows "bound m (subst m t p)"
chaieb@33152
   686
using tnb tmsubst_nb incrtm0_tmbound
haftmann@39246
   687
by (induct p arbitrary: m t rule: fm.induct, auto)
chaieb@33152
   688
chaieb@33152
   689
lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
chaieb@33152
   690
by (induct p rule: not.induct, auto)
chaieb@33152
   691
lemma not_bn0[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
chaieb@33152
   692
by (induct p rule: not.induct, auto)
chaieb@33152
   693
lemma not_nb[simp]: "bound n p \<Longrightarrow> bound n (not p)"
chaieb@33152
   694
by (induct p rule: not.induct, auto)
chaieb@33152
   695
lemma not_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n (not p)"
chaieb@33152
   696
 by (induct p rule: not.induct, auto)
chaieb@33152
   697
chaieb@33152
   698
lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
chaieb@33152
   699
using conj_def by auto 
chaieb@33152
   700
lemma conj_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
chaieb@33152
   701
using conj_def by auto 
chaieb@33152
   702
lemma conj_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (conj p q)"
chaieb@33152
   703
using conj_def by auto 
chaieb@33152
   704
lemma conj_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (conj p q)"
chaieb@33152
   705
using conj_def by auto 
chaieb@33152
   706
chaieb@33152
   707
lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
chaieb@33152
   708
using disj_def by auto 
chaieb@33152
   709
lemma disj_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
chaieb@33152
   710
using disj_def by auto 
chaieb@33152
   711
lemma disj_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (disj p q)"
chaieb@33152
   712
using disj_def by auto 
chaieb@33152
   713
lemma disj_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (disj p q)"
chaieb@33152
   714
using disj_def by auto 
chaieb@33152
   715
chaieb@33152
   716
lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
chaieb@33152
   717
using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
chaieb@33152
   718
lemma imp_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
chaieb@33152
   719
using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
chaieb@33152
   720
lemma imp_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (imp p q)"
chaieb@33152
   721
using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
chaieb@33152
   722
lemma imp_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (imp p q)"
chaieb@33152
   723
using imp_def by auto 
chaieb@33152
   724
chaieb@33152
   725
lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
chaieb@33152
   726
  by (unfold iff_def,cases "p=q", auto)
chaieb@33152
   727
lemma iff_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
chaieb@33152
   728
using iff_def by (unfold iff_def,cases "p=q", auto)
chaieb@33152
   729
lemma iff_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (iff p q)"
chaieb@33152
   730
using iff_def by (unfold iff_def,cases "p=q", auto)
chaieb@33152
   731
lemma iff_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (iff p q)"
chaieb@33152
   732
using iff_def by auto 
chaieb@33152
   733
lemma decr0_qf: "bound0 p \<Longrightarrow> qfree (decr0 p)"
chaieb@33152
   734
by (induct p, simp_all)
chaieb@33152
   735
krauss@41822
   736
fun isatom :: "fm \<Rightarrow> bool" (* test for atomicity *) where
chaieb@33152
   737
  "isatom T = True"
krauss@41822
   738
| "isatom F = True"
krauss@41822
   739
| "isatom (Lt a) = True"
krauss@41822
   740
| "isatom (Le a) = True"
krauss@41822
   741
| "isatom (Eq a) = True"
krauss@41822
   742
| "isatom (NEq a) = True"
krauss@41822
   743
| "isatom p = False"
chaieb@33152
   744
chaieb@33152
   745
lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
chaieb@33152
   746
by (induct p, simp_all)
chaieb@33152
   747
haftmann@35416
   748
definition djf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   749
  "djf f p q \<equiv> (if q=T then T else if q=F then f p else 
chaieb@33152
   750
  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
haftmann@35416
   751
definition evaldjf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
chaieb@33152
   752
  "evaldjf f ps \<equiv> foldr (djf f) ps F"
chaieb@33152
   753
chaieb@33152
   754
lemma djf_Or: "Ifm vs bs (djf f p q) = Ifm vs bs (Or (f p) q)"
chaieb@33152
   755
by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
chaieb@33152
   756
(cases "f p", simp_all add: Let_def djf_def) 
chaieb@33152
   757
chaieb@33152
   758
lemma evaldjf_ex: "Ifm vs bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm vs bs (f p))"
chaieb@33152
   759
  by(induct ps, simp_all add: evaldjf_def djf_Or)
chaieb@33152
   760
chaieb@33152
   761
lemma evaldjf_bound0: 
chaieb@33152
   762
  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
chaieb@33152
   763
  shows "bound0 (evaldjf f xs)"
chaieb@33152
   764
  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
chaieb@33152
   765
chaieb@33152
   766
lemma evaldjf_qf: 
chaieb@33152
   767
  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
chaieb@33152
   768
  shows "qfree (evaldjf f xs)"
chaieb@33152
   769
  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
chaieb@33152
   770
krauss@41822
   771
fun disjuncts :: "fm \<Rightarrow> fm list" where
chaieb@33152
   772
  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
krauss@41822
   773
| "disjuncts F = []"
krauss@41822
   774
| "disjuncts p = [p]"
chaieb@33152
   775
chaieb@33152
   776
lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm vs bs q) = Ifm vs bs p"
chaieb@33152
   777
by(induct p rule: disjuncts.induct, auto)
chaieb@33152
   778
chaieb@33152
   779
lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
chaieb@33152
   780
proof-
chaieb@33152
   781
  assume nb: "bound0 p"
chaieb@33152
   782
  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
chaieb@33152
   783
  thus ?thesis by (simp only: list_all_iff)
chaieb@33152
   784
qed
chaieb@33152
   785
chaieb@33152
   786
lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
chaieb@33152
   787
proof-
chaieb@33152
   788
  assume qf: "qfree p"
chaieb@33152
   789
  hence "list_all qfree (disjuncts p)"
chaieb@33152
   790
    by (induct p rule: disjuncts.induct, auto)
chaieb@33152
   791
  thus ?thesis by (simp only: list_all_iff)
chaieb@33152
   792
qed
chaieb@33152
   793
haftmann@35416
   794
definition DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   795
  "DJ f p \<equiv> evaldjf f (disjuncts p)"
chaieb@33152
   796
chaieb@33152
   797
lemma DJ: assumes fdj: "\<forall> p q. Ifm vs bs (f (Or p q)) = Ifm vs bs (Or (f p) (f q))"
chaieb@33152
   798
  and fF: "f F = F"
chaieb@33152
   799
  shows "Ifm vs bs (DJ f p) = Ifm vs bs (f p)"
chaieb@33152
   800
proof-
chaieb@33152
   801
  have "Ifm vs bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm vs bs (f q))"
chaieb@33152
   802
    by (simp add: DJ_def evaldjf_ex) 
chaieb@33152
   803
  also have "\<dots> = Ifm vs bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
chaieb@33152
   804
  finally show ?thesis .
chaieb@33152
   805
qed
chaieb@33152
   806
chaieb@33152
   807
lemma DJ_qf: assumes 
chaieb@33152
   808
  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
chaieb@33152
   809
  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
chaieb@33152
   810
proof(clarify)
chaieb@33152
   811
  fix  p assume qf: "qfree p"
chaieb@33152
   812
  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
chaieb@33152
   813
  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
chaieb@33152
   814
  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
chaieb@33152
   815
  
chaieb@33152
   816
  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
chaieb@33152
   817
qed
chaieb@33152
   818
chaieb@33152
   819
lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
chaieb@33152
   820
  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm vs bs ((DJ qe p)) = Ifm vs bs (E p))"
chaieb@33152
   821
proof(clarify)
chaieb@33152
   822
  fix p::fm and bs
chaieb@33152
   823
  assume qf: "qfree p"
chaieb@33152
   824
  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
chaieb@33152
   825
  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
chaieb@33152
   826
  have "Ifm vs bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm vs bs (qe q))"
chaieb@33152
   827
    by (simp add: DJ_def evaldjf_ex)
chaieb@33152
   828
  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm vs bs (E q))" using qe disjuncts_qf[OF qf] by auto
chaieb@33152
   829
  also have "\<dots> = Ifm vs bs (E p)" by (induct p rule: disjuncts.induct, auto)
chaieb@33152
   830
  finally show "qfree (DJ qe p) \<and> Ifm vs bs (DJ qe p) = Ifm vs bs (E p)" using qfth by blast
chaieb@33152
   831
qed
chaieb@33152
   832
krauss@41822
   833
fun conjuncts :: "fm \<Rightarrow> fm list" where
chaieb@33152
   834
  "conjuncts (And p q) = (conjuncts p) @ (conjuncts q)"
krauss@41822
   835
| "conjuncts T = []"
krauss@41822
   836
| "conjuncts p = [p]"
chaieb@33152
   837
haftmann@35416
   838
definition list_conj :: "fm list \<Rightarrow> fm" where
chaieb@33152
   839
  "list_conj ps \<equiv> foldr conj ps T"
chaieb@33152
   840
haftmann@35416
   841
definition CJNB :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@33152
   842
  "CJNB f p \<equiv> (let cjs = conjuncts p ; (yes,no) = partition bound0 cjs
chaieb@33152
   843
                   in conj (decr0 (list_conj yes)) (f (list_conj no)))"
chaieb@33152
   844
chaieb@33152
   845
lemma conjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). qfree q"
chaieb@33152
   846
proof-
chaieb@33152
   847
  assume qf: "qfree p"
chaieb@33152
   848
  hence "list_all qfree (conjuncts p)"
chaieb@33152
   849
    by (induct p rule: conjuncts.induct, auto)
chaieb@33152
   850
  thus ?thesis by (simp only: list_all_iff)
chaieb@33152
   851
qed
chaieb@33152
   852
chaieb@33152
   853
lemma conjuncts: "(\<forall> q\<in> set (conjuncts p). Ifm vs bs q) = Ifm vs bs p"
chaieb@33152
   854
by(induct p rule: conjuncts.induct, auto)
chaieb@33152
   855
chaieb@33152
   856
lemma conjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). bound0 q"
chaieb@33152
   857
proof-
chaieb@33152
   858
  assume nb: "bound0 p"
chaieb@33152
   859
  hence "list_all bound0 (conjuncts p)" by (induct p rule:conjuncts.induct,auto)
chaieb@33152
   860
  thus ?thesis by (simp only: list_all_iff)
chaieb@33152
   861
qed
chaieb@33152
   862
chaieb@33152
   863
fun islin :: "fm \<Rightarrow> bool" where
chaieb@33152
   864
  "islin (And p q) = (islin p \<and> islin q \<and> p \<noteq> T \<and> p \<noteq> F \<and> q \<noteq> T \<and> q \<noteq> F)"
chaieb@33152
   865
| "islin (Or p q) = (islin p \<and> islin q \<and> p \<noteq> T \<and> p \<noteq> F \<and> q \<noteq> T \<and> q \<noteq> F)"
chaieb@33152
   866
| "islin (Eq (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
chaieb@33152
   867
| "islin (NEq (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
chaieb@33152
   868
| "islin (Lt (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
chaieb@33152
   869
| "islin (Le (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
chaieb@33152
   870
| "islin (NOT p) = False"
chaieb@33152
   871
| "islin (Imp p q) = False"
chaieb@33152
   872
| "islin (Iff p q) = False"
chaieb@33152
   873
| "islin p = bound0 p"
chaieb@33152
   874
chaieb@33152
   875
lemma islin_stupid: assumes nb: "tmbound0 p"
chaieb@33152
   876
  shows "islin (Lt p)" and "islin (Le p)" and "islin (Eq p)" and "islin (NEq p)"
chaieb@33152
   877
  using nb by (cases p, auto, case_tac nat, auto)+
chaieb@33152
   878
chaieb@33152
   879
definition "lt p = (case p of CP (C c) \<Rightarrow> if 0>\<^sub>N c then T else F| _ \<Rightarrow> Lt p)"
chaieb@33152
   880
definition "le p = (case p of CP (C c) \<Rightarrow> if 0\<ge>\<^sub>N c then T else F | _ \<Rightarrow> Le p)"
haftmann@38864
   881
definition eq where "eq p = (case p of CP (C c) \<Rightarrow> if c = 0\<^sub>N then T else F | _ \<Rightarrow> Eq p)"
chaieb@33152
   882
definition "neq p = not (eq p)"
chaieb@33152
   883
chaieb@33152
   884
lemma lt: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (lt p) = Ifm vs bs (Lt p)"
chaieb@33152
   885
  apply(simp add: lt_def)
chaieb@33152
   886
  apply(cases p, simp_all)
chaieb@33152
   887
  apply (case_tac poly, simp_all add: isnpoly_def)
chaieb@33152
   888
  done
chaieb@33152
   889
chaieb@33152
   890
lemma le: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (le p) = Ifm vs bs (Le p)"
chaieb@33152
   891
  apply(simp add: le_def)
chaieb@33152
   892
  apply(cases p, simp_all)
chaieb@33152
   893
  apply (case_tac poly, simp_all add: isnpoly_def)
chaieb@33152
   894
  done
chaieb@33152
   895
chaieb@33152
   896
lemma eq: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (eq p) = Ifm vs bs (Eq p)"
chaieb@33152
   897
  apply(simp add: eq_def)
chaieb@33152
   898
  apply(cases p, simp_all)
chaieb@33152
   899
  apply (case_tac poly, simp_all add: isnpoly_def)
chaieb@33152
   900
  done
chaieb@33152
   901
chaieb@33152
   902
lemma neq: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (neq p) = Ifm vs bs (NEq p)"
chaieb@33152
   903
  by(simp add: neq_def eq)
chaieb@33152
   904
chaieb@33152
   905
lemma lt_lin: "tmbound0 p \<Longrightarrow> islin (lt p)"
chaieb@33152
   906
  apply (simp add: lt_def)
chaieb@33152
   907
  apply (cases p, simp_all)
chaieb@33152
   908
  apply (case_tac poly, simp_all)
chaieb@33152
   909
  apply (case_tac nat, simp_all)
chaieb@33152
   910
  done
chaieb@33152
   911
chaieb@33152
   912
lemma le_lin: "tmbound0 p \<Longrightarrow> islin (le p)"
chaieb@33152
   913
  apply (simp add: le_def)
chaieb@33152
   914
  apply (cases p, simp_all)
chaieb@33152
   915
  apply (case_tac poly, simp_all)
chaieb@33152
   916
  apply (case_tac nat, simp_all)
chaieb@33152
   917
  done
chaieb@33152
   918
chaieb@33152
   919
lemma eq_lin: "tmbound0 p \<Longrightarrow> islin (eq p)"
chaieb@33152
   920
  apply (simp add: eq_def)
chaieb@33152
   921
  apply (cases p, simp_all)
chaieb@33152
   922
  apply (case_tac poly, simp_all)
chaieb@33152
   923
  apply (case_tac nat, simp_all)
chaieb@33152
   924
  done
chaieb@33152
   925
chaieb@33152
   926
lemma neq_lin: "tmbound0 p \<Longrightarrow> islin (neq p)"
chaieb@33152
   927
  apply (simp add: neq_def eq_def)
chaieb@33152
   928
  apply (cases p, simp_all)
chaieb@33152
   929
  apply (case_tac poly, simp_all)
chaieb@33152
   930
  apply (case_tac nat, simp_all)
chaieb@33152
   931
  done
chaieb@33152
   932
chaieb@33152
   933
definition "simplt t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then lt s else Lt (CNP 0 c s))"
chaieb@33152
   934
definition "simple t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then le s else Le (CNP 0 c s))"
chaieb@33152
   935
definition "simpeq t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then eq s else Eq (CNP 0 c s))"
chaieb@33152
   936
definition "simpneq t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then neq s else NEq (CNP 0 c s))"
chaieb@33152
   937
haftmann@36409
   938
lemma simplt_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   939
  shows "islin (simplt t)"
chaieb@33152
   940
  unfolding simplt_def 
chaieb@33152
   941
  using split0_nb0'
chaieb@33152
   942
by (auto simp add: lt_lin Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly])
chaieb@33152
   943
  
haftmann@36409
   944
lemma simple_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   945
  shows "islin (simple t)"
chaieb@33152
   946
  unfolding simple_def 
chaieb@33152
   947
  using split0_nb0'
chaieb@33152
   948
by (auto simp add: Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly] le_lin)
haftmann@36409
   949
lemma simpeq_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   950
  shows "islin (simpeq t)"
chaieb@33152
   951
  unfolding simpeq_def 
chaieb@33152
   952
  using split0_nb0'
chaieb@33152
   953
by (auto simp add: Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly] eq_lin)
chaieb@33152
   954
haftmann@36409
   955
lemma simpneq_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   956
  shows "islin (simpneq t)"
chaieb@33152
   957
  unfolding simpneq_def 
chaieb@33152
   958
  using split0_nb0'
chaieb@33152
   959
by (auto simp add: Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly] neq_lin)
chaieb@33152
   960
chaieb@33152
   961
lemma really_stupid: "\<not> (\<forall>c1 s'. (c1, s') \<noteq> split0 s)"
chaieb@33152
   962
  by (cases "split0 s", auto)
haftmann@36409
   963
lemma split0_npoly:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
   964
  and n: "allpolys isnpoly t"
chaieb@33152
   965
  shows "isnpoly (fst (split0 t))" and "allpolys isnpoly (snd (split0 t))"
chaieb@33152
   966
  using n
chaieb@33152
   967
  by (induct t rule: split0.induct, auto simp add: Let_def split_def polyadd_norm polymul_norm polyneg_norm polysub_norm really_stupid)
chaieb@33152
   968
lemma simplt[simp]:
chaieb@33152
   969
  shows "Ifm vs bs (simplt t) = Ifm vs bs (Lt t)"
chaieb@33152
   970
proof-
chaieb@33152
   971
  have n: "allpolys isnpoly (simptm t)" by simp
chaieb@33152
   972
  let ?t = "simptm t"
chaieb@33152
   973
  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
chaieb@33152
   974
      using split0[of "simptm t" vs bs] lt[OF split0_npoly(2)[OF n], of vs bs]
chaieb@33152
   975
      by (simp add: simplt_def Let_def split_def lt)}
chaieb@33152
   976
  moreover
chaieb@33152
   977
  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
chaieb@33152
   978
    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simplt_def Let_def split_def)
chaieb@33152
   979
  }
chaieb@33152
   980
  ultimately show ?thesis by blast
chaieb@33152
   981
qed
chaieb@33152
   982
chaieb@33152
   983
lemma simple[simp]:
chaieb@33152
   984
  shows "Ifm vs bs (simple t) = Ifm vs bs (Le t)"
chaieb@33152
   985
proof-
chaieb@33152
   986
  have n: "allpolys isnpoly (simptm t)" by simp
chaieb@33152
   987
  let ?t = "simptm t"
chaieb@33152
   988
  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
chaieb@33152
   989
      using split0[of "simptm t" vs bs] le[OF split0_npoly(2)[OF n], of vs bs]
chaieb@33152
   990
      by (simp add: simple_def Let_def split_def le)}
chaieb@33152
   991
  moreover
chaieb@33152
   992
  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
chaieb@33152
   993
    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simple_def Let_def split_def)
chaieb@33152
   994
  }
chaieb@33152
   995
  ultimately show ?thesis by blast
chaieb@33152
   996
qed
chaieb@33152
   997
chaieb@33152
   998
lemma simpeq[simp]:
chaieb@33152
   999
  shows "Ifm vs bs (simpeq t) = Ifm vs bs (Eq t)"
chaieb@33152
  1000
proof-
chaieb@33152
  1001
  have n: "allpolys isnpoly (simptm t)" by simp
chaieb@33152
  1002
  let ?t = "simptm t"
chaieb@33152
  1003
  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
chaieb@33152
  1004
      using split0[of "simptm t" vs bs] eq[OF split0_npoly(2)[OF n], of vs bs]
chaieb@33152
  1005
      by (simp add: simpeq_def Let_def split_def)}
chaieb@33152
  1006
  moreover
chaieb@33152
  1007
  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
chaieb@33152
  1008
    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simpeq_def Let_def split_def)
chaieb@33152
  1009
  }
chaieb@33152
  1010
  ultimately show ?thesis by blast
chaieb@33152
  1011
qed
chaieb@33152
  1012
chaieb@33152
  1013
lemma simpneq[simp]:
chaieb@33152
  1014
  shows "Ifm vs bs (simpneq t) = Ifm vs bs (NEq t)"
chaieb@33152
  1015
proof-
chaieb@33152
  1016
  have n: "allpolys isnpoly (simptm t)" by simp
chaieb@33152
  1017
  let ?t = "simptm t"
chaieb@33152
  1018
  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
chaieb@33152
  1019
      using split0[of "simptm t" vs bs] neq[OF split0_npoly(2)[OF n], of vs bs]
chaieb@33152
  1020
      by (simp add: simpneq_def Let_def split_def )}
chaieb@33152
  1021
  moreover
chaieb@33152
  1022
  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
chaieb@33152
  1023
    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simpneq_def Let_def split_def)
chaieb@33152
  1024
  }
chaieb@33152
  1025
  ultimately show ?thesis by blast
chaieb@33152
  1026
qed
chaieb@33152
  1027
chaieb@33152
  1028
lemma lt_nb: "tmbound0 t \<Longrightarrow> bound0 (lt t)"
chaieb@33152
  1029
  apply (simp add: lt_def)
chaieb@33152
  1030
  apply (cases t, auto)
chaieb@33152
  1031
  apply (case_tac poly, auto)
chaieb@33152
  1032
  done
chaieb@33152
  1033
chaieb@33152
  1034
lemma le_nb: "tmbound0 t \<Longrightarrow> bound0 (le t)"
chaieb@33152
  1035
  apply (simp add: le_def)
chaieb@33152
  1036
  apply (cases t, auto)
chaieb@33152
  1037
  apply (case_tac poly, auto)
chaieb@33152
  1038
  done
chaieb@33152
  1039
chaieb@33152
  1040
lemma eq_nb: "tmbound0 t \<Longrightarrow> bound0 (eq t)"
chaieb@33152
  1041
  apply (simp add: eq_def)
chaieb@33152
  1042
  apply (cases t, auto)
chaieb@33152
  1043
  apply (case_tac poly, auto)
chaieb@33152
  1044
  done
chaieb@33152
  1045
chaieb@33152
  1046
lemma neq_nb: "tmbound0 t \<Longrightarrow> bound0 (neq t)"
chaieb@33152
  1047
  apply (simp add: neq_def eq_def)
chaieb@33152
  1048
  apply (cases t, auto)
chaieb@33152
  1049
  apply (case_tac poly, auto)
chaieb@33152
  1050
  done
chaieb@33152
  1051
haftmann@36409
  1052
lemma simplt_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
  1053
  shows "tmbound0 t \<Longrightarrow> bound0 (simplt t)"
wenzelm@48562
  1054
  using split0 [of "simptm t" "vs::'a list" bs]
chaieb@33152
  1055
proof(simp add: simplt_def Let_def split_def)
chaieb@33152
  1056
  assume nb: "tmbound0 t"
chaieb@33152
  1057
  hence nb': "tmbound0 (simptm t)" by simp
chaieb@33152
  1058
  let ?c = "fst (split0 (simptm t))"
chaieb@33152
  1059
  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
chaieb@33152
  1060
  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
chaieb@33152
  1061
  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
chaieb@33152
  1062
  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
chaieb@33152
  1063
  from iffD1[OF isnpolyh_unique[OF ths] th]
chaieb@33152
  1064
  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
chaieb@33152
  1065
  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (lt (snd (split0 (simptm t))))) \<and>
chaieb@33152
  1066
       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simplt_def Let_def split_def lt_nb)
chaieb@33152
  1067
qed
chaieb@33152
  1068
haftmann@36409
  1069
lemma simple_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
  1070
  shows "tmbound0 t \<Longrightarrow> bound0 (simple t)"
wenzelm@48562
  1071
  using split0 [of "simptm t" "vs::'a list" bs]
chaieb@33152
  1072
proof(simp add: simple_def Let_def split_def)
chaieb@33152
  1073
  assume nb: "tmbound0 t"
chaieb@33152
  1074
  hence nb': "tmbound0 (simptm t)" by simp
chaieb@33152
  1075
  let ?c = "fst (split0 (simptm t))"
chaieb@33152
  1076
  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
chaieb@33152
  1077
  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
chaieb@33152
  1078
  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
chaieb@33152
  1079
  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
chaieb@33152
  1080
  from iffD1[OF isnpolyh_unique[OF ths] th]
chaieb@33152
  1081
  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
chaieb@33152
  1082
  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (le (snd (split0 (simptm t))))) \<and>
chaieb@33152
  1083
       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simplt_def Let_def split_def le_nb)
chaieb@33152
  1084
qed
chaieb@33152
  1085
haftmann@36409
  1086
lemma simpeq_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
  1087
  shows "tmbound0 t \<Longrightarrow> bound0 (simpeq t)"
wenzelm@48562
  1088
  using split0 [of "simptm t" "vs::'a list" bs]
chaieb@33152
  1089
proof(simp add: simpeq_def Let_def split_def)
chaieb@33152
  1090
  assume nb: "tmbound0 t"
chaieb@33152
  1091
  hence nb': "tmbound0 (simptm t)" by simp
chaieb@33152
  1092
  let ?c = "fst (split0 (simptm t))"
chaieb@33152
  1093
  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
chaieb@33152
  1094
  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
chaieb@33152
  1095
  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
chaieb@33152
  1096
  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
chaieb@33152
  1097
  from iffD1[OF isnpolyh_unique[OF ths] th]
chaieb@33152
  1098
  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
chaieb@33152
  1099
  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (eq (snd (split0 (simptm t))))) \<and>
chaieb@33152
  1100
       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simpeq_def Let_def split_def eq_nb)
chaieb@33152
  1101
qed
chaieb@33152
  1102
haftmann@36409
  1103
lemma simpneq_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
  1104
  shows "tmbound0 t \<Longrightarrow> bound0 (simpneq t)"
wenzelm@48562
  1105
  using split0 [of "simptm t" "vs::'a list" bs]
chaieb@33152
  1106
proof(simp add: simpneq_def Let_def split_def)
chaieb@33152
  1107
  assume nb: "tmbound0 t"
chaieb@33152
  1108
  hence nb': "tmbound0 (simptm t)" by simp
chaieb@33152
  1109
  let ?c = "fst (split0 (simptm t))"
chaieb@33152
  1110
  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
chaieb@33152
  1111
  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
chaieb@33152
  1112
  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
chaieb@33152
  1113
  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
chaieb@33152
  1114
  from iffD1[OF isnpolyh_unique[OF ths] th]
chaieb@33152
  1115
  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
chaieb@33152
  1116
  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (neq (snd (split0 (simptm t))))) \<and>
chaieb@33152
  1117
       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simpneq_def Let_def split_def neq_nb)
chaieb@33152
  1118
qed
chaieb@33152
  1119
krauss@41822
  1120
fun conjs   :: "fm \<Rightarrow> fm list" where
chaieb@33152
  1121
  "conjs (And p q) = (conjs p)@(conjs q)"
krauss@41822
  1122
| "conjs T = []"
krauss@41822
  1123
| "conjs p = [p]"
chaieb@33152
  1124
lemma conjs_ci: "(\<forall> q \<in> set (conjs p). Ifm vs bs q) = Ifm vs bs p"
chaieb@33152
  1125
by (induct p rule: conjs.induct, auto)
haftmann@35416
  1126
definition list_disj :: "fm list \<Rightarrow> fm" where
chaieb@33152
  1127
  "list_disj ps \<equiv> foldr disj ps F"
chaieb@33152
  1128
chaieb@33152
  1129
lemma list_conj: "Ifm vs bs (list_conj ps) = (\<forall>p\<in> set ps. Ifm vs bs p)"
chaieb@33152
  1130
  by (induct ps, auto simp add: list_conj_def)
chaieb@33152
  1131
lemma list_conj_qf: " \<forall>p\<in> set ps. qfree p \<Longrightarrow> qfree (list_conj ps)"
haftmann@50045
  1132
  by (induct ps, auto simp add: list_conj_def)
chaieb@33152
  1133
lemma list_disj: "Ifm vs bs (list_disj ps) = (\<exists>p\<in> set ps. Ifm vs bs p)"
chaieb@33152
  1134
  by (induct ps, auto simp add: list_disj_def)
chaieb@33152
  1135
chaieb@33152
  1136
lemma conj_boundslt: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (conj p q)"
chaieb@33152
  1137
  unfolding conj_def by auto
chaieb@33152
  1138
chaieb@33152
  1139
lemma conjs_nb: "bound n p \<Longrightarrow> \<forall>q\<in> set (conjs p). bound n q"
chaieb@33152
  1140
  apply (induct p rule: conjs.induct) 
chaieb@33152
  1141
  apply (unfold conjs.simps)
chaieb@33152
  1142
  apply (unfold set_append)
chaieb@33152
  1143
  apply (unfold ball_Un)
chaieb@33152
  1144
  apply (unfold bound.simps)
chaieb@33152
  1145
  apply auto
chaieb@33152
  1146
  done
chaieb@33152
  1147
chaieb@33152
  1148
lemma conjs_boundslt: "boundslt n p \<Longrightarrow> \<forall>q\<in> set (conjs p). boundslt n q"
chaieb@33152
  1149
  apply (induct p rule: conjs.induct) 
chaieb@33152
  1150
  apply (unfold conjs.simps)
chaieb@33152
  1151
  apply (unfold set_append)
chaieb@33152
  1152
  apply (unfold ball_Un)
chaieb@33152
  1153
  apply (unfold boundslt.simps)
chaieb@33152
  1154
  apply blast
chaieb@33152
  1155
by simp_all
chaieb@33152
  1156
chaieb@33152
  1157
lemma list_conj_boundslt: " \<forall>p\<in> set ps. boundslt n p \<Longrightarrow> boundslt n (list_conj ps)"
chaieb@33152
  1158
  unfolding list_conj_def
chaieb@33152
  1159
  by (induct ps, auto simp add: conj_boundslt)
chaieb@33152
  1160
chaieb@33152
  1161
lemma list_conj_nb: assumes bnd: "\<forall>p\<in> set ps. bound n p"
chaieb@33152
  1162
  shows "bound n (list_conj ps)"
chaieb@33152
  1163
  using bnd
chaieb@33152
  1164
  unfolding list_conj_def
chaieb@33152
  1165
  by (induct ps, auto simp add: conj_nb)
chaieb@33152
  1166
chaieb@33152
  1167
lemma list_conj_nb': "\<forall>p\<in>set ps. bound0 p \<Longrightarrow> bound0 (list_conj ps)"
chaieb@33152
  1168
unfolding list_conj_def by (induct ps , auto)
chaieb@33152
  1169
chaieb@33152
  1170
lemma CJNB_qe: 
chaieb@33152
  1171
  assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
chaieb@33152
  1172
  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (CJNB qe p) \<and> (Ifm vs bs ((CJNB qe p)) = Ifm vs bs (E p))"
chaieb@33152
  1173
proof(clarify)
chaieb@33152
  1174
  fix bs p
chaieb@33152
  1175
  assume qfp: "qfree p"
chaieb@33152
  1176
  let ?cjs = "conjuncts p"
chaieb@33152
  1177
  let ?yes = "fst (partition bound0 ?cjs)"
chaieb@33152
  1178
  let ?no = "snd (partition bound0 ?cjs)"
chaieb@33152
  1179
  let ?cno = "list_conj ?no"
chaieb@33152
  1180
  let ?cyes = "list_conj ?yes"
chaieb@33152
  1181
  have part: "partition bound0 ?cjs = (?yes,?no)" by simp
chaieb@33152
  1182
  from partition_P[OF part] have "\<forall> q\<in> set ?yes. bound0 q" by blast 
chaieb@33152
  1183
  hence yes_nb: "bound0 ?cyes" by (simp add: list_conj_nb') 
chaieb@33152
  1184
  hence yes_qf: "qfree (decr0 ?cyes )" by (simp add: decr0_qf)
chaieb@33152
  1185
  from conjuncts_qf[OF qfp] partition_set[OF part] 
chaieb@33152
  1186
  have " \<forall>q\<in> set ?no. qfree q" by auto
chaieb@33152
  1187
  hence no_qf: "qfree ?cno"by (simp add: list_conj_qf)
chaieb@33152
  1188
  with qe have cno_qf:"qfree (qe ?cno )" 
chaieb@33152
  1189
    and noE: "Ifm vs bs (qe ?cno) = Ifm vs bs (E ?cno)" by blast+
chaieb@33152
  1190
  from cno_qf yes_qf have qf: "qfree (CJNB qe p)" 
chaieb@33152
  1191
    by (simp add: CJNB_def Let_def conj_qf split_def)
chaieb@33152
  1192
  {fix bs
chaieb@33152
  1193
    from conjuncts have "Ifm vs bs p = (\<forall>q\<in> set ?cjs. Ifm vs bs q)" by blast
chaieb@33152
  1194
    also have "\<dots> = ((\<forall>q\<in> set ?yes. Ifm vs bs q) \<and> (\<forall>q\<in> set ?no. Ifm vs bs q))"
chaieb@33152
  1195
      using partition_set[OF part] by auto
chaieb@33152
  1196
    finally have "Ifm vs bs p = ((Ifm vs bs ?cyes) \<and> (Ifm vs bs ?cno))" using list_conj[of vs bs] by simp}
chaieb@33152
  1197
  hence "Ifm vs bs (E p) = (\<exists>x. (Ifm vs (x#bs) ?cyes) \<and> (Ifm vs (x#bs) ?cno))" by simp
chaieb@33152
  1198
  also have "\<dots> = (\<exists>x. (Ifm vs (y#bs) ?cyes) \<and> (Ifm vs (x#bs) ?cno))"
chaieb@33152
  1199
    using bound0_I[OF yes_nb, where bs="bs" and b'="y"] by blast
chaieb@33152
  1200
  also have "\<dots> = (Ifm vs bs (decr0 ?cyes) \<and> Ifm vs bs (E ?cno))"
hoelzl@33639
  1201
    by (auto simp add: decr0[OF yes_nb] simp del: partition_filter_conv)
chaieb@33152
  1202
  also have "\<dots> = (Ifm vs bs (conj (decr0 ?cyes) (qe ?cno)))"
chaieb@33152
  1203
    using qe[rule_format, OF no_qf] by auto
chaieb@33152
  1204
  finally have "Ifm vs bs (E p) = Ifm vs bs (CJNB qe p)" 
chaieb@33152
  1205
    by (simp add: Let_def CJNB_def split_def)
chaieb@33152
  1206
  with qf show "qfree (CJNB qe p) \<and> Ifm vs bs (CJNB qe p) = Ifm vs bs (E p)" by blast
chaieb@33152
  1207
qed
chaieb@33152
  1208
chaieb@33152
  1209
consts simpfm :: "fm \<Rightarrow> fm"
chaieb@33152
  1210
recdef simpfm "measure fmsize"
chaieb@33152
  1211
  "simpfm (Lt t) = simplt (simptm t)"
chaieb@33152
  1212
  "simpfm (Le t) = simple (simptm t)"
chaieb@33152
  1213
  "simpfm (Eq t) = simpeq(simptm t)"
chaieb@33152
  1214
  "simpfm (NEq t) = simpneq(simptm t)"
chaieb@33152
  1215
  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
chaieb@33152
  1216
  "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
chaieb@33152
  1217
  "simpfm (Imp p q) = disj (simpfm (NOT p)) (simpfm q)"
chaieb@33152
  1218
  "simpfm (Iff p q) = disj (conj (simpfm p) (simpfm q)) (conj (simpfm (NOT p)) (simpfm (NOT q)))"
chaieb@33152
  1219
  "simpfm (NOT (And p q)) = disj (simpfm (NOT p)) (simpfm (NOT q))"
chaieb@33152
  1220
  "simpfm (NOT (Or p q)) = conj (simpfm (NOT p)) (simpfm (NOT q))"
chaieb@33152
  1221
  "simpfm (NOT (Imp p q)) = conj (simpfm p) (simpfm (NOT q))"
chaieb@33152
  1222
  "simpfm (NOT (Iff p q)) = disj (conj (simpfm p) (simpfm (NOT q))) (conj (simpfm (NOT p)) (simpfm q))"
chaieb@33152
  1223
  "simpfm (NOT (Eq t)) = simpneq t"
chaieb@33152
  1224
  "simpfm (NOT (NEq t)) = simpeq t"
chaieb@33152
  1225
  "simpfm (NOT (Le t)) = simplt (Neg t)"
chaieb@33152
  1226
  "simpfm (NOT (Lt t)) = simple (Neg t)"
chaieb@33152
  1227
  "simpfm (NOT (NOT p)) = simpfm p"
chaieb@33152
  1228
  "simpfm (NOT T) = F"
chaieb@33152
  1229
  "simpfm (NOT F) = T"
chaieb@33152
  1230
  "simpfm p = p"
chaieb@33152
  1231
chaieb@33152
  1232
lemma simpfm[simp]: "Ifm vs bs (simpfm p) = Ifm vs bs p"
chaieb@33152
  1233
by(induct p arbitrary: bs rule: simpfm.induct, auto)
chaieb@33152
  1234
haftmann@36409
  1235
lemma simpfm_bound0:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
  1236
  shows "bound0 p \<Longrightarrow> bound0 (simpfm p)"
chaieb@33152
  1237
by (induct p rule: simpfm.induct, auto)
chaieb@33152
  1238
chaieb@33152
  1239
lemma lt_qf[simp]: "qfree (lt t)"
chaieb@33152
  1240
  apply (cases t, auto simp add: lt_def)
chaieb@33152
  1241
  by (case_tac poly, auto)
chaieb@33152
  1242
chaieb@33152
  1243
lemma le_qf[simp]: "qfree (le t)"
chaieb@33152
  1244
  apply (cases t, auto simp add: le_def)
chaieb@33152
  1245
  by (case_tac poly, auto)
chaieb@33152
  1246
chaieb@33152
  1247
lemma eq_qf[simp]: "qfree (eq t)"
chaieb@33152
  1248
  apply (cases t, auto simp add: eq_def)
chaieb@33152
  1249
  by (case_tac poly, auto)
chaieb@33152
  1250
chaieb@33152
  1251
lemma neq_qf[simp]: "qfree (neq t)" by (simp add: neq_def)
chaieb@33152
  1252
chaieb@33152
  1253
lemma simplt_qf[simp]: "qfree (simplt t)" by (simp add: simplt_def Let_def split_def)
chaieb@33152
  1254
lemma simple_qf[simp]: "qfree (simple t)" by (simp add: simple_def Let_def split_def)
chaieb@33152
  1255
lemma simpeq_qf[simp]: "qfree (simpeq t)" by (simp add: simpeq_def Let_def split_def)
chaieb@33152
  1256
lemma simpneq_qf[simp]: "qfree (simpneq t)" by (simp add: simpneq_def Let_def split_def)
chaieb@33152
  1257
chaieb@33152
  1258
lemma simpfm_qf[simp]: "qfree p \<Longrightarrow> qfree (simpfm p)"
chaieb@33152
  1259
by (induct p rule: simpfm.induct, auto simp add: disj_qf imp_qf iff_qf conj_qf not_qf Let_def)
chaieb@33152
  1260
chaieb@33152
  1261
lemma disj_lin: "islin p \<Longrightarrow> islin q \<Longrightarrow> islin (disj p q)" by (simp add: disj_def)
chaieb@33152
  1262
lemma conj_lin: "islin p \<Longrightarrow> islin q \<Longrightarrow> islin (conj p q)" by (simp add: conj_def)
chaieb@33152
  1263
haftmann@36409
  1264
lemma   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
chaieb@33152
  1265
  shows "qfree p \<Longrightarrow> islin (simpfm p)" 
chaieb@33152
  1266
  apply (induct p rule: simpfm.induct)
chaieb@33152
  1267
  apply (simp_all add: conj_lin disj_lin)
chaieb@33152
  1268
  done
chaieb@33152
  1269
chaieb@33152
  1270
consts prep :: "fm \<Rightarrow> fm"
chaieb@33152
  1271
recdef prep "measure fmsize"
chaieb@33152
  1272
  "prep (E T) = T"
chaieb@33152
  1273
  "prep (E F) = F"
chaieb@33152
  1274
  "prep (E (Or p q)) = disj (prep (E p)) (prep (E q))"
chaieb@33152
  1275
  "prep (E (Imp p q)) = disj (prep (E (NOT p))) (prep (E q))"
chaieb@33152
  1276
  "prep (E (Iff p q)) = disj (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
chaieb@33152
  1277
  "prep (E (NOT (And p q))) = disj (prep (E (NOT p))) (prep (E(NOT q)))"
chaieb@33152
  1278
  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
chaieb@33152
  1279
  "prep (E (NOT (Iff p q))) = disj (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
chaieb@33152
  1280
  "prep (E p) = E (prep p)"
chaieb@33152
  1281
  "prep (A (And p q)) = conj (prep (A p)) (prep (A q))"
chaieb@33152
  1282
  "prep (A p) = prep (NOT (E (NOT p)))"
chaieb@33152
  1283
  "prep (NOT (NOT p)) = prep p"
chaieb@33152
  1284
  "prep (NOT (And p q)) = disj (prep (NOT p)) (prep (NOT q))"
chaieb@33152
  1285
  "prep (NOT (A p)) = prep (E (NOT p))"
chaieb@33152
  1286
  "prep (NOT (Or p q)) = conj (prep (NOT p)) (prep (NOT q))"
chaieb@33152
  1287
  "prep (NOT (Imp p q)) = conj (prep p) (prep (NOT q))"
chaieb@33152
  1288
  "prep (NOT (Iff p q)) = disj (prep (And p (NOT q))) (prep (And (NOT p) q))"
chaieb@33152
  1289
  "prep (NOT p) = not (prep p)"
chaieb@33152
  1290
  "prep (Or p q) = disj (prep p) (prep q)"
chaieb@33152
  1291
  "prep (And p q) = conj (prep p) (prep q)"
chaieb@33152
  1292
  "prep (Imp p q) = prep (Or (NOT p) q)"
chaieb@33152
  1293
  "prep (Iff p q) = disj (prep (And p q)) (prep (And (NOT p) (NOT q)))"
chaieb@33152
  1294
  "prep p = p"
chaieb@33152
  1295
(hints simp add: fmsize_pos)
chaieb@33152
  1296
lemma prep: "Ifm vs bs (prep p) = Ifm vs bs p"
chaieb@33152
  1297
by (induct p arbitrary: bs rule: prep.induct, auto)
chaieb@33152
  1298
chaieb@33152
  1299
chaieb@33152
  1300
chaieb@33152
  1301
  (* Generic quantifier elimination *)
krauss@41822
  1302
function (sequential) qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm" where
chaieb@33152
  1303
  "qelim (E p) = (\<lambda> qe. DJ (CJNB qe) (qelim p qe))"
krauss@41822
  1304
| "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
krauss@41822
  1305
| "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
krauss@41822
  1306
| "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
krauss@41822
  1307
| "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
krauss@41822
  1308
| "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
krauss@41822
  1309
| "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
krauss@41822
  1310
| "qelim p = (\<lambda> y. simpfm p)"
krauss@41822
  1311
by pat_completeness simp_all
krauss@41822
  1312
termination by (relation "measure fmsize") auto
chaieb@33152
  1313
chaieb@33152
  1314
lemma qelim:
chaieb@33152
  1315
  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
chaieb@33152
  1316
  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm vs bs (qelim p qe) = Ifm vs bs p)"
chaieb@33152
  1317
using qe_inv DJ_qe[OF CJNB_qe[OF qe_inv]]
chaieb@33152
  1318
by (induct p rule: qelim.induct) auto
chaieb@33152
  1319
chaieb@33152
  1320
subsection{* Core Procedure *}
chaieb@33152
  1321
krauss@41822
  1322
fun minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*) where
chaieb@33152
  1323
  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
krauss@41822
  1324
| "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
krauss@41822
  1325
| "minusinf (Eq  (CNP 0 c e)) = conj (eq (CP c)) (eq e)"
krauss@41822
  1326
| "minusinf (NEq (CNP 0 c e)) = disj (not (eq e)) (not (eq (CP c)))"
krauss@41822
  1327
| "minusinf (Lt  (CNP 0 c e)) = disj (conj (eq (CP c)) (lt e)) (lt (CP (~\<^sub>p c)))"
krauss@41822
  1328
| "minusinf (Le  (CNP 0 c e)) = disj (conj (eq (CP c)) (le e)) (lt (CP (~\<^sub>p c)))"
krauss@41822
  1329
| "minusinf p = p"
chaieb@33152
  1330
krauss@41822
  1331
fun plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*) where
chaieb@33152
  1332
  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
krauss@41822
  1333
| "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
krauss@41822
  1334
| "plusinf (Eq  (CNP 0 c e)) = conj (eq (CP c)) (eq e)"
krauss@41822
  1335
| "plusinf (NEq (CNP 0 c e)) = disj (not (eq e)) (not (eq (CP c)))"
krauss@41822
  1336
| "plusinf (Lt  (CNP 0 c e)) = disj (conj (eq (CP c)) (lt e)) (lt (CP c))"
krauss@41822
  1337
| "plusinf (Le  (CNP 0 c e)) = disj (conj (eq (CP c)) (le e)) (lt (CP c))"
krauss@41822
  1338
| "plusinf p = p"
chaieb@33152
  1339
chaieb@33152
  1340
lemma minusinf_inf: assumes lp:"islin p"
chaieb@33152
  1341
  shows "\<exists>z. \<forall>x < z. Ifm vs (x#bs) (minusinf p) \<longleftrightarrow> Ifm vs (x#bs) p"
chaieb@33152
  1342
  using lp
chaieb@33152
  1343
proof (induct p rule: minusinf.induct)
chaieb@33152
  1344
  case 1 thus ?case by (auto,rule_tac x="min z za" in exI, auto)
chaieb@33152
  1345
next
chaieb@33152
  1346
  case 2 thus ?case by (auto,rule_tac x="min z za" in exI, auto)
chaieb@33152
  1347
next
chaieb@33152
  1348
  case (3 c e) hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1349
  from 3 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1350
  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
chaieb@33152
  1351
  let ?c = "Ipoly vs c"
chaieb@33152
  1352
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1353
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1354
  moreover {assume "?c = 0" hence ?case 
chaieb@33152
  1355
      using eq[OF nc(2), of vs] eq[OF nc(1), of vs] by auto}
chaieb@33152
  1356
  moreover {assume cp: "?c > 0"
chaieb@33152
  1357
    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1358
        using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1359
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1360
      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Eq (CNP 0 c e)))"
wenzelm@33268
  1361
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x" and vs=vs and bs=bs] by auto} hence ?case by auto}
chaieb@33152
  1362
  moreover {assume cp: "?c < 0"
chaieb@33152
  1363
    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
wenzelm@33268
  1364
        using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1365
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1366
      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Eq (CNP 0 c e)))"
wenzelm@33268
  1367
        using tmbound0_I[OF nbe, where b="y" and b'="x"] eqs by auto} hence ?case by auto}
chaieb@33152
  1368
  ultimately show ?case by blast
chaieb@33152
  1369
next
chaieb@33152
  1370
  case (4 c e)  hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1371
  from 4 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1372
  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
chaieb@33152
  1373
  let ?c = "Ipoly vs c"
chaieb@33152
  1374
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1375
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1376
  moreover {assume "?c = 0" hence ?case using eqs by auto}
chaieb@33152
  1377
  moreover {assume cp: "?c > 0"
chaieb@33152
  1378
    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1379
        using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1380
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1381
      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (NEq (CNP 0 c e)))"
wenzelm@33268
  1382
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
chaieb@33152
  1383
  moreover {assume cp: "?c < 0"
chaieb@33152
  1384
    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
wenzelm@33268
  1385
        using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1386
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1387
      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (NEq (CNP 0 c e)))"
wenzelm@33268
  1388
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
chaieb@33152
  1389
  ultimately show ?case by blast
chaieb@33152
  1390
next
chaieb@33152
  1391
  case (5 c e)  hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1392
  from 5 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1393
  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
chaieb@33152
  1394
  note eqs = lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] lt[OF nc(2), where ?'a = 'a]
chaieb@33152
  1395
  let ?c = "Ipoly vs c"
chaieb@33152
  1396
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1397
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1398
  moreover {assume "?c = 0" hence ?case using eqs by auto}
chaieb@33152
  1399
  moreover {assume cp: "?c > 0"
chaieb@33152
  1400
    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1401
        using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1402
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1403
      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Lt (CNP 0 c e)))"
wenzelm@33268
  1404
        using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
chaieb@33152
  1405
  moreover {assume cp: "?c < 0"
chaieb@33152
  1406
    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
wenzelm@33268
  1407
        using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1408
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1409
      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Lt (CNP 0 c e)))"
wenzelm@33268
  1410
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] cp by auto} hence ?case by auto}
chaieb@33152
  1411
  ultimately show ?case by blast
chaieb@33152
  1412
next
chaieb@33152
  1413
  case (6 c e)  hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1414
  from 6 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1415
  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
chaieb@33152
  1416
  note eqs = lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] le[OF nc(2), where ?'a = 'a]
chaieb@33152
  1417
  let ?c = "Ipoly vs c"
chaieb@33152
  1418
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1419
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1420
  moreover {assume "?c = 0" hence ?case using eqs by auto}
chaieb@33152
  1421
  moreover {assume cp: "?c > 0"
chaieb@33152
  1422
    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1423
        using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1424
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1425
      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Le (CNP 0 c e)))"
wenzelm@33268
  1426
        using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
chaieb@33152
  1427
  moreover {assume cp: "?c < 0"
chaieb@33152
  1428
    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
wenzelm@33268
  1429
        using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1430
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1431
      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Le (CNP 0 c e)))"
wenzelm@33268
  1432
        using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
chaieb@33152
  1433
  ultimately show ?case by blast
chaieb@33152
  1434
qed (auto)
chaieb@33152
  1435
chaieb@33152
  1436
lemma plusinf_inf: assumes lp:"islin p"
chaieb@33152
  1437
  shows "\<exists>z. \<forall>x > z. Ifm vs (x#bs) (plusinf p) \<longleftrightarrow> Ifm vs (x#bs) p"
chaieb@33152
  1438
  using lp
chaieb@33152
  1439
proof (induct p rule: plusinf.induct)
chaieb@33152
  1440
  case 1 thus ?case by (auto,rule_tac x="max z za" in exI, auto)
chaieb@33152
  1441
next
chaieb@33152
  1442
  case 2 thus ?case by (auto,rule_tac x="max z za" in exI, auto)
chaieb@33152
  1443
next
chaieb@33152
  1444
  case (3 c e) hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1445
  from 3 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1446
  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
chaieb@33152
  1447
  let ?c = "Ipoly vs c"
chaieb@33152
  1448
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1449
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1450
  moreover {assume "?c = 0" hence ?case 
chaieb@33152
  1451
      using eq[OF nc(2), of vs] eq[OF nc(1), of vs] by auto}
chaieb@33152
  1452
  moreover {assume cp: "?c > 0"
chaieb@33152
  1453
    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e" 
wenzelm@33268
  1454
        using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1455
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1456
      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Eq (CNP 0 c e)))"
wenzelm@33268
  1457
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x" and vs=vs and bs=bs] by auto} hence ?case by auto}
chaieb@33152
  1458
  moreover {assume cp: "?c < 0"
chaieb@33152
  1459
    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1460
        using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1461
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1462
      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Eq (CNP 0 c e)))"
wenzelm@33268
  1463
        using tmbound0_I[OF nbe, where b="y" and b'="x"] eqs by auto} hence ?case by auto}
chaieb@33152
  1464
  ultimately show ?case by blast
chaieb@33152
  1465
next
wenzelm@41807
  1466
  case (4 c e) hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1467
  from 4 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1468
  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
chaieb@33152
  1469
  let ?c = "Ipoly vs c"
chaieb@33152
  1470
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1471
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1472
  moreover {assume "?c = 0" hence ?case using eqs by auto}
chaieb@33152
  1473
  moreover {assume cp: "?c > 0"
chaieb@33152
  1474
    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
wenzelm@33268
  1475
        using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1476
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1477
      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (NEq (CNP 0 c e)))"
wenzelm@33268
  1478
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
chaieb@33152
  1479
  moreover {assume cp: "?c < 0"
chaieb@33152
  1480
    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1481
        using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1482
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1483
      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (NEq (CNP 0 c e)))"
wenzelm@33268
  1484
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
chaieb@33152
  1485
  ultimately show ?case by blast
chaieb@33152
  1486
next
wenzelm@41807
  1487
  case (5 c e) hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1488
  from 5 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1489
  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
chaieb@33152
  1490
  note eqs = lt[OF nc(1), where ?'a = 'a] lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] lt[OF nc(2), where ?'a = 'a]
chaieb@33152
  1491
  let ?c = "Ipoly vs c"
chaieb@33152
  1492
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1493
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1494
  moreover {assume "?c = 0" hence ?case using eqs by auto}
chaieb@33152
  1495
  moreover {assume cp: "?c > 0"
chaieb@33152
  1496
    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
wenzelm@33268
  1497
        using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1498
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1499
      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Lt (CNP 0 c e)))"
wenzelm@33268
  1500
        using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
chaieb@33152
  1501
  moreover {assume cp: "?c < 0"
chaieb@33152
  1502
    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1503
        using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1504
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1505
      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Lt (CNP 0 c e)))"
wenzelm@33268
  1506
        using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] cp by auto} hence ?case by auto}
chaieb@33152
  1507
  ultimately show ?case by blast
chaieb@33152
  1508
next
chaieb@33152
  1509
  case (6 c e)  hence nbe: "tmbound0 e" by simp
wenzelm@41807
  1510
  from 6 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
chaieb@33152
  1511
  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
chaieb@33152
  1512
  note eqs = lt[OF nc(1), where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] le[OF nc(2), where ?'a = 'a]
chaieb@33152
  1513
  let ?c = "Ipoly vs c"
chaieb@33152
  1514
  let ?e = "Itm vs (y#bs) e"
chaieb@33152
  1515
  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
chaieb@33152
  1516
  moreover {assume "?c = 0" hence ?case using eqs by auto}
chaieb@33152
  1517
  moreover {assume cp: "?c > 0"
chaieb@33152
  1518
    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
wenzelm@33268
  1519
        using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1520
      hence "?c * x + ?e > 0" by simp
chaieb@33152
  1521
      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Le (CNP 0 c e)))"
wenzelm@33268
  1522
        using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
chaieb@33152
  1523
  moreover {assume cp: "?c < 0"
chaieb@33152
  1524
    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
wenzelm@33268
  1525
        using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
chaieb@33152
  1526
      hence "?c * x + ?e < 0" by simp
chaieb@33152
  1527
      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Le (CNP 0 c e)))"
wenzelm@33268
  1528
        using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
chaieb@33152
  1529
  ultimately show ?case by blast
chaieb@33152
  1530
qed (auto)
chaieb@33152
  1531
chaieb@33152
  1532
lemma minusinf_nb: "islin p \<Longrightarrow> bound0 (minusinf p)" 
chaieb@33152
  1533
  by (induct p rule: minusinf.induct, auto simp add: eq_nb lt_nb le_nb)
chaieb@33152
  1534
lemma plusinf_nb: "islin p \<Longrightarrow> bound0 (plusinf p)" 
chaieb@33152
  1535
  by (induct p rule: minusinf.induct, auto simp add: eq_nb lt_nb le_nb)
chaieb@33152
  1536
chaieb@33152
  1537
lemma minusinf_ex: assumes lp: "islin p" and ex: "Ifm vs (x#bs) (minusinf p)"
chaieb@33152
  1538
  shows "\<exists>x. Ifm vs (x#bs) p"
chaieb@33152
  1539
proof-
chaieb@33152
  1540
  from bound0_I [OF minusinf_nb[OF lp], where b="a" and bs ="bs"] ex
chaieb@33152
  1541
  have th: "\<forall> x. Ifm vs (x#bs) (minusinf p)" by auto
chaieb@33152
  1542
  from minusinf_inf[OF lp, where bs="bs"] 
chaieb@33152
  1543
  obtain z where z_def: "\<forall>x<z. Ifm vs (x # bs) (minusinf p) = Ifm vs (x # bs) p" by blast
chaieb@33152
  1544
  from th have "Ifm vs ((z - 1)#bs) (minusinf p)" by simp
chaieb@33152
  1545
  moreover have "z - 1 < z" by simp
chaieb@33152
  1546
  ultimately show ?thesis using z_def by auto
chaieb@33152
  1547
qed
chaieb@33152
  1548
chaieb@33152
  1549
lemma plusinf_ex: assumes lp: "islin p" and ex: "Ifm vs (x#bs) (plusinf p)"
chaieb@33152
  1550
  shows "\<exists>x. Ifm vs (x#bs) p"
chaieb@33152
  1551
proof-
chaieb@33152
  1552
  from bound0_I [OF plusinf_nb[OF lp], where b="a" and bs ="bs"] ex
chaieb@33152
  1553
  have th: "\<forall> x. Ifm vs (x#bs) (plusinf p)" by auto
chaieb@33152
  1554
  from plusinf_inf[OF lp, where bs="bs"] 
chaieb@33152
  1555
  obtain z where z_def: "\<forall>x>z. Ifm vs (x # bs) (plusinf p) = Ifm vs (x # bs) p" by blast
chaieb@33152
  1556
  from th have "Ifm vs ((z + 1)#bs) (plusinf p)" by simp
chaieb@33152
  1557
  moreover have "z + 1 > z" by simp
chaieb@33152
  1558
  ultimately show ?thesis using z_def by auto
chaieb@33152
  1559
qed
chaieb@33152
  1560
chaieb@33152
  1561
fun uset :: "fm \<Rightarrow> (poly \<times> tm) list" where
chaieb@33152
  1562
  "uset (And p q) = uset p @ uset q"
chaieb@33152
  1563
| "uset (Or p q) = uset p @ uset q"
chaieb@33152
  1564
| "uset (Eq (CNP 0 a e))  = [(a,e)]"
chaieb@33152
  1565
| "uset (Le (CNP 0 a e))  = [(a,e)]"
chaieb@33152
  1566
| "uset (Lt (CNP 0 a e))  = [(a,e)]"
chaieb@33152
  1567
| "uset (NEq (CNP 0 a e)) = [(a,e)]"
chaieb@33152
  1568
| "uset p = []"
chaieb@33152
  1569
chaieb@33152
  1570
lemma uset_l:
chaieb@33152
  1571
  assumes lp: "islin p"
chaieb@33152
  1572
  shows "\<forall> (c,s) \<in> set (uset p). isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s"
chaieb@33152
  1573
using lp by(induct p rule: uset.induct,auto)
chaieb@33152
  1574
chaieb@33152
  1575
lemma minusinf_uset0:
chaieb@33152
  1576
  assumes lp: "islin p"
chaieb@33152
  1577
  and nmi: "\<not> (Ifm vs (x#bs) (minusinf p))"
chaieb@33152
  1578
  and ex: "Ifm vs (x#bs) p" (is "?I x p")
chaieb@33152
  1579
  shows "\<exists> (c,s) \<in> set (uset p). x \<ge> - Itm vs (x#bs) s / Ipoly vs c" 
chaieb@33152
  1580
proof-
chaieb@33152
  1581
  have "\<exists> (c,s) \<in> set (uset p). (Ipoly vs c < 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s)" 
chaieb@33152
  1582
    using lp nmi ex
nipkow@41842
  1583
    apply (induct p rule: minusinf.induct, auto simp add: eq le lt polyneg_norm)
chaieb@33152
  1584
    apply (auto simp add: linorder_not_less order_le_less)
chaieb@33152
  1585
    done 
chaieb@33152
  1586
  then obtain c s where csU: "(c,s) \<in> set (uset p)" and x: "(Ipoly vs c < 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s)" by blast
chaieb@33152
  1587
  hence "x \<ge> (- Itm vs (x#bs) s) / Ipoly vs c"
chaieb@33152
  1588
    using divide_le_eq[of "- Itm vs (x#bs) s" "Ipoly vs c" x]
haftmann@50045
  1589
      by (auto simp add: mult_commute)
chaieb@33152
  1590
  thus ?thesis using csU by auto
chaieb@33152
  1591
qed
chaieb@33152
  1592
chaieb@33152
  1593
lemma minusinf_uset:
chaieb@33152
  1594
  assumes lp: "islin p"
chaieb@33152
  1595
  and nmi: "\<not> (Ifm vs (a#bs) (minusinf p))"
chaieb@33152
  1596
  and ex: "Ifm vs (x#bs) p" (is "?I x p")
chaieb@33152
  1597
  shows "\<exists> (c,s) \<in> set (uset p). x \<ge> - Itm vs (a#bs) s / Ipoly vs c" 
chaieb@33152
  1598
proof-
chaieb@33152
  1599
  from nmi have nmi': "\<not> (Ifm vs (x#bs) (minusinf p))" 
chaieb@33152
  1600
    by (simp add: bound0_I[OF minusinf_nb[OF lp], where b=x and b'=a])
chaieb@33152
  1601
  from minusinf_uset0[OF lp nmi' ex] 
chaieb@33152
  1602
  obtain c s where csU: "(c,s) \<in> set (uset p)" and th: "x \<ge> - Itm vs (x#bs) s / Ipoly vs c" by blast
chaieb@33152
  1603
  from uset_l[OF lp, rule_format, OF csU] have nb: "tmbound0 s" by simp
chaieb@33152
  1604
  from th tmbound0_I[OF nb, of vs x bs a] csU show ?thesis by auto
chaieb@33152
  1605
qed
chaieb@33152
  1606
chaieb@33152
  1607
chaieb@33152
  1608
lemma plusinf_uset0:
chaieb@33152
  1609
  assumes lp: "islin p"
chaieb@33152
  1610
  and nmi: "\<not> (Ifm vs (x#bs) (plusinf p))"
chaieb@33152
  1611
  and ex: "Ifm vs (x#bs) p" (is "?I x p")
chaieb@33152
  1612
  shows "\<exists> (c,s) \<in> set (uset p). x \<le> - Itm vs (x#bs) s / Ipoly vs c" 
chaieb@33152
  1613
proof-
chaieb@33152
  1614
  have "\<exists> (c,s) \<in> set (uset p). (Ipoly vs c < 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s)" 
chaieb@33152
  1615
    using lp nmi ex
nipkow@41842
  1616
    apply (induct p rule: minusinf.induct, auto simp add: eq le lt polyneg_norm)
chaieb@33152
  1617
    apply (auto simp add: linorder_not_less order_le_less)
chaieb@33152
  1618
    done 
chaieb@33152
  1619
  then obtain c s where csU: "(c,s) \<in> set (uset p)" and x: "(Ipoly vs c < 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s)" by blast
chaieb@33152
  1620
  hence "x \<le> (- Itm vs (x#bs) s) / Ipoly vs c"
chaieb@33152
  1621
    using le_divide_eq[of x "- Itm vs (x#bs) s" "Ipoly vs c"]
chaieb@33152
  1622
    by (auto simp add: mult_commute del: divide_minus_left)
chaieb@33152
  1623
  thus ?thesis using csU by auto
chaieb@33152
  1624
qed
chaieb@33152
  1625
chaieb@33152
  1626
lemma plusinf_uset:
chaieb@33152
  1627
  assumes lp: "islin p"
chaieb@33152
  1628
  and nmi: "\<not> (Ifm vs (a#bs) (plusinf p))"
chaieb@33152
  1629
  and ex: "Ifm vs (x#bs) p" (is "?I x p")
chaieb@33152
  1630
  shows "\<exists> (c,s) \<in> set (uset p). x \<le> - Itm vs (a#bs) s / Ipoly vs c" 
chaieb@33152
  1631
proof-
chaieb@33152
  1632
  from nmi have nmi': "\<not> (Ifm vs (x#bs) (plusinf p))" 
chaieb@33152
  1633
    by (simp add: bound0_I[OF plusinf_nb[OF lp], where b=x and b'=a])
chaieb@33152
  1634
  from plusinf_uset0[OF lp nmi' ex] 
chaieb@33152
  1635
  obtain c s where csU: "(c,s) \<in> set (uset p)" and th: "x \<le> - Itm vs (x#bs) s / Ipoly vs c" by blast
chaieb@33152
  1636
  from uset_l[OF lp, rule_format, OF csU] have nb: "tmbound0 s" by simp
chaieb@33152
  1637
  from th tmbound0_I[OF nb, of vs x bs a] csU show ?thesis by auto
chaieb@33152
  1638
qed
chaieb@33152
  1639
chaieb@33152
  1640
lemma lin_dense: 
chaieb@33152
  1641
  assumes lp: "islin p"
chaieb@33152
  1642
  and noS: "\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> (\<lambda> (c,t). - Itm vs (x#bs) t / Ipoly vs c) ` set (uset p)" 
chaieb@33152
  1643
  (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (c,t). - ?Nt x t / ?N c) ` ?U p")
chaieb@33152
  1644
  and lx: "l < x" and xu:"x < u" and px:" Ifm vs (x#bs) p"
chaieb@33152
  1645
  and ly: "l < y" and yu: "y < u"
chaieb@33152
  1646
  shows "Ifm vs (y#bs) p"
chaieb@33152
  1647
using lp px noS
chaieb@33152
  1648
proof (induct p rule: islin.induct) 
chaieb@33152
  1649
  case (5 c s)
chaieb@33152
  1650
  from "5.prems" 
chaieb@33152
  1651
  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
chaieb@33152
  1652
    and px: "Ifm vs (x # bs) (Lt (CNP 0 c s))"
chaieb@33152
  1653
    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
chaieb@33152
  1654
  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
chaieb@33152
  1655
  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
chaieb@33152
  1656
  have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
chaieb@33152
  1657
  moreover
chaieb@33152
  1658
  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
chaieb@33152
  1659
  moreover
chaieb@33152
  1660
  {assume c: "?N c > 0"
chaieb@33152
  1661
      from px pos_less_divide_eq[OF c, where a="x" and b="-?Nt x s"]  
chaieb@33152
  1662
      have px': "x < - ?Nt x s / ?N c" 
haftmann@36348
  1663
        by (auto simp add: not_less field_simps) 
chaieb@33152
  1664
    {assume y: "y < - ?Nt x s / ?N c" 
chaieb@33152
  1665
      hence "y * ?N c < - ?Nt x s"
wenzelm@33268
  1666
        by (simp add: pos_less_divide_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
haftmann@36348
  1667
      hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
chaieb@33152
  1668
      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
chaieb@33152
  1669
    moreover
chaieb@33152
  1670
    {assume y: "y > -?Nt x s / ?N c" 
chaieb@33152
  1671
      with yu have eu: "u > - ?Nt x s / ?N c" by auto
chaieb@33152
  1672
      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
chaieb@33152
  1673
      with lx px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1674
    ultimately have ?case using ycs by blast
chaieb@33152
  1675
  }
chaieb@33152
  1676
  moreover
chaieb@33152
  1677
  {assume c: "?N c < 0"
chaieb@33152
  1678
      from px neg_divide_less_eq[OF c, where a="x" and b="-?Nt x s"]  
chaieb@33152
  1679
      have px': "x > - ?Nt x s / ?N c" 
haftmann@36348
  1680
        by (auto simp add: not_less field_simps) 
chaieb@33152
  1681
    {assume y: "y > - ?Nt x s / ?N c" 
chaieb@33152
  1682
      hence "y * ?N c < - ?Nt x s"
wenzelm@33268
  1683
        by (simp add: neg_divide_less_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
haftmann@36348
  1684
      hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
chaieb@33152
  1685
      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
chaieb@33152
  1686
    moreover
chaieb@33152
  1687
    {assume y: "y < -?Nt x s / ?N c" 
chaieb@33152
  1688
      with ly have eu: "l < - ?Nt x s / ?N c" by auto
chaieb@33152
  1689
      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
chaieb@33152
  1690
      with xu px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1691
    ultimately have ?case using ycs by blast
chaieb@33152
  1692
  }
chaieb@33152
  1693
  ultimately show ?case by blast
chaieb@33152
  1694
next
chaieb@33152
  1695
  case (6 c s)
chaieb@33152
  1696
  from "6.prems" 
chaieb@33152
  1697
  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
chaieb@33152
  1698
    and px: "Ifm vs (x # bs) (Le (CNP 0 c s))"
chaieb@33152
  1699
    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
chaieb@33152
  1700
  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
chaieb@33152
  1701
  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
chaieb@33152
  1702
  have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
chaieb@33152
  1703
  moreover
chaieb@33152
  1704
  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
chaieb@33152
  1705
  moreover
chaieb@33152
  1706
  {assume c: "?N c > 0"
chaieb@33152
  1707
      from px pos_le_divide_eq[OF c, where a="x" and b="-?Nt x s"]  
haftmann@36348
  1708
      have px': "x <= - ?Nt x s / ?N c" by (simp add: not_less field_simps) 
chaieb@33152
  1709
    {assume y: "y < - ?Nt x s / ?N c" 
chaieb@33152
  1710
      hence "y * ?N c < - ?Nt x s"
wenzelm@33268
  1711
        by (simp add: pos_less_divide_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
haftmann@36348
  1712
      hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
chaieb@33152
  1713
      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
chaieb@33152
  1714
    moreover
chaieb@33152
  1715
    {assume y: "y > -?Nt x s / ?N c" 
chaieb@33152
  1716
      with yu have eu: "u > - ?Nt x s / ?N c" by auto
chaieb@33152
  1717
      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
chaieb@33152
  1718
      with lx px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1719
    ultimately have ?case using ycs by blast
chaieb@33152
  1720
  }
chaieb@33152
  1721
  moreover
chaieb@33152
  1722
  {assume c: "?N c < 0"
chaieb@33152
  1723
      from px neg_divide_le_eq[OF c, where a="x" and b="-?Nt x s"]  
haftmann@36348
  1724
      have px': "x >= - ?Nt x s / ?N c" by (simp add: field_simps) 
chaieb@33152
  1725
    {assume y: "y > - ?Nt x s / ?N c" 
chaieb@33152
  1726
      hence "y * ?N c < - ?Nt x s"
wenzelm@33268
  1727
        by (simp add: neg_divide_less_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
haftmann@36348
  1728
      hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
chaieb@33152
  1729
      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
chaieb@33152
  1730
    moreover
chaieb@33152
  1731
    {assume y: "y < -?Nt x s / ?N c" 
chaieb@33152
  1732
      with ly have eu: "l < - ?Nt x s / ?N c" by auto
chaieb@33152
  1733
      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
chaieb@33152
  1734
      with xu px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1735
    ultimately have ?case using ycs by blast
chaieb@33152
  1736
  }
chaieb@33152
  1737
  ultimately show ?case by blast
chaieb@33152
  1738
next
chaieb@33152
  1739
    case (3 c s)
chaieb@33152
  1740
  from "3.prems" 
chaieb@33152
  1741
  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
chaieb@33152
  1742
    and px: "Ifm vs (x # bs) (Eq (CNP 0 c s))"
chaieb@33152
  1743
    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
chaieb@33152
  1744
  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
chaieb@33152
  1745
  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
chaieb@33152
  1746
  have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
chaieb@33152
  1747
  moreover
chaieb@33152
  1748
  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
chaieb@33152
  1749
  moreover
chaieb@33152
  1750
  {assume c: "?N c > 0" hence cnz: "?N c \<noteq> 0" by simp
chaieb@33152
  1751
    from px eq_divide_eq[of "x" "-?Nt x s" "?N c"]  cnz
haftmann@36348
  1752
    have px': "x = - ?Nt x s / ?N c" by (simp add: field_simps)
chaieb@33152
  1753
    {assume y: "y < -?Nt x s / ?N c" 
chaieb@33152
  1754
      with ly have eu: "l < - ?Nt x s / ?N c" by auto
chaieb@33152
  1755
      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
chaieb@33152
  1756
      with xu px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1757
    moreover
chaieb@33152
  1758
    {assume y: "y > -?Nt x s / ?N c" 
chaieb@33152
  1759
      with yu have eu: "u > - ?Nt x s / ?N c" by auto
chaieb@33152
  1760
      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
chaieb@33152
  1761
      with lx px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1762
    ultimately have ?case using ycs by blast
chaieb@33152
  1763
  }
chaieb@33152
  1764
  moreover
chaieb@33152
  1765
  {assume c: "?N c < 0" hence cnz: "?N c \<noteq> 0" by simp
chaieb@33152
  1766
    from px eq_divide_eq[of "x" "-?Nt x s" "?N c"]  cnz
haftmann@36348
  1767
    have px': "x = - ?Nt x s / ?N c" by (simp add: field_simps)
chaieb@33152
  1768
    {assume y: "y < -?Nt x s / ?N c" 
chaieb@33152
  1769
      with ly have eu: "l < - ?Nt x s / ?N c" by auto
chaieb@33152
  1770
      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
chaieb@33152
  1771
      with xu px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1772
    moreover
chaieb@33152
  1773
    {assume y: "y > -?Nt x s / ?N c" 
chaieb@33152
  1774
      with yu have eu: "u > - ?Nt x s / ?N c" by auto
chaieb@33152
  1775
      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
chaieb@33152
  1776
      with lx px' have "False" by simp  hence ?case by simp }
chaieb@33152
  1777
    ultimately have ?case using ycs by blast
chaieb@33152
  1778
  }
chaieb@33152
  1779
  ultimately show ?case by blast
chaieb@33152
  1780
next
chaieb@33152
  1781
    case (4 c s)
chaieb@33152
  1782
  from "4.prems" 
chaieb@33152
  1783
  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
chaieb@33152
  1784
    and px: "Ifm vs (x # bs) (NEq (CNP 0 c s))"
chaieb@33152
  1785
    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
chaieb@33152
  1786
  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
chaieb@33152
  1787
  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
chaieb@33152
  1788
  have ccs: "?N c = 0 \<or> ?N c \<noteq> 0" by dlo
chaieb@33152
  1789
  moreover
chaieb@33152
  1790
  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
chaieb@33152
  1791
  moreover
chaieb@33152
  1792
  {assume c: "?N c \<noteq> 0"
chaieb@33152
  1793
    from yne c eq_divide_eq[of "y" "- ?Nt x s" "?N c"] have ?case
haftmann@36348
  1794
      by (simp add: field_simps tmbound0_I[OF lin(3), of vs x bs y] sum_eq[symmetric]) }
chaieb@33152
  1795
  ultimately show ?case by blast
nipkow@41842
  1796
qed (auto simp add: tmbound0_I[where vs=vs and bs="bs" and b="y" and b'="x"] bound0_I[where vs=vs and bs="bs" and b="y" and b'="x"])
chaieb@33152
  1797
chaieb@33152
  1798
lemma inf_uset:
chaieb@33152
  1799
  assumes lp: "islin p"
chaieb@33152
  1800
  and nmi: "\<not> (Ifm vs (x#bs) (minusinf p))" (is "\<not> (Ifm vs (x#bs) (?M p))")
chaieb@33152
  1801
  and npi: "\<not> (Ifm vs (x#bs) (plusinf p))" (is "\<not> (Ifm vs (x#bs) (?P p))")
chaieb@33152
  1802
  and ex: "\<exists> x.  Ifm vs (x#bs) p" (is "\<exists> x. ?I x p")
huffman@45499
  1803
  shows "\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). ?I ((- Itm vs (x#bs) t / Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) / 2) p" 
chaieb@33152
  1804
proof-
chaieb@33152
  1805
  let ?Nt = "\<lambda> x t. Itm vs (x#bs) t"
chaieb@33152
  1806
  let ?N = "Ipoly vs"
chaieb@33152
  1807
  let ?U = "set (uset p)"
chaieb@33152
  1808
  from ex obtain a where pa: "?I a p" by blast
chaieb@33152
  1809
  from bound0_I[OF minusinf_nb[OF lp], where bs="bs" and b="x" and b'="a"] nmi
chaieb@33152
  1810
  have nmi': "\<not> (?I a (?M p))" by simp
chaieb@33152
  1811
  from bound0_I[OF plusinf_nb[OF lp], where bs="bs" and b="x" and b'="a"] npi
chaieb@33152
  1812
  have npi': "\<not> (?I a (?P p))" by simp
huffman@45499
  1813
  have "\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). ?I ((- ?Nt a t/?N c + - ?Nt a s /?N d) / 2) p"
chaieb@33152
  1814
  proof-
chaieb@33152
  1815
    let ?M = "(\<lambda> (c,t). - ?Nt a t / ?N c) ` ?U"
chaieb@33152
  1816
    have fM: "finite ?M" by auto
chaieb@33152
  1817
    from minusinf_uset[OF lp nmi pa] plusinf_uset[OF lp npi pa] 
chaieb@33152
  1818
    have "\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). a \<le> - ?Nt x t / ?N c \<and> a \<ge> - ?Nt x s / ?N d" by blast
chaieb@33152
  1819
    then obtain "c" "t" "d" "s" where 
chaieb@33152
  1820
      ctU: "(c,t) \<in> ?U" and dsU: "(d,s) \<in> ?U" 
chaieb@33152
  1821
      and xs1: "a \<le> - ?Nt x s / ?N d" and tx1: "a \<ge> - ?Nt x t / ?N c" by blast
chaieb@33152
  1822
    from uset_l[OF lp] ctU dsU tmbound0_I[where bs="bs" and b="x" and b'="a"] xs1 tx1 
chaieb@33152
  1823
    have xs: "a \<le> - ?Nt a s / ?N d" and tx: "a \<ge> - ?Nt a t / ?N c" by auto
chaieb@33152
  1824
    from ctU have Mne: "?M \<noteq> {}" by auto
chaieb@33152
  1825
    hence Une: "?U \<noteq> {}" by simp
chaieb@33152
  1826
    let ?l = "Min ?M"
chaieb@33152
  1827
    let ?u = "Max ?M"
chaieb@33152
  1828
    have linM: "?l \<in> ?M" using fM Mne by simp
chaieb@33152
  1829
    have uinM: "?u \<in> ?M" using fM Mne by simp
chaieb@33152
  1830
    have ctM: "- ?Nt a t / ?N c \<in> ?M" using ctU by auto
chaieb@33152
  1831
    have dsM: "- ?Nt a s / ?N d \<in> ?M" using dsU by auto 
chaieb@33152
  1832
    have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
chaieb@33152
  1833
    have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
chaieb@33152
  1834
    have "?l \<le> - ?Nt a t / ?N c" using ctM Mne by simp hence lx: "?l \<le> a" using tx by simp
chaieb@33152
  1835
    have "- ?Nt a s / ?N d \<le> ?u" using dsM Mne by simp hence xu: "a \<le> ?u" using xs by simp
chaieb@33152
  1836
    from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
chaieb@33152
  1837
    have "(\<exists> s\<in> ?M. ?I s p) \<or> 
chaieb@33152
  1838
      (\<exists> t1\<in> ?M. \<exists> t2 \<in> ?M. (\<forall> y. t1 < y \<and> y < t2 \<longrightarrow> y \<notin> ?M) \<and> t1 < a \<and> a < t2 \<and> ?I a p)" .
chaieb@33152
  1839
    moreover {fix u assume um: "u\<in> ?M" and pu: "?I u p"
chaieb@33152
  1840
      hence "\<exists> (nu,tu) \<in> ?U. u = - ?Nt a tu / ?N nu" by auto
chaieb@33152
  1841
      then obtain "tu" "nu" where tuU: "(nu,tu) \<in> ?U" and tuu:"u= - ?Nt a tu / ?N nu" by blast
huffman@45499
  1842
      from pu tuu
huffman@45499
  1843
      have "?I (((- ?Nt a tu / ?N nu) + (- ?Nt a tu / ?N nu)) / 2) p" by simp
chaieb@33152
  1844
      with tuU have ?thesis by blast}
chaieb@33152
  1845
    moreover{
chaieb@33152
  1846
      assume "\<exists> t1\<in> ?M. \<exists> t2 \<in> ?M. (\<forall> y. t1 < y \<and> y < t2 \<longrightarrow> y \<notin> ?M) \<and> t1 < a \<and> a < t2 \<and> ?I a p"
chaieb@33152
  1847
      then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M" 
wenzelm@33268
  1848
        and noM: "\<forall> y. t1 < y \<and> y < t2 \<longrightarrow> y \<notin> ?M" and t1x: "t1 < a" and xt2: "a < t2" and px: "?I a p"
wenzelm@33268
  1849
        by blast
chaieb@33152
  1850
      from t1M have "\<exists> (t1n,t1u) \<in> ?U. t1 = - ?Nt a t1u / ?N t1n" by auto
chaieb@33152
  1851
      then obtain "t1u" "t1n" where t1uU: "(t1n,t1u) \<in> ?U" and t1u: "t1 = - ?Nt a t1u / ?N t1n" by blast
chaieb@33152
  1852
      from t2M have "\<exists> (t2n,t2u) \<in> ?U. t2 = - ?Nt a t2u / ?N t2n" by auto
chaieb@33152
  1853
      then obtain "t2u" "t2n" where t2uU: "(t2n,t2u) \<in> ?U" and t2u: "t2 = - ?Nt a t2u / ?N t2n" by blast
chaieb@33152
  1854
      from t1x xt2 have t1t2: "t1 < t2" by simp
huffman@45499
  1855
      let ?u = "(t1 + t2) / 2"
chaieb@33152
  1856
      from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
chaieb@33152
  1857
      from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
chaieb@33152
  1858
      with t1uU t2uU t1u t2u have ?thesis by blast}
chaieb@33152
  1859
    ultimately show ?thesis by blast
chaieb@33152
  1860
  qed
chaieb@33152
  1861
  then obtain "l" "n" "s"  "m" where lnU: "(n,l) \<in> ?U" and smU:"(m,s) \<in> ?U" 
huffman@45499
  1862
    and pu: "?I ((- ?Nt a l / ?N n + - ?Nt a s / ?N m) / 2) p" by blast
chaieb@33152
  1863
  from lnU smU uset_l[OF lp] have nbl: "tmbound0 l" and nbs: "tmbound0 s" by auto
chaieb@33152
  1864
  from tmbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"] 
chaieb@33152
  1865
    tmbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
huffman@45499
  1866
  have "?I ((- ?Nt x l / ?N n + - ?Nt x s / ?N m) / 2) p" by simp
chaieb@33152
  1867
  with lnU smU
chaieb@33152
  1868
  show ?thesis by auto
chaieb@33152
  1869
qed
chaieb@33152
  1870
chaieb@33152
  1871
    (* The Ferrante - Rackoff Theorem *)
chaieb@33152
  1872
chaieb@33152
  1873
theorem fr_eq: 
chaieb@33152
  1874
  assumes lp: "islin p"
huffman@45499
  1875
  shows "(\<exists> x. Ifm vs (x#bs) p) = ((Ifm vs (x#bs) (minusinf p)) \<or> (Ifm vs (x#bs) (plusinf p)) \<or> (\<exists> (n,t) \<in> set (uset p). \<exists> (m,s) \<in> set (uset p). Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs n + - Itm vs (x#bs) s / Ipoly vs m) / 2)#bs) p))"
chaieb@33152
  1876
  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
chaieb@33152
  1877
proof
chaieb@33152
  1878
  assume px: "\<exists> x. ?I x p"
chaieb@33152
  1879
  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
chaieb@33152
  1880
  moreover {assume "?M \<or> ?P" hence "?D" by blast}
chaieb@33152
  1881
  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
chaieb@33152
  1882
    from inf_uset[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
chaieb@33152
  1883
  ultimately show "?D" by blast
chaieb@33152
  1884
next
chaieb@33152
  1885
  assume "?D" 
chaieb@33152
  1886
  moreover {assume m:"?M" from minusinf_ex[OF lp m] have "?E" .}
chaieb@33152
  1887
  moreover {assume p: "?P" from plusinf_ex[OF lp p] have "?E" . }
chaieb@33152
  1888
  moreover {assume f:"?F" hence "?E" by blast}
chaieb@33152
  1889
  ultimately show "?E" by blast
chaieb@33152
  1890
qed
chaieb@33152
  1891
chaieb@33152
  1892
section{* First implementation : Naive by encoding all case splits locally *}
chaieb@33152
  1893
definition "msubsteq c t d s a r = 
chaieb@33152
  1894
  evaldjf (split conj) 
chaieb@33152
  1895
  [(let cd = c *\<^sub>p d in (NEq (CP cd), Eq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
chaieb@33152
  1896
   (conj (Eq (CP c)) (NEq (CP d)) , Eq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
chaieb@33152
  1897
   (conj (NEq (CP c)) (Eq (CP d)) , Eq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
chaieb@33152
  1898
   (conj (Eq (CP c)) (Eq (CP d)) , Eq r)]"
chaieb@33152
  1899
chaieb@33152
  1900
lemma msubsteq_nb: assumes lp: "islin (Eq (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
chaieb@33152
  1901
  shows "bound0 (msubsteq c t d s a r)"
chaieb@33152
  1902
proof-
chaieb@33152
  1903
  have th: "\<forall>x\<in> set [(let cd = c *\<^sub>p d in (NEq (CP cd), Eq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
chaieb@33152
  1904
   (conj (Eq (CP c)) (NEq (CP d)) , Eq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
chaieb@33152
  1905
   (conj (NEq (CP c)) (Eq (CP d)) , Eq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
chaieb@33152
  1906
   (conj (Eq (CP c)) (Eq (CP d)) , Eq r)]. bound0 (split conj x)"
chaieb@33152
  1907
    using lp by (simp add: Let_def t s )
chaieb@33152
  1908
  from evaldjf_bound0[OF th] show ?thesis by (simp add: msubsteq_def)
chaieb@33152
  1909
qed
chaieb@33152
  1910
chaieb@33152
  1911
lemma msubsteq: assumes lp: "islin (Eq (CNP 0 a r))"
huffman@45499
  1912
  shows "Ifm vs (x#bs) (msubsteq c t d s a r) = Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) / 2)#bs) (Eq (CNP 0 a r))" (is "?lhs = ?rhs")
chaieb@33152
  1913
proof-
chaieb@33152
  1914
  let ?Nt = "\<lambda>(x::'a) t. Itm vs (x#bs) t"
chaieb@33152
  1915
  let ?N = "\<lambda>p. Ipoly vs p"
chaieb@33152
  1916
  let ?c = "?N c"
chaieb@33152
  1917
  let ?d = "?N d"
chaieb@33152
  1918
  let ?t = "?Nt x t"
chaieb@33152
  1919
  let ?s = "?Nt x s"
chaieb@33152
  1920
  let ?a = "?N a"
chaieb@33152
  1921
  let ?r = "?Nt x r"
chaieb@33152
  1922
  from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
chaieb@33152
  1923
  note r= tmbound0_I[OF lin(3), of vs _ bs x]
chaieb@33152
  1924
  have cd_cs: "?c * ?d \<noteq> 0 \<or> (?c = 0 \<and> ?d = 0) \<or> (?c = 0 \<and> ?d \<noteq> 0) \<or> (?c \<noteq> 0 \<and> ?d = 0)" by auto
chaieb@33152
  1925
  moreover
chaieb@33152
  1926
  {assume c: "?c = 0" and d: "?d=0"
chaieb@33152
  1927
    hence ?thesis  by (simp add: r[of 0] msubsteq_def Let_def evaldjf_ex)}
chaieb@33152
  1928
  moreover 
chaieb@33152
  1929
  {assume c: "?c = 0" and d: "?d\<noteq>0"
huffman@45499
  1930
    from c have th: "(- ?t / ?c + - ?s / ?d)/2 = -?s / (2*?d)" by simp
huffman@45499
  1931
    have "?rhs = Ifm vs (-?s / (2*?d) # bs) (Eq (CNP 0 a r))" by (simp only: th)
huffman@45499
  1932
    also have "\<dots> \<longleftrightarrow> ?a * (-?s / (2*?d)) + ?r = 0" by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"])
huffman@45499
  1933
    also have "\<dots> \<longleftrightarrow> 2*?d * (?a * (-?s / (2*?d)) + ?r) = 0"
huffman@45499
  1934
      using d mult_cancel_left[of "2*?d" "(?a * (-?s / (2*?d)) + ?r)" 0] by simp
huffman@45499
  1935
    also have "\<dots> \<longleftrightarrow> (- ?a * ?s) * (2*?d / (2*?d)) + 2*?d*?r= 0"
webertj@49962
  1936
      by (simp add: field_simps distrib_left[of "2*?d"] del: distrib_left)
chaieb@33152
  1937
    
huffman@45499
  1938
    also have "\<dots> \<longleftrightarrow> - (?a * ?s) + 2*?d*?r = 0" using d by simp 
chaieb@33152
  1939
    finally have ?thesis using c d 
huffman@47108
  1940
      by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubsteq_def Let_def evaldjf_ex)
huffman@45499
  1941
  }
chaieb@33152
  1942
  moreover
chaieb@33152
  1943
  {assume c: "?c \<noteq> 0" and d: "?d=0"
huffman@45499
  1944
    from d have th: "(- ?t / ?c + - ?s / ?d)/2 = -?t / (2*?c)" by simp
huffman@45499
  1945
    have "?rhs = Ifm vs (-?t / (2*?c) # bs) (Eq (CNP 0 a r))" by (simp only: th)
huffman@45499
  1946
    also have "\<dots> \<longleftrightarrow> ?a * (-?t / (2*?c)) + ?r = 0" by (simp add: r[of "- (?t/ (2 * ?c))"])
huffman@45499
  1947
    also have "\<dots> \<longleftrightarrow> 2*?c * (?a * (-?t / (2*?c)) + ?r) = 0" 
huffman@45499
  1948
      using c mult_cancel_left[of "2*?c" "(?a * (-?t / (2*?c)) + ?r)" 0] by simp
huffman@45499
  1949
    also have "\<dots> \<longleftrightarrow> (?a * -?t)* (2*?c) / (2*?c) + 2*?c*?r= 0"
webertj@49962
  1950
      by (simp add: field_simps distrib_left[of "2*?c"] del: distrib_left)
huffman@45499
  1951
    also have "\<dots> \<longleftrightarrow> - (?a * ?t) + 2*?c*?r = 0" using c by simp 
chaieb@33152
  1952
    finally have ?thesis using c d 
huffman@47108
  1953
      by (simp add: r[of "- (?t/ (2*?c))"] msubsteq_def Let_def evaldjf_ex)
huffman@45499
  1954
  }
chaieb@33152
  1955
  moreover
huffman@45499
  1956
  {assume c: "?c \<noteq> 0" and d: "?d\<noteq>0" hence dc: "?c * ?d *2 \<noteq> 0" by simp
chaieb@33152
  1957
    from add_frac_eq[OF c d, of "- ?t" "- ?s"]
huffman@45499
  1958
    have th: "(- ?t / ?c + - ?s / ?d)/2 = - (?d * ?t + ?c* ?s )/ (2*?c*?d)" 
haftmann@36348
  1959
      by (simp add: field_simps)
huffman@45499
  1960
    have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ (2*?c*?d) # bs) (Eq (CNP 0 a r))" by (simp only: th)
huffman@45499
  1961
    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ (2*?c*?d)) + ?r = 0" 
huffman@45499
  1962
      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / (2 * ?c * ?d)"])
huffman@45499
  1963
    also have "\<dots> \<longleftrightarrow> (2 * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ (2*?c*?d)) + ?r) =0 "
huffman@45499
  1964
      using c d mult_cancel_left[of "2 * ?c * ?d" "?a * (- (?d * ?t + ?c* ?s)/ (2*?c*?d)) + ?r" 0] by simp
huffman@45499
  1965
    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + 2*?c*?d*?r =0" 
haftmann@36348
  1966
      using nonzero_mult_divide_cancel_left [OF dc] c d
webertj@49962
  1967
      by (simp add: algebra_simps diff_divide_distrib del: distrib_right)
chaieb@33152
  1968
    finally  have ?thesis using c d 
huffman@45499
  1969
      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / (2 * ?c * ?d)"] msubsteq_def Let_def evaldjf_ex field_simps)
huffman@45499
  1970
  }
chaieb@33152
  1971
  ultimately show ?thesis by blast
chaieb@33152
  1972
qed
chaieb@33152
  1973
chaieb@33152
  1974
chaieb@33152
  1975
definition "msubstneq c t d s a r = 
chaieb@33152
  1976
  evaldjf (split conj) 
chaieb@33152
  1977
  [(let cd = c *\<^sub>p d in (NEq (CP cd), NEq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
chaieb@33152
  1978
   (conj (Eq (CP c)) (NEq (CP d)) , NEq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
chaieb@33152
  1979
   (conj (NEq (CP c)) (Eq (CP d)) , NEq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
chaieb@33152
  1980
   (conj (Eq (CP c)) (Eq (CP d)) , NEq r)]"
chaieb@33152
  1981
chaieb@33152
  1982
lemma msubstneq_nb: assumes lp: "islin (NEq (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
chaieb@33152
  1983
  shows "bound0 (msubstneq c t d s a r)"
chaieb@33152
  1984
proof-
chaieb@33152
  1985
  have th: "\<forall>x\<in> set [(let cd = c *\<^sub>p d in (NEq (CP cd), NEq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))), 
chaieb@33152
  1986
    (conj (Eq (CP c)) (NEq (CP d)) , NEq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
chaieb@33152
  1987
    (conj (NEq (CP c)) (Eq (CP d)) , NEq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
chaieb@33152
  1988
    (conj (Eq (CP c)) (Eq (CP d)) , NEq r)]. bound0 (split conj x)"
chaieb@33152
  1989
    using lp by (simp add: Let_def t s )
chaieb@33152
  1990
  from evaldjf_bound0[OF th] show ?thesis by (simp add: msubstneq_def)
chaieb@33152
  1991
qed
chaieb@33152
  1992
chaieb@33152
  1993
lemma msubstneq: assumes lp: "islin (Eq (CNP 0 a r))"
huffman@45499
  1994
  shows "Ifm vs (x#bs) (msubstneq c t d s a r) = Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) /2)#bs) (NEq (CNP 0 a r))" (is "?lhs = ?rhs")
chaieb@33152
  1995
proof-
chaieb@33152
  1996
  let ?Nt = "\<lambda>(x::'a) t. Itm vs (x#bs) t"
chaieb@33152
  1997
  let ?N = "\<lambda>p. Ipoly vs p"
chaieb@33152
  1998
  let ?c = "?N c"
chaieb@33152
  1999
  let ?d = "?N d"
chaieb@33152
  2000
  let ?t = "?Nt x t"
chaieb@33152
  2001
  let ?s = "?Nt x s"
chaieb@33152
  2002
  let ?a = "?N a"
chaieb@33152
  2003
  let ?r = "?Nt x r"
chaieb@33152
  2004
  from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
chaieb@33152
  2005
  note r= tmbound0_I[OF lin(3), of vs _ bs x]
chaieb@33152
  2006
  have cd_cs: "?c * ?d \<noteq> 0 \<or> (?c = 0 \<and> ?d = 0) \<or> (?c = 0 \<and> ?d \<noteq> 0) \<or> (?c \<noteq> 0 \<and> ?d = 0)" by auto
chaieb@33152
  2007
  moreover
chaieb@33152
  2008
  {assume c: "?c = 0" and d: "?d=0"
chaieb@33152
  2009
    hence ?thesis  by (simp add: r[of 0] msubstneq_def Let_def evaldjf_ex)}
chaieb@33152
  2010
  moreover 
chaieb@33152
  2011
  {assume c: "?c = 0" and d: "?d\<noteq>0"
huffman@45499
  2012
    from c have th: "(- ?t / ?c + - ?s / ?d)/2 = -?s / (2*?d)" by simp
huffman@45499
  2013
    have "?rhs = Ifm vs (-?s / (2*?d) # bs) (NEq (CNP 0 a r))" by (simp only: th)
huffman@45499
  2014
    also have "\<dots> \<longleftrightarrow> ?a * (-?s / (2*?d)) + ?r \<noteq> 0" by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"])
huffman@45499
  2015
    also have "\<dots> \<longleftrightarrow> 2*?d * (?a * (-?s / (2*?d)) + ?r) \<noteq> 0" 
huffman@45499
  2016
      using d mult_cancel_left[of "2*?d" "(?a * (-?s / (2*?d)) + ?r)" 0] by simp
huffman@45499
  2017
    also have "\<dots> \<longleftrightarrow> (- ?a * ?s) * (2*?d / (2*?d)) + 2*?d*?r\<noteq> 0"
webertj@49962
  2018
      by (simp add: field_simps distrib_left[of "2*?d"] del: distrib_left)
chaieb@33152
  2019
    
huffman@45499
  2020
    also have "\<dots> \<longleftrightarrow> - (?a * ?s) + 2*?d*?r \<noteq> 0" using d by simp 
chaieb@33152
  2021
    finally have ?thesis using c d 
huffman@47108
  2022
      by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubstneq_def Let_def evaldjf_ex)
huffman@45499
  2023
  }
chaieb@33152
  2024
  moreover
chaieb@33152
  2025
  {assume c: "?c \<noteq> 0" and d: "?d=0"
huffman@45499
  2026
    from d have th: "(- ?t / ?c + - ?s / ?d)/2 = -?t / (2*?c)" by simp
huffman@45499
  2027
    have "?rhs = Ifm vs (-?t / (2*?c) # bs) (NEq (CNP 0 a r))" by (simp only: th)
huffman@45499
  2028
    also have "\<dots> \<longleftrightarrow> ?a * (-?t / (2*?c)) + ?r \<noteq> 0" by (simp add: r[of "- (?t/ (2 * ?c))"])
huffman@45499
  2029