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