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