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