]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_omdoc/cic2content.ml
* removed currified constructors everywhere. A bug in the ocaml compiler
[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 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 -> raise NotImplemented
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 =
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,_,_) -> l 
546            ) in
547         let (_,_,_,constructors) = List.nth inductive_types typeno in 
548         let teid = get_id te in
549         let pp = List.map2 
550           (fun p (name,_) -> (K.ArgProof (aux ~name p))) 
551            patterns constructors in
552         let context,term =
553           (match 
554              build_subproofs_and_args 
555                seed ~ids_to_inner_types ~ids_to_inner_sorts [te]
556            with
557              l,[t] -> l,t
558            | _ -> assert false) in
559         { K.proof_name = name;
560           K.proof_id   = gen_id proof_prefix seed;
561           K.proof_context = []; 
562           K.proof_apply_context = serialize seed context;
563           K.proof_conclude = 
564             { K.conclude_id = gen_id conclude_prefix seed; 
565               K.conclude_aref = id;
566               K.conclude_method = "Case";
567               K.conclude_args = 
568                 (K.Aux (UriManager.string_of_uri uri))::
569                 (K.Aux (string_of_int typeno))::(K.Term ty)::term::pp;
570               K.conclude_conclusion = 
571                 try Some 
572                   (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
573                 with Not_found -> None  
574              }
575         }
576     | C.AFix (id, no, funs) -> 
577         let proofs = 
578           List.map 
579             (function (_,name,_,_,bo) -> `Proof (aux ~name bo)) funs in
580         let decreasing_args = 
581           List.map (function (_,_,n,_,_) -> n) funs in
582         let jo = 
583           { K.joint_id = gen_id joint_prefix seed;
584             K.joint_kind = `Recursive decreasing_args;
585             K.joint_defs = proofs
586           } 
587         in
588           { K.proof_name = name;
589             K.proof_id  = gen_id proof_prefix seed;
590             K.proof_context = [`Joint jo]; 
591             K.proof_apply_context = [];
592             K.proof_conclude = 
593               { K.conclude_id = gen_id conclude_prefix seed; 
594                 K.conclude_aref = id;
595                 K.conclude_method = "Exact";
596                 K.conclude_args =
597                 [ K.Premise
598                   { K.premise_id = gen_id premise_prefix seed; 
599                     K.premise_xref = jo.K.joint_id;
600                     K.premise_binder = Some "tiralo fuori";
601                     K.premise_n = Some no;
602                   }
603                 ];
604                 K.conclude_conclusion =
605                    try Some 
606                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
607                    with Not_found -> None
608               }
609         } 
610     | C.ACoFix (id,no,funs) -> 
611         let proofs = 
612           List.map 
613             (function (_,name,_,bo) -> `Proof (aux ~name bo)) funs in
614         let jo = 
615           { K.joint_id = gen_id joint_prefix seed;
616             K.joint_kind = `CoRecursive;
617             K.joint_defs = proofs
618           } 
619         in
620           { K.proof_name = name;
621             K.proof_id   = gen_id proof_prefix seed;
622             K.proof_context = [`Joint jo]; 
623             K.proof_apply_context = [];
624             K.proof_conclude = 
625               { K.conclude_id = gen_id conclude_prefix seed; 
626                 K.conclude_aref = id;
627                 K.conclude_method = "Exact";
628                 K.conclude_args =
629                 [ K.Premise
630                   { K.premise_id = gen_id premise_prefix seed; 
631                     K.premise_xref = jo.K.joint_id;
632                     K.premise_binder = Some "tiralo fuori";
633                     K.premise_n = Some no;
634                   }
635                 ];
636                 K.conclude_conclusion =
637                   try Some 
638                     (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
639                   with Not_found -> None
640               };
641         } 
642      in 
643      let id = get_id t in
644      generate_conversion seed false id t1 ~ids_to_inner_types
645 in aux ?name t
646
647 and inductive seed name id li ~ids_to_inner_types ~ids_to_inner_sorts =
648   let aux ?name = acic2content seed  ~ids_to_inner_types ~ids_to_inner_sorts in
649   let module C2A = Cic2acic in
650   let module K = Content in
651   let module C = Cic in
652   match li with 
653     C.AConst (idc,uri,exp_named_subst)::args ->
654       let uri_str = UriManager.string_of_uri uri in
655       let suffix = Str.regexp_string "_ind.con" in
656       let len = String.length uri_str in 
657       let n = (try (Str.search_backward suffix uri_str len)
658                with Not_found -> -1) in
659       if n<0 then raise NotApplicable
660       else 
661         let method_name =
662           if (uri_str = "cic:/Coq/Init/Logic_Type/exT_ind.con" or
663               uri_str = "cic:/Coq/Init/Logic/ex_ind.con") then "Exists"
664           else if uri_str = "cic:/Coq/Init/Logic/and_ind.con" then "AndInd"
665           else if uri_str = "cic:/Coq/Init/Logic/False_ind.con" then "FalseInd"
666           else "ByInduction" in
667         let prefix = String.sub uri_str 0 n in
668         let ind_str = (prefix ^ ".ind") in 
669         let ind_uri = UriManager.uri_of_string ind_str in
670         let inductive_types,noparams =
671            (match CicEnvironment.get_obj ind_uri with
672                Cic.Constant _ -> assert false
673              | Cic.Variable _ -> assert false
674              | Cic.CurrentProof _ -> assert false
675              | Cic.InductiveDefinition (l,_,n) -> (l,n) 
676            ) in
677         let rec split n l =
678           if n = 0 then ([],l) else
679           let p,a = split (n-1) (List.tl l) in
680           ((List.hd l::p),a) in
681         let params_and_IP,tail_args = split (noparams+1) args in
682         let constructors = 
683             (match inductive_types with
684               [(_,_,_,l)] -> l
685             | _ -> raise NotApplicable) (* don't care for mutual ind *) in
686         let constructors1 = 
687           let rec clean_up n t =
688              if n = 0 then t else
689              (match t with
690                 (label,Cic.Prod (_,_,t)) -> clean_up (n-1) (label,t)
691               | _ -> assert false) in
692           List.map (clean_up noparams) constructors in
693         let no_constructors= List.length constructors in
694         let args_for_cases, other_args = 
695           split no_constructors tail_args in
696         let subproofs,other_method_args =
697           build_subproofs_and_args seed other_args
698              ~ids_to_inner_types ~ids_to_inner_sorts in
699         let method_args=
700           let rec build_method_args =
701             function
702                 [],_-> [] (* extra args are ignored ???? *)
703               | (name,ty)::tlc,arg::tla ->
704                   let idarg = get_id arg in
705                   let sortarg = 
706                     (try (Hashtbl.find ids_to_inner_sorts idarg)
707                      with Not_found -> "Type") in
708                   let hdarg = 
709                     if sortarg = "Prop" then
710                       let (co,bo) = 
711                         let rec bc = 
712                           function 
713                             Cic.Prod (_,s,t),Cic.ALambda(idl,n,s1,t1) ->
714                               let ce = 
715                                 build_decl_item 
716                                   seed idl n s1 ~ids_to_inner_sorts in
717                               if (occur ind_uri s) then
718                                 ( match t1 with
719                                    Cic.ALambda(id2,n2,s2,t2) ->
720                                      let inductive_hyp =
721                                        `Hypothesis
722                                          { K.dec_name = name_of n2;
723                                            K.dec_id =
724                                             gen_id declaration_prefix seed; 
725                                            K.dec_inductive = true;
726                                            K.dec_aref = id2;
727                                            K.dec_type = s2
728                                          } in
729                                      let (context,body) = bc (t,t2) in
730                                      (ce::inductive_hyp::context,body)
731                                  | _ -> assert false)
732                               else 
733                                 ( 
734                                 let (context,body) = bc (t,t1) in
735                                 (ce::context,body))
736                             | _ , t -> ([],aux t) in
737                         bc (ty,arg) in
738                       K.ArgProof
739                        { bo with
740                          K.proof_name = Some name;
741                          K.proof_context = co; 
742                        };
743                     else (K.Term arg) in
744                   hdarg::(build_method_args (tlc,tla))
745               | _ -> assert false in
746           build_method_args (constructors1,args_for_cases) in
747           { K.proof_name = name;
748             K.proof_id   = gen_id proof_prefix seed;
749             K.proof_context = []; 
750             K.proof_apply_context = serialize seed subproofs;
751             K.proof_conclude = 
752               { K.conclude_id = gen_id conclude_prefix seed; 
753                 K.conclude_aref = id;
754                 K.conclude_method = method_name;
755                 K.conclude_args =
756                   K.Aux (string_of_int no_constructors) 
757                   ::K.Term (C.AAppl(id,((C.AConst(idc,uri,exp_named_subst))::params_and_IP)))
758                   ::method_args@other_method_args;
759                 K.conclude_conclusion = 
760                    try Some 
761                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
762                    with Not_found -> None  
763               }
764           } 
765   | _ -> raise NotApplicable
766
767 and rewrite seed name id li ~ids_to_inner_types ~ids_to_inner_sorts =
768   let aux ?name = acic2content seed ~ids_to_inner_types ~ids_to_inner_sorts in
769   let module C2A = Cic2acic in
770   let module K = Content in
771   let module C = Cic in
772   match li with 
773     C.AConst (sid,uri,exp_named_subst)::args ->
774       let uri_str = UriManager.string_of_uri uri in
775       if uri_str = "cic:/Coq/Init/Logic/eq_ind.con" or
776          uri_str = "cic:/Coq/Init/Logic/eq_ind_r.con" then 
777         let subproofs,arg = 
778           (match 
779              build_subproofs_and_args 
780                seed ~ids_to_inner_types ~ids_to_inner_sorts [List.nth args 3]
781            with 
782              l,[p] -> l,p
783            | _,_ -> assert false) in 
784         let method_args =
785           let rec ma_aux n = function
786               [] -> []
787             | a::tl -> 
788                 let hd = 
789                   if n = 0 then arg
790                   else 
791                     let aid = get_id a in
792                     let asort = (try (Hashtbl.find ids_to_inner_sorts aid)
793                       with Not_found -> "Type") in
794                     if asort = "Prop" then
795                       K.ArgProof (aux a)
796                     else K.Term a in
797                 hd::(ma_aux (n-1) tl) in
798           (ma_aux 3 args) in 
799           { K.proof_name = name;
800             K.proof_id  = gen_id proof_prefix seed;
801             K.proof_context = []; 
802             K.proof_apply_context = serialize seed subproofs;
803             K.proof_conclude = 
804               { K.conclude_id = gen_id conclude_prefix seed; 
805                 K.conclude_aref = id;
806                 K.conclude_method = "Rewrite";
807                 K.conclude_args = 
808                   K.Term (C.AConst (sid,uri,exp_named_subst))::method_args;
809                 K.conclude_conclusion = 
810                    try Some 
811                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
812                    with Not_found -> None
813               }
814           } 
815       else raise NotApplicable
816   | _ -> raise NotApplicable
817 ;; 
818
819 let map_conjectures
820  seed ~ids_to_inner_sorts ~ids_to_inner_types (id,n,context,ty)
821 =
822  let module K = Content in
823  let context' =
824   List.map
825    (function
826        (id,None) -> None
827      | (id,Some (name,Cic.ADecl t)) ->
828          Some
829           (* We should call build_decl_item, but we have not computed *)
830           (* the inner-types ==> we always produce a declaration      *)
831           (`Declaration
832             { K.dec_name = name_of name;
833               K.dec_id = gen_id declaration_prefix seed; 
834               K.dec_inductive = false;
835               K.dec_aref = get_id t;
836               K.dec_type = t
837             })
838      | (id,Some (name,Cic.ADef t)) ->
839          Some
840           (* We should call build_def_item, but we have not computed *)
841           (* the inner-types ==> we always produce a declaration     *)
842           (`Definition
843              { K.def_name = name_of name;
844                K.def_id = gen_id definition_prefix seed; 
845                K.def_aref = get_id t;
846                K.def_term = t
847              })
848    ) context
849  in
850   (id,n,context',ty)
851 ;;
852
853 let rec annobj2content ~ids_to_inner_sorts ~ids_to_inner_types = 
854   let module C = Cic in
855   let module K = Content in
856   let module C2A = Cic2acic in
857   let seed = ref 0 in
858   function
859       C.ACurrentProof (_,_,n,conjectures,bo,ty,params) ->
860         (gen_id object_prefix seed, params,
861           Some
862            (List.map
863              (map_conjectures seed ~ids_to_inner_sorts ~ids_to_inner_types)
864              conjectures),
865           `Def (K.Const,ty,
866             build_def_item seed (get_id bo) (C.Name n) bo 
867              ~ids_to_inner_sorts ~ids_to_inner_types))
868     | C.AConstant (_,_,n,Some bo,ty,params) ->
869          (gen_id object_prefix seed, params, None,
870            `Def (K.Const,ty,
871              build_def_item seed (get_id bo) (C.Name n) bo 
872                ~ids_to_inner_sorts ~ids_to_inner_types))
873     | C.AConstant (id,_,n,None,ty,params) ->
874          (gen_id object_prefix seed, params, None,
875            `Decl (K.Const,
876              build_decl_item seed id (C.Name n) ty 
877                ~ids_to_inner_sorts))
878     | C.AVariable (_,n,Some bo,ty,params) ->
879          (gen_id object_prefix seed, params, None,
880            `Def (K.Var,ty,
881              build_def_item seed (get_id bo) (C.Name n) bo
882                ~ids_to_inner_sorts ~ids_to_inner_types))
883     | C.AVariable (id,n,None,ty,params) ->
884          (gen_id object_prefix seed, params, None,
885            `Decl (K.Var,
886              build_decl_item seed id (C.Name n) ty
887               ~ids_to_inner_sorts))
888     | C.AInductiveDefinition (id,l,params,nparams) ->
889          (gen_id object_prefix seed, params, None,
890             `Joint
891               { K.joint_id = gen_id joint_prefix seed;
892                 K.joint_kind = `Inductive nparams;
893                 K.joint_defs = List.map (build_inductive seed) l
894               }) 
895
896 and
897     build_inductive seed = 
898      let module K = Content in
899       fun (_,n,b,ty,l) ->
900         `Inductive
901           { K.inductive_id = gen_id inductive_prefix seed;
902             K.inductive_kind = b;
903             K.inductive_type = ty;
904             K.inductive_constructors = build_constructors seed l
905            }
906
907 and 
908     build_constructors seed l =
909      let module K = Content in
910       List.map 
911        (fun (n,t) ->
912            { K.dec_name = Some n;
913              K.dec_id = gen_id declaration_prefix seed;
914              K.dec_inductive = false;
915              K.dec_aref = "";
916              K.dec_type = t
917            }) l
918 ;;
919    
920 (* 
921 and 'term cinductiveType = 
922  id * string * bool * 'term *                (* typename, inductive, arity *)
923    'term cconstructor list                   (*  constructors        *)
924
925 and 'term cconstructor =
926  string * 'term    
927 *)
928
929