]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_omdoc/cic2content.ml
added annotations to Cic.Implicit
[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                       (match CicEnvironment.get_obj uri with
372                          Cic.Constant _ -> assert false
373                        | Cic.Variable _ -> assert false
374                        | Cic.CurrentProof _ -> assert false
375                        | Cic.InductiveDefinition (l,_,_) -> l 
376                       ) in
377                     let (_,_,_,constructors) = 
378                       List.nth inductive_types tyno in 
379                     let name,_ = List.nth constructors (consno - 1) in
380                     K.Lemma 
381                       { K.lemma_id = gen_id lemma_prefix seed;
382                         K.lemma_name = name;
383                         K.lemma_uri = 
384                           UriManager.string_of_uri uri ^ "#xpointer(1/" ^
385                           string_of_int (tyno+1) ^ "/" ^ string_of_int consno ^
386                           ")"
387                       }
388                  else (K.Term t) 
389              | _ -> (K.Term t)) in
390           subproofs,hd::args
391   in 
392   match (aux l) with
393     [p],args -> 
394       [{p with K.proof_name = None}], 
395         List.map 
396           (function 
397               K.Premise prem when prem.K.premise_xref = p.K.proof_id ->
398                K.Premise {prem with K.premise_binder = None}
399             | i -> i) args
400   | p,a as c -> c
401
402 and
403
404 build_def_item seed id n t ~ids_to_inner_sorts ~ids_to_inner_types =
405  let module K = Content in
406   try
407    let sort = Hashtbl.find ids_to_inner_sorts id in
408    if sort = "Prop" then
409        (let p = 
410         (acic2content seed ?name:(name_of n) ~ids_to_inner_sorts  ~ids_to_inner_types t)
411        in 
412         `Proof p;)
413    else 
414       `Definition
415         { K.def_name = name_of n;
416           K.def_id = gen_id definition_prefix seed; 
417           K.def_aref = id;
418           K.def_term = t
419         }
420   with
421    Not_found -> assert false
422
423 (* the following function must be called with an object of sort
424 Prop. For debugging purposes this is tested again, possibly raising an 
425 Not_a_proof exception *)
426
427 and acic2content seed ?name ~ids_to_inner_sorts ~ids_to_inner_types t =
428   let rec aux ?name t =
429   let module C = Cic in
430   let module K = Content in
431   let module C2A = Cic2acic in
432   let t1 =
433     match t with 
434       C.ARel (id,idref,n,b) as t ->
435         let sort = Hashtbl.find ids_to_inner_sorts id in
436         if sort = "Prop" then
437           generate_exact seed t id name ~ids_to_inner_types 
438         else raise Not_a_proof
439     | C.AVar (id,uri,exp_named_subst) as t ->
440         let sort = Hashtbl.find ids_to_inner_sorts id in
441         if sort = "Prop" then
442           generate_exact seed t id name ~ids_to_inner_types 
443         else raise Not_a_proof
444     | C.AMeta (id,n,l) as t ->
445         let sort = Hashtbl.find ids_to_inner_sorts id in
446         if sort = "Prop" then
447           generate_exact seed t id name ~ids_to_inner_types 
448         else raise Not_a_proof
449     | C.ASort (id,s) -> raise Not_a_proof
450     | C.AImplicit _ -> raise NotImplemented
451     | C.AProd (_,_,_,_) -> raise Not_a_proof
452     | C.ACast (id,v,t) -> aux v
453     | C.ALambda (id,n,s,t) -> 
454         let sort = Hashtbl.find ids_to_inner_sorts id in
455         if sort = "Prop" then 
456           let proof = aux t in
457           let proof' = 
458             if proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
459                match proof.K.proof_conclude.K.conclude_args with
460                  [K.ArgProof p] -> p
461                | _ -> assert false                  
462             else proof in
463           let proof'' =
464             { proof' with
465               K.proof_name = None;
466               K.proof_context = 
467                 (build_decl_item seed id n s ids_to_inner_sorts)::
468                   proof'.K.proof_context
469             }
470           in
471           generate_intros_let_tac seed id n s true proof'' name ~ids_to_inner_types
472         else raise Not_a_proof 
473     | C.ALetIn (id,n,s,t) ->
474         let sort = Hashtbl.find ids_to_inner_sorts id in
475         if sort = "Prop" then
476           let proof = aux t in
477           let proof' = 
478             if proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
479                match proof.K.proof_conclude.K.conclude_args with
480                  [K.ArgProof p] -> p
481                | _ -> assert false                  
482             else proof in
483           let proof'' =
484             { proof' with
485                K.proof_name = None;
486                K.proof_context = 
487                  ((build_def_item seed id n s ids_to_inner_sorts 
488                    ids_to_inner_types):> Cic.annterm K.in_proof_context_element)
489                  ::proof'.K.proof_context;
490             }
491           in
492           generate_intros_let_tac seed id n s false proof'' name ~ids_to_inner_types
493         else raise Not_a_proof 
494     | C.AAppl (id,li) ->
495         (try rewrite 
496            seed name id li ~ids_to_inner_types ~ids_to_inner_sorts
497          with NotApplicable ->
498          try inductive 
499           seed name id li ~ids_to_inner_types ~ids_to_inner_sorts
500          with NotApplicable ->
501           let subproofs, args =
502             build_subproofs_and_args 
503               seed li ~ids_to_inner_types ~ids_to_inner_sorts in
504 (*            
505           let args_to_lift = 
506             List.filter (test_for_lifting ~ids_to_inner_types) li in
507           let subproofs = 
508             match args_to_lift with
509                 [_] -> List.map aux args_to_lift 
510             | _ -> List.map (aux ~name:"H") args_to_lift in
511           let args = build_args seed li subproofs 
512                  ~ids_to_inner_types ~ids_to_inner_sorts in *)
513             { K.proof_name = name;
514               K.proof_id   = gen_id proof_prefix seed;
515               K.proof_context = [];
516               K.proof_apply_context = serialize seed subproofs;
517               K.proof_conclude = 
518                 { K.conclude_id = gen_id conclude_prefix seed;
519                   K.conclude_aref = id;
520                   K.conclude_method = "Apply";
521                   K.conclude_args = args;
522                   K.conclude_conclusion = 
523                      try Some 
524                        (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
525                      with Not_found -> None
526                  };
527             })
528     | C.AConst (id,uri,exp_named_subst) as t ->
529         let sort = Hashtbl.find ids_to_inner_sorts id in
530         if sort = "Prop" then
531           generate_exact seed t id name ~ids_to_inner_types
532         else raise Not_a_proof
533     | C.AMutInd (id,uri,i,exp_named_subst) -> raise Not_a_proof
534     | C.AMutConstruct (id,uri,i,j,exp_named_subst) as t ->
535         let sort = Hashtbl.find ids_to_inner_sorts id in
536         if sort = "Prop" then 
537           generate_exact seed t id name ~ids_to_inner_types
538         else raise Not_a_proof
539     | C.AMutCase (id,uri,typeno,ty,te,patterns) ->
540         let inductive_types,noparams =
541            (match CicEnvironment.get_obj uri with
542                Cic.Constant _ -> assert false
543              | Cic.Variable _ -> assert false
544              | Cic.CurrentProof _ -> assert false
545              | Cic.InductiveDefinition (l,_,n) -> l,n 
546            ) in
547         let (_,_,_,constructors) = List.nth inductive_types typeno in
548         let name_and_arities = 
549           let rec count_prods =
550             function 
551                C.Prod (_,_,t) -> 1 + count_prods t
552              | _ -> 0 in
553           List.map 
554             (function (n,t) -> Some n,((count_prods t) - noparams)) constructors in
555         let pp = 
556           let build_proof p (name,arity) =
557             let rec make_context_and_body c p n =
558               if n = 0 then c,(aux p)
559               else 
560                 (match p with
561                    Cic.ALambda(idl,vname,s1,t1) ->
562                      let ce = 
563                        build_decl_item seed idl vname s1 ~ids_to_inner_sorts in
564                      make_context_and_body (ce::c) t1 (n-1)
565                    | _ -> assert false) in
566              let context,body = make_context_and_body [] p arity in
567                K.ArgProof
568                 {body with K.proof_name = name; K.proof_context=context} in
569           List.map2 build_proof patterns name_and_arities in
570         let teid = get_id te in
571         let context,term =
572           (match 
573              build_subproofs_and_args 
574                seed ~ids_to_inner_types ~ids_to_inner_sorts [te]
575            with
576              l,[t] -> l,t
577            | _ -> assert false) in
578         { K.proof_name = name;
579           K.proof_id   = gen_id proof_prefix seed;
580           K.proof_context = []; 
581           K.proof_apply_context = serialize seed context;
582           K.proof_conclude = 
583             { K.conclude_id = gen_id conclude_prefix seed; 
584               K.conclude_aref = id;
585               K.conclude_method = "Case";
586               K.conclude_args = 
587                 (K.Aux (UriManager.string_of_uri uri))::
588                 (K.Aux (string_of_int typeno))::(K.Term ty)::term::pp;
589               K.conclude_conclusion = 
590                 try Some 
591                   (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
592                 with Not_found -> None  
593              }
594         }
595     | C.AFix (id, no, funs) -> 
596         let proofs = 
597           List.map 
598             (function (_,name,_,_,bo) -> `Proof (aux ~name bo)) funs in
599         let decreasing_args = 
600           List.map (function (_,_,n,_,_) -> n) funs in
601         let jo = 
602           { K.joint_id = gen_id joint_prefix seed;
603             K.joint_kind = `Recursive decreasing_args;
604             K.joint_defs = proofs
605           } 
606         in
607           { K.proof_name = name;
608             K.proof_id  = gen_id proof_prefix seed;
609             K.proof_context = [`Joint jo]; 
610             K.proof_apply_context = [];
611             K.proof_conclude = 
612               { K.conclude_id = gen_id conclude_prefix seed; 
613                 K.conclude_aref = id;
614                 K.conclude_method = "Exact";
615                 K.conclude_args =
616                 [ K.Premise
617                   { K.premise_id = gen_id premise_prefix seed; 
618                     K.premise_xref = jo.K.joint_id;
619                     K.premise_binder = Some "tiralo fuori";
620                     K.premise_n = Some no;
621                   }
622                 ];
623                 K.conclude_conclusion =
624                    try Some 
625                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
626                    with Not_found -> None
627               }
628         } 
629     | C.ACoFix (id,no,funs) -> 
630         let proofs = 
631           List.map 
632             (function (_,name,_,bo) -> `Proof (aux ~name bo)) funs in
633         let jo = 
634           { K.joint_id = gen_id joint_prefix seed;
635             K.joint_kind = `CoRecursive;
636             K.joint_defs = proofs
637           } 
638         in
639           { K.proof_name = name;
640             K.proof_id   = gen_id proof_prefix seed;
641             K.proof_context = [`Joint jo]; 
642             K.proof_apply_context = [];
643             K.proof_conclude = 
644               { K.conclude_id = gen_id conclude_prefix seed; 
645                 K.conclude_aref = id;
646                 K.conclude_method = "Exact";
647                 K.conclude_args =
648                 [ K.Premise
649                   { K.premise_id = gen_id premise_prefix seed; 
650                     K.premise_xref = jo.K.joint_id;
651                     K.premise_binder = Some "tiralo fuori";
652                     K.premise_n = Some no;
653                   }
654                 ];
655                 K.conclude_conclusion =
656                   try Some 
657                     (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
658                   with Not_found -> None
659               };
660         } 
661      in 
662      let id = get_id t in
663      generate_conversion seed false id t1 ~ids_to_inner_types
664 in aux ?name t
665
666 and inductive seed name id li ~ids_to_inner_types ~ids_to_inner_sorts =
667   let aux ?name = acic2content seed  ~ids_to_inner_types ~ids_to_inner_sorts in
668   let module C2A = Cic2acic in
669   let module K = Content in
670   let module C = Cic in
671   match li with 
672     C.AConst (idc,uri,exp_named_subst)::args ->
673       let uri_str = UriManager.string_of_uri uri in
674       let suffix = Str.regexp_string "_ind.con" in
675       let len = String.length uri_str in 
676       let n = (try (Str.search_backward suffix uri_str len)
677                with Not_found -> -1) in
678       if n<0 then raise NotApplicable
679       else 
680         let method_name =
681           if (uri_str = "cic:/Coq/Init/Logic_Type/exT_ind.con" or
682               uri_str = "cic:/Coq/Init/Logic/ex_ind.con") then "Exists"
683           else if uri_str = "cic:/Coq/Init/Logic/and_ind.con" then "AndInd"
684           else if uri_str = "cic:/Coq/Init/Logic/False_ind.con" then "FalseInd"
685           else "ByInduction" in
686         let prefix = String.sub uri_str 0 n in
687         let ind_str = (prefix ^ ".ind") in 
688         let ind_uri = UriManager.uri_of_string ind_str in
689         let inductive_types,noparams =
690            (match CicEnvironment.get_obj ind_uri with
691                Cic.Constant _ -> assert false
692              | Cic.Variable _ -> assert false
693              | Cic.CurrentProof _ -> assert false
694              | Cic.InductiveDefinition (l,_,n) -> (l,n) 
695            ) in
696         let rec split n l =
697           if n = 0 then ([],l) else
698           let p,a = split (n-1) (List.tl l) in
699           ((List.hd l::p),a) in
700         let params_and_IP,tail_args = split (noparams+1) args in
701         let constructors = 
702             (match inductive_types with
703               [(_,_,_,l)] -> l
704             | _ -> raise NotApplicable) (* don't care for mutual ind *) in
705         let constructors1 = 
706           let rec clean_up n t =
707              if n = 0 then t else
708              (match t with
709                 (label,Cic.Prod (_,_,t)) -> clean_up (n-1) (label,t)
710               | _ -> assert false) in
711           List.map (clean_up noparams) constructors in
712         let no_constructors= List.length constructors in
713         let args_for_cases, other_args = 
714           split no_constructors tail_args in
715         let subproofs,other_method_args =
716           build_subproofs_and_args seed other_args
717              ~ids_to_inner_types ~ids_to_inner_sorts in
718         let method_args=
719           let rec build_method_args =
720             function
721                 [],_-> [] (* extra args are ignored ???? *)
722               | (name,ty)::tlc,arg::tla ->
723                   let idarg = get_id arg in
724                   let sortarg = 
725                     (try (Hashtbl.find ids_to_inner_sorts idarg)
726                      with Not_found -> "Type") in
727                   let hdarg = 
728                     if sortarg = "Prop" then
729                       let (co,bo) = 
730                         let rec bc = 
731                           function 
732                             Cic.Prod (_,s,t),Cic.ALambda(idl,n,s1,t1) ->
733                               let ce = 
734                                 build_decl_item 
735                                   seed idl n s1 ~ids_to_inner_sorts in
736                               if (occur ind_uri s) then
737                                 ( match t1 with
738                                    Cic.ALambda(id2,n2,s2,t2) ->
739                                      let inductive_hyp =
740                                        `Hypothesis
741                                          { K.dec_name = name_of n2;
742                                            K.dec_id =
743                                             gen_id declaration_prefix seed; 
744                                            K.dec_inductive = true;
745                                            K.dec_aref = id2;
746                                            K.dec_type = s2
747                                          } in
748                                      let (context,body) = bc (t,t2) in
749                                      (ce::inductive_hyp::context,body)
750                                  | _ -> assert false)
751                               else 
752                                 ( 
753                                 let (context,body) = bc (t,t1) in
754                                 (ce::context,body))
755                             | _ , t -> ([],aux t) in
756                         bc (ty,arg) in
757                       K.ArgProof
758                        { bo with
759                          K.proof_name = Some name;
760                          K.proof_context = co; 
761                        };
762                     else (K.Term arg) in
763                   hdarg::(build_method_args (tlc,tla))
764               | _ -> assert false in
765           build_method_args (constructors1,args_for_cases) in
766           { K.proof_name = name;
767             K.proof_id   = gen_id proof_prefix seed;
768             K.proof_context = []; 
769             K.proof_apply_context = serialize seed subproofs;
770             K.proof_conclude = 
771               { K.conclude_id = gen_id conclude_prefix seed; 
772                 K.conclude_aref = id;
773                 K.conclude_method = method_name;
774                 K.conclude_args =
775                   K.Aux (string_of_int no_constructors) 
776                   ::K.Term (C.AAppl(id,((C.AConst(idc,uri,exp_named_subst))::params_and_IP)))
777                   ::method_args@other_method_args;
778                 K.conclude_conclusion = 
779                    try Some 
780                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
781                    with Not_found -> None  
782               }
783           } 
784   | _ -> raise NotApplicable
785
786 and rewrite seed name id li ~ids_to_inner_types ~ids_to_inner_sorts =
787   let aux ?name = acic2content seed ~ids_to_inner_types ~ids_to_inner_sorts in
788   let module C2A = Cic2acic in
789   let module K = Content in
790   let module C = Cic in
791   match li with 
792     C.AConst (sid,uri,exp_named_subst)::args ->
793       let uri_str = UriManager.string_of_uri uri in
794       if uri_str = "cic:/Coq/Init/Logic/eq_ind.con" or
795          uri_str = "cic:/Coq/Init/Logic/eq_ind_r.con" then 
796         let subproofs,arg = 
797           (match 
798              build_subproofs_and_args 
799                seed ~ids_to_inner_types ~ids_to_inner_sorts [List.nth args 3]
800            with 
801              l,[p] -> l,p
802            | _,_ -> assert false) in 
803         let method_args =
804           let rec ma_aux n = function
805               [] -> []
806             | a::tl -> 
807                 let hd = 
808                   if n = 0 then arg
809                   else 
810                     let aid = get_id a in
811                     let asort = (try (Hashtbl.find ids_to_inner_sorts aid)
812                       with Not_found -> "Type") in
813                     if asort = "Prop" then
814                       K.ArgProof (aux a)
815                     else K.Term a in
816                 hd::(ma_aux (n-1) tl) in
817           (ma_aux 3 args) in 
818           { K.proof_name = name;
819             K.proof_id  = gen_id proof_prefix seed;
820             K.proof_context = []; 
821             K.proof_apply_context = serialize seed subproofs;
822             K.proof_conclude = 
823               { K.conclude_id = gen_id conclude_prefix seed; 
824                 K.conclude_aref = id;
825                 K.conclude_method = "Rewrite";
826                 K.conclude_args = 
827                   K.Term (C.AConst (sid,uri,exp_named_subst))::method_args;
828                 K.conclude_conclusion = 
829                    try Some 
830                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
831                    with Not_found -> None
832               }
833           } 
834       else raise NotApplicable
835   | _ -> raise NotApplicable
836 ;; 
837
838 let map_conjectures
839  seed ~ids_to_inner_sorts ~ids_to_inner_types (id,n,context,ty)
840 =
841  let module K = Content in
842  let context' =
843   List.map
844    (function
845        (id,None) -> None
846      | (id,Some (name,Cic.ADecl t)) ->
847          Some
848           (* We should call build_decl_item, but we have not computed *)
849           (* the inner-types ==> we always produce a declaration      *)
850           (`Declaration
851             { K.dec_name = name_of name;
852               K.dec_id = gen_id declaration_prefix seed; 
853               K.dec_inductive = false;
854               K.dec_aref = get_id t;
855               K.dec_type = t
856             })
857      | (id,Some (name,Cic.ADef t)) ->
858          Some
859           (* We should call build_def_item, but we have not computed *)
860           (* the inner-types ==> we always produce a declaration     *)
861           (`Definition
862              { K.def_name = name_of name;
863                K.def_id = gen_id definition_prefix seed; 
864                K.def_aref = get_id t;
865                K.def_term = t
866              })
867    ) context
868  in
869   (id,n,context',ty)
870 ;;
871
872 (* map_sequent is similar to map_conjectures, but the for the hid
873 of the hypothesis, which are preserved instead of generating
874 fresh ones. We shall have to adopt a uniform policy, soon or later *)
875
876 let map_sequent ((id,n,context,ty):Cic.annconjecture) =
877  let module K = Content in
878  let context' =
879   List.map
880    (function
881        (id,None) -> None
882      | (id,Some (name,Cic.ADecl t)) ->
883          Some
884           (* We should call build_decl_item, but we have not computed *)
885           (* the inner-types ==> we always produce a declaration      *)
886           (`Declaration
887             { K.dec_name = name_of name;
888               K.dec_id = id; 
889               K.dec_inductive = false;
890               K.dec_aref = get_id t;
891               K.dec_type = t
892             })
893      | (id,Some (name,Cic.ADef t)) ->
894          Some
895           (* We should call build_def_item, but we have not computed *)
896           (* the inner-types ==> we always produce a declaration     *)
897           (`Definition
898              { K.def_name = name_of name;
899                K.def_id = id; 
900                K.def_aref = get_id t;
901                K.def_term = t
902              })
903    ) context
904  in
905   (id,n,context',ty)
906 ;;
907
908 let rec annobj2content ~ids_to_inner_sorts ~ids_to_inner_types = 
909   let module C = Cic in
910   let module K = Content in
911   let module C2A = Cic2acic in
912   let seed = ref 0 in
913   function
914       C.ACurrentProof (_,_,n,conjectures,bo,ty,params) ->
915         (gen_id object_prefix seed, params,
916           Some
917            (List.map
918              (map_conjectures seed ~ids_to_inner_sorts ~ids_to_inner_types)
919              conjectures),
920           `Def (K.Const,ty,
921             build_def_item seed (get_id bo) (C.Name n) bo 
922              ~ids_to_inner_sorts ~ids_to_inner_types))
923     | C.AConstant (_,_,n,Some bo,ty,params) ->
924          (gen_id object_prefix seed, params, None,
925            `Def (K.Const,ty,
926              build_def_item seed (get_id bo) (C.Name n) bo 
927                ~ids_to_inner_sorts ~ids_to_inner_types))
928     | C.AConstant (id,_,n,None,ty,params) ->
929          (gen_id object_prefix seed, params, None,
930            `Decl (K.Const,
931              build_decl_item seed id (C.Name n) ty 
932                ~ids_to_inner_sorts))
933     | C.AVariable (_,n,Some bo,ty,params) ->
934          (gen_id object_prefix seed, params, None,
935            `Def (K.Var,ty,
936              build_def_item seed (get_id bo) (C.Name n) bo
937                ~ids_to_inner_sorts ~ids_to_inner_types))
938     | C.AVariable (id,n,None,ty,params) ->
939          (gen_id object_prefix seed, params, None,
940            `Decl (K.Var,
941              build_decl_item seed id (C.Name n) ty
942               ~ids_to_inner_sorts))
943     | C.AInductiveDefinition (id,l,params,nparams) ->
944          (gen_id object_prefix seed, params, None,
945             `Joint
946               { K.joint_id = gen_id joint_prefix seed;
947                 K.joint_kind = `Inductive nparams;
948                 K.joint_defs = List.map (build_inductive seed) l
949               }) 
950
951 and
952     build_inductive seed = 
953      let module K = Content in
954       fun (_,n,b,ty,l) ->
955         `Inductive
956           { K.inductive_id = gen_id inductive_prefix seed;
957             K.inductive_kind = b;
958             K.inductive_type = ty;
959             K.inductive_constructors = build_constructors seed l
960            }
961
962 and 
963     build_constructors seed l =
964      let module K = Content in
965       List.map 
966        (fun (n,t) ->
967            { K.dec_name = Some n;
968              K.dec_id = gen_id declaration_prefix seed;
969              K.dec_inductive = false;
970              K.dec_aref = "";
971              K.dec_type = t
972            }) l
973 ;;
974    
975 (* 
976 and 'term cinductiveType = 
977  id * string * bool * 'term *                (* typename, inductive, arity *)
978    'term cconstructor list                   (*  constructors        *)
979
980 and 'term cconstructor =
981  string * 'term    
982 *)
983
984