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;;
68 (*CSC: cut&paste da cicPp.ml *)
69 (* get_nth l n returns the nth element of the list l if it exists or *)
70 (* raises NotEnoughElements if l has less than n elements *)
74 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
75 | (_,_) -> raise NotEnoughElements
78 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
79 ids_to_inner_types metasenv context idrefs t expectedty
81 let module D = DoubleTypeInference in
83 let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
84 let time1 = Sys.time () in
86 let time0 = Sys.time () in
87 let prova = CicTypeChecker.type_of_aux' metasenv context t in
88 let time1 = Sys.time () in
89 prerr_endline ("*** Fine type_inference:" ^ (string_of_float (time1 -. time0)));
90 let res = D.double_type_of metasenv context t expectedty in
91 let time2 = Sys.time () in
92 prerr_endline ("*** Fine double_type_inference:" ^ (string_of_float (time2 -. time1)));
95 let time2 = Sys.time () in
97 ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
98 let rec aux computeinnertypes father context idrefs tt =
99 let fresh_id'' = fresh_id' father tt in
100 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
101 let aux' = aux computeinnertypes (Some fresh_id'') in
102 (* First of all we compute the inner type and the inner sort *)
103 (* of the term. They may be useful in what follows. *)
104 (*CSC: This is a very inefficient way of computing inner types *)
105 (*CSC: and inner sorts: very deep terms have their types/sorts *)
106 (*CSC: computed again and again. *)
107 let string_of_sort t =
108 match CicReduction.whd context t with
109 C.Sort C.Prop -> "Prop"
110 | C.Sort C.Set -> "Set"
111 | C.Sort C.Type -> "Type"
112 | C.Sort C.CProp -> "CProp"
115 let ainnertypes,innertype,innersort,expected_available =
116 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
117 (*CSC: (expected type + inferred type). Just for now we use the usual *)
118 (*CSC: type-inference, but the result is very poor. As a very weak *)
119 (*CSC: patch, I apply whd to the computed type. Full beta *)
120 (*CSC: reduction would be a much better option. *)
121 (*CSC: solo per testare i tempi *)
125 let {D.synthesized = synthesized; D.expected = expected} =
126 if computeinnertypes then
127 D.CicHash.find terms_to_types tt
129 (* We are already in an inner-type and Coscoy's double *)
130 (* type inference algorithm has not been applied. *)
132 (***CSC: patch per provare i tempi
133 CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
137 incr number_new_type_of_aux' ;
138 let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
139 let ainnertypes,expected_available =
140 if computeinnertypes then
141 let annexpected,expected_available =
144 | Some expectedty' ->
146 (aux false (Some fresh_id'') context idrefs expectedty'),
151 aux false (Some fresh_id'') context idrefs synthesized ;
152 annexpected = annexpected
153 }, expected_available
157 ainnertypes,synthesized, string_of_sort innersort, expected_available
160 Not_found -> (* l'inner-type non e' nella tabella ==> sort <> Prop *)
161 (* CSC: Type or Set? I can not tell *)
162 None,Cic.Sort Cic.Type,"Type",false
165 let add_inner_type id =
166 match ainnertypes with
168 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
173 match get_nth context n with
174 (Some (C.Name s,_)) -> s
175 | _ -> "__" ^ string_of_int n
177 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
178 if innersort = "Prop" && expected_available then
179 add_inner_type fresh_id'' ;
180 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
181 | C.Var (uri,exp_named_subst) ->
182 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
183 if innersort = "Prop" && expected_available then
184 add_inner_type fresh_id'' ;
185 let exp_named_subst' =
187 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
189 C.AVar (fresh_id'', uri,exp_named_subst')
191 let (_,canonical_context,_) =
192 List.find (function (m,_,_) -> n = m) metasenv
194 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
195 if innersort = "Prop" && expected_available then
196 add_inner_type fresh_id'' ;
197 C.AMeta (fresh_id'', n,
202 | _, Some t -> Some (aux' context idrefs t)
203 | Some _, None -> assert false (* due to typing rules *))
204 canonical_context l))
205 | C.Sort s -> C.ASort (fresh_id'', s)
206 | C.Implicit annotation -> C.AImplicit (fresh_id'', annotation)
208 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
209 if innersort = "Prop" then
210 add_inner_type fresh_id'' ;
211 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
213 xxx_add ids_to_inner_sorts fresh_id''
214 (string_of_sort innertype) ;
215 let sourcetype = xxx_type_of_aux' metasenv context s in
216 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
217 (string_of_sort sourcetype) ;
222 if DoubleTypeInference.does_not_occur 1 t then
228 (fresh_id'', n', aux' context idrefs s,
229 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
230 | C.Lambda (n,s,t) ->
231 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
232 let sourcetype = xxx_type_of_aux' metasenv context s in
233 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
234 (string_of_sort sourcetype) ;
235 if innersort = "Prop" then
237 let father_is_lambda =
241 match Hashtbl.find ids_to_terms father' with
245 if (not father_is_lambda) || expected_available then
246 add_inner_type fresh_id''
249 (fresh_id'',n, aux' context idrefs s,
250 aux' ((Some (n, C.Decl s)::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'' ;
256 (fresh_id'', n, aux' context idrefs s,
257 aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
259 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
260 if innersort = "Prop" then
261 add_inner_type fresh_id'' ;
262 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
263 | C.Const (uri,exp_named_subst) ->
264 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
265 if innersort = "Prop" && expected_available then
266 add_inner_type fresh_id'' ;
267 let exp_named_subst' =
269 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
271 C.AConst (fresh_id'', uri, exp_named_subst')
272 | C.MutInd (uri,tyno,exp_named_subst) ->
273 let exp_named_subst' =
275 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
277 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
278 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
279 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
280 if innersort = "Prop" && expected_available then
281 add_inner_type fresh_id'' ;
282 let exp_named_subst' =
284 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
286 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
287 | C.MutCase (uri, tyno, outty, term, patterns) ->
288 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
289 if innersort = "Prop" then
290 add_inner_type fresh_id'' ;
291 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
292 aux' context idrefs term, List.map (aux' context idrefs) patterns)
293 | C.Fix (funno, funs) ->
295 List.map (function _ -> gen_id seed) funs in
296 let new_idrefs = List.rev fresh_idrefs @ idrefs in
298 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
300 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
301 if innersort = "Prop" then
302 add_inner_type fresh_id'' ;
303 C.AFix (fresh_id'', funno,
305 (fun id (name, indidx, ty, bo) ->
306 (id, name, indidx, aux' context idrefs ty,
307 aux' (tys@context) new_idrefs bo)
310 | C.CoFix (funno, funs) ->
312 List.map (function _ -> gen_id seed) funs in
313 let new_idrefs = List.rev fresh_idrefs @ idrefs in
315 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
317 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
318 if innersort = "Prop" then
319 add_inner_type fresh_id'' ;
320 C.ACoFix (fresh_id'', funno,
322 (fun id (name, ty, bo) ->
323 (id, name, aux' context idrefs ty,
324 aux' (tys@context) new_idrefs bo)
328 let timea = Sys.time () in
329 let res = aux true None context idrefs t in
330 let timeb = Sys.time () in
332 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
336 let acic_of_cic_context metasenv context idrefs t =
337 let ids_to_terms = Hashtbl.create 503 in
338 let ids_to_father_ids = Hashtbl.create 503 in
339 let ids_to_inner_sorts = Hashtbl.create 503 in
340 let ids_to_inner_types = Hashtbl.create 503 in
342 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
343 ids_to_inner_types metasenv context idrefs t,
344 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
347 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
348 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
349 metasenv (metano,context,goal) =
350 let acic_of_cic_context =
351 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
352 ids_to_inner_types metasenv in
353 let _, acontext,final_idrefs =
355 (fun binding (context, acontext,idrefs) ->
356 let hid = "h" ^ string_of_int !hypotheses_seed in
357 Hashtbl.add ids_to_hypotheses hid binding ;
358 incr hypotheses_seed ;
360 Some (n,Cic.Def (t,None)) ->
361 let acic = acic_of_cic_context context idrefs t None in
363 ((hid,Some (n,Cic.ADef acic))::acontext),(hid::idrefs)
364 | Some (n,Cic.Decl t) ->
365 let acic = acic_of_cic_context context idrefs t None in
367 ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
369 (* Invariant: "" is never looked up *)
370 (None::context),((hid,None)::acontext),""::idrefs
371 | Some (_,Cic.Def (_,Some _)) -> assert false
375 let agoal = acic_of_cic_context context final_idrefs goal None in
376 (metano,acontext,agoal)
379 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) =
380 let ids_to_terms = Hashtbl.create 503 in
381 let ids_to_father_ids = Hashtbl.create 503 in
382 let ids_to_inner_sorts = Hashtbl.create 503 in
383 let ids_to_inner_types = Hashtbl.create 503 in
384 let ids_to_hypotheses = Hashtbl.create 23 in
385 let hypotheses_seed = ref 0 in
386 let seed = ref 1 in (* 'i0' is used for the whole sequent *)
387 let (metano,acontext,agoal) =
388 aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
389 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
391 ("i0",metano,acontext,agoal),
392 ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses
395 let acic_object_of_cic_object obj =
396 let module C = Cic in
397 let module E = Eta_fixing in
398 let ids_to_terms = Hashtbl.create 503 in
399 let ids_to_father_ids = Hashtbl.create 503 in
400 let ids_to_inner_sorts = Hashtbl.create 503 in
401 let ids_to_inner_types = Hashtbl.create 503 in
402 let ids_to_conjectures = Hashtbl.create 11 in
403 let ids_to_hypotheses = Hashtbl.create 127 in
404 let hypotheses_seed = ref 0 in
405 let conjectures_seed = ref 0 in
407 let acic_term_of_cic_term_context' =
408 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
409 ids_to_inner_types in
410 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
411 let aconjecture_of_conjecture' = aconjecture_of_conjecture seed
412 ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
413 ids_to_hypotheses hypotheses_seed in
416 C.Constant (id,Some bo,ty,params) ->
417 let bo' = E.eta_fix [] bo in
418 let ty' = E.eta_fix [] ty in
419 let abo = acic_term_of_cic_term' bo' (Some ty') in
420 let aty = acic_term_of_cic_term' ty' None in
422 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
423 | C.Constant (id,None,ty,params) ->
424 let ty' = E.eta_fix [] ty in
425 let aty = acic_term_of_cic_term' ty' None in
427 ("mettereaposto",None,id,None,aty,params)
428 | C.Variable (id,bo,ty,params) ->
429 let ty' = E.eta_fix [] ty in
434 let bo' = E.eta_fix [] bo in
435 Some (acic_term_of_cic_term' bo' (Some ty'))
437 let aty = acic_term_of_cic_term' ty' None in
439 ("mettereaposto",id,abo,aty, params)
440 | C.CurrentProof (id,conjectures,bo,ty,params) ->
443 (function (i,canonical_context,term) ->
444 let canonical_context' =
448 | Some (n, C.Decl t)-> Some (n, C.Decl (E.eta_fix conjectures t))
449 | Some (n, C.Def (t,None)) ->
450 Some (n, C.Def ((E.eta_fix conjectures t),None))
451 | Some (_,C.Def (_,Some _)) -> assert false
454 let term' = E.eta_fix conjectures term in
455 (i,canonical_context',term')
460 (function (i,canonical_context,term) as conjecture ->
461 let cid = "c" ^ string_of_int !conjectures_seed in
462 xxx_add ids_to_conjectures cid conjecture ;
463 incr conjectures_seed ;
464 let (i,acanonical_context,aterm)
465 = aconjecture_of_conjecture' conjectures conjecture in
466 (cid,i,acanonical_context,aterm))
468 (* let idrefs',revacanonical_context =
469 let rec aux context idrefs =
473 let hid = "h" ^ string_of_int !hypotheses_seed in
474 let new_idrefs = hid::idrefs in
475 xxx_add ids_to_hypotheses hid hyp ;
476 incr hypotheses_seed ;
478 (Some (n,C.Decl t)) ->
479 let final_idrefs,atl =
480 aux (hyp::context) new_idrefs tl in
482 acic_term_of_cic_term_context'
483 conjectures context idrefs t None
485 final_idrefs,(hid,Some (n,C.ADecl at))::atl
486 | (Some (n,C.Def (t,_))) ->
487 let final_idrefs,atl =
488 aux (hyp::context) new_idrefs tl in
490 acic_term_of_cic_term_context'
491 conjectures context idrefs t None
493 final_idrefs,(hid,Some (n,C.ADef at))::atl
495 let final_idrefs,atl =
496 aux (hyp::context) new_idrefs tl
498 final_idrefs,(hid,None)::atl
500 aux [] [] (List.rev canonical_context)
503 acic_term_of_cic_term_context' conjectures
504 canonical_context idrefs' term None
506 (cid,i,(List.rev revacanonical_context),aterm)
508 let time1 = Sys.time () in
509 let bo' = E.eta_fix conjectures' bo in
510 let ty' = E.eta_fix conjectures' ty in
511 let time2 = Sys.time () in
513 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
514 hashtbl_add_time := 0.0 ;
515 type_of_aux'_add_time := 0.0 ;
516 DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
518 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
519 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
520 let time3 = Sys.time () in
522 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
524 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
526 ("++++++++++++ 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) ;
528 ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
530 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
532 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
534 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
535 | C.InductiveDefinition (tys,params,paramsno) ->
538 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
539 let idrefs = List.map (function _ -> gen_id seed) tys in
542 (fun id (name,inductive,ty,cons) ->
545 (function (name,ty) ->
547 acic_term_of_cic_term_context' [] context idrefs ty None)
550 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
551 ) (List.rev idrefs) tys
553 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
555 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
556 ids_to_conjectures,ids_to_hypotheses