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