]> matita.cs.unibo.it Git - helm.git/blob - components/cic_acic/cic2acic.ml
CSC & Andrea patch to speedup the process: typeof called instead of Hashtbl lookup
[helm.git] / components / cic_acic / cic2acic.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 (* $Id$ *)
27
28 type sort_kind = [ `Prop | `Set | `Type of CicUniv.universe | `CProp ]
29
30 let string_of_sort = function
31   | `Prop -> "Prop"
32   | `Set -> "Set"
33   | `Type u -> "Type:" ^ string_of_int (CicUniv.univno u)
34   | `CProp -> "CProp"
35
36 let sort_of_sort = function
37   | Cic.Prop  -> `Prop
38   | Cic.Set   -> `Set
39   | Cic.Type u -> `Type u
40   | Cic.CProp -> `CProp
41
42 (* let hashtbl_add_time = ref 0.0;; *)
43
44 let xxx_add_profiler = HExtlib.profile "xxx_add";;
45 let xxx_add h k v =
46  xxx_add_profiler.HExtlib.profile (Hashtbl.add h k) v
47 ;;
48
49 let xxx_type_of_aux' m c t =
50  let res,_ =
51    try
52     CicTypeChecker.type_of_aux' m c t CicUniv.empty_ugraph
53    with
54    | CicTypeChecker.AssertFailure _
55    | CicTypeChecker.TypeCheckerFailure _ ->
56        Cic.Sort Cic.Prop, CicUniv.empty_ugraph
57   in
58  res
59 ;;
60
61 let xxx_type_of_aux'_profiler = HExtlib.profile "xxx_type_of_aux'";;
62 let xxx_type_of_aux' m c t =
63  xxx_type_of_aux'_profiler.HExtlib.profile (xxx_type_of_aux' m c) t
64
65 type anntypes =
66  {annsynthesized : Cic.annterm ; annexpected : Cic.annterm option}
67 ;;
68
69 let gen_id seed =
70  let res = "i" ^ string_of_int !seed in
71   incr seed ;
72   res
73 ;;
74
75 let fresh_id seed ids_to_terms ids_to_father_ids =
76  fun father t ->
77   let res = gen_id seed in
78    xxx_add ids_to_father_ids res father ;
79    xxx_add ids_to_terms res t ;
80    res
81 ;;
82
83 let source_id_of_id id = "#source#" ^ id;;
84
85 exception NotEnoughElements;;
86
87 (*CSC: cut&paste da cicPp.ml *)
88 (* get_nth l n   returns the nth element of the list l if it exists or *)
89 (* raises NotEnoughElements if l has less than n elements             *)
90 let rec get_nth l n =
91  match (n,l) with
92     (1, he::_) -> he
93   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
94   | (_,_) -> raise NotEnoughElements
95 ;;
96
97
98 let profiler_for_find = HExtlib.profile "CicHash" ;;
99 let profiler_for_whd = HExtlib.profile "whd" ;;
100
101 let cic_CicHash_find a b =  
102   profiler_for_find.HExtlib.profile (Cic.CicHash.find a) b
103 ;;
104
105 let cicReduction_whd c t = 
106  profiler_for_whd.HExtlib.profile (CicReduction.whd c) t
107 ;;
108
109 let acic_of_cic_context' ~computeinnertypes:global_computeinnertypes
110   seed ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
111   metasenv context idrefs t expectedty
112 =
113  let module D = DoubleTypeInference in
114  let module C = Cic in
115   let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
116 (*    let time1 = Sys.time () in *)
117    let terms_to_types =
118 (*
119      let time0 = Sys.time () in
120      let prova = CicTypeChecker.type_of_aux' metasenv context t in
121      let time1 = Sys.time () in
122      prerr_endline ("*** Fine type_inference:" ^ (string_of_float (time1 -. time0)));
123      let res = D.double_type_of metasenv context t expectedty in
124      let time2 = Sys.time () in
125    prerr_endline ("*** Fine double_type_inference:" ^ (string_of_float (time2 -. time1)));
126      res 
127 *)
128     if global_computeinnertypes then
129      D.double_type_of metasenv context t expectedty
130     else
131      Cic.CicHash.create 1 (* empty table *)
132    in
133 (*
134    let time2 = Sys.time () in
135    prerr_endline
136     ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
137 *)
138     let time = ref 0. in
139     let rec aux computeinnertypes father context idrefs tt =
140      let fresh_id'' = fresh_id' father tt in
141      (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
142       (* First of all we compute the inner type and the inner sort *)
143       (* of the term. They may be useful in what follows.          *)
144       (*CSC: This is a very inefficient way of computing inner types *)
145       (*CSC: and inner sorts: very deep terms have their types/sorts *)
146       (*CSC: computed again and again.                               *)
147       let sort_of t =
148        match cicReduction_whd context t with 
149           C.Sort C.Prop  -> `Prop
150         | C.Sort C.Set   -> `Set
151         | C.Sort (C.Type u) -> `Type u
152         | C.Meta _       -> `Type (CicUniv.fresh())
153         | C.Sort C.CProp -> `CProp
154         | t              ->
155             prerr_endline ("Cic2acic.sort_of applied to: " ^ CicPp.ppterm t) ;
156             assert false
157       in
158        let ainnertypes,innertype,innersort,expected_available =
159
160 (*CSC: Here we need the algorithm for Coscoy's double type-inference  *)
161 (*CSC: (expected type + inferred type). Just for now we use the usual *)
162 (*CSC: type-inference, but the result is very poor. As a very weak    *)
163 (*CSC: patch, I apply whd to the computed type. Full beta             *)
164 (*CSC: reduction would be a much better option.                       *)
165 (*CSC: solo per testare i tempi *)
166 (*XXXXXXX *)
167         try
168 (* *)
169         let {D.synthesized = synthesized; D.expected = expected} =
170          if computeinnertypes then
171           cic_CicHash_find terms_to_types tt
172          else
173           (* We are already in an inner-type and Coscoy's double *)
174           (* type inference algorithm has not been applied.      *)
175           { D.synthesized =
176 (***CSC: patch per provare i tempi
177             CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
178             (*if global_computeinnertypes then
179               Cic.Sort (Cic.Type (CicUniv.fresh()))
180             else*)
181               cicReduction_whd context (xxx_type_of_aux' metasenv context tt);
182           D.expected = None}
183         in
184 (*          incr number_new_type_of_aux' ; *)
185          let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
186           let ainnertypes,expected_available =
187            if computeinnertypes then
188             let annexpected,expected_available =
189                match expected with
190                   None -> None,false
191                 | Some expectedty' ->
192                    Some
193                     (aux false (Some fresh_id'') context idrefs expectedty'),
194                     true
195             in
196              Some
197               {annsynthesized =
198                 aux false (Some fresh_id'') context idrefs synthesized ;
199                annexpected = annexpected
200               }, expected_available
201            else
202             None,false
203           in
204            ainnertypes,synthesized, sort_of innersort, expected_available
205 (*XXXXXXXX *)
206         with
207          Not_found ->  (* l'inner-type non e' nella tabella ==> sort <> Prop *)
208           (* CSC: Type or Set? I can not tell *)
209           let u = CicUniv.fresh() in
210           None,Cic.Sort (Cic.Type u),`Type u,false 
211           (* TASSI non dovrebbe fare danni *)
212 (* *)
213        in
214         let aux' =
215          if innersort = `Prop then
216           aux computeinnertypes (Some fresh_id'')
217          else
218           aux false (Some fresh_id'')
219         in
220         let add_inner_type id =
221          match ainnertypes with
222             None -> ()
223           | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
224         in
225          match tt with
226             C.Rel n ->
227              let id =
228               match get_nth context n with
229                  (Some (C.Name s,_)) -> s
230                | _ -> "__" ^ string_of_int n
231              in
232               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
233               if innersort = `Prop  && expected_available then
234                add_inner_type fresh_id'' ;
235               C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
236           | C.Var (uri,exp_named_subst) ->
237              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
238              if innersort = `Prop  && expected_available then
239               add_inner_type fresh_id'' ;
240              let exp_named_subst' =
241               List.map
242                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
243              in
244               C.AVar (fresh_id'', uri,exp_named_subst')
245           | C.Meta (n,l) ->
246              let (_,canonical_context,_) = CicUtil.lookup_meta n metasenv in
247              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
248              if innersort = `Prop  && expected_available then
249               add_inner_type fresh_id'' ;
250              C.AMeta (fresh_id'', n,
251               (List.map2
252                 (fun ct t ->
253                   match (ct, t) with
254                   | None, _ -> None
255                   | _, Some t -> Some (aux' context idrefs t)
256                   | Some _, None -> assert false (* due to typing rules *))
257                 canonical_context l))
258           | C.Sort s -> C.ASort (fresh_id'', s)
259           | C.Implicit annotation -> C.AImplicit (fresh_id'', annotation)
260           | C.Cast (v,t) ->
261              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
262              if innersort = `Prop then
263               add_inner_type fresh_id'' ;
264              C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
265           | C.Prod (n,s,t) ->
266               xxx_add ids_to_inner_sorts fresh_id''
267                (sort_of innertype) ;
268                     let sourcetype = xxx_type_of_aux' metasenv context s in
269                      xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
270                       (sort_of sourcetype) ;
271               let n' =
272                match n with
273                   C.Anonymous -> n
274                 | C.Name n' ->
275                    if DoubleTypeInference.does_not_occur 1 t then
276                     C.Anonymous
277                    else
278                     C.Name n'
279               in
280                C.AProd
281                 (fresh_id'', n', aux' context idrefs s,
282                  aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
283           | C.Lambda (n,s,t) ->
284              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
285                    let sourcetype = xxx_type_of_aux' metasenv context s in
286                     xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
287                      (sort_of sourcetype) ;
288               if innersort = `Prop then
289                begin
290                 let father_is_lambda =
291                  match father with
292                     None -> false
293                   | Some father' ->
294                      match Hashtbl.find ids_to_terms father' with
295                         C.Lambda _ -> true
296                       | _ -> false
297                 in
298                  if (not father_is_lambda) || expected_available then
299                   add_inner_type fresh_id''
300                end ;
301               C.ALambda
302                (fresh_id'',n, aux' context idrefs s,
303                 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
304           | C.LetIn (n,s,t) ->
305              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
306              if innersort = `Prop then
307               add_inner_type fresh_id'' ;
308              C.ALetIn
309               (fresh_id'', n, aux' context idrefs s,
310                aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
311           | C.Appl l ->
312              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
313              if innersort = `Prop then
314               add_inner_type fresh_id'' ;
315              C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
316           | C.Const (uri,exp_named_subst) ->
317              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
318              if innersort = `Prop  && expected_available then
319               add_inner_type fresh_id'' ;
320              let exp_named_subst' =
321               List.map
322                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
323              in
324               C.AConst (fresh_id'', uri, exp_named_subst')
325           | C.MutInd (uri,tyno,exp_named_subst) ->
326              let exp_named_subst' =
327               List.map
328                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
329              in
330               C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
331           | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
332              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
333              if innersort = `Prop  && expected_available then
334               add_inner_type fresh_id'' ;
335              let exp_named_subst' =
336               List.map
337                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
338              in
339               C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
340           | C.MutCase (uri, tyno, outty, term, patterns) ->
341              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
342              if innersort = `Prop then
343               add_inner_type fresh_id'' ;
344              C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
345               aux' context idrefs term, List.map (aux' context idrefs) patterns)
346           | C.Fix (funno, funs) ->
347              let fresh_idrefs =
348               List.map (function _ -> gen_id seed) funs in
349              let new_idrefs = List.rev fresh_idrefs @ idrefs in
350              let tys =
351               List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
352              in
353               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
354               if innersort = `Prop then
355                add_inner_type fresh_id'' ;
356               C.AFix (fresh_id'', funno,
357                List.map2
358                 (fun id (name, indidx, ty, bo) ->
359                   (id, name, indidx, aux' context idrefs ty,
360                     aux' (tys@context) new_idrefs bo)
361                 ) fresh_idrefs funs
362              )
363           | C.CoFix (funno, funs) ->
364              let fresh_idrefs =
365               List.map (function _ -> gen_id seed) funs in
366              let new_idrefs = List.rev fresh_idrefs @ idrefs in
367              let tys =
368               List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
369              in
370               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
371               if innersort = `Prop then
372                add_inner_type fresh_id'' ;
373               C.ACoFix (fresh_id'', funno,
374                List.map2
375                 (fun id (name, ty, bo) ->
376                   (id, name, aux' context idrefs ty,
377                     aux' (tys@context) new_idrefs bo)
378                 ) fresh_idrefs funs
379               )
380         in
381 (*
382          let timea = Sys.time () in
383          let res = aux true None context idrefs t in
384          let timeb = Sys.time () in
385           prerr_endline
386            ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
387           res
388 *)
389   let res = aux global_computeinnertypes None context idrefs t in
390   prerr_endline (">>>> aux : " ^ string_of_float !time);
391   res
392 ;;
393
394 let acic_of_cic_context ~computeinnertypes metasenv context idrefs t =
395  let ids_to_terms = Hashtbl.create 503 in
396  let ids_to_father_ids = Hashtbl.create 503 in
397  let ids_to_inner_sorts = Hashtbl.create 503 in
398  let ids_to_inner_types = Hashtbl.create 503 in
399  let seed = ref 0 in
400    acic_of_cic_context' ~computeinnertypes seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
401     ids_to_inner_types metasenv context idrefs t,
402    ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
403 ;;
404
405 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids 
406   ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
407   metasenv (metano,context,goal)
408
409   let computeinnertypes = false in
410   let acic_of_cic_context =
411     acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
412       ids_to_inner_types  metasenv in
413   let _, acontext,final_idrefs =
414     (List.fold_right
415       (fun binding (context, acontext,idrefs) ->
416          let hid = "h" ^ string_of_int !hypotheses_seed in
417            Hashtbl.add ids_to_hypotheses hid binding ;
418            incr hypotheses_seed ;
419            match binding with
420                Some (n,Cic.Def (t,_)) ->
421                  let acic = acic_of_cic_context ~computeinnertypes context idrefs t None in
422                  Hashtbl.replace ids_to_father_ids (CicUtil.id_of_annterm acic)
423                   (Some hid);
424                  (binding::context),
425                    ((hid,Some (n,Cic.ADef acic))::acontext),(hid::idrefs)
426              | Some (n,Cic.Decl t) ->
427                  let acic = acic_of_cic_context ~computeinnertypes context idrefs t None in
428                  Hashtbl.replace ids_to_father_ids (CicUtil.id_of_annterm acic)
429                   (Some hid);
430                  (binding::context),
431                    ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
432              | None ->
433                  (* Invariant: "" is never looked up *)
434                   (None::context),((hid,None)::acontext),""::idrefs
435          ) context ([],[],[])
436        )
437   in 
438   let agoal = acic_of_cic_context ~computeinnertypes context final_idrefs goal None in
439   (metano,acontext,agoal)
440 ;;
441
442 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) = 
443     let ids_to_terms = Hashtbl.create 503 in
444     let ids_to_father_ids = Hashtbl.create 503 in
445     let ids_to_inner_sorts = Hashtbl.create 503 in
446     let ids_to_inner_types = Hashtbl.create 503 in
447     let ids_to_hypotheses = Hashtbl.create 23 in
448     let hypotheses_seed = ref 0 in
449     let seed = ref 1 in (* 'i0' is used for the whole sequent *)
450     let unsh_sequent =
451      let i,canonical_context,term = sequent in
452       let canonical_context' =
453        List.fold_right
454         (fun d canonical_context' ->
455           let d =
456            match d with
457               None -> None
458             | Some (n, Cic.Decl t)->
459                Some (n, Cic.Decl (Unshare.unshare t))
460             | Some (n, Cic.Def (t,None)) ->
461                Some (n, Cic.Def ((Unshare.unshare t),None))
462             | Some (n,Cic.Def (bo,Some ty)) ->
463                Some (n, Cic.Def (Unshare.unshare bo,Some (Unshare.unshare ty)))
464           in
465            d::canonical_context'
466         ) canonical_context []
467       in
468       let term' = Unshare.unshare term in
469        (i,canonical_context',term')
470     in
471     let (metano,acontext,agoal) = 
472       aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids 
473       ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
474       metasenv unsh_sequent in
475     (unsh_sequent,
476      (("i0",metano,acontext,agoal), 
477       ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses))
478 ;;
479
480 let acic_object_of_cic_object ?(eta_fix=true) obj =
481  let module C = Cic in
482  let module E = Eta_fixing in
483   let ids_to_terms = Hashtbl.create 503 in
484   let ids_to_father_ids = Hashtbl.create 503 in
485   let ids_to_inner_sorts = Hashtbl.create 503 in
486   let ids_to_inner_types = Hashtbl.create 503 in
487   let ids_to_conjectures = Hashtbl.create 11 in
488   let ids_to_hypotheses = Hashtbl.create 127 in
489   let hypotheses_seed = ref 0 in
490   let conjectures_seed = ref 0 in
491   let seed = ref 0 in
492   let acic_term_of_cic_term_context' =
493    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
494     ids_to_inner_types in
495   let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
496   let aconjecture_of_conjecture' = aconjecture_of_conjecture seed 
497     ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types 
498     ids_to_hypotheses hypotheses_seed in 
499    let eta_fix metasenv context t =
500     let t = if eta_fix then E.eta_fix metasenv context t else t in
501      Unshare.unshare t in
502    let aobj =
503     match obj with
504       C.Constant (id,Some bo,ty,params,attrs) ->
505        let bo' = eta_fix [] [] bo in
506        let ty' = eta_fix [] [] ty in
507        let abo = acic_term_of_cic_term' ~computeinnertypes:true bo' (Some ty') in
508        let aty = acic_term_of_cic_term' ~computeinnertypes:false ty' None in
509         C.AConstant
510          ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params,attrs)
511     | C.Constant (id,None,ty,params,attrs) ->
512        let ty' = eta_fix [] [] ty in
513        let aty = acic_term_of_cic_term' ~computeinnertypes:false ty' None in
514         C.AConstant
515          ("mettereaposto",None,id,None,aty,params,attrs)
516     | C.Variable (id,bo,ty,params,attrs) ->
517        let ty' = eta_fix [] [] ty in
518        let abo =
519         match bo with
520            None -> None
521          | Some bo ->
522             let bo' = eta_fix [] [] bo in
523              Some (acic_term_of_cic_term' ~computeinnertypes:true bo' (Some ty'))
524        in
525        let aty = acic_term_of_cic_term' ~computeinnertypes:false ty' None in
526         C.AVariable
527          ("mettereaposto",id,abo,aty,params,attrs)
528     | C.CurrentProof (id,conjectures,bo,ty,params,attrs) ->
529        let conjectures' =
530         List.map
531          (function (i,canonical_context,term) ->
532            let canonical_context' =
533             List.fold_right
534              (fun d canonical_context' ->
535                let d =
536                 match d with
537                    None -> None
538                  | Some (n, C.Decl t)->
539                     Some (n, C.Decl (eta_fix conjectures canonical_context' t))
540                  | Some (n, C.Def (t,None)) ->
541                     Some (n,
542                      C.Def ((eta_fix conjectures canonical_context' t),None))
543                  | Some (_,C.Def (_,Some _)) -> assert false
544                in
545                 d::canonical_context'
546              ) canonical_context []
547            in
548            let term' = eta_fix conjectures canonical_context' term in
549             (i,canonical_context',term')
550          ) conjectures
551        in
552        let aconjectures = 
553         List.map
554          (function (i,canonical_context,term) as conjecture ->
555            let cid = "c" ^ string_of_int !conjectures_seed in
556             xxx_add ids_to_conjectures cid conjecture ;
557             incr conjectures_seed ;
558            let (i,acanonical_context,aterm) 
559              = aconjecture_of_conjecture' conjectures conjecture in
560            (cid,i,acanonical_context,aterm))
561           conjectures' in 
562 (*        let time1 = Sys.time () in *)
563        let bo' = eta_fix conjectures' [] bo in
564        let ty' = eta_fix conjectures' [] ty in
565 (*
566        let time2 = Sys.time () in
567        prerr_endline
568         ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
569        hashtbl_add_time := 0.0 ;
570        type_of_aux'_add_time := 0.0 ;
571        DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
572 *)
573        let abo =
574         acic_term_of_cic_term_context' ~computeinnertypes:true conjectures' [] [] bo' (Some ty') in
575        let aty = acic_term_of_cic_term_context' ~computeinnertypes:false conjectures' [] [] ty' None in
576 (*
577        let time3 = Sys.time () in
578        prerr_endline
579         ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
580        prerr_endline
581         ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
582        prerr_endline
583         ("++++++++++++ Tempi della type_of_aux'_add_time nella double_type_inference(" ^ string_of_int !DoubleTypeInference.number_new_type_of_aux'_double_work ^ ";" ^ string_of_int !DoubleTypeInference.number_new_type_of_aux'_prop ^ "/" ^ string_of_int !DoubleTypeInference.number_new_type_of_aux' ^ "): " ^ string_of_float !DoubleTypeInference.type_of_aux'_add_time) ;
584        prerr_endline
585         ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
586        prerr_endline
587         ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
588        prerr_endline
589         ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
590 *)
591         C.ACurrentProof
592          ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params,attrs)
593     | C.InductiveDefinition (tys,params,paramsno,attrs) ->
594        let tys =
595         List.map
596          (fun (name,i,arity,cl) ->
597            (name,i,Unshare.unshare arity,
598              List.map (fun (name,ty) -> name,Unshare.unshare ty) cl)) tys in
599        let context =
600         List.map
601          (fun (name,_,arity,_) ->
602            Some (C.Name name, C.Decl (Unshare.unshare arity))) tys in
603        let idrefs = List.map (function _ -> gen_id seed) tys in
604        let atys =
605         List.map2
606          (fun id (name,inductive,ty,cons) ->
607            let acons =
608             List.map
609              (function (name,ty) ->
610                (name,
611                  acic_term_of_cic_term_context' ~computeinnertypes:false [] context idrefs ty None)
612              ) cons
613            in
614             (id,name,inductive,
615              acic_term_of_cic_term' ~computeinnertypes:false ty None,acons)
616          ) (List.rev idrefs) tys
617        in
618         C.AInductiveDefinition ("mettereaposto",atys,params,paramsno,attrs)
619    in
620     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
621      ids_to_conjectures,ids_to_hypotheses
622 ;;
623
624 let plain_acic_term_of_cic_term =
625  let module C = Cic in
626  let mk_fresh_id =
627   let id = ref 0 in
628    function () -> incr id; "i" ^ string_of_int !id in
629  let rec aux context t =
630   let fresh_id = mk_fresh_id () in
631   match t with
632      C.Rel n ->
633       let idref,id =
634        match get_nth context n with
635           idref,(Some (C.Name s,_)) -> idref,s
636         | idref,_ -> idref,"__" ^ string_of_int n
637       in
638        C.ARel (fresh_id, idref, n, id)
639    | C.Var (uri,exp_named_subst) ->
640       let exp_named_subst' =
641        List.map
642         (function i,t -> i, (aux context t)) exp_named_subst
643       in
644        C.AVar (fresh_id,uri,exp_named_subst')
645    | C.Implicit _
646    | C.Meta _ -> assert false
647    | C.Sort s -> C.ASort (fresh_id, s)
648    | C.Cast (v,t) ->
649       C.ACast (fresh_id, aux context v, aux context t)
650    | C.Prod (n,s,t) ->
651         C.AProd
652          (fresh_id, n, aux context s,
653           aux ((fresh_id, Some (n, C.Decl s))::context) t)
654    | C.Lambda (n,s,t) ->
655        C.ALambda
656         (fresh_id,n, aux context s,
657          aux ((fresh_id, Some (n, C.Decl s))::context) t)
658    | C.LetIn (n,s,t) ->
659       C.ALetIn
660        (fresh_id, n, aux context s,
661         aux ((fresh_id, Some (n, C.Def(s,None)))::context) t)
662    | C.Appl l ->
663       C.AAppl (fresh_id, List.map (aux context) l)
664    | C.Const (uri,exp_named_subst) ->
665       let exp_named_subst' =
666        List.map
667         (function i,t -> i, (aux context t)) exp_named_subst
668       in
669        C.AConst (fresh_id, uri, exp_named_subst')
670    | C.MutInd (uri,tyno,exp_named_subst) ->
671       let exp_named_subst' =
672        List.map
673         (function i,t -> i, (aux context t)) exp_named_subst
674       in
675        C.AMutInd (fresh_id, uri, tyno, exp_named_subst')
676    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
677       let exp_named_subst' =
678        List.map
679         (function i,t -> i, (aux context t)) exp_named_subst
680       in
681        C.AMutConstruct (fresh_id, uri, tyno, consno, exp_named_subst')
682    | C.MutCase (uri, tyno, outty, term, patterns) ->
683       C.AMutCase (fresh_id, uri, tyno, aux context outty,
684        aux context term, List.map (aux context) patterns)
685    | C.Fix (funno, funs) ->
686       let tys =
687        List.map
688         (fun (name,_,ty,_) -> mk_fresh_id (), Some (C.Name name, C.Decl ty)) funs
689       in
690        C.AFix (fresh_id, funno,
691         List.map2
692          (fun (id,_) (name, indidx, ty, bo) ->
693            (id, name, indidx, aux context ty, aux (tys@context) bo)
694          ) tys funs
695       )
696    | C.CoFix (funno, funs) ->
697       let tys =
698        List.map (fun (name,ty,_) ->
699         mk_fresh_id (),Some (C.Name name, C.Decl ty)) funs
700       in
701        C.ACoFix (fresh_id, funno,
702         List.map2
703          (fun (id,_) (name, ty, bo) ->
704            (id, name, aux context ty, aux (tys@context) bo)
705          ) tys funs
706        )
707  in
708   aux
709 ;;
710
711 let plain_acic_object_of_cic_object obj =
712  let module C = Cic in
713  let mk_fresh_id =
714   let id = ref 0 in
715    function () -> incr id; "it" ^ string_of_int !id
716  in
717   match obj with
718     C.Constant (id,Some bo,ty,params,attrs) ->
719      let abo = plain_acic_term_of_cic_term [] bo in
720      let aty = plain_acic_term_of_cic_term [] ty in
721       C.AConstant
722        ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params,attrs)
723   | C.Constant (id,None,ty,params,attrs) ->
724      let aty = plain_acic_term_of_cic_term [] ty in
725       C.AConstant
726        ("mettereaposto",None,id,None,aty,params,attrs)
727   | C.Variable (id,bo,ty,params,attrs) ->
728      let abo =
729       match bo with
730          None -> None
731        | Some bo -> Some (plain_acic_term_of_cic_term [] bo)
732      in
733      let aty = plain_acic_term_of_cic_term [] ty in
734       C.AVariable
735        ("mettereaposto",id,abo,aty,params,attrs)
736   | C.CurrentProof _ -> assert false
737   | C.InductiveDefinition (tys,params,paramsno,attrs) ->
738      let context =
739       List.map
740        (fun (name,_,arity,_) ->
741          mk_fresh_id (), Some (C.Name name, C.Decl arity)) tys in
742      let atys =
743       List.map2
744        (fun (id,_) (name,inductive,ty,cons) ->
745          let acons =
746           List.map
747            (function (name,ty) ->
748              (name,
749                plain_acic_term_of_cic_term context ty)
750            ) cons
751          in
752           (id,name,inductive,plain_acic_term_of_cic_term [] ty,acons)
753        ) context tys
754      in
755       C.AInductiveDefinition ("mettereaposto",atys,params,paramsno,attrs)
756 ;;