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