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

« back to all changes in this revision

Viewing changes to contrib/setoid_ring/Ring_tac.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
Set Implicit Arguments.
 
2
Require Import Setoid.
 
3
Require Import BinPos.
 
4
Require Import Ring_polynom.
 
5
Require Import BinList.
 
6
Require Import InitialRing.
 
7
  
 
8
 
 
9
(* adds a definition id' on the normal form of t and an hypothesis id
 
10
   stating that t = id' (tries to produces a proof as small as possible) *)
 
11
Ltac compute_assertion id  id' t :=
 
12
  let t' := eval vm_compute in t in
 
13
  pose (id' := t');
 
14
  assert (id : t = id');
 
15
  [vm_cast_no_check (refl_equal id')|idtac].
 
16
(* [exact_no_check (refl_equal id'<: t = id')|idtac]). *)
 
17
 
 
18
(********************************************************************)
 
19
(* Tacticals to build reflexive tactics *)
 
20
 
 
21
Ltac OnEquation req :=
 
22
  match goal with
 
23
  | |- req ?lhs ?rhs => (fun f => f lhs rhs)
 
24
  | _ => fail 1 "Goal is not an equation (of expected equality)"
 
25
  end.
 
26
 
 
27
Ltac OnMainSubgoal H ty :=
 
28
  match ty with
 
29
  | _ -> ?ty' =>
 
30
     let subtac := OnMainSubgoal H ty' in
 
31
     fun tac => lapply H; [clear H; intro H; subtac tac | idtac]
 
32
  | _ => (fun tac => tac)
 
33
  end.
 
34
 
 
35
Ltac ApplyLemmaThen lemma expr tac :=
 
36
  let nexpr := fresh "expr_nf" in
 
37
  let H := fresh "eq_nf" in
 
38
  let Heq := fresh "thm" in
 
39
  let nf_spec :=
 
40
    match type of (lemma expr) with
 
41
      forall x, ?nf_spec = x -> _ => nf_spec
 
42
    | _ => fail 1 "ApplyLemmaThen: cannot find norm expression"
 
43
    end in
 
44
  compute_assertion H nexpr nf_spec;
 
45
  assert (Heq:=lemma _ _ H) || fail "anomaly: failed to apply lemma";
 
46
  clear H;
 
47
  OnMainSubgoal Heq ltac:(type of Heq) ltac:(tac Heq; clear Heq nexpr).
 
48
 
 
49
Ltac ApplyLemmaThenAndCont lemma expr tac CONT_tac cont_arg :=
 
50
  let npe := fresh "expr_nf" in
 
51
  let H := fresh "eq_nf" in
 
52
  let Heq := fresh "thm" in
 
53
  let npe_spec :=
 
54
    match type of (lemma expr) with
 
55
      forall npe, ?npe_spec = npe -> _ => npe_spec
 
56
    | _ => fail 1 "ApplyLemmaThenAndCont: cannot find norm expression"
 
57
    end in
 
58
  (compute_assertion H npe npe_spec;
 
59
   (assert (Heq:=lemma _ _ H) || fail "anomaly: failed to apply lemma");
 
60
   clear H;
 
61
   OnMainSubgoal Heq ltac:(type of Heq)
 
62
     ltac:(try tac Heq; clear Heq npe;CONT_tac cont_arg)).
 
63
 
 
64
(* General scheme of reflexive tactics using of correctness lemma
 
65
   that involves normalisation of one expression *)
 
66
 
 
67
Ltac ReflexiveRewriteTactic FV_tac SYN_tac MAIN_tac LEMMA_tac fv terms :=
 
68
  (* extend the atom list *)
 
69
  let fv := list_fold_left FV_tac fv terms in
 
70
  let RW_tac lemma := 
 
71
     let fcons term CONT_tac cont_arg := 
 
72
      let expr := SYN_tac term fv in
 
73
      (ApplyLemmaThenAndCont lemma expr MAIN_tac CONT_tac cont_arg) in
 
74
     (* rewrite steps *)
 
75
     lazy_list_fold_right fcons ltac:(idtac) terms in
 
76
  LEMMA_tac fv RW_tac.
 
77
 
 
78
(********************************************************)
 
79
 
 
80
 
 
81
(* Building the atom list of a ring expression *)
 
82
Ltac FV Cst CstPow add mul sub opp pow t fv :=
 
83
 let rec TFV t fv :=
 
84
  match Cst t with
 
85
  | NotConstant =>
 
86
      match t with
 
87
      | (add ?t1 ?t2) => TFV t2 ltac:(TFV t1 fv)
 
88
      | (mul ?t1 ?t2) => TFV t2 ltac:(TFV t1 fv)
 
89
      | (sub ?t1 ?t2) => TFV t2 ltac:(TFV t1 fv)
 
90
      | (opp ?t1) => TFV t1 fv
 
91
      | (pow ?t1 ?n) =>
 
92
        match CstPow n with
 
93
        | InitialRing.NotConstant => AddFvTail t fv
 
94
        | _ => TFV t1 fv
 
95
        end
 
96
      | _ => AddFvTail t fv
 
97
      end
 
98
  | _ => fv
 
99
  end
 
100
 in TFV t fv.
 
101
 
 
102
 (* syntaxification of ring expressions *)
 
103
Ltac mkPolexpr C Cst CstPow radd rmul rsub ropp rpow t fv := 
 
104
 let rec mkP t :=
 
105
    let f :=
 
106
    match Cst t with
 
107
    | InitialRing.NotConstant =>
 
108
        match t with 
 
109
        | (radd ?t1 ?t2) => 
 
110
          fun _ =>
 
111
          let e1 := mkP t1 in
 
112
          let e2 := mkP t2 in constr:(PEadd e1 e2)
 
113
        | (rmul ?t1 ?t2) => 
 
114
          fun _ =>
 
115
          let e1 := mkP t1 in
 
116
          let e2 := mkP t2 in constr:(PEmul e1 e2)
 
117
        | (rsub ?t1 ?t2) => 
 
118
          fun _ => 
 
119
          let e1 := mkP t1 in
 
120
          let e2 := mkP t2 in constr:(PEsub e1 e2)
 
121
        | (ropp ?t1) =>
 
122
          fun _ =>
 
123
          let e1 := mkP t1 in constr:(PEopp e1)
 
124
        | (rpow ?t1 ?n) =>
 
125
          match CstPow n with
 
126
          | InitialRing.NotConstant => 
 
127
            fun _ => let p := Find_at t fv in constr:(PEX C p)
 
128
          | ?c => fun _ => let e1 := mkP t1 in constr:(PEpow e1 c)
 
129
          end
 
130
        | _ =>
 
131
          fun _ => let p := Find_at t fv in constr:(PEX C p)
 
132
        end
 
133
    | ?c => fun _ => constr:(@PEc C c)
 
134
    end in
 
135
    f ()
 
136
 in mkP t.
 
137
 
 
138
Ltac ParseRingComponents lemma :=
 
139
  match type of lemma with
 
140
  | context [@PEeval ?R ?rO ?add ?mul ?sub ?opp ?C ?phi ?Cpow ?powphi ?pow _ _] =>
 
141
      (fun f => f R add mul sub opp pow C)
 
142
  | _ => fail 1 "ring anomaly: bad correctness lemma (parse)"
 
143
  end.
 
144
 
 
145
(* ring tactics *)
 
146
 
 
147
Ltac relation_carrier req :=
 
148
  let ty := type of req in
 
149
  match eval hnf in ty with
 
150
   ?R -> _ => R
 
151
  | _ => fail 1000 "Equality has no relation type"
 
152
  end.
 
153
 
 
154
Ltac FV_hypo_tac mkFV req lH :=
 
155
  let R := relation_carrier req in
 
156
  let FV_hypo_l_tac h :=
 
157
    match h with @mkhypo (req ?pe _) _ => mkFV pe end in
 
158
  let FV_hypo_r_tac h :=
 
159
    match h with @mkhypo (req _ ?pe) _ => mkFV pe end in
 
160
  let fv := list_fold_right FV_hypo_l_tac (@nil R) lH in
 
161
  list_fold_right FV_hypo_r_tac fv lH.
 
162
 
 
163
Ltac mkHyp_tac C req mkPE lH :=
 
164
  let mkHyp h res := 
 
165
   match h with 
 
166
   | @mkhypo (req ?r1 ?r2) _ =>
 
167
     let pe1 := mkPE r1 in
 
168
     let pe2 := mkPE r2 in
 
169
     constr:(cons (pe1,pe2) res)
 
170
   | _ => fail 1 "hypothesis is not a ring equality"
 
171
   end in
 
172
  list_fold_right mkHyp (@nil (PExpr C * PExpr C)) lH.
 
173
     
 
174
Ltac proofHyp_tac lH :=
 
175
  let get_proof h :=
 
176
    match h with
 
177
    | @mkhypo _ ?p => p
 
178
    end in
 
179
  let rec bh l :=
 
180
    match l with
 
181
    | nil => constr:(I)
 
182
    | cons ?h nil => get_proof h
 
183
    | cons ?h ?tl => 
 
184
      let l := get_proof h in
 
185
      let r := bh tl in  
 
186
      constr:(conj l r)
 
187
    end in
 
188
  bh lH.
 
189
 
 
190
Definition ring_subst_niter := (10*10*10)%nat.
 
191
 
 
192
Ltac Ring Cst_tac CstPow_tac lemma1 req n lH :=
 
193
  let Main lhs rhs R radd rmul rsub ropp rpow C :=
 
194
    let mkFV := FV Cst_tac CstPow_tac radd rmul rsub ropp rpow in
 
195
    let mkPol := mkPolexpr C Cst_tac CstPow_tac radd rmul rsub ropp rpow in
 
196
    let fv := FV_hypo_tac mkFV req lH in
 
197
    let fv := mkFV lhs fv in
 
198
    let fv := mkFV rhs fv in
 
199
    check_fv fv;
 
200
    let pe1 := mkPol lhs fv in
 
201
    let pe2 := mkPol rhs fv in
 
202
    let lpe := mkHyp_tac C req ltac:(fun t => mkPol t fv) lH in
 
203
    let vlpe := fresh "hyp_list" in
 
204
    let vfv := fresh "fv_list" in
 
205
    pose (vlpe := lpe);
 
206
    pose (vfv := fv);
 
207
    (apply (lemma1 n vfv vlpe pe1 pe2)
 
208
      || fail "typing error while applying ring");
 
209
    [ ((let prh := proofHyp_tac lH in exact prh)
 
210
        || idtac "can not automatically proof hypothesis : maybe a left member of a hypothesis is not a monomial") 
 
211
    | vm_compute;
 
212
      (exact (refl_equal true) || fail "not a valid ring equation")] in
 
213
  ParseRingComponents lemma1 ltac:(OnEquation req Main).
 
214
 
 
215
Ltac Ring_norm_gen f Cst_tac CstPow_tac lemma2 req n lH rl :=
 
216
  let Main R add mul sub opp pow C :=
 
217
    let mkFV := FV Cst_tac CstPow_tac add mul sub opp pow in
 
218
    let mkPol := mkPolexpr C Cst_tac CstPow_tac add mul sub opp pow in
 
219
    let fv := FV_hypo_tac mkFV req lH in
 
220
    let simpl_ring H := (protect_fv "ring" in H; f H) in
 
221
    let lemma_tac fv RW_tac := 
 
222
      let rr_lemma := fresh "r_rw_lemma" in
 
223
      let lpe := mkHyp_tac C req ltac:(fun t => mkPol t fv) lH in
 
224
      let vlpe := fresh "list_hyp" in
 
225
      let vlmp := fresh "list_hyp_norm" in
 
226
      let vlmp_eq := fresh "list_hyp_norm_eq" in
 
227
      let prh := proofHyp_tac lH in
 
228
      pose (vlpe := lpe);
 
229
      match type of lemma2 with
 
230
      | context [mk_monpol_list ?cO ?cI ?cadd ?cmul ?csub ?copp ?cdiv ?ceqb _]
 
231
            =>
 
232
        compute_assertion vlmp_eq vlmp 
 
233
            (mk_monpol_list cO cI cadd cmul csub copp cdiv ceqb vlpe);
 
234
         (assert (rr_lemma := lemma2 n vlpe fv prh vlmp vlmp_eq)
 
235
          || fail 1 "type error when build the rewriting lemma");   
 
236
         RW_tac rr_lemma;
 
237
         try clear rr_lemma vlmp_eq vlmp vlpe
 
238
      | _ => fail 1 "ring_simplify anomaly: bad correctness lemma"
 
239
      end in
 
240
    ReflexiveRewriteTactic mkFV mkPol simpl_ring lemma_tac fv rl in
 
241
  ParseRingComponents lemma2 Main.
 
242
 
 
243
 
 
244
Ltac Ring_gen
 
245
  req sth ext morph arth cst_tac pow_tac lemma1 lemma2 pre post lH rl :=
 
246
  pre();Ring cst_tac pow_tac lemma1 req ring_subst_niter lH.
 
247
 
 
248
Ltac Get_goal := match goal with [|- ?G] => G end.
 
249
 
 
250
Tactic Notation (at level 0) "ring" :=
 
251
  let G := Get_goal in
 
252
  ring_lookup Ring_gen [] G.
 
253
 
 
254
Tactic Notation (at level 0) "ring" "[" constr_list(lH) "]" :=
 
255
  let G := Get_goal in
 
256
  ring_lookup Ring_gen [lH] G.
 
257
 
 
258
(* Simplification *)
 
259
 
 
260
Ltac Ring_simplify_gen f :=
 
261
  fun req sth ext morph arth cst_tac pow_tac lemma1 lemma2 pre post lH rl =>
 
262
     let l := fresh "to_rewrite" in
 
263
     pose (l:= rl);
 
264
     generalize (refl_equal l);
 
265
     unfold l at 2;
 
266
     pre(); 
 
267
     let Tac RL :=
 
268
       let Heq := fresh "Heq" in
 
269
       intros Heq;clear Heq l;
 
270
       Ring_norm_gen f cst_tac pow_tac lemma2 req ring_subst_niter lH RL; 
 
271
       post() in
 
272
     let Main :=
 
273
       match goal with
 
274
       | [|- l = ?RL -> _ ] => (fun f => f RL)
 
275
       | _ => fail 1 "ring_simplify anomaly: bad goal after pre"
 
276
       end in
 
277
     Main Tac.
 
278
 
 
279
Ltac Ring_simplify := Ring_simplify_gen ltac:(fun H => rewrite H).
 
280
 
 
281
Tactic Notation (at level 0) "ring_simplify" constr_list(rl) := 
 
282
  let G := Get_goal in
 
283
  ring_lookup Ring_simplify [] rl G.
 
284
 
 
285
Tactic Notation (at level 0) 
 
286
  "ring_simplify" "[" constr_list(lH) "]" constr_list(rl) :=
 
287
  let G := Get_goal in
 
288
  ring_lookup Ring_simplify [lH] rl G.
 
289
 
 
290
(* MON DIEU QUE C'EST MOCHE !!!!!!!!!!!!! *)
 
291
 
 
292
Tactic Notation "ring_simplify" constr_list(rl) "in" hyp(H):=   
 
293
  let G := Get_goal in
 
294
  let t := type of H in   
 
295
  let g := fresh "goal" in
 
296
  set (g:= G);
 
297
  generalize H;clear H;
 
298
  ring_lookup Ring_simplify [] rl t;
 
299
  intro H;
 
300
  unfold g;clear g.
 
301
 
 
302
Tactic Notation 
 
303
  "ring_simplify" "["constr_list(lH)"]" constr_list(rl) "in" hyp(H):=   
 
304
  let G := Get_goal in
 
305
  let t := type of H in   
 
306
  let g := fresh "goal" in
 
307
  set (g:= G);
 
308
  generalize H;clear H;
 
309
  ring_lookup Ring_simplify [lH] rl t;
 
310
  intro H;
 
311
  unfold g;clear g.
 
312
 
 
313
 
 
314
 
 
315
(*     LE RESTE MARCHE PAS DOMMAGE  .....  *)
 
316
 
 
317
 
 
318
 
 
319
 
 
320
 
 
321
 
 
322
 
 
323
 
 
324
 
 
325
 
 
326
 
 
327
 
 
328
 
 
329
 
 
330
 
 
331
(*
 
332
 
 
333
 
 
334
 
 
335
 
 
336
 
 
337
 
 
338
 
 
339
 
 
340
Ltac Ring_simplify_in hyp:= Ring_simplify_gen ltac:(fun H => rewrite H in hyp).
 
341
 
 
342
 
 
343
Tactic Notation (at level 0) 
 
344
  "ring_simplify" "[" constr_list(lH) "]" constr_list(rl) := 
 
345
  match goal with [|- ?G] => ring_lookup Ring_simplify [lH] rl G end.
 
346
 
 
347
Tactic Notation (at level 0) 
 
348
  "ring_simplify" constr_list(rl) := 
 
349
  match goal with [|- ?G] => ring_lookup Ring_simplify [] rl G end.
 
350
 
 
351
Tactic Notation (at level 0) 
 
352
  "ring_simplify" "[" constr_list(lH) "]" constr_list(rl) "in" hyp(h):= 
 
353
  let t := type of h in
 
354
  ring_lookup 
 
355
   (fun req sth ext morph arth cst_tac pow_tac lemma1 lemma2 pre post lH rl =>
 
356
     pre(); 
 
357
     Ring_norm_gen ltac:(fun EQ => rewrite EQ in h) cst_tac pow_tac lemma2 req ring_subst_niter lH rl; 
 
358
     post()) 
 
359
  [lH] rl t. 
 
360
(*  ring_lookup ltac:(Ring_simplify_in h) [lH] rl [t]. NE MARCHE PAS ??? *)
 
361
 
 
362
Ltac Ring_simpl_in hyp := Ring_norm_gen ltac:(fun H => rewrite H in hyp).
 
363
 
 
364
Tactic Notation (at level 0) 
 
365
  "ring_simplify" constr_list(rl) "in" constr(h):= 
 
366
  let t := type of h in
 
367
  ring_lookup   
 
368
   (fun req sth ext morph arth cst_tac pow_tac lemma1 lemma2 pre post lH rl =>
 
369
     pre(); 
 
370
     Ring_simpl_in h cst_tac pow_tac lemma2 req ring_subst_niter lH rl; 
 
371
     post())
 
372
 [] rl t.
 
373
 
 
374
Ltac rw_in H Heq := rewrite Heq in H.
 
375
 
 
376
Ltac simpl_in H := 
 
377
  let t := type of H in
 
378
   ring_lookup 
 
379
   (fun req sth ext morph arth cst_tac pow_tac lemma1 lemma2 pre post lH rl =>
 
380
     pre(); 
 
381
     Ring_norm_gen ltac:(fun Heq => rewrite Heq in H) cst_tac pow_tac lemma2 req ring_subst_niter lH rl; 
 
382
     post()) 
 
383
   [] t.
 
384
 
 
385
 
 
386
*)