]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_omdoc/cic2content.ml
The Aux argument of conclude is now of type string (used to be int).
[helm.git] / helm / ocaml / cic_omdoc / cic2content.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (**************************************************************************)
27 (*                                                                        *)
28 (*                           PROJECT HELM                                 *)
29 (*                                                                        *)
30 (*                Andrea Asperti <asperti@cs.unibo.it>                    *)
31 (*                             16/62003                                   *)
32 (*                                                                        *)
33 (**************************************************************************)
34
35 (* e se mettessi la conversione di BY nell'apply_context ? *)
36 (* sarebbe carino avere l'invariante che la proof2pres
37 generasse sempre prove con contesto vuoto *)
38  
39 let gen_id seed =
40  let res = "p" ^ string_of_int !seed in
41   incr seed ;
42   res
43 ;;
44
45 let name_of = function
46     Cic.Anonymous -> None
47   | Cic.Name b -> Some b;;
48  
49 exception Not_a_proof;;
50 exception NotImplemented;;
51 exception NotApplicable;;
52    
53 (* we do not care for positivity, here, that in any case is enforced by
54    well typing. Just a brutal search *)
55
56 let rec occur uri = 
57   let module C = Cic in
58   function
59       C.Rel _ -> false
60     | C.Var _ -> false
61     | C.Meta _ -> false
62     | C.Sort _ -> false
63     | C.Implicit -> raise NotImplemented
64     | C.Prod (_,s,t) -> (occur uri s) or (occur uri t)
65     | C.Cast (te,ty) -> (occur uri te)
66     | C.Lambda (_,s,t) -> (occur uri s) or (occur uri t) (* or false ?? *)
67     | C.LetIn (_,s,t) -> (occur uri s) or (occur uri t)
68     | C.Appl l -> 
69         List.fold_left 
70           (fun b a -> 
71              if b then b  
72              else (occur uri a)) false l
73     | C.Const (_,_) -> false
74     | C.MutInd (uri1,_,_) -> if uri = uri1 then true else false
75     | C.MutConstruct (_,_,_,_) -> false
76     | C.MutCase _ -> false (* presuming too much?? *)
77     | C.Fix _ -> false (* presuming too much?? *)
78     | C.CoFix (_,_) -> false (* presuming too much?? *)
79 ;;
80
81 let get_id = 
82   let module C = Cic in
83   function
84       C.ARel (id,_,_,_) -> id
85     | C.AVar (id,_,_) -> id
86     | C.AMeta (id,_,_) -> id
87     | C.ASort (id,_) -> id
88     | C.AImplicit _ -> raise NotImplemented
89     | C.AProd (id,_,_,_) -> id
90     | C.ACast (id,_,_) -> id
91     | C.ALambda (id,_,_,_) -> id
92     | C.ALetIn (id,_,_,_) -> id
93     | C.AAppl (id,_) -> id
94     | C.AConst (id,_,_) -> id
95     | C.AMutInd (id,_,_,_) -> id
96     | C.AMutConstruct (id,_,_,_,_) -> id
97     | C.AMutCase (id,_,_,_,_,_) -> id
98     | C.AFix (id,_,_) -> id
99     | C.ACoFix (id,_,_) -> id
100 ;;
101
102 let test_for_lifting ~ids_to_inner_types = 
103   let module C = Cic in
104   let module C2A = Cic2acic in
105   (* atomic terms are never lifted, according to my policy *)
106   function
107       C.ARel (id,_,_,_) -> false
108     | C.AVar (id,_,_) -> false
109     | C.AMeta (id,_,_) -> false
110     | C.ASort (id,_) -> false
111     | C.AImplicit _ -> raise NotImplemented
112     | C.AProd (id,_,_,_) -> false
113     | C.ACast (id,_,_) -> 
114          (try 
115             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
116             true;
117           with Not_found -> false)
118     | C.ALambda (id,_,_,_) -> 
119          (try 
120             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
121             true;
122           with Not_found -> false)
123     | C.ALetIn (id,_,_,_) -> 
124          (try 
125             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
126             true;
127           with Not_found -> false)
128     | C.AAppl (id,_) ->
129          (try 
130             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
131             true;
132           with Not_found -> false) 
133     | C.AConst (id,_,_) -> 
134          (try 
135             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
136             true;
137           with Not_found -> false) 
138     | C.AMutInd (id,_,_,_) -> false
139     | C.AMutConstruct (id,_,_,_,_) -> 
140        (try 
141             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
142             true;
143           with Not_found -> false)
144         (* oppure: false *)
145     | C.AMutCase (id,_,_,_,_,_) ->
146          (try 
147             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
148             true;
149           with Not_found -> false)
150     | C.AFix (id,_,_) ->
151           (try 
152             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
153             true;
154           with Not_found -> false)
155     | C.ACoFix (id,_,_) ->
156          (try 
157             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
158             true;
159           with Not_found -> false)
160 ;;
161
162 let build_args seed l subproofs ~ids_to_inner_types ~ids_to_inner_sorts =
163   let module C = Cic in
164   let module K = Content in
165   let rec aux l subrpoofs =
166     match l with
167       [] -> []
168     | t::l1 -> 
169         if (test_for_lifting t ~ids_to_inner_types) then
170           (match subproofs with
171              [] -> assert false
172            | p::tl -> 
173               let new_arg = 
174                 K.Premise
175                   { K.premise_id = gen_id seed;
176                     K.premise_xref = p.K.proof_id;
177                     K.premise_binder = p.K.proof_name;
178                     K.premise_n = None
179                   }
180                 in new_arg::(aux l1 tl))
181         else 
182           let hd = 
183             (match t with 
184                C.ARel (idr,idref,n,b) ->
185                  let sort = 
186                    (try Hashtbl.find ids_to_inner_sorts idr 
187                     with Not_found -> "Type") in 
188                  if sort ="Prop" then 
189                     K.Premise 
190                       { K.premise_id = gen_id seed;
191                         K.premise_xref = idr;
192                         K.premise_binder = Some b;
193                         K.premise_n = Some n
194                       }
195                  else (K.Term t)
196              | _ -> (K.Term t)) in 
197           hd::(aux l1 subproofs)
198   in aux l subproofs
199 ;;
200
201 (* transform a proof p into a proof list, concatenating the last 
202 conclude element to the apply_context list, in case context is
203 empty. Otherwise, it just returns [p] *)
204
205 let flat seed p = 
206  let module K = Content in
207   if (p.K.proof_context = []) then
208     if p.K.proof_apply_context = [] then [p]
209     else 
210       let p1 =
211         { p with
212           K.proof_context = []; 
213           K.proof_apply_context = []
214         } in
215       p.K.proof_apply_context@[p1]
216   else 
217     [p]
218 ;;
219
220 let rec serialize seed = 
221   function 
222       [] -> []
223     | p::tl -> (flat seed p)@(serialize seed tl);;
224
225 (* top_down = true if the term is a LAMBDA or a decl *)
226 let generate_conversion seed top_down id inner_proof ~ids_to_inner_types =
227  let module C2A = Cic2acic in
228  let module K = Content in
229  let exp = (try ((Hashtbl.find ids_to_inner_types id).C2A.annexpected)
230             with Not_found -> None)
231  in
232  match exp with
233      None -> inner_proof
234    | Some expty ->
235        if inner_proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
236          { K.proof_name = None ;
237             K.proof_id   = gen_id seed;
238             K.proof_context = [] ;
239             K.proof_apply_context = [];
240             K.proof_conclude = 
241               { K.conclude_id = gen_id seed; 
242                 K.conclude_aref = id;
243                 K.conclude_method = "TD_Conversion";
244                 K.conclude_args = [K.ArgProof inner_proof];
245                 K.conclude_conclusion = Some expty
246               };
247           }
248         else
249           { K.proof_name = None ;
250             K.proof_id   = gen_id seed;
251             K.proof_context = [] ;
252             K.proof_apply_context = [inner_proof];
253             K.proof_conclude = 
254               { K.conclude_id = gen_id seed; 
255                 K.conclude_aref = id;
256                 K.conclude_method = "BU_Conversion";
257                 K.conclude_args =  
258                  [K.Premise 
259                   { K.premise_id = gen_id seed;
260                     K.premise_xref = inner_proof.K.proof_id; 
261                     K.premise_binder = None;
262                     K.premise_n = None
263                   } 
264                  ]; 
265                 K.conclude_conclusion = Some expty
266               };
267           }
268 ;;
269
270 let generate_exact seed t id name ~ids_to_inner_types =
271   let module C2A = Cic2acic in
272   let module K = Content in
273     { K.proof_name = name;
274       K.proof_id   = id ;
275       K.proof_context = [] ;
276       K.proof_apply_context = [];
277       K.proof_conclude = 
278         { K.conclude_id = gen_id seed; 
279           K.conclude_aref = id;
280           K.conclude_method = "Exact";
281           K.conclude_args = [K.Term t];
282           K.conclude_conclusion = 
283               try Some (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
284               with Not_found -> None
285         };
286     }
287 ;;
288
289 let generate_intros_let_tac seed id n s is_intro inner_proof name ~ids_to_inner_types =
290   let module C2A = Cic2acic in
291   let module C = Cic in
292   let module K = Content in
293     { K.proof_name = name;
294       K.proof_id   = id ;
295       K.proof_context = [] ;
296       K.proof_apply_context = [];
297       K.proof_conclude = 
298         { K.conclude_id = gen_id seed; 
299           K.conclude_aref = id;
300           K.conclude_method = "Intros+LetTac";
301           K.conclude_args = [K.ArgProof inner_proof];
302           K.conclude_conclusion = 
303             try Some 
304              (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
305             with Not_found -> 
306               (match inner_proof.K.proof_conclude.K.conclude_conclusion with
307                  None -> None
308               | Some t -> 
309                   if is_intro then Some (C.AProd ("gen"^id,n,s,t))
310                   else Some (C.ALetIn ("gen"^id,n,s,t)))
311         };
312     }
313 ;;
314
315 let build_decl_item seed id n s ~ids_to_inner_sorts =
316  let module K = Content in
317   try
318    let sort = Hashtbl.find ids_to_inner_sorts (Cic2acic.source_id_of_id id) in
319    if sort = "Prop" then
320       `Hypothesis
321         { K.dec_name = name_of n;
322           K.dec_id = gen_id seed; 
323           K.dec_inductive = false;
324           K.dec_aref = id;
325           K.dec_type = s
326         }
327    else 
328       `Declaration
329         { K.dec_name = name_of n;
330           K.dec_id = gen_id seed; 
331           K.dec_inductive = false;
332           K.dec_aref = id;
333           K.dec_type = s
334         }
335   with
336    Not_found -> assert false
337 ;;
338
339 let rec build_def_item seed id n t ~ids_to_inner_sorts ~ids_to_inner_types =
340  let module K = Content in
341   try
342    let sort = Hashtbl.find ids_to_inner_sorts id in
343    if sort = "Prop" then
344       `Proof (acic2content seed ?name:(name_of n) ~ids_to_inner_sorts  ~ids_to_inner_types t)
345    else 
346       `Definition
347         { K.def_name = name_of n;
348           K.def_id = gen_id seed; 
349           K.def_aref = id;
350           K.def_term = t
351         }
352   with
353    Not_found -> assert false
354
355 (* the following function must be called with an object of sort
356 Prop. For debugging purposes this is tested again, possibly raising an 
357 Not_a_proof exception *)
358
359 and acic2content seed ?name ~ids_to_inner_sorts ~ids_to_inner_types t =
360   let rec aux ?name t =
361   let module C = Cic in
362   let module K = Content in
363   let module C2A = Cic2acic in
364   let t1 =
365     match t with 
366       C.ARel (id,idref,n,b) as t ->
367         let sort = Hashtbl.find ids_to_inner_sorts id in
368         if sort = "Prop" then
369           generate_exact seed t id name ~ids_to_inner_types 
370         else raise Not_a_proof
371     | C.AVar (id,uri,exp_named_subst) as t ->
372         let sort = Hashtbl.find ids_to_inner_sorts id in
373         if sort = "Prop" then
374           generate_exact seed t id name ~ids_to_inner_types 
375         else raise Not_a_proof
376     | C.AMeta (id,n,l) as t ->
377         let sort = Hashtbl.find ids_to_inner_sorts id in
378         if sort = "Prop" then
379           generate_exact seed t id name ~ids_to_inner_types 
380         else raise Not_a_proof
381     | C.ASort (id,s) -> raise Not_a_proof
382     | C.AImplicit _ -> raise NotImplemented
383     | C.AProd (_,_,_,_) -> raise Not_a_proof
384     | C.ACast (id,v,t) -> aux v
385     | C.ALambda (id,n,s,t) -> 
386         let sort = Hashtbl.find ids_to_inner_sorts id in
387         if sort = "Prop" then 
388           let proof = aux t in
389           let proof' = 
390             if proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
391                match proof.K.proof_conclude.K.conclude_args with
392                  [K.ArgProof p] -> p
393                | _ -> assert false                  
394             else proof in
395           let proof'' =
396             { proof' with
397               K.proof_name = None;
398               K.proof_context = 
399                 (build_decl_item seed id n s ids_to_inner_sorts)::
400                   proof'.K.proof_context
401             }
402           in
403           generate_intros_let_tac seed id n s true proof'' name ~ids_to_inner_types
404         else raise Not_a_proof 
405     | C.ALetIn (id,n,s,t) ->
406         let sort = Hashtbl.find ids_to_inner_sorts id in
407         if sort = "Prop" then 
408           let proof = aux t in
409           let proof' = 
410             if proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
411                match proof.K.proof_conclude.K.conclude_args with
412                  [K.ArgProof p] -> p
413                | _ -> assert false                  
414             else proof in
415           let proof'' =
416             { proof' with
417                K.proof_name = name;
418                K.proof_context = 
419                  ((build_def_item seed id n s ids_to_inner_sorts 
420                    ids_to_inner_types):> Cic.annterm K.in_proof_context_element)
421                  ::proof'.K.proof_context;
422             }
423           in
424           generate_intros_let_tac seed id n s false proof'' name ~ids_to_inner_types
425         else raise Not_a_proof 
426     | C.AAppl (id,li) ->
427         (try rewrite 
428            seed name id li ids_to_inner_types ids_to_inner_sorts
429          with NotApplicable ->
430          try inductive 
431           seed name id li ids_to_inner_types ids_to_inner_sorts
432          with NotApplicable ->
433           let args_to_lift = 
434             List.filter (test_for_lifting ~ids_to_inner_types) li in
435           let subproofs = 
436             match args_to_lift with
437                 [_] -> List.map aux args_to_lift 
438             | _ -> List.map (aux ~name:"H") args_to_lift in
439           let args = build_args seed li subproofs 
440                  ~ids_to_inner_types ~ids_to_inner_sorts in
441             { K.proof_name = name;
442               K.proof_id   = gen_id seed;
443               K.proof_context = [];
444               K.proof_apply_context = serialize seed subproofs;
445               K.proof_conclude = 
446                 { K.conclude_id = gen_id seed;
447                   K.conclude_aref = id;
448                   K.conclude_method = "Apply";
449                   K.conclude_args = args;
450                   K.conclude_conclusion = 
451                      try Some 
452                        (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
453                      with Not_found -> None
454                  };
455             })
456     | C.AConst (id,uri,exp_named_subst) as t ->
457         let sort = Hashtbl.find ids_to_inner_sorts id in
458         if sort = "Prop" then
459           generate_exact seed t id name ~ids_to_inner_types
460         else raise Not_a_proof
461     | C.AMutInd (id,uri,i,exp_named_subst) -> raise Not_a_proof
462     | C.AMutConstruct (id,uri,i,j,exp_named_subst) as t ->
463         let sort = Hashtbl.find ids_to_inner_sorts id in
464         if sort = "Prop" then 
465           generate_exact seed t id name ~ids_to_inner_types
466         else raise Not_a_proof
467     | C.AMutCase (id,uri,typeno,ty,te,patterns) ->
468         let inductive_types =
469            (match CicEnvironment.get_obj uri with
470                Cic.Constant _ -> assert false
471              | Cic.Variable _ -> assert false
472              | Cic.CurrentProof _ -> assert false
473              | Cic.InductiveDefinition (l,_,_) -> l 
474            ) in
475         let (_,_,_,constructors) = List.nth inductive_types typeno in 
476         let teid = get_id te in
477         let pp = List.map2 
478           (fun p (name,_) -> (K.ArgProof (aux ~name p))) 
479            patterns constructors in
480         let apply_context,term =
481           (match 
482             (try Some (Hashtbl.find ids_to_inner_types teid).C2A.annsynthesized
483              with Not_found -> None)
484            with
485              Some tety -> 
486                let p = (aux te) in
487                (flat seed p, 
488                 K.Premise
489                  { K.premise_id = gen_id seed; 
490                    K.premise_xref = p.K.proof_id;
491                    K.premise_binder = p.K.proof_name;
492                    K.premise_n = None
493                  })
494            | None -> [],K.Term te) in
495         { K.proof_name = name;
496           K.proof_id   = gen_id seed;
497           K.proof_context = []; 
498           K.proof_apply_context = apply_context;
499           K.proof_conclude = 
500             { K.conclude_id = gen_id seed; 
501               K.conclude_aref = id;
502               K.conclude_method = "Case";
503               K.conclude_args = 
504                 (K.Aux (UriManager.string_of_uri uri))::
505                 (K.Aux (string_of_int typeno))::(K.Term ty)::term::pp;
506               K.conclude_conclusion = 
507                 try Some 
508                   (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
509                 with Not_found -> None  
510              }
511         }
512     | C.AFix (id, no, funs) -> 
513         let proofs = 
514           List.map 
515             (function (_,name,_,_,bo) -> `Proof (aux ~name bo)) funs in
516         let decreasing_args = 
517           List.map (function (_,_,n,_,_) -> n) funs in
518         let jo = 
519           { K.joint_id = gen_id seed;
520             K.joint_kind = `Recursive decreasing_args;
521             K.joint_defs = proofs
522           } 
523         in
524           { K.proof_name = name;
525             K.proof_id   = gen_id seed;
526             K.proof_context = [`Joint jo]; 
527             K.proof_apply_context = [];
528             K.proof_conclude = 
529               { K.conclude_id = gen_id seed; 
530                 K.conclude_aref = id;
531                 K.conclude_method = "Exact";
532                 K.conclude_args =
533                 [ K.Premise
534                   { K.premise_id = gen_id seed; 
535                     K.premise_xref = jo.K.joint_id;
536                     K.premise_binder = Some "tiralo fuori";
537                     K.premise_n = Some no;
538                   }
539                 ];
540                 K.conclude_conclusion =
541                    try Some 
542                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
543                    with Not_found -> None
544               }
545         } 
546     | C.ACoFix (id,no,funs) -> 
547         let proofs = 
548           List.map 
549             (function (_,name,_,bo) -> `Proof (aux ~name bo)) funs in
550         let jo = 
551           { K.joint_id = gen_id seed;
552             K.joint_kind = `CoRecursive;
553             K.joint_defs = proofs
554           } 
555         in
556           { K.proof_name = name;
557             K.proof_id   = gen_id seed;
558             K.proof_context = [`Joint jo]; 
559             K.proof_apply_context = [];
560             K.proof_conclude = 
561               { K.conclude_id = gen_id seed; 
562                 K.conclude_aref = id;
563                 K.conclude_method = "Exact";
564                 K.conclude_args =
565                 [ K.Premise
566                   { K.premise_id = gen_id seed; 
567                     K.premise_xref = jo.K.joint_id;
568                     K.premise_binder = Some "tiralo fuori";
569                     K.premise_n = Some no;
570                   }
571                 ];
572                 K.conclude_conclusion =
573                   try Some 
574                     (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
575                   with Not_found -> None
576               };
577         } 
578      in 
579      let id = get_id t in
580      generate_conversion seed false id t1 ~ids_to_inner_types
581 in aux ?name t
582
583 and inductive seed name id li ids_to_inner_types ids_to_inner_sorts =
584   let aux ?name = acic2content seed  ~ids_to_inner_types ~ids_to_inner_sorts in
585   let module C2A = Cic2acic in
586   let module K = Content in
587   let module C = Cic in
588   match li with 
589     C.AConst (idc,uri,exp_named_subst)::args ->
590       let uri_str = UriManager.string_of_uri uri in
591       let suffix = Str.regexp_string "_ind.con" in
592       let len = String.length uri_str in 
593       let n = (try (Str.search_backward suffix uri_str len)
594                with Not_found -> -1) in
595       if n<0 then raise NotApplicable
596       else 
597         let prefix = String.sub uri_str 0 n in
598         let ind_str = (prefix ^ ".ind") in 
599         let ind_uri = UriManager.uri_of_string ind_str in
600         let inductive_types,noparams =
601            (match CicEnvironment.get_obj ind_uri with
602                Cic.Constant _ -> assert false
603              | Cic.Variable _ -> assert false
604              | Cic.CurrentProof _ -> assert false
605              | Cic.InductiveDefinition (l,_,n) -> (l,n) 
606            ) in
607         let rec split n l =
608           if n = 0 then ([],l) else
609           let p,a = split (n-1) (List.tl l) in
610           ((List.hd l::p),a) in
611         let params_and_IP,tail_args = split (noparams+1) args in 
612         let constructors = 
613             (match inductive_types with
614               [(_,_,_,l)] -> l
615             | _ -> raise NotApplicable) (* don't care for mutual ind *) in
616         let constructors1 = 
617           let rec clean_up n t =
618              if n = 0 then t else
619              (match t with
620                 (label,Cic.Prod (_,_,t)) -> clean_up (n-1) (label,t)
621               | _ -> assert false) in
622           List.map (clean_up noparams) constructors in
623         let no_constructors= List.length constructors in
624         let args_for_cases, other_args = 
625           split no_constructors tail_args in
626         let args_to_lift = 
627           List.filter (test_for_lifting ~ids_to_inner_types) other_args in
628         let subproofs = 
629           match args_to_lift with
630             [_] -> List.map aux args_to_lift 
631           | _ -> List.map (aux ~name:"H") args_to_lift in
632         prerr_endline "****** end subproofs *******"; flush stderr;
633         let other_method_args = 
634           build_args seed other_args subproofs 
635              ~ids_to_inner_types ~ids_to_inner_sorts in
636 (*
637         let rparams,inductive_arg =
638           let rec aux =
639             function 
640               [] -> assert false            
641             | [ia] -> [],ia
642             | a::tl -> let (p,ia) = aux tl in (a::p,ia) in
643           aux other_method_args in 
644 *)
645         prerr_endline "****** end other *******"; flush stderr;
646         let method_args=
647           let rec build_method_args =
648             function
649                 [],_-> [] (* extra args are ignored ???? *)
650               | (name,ty)::tlc,arg::tla ->
651                   let idarg = get_id arg in
652                   let sortarg = 
653                     (try (Hashtbl.find ids_to_inner_sorts idarg)
654                      with Not_found -> "Type") in
655                   let hdarg = 
656                     if sortarg = "Prop" then
657                       let (co,bo) = 
658                         let rec bc = 
659                           function 
660                             Cic.Prod (_,s,t),Cic.ALambda(idl,n,s1,t1) ->
661                               let ce = 
662                                 build_decl_item 
663                                   seed idl n s1 ~ids_to_inner_sorts in
664                               if (occur ind_uri s) then
665                                 (  prerr_endline ("inductive:" ^ (UriManager.string_of_uri ind_uri) ^ (CicPp.ppterm s)); flush stderr; 
666                                    match t1 with
667                                    Cic.ALambda(id2,n2,s2,t2) ->
668                                      let inductive_hyp =
669                                        `Hypothesis
670                                          { K.dec_name = name_of n2;
671                                            K.dec_id = gen_id seed; 
672                                            K.dec_inductive = true;
673                                            K.dec_aref = id2;
674                                            K.dec_type = s2
675                                          } in
676                                      let (context,body) = bc (t,t2) in
677                                      (ce::inductive_hyp::context,body)
678                                  | _ -> assert false)
679                               else 
680                                 (  prerr_endline ("no inductive:" ^ (UriManager.string_of_uri ind_uri) ^ (CicPp.ppterm s)); flush stderr; 
681                                 let (context,body) = bc (t,t1) in
682                                 (ce::context,body))
683                             | _ , t -> ([],aux t) in
684                         bc (ty,arg) in
685                       K.ArgProof
686                        { bo with
687                          K.proof_name = Some name;
688                          K.proof_context = co; 
689                        };
690                     else (K.Term arg) in
691                   hdarg::(build_method_args (tlc,tla))
692               | _ -> assert false in
693           build_method_args (constructors1,args_for_cases) in
694           { K.proof_name = None;
695             K.proof_id   = gen_id seed;
696             K.proof_context = []; 
697             K.proof_apply_context = subproofs;
698             K.proof_conclude = 
699               { K.conclude_id = gen_id seed; 
700                 K.conclude_aref = id;
701                 K.conclude_method = "ByInduction";
702                 K.conclude_args =
703                   K.Aux (string_of_int no_constructors) 
704                   ::K.Term (C.AAppl id ((C.AConst(idc,uri,exp_named_subst))::params_and_IP))
705                   ::method_args@other_method_args;
706                 K.conclude_conclusion = 
707                    try Some 
708                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
709                    with Not_found -> None  
710               }
711           } 
712   | _ -> raise NotApplicable
713
714 and rewrite seed name id li ids_to_inner_types ids_to_inner_sorts =
715   let aux ?name = acic2content seed ~ids_to_inner_types ~ids_to_inner_sorts in
716   let module C2A = Cic2acic in
717   let module K = Content in
718   let module C = Cic in
719   match li with 
720     C.AConst (sid,uri,exp_named_subst)::args ->
721       let uri_str = UriManager.string_of_uri uri in
722       if uri_str = "cic:/Coq/Init/Logic/eq_ind.con" or
723          uri_str = "cic:/Coq/Init/Logic/eq_ind_r.con" then 
724         let subproof = aux (List.nth args 3) in
725         let method_args =
726           let rec ma_aux n = function
727               [] -> []
728             | a::tl -> 
729                 let hd = 
730                   if n = 0 then
731                     K.Premise
732                      { K.premise_id = gen_id seed;
733                        K.premise_xref = subproof.K.proof_id;
734                        K.premise_binder = None;
735                        K.premise_n = None
736                      }
737                   else 
738                     let aid = get_id a in
739                     let asort = (try (Hashtbl.find ids_to_inner_sorts aid)
740                       with Not_found -> "Type") in
741                     if asort = "Prop" then
742                       K.ArgProof (aux a)
743                     else K.Term a in
744                 hd::(ma_aux (n-1) tl) in
745           (ma_aux 3 args) in 
746           { K.proof_name = None;
747             K.proof_id   = gen_id seed;
748             K.proof_context = []; 
749             K.proof_apply_context = [subproof];
750             K.proof_conclude = 
751               { K.conclude_id = gen_id seed; 
752                 K.conclude_aref = id;
753                 K.conclude_method = "Rewrite";
754                 K.conclude_args = 
755                   K.Term (C.AConst (sid,uri,exp_named_subst))::method_args;
756                 K.conclude_conclusion = 
757                    try Some 
758                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
759                    with Not_found -> None
760               }
761           } 
762       else raise NotApplicable
763   | _ -> raise NotApplicable
764 ;; 
765
766 let map_conjectures
767  seed ~ids_to_inner_sorts ~ids_to_inner_types (id,n,context,ty)
768 =
769  let module K = Content in
770  let context' =
771   List.map
772    (function
773        (id,None) as item -> item
774      | (id,Some (name,Cic.ADecl t)) ->
775          id,
776           Some
777            (* We should call build_decl_item, but we have not computed *)
778            (* the inner-types ==> we always produce a declaration      *)
779            (`Declaration
780              { K.dec_name = name_of name;
781                K.dec_id = gen_id seed; 
782                K.dec_inductive = false;
783                K.dec_aref = get_id t;
784                K.dec_type = t
785              })
786      | (id,Some (name,Cic.ADef t)) ->
787          id,
788           Some
789            (* We should call build_def_item, but we have not computed *)
790            (* the inner-types ==> we always produce a declaration     *)
791            (`Definition
792               { K.def_name = name_of name;
793                 K.def_id = gen_id seed; 
794                 K.def_aref = get_id t;
795                 K.def_term = t
796               })
797    ) context
798  in
799   (id,n,context',ty)
800 ;;
801
802 let rec annobj2content ~ids_to_inner_sorts ~ids_to_inner_types = 
803   let module C = Cic in
804   let module K = Content in
805   let module C2A = Cic2acic in
806   let seed = ref 0 in
807   function
808       C.ACurrentProof (_,_,n,conjectures,bo,ty,params) ->
809         (gen_id seed, params,
810           Some
811            (List.map
812              (map_conjectures seed ~ids_to_inner_sorts ~ids_to_inner_types)
813              conjectures),
814           `Def (K.Const,ty,
815             build_def_item seed (get_id bo) (C.Name n) bo 
816              ~ids_to_inner_sorts ~ids_to_inner_types))
817     | C.AConstant (_,_,n,Some bo,ty,params) ->
818          (gen_id seed, params, None,
819            `Def (K.Const,ty,
820              build_def_item seed (get_id bo) (C.Name n) bo 
821                ~ids_to_inner_sorts ~ids_to_inner_types))
822     | C.AConstant (id,_,n,None,ty,params) ->
823          (gen_id seed, params, None,
824            `Decl (K.Const,
825              build_decl_item seed id (C.Name n) ty 
826                ~ids_to_inner_sorts))
827     | C.AVariable (_,n,Some bo,ty,params) ->
828          (gen_id seed, params, None,
829            `Def (K.Var,ty,
830              build_def_item seed (get_id bo) (C.Name n) bo
831                ~ids_to_inner_sorts ~ids_to_inner_types))
832     | C.AVariable (id,n,None,ty,params) ->
833          (gen_id seed, params, None,
834            `Decl (K.Var,
835              build_decl_item seed id (C.Name n) ty
836               ~ids_to_inner_sorts))
837     | C.AInductiveDefinition (id,l,params,nparams) ->
838          (gen_id seed, params, None,
839             `Joint
840               { K.joint_id = gen_id seed;
841                 K.joint_kind = `Inductive nparams;
842                 K.joint_defs = List.map (build_inductive seed) l
843               }) 
844
845 and
846     build_inductive seed = 
847      let module K = Content in
848       fun (_,n,b,ty,l) ->
849         `Inductive
850           { K.inductive_id = gen_id seed;
851             K.inductive_kind = b;
852             K.inductive_type = ty;
853             K.inductive_constructors = build_constructors seed l
854            }
855
856 and 
857     build_constructors seed l =
858      let module K = Content in
859       List.map 
860        (fun (n,t) ->
861            { K.dec_name = Some n;
862              K.dec_id = gen_id seed;
863              K.dec_inductive = false;
864              K.dec_aref = "";
865              K.dec_type = t
866            }) l
867 ;;
868    
869 (* 
870 and 'term cinductiveType = 
871  id * string * bool * 'term *                (* typename, inductive, arity *)
872    'term cconstructor list                   (*  constructors        *)
873
874 and 'term cconstructor =
875  string * 'term    
876 *)
877
878