src/HOL/Decision_Procs/MIR.thy
author wenzelm
Wed Aug 10 20:53:43 2011 +0200 (2011-08-10)
changeset 44121 44adaa6db327
parent 44013 5cfc1c36ae97
child 44890 22f665a2e91c
permissions -rw-r--r--
old term operations are legacy;
hoelzl@30439
     1
(*  Title:      HOL/Decision_Procs/MIR.thy
chaieb@23264
     2
    Author:     Amine Chaieb
chaieb@23264
     3
*)
chaieb@23264
     4
chaieb@23264
     5
theory MIR
nipkow@41849
     6
imports Complex_Main Dense_Linear_Order DP_Library
krauss@44013
     7
  "~~/src/HOL/Library/Efficient_Nat" "~~/src/HOL/Library/Old_Recdef"
haftmann@29788
     8
uses ("mir_tac.ML")
haftmann@27368
     9
begin
chaieb@23264
    10
haftmann@27456
    11
section {* Quantifier elimination for @{text "\<real> (0, 1, +, floor, <)"} *}
haftmann@27456
    12
chaieb@23264
    13
declare real_of_int_floor_cancel [simp del]
chaieb@23264
    14
nipkow@41849
    15
lemma myle: fixes a b :: "'a::{ordered_ab_group_add}"
nipkow@41849
    16
  shows "(a \<le> b) = (0 \<le> b - a)"
nipkow@41849
    17
by (metis add_0_left add_le_cancel_right diff_add_cancel)
nipkow@41849
    18
nipkow@41849
    19
lemma myless: fixes a b :: "'a::{ordered_ab_group_add}"
nipkow@41849
    20
  shows "(a < b) = (0 < b - a)"
nipkow@41849
    21
by (metis le_iff_diff_le_0 less_le_not_le myle)
chaieb@23264
    22
chaieb@23264
    23
  (* Maybe should be added to the library \<dots> *)
chaieb@23264
    24
lemma floor_int_eq: "(real n\<le> x \<and> x < real (n+1)) = (floor x = n)"
chaieb@23264
    25
proof( auto)
chaieb@23264
    26
  assume lb: "real n \<le> x"
chaieb@23264
    27
    and ub: "x < real n + 1"
chaieb@23264
    28
  have "real (floor x) \<le> x" by simp 
chaieb@23264
    29
  hence "real (floor x) < real (n + 1) " using ub by arith
chaieb@23264
    30
  hence "floor x < n+1" by simp
huffman@30097
    31
  moreover from lb have "n \<le> floor x" using floor_mono[where x="real n" and y="x"] 
chaieb@23264
    32
    by simp ultimately show "floor x = n" by simp
chaieb@23264
    33
qed
chaieb@23264
    34
chaieb@23264
    35
(* Periodicity of dvd *)
chaieb@23264
    36
lemma dvd_period:
chaieb@23264
    37
  assumes advdd: "(a::int) dvd d"
chaieb@23264
    38
  shows "(a dvd (x + t)) = (a dvd ((x+ c*d) + t))"
chaieb@23264
    39
  using advdd  
chaieb@23264
    40
proof-
chaieb@23316
    41
  {fix x k
chaieb@23316
    42
    from inf_period(3)[OF advdd, rule_format, where x=x and k="-k"]  
chaieb@23316
    43
    have " ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))" by simp}
chaieb@23316
    44
  hence "\<forall>x.\<forall>k. ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))"  by simp
chaieb@23264
    45
  then show ?thesis by simp
chaieb@23264
    46
qed
chaieb@23264
    47
wenzelm@32960
    48
(* The Divisibility relation between reals *)
haftmann@23858
    49
definition
haftmann@23858
    50
  rdvd:: "real \<Rightarrow> real \<Rightarrow> bool" (infixl "rdvd" 50)
haftmann@23858
    51
where
haftmann@23858
    52
  rdvd_def: "x rdvd y \<longleftrightarrow> (\<exists>k\<Colon>int. y = x * real k)"
chaieb@23264
    53
chaieb@23264
    54
lemma int_rdvd_real: 
chaieb@23264
    55
  shows "real (i::int) rdvd x = (i dvd (floor x) \<and> real (floor x) = x)" (is "?l = ?r")
chaieb@23264
    56
proof
chaieb@23264
    57
  assume "?l" 
chaieb@23264
    58
  hence th: "\<exists> k. x=real (i*k)" by (simp add: rdvd_def)
chaieb@23264
    59
  hence th': "real (floor x) = x" by (auto simp del: real_of_int_mult)
chaieb@23264
    60
  with th have "\<exists> k. real (floor x) = real (i*k)" by simp
chaieb@23264
    61
  hence "\<exists> k. floor x = i*k" by (simp only: real_of_int_inject)
chaieb@23264
    62
  thus ?r  using th' by (simp add: dvd_def) 
chaieb@23264
    63
next
chaieb@23264
    64
  assume "?r" hence "(i\<Colon>int) dvd \<lfloor>x\<Colon>real\<rfloor>" ..
chaieb@23264
    65
  hence "\<exists> k. real (floor x) = real (i*k)" 
chaieb@23264
    66
    by (simp only: real_of_int_inject) (simp add: dvd_def)
wenzelm@41807
    67
  thus ?l using `?r` by (simp add: rdvd_def)
chaieb@23264
    68
qed
chaieb@23264
    69
chaieb@23264
    70
lemma int_rdvd_iff: "(real (i::int) rdvd real t) = (i dvd t)"
chaieb@23264
    71
by (auto simp add: rdvd_def dvd_def) (rule_tac x="k" in exI, simp only :real_of_int_mult[symmetric])
chaieb@23264
    72
chaieb@23264
    73
chaieb@23264
    74
lemma rdvd_abs1: 
chaieb@23264
    75
  "(abs (real d) rdvd t) = (real (d ::int) rdvd t)"
chaieb@23264
    76
proof
chaieb@23264
    77
  assume d: "real d rdvd t"
chaieb@23264
    78
  from d int_rdvd_real have d2: "d dvd (floor t)" and ti: "real (floor t) = t" by auto
chaieb@23264
    79
nipkow@30042
    80
  from iffD2[OF abs_dvd_iff] d2 have "(abs d) dvd (floor t)" by blast
chaieb@23264
    81
  with ti int_rdvd_real[symmetric] have "real (abs d) rdvd t" by blast 
chaieb@23264
    82
  thus "abs (real d) rdvd t" by simp
chaieb@23264
    83
next
chaieb@23264
    84
  assume "abs (real d) rdvd t" hence "real (abs d) rdvd t" by simp
chaieb@23264
    85
  with int_rdvd_real[where i="abs d" and x="t"] have d2: "abs d dvd floor t" and ti: "real (floor t) =t" by auto
nipkow@30042
    86
  from iffD1[OF abs_dvd_iff] d2 have "d dvd floor t" by blast
chaieb@23264
    87
  with ti int_rdvd_real[symmetric] show "real d rdvd t" by blast
chaieb@23264
    88
qed
chaieb@23264
    89
chaieb@23264
    90
lemma rdvd_minus: "(real (d::int) rdvd t) = (real d rdvd -t)"
chaieb@23264
    91
  apply (auto simp add: rdvd_def)
chaieb@23264
    92
  apply (rule_tac x="-k" in exI, simp) 
chaieb@23264
    93
  apply (rule_tac x="-k" in exI, simp)
chaieb@23264
    94
done
chaieb@23264
    95
chaieb@23264
    96
lemma rdvd_left_0_eq: "(0 rdvd t) = (t=0)"
chaieb@23264
    97
by (auto simp add: rdvd_def)
chaieb@23264
    98
chaieb@23264
    99
lemma rdvd_mult: 
chaieb@23264
   100
  assumes knz: "k\<noteq>0"
chaieb@23264
   101
  shows "(real (n::int) * real (k::int) rdvd x * real k) = (real n rdvd x)"
chaieb@23264
   102
using knz by (simp add:rdvd_def)
chaieb@23264
   103
chaieb@23264
   104
  (*********************************************************************************)
chaieb@23264
   105
  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
chaieb@23264
   106
  (*********************************************************************************)
chaieb@23264
   107
chaieb@23264
   108
datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
chaieb@23264
   109
  | Mul int num | Floor num| CF int num num
chaieb@23264
   110
chaieb@23264
   111
  (* A size for num to make inductive proofs simpler*)
haftmann@25765
   112
primrec num_size :: "num \<Rightarrow> nat" where
chaieb@23264
   113
 "num_size (C c) = 1"
chaieb@23264
   114
| "num_size (Bound n) = 1"
chaieb@23264
   115
| "num_size (Neg a) = 1 + num_size a"
chaieb@23264
   116
| "num_size (Add a b) = 1 + num_size a + num_size b"
chaieb@23264
   117
| "num_size (Sub a b) = 3 + num_size a + num_size b"
chaieb@23264
   118
| "num_size (CN n c a) = 4 + num_size a "
chaieb@23264
   119
| "num_size (CF c a b) = 4 + num_size a + num_size b"
chaieb@23264
   120
| "num_size (Mul c a) = 1 + num_size a"
chaieb@23264
   121
| "num_size (Floor a) = 1 + num_size a"
chaieb@23264
   122
chaieb@23264
   123
  (* Semantics of numeral terms (num) *)
haftmann@25765
   124
primrec Inum :: "real list \<Rightarrow> num \<Rightarrow> real" where
chaieb@23264
   125
  "Inum bs (C c) = (real c)"
chaieb@23264
   126
| "Inum bs (Bound n) = bs!n"
chaieb@23264
   127
| "Inum bs (CN n c a) = (real c) * (bs!n) + (Inum bs a)"
chaieb@23264
   128
| "Inum bs (Neg a) = -(Inum bs a)"
chaieb@23264
   129
| "Inum bs (Add a b) = Inum bs a + Inum bs b"
chaieb@23264
   130
| "Inum bs (Sub a b) = Inum bs a - Inum bs b"
chaieb@23264
   131
| "Inum bs (Mul c a) = (real c) * Inum bs a"
chaieb@23264
   132
| "Inum bs (Floor a) = real (floor (Inum bs a))"
chaieb@23264
   133
| "Inum bs (CF c a b) = real c * real (floor (Inum bs a)) + Inum bs b"
chaieb@23264
   134
definition "isint t bs \<equiv> real (floor (Inum bs t)) = Inum bs t"
chaieb@23264
   135
chaieb@23264
   136
lemma isint_iff: "isint n bs = (real (floor (Inum bs n)) = Inum bs n)"
chaieb@23264
   137
by (simp add: isint_def)
chaieb@23264
   138
chaieb@23264
   139
lemma isint_Floor: "isint (Floor n) bs"
chaieb@23264
   140
  by (simp add: isint_iff)
chaieb@23264
   141
chaieb@23264
   142
lemma isint_Mul: "isint e bs \<Longrightarrow> isint (Mul c e) bs"
chaieb@23264
   143
proof-
chaieb@23264
   144
  let ?e = "Inum bs e"
chaieb@23264
   145
  let ?fe = "floor ?e"
chaieb@23264
   146
  assume be: "isint e bs" hence efe:"real ?fe = ?e" by (simp add: isint_iff)
chaieb@23264
   147
  have "real ((floor (Inum bs (Mul c e)))) = real (floor (real (c * ?fe)))" using efe by simp
chaieb@23264
   148
  also have "\<dots> = real (c* ?fe)" by (simp only: floor_real_of_int) 
chaieb@23264
   149
  also have "\<dots> = real c * ?e" using efe by simp
chaieb@23264
   150
  finally show ?thesis using isint_iff by simp
chaieb@23264
   151
qed
chaieb@23264
   152
chaieb@23264
   153
lemma isint_neg: "isint e bs \<Longrightarrow> isint (Neg e) bs"
chaieb@23264
   154
proof-
chaieb@23264
   155
  let ?I = "\<lambda> t. Inum bs t"
chaieb@23264
   156
  assume ie: "isint e bs"
chaieb@23264
   157
  hence th: "real (floor (?I e)) = ?I e" by (simp add: isint_def)  
chaieb@23264
   158
  have "real (floor (?I (Neg e))) = real (floor (- (real (floor (?I e)))))" by (simp add: th)
chaieb@23264
   159
  also have "\<dots> = - real (floor (?I e))" by(simp add: floor_minus_real_of_int) 
chaieb@23264
   160
  finally show "isint (Neg e) bs" by (simp add: isint_def th)
chaieb@23264
   161
qed
chaieb@23264
   162
chaieb@23264
   163
lemma isint_sub: 
chaieb@23264
   164
  assumes ie: "isint e bs" shows "isint (Sub (C c) e) bs"
chaieb@23264
   165
proof-
chaieb@23264
   166
  let ?I = "\<lambda> t. Inum bs t"
chaieb@23264
   167
  from ie have th: "real (floor (?I e)) = ?I e" by (simp add: isint_def)  
chaieb@23264
   168
  have "real (floor (?I (Sub (C c) e))) = real (floor ((real (c -floor (?I e)))))" by (simp add: th)
chaieb@23264
   169
  also have "\<dots> = real (c- floor (?I e))" by(simp add: floor_minus_real_of_int) 
chaieb@23264
   170
  finally show "isint (Sub (C c) e) bs" by (simp add: isint_def th)
chaieb@23264
   171
qed
chaieb@23264
   172
chaieb@23264
   173
lemma isint_add: assumes
chaieb@23264
   174
  ai:"isint a bs" and bi: "isint b bs" shows "isint (Add a b) bs"
chaieb@23264
   175
proof-
chaieb@23264
   176
  let ?a = "Inum bs a"
chaieb@23264
   177
  let ?b = "Inum bs b"
chaieb@23264
   178
  from ai bi isint_iff have "real (floor (?a + ?b)) = real (floor (real (floor ?a) + real (floor ?b)))" by simp
chaieb@23264
   179
  also have "\<dots> = real (floor ?a) + real (floor ?b)" by simp
chaieb@23264
   180
  also have "\<dots> = ?a + ?b" using ai bi isint_iff by simp
chaieb@23264
   181
  finally show "isint (Add a b) bs" by (simp add: isint_iff)
chaieb@23264
   182
qed
chaieb@23264
   183
chaieb@23264
   184
lemma isint_c: "isint (C j) bs"
chaieb@23264
   185
  by (simp add: isint_iff)
chaieb@23264
   186
chaieb@23264
   187
chaieb@23264
   188
    (* FORMULAE *)
chaieb@23264
   189
datatype fm  = 
chaieb@23264
   190
  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num| Dvd int num| NDvd int num|
chaieb@23264
   191
  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
chaieb@23264
   192
chaieb@23264
   193
chaieb@23264
   194
  (* A size for fm *)
chaieb@23264
   195
fun fmsize :: "fm \<Rightarrow> nat" where
chaieb@23264
   196
 "fmsize (NOT p) = 1 + fmsize p"
chaieb@23264
   197
| "fmsize (And p q) = 1 + fmsize p + fmsize q"
chaieb@23264
   198
| "fmsize (Or p q) = 1 + fmsize p + fmsize q"
chaieb@23264
   199
| "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
chaieb@23264
   200
| "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
chaieb@23264
   201
| "fmsize (E p) = 1 + fmsize p"
chaieb@23264
   202
| "fmsize (A p) = 4+ fmsize p"
chaieb@23264
   203
| "fmsize (Dvd i t) = 2"
chaieb@23264
   204
| "fmsize (NDvd i t) = 2"
chaieb@23264
   205
| "fmsize p = 1"
chaieb@23264
   206
  (* several lemmas about fmsize *)
wenzelm@32960
   207
lemma fmsize_pos: "fmsize p > 0"
chaieb@23264
   208
by (induct p rule: fmsize.induct) simp_all
chaieb@23264
   209
chaieb@23264
   210
  (* Semantics of formulae (fm) *)
haftmann@25765
   211
primrec Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool" where
chaieb@23264
   212
  "Ifm bs T = True"
chaieb@23264
   213
| "Ifm bs F = False"
chaieb@23264
   214
| "Ifm bs (Lt a) = (Inum bs a < 0)"
chaieb@23264
   215
| "Ifm bs (Gt a) = (Inum bs a > 0)"
chaieb@23264
   216
| "Ifm bs (Le a) = (Inum bs a \<le> 0)"
chaieb@23264
   217
| "Ifm bs (Ge a) = (Inum bs a \<ge> 0)"
chaieb@23264
   218
| "Ifm bs (Eq a) = (Inum bs a = 0)"
chaieb@23264
   219
| "Ifm bs (NEq a) = (Inum bs a \<noteq> 0)"
chaieb@23264
   220
| "Ifm bs (Dvd i b) = (real i rdvd Inum bs b)"
chaieb@23264
   221
| "Ifm bs (NDvd i b) = (\<not>(real i rdvd Inum bs b))"
chaieb@23264
   222
| "Ifm bs (NOT p) = (\<not> (Ifm bs p))"
chaieb@23264
   223
| "Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
chaieb@23264
   224
| "Ifm bs (Or p q) = (Ifm bs p \<or> Ifm bs q)"
chaieb@23264
   225
| "Ifm bs (Imp p q) = ((Ifm bs p) \<longrightarrow> (Ifm bs q))"
chaieb@23264
   226
| "Ifm bs (Iff p q) = (Ifm bs p = Ifm bs q)"
chaieb@23264
   227
| "Ifm bs (E p) = (\<exists> x. Ifm (x#bs) p)"
chaieb@23264
   228
| "Ifm bs (A p) = (\<forall> x. Ifm (x#bs) p)"
chaieb@23264
   229
chaieb@23264
   230
consts prep :: "fm \<Rightarrow> fm"
chaieb@23264
   231
recdef prep "measure fmsize"
chaieb@23264
   232
  "prep (E T) = T"
chaieb@23264
   233
  "prep (E F) = F"
chaieb@23264
   234
  "prep (E (Or p q)) = Or (prep (E p)) (prep (E q))"
chaieb@23264
   235
  "prep (E (Imp p q)) = Or (prep (E (NOT p))) (prep (E q))"
chaieb@23264
   236
  "prep (E (Iff p q)) = Or (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
chaieb@23264
   237
  "prep (E (NOT (And p q))) = Or (prep (E (NOT p))) (prep (E(NOT q)))"
chaieb@23264
   238
  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
chaieb@23264
   239
  "prep (E (NOT (Iff p q))) = Or (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
chaieb@23264
   240
  "prep (E p) = E (prep p)"
chaieb@23264
   241
  "prep (A (And p q)) = And (prep (A p)) (prep (A q))"
chaieb@23264
   242
  "prep (A p) = prep (NOT (E (NOT p)))"
chaieb@23264
   243
  "prep (NOT (NOT p)) = prep p"
chaieb@23264
   244
  "prep (NOT (And p q)) = Or (prep (NOT p)) (prep (NOT q))"
chaieb@23264
   245
  "prep (NOT (A p)) = prep (E (NOT p))"
chaieb@23264
   246
  "prep (NOT (Or p q)) = And (prep (NOT p)) (prep (NOT q))"
chaieb@23264
   247
  "prep (NOT (Imp p q)) = And (prep p) (prep (NOT q))"
chaieb@23264
   248
  "prep (NOT (Iff p q)) = Or (prep (And p (NOT q))) (prep (And (NOT p) q))"
chaieb@23264
   249
  "prep (NOT p) = NOT (prep p)"
chaieb@23264
   250
  "prep (Or p q) = Or (prep p) (prep q)"
chaieb@23264
   251
  "prep (And p q) = And (prep p) (prep q)"
chaieb@23264
   252
  "prep (Imp p q) = prep (Or (NOT p) q)"
chaieb@23264
   253
  "prep (Iff p q) = Or (prep (And p q)) (prep (And (NOT p) (NOT q)))"
chaieb@23264
   254
  "prep p = p"
nipkow@25162
   255
(hints simp add: fmsize_pos)
chaieb@23264
   256
lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
chaieb@23264
   257
by (induct p rule: prep.induct, auto)
chaieb@23264
   258
chaieb@23264
   259
chaieb@23264
   260
  (* Quantifier freeness *)
haftmann@25765
   261
fun qfree:: "fm \<Rightarrow> bool" where
chaieb@23264
   262
  "qfree (E p) = False"
haftmann@25765
   263
  | "qfree (A p) = False"
haftmann@25765
   264
  | "qfree (NOT p) = qfree p" 
haftmann@25765
   265
  | "qfree (And p q) = (qfree p \<and> qfree q)" 
haftmann@25765
   266
  | "qfree (Or  p q) = (qfree p \<and> qfree q)" 
haftmann@25765
   267
  | "qfree (Imp p q) = (qfree p \<and> qfree q)" 
haftmann@25765
   268
  | "qfree (Iff p q) = (qfree p \<and> qfree q)"
haftmann@25765
   269
  | "qfree p = True"
chaieb@23264
   270
chaieb@23264
   271
  (* Boundedness and substitution *)
haftmann@25765
   272
primrec numbound0 :: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *) where
chaieb@23264
   273
  "numbound0 (C c) = True"
haftmann@25765
   274
  | "numbound0 (Bound n) = (n>0)"
haftmann@25765
   275
  | "numbound0 (CN n i a) = (n > 0 \<and> numbound0 a)"
haftmann@25765
   276
  | "numbound0 (Neg a) = numbound0 a"
haftmann@25765
   277
  | "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
haftmann@25765
   278
  | "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
haftmann@25765
   279
  | "numbound0 (Mul i a) = numbound0 a"
haftmann@25765
   280
  | "numbound0 (Floor a) = numbound0 a"
haftmann@25765
   281
  | "numbound0 (CF c a b) = (numbound0 a \<and> numbound0 b)" 
haftmann@25765
   282
chaieb@23264
   283
lemma numbound0_I:
chaieb@23264
   284
  assumes nb: "numbound0 a"
chaieb@23264
   285
  shows "Inum (b#bs) a = Inum (b'#bs) a"
nipkow@41849
   286
  using nb by (induct a) auto
chaieb@23264
   287
chaieb@23264
   288
lemma numbound0_gen: 
chaieb@23264
   289
  assumes nb: "numbound0 t" and ti: "isint t (x#bs)"
chaieb@23264
   290
  shows "\<forall> y. isint t (y#bs)"
chaieb@23264
   291
using nb ti 
chaieb@23264
   292
proof(clarify)
chaieb@23264
   293
  fix y
chaieb@23264
   294
  from numbound0_I[OF nb, where bs="bs" and b="y" and b'="x"] ti[simplified isint_def]
chaieb@23264
   295
  show "isint t (y#bs)"
chaieb@23264
   296
    by (simp add: isint_def)
chaieb@23264
   297
qed
chaieb@23264
   298
haftmann@25765
   299
primrec bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *) where
chaieb@23264
   300
  "bound0 T = True"
haftmann@25765
   301
  | "bound0 F = True"
haftmann@25765
   302
  | "bound0 (Lt a) = numbound0 a"
haftmann@25765
   303
  | "bound0 (Le a) = numbound0 a"
haftmann@25765
   304
  | "bound0 (Gt a) = numbound0 a"
haftmann@25765
   305
  | "bound0 (Ge a) = numbound0 a"
haftmann@25765
   306
  | "bound0 (Eq a) = numbound0 a"
haftmann@25765
   307
  | "bound0 (NEq a) = numbound0 a"
haftmann@25765
   308
  | "bound0 (Dvd i a) = numbound0 a"
haftmann@25765
   309
  | "bound0 (NDvd i a) = numbound0 a"
haftmann@25765
   310
  | "bound0 (NOT p) = bound0 p"
haftmann@25765
   311
  | "bound0 (And p q) = (bound0 p \<and> bound0 q)"
haftmann@25765
   312
  | "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
haftmann@25765
   313
  | "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
haftmann@25765
   314
  | "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
haftmann@25765
   315
  | "bound0 (E p) = False"
haftmann@25765
   316
  | "bound0 (A p) = False"
chaieb@23264
   317
chaieb@23264
   318
lemma bound0_I:
chaieb@23264
   319
  assumes bp: "bound0 p"
chaieb@23264
   320
  shows "Ifm (b#bs) p = Ifm (b'#bs) p"
haftmann@25765
   321
 using bp numbound0_I [where b="b" and bs="bs" and b'="b'"]
nipkow@41849
   322
  by (induct p) auto
haftmann@25765
   323
haftmann@25765
   324
primrec numsubst0:: "num \<Rightarrow> num \<Rightarrow> num" (* substitute a num into a num for Bound 0 *) where
chaieb@23264
   325
  "numsubst0 t (C c) = (C c)"
haftmann@25765
   326
  | "numsubst0 t (Bound n) = (if n=0 then t else Bound n)"
haftmann@25765
   327
  | "numsubst0 t (CN n i a) = (if n=0 then Add (Mul i t) (numsubst0 t a) else CN n i (numsubst0 t a))"
haftmann@25765
   328
  | "numsubst0 t (CF i a b) = CF i (numsubst0 t a) (numsubst0 t b)"
haftmann@25765
   329
  | "numsubst0 t (Neg a) = Neg (numsubst0 t a)"
haftmann@25765
   330
  | "numsubst0 t (Add a b) = Add (numsubst0 t a) (numsubst0 t b)"
haftmann@25765
   331
  | "numsubst0 t (Sub a b) = Sub (numsubst0 t a) (numsubst0 t b)" 
haftmann@25765
   332
  | "numsubst0 t (Mul i a) = Mul i (numsubst0 t a)"
haftmann@25765
   333
  | "numsubst0 t (Floor a) = Floor (numsubst0 t a)"
chaieb@23264
   334
chaieb@23264
   335
lemma numsubst0_I:
chaieb@23264
   336
  shows "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b#bs) a)#bs) t"
nipkow@41849
   337
  by (induct t) simp_all
chaieb@23264
   338
haftmann@25765
   339
primrec subst0:: "num \<Rightarrow> fm \<Rightarrow> fm" (* substitue a num into a formula for Bound 0 *) where
chaieb@23264
   340
  "subst0 t T = T"
haftmann@25765
   341
  | "subst0 t F = F"
haftmann@25765
   342
  | "subst0 t (Lt a) = Lt (numsubst0 t a)"
haftmann@25765
   343
  | "subst0 t (Le a) = Le (numsubst0 t a)"
haftmann@25765
   344
  | "subst0 t (Gt a) = Gt (numsubst0 t a)"
haftmann@25765
   345
  | "subst0 t (Ge a) = Ge (numsubst0 t a)"
haftmann@25765
   346
  | "subst0 t (Eq a) = Eq (numsubst0 t a)"
haftmann@25765
   347
  | "subst0 t (NEq a) = NEq (numsubst0 t a)"
haftmann@25765
   348
  | "subst0 t (Dvd i a) = Dvd i (numsubst0 t a)"
haftmann@25765
   349
  | "subst0 t (NDvd i a) = NDvd i (numsubst0 t a)"
haftmann@25765
   350
  | "subst0 t (NOT p) = NOT (subst0 t p)"
haftmann@25765
   351
  | "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
haftmann@25765
   352
  | "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
haftmann@25765
   353
  | "subst0 t (Imp p q) = Imp (subst0 t p) (subst0 t q)"
haftmann@25765
   354
  | "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
chaieb@23264
   355
chaieb@23264
   356
lemma subst0_I: assumes qfp: "qfree p"
chaieb@23264
   357
  shows "Ifm (b#bs) (subst0 a p) = Ifm ((Inum (b#bs) a)#bs) p"
chaieb@23264
   358
  using qfp numsubst0_I[where b="b" and bs="bs" and a="a"]
nipkow@41849
   359
  by (induct p) simp_all
chaieb@23264
   360
krauss@41839
   361
fun decrnum:: "num \<Rightarrow> num" where
chaieb@23264
   362
  "decrnum (Bound n) = Bound (n - 1)"
krauss@41839
   363
| "decrnum (Neg a) = Neg (decrnum a)"
krauss@41839
   364
| "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
krauss@41839
   365
| "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
krauss@41839
   366
| "decrnum (Mul c a) = Mul c (decrnum a)"
krauss@41839
   367
| "decrnum (Floor a) = Floor (decrnum a)"
krauss@41839
   368
| "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
krauss@41839
   369
| "decrnum (CF c a b) = CF c (decrnum a) (decrnum b)"
krauss@41839
   370
| "decrnum a = a"
krauss@41839
   371
krauss@41839
   372
fun decr :: "fm \<Rightarrow> fm" where
chaieb@23264
   373
  "decr (Lt a) = Lt (decrnum a)"
krauss@41839
   374
| "decr (Le a) = Le (decrnum a)"
krauss@41839
   375
| "decr (Gt a) = Gt (decrnum a)"
krauss@41839
   376
| "decr (Ge a) = Ge (decrnum a)"
krauss@41839
   377
| "decr (Eq a) = Eq (decrnum a)"
krauss@41839
   378
| "decr (NEq a) = NEq (decrnum a)"
krauss@41839
   379
| "decr (Dvd i a) = Dvd i (decrnum a)"
krauss@41839
   380
| "decr (NDvd i a) = NDvd i (decrnum a)"
krauss@41839
   381
| "decr (NOT p) = NOT (decr p)" 
krauss@41839
   382
| "decr (And p q) = And (decr p) (decr q)"
krauss@41839
   383
| "decr (Or p q) = Or (decr p) (decr q)"
krauss@41839
   384
| "decr (Imp p q) = Imp (decr p) (decr q)"
krauss@41839
   385
| "decr (Iff p q) = Iff (decr p) (decr q)"
krauss@41839
   386
| "decr p = p"
chaieb@23264
   387
chaieb@23264
   388
lemma decrnum: assumes nb: "numbound0 t"
chaieb@23264
   389
  shows "Inum (x#bs) t = Inum bs (decrnum t)"
nipkow@41849
   390
  using nb by (induct t rule: decrnum.induct, simp_all)
chaieb@23264
   391
chaieb@23264
   392
lemma decr: assumes nb: "bound0 p"
chaieb@23264
   393
  shows "Ifm (x#bs) p = Ifm bs (decr p)"
chaieb@23264
   394
  using nb 
nipkow@41849
   395
  by (induct p rule: decr.induct, simp_all add: decrnum)
chaieb@23264
   396
chaieb@23264
   397
lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
chaieb@23264
   398
by (induct p, simp_all)
chaieb@23264
   399
krauss@41839
   400
fun isatom :: "fm \<Rightarrow> bool" (* test for atomicity *) where
chaieb@23264
   401
  "isatom T = True"
krauss@41839
   402
| "isatom F = True"
krauss@41839
   403
| "isatom (Lt a) = True"
krauss@41839
   404
| "isatom (Le a) = True"
krauss@41839
   405
| "isatom (Gt a) = True"
krauss@41839
   406
| "isatom (Ge a) = True"
krauss@41839
   407
| "isatom (Eq a) = True"
krauss@41839
   408
| "isatom (NEq a) = True"
krauss@41839
   409
| "isatom (Dvd i b) = True"
krauss@41839
   410
| "isatom (NDvd i b) = True"
krauss@41839
   411
| "isatom p = False"
chaieb@23264
   412
chaieb@23264
   413
lemma numsubst0_numbound0: assumes nb: "numbound0 t"
chaieb@23264
   414
  shows "numbound0 (numsubst0 t a)"
haftmann@25765
   415
using nb by (induct a, auto)
chaieb@23264
   416
chaieb@23264
   417
lemma subst0_bound0: assumes qf: "qfree p" and nb: "numbound0 t"
chaieb@23264
   418
  shows "bound0 (subst0 t p)"
haftmann@25765
   419
using qf numsubst0_numbound0[OF nb] by (induct p, auto)
chaieb@23264
   420
chaieb@23264
   421
lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
chaieb@23264
   422
by (induct p, simp_all)
chaieb@23264
   423
chaieb@23264
   424
haftmann@25765
   425
definition djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@25765
   426
  "djf f p q = (if q=T then T else if q=F then f p else 
chaieb@23264
   427
  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or fp q))"
haftmann@25765
   428
haftmann@25765
   429
definition evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
haftmann@25765
   430
  "evaldjf f ps = foldr (djf f) ps F"
chaieb@23264
   431
chaieb@23264
   432
lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
chaieb@23264
   433
by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
chaieb@23264
   434
(cases "f p", simp_all add: Let_def djf_def) 
chaieb@23264
   435
chaieb@23264
   436
lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
chaieb@23264
   437
  by(induct ps, simp_all add: evaldjf_def djf_Or)
chaieb@23264
   438
chaieb@23264
   439
lemma evaldjf_bound0: 
chaieb@23264
   440
  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
chaieb@23264
   441
  shows "bound0 (evaldjf f xs)"
chaieb@23264
   442
  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
chaieb@23264
   443
chaieb@23264
   444
lemma evaldjf_qf: 
chaieb@23264
   445
  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
chaieb@23264
   446
  shows "qfree (evaldjf f xs)"
chaieb@23264
   447
  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
chaieb@23264
   448
krauss@41839
   449
fun disjuncts :: "fm \<Rightarrow> fm list" where
chaieb@23264
   450
  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
krauss@41839
   451
| "disjuncts F = []"
krauss@41839
   452
| "disjuncts p = [p]"
krauss@41839
   453
krauss@41839
   454
fun conjuncts :: "fm \<Rightarrow> fm list" where
chaieb@23264
   455
  "conjuncts (And p q) = (conjuncts p) @ (conjuncts q)"
krauss@41839
   456
| "conjuncts T = []"
krauss@41839
   457
| "conjuncts p = [p]"
krauss@41839
   458
chaieb@23264
   459
lemma conjuncts: "(\<forall> q\<in> set (conjuncts p). Ifm bs q) = Ifm bs p"
chaieb@23264
   460
by(induct p rule: conjuncts.induct, auto)
chaieb@23264
   461
chaieb@23264
   462
lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
chaieb@23264
   463
proof-
chaieb@23264
   464
  assume qf: "qfree p"
chaieb@23264
   465
  hence "list_all qfree (disjuncts p)"
chaieb@23264
   466
    by (induct p rule: disjuncts.induct, auto)
chaieb@23264
   467
  thus ?thesis by (simp only: list_all_iff)
chaieb@23264
   468
qed
chaieb@23264
   469
lemma conjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). qfree q"
chaieb@23264
   470
proof-
chaieb@23264
   471
  assume qf: "qfree p"
chaieb@23264
   472
  hence "list_all qfree (conjuncts p)"
chaieb@23264
   473
    by (induct p rule: conjuncts.induct, auto)
chaieb@23264
   474
  thus ?thesis by (simp only: list_all_iff)
chaieb@23264
   475
qed
chaieb@23264
   476
haftmann@35416
   477
definition DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@23264
   478
  "DJ f p \<equiv> evaldjf f (disjuncts p)"
chaieb@23264
   479
chaieb@23264
   480
lemma DJ: assumes fdj: "\<forall> p q. f (Or p q) = Or (f p) (f q)"
chaieb@23264
   481
  and fF: "f F = F"
chaieb@23264
   482
  shows "Ifm bs (DJ f p) = Ifm bs (f p)"
chaieb@23264
   483
proof-
chaieb@23264
   484
  have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
chaieb@23264
   485
    by (simp add: DJ_def evaldjf_ex) 
chaieb@23264
   486
  also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
chaieb@23264
   487
  finally show ?thesis .
chaieb@23264
   488
qed
chaieb@23264
   489
chaieb@23264
   490
lemma DJ_qf: assumes 
chaieb@23264
   491
  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
chaieb@23264
   492
  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
chaieb@23264
   493
proof(clarify)
chaieb@23264
   494
  fix  p assume qf: "qfree p"
chaieb@23264
   495
  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
chaieb@23264
   496
  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
chaieb@23264
   497
  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
chaieb@23264
   498
  
chaieb@23264
   499
  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
chaieb@23264
   500
qed
chaieb@23264
   501
chaieb@23264
   502
lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
chaieb@23264
   503
  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
chaieb@23264
   504
proof(clarify)
chaieb@23264
   505
  fix p::fm and bs
chaieb@23264
   506
  assume qf: "qfree p"
chaieb@23264
   507
  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
chaieb@23264
   508
  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
chaieb@23264
   509
  have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
chaieb@23264
   510
    by (simp add: DJ_def evaldjf_ex)
chaieb@23264
   511
  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
chaieb@23264
   512
  also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
chaieb@23264
   513
  finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
chaieb@23264
   514
qed
chaieb@23264
   515
  (* Simplification *)
chaieb@23264
   516
chaieb@23264
   517
  (* Algebraic simplifications for nums *)
krauss@41839
   518
fun bnds:: "num \<Rightarrow> nat list" where
chaieb@23264
   519
  "bnds (Bound n) = [n]"
krauss@41839
   520
| "bnds (CN n c a) = n#(bnds a)"
krauss@41839
   521
| "bnds (Neg a) = bnds a"
krauss@41839
   522
| "bnds (Add a b) = (bnds a)@(bnds b)"
krauss@41839
   523
| "bnds (Sub a b) = (bnds a)@(bnds b)"
krauss@41839
   524
| "bnds (Mul i a) = bnds a"
krauss@41839
   525
| "bnds (Floor a) = bnds a"
krauss@41839
   526
| "bnds (CF c a b) = (bnds a)@(bnds b)"
krauss@41839
   527
| "bnds a = []"
krauss@41839
   528
fun lex_ns:: "nat list \<Rightarrow> nat list \<Rightarrow> bool" where
krauss@41839
   529
  "lex_ns [] ms = True"
krauss@41839
   530
| "lex_ns ns [] = False"
krauss@41839
   531
| "lex_ns (n#ns) (m#ms) = (n<m \<or> ((n = m) \<and> lex_ns ns ms)) "
haftmann@35416
   532
definition lex_bnd :: "num \<Rightarrow> num \<Rightarrow> bool" where
krauss@41839
   533
  "lex_bnd t s \<equiv> lex_ns (bnds t) (bnds s)"
krauss@41839
   534
krauss@41839
   535
fun maxcoeff:: "num \<Rightarrow> int" where
chaieb@23264
   536
  "maxcoeff (C i) = abs i"
krauss@41839
   537
| "maxcoeff (CN n c t) = max (abs c) (maxcoeff t)"
krauss@41839
   538
| "maxcoeff (CF c t s) = max (abs c) (maxcoeff s)"
krauss@41839
   539
| "maxcoeff t = 1"
chaieb@23264
   540
chaieb@23264
   541
lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
chaieb@23264
   542
  apply (induct t rule: maxcoeff.induct, auto) 
chaieb@23264
   543
  done
chaieb@23264
   544
krauss@41839
   545
fun numgcdh:: "num \<Rightarrow> int \<Rightarrow> int" where
huffman@31706
   546
  "numgcdh (C i) = (\<lambda>g. gcd i g)"
krauss@41839
   547
| "numgcdh (CN n c t) = (\<lambda>g. gcd c (numgcdh t g))"
krauss@41839
   548
| "numgcdh (CF c s t) = (\<lambda>g. gcd c (numgcdh t g))"
krauss@41839
   549
| "numgcdh t = (\<lambda>g. 1)"
haftmann@23858
   550
haftmann@23858
   551
definition
haftmann@23858
   552
  numgcd :: "num \<Rightarrow> int"
haftmann@23858
   553
where
haftmann@23858
   554
  numgcd_def: "numgcd t = numgcdh t (maxcoeff t)"
chaieb@23264
   555
krauss@41839
   556
fun reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num" where
chaieb@23264
   557
  "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
krauss@41839
   558
| "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
krauss@41839
   559
| "reducecoeffh (CF c s t) = (\<lambda> g. CF (c div g)  s (reducecoeffh t g))"
krauss@41839
   560
| "reducecoeffh t = (\<lambda>g. t)"
chaieb@23264
   561
haftmann@23858
   562
definition
haftmann@23858
   563
  reducecoeff :: "num \<Rightarrow> num"
haftmann@23858
   564
where
haftmann@23858
   565
  reducecoeff_def: "reducecoeff t =
chaieb@23264
   566
  (let g = numgcd t in 
chaieb@23264
   567
  if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
chaieb@23264
   568
krauss@41839
   569
fun dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool" where
chaieb@23264
   570
  "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
krauss@41839
   571
| "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
krauss@41839
   572
| "dvdnumcoeff (CF c s t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
krauss@41839
   573
| "dvdnumcoeff t = (\<lambda>g. False)"
chaieb@23264
   574
chaieb@23264
   575
lemma dvdnumcoeff_trans: 
chaieb@23264
   576
  assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
chaieb@23264
   577
  shows "dvdnumcoeff t g"
chaieb@23264
   578
  using dgt' gdg 
nipkow@30042
   579
  by (induct t rule: dvdnumcoeff.induct, simp_all add: gdg dvd_trans[OF gdg])
nipkow@30042
   580
nipkow@30042
   581
declare dvd_trans [trans add]
chaieb@23264
   582
chaieb@23264
   583
lemma numgcd0:
chaieb@23264
   584
  assumes g0: "numgcd t = 0"
chaieb@23264
   585
  shows "Inum bs t = 0"
chaieb@23264
   586
proof-
chaieb@23264
   587
  have "\<And>x. numgcdh t x= 0 \<Longrightarrow> Inum bs t = 0"
huffman@31706
   588
    by (induct t rule: numgcdh.induct, auto)
chaieb@23264
   589
  thus ?thesis using g0[simplified numgcd_def] by blast
chaieb@23264
   590
qed
chaieb@23264
   591
chaieb@23264
   592
lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
chaieb@23264
   593
  using gp
huffman@31706
   594
  by (induct t rule: numgcdh.induct, auto)
chaieb@23264
   595
chaieb@23264
   596
lemma numgcd_pos: "numgcd t \<ge>0"
chaieb@23264
   597
  by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
chaieb@23264
   598
chaieb@23264
   599
lemma reducecoeffh:
chaieb@23264
   600
  assumes gt: "dvdnumcoeff t g" and gp: "g > 0" 
chaieb@23264
   601
  shows "real g *(Inum bs (reducecoeffh t g)) = Inum bs t"
chaieb@23264
   602
  using gt
chaieb@23264
   603
proof(induct t rule: reducecoeffh.induct) 
chaieb@23264
   604
  case (1 i) hence gd: "g dvd i" by simp
chaieb@23264
   605
  from gp have gnz: "g \<noteq> 0" by simp
wenzelm@41807
   606
  from assms 1 show ?case by (simp add: real_of_int_div[OF gnz gd])
chaieb@23264
   607
next
chaieb@23264
   608
  case (2 n c t)  hence gd: "g dvd c" by simp
chaieb@23264
   609
  from gp have gnz: "g \<noteq> 0" by simp
wenzelm@41807
   610
  from assms 2 show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps)
chaieb@23264
   611
next
chaieb@23264
   612
  case (3 c s t)  hence gd: "g dvd c" by simp
chaieb@23264
   613
  from gp have gnz: "g \<noteq> 0" by simp
wenzelm@41807
   614
  from assms 3 show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps) 
chaieb@23264
   615
qed (auto simp add: numgcd_def gp)
wenzelm@41807
   616
krauss@41839
   617
fun ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool" where
chaieb@23264
   618
  "ismaxcoeff (C i) = (\<lambda> x. abs i \<le> x)"
krauss@41839
   619
| "ismaxcoeff (CN n c t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
krauss@41839
   620
| "ismaxcoeff (CF c s t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
krauss@41839
   621
| "ismaxcoeff t = (\<lambda>x. True)"
chaieb@23264
   622
chaieb@23264
   623
lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
chaieb@23264
   624
by (induct t rule: ismaxcoeff.induct, auto)
chaieb@23264
   625
chaieb@23264
   626
lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
chaieb@23264
   627
proof (induct t rule: maxcoeff.induct)
chaieb@23264
   628
  case (2 n c t)
chaieb@23264
   629
  hence H:"ismaxcoeff t (maxcoeff t)" .
wenzelm@41807
   630
  have thh: "maxcoeff t \<le> max (abs c) (maxcoeff t)" by simp
chaieb@23264
   631
  from ismaxcoeff_mono[OF H thh] show ?case by (simp add: le_maxI1)
chaieb@23264
   632
next
chaieb@23264
   633
  case (3 c t s) 
chaieb@23264
   634
  hence H1:"ismaxcoeff s (maxcoeff s)" by auto
chaieb@23264
   635
  have thh1: "maxcoeff s \<le> max \<bar>c\<bar> (maxcoeff s)" by (simp add: max_def)
chaieb@23264
   636
  from ismaxcoeff_mono[OF H1 thh1] show ?case by (simp add: le_maxI1)
chaieb@23264
   637
qed simp_all
chaieb@23264
   638
huffman@31706
   639
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
   640
  apply (unfold gcd_int_def)
chaieb@23264
   641
  apply (cases "i = 0", simp_all)
chaieb@23264
   642
  apply (cases "j = 0", simp_all)
chaieb@23264
   643
  apply (cases "abs i = 1", simp_all)
chaieb@23264
   644
  apply (cases "abs j = 1", simp_all)
chaieb@23264
   645
  apply auto
chaieb@23264
   646
  done
chaieb@23264
   647
lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
wenzelm@41807
   648
  by (induct t rule: numgcdh.induct) auto
chaieb@23264
   649
chaieb@23264
   650
lemma dvdnumcoeff_aux:
chaieb@23264
   651
  assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
chaieb@23264
   652
  shows "dvdnumcoeff t (numgcdh t m)"
wenzelm@41807
   653
using assms
chaieb@23264
   654
proof(induct t rule: numgcdh.induct)
chaieb@23264
   655
  case (2 n c t) 
chaieb@23264
   656
  let ?g = "numgcdh t m"
wenzelm@41807
   657
  from 2 have th:"gcd c ?g > 1" by simp
haftmann@27556
   658
  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
chaieb@23264
   659
  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
   660
  moreover {assume "abs c > 1" and gp: "?g > 1" with 2
chaieb@23264
   661
    have th: "dvdnumcoeff t ?g" by simp
huffman@31706
   662
    have th': "gcd c ?g dvd ?g" by simp
huffman@31706
   663
    from dvdnumcoeff_trans[OF th' th] have ?case by simp }
chaieb@23264
   664
  moreover {assume "abs c = 0 \<and> ?g > 1"
wenzelm@41807
   665
    with 2 have th: "dvdnumcoeff t ?g" by simp
huffman@31706
   666
    have th': "gcd c ?g dvd ?g" by simp
huffman@31706
   667
    from dvdnumcoeff_trans[OF th' th] have ?case by simp
chaieb@23264
   668
    hence ?case by simp }
chaieb@23264
   669
  moreover {assume "abs c > 1" and g0:"?g = 0" 
wenzelm@41807
   670
    from numgcdh0[OF g0] have "m=0". with 2 g0 have ?case by simp }
chaieb@23264
   671
  ultimately show ?case by blast
chaieb@23264
   672
next
chaieb@23264
   673
  case (3 c s t) 
chaieb@23264
   674
  let ?g = "numgcdh t m"
wenzelm@41807
   675
  from 3 have th:"gcd c ?g > 1" by simp
haftmann@27556
   676
  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
chaieb@23264
   677
  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
   678
  moreover {assume "abs c > 1" and gp: "?g > 1" with 3
chaieb@23264
   679
    have th: "dvdnumcoeff t ?g" by simp
huffman@31706
   680
    have th': "gcd c ?g dvd ?g" by simp
huffman@31706
   681
    from dvdnumcoeff_trans[OF th' th] have ?case by simp }
chaieb@23264
   682
  moreover {assume "abs c = 0 \<and> ?g > 1"
wenzelm@41807
   683
    with 3 have th: "dvdnumcoeff t ?g" by simp
huffman@31706
   684
    have th': "gcd c ?g dvd ?g" by simp
huffman@31706
   685
    from dvdnumcoeff_trans[OF th' th] have ?case by simp
chaieb@23264
   686
    hence ?case by simp }
chaieb@23264
   687
  moreover {assume "abs c > 1" and g0:"?g = 0" 
wenzelm@41807
   688
    from numgcdh0[OF g0] have "m=0". with 3 g0 have ?case by simp }
chaieb@23264
   689
  ultimately show ?case by blast
huffman@31706
   690
qed auto
chaieb@23264
   691
chaieb@23264
   692
lemma dvdnumcoeff_aux2:
chaieb@23264
   693
  assumes "numgcd t > 1" shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
wenzelm@41807
   694
  using assms 
chaieb@23264
   695
proof (simp add: numgcd_def)
chaieb@23264
   696
  let ?mc = "maxcoeff t"
chaieb@23264
   697
  let ?g = "numgcdh t ?mc"
chaieb@23264
   698
  have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
chaieb@23264
   699
  have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
chaieb@23264
   700
  assume H: "numgcdh t ?mc > 1"
wenzelm@41807
   701
  from dvdnumcoeff_aux[OF th1 th2 H] show "dvdnumcoeff t ?g" .
chaieb@23264
   702
qed
chaieb@23264
   703
chaieb@23264
   704
lemma reducecoeff: "real (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
chaieb@23264
   705
proof-
chaieb@23264
   706
  let ?g = "numgcd t"
chaieb@23264
   707
  have "?g \<ge> 0"  by (simp add: numgcd_pos)
wenzelm@32960
   708
  hence "?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
chaieb@23264
   709
  moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)} 
chaieb@23264
   710
  moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)} 
chaieb@23264
   711
  moreover { assume g1:"?g > 1"
chaieb@23264
   712
    from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
chaieb@23264
   713
    from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis 
chaieb@23264
   714
      by (simp add: reducecoeff_def Let_def)} 
chaieb@23264
   715
  ultimately show ?thesis by blast
chaieb@23264
   716
qed
chaieb@23264
   717
chaieb@23264
   718
lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
chaieb@23264
   719
by (induct t rule: reducecoeffh.induct, auto)
chaieb@23264
   720
chaieb@23264
   721
lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
chaieb@23264
   722
using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
chaieb@23264
   723
chaieb@23264
   724
consts
chaieb@23264
   725
  numadd:: "num \<times> num \<Rightarrow> num"
chaieb@23264
   726
chaieb@23264
   727
recdef numadd "measure (\<lambda> (t,s). size t + size s)"
chaieb@23264
   728
  "numadd (CN n1 c1 r1,CN n2 c2 r2) =
chaieb@23264
   729
  (if n1=n2 then 
chaieb@23264
   730
  (let c = c1 + c2
chaieb@23264
   731
  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
chaieb@23264
   732
  else if n1 \<le> n2 then CN n1 c1 (numadd (r1,CN n2 c2 r2))
chaieb@23264
   733
  else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
chaieb@23264
   734
  "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"  
chaieb@23264
   735
  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
chaieb@23264
   736
  "numadd (CF c1 t1 r1,CF c2 t2 r2) = 
chaieb@23264
   737
   (if t1 = t2 then 
chaieb@23264
   738
    (let c=c1+c2; s= numadd(r1,r2) in (if c=0 then s else CF c t1 s))
chaieb@23264
   739
   else if lex_bnd t1 t2 then CF c1 t1 (numadd(r1,CF c2 t2 r2))
chaieb@23264
   740
   else CF c2 t2 (numadd(CF c1 t1 r1,r2)))"
chaieb@23264
   741
  "numadd (CF c1 t1 r1,C c) = CF c1 t1 (numadd (r1, C c))"
chaieb@23264
   742
  "numadd (C c,CF c1 t1 r1) = CF c1 t1 (numadd (r1, C c))"
chaieb@23264
   743
  "numadd (C b1, C b2) = C (b1+b2)"
chaieb@23264
   744
  "numadd (a,b) = Add a b"
chaieb@23264
   745
chaieb@23264
   746
lemma numadd[simp]: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
chaieb@23264
   747
apply (induct t s rule: numadd.induct, simp_all add: Let_def)
nipkow@23477
   748
 apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
nipkow@29667
   749
  apply (case_tac "n1 = n2", simp_all add: algebra_simps)
nipkow@23477
   750
  apply (simp only: left_distrib[symmetric])
nipkow@23477
   751
 apply simp
chaieb@23264
   752
apply (case_tac "lex_bnd t1 t2", simp_all)
nipkow@23477
   753
 apply (case_tac "c1+c2 = 0")
nipkow@29667
   754
  by (case_tac "t1 = t2", simp_all add: algebra_simps left_distrib[symmetric] real_of_int_mult[symmetric] real_of_int_add[symmetric]del: real_of_int_mult real_of_int_add left_distrib)
chaieb@23264
   755
chaieb@23264
   756
lemma numadd_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
chaieb@23264
   757
by (induct t s rule: numadd.induct, auto simp add: Let_def)
chaieb@23264
   758
krauss@41839
   759
fun nummul:: "num \<Rightarrow> int \<Rightarrow> num" where
chaieb@23264
   760
  "nummul (C j) = (\<lambda> i. C (i*j))"
krauss@41839
   761
| "nummul (CN n c t) = (\<lambda> i. CN n (c*i) (nummul t i))"
krauss@41839
   762
| "nummul (CF c t s) = (\<lambda> i. CF (c*i) t (nummul s i))"
krauss@41839
   763
| "nummul (Mul c t) = (\<lambda> i. nummul t (i*c))"
krauss@41839
   764
| "nummul t = (\<lambda> i. Mul i t)"
chaieb@23264
   765
chaieb@23264
   766
lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
nipkow@29667
   767
by (induct t rule: nummul.induct, auto simp add: algebra_simps)
chaieb@23264
   768
chaieb@23264
   769
lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
chaieb@23264
   770
by (induct t rule: nummul.induct, auto)
chaieb@23264
   771
haftmann@35416
   772
definition numneg :: "num \<Rightarrow> num" where
chaieb@23264
   773
  "numneg t \<equiv> nummul t (- 1)"
chaieb@23264
   774
haftmann@35416
   775
definition numsub :: "num \<Rightarrow> num \<Rightarrow> num" where
chaieb@23264
   776
  "numsub s t \<equiv> (if s = t then C 0 else numadd (s,numneg t))"
chaieb@23264
   777
chaieb@23264
   778
lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
chaieb@23264
   779
using numneg_def nummul by simp
chaieb@23264
   780
chaieb@23264
   781
lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
chaieb@23264
   782
using numneg_def by simp
chaieb@23264
   783
chaieb@23264
   784
lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
chaieb@23264
   785
using numsub_def by simp
chaieb@23264
   786
chaieb@23264
   787
lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
chaieb@23264
   788
using numsub_def by simp
chaieb@23264
   789
chaieb@23264
   790
lemma isint_CF: assumes si: "isint s bs" shows "isint (CF c t s) bs"
chaieb@23264
   791
proof-
chaieb@23264
   792
  have cti: "isint (Mul c (Floor t)) bs" by (simp add: isint_Mul isint_Floor)
chaieb@23264
   793
  
chaieb@23264
   794
  have "?thesis = isint (Add (Mul c (Floor t)) s) bs" by (simp add: isint_def)
chaieb@23264
   795
  also have "\<dots>" by (simp add: isint_add cti si)
chaieb@23264
   796
  finally show ?thesis .
chaieb@23264
   797
qed
chaieb@23264
   798
krauss@41839
   799
fun split_int:: "num \<Rightarrow> num \<times> num" where
chaieb@23264
   800
  "split_int (C c) = (C 0, C c)"
krauss@41839
   801
| "split_int (CN n c b) = 
chaieb@23264
   802
     (let (bv,bi) = split_int b 
chaieb@23264
   803
       in (CN n c bv, bi))"
krauss@41839
   804
| "split_int (CF c a b) = 
chaieb@23264
   805
     (let (bv,bi) = split_int b 
chaieb@23264
   806
       in (bv, CF c a bi))"
krauss@41839
   807
| "split_int a = (a,C 0)"
chaieb@23264
   808
wenzelm@41807
   809
lemma split_int: "\<And>tv ti. split_int t = (tv,ti) \<Longrightarrow> (Inum bs (Add tv ti) = Inum bs t) \<and> isint ti bs"
chaieb@23264
   810
proof (induct t rule: split_int.induct)
chaieb@23264
   811
  case (2 c n b tv ti)
chaieb@23264
   812
  let ?bv = "fst (split_int b)"
chaieb@23264
   813
  let ?bi = "snd (split_int b)"
chaieb@23264
   814
  have "split_int b = (?bv,?bi)" by simp
wenzelm@41807
   815
  with 2(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
wenzelm@41807
   816
  from 2(2) have tibi: "ti = ?bi" by (simp add: Let_def split_def)
wenzelm@41807
   817
  from 2(2) b[symmetric] bii show ?case by (auto simp add: Let_def split_def)
chaieb@23264
   818
next
chaieb@23264
   819
  case (3 c a b tv ti) 
chaieb@23264
   820
  let ?bv = "fst (split_int b)"
chaieb@23264
   821
  let ?bi = "snd (split_int b)"
chaieb@23264
   822
  have "split_int b = (?bv,?bi)" by simp
wenzelm@41807
   823
  with 3(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
wenzelm@41807
   824
  from 3(2) have tibi: "ti = CF c a ?bi"
wenzelm@41807
   825
    by (simp add: Let_def split_def)
wenzelm@41807
   826
  from 3(2) b[symmetric] bii show ?case
wenzelm@41807
   827
    by (auto simp add: Let_def split_def isint_Floor isint_add isint_Mul isint_CF)
nipkow@29667
   828
qed (auto simp add: Let_def isint_iff isint_Floor isint_add isint_Mul split_def algebra_simps)
chaieb@23264
   829
chaieb@23264
   830
lemma split_int_nb: "numbound0 t \<Longrightarrow> numbound0 (fst (split_int t)) \<and> numbound0 (snd (split_int t)) "
wenzelm@41807
   831
  by (induct t rule: split_int.induct) (auto simp add: Let_def split_def)
wenzelm@41807
   832
wenzelm@41807
   833
definition numfloor:: "num \<Rightarrow> num"
haftmann@23858
   834
where
wenzelm@41807
   835
  "numfloor t = (let (tv,ti) = split_int t in 
chaieb@23264
   836
  (case tv of C i \<Rightarrow> numadd (tv,ti) 
chaieb@23264
   837
  | _ \<Rightarrow> numadd(CF 1 tv (C 0),ti)))"
chaieb@23264
   838
chaieb@23264
   839
lemma numfloor[simp]: "Inum bs (numfloor t) = Inum bs (Floor t)" (is "?n t = ?N (Floor t)")
chaieb@23264
   840
proof-
chaieb@23264
   841
  let ?tv = "fst (split_int t)"
chaieb@23264
   842
  let ?ti = "snd (split_int t)"
chaieb@23264
   843
  have tvti:"split_int t = (?tv,?ti)" by simp
chaieb@23264
   844
  {assume H: "\<forall> v. ?tv \<noteq> C v"
chaieb@23264
   845
    hence th1: "?n t = ?N (Add (Floor ?tv) ?ti)" 
chaieb@23264
   846
      by (cases ?tv, auto simp add: numfloor_def Let_def split_def numadd)
chaieb@23264
   847
    from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
chaieb@23264
   848
    hence "?N (Floor t) = real (floor (?N (Add ?tv ?ti)))" by simp 
chaieb@23264
   849
    also have "\<dots> = real (floor (?N ?tv) + (floor (?N ?ti)))"
chaieb@23264
   850
      by (simp,subst tii[simplified isint_iff, symmetric]) simp
chaieb@23264
   851
    also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
chaieb@23264
   852
    finally have ?thesis using th1 by simp}
chaieb@23264
   853
  moreover {fix v assume H:"?tv = C v" 
chaieb@23264
   854
    from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
chaieb@23264
   855
    hence "?N (Floor t) = real (floor (?N (Add ?tv ?ti)))" by simp 
chaieb@23264
   856
    also have "\<dots> = real (floor (?N ?tv) + (floor (?N ?ti)))"
chaieb@23264
   857
      by (simp,subst tii[simplified isint_iff, symmetric]) simp
chaieb@23264
   858
    also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
chaieb@23264
   859
    finally have ?thesis by (simp add: H numfloor_def Let_def split_def numadd) }
chaieb@23264
   860
  ultimately show ?thesis by auto
chaieb@23264
   861
qed
chaieb@23264
   862
chaieb@23264
   863
lemma numfloor_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numfloor t)"
chaieb@23264
   864
  using split_int_nb[where t="t"]
chaieb@23264
   865
  by (cases "fst(split_int t)" , auto simp add: numfloor_def Let_def split_def  numadd_nb)
chaieb@23264
   866
krauss@41839
   867
function simpnum:: "num \<Rightarrow> num" where
chaieb@23264
   868
  "simpnum (C j) = C j"
krauss@41839
   869
| "simpnum (Bound n) = CN n 1 (C 0)"
krauss@41839
   870
| "simpnum (Neg t) = numneg (simpnum t)"
krauss@41839
   871
| "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
krauss@41839
   872
| "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
krauss@41839
   873
| "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
krauss@41839
   874
| "simpnum (Floor t) = numfloor (simpnum t)"
krauss@41839
   875
| "simpnum (CN n c t) = (if c=0 then simpnum t else CN n c (simpnum t))"
krauss@41839
   876
| "simpnum (CF c t s) = simpnum(Add (Mul c (Floor t)) s)"
krauss@41839
   877
by pat_completeness auto
krauss@41839
   878
termination by (relation "measure num_size") auto
chaieb@23264
   879
chaieb@23264
   880
lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
chaieb@23264
   881
by (induct t rule: simpnum.induct, auto)
chaieb@23264
   882
chaieb@23264
   883
lemma simpnum_numbound0[simp]: 
chaieb@23264
   884
  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
chaieb@23264
   885
by (induct t rule: simpnum.induct, auto)
chaieb@23264
   886
krauss@41839
   887
fun nozerocoeff:: "num \<Rightarrow> bool" where
chaieb@23264
   888
  "nozerocoeff (C c) = True"
krauss@41839
   889
| "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
krauss@41839
   890
| "nozerocoeff (CF c s t) = (c \<noteq> 0 \<and> nozerocoeff t)"
krauss@41839
   891
| "nozerocoeff (Mul c t) = (c\<noteq>0 \<and> nozerocoeff t)"
krauss@41839
   892
| "nozerocoeff t = True"
chaieb@23264
   893
chaieb@23264
   894
lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
chaieb@23264
   895
by (induct a b rule: numadd.induct,auto simp add: Let_def)
chaieb@23264
   896
chaieb@23264
   897
lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
chaieb@23264
   898
  by (induct a rule: nummul.induct,auto simp add: Let_def numadd_nz)
chaieb@23264
   899
chaieb@23264
   900
lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
chaieb@23264
   901
by (simp add: numneg_def nummul_nz)
chaieb@23264
   902
chaieb@23264
   903
lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
chaieb@23264
   904
by (simp add: numsub_def numneg_nz numadd_nz)
chaieb@23264
   905
chaieb@23264
   906
lemma split_int_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (fst (split_int t)) \<and> nozerocoeff (snd (split_int t))"
chaieb@23264
   907
by (induct t rule: split_int.induct,auto simp add: Let_def split_def)
chaieb@23264
   908
chaieb@23264
   909
lemma numfloor_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (numfloor t)"
chaieb@23264
   910
by (simp add: numfloor_def Let_def split_def)
chaieb@23264
   911
(cases "fst (split_int t)", simp_all add: split_int_nz numadd_nz)
chaieb@23264
   912
chaieb@23264
   913
lemma simpnum_nz: "nozerocoeff (simpnum t)"
chaieb@23264
   914
by(induct t rule: simpnum.induct, auto simp add: numadd_nz numneg_nz numsub_nz nummul_nz numfloor_nz)
chaieb@23264
   915
chaieb@23264
   916
lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
chaieb@23264
   917
proof (induct t rule: maxcoeff.induct)
chaieb@23264
   918
  case (2 n c t)
chaieb@23264
   919
  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
chaieb@23264
   920
  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
chaieb@23264
   921
  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
wenzelm@41807
   922
  with 2 show ?case by simp
chaieb@23264
   923
next
chaieb@23264
   924
  case (3 c s t) 
chaieb@23264
   925
  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
chaieb@23264
   926
  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
chaieb@23264
   927
  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
wenzelm@41807
   928
  with 3 show ?case by simp
chaieb@23264
   929
qed auto
chaieb@23264
   930
chaieb@23264
   931
lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
chaieb@23264
   932
proof-
chaieb@23264
   933
  from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
chaieb@23264
   934
  from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
chaieb@23264
   935
  from maxcoeff_nz[OF nz th] show ?thesis .
chaieb@23264
   936
qed
chaieb@23264
   937
haftmann@35416
   938
definition simp_num_pair :: "(num \<times> int) \<Rightarrow> num \<times> int" where
chaieb@23264
   939
  "simp_num_pair \<equiv> (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
chaieb@23264
   940
   (let t' = simpnum t ; g = numgcd t' in 
huffman@31706
   941
      if g > 1 then (let g' = gcd n g in 
chaieb@23264
   942
        if g' = 1 then (t',n) 
chaieb@23264
   943
        else (reducecoeffh t' g', n div g')) 
chaieb@23264
   944
      else (t',n))))"
chaieb@23264
   945
chaieb@23264
   946
lemma simp_num_pair_ci:
chaieb@23264
   947
  shows "((\<lambda> (t,n). Inum bs t / real n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real n) (t,n))"
chaieb@23264
   948
  (is "?lhs = ?rhs")
chaieb@23264
   949
proof-
chaieb@23264
   950
  let ?t' = "simpnum t"
chaieb@23264
   951
  let ?g = "numgcd ?t'"
huffman@31706
   952
  let ?g' = "gcd n ?g"
chaieb@23264
   953
  {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
chaieb@23264
   954
  moreover
chaieb@23264
   955
  { assume nnz: "n \<noteq> 0"
chaieb@23264
   956
    {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
chaieb@23264
   957
    moreover
chaieb@23264
   958
    {assume g1:"?g>1" hence g0: "?g > 0" by simp
huffman@31706
   959
      from g1 nnz have gp0: "?g' \<noteq> 0" by simp
nipkow@31952
   960
      hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith
chaieb@23264
   961
      hence "?g'= 1 \<or> ?g' > 1" by arith
chaieb@23264
   962
      moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
chaieb@23264
   963
      moreover {assume g'1:"?g'>1"
wenzelm@32960
   964
        from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
wenzelm@32960
   965
        let ?tt = "reducecoeffh ?t' ?g'"
wenzelm@32960
   966
        let ?t = "Inum bs ?tt"
wenzelm@32960
   967
        have gpdg: "?g' dvd ?g" by simp
wenzelm@32960
   968
        have gpdd: "?g' dvd n" by simp
wenzelm@32960
   969
        have gpdgp: "?g' dvd ?g'" by simp
wenzelm@32960
   970
        from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
wenzelm@32960
   971
        have th2:"real ?g' * ?t = Inum bs ?t'" by simp
wenzelm@41807
   972
        from nnz g1 g'1 have "?lhs = ?t / real (n div ?g')" by (simp add: simp_num_pair_def Let_def)
wenzelm@32960
   973
        also have "\<dots> = (real ?g' * ?t) / (real ?g' * (real (n div ?g')))" by simp
wenzelm@32960
   974
        also have "\<dots> = (Inum bs ?t' / real n)"
wenzelm@32960
   975
          using real_of_int_div[OF gp0 gpdd] th2 gp0 by simp
wenzelm@32960
   976
        finally have "?lhs = Inum bs t / real n" by simp
wenzelm@41807
   977
        then have ?thesis using nnz g1 g'1 by (simp add: simp_num_pair_def) }
wenzelm@41807
   978
      ultimately have ?thesis by blast }
wenzelm@41807
   979
    ultimately have ?thesis by blast }
chaieb@23264
   980
  ultimately show ?thesis by blast
chaieb@23264
   981
qed
chaieb@23264
   982
wenzelm@41807
   983
lemma simp_num_pair_l:
wenzelm@41807
   984
  assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
chaieb@23264
   985
  shows "numbound0 t' \<and> n' >0"
chaieb@23264
   986
proof-
wenzelm@41807
   987
  let ?t' = "simpnum t"
chaieb@23264
   988
  let ?g = "numgcd ?t'"
huffman@31706
   989
  let ?g' = "gcd n ?g"
wenzelm@41807
   990
  { assume nz: "n = 0" hence ?thesis using assms by (simp add: Let_def simp_num_pair_def) }
chaieb@23264
   991
  moreover
chaieb@23264
   992
  { assume nnz: "n \<noteq> 0"
wenzelm@41807
   993
    {assume "\<not> ?g > 1" hence ?thesis using assms by (auto simp add: Let_def simp_num_pair_def) }
chaieb@23264
   994
    moreover
chaieb@23264
   995
    {assume g1:"?g>1" hence g0: "?g > 0" by simp
huffman@31706
   996
      from g1 nnz have gp0: "?g' \<noteq> 0" by simp
nipkow@31952
   997
      hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith
chaieb@23264
   998
      hence "?g'= 1 \<or> ?g' > 1" by arith
wenzelm@41807
   999
      moreover {assume "?g'=1" hence ?thesis using assms g1 g0
wenzelm@41807
  1000
          by (auto simp add: Let_def simp_num_pair_def) }
chaieb@23264
  1001
      moreover {assume g'1:"?g'>1"
wenzelm@32960
  1002
        have gpdg: "?g' dvd ?g" by simp
wenzelm@32960
  1003
        have gpdd: "?g' dvd n" by simp
wenzelm@32960
  1004
        have gpdgp: "?g' dvd ?g'" by simp
wenzelm@32960
  1005
        from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
wenzelm@32960
  1006
        from zdiv_mono1[OF g'n g'p, simplified zdiv_self[OF gp0]]
wenzelm@32960
  1007
        have "n div ?g' >0" by simp
wenzelm@41807
  1008
        hence ?thesis using assms g1 g'1
wenzelm@32960
  1009
          by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0)}
wenzelm@41807
  1010
      ultimately have ?thesis by blast }
wenzelm@41807
  1011
    ultimately have ?thesis by blast } 
chaieb@23264
  1012
  ultimately show ?thesis by blast
chaieb@23264
  1013
qed
chaieb@23264
  1014
krauss@41839
  1015
fun not:: "fm \<Rightarrow> fm" where
chaieb@23264
  1016
  "not (NOT p) = p"
krauss@41839
  1017
| "not T = F"
krauss@41839
  1018
| "not F = T"
krauss@41839
  1019
| "not (Lt t) = Ge t"
krauss@41839
  1020
| "not (Le t) = Gt t"
krauss@41839
  1021
| "not (Gt t) = Le t"
krauss@41839
  1022
| "not (Ge t) = Lt t"
krauss@41839
  1023
| "not (Eq t) = NEq t"
krauss@41839
  1024
| "not (NEq t) = Eq t"
krauss@41839
  1025
| "not (Dvd i t) = NDvd i t"
krauss@41839
  1026
| "not (NDvd i t) = Dvd i t"
krauss@41839
  1027
| "not (And p q) = Or (not p) (not q)"
krauss@41839
  1028
| "not (Or p q) = And (not p) (not q)"
krauss@41839
  1029
| "not p = NOT p"
chaieb@23264
  1030
lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
wenzelm@41807
  1031
  by (induct p) auto
chaieb@23264
  1032
lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
wenzelm@41807
  1033
  by (induct p) auto
chaieb@23264
  1034
lemma not_nb[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
wenzelm@41807
  1035
  by (induct p) auto
chaieb@23264
  1036
haftmann@35416
  1037
definition conj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@23264
  1038
  "conj p q \<equiv> (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else 
chaieb@23264
  1039
   if p = q then p else And p q)"
chaieb@23264
  1040
lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
wenzelm@41807
  1041
  by (cases "p=F \<or> q=F", simp_all add: conj_def) (cases p, simp_all)
chaieb@23264
  1042
chaieb@23264
  1043
lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
wenzelm@41807
  1044
  using conj_def by auto 
chaieb@23264
  1045
lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
wenzelm@41807
  1046
  using conj_def by auto 
chaieb@23264
  1047
haftmann@35416
  1048
definition disj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@23264
  1049
  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
chaieb@23264
  1050
       else if p=q then p else Or p q)"
chaieb@23264
  1051
chaieb@23264
  1052
lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
wenzelm@41807
  1053
  by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
chaieb@23264
  1054
lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
wenzelm@41807
  1055
  using disj_def by auto 
chaieb@23264
  1056
lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
wenzelm@41807
  1057
  using disj_def by auto 
chaieb@23264
  1058
haftmann@35416
  1059
definition imp :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@23264
  1060
  "imp p q \<equiv> (if (p = F \<or> q=T \<or> p=q) then T else if p=T then q else if q=F then not p 
chaieb@23264
  1061
    else Imp p q)"
chaieb@23264
  1062
lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
wenzelm@41807
  1063
  by (cases "p=F \<or> q=T",simp_all add: imp_def)
chaieb@23264
  1064
lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
wenzelm@41807
  1065
  using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
chaieb@23264
  1066
haftmann@35416
  1067
definition iff :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
chaieb@23264
  1068
  "iff p q \<equiv> (if (p = q) then T else if (p = not q \<or> not p = q) then F else 
chaieb@23264
  1069
       if p=F then not q else if q=F then not p else if p=T then q else if q=T then p else 
chaieb@23264
  1070
  Iff p q)"
chaieb@23264
  1071
lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
chaieb@23264
  1072
  by (unfold iff_def,cases "p=q", simp,cases "p=not q", simp add:not) 
chaieb@23264
  1073
(cases "not p= q", auto simp add:not)
chaieb@23264
  1074
lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
chaieb@23264
  1075
  by (unfold iff_def,cases "p=q", auto)
chaieb@23264
  1076
krauss@41839
  1077
fun check_int:: "num \<Rightarrow> bool" where
chaieb@23264
  1078
  "check_int (C i) = True"
krauss@41839
  1079
| "check_int (Floor t) = True"
krauss@41839
  1080
| "check_int (Mul i t) = check_int t"
krauss@41839
  1081
| "check_int (Add t s) = (check_int t \<and> check_int s)"
krauss@41839
  1082
| "check_int (Neg t) = check_int t"
krauss@41839
  1083
| "check_int (CF c t s) = check_int s"
krauss@41839
  1084
| "check_int t = False"
chaieb@23264
  1085
lemma check_int: "check_int t \<Longrightarrow> isint t bs"
chaieb@23264
  1086
by (induct t, auto simp add: isint_add isint_Floor isint_Mul isint_neg isint_c isint_CF)
chaieb@23264
  1087
chaieb@23264
  1088
lemma rdvd_left1_int: "real \<lfloor>t\<rfloor> = t \<Longrightarrow> 1 rdvd t"
chaieb@23264
  1089
  by (simp add: rdvd_def,rule_tac x="\<lfloor>t\<rfloor>" in exI) simp
chaieb@23264
  1090
chaieb@23264
  1091
lemma rdvd_reduce: 
chaieb@23264
  1092
  assumes gd:"g dvd d" and gc:"g dvd c" and gp: "g > 0"
chaieb@23264
  1093
  shows "real (d::int) rdvd real (c::int)*t = (real (d div g) rdvd real (c div g)*t)"
chaieb@23264
  1094
proof
chaieb@23264
  1095
  assume d: "real d rdvd real c * t"
chaieb@23264
  1096
  from d rdvd_def obtain k where k_def: "real c * t = real d* real (k::int)" by auto
chaieb@23264
  1097
  from gd dvd_def obtain kd where kd_def: "d = g * kd" by auto
chaieb@23264
  1098
  from gc dvd_def obtain kc where kc_def: "c = g * kc" by auto
chaieb@23264
  1099
  from k_def kd_def kc_def have "real g * real kc * t = real g * real kd * real k" by simp
chaieb@23264
  1100
  hence "real kc * t = real kd * real k" using gp by simp
chaieb@23264
  1101
  hence th:"real kd rdvd real kc * t" using rdvd_def by blast
chaieb@23264
  1102
  from kd_def gp have th':"kd = d div g" by simp
chaieb@23264
  1103
  from kc_def gp have "kc = c div g" by simp
chaieb@23264
  1104
  with th th' show "real (d div g) rdvd real (c div g) * t" by simp
chaieb@23264
  1105
next
chaieb@23264
  1106
  assume d: "real (d div g) rdvd real (c div g) * t"
chaieb@23264
  1107
  from gp have gnz: "g \<noteq> 0" by simp
chaieb@23264
  1108
  thus "real d rdvd real c * t" using d rdvd_mult[OF gnz, where n="d div g" and x="real (c div g) * t"] real_of_int_div[OF gnz gd] real_of_int_div[OF gnz gc] by simp
chaieb@23264
  1109
qed
chaieb@23264
  1110
haftmann@35416
  1111
definition simpdvd :: "int \<Rightarrow> num \<Rightarrow> (int \<times> num)" where
chaieb@23264
  1112
  "simpdvd d t \<equiv> 
chaieb@23264
  1113
   (let g = numgcd t in 
huffman@31706
  1114
      if g > 1 then (let g' = gcd d g in 
chaieb@23264
  1115
        if g' = 1 then (d, t) 
chaieb@23264
  1116
        else (d div g',reducecoeffh t g')) 
chaieb@23264
  1117
      else (d, t))"
chaieb@23264
  1118
lemma simpdvd: 
chaieb@23264
  1119
  assumes tnz: "nozerocoeff t" and dnz: "d \<noteq> 0"
chaieb@23264
  1120
  shows "Ifm bs (Dvd (fst (simpdvd d t)) (snd (simpdvd d t))) = Ifm bs (Dvd d t)"
chaieb@23264
  1121
proof-
chaieb@23264
  1122
  let ?g = "numgcd t"
huffman@31706
  1123
  let ?g' = "gcd d ?g"
chaieb@23264
  1124
  {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simpdvd_def)}
chaieb@23264
  1125
  moreover
chaieb@23264
  1126
  {assume g1:"?g>1" hence g0: "?g > 0" by simp
huffman@31706
  1127
    from g1 dnz have gp0: "?g' \<noteq> 0" by simp
nipkow@31952
  1128
    hence g'p: "?g' > 0" using gcd_ge_0_int[where x="d" and y="numgcd t"] by arith
chaieb@23264
  1129
    hence "?g'= 1 \<or> ?g' > 1" by arith
chaieb@23264
  1130
    moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simpdvd_def)}
chaieb@23264
  1131
    moreover {assume g'1:"?g'>1"
chaieb@23264
  1132
      from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" ..
chaieb@23264
  1133
      let ?tt = "reducecoeffh t ?g'"
chaieb@23264
  1134
      let ?t = "Inum bs ?tt"
huffman@31706
  1135
      have gpdg: "?g' dvd ?g" by simp
huffman@31706
  1136
      have gpdd: "?g' dvd d" by simp
chaieb@23264
  1137
      have gpdgp: "?g' dvd ?g'" by simp
chaieb@23264
  1138
      from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
chaieb@23264
  1139
      have th2:"real ?g' * ?t = Inum bs t" by simp
wenzelm@41807
  1140
      from assms g1 g0 g'1
wenzelm@41807
  1141
      have "Ifm bs (Dvd (fst (simpdvd d t)) (snd(simpdvd d t))) = Ifm bs (Dvd (d div ?g') ?tt)"
wenzelm@32960
  1142
        by (simp add: simpdvd_def Let_def)
chaieb@23264
  1143
      also have "\<dots> = (real d rdvd (Inum bs t))"
wenzelm@32960
  1144
        using rdvd_reduce[OF gpdd gpdgp g'p, where t="?t", simplified zdiv_self[OF gp0]] 
wenzelm@32960
  1145
          th2[symmetric] by simp
chaieb@23264
  1146
      finally have ?thesis by simp  }
chaieb@23264
  1147
    ultimately have ?thesis by blast
chaieb@23264
  1148
  }
chaieb@23264
  1149
  ultimately show ?thesis by blast
chaieb@23264
  1150
qed
chaieb@23264
  1151
krauss@41839
  1152
function (sequential) simpfm :: "fm \<Rightarrow> fm" where
chaieb@23264
  1153
  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
krauss@41839
  1154
| "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
krauss@41839
  1155
| "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
krauss@41839
  1156
| "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
krauss@41839
  1157
| "simpfm (NOT p) = not (simpfm p)"
krauss@41839
  1158
| "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
chaieb@23264
  1159
  | _ \<Rightarrow> Lt (reducecoeff a'))"
krauss@41839
  1160
| "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le (reducecoeff a'))"
krauss@41839
  1161
| "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt (reducecoeff a'))"
krauss@41839
  1162
| "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge (reducecoeff a'))"
krauss@41839
  1163
| "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq (reducecoeff a'))"
krauss@41839
  1164
| "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq (reducecoeff a'))"
krauss@41839
  1165
| "simpfm (Dvd i a) = (if i=0 then simpfm (Eq a)
chaieb@23264
  1166
             else if (abs i = 1) \<and> check_int a then T
chaieb@23264
  1167
             else let a' = simpnum a in case a' of C v \<Rightarrow> if (i dvd v)  then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in Dvd d t))"
krauss@41839
  1168
| "simpfm (NDvd i a) = (if i=0 then simpfm (NEq a) 
chaieb@23264
  1169
             else if (abs i = 1) \<and> check_int a then F
chaieb@23264
  1170
             else let a' = simpnum a in case a' of C v \<Rightarrow> if (\<not>(i dvd v)) then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in NDvd d t))"
krauss@41839
  1171
| "simpfm p = p"
krauss@41839
  1172
by pat_completeness auto
krauss@41839
  1173
termination by (relation "measure fmsize") auto
chaieb@23264
  1174
chaieb@23264
  1175
lemma simpfm[simp]: "Ifm bs (simpfm p) = Ifm bs p"
chaieb@23264
  1176
proof(induct p rule: simpfm.induct)
chaieb@23264
  1177
  case (6 a) let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1178
  {fix v assume "?sa = C v" hence ?case using sa by simp }
chaieb@23264
  1179
  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
chaieb@23264
  1180
    let ?g = "numgcd ?sa"
chaieb@23264
  1181
    let ?rsa = "reducecoeff ?sa"
chaieb@23264
  1182
    let ?r = "Inum bs ?rsa"
chaieb@23264
  1183
    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
chaieb@23264
  1184
    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
chaieb@23264
  1185
    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
chaieb@23264
  1186
    hence gp: "real ?g > 0" by simp
chaieb@23264
  1187
    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
chaieb@23264
  1188
    with sa have "Inum bs a < 0 = (real ?g * ?r < real ?g * 0)" by simp
chaieb@23264
  1189
    also have "\<dots> = (?r < 0)" using gp
chaieb@23264
  1190
      by (simp only: mult_less_cancel_left) simp
chaieb@23264
  1191
    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
chaieb@23264
  1192
  ultimately show ?case by blast
chaieb@23264
  1193
next
chaieb@23264
  1194
  case (7 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1195
  {fix v assume "?sa = C v" hence ?case using sa by simp }
chaieb@23264
  1196
  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
chaieb@23264
  1197
    let ?g = "numgcd ?sa"
chaieb@23264
  1198
    let ?rsa = "reducecoeff ?sa"
chaieb@23264
  1199
    let ?r = "Inum bs ?rsa"
chaieb@23264
  1200
    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
chaieb@23264
  1201
    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
chaieb@23264
  1202
    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
chaieb@23264
  1203
    hence gp: "real ?g > 0" by simp
chaieb@23264
  1204
    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
chaieb@23264
  1205
    with sa have "Inum bs a \<le> 0 = (real ?g * ?r \<le> real ?g * 0)" by simp
chaieb@23264
  1206
    also have "\<dots> = (?r \<le> 0)" using gp
chaieb@23264
  1207
      by (simp only: mult_le_cancel_left) simp
chaieb@23264
  1208
    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
chaieb@23264
  1209
  ultimately show ?case by blast
chaieb@23264
  1210
next
chaieb@23264
  1211
  case (8 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1212
  {fix v assume "?sa = C v" hence ?case using sa by simp }
chaieb@23264
  1213
  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
chaieb@23264
  1214
    let ?g = "numgcd ?sa"
chaieb@23264
  1215
    let ?rsa = "reducecoeff ?sa"
chaieb@23264
  1216
    let ?r = "Inum bs ?rsa"
chaieb@23264
  1217
    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
chaieb@23264
  1218
    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
chaieb@23264
  1219
    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
chaieb@23264
  1220
    hence gp: "real ?g > 0" by simp
chaieb@23264
  1221
    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
chaieb@23264
  1222
    with sa have "Inum bs a > 0 = (real ?g * ?r > real ?g * 0)" by simp
chaieb@23264
  1223
    also have "\<dots> = (?r > 0)" using gp
chaieb@23264
  1224
      by (simp only: mult_less_cancel_left) simp
chaieb@23264
  1225
    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
chaieb@23264
  1226
  ultimately show ?case by blast
chaieb@23264
  1227
next
chaieb@23264
  1228
  case (9 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1229
  {fix v assume "?sa = C v" hence ?case using sa by simp }
chaieb@23264
  1230
  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
chaieb@23264
  1231
    let ?g = "numgcd ?sa"
chaieb@23264
  1232
    let ?rsa = "reducecoeff ?sa"
chaieb@23264
  1233
    let ?r = "Inum bs ?rsa"
chaieb@23264
  1234
    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
chaieb@23264
  1235
    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
chaieb@23264
  1236
    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
chaieb@23264
  1237
    hence gp: "real ?g > 0" by simp
chaieb@23264
  1238
    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
chaieb@23264
  1239
    with sa have "Inum bs a \<ge> 0 = (real ?g * ?r \<ge> real ?g * 0)" by simp
chaieb@23264
  1240
    also have "\<dots> = (?r \<ge> 0)" using gp
chaieb@23264
  1241
      by (simp only: mult_le_cancel_left) simp
chaieb@23264
  1242
    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
chaieb@23264
  1243
  ultimately show ?case by blast
chaieb@23264
  1244
next
chaieb@23264
  1245
  case (10 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1246
  {fix v assume "?sa = C v" hence ?case using sa by simp }
chaieb@23264
  1247
  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
chaieb@23264
  1248
    let ?g = "numgcd ?sa"
chaieb@23264
  1249
    let ?rsa = "reducecoeff ?sa"
chaieb@23264
  1250
    let ?r = "Inum bs ?rsa"
chaieb@23264
  1251
    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
chaieb@23264
  1252
    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
chaieb@23264
  1253
    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
chaieb@23264
  1254
    hence gp: "real ?g > 0" by simp
chaieb@23264
  1255
    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
chaieb@23264
  1256
    with sa have "Inum bs a = 0 = (real ?g * ?r = 0)" by simp
chaieb@23264
  1257
    also have "\<dots> = (?r = 0)" using gp
chaieb@23264
  1258
      by (simp add: mult_eq_0_iff)
chaieb@23264
  1259
    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
chaieb@23264
  1260
  ultimately show ?case by blast
chaieb@23264
  1261
next
chaieb@23264
  1262
  case (11 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1263
  {fix v assume "?sa = C v" hence ?case using sa by simp }
chaieb@23264
  1264
  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
chaieb@23264
  1265
    let ?g = "numgcd ?sa"
chaieb@23264
  1266
    let ?rsa = "reducecoeff ?sa"
chaieb@23264
  1267
    let ?r = "Inum bs ?rsa"
chaieb@23264
  1268
    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
chaieb@23264
  1269
    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
chaieb@23264
  1270
    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
chaieb@23264
  1271
    hence gp: "real ?g > 0" by simp
chaieb@23264
  1272
    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
chaieb@23264
  1273
    with sa have "Inum bs a \<noteq> 0 = (real ?g * ?r \<noteq> 0)" by simp
chaieb@23264
  1274
    also have "\<dots> = (?r \<noteq> 0)" using gp
chaieb@23264
  1275
      by (simp add: mult_eq_0_iff)
chaieb@23264
  1276
    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
chaieb@23264
  1277
  ultimately show ?case by blast
chaieb@23264
  1278
next
chaieb@23264
  1279
  case (12 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1280
  have "i=0 \<or> (abs i = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((abs i \<noteq> 1) \<or> (\<not> check_int a)))" by auto
chaieb@23264
  1281
  {assume "i=0" hence ?case using "12.hyps" by (simp add: rdvd_left_0_eq Let_def)}
chaieb@23264
  1282
  moreover 
chaieb@23264
  1283
  {assume ai1: "abs i = 1" and ai: "check_int a" 
chaieb@23264
  1284
    hence "i=1 \<or> i= - 1" by arith
chaieb@23264
  1285
    moreover {assume i1: "i = 1" 
chaieb@23264
  1286
      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
chaieb@23264
  1287
      have ?case using i1 ai by simp }
chaieb@23264
  1288
    moreover {assume i1: "i = - 1" 
chaieb@23264
  1289
      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
wenzelm@32960
  1290
        rdvd_abs1[where d="- 1" and t="Inum bs a"]
chaieb@23264
  1291
      have ?case using i1 ai by simp }
chaieb@23264
  1292
    ultimately have ?case by blast}
chaieb@23264
  1293
  moreover   
chaieb@23264
  1294
  {assume inz: "i\<noteq>0" and cond: "(abs i \<noteq> 1) \<or> (\<not> check_int a)"
chaieb@23264
  1295
    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
wenzelm@32960
  1296
        by (cases "abs i = 1", auto simp add: int_rdvd_iff) }
chaieb@23264
  1297
    moreover {assume H:"\<not> (\<exists> v. ?sa = C v)" 
chaieb@23264
  1298
      hence th: "simpfm (Dvd i a) = Dvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond by (cases ?sa, auto simp add: Let_def split_def)
chaieb@23264
  1299
      from simpnum_nz have nz:"nozerocoeff ?sa" by simp
chaieb@23264
  1300
      from simpdvd [OF nz inz] th have ?case using sa by simp}
chaieb@23264
  1301
    ultimately have ?case by blast}
chaieb@23264
  1302
  ultimately show ?case by blast
chaieb@23264
  1303
next
chaieb@23264
  1304
  case (13 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
chaieb@23264
  1305
  have "i=0 \<or> (abs i = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((abs i \<noteq> 1) \<or> (\<not> check_int a)))" by auto
chaieb@23264
  1306
  {assume "i=0" hence ?case using "13.hyps" by (simp add: rdvd_left_0_eq Let_def)}
chaieb@23264
  1307
  moreover 
chaieb@23264
  1308
  {assume ai1: "abs i = 1" and ai: "check_int a" 
chaieb@23264
  1309
    hence "i=1 \<or> i= - 1" by arith
chaieb@23264
  1310
    moreover {assume i1: "i = 1" 
chaieb@23264
  1311
      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
chaieb@23264
  1312
      have ?case using i1 ai by simp }
chaieb@23264
  1313
    moreover {assume i1: "i = - 1" 
chaieb@23264
  1314
      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
wenzelm@32960
  1315
        rdvd_abs1[where d="- 1" and t="Inum bs a"]
chaieb@23264
  1316
      have ?case using i1 ai by simp }
chaieb@23264
  1317
    ultimately have ?case by blast}
chaieb@23264
  1318
  moreover   
chaieb@23264
  1319
  {assume inz: "i\<noteq>0" and cond: "(abs i \<noteq> 1) \<or> (\<not> check_int a)"
chaieb@23264
  1320
    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
wenzelm@32960
  1321
        by (cases "abs i = 1", auto simp add: int_rdvd_iff) }
chaieb@23264
  1322
    moreover {assume H:"\<not> (\<exists> v. ?sa = C v)" 
chaieb@23264
  1323
      hence th: "simpfm (NDvd i a) = NDvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond 
wenzelm@32960
  1324
        by (cases ?sa, auto simp add: Let_def split_def)
chaieb@23264
  1325
      from simpnum_nz have nz:"nozerocoeff ?sa" by simp
chaieb@23264
  1326
      from simpdvd [OF nz inz] th have ?case using sa by simp}
chaieb@23264
  1327
    ultimately have ?case by blast}
chaieb@23264
  1328
  ultimately show ?case by blast
chaieb@23264
  1329
qed (induct p rule: simpfm.induct, simp_all)
chaieb@23264
  1330
chaieb@23264
  1331
lemma simpdvd_numbound0: "numbound0 t \<Longrightarrow> numbound0 (snd (simpdvd d t))"
chaieb@23264
  1332
  by (simp add: simpdvd_def Let_def split_def reducecoeffh_numbound0)
chaieb@23264
  1333
chaieb@23264
  1334
lemma simpfm_bound0[simp]: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
chaieb@23264
  1335
proof(induct p rule: simpfm.induct)
chaieb@23264
  1336
  case (6 a) hence nb: "numbound0 a" by simp
chaieb@23264
  1337
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1338
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
chaieb@23264
  1339
next
chaieb@23264
  1340
  case (7 a) hence nb: "numbound0 a" by simp
chaieb@23264
  1341
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1342
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
chaieb@23264
  1343
next
chaieb@23264
  1344
  case (8 a) hence nb: "numbound0 a" by simp
chaieb@23264
  1345
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1346
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
chaieb@23264
  1347
next
chaieb@23264
  1348
  case (9 a) hence nb: "numbound0 a" by simp
chaieb@23264
  1349
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1350
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
chaieb@23264
  1351
next
chaieb@23264
  1352
  case (10 a) hence nb: "numbound0 a" by simp
chaieb@23264
  1353
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1354
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
chaieb@23264
  1355
next
chaieb@23264
  1356
  case (11 a) hence nb: "numbound0 a" by simp
chaieb@23264
  1357
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1358
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
chaieb@23264
  1359
next
chaieb@23264
  1360
  case (12 i a) hence nb: "numbound0 a" by simp
chaieb@23264
  1361
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1362
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
chaieb@23264
  1363
next
chaieb@23264
  1364
  case (13 i a) hence nb: "numbound0 a" by simp
chaieb@23264
  1365
  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
chaieb@23264
  1366
  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
chaieb@23264
  1367
qed(auto simp add: disj_def imp_def iff_def conj_def)
chaieb@23264
  1368
chaieb@23264
  1369
lemma simpfm_qf[simp]: "qfree p \<Longrightarrow> qfree (simpfm p)"
chaieb@23264
  1370
by (induct p rule: simpfm.induct, auto simp add: Let_def)
chaieb@23264
  1371
(case_tac "simpnum a",auto simp add: split_def Let_def)+
chaieb@23264
  1372
chaieb@23264
  1373
chaieb@23264
  1374
  (* Generic quantifier elimination *)
chaieb@23264
  1375
haftmann@35416
  1376
definition list_conj :: "fm list \<Rightarrow> fm" where
chaieb@23264
  1377
  "list_conj ps \<equiv> foldr conj ps T"
chaieb@23264
  1378
lemma list_conj: "Ifm bs (list_conj ps) = (\<forall>p\<in> set ps. Ifm bs p)"
chaieb@23264
  1379
  by (induct ps, auto simp add: list_conj_def)
chaieb@23264
  1380
lemma list_conj_qf: " \<forall>p\<in> set ps. qfree p \<Longrightarrow> qfree (list_conj ps)"
chaieb@23264
  1381
  by (induct ps, auto simp add: list_conj_def)
chaieb@23264
  1382
lemma list_conj_nb: " \<forall>p\<in> set ps. bound0 p \<Longrightarrow> bound0 (list_conj ps)"
chaieb@23264
  1383
  by (induct ps, auto simp add: list_conj_def)
haftmann@35416
  1384
definition CJNB :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
haftmann@29788
  1385
  "CJNB f p \<equiv> (let cjs = conjuncts p ; (yes,no) = List.partition bound0 cjs
chaieb@23264
  1386
                   in conj (decr (list_conj yes)) (f (list_conj no)))"
chaieb@23264
  1387
chaieb@23264
  1388
lemma CJNB_qe: 
chaieb@23264
  1389
  assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
chaieb@23264
  1390
  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (CJNB qe p) \<and> (Ifm bs ((CJNB qe p)) = Ifm bs (E p))"
chaieb@23264
  1391
proof(clarify)
chaieb@23264
  1392
  fix bs p
chaieb@23264
  1393
  assume qfp: "qfree p"
chaieb@23264
  1394
  let ?cjs = "conjuncts p"
haftmann@29788
  1395
  let ?yes = "fst (List.partition bound0 ?cjs)"
haftmann@29788
  1396
  let ?no = "snd (List.partition bound0 ?cjs)"
chaieb@23264
  1397
  let ?cno = "list_conj ?no"
chaieb@23264
  1398
  let ?cyes = "list_conj ?yes"
haftmann@29788
  1399
  have part: "List.partition bound0 ?cjs = (?yes,?no)" by simp
chaieb@23264
  1400
  from partition_P[OF part] have "\<forall> q\<in> set ?yes. bound0 q" by blast 
chaieb@23264
  1401
  hence yes_nb: "bound0 ?cyes" by (simp add: list_conj_nb) 
chaieb@23264
  1402
  hence yes_qf: "qfree (decr ?cyes )" by (simp add: decr_qf)
chaieb@23264
  1403
  from conjuncts_qf[OF qfp] partition_set[OF part] 
chaieb@23264
  1404
  have " \<forall>q\<in> set ?no. qfree q" by auto
chaieb@23264
  1405
  hence no_qf: "qfree ?cno"by (simp add: list_conj_qf)
chaieb@23264
  1406
  with qe have cno_qf:"qfree (qe ?cno )" 
chaieb@23264
  1407
    and noE: "Ifm bs (qe ?cno) = Ifm bs (E ?cno)" by blast+
chaieb@23264
  1408
  from cno_qf yes_qf have qf: "qfree (CJNB qe p)" 
chaieb@23264
  1409
    by (simp add: CJNB_def Let_def conj_qf split_def)
chaieb@23264
  1410
  {fix bs
chaieb@23264
  1411
    from conjuncts have "Ifm bs p = (\<forall>q\<in> set ?cjs. Ifm bs q)" by blast
chaieb@23264
  1412
    also have "\<dots> = ((\<forall>q\<in> set ?yes. Ifm bs q) \<and> (\<forall>q\<in> set ?no. Ifm bs q))"
chaieb@23264
  1413
      using partition_set[OF part] by auto
chaieb@23264
  1414
    finally have "Ifm bs p = ((Ifm bs ?cyes) \<and> (Ifm bs ?cno))" using list_conj by simp}
chaieb@23264
  1415
  hence "Ifm bs (E p) = (\<exists>x. (Ifm (x#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))" by simp
wenzelm@26932
  1416
  also fix y have "\<dots> = (\<exists>x. (Ifm (y#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))"
chaieb@23264
  1417
    using bound0_I[OF yes_nb, where bs="bs" and b'="y"] by blast
chaieb@23264
  1418
  also have "\<dots> = (Ifm bs (decr ?cyes) \<and> Ifm bs (E ?cno))"
hoelzl@33639
  1419
    by (auto simp add: decr[OF yes_nb] simp del: partition_filter_conv)
chaieb@23264
  1420
  also have "\<dots> = (Ifm bs (conj (decr ?cyes) (qe ?cno)))"
chaieb@23264
  1421
    using qe[rule_format, OF no_qf] by auto
chaieb@23264
  1422
  finally have "Ifm bs (E p) = Ifm bs (CJNB qe p)" 
chaieb@23264
  1423
    by (simp add: Let_def CJNB_def split_def)
chaieb@23264
  1424
  with qf show "qfree (CJNB qe p) \<and> Ifm bs (CJNB qe p) = Ifm bs (E p)" by blast
chaieb@23264
  1425
qed
chaieb@23264
  1426
krauss@41839
  1427
function (sequential) qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm" where
chaieb@23264
  1428
  "qelim (E p) = (\<lambda> qe. DJ (CJNB qe) (qelim p qe))"
krauss@41839
  1429
| "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
krauss@41839
  1430
| "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
krauss@41839
  1431
| "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
krauss@41839
  1432
| "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
krauss@41839
  1433
| "qelim (Imp p q) = (\<lambda> qe. disj (qelim (NOT p) qe) (qelim q qe))"
krauss@41839
  1434
| "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
krauss@41839
  1435
| "qelim p = (\<lambda> y. simpfm p)"
krauss@41839
  1436
by pat_completeness auto
krauss@41839
  1437
termination by (relation "measure fmsize") auto
chaieb@23264
  1438
chaieb@23264
  1439
lemma qelim_ci:
chaieb@23264
  1440
  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
chaieb@23264
  1441
  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
wenzelm@41807
  1442
  using qe_inv DJ_qe[OF CJNB_qe[OF qe_inv]] 
wenzelm@41807
  1443
  by (induct p rule: qelim.induct) (auto simp del: simpfm.simps)
chaieb@23264
  1444
chaieb@23264
  1445
chaieb@23316
  1446
text {* The @{text "\<int>"} Part *}
chaieb@23316
  1447
text{* Linearity for fm where Bound 0 ranges over @{text "\<int>"} *}
krauss@41839
  1448
krauss@41839
  1449
function zsplit0 :: "num \<Rightarrow> int \<times> num" (* splits the bounded from the unbounded part*) where
chaieb@23264
  1450
  "zsplit0 (C c) = (0,C c)"
krauss@41839
  1451
| "zsplit0 (Bound n) = (if n=0 then (1, C 0) else (0,Bound n))"
krauss@41839
  1452
| "zsplit0 (CN n c a) = zsplit0 (Add (Mul c (Bound n)) a)"
krauss@41839
  1453
| "zsplit0 (CF c a b) = zsplit0 (Add (Mul c (Floor a)) b)"
krauss@41839
  1454
| "zsplit0 (Neg a) = (let (i',a') =  zsplit0 a in (-i', Neg a'))"
krauss@41839
  1455
| "zsplit0 (Add a b) = (let (ia,a') =  zsplit0 a ; 
chaieb@23264
  1456
                            (ib,b') =  zsplit0 b 
chaieb@23264
  1457
                            in (ia+ib, Add a' b'))"
krauss@41839
  1458
| "zsplit0 (Sub a b) = (let (ia,a') =  zsplit0 a ; 
chaieb@23264
  1459
                            (ib,b') =  zsplit0 b 
chaieb@23264
  1460
                            in (ia-ib, Sub a' b'))"
krauss@41839
  1461
| "zsplit0 (Mul i a) = (let (i',a') =  zsplit0 a in (i*i', Mul i a'))"
krauss@41839
  1462
| "zsplit0 (Floor a) = (let (i',a') =  zsplit0 a in (i',Floor a'))"
krauss@41839
  1463
by pat_completeness auto
krauss@41839
  1464
termination by (relation "measure num_size") auto
chaieb@23264
  1465
chaieb@23264
  1466
lemma zsplit0_I:
chaieb@23264
  1467
  shows "\<And> n a. zsplit0 t = (n,a) \<Longrightarrow> (Inum ((real (x::int)) #bs) (CN 0 n a) = Inum (real x #bs) t) \<and> numbound0 a"
chaieb@23264
  1468
  (is "\<And> n a. ?S t = (n,a) \<Longrightarrow> (?I x (CN 0 n a) = ?I x t) \<and> ?N a")
chaieb@23264
  1469
proof(induct t rule: zsplit0.induct)
chaieb@23264
  1470
  case (1 c n a) thus ?case by auto 
chaieb@23264
  1471
next
chaieb@23264
  1472
  case (2 m n a) thus ?case by (cases "m=0") auto
chaieb@23264
  1473
next
chaieb@23264
  1474
  case (3 n i a n a') thus ?case by auto
chaieb@23264
  1475
next 
chaieb@23264
  1476
  case (4 c a b n a') thus ?case by auto
chaieb@23264
  1477
next
chaieb@23264
  1478
  case (5 t n a)
chaieb@23264
  1479
  let ?nt = "fst (zsplit0 t)"
chaieb@23264
  1480
  let ?at = "snd (zsplit0 t)"
wenzelm@41807
  1481
  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Neg ?at \<and> n=-?nt" using 5 
chaieb@23264
  1482
    by (simp add: Let_def split_def)
wenzelm@41891
  1483
  from abj 5 have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
chaieb@23264
  1484
  from th2[simplified] th[simplified] show ?case by simp
chaieb@23264
  1485
next
chaieb@23264
  1486
  case (6 s t n a)
chaieb@23264
  1487
  let ?ns = "fst (zsplit0 s)"
chaieb@23264
  1488
  let ?as = "snd (zsplit0 s)"
chaieb@23264
  1489
  let ?nt = "fst (zsplit0 t)"
chaieb@23264
  1490
  let ?at = "snd (zsplit0 t)"
chaieb@23264
  1491
  have abjs: "zsplit0 s = (?ns,?as)" by simp 
chaieb@23264
  1492
  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
wenzelm@41891
  1493
  ultimately have th: "a=Add ?as ?at \<and> n=?ns + ?nt" using 6
chaieb@23264
  1494
    by (simp add: Let_def split_def)
chaieb@23264
  1495
  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
wenzelm@41891
  1496
  from 6 have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real x # bs) (CN 0 xa xb) = Inum (real x # bs) t \<and> numbound0 xb)" by blast (*FIXME*)
chaieb@23264
  1497
  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
wenzelm@41891
  1498
  from abjs 6  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
chaieb@23264
  1499
  from th3[simplified] th2[simplified] th[simplified] show ?case 
chaieb@23264
  1500
    by (simp add: left_distrib)
chaieb@23264
  1501
next
chaieb@23264
  1502
  case (7 s t n a)
chaieb@23264
  1503
  let ?ns = "fst (zsplit0 s)"
chaieb@23264
  1504
  let ?as = "snd (zsplit0 s)"
chaieb@23264
  1505
  let ?nt = "fst (zsplit0 t)"
chaieb@23264
  1506
  let ?at = "snd (zsplit0 t)"
chaieb@23264
  1507
  have abjs: "zsplit0 s = (?ns,?as)" by simp 
chaieb@23264
  1508
  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
wenzelm@41891
  1509
  ultimately have th: "a=Sub ?as ?at \<and> n=?ns - ?nt" using 7
chaieb@23264
  1510
    by (simp add: Let_def split_def)
chaieb@23264
  1511
  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
wenzelm@41891
  1512
  from 7 have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real x # bs) (CN 0 xa xb) = Inum (real x # bs) t \<and> numbound0 xb)" by blast (*FIXME*)
chaieb@23264
  1513
  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
wenzelm@41891
  1514
  from abjs 7 have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
chaieb@23264
  1515
  from th3[simplified] th2[simplified] th[simplified] show ?case 
chaieb@23264
  1516
    by (simp add: left_diff_distrib)
chaieb@23264
  1517
next
chaieb@23264
  1518
  case (8 i t n a)
chaieb@23264
  1519
  let ?nt = "fst (zsplit0 t)"
chaieb@23264
  1520
  let ?at = "snd (zsplit0 t)"
wenzelm@41891
  1521
  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Mul i ?at \<and> n=i*?nt" using 8
chaieb@23264
  1522
    by (simp add: Let_def split_def)
wenzelm@41891
  1523
  from abj 8 have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
wenzelm@41891
  1524
  hence "?I x (Mul i t) = (real i) * ?I x (CN 0 ?nt ?at)" by simp
chaieb@23264
  1525
  also have "\<dots> = ?I x (CN 0 (i*?nt) (Mul i ?at))" by (simp add: right_distrib)
chaieb@23264
  1526
  finally show ?case using th th2 by simp
chaieb@23264
  1527
next
chaieb@23264
  1528
  case (9 t n a)
chaieb@23264
  1529
  let ?nt = "fst (zsplit0 t)"
chaieb@23264
  1530
  let ?at = "snd (zsplit0 t)"
wenzelm@41891
  1531
  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a= Floor ?at \<and> n=?nt" using 9
chaieb@23264
  1532
    by (simp add: Let_def split_def)
wenzelm@41891
  1533
  from abj 9 have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
chaieb@23264
  1534
  hence na: "?N a" using th by simp
chaieb@23264
  1535
  have th': "(real ?nt)*(real x) = real (?nt * x)" by simp
chaieb@23264
  1536
  have "?I x (Floor t) = ?I x (Floor (CN 0 ?nt ?at))" using th2 by simp
chaieb@23264
  1537
  also have "\<dots> = real (floor ((real ?nt)* real(x) + ?I x ?at))" by simp
chaieb@23264
  1538
  also have "\<dots> = real (floor (?I x ?at + real (?nt* x)))" by (simp add: add_ac)
chaieb@23264
  1539
  also have "\<dots> = real (floor (?I x ?at) + (?nt* x))" 
chaieb@23264
  1540
    using floor_add[where x="?I x ?at" and a="?nt* x"] by simp 
chaieb@23264
  1541
  also have "\<dots> = real (?nt)*(real x) + real (floor (?I x ?at))" by (simp add: add_ac)
chaieb@23264
  1542
  finally have "?I x (Floor t) = ?I x (CN 0 n a)" using th by simp
chaieb@23264
  1543
  with na show ?case by simp
chaieb@23264
  1544
qed
chaieb@23264
  1545
chaieb@23264
  1546
consts
chaieb@23264
  1547
  iszlfm :: "fm \<Rightarrow> real list \<Rightarrow> bool"   (* Linearity test for fm *)
chaieb@23264
  1548
  zlfm :: "fm \<Rightarrow> fm"       (* Linearity transformation for fm *)
chaieb@23264
  1549
recdef iszlfm "measure size"
chaieb@23264
  1550
  "iszlfm (And p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)" 
chaieb@23264
  1551
  "iszlfm (Or p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)" 
chaieb@23264
  1552
  "iszlfm (Eq  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1553
  "iszlfm (NEq (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1554
  "iszlfm (Lt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1555
  "iszlfm (Le  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1556
  "iszlfm (Gt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1557
  "iszlfm (Ge  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1558
  "iszlfm (Dvd i (CN 0 c e)) = 
chaieb@23264
  1559
                 (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1560
  "iszlfm (NDvd i (CN 0 c e))= 
chaieb@23264
  1561
                 (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
chaieb@23264
  1562
  "iszlfm p = (\<lambda> bs. isatom p \<and> (bound0 p))"
chaieb@23264
  1563
chaieb@23264
  1564
lemma zlin_qfree: "iszlfm p bs \<Longrightarrow> qfree p"
chaieb@23264
  1565
  by (induct p rule: iszlfm.induct) auto
chaieb@23264
  1566
chaieb@23264
  1567
lemma iszlfm_gen:
chaieb@23264
  1568
  assumes lp: "iszlfm p (x#bs)"
chaieb@23264
  1569
  shows "\<forall> y. iszlfm p (y#bs)"
chaieb@23264
  1570
proof
chaieb@23264
  1571
  fix y
chaieb@23264
  1572
  show "iszlfm p (y#bs)"
chaieb@23264
  1573
    using lp
chaieb@23264
  1574
  by(induct p rule: iszlfm.induct, simp_all add: numbound0_gen[rule_format, where x="x" and y="y"])
chaieb@23264
  1575
qed
chaieb@23264
  1576
chaieb@23264
  1577
lemma conj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (conj p q) bs"
chaieb@23264
  1578
  using conj_def by (cases p,auto)
chaieb@23264
  1579
lemma disj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (disj p q) bs"
chaieb@23264
  1580
  using disj_def by (cases p,auto)
chaieb@23264
  1581
chaieb@23264
  1582
recdef zlfm "measure fmsize"
chaieb@23264
  1583
  "zlfm (And p q) = conj (zlfm p) (zlfm q)"
chaieb@23264
  1584
  "zlfm (Or p q) = disj (zlfm p) (zlfm q)"
chaieb@23264
  1585
  "zlfm (Imp p q) = disj (zlfm (NOT p)) (zlfm q)"
chaieb@23264
  1586
  "zlfm (Iff p q) = disj (conj (zlfm p) (zlfm q)) (conj (zlfm (NOT p)) (zlfm (NOT q)))"
chaieb@23264
  1587
  "zlfm (Lt a) = (let (c,r) = zsplit0 a in 
chaieb@23264
  1588
     if c=0 then Lt r else 
chaieb@23264
  1589
     if c>0 then Or (Lt (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r))) 
chaieb@23264
  1590
     else Or (Gt (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
chaieb@23264
  1591
  "zlfm (Le a) = (let (c,r) = zsplit0 a in 
chaieb@23264
  1592
     if c=0 then Le r else 
chaieb@23264
  1593
     if c>0 then Or (Le (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r))) 
chaieb@23264
  1594
     else Or (Ge (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
chaieb@23264
  1595
  "zlfm (Gt a) = (let (c,r) = zsplit0 a in 
chaieb@23264
  1596
     if c=0 then Gt r else 
chaieb@23264
  1597
     if c>0 then Or (Gt (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r))) 
chaieb@23264
  1598
     else Or (Lt (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
chaieb@23264
  1599
  "zlfm (Ge a) = (let (c,r) = zsplit0 a in 
chaieb@23264
  1600
     if c=0 then Ge r else 
chaieb@23264
  1601
     if c>0 then Or (Ge (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r))) 
chaieb@23264
  1602
     else Or (Le (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
chaieb@23264
  1603
  "zlfm (Eq a) = (let (c,r) = zsplit0 a in 
chaieb@23264
  1604
              if c=0 then Eq r else 
chaieb@23264
  1605
      if c>0 then (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Eq (Add (Floor (Neg r)) r)))
chaieb@23264
  1606
      else (And (Eq (CN 0 (-c) (Floor (Neg r)))) (Eq (Add (Floor (Neg r)) r))))"
chaieb@23264
  1607
  "zlfm (NEq a) = (let (c,r) = zsplit0 a in 
chaieb@23264
  1608
              if c=0 then NEq r else 
chaieb@23264
  1609
      if c>0 then (Or (NEq (CN 0 c (Neg (Floor (Neg r))))) (NEq (Add (Floor (Neg r)) r)))
chaieb@23264
  1610
      else (Or (NEq (CN 0 (-c) (Floor (Neg r)))) (NEq (Add (Floor (Neg r)) r))))"
chaieb@23264
  1611
  "zlfm (Dvd i a) = (if i=0 then zlfm (Eq a) 
chaieb@23264
  1612
  else (let (c,r) = zsplit0 a in 
chaieb@23264
  1613
              if c=0 then Dvd (abs i) r else 
chaieb@23264
  1614
      if c>0 then And (Eq (Sub (Floor r) r)) (Dvd (abs i) (CN 0 c (Floor r))) 
chaieb@23264
  1615
      else And (Eq (Sub (Floor r) r)) (Dvd (abs i) (CN 0 (-c) (Neg (Floor r))))))"
chaieb@23264
  1616
  "zlfm (NDvd i a) = (if i=0 then zlfm (NEq a) 
chaieb@23264
  1617
  else (let (c,r) = zsplit0 a in 
chaieb@23264
  1618
              if c=0 then NDvd (abs i) r else 
chaieb@23264
  1619
      if c>0 then Or (NEq (Sub (Floor r) r)) (NDvd (abs i) (CN 0 c (Floor r))) 
chaieb@23264
  1620
      else Or (NEq (Sub (Floor r) r)) (NDvd (abs i) (CN 0 (-c) (Neg (Floor r))))))"
chaieb@23264
  1621
  "zlfm (NOT (And p q)) = disj (zlfm (NOT p)) (zlfm (NOT q))"
chaieb@23264
  1622
  "zlfm (NOT (Or p q)) = conj (zlfm (NOT p)) (zlfm (NOT q))"
chaieb@23264
  1623
  "zlfm (NOT (Imp p q)) = conj (zlfm p) (zlfm (NOT q))"
chaieb@23264
  1624
  "zlfm (NOT (Iff p q)) = disj (conj(zlfm p) (zlfm(NOT q))) (conj (zlfm(NOT p)) (zlfm q))"
chaieb@23264
  1625
  "zlfm (NOT (NOT p)) = zlfm p"
chaieb@23264
  1626
  "zlfm (NOT T) = F"
chaieb@23264
  1627
  "zlfm (NOT F) = T"
chaieb@23264
  1628
  "zlfm (NOT (Lt a)) = zlfm (Ge a)"
chaieb@23264
  1629
  "zlfm (NOT (Le a)) = zlfm (Gt a)"
chaieb@23264
  1630
  "zlfm (NOT (Gt a)) = zlfm (Le a)"
chaieb@23264
  1631
  "zlfm (NOT (Ge a)) = zlfm (Lt a)"
chaieb@23264
  1632
  "zlfm (NOT (Eq a)) = zlfm (NEq a)"
chaieb@23264
  1633
  "zlfm (NOT (NEq a)) = zlfm (Eq a)"
chaieb@23264
  1634
  "zlfm (NOT (Dvd i a)) = zlfm (NDvd i a)"
chaieb@23264
  1635
  "zlfm (NOT (NDvd i a)) = zlfm (Dvd i a)"
chaieb@23264
  1636
  "zlfm p = p" (hints simp add: fmsize_pos)
chaieb@23264
  1637
chaieb@23264
  1638
lemma split_int_less_real: 
chaieb@23264
  1639
  "(real (a::int) < b) = (a < floor b \<or> (a = floor b \<and> real (floor b) < b))"
chaieb@23264
  1640
proof( auto)
chaieb@23264
  1641
  assume alb: "real a < b" and agb: "\<not> a < floor b"
chaieb@23264
  1642
  from agb have "floor b \<le> a" by simp hence th: "b < real a + 1" by (simp only: floor_le_eq)
chaieb@23264
  1643
  from floor_eq[OF alb th] show "a= floor b" by simp 
chaieb@23264
  1644
next
chaieb@23264
  1645
  assume alb: "a < floor b"
chaieb@23264
  1646
  hence "real a < real (floor b)" by simp
chaieb@23264
  1647
  moreover have "real (floor b) \<le> b" by simp ultimately show  "real a < b" by arith 
chaieb@23264
  1648
qed
chaieb@23264
  1649
chaieb@23264
  1650
lemma split_int_less_real': 
chaieb@23264
  1651
  "(real (a::int) + b < 0) = (real a - real (floor(-b)) < 0 \<or> (real a - real (floor (-b)) = 0 \<and> real (floor (-b)) + b < 0))"
chaieb@23264
  1652
proof- 
chaieb@23264
  1653
  have "(real a + b <0) = (real a < -b)" by arith
chaieb@23264
  1654
  with split_int_less_real[where a="a" and b="-b"] show ?thesis by arith  
chaieb@23264
  1655
qed
chaieb@23264
  1656
chaieb@23264
  1657
lemma split_int_gt_real': 
chaieb@23264
  1658
  "(real (a::int) + b > 0) = (real a + real (floor b) > 0 \<or> (real a + real (floor b) = 0 \<and> real (floor b) - b < 0))"
chaieb@23264
  1659
proof- 
chaieb@23264
  1660
  have th: "(real a + b >0) = (real (-a) + (-b)< 0)" by arith
nipkow@41849
  1661
  show ?thesis using myless[of _ "real (floor b)"] 
chaieb@23264
  1662
    by (simp only:th split_int_less_real'[where a="-a" and b="-b"]) 
haftmann@37887
  1663
    (simp add: algebra_simps diff_minus[symmetric],arith)
chaieb@23264
  1664
qed
chaieb@23264
  1665
chaieb@23264
  1666
lemma split_int_le_real: 
chaieb@23264
  1667
  "(real (a::int) \<le> b) = (a \<le> floor b \<or> (a = floor b \<and> real (floor b) < b))"
chaieb@23264
  1668
proof( auto)
chaieb@23264
  1669
  assume alb: "real a \<le> b" and agb: "\<not> a \<le> floor b"
huffman@30097
  1670
  from alb have "floor (real a) \<le> floor b " by (simp only: floor_mono) 
chaieb@23264
  1671
  hence "a \<le> floor b" by simp with agb show "False" by simp
chaieb@23264
  1672
next
chaieb@23264
  1673
  assume alb: "a \<le> floor b"
huffman@30097
  1674
  hence "real a \<le> real (floor b)" by (simp only: floor_mono)
chaieb@23264
  1675
  also have "\<dots>\<le> b" by simp  finally show  "real a \<le> b" . 
chaieb@23264
  1676
qed
chaieb@23264
  1677
chaieb@23264
  1678
lemma split_int_le_real': 
chaieb@23264
  1679
  "(real (a::int) + b \<le> 0) = (real a - real (floor(-b)) \<le> 0 \<or> (real a - real (floor (-b)) = 0 \<and> real (floor (-b)) + b < 0))"
chaieb@23264
  1680
proof- 
chaieb@23264
  1681
  have "(real a + b \<le>0) = (real a \<le> -b)" by arith
chaieb@23264
  1682
  with split_int_le_real[where a="a" and b="-b"] show ?thesis by arith  
chaieb@23264
  1683
qed
chaieb@23264
  1684
chaieb@23264
  1685
lemma split_int_ge_real': 
chaieb@23264
  1686
  "(real (a::int) + b \<ge> 0) = (real a + real (floor b) \<ge> 0 \<or> (real a + real (floor b) = 0 \<and> real (floor b) - b < 0))"
chaieb@23264
  1687
proof- 
chaieb@23264
  1688
  have th: "(real a + b \<ge>0) = (real (-a) + (-b) \<le> 0)" by arith
chaieb@23264
  1689
  show ?thesis by (simp only: th split_int_le_real'[where a="-a" and b="-b"])
haftmann@37887
  1690
    (simp add: algebra_simps diff_minus[symmetric],arith)
chaieb@23264
  1691
qed
chaieb@23264
  1692
chaieb@23264
  1693
lemma split_int_eq_real: "(real (a::int) = b) = ( a = floor b \<and> b = real (floor b))" (is "?l = ?r")
chaieb@23264
  1694
by auto
chaieb@23264
  1695
chaieb@23264
  1696
lemma split_int_eq_real': "(real (a::int) + b = 0) = ( a - floor (-b) = 0 \<and> real (floor (-b)) + b = 0)" (is "?l = ?r")
chaieb@23264
  1697
proof-
chaieb@23264
  1698
  have "?l = (real a = -b)" by arith
chaieb@23264
  1699
  with split_int_eq_real[where a="a" and b="-b"] show ?thesis by simp arith
chaieb@23264
  1700
qed
chaieb@23264
  1701
chaieb@23264
  1702
lemma zlfm_I:
chaieb@23264
  1703
  assumes qfp: "qfree p"
chaieb@23264
  1704
  shows "(Ifm (real i #bs) (zlfm p) = Ifm (real i# bs) p) \<and> iszlfm (zlfm p) (real (i::int) #bs)"
chaieb@23264
  1705
  (is "(?I (?l p) = ?I p) \<and> ?L (?l p)")
chaieb@23264
  1706
using qfp
chaieb@23264
  1707
proof(induct p rule: zlfm.induct)
chaieb@23264
  1708
  case (5 a) 
chaieb@23264
  1709
  let ?c = "fst (zsplit0 a)"
chaieb@23264
  1710
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1711
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1712
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1713
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1714
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1715
  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1716
  moreover
chaieb@23264
  1717
  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1718
      by (cases "?r", simp_all add: Let_def split_def,case_tac "nat", simp_all)}
chaieb@23264
  1719
  moreover
chaieb@23264
  1720
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))" 
chaieb@23264
  1721
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1722
    have "?I (Lt a) = (real (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1723
    also have "\<dots> = (?I (?l (Lt a)))" apply (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) by (simp add: Ia cp cnz Let_def split_def diff_minus)
chaieb@23264
  1724
    finally have ?case using l by simp}
chaieb@23264
  1725
  moreover
chaieb@23264
  1726
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))" 
chaieb@23264
  1727
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1728
    have "?I (Lt a) = (real (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1729
    also from cn cnz have "\<dots> = (?I (?l (Lt a)))" by (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_minus[symmetric] add_ac, arith)
chaieb@23264
  1730
    finally have ?case using l by simp}
chaieb@23264
  1731
  ultimately show ?case by blast
chaieb@23264
  1732
next
chaieb@23264
  1733
  case (6 a)
chaieb@23264
  1734
  let ?c = "fst (zsplit0 a)"
chaieb@23264
  1735
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1736
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1737
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1738
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1739
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1740
  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1741
  moreover
chaieb@23264
  1742
  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1743
      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat",simp_all)}
chaieb@23264
  1744
  moreover
chaieb@23264
  1745
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))" 
chaieb@23264
  1746
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1747
    have "?I (Le a) = (real (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1748
    also have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_minus)
chaieb@23264
  1749
    finally have ?case using l by simp}
chaieb@23264
  1750
  moreover
chaieb@23264
  1751
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))" 
chaieb@23264
  1752
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1753
    have "?I (Le a) = (real (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1754
    also from cn cnz have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_minus[symmetric] add_ac ,arith)
chaieb@23264
  1755
    finally have ?case using l by simp}
chaieb@23264
  1756
  ultimately show ?case by blast
chaieb@23264
  1757
next
chaieb@23264
  1758
  case (7 a) 
chaieb@23264
  1759
  let ?c = "fst (zsplit0 a)"
chaieb@23264
  1760
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1761
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1762
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1763
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1764
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1765
  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1766
  moreover
chaieb@23264
  1767
  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1768
      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
chaieb@23264
  1769
  moreover
chaieb@23264
  1770
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))" 
chaieb@23264
  1771
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1772
    have "?I (Gt a) = (real (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1773
    also have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_minus)
chaieb@23264
  1774
    finally have ?case using l by simp}
chaieb@23264
  1775
  moreover
chaieb@23264
  1776
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))" 
chaieb@23264
  1777
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1778
    have "?I (Gt a) = (real (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1779
    also from cn cnz have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_minus[symmetric] add_ac, arith)
chaieb@23264
  1780
    finally have ?case using l by simp}
chaieb@23264
  1781
  ultimately show ?case by blast
chaieb@23264
  1782
next
chaieb@23264
  1783
  case (8 a)
chaieb@23264
  1784
   let ?c = "fst (zsplit0 a)"
chaieb@23264
  1785
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1786
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1787
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1788
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1789
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1790
  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1791
  moreover
chaieb@23264
  1792
  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1793
      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
chaieb@23264
  1794
  moreover
chaieb@23264
  1795
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))" 
chaieb@23264
  1796
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1797
    have "?I (Ge a) = (real (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1798
    also have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_minus)
chaieb@23264
  1799
    finally have ?case using l by simp}
chaieb@23264
  1800
  moreover
chaieb@23264
  1801
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))" 
chaieb@23264
  1802
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1803
    have "?I (Ge a) = (real (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
haftmann@37887
  1804
    also from cn cnz have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_minus[symmetric] add_ac, arith)
chaieb@23264
  1805
    finally have ?case using l by simp}
chaieb@23264
  1806
  ultimately show ?case by blast
chaieb@23264
  1807
next
chaieb@23264
  1808
  case (9 a)
chaieb@23264
  1809
  let ?c = "fst (zsplit0 a)"
chaieb@23264
  1810
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1811
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1812
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1813
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1814
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1815
  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1816
  moreover
chaieb@23264
  1817
  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1818
      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
chaieb@23264
  1819
  moreover
chaieb@23264
  1820
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))" 
chaieb@23264
  1821
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1822
    have "?I (Eq a) = (real (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
chaieb@23264
  1823
    also have "\<dots> = (?I (?l (Eq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult)
chaieb@23264
  1824
    finally have ?case using l by simp}
chaieb@23264
  1825
  moreover
chaieb@23264
  1826
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))" 
chaieb@23264
  1827
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1828
    have "?I (Eq a) = (real (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
chaieb@23264
  1829
    also from cn cnz have "\<dots> = (?I (?l (Eq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult,arith)
chaieb@23264
  1830
    finally have ?case using l by simp}
chaieb@23264
  1831
  ultimately show ?case by blast
chaieb@23264
  1832
next
chaieb@23264
  1833
  case (10 a)
chaieb@23264
  1834
  let ?c = "fst (zsplit0 a)"
chaieb@23264
  1835
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1836
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1837
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1838
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1839
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1840
  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1841
  moreover
chaieb@23264
  1842
  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1843
      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
chaieb@23264
  1844
  moreover
chaieb@23264
  1845
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))" 
chaieb@23264
  1846
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1847
    have "?I (NEq a) = (real (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
chaieb@23264
  1848
    also have "\<dots> = (?I (?l (NEq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult)
chaieb@23264
  1849
    finally have ?case using l by simp}
chaieb@23264
  1850
  moreover
chaieb@23264
  1851
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))" 
chaieb@23264
  1852
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1853
    have "?I (NEq a) = (real (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
chaieb@23264
  1854
    also from cn cnz have "\<dots> = (?I (?l (NEq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult,arith)
chaieb@23264
  1855
    finally have ?case using l by simp}
chaieb@23264
  1856
  ultimately show ?case by blast
chaieb@23264
  1857
next
chaieb@23264
  1858
  case (11 j a)
chaieb@23264
  1859
  let ?c = "fst (zsplit0 a)"
chaieb@23264
  1860
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1861
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1862
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1863
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1864
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1865
  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1866
  moreover
wenzelm@41891
  1867
  { assume j: "j=0" hence z: "zlfm (Dvd j a) = (zlfm (Eq a))" by (simp add: Let_def) 
wenzelm@41891
  1868
    hence ?case using 11 j by (simp del: zlfm.simps add: rdvd_left_0_eq)}
chaieb@23264
  1869
  moreover
chaieb@23264
  1870
  {assume "?c=0" and "j\<noteq>0" hence ?case 
chaieb@23264
  1871
      using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
chaieb@23264
  1872
      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
chaieb@23264
  1873
  moreover
chaieb@23264
  1874
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
chaieb@23264
  1875
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1876
    have "?I (Dvd j a) = (real j rdvd (real (?c * i) + (?N ?r)))" 
chaieb@23264
  1877
      using Ia by (simp add: Let_def split_def)
chaieb@23264
  1878
    also have "\<dots> = (real (abs j) rdvd real (?c*i) + (?N ?r))" 
chaieb@23264
  1879
      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
chaieb@23264
  1880
    also have "\<dots> = ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
chaieb@23264
  1881
       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r))))" 
chaieb@23264
  1882
      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
chaieb@23264
  1883
    also have "\<dots> = (?I (?l (Dvd j a)))" using cp cnz jnz  
chaieb@23264
  1884
      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
wenzelm@32960
  1885
        del: real_of_int_mult) (auto simp add: add_ac)
chaieb@23264
  1886
    finally have ?case using l jnz  by simp }
chaieb@23264
  1887
  moreover
chaieb@23264
  1888
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
chaieb@23264
  1889
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1890
    have "?I (Dvd j a) = (real j rdvd (real (?c * i) + (?N ?r)))" 
chaieb@23264
  1891
      using Ia by (simp add: Let_def split_def)
chaieb@23264
  1892
    also have "\<dots> = (real (abs j) rdvd real (?c*i) + (?N ?r))" 
chaieb@23264
  1893
      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
chaieb@23264
  1894
    also have "\<dots> = ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
chaieb@23264
  1895
       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r))))" 
chaieb@23264
  1896
      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
chaieb@23264
  1897
    also have "\<dots> = (?I (?l (Dvd j a)))" using cn cnz jnz
chaieb@23264
  1898
      using rdvd_minus [where d="abs j" and t="real (?c*i + floor (?N ?r))", simplified, symmetric]
chaieb@23264
  1899
      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
wenzelm@32960
  1900
        del: real_of_int_mult) (auto simp add: add_ac)
chaieb@23264
  1901
    finally have ?case using l jnz by blast }
chaieb@23264
  1902
  ultimately show ?case by blast
chaieb@23264
  1903
next
chaieb@23264
  1904
  case (12 j a)
chaieb@23264
  1905
  let ?c = "fst (zsplit0 a)"
chaieb@23264
  1906
  let ?r = "snd (zsplit0 a)"
chaieb@23264
  1907
  have spl: "zsplit0 a = (?c,?r)" by simp
chaieb@23264
  1908
  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
chaieb@23264
  1909
  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
chaieb@23264
  1910
  let ?N = "\<lambda> t. Inum (real i#bs) t"
chaieb@23264
  1911
  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
chaieb@23264
  1912
  moreover
wenzelm@41891
  1913
  {assume j: "j=0" hence z: "zlfm (NDvd j a) = (zlfm (NEq a))" by (simp add: Let_def) 
wenzelm@41891
  1914
    hence ?case using 12 j by (simp del: zlfm.simps add: rdvd_left_0_eq)}
chaieb@23264
  1915
  moreover
chaieb@23264
  1916
  {assume "?c=0" and "j\<noteq>0" hence ?case 
chaieb@23264
  1917
      using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
chaieb@23264
  1918
      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
chaieb@23264
  1919
  moreover
chaieb@23264
  1920
  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))" 
chaieb@23264
  1921
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1922
    have "?I (NDvd j a) = (\<not> (real j rdvd (real (?c * i) + (?N ?r))))" 
chaieb@23264
  1923
      using Ia by (simp add: Let_def split_def)
chaieb@23264
  1924
    also have "\<dots> = (\<not> (real (abs j) rdvd real (?c*i) + (?N ?r)))" 
chaieb@23264
  1925
      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
chaieb@23264
  1926
    also have "\<dots> = (\<not> ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
chaieb@23264
  1927
       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r)))))" 
chaieb@23264
  1928
      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
chaieb@23264
  1929
    also have "\<dots> = (?I (?l (NDvd j a)))" using cp cnz jnz  
chaieb@23264
  1930
      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
wenzelm@32960
  1931
        del: real_of_int_mult) (auto simp add: add_ac)
chaieb@23264
  1932
    finally have ?case using l jnz  by simp }
chaieb@23264
  1933
  moreover
chaieb@23264
  1934
  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))" 
chaieb@23264
  1935
      by (simp add: nb Let_def split_def isint_Floor isint_neg)
chaieb@23264
  1936
    have "?I (NDvd j a) = (\<not> (real j rdvd (real (?c * i) + (?N ?r))))" 
chaieb@23264
  1937
      using Ia by (simp add: Let_def split_def)
chaieb@23264
  1938
    also have "\<dots> = (\<not> (real (abs j) rdvd real (?c*i) + (?N ?r)))" 
chaieb@23264
  1939
      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
chaieb@23264
  1940
    also have "\<dots> = (\<not> ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
chaieb@23264
  1941
       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r)))))" 
chaieb@23264
  1942
      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
chaieb@23264
  1943
    also have "\<dots> = (?I (?l (NDvd j a)))" using cn cnz jnz
chaieb@23264
  1944
      using rdvd_minus [where d="abs j" and t="real (?c*i + floor (?N ?r))", simplified, symmetric]
chaieb@23264
  1945
      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
wenzelm@32960
  1946
        del: real_of_int_mult) (auto simp add: add_ac)
chaieb@23264
  1947
    finally have ?case using l jnz by blast }
chaieb@23264
  1948
  ultimately show ?case by blast
chaieb@23264
  1949
qed auto
chaieb@23264
  1950
chaieb@23316
  1951
text{* plusinf : Virtual substitution of @{text "+\<infinity>"}
chaieb@23316
  1952
       minusinf: Virtual substitution of @{text "-\<infinity>"}
chaieb@23316
  1953
       @{text "\<delta>"} Compute lcm @{text "d| Dvd d  c*x+t \<in> p"}
chaieb@23316
  1954
       @{text "d\<delta>"} checks if a given l divides all the ds above*}
chaieb@23316
  1955
krauss@41839
  1956
fun minusinf:: "fm \<Rightarrow> fm" where
chaieb@23264
  1957
  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
krauss@41839
  1958
| "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
krauss@41839
  1959
| "minusinf (Eq  (CN 0 c e)) = F"
krauss@41839
  1960
| "minusinf (NEq (CN 0 c e)) = T"
krauss@41839
  1961
| "minusinf (Lt  (CN 0 c e)) = T"
krauss@41839
  1962
| "minusinf (Le  (CN 0 c e)) = T"
krauss@41839
  1963
| "minusinf (Gt  (CN 0 c e)) = F"
krauss@41839
  1964
| "minusinf (Ge  (CN 0 c e)) = F"
krauss@41839
  1965
| "minusinf p = p"
chaieb@23264
  1966
chaieb@23264
  1967
lemma minusinf_qfree: "qfree p \<Longrightarrow> qfree (minusinf p)"
chaieb@23264
  1968
  by (induct p rule: minusinf.induct, auto)
chaieb@23264
  1969
krauss@41839
  1970
fun plusinf:: "fm \<Rightarrow> fm" where
chaieb@23264
  1971
  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
krauss@41839
  1972
| "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
krauss@41839
  1973
| "plusinf (Eq  (CN 0 c e)) = F"
krauss@41839
  1974
| "plusinf (NEq (CN 0 c e)) = T"
krauss@41839
  1975
| "plusinf (Lt  (CN 0 c e)) = F"
krauss@41839
  1976
| "plusinf (Le  (CN 0 c e)) = F"
krauss@41839
  1977
| "plusinf (Gt  (CN 0 c e)) = T"
krauss@41839
  1978
| "plusinf (Ge  (CN 0 c e)) = T"
krauss@41839
  1979
| "plusinf p = p"
krauss@41839
  1980
krauss@41839
  1981
fun \<delta> :: "fm \<Rightarrow> int" where
huffman@31706
  1982
  "\<delta> (And p q) = lcm (\<delta> p) (\<delta> q)" 
krauss@41839
  1983
| "\<delta> (Or p q) = lcm (\<delta> p) (\<delta> q)" 
krauss@41839
  1984
| "\<delta> (Dvd i (CN 0 c e)) = i"
krauss@41839
  1985
| "\<delta> (NDvd i (CN 0 c e)) = i"
krauss@41839
  1986
| "\<delta> p = 1"
krauss@41839
  1987
krauss@41839
  1988
fun d\<delta> :: "fm \<Rightarrow> int \<Rightarrow> bool" where
chaieb@23264
  1989
  "d\<delta> (And p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
krauss@41839
  1990
| "d\<delta> (Or p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
krauss@41839
  1991
| "d\<delta> (Dvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
krauss@41839
  1992
| "d\<delta> (NDvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
krauss@41839
  1993
| "d\<delta> p = (\<lambda> d. True)"
chaieb@23264
  1994
chaieb@23264
  1995
lemma delta_mono: 
chaieb@23264
  1996
  assumes lin: "iszlfm p bs"
chaieb@23264
  1997
  and d: "d dvd d'"
chaieb@23264
  1998
  and ad: "d\<delta> p d"
chaieb@23264
  1999
  shows "d\<delta> p d'"
chaieb@23264
  2000
  using lin ad d
chaieb@23264
  2001
proof(induct p rule: iszlfm.induct)
chaieb@23264
  2002
  case (9 i c e)  thus ?case using d
nipkow@30042
  2003
    by (simp add: dvd_trans[of "i" "d" "d'"])
chaieb@23264
  2004
next
chaieb@23264
  2005
  case (10 i c e) thus ?case using d
nipkow@30042
  2006
    by (simp add: dvd_trans[of "i" "d" "d'"])
chaieb@23264
  2007
qed simp_all
chaieb@23264
  2008
chaieb@23264
  2009
lemma \<delta> : assumes lin:"iszlfm p bs"
chaieb@23264
  2010
  shows "d\<delta> p (\<delta> p) \<and> \<delta> p >0"
chaieb@23264
  2011
using lin
chaieb@23264
  2012
proof (induct p rule: iszlfm.induct)
chaieb@23264
  2013
  case (1 p q) 
chaieb@23264
  2014
  let ?d = "\<delta> (And p q)"
wenzelm@41891
  2015
  from 1 lcm_pos_int have dp: "?d >0" by simp
wenzelm@41891
  2016
  have d1: "\<delta> p dvd \<delta> (And p q)" using 1 by simp 
wenzelm@41891
  2017
  hence th: "d\<delta> p ?d" 
wenzelm@41891
  2018
    using delta_mono 1 by (simp only: iszlfm.simps) blast
wenzelm@41891
  2019
  have "\<delta> q dvd \<delta> (And p q)" using 1 by simp 
wenzelm@41891
  2020
  hence th': "d\<delta> q ?d" using delta_mono 1 by (simp only: iszlfm.simps) blast
chaieb@23997
  2021
  from th th' dp show ?case by simp 
chaieb@23264
  2022
next
chaieb@23264
  2023
  case (2 p q)  
chaieb@23264
  2024
  let ?d = "\<delta> (And p q)"
wenzelm@41891
  2025
  from 2 lcm_pos_int have dp: "?d >0" by simp
wenzelm@41891
  2026
  have "\<delta> p dvd \<delta> (And p q)" using 2 by simp
wenzelm@41891
  2027
  hence th: "d\<delta> p ?d" using delta_mono 2 by (simp only: iszlfm.simps) blast
wenzelm@41891
  2028
  have "\<delta> q dvd \<delta> (And p q)" using 2 by simp
wenzelm@41891
  2029
  hence th': "d\<delta> q ?d" using delta_mono 2 by (simp only: iszlfm.simps) blast
nipkow@31730
  2030
  from th th' dp show ?case by simp
chaieb@23264
  2031
qed simp_all
chaieb@23264
  2032
chaieb@23264
  2033
chaieb@23264
  2034
lemma minusinf_inf:
chaieb@23264
  2035
  assumes linp: "iszlfm p (a # bs)"
chaieb@23264
  2036
  shows "\<exists> (z::int). \<forall> x < z. Ifm ((real x)#bs) (minusinf p) = Ifm ((real x)#bs) p"
chaieb@23264
  2037
  (is "?P p" is "\<exists> (z::int). \<forall> x < z. ?I x (?M p) = ?I x p")
chaieb@23264
  2038
using linp
chaieb@23264
  2039
proof (induct p rule: minusinf.induct)
chaieb@23264
  2040
  case (1 f g)
wenzelm@41891
  2041
  then have "?P f" by simp
chaieb@23264
  2042
  then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
wenzelm@41891
  2043
  with 1 have "?P g" by simp
chaieb@23264
  2044
  then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
chaieb@23264
  2045
  let ?z = "min z1 z2"
chaieb@23264
  2046
  from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (And f g)) = ?I x (And f g)" by simp
chaieb@23264
  2047
  thus ?case by blast
chaieb@23264
  2048
next
wenzelm@41891
  2049
  case (2 f g)
wenzelm@41891
  2050
  then have "?P f" by simp
chaieb@23264
  2051
  then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
wenzelm@41891
  2052
  with 2 have "?P g" by simp
chaieb@23264
  2053
  then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
chaieb@23264
  2054
  let ?z = "min z1 z2"
chaieb@23264
  2055
  from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (Or f g)) = ?I x (Or f g)" by simp
chaieb@23264
  2056
  thus ?case by blast
chaieb@23264
  2057
next
chaieb@23264
  2058
  case (3 c e) 
wenzelm@41891
  2059
  then have "c > 0" by simp
wenzelm@41891
  2060
  hence rcpos: "real c > 0" by simp
wenzelm@41891
  2061
  from 3 have nbe: "numbound0 e" by simp
wenzelm@26932
  2062
  fix y
chaieb@23264
  2063
  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Eq (CN 0 c e))) = ?I x (Eq (CN 0 c e))"
chaieb@23264
  2064
  proof (simp add: less_floor_eq , rule allI, rule impI) 
chaieb@23264
  2065
    fix x
chaieb@23264
  2066
    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
chaieb@23264
  2067
    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
chaieb@23264
  2068
    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
huffman@36778
  2069
      by (simp only: mult_strict_left_mono [OF th1 rcpos])
chaieb@23264
  2070
    hence "real c * real x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
chaieb@23264
  2071
    thus "real c * real x + Inum (real x # bs) e \<noteq> 0" 
chaieb@23264
  2072
      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"]  by simp
chaieb@23264
  2073
  qed
chaieb@23264
  2074
  thus ?case by blast
chaieb@23264
  2075
next
chaieb@23264
  2076
  case (4 c e) 
wenzelm@41891
  2077
  then have "c > 0" by simp hence rcpos: "real c > 0" by simp
wenzelm@41891
  2078
  from 4 have nbe: "numbound0 e" by simp
wenzelm@26932
  2079
  fix y
chaieb@23264
  2080
  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (NEq (CN 0 c e))) = ?I x (NEq (CN 0 c e))"
chaieb@23264
  2081
  proof (simp add: less_floor_eq , rule allI, rule impI) 
chaieb@23264
  2082
    fix x
chaieb@23264
  2083
    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
chaieb@23264
  2084
    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
chaieb@23264
  2085
    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
huffman@36778
  2086
      by (simp only: mult_strict_left_mono [OF th1 rcpos])
chaieb@23264
  2087
    hence "real c * real x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
chaieb@23264
  2088
    thus "real c * real x + Inum (real x # bs) e \<noteq> 0" 
chaieb@23264
  2089
      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"]  by simp
chaieb@23264
  2090
  qed
chaieb@23264
  2091
  thus ?case by blast
chaieb@23264
  2092
next
chaieb@23264
  2093
  case (5 c e) 
wenzelm@41891
  2094
  then have "c > 0" by simp hence rcpos: "real c > 0" by simp
wenzelm@41891
  2095
  from 5 have nbe: "numbound0 e" by simp
wenzelm@26932
  2096
  fix y
chaieb@23264
  2097
  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Lt (CN 0 c e))) = ?I x (Lt (CN 0 c e))"
chaieb@23264
  2098
  proof (simp add: less_floor_eq , rule allI, rule impI) 
chaieb@23264
  2099
    fix x
chaieb@23264
  2100
    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
chaieb@23264
  2101
    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
chaieb@23264
  2102
    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
huffman@36778
  2103
      by (simp only: mult_strict_left_mono [OF th1 rcpos])
chaieb@23264
  2104
    thus "real c * real x + Inum (real x # bs) e < 0" 
chaieb@23264
  2105
      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
chaieb@23264
  2106
  qed
chaieb@23264
  2107
  thus ?case by blast
chaieb@23264
  2108
next
chaieb@23264
  2109
  case (6 c e) 
wenzelm@41891
  2110
  then have "c > 0" by simp hence rcpos: "real c > 0" by simp
wenzelm@41891
  2111
  from 6 have nbe: "numbound0 e" by simp
wenzelm@26932
  2112
  fix y
chaieb@23264
  2113
  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Le (CN 0 c e))) = ?I x (Le (CN 0 c e))"
chaieb@23264
  2114
  proof (simp add: less_floor_eq , rule allI, rule impI) 
chaieb@23264
  2115
    fix x
chaieb@23264
  2116
    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e /