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 let time0 = Sys.time () in
88 let prova = CicTypeChecker.type_of_aux' metasenv context t in
89 let time1 = Sys.time () in
90 prerr_endline ("*** Fine type_inference:" ^ (string_of_float (time1 -. time0)));
91 let res = D.double_type_of metasenv context t expectedty in
92 let time2 = Sys.time () in
93 prerr_endline ("*** Fine double_type_inference:" ^ (string_of_float (time2 -. time1)));
96 let time2 = Sys.time () in
98 ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
99 let rec aux computeinnertypes father context idrefs tt =
100 let fresh_id'' = fresh_id' father tt in
101 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
102 let aux' = aux computeinnertypes (Some fresh_id'') in
103 (* First of all we compute the inner type and the inner sort *)
104 (* of the term. They may be useful in what follows. *)
105 (*CSC: This is a very inefficient way of computing inner types *)
106 (*CSC: and inner sorts: very deep terms have their types/sorts *)
107 (*CSC: computed again and again. *)
108 let string_of_sort t =
109 match CicReduction.whd context t with
110 C.Sort C.Prop -> "Prop"
111 | C.Sort C.Set -> "Set"
112 | C.Sort C.Type -> "Type"
113 | C.Sort C.CProp -> "CProp"
116 let ainnertypes,innertype,innersort,expected_available =
117 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
118 (*CSC: (expected type + inferred type). Just for now we use the usual *)
119 (*CSC: type-inference, but the result is very poor. As a very weak *)
120 (*CSC: patch, I apply whd to the computed type. Full beta *)
121 (*CSC: reduction would be a much better option. *)
122 (*CSC: solo per testare i tempi *)
126 let {D.synthesized = synthesized; D.expected = expected} =
127 if computeinnertypes then
128 D.CicHash.find terms_to_types tt
130 (* We are already in an inner-type and Coscoy's double *)
131 (* type inference algorithm has not been applied. *)
133 (***CSC: patch per provare i tempi
134 CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
138 incr number_new_type_of_aux' ;
139 let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
140 let ainnertypes,expected_available =
141 if computeinnertypes then
142 let annexpected,expected_available =
145 | Some expectedty' ->
147 (aux false (Some fresh_id'') context idrefs expectedty'),
152 aux false (Some fresh_id'') context idrefs synthesized ;
153 annexpected = annexpected
154 }, expected_available
158 ainnertypes,synthesized, string_of_sort innersort, expected_available
161 Not_found -> (* l'inner-type non e' nella tabella ==> sort <> Prop *)
162 (* CSC: Type or Set? I can not tell *)
163 None,Cic.Sort Cic.Type,"Type",false
166 let add_inner_type id =
167 match ainnertypes with
169 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
174 match get_nth context n with
175 (Some (C.Name s,_)) -> s
176 | _ -> raise NameExpected
178 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
179 if innersort = "Prop" && expected_available then
180 add_inner_type fresh_id'' ;
181 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
182 | C.Var (uri,exp_named_subst) ->
183 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
184 if innersort = "Prop" && expected_available then
185 add_inner_type fresh_id'' ;
186 let exp_named_subst' =
188 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
190 C.AVar (fresh_id'', uri,exp_named_subst')
192 let (_,canonical_context,_) =
193 List.find (function (m,_,_) -> n = m) metasenv
195 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
196 if innersort = "Prop" && expected_available then
197 add_inner_type fresh_id'' ;
198 C.AMeta (fresh_id'', n,
203 | _, Some t -> Some (aux' context idrefs t)
204 | Some _, None -> assert false (* due to typing rules *))
205 canonical_context l))
206 | C.Sort s -> C.ASort (fresh_id'', s)
207 | C.Implicit -> C.AImplicit (fresh_id'')
209 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
210 if innersort = "Prop" then
211 add_inner_type fresh_id'' ;
212 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
214 xxx_add ids_to_inner_sorts fresh_id''
215 (string_of_sort innertype) ;
216 let sourcetype = xxx_type_of_aux' metasenv context s in
217 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
218 (string_of_sort sourcetype) ;
223 if DoubleTypeInference.does_not_occur 1 t then
229 (fresh_id'', n', aux' context idrefs s,
230 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
231 | C.Lambda (n,s,t) ->
232 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
233 let sourcetype = xxx_type_of_aux' metasenv context s in
234 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
235 (string_of_sort sourcetype) ;
236 if innersort = "Prop" then
238 let father_is_lambda =
242 match Hashtbl.find ids_to_terms father' with
246 if (not father_is_lambda) || expected_available then
247 add_inner_type fresh_id''
250 (fresh_id'',n, aux' context idrefs s,
251 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
253 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
254 if innersort = "Prop" then
255 add_inner_type fresh_id'' ;
257 (fresh_id'', n, aux' context idrefs s,
258 aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
260 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
261 if innersort = "Prop" then
262 add_inner_type fresh_id'' ;
263 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
264 | C.Const (uri,exp_named_subst) ->
265 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
266 if innersort = "Prop" && expected_available then
267 add_inner_type fresh_id'' ;
268 let exp_named_subst' =
270 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
272 C.AConst (fresh_id'', uri, exp_named_subst')
273 | C.MutInd (uri,tyno,exp_named_subst) ->
274 let exp_named_subst' =
276 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
278 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
279 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
280 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
281 if innersort = "Prop" && expected_available then
282 add_inner_type fresh_id'' ;
283 let exp_named_subst' =
285 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
287 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
288 | C.MutCase (uri, tyno, outty, term, patterns) ->
289 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
290 if innersort = "Prop" then
291 add_inner_type fresh_id'' ;
292 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
293 aux' context idrefs term, List.map (aux' context idrefs) patterns)
294 | C.Fix (funno, funs) ->
296 List.map (function _ -> gen_id seed) funs in
297 let new_idrefs = List.rev fresh_idrefs @ idrefs in
299 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
301 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
302 if innersort = "Prop" then
303 add_inner_type fresh_id'' ;
304 C.AFix (fresh_id'', funno,
306 (fun id (name, indidx, ty, bo) ->
307 (id, name, indidx, aux' context idrefs ty,
308 aux' (tys@context) new_idrefs bo)
311 | C.CoFix (funno, funs) ->
313 List.map (function _ -> gen_id seed) funs in
314 let new_idrefs = List.rev fresh_idrefs @ idrefs in
316 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
318 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
319 if innersort = "Prop" then
320 add_inner_type fresh_id'' ;
321 C.ACoFix (fresh_id'', funno,
323 (fun id (name, ty, bo) ->
324 (id, name, aux' context idrefs ty,
325 aux' (tys@context) new_idrefs bo)
329 let timea = Sys.time () in
330 let res = aux true None context idrefs t in
331 let timeb = Sys.time () in
333 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
337 let acic_of_cic_context metasenv context idrefs t =
338 let ids_to_terms = Hashtbl.create 503 in
339 let ids_to_father_ids = Hashtbl.create 503 in
340 let ids_to_inner_sorts = Hashtbl.create 503 in
341 let ids_to_inner_types = Hashtbl.create 503 in
343 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
344 ids_to_inner_types metasenv context idrefs t,
345 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
348 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
349 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
350 metasenv (metano,context,goal) =
351 let acic_of_cic_context =
352 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
353 ids_to_inner_types metasenv in
354 let _, acontext,final_idrefs =
356 (fun binding (context, acontext,idrefs) ->
357 let hid = "h" ^ string_of_int !hypotheses_seed in
358 Hashtbl.add ids_to_hypotheses hid binding ;
359 incr hypotheses_seed ;
361 Some (n,Cic.Def (t,None)) ->
362 let acic = acic_of_cic_context context idrefs t None in
364 ((hid,Some (n,Cic.ADef acic))::acontext),(hid::idrefs)
365 | Some (n,Cic.Decl t) ->
366 let acic = acic_of_cic_context context idrefs t None in
368 ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
370 (* Invariant: "" is never looked up *)
371 (None::context),((hid,None)::acontext),""::idrefs
372 | Some (_,Cic.Def (_,Some _)) -> assert false
376 let agoal = acic_of_cic_context context final_idrefs goal None in
377 (metano,acontext,agoal)
380 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) =
381 let ids_to_terms = Hashtbl.create 503 in
382 let ids_to_father_ids = Hashtbl.create 503 in
383 let ids_to_inner_sorts = Hashtbl.create 503 in
384 let ids_to_inner_types = Hashtbl.create 503 in
385 let ids_to_hypotheses = Hashtbl.create 23 in
386 let hypotheses_seed = ref 0 in
387 let seed = ref 1 in (* 'i0' is used for the whole sequent *)
388 let (metano,acontext,agoal) =
389 aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
390 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
392 ("i0",metano,acontext,agoal),
393 ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses
396 let acic_object_of_cic_object obj =
397 let module C = Cic in
398 let module E = Eta_fixing in
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 ids_to_conjectures = Hashtbl.create 11 in
404 let ids_to_hypotheses = Hashtbl.create 127 in
405 let hypotheses_seed = ref 0 in
406 let conjectures_seed = ref 0 in
408 let acic_term_of_cic_term_context' =
409 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
410 ids_to_inner_types in
411 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
412 let aconjecture_of_conjecture' = aconjecture_of_conjecture seed
413 ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
414 ids_to_hypotheses hypotheses_seed in
417 C.Constant (id,Some bo,ty,params) ->
418 let bo' = E.eta_fix [] bo in
419 let ty' = E.eta_fix [] ty in
420 let abo = acic_term_of_cic_term' bo' (Some ty') in
421 let aty = acic_term_of_cic_term' ty' None in
423 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
424 | C.Constant (id,None,ty,params) ->
425 let ty' = E.eta_fix [] ty in
426 let aty = acic_term_of_cic_term' ty' None in
428 ("mettereaposto",None,id,None,aty,params)
429 | C.Variable (id,bo,ty,params) ->
430 let ty' = E.eta_fix [] ty in
435 let bo' = E.eta_fix [] bo in
436 Some (acic_term_of_cic_term' bo' (Some ty'))
438 let aty = acic_term_of_cic_term' ty' None in
440 ("mettereaposto",id,abo,aty, params)
441 | C.CurrentProof (id,conjectures,bo,ty,params) ->
444 (function (i,canonical_context,term) ->
445 let canonical_context' =
449 | Some (n, C.Decl t)-> Some (n, C.Decl (E.eta_fix conjectures t))
450 | Some (n, C.Def (t,None)) ->
451 Some (n, C.Def ((E.eta_fix conjectures t),None))
452 | Some (_,C.Def (_,Some _)) -> assert false
455 let term' = E.eta_fix conjectures term in
456 (i,canonical_context',term')
461 (function (i,canonical_context,term) as conjecture ->
462 let cid = "c" ^ string_of_int !conjectures_seed in
463 xxx_add ids_to_conjectures cid conjecture ;
464 incr conjectures_seed ;
465 let (i,acanonical_context,aterm)
466 = aconjecture_of_conjecture' conjectures conjecture in
467 (cid,i,acanonical_context,aterm))
469 (* let idrefs',revacanonical_context =
470 let rec aux context idrefs =
474 let hid = "h" ^ string_of_int !hypotheses_seed in
475 let new_idrefs = hid::idrefs in
476 xxx_add ids_to_hypotheses hid hyp ;
477 incr hypotheses_seed ;
479 (Some (n,C.Decl t)) ->
480 let final_idrefs,atl =
481 aux (hyp::context) new_idrefs tl in
483 acic_term_of_cic_term_context'
484 conjectures context idrefs t None
486 final_idrefs,(hid,Some (n,C.ADecl at))::atl
487 | (Some (n,C.Def (t,_))) ->
488 let final_idrefs,atl =
489 aux (hyp::context) new_idrefs tl in
491 acic_term_of_cic_term_context'
492 conjectures context idrefs t None
494 final_idrefs,(hid,Some (n,C.ADef at))::atl
496 let final_idrefs,atl =
497 aux (hyp::context) new_idrefs tl
499 final_idrefs,(hid,None)::atl
501 aux [] [] (List.rev canonical_context)
504 acic_term_of_cic_term_context' conjectures
505 canonical_context idrefs' term None
507 (cid,i,(List.rev revacanonical_context),aterm)
509 let time1 = Sys.time () in
510 let bo' = E.eta_fix conjectures' bo in
511 let ty' = E.eta_fix conjectures' ty in
512 let time2 = Sys.time () in
514 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
515 hashtbl_add_time := 0.0 ;
516 type_of_aux'_add_time := 0.0 ;
517 DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
519 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
520 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
521 let time3 = Sys.time () in
523 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
525 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
527 ("++++++++++++ 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) ;
529 ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
531 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
533 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
535 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
536 | C.InductiveDefinition (tys,params,paramsno) ->
539 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
540 let idrefs = List.map (function _ -> gen_id seed) tys in
543 (fun id (name,inductive,ty,cons) ->
546 (function (name,ty) ->
548 acic_term_of_cic_term_context' [] context idrefs ty None)
551 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
552 ) (List.rev idrefs) tys
554 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
556 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
557 ids_to_conjectures,ids_to_hypotheses