1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 let hashtbl_add_time = ref 0.0;;
29 let t1 = Sys.time () in
31 let t2 = Sys.time () in
32 hashtbl_add_time := !hashtbl_add_time +. t2 -. t1
35 let number_new_type_of_aux' = ref 0;;
36 let type_of_aux'_add_time = ref 0.0;;
38 let xxx_type_of_aux' m c t =
39 let t1 = Sys.time () in
40 let res = CicTypeChecker.type_of_aux' m c t in
41 let t2 = Sys.time () in
42 type_of_aux'_add_time := !type_of_aux'_add_time +. t2 -. t1 ;
47 {annsynthesized : Cic.annterm ; annexpected : Cic.annterm option}
51 let res = "i" ^ string_of_int !seed in
56 let fresh_id seed ids_to_terms ids_to_father_ids =
58 let res = gen_id seed in
59 xxx_add ids_to_father_ids res father ;
60 xxx_add ids_to_terms res t ;
64 let source_id_of_id id = "#source#" ^ id;;
66 exception NotEnoughElements;;
67 exception NameExpected;;
69 (*CSC: cut&paste da cicPp.ml *)
70 (* get_nth l n returns the nth element of the list l if it exists or *)
71 (* raises NotEnoughElements if l has less than n elements *)
75 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
76 | (_,_) -> raise NotEnoughElements
79 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
80 ids_to_inner_types metasenv context idrefs t expectedty
82 let module D = DoubleTypeInference in
84 let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
85 let time1 = Sys.time () in
87 D.double_type_of metasenv context t expectedty
89 let time2 = Sys.time () in
91 ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
92 let rec aux computeinnertypes father context idrefs tt =
93 let fresh_id'' = fresh_id' father tt in
94 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
95 let aux' = aux computeinnertypes (Some fresh_id'') in
96 (* First of all we compute the inner type and the inner sort *)
97 (* of the term. They may be useful in what follows. *)
98 (*CSC: This is a very inefficient way of computing inner types *)
99 (*CSC: and inner sorts: very deep terms have their types/sorts *)
100 (*CSC: computed again and again. *)
101 let string_of_sort t =
102 match CicReduction.whd context t with
103 C.Sort C.Prop -> "Prop"
104 | C.Sort C.Set -> "Set"
105 | C.Sort C.Type -> "Type"
108 let ainnertypes,innertype,innersort,expected_available =
109 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
110 (*CSC: (expected type + inferred type). Just for now we use the usual *)
111 (*CSC: type-inference, but the result is very poor. As a very weak *)
112 (*CSC: patch, I apply whd to the computed type. Full beta *)
113 (*CSC: reduction would be a much better option. *)
114 (*CSC: solo per testare i tempi *)
118 let {D.synthesized = synthesized; D.expected = expected} =
119 if computeinnertypes then
120 D.CicHash.find terms_to_types tt
122 (* We are already in an inner-type and Coscoy's double *)
123 (* type inference algorithm has not been applied. *)
125 (***CSC: patch per provare i tempi
126 CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
130 incr number_new_type_of_aux' ;
131 let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
132 let ainnertypes,expected_available =
133 if computeinnertypes then
134 let annexpected,expected_available =
137 | Some expectedty' ->
139 (aux false (Some fresh_id'') context idrefs expectedty'),
144 aux false (Some fresh_id'') context idrefs synthesized ;
145 annexpected = annexpected
146 }, expected_available
150 ainnertypes,synthesized, string_of_sort innersort, expected_available
153 Not_found -> (* l'inner-type non e' nella tabella ==> sort <> Prop *)
154 (* CSC: Type or Set? I can not tell *)
155 None,Cic.Sort Cic.Type,"Type",false
158 let add_inner_type id =
159 match ainnertypes with
161 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
166 match get_nth context n with
167 (Some (C.Name s,_)) -> s
168 | _ -> raise NameExpected
170 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
171 if innersort = "Prop" && expected_available then
172 add_inner_type fresh_id'' ;
173 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
174 | C.Var (uri,exp_named_subst) ->
175 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
176 if innersort = "Prop" && expected_available then
177 add_inner_type fresh_id'' ;
178 let exp_named_subst' =
180 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
182 C.AVar (fresh_id'', uri,exp_named_subst')
184 let (_,canonical_context,_) =
185 List.find (function (m,_,_) -> n = m) metasenv
187 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
188 if innersort = "Prop" && expected_available then
189 add_inner_type fresh_id'' ;
190 C.AMeta (fresh_id'', n,
195 | _, Some t -> Some (aux' context idrefs t)
196 | Some _, None -> assert false (* due to typing rules *))
197 canonical_context l))
198 | C.Sort s -> C.ASort (fresh_id'', s)
199 | C.Implicit -> C.AImplicit (fresh_id'')
201 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
202 if innersort = "Prop" then
203 add_inner_type fresh_id'' ;
204 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
206 xxx_add ids_to_inner_sorts fresh_id''
207 (string_of_sort innertype) ;
208 let sourcetype = xxx_type_of_aux' metasenv context s in
209 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
210 (string_of_sort sourcetype) ;
215 if DoubleTypeInference.does_not_occur 1 t then
221 (fresh_id'', n', aux' context idrefs s,
222 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
223 | C.Lambda (n,s,t) ->
224 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
225 let sourcetype = xxx_type_of_aux' metasenv context s in
226 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
227 (string_of_sort sourcetype) ;
228 if innersort = "Prop" then
230 let father_is_lambda =
234 match Hashtbl.find ids_to_terms father' with
238 if (not father_is_lambda) || expected_available then
239 add_inner_type fresh_id''
242 (fresh_id'',n, aux' context idrefs s,
243 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
245 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
246 if innersort = "Prop" then
247 add_inner_type fresh_id'' ;
249 (fresh_id'', n, aux' context idrefs s,
250 aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
252 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
253 if innersort = "Prop" then
254 add_inner_type fresh_id'' ;
255 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
256 | C.Const (uri,exp_named_subst) ->
257 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
258 if innersort = "Prop" && expected_available then
259 add_inner_type fresh_id'' ;
260 let exp_named_subst' =
262 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
264 C.AConst (fresh_id'', uri, exp_named_subst')
265 | C.MutInd (uri,tyno,exp_named_subst) ->
266 let exp_named_subst' =
268 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
270 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
271 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
272 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
273 if innersort = "Prop" && expected_available then
274 add_inner_type fresh_id'' ;
275 let exp_named_subst' =
277 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
279 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
280 | C.MutCase (uri, tyno, outty, term, patterns) ->
281 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
282 if innersort = "Prop" then
283 add_inner_type fresh_id'' ;
284 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
285 aux' context idrefs term, List.map (aux' context idrefs) patterns)
286 | C.Fix (funno, funs) ->
288 List.map (function _ -> gen_id seed) funs in
289 let new_idrefs = List.rev fresh_idrefs @ idrefs in
291 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
293 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
294 if innersort = "Prop" then
295 add_inner_type fresh_id'' ;
296 C.AFix (fresh_id'', funno,
298 (fun id (name, indidx, ty, bo) ->
299 (id, name, indidx, aux' context idrefs ty,
300 aux' (tys@context) new_idrefs bo)
303 | C.CoFix (funno, funs) ->
305 List.map (function _ -> gen_id seed) funs in
306 let new_idrefs = List.rev fresh_idrefs @ idrefs in
308 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
310 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
311 if innersort = "Prop" then
312 add_inner_type fresh_id'' ;
313 C.ACoFix (fresh_id'', funno,
315 (fun id (name, ty, bo) ->
316 (id, name, aux' context idrefs ty,
317 aux' (tys@context) new_idrefs bo)
321 let timea = Sys.time () in
322 let res = aux true None context idrefs t in
323 let timeb = Sys.time () in
325 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
329 let acic_of_cic_context metasenv context idrefs t =
330 let ids_to_terms = Hashtbl.create 503 in
331 let ids_to_father_ids = Hashtbl.create 503 in
332 let ids_to_inner_sorts = Hashtbl.create 503 in
333 let ids_to_inner_types = Hashtbl.create 503 in
335 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
336 ids_to_inner_types metasenv context idrefs t,
337 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
340 let acic_object_of_cic_object obj =
341 let module C = Cic in
342 let module E = Eta_fixing in
343 let ids_to_terms = Hashtbl.create 503 in
344 let ids_to_father_ids = Hashtbl.create 503 in
345 let ids_to_inner_sorts = Hashtbl.create 503 in
346 let ids_to_inner_types = Hashtbl.create 503 in
347 let ids_to_conjectures = Hashtbl.create 11 in
348 let ids_to_hypotheses = Hashtbl.create 127 in
349 let hypotheses_seed = ref 0 in
350 let conjectures_seed = ref 0 in
352 let acic_term_of_cic_term_context' =
353 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
354 ids_to_inner_types in
355 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
358 C.Constant (id,Some bo,ty,params) ->
359 let bo' = E.eta_fix [] bo in
360 let ty' = E.eta_fix [] ty in
361 let abo = acic_term_of_cic_term' bo' (Some ty') in
362 let aty = acic_term_of_cic_term' ty' None in
364 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
365 | C.Constant (id,None,ty,params) ->
366 let ty' = E.eta_fix [] ty in
367 let aty = acic_term_of_cic_term' ty' None in
369 ("mettereaposto",None,id,None,aty,params)
370 | C.Variable (id,bo,ty,params) ->
371 let ty' = E.eta_fix [] ty in
376 let bo' = E.eta_fix [] bo in
377 Some (acic_term_of_cic_term' bo' (Some ty'))
379 let aty = acic_term_of_cic_term' ty' None in
381 ("mettereaposto",id,abo,aty, params)
382 | C.CurrentProof (id,conjectures,bo,ty,params) ->
385 (function (i,canonical_context,term) ->
386 let canonical_context' =
390 | Some (n, C.Decl t)-> Some (n, C.Decl (E.eta_fix conjectures t))
391 | Some (n, C.Def (t,None)) ->
392 Some (n, C.Def ((E.eta_fix conjectures t),None))
393 | Some (_,C.Def (_,Some _)) -> assert false
396 let term' = E.eta_fix conjectures term in
397 (i,canonical_context',term')
402 (function (i,canonical_context,term) as conjecture ->
403 let cid = "c" ^ string_of_int !conjectures_seed in
404 xxx_add ids_to_conjectures cid conjecture ;
405 incr conjectures_seed ;
406 let idrefs',revacanonical_context =
407 let rec aux context idrefs =
411 let hid = "h" ^ string_of_int !hypotheses_seed in
412 let new_idrefs = hid::idrefs in
413 xxx_add ids_to_hypotheses hid hyp ;
414 incr hypotheses_seed ;
416 (Some (n,C.Decl t)) ->
417 let final_idrefs,atl =
418 aux (hyp::context) new_idrefs tl in
420 acic_term_of_cic_term_context'
421 conjectures context idrefs t None
423 final_idrefs,(hid,Some (n,C.ADecl at))::atl
424 | (Some (n,C.Def (t,_))) ->
425 let final_idrefs,atl =
426 aux (hyp::context) new_idrefs tl in
428 acic_term_of_cic_term_context'
429 conjectures context idrefs t None
431 final_idrefs,(hid,Some (n,C.ADef at))::atl
433 let final_idrefs,atl =
434 aux (hyp::context) new_idrefs tl
436 final_idrefs,(hid,None)::atl
438 aux [] [] (List.rev canonical_context)
441 acic_term_of_cic_term_context' conjectures
442 canonical_context idrefs' term None
444 (cid,i,(List.rev revacanonical_context),aterm)
446 let time1 = Sys.time () in
447 let bo' = E.eta_fix conjectures' bo in
448 let ty' = E.eta_fix conjectures' ty in
449 let time2 = Sys.time () in
451 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
452 hashtbl_add_time := 0.0 ;
453 type_of_aux'_add_time := 0.0 ;
455 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
456 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
457 let time3 = Sys.time () in
459 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
461 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
463 ("++++++++++++ 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) ;
465 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
467 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
469 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
470 | C.InductiveDefinition (tys,params,paramsno) ->
473 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
474 let idrefs = List.map (function _ -> gen_id seed) tys in
477 (fun id (name,inductive,ty,cons) ->
480 (function (name,ty) ->
482 acic_term_of_cic_term_context' [] context idrefs ty None)
485 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
486 ) (List.rev idrefs) tys
488 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
490 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
491 ids_to_conjectures,ids_to_hypotheses