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