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