src/HOL/Decision_Procs/Ferrack.thy
author wenzelm
Fri Mar 06 15:58:56 2015 +0100 (2015-03-06)
changeset 59621 291934bac95e
parent 59580 cbc38731d42f
child 60533 1e7ccd864b62
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
hoelzl@30439
     1
(*  Title:      HOL/Decision_Procs/Ferrack.thy
haftmann@29789
     2
    Author:     Amine Chaieb
haftmann@29789
     3
*)
haftmann@29789
     4
haftmann@29789
     5
theory Ferrack
nipkow@41849
     6
imports Complex_Main Dense_Linear_Order DP_Library
haftmann@51143
     7
  "~~/src/HOL/Library/Code_Target_Numeral" "~~/src/HOL/Library/Old_Recdef"
haftmann@29789
     8
begin
haftmann@29789
     9
haftmann@29789
    10
section {* Quantifier elimination for @{text "\<real> (0, 1, +, <)"} *}
haftmann@29789
    11
haftmann@29789
    12
  (*********************************************************************************)
haftmann@29789
    13
  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
haftmann@29789
    14
  (*********************************************************************************)
haftmann@29789
    15
blanchet@58310
    16
datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
haftmann@29789
    17
  | Mul int num 
haftmann@29789
    18
haftmann@29789
    19
  (* A size for num to make inductive proofs simpler*)
haftmann@36853
    20
primrec num_size :: "num \<Rightarrow> nat" where
haftmann@29789
    21
  "num_size (C c) = 1"
haftmann@36853
    22
| "num_size (Bound n) = 1"
haftmann@36853
    23
| "num_size (Neg a) = 1 + num_size a"
haftmann@36853
    24
| "num_size (Add a b) = 1 + num_size a + num_size b"
haftmann@36853
    25
| "num_size (Sub a b) = 3 + num_size a + num_size b"
haftmann@36853
    26
| "num_size (Mul c a) = 1 + num_size a"
haftmann@36853
    27
| "num_size (CN n c a) = 3 + num_size a "
haftmann@29789
    28
haftmann@29789
    29
  (* Semantics of numeral terms (num) *)
haftmann@36853
    30
primrec Inum :: "real list \<Rightarrow> num \<Rightarrow> real" where
haftmann@29789
    31
  "Inum bs (C c) = (real c)"
haftmann@36853
    32
| "Inum bs (Bound n) = bs!n"
haftmann@36853
    33
| "Inum bs (CN n c a) = (real c) * (bs!n) + (Inum bs a)"
haftmann@36853
    34
| "Inum bs (Neg a) = -(Inum bs a)"
haftmann@36853
    35
| "Inum bs (Add a b) = Inum bs a + Inum bs b"
haftmann@36853
    36
| "Inum bs (Sub a b) = Inum bs a - Inum bs b"
haftmann@36853
    37
| "Inum bs (Mul c a) = (real c) * Inum bs a"
haftmann@29789
    38
    (* FORMULAE *)
blanchet@58310
    39
datatype fm  = 
haftmann@29789
    40
  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num|
haftmann@29789
    41
  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
haftmann@29789
    42
haftmann@29789
    43
haftmann@29789
    44
  (* A size for fm *)
haftmann@36853
    45
fun fmsize :: "fm \<Rightarrow> nat" where
haftmann@29789
    46
  "fmsize (NOT p) = 1 + fmsize p"
haftmann@36853
    47
| "fmsize (And p q) = 1 + fmsize p + fmsize q"
haftmann@36853
    48
| "fmsize (Or p q) = 1 + fmsize p + fmsize q"
haftmann@36853
    49
| "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
haftmann@36853
    50
| "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
haftmann@36853
    51
| "fmsize (E p) = 1 + fmsize p"
haftmann@36853
    52
| "fmsize (A p) = 4+ fmsize p"
haftmann@36853
    53
| "fmsize p = 1"
haftmann@29789
    54
  (* several lemmas about fmsize *)
haftmann@29789
    55
lemma fmsize_pos: "fmsize p > 0"
haftmann@29789
    56
by (induct p rule: fmsize.induct) simp_all
haftmann@29789
    57
haftmann@29789
    58
  (* Semantics of formulae (fm) *)
haftmann@36853
    59
primrec Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool" where
haftmann@29789
    60
  "Ifm bs T = True"
haftmann@36853
    61
| "Ifm bs F = False"
haftmann@36853
    62
| "Ifm bs (Lt a) = (Inum bs a < 0)"
haftmann@36853
    63
| "Ifm bs (Gt a) = (Inum bs a > 0)"
haftmann@36853
    64
| "Ifm bs (Le a) = (Inum bs a \<le> 0)"
haftmann@36853
    65
| "Ifm bs (Ge a) = (Inum bs a \<ge> 0)"
haftmann@36853
    66
| "Ifm bs (Eq a) = (Inum bs a = 0)"
haftmann@36853
    67
| "Ifm bs (NEq a) = (Inum bs a \<noteq> 0)"
haftmann@36853
    68
| "Ifm bs (NOT p) = (\<not> (Ifm bs p))"
haftmann@36853
    69
| "Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
haftmann@36853
    70
| "Ifm bs (Or p q) = (Ifm bs p \<or> Ifm bs q)"
haftmann@36853
    71
| "Ifm bs (Imp p q) = ((Ifm bs p) \<longrightarrow> (Ifm bs q))"
haftmann@36853
    72
| "Ifm bs (Iff p q) = (Ifm bs p = Ifm bs q)"
haftmann@36853
    73
| "Ifm bs (E p) = (\<exists> x. Ifm (x#bs) p)"
haftmann@36853
    74
| "Ifm bs (A p) = (\<forall> x. Ifm (x#bs) p)"
haftmann@29789
    75
haftmann@29789
    76
lemma IfmLeSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Le (Sub s t)) = (s' \<le> t')"
haftmann@29789
    77
apply simp
haftmann@29789
    78
done
haftmann@29789
    79
haftmann@29789
    80
lemma IfmLtSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Lt (Sub s t)) = (s' < t')"
haftmann@29789
    81
apply simp
haftmann@29789
    82
done
haftmann@29789
    83
lemma IfmEqSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Eq (Sub s t)) = (s' = t')"
haftmann@29789
    84
apply simp
haftmann@29789
    85
done
haftmann@29789
    86
lemma IfmNOT: " (Ifm bs p = P) \<Longrightarrow> (Ifm bs (NOT p) = (\<not>P))"
haftmann@29789
    87
apply simp
haftmann@29789
    88
done
haftmann@29789
    89
lemma IfmAnd: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (And p q) = (P \<and> Q))"
haftmann@29789
    90
apply simp
haftmann@29789
    91
done
haftmann@29789
    92
lemma IfmOr: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Or p q) = (P \<or> Q))"
haftmann@29789
    93
apply simp
haftmann@29789
    94
done
haftmann@29789
    95
lemma IfmImp: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Imp p q) = (P \<longrightarrow> Q))"
haftmann@29789
    96
apply simp
haftmann@29789
    97
done
haftmann@29789
    98
lemma IfmIff: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Iff p q) = (P = Q))"
haftmann@29789
    99
apply simp
haftmann@29789
   100
done
haftmann@29789
   101
haftmann@29789
   102
lemma IfmE: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (E p) = (\<exists>x. P x))"
haftmann@29789
   103
apply simp
haftmann@29789
   104
done
haftmann@29789
   105
lemma IfmA: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (A p) = (\<forall>x. P x))"
haftmann@29789
   106
apply simp
haftmann@29789
   107
done
haftmann@29789
   108
haftmann@36853
   109
fun not:: "fm \<Rightarrow> fm" where
haftmann@29789
   110
  "not (NOT p) = p"
haftmann@36853
   111
| "not T = F"
haftmann@36853
   112
| "not F = T"
haftmann@36853
   113
| "not p = NOT p"
haftmann@29789
   114
lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
haftmann@29789
   115
by (cases p) auto
haftmann@29789
   116
haftmann@35416
   117
definition conj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@36853
   118
  "conj p q = (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else 
haftmann@29789
   119
   if p = q then p else And p q)"
haftmann@29789
   120
lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
haftmann@29789
   121
by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
haftmann@29789
   122
haftmann@35416
   123
definition disj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@36853
   124
  "disj p q = (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
haftmann@29789
   125
       else if p=q then p else Or p q)"
haftmann@29789
   126
haftmann@29789
   127
lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
haftmann@29789
   128
by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
haftmann@29789
   129
haftmann@35416
   130
definition imp :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@36853
   131
  "imp p q = (if (p = F \<or> q=T \<or> p=q) then T else if p=T then q else if q=F then not p 
haftmann@29789
   132
    else Imp p q)"
haftmann@29789
   133
lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
haftmann@29789
   134
by (cases "p=F \<or> q=T",simp_all add: imp_def) 
haftmann@29789
   135
haftmann@35416
   136
definition iff :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@36853
   137
  "iff p q = (if (p = q) then T else if (p = NOT q \<or> NOT p = q) then F else 
haftmann@29789
   138
       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 
haftmann@29789
   139
  Iff p q)"
haftmann@29789
   140
lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
haftmann@29789
   141
  by (unfold iff_def,cases "p=q", simp,cases "p=NOT q", simp) (cases "NOT p= q", auto)
haftmann@29789
   142
haftmann@29789
   143
lemma conj_simps:
haftmann@29789
   144
  "conj F Q = F"
haftmann@29789
   145
  "conj P F = F"
haftmann@29789
   146
  "conj T Q = Q"
haftmann@29789
   147
  "conj P T = P"
haftmann@29789
   148
  "conj P P = P"
haftmann@29789
   149
  "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> conj P Q = And P Q"
haftmann@29789
   150
  by (simp_all add: conj_def)
haftmann@29789
   151
haftmann@29789
   152
lemma disj_simps:
haftmann@29789
   153
  "disj T Q = T"
haftmann@29789
   154
  "disj P T = T"
haftmann@29789
   155
  "disj F Q = Q"
haftmann@29789
   156
  "disj P F = P"
haftmann@29789
   157
  "disj P P = P"
haftmann@29789
   158
  "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> disj P Q = Or P Q"
haftmann@29789
   159
  by (simp_all add: disj_def)
haftmann@29789
   160
lemma imp_simps:
haftmann@29789
   161
  "imp F Q = T"
haftmann@29789
   162
  "imp P T = T"
haftmann@29789
   163
  "imp T Q = Q"
haftmann@29789
   164
  "imp P F = not P"
haftmann@29789
   165
  "imp P P = T"
haftmann@29789
   166
  "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> imp P Q = Imp P Q"
haftmann@29789
   167
  by (simp_all add: imp_def)
haftmann@29789
   168
lemma trivNOT: "p \<noteq> NOT p" "NOT p \<noteq> p"
haftmann@29789
   169
apply (induct p, auto)
haftmann@29789
   170
done
haftmann@29789
   171
haftmann@29789
   172
lemma iff_simps:
haftmann@29789
   173
  "iff p p = T"
haftmann@29789
   174
  "iff p (NOT p) = F"
haftmann@29789
   175
  "iff (NOT p) p = F"
haftmann@29789
   176
  "iff p F = not p"
haftmann@29789
   177
  "iff F p = not p"
haftmann@29789
   178
  "p \<noteq> NOT T \<Longrightarrow> iff T p = p"
haftmann@29789
   179
  "p\<noteq> NOT T \<Longrightarrow> iff p T = p"
haftmann@29789
   180
  "p\<noteq>q \<Longrightarrow> p\<noteq> NOT q \<Longrightarrow> q\<noteq> NOT p \<Longrightarrow> p\<noteq> F \<Longrightarrow> q\<noteq> F \<Longrightarrow> p \<noteq> T \<Longrightarrow> q \<noteq> T \<Longrightarrow> iff p q = Iff p q"
haftmann@29789
   181
  using trivNOT
haftmann@29789
   182
  by (simp_all add: iff_def, cases p, auto)
haftmann@29789
   183
  (* Quantifier freeness *)
haftmann@36853
   184
fun qfree:: "fm \<Rightarrow> bool" where
haftmann@29789
   185
  "qfree (E p) = False"
haftmann@36853
   186
| "qfree (A p) = False"
haftmann@36853
   187
| "qfree (NOT p) = qfree p" 
haftmann@36853
   188
| "qfree (And p q) = (qfree p \<and> qfree q)" 
haftmann@36853
   189
| "qfree (Or  p q) = (qfree p \<and> qfree q)" 
haftmann@36853
   190
| "qfree (Imp p q) = (qfree p \<and> qfree q)" 
haftmann@36853
   191
| "qfree (Iff p q) = (qfree p \<and> qfree q)"
haftmann@36853
   192
| "qfree p = True"
haftmann@29789
   193
haftmann@29789
   194
  (* Boundedness and substitution *)
haftmann@36853
   195
primrec numbound0:: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *) where
haftmann@29789
   196
  "numbound0 (C c) = True"
haftmann@36853
   197
| "numbound0 (Bound n) = (n>0)"
haftmann@36853
   198
| "numbound0 (CN n c a) = (n\<noteq>0 \<and> numbound0 a)"
haftmann@36853
   199
| "numbound0 (Neg a) = numbound0 a"
haftmann@36853
   200
| "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
haftmann@36853
   201
| "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
haftmann@36853
   202
| "numbound0 (Mul i a) = numbound0 a"
haftmann@36853
   203
haftmann@29789
   204
lemma numbound0_I:
haftmann@29789
   205
  assumes nb: "numbound0 a"
haftmann@29789
   206
  shows "Inum (b#bs) a = Inum (b'#bs) a"
haftmann@29789
   207
using nb
nipkow@41842
   208
by (induct a) simp_all
haftmann@29789
   209
haftmann@36853
   210
primrec bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *) where
haftmann@29789
   211
  "bound0 T = True"
haftmann@36853
   212
| "bound0 F = True"
haftmann@36853
   213
| "bound0 (Lt a) = numbound0 a"
haftmann@36853
   214
| "bound0 (Le a) = numbound0 a"
haftmann@36853
   215
| "bound0 (Gt a) = numbound0 a"
haftmann@36853
   216
| "bound0 (Ge a) = numbound0 a"
haftmann@36853
   217
| "bound0 (Eq a) = numbound0 a"
haftmann@36853
   218
| "bound0 (NEq a) = numbound0 a"
haftmann@36853
   219
| "bound0 (NOT p) = bound0 p"
haftmann@36853
   220
| "bound0 (And p q) = (bound0 p \<and> bound0 q)"
haftmann@36853
   221
| "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
haftmann@36853
   222
| "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
haftmann@36853
   223
| "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
haftmann@36853
   224
| "bound0 (E p) = False"
haftmann@36853
   225
| "bound0 (A p) = False"
haftmann@29789
   226
haftmann@29789
   227
lemma bound0_I:
haftmann@29789
   228
  assumes bp: "bound0 p"
haftmann@29789
   229
  shows "Ifm (b#bs) p = Ifm (b'#bs) p"
haftmann@29789
   230
using bp numbound0_I[where b="b" and bs="bs" and b'="b'"]
nipkow@41842
   231
by (induct p) auto
haftmann@29789
   232
haftmann@29789
   233
lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
haftmann@29789
   234
by (cases p, auto)
haftmann@29789
   235
lemma not_bn[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
haftmann@29789
   236
by (cases p, auto)
haftmann@29789
   237
haftmann@29789
   238
haftmann@29789
   239
lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
haftmann@29789
   240
using conj_def by auto 
haftmann@29789
   241
lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
haftmann@29789
   242
using conj_def by auto 
haftmann@29789
   243
haftmann@29789
   244
lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
haftmann@29789
   245
using disj_def by auto 
haftmann@29789
   246
lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
haftmann@29789
   247
using disj_def by auto 
haftmann@29789
   248
haftmann@29789
   249
lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
haftmann@29789
   250
using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
haftmann@29789
   251
lemma imp_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
haftmann@29789
   252
using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
haftmann@29789
   253
haftmann@29789
   254
lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
haftmann@29789
   255
  by (unfold iff_def,cases "p=q", auto)
haftmann@29789
   256
lemma iff_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
haftmann@29789
   257
using iff_def by (unfold iff_def,cases "p=q", auto)
haftmann@29789
   258
haftmann@36853
   259
fun decrnum:: "num \<Rightarrow> num"  where
haftmann@29789
   260
  "decrnum (Bound n) = Bound (n - 1)"
haftmann@36853
   261
| "decrnum (Neg a) = Neg (decrnum a)"
haftmann@36853
   262
| "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
haftmann@36853
   263
| "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
haftmann@36853
   264
| "decrnum (Mul c a) = Mul c (decrnum a)"
haftmann@36853
   265
| "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
haftmann@36853
   266
| "decrnum a = a"
haftmann@29789
   267
haftmann@36853
   268
fun decr :: "fm \<Rightarrow> fm" where
haftmann@29789
   269
  "decr (Lt a) = Lt (decrnum a)"
haftmann@36853
   270
| "decr (Le a) = Le (decrnum a)"
haftmann@36853
   271
| "decr (Gt a) = Gt (decrnum a)"
haftmann@36853
   272
| "decr (Ge a) = Ge (decrnum a)"
haftmann@36853
   273
| "decr (Eq a) = Eq (decrnum a)"
haftmann@36853
   274
| "decr (NEq a) = NEq (decrnum a)"
haftmann@36853
   275
| "decr (NOT p) = NOT (decr p)" 
haftmann@36853
   276
| "decr (And p q) = conj (decr p) (decr q)"
haftmann@36853
   277
| "decr (Or p q) = disj (decr p) (decr q)"
haftmann@36853
   278
| "decr (Imp p q) = imp (decr p) (decr q)"
haftmann@36853
   279
| "decr (Iff p q) = iff (decr p) (decr q)"
haftmann@36853
   280
| "decr p = p"
haftmann@29789
   281
haftmann@29789
   282
lemma decrnum: assumes nb: "numbound0 t"
haftmann@29789
   283
  shows "Inum (x#bs) t = Inum bs (decrnum t)"
nipkow@41842
   284
  using nb by (induct t rule: decrnum.induct, simp_all)
haftmann@29789
   285
haftmann@29789
   286
lemma decr: assumes nb: "bound0 p"
haftmann@29789
   287
  shows "Ifm (x#bs) p = Ifm bs (decr p)"
haftmann@29789
   288
  using nb 
nipkow@41842
   289
  by (induct p rule: decr.induct, simp_all add: decrnum)
haftmann@29789
   290
haftmann@29789
   291
lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
haftmann@29789
   292
by (induct p, simp_all)
haftmann@29789
   293
haftmann@36853
   294
fun isatom :: "fm \<Rightarrow> bool" (* test for atomicity *) where
haftmann@29789
   295
  "isatom T = True"
haftmann@36853
   296
| "isatom F = True"
haftmann@36853
   297
| "isatom (Lt a) = True"
haftmann@36853
   298
| "isatom (Le a) = True"
haftmann@36853
   299
| "isatom (Gt a) = True"
haftmann@36853
   300
| "isatom (Ge a) = True"
haftmann@36853
   301
| "isatom (Eq a) = True"
haftmann@36853
   302
| "isatom (NEq a) = True"
haftmann@36853
   303
| "isatom p = False"
haftmann@29789
   304
haftmann@29789
   305
lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
haftmann@29789
   306
by (induct p, simp_all)
haftmann@29789
   307
haftmann@35416
   308
definition djf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@36853
   309
  "djf f p q = (if q=T then T else if q=F then f p else 
haftmann@29789
   310
  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
haftmann@35416
   311
definition evaldjf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
haftmann@36853
   312
  "evaldjf f ps = foldr (djf f) ps F"
haftmann@29789
   313
haftmann@29789
   314
lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
haftmann@29789
   315
by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
haftmann@29789
   316
(cases "f p", simp_all add: Let_def djf_def) 
haftmann@29789
   317
haftmann@29789
   318
haftmann@29789
   319
lemma djf_simps:
haftmann@29789
   320
  "djf f p T = T"
haftmann@29789
   321
  "djf f p F = f p"
haftmann@29789
   322
  "q\<noteq>T \<Longrightarrow> q\<noteq>F \<Longrightarrow> djf f p q = (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q)"
haftmann@29789
   323
  by (simp_all add: djf_def)
haftmann@29789
   324
haftmann@29789
   325
lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
haftmann@29789
   326
  by(induct ps, simp_all add: evaldjf_def djf_Or)
haftmann@29789
   327
haftmann@29789
   328
lemma evaldjf_bound0: 
haftmann@29789
   329
  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
haftmann@29789
   330
  shows "bound0 (evaldjf f xs)"
blanchet@55422
   331
  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
haftmann@29789
   332
haftmann@29789
   333
lemma evaldjf_qf: 
haftmann@29789
   334
  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
haftmann@29789
   335
  shows "qfree (evaldjf f xs)"
blanchet@55422
   336
  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
haftmann@29789
   337
haftmann@36853
   338
fun disjuncts :: "fm \<Rightarrow> fm list" where
haftmann@36853
   339
  "disjuncts (Or p q) = disjuncts p @ disjuncts q"
haftmann@36853
   340
| "disjuncts F = []"
haftmann@36853
   341
| "disjuncts p = [p]"
haftmann@29789
   342
haftmann@29789
   343
lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bs q) = Ifm bs p"
haftmann@29789
   344
by(induct p rule: disjuncts.induct, auto)
haftmann@29789
   345
haftmann@29789
   346
lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
haftmann@29789
   347
proof-
haftmann@29789
   348
  assume nb: "bound0 p"
haftmann@29789
   349
  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
haftmann@29789
   350
  thus ?thesis by (simp only: list_all_iff)
haftmann@29789
   351
qed
haftmann@29789
   352
haftmann@29789
   353
lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
haftmann@29789
   354
proof-
haftmann@29789
   355
  assume qf: "qfree p"
haftmann@29789
   356
  hence "list_all qfree (disjuncts p)"
haftmann@29789
   357
    by (induct p rule: disjuncts.induct, auto)
haftmann@29789
   358
  thus ?thesis by (simp only: list_all_iff)
haftmann@29789
   359
qed
haftmann@29789
   360
haftmann@35416
   361
definition DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@36853
   362
  "DJ f p = evaldjf f (disjuncts p)"
haftmann@29789
   363
haftmann@29789
   364
lemma DJ: assumes fdj: "\<forall> p q. Ifm bs (f (Or p q)) = Ifm bs (Or (f p) (f q))"
haftmann@29789
   365
  and fF: "f F = F"
haftmann@29789
   366
  shows "Ifm bs (DJ f p) = Ifm bs (f p)"
haftmann@29789
   367
proof-
haftmann@29789
   368
  have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
haftmann@29789
   369
    by (simp add: DJ_def evaldjf_ex) 
haftmann@29789
   370
  also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
haftmann@29789
   371
  finally show ?thesis .
haftmann@29789
   372
qed
haftmann@29789
   373
haftmann@29789
   374
lemma DJ_qf: assumes 
haftmann@29789
   375
  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
haftmann@29789
   376
  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
haftmann@29789
   377
proof(clarify)
haftmann@29789
   378
  fix  p assume qf: "qfree p"
haftmann@29789
   379
  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
haftmann@29789
   380
  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
haftmann@29789
   381
  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
haftmann@29789
   382
  
haftmann@29789
   383
  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
haftmann@29789
   384
qed
haftmann@29789
   385
haftmann@29789
   386
lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
haftmann@29789
   387
  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
haftmann@29789
   388
proof(clarify)
haftmann@29789
   389
  fix p::fm and bs
haftmann@29789
   390
  assume qf: "qfree p"
haftmann@29789
   391
  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
haftmann@29789
   392
  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
haftmann@29789
   393
  have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
haftmann@29789
   394
    by (simp add: DJ_def evaldjf_ex)
haftmann@29789
   395
  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
haftmann@29789
   396
  also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
haftmann@29789
   397
  finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
haftmann@29789
   398
qed
haftmann@29789
   399
  (* Simplification *)
haftmann@36853
   400
haftmann@36853
   401
fun maxcoeff:: "num \<Rightarrow> int" where
haftmann@29789
   402
  "maxcoeff (C i) = abs i"
haftmann@36853
   403
| "maxcoeff (CN n c t) = max (abs c) (maxcoeff t)"
haftmann@36853
   404
| "maxcoeff t = 1"
haftmann@29789
   405
haftmann@29789
   406
lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
haftmann@29789
   407
  by (induct t rule: maxcoeff.induct, auto)
haftmann@29789
   408
haftmann@36853
   409
fun numgcdh:: "num \<Rightarrow> int \<Rightarrow> int" where
huffman@31706
   410
  "numgcdh (C i) = (\<lambda>g. gcd i g)"
haftmann@36853
   411
| "numgcdh (CN n c t) = (\<lambda>g. gcd c (numgcdh t g))"
haftmann@36853
   412
| "numgcdh t = (\<lambda>g. 1)"
haftmann@36853
   413
haftmann@36853
   414
definition numgcd :: "num \<Rightarrow> int" where
haftmann@36853
   415
  "numgcd t = numgcdh t (maxcoeff t)"
haftmann@29789
   416
haftmann@36853
   417
fun reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num" where
haftmann@29789
   418
  "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
haftmann@36853
   419
| "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
haftmann@36853
   420
| "reducecoeffh t = (\<lambda>g. t)"
haftmann@29789
   421
haftmann@36853
   422
definition reducecoeff :: "num \<Rightarrow> num" where
haftmann@36853
   423
  "reducecoeff t =
haftmann@29789
   424
  (let g = numgcd t in 
haftmann@29789
   425
  if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
haftmann@29789
   426
haftmann@36853
   427
fun dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool" where
haftmann@29789
   428
  "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
haftmann@36853
   429
| "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
haftmann@36853
   430
| "dvdnumcoeff t = (\<lambda>g. False)"
haftmann@29789
   431
haftmann@29789
   432
lemma dvdnumcoeff_trans: 
haftmann@29789
   433
  assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
haftmann@29789
   434
  shows "dvdnumcoeff t g"
haftmann@29789
   435
  using dgt' gdg 
nipkow@30042
   436
  by (induct t rule: dvdnumcoeff.induct, simp_all add: gdg dvd_trans[OF gdg])
haftmann@29789
   437
nipkow@30042
   438
declare dvd_trans [trans add]
haftmann@29789
   439
haftmann@29789
   440
lemma natabs0: "(nat (abs x) = 0) = (x = 0)"
haftmann@29789
   441
by arith
haftmann@29789
   442
haftmann@29789
   443
lemma numgcd0:
haftmann@29789
   444
  assumes g0: "numgcd t = 0"
haftmann@29789
   445
  shows "Inum bs t = 0"
haftmann@29789
   446
  using g0[simplified numgcd_def] 
haftmann@54863
   447
  by (induct t rule: numgcdh.induct, auto simp add: natabs0 maxcoeff_pos max.absorb2)
haftmann@29789
   448
haftmann@29789
   449
lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
haftmann@29789
   450
  using gp
huffman@31706
   451
  by (induct t rule: numgcdh.induct, auto)
haftmann@29789
   452
haftmann@29789
   453
lemma numgcd_pos: "numgcd t \<ge>0"
haftmann@29789
   454
  by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
haftmann@29789
   455
haftmann@29789
   456
lemma reducecoeffh:
haftmann@29789
   457
  assumes gt: "dvdnumcoeff t g" and gp: "g > 0" 
haftmann@29789
   458
  shows "real g *(Inum bs (reducecoeffh t g)) = Inum bs t"
haftmann@29789
   459
  using gt
wenzelm@41807
   460
proof (induct t rule: reducecoeffh.induct) 
wenzelm@41807
   461
  case (1 i)
wenzelm@41807
   462
  hence gd: "g dvd i" by simp
bulwahn@46670
   463
  with assms show ?case by (simp add: real_of_int_div[OF gd])
haftmann@29789
   464
next
wenzelm@41807
   465
  case (2 n c t)
wenzelm@41807
   466
  hence gd: "g dvd c" by simp
bulwahn@46670
   467
  from assms 2 show ?case by (simp add: real_of_int_div[OF gd] algebra_simps)
haftmann@29789
   468
qed (auto simp add: numgcd_def gp)
haftmann@36853
   469
haftmann@36853
   470
fun ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool" where
haftmann@29789
   471
  "ismaxcoeff (C i) = (\<lambda> x. abs i \<le> x)"
haftmann@36853
   472
| "ismaxcoeff (CN n c t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
haftmann@36853
   473
| "ismaxcoeff t = (\<lambda>x. True)"
haftmann@29789
   474
haftmann@29789
   475
lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
wenzelm@41807
   476
  by (induct t rule: ismaxcoeff.induct) auto
haftmann@29789
   477
haftmann@29789
   478
lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
haftmann@29789
   479
proof (induct t rule: maxcoeff.induct)
haftmann@29789
   480
  case (2 n c t)
haftmann@29789
   481
  hence H:"ismaxcoeff t (maxcoeff t)" .
wenzelm@41807
   482
  have thh: "maxcoeff t \<le> max (abs c) (maxcoeff t)" by simp
wenzelm@41807
   483
  from ismaxcoeff_mono[OF H thh] show ?case by simp
haftmann@29789
   484
qed simp_all
haftmann@29789
   485
huffman@31706
   486
lemma zgcd_gt1: "gcd i j > (1::int) \<Longrightarrow> ((abs i > 1 \<and> abs j > 1) \<or> (abs i = 0 \<and> abs j > 1) \<or> (abs i > 1 \<and> abs j = 0))"
huffman@31706
   487
  apply (cases "abs i = 0", simp_all add: gcd_int_def)
haftmann@29789
   488
  apply (cases "abs j = 0", simp_all)
haftmann@29789
   489
  apply (cases "abs i = 1", simp_all)
haftmann@29789
   490
  apply (cases "abs j = 1", simp_all)
haftmann@29789
   491
  apply auto
haftmann@29789
   492
  done
haftmann@29789
   493
lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
huffman@31706
   494
  by (induct t rule: numgcdh.induct, auto)
haftmann@29789
   495
haftmann@29789
   496
lemma dvdnumcoeff_aux:
haftmann@29789
   497
  assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
haftmann@29789
   498
  shows "dvdnumcoeff t (numgcdh t m)"
wenzelm@41807
   499
using assms
haftmann@29789
   500
proof(induct t rule: numgcdh.induct)
haftmann@29789
   501
  case (2 n c t) 
haftmann@29789
   502
  let ?g = "numgcdh t m"
wenzelm@41807
   503
  from 2 have th:"gcd c ?g > 1" by simp
haftmann@29789
   504
  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
haftmann@29789
   505
  have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
wenzelm@41807
   506
  moreover {assume "abs c > 1" and gp: "?g > 1" with 2
haftmann@29789
   507
    have th: "dvdnumcoeff t ?g" by simp
huffman@31706
   508
    have th': "gcd c ?g dvd ?g" by simp
huffman@31706
   509
    from dvdnumcoeff_trans[OF th' th] have ?case by simp }
haftmann@29789
   510
  moreover {assume "abs c = 0 \<and> ?g > 1"
wenzelm@41807
   511
    with 2 have th: "dvdnumcoeff t ?g" by simp
huffman@31706
   512
    have th': "gcd c ?g dvd ?g" by simp
huffman@31706
   513
    from dvdnumcoeff_trans[OF th' th] have ?case by simp
haftmann@29789
   514
    hence ?case by simp }
haftmann@29789
   515
  moreover {assume "abs c > 1" and g0:"?g = 0" 
wenzelm@41807
   516
    from numgcdh0[OF g0] have "m=0". with 2 g0 have ?case by simp }
haftmann@29789
   517
  ultimately show ?case by blast
huffman@31706
   518
qed auto
haftmann@29789
   519
haftmann@29789
   520
lemma dvdnumcoeff_aux2:
wenzelm@41807
   521
  assumes "numgcd t > 1"
wenzelm@41807
   522
  shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
wenzelm@41807
   523
  using assms
haftmann@29789
   524
proof (simp add: numgcd_def)
haftmann@29789
   525
  let ?mc = "maxcoeff t"
haftmann@29789
   526
  let ?g = "numgcdh t ?mc"
haftmann@29789
   527
  have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
haftmann@29789
   528
  have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
haftmann@29789
   529
  assume H: "numgcdh t ?mc > 1"
haftmann@29789
   530
  from dvdnumcoeff_aux[OF th1 th2 H]  show "dvdnumcoeff t ?g" .
haftmann@29789
   531
qed
haftmann@29789
   532
haftmann@29789
   533
lemma reducecoeff: "real (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
haftmann@29789
   534
proof-
haftmann@29789
   535
  let ?g = "numgcd t"
haftmann@29789
   536
  have "?g \<ge> 0"  by (simp add: numgcd_pos)
wenzelm@32960
   537
  hence "?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
haftmann@29789
   538
  moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)} 
haftmann@29789
   539
  moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)} 
haftmann@29789
   540
  moreover { assume g1:"?g > 1"
haftmann@29789
   541
    from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
haftmann@29789
   542
    from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis 
haftmann@29789
   543
      by (simp add: reducecoeff_def Let_def)} 
haftmann@29789
   544
  ultimately show ?thesis by blast
haftmann@29789
   545
qed
haftmann@29789
   546
haftmann@29789
   547
lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
haftmann@29789
   548
by (induct t rule: reducecoeffh.induct, auto)
haftmann@29789
   549
haftmann@29789
   550
lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
haftmann@29789
   551
using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
haftmann@29789
   552
haftmann@29789
   553
consts
haftmann@29789
   554
  numadd:: "num \<times> num \<Rightarrow> num"
haftmann@36853
   555
haftmann@29789
   556
recdef numadd "measure (\<lambda> (t,s). size t + size s)"
haftmann@29789
   557
  "numadd (CN n1 c1 r1,CN n2 c2 r2) =
haftmann@29789
   558
  (if n1=n2 then 
haftmann@29789
   559
  (let c = c1 + c2
haftmann@29789
   560
  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
haftmann@29789
   561
  else if n1 \<le> n2 then (CN n1 c1 (numadd (r1,CN n2 c2 r2))) 
haftmann@29789
   562
  else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
haftmann@29789
   563
  "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"  
haftmann@29789
   564
  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
haftmann@29789
   565
  "numadd (C b1, C b2) = C (b1+b2)"
haftmann@29789
   566
  "numadd (a,b) = Add a b"
haftmann@29789
   567
haftmann@29789
   568
lemma numadd[simp]: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
haftmann@29789
   569
apply (induct t s rule: numadd.induct, simp_all add: Let_def)
haftmann@29789
   570
apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
haftmann@29789
   571
apply (case_tac "n1 = n2", simp_all add: algebra_simps)
webertj@49962
   572
by (simp only: distrib_right[symmetric],simp)
haftmann@29789
   573
haftmann@29789
   574
lemma numadd_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
haftmann@29789
   575
by (induct t s rule: numadd.induct, auto simp add: Let_def)
haftmann@29789
   576
haftmann@36853
   577
fun nummul:: "num \<Rightarrow> int \<Rightarrow> num" where
haftmann@29789
   578
  "nummul (C j) = (\<lambda> i. C (i*j))"
haftmann@36853
   579
| "nummul (CN n c a) = (\<lambda> i. CN n (i*c) (nummul a i))"
haftmann@36853
   580
| "nummul t = (\<lambda> i. Mul i t)"
haftmann@29789
   581
haftmann@29789
   582
lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
haftmann@29789
   583
by (induct t rule: nummul.induct, auto simp add: algebra_simps)
haftmann@29789
   584
haftmann@29789
   585
lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
haftmann@29789
   586
by (induct t rule: nummul.induct, auto )
haftmann@29789
   587
haftmann@35416
   588
definition numneg :: "num \<Rightarrow> num" where
haftmann@36853
   589
  "numneg t = nummul t (- 1)"
haftmann@29789
   590
haftmann@35416
   591
definition numsub :: "num \<Rightarrow> num \<Rightarrow> num" where
haftmann@36853
   592
  "numsub s t = (if s = t then C 0 else numadd (s,numneg t))"
haftmann@29789
   593
haftmann@29789
   594
lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
haftmann@29789
   595
using numneg_def by simp
haftmann@29789
   596
haftmann@29789
   597
lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
haftmann@29789
   598
using numneg_def by simp
haftmann@29789
   599
haftmann@29789
   600
lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
haftmann@29789
   601
using numsub_def by simp
haftmann@29789
   602
haftmann@29789
   603
lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
haftmann@29789
   604
using numsub_def by simp
haftmann@29789
   605
haftmann@36853
   606
primrec simpnum:: "num \<Rightarrow> num" where
haftmann@29789
   607
  "simpnum (C j) = C j"
haftmann@36853
   608
| "simpnum (Bound n) = CN n 1 (C 0)"
haftmann@36853
   609
| "simpnum (Neg t) = numneg (simpnum t)"
haftmann@36853
   610
| "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
haftmann@36853
   611
| "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
haftmann@36853
   612
| "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
haftmann@36853
   613
| "simpnum (CN n c t) = (if c = 0 then simpnum t else numadd (CN n c (C 0),simpnum t))"
haftmann@29789
   614
haftmann@29789
   615
lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
haftmann@36853
   616
by (induct t) simp_all
haftmann@29789
   617
haftmann@29789
   618
lemma simpnum_numbound0[simp]: 
haftmann@29789
   619
  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
haftmann@36853
   620
by (induct t) simp_all
haftmann@29789
   621
haftmann@36853
   622
fun nozerocoeff:: "num \<Rightarrow> bool" where
haftmann@29789
   623
  "nozerocoeff (C c) = True"
haftmann@36853
   624
| "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
haftmann@36853
   625
| "nozerocoeff t = True"
haftmann@29789
   626
haftmann@29789
   627
lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
haftmann@29789
   628
by (induct a b rule: numadd.induct,auto simp add: Let_def)
haftmann@29789
   629
haftmann@29789
   630
lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
haftmann@29789
   631
by (induct a rule: nummul.induct,auto simp add: Let_def numadd_nz)
haftmann@29789
   632
haftmann@29789
   633
lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
haftmann@29789
   634
by (simp add: numneg_def nummul_nz)
haftmann@29789
   635
haftmann@29789
   636
lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
haftmann@29789
   637
by (simp add: numsub_def numneg_nz numadd_nz)
haftmann@29789
   638
haftmann@29789
   639
lemma simpnum_nz: "nozerocoeff (simpnum t)"
haftmann@36853
   640
by(induct t) (simp_all add: numadd_nz numneg_nz numsub_nz nummul_nz)
haftmann@29789
   641
haftmann@29789
   642
lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
haftmann@29789
   643
proof (induct t rule: maxcoeff.induct)
haftmann@29789
   644
  case (2 n c t)
wenzelm@41807
   645
  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp_all
wenzelm@41807
   646
  have "max (abs c) (maxcoeff t) \<ge> abs c" by simp
haftmann@29789
   647
  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
wenzelm@41807
   648
  with 2 show ?case by simp
haftmann@29789
   649
qed auto
haftmann@29789
   650
haftmann@29789
   651
lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
haftmann@29789
   652
proof-
haftmann@29789
   653
  from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
haftmann@29789
   654
  from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
haftmann@29789
   655
  from maxcoeff_nz[OF nz th] show ?thesis .
haftmann@29789
   656
qed
haftmann@29789
   657
haftmann@35416
   658
definition simp_num_pair :: "(num \<times> int) \<Rightarrow> num \<times> int" where
haftmann@36853
   659
  "simp_num_pair = (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
haftmann@29789
   660
   (let t' = simpnum t ; g = numgcd t' in 
huffman@31706
   661
      if g > 1 then (let g' = gcd n g in 
haftmann@29789
   662
        if g' = 1 then (t',n) 
haftmann@29789
   663
        else (reducecoeffh t' g', n div g')) 
haftmann@29789
   664
      else (t',n))))"
haftmann@29789
   665
haftmann@29789
   666
lemma simp_num_pair_ci:
haftmann@29789
   667
  shows "((\<lambda> (t,n). Inum bs t / real n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real n) (t,n))"
haftmann@29789
   668
  (is "?lhs = ?rhs")
haftmann@29789
   669
proof-
haftmann@29789
   670
  let ?t' = "simpnum t"
haftmann@29789
   671
  let ?g = "numgcd ?t'"
huffman@31706
   672
  let ?g' = "gcd n ?g"
haftmann@29789
   673
  {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
haftmann@29789
   674
  moreover
haftmann@29789
   675
  { assume nnz: "n \<noteq> 0"
wenzelm@44779
   676
    {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def) }
haftmann@29789
   677
    moreover
haftmann@29789
   678
    {assume g1:"?g>1" hence g0: "?g > 0" by simp
huffman@31706
   679
      from g1 nnz have gp0: "?g' \<noteq> 0" by simp
nipkow@31952
   680
      hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith 
haftmann@29789
   681
      hence "?g'= 1 \<or> ?g' > 1" by arith
wenzelm@44779
   682
      moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
haftmann@29789
   683
      moreover {assume g'1:"?g'>1"
wenzelm@32960
   684
        from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
wenzelm@32960
   685
        let ?tt = "reducecoeffh ?t' ?g'"
wenzelm@32960
   686
        let ?t = "Inum bs ?tt"
wenzelm@32960
   687
        have gpdg: "?g' dvd ?g" by simp
wenzelm@32960
   688
        have gpdd: "?g' dvd n" by simp 
wenzelm@32960
   689
        have gpdgp: "?g' dvd ?g'" by simp
wenzelm@32960
   690
        from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
wenzelm@32960
   691
        have th2:"real ?g' * ?t = Inum bs ?t'" by simp
wenzelm@41807
   692
        from g1 g'1 have "?lhs = ?t / real (n div ?g')" by (simp add: simp_num_pair_def Let_def)
wenzelm@32960
   693
        also have "\<dots> = (real ?g' * ?t) / (real ?g' * (real (n div ?g')))" by simp
wenzelm@32960
   694
        also have "\<dots> = (Inum bs ?t' / real n)"
bulwahn@46670
   695
          using real_of_int_div[OF gpdd] th2 gp0 by simp
wenzelm@41807
   696
        finally have "?lhs = Inum bs t / real n" by simp
wenzelm@41807
   697
        then have ?thesis by (simp add: simp_num_pair_def) }
wenzelm@41807
   698
      ultimately have ?thesis by blast }
wenzelm@41807
   699
    ultimately have ?thesis by blast }
haftmann@29789
   700
  ultimately show ?thesis by blast
haftmann@29789
   701
qed
haftmann@29789
   702
haftmann@29789
   703
lemma simp_num_pair_l: assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
haftmann@29789
   704
  shows "numbound0 t' \<and> n' >0"
haftmann@29789
   705
proof-
wenzelm@41807
   706
  let ?t' = "simpnum t"
haftmann@29789
   707
  let ?g = "numgcd ?t'"
huffman@31706
   708
  let ?g' = "gcd n ?g"
wenzelm@41807
   709
  { assume nz: "n = 0" hence ?thesis using assms by (simp add: Let_def simp_num_pair_def) }
haftmann@29789
   710
  moreover
haftmann@29789
   711
  { assume nnz: "n \<noteq> 0"
wenzelm@41807
   712
    { assume "\<not> ?g > 1" hence ?thesis using assms
wenzelm@41807
   713
        by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0) }
haftmann@29789
   714
    moreover
wenzelm@41807
   715
    { assume g1:"?g>1" hence g0: "?g > 0" by simp
huffman@31706
   716
      from g1 nnz have gp0: "?g' \<noteq> 0" by simp
nipkow@31952
   717
      hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith
haftmann@29789
   718
      hence "?g'= 1 \<or> ?g' > 1" by arith
wenzelm@41807
   719
      moreover {
wenzelm@41807
   720
        assume "?g' = 1" hence ?thesis using assms g1
wenzelm@41807
   721
          by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0) }
wenzelm@41807
   722
      moreover {
wenzelm@41807
   723
        assume g'1: "?g' > 1"
wenzelm@32960
   724
        have gpdg: "?g' dvd ?g" by simp
wenzelm@41807
   725
        have gpdd: "?g' dvd n" by simp
wenzelm@32960
   726
        have gpdgp: "?g' dvd ?g'" by simp
wenzelm@32960
   727
        from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
huffman@47142
   728
        from zdiv_mono1[OF g'n g'p, simplified div_self[OF gp0]]
wenzelm@32960
   729
        have "n div ?g' >0" by simp
wenzelm@41807
   730
        hence ?thesis using assms g1 g'1
wenzelm@41807
   731
          by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0 simpnum_numbound0) }
wenzelm@41807
   732
      ultimately have ?thesis by blast }
wenzelm@41807
   733
    ultimately have ?thesis by blast }
haftmann@29789
   734
  ultimately show ?thesis by blast
haftmann@29789
   735
qed
haftmann@29789
   736
haftmann@36853
   737
fun simpfm :: "fm \<Rightarrow> fm" where
haftmann@29789
   738
  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
haftmann@36853
   739
| "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
haftmann@36853
   740
| "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
haftmann@36853
   741
| "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
haftmann@36853
   742
| "simpfm (NOT p) = not (simpfm p)"
haftmann@36853
   743
| "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
haftmann@29789
   744
  | _ \<Rightarrow> Lt a')"
haftmann@36853
   745
| "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le a')"
haftmann@36853
   746
| "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt a')"
haftmann@36853
   747
| "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge a')"
haftmann@36853
   748
| "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq a')"
haftmann@36853
   749
| "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq a')"
haftmann@36853
   750
| "simpfm p = p"
haftmann@29789
   751
lemma simpfm: "Ifm bs (simpfm p) = Ifm bs p"
haftmann@29789
   752
proof(induct p rule: simpfm.induct)
haftmann@29789
   753
  case (6 a) let ?sa = "simpnum a" from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
haftmann@29789
   754
  {fix v assume "?sa = C v" hence ?case using sa by simp }
haftmann@29789
   755
  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
haftmann@29789
   756
      by (cases ?sa, simp_all add: Let_def)}
haftmann@29789
   757
  ultimately show ?case by blast
haftmann@29789
   758
next
haftmann@29789
   759
  case (7 a)  let ?sa = "simpnum a" 
haftmann@29789
   760
  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
haftmann@29789
   761
  {fix v assume "?sa = C v" hence ?case using sa by simp }
haftmann@29789
   762
  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
haftmann@29789
   763
      by (cases ?sa, simp_all add: Let_def)}
haftmann@29789
   764
  ultimately show ?case by blast
haftmann@29789
   765
next
haftmann@29789
   766
  case (8 a)  let ?sa = "simpnum a" 
haftmann@29789
   767
  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
haftmann@29789
   768
  {fix v assume "?sa = C v" hence ?case using sa by simp }
haftmann@29789
   769
  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
haftmann@29789
   770
      by (cases ?sa, simp_all add: Let_def)}
haftmann@29789
   771
  ultimately show ?case by blast
haftmann@29789
   772
next
haftmann@29789
   773
  case (9 a)  let ?sa = "simpnum a" 
haftmann@29789
   774
  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
haftmann@29789
   775
  {fix v assume "?sa = C v" hence ?case using sa by simp }
haftmann@29789
   776
  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
haftmann@29789
   777
      by (cases ?sa, simp_all add: Let_def)}
haftmann@29789
   778
  ultimately show ?case by blast
haftmann@29789
   779
next
haftmann@29789
   780
  case (10 a)  let ?sa = "simpnum a" 
haftmann@29789
   781
  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
haftmann@29789
   782
  {fix v assume "?sa = C v" hence ?case using sa by simp }
haftmann@29789
   783
  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
haftmann@29789
   784
      by (cases ?sa, simp_all add: Let_def)}
haftmann@29789
   785
  ultimately show ?case by blast
haftmann@29789
   786
next
haftmann@29789
   787
  case (11 a)  let ?sa = "simpnum a" 
haftmann@29789
   788
  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
haftmann@29789
   789
  {fix v assume "?sa = C v" hence ?case using sa by simp }
haftmann@29789
   790
  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
haftmann@29789
   791
      by (cases ?sa, simp_all add: Let_def)}
haftmann@29789
   792
  ultimately show ?case by blast
haftmann@29789
   793
qed (induct p rule: simpfm.induct, simp_all add: conj disj imp iff not)
haftmann@29789
   794
haftmann@29789
   795
haftmann@29789
   796
lemma simpfm_bound0: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
haftmann@29789
   797
proof(induct p rule: simpfm.induct)
haftmann@29789
   798
  case (6 a) hence nb: "numbound0 a" by simp
haftmann@29789
   799
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
wenzelm@44779
   800
  thus ?case by (cases "simpnum a") (auto simp add: Let_def)
haftmann@29789
   801
next
haftmann@29789
   802
  case (7 a) hence nb: "numbound0 a" by simp
haftmann@29789
   803
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
wenzelm@44779
   804
  thus ?case by (cases "simpnum a") (auto simp add: Let_def)
haftmann@29789
   805
next
haftmann@29789
   806
  case (8 a) hence nb: "numbound0 a" by simp
haftmann@29789
   807
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
wenzelm@44779
   808
  thus ?case by (cases "simpnum a") (auto simp add: Let_def)
haftmann@29789
   809
next
haftmann@29789
   810
  case (9 a) hence nb: "numbound0 a" by simp
haftmann@29789
   811
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
wenzelm@44779
   812
  thus ?case by (cases "simpnum a") (auto simp add: Let_def)
haftmann@29789
   813
next
haftmann@29789
   814
  case (10 a) hence nb: "numbound0 a" by simp
haftmann@29789
   815
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
wenzelm@44779
   816
  thus ?case by (cases "simpnum a") (auto simp add: Let_def)
haftmann@29789
   817
next
haftmann@29789
   818
  case (11 a) hence nb: "numbound0 a" by simp
haftmann@29789
   819
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
wenzelm@44779
   820
  thus ?case by (cases "simpnum a") (auto simp add: Let_def)
haftmann@29789
   821
qed(auto simp add: disj_def imp_def iff_def conj_def not_bn)
haftmann@29789
   822
haftmann@29789
   823
lemma simpfm_qf: "qfree p \<Longrightarrow> qfree (simpfm p)"
wenzelm@44779
   824
  apply (induct p rule: simpfm.induct)
wenzelm@44779
   825
  apply (auto simp add: Let_def)
wenzelm@44779
   826
  apply (case_tac "simpnum a", auto)+
wenzelm@44779
   827
  done
haftmann@29789
   828
haftmann@29789
   829
consts prep :: "fm \<Rightarrow> fm"
haftmann@29789
   830
recdef prep "measure fmsize"
haftmann@29789
   831
  "prep (E T) = T"
haftmann@29789
   832
  "prep (E F) = F"
haftmann@29789
   833
  "prep (E (Or p q)) = disj (prep (E p)) (prep (E q))"
haftmann@29789
   834
  "prep (E (Imp p q)) = disj (prep (E (NOT p))) (prep (E q))"
haftmann@29789
   835
  "prep (E (Iff p q)) = disj (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
haftmann@29789
   836
  "prep (E (NOT (And p q))) = disj (prep (E (NOT p))) (prep (E(NOT q)))"
haftmann@29789
   837
  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
haftmann@29789
   838
  "prep (E (NOT (Iff p q))) = disj (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
haftmann@29789
   839
  "prep (E p) = E (prep p)"
haftmann@29789
   840
  "prep (A (And p q)) = conj (prep (A p)) (prep (A q))"
haftmann@29789
   841
  "prep (A p) = prep (NOT (E (NOT p)))"
haftmann@29789
   842
  "prep (NOT (NOT p)) = prep p"
haftmann@29789
   843
  "prep (NOT (And p q)) = disj (prep (NOT p)) (prep (NOT q))"
haftmann@29789
   844
  "prep (NOT (A p)) = prep (E (NOT p))"
haftmann@29789
   845
  "prep (NOT (Or p q)) = conj (prep (NOT p)) (prep (NOT q))"
haftmann@29789
   846
  "prep (NOT (Imp p q)) = conj (prep p) (prep (NOT q))"
haftmann@29789
   847
  "prep (NOT (Iff p q)) = disj (prep (And p (NOT q))) (prep (And (NOT p) q))"
haftmann@29789
   848
  "prep (NOT p) = not (prep p)"
haftmann@29789
   849
  "prep (Or p q) = disj (prep p) (prep q)"
haftmann@29789
   850
  "prep (And p q) = conj (prep p) (prep q)"
haftmann@29789
   851
  "prep (Imp p q) = prep (Or (NOT p) q)"
haftmann@29789
   852
  "prep (Iff p q) = disj (prep (And p q)) (prep (And (NOT p) (NOT q)))"
haftmann@29789
   853
  "prep p = p"
haftmann@29789
   854
(hints simp add: fmsize_pos)
haftmann@29789
   855
lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
wenzelm@44779
   856
  by (induct p rule: prep.induct) auto
haftmann@29789
   857
haftmann@29789
   858
  (* Generic quantifier elimination *)
haftmann@36853
   859
function (sequential) qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm" where
haftmann@29789
   860
  "qelim (E p) = (\<lambda> qe. DJ qe (qelim p qe))"
haftmann@36853
   861
| "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
haftmann@36853
   862
| "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
haftmann@36853
   863
| "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
haftmann@36853
   864
| "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
haftmann@36853
   865
| "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
haftmann@36853
   866
| "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
haftmann@36853
   867
| "qelim p = (\<lambda> y. simpfm p)"
haftmann@36853
   868
by pat_completeness auto
haftmann@36853
   869
termination qelim by (relation "measure fmsize") simp_all
haftmann@29789
   870
haftmann@29789
   871
lemma qelim_ci:
haftmann@29789
   872
  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
haftmann@29789
   873
  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
haftmann@29789
   874
using qe_inv DJ_qe[OF qe_inv] 
haftmann@29789
   875
by(induct p rule: qelim.induct) 
haftmann@29789
   876
(auto simp add: not disj conj iff imp not_qf disj_qf conj_qf imp_qf iff_qf 
haftmann@29789
   877
  simpfm simpfm_qf simp del: simpfm.simps)
haftmann@29789
   878
haftmann@36853
   879
fun minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*) where
haftmann@29789
   880
  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
haftmann@36853
   881
| "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
haftmann@36853
   882
| "minusinf (Eq  (CN 0 c e)) = F"
haftmann@36853
   883
| "minusinf (NEq (CN 0 c e)) = T"
haftmann@36853
   884
| "minusinf (Lt  (CN 0 c e)) = T"
haftmann@36853
   885
| "minusinf (Le  (CN 0 c e)) = T"
haftmann@36853
   886
| "minusinf (Gt  (CN 0 c e)) = F"
haftmann@36853
   887
| "minusinf (Ge  (CN 0 c e)) = F"
haftmann@36853
   888
| "minusinf p = p"
haftmann@29789
   889
haftmann@36853
   890
fun plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*) where
haftmann@29789
   891
  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
haftmann@36853
   892
| "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
haftmann@36853
   893
| "plusinf (Eq  (CN 0 c e)) = F"
haftmann@36853
   894
| "plusinf (NEq (CN 0 c e)) = T"
haftmann@36853
   895
| "plusinf (Lt  (CN 0 c e)) = F"
haftmann@36853
   896
| "plusinf (Le  (CN 0 c e)) = F"
haftmann@36853
   897
| "plusinf (Gt  (CN 0 c e)) = T"
haftmann@36853
   898
| "plusinf (Ge  (CN 0 c e)) = T"
haftmann@36853
   899
| "plusinf p = p"
haftmann@29789
   900
haftmann@36853
   901
fun isrlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *) where
haftmann@29789
   902
  "isrlfm (And p q) = (isrlfm p \<and> isrlfm q)" 
haftmann@36853
   903
| "isrlfm (Or p q) = (isrlfm p \<and> isrlfm q)" 
haftmann@36853
   904
| "isrlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
haftmann@36853
   905
| "isrlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
haftmann@36853
   906
| "isrlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
haftmann@36853
   907
| "isrlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
haftmann@36853
   908
| "isrlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
haftmann@36853
   909
| "isrlfm (Ge  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
haftmann@36853
   910
| "isrlfm p = (isatom p \<and> (bound0 p))"
haftmann@29789
   911
haftmann@29789
   912
  (* splits the bounded from the unbounded part*)
haftmann@36853
   913
function (sequential) rsplit0 :: "num \<Rightarrow> int \<times> num" where
haftmann@29789
   914
  "rsplit0 (Bound 0) = (1,C 0)"
haftmann@36853
   915
| "rsplit0 (Add a b) = (let (ca,ta) = rsplit0 a ; (cb,tb) = rsplit0 b 
haftmann@29789
   916
              in (ca+cb, Add ta tb))"
haftmann@36853
   917
| "rsplit0 (Sub a b) = rsplit0 (Add a (Neg b))"
haftmann@36853
   918
| "rsplit0 (Neg a) = (let (c,t) = rsplit0 a in (-c,Neg t))"
haftmann@36853
   919
| "rsplit0 (Mul c a) = (let (ca,ta) = rsplit0 a in (c*ca,Mul c ta))"
haftmann@36853
   920
| "rsplit0 (CN 0 c a) = (let (ca,ta) = rsplit0 a in (c+ca,ta))"
haftmann@36853
   921
| "rsplit0 (CN n c a) = (let (ca,ta) = rsplit0 a in (ca,CN n c ta))"
haftmann@36853
   922
| "rsplit0 t = (0,t)"
haftmann@36853
   923
by pat_completeness auto
haftmann@36853
   924
termination rsplit0 by (relation "measure num_size") simp_all
haftmann@36853
   925
haftmann@29789
   926
lemma rsplit0: 
haftmann@29789
   927
  shows "Inum bs ((split (CN 0)) (rsplit0 t)) = Inum bs t \<and> numbound0 (snd (rsplit0 t))"
haftmann@29789
   928
proof (induct t rule: rsplit0.induct)
haftmann@29789
   929
  case (2 a b) 
haftmann@29789
   930
  let ?sa = "rsplit0 a" let ?sb = "rsplit0 b"
haftmann@29789
   931
  let ?ca = "fst ?sa" let ?cb = "fst ?sb"
haftmann@29789
   932
  let ?ta = "snd ?sa" let ?tb = "snd ?sb"
wenzelm@41807
   933
  from 2 have nb: "numbound0 (snd(rsplit0 (Add a b)))" 
haftmann@36853
   934
    by (cases "rsplit0 a") (auto simp add: Let_def split_def)
haftmann@29789
   935
  have "Inum bs ((split (CN 0)) (rsplit0 (Add a b))) = 
haftmann@29789
   936
    Inum bs ((split (CN 0)) ?sa)+Inum bs ((split (CN 0)) ?sb)"
haftmann@29789
   937
    by (simp add: Let_def split_def algebra_simps)
wenzelm@41807
   938
  also have "\<dots> = Inum bs a + Inum bs b" using 2 by (cases "rsplit0 a") auto
haftmann@29789
   939
  finally show ?case using nb by simp 
webertj@49962
   940
qed (auto simp add: Let_def split_def algebra_simps, simp add: distrib_left[symmetric])
haftmann@29789
   941
haftmann@29789
   942
    (* Linearize a formula*)
haftmann@29789
   943
definition
haftmann@29789
   944
  lt :: "int \<Rightarrow> num \<Rightarrow> fm"
haftmann@29789
   945
where
haftmann@29789
   946
  "lt c t = (if c = 0 then (Lt t) else if c > 0 then (Lt (CN 0 c t)) 
haftmann@29789
   947
    else (Gt (CN 0 (-c) (Neg t))))"
haftmann@29789
   948
haftmann@29789
   949
definition
haftmann@29789
   950
  le :: "int \<Rightarrow> num \<Rightarrow> fm"
haftmann@29789
   951
where
haftmann@29789
   952
  "le c t = (if c = 0 then (Le t) else if c > 0 then (Le (CN 0 c t)) 
haftmann@29789
   953
    else (Ge (CN 0 (-c) (Neg t))))"
haftmann@29789
   954
haftmann@29789
   955
definition
haftmann@29789
   956
  gt :: "int \<Rightarrow> num \<Rightarrow> fm"
haftmann@29789
   957
where
haftmann@29789
   958
  "gt c t = (if c = 0 then (Gt t) else if c > 0 then (Gt (CN 0 c t)) 
haftmann@29789
   959
    else (Lt (CN 0 (-c) (Neg t))))"
haftmann@29789
   960
haftmann@29789
   961
definition
haftmann@29789
   962
  ge :: "int \<Rightarrow> num \<Rightarrow> fm"
haftmann@29789
   963
where
haftmann@29789
   964
  "ge c t = (if c = 0 then (Ge t) else if c > 0 then (Ge (CN 0 c t)) 
haftmann@29789
   965
    else (Le (CN 0 (-c) (Neg t))))"
haftmann@29789
   966
haftmann@29789
   967
definition
haftmann@29789
   968
  eq :: "int \<Rightarrow> num \<Rightarrow> fm"
haftmann@29789
   969
where
haftmann@29789
   970
  "eq c t = (if c = 0 then (Eq t) else if c > 0 then (Eq (CN 0 c t)) 
haftmann@29789
   971
    else (Eq (CN 0 (-c) (Neg t))))"
haftmann@29789
   972
haftmann@29789
   973
definition
haftmann@29789
   974
  neq :: "int \<Rightarrow> num \<Rightarrow> fm"
haftmann@29789
   975
where
haftmann@29789
   976
  "neq c t = (if c = 0 then (NEq t) else if c > 0 then (NEq (CN 0 c t)) 
haftmann@29789
   977
    else (NEq (CN 0 (-c) (Neg t))))"
haftmann@29789
   978
haftmann@29789
   979
lemma lt: "numnoabs t \<Longrightarrow> Ifm bs (split lt (rsplit0 t)) = Ifm bs (Lt t) \<and> isrlfm (split lt (rsplit0 t))"
haftmann@29789
   980
using rsplit0[where bs = "bs" and t="t"]
blanchet@58259
   981
by (auto simp add: lt_def split_def,cases "snd(rsplit0 t)",auto,rename_tac nat a b,case_tac "nat",auto)
haftmann@29789
   982
haftmann@29789
   983
lemma le: "numnoabs t \<Longrightarrow> Ifm bs (split le (rsplit0 t)) = Ifm bs (Le t) \<and> isrlfm (split le (rsplit0 t))"
haftmann@29789
   984
using rsplit0[where bs = "bs" and t="t"]
blanchet@58259
   985
by (auto simp add: le_def split_def) (cases "snd(rsplit0 t)",auto,rename_tac nat a b,case_tac "nat",auto)
haftmann@29789
   986
haftmann@29789
   987
lemma gt: "numnoabs t \<Longrightarrow> Ifm bs (split gt (rsplit0 t)) = Ifm bs (Gt t) \<and> isrlfm (split gt (rsplit0 t))"
haftmann@29789
   988
using rsplit0[where bs = "bs" and t="t"]
blanchet@58259
   989
by (auto simp add: gt_def split_def) (cases "snd(rsplit0 t)",auto,rename_tac nat a b,case_tac "nat",auto)
haftmann@29789
   990
haftmann@29789
   991
lemma ge: "numnoabs t \<Longrightarrow> Ifm bs (split ge (rsplit0 t)) = Ifm bs (Ge t) \<and> isrlfm (split ge (rsplit0 t))"
haftmann@29789
   992
using rsplit0[where bs = "bs" and t="t"]
blanchet@58259
   993
by (auto simp add: ge_def split_def) (cases "snd(rsplit0 t)",auto,rename_tac nat a b,case_tac "nat",auto)
haftmann@29789
   994
haftmann@29789
   995
lemma eq: "numnoabs t \<Longrightarrow> Ifm bs (split eq (rsplit0 t)) = Ifm bs (Eq t) \<and> isrlfm (split eq (rsplit0 t))"
haftmann@29789
   996
using rsplit0[where bs = "bs" and t="t"]
blanchet@58259
   997
by (auto simp add: eq_def split_def) (cases "snd(rsplit0 t)",auto,rename_tac nat a b,case_tac "nat",auto)
haftmann@29789
   998
haftmann@29789
   999
lemma neq: "numnoabs t \<Longrightarrow> Ifm bs (split neq (rsplit0 t)) = Ifm bs (NEq t) \<and> isrlfm (split neq (rsplit0 t))"
haftmann@29789
  1000
using rsplit0[where bs = "bs" and t="t"]
blanchet@58259
  1001
by (auto simp add: neq_def split_def) (cases "snd(rsplit0 t)",auto,rename_tac nat a b,case_tac "nat",auto)
haftmann@29789
  1002
haftmann@29789
  1003
lemma conj_lin: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (conj p q)"
haftmann@29789
  1004
by (auto simp add: conj_def)
haftmann@29789
  1005
lemma disj_lin: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (disj p q)"
haftmann@29789
  1006
by (auto simp add: disj_def)
haftmann@29789
  1007
haftmann@29789
  1008
consts rlfm :: "fm \<Rightarrow> fm"
haftmann@29789
  1009
recdef rlfm "measure fmsize"
haftmann@29789
  1010
  "rlfm (And p q) = conj (rlfm p) (rlfm q)"
haftmann@29789
  1011
  "rlfm (Or p q) = disj (rlfm p) (rlfm q)"
haftmann@29789
  1012
  "rlfm (Imp p q) = disj (rlfm (NOT p)) (rlfm q)"
haftmann@29789
  1013
  "rlfm (Iff p q) = disj (conj (rlfm p) (rlfm q)) (conj (rlfm (NOT p)) (rlfm (NOT q)))"
haftmann@29789
  1014
  "rlfm (Lt a) = split lt (rsplit0 a)"
haftmann@29789
  1015
  "rlfm (Le a) = split le (rsplit0 a)"
haftmann@29789
  1016
  "rlfm (Gt a) = split gt (rsplit0 a)"
haftmann@29789
  1017
  "rlfm (Ge a) = split ge (rsplit0 a)"
haftmann@29789
  1018
  "rlfm (Eq a) = split eq (rsplit0 a)"
haftmann@29789
  1019
  "rlfm (NEq a) = split neq (rsplit0 a)"
haftmann@29789
  1020
  "rlfm (NOT (And p q)) = disj (rlfm (NOT p)) (rlfm (NOT q))"
haftmann@29789
  1021
  "rlfm (NOT (Or p q)) = conj (rlfm (NOT p)) (rlfm (NOT q))"
haftmann@29789
  1022
  "rlfm (NOT (Imp p q)) = conj (rlfm p) (rlfm (NOT q))"
haftmann@29789
  1023
  "rlfm (NOT (Iff p q)) = disj (conj(rlfm p) (rlfm(NOT q))) (conj(rlfm(NOT p)) (rlfm q))"
haftmann@29789
  1024
  "rlfm (NOT (NOT p)) = rlfm p"
haftmann@29789
  1025
  "rlfm (NOT T) = F"
haftmann@29789
  1026
  "rlfm (NOT F) = T"
haftmann@29789
  1027
  "rlfm (NOT (Lt a)) = rlfm (Ge a)"
haftmann@29789
  1028
  "rlfm (NOT (Le a)) = rlfm (Gt a)"
haftmann@29789
  1029
  "rlfm (NOT (Gt a)) = rlfm (Le a)"
haftmann@29789
  1030
  "rlfm (NOT (Ge a)) = rlfm (Lt a)"
haftmann@29789
  1031
  "rlfm (NOT (Eq a)) = rlfm (NEq a)"
haftmann@29789
  1032
  "rlfm (NOT (NEq a)) = rlfm (Eq a)"
haftmann@29789
  1033
  "rlfm p = p" (hints simp add: fmsize_pos)
haftmann@29789
  1034
haftmann@29789
  1035
lemma rlfm_I:
haftmann@29789
  1036
  assumes qfp: "qfree p"
haftmann@29789
  1037
  shows "(Ifm bs (rlfm p) = Ifm bs p) \<and> isrlfm (rlfm p)"
haftmann@29789
  1038
  using qfp 
wenzelm@44779
  1039
by (induct p rule: rlfm.induct) (auto simp add: lt le gt ge eq neq conj disj conj_lin disj_lin)
haftmann@29789
  1040
haftmann@29789
  1041
    (* Operations needed for Ferrante and Rackoff *)
haftmann@29789
  1042
lemma rminusinf_inf:
haftmann@29789
  1043
  assumes lp: "isrlfm p"
haftmann@29789
  1044
  shows "\<exists> z. \<forall> x < z. Ifm (x#bs) (minusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
haftmann@29789
  1045
using lp
haftmann@29789
  1046
proof (induct p rule: minusinf.induct)
wenzelm@44779
  1047
  case (1 p q)
wenzelm@44779
  1048
  thus ?case apply auto apply (rule_tac x= "min z za" in exI) apply auto done
haftmann@29789
  1049
next
wenzelm@44779
  1050
  case (2 p q)
wenzelm@44779
  1051
  thus ?case apply auto apply (rule_tac x= "min z za" in exI) apply auto done
haftmann@29789
  1052
next
haftmann@29789
  1053
  case (3 c e) 
wenzelm@41807
  1054
  from 3 have nb: "numbound0 e" by simp
wenzelm@41807
  1055
  from 3 have cp: "real c > 0" by simp
haftmann@29789
  1056
  fix a
haftmann@29789
  1057
  let ?e="Inum (a#bs) e"
haftmann@29789
  1058
  let ?z = "(- ?e) / real c"
haftmann@29789
  1059
  {fix x
haftmann@29789
  1060
    assume xz: "x < ?z"
haftmann@29789
  1061
    hence "(real c * x < - ?e)" 
haftmann@57514
  1062
      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps) 
haftmann@29789
  1063
    hence "real c * x + ?e < 0" by arith
haftmann@29789
  1064
    hence "real c * x + ?e \<noteq> 0" by simp
haftmann@29789
  1065
    with xz have "?P ?z x (Eq (CN 0 c e))"
haftmann@29789
  1066
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp  }
haftmann@29789
  1067
  hence "\<forall> x < ?z. ?P ?z x (Eq (CN 0 c e))" by simp
haftmann@29789
  1068
  thus ?case by blast
haftmann@29789
  1069
next
haftmann@29789
  1070
  case (4 c e)   
wenzelm@41807
  1071
  from 4 have nb: "numbound0 e" by simp
wenzelm@41807
  1072
  from 4 have cp: "real c > 0" by simp
haftmann@29789
  1073
  fix a
haftmann@29789
  1074
  let ?e="Inum (a#bs) e"
haftmann@29789
  1075
  let ?z = "(- ?e) / real c"
haftmann@29789
  1076
  {fix x
haftmann@29789
  1077
    assume xz: "x < ?z"
haftmann@29789
  1078
    hence "(real c * x < - ?e)" 
haftmann@57514
  1079
      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps) 
haftmann@29789
  1080
    hence "real c * x + ?e < 0" by arith
haftmann@29789
  1081
    hence "real c * x + ?e \<noteq> 0" by simp
haftmann@29789
  1082
    with xz have "?P ?z x (NEq (CN 0 c e))"
haftmann@29789
  1083
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1084
  hence "\<forall> x < ?z. ?P ?z x (NEq (CN 0 c e))" by simp
haftmann@29789
  1085
  thus ?case by blast
haftmann@29789
  1086
next
haftmann@29789
  1087
  case (5 c e) 
wenzelm@41807
  1088
  from 5 have nb: "numbound0 e" by simp
wenzelm@41807
  1089
  from 5 have cp: "real c > 0" by simp
haftmann@29789
  1090
  fix a
haftmann@29789
  1091
  let ?e="Inum (a#bs) e"
haftmann@29789
  1092
  let ?z = "(- ?e) / real c"
haftmann@29789
  1093
  {fix x
haftmann@29789
  1094
    assume xz: "x < ?z"
haftmann@29789
  1095
    hence "(real c * x < - ?e)" 
haftmann@57514
  1096
      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps) 
haftmann@29789
  1097
    hence "real c * x + ?e < 0" by arith
haftmann@29789
  1098
    with xz have "?P ?z x (Lt (CN 0 c e))"
haftmann@29789
  1099
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]  by simp }
haftmann@29789
  1100
  hence "\<forall> x < ?z. ?P ?z x (Lt (CN 0 c e))" by simp
haftmann@29789
  1101
  thus ?case by blast
haftmann@29789
  1102
next
haftmann@29789
  1103
  case (6 c e)  
wenzelm@41807
  1104
  from 6 have nb: "numbound0 e" by simp
wenzelm@41807
  1105
  from lp 6 have cp: "real c > 0" by simp
haftmann@29789
  1106
  fix a
haftmann@29789
  1107
  let ?e="Inum (a#bs) e"
haftmann@29789
  1108
  let ?z = "(- ?e) / real c"
haftmann@29789
  1109
  {fix x
haftmann@29789
  1110
    assume xz: "x < ?z"
haftmann@29789
  1111
    hence "(real c * x < - ?e)" 
haftmann@57514
  1112
      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps) 
haftmann@29789
  1113
    hence "real c * x + ?e < 0" by arith
haftmann@29789
  1114
    with xz have "?P ?z x (Le (CN 0 c e))"
haftmann@29789
  1115
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1116
  hence "\<forall> x < ?z. ?P ?z x (Le (CN 0 c e))" by simp
haftmann@29789
  1117
  thus ?case by blast
haftmann@29789
  1118
next
haftmann@29789
  1119
  case (7 c e)  
wenzelm@41807
  1120
  from 7 have nb: "numbound0 e" by simp
wenzelm@41807
  1121
  from 7 have cp: "real c > 0" by simp
haftmann@29789
  1122
  fix a
haftmann@29789
  1123
  let ?e="Inum (a#bs) e"
haftmann@29789
  1124
  let ?z = "(- ?e) / real c"
haftmann@29789
  1125
  {fix x
haftmann@29789
  1126
    assume xz: "x < ?z"
haftmann@29789
  1127
    hence "(real c * x < - ?e)" 
haftmann@57514
  1128
      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps) 
haftmann@29789
  1129
    hence "real c * x + ?e < 0" by arith
haftmann@29789
  1130
    with xz have "?P ?z x (Gt (CN 0 c e))"
haftmann@29789
  1131
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1132
  hence "\<forall> x < ?z. ?P ?z x (Gt (CN 0 c e))" by simp
haftmann@29789
  1133
  thus ?case by blast
haftmann@29789
  1134
next
haftmann@29789
  1135
  case (8 c e)  
wenzelm@41807
  1136
  from 8 have nb: "numbound0 e" by simp
wenzelm@41807
  1137
  from 8 have cp: "real c > 0" by simp
haftmann@29789
  1138
  fix a
haftmann@29789
  1139
  let ?e="Inum (a#bs) e"
haftmann@29789
  1140
  let ?z = "(- ?e) / real c"
haftmann@29789
  1141
  {fix x
haftmann@29789
  1142
    assume xz: "x < ?z"
haftmann@29789
  1143
    hence "(real c * x < - ?e)" 
haftmann@57514
  1144
      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps) 
haftmann@29789
  1145
    hence "real c * x + ?e < 0" by arith
haftmann@29789
  1146
    with xz have "?P ?z x (Ge (CN 0 c e))"
haftmann@29789
  1147
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1148
  hence "\<forall> x < ?z. ?P ?z x (Ge (CN 0 c e))" by simp
haftmann@29789
  1149
  thus ?case by blast
haftmann@29789
  1150
qed simp_all
haftmann@29789
  1151
haftmann@29789
  1152
lemma rplusinf_inf:
haftmann@29789
  1153
  assumes lp: "isrlfm p"
haftmann@29789
  1154
  shows "\<exists> z. \<forall> x > z. Ifm (x#bs) (plusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
haftmann@29789
  1155
using lp
haftmann@29789
  1156
proof (induct p rule: isrlfm.induct)
haftmann@29789
  1157
  case (1 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
haftmann@29789
  1158
next
haftmann@29789
  1159
  case (2 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
haftmann@29789
  1160
next
haftmann@29789
  1161
  case (3 c e) 
wenzelm@41807
  1162
  from 3 have nb: "numbound0 e" by simp
wenzelm@41807
  1163
  from 3 have cp: "real c > 0" by simp
haftmann@29789
  1164
  fix a
haftmann@29789
  1165
  let ?e="Inum (a#bs) e"
haftmann@29789
  1166
  let ?z = "(- ?e) / real c"
haftmann@29789
  1167
  {fix x
haftmann@29789
  1168
    assume xz: "x > ?z"
haftmann@29789
  1169
    with mult_strict_right_mono [OF xz cp] cp
haftmann@57514
  1170
    have "(real c * x > - ?e)" by (simp add: ac_simps)
haftmann@29789
  1171
    hence "real c * x + ?e > 0" by arith
haftmann@29789
  1172
    hence "real c * x + ?e \<noteq> 0" by simp
haftmann@29789
  1173
    with xz have "?P ?z x (Eq (CN 0 c e))"
haftmann@29789
  1174
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1175
  hence "\<forall> x > ?z. ?P ?z x (Eq (CN 0 c e))" by simp
haftmann@29789
  1176
  thus ?case by blast
haftmann@29789
  1177
next
haftmann@29789
  1178
  case (4 c e) 
wenzelm@41807
  1179
  from 4 have nb: "numbound0 e" by simp
wenzelm@41807
  1180
  from 4 have cp: "real c > 0" by simp
haftmann@29789
  1181
  fix a
haftmann@29789
  1182
  let ?e="Inum (a#bs) e"
haftmann@29789
  1183
  let ?z = "(- ?e) / real c"
haftmann@29789
  1184
  {fix x
haftmann@29789
  1185
    assume xz: "x > ?z"
haftmann@29789
  1186
    with mult_strict_right_mono [OF xz cp] cp
haftmann@57514
  1187
    have "(real c * x > - ?e)" by (simp add: ac_simps)
haftmann@29789
  1188
    hence "real c * x + ?e > 0" by arith
haftmann@29789
  1189
    hence "real c * x + ?e \<noteq> 0" by simp
haftmann@29789
  1190
    with xz have "?P ?z x (NEq (CN 0 c e))"
haftmann@29789
  1191
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1192
  hence "\<forall> x > ?z. ?P ?z x (NEq (CN 0 c e))" by simp
haftmann@29789
  1193
  thus ?case by blast
haftmann@29789
  1194
next
haftmann@29789
  1195
  case (5 c e) 
wenzelm@41807
  1196
  from 5 have nb: "numbound0 e" by simp
wenzelm@41807
  1197
  from 5 have cp: "real c > 0" by simp
haftmann@29789
  1198
  fix a
haftmann@29789
  1199
  let ?e="Inum (a#bs) e"
haftmann@29789
  1200
  let ?z = "(- ?e) / real c"
haftmann@29789
  1201
  {fix x
haftmann@29789
  1202
    assume xz: "x > ?z"
haftmann@29789
  1203
    with mult_strict_right_mono [OF xz cp] cp
haftmann@57514
  1204
    have "(real c * x > - ?e)" by (simp add: ac_simps)
haftmann@29789
  1205
    hence "real c * x + ?e > 0" by arith
haftmann@29789
  1206
    with xz have "?P ?z x (Lt (CN 0 c e))"
haftmann@29789
  1207
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1208
  hence "\<forall> x > ?z. ?P ?z x (Lt (CN 0 c e))" by simp
haftmann@29789
  1209
  thus ?case by blast
haftmann@29789
  1210
next
haftmann@29789
  1211
  case (6 c e) 
wenzelm@41807
  1212
  from 6 have nb: "numbound0 e" by simp
wenzelm@41807
  1213
  from 6 have cp: "real c > 0" by simp
haftmann@29789
  1214
  fix a
haftmann@29789
  1215
  let ?e="Inum (a#bs) e"
haftmann@29789
  1216
  let ?z = "(- ?e) / real c"
haftmann@29789
  1217
  {fix x
haftmann@29789
  1218
    assume xz: "x > ?z"
haftmann@29789
  1219
    with mult_strict_right_mono [OF xz cp] cp
haftmann@57514
  1220
    have "(real c * x > - ?e)" by (simp add: ac_simps)
haftmann@29789
  1221
    hence "real c * x + ?e > 0" by arith
haftmann@29789
  1222
    with xz have "?P ?z x (Le (CN 0 c e))"
haftmann@29789
  1223
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1224
  hence "\<forall> x > ?z. ?P ?z x (Le (CN 0 c e))" by simp
haftmann@29789
  1225
  thus ?case by blast
haftmann@29789
  1226
next
haftmann@29789
  1227
  case (7 c e) 
wenzelm@41807
  1228
  from 7 have nb: "numbound0 e" by simp
wenzelm@41807
  1229
  from 7 have cp: "real c > 0" by simp
haftmann@29789
  1230
  fix a
haftmann@29789
  1231
  let ?e="Inum (a#bs) e"
haftmann@29789
  1232
  let ?z = "(- ?e) / real c"
haftmann@29789
  1233
  {fix x
haftmann@29789
  1234
    assume xz: "x > ?z"
haftmann@29789
  1235
    with mult_strict_right_mono [OF xz cp] cp
haftmann@57514
  1236
    have "(real c * x > - ?e)" by (simp add: ac_simps)
haftmann@29789
  1237
    hence "real c * x + ?e > 0" by arith
haftmann@29789
  1238
    with xz have "?P ?z x (Gt (CN 0 c e))"
haftmann@29789
  1239
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
haftmann@29789
  1240
  hence "\<forall> x > ?z. ?P ?z x (Gt (CN 0 c e))" by simp
haftmann@29789
  1241
  thus ?case by blast
haftmann@29789
  1242
next
haftmann@29789
  1243
  case (8 c e) 
wenzelm@41807
  1244
  from 8 have nb: "numbound0 e" by simp
wenzelm@41807
  1245
  from 8 have cp: "real c > 0" by simp
haftmann@29789
  1246
  fix a
haftmann@29789
  1247
  let ?e="Inum (a#bs) e"
haftmann@29789
  1248
  let ?z = "(- ?e) / real c"
haftmann@29789
  1249
  {fix x
haftmann@29789
  1250
    assume xz: "x > ?z"
haftmann@29789
  1251
    with mult_strict_right_mono [OF xz cp] cp
haftmann@57514
  1252
    have "(real c * x > - ?e)" by (simp add: ac_simps)
haftmann@29789
  1253
    hence "real c * x + ?e > 0" by arith
haftmann@29789
  1254
    with xz have "?P ?z x (Ge (CN 0 c e))"
haftmann@29789
  1255
      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]   by simp }
haftmann@29789
  1256
  hence "\<forall> x > ?z. ?P ?z x (Ge (CN 0 c e))" by simp
haftmann@29789
  1257
  thus ?case by blast
haftmann@29789
  1258
qed simp_all
haftmann@29789
  1259
haftmann@29789
  1260
lemma rminusinf_bound0:
haftmann@29789
  1261
  assumes lp: "isrlfm p"
haftmann@29789
  1262
  shows "bound0 (minusinf p)"
haftmann@29789
  1263
  using lp
haftmann@29789
  1264
  by (induct p rule: minusinf.induct) simp_all
haftmann@29789
  1265
haftmann@29789
  1266
lemma rplusinf_bound0:
haftmann@29789
  1267
  assumes lp: "isrlfm p"
haftmann@29789
  1268
  shows "bound0 (plusinf p)"
haftmann@29789
  1269
  using lp
haftmann@29789
  1270
  by (induct p rule: plusinf.induct) simp_all
haftmann@29789
  1271
haftmann@29789
  1272
lemma rminusinf_ex:
haftmann@29789
  1273
  assumes lp: "isrlfm p"
haftmann@29789
  1274
  and ex: "Ifm (a#bs) (minusinf p)"
haftmann@29789
  1275
  shows "\<exists> x. Ifm (x#bs) p"
haftmann@29789
  1276
proof-
haftmann@29789
  1277
  from bound0_I [OF rminusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
haftmann@29789
  1278
  have th: "\<forall> x. Ifm (x#bs) (minusinf p)" by auto
haftmann@29789
  1279
  from rminusinf_inf[OF lp, where bs="bs"] 
haftmann@29789
  1280
  obtain z where z_def: "\<forall>x<z. Ifm (x # bs) (minusinf p) = Ifm (x # bs) p" by blast
haftmann@29789
  1281
  from th have "Ifm ((z - 1)#bs) (minusinf p)" by simp
haftmann@29789
  1282
  moreover have "z - 1 < z" by simp
haftmann@29789
  1283
  ultimately show ?thesis using z_def by auto
haftmann@29789
  1284
qed
haftmann@29789
  1285
haftmann@29789
  1286
lemma rplusinf_ex:
haftmann@29789
  1287
  assumes lp: "isrlfm p"
haftmann@29789
  1288
  and ex: "Ifm (a#bs) (plusinf p)"
haftmann@29789
  1289
  shows "\<exists> x. Ifm (x#bs) p"
haftmann@29789
  1290
proof-
haftmann@29789
  1291
  from bound0_I [OF rplusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
haftmann@29789
  1292
  have th: "\<forall> x. Ifm (x#bs) (plusinf p)" by auto
haftmann@29789
  1293
  from rplusinf_inf[OF lp, where bs="bs"] 
haftmann@29789
  1294
  obtain z where z_def: "\<forall>x>z. Ifm (x # bs) (plusinf p) = Ifm (x # bs) p" by blast
haftmann@29789
  1295
  from th have "Ifm ((z + 1)#bs) (plusinf p)" by simp
haftmann@29789
  1296
  moreover have "z + 1 > z" by simp
haftmann@29789
  1297
  ultimately show ?thesis using z_def by auto
haftmann@29789
  1298
qed
haftmann@29789
  1299
haftmann@29789
  1300
consts 
haftmann@29789
  1301
  uset:: "fm \<Rightarrow> (num \<times> int) list"
haftmann@29789
  1302
  usubst :: "fm \<Rightarrow> (num \<times> int) \<Rightarrow> fm "
haftmann@29789
  1303
recdef uset "measure size"
haftmann@29789
  1304
  "uset (And p q) = (uset p @ uset q)" 
haftmann@29789
  1305
  "uset (Or p q) = (uset p @ uset q)" 
haftmann@29789
  1306
  "uset (Eq  (CN 0 c e)) = [(Neg e,c)]"
haftmann@29789
  1307
  "uset (NEq (CN 0 c e)) = [(Neg e,c)]"
haftmann@29789
  1308
  "uset (Lt  (CN 0 c e)) = [(Neg e,c)]"
haftmann@29789
  1309
  "uset (Le  (CN 0 c e)) = [(Neg e,c)]"
haftmann@29789
  1310
  "uset (Gt  (CN 0 c e)) = [(Neg e,c)]"
haftmann@29789
  1311
  "uset (Ge  (CN 0 c e)) = [(Neg e,c)]"
haftmann@29789
  1312
  "uset p = []"
haftmann@29789
  1313
recdef usubst "measure size"
haftmann@29789
  1314
  "usubst (And p q) = (\<lambda> (t,n). And (usubst p (t,n)) (usubst q (t,n)))"
haftmann@29789
  1315
  "usubst (Or p q) = (\<lambda> (t,n). Or (usubst p (t,n)) (usubst q (t,n)))"
haftmann@29789
  1316
  "usubst (Eq (CN 0 c e)) = (\<lambda> (t,n). Eq (Add (Mul c t) (Mul n e)))"
haftmann@29789
  1317
  "usubst (NEq (CN 0 c e)) = (\<lambda> (t,n). NEq (Add (Mul c t) (Mul n e)))"
haftmann@29789
  1318
  "usubst (Lt (CN 0 c e)) = (\<lambda> (t,n). Lt (Add (Mul c t) (Mul n e)))"
haftmann@29789
  1319
  "usubst (Le (CN 0 c e)) = (\<lambda> (t,n). Le (Add (Mul c t) (Mul n e)))"
haftmann@29789
  1320
  "usubst (Gt (CN 0 c e)) = (\<lambda> (t,n). Gt (Add (Mul c t) (Mul n e)))"
haftmann@29789
  1321
  "usubst (Ge (CN 0 c e)) = (\<lambda> (t,n). Ge (Add (Mul c t) (Mul n e)))"
haftmann@29789
  1322
  "usubst p = (\<lambda> (t,n). p)"
haftmann@29789
  1323
haftmann@29789
  1324
lemma usubst_I: assumes lp: "isrlfm p"
haftmann@29789
  1325
  and np: "real n > 0" and nbt: "numbound0 t"
haftmann@29789
  1326
  shows "(Ifm (x#bs) (usubst p (t,n)) = Ifm (((Inum (x#bs) t)/(real n))#bs) p) \<and> bound0 (usubst p (t,n))" (is "(?I x (usubst p (t,n)) = ?I ?u p) \<and> ?B p" is "(_ = ?I (?t/?n) p) \<and> _" is "(_ = ?I (?N x t /_) p) \<and> _")
haftmann@29789
  1327
  using lp
haftmann@29789
  1328
proof(induct p rule: usubst.induct)
wenzelm@41807
  1329
  case (5 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
haftmann@29789
  1330
  have "?I ?u (Lt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) < 0)"
haftmann@29789
  1331
    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
haftmann@29789
  1332
  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) < 0)"
haftmann@29789
  1333
    by (simp only: pos_less_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
haftmann@29789
  1334
      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
haftmann@29789
  1335
  also have "\<dots> = (real c *?t + ?n* (?N x e) < 0)"
haftmann@29789
  1336
    using np by simp 
haftmann@29789
  1337
  finally show ?case using nbt nb by (simp add: algebra_simps)
haftmann@29789
  1338
next
wenzelm@41807
  1339
  case (6 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
haftmann@29789
  1340
  have "?I ?u (Le (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<le> 0)"
haftmann@29789
  1341
    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
haftmann@29789
  1342
  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<le> 0)"
haftmann@29789
  1343
    by (simp only: pos_le_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
haftmann@29789
  1344
      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
haftmann@29789
  1345
  also have "\<dots> = (real c *?t + ?n* (?N x e) \<le> 0)"
haftmann@29789
  1346
    using np by simp 
haftmann@29789
  1347
  finally show ?case using nbt nb by (simp add: algebra_simps)
haftmann@29789
  1348
next
wenzelm@41807
  1349
  case (7 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
haftmann@29789
  1350
  have "?I ?u (Gt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) > 0)"
haftmann@29789
  1351
    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
haftmann@29789
  1352
  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) > 0)"
haftmann@29789
  1353
    by (simp only: pos_divide_less_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
haftmann@29789
  1354
      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
haftmann@29789
  1355
  also have "\<dots> = (real c *?t + ?n* (?N x e) > 0)"
haftmann@29789
  1356
    using np by simp 
haftmann@29789
  1357
  finally show ?case using nbt nb by (simp add: algebra_simps)
haftmann@29789
  1358
next
wenzelm@41807
  1359
  case (8 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
haftmann@29789
  1360
  have "?I ?u (Ge (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<ge> 0)"
haftmann@29789
  1361
    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
haftmann@29789
  1362
  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<ge> 0)"
haftmann@29789
  1363
    by (simp only: pos_divide_le_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
haftmann@29789
  1364
      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
haftmann@29789
  1365
  also have "\<dots> = (real c *?t + ?n* (?N x e) \<ge> 0)"
haftmann@29789
  1366
    using np by simp 
haftmann@29789
  1367
  finally show ?case using nbt nb by (simp add: algebra_simps)
haftmann@29789
  1368
next
wenzelm@41807
  1369
  case (3 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
haftmann@29789
  1370
  from np have np: "real n \<noteq> 0" by simp
haftmann@29789
  1371
  have "?I ?u (Eq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) = 0)"
haftmann@29789
  1372
    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
haftmann@29789
  1373
  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) = 0)"
haftmann@29789
  1374
    by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
haftmann@29789
  1375
      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
haftmann@29789
  1376
  also have "\<dots> = (real c *?t + ?n* (?N x e) = 0)"
haftmann@29789
  1377
    using np by simp 
haftmann@29789
  1378
  finally show ?case using nbt nb by (simp add: algebra_simps)
haftmann@29789
  1379
next
wenzelm@41807
  1380
  case (4 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
haftmann@29789
  1381
  from np have np: "real n \<noteq> 0" by simp
haftmann@29789
  1382
  have "?I ?u (NEq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<noteq> 0)"
haftmann@29789
  1383
    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
haftmann@29789
  1384
  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<noteq> 0)"
haftmann@29789
  1385
    by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
haftmann@29789
  1386
      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
haftmann@29789
  1387
  also have "\<dots> = (real c *?t + ?n* (?N x e) \<noteq> 0)"
haftmann@29789
  1388
    using np by simp 
haftmann@29789
  1389
  finally show ?case using nbt nb by (simp add: algebra_simps)
nipkow@41842
  1390
qed(simp_all add: nbt numbound0_I[where bs ="bs" and b="(Inum (x#bs) t)/ real n" and b'="x"])
haftmann@29789
  1391
haftmann@29789
  1392
lemma uset_l:
haftmann@29789
  1393
  assumes lp: "isrlfm p"
haftmann@29789
  1394
  shows "\<forall> (t,k) \<in> set (uset p). numbound0 t \<and> k >0"
haftmann@29789
  1395
using lp
haftmann@29789
  1396
by(induct p rule: uset.induct,auto)
haftmann@29789
  1397
haftmann@29789
  1398
lemma rminusinf_uset:
haftmann@29789
  1399
  assumes lp: "isrlfm p"
haftmann@29789
  1400
  and nmi: "\<not> (Ifm (a#bs) (minusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
haftmann@29789
  1401
  and ex: "Ifm (x#bs) p" (is "?I x p")
haftmann@29789
  1402
  shows "\<exists> (s,m) \<in> set (uset p). x \<ge> Inum (a#bs) s / real m" (is "\<exists> (s,m) \<in> ?U p. x \<ge> ?N a s / real m")
haftmann@29789
  1403
proof-
haftmann@29789
  1404
  have "\<exists> (s,m) \<in> set (uset p). real m * x \<ge> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real m *x \<ge> ?N a s")
haftmann@29789
  1405
    using lp nmi ex
nipkow@41842
  1406
    by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"])
haftmann@29789
  1407
  then obtain s m where smU: "(s,m) \<in> set (uset p)" and mx: "real m * x \<ge> ?N a s" by blast
haftmann@29789
  1408
  from uset_l[OF lp] smU have mp: "real m > 0" by auto
haftmann@29789
  1409
  from pos_divide_le_eq[OF mp, where a="x" and b="?N a s", symmetric] mx have "x \<ge> ?N a s / real m" 
haftmann@57512
  1410
    by (auto simp add: mult.commute)
haftmann@29789
  1411
  thus ?thesis using smU by auto
haftmann@29789
  1412
qed
haftmann@29789
  1413
haftmann@29789
  1414
lemma rplusinf_uset:
haftmann@29789
  1415
  assumes lp: "isrlfm p"
haftmann@29789
  1416
  and nmi: "\<not> (Ifm (a#bs) (plusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
haftmann@29789
  1417
  and ex: "Ifm (x#bs) p" (is "?I x p")
haftmann@29789
  1418
  shows "\<exists> (s,m) \<in> set (uset p). x \<le> Inum (a#bs) s / real m" (is "\<exists> (s,m) \<in> ?U p. x \<le> ?N a s / real m")
haftmann@29789
  1419
proof-
haftmann@29789
  1420
  have "\<exists> (s,m) \<in> set (uset p). real m * x \<le> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real m *x \<le> ?N a s")
haftmann@29789
  1421
    using lp nmi ex
nipkow@41842
  1422
    by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"])
haftmann@29789
  1423
  then obtain s m where smU: "(s,m) \<in> set (uset p)" and mx: "real m * x \<le> ?N a s" by blast
haftmann@29789
  1424
  from uset_l[OF lp] smU have mp: "real m > 0" by auto
haftmann@29789
  1425
  from pos_le_divide_eq[OF mp, where a="x" and b="?N a s", symmetric] mx have "x \<le> ?N a s / real m" 
haftmann@57512
  1426
    by (auto simp add: mult.commute)
haftmann@29789
  1427
  thus ?thesis using smU by auto
haftmann@29789
  1428
qed
haftmann@29789
  1429
haftmann@29789
  1430
lemma lin_dense: 
haftmann@29789
  1431
  assumes lp: "isrlfm p"
haftmann@29789
  1432
  and noS: "\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> (\<lambda> (t,n). Inum (x#bs) t / real n) ` set (uset p)" 
haftmann@29789
  1433
  (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (t,n). ?N x t / real n ) ` (?U p)")
haftmann@29789
  1434
  and lx: "l < x" and xu:"x < u" and px:" Ifm (x#bs) p"
haftmann@29789
  1435
  and ly: "l < y" and yu: "y < u"
haftmann@29789
  1436
  shows "Ifm (y#bs) p"
haftmann@29789
  1437
using lp px noS
haftmann@29789
  1438
proof (induct p rule: isrlfm.induct)
haftmann@29789
  1439
  case (5 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
wenzelm@41807
  1440
  from 5 have "x * real c + ?N x e < 0" by (simp add: algebra_simps)
wenzelm@41807
  1441
  hence pxc: "x < (- ?N x e) / real c" 
wenzelm@41807
  1442
    by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="-?N x e"])
wenzelm@41807
  1443
  from 5 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
wenzelm@41807
  1444
  with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
wenzelm@41807
  1445
  hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
wenzelm@41807
  1446
  moreover {assume y: "y < (-?N x e)/ real c"
wenzelm@41807
  1447
    hence "y * real c < - ?N x e"
wenzelm@41807
  1448
      by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
wenzelm@41807
  1449
    hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
wenzelm@41807
  1450
    hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
wenzelm@41807
  1451
  moreover {assume y: "y > (- ?N x e) / real c" 
wenzelm@41807
  1452
    with yu have eu: "u > (- ?N x e) / real c" by auto
wenzelm@41807
  1453
    with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
wenzelm@41807
  1454
    with lx pxc have "False" by auto
wenzelm@41807
  1455
    hence ?case by simp }
wenzelm@41807
  1456
  ultimately show ?case by blast
haftmann@29789
  1457
next
haftmann@29789
  1458
  case (6 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp +
wenzelm@41807
  1459
  from 6 have "x * real c + ?N x e \<le> 0" by (simp add: algebra_simps)
wenzelm@41807
  1460
  hence pxc: "x \<le> (- ?N x e) / real c" 
wenzelm@41807
  1461
    by (simp only: pos_le_divide_eq[OF cp, where a="x" and b="-?N x e"])
wenzelm@41807
  1462
  from 6 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
wenzelm@41807
  1463
  with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
wenzelm@41807
  1464
  hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
wenzelm@41807
  1465
  moreover {assume y: "y < (-?N x e)/ real c"
wenzelm@41807
  1466
    hence "y * real c < - ?N x e"
wenzelm@41807
  1467
      by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
wenzelm@41807
  1468
    hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
wenzelm@41807
  1469
    hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
wenzelm@41807
  1470
  moreover {assume y: "y > (- ?N x e) / real c" 
wenzelm@41807
  1471
    with yu have eu: "u > (- ?N x e) / real c" by auto
wenzelm@41807
  1472
    with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
wenzelm@41807
  1473
    with lx pxc have "False" by auto
wenzelm@41807
  1474
    hence ?case by simp }
wenzelm@41807
  1475
  ultimately show ?case by blast
haftmann@29789
  1476
next
haftmann@29789
  1477
  case (7 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
wenzelm@41807
  1478
  from 7 have "x * real c + ?N x e > 0" by (simp add: algebra_simps)
wenzelm@41807
  1479
  hence pxc: "x > (- ?N x e) / real c" 
wenzelm@41807
  1480
    by (simp only: pos_divide_less_eq[OF cp, where a="x" and b="-?N x e"])
wenzelm@41807
  1481
  from 7 have noSc: "\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
wenzelm@41807
  1482
  with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
wenzelm@41807
  1483
  hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
wenzelm@41807
  1484
  moreover {assume y: "y > (-?N x e)/ real c"
wenzelm@41807
  1485
    hence "y * real c > - ?N x e"
wenzelm@41807
  1486
      by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
wenzelm@41807
  1487
    hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
wenzelm@41807
  1488
    hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
wenzelm@41807
  1489
  moreover {assume y: "y < (- ?N x e) / real c" 
wenzelm@41807
  1490
    with ly have eu: "l < (- ?N x e) / real c" by auto
wenzelm@41807
  1491
    with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
wenzelm@41807
  1492
    with xu pxc have "False" by auto
wenzelm@41807
  1493
    hence ?case by simp }
wenzelm@41807
  1494
  ultimately show ?case by blast
haftmann@29789
  1495
next
haftmann@29789
  1496
  case (8 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
wenzelm@41807
  1497
  from 8 have "x * real c + ?N x e \<ge> 0" by (simp add: algebra_simps)
wenzelm@41807
  1498
  hence pxc: "x \<ge> (- ?N x e) / real c" 
wenzelm@41807
  1499
    by (simp only: pos_divide_le_eq[OF cp, where a="x" and b="-?N x e"])
wenzelm@41807
  1500
  from 8 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
wenzelm@41807
  1501
  with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
wenzelm@41807
  1502
  hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
wenzelm@41807
  1503
  moreover {assume y: "y > (-?N x e)/ real c"
wenzelm@41807
  1504
    hence "y * real c > - ?N x e"
wenzelm@41807
  1505
      by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
wenzelm@41807
  1506
    hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
wenzelm@41807
  1507
    hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
wenzelm@41807
  1508
  moreover {assume y: "y < (- ?N x e) / real c" 
wenzelm@41807
  1509
    with ly have eu: "l < (- ?N x e) / real c" by auto
wenzelm@41807
  1510
    with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
wenzelm@41807
  1511
    with xu pxc have "False" by auto
wenzelm@41807
  1512
    hence ?case by simp }
wenzelm@41807
  1513
  ultimately show ?case by blast
haftmann@29789
  1514
next
haftmann@29789
  1515
  case (3 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
wenzelm@41807
  1516
  from cp have cnz: "real c \<noteq> 0" by simp
wenzelm@41807
  1517
  from 3 have "x * real c + ?N x e = 0" by (simp add: algebra_simps)
wenzelm@41807
  1518
  hence pxc: "x = (- ?N x e) / real c" 
wenzelm@41807
  1519
    by (simp only: nonzero_eq_divide_eq[OF cnz, where a="x" and b="-?N x e"])
wenzelm@41807
  1520
  from 3 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
wenzelm@41807
  1521
  with lx xu have yne: "x \<noteq> - ?N x e / real c" by auto
wenzelm@41807
  1522
  with pxc show ?case by simp
haftmann@29789
  1523
next
haftmann@29789
  1524
  case (4 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
wenzelm@41807
  1525
  from cp have cnz: "real c \<noteq> 0" by simp
wenzelm@41807
  1526
  from 4 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
wenzelm@41807
  1527
  with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
wenzelm@41807
  1528
  hence "y* real c \<noteq> -?N x e"      
wenzelm@41807
  1529
    by (simp only: nonzero_eq_divide_eq[OF cnz, where a="y" and b="-?N x e"]) simp
wenzelm@41807
  1530
  hence "y* real c + ?N x e \<noteq> 0" by (simp add: algebra_simps)
wenzelm@41807
  1531
  thus ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] 
wenzelm@41807
  1532
    by (simp add: algebra_simps)
nipkow@41842
  1533
qed (auto simp add: numbound0_I[where bs="bs" and b="y" and b'="x"])
haftmann@29789
  1534
haftmann@29789
  1535
lemma finite_set_intervals:
haftmann@29789
  1536
  assumes px: "P (x::real)" 
haftmann@29789
  1537
  and lx: "l \<le> x" and xu: "x \<le> u"
haftmann@29789
  1538
  and linS: "l\<in> S" and uinS: "u \<in> S"
haftmann@29789
  1539
  and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
haftmann@29789
  1540
  shows "\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a \<le> x \<and> x \<le> b \<and> P x"
haftmann@29789
  1541
proof-
haftmann@29789
  1542
  let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
haftmann@29789
  1543
  let ?xM = "{y. y\<in> S \<and> x \<le> y}"
haftmann@29789
  1544
  let ?a = "Max ?Mx"
haftmann@29789
  1545
  let ?b = "Min ?xM"
haftmann@29789
  1546
  have MxS: "?Mx \<subseteq> S" by blast
haftmann@29789
  1547
  hence fMx: "finite ?Mx" using fS finite_subset by auto
haftmann@29789
  1548
  from lx linS have linMx: "l \<in> ?Mx" by blast
haftmann@29789
  1549
  hence Mxne: "?Mx \<noteq> {}" by blast
haftmann@29789
  1550
  have xMS: "?xM \<subseteq> S" by blast
haftmann@29789
  1551
  hence fxM: "finite ?xM" using fS finite_subset by auto
haftmann@29789
  1552
  from xu uinS have linxM: "u \<in> ?xM" by blast
haftmann@29789
  1553
  hence xMne: "?xM \<noteq> {}" by blast
haftmann@29789
  1554
  have ax:"?a \<le> x" using Mxne fMx by auto
haftmann@29789
  1555
  have xb:"x \<le> ?b" using xMne fxM by auto
haftmann@29789
  1556
  have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
haftmann@29789
  1557
  have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
haftmann@29789
  1558
  have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
haftmann@29789
  1559
  proof(clarsimp)
haftmann@29789
  1560
    fix y
haftmann@29789
  1561
    assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
haftmann@29789
  1562
    from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by auto
haftmann@29789
  1563
    moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by simp}
haftmann@29789
  1564
    moreover {assume "y \<in> ?xM" hence "y \<ge> ?b" using xMne fxM by auto with yb have "False" by simp}
haftmann@29789
  1565
    ultimately show "False" by blast
haftmann@29789
  1566
  qed
haftmann@29789
  1567
  from ainS binS noy ax xb px show ?thesis by blast
haftmann@29789
  1568
qed
haftmann@29789
  1569
haftmann@29789
  1570
lemma rinf_uset:
haftmann@29789
  1571
  assumes lp: "isrlfm p"
haftmann@29789
  1572
  and nmi: "\<not> (Ifm (x#bs) (minusinf p))" (is "\<not> (Ifm (x#bs) (?M p))")
haftmann@29789
  1573
  and npi: "\<not> (Ifm (x#bs) (plusinf p))" (is "\<not> (Ifm (x#bs) (?P p))")
haftmann@29789
  1574
  and ex: "\<exists> x.  Ifm (x#bs) p" (is "\<exists> x. ?I x p")
haftmann@29789
  1575
  shows "\<exists> (l,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). ?I ((Inum (x#bs) l / real n + Inum (x#bs) s / real m) / 2) p" 
haftmann@29789
  1576
proof-
haftmann@29789
  1577
  let ?N = "\<lambda> x t. Inum (x#bs) t"
haftmann@29789
  1578
  let ?U = "set (uset p)"
haftmann@29789
  1579
  from ex obtain a where pa: "?I a p" by blast
haftmann@29789
  1580
  from bound0_I[OF rminusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] nmi
haftmann@29789
  1581
  have nmi': "\<not> (?I a (?M p))" by simp
haftmann@29789
  1582
  from bound0_I[OF rplusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] npi
haftmann@29789
  1583
  have npi': "\<not> (?I a (?P p))" by simp
haftmann@29789
  1584
  have "\<exists> (l,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). ?I ((?N a l/real n + ?N a s /real m) / 2) p"
haftmann@29789
  1585
  proof-
haftmann@29789
  1586
    let ?M = "(\<lambda> (t,c). ?N a t / real c) ` ?U"
haftmann@29789
  1587
    have fM: "finite ?M" by auto
haftmann@29789
  1588
    from rminusinf_uset[OF lp nmi pa] rplusinf_uset[OF lp npi pa] 
haftmann@29789
  1589
    have "\<exists> (l,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). a \<le> ?N x l / real n \<and> a \<ge> ?N x s / real m" by blast
haftmann@29789
  1590
    then obtain "t" "n" "s" "m" where 
haftmann@29789
  1591
      tnU: "(t,n) \<in> ?U" and smU: "(s,m) \<in> ?U" 
haftmann@29789
  1592
      and xs1: "a \<le> ?N x s / real m" and tx1: "a \<ge> ?N x t / real n" by blast
haftmann@29789
  1593
    from uset_l[OF lp] tnU smU numbound0_I[where bs="bs" and b="x" and b'="a"] xs1 tx1 have xs: "a \<le> ?N a s / real m" and tx: "a \<ge> ?N a t / real n" by auto
haftmann@29789
  1594
    from tnU have Mne: "?M \<noteq> {}" by auto
haftmann@29789
  1595
    hence Une: "?U \<noteq> {}" by simp
haftmann@29789
  1596
    let ?l = "Min ?M"
haftmann@29789
  1597
    let ?u = "Max ?M"
haftmann@29789
  1598
    have linM: "?l \<in> ?M" using fM Mne by simp
haftmann@29789
  1599
    have uinM: "?u \<in> ?M" using fM Mne by simp
haftmann@29789
  1600
    have tnM: "?N a t / real n \<in> ?M" using tnU by auto
haftmann@29789
  1601
    have smM: "?N a s / real m \<in> ?M" using smU by auto 
haftmann@29789
  1602
    have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
haftmann@29789
  1603
    have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
haftmann@29789
  1604
    have "?l \<le> ?N a t / real n" using tnM Mne by simp hence lx: "?l \<le> a" using tx by simp
haftmann@29789
  1605
    have "?N a s / real m \<le> ?u" using smM Mne by simp hence xu: "a \<le> ?u" using xs by simp
haftmann@29789
  1606
    from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
haftmann@29789
  1607
    have "(\<exists> s\<in> ?M. ?I s p) \<or> 
haftmann@29789
  1608
      (\<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)" .
haftmann@29789
  1609
    moreover { fix u assume um: "u\<in> ?M" and pu: "?I u p"
haftmann@29789
  1610
      hence "\<exists> (tu,nu) \<in> ?U. u = ?N a tu / real nu" by auto
haftmann@29789
  1611
      then obtain "tu" "nu" where tuU: "(tu,nu) \<in> ?U" and tuu:"u= ?N a tu / real nu" by blast
haftmann@29789
  1612
      have "(u + u) / 2 = u" by auto with pu tuu 
haftmann@29789
  1613
      have "?I (((?N a tu / real nu) + (?N a tu / real nu)) / 2) p" by simp
haftmann@29789
  1614
      with tuU have ?thesis by blast}
haftmann@29789
  1615
    moreover{
haftmann@29789
  1616
      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"
haftmann@29789
  1617
      then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M" 
wenzelm@32960
  1618
        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@32960
  1619
        by blast
haftmann@29789
  1620
      from t1M have "\<exists> (t1u,t1n) \<in> ?U. t1 = ?N a t1u / real t1n" by auto
haftmann@29789
  1621
      then obtain "t1u" "t1n" where t1uU: "(t1u,t1n) \<in> ?U" and t1u: "t1 = ?N a t1u / real t1n" by blast
haftmann@29789
  1622
      from t2M have "\<exists> (t2u,t2n) \<in> ?U. t2 = ?N a t2u / real t2n" by auto
haftmann@29789
  1623
      then obtain "t2u" "t2n" where t2uU: "(t2u,t2n) \<in> ?U" and t2u: "t2 = ?N a t2u / real t2n" by blast
haftmann@29789
  1624
      from t1x xt2 have t1t2: "t1 < t2" by simp
haftmann@29789
  1625
      let ?u = "(t1 + t2) / 2"
haftmann@29789
  1626
      from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
haftmann@29789
  1627
      from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
haftmann@29789
  1628
      with t1uU t2uU t1u t2u have ?thesis by blast}
haftmann@29789
  1629
    ultimately show ?thesis by blast
haftmann@29789
  1630
  qed
haftmann@29789
  1631
  then obtain "l" "n" "s"  "m" where lnU: "(l,n) \<in> ?U" and smU:"(s,m) \<in> ?U" 
haftmann@29789
  1632
    and pu: "?I ((?N a l / real n + ?N a s / real m) / 2) p" by blast
haftmann@29789
  1633
  from lnU smU uset_l[OF lp] have nbl: "numbound0 l" and nbs: "numbound0 s" by auto
haftmann@29789
  1634
  from numbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"] 
haftmann@29789
  1635
    numbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
haftmann@29789
  1636
  have "?I ((?N x l / real n + ?N x s / real m) / 2) p" by simp
haftmann@29789
  1637
  with lnU smU
haftmann@29789
  1638
  show ?thesis by auto
haftmann@29789
  1639
qed
haftmann@29789
  1640
    (* The Ferrante - Rackoff Theorem *)
haftmann@29789
  1641
haftmann@29789
  1642
theorem fr_eq: 
haftmann@29789
  1643
  assumes lp: "isrlfm p"
haftmann@29789
  1644
  shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). Ifm ((((Inum (x#bs) t)/  real n + (Inum (x#bs) s) / real m) /2)#bs) p))"
haftmann@29789
  1645
  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
haftmann@29789
  1646
proof
haftmann@29789
  1647
  assume px: "\<exists> x. ?I x p"
haftmann@29789
  1648
  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
haftmann@29789
  1649
  moreover {assume "?M \<or> ?P" hence "?D" by blast}
haftmann@29789
  1650
  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
haftmann@29789
  1651
    from rinf_uset[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
haftmann@29789
  1652
  ultimately show "?D" by blast
haftmann@29789
  1653
next
haftmann@29789
  1654
  assume "?D" 
haftmann@29789
  1655
  moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
haftmann@29789
  1656
  moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
haftmann@29789
  1657
  moreover {assume f:"?F" hence "?E" by blast}
haftmann@29789
  1658
  ultimately show "?E" by blast
haftmann@29789
  1659
qed
haftmann@29789
  1660
haftmann@29789
  1661
haftmann@29789
  1662
lemma fr_equsubst: 
haftmann@29789
  1663
  assumes lp: "isrlfm p"
haftmann@29789
  1664
  shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,k) \<in> set (uset p). \<exists> (s,l) \<in> set (uset p). Ifm (x#bs) (usubst p (Add(Mul l t) (Mul k s) , 2*k*l))))"
haftmann@29789
  1665
  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
haftmann@29789
  1666
proof
haftmann@29789
  1667
  assume px: "\<exists> x. ?I x p"
haftmann@29789
  1668
  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
haftmann@29789
  1669
  moreover {assume "?M \<or> ?P" hence "?D" by blast}
haftmann@29789
  1670
  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
haftmann@29789
  1671
    let ?f ="\<lambda> (t,n). Inum (x#bs) t / real n"
haftmann@29789
  1672
    let ?N = "\<lambda> t. Inum (x#bs) t"
haftmann@29789
  1673
    {fix t n s m assume "(t,n)\<in> set (uset p)" and "(s,m) \<in> set (uset p)"
haftmann@29789
  1674
      with uset_l[OF lp] have tnb: "numbound0 t" and np:"real n > 0" and snb: "numbound0 s" and mp:"real m > 0"
wenzelm@32960
  1675
        by auto
haftmann@29789
  1676
      let ?st = "Add (Mul m t) (Mul n s)"
haftmann@57512
  1677
      from np mp have mnp: "real (2*n*m) > 0" by (simp add: mult.commute)
haftmann@29789
  1678
      from tnb snb have st_nb: "numbound0 ?st" by simp
haftmann@29789
  1679
      have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
wenzelm@32960
  1680
        using mnp mp np by (simp add: algebra_simps add_divide_distrib)
haftmann@29789
  1681
      from usubst_I[OF lp mnp st_nb, where x="x" and bs="bs"] 
haftmann@29789
  1682
      have "?I x (usubst p (?st,2*n*m)) = ?I ((?N t / real n + ?N s / real m) /2) p" by (simp only: st[symmetric])}
haftmann@29789
  1683
    with rinf_uset[OF lp nmi npi px] have "?F" by blast hence "?D" by blast}
haftmann@29789
  1684
  ultimately show "?D" by blast
haftmann@29789
  1685
next
haftmann@29789
  1686
  assume "?D" 
haftmann@29789
  1687
  moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
haftmann@29789
  1688
  moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
haftmann@29789
  1689
  moreover {fix t k s l assume "(t,k) \<in> set (uset p)" and "(s,l) \<in> set (uset p)" 
haftmann@29789
  1690
    and px:"?I x (usubst p (Add (Mul l t) (Mul k s), 2*k*l))"
haftmann@29789
  1691
    with uset_l[OF lp] have tnb: "numbound0 t" and np:"real k > 0" and snb: "numbound0 s" and mp:"real l > 0" by auto
haftmann@29789
  1692
    let ?st = "Add (Mul l t) (Mul k s)"
haftmann@57512
  1693
    from np mp have mnp: "real (2*k*l) > 0" by (simp add: mult.commute)
haftmann@29789
  1694
    from tnb snb have st_nb: "numbound0 ?st" by simp
haftmann@29789
  1695
    from usubst_I[OF lp mnp st_nb, where bs="bs"] px have "?E" by auto}
haftmann@29789
  1696
  ultimately show "?E" by blast
haftmann@29789
  1697
qed
haftmann@29789
  1698
haftmann@29789
  1699
haftmann@29789
  1700
    (* Implement the right hand side of Ferrante and Rackoff's Theorem. *)
haftmann@35416
  1701
definition ferrack :: "fm \<Rightarrow> fm" where
haftmann@36853
  1702
  "ferrack p = (let p' = rlfm (simpfm p); mp = minusinf p'; pp = plusinf p'
haftmann@29789
  1703
                in if (mp = T \<or> pp = T) then T else 
haftmann@36853
  1704
                   (let U = remdups(map simp_num_pair 
haftmann@29789
  1705
                     (map (\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m))
haftmann@29789
  1706
                           (alluopairs (uset p')))) 
haftmann@29789
  1707
                    in decr (disj mp (disj pp (evaldjf (simpfm o (usubst p')) U)))))"
haftmann@29789
  1708
haftmann@29789
  1709
lemma uset_cong_aux:
haftmann@29789
  1710
  assumes Ul: "\<forall> (t,n) \<in> set U. numbound0 t \<and> n >0"
haftmann@29789
  1711
  shows "((\<lambda> (t,n). Inum (x#bs) t /real n) ` (set (map (\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m)) (alluopairs U)))) = ((\<lambda> ((t,n),(s,m)). (Inum (x#bs) t /real n + Inum (x#bs) s /real m)/2) ` (set U \<times> set U))"
haftmann@29789
  1712
  (is "?lhs = ?rhs")
haftmann@29789
  1713
proof(auto)
haftmann@29789
  1714
  fix t n s m
haftmann@29789
  1715
  assume "((t,n),(s,m)) \<in> set (alluopairs U)"
haftmann@29789
  1716
  hence th: "((t,n),(s,m)) \<in> (set U \<times> set U)"
haftmann@29789
  1717
    using alluopairs_set1[where xs="U"] by blast
haftmann@29789
  1718
  let ?N = "\<lambda> t. Inum (x#bs) t"
haftmann@29789
  1719
  let ?st= "Add (Mul m t) (Mul n s)"
haftmann@29789
  1720
  from Ul th have mnz: "m \<noteq> 0" by auto
haftmann@29789
  1721
  from Ul th have  nnz: "n \<noteq> 0" by auto  
haftmann@29789
  1722
  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
haftmann@29789
  1723
   using mnz nnz by (simp add: algebra_simps add_divide_distrib)
haftmann@29789
  1724
 
haftmann@29789
  1725
  thus "(real m *  Inum (x # bs) t + real n * Inum (x # bs) s) /
haftmann@29789
  1726
       (2 * real n * real m)
haftmann@29789
  1727
       \<in> (\<lambda>((t, n), s, m).
haftmann@29789
  1728
             (Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2) `
haftmann@29789
  1729
         (set U \<times> set U)"using mnz nnz th  
haftmann@29789
  1730
    apply (auto simp add: th add_divide_distrib algebra_simps split_def image_def)
haftmann@29789
  1731
    by (rule_tac x="(s,m)" in bexI,simp_all) 
haftmann@57512
  1732
  (rule_tac x="(t,n)" in bexI,simp_all add: mult.commute)
haftmann@29789
  1733
next
haftmann@29789
  1734
  fix t n s m
haftmann@29789
  1735
  assume tnU: "(t,n) \<in> set U" and smU:"(s,m) \<in> set U" 
haftmann@29789
  1736
  let ?N = "\<lambda> t. Inum (x#bs) t"
haftmann@29789
  1737
  let ?st= "Add (Mul m t) (Mul n s)"
haftmann@29789
  1738
  from Ul smU have mnz: "m \<noteq> 0" by auto
haftmann@29789
  1739
  from Ul tnU have  nnz: "n \<noteq> 0" by auto  
haftmann@29789
  1740
  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
haftmann@29789
  1741
   using mnz nnz by (simp add: algebra_simps add_divide_distrib)
haftmann@29789
  1742
 let ?P = "\<lambda> (t',n') (s',m'). (Inum (x # bs) t / real n + Inum (x # bs) s / real m)/2 = (Inum (x # bs) t' / real n' + Inum (x # bs) s' / real m')/2"
haftmann@29789
  1743
 have Pc:"\<forall> a b. ?P a b = ?P b a"
haftmann@29789
  1744
   by auto
haftmann@29789
  1745
 from Ul alluopairs_set1 have Up:"\<forall> ((t,n),(s,m)) \<in> set (alluopairs U). n \<noteq> 0 \<and> m \<noteq> 0" by blast
haftmann@29789
  1746
 from alluopairs_ex[OF Pc, where xs="U"] tnU smU
haftmann@29789
  1747
 have th':"\<exists> ((t',n'),(s',m')) \<in> set (alluopairs U). ?P (t',n') (s',m')"
haftmann@29789
  1748
   by blast
haftmann@29789
  1749
 then obtain t' n' s' m' where ts'_U: "((t',n'),(s',m')) \<in> set (alluopairs U)" 
haftmann@29789
  1750
   and Pts': "?P (t',n') (s',m')" by blast
haftmann@29789
  1751
 from ts'_U Up have mnz': "m' \<noteq> 0" and nnz': "n'\<noteq> 0" by auto
haftmann@29789
  1752
 let ?st' = "Add (Mul m' t') (Mul n' s')"
haftmann@29789
  1753
   have st': "(?N t' / real n' + ?N s' / real m')/2 = ?N ?st' / real (2*n'*m')"
haftmann@29789
  1754
   using mnz' nnz' by (simp add: algebra_simps add_divide_distrib)
haftmann@29789
  1755
 from Pts' have 
haftmann@29789
  1756
   "(Inum (x # bs) t / real n + Inum (x # bs) s / real m)/2 = (Inum (x # bs) t' / real n' + Inum (x # bs) s' / real m')/2" by simp
haftmann@29789
  1757
 also have "\<dots> = ((\<lambda>(t, n). Inum (x # bs) t / real n) ((\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) ((t',n'),(s',m'))))" by (simp add: st')
haftmann@29789
  1758
 finally show "(Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2
haftmann@29789
  1759
          \<in> (\<lambda>(t, n). Inum (x # bs) t / real n) `
haftmann@29789
  1760
            (\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) `
haftmann@29789
  1761
            set (alluopairs U)"
haftmann@29789
  1762
   using ts'_U by blast
haftmann@29789
  1763
qed
haftmann@29789
  1764
haftmann@29789
  1765
lemma uset_cong:
haftmann@29789
  1766
  assumes lp: "isrlfm p"
haftmann@29789
  1767
  and UU': "((\<lambda> (t,n). Inum (x#bs) t /real n) ` U') = ((\<lambda> ((t,n),(s,m)). (Inum (x#bs) t /real n + Inum (x#bs) s /real m)/2) ` (U \<times> U))" (is "?f ` U' = ?g ` (U\<times>U)")
haftmann@29789
  1768
  and U: "\<forall> (t,n) \<in> U. numbound0 t \<and> n > 0"
haftmann@29789
  1769
  and U': "\<forall> (t,n) \<in> U'. numbound0 t \<and> n > 0"
haftmann@29789
  1770
  shows "(\<exists> (t,n) \<in> U. \<exists> (s,m) \<in> U. Ifm (x#bs) (usubst p (Add (Mul m t) (Mul n s),2*n*m))) = (\<exists> (t,n) \<in> U'. Ifm (x#bs) (usubst p (t,n)))"
haftmann@29789
  1771
  (is "?lhs = ?rhs")
haftmann@29789
  1772
proof
haftmann@29789
  1773
  assume ?lhs
haftmann@29789
  1774
  then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
haftmann@29789
  1775
    Pst: "Ifm (x#bs) (usubst p (Add (Mul m t) (Mul n s),2*n*m))" by blast
haftmann@29789
  1776
  let ?N = "\<lambda> t. Inum (x#bs) t"
haftmann@29789
  1777
  from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
haftmann@29789
  1778
    and snb: "numbound0 s" and mp:"m > 0"  by auto
haftmann@29789
  1779
  let ?st= "Add (Mul m t) (Mul n s)"
nipkow@56544
  1780
  from np mp have mnp: "real (2*n*m) > 0" 
haftmann@57512
  1781
      by (simp add: mult.commute real_of_int_mult[symmetric] del: real_of_int_mult)
haftmann@29789
  1782
    from tnb snb have stnb: "numbound0 ?st" by simp
haftmann@29789
  1783
  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
haftmann@29789
  1784
   using mp np by (simp add: algebra_simps add_divide_distrib)
haftmann@29789
  1785
  from tnU smU UU' have "?g ((t,n),(s,m)) \<in> ?f ` U'" by blast
haftmann@29789
  1786
  hence "\<exists> (t',n') \<in> U'. ?g ((t,n),(s,m)) = ?f (t',n')"
haftmann@29789
  1787
    by auto (rule_tac x="(a,b)" in bexI, auto)
haftmann@29789
  1788
  then obtain t' n' where tnU': "(t',n') \<in> U'" and th: "?g ((t,n),(s,m)) = ?f (t',n')" by blast
haftmann@29789
  1789
  from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
haftmann@29789
  1790
  from usubst_I[OF lp mnp stnb, where bs="bs" and x="x"] Pst 
haftmann@29789
  1791
  have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
haftmann@29789
  1792
  from conjunct1[OF usubst_I[OF lp np' tnb', where bs="bs" and x="x"], symmetric] th[simplified split_def fst_conv snd_conv,symmetric] Pst2[simplified st[symmetric]]
haftmann@29789
  1793
  have "Ifm (x # bs) (usubst p (t', n')) " by (simp only: st) 
haftmann@29789
  1794
  then show ?rhs using tnU' by auto 
haftmann@29789
  1795
next
haftmann@29789
  1796
  assume ?rhs
haftmann@29789
  1797
  then obtain t' n' where tnU': "(t',n') \<in> U'" and Pt': "Ifm (x # bs) (usubst p (t', n'))" 
haftmann@29789
  1798
    by blast
haftmann@29789
  1799
  from tnU' UU' have "?f (t',n') \<in> ?g ` (U\<times>U)" by blast
haftmann@29789
  1800
  hence "\<exists> ((t,n),(s,m)) \<in> (U\<times>U). ?f (t',n') = ?g ((t,n),(s,m))" 
haftmann@29789
  1801
    by auto (rule_tac x="(a,b)" in bexI, auto)
haftmann@29789
  1802
  then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
haftmann@29789
  1803
    th: "?f (t',n') = ?g((t,n),(s,m)) "by blast
haftmann@29789
  1804
    let ?N = "\<lambda> t. Inum (x#bs) t"
haftmann@29789
  1805
  from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
haftmann@29789
  1806
    and snb: "numbound0 s" and mp:"m > 0"  by auto
haftmann@29789
  1807
  let ?st= "Add (Mul m t) (Mul n s)"
nipkow@56544
  1808
  from np mp have mnp: "real (2*n*m) > 0" 
haftmann@57512
  1809
      by (simp add: mult.commute real_of_int_mult[symmetric] del: real_of_int_mult)
haftmann@29789
  1810
    from tnb snb have stnb: "numbound0 ?st" by simp
haftmann@29789
  1811
  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
haftmann@29789
  1812
   using mp np by (simp add: algebra_simps add_divide_distrib)
haftmann@29789
  1813
  from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
haftmann@29789
  1814
  from usubst_I[OF lp np' tnb', where bs="bs" and x="x",simplified th[simplified split_def fst_conv snd_conv] st] Pt'
haftmann@29789
  1815
  have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
haftmann@29789
  1816
  with usubst_I[OF lp mnp stnb, where x="x" and bs="bs"] tnU smU show ?lhs by blast
haftmann@29789
  1817
qed
haftmann@29789
  1818
haftmann@51143
  1819
lemma ferrack:
haftmann@29789
  1820
  assumes qf: "qfree p"
haftmann@29789
  1821
  shows "qfree (ferrack p) \<and> ((Ifm bs (ferrack p)) = (\<exists> x. Ifm (x#bs) p))"
haftmann@29789
  1822
  (is "_ \<and> (?rhs = ?lhs)")
haftmann@29789
  1823
proof-
haftmann@29789
  1824
  let ?I = "\<lambda> x p. Ifm (x#bs) p"
haftmann@29789
  1825
  fix x
haftmann@29789
  1826
  let ?N = "\<lambda> t. Inum (x#bs) t"
haftmann@29789
  1827
  let ?q = "rlfm (simpfm p)" 
haftmann@29789
  1828
  let ?U = "uset ?q"
haftmann@29789
  1829
  let ?Up = "alluopairs ?U"
haftmann@29789
  1830
  let ?g = "\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m)"
haftmann@29789
  1831
  let ?S = "map ?g ?Up"
haftmann@29789
  1832
  let ?SS = "map simp_num_pair ?S"
haftmann@36853
  1833
  let ?Y = "remdups ?SS"
haftmann@29789
  1834
  let ?f= "(\<lambda> (t,n). ?N t / real n)"
haftmann@29789
  1835
  let ?h = "\<lambda> ((t,n),(s,m)). (?N t/real n + ?N s/ real m) /2"
haftmann@29789
  1836
  let ?F = "\<lambda> p. \<exists> a \<in> set (uset p). \<exists> b \<in> set (uset p). ?I x (usubst p (?g(a,b)))"
haftmann@29789
  1837
  let ?ep = "evaldjf (simpfm o (usubst ?q)) ?Y"
haftmann@29789
  1838
  from rlfm_I[OF simpfm_qf[OF qf]] have lq: "isrlfm ?q" by blast
haftmann@29789
  1839
  from alluopairs_set1[where xs="?U"] have UpU: "set ?Up \<le> (set ?U \<times> set ?U)" by simp
haftmann@29789
  1840
  from uset_l[OF lq] have U_l: "\<forall> (t,n) \<in> set ?U. numbound0 t \<and> n > 0" .
haftmann@29789
  1841
  from U_l UpU 
haftmann@29789
  1842
  have "\<forall> ((t,n),(s,m)) \<in> set ?Up. numbound0 t \<and> n> 0 \<and> numbound0 s \<and> m > 0" by auto
nipkow@56544
  1843
  hence Snb: "\<forall> (t,n) \<in> set ?S. numbound0 t \<and> n > 0 " by auto
haftmann@29789
  1844
  have Y_l: "\<forall> (t,n) \<in> set ?Y. numbound0 t \<and> n > 0" 
haftmann@29789
  1845
  proof-
haftmann@29789
  1846
    { fix t n assume tnY: "(t,n) \<in> set ?Y" 
haftmann@29789
  1847
      hence "(t,n) \<in> set ?SS" by simp
haftmann@29789
  1848
      hence "\<exists> (t',n') \<in> set ?S. simp_num_pair (t',n') = (t,n)"
hoelzl@33639
  1849
        by (auto simp add: split_def simp del: map_map)
hoelzl@33639
  1850
           (rule_tac x="((aa,ba),(ab,bb))" in bexI, simp_all)
haftmann@29789
  1851
      then obtain t' n' where tn'S: "(t',n') \<in> set ?S" and tns: "simp_num_pair (t',n') = (t,n)" by blast
haftmann@29789
  1852
      from tn'S Snb have tnb: "numbound0 t'" and np: "n' > 0" by auto
haftmann@29789
  1853
      from simp_num_pair_l[OF tnb np tns]
haftmann@29789
  1854
      have "numbound0 t \<and> n > 0" . }
haftmann@29789
  1855
    thus ?thesis by blast
haftmann@29789
  1856
  qed
haftmann@29789
  1857
haftmann@29789
  1858
  have YU: "(?f ` set ?Y) = (?h ` (set ?U \<times> set ?U))"
haftmann@29789
  1859
  proof-
haftmann@29789
  1860
     from simp_num_pair_ci[where bs="x#bs"] have 
haftmann@29789
  1861
    "\<forall>x. (?f o simp_num_pair) x = ?f x" by auto
haftmann@29789
  1862
     hence th: "?f o simp_num_pair = ?f" using ext by blast
haftmann@56154
  1863
    have "(?f ` set ?Y) = ((?f o simp_num_pair) ` set ?S)" by (simp add: comp_assoc image_comp)
haftmann@29789
  1864
    also have "\<dots> = (?f ` set ?S)" by (simp add: th)
haftmann@29789
  1865
    also have "\<dots> = ((?f o ?g) ` set ?Up)" 
haftmann@56154
  1866
      by (simp only: set_map o_def image_comp)
haftmann@29789
  1867
    also have "\<dots> = (?h ` (set ?U \<times> set ?U))"
haftmann@56154
  1868
      using uset_cong_aux[OF U_l, where x="x" and bs="bs", simplified set_map image_comp] by blast
haftmann@29789
  1869
    finally show ?thesis .
haftmann@29789
  1870
  qed
haftmann@29789
  1871
  have "\<forall> (t,n) \<in> set ?Y. bound0 (simpfm (usubst ?q (t,n)))"
haftmann@29789
  1872
  proof-
haftmann@29789
  1873
    { fix t n assume tnY: "(t,n) \<in> set ?Y"
haftmann@29789
  1874
      with Y_l have tnb: "numbound0 t" and np: "real n > 0" by auto
haftmann@29789
  1875
      from usubst_I[OF lq np tnb]
haftmann@29789
  1876
    have "bound0 (usubst ?q (t,n))"  by simp hence "bound0 (simpfm (usubst ?q (t,n)))" 
haftmann@29789
  1877
      using simpfm_bound0 by simp}
haftmann@29789
  1878
    thus ?thesis by blast
haftmann@29789
  1879
  qed
haftmann@29789
  1880
  hence ep_nb: "bound0 ?ep"  using evaldjf_bound0[where xs="?Y" and f="simpfm o (usubst ?q)"] by auto
haftmann@29789
  1881
  let ?mp = "minusinf ?q"
haftmann@29789
  1882
  let ?pp = "plusinf ?q"
haftmann@29789
  1883
  let ?M = "?I x ?mp"
haftmann@29789
  1884
  let ?P = "?I x ?pp"
haftmann@29789
  1885
  let ?res = "disj ?mp (disj ?pp ?ep)"
haftmann@29789
  1886
  from rminusinf_bound0[OF lq] rplusinf_bound0[OF lq] ep_nb
haftmann@29789
  1887
  have nbth: "bound0 ?res" by auto
haftmann@29789
  1888
haftmann@29789
  1889
  from conjunct1[OF rlfm_I[OF simpfm_qf[OF qf]]] simpfm  
haftmann@29789
  1890
haftmann@29789
  1891
  have th: "?lhs = (\<exists> x. ?I x ?q)" by auto 
haftmann@29789
  1892
  from th fr_equsubst[OF lq, where bs="bs" and x="x"] have lhfr: "?lhs = (?M \<or> ?P \<or> ?F ?q)"
haftmann@29789
  1893
    by (simp only: split_def fst_conv snd_conv)
haftmann@29789
  1894
  also have "\<dots> = (?M \<or> ?P \<or> (\<exists> (t,n) \<in> set ?Y. ?I x (simpfm (usubst ?q (t,n)))))" 
haftmann@29789
  1895
    using uset_cong[OF lq YU U_l Y_l]  by (simp only: split_def fst_conv snd_conv simpfm) 
haftmann@29789
  1896
  also have "\<dots> = (Ifm (x#bs) ?res)"
haftmann@29789
  1897
    using evaldjf_ex[where ps="?Y" and bs = "x#bs" and f="simpfm o (usubst ?q)",symmetric]
haftmann@29789
  1898
    by (simp add: split_def pair_collapse)
haftmann@29789
  1899
  finally have lheq: "?lhs =  (Ifm bs (decr ?res))" using decr[OF nbth] by blast
haftmann@29789
  1900
  hence lr: "?lhs = ?rhs" apply (unfold ferrack_def Let_def)
haftmann@29789
  1901
    by (cases "?mp = T \<or> ?pp = T", auto) (simp add: disj_def)+
haftmann@29789
  1902
  from decr_qf[OF nbth] have "qfree (ferrack p)" by (auto simp add: Let_def ferrack_def)
haftmann@29789
  1903
  with lr show ?thesis by blast
haftmann@29789
  1904
qed
haftmann@29789
  1905
haftmann@29789
  1906
definition linrqe:: "fm \<Rightarrow> fm" where
haftmann@29789
  1907
  "linrqe p = qelim (prep p) ferrack"
haftmann@29789
  1908
haftmann@29789
  1909
theorem linrqe: "Ifm bs (linrqe p) = Ifm bs p \<and> qfree (linrqe p)"
haftmann@29789
  1910
using ferrack qelim_ci prep
haftmann@29789
  1911
unfolding linrqe_def by auto
haftmann@29789
  1912
haftmann@29789
  1913
definition ferrack_test :: "unit \<Rightarrow> fm" where
haftmann@29789
  1914
  "ferrack_test u = linrqe (A (A (Imp (Lt (Sub (Bound 1) (Bound 0)))
haftmann@29789
  1915
    (E (Eq (Sub (Add (Bound 0) (Bound 2)) (Bound 1)))))))"
haftmann@29789
  1916
wenzelm@51272
  1917
ML_val {* @{code ferrack_test} () *}
haftmann@29789
  1918
haftmann@29789
  1919
oracle linr_oracle = {*
haftmann@29789
  1920
let
haftmann@29789
  1921
haftmann@51143
  1922
val mk_C = @{code C} o @{code int_of_integer};
haftmann@51143
  1923
val mk_Bound = @{code Bound} o @{code nat_of_integer};
haftmann@51143
  1924
haftmann@51143
  1925
fun num_of_term vs (Free vT) = mk_Bound (find_index (fn vT' => vT = vT') vs)
haftmann@51143
  1926
  | num_of_term vs @{term "real (0::int)"} = mk_C 0
haftmann@51143
  1927
  | num_of_term vs @{term "real (1::int)"} = mk_C 1
haftmann@51143
  1928
  | num_of_term vs @{term "0::real"} = mk_C 0
haftmann@51143
  1929
  | num_of_term vs @{term "1::real"} = mk_C 1
haftmann@51143
  1930
  | num_of_term vs (Bound i) = mk_Bound i
haftmann@29789
  1931
  | num_of_term vs (@{term "uminus :: real \<Rightarrow> real"} $ t') = @{code Neg} (num_of_term vs t')
haftmann@36853
  1932
  | num_of_term vs (@{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) =
haftmann@36853
  1933
     @{code Add} (num_of_term vs t1, num_of_term vs t2)
haftmann@36853
  1934
  | num_of_term vs (@{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) =
haftmann@36853
  1935
     @{code Sub} (num_of_term vs t1, num_of_term vs t2)
haftmann@36853
  1936
  | num_of_term vs (@{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) = (case num_of_term vs t1
haftmann@29789
  1937
     of @{code C} i => @{code Mul} (i, num_of_term vs t2)
haftmann@36853
  1938
      | _ => error "num_of_term: unsupported multiplication")
huffman@47108
  1939
  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ t') =
haftmann@51143
  1940
     (mk_C (snd (HOLogic.dest_number t'))
huffman@47108
  1941
       handle TERM _ => error ("num_of_term: unknown term"))
huffman@47108
  1942
  | num_of_term vs t' =
haftmann@51143
  1943
     (mk_C (snd (HOLogic.dest_number t'))
huffman@47108
  1944
       handle TERM _ => error ("num_of_term: unknown term"));
haftmann@29789
  1945
haftmann@29789
  1946
fun fm_of_term vs @{term True} = @{code T}
haftmann@29789
  1947
  | fm_of_term vs @{term False} = @{code F}
haftmann@36853
  1948
  | fm_of_term vs (@{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
haftmann@36853
  1949
      @{code Lt} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
haftmann@36853
  1950
  | fm_of_term vs (@{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
haftmann@36853
  1951
      @{code Le} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
haftmann@36853
  1952
  | fm_of_term vs (@{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
haftmann@36853
  1953
      @{code Eq} (@{code Sub} (num_of_term vs t1, num_of_term vs t2)) 
haftmann@36853
  1954
  | fm_of_term vs (@{term "op \<longleftrightarrow> :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ t1 $ t2) =
haftmann@36853
  1955
      @{code Iff} (fm_of_term vs t1, fm_of_term vs t2)
haftmann@38795
  1956
  | fm_of_term vs (@{term HOL.conj} $ t1 $ t2) = @{code And} (fm_of_term vs t1, fm_of_term vs t2)
haftmann@38795
  1957
  | fm_of_term vs (@{term HOL.disj} $ t1 $ t2) = @{code Or} (fm_of_term vs t1, fm_of_term vs t2)
haftmann@38786
  1958
  | fm_of_term vs (@{term HOL.implies} $ t1 $ t2) = @{code Imp} (fm_of_term vs t1, fm_of_term vs t2)
haftmann@29789
  1959
  | fm_of_term vs (@{term "Not"} $ t') = @{code NOT} (fm_of_term vs t')
haftmann@38558
  1960
  | fm_of_term vs (Const (@{const_name Ex}, _) $ Abs (xn, xT, p)) =
haftmann@36853
  1961
      @{code E} (fm_of_term (("", dummyT) :: vs) p)
haftmann@38558
  1962
  | fm_of_term vs (Const (@{const_name All}, _) $ Abs (xn, xT, p)) =
haftmann@36853
  1963
      @{code A} (fm_of_term (("", dummyT) ::  vs) p)
haftmann@29789
  1964
  | fm_of_term vs t = error ("fm_of_term : unknown term " ^ Syntax.string_of_term @{context} t);
haftmann@29789
  1965
haftmann@51143
  1966
fun term_of_num vs (@{code C} i) = @{term "real :: int \<Rightarrow> real"} $
haftmann@51143
  1967
      HOLogic.mk_number HOLogic.intT (@{code integer_of_int} i)
haftmann@51143
  1968
  | term_of_num vs (@{code Bound} n) = Free (nth vs (@{code integer_of_nat} n))
haftmann@29789
  1969
  | term_of_num vs (@{code Neg} t') = @{term "uminus :: real \<Rightarrow> real"} $ term_of_num vs t'
haftmann@29789
  1970
  | term_of_num vs (@{code Add} (t1, t2)) = @{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $
haftmann@29789
  1971
      term_of_num vs t1 $ term_of_num vs t2
haftmann@29789
  1972
  | term_of_num vs (@{code Sub} (t1, t2)) = @{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $
haftmann@29789
  1973
      term_of_num vs t1 $ term_of_num vs t2
haftmann@29789
  1974
  | term_of_num vs (@{code Mul} (i, t2)) = @{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $
haftmann@29789
  1975
      term_of_num vs (@{code C} i) $ term_of_num vs t2
haftmann@29789
  1976
  | term_of_num vs (@{code CN} (n, i, t)) = term_of_num vs (@{code Add} (@{code Mul} (i, @{code Bound} n), t));
haftmann@29789
  1977
wenzelm@45740
  1978
fun term_of_fm vs @{code T} = @{term True} 
wenzelm@45740
  1979
  | term_of_fm vs @{code F} = @{term False}
haftmann@29789
  1980
  | term_of_fm vs (@{code Lt} t) = @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $
haftmann@29789
  1981
      term_of_num vs t $ @{term "0::real"}
haftmann@29789
  1982
  | term_of_fm vs (@{code Le} t) = @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $
haftmann@29789
  1983
      term_of_num vs t $ @{term "0::real"}
haftmann@29789
  1984
  | term_of_fm vs (@{code Gt} t) = @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $
haftmann@29789
  1985
      @{term "0::real"} $ term_of_num vs t
haftmann@29789
  1986
  | term_of_fm vs (@{code Ge} t) = @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $
haftmann@29789
  1987
      @{term "0::real"} $ term_of_num vs t
haftmann@29789
  1988
  | term_of_fm vs (@{code Eq} t) = @{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $
haftmann@29789
  1989
      term_of_num vs t $ @{term "0::real"}
haftmann@29789
  1990
  | term_of_fm vs (@{code NEq} t) = term_of_fm vs (@{code NOT} (@{code Eq} t))
haftmann@29789
  1991
  | term_of_fm vs (@{code NOT} t') = HOLogic.Not $ term_of_fm vs t'
haftmann@29789
  1992
  | term_of_fm vs (@{code And} (t1, t2)) = HOLogic.conj $ term_of_fm vs t1 $ term_of_fm vs t2
haftmann@29789
  1993
  | term_of_fm vs (@{code Or} (t1, t2)) = HOLogic.disj $ term_of_fm vs t1 $ term_of_fm vs t2
haftmann@29789
  1994
  | term_of_fm vs (@{code Imp}  (t1, t2)) = HOLogic.imp $ term_of_fm vs t1 $ term_of_fm vs t2
haftmann@29789
  1995
  | term_of_fm vs (@{code Iff} (t1, t2)) = @{term "op \<longleftrightarrow> :: bool \<Rightarrow> bool \<Rightarrow> bool"} $
haftmann@36853
  1996
      term_of_fm vs t1 $ term_of_fm vs t2;
haftmann@29789
  1997
haftmann@36853
  1998
in fn (ctxt, t) =>
haftmann@29789
  1999
  let 
haftmann@36853
  2000
    val vs = Term.add_frees t [];
haftmann@36853
  2001
    val t' = (term_of_fm vs o @{code linrqe} o fm_of_term vs) t;
wenzelm@59621
  2002
  in (Thm.cterm_of ctxt o HOLogic.mk_Trueprop o HOLogic.mk_eq) (t, t') end
haftmann@29789
  2003
end;
haftmann@29789
  2004
*}
haftmann@29789
  2005
wenzelm@48891
  2006
ML_file "ferrack_tac.ML"
wenzelm@47432
  2007
wenzelm@47432
  2008
method_setup rferrack = {*
wenzelm@53168
  2009
  Scan.lift (Args.mode "no_quantify") >>
wenzelm@47432
  2010
    (fn q => fn ctxt => SIMPLE_METHOD' (Ferrack_Tac.linr_tac ctxt (not q)))
wenzelm@47432
  2011
*} "decision procedure for linear real arithmetic"
wenzelm@47432
  2012
haftmann@29789
  2013
haftmann@29789
  2014
lemma
haftmann@29789
  2015
  fixes x :: real
haftmann@29789
  2016
  shows "2 * x \<le> 2 * x \<and> 2 * x \<le> 2 * x + 1"
wenzelm@49070
  2017
  by rferrack
haftmann@29789
  2018
haftmann@29789
  2019
lemma
haftmann@29789
  2020
  fixes x :: real
haftmann@29789
  2021
  shows "\<exists>y \<le> x. x = y + 1"
wenzelm@49070
  2022
  by rferrack
haftmann@29789
  2023
haftmann@29789
  2024
lemma
haftmann@29789
  2025
  fixes x :: real
haftmann@29789
  2026
  shows "\<not> (\<exists>z. x + z = x + z + 1)"
wenzelm@49070
  2027
  by rferrack
haftmann@29789
  2028
haftmann@29789
  2029
end