]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_omdoc/cic2content.ml
** UNTESTED **
[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 = 
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,_,_) -> false
109     | C.AMeta (id,_,_) -> false
110     | C.ASort (id,_) -> false
111     | C.AImplicit _ -> raise NotImplemented
112     | C.AProd (id,_,_,_) -> false
113     | C.ACast (id,_,_) -> 
114          (try 
115             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
116             true;
117           with Not_found -> false)
118     | C.ALambda (id,_,_,_) -> 
119          (try 
120             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
121             true;
122           with Not_found -> false)
123     | C.ALetIn (id,_,_,_) -> 
124          (try 
125             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
126             true;
127           with Not_found -> false)
128     | C.AAppl (id,_) ->
129          (try 
130             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
131             true;
132           with Not_found -> false) 
133     | C.AConst (id,_,_) -> 
134          (try 
135             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
136             true;
137           with Not_found -> false) 
138     | C.AMutInd (id,_,_,_) -> false
139     | C.AMutConstruct (id,_,_,_,_) -> 
140        (try 
141             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
142             true;
143           with Not_found -> false)
144         (* oppure: false *)
145     | C.AMutCase (id,_,_,_,_,_) ->
146          (try 
147             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
148             true;
149           with Not_found -> false)
150     | C.AFix (id,_,_) ->
151           (try 
152             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
153             true;
154           with Not_found -> false)
155     | C.ACoFix (id,_,_) ->
156          (try 
157             ignore (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized;
158             true;
159           with Not_found -> false)
160 ;;
161
162 let build_args seed l subproofs ~ids_to_inner_types ~ids_to_inner_sorts =
163   let module C = Cic in
164   let module K = Content in
165   let rec aux l subrpoofs =
166     match l with
167       [] -> []
168     | t::l1 -> 
169         if (test_for_lifting t ~ids_to_inner_types) then
170           (match subproofs with
171              [] -> assert false
172            | p::tl -> 
173               let new_arg = 
174                 K.Premise
175                   { K.premise_id = gen_id seed;
176                     K.premise_xref = p.K.proof_id;
177                     K.premise_binder = p.K.proof_name;
178                     K.premise_n = None
179                   }
180                 in new_arg::(aux l1 tl))
181         else 
182           let hd = 
183             (match t with 
184                C.ARel (idr,idref,n,b) ->
185                  let sort = 
186                    (try Hashtbl.find ids_to_inner_sorts idr 
187                     with Not_found -> "Type") in 
188                  if sort ="Prop" then 
189                     K.Premise 
190                       { K.premise_id = gen_id seed;
191                         K.premise_xref = idr;
192                         K.premise_binder = Some b;
193                         K.premise_n = Some n
194                       }
195                  else (K.Term t)
196              | _ -> (K.Term t)) in 
197           hd::(aux l1 subproofs)
198   in aux l subproofs
199 ;;
200
201 (* transform a proof p into a proof list, concatenating the last 
202 conclude element to the apply_context list, in case context is
203 empty. Otherwise, it just returns [p] *)
204
205 let flat seed p = 
206  let module K = Content in
207   if (p.K.proof_context = []) then
208     if p.K.proof_apply_context = [] then [p]
209     else 
210       let p1 =
211         { p with
212           K.proof_id = gen_id seed;
213           K.proof_context = []; 
214           K.proof_apply_context = []
215         } in
216       p.K.proof_apply_context@[p1]
217   else 
218     [p]
219 ;;
220
221 let rec serialize seed = 
222   function 
223       [] -> []
224     | p::tl -> (flat seed p)@(serialize seed tl);;
225
226 (* top_down = true if the term is a LAMBDA or a decl *)
227 let generate_conversion seed top_down id inner_proof ~ids_to_inner_types =
228  let module C2A = Cic2acic in
229  let module K = Content in
230  let exp = (try ((Hashtbl.find ids_to_inner_types id).C2A.annexpected)
231             with Not_found -> None)
232  in
233  match exp with
234      None -> inner_proof
235    | Some expty ->
236        if inner_proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
237          { K.proof_name = None ;
238             K.proof_id   = gen_id seed;
239             K.proof_context = [] ;
240             K.proof_apply_context = [];
241             K.proof_conclude = 
242               { K.conclude_id = gen_id seed; 
243                 K.conclude_aref = id;
244                 K.conclude_method = "TD_Conversion";
245                 K.conclude_args = [K.ArgProof inner_proof];
246                 K.conclude_conclusion = Some expty
247               };
248           }
249         else
250           { K.proof_name = None ;
251             K.proof_id   = gen_id seed;
252             K.proof_context = [] ;
253             K.proof_apply_context = [inner_proof];
254             K.proof_conclude = 
255               { K.conclude_id = gen_id seed; 
256                 K.conclude_aref = id;
257                 K.conclude_method = "BU_Conversion";
258                 K.conclude_args =  
259                  [K.Premise 
260                   { K.premise_id = gen_id seed;
261                     K.premise_xref = inner_proof.K.proof_id; 
262                     K.premise_binder = None;
263                     K.premise_n = None
264                   } 
265                  ]; 
266                 K.conclude_conclusion = Some expty
267               };
268           }
269 ;;
270
271 let generate_exact seed t id name ~ids_to_inner_types =
272   let module C2A = Cic2acic in
273   let module K = Content in
274     { K.proof_name = name;
275       K.proof_id   = id ;
276       K.proof_context = [] ;
277       K.proof_apply_context = [];
278       K.proof_conclude = 
279         { K.conclude_id = gen_id seed; 
280           K.conclude_aref = id;
281           K.conclude_method = "Exact";
282           K.conclude_args = [K.Term t];
283           K.conclude_conclusion = 
284               try Some (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
285               with Not_found -> None
286         };
287     }
288 ;;
289
290 let generate_intros_let_tac seed id n s is_intro inner_proof name ~ids_to_inner_types =
291   let module C2A = Cic2acic in
292   let module C = Cic in
293   let module K = Content in
294     { K.proof_name = name;
295       K.proof_id   = id ;
296       K.proof_context = [] ;
297       K.proof_apply_context = [];
298       K.proof_conclude = 
299         { K.conclude_id = gen_id seed; 
300           K.conclude_aref = id;
301           K.conclude_method = "Intros+LetTac";
302           K.conclude_args = [K.ArgProof inner_proof];
303           K.conclude_conclusion = 
304             try Some 
305              (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
306             with Not_found -> 
307               (match inner_proof.K.proof_conclude.K.conclude_conclusion with
308                  None -> None
309               | Some t -> 
310                   if is_intro then Some (C.AProd ("gen"^id,n,s,t))
311                   else Some (C.ALetIn ("gen"^id,n,s,t)))
312         };
313     }
314 ;;
315
316 let build_decl_item seed id n s ~ids_to_inner_sorts =
317  let module K = Content in
318   try
319    let sort = Hashtbl.find ids_to_inner_sorts (Cic2acic.source_id_of_id id) in
320    if sort = "Prop" then
321       `Hypothesis
322         { K.dec_name = name_of n;
323           K.dec_id = gen_id seed; 
324           K.dec_inductive = false;
325           K.dec_aref = id;
326           K.dec_type = s
327         }
328    else 
329       `Declaration
330         { K.dec_name = name_of n;
331           K.dec_id = gen_id seed; 
332           K.dec_inductive = false;
333           K.dec_aref = id;
334           K.dec_type = s
335         }
336   with
337    Not_found -> assert false
338 ;;
339
340 let rec build_def_item seed id n t ~ids_to_inner_sorts ~ids_to_inner_types =
341  let module K = Content in
342   try
343    let sort = Hashtbl.find ids_to_inner_sorts id in
344    if sort = "Prop" then
345       `Proof (acic2content seed ?name:(name_of n) ~ids_to_inner_sorts  ~ids_to_inner_types t)
346    else 
347       `Definition
348         { K.def_name = name_of n;
349           K.def_id = gen_id seed; 
350           K.def_aref = id;
351           K.def_term = t
352         }
353   with
354    Not_found -> assert false
355
356 (* the following function must be called with an object of sort
357 Prop. For debugging purposes this is tested again, possibly raising an 
358 Not_a_proof exception *)
359
360 and acic2content seed ?name ~ids_to_inner_sorts ~ids_to_inner_types t =
361   let rec aux ?name t =
362   let module C = Cic in
363   let module K = Content in
364   let module C2A = Cic2acic in
365   let t1 =
366     match t with 
367       C.ARel (id,idref,n,b) as t ->
368         let sort = Hashtbl.find ids_to_inner_sorts id in
369         if sort = "Prop" then
370           generate_exact seed t id name ~ids_to_inner_types 
371         else raise Not_a_proof
372     | C.AVar (id,uri,exp_named_subst) as t ->
373         let sort = Hashtbl.find ids_to_inner_sorts id in
374         if sort = "Prop" then
375           generate_exact seed t id name ~ids_to_inner_types 
376         else raise Not_a_proof
377     | C.AMeta (id,n,l) as t ->
378         let sort = Hashtbl.find ids_to_inner_sorts id in
379         if sort = "Prop" then
380           generate_exact seed t id name ~ids_to_inner_types 
381         else raise Not_a_proof
382     | C.ASort (id,s) -> raise Not_a_proof
383     | C.AImplicit _ -> raise NotImplemented
384     | C.AProd (_,_,_,_) -> raise Not_a_proof
385     | C.ACast (id,v,t) -> aux v
386     | C.ALambda (id,n,s,t) -> 
387         let sort = Hashtbl.find ids_to_inner_sorts id in
388         if sort = "Prop" then 
389           let proof = aux t in
390           let proof' = 
391             if proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
392                match proof.K.proof_conclude.K.conclude_args with
393                  [K.ArgProof p] -> p
394                | _ -> assert false                  
395             else proof in
396           let proof'' =
397             { proof' with
398               K.proof_name = None;
399               K.proof_context = 
400                 (build_decl_item seed id n s ids_to_inner_sorts)::
401                   proof'.K.proof_context
402             }
403           in
404           generate_intros_let_tac seed id n s true proof'' name ~ids_to_inner_types
405         else raise Not_a_proof 
406     | C.ALetIn (id,n,s,t) ->
407         let sort = Hashtbl.find ids_to_inner_sorts id in
408         if sort = "Prop" then 
409           let proof = aux t in
410           let proof' = 
411             if proof.K.proof_conclude.K.conclude_method = "Intros+LetTac" then
412                match proof.K.proof_conclude.K.conclude_args with
413                  [K.ArgProof p] -> p
414                | _ -> assert false                  
415             else proof in
416           let proof'' =
417             { proof' with
418                K.proof_name = name;
419                K.proof_context = 
420                  ((build_def_item seed id n s ids_to_inner_sorts 
421                    ids_to_inner_types):> Cic.annterm K.in_proof_context_element)
422                  ::proof'.K.proof_context;
423             }
424           in
425           generate_intros_let_tac seed id n s false proof'' name ~ids_to_inner_types
426         else raise Not_a_proof 
427     | C.AAppl (id,li) ->
428         (try rewrite 
429            seed name id li ids_to_inner_types ids_to_inner_sorts
430          with NotApplicable ->
431          try inductive 
432           seed name id li ids_to_inner_types ids_to_inner_sorts
433          with NotApplicable ->
434           let args_to_lift = 
435             List.filter (test_for_lifting ~ids_to_inner_types) li in
436           let subproofs = 
437             match args_to_lift with
438                 [_] -> List.map aux args_to_lift 
439             | _ -> List.map (aux ~name:"H") args_to_lift in
440           let args = build_args seed li subproofs 
441                  ~ids_to_inner_types ~ids_to_inner_sorts in
442             { K.proof_name = name;
443               K.proof_id   = gen_id seed;
444               K.proof_context = [];
445               K.proof_apply_context = serialize seed subproofs;
446               K.proof_conclude = 
447                 { K.conclude_id = gen_id seed;
448                   K.conclude_aref = id;
449                   K.conclude_method = "Apply";
450                   K.conclude_args = args;
451                   K.conclude_conclusion = 
452                      try Some 
453                        (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
454                      with Not_found -> None
455                  };
456             })
457     | C.AConst (id,uri,exp_named_subst) as t ->
458         let sort = Hashtbl.find ids_to_inner_sorts id in
459         if sort = "Prop" then
460           generate_exact seed t id name ~ids_to_inner_types
461         else raise Not_a_proof
462     | C.AMutInd (id,uri,i,exp_named_subst) -> raise Not_a_proof
463     | C.AMutConstruct (id,uri,i,j,exp_named_subst) as t ->
464         let sort = Hashtbl.find ids_to_inner_sorts id in
465         if sort = "Prop" then 
466           generate_exact seed t id name ~ids_to_inner_types
467         else raise Not_a_proof
468     | C.AMutCase (id,uri,typeno,ty,te,patterns) ->
469         let teid = get_id te in
470         let pp = List.map (function p -> (K.ArgProof (aux p))) patterns in
471         (match 
472           (try Some (Hashtbl.find ids_to_inner_types teid).C2A.annsynthesized
473            with Not_found -> None)
474          with
475              Some tety -> (* we must lift up the argument *)
476                let p = (aux te) in
477                { K.proof_name = Some "name";
478                  K.proof_id   = gen_id seed;
479                  K.proof_context = []; 
480                  K.proof_apply_context = flat seed p;
481                  K.proof_conclude = 
482                    { K.conclude_id = gen_id seed; 
483                      K.conclude_aref = id;
484                      K.conclude_method = "Case";
485                      K.conclude_args = (K.Term ty)::(K.Term te)::pp;
486                      K.conclude_conclusion = 
487                        try Some 
488                         (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
489                        with Not_found -> None  
490                    }
491                }
492            | None ->
493                { K.proof_name = name;
494                  K.proof_id   = gen_id seed;
495                  K.proof_context = []; 
496                  K.proof_apply_context = [];
497                  K.proof_conclude = 
498                    { K.conclude_id = gen_id seed; 
499                      K.conclude_aref = id;
500                      K.conclude_method = "Case";
501                      K.conclude_args = (K.Term ty)::(K.Term te)::pp;
502                      K.conclude_conclusion = 
503                        try Some 
504                         (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
505                        with Not_found -> None 
506                    }
507                }
508          )  
509     | C.AFix (id, no, funs) -> 
510         let proofs = 
511           List.map 
512             (function (_,name,_,_,bo) -> `Proof (aux ~name bo)) funs in
513         let decreasing_args = 
514           List.map (function (_,_,n,_,_) -> n) funs in
515         let jo = 
516           { K.joint_id = gen_id seed;
517             K.joint_kind = `Recursive decreasing_args;
518             K.joint_defs = proofs
519           } 
520         in
521           { K.proof_name = name;
522             K.proof_id   = gen_id seed;
523             K.proof_context = [`Joint jo]; 
524             K.proof_apply_context = [];
525             K.proof_conclude = 
526               { K.conclude_id = gen_id seed; 
527                 K.conclude_aref = id;
528                 K.conclude_method = "Exact";
529                 K.conclude_args =
530                 [ K.Premise
531                   { K.premise_id = gen_id seed; 
532                     K.premise_xref = jo.K.joint_id;
533                     K.premise_binder = Some "tiralo fuori";
534                     K.premise_n = Some no;
535                   }
536                 ];
537                 K.conclude_conclusion =
538                    try Some 
539                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
540                    with Not_found -> None
541               }
542         } 
543     | C.ACoFix (id,no,funs) -> 
544         let proofs = 
545           List.map 
546             (function (_,name,_,bo) -> `Proof (aux ~name bo)) funs in
547         let jo = 
548           { K.joint_id = gen_id seed;
549             K.joint_kind = `CoRecursive;
550             K.joint_defs = proofs
551           } 
552         in
553           { K.proof_name = name;
554             K.proof_id   = gen_id seed;
555             K.proof_context = [`Joint jo]; 
556             K.proof_apply_context = [];
557             K.proof_conclude = 
558               { K.conclude_id = gen_id seed; 
559                 K.conclude_aref = id;
560                 K.conclude_method = "Exact";
561                 K.conclude_args =
562                 [ K.Premise
563                   { K.premise_id = gen_id seed; 
564                     K.premise_xref = jo.K.joint_id;
565                     K.premise_binder = Some "tiralo fuori";
566                     K.premise_n = Some no;
567                   }
568                 ];
569                 K.conclude_conclusion =
570                   try Some 
571                     (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
572                   with Not_found -> None
573               };
574         } 
575      in 
576      let id = get_id t in
577      generate_conversion seed false id t1 ~ids_to_inner_types
578 in aux ?name t
579
580 and inductive seed name id li ids_to_inner_types ids_to_inner_sorts =
581   let aux ?name = acic2content seed  ~ids_to_inner_types ~ids_to_inner_sorts in
582   let module C2A = Cic2acic in
583   let module K = Content in
584   let module C = Cic in
585   match li with 
586     C.AConst (idc,uri,exp_named_subst)::args ->
587       let uri_str = UriManager.string_of_uri uri in
588       let suffix = Str.regexp_string "_ind.con" in
589       let len = String.length uri_str in 
590       let n = (try (Str.search_backward suffix uri_str len)
591                with Not_found -> -1) in
592       if n<0 then raise NotApplicable
593       else 
594         let prefix = String.sub uri_str 0 n in
595         let ind_str = (prefix ^ ".ind") in 
596         let ind_uri = UriManager.uri_of_string ind_str in
597         let inductive_types,noparams =
598            (match CicEnvironment.get_obj ind_uri with
599                Cic.Constant _ -> assert false
600              | Cic.Variable _ -> assert false
601              | Cic.CurrentProof _ -> assert false
602              | Cic.InductiveDefinition (l,_,n) -> (l,n) 
603            ) in
604         let rec split n l =
605           if n = 0 then ([],l) else
606           let p,a = split (n-1) (List.tl l) in
607           ((List.hd l::p),a) in
608         let params_and_IP,tail_args = split (noparams+1) args in 
609         let constructors = 
610             (match inductive_types with
611               [(_,_,_,l)] -> l
612             | _ -> raise NotApplicable) (* don't care for mutual ind *) in
613         let constructors1 = 
614           let rec clean_up n t =
615              if n = 0 then t else
616              (match t with
617                 (label,Cic.Prod (_,_,t)) -> clean_up (n-1) (label,t)
618               | _ -> assert false) in
619           List.map (clean_up noparams) constructors in
620         let no_constructors= List.length constructors in
621         let args_for_cases, other_args = 
622           split no_constructors tail_args in
623         let args_to_lift = 
624           List.filter (test_for_lifting ~ids_to_inner_types) other_args in
625         let subproofs = 
626           match args_to_lift with
627             [_] -> List.map aux args_to_lift 
628           | _ -> List.map (aux ~name:"H") args_to_lift in
629         prerr_endline "****** end subproofs *******"; flush stderr;
630         let other_method_args = 
631           build_args seed other_args subproofs 
632              ~ids_to_inner_types ~ids_to_inner_sorts in
633 (*
634         let rparams,inductive_arg =
635           let rec aux =
636             function 
637               [] -> assert false            
638             | [ia] -> [],ia
639             | a::tl -> let (p,ia) = aux tl in (a::p,ia) in
640           aux other_method_args in 
641 *)
642         prerr_endline "****** end other *******"; flush stderr;
643         let method_args=
644           let rec build_method_args =
645             function
646                 [],_-> [] (* extra args are ignored ???? *)
647               | (name,ty)::tlc,arg::tla ->
648                   let idarg = get_id arg in
649                   let sortarg = 
650                     (try (Hashtbl.find ids_to_inner_sorts idarg)
651                      with Not_found -> "Type") in
652                   let hdarg = 
653                     if sortarg = "Prop" then
654                       let (co,bo) = 
655                         let rec bc = 
656                           function 
657                             Cic.Prod (_,s,t),Cic.ALambda(idl,n,s1,t1) ->
658                               let ce = 
659                                 build_decl_item 
660                                   seed idl n s1 ~ids_to_inner_sorts in
661                               if (occur ind_uri s) then
662                                 (  prerr_endline ("inductive:" ^ (UriManager.string_of_uri ind_uri) ^ (CicPp.ppterm s)); flush stderr; 
663                                    match t1 with
664                                    Cic.ALambda(id2,n2,s2,t2) ->
665                                      let inductive_hyp =
666                                        `Hypothesis
667                                          { K.dec_name = name_of n2;
668                                            K.dec_id = gen_id seed; 
669                                            K.dec_inductive = true;
670                                            K.dec_aref = id2;
671                                            K.dec_type = s2
672                                          } in
673                                      let (context,body) = bc (t,t2) in
674                                      (ce::inductive_hyp::context,body)
675                                  | _ -> assert false)
676                               else 
677                                 (  prerr_endline ("no inductive:" ^ (UriManager.string_of_uri ind_uri) ^ (CicPp.ppterm s)); flush stderr; 
678                                 let (context,body) = bc (t,t1) in
679                                 (ce::context,body))
680                             | _ , t -> ([],aux t) in
681                         bc (ty,arg) in
682                       K.ArgProof
683                        { bo with
684                          K.proof_name = Some name;
685                          K.proof_context = co; 
686                        };
687                     else (K.Term arg) in
688                   hdarg::(build_method_args (tlc,tla))
689               | _ -> assert false in
690           build_method_args (constructors1,args_for_cases) in
691           { K.proof_name = None;
692             K.proof_id   = gen_id seed;
693             K.proof_context = []; 
694             K.proof_apply_context = subproofs;
695             K.proof_conclude = 
696               { K.conclude_id = gen_id seed; 
697                 K.conclude_aref = id;
698                 K.conclude_method = "ByInduction";
699                 K.conclude_args =
700                   K.Aux no_constructors 
701                   ::K.Term (C.AAppl id ((C.AConst(idc,uri,exp_named_subst))::params_and_IP))
702                   ::method_args@other_method_args;
703                 K.conclude_conclusion = 
704                    try Some 
705                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
706                    with Not_found -> None  
707               }
708           } 
709   | _ -> raise NotApplicable
710
711 and rewrite seed name id li ids_to_inner_types ids_to_inner_sorts =
712   let aux ?name = acic2content seed ~ids_to_inner_types ~ids_to_inner_sorts in
713   let module C2A = Cic2acic in
714   let module K = Content in
715   let module C = Cic in
716   match li with 
717     C.AConst (sid,uri,exp_named_subst)::args ->
718       let uri_str = UriManager.string_of_uri uri in
719       if uri_str = "cic:/Coq/Init/Logic/eq_ind.con" or
720          uri_str = "cic:/Coq/Init/Logic/eq_ind_r.con" then 
721         let subproof = aux (List.nth args 3) in
722         let method_args =
723           let rec ma_aux n = function
724               [] -> []
725             | a::tl -> 
726                 let hd = 
727                   if n = 0 then
728                     K.Premise
729                      { K.premise_id = gen_id seed;
730                        K.premise_xref = subproof.K.proof_id;
731                        K.premise_binder = None;
732                        K.premise_n = None
733                      }
734                   else 
735                     let aid = get_id a in
736                     let asort = (try (Hashtbl.find ids_to_inner_sorts aid)
737                       with Not_found -> "Type") in
738                     if asort = "Prop" then
739                       K.ArgProof (aux a)
740                     else K.Term a in
741                 hd::(ma_aux (n-1) tl) in
742           (ma_aux 3 args) in 
743           { K.proof_name = None;
744             K.proof_id   = gen_id seed;
745             K.proof_context = []; 
746             K.proof_apply_context = [subproof];
747             K.proof_conclude = 
748               { K.conclude_id = gen_id seed; 
749                 K.conclude_aref = id;
750                 K.conclude_method = "Rewrite";
751                 K.conclude_args = 
752                   K.Term (C.AConst (sid,uri,exp_named_subst))::method_args;
753                 K.conclude_conclusion = 
754                    try Some 
755                      (Hashtbl.find ids_to_inner_types id).C2A.annsynthesized
756                    with Not_found -> None
757               }
758           } 
759       else raise NotApplicable
760   | _ -> raise NotApplicable
761 ;; 
762
763 let map_conjectures
764  seed ~ids_to_inner_sorts ~ids_to_inner_types (id,n,context,ty)
765 =
766  let module K = Content in
767  let context' =
768   List.map
769    (function
770        (id,None) as item -> item
771      | (id,Some (name,Cic.ADecl t)) ->
772          id,
773           Some
774            (* We should call build_decl_item, but we have not computed *)
775            (* the inner-types ==> we always produce a declaration      *)
776            (`Declaration
777              { K.dec_name = name_of name;
778                K.dec_id = gen_id seed; 
779                K.dec_inductive = false;
780                K.dec_aref = get_id t;
781                K.dec_type = t
782              })
783      | (id,Some (name,Cic.ADef t)) ->
784          id,
785           Some
786            (* We should call build_def_item, but we have not computed *)
787            (* the inner-types ==> we always produce a declaration     *)
788            (`Definition
789               { K.def_name = name_of name;
790                 K.def_id = gen_id seed; 
791                 K.def_aref = get_id t;
792                 K.def_term = t
793               })
794    ) context
795  in
796   (id,n,context',ty)
797 ;;
798
799 let rec annobj2content ~ids_to_inner_sorts ~ids_to_inner_types = 
800   let module C = Cic in
801   let module K = Content in
802   let module C2A = Cic2acic in
803   let seed = ref 0 in
804   function
805       C.ACurrentProof (_,_,n,conjectures,bo,ty,params) ->
806         (gen_id seed, params,
807           Some
808            (List.map
809              (map_conjectures seed ~ids_to_inner_sorts ~ids_to_inner_types)
810              conjectures),
811           `Def (K.Const,ty,
812             build_def_item seed (get_id bo) (C.Name n) bo 
813              ~ids_to_inner_sorts ~ids_to_inner_types))
814     | C.AConstant (_,_,n,Some bo,ty,params) ->
815          (gen_id seed, params, None,
816            `Def (K.Const,ty,
817              build_def_item seed (get_id bo) (C.Name n) bo 
818                ~ids_to_inner_sorts ~ids_to_inner_types))
819     | C.AConstant (id,_,n,None,ty,params) ->
820          (gen_id seed, params, None,
821            `Decl (K.Const,
822              build_decl_item seed id (C.Name n) ty 
823                ~ids_to_inner_sorts))
824     | C.AVariable (_,n,Some bo,ty,params) ->
825          (gen_id seed, params, None,
826            `Def (K.Var,ty,
827              build_def_item seed (get_id bo) (C.Name n) bo
828                ~ids_to_inner_sorts ~ids_to_inner_types))
829     | C.AVariable (id,n,None,ty,params) ->
830          (gen_id seed, params, None,
831            `Decl (K.Var,
832              build_decl_item seed id (C.Name n) ty
833               ~ids_to_inner_sorts))
834     | C.AInductiveDefinition (id,l,params,nparams) ->
835          (gen_id seed, params, None,
836             `Joint
837               { K.joint_id = gen_id seed;
838                 K.joint_kind = `Inductive nparams;
839                 K.joint_defs = List.map (build_inductive seed) l
840               }) 
841
842 and
843     build_inductive seed = 
844      let module K = Content in
845       fun (_,n,b,ty,l) ->
846         `Inductive
847           { K.inductive_id = gen_id seed;
848             K.inductive_kind = b;
849             K.inductive_type = ty;
850             K.inductive_constructors = build_constructors seed l
851            }
852
853 and 
854     build_constructors seed l =
855      let module K = Content in
856       List.map 
857        (fun (n,t) ->
858            { K.dec_name = Some n;
859              K.dec_id = gen_id seed;
860              K.dec_inductive = false;
861              K.dec_aref = "";
862              K.dec_type = t
863            }) l
864 ;;
865    
866 (* 
867 and 'term cinductiveType = 
868  id * string * bool * 'term *                (* typename, inductive, arity *)
869    'term cconstructor list                   (*  constructors        *)
870
871 and 'term cconstructor =
872  string * 'term    
873 *)
874
875