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