~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to contrib/micromega/ZMicromega.v

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(************************************************************************)
 
2
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
 
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
 
4
(*   \VV/  **************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the       *)
 
6
(*         *       GNU Lesser General Public License Version 2.1        *)
 
7
(************************************************************************)
 
8
(*                                                                      *)
 
9
(* Micromega: A reflexive tactic using the Positivstellensatz           *)
 
10
(*                                                                      *)
 
11
(*  Frédéric Besson (Irisa/Inria) 2006-2008                             *)
 
12
(*                                                                      *)
 
13
(************************************************************************)
 
14
 
 
15
Require Import OrderedRing.
 
16
Require Import RingMicromega.
 
17
Require Import ZCoeff.
 
18
Require Import Refl.
 
19
Require Import ZArith.
 
20
Require Import List.
 
21
Require Import Bool.
 
22
 
 
23
Ltac flatten_bool :=
 
24
  repeat match goal with
 
25
           [ id : (_ && _)%bool = true |- _ ] =>  destruct (andb_prop _ _ id); clear id
 
26
           |  [ id : (_ || _)%bool = true |- _ ] =>  destruct (orb_prop _ _ id); clear id
 
27
         end.
 
28
 
 
29
Require Import EnvRing.
 
30
 
 
31
Open Scope Z_scope.
 
32
  
 
33
Lemma Zsor : SOR 0 1 Zplus Zmult Zminus Zopp (@eq Z) Zle Zlt.
 
34
Proof.
 
35
  constructor ; intros ; subst ; try (intuition (auto  with zarith)).
 
36
  apply Zsth.
 
37
  apply Zth.
 
38
  destruct (Ztrichotomy n m) ; intuition (auto with zarith).
 
39
  apply Zmult_lt_0_compat ; auto.
 
40
Qed.
 
41
 
 
42
Lemma ZSORaddon :
 
43
  SORaddon 0 1 Zplus Zmult Zminus Zopp  (@eq Z) Zle (* ring elements *)
 
44
  0%Z 1%Z Zplus Zmult Zminus Zopp (* coefficients *)
 
45
  Zeq_bool Zle_bool
 
46
  (fun x => x) (fun x => x) (pow_N 1 Zmult).
 
47
Proof.
 
48
  constructor.
 
49
  constructor ; intros ; try reflexivity.
 
50
  apply Zeq_bool_eq ; auto.
 
51
  constructor.
 
52
  reflexivity.
 
53
  intros x y.
 
54
  apply Zeq_bool_neq ; auto.
 
55
  apply Zle_bool_imp_le.
 
56
Qed.
 
57
 
 
58
 
 
59
(*Definition Zeval_expr :=  eval_pexpr 0 Zplus Zmult Zminus Zopp  (fun x => x) (fun x => Z_of_N x) (Zpower).*)
 
60
 
 
61
Fixpoint Zeval_expr (env: PolEnv Z) (e: PExpr Z) : Z :=
 
62
  match e with
 
63
    | PEc c =>  c
 
64
    | PEX j =>  env j
 
65
    | PEadd pe1 pe2 => (Zeval_expr env pe1) + (Zeval_expr env pe2)
 
66
    | PEsub pe1 pe2 => (Zeval_expr env pe1) - (Zeval_expr env pe2)
 
67
    | PEmul pe1 pe2 => (Zeval_expr env pe1) * (Zeval_expr env pe2)
 
68
    | PEopp pe1 => - (Zeval_expr env pe1)
 
69
    | PEpow pe1 n => Zpower (Zeval_expr env pe1)  (Z_of_N n)
 
70
  end.
 
71
 
 
72
Lemma Zeval_expr_simpl : forall env e,
 
73
  Zeval_expr env e = 
 
74
  match e with
 
75
    | PEc c =>  c
 
76
    | PEX j =>  env j
 
77
    | PEadd pe1 pe2 => (Zeval_expr env pe1) + (Zeval_expr env pe2)
 
78
    | PEsub pe1 pe2 => (Zeval_expr env pe1) - (Zeval_expr env pe2)
 
79
    | PEmul pe1 pe2 => (Zeval_expr env pe1) * (Zeval_expr env pe2)
 
80
    | PEopp pe1 => - (Zeval_expr env pe1)
 
81
    | PEpow pe1 n => Zpower (Zeval_expr env pe1)  (Z_of_N n)
 
82
  end.
 
83
Proof.
 
84
  destruct e ; reflexivity.
 
85
Qed.
 
86
 
 
87
 
 
88
Definition Zeval_expr' := eval_pexpr  Zplus Zmult Zminus Zopp (fun x => x) (fun x => x) (pow_N 1 Zmult).
 
89
 
 
90
Lemma ZNpower : forall r n, r ^ Z_of_N n = pow_N 1 Zmult r n.
 
91
Proof.
 
92
  destruct n.
 
93
  reflexivity.
 
94
  simpl.
 
95
  unfold Zpower_pos.
 
96
  replace (pow_pos Zmult r p) with (1 * (pow_pos Zmult r p)) by ring.
 
97
  generalize 1.
 
98
  induction p; simpl ; intros ; repeat rewrite IHp ; ring.
 
99
Qed.
 
100
 
 
101
 
 
102
 
 
103
Lemma Zeval_expr_compat : forall env e, Zeval_expr env e = Zeval_expr' env e.
 
104
Proof.
 
105
  induction e ; simpl ; subst ; try congruence.
 
106
  rewrite IHe.
 
107
  apply ZNpower.
 
108
Qed.
 
109
 
 
110
Definition Zeval_op2 (o : Op2) : Z -> Z -> Prop :=
 
111
match o with
 
112
| OpEq =>  @eq Z
 
113
| OpNEq => fun x y  => ~ x = y
 
114
| OpLe => Zle
 
115
| OpGe => Zge
 
116
| OpLt => Zlt
 
117
| OpGt => Zgt
 
118
end.
 
119
 
 
120
Definition Zeval_formula (e: PolEnv Z) (ff : Formula Z) :=
 
121
  let (lhs,o,rhs) := ff in Zeval_op2 o (Zeval_expr e lhs) (Zeval_expr e rhs).
 
122
 
 
123
Definition Zeval_formula' :=
 
124
  eval_formula  Zplus Zmult Zminus Zopp (@eq Z) Zle Zlt (fun x => x) (fun x => x) (pow_N 1 Zmult).
 
125
 
 
126
Lemma Zeval_formula_compat : forall env f, Zeval_formula env f <-> Zeval_formula' env f.
 
127
Proof.
 
128
  intros.
 
129
  unfold Zeval_formula.
 
130
  destruct f.
 
131
  repeat rewrite Zeval_expr_compat.
 
132
  unfold Zeval_formula'.
 
133
  unfold Zeval_expr'.
 
134
  split ; destruct Fop ; simpl; auto with zarith.
 
135
Qed.
 
136
 
 
137
 
 
138
 
 
139
Definition Zeval_nformula :=
 
140
  eval_nformula 0 Zplus Zmult Zminus Zopp (@eq Z) Zle Zlt (fun x => x) (fun x => x) (pow_N 1 Zmult).
 
141
 
 
142
Definition Zeval_op1 (o : Op1) : Z -> Prop :=
 
143
match o with
 
144
| Equal => fun x : Z => x = 0
 
145
| NonEqual => fun x : Z => x <> 0
 
146
| Strict => fun x : Z => 0 < x
 
147
| NonStrict => fun x : Z => 0 <= x
 
148
end.
 
149
 
 
150
Lemma Zeval_nformula_simpl : forall env f, Zeval_nformula env f = (let (p, op) := f in Zeval_op1 op (Zeval_expr env p)).
 
151
Proof.
 
152
  intros.
 
153
  destruct f.
 
154
  rewrite Zeval_expr_compat.
 
155
  reflexivity.
 
156
Qed.
 
157
  
 
158
Lemma Zeval_nformula_dec : forall env d, (Zeval_nformula env d) \/ ~ (Zeval_nformula env d).
 
159
Proof.
 
160
  exact (fun env d =>eval_nformula_dec Zsor (fun x => x) (fun x => x) (pow_N 1%Z Zmult) env d).
 
161
Qed.
 
162
 
 
163
Definition ZWitness := ConeMember Z.
 
164
 
 
165
Definition ZWeakChecker := check_normalised_formulas 0 1 Zplus Zmult Zminus Zopp Zeq_bool Zle_bool.
 
166
 
 
167
Lemma ZWeakChecker_sound :   forall (l : list (NFormula Z)) (cm : ZWitness),
 
168
  ZWeakChecker l cm = true ->
 
169
  forall env, make_impl (Zeval_nformula env) l False.
 
170
Proof.
 
171
  intros l cm H.
 
172
  intro.
 
173
  unfold Zeval_nformula.
 
174
  apply (checker_nf_sound Zsor ZSORaddon l cm).
 
175
  unfold ZWeakChecker in H.
 
176
  exact H.
 
177
Qed.
 
178
 
 
179
Definition xnormalise (t:Formula Z) : list (NFormula Z)  :=
 
180
  let (lhs,o,rhs) := t in
 
181
    match o with
 
182
      | OpEq => 
 
183
        ((PEsub lhs (PEadd rhs (PEc 1))),NonStrict)::((PEsub rhs (PEadd lhs (PEc 1))),NonStrict)::nil
 
184
      | OpNEq => (PEsub lhs rhs,Equal) :: nil
 
185
      | OpGt   => (PEsub rhs lhs,NonStrict) :: nil
 
186
      | OpLt => (PEsub lhs rhs,NonStrict) :: nil
 
187
      | OpGe => (PEsub rhs (PEadd lhs (PEc 1)),NonStrict) :: nil
 
188
      | OpLe => (PEsub lhs (PEadd rhs (PEc 1)),NonStrict) :: nil
 
189
    end.
 
190
 
 
191
Require Import Tauto.
 
192
 
 
193
Definition normalise (t:Formula Z) : cnf (NFormula Z) :=
 
194
  List.map  (fun x => x::nil) (xnormalise t).
 
195
 
 
196
 
 
197
Lemma normalise_correct : forall env t, eval_cnf (Zeval_nformula env) (normalise t) <-> Zeval_formula env t.
 
198
Proof.
 
199
  unfold normalise, xnormalise ; simpl ; intros env t.
 
200
  rewrite Zeval_formula_compat.
 
201
  unfold eval_cnf.
 
202
  destruct t as [lhs o rhs]; case_eq o ; simpl;
 
203
    generalize (   eval_pexpr  Zplus Zmult Zminus Zopp (fun x : Z => x)
 
204
      (fun x : BinNat.N => x) (pow_N 1 Zmult) env lhs);
 
205
    generalize (eval_pexpr  Zplus Zmult Zminus Zopp (fun x : Z => x)
 
206
      (fun x : BinNat.N => x) (pow_N 1 Zmult) env rhs) ; intros z1 z2 ; intros ; subst;
 
207
    intuition (auto with zarith).
 
208
Qed.
 
209
  
 
210
Definition xnegate (t:RingMicromega.Formula Z) : list (NFormula Z)  :=
 
211
  let (lhs,o,rhs) := t in
 
212
    match o with
 
213
      | OpEq  => (PEsub lhs rhs,Equal) :: nil
 
214
      | OpNEq => ((PEsub lhs (PEadd rhs (PEc 1))),NonStrict)::((PEsub rhs (PEadd lhs (PEc 1))),NonStrict)::nil
 
215
      | OpGt  => (PEsub lhs (PEadd rhs (PEc 1)),NonStrict) :: nil
 
216
      | OpLt  => (PEsub rhs (PEadd lhs (PEc 1)),NonStrict) :: nil
 
217
      | OpGe  => (PEsub lhs rhs,NonStrict) :: nil
 
218
      | OpLe  => (PEsub rhs lhs,NonStrict) :: nil
 
219
    end.
 
220
 
 
221
Definition negate (t:RingMicromega.Formula Z) : cnf (NFormula Z) :=
 
222
  List.map  (fun x => x::nil) (xnegate t).
 
223
 
 
224
Lemma negate_correct : forall env t, eval_cnf (Zeval_nformula env) (negate t) <-> ~ Zeval_formula env t.
 
225
Proof.
 
226
  unfold negate, xnegate ; simpl ; intros env t.
 
227
  rewrite Zeval_formula_compat.
 
228
  unfold eval_cnf.
 
229
  destruct t as [lhs o rhs]; case_eq o ; simpl ;
 
230
    generalize (   eval_pexpr  Zplus Zmult Zminus Zopp (fun x : Z => x)
 
231
      (fun x : BinNat.N => x) (pow_N 1 Zmult) env lhs);
 
232
    generalize (eval_pexpr  Zplus Zmult Zminus Zopp (fun x : Z => x)
 
233
      (fun x : BinNat.N => x) (pow_N 1 Zmult) env rhs) ; intros z1 z2 ; intros ; 
 
234
    intuition (auto with zarith).
 
235
Qed.
 
236
 
 
237
 
 
238
Definition ZweakTautoChecker (w: list ZWitness) (f : BFormula (Formula Z)) : bool :=
 
239
  @tauto_checker (Formula Z) (NFormula Z) normalise negate  ZWitness ZWeakChecker f w.
 
240
 
 
241
(* To get a complete checker, the proof format has to be enriched *)
 
242
 
 
243
Require Import Zdiv.
 
244
Open Scope Z_scope.
 
245
 
 
246
Definition ceiling (a b:Z) : Z :=
 
247
  let (q,r) := Zdiv_eucl a b in
 
248
    match r with
 
249
      | Z0 => q
 
250
      | _  => q + 1
 
251
    end.
 
252
 
 
253
Lemma narrow_interval_lower_bound : forall a b x, a > 0 -> a * x  >= b -> x >= ceiling b a.
 
254
Proof.
 
255
  unfold ceiling.
 
256
  intros.
 
257
  generalize (Z_div_mod b a H).
 
258
  destruct (Zdiv_eucl b a).
 
259
  intros.
 
260
  destruct H1.
 
261
  destruct H2.
 
262
  subst.
 
263
  destruct (Ztrichotomy z0 0) as [ HH1 | [HH2 | HH3]]; destruct z0 ; try auto with zarith ; try discriminate.
 
264
  assert (HH :x >= z \/ x < z) by (destruct (Ztrichotomy x z) ; auto with zarith).
 
265
  destruct HH ;auto.
 
266
  generalize (Zmult_lt_compat_l _ _ _ H3 H1).
 
267
  auto with zarith.
 
268
  clear H2.
 
269
  assert (HH :x >= z +1 \/ x <= z) by (destruct (Ztrichotomy x z) ; intuition (auto with zarith)).
 
270
  destruct HH ;auto.
 
271
  assert (0 < a) by auto with zarith.
 
272
  generalize (Zmult_lt_0_le_compat_r _ _ _ H2 H1).
 
273
  intros.
 
274
  rewrite Zmult_comm  in H4.
 
275
  rewrite (Zmult_comm z)  in H4.
 
276
  auto with zarith.
 
277
Qed.
 
278
 
 
279
Lemma narrow_interval_upper_bound : forall a b x, a > 0 -> a * x  <= b -> x <= Zdiv b a.
 
280
Proof.
 
281
  unfold Zdiv.
 
282
  intros.
 
283
  generalize (Z_div_mod b a H).
 
284
  destruct (Zdiv_eucl b a).
 
285
  intros.
 
286
  destruct H1.
 
287
  destruct H2.
 
288
  subst.
 
289
  assert (HH :x <= z \/ z <= x -1) by (destruct (Ztrichotomy x z) ; intuition (auto with zarith)).
 
290
  destruct HH ;auto.
 
291
  assert (0 < a) by auto with zarith.
 
292
  generalize (Zmult_lt_0_le_compat_r _ _ _ H4 H1).
 
293
  intros.
 
294
  ring_simplify in H5.
 
295
  rewrite Zmult_comm in H5.
 
296
  auto with zarith.
 
297
Qed.
 
298
 
 
299
 
 
300
(* In this case, a certificate is made of a pair of inequations, in 1 variable,
 
301
   that do not have an integer solution.
 
302
   => modify the fourier elimination
 
303
   *)
 
304
Require Import QArith.
 
305
 
 
306
 
 
307
Inductive ProofTerm : Type :=
 
308
| RatProof : ZWitness -> ProofTerm
 
309
| CutProof : PExprC Z -> Q -> ZWitness -> ProofTerm -> ProofTerm
 
310
| EnumProof : Q ->  PExprC Z ->  Q -> ZWitness -> ZWitness -> list ProofTerm -> ProofTerm.
 
311
 
 
312
(* n/d <= x  -> d*x - n >= 0 *)
 
313
 
 
314
Definition makeLb (v:PExpr Z) (q:Q) : NFormula Z :=
 
315
  let (n,d) := q in (PEsub (PEmul (PEc (Zpos d)) v) (PEc  n),NonStrict).
 
316
 
 
317
(* x <= n/d  -> d * x <= d  *)
 
318
Definition makeUb (v:PExpr Z) (q:Q) : NFormula Z :=
 
319
  let (n,d) := q in
 
320
    (PEsub (PEc n) (PEmul (PEc (Zpos d)) v), NonStrict).
 
321
 
 
322
Definition qceiling (q:Q) : Z :=
 
323
  let (n,d) := q in ceiling n (Zpos d).
 
324
 
 
325
Definition qfloor (q:Q) : Z :=
 
326
  let (n,d) := q in Zdiv n (Zpos d).
 
327
 
 
328
Definition makeLbCut (v:PExprC Z) (q:Q) : NFormula Z :=
 
329
  (PEsub v  (PEc (qceiling  q)), NonStrict).
 
330
 
 
331
Definition neg_nformula (f : NFormula Z) :=
 
332
  let (e,o) := f in
 
333
    (PEopp (PEadd e (PEc 1%Z)), o).
 
334
    
 
335
Lemma neg_nformula_sound : forall env f, snd f = NonStrict ->( ~ (Zeval_nformula env (neg_nformula f)) <-> Zeval_nformula env f).
 
336
Proof.
 
337
  unfold neg_nformula.
 
338
  destruct f. 
 
339
  simpl.
 
340
  intros ; subst ; simpl in *.
 
341
  split;  auto with zarith.
 
342
Qed.
 
343
 
 
344
 
 
345
Definition cutChecker (l:list (NFormula Z)) (e: PExpr Z) (lb:Q) (pf : ZWitness) : option (NFormula Z) :=
 
346
    let (lb,lc) :=  (makeLb e lb,makeLbCut e lb) in
 
347
      if ZWeakChecker (neg_nformula lb::l) pf then Some lc else None.
 
348
 
 
349
 
 
350
Fixpoint ZChecker (l:list (NFormula Z)) (pf : ProofTerm)  {struct pf} : bool :=
 
351
  match pf with
 
352
    | RatProof pf => ZWeakChecker l pf
 
353
    | CutProof e q pf rst => 
 
354
      match cutChecker l e q pf with
 
355
        | None => false
 
356
        | Some c => ZChecker  (c::l) rst
 
357
      end
 
358
    | EnumProof lb e ub  pf1 pf2 rst => 
 
359
      match cutChecker l e lb  pf1 , cutChecker l (PEopp e) (Qopp ub) pf2 with
 
360
        | None ,  _  | _ , None => false
 
361
        | Some _  , Some _ => let (lb',ub') := (qceiling lb, Zopp (qceiling (- ub))) in
 
362
          (fix label (pfs:list ProofTerm) :=
 
363
            fun lb ub => 
 
364
              match pfs with
 
365
                | nil => if Z_gt_dec lb ub then true else false
 
366
                | pf::rsr => andb (ZChecker  ((PEsub e (PEc lb), Equal) :: l) pf) (label rsr (Zplus lb 1%Z) ub) 
 
367
              end)
 
368
               rst lb' ub'
 
369
      end
 
370
  end.
 
371
 
 
372
 
 
373
Lemma ZChecker_simpl : forall (pf : ProofTerm) (l:list (NFormula Z)),
 
374
  ZChecker  l pf = 
 
375
  match pf with
 
376
    | RatProof pf => ZWeakChecker l pf
 
377
    | CutProof e q pf rst => 
 
378
      match cutChecker l e q pf with
 
379
        | None => false
 
380
        | Some c => ZChecker  (c::l) rst
 
381
      end
 
382
    | EnumProof lb e ub  pf1 pf2 rst => 
 
383
      match cutChecker l e lb  pf1 , cutChecker l (PEopp e) (Qopp ub) pf2 with
 
384
        | None ,  _  | _ , None => false
 
385
        | Some _  , Some _ => let (lb',ub') := (qceiling lb, Zopp (qceiling (- ub))) in
 
386
          (fix label (pfs:list ProofTerm) :=
 
387
            fun lb ub => 
 
388
              match pfs with
 
389
                | nil => if Z_gt_dec lb ub then true else false
 
390
                | pf::rsr => andb (ZChecker  ((PEsub e (PEc lb), Equal) :: l) pf) (label rsr (Zplus lb 1%Z) ub) 
 
391
              end)
 
392
               rst lb' ub'
 
393
      end
 
394
  end.
 
395
Proof.
 
396
  destruct pf ; reflexivity.
 
397
Qed.
 
398
 
 
399
(*
 
400
Fixpoint depth (n:nat) : ProofTerm -> option nat :=
 
401
  match n with
 
402
    | O => fun pf => None
 
403
    | S n => 
 
404
      fun pf => 
 
405
        match pf with
 
406
          | RatProof _ => Some O
 
407
          | CutProof _ _ _ p =>  option_map S  (depth n p)
 
408
          | EnumProof  _ _ _ _ _ l => 
 
409
            let f := fun pf x => 
 
410
              match x , depth n pf with
 
411
                | None ,  _ | _ , None => None
 
412
                | Some n1 , Some n2 => Some (Max.max n1 n2)
 
413
              end in
 
414
            List.fold_right f  (Some O) l
 
415
        end
 
416
  end.
 
417
*)
 
418
Fixpoint bdepth (pf : ProofTerm) : nat :=
 
419
  match pf with
 
420
    | RatProof _ =>  O
 
421
    | CutProof _ _ _ p =>   S  (bdepth p)
 
422
    | EnumProof _ _ _ _ _ l => S (List.fold_right (fun pf x => Max.max (bdepth pf) x)   O l)
 
423
  end.
 
424
 
 
425
Require Import Wf_nat.
 
426
 
 
427
Lemma in_bdepth : forall l a b p c c0  y, In y l ->  ltof ProofTerm bdepth y (EnumProof a b p c c0 l).
 
428
Proof.
 
429
  induction l.
 
430
  simpl.
 
431
  tauto.
 
432
  simpl.
 
433
  intros.
 
434
  destruct H.
 
435
  subst.
 
436
  unfold ltof.
 
437
  simpl.
 
438
  generalize (         (fold_right
 
439
            (fun (pf : ProofTerm) (x : nat) => Max.max (bdepth pf) x) 0%nat l)).
 
440
  intros.
 
441
  generalize (bdepth y) ; intros.
 
442
  generalize (Max.max_l n0 n) (Max.max_r n0 n).
 
443
  omega.
 
444
  generalize (IHl a0 b p c c0 y  H).
 
445
  unfold ltof.
 
446
  simpl.
 
447
  generalize (      (fold_right (fun (pf : ProofTerm) (x : nat) => Max.max (bdepth pf) x) 0%nat
 
448
         l)).
 
449
  intros.
 
450
  generalize (Max.max_l (bdepth a) n) (Max.max_r (bdepth a) n).
 
451
  omega.
 
452
Qed.
 
453
 
 
454
Lemma lb_lbcut : forall env e q,  Zeval_nformula env (makeLb e q) -> Zeval_nformula env (makeLbCut e q).
 
455
Proof.
 
456
  unfold makeLb, makeLbCut.
 
457
  destruct q.
 
458
  rewrite Zeval_nformula_simpl.
 
459
  rewrite Zeval_nformula_simpl.
 
460
  unfold Zeval_op1.
 
461
  rewrite Zeval_expr_simpl.
 
462
  rewrite Zeval_expr_simpl.
 
463
  rewrite Zeval_expr_simpl.
 
464
  intro.
 
465
  rewrite Zeval_expr_simpl.
 
466
  revert H.
 
467
  generalize (Zeval_expr env e).
 
468
  rewrite Zeval_expr_simpl.
 
469
  rewrite Zeval_expr_simpl.
 
470
  unfold qceiling.
 
471
  intros.
 
472
  assert ( z >= ceiling Qnum (' Qden))%Z.
 
473
  apply narrow_interval_lower_bound.
 
474
  compute.
 
475
  reflexivity.
 
476
  destruct z ; auto with zarith.
 
477
  auto with zarith.
 
478
Qed.
 
479
 
 
480
Lemma cutChecker_sound : forall e lb pf l res, cutChecker l e lb pf = Some res -> 
 
481
  forall env, make_impl  (Zeval_nformula env) l (Zeval_nformula env res).
 
482
Proof.
 
483
  unfold cutChecker.
 
484
  intros.
 
485
  revert H.
 
486
  case_eq (ZWeakChecker (neg_nformula (makeLb e lb) :: l) pf); intros ; [idtac | discriminate].
 
487
  generalize (ZWeakChecker_sound _ _  H env).
 
488
  intros.
 
489
  inversion H0 ; subst ; clear H0.
 
490
  apply -> make_conj_impl.
 
491
  simpl in H1.
 
492
  rewrite <- make_conj_impl in H1.
 
493
  intros.
 
494
  apply -> neg_nformula_sound ; auto.
 
495
  red ; intros.
 
496
  apply H1 ; auto.
 
497
  clear H H1 H0.
 
498
  generalize (lb_lbcut env e lb).
 
499
  intros.
 
500
  destruct (Zeval_nformula_dec  env ((neg_nformula (makeLb e lb)))).
 
501
  auto.
 
502
  rewrite -> neg_nformula_sound in H0.
 
503
  assert (HH := H H0).
 
504
  rewrite <- neg_nformula_sound in HH.
 
505
  tauto.
 
506
  reflexivity.
 
507
  unfold makeLb.
 
508
  destruct lb.
 
509
  reflexivity.
 
510
Qed.
 
511
 
 
512
 
 
513
Lemma cutChecker_sound_bound : forall e lb pf l res, cutChecker l e lb pf = Some res -> 
 
514
  forall env,  make_conj  (Zeval_nformula env) l  -> (Zeval_expr env e >= qceiling lb)%Z.
 
515
Proof.
 
516
  intros.
 
517
  generalize (cutChecker_sound _ _ _ _ _ H env).
 
518
  intros.
 
519
  rewrite  <- (make_conj_impl) in H1. 
 
520
  generalize (H1 H0).
 
521
  unfold cutChecker in H.
 
522
  destruct (ZWeakChecker (neg_nformula (makeLb e lb) :: l) pf).
 
523
  unfold makeLbCut in H.
 
524
  inversion H ; subst.
 
525
  clear H.
 
526
  simpl.
 
527
  rewrite Zeval_expr_compat.
 
528
  unfold Zeval_expr'.
 
529
  auto with zarith.
 
530
  discriminate.
 
531
Qed.
 
532
 
 
533
 
 
534
Lemma ZChecker_sound : forall w l, ZChecker l w = true -> forall env, make_impl  (Zeval_nformula env)  l False.
 
535
Proof.
 
536
  induction w    using (well_founded_ind (well_founded_ltof _ bdepth)).
 
537
  destruct w.
 
538
  (* RatProof *)
 
539
  simpl.
 
540
  intros.
 
541
  eapply ZWeakChecker_sound.
 
542
  apply H0.
 
543
  (* CutProof *)
 
544
  simpl.
 
545
  intro.
 
546
  case_eq (cutChecker l p q z) ; intros.
 
547
  generalize (cutChecker_sound _ _ _ _ _ H0 env).
 
548
  intro.
 
549
  assert (make_impl  (Zeval_nformula env) (n::l) False).
 
550
  eapply (H w) ; auto.
 
551
  unfold ltof.
 
552
  simpl.
 
553
  auto with arith.
 
554
  simpl in H3.
 
555
  rewrite <- make_conj_impl in H2.
 
556
  rewrite <- make_conj_impl in H3.
 
557
  rewrite <- make_conj_impl.
 
558
  tauto.
 
559
  discriminate.
 
560
  (* EnumProof *)
 
561
  intro.
 
562
  rewrite ZChecker_simpl.
 
563
  case_eq (cutChecker l0 p q z).
 
564
  rename q into llb.
 
565
  case_eq (cutChecker l0 (PEopp p) (- q0) z0).
 
566
  intros.
 
567
  rename q0 into uub.
 
568
  (* get the bounds of the enum *)
 
569
  rewrite <- make_conj_impl.
 
570
  intro.
 
571
  assert (qceiling llb <= Zeval_expr env p <= - qceiling ( - uub))%Z.
 
572
  generalize (cutChecker_sound_bound _ _ _ _ _ H0 env H3).
 
573
  generalize (cutChecker_sound_bound _ _ _ _ _ H1 env H3).
 
574
  intros.
 
575
  rewrite Zeval_expr_simpl in H5.
 
576
  auto with zarith.
 
577
  clear H0 H1.
 
578
  revert H2 H3 H4.
 
579
  generalize (qceiling llb) (- qceiling (- uub))%Z.
 
580
  set (FF := (fix label (pfs : list ProofTerm) (lb ub : Z) {struct pfs} : bool :=
 
581
    match pfs with
 
582
      | nil => if Z_gt_dec lb ub then true else false
 
583
      | pf :: rsr =>
 
584
        (ZChecker ((PEsub p (PEc lb), Equal) :: l0) pf &&
 
585
          label rsr (lb + 1)%Z ub)%bool
 
586
    end)).
 
587
  intros z1 z2.
 
588
  intros.
 
589
  assert (forall x, z1 <= x <= z2 -> exists pr, 
 
590
    (In pr l /\
 
591
      ZChecker  ((PEsub p (PEc x),Equal) :: l0) pr = true))%Z.
 
592
  clear H.
 
593
  revert H2.
 
594
  clear H4.
 
595
  revert z1 z2.
 
596
  induction l;simpl ;intros.
 
597
  destruct (Z_gt_dec z1 z2).
 
598
  intros.
 
599
  apply False_ind ; omega.
 
600
  discriminate.
 
601
  intros.
 
602
  simpl in H2.
 
603
  flatten_bool.
 
604
  assert (HH:(x = z1 \/ z1 +1 <=x)%Z) by omega.
 
605
  destruct HH.
 
606
  subst.
 
607
  exists a ; auto.
 
608
  assert (z1 + 1 <= x <= z2)%Z by omega.
 
609
  destruct (IHl _ _ H1 _ H4).
 
610
  destruct H5.
 
611
  exists x0 ; split;auto.
 
612
  (*/asser *)
 
613
  destruct (H0 _ H4) as [pr [Hin Hcheker]].
 
614
  assert (make_impl (Zeval_nformula env) ((PEsub p (PEc (Zeval_expr env p)),Equal) :: l0) False).
 
615
  apply (H pr);auto.
 
616
  apply in_bdepth ; auto.
 
617
  rewrite <- make_conj_impl in H1.
 
618
  apply H1.
 
619
  rewrite  make_conj_cons.
 
620
  split ;auto.
 
621
  rewrite Zeval_nformula_simpl;
 
622
    unfold Zeval_op1;
 
623
      rewrite Zeval_expr_simpl.
 
624
  generalize (Zeval_expr env p).
 
625
  intros.
 
626
  rewrite Zeval_expr_simpl.
 
627
  auto with zarith.
 
628
  intros ; discriminate.
 
629
  intros ; discriminate.
 
630
Qed.
 
631
 
 
632
Definition ZTautoChecker  (f : BFormula (Formula Z)) (w: list ProofTerm): bool :=
 
633
  @tauto_checker (Formula Z) (NFormula Z) normalise negate  ProofTerm ZChecker f w.
 
634
 
 
635
Lemma ZTautoChecker_sound : forall f w, ZTautoChecker f w = true -> forall env, eval_f  (Zeval_formula env)  f.
 
636
Proof.
 
637
  intros f w.
 
638
  unfold ZTautoChecker.
 
639
  apply (tauto_checker_sound  Zeval_formula Zeval_nformula).
 
640
  apply Zeval_nformula_dec.
 
641
  intros env t.
 
642
  rewrite normalise_correct ; auto.
 
643
  intros env t.
 
644
  rewrite negate_correct ; auto.
 
645
  intros t w0.
 
646
  apply ZChecker_sound.
 
647
Qed.
 
648
 
 
649
 
 
650
Open Scope Z_scope.
 
651
 
 
652
 
 
653
Fixpoint map_cone (f: nat -> nat) (e:ZWitness) : ZWitness :=
 
654
  match e with
 
655
    | S_In n         => S_In _ (f n)
 
656
    | S_Ideal e cm   => S_Ideal e (map_cone f cm)
 
657
    | S_Square _     => e
 
658
    | S_Monoid l     => S_Monoid _ (List.map f l)
 
659
    | S_Mult cm1 cm2 => S_Mult (map_cone f cm1) (map_cone f cm2)
 
660
    | S_Add cm1 cm2  => S_Add (map_cone f cm1) (map_cone f cm2)
 
661
    |  _             => e
 
662
  end.
 
663
 
 
664
Fixpoint indexes (e:ZWitness) : list nat :=
 
665
  match e with
 
666
    | S_In n         => n::nil
 
667
    | S_Ideal e cm   => indexes cm
 
668
    | S_Square e     => nil
 
669
    | S_Monoid l     => l
 
670
    | S_Mult cm1 cm2 => (indexes cm1)++ (indexes cm2)
 
671
    | S_Add cm1 cm2  => (indexes cm1)++ (indexes cm2)
 
672
    |  _             => nil
 
673
  end.
 
674
  
 
675
(** To ease bindings from ml code **)
 
676
(*Definition varmap := Quote.varmap.*)
 
677
Definition make_impl := Refl.make_impl.
 
678
Definition make_conj := Refl.make_conj.
 
679
 
 
680
Require VarMap.
 
681
 
 
682
(*Definition varmap_type := VarMap.t Z. *)
 
683
Definition env := PolEnv Z.
 
684
Definition node := @VarMap.Node Z.
 
685
Definition empty := @VarMap.Empty Z.
 
686
Definition leaf := @VarMap.Leaf Z.
 
687
 
 
688
Definition coneMember := ZWitness.
 
689
 
 
690
Definition eval := Zeval_formula.
 
691
 
 
692
Definition prod_pos_nat := prod positive nat.
 
693
 
 
694
Require Import Int.
 
695
 
 
696
 
 
697
Definition n_of_Z (z:Z) : BinNat.N :=
 
698
  match z with
 
699
    | Z0 => N0
 
700
    | Zpos p => Npos p
 
701
    | Zneg p => N0
 
702
  end.
 
703
 
 
704
 
 
705