]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/cic_acic/cic2acic.ml
bla bla bla
[helm.git] / helm / software / 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 of CicUniv.universe  ]
29
30 let string_of_sort = function
31   | `Prop -> "Prop"
32   | `Set -> "Set"
33   | `Type u -> "Type:" ^ string_of_int (CicUniv.univno u) ^ ":" ^ UriManager.string_of_uri (CicUniv.univuri u)
34   | `CProp u -> "CProp:" ^ string_of_int (CicUniv.univno u) ^ ":" ^ UriManager.string_of_uri (CicUniv.univuri u)
35
36
37 let sort_of_sort = function
38   | Cic.Prop  -> `Prop
39   | Cic.Set   -> `Set
40   | Cic.Type u -> `Type u
41   | Cic.CProp u -> `CProp u
42
43 (* let hashtbl_add_time = ref 0.0;; *)
44
45 let xxx_add_profiler = HExtlib.profile "xxx_add";;
46 let xxx_add h k v =
47  xxx_add_profiler.HExtlib.profile (Hashtbl.add h k) v
48 ;;
49
50 let xxx_type_of_aux' m c t =
51  let res,_ =
52    try
53     CicTypeChecker.type_of_aux' m c t CicUniv.oblivion_ugraph
54    with
55    | CicTypeChecker.AssertFailure _
56    | CicTypeChecker.TypeCheckerFailure _ ->
57        Cic.Sort Cic.Prop, CicUniv.oblivion_ugraph
58   in
59  res
60 ;;
61
62 let xxx_type_of_aux'_profiler = HExtlib.profile "xxx_type_of_aux'";;
63 let xxx_type_of_aux' m c t =
64  xxx_type_of_aux'_profiler.HExtlib.profile (xxx_type_of_aux' m c) t
65
66 type anntypes =
67  {annsynthesized : Cic.annterm ; annexpected : Cic.annterm option}
68 ;;
69
70 let gen_id seed =
71  let res = "i" ^ string_of_int !seed in
72   incr seed ;
73   res
74 ;;
75
76 let fresh_id seed ids_to_terms ids_to_father_ids =
77  fun father t ->
78   let res = gen_id seed in
79    xxx_add ids_to_father_ids res father ;
80    xxx_add ids_to_terms res t ;
81    res
82 ;;
83
84 let source_id_of_id id = "#source#" ^ id;;
85
86 exception NotEnoughElements of string;;
87
88 (*CSC: cut&paste da cicPp.ml *)
89 (* get_nth l n   returns the nth element of the list l if it exists or *)
90 (* raises NotEnoughElements if l has less than n elements             *)
91 let rec get_nth msg l n =
92  match (n,l) with
93     (1, he::_) -> he
94   | (n, he::tail) when n > 1 -> get_nth msg tail (n-1)
95   | (_,_) -> raise (NotEnoughElements msg)
96 ;;
97
98
99 let profiler_for_find = HExtlib.profile "CicHash" ;;
100 let profiler_for_whd = HExtlib.profile "whd" ;;
101
102 let cic_CicHash_find a b =  
103   profiler_for_find.HExtlib.profile (Cic.CicHash.find a) b
104 ;;
105
106 let cicReduction_whd c t = 
107  profiler_for_whd.HExtlib.profile (CicReduction.whd c) t
108 ;;
109
110 let acic_of_cic_context' ~computeinnertypes:global_computeinnertypes
111   seed ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
112   metasenv context idrefs t expectedty
113 =
114  let module D = DoubleTypeInference in
115  let module C = Cic in
116   let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
117 (*    let time1 = Sys.time () in *)
118    let terms_to_types =
119 (*
120      let time0 = Sys.time () in
121      let prova = CicTypeChecker.type_of_aux' metasenv context t in
122      let time1 = Sys.time () in
123      prerr_endline ("*** Fine type_inference:" ^ (string_of_float (time1 -. time0)));
124      let res = D.double_type_of metasenv context t expectedty in
125      let time2 = Sys.time () in
126    prerr_endline ("*** Fine double_type_inference:" ^ (string_of_float (time2 -. time1)));
127      res 
128 *)
129     if global_computeinnertypes then
130      D.double_type_of metasenv context t expectedty
131     else
132      Cic.CicHash.create 1 (* empty table *)
133    in
134 (*
135    let time2 = Sys.time () in
136    prerr_endline
137     ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
138 *)
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 u) -> `CProp u
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 "1" 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,ty,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, aux' context idrefs ty,
310                 aux' ((Some (n, C.Def(s,ty)))::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.fold_left
352                  (fun (types,len) (n,_,ty,_) ->
353                     (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
354                      len+1)
355                  ) ([],0) funs
356              in
357               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
358               if innersort = `Prop then
359                add_inner_type fresh_id'' ;
360               C.AFix (fresh_id'', funno,
361                List.map2
362                 (fun id (name, indidx, ty, bo) ->
363                   (id, name, indidx, aux' context idrefs ty,
364                     aux' (tys@context) new_idrefs bo)
365                 ) fresh_idrefs funs
366              )
367           | C.CoFix (funno, funs) ->
368              let fresh_idrefs =
369               List.map (function _ -> gen_id seed) funs in
370              let new_idrefs = List.rev fresh_idrefs @ idrefs in
371              let tys,_ =
372                List.fold_left
373                  (fun (types,len) (n,ty,_) ->
374                     (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
375                      len+1)
376                  ) ([],0) funs
377              in
378               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
379               if innersort = `Prop then
380                add_inner_type fresh_id'' ;
381               C.ACoFix (fresh_id'', funno,
382                List.map2
383                 (fun id (name, ty, bo) ->
384                   (id, name, aux' context idrefs ty,
385                     aux' (tys@context) new_idrefs bo)
386                 ) fresh_idrefs funs
387               )
388         in
389 (*
390          let timea = Sys.time () in
391          let res = aux true None context idrefs t in
392          let timeb = Sys.time () in
393           prerr_endline
394            ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
395           res
396 *)
397   aux global_computeinnertypes None context idrefs t
398 ;;
399
400 let acic_of_cic_context ~computeinnertypes metasenv context idrefs t =
401  let ids_to_terms = Hashtbl.create 503 in
402  let ids_to_father_ids = Hashtbl.create 503 in
403  let ids_to_inner_sorts = Hashtbl.create 503 in
404  let ids_to_inner_types = Hashtbl.create 503 in
405  let seed = ref 0 in
406    acic_of_cic_context' ~computeinnertypes seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
407     ids_to_inner_types metasenv context idrefs t,
408    ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
409 ;;
410
411 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids 
412   ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
413   metasenv (metano,context,goal)
414
415   let computeinnertypes = false in
416   let acic_of_cic_context =
417     acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
418       ids_to_inner_types  metasenv in
419   let _, acontext,final_idrefs =
420     (List.fold_right
421       (fun binding (context, acontext,idrefs) ->
422          let hid = "h" ^ string_of_int !hypotheses_seed in
423            Hashtbl.add ids_to_hypotheses hid binding ;
424            incr hypotheses_seed ;
425            match binding with
426                Some (n,Cic.Def (t,ty)) ->
427                  let acic =
428                   acic_of_cic_context ~computeinnertypes context idrefs t
429                    None in
430                  let acic2 =
431                   acic_of_cic_context ~computeinnertypes context idrefs ty
432                    None
433                  in
434                   Hashtbl.replace ids_to_father_ids (CicUtil.id_of_annterm acic)
435                    (Some hid);
436                   Hashtbl.replace ids_to_father_ids
437                    (CicUtil.id_of_annterm acic2) (Some hid);
438                   (binding::context),
439                     ((hid,Some (n,Cic.ADef (acic,acic2)))::acontext),
440                     (hid::idrefs)
441              | Some (n,Cic.Decl t) ->
442                  let acic = acic_of_cic_context ~computeinnertypes context idrefs t None in
443                  Hashtbl.replace ids_to_father_ids (CicUtil.id_of_annterm acic)
444                   (Some hid);
445                  (binding::context),
446                    ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
447              | None ->
448                  (* Invariant: "" is never looked up *)
449                   (None::context),((hid,None)::acontext),""::idrefs
450          ) context ([],[],[])
451        )
452   in 
453   let agoal = acic_of_cic_context ~computeinnertypes context final_idrefs goal None in
454   (metano,acontext,agoal)
455 ;;
456
457 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) = 
458     let ids_to_terms = Hashtbl.create 503 in
459     let ids_to_father_ids = Hashtbl.create 503 in
460     let ids_to_inner_sorts = Hashtbl.create 503 in
461     let ids_to_inner_types = Hashtbl.create 503 in
462     let ids_to_hypotheses = Hashtbl.create 23 in
463     let hypotheses_seed = ref 0 in
464     let seed = ref 1 in (* 'i0' is used for the whole sequent *)
465     let unsh_sequent =
466      let i,canonical_context,term = sequent in
467       let canonical_context' =
468        List.fold_right
469         (fun d canonical_context' ->
470           let d =
471            match d with
472               None -> None
473             | Some (n, Cic.Decl t)->
474                Some (n, Cic.Decl (Unshare.unshare t))
475             | Some (n,Cic.Def (bo,ty)) ->
476                Some (n, Cic.Def (Unshare.unshare bo,Unshare.unshare ty))
477           in
478            d::canonical_context'
479         ) canonical_context []
480       in
481       let term' = Unshare.unshare term in
482        (i,canonical_context',term')
483     in
484     let (metano,acontext,agoal) = 
485       aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids 
486       ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
487       metasenv unsh_sequent in
488     (unsh_sequent,
489      (("i0",metano,acontext,agoal), 
490       ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses))
491 ;;
492
493 let acic_object_of_cic_object ?(eta_fix=false) obj =
494  let module C = Cic in
495  let module E = Eta_fixing in
496   let ids_to_terms = Hashtbl.create 503 in
497   let ids_to_father_ids = Hashtbl.create 503 in
498   let ids_to_inner_sorts = Hashtbl.create 503 in
499   let ids_to_inner_types = Hashtbl.create 503 in
500   let ids_to_conjectures = Hashtbl.create 11 in
501   let ids_to_hypotheses = Hashtbl.create 127 in
502   let hypotheses_seed = ref 0 in
503   let conjectures_seed = ref 0 in
504   let seed = ref 0 in
505   let acic_term_of_cic_term_context' =
506    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
507     ids_to_inner_types in
508   let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
509   let aconjecture_of_conjecture' = aconjecture_of_conjecture seed 
510     ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types 
511     ids_to_hypotheses hypotheses_seed in 
512    let eta_fix_and_unshare metasenv context t =
513     let t = if eta_fix then E.eta_fix metasenv context t else t in
514      Unshare.unshare t in
515    let aobj =
516     match obj with
517       C.Constant (id,Some bo,ty,params,attrs) ->
518        let bo' = (*eta_fix_and_unshare[] [] bo*) Unshare.unshare bo in
519        let ty' = eta_fix_and_unshare [] [] ty in
520        let abo = acic_term_of_cic_term' ~computeinnertypes:true bo' (Some ty') in
521        let aty = acic_term_of_cic_term' ~computeinnertypes:false ty' None in
522         C.AConstant
523          ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params,attrs)
524     | C.Constant (id,None,ty,params,attrs) ->
525        let ty' = eta_fix_and_unshare [] [] ty in
526        let aty = acic_term_of_cic_term' ~computeinnertypes:false ty' None in
527         C.AConstant
528          ("mettereaposto",None,id,None,aty,params,attrs)
529     | C.Variable (id,bo,ty,params,attrs) ->
530        let ty' = eta_fix_and_unshare [] [] ty in
531        let abo =
532         match bo with
533            None -> None
534          | Some bo ->
535             let bo' = eta_fix_and_unshare [] [] bo in
536              Some (acic_term_of_cic_term' ~computeinnertypes:true bo' (Some ty'))
537        in
538        let aty = acic_term_of_cic_term' ~computeinnertypes:false ty' None in
539         C.AVariable
540          ("mettereaposto",id,abo,aty,params,attrs)
541     | C.CurrentProof (id,conjectures,bo,ty,params,attrs) ->
542        let conjectures' =
543         List.map
544          (function (i,canonical_context,term) ->
545            let canonical_context' =
546             List.fold_right
547              (fun d canonical_context' ->
548                let d =
549                 match d with
550                    None -> None
551                  | Some (n, C.Decl t)->
552                     Some (n, C.Decl (eta_fix_and_unshare conjectures canonical_context' t))
553                  | Some (n, C.Def (t,ty)) ->
554                     Some (n,
555                      C.Def
556                       (eta_fix_and_unshare conjectures canonical_context' t,
557                        eta_fix_and_unshare conjectures canonical_context' ty))
558                in
559                 d::canonical_context'
560              ) canonical_context []
561            in
562            let term' = eta_fix_and_unshare conjectures canonical_context' term in
563             (i,canonical_context',term')
564          ) conjectures
565        in
566        let aconjectures = 
567         List.map
568          (function (i,canonical_context,term) as conjecture ->
569            let cid = "c" ^ string_of_int !conjectures_seed in
570             xxx_add ids_to_conjectures cid conjecture ;
571             incr conjectures_seed ;
572            let (i,acanonical_context,aterm) 
573              = aconjecture_of_conjecture' conjectures conjecture in
574            (cid,i,acanonical_context,aterm))
575           conjectures' in 
576        (* let bo' = eta_fix conjectures' [] bo in *)
577        let bo' = bo in
578        let ty' = eta_fix_and_unshare conjectures' [] ty in
579 (*
580        let time2 = Sys.time () in
581        prerr_endline
582         ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
583        hashtbl_add_time := 0.0 ;
584        type_of_aux'_add_time := 0.0 ;
585        DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
586 *)
587        let abo =
588         acic_term_of_cic_term_context' ~computeinnertypes:true conjectures' [] [] bo' (Some ty') in
589        let aty = acic_term_of_cic_term_context' ~computeinnertypes:false conjectures' [] [] ty' None in
590 (*
591        let time3 = Sys.time () in
592        prerr_endline
593         ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
594        prerr_endline
595         ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
596        prerr_endline
597         ("++++++++++++ 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) ;
598        prerr_endline
599         ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
600        prerr_endline
601         ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
602        prerr_endline
603         ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
604 *)
605         C.ACurrentProof
606          ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params,attrs)
607     | C.InductiveDefinition (tys,params,paramsno,attrs) ->
608        let tys =
609         List.map
610          (fun (name,i,arity,cl) ->
611            (name,i,Unshare.unshare arity,
612              List.map (fun (name,ty) -> name,Unshare.unshare ty) cl)) tys in
613        let context =
614         List.map
615          (fun (name,_,arity,_) ->
616            Some (C.Name name, C.Decl (Unshare.unshare arity))) tys in
617        let idrefs = List.map (function _ -> gen_id seed) tys in
618        let atys =
619         List.map2
620          (fun id (name,inductive,ty,cons) ->
621            let acons =
622             List.map
623              (function (name,ty) ->
624                (name,
625                  acic_term_of_cic_term_context' ~computeinnertypes:false [] context idrefs ty None)
626              ) cons
627            in
628             (id,name,inductive,
629              acic_term_of_cic_term' ~computeinnertypes:false ty None,acons)
630          ) (List.rev idrefs) tys
631        in
632         C.AInductiveDefinition ("mettereaposto",atys,params,paramsno,attrs)
633    in
634     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
635      ids_to_conjectures,ids_to_hypotheses
636 ;;
637
638 let plain_acic_term_of_cic_term =
639  let module C = Cic in
640  let mk_fresh_id =
641   let id = ref 0 in
642    function () -> incr id; "i" ^ string_of_int !id in
643  let rec aux context t =
644   let fresh_id = mk_fresh_id () in
645   match t with
646      C.Rel n ->
647       let idref,id =
648        match get_nth "2" context n with
649           idref,(Some (C.Name s,_)) -> idref,s
650         | idref,_ -> idref,"__" ^ string_of_int n
651       in
652        C.ARel (fresh_id, idref, n, id)
653    | C.Var (uri,exp_named_subst) ->
654       let exp_named_subst' =
655        List.map
656         (function i,t -> i, (aux context t)) exp_named_subst
657       in
658        C.AVar (fresh_id,uri,exp_named_subst')
659    | C.Implicit _
660    | C.Meta _ -> assert false
661    | C.Sort s -> C.ASort (fresh_id, s)
662    | C.Cast (v,t) ->
663       C.ACast (fresh_id, aux context v, aux context t)
664    | C.Prod (n,s,t) ->
665         C.AProd
666          (fresh_id, n, aux context s,
667           aux ((fresh_id, Some (n, C.Decl s))::context) t)
668    | C.Lambda (n,s,t) ->
669        C.ALambda
670         (fresh_id,n, aux context s,
671          aux ((fresh_id, Some (n, C.Decl s))::context) t)
672    | C.LetIn (n,s,ty,t) ->
673       C.ALetIn
674        (fresh_id, n, aux context s, aux context ty,
675         aux ((fresh_id, Some (n, C.Def(s,ty)))::context) t)
676    | C.Appl l ->
677       C.AAppl (fresh_id, List.map (aux context) l)
678    | C.Const (uri,exp_named_subst) ->
679       let exp_named_subst' =
680        List.map
681         (function i,t -> i, (aux context t)) exp_named_subst
682       in
683        C.AConst (fresh_id, uri, exp_named_subst')
684    | C.MutInd (uri,tyno,exp_named_subst) ->
685       let exp_named_subst' =
686        List.map
687         (function i,t -> i, (aux context t)) exp_named_subst
688       in
689        C.AMutInd (fresh_id, uri, tyno, exp_named_subst')
690    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
691       let exp_named_subst' =
692        List.map
693         (function i,t -> i, (aux context t)) exp_named_subst
694       in
695        C.AMutConstruct (fresh_id, uri, tyno, consno, exp_named_subst')
696    | C.MutCase (uri, tyno, outty, term, patterns) ->
697       C.AMutCase (fresh_id, uri, tyno, aux context outty,
698        aux context term, List.map (aux context) patterns)
699    | C.Fix (funno, funs) ->
700       let tys,_ =
701         List.fold_left
702           (fun (types,len) (n,_,ty,_) ->
703             (mk_fresh_id (),(Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
704               len+1
705           ) ([],0) funs
706       in
707        C.AFix (fresh_id, funno,
708         List.map2
709          (fun (id,_) (name, indidx, ty, bo) ->
710            (id, name, indidx, aux context ty, aux (tys@context) bo)
711          ) tys funs
712       )
713    | C.CoFix (funno, funs) ->
714       let tys,_ =
715         List.fold_left
716           (fun (types,len) (n,ty,_) ->
717             (mk_fresh_id (),(Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))))::types,
718               len+1
719           ) ([],0) funs
720       in
721        C.ACoFix (fresh_id, funno,
722         List.map2
723          (fun (id,_) (name, ty, bo) ->
724            (id, name, aux context ty, aux (tys@context) bo)
725          ) tys funs
726        )
727  in
728   aux
729 ;;
730
731 let plain_acic_object_of_cic_object obj =
732  let module C = Cic in
733  let mk_fresh_id =
734   let id = ref 0 in
735    function () -> incr id; "it" ^ string_of_int !id
736  in
737   match obj with
738     C.Constant (id,Some bo,ty,params,attrs) ->
739      let abo = plain_acic_term_of_cic_term [] bo in
740      let aty = plain_acic_term_of_cic_term [] ty in
741       C.AConstant
742        ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params,attrs)
743   | C.Constant (id,None,ty,params,attrs) ->
744      let aty = plain_acic_term_of_cic_term [] ty in
745       C.AConstant
746        ("mettereaposto",None,id,None,aty,params,attrs)
747   | C.Variable (id,bo,ty,params,attrs) ->
748      let abo =
749       match bo with
750          None -> None
751        | Some bo -> Some (plain_acic_term_of_cic_term [] bo)
752      in
753      let aty = plain_acic_term_of_cic_term [] ty in
754       C.AVariable
755        ("mettereaposto",id,abo,aty,params,attrs)
756   | C.CurrentProof _ -> assert false
757   | C.InductiveDefinition (tys,params,paramsno,attrs) ->
758      let context =
759       List.map
760        (fun (name,_,arity,_) ->
761          mk_fresh_id (), Some (C.Name name, C.Decl arity)) tys in
762      let atys =
763       List.map2
764        (fun (id,_) (name,inductive,ty,cons) ->
765          let acons =
766           List.map
767            (function (name,ty) ->
768              (name,
769                plain_acic_term_of_cic_term context ty)
770            ) cons
771          in
772           (id,name,inductive,plain_acic_term_of_cic_term [] ty,acons)
773        ) context tys
774      in
775       C.AInductiveDefinition ("mettereaposto",atys,params,paramsno,attrs)
776 ;;