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 CicUniv.empty_ugraph 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
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 D.double_type_of metasenv context t expectedty
99 let time2 = Sys.time () in
101 ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
103 let rec aux computeinnertypes father context idrefs tt =
104 let fresh_id'' = fresh_id' father tt in
105 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
106 let aux' = aux computeinnertypes (Some fresh_id'') in
107 (* First of all we compute the inner type and the inner sort *)
108 (* of the term. They may be useful in what follows. *)
109 (*CSC: This is a very inefficient way of computing inner types *)
110 (*CSC: and inner sorts: very deep terms have their types/sorts *)
111 (*CSC: computed again and again. *)
112 let string_of_sort t =
113 match CicReduction.whd context t with
114 C.Sort C.Prop -> "Prop"
115 | C.Sort C.Set -> "Set"
116 | C.Sort (C.Type _) -> "Type" (* TASSI OK*)
117 | C.Sort C.CProp -> "CProp"
119 prerr_endline "Cic2acic: string_of_sort applied to a meta" ;
122 prerr_endline ("Cic2acic: string_of_sort applied to: " ^ CicPp.ppterm t) ;
125 let ainnertypes,innertype,innersort,expected_available =
126 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
127 (*CSC: (expected type + inferred type). Just for now we use the usual *)
128 (*CSC: type-inference, but the result is very poor. As a very weak *)
129 (*CSC: patch, I apply whd to the computed type. Full beta *)
130 (*CSC: reduction would be a much better option. *)
131 (*CSC: solo per testare i tempi *)
135 let {D.synthesized = synthesized; D.expected = expected} =
136 if computeinnertypes then
137 D.CicHash.find terms_to_types tt
139 (* We are already in an inner-type and Coscoy's double *)
140 (* type inference algorithm has not been applied. *)
142 (***CSC: patch per provare i tempi
143 CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
144 Cic.Sort (Cic.Type (CicUniv.fresh())); (* TASSI: non dovrebbe fare danni *)
147 incr number_new_type_of_aux' ;
148 let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
149 let ainnertypes,expected_available =
150 if computeinnertypes then
151 let annexpected,expected_available =
154 | Some expectedty' ->
156 (aux false (Some fresh_id'') context idrefs expectedty'),
161 aux false (Some fresh_id'') context idrefs synthesized ;
162 annexpected = annexpected
163 }, expected_available
167 ainnertypes,synthesized, string_of_sort innersort, expected_available
170 Not_found -> (* l'inner-type non e' nella tabella ==> sort <> Prop *)
171 (* CSC: Type or Set? I can not tell *)
172 None,Cic.Sort (Cic.Type (CicUniv.fresh())),"Type",false
173 (* TASSI non dovrebbe fare danni *)
176 let add_inner_type id =
177 match ainnertypes with
179 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
184 match get_nth context n with
185 (Some (C.Name s,_)) -> s
186 | _ -> "__" ^ string_of_int n
188 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
189 if innersort = "Prop" && expected_available then
190 add_inner_type fresh_id'' ;
191 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
192 | C.Var (uri,exp_named_subst) ->
193 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
194 if innersort = "Prop" && expected_available then
195 add_inner_type fresh_id'' ;
196 let exp_named_subst' =
198 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
200 C.AVar (fresh_id'', uri,exp_named_subst')
202 let (_,canonical_context,_) = CicUtil.lookup_meta n metasenv in
203 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
204 if innersort = "Prop" && expected_available then
205 add_inner_type fresh_id'' ;
206 C.AMeta (fresh_id'', n,
211 | _, Some t -> Some (aux' context idrefs t)
212 | Some _, None -> assert false (* due to typing rules *))
213 canonical_context l))
214 | C.Sort s -> C.ASort (fresh_id'', s)
215 | C.Implicit annotation -> C.AImplicit (fresh_id'', annotation)
217 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
218 if innersort = "Prop" then
219 add_inner_type fresh_id'' ;
220 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
222 xxx_add ids_to_inner_sorts fresh_id''
223 (string_of_sort innertype) ;
224 let sourcetype = xxx_type_of_aux' metasenv context s in
225 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
226 (string_of_sort sourcetype) ;
231 if DoubleTypeInference.does_not_occur 1 t then
237 (fresh_id'', n', aux' context idrefs s,
238 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
239 | C.Lambda (n,s,t) ->
240 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
241 let sourcetype = xxx_type_of_aux' metasenv context s in
242 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
243 (string_of_sort sourcetype) ;
244 if innersort = "Prop" then
246 let father_is_lambda =
250 match Hashtbl.find ids_to_terms father' with
254 if (not father_is_lambda) || expected_available then
255 add_inner_type fresh_id''
258 (fresh_id'',n, aux' context idrefs s,
259 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
261 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
262 if innersort = "Prop" then
263 add_inner_type fresh_id'' ;
265 (fresh_id'', n, aux' context idrefs s,
266 aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
268 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
269 if innersort = "Prop" then
270 add_inner_type fresh_id'' ;
271 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
272 | C.Const (uri,exp_named_subst) ->
273 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
274 if innersort = "Prop" && expected_available then
275 add_inner_type fresh_id'' ;
276 let exp_named_subst' =
278 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
280 C.AConst (fresh_id'', uri, exp_named_subst')
281 | C.MutInd (uri,tyno,exp_named_subst) ->
282 let exp_named_subst' =
284 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
286 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
287 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
288 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
289 if innersort = "Prop" && expected_available then
290 add_inner_type fresh_id'' ;
291 let exp_named_subst' =
293 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
295 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
296 | C.MutCase (uri, tyno, outty, term, patterns) ->
297 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
298 if innersort = "Prop" then
299 add_inner_type fresh_id'' ;
300 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
301 aux' context idrefs term, List.map (aux' context idrefs) patterns)
302 | C.Fix (funno, funs) ->
304 List.map (function _ -> gen_id seed) funs in
305 let new_idrefs = List.rev fresh_idrefs @ idrefs in
307 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
309 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
310 if innersort = "Prop" then
311 add_inner_type fresh_id'' ;
312 C.AFix (fresh_id'', funno,
314 (fun id (name, indidx, ty, bo) ->
315 (id, name, indidx, aux' context idrefs ty,
316 aux' (tys@context) new_idrefs bo)
319 | C.CoFix (funno, funs) ->
321 List.map (function _ -> gen_id seed) funs in
322 let new_idrefs = List.rev fresh_idrefs @ idrefs in
324 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
326 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
327 if innersort = "Prop" then
328 add_inner_type fresh_id'' ;
329 C.ACoFix (fresh_id'', funno,
331 (fun id (name, ty, bo) ->
332 (id, name, aux' context idrefs ty,
333 aux' (tys@context) new_idrefs bo)
338 let timea = Sys.time () in
339 let res = aux true None context idrefs t in
340 let timeb = Sys.time () in
342 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
345 aux true None context idrefs t
348 let acic_of_cic_context metasenv context idrefs t =
349 let ids_to_terms = Hashtbl.create 503 in
350 let ids_to_father_ids = Hashtbl.create 503 in
351 let ids_to_inner_sorts = Hashtbl.create 503 in
352 let ids_to_inner_types = Hashtbl.create 503 in
354 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
355 ids_to_inner_types metasenv context idrefs t,
356 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
359 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
360 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
361 metasenv (metano,context,goal) =
362 let acic_of_cic_context =
363 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
364 ids_to_inner_types metasenv in
365 let _, acontext,final_idrefs =
367 (fun binding (context, acontext,idrefs) ->
368 let hid = "h" ^ string_of_int !hypotheses_seed in
369 Hashtbl.add ids_to_hypotheses hid binding ;
370 incr hypotheses_seed ;
372 Some (n,Cic.Def (t,None)) ->
373 let acic = acic_of_cic_context context idrefs t None in
375 ((hid,Some (n,Cic.ADef acic))::acontext),(hid::idrefs)
376 | Some (n,Cic.Decl t) ->
377 let acic = acic_of_cic_context context idrefs t None in
379 ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
381 (* Invariant: "" is never looked up *)
382 (None::context),((hid,None)::acontext),""::idrefs
383 | Some (_,Cic.Def (_,Some _)) -> assert false
387 let agoal = acic_of_cic_context context final_idrefs goal None in
388 (metano,acontext,agoal)
391 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) =
392 let ids_to_terms = Hashtbl.create 503 in
393 let ids_to_father_ids = Hashtbl.create 503 in
394 let ids_to_inner_sorts = Hashtbl.create 503 in
395 let ids_to_inner_types = Hashtbl.create 503 in
396 let ids_to_hypotheses = Hashtbl.create 23 in
397 let hypotheses_seed = ref 0 in
398 let seed = ref 1 in (* 'i0' is used for the whole sequent *)
399 let (metano,acontext,agoal) =
400 aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
401 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
403 ("i0",metano,acontext,agoal),
404 ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses
407 let acic_object_of_cic_object ?(eta_fix=true) obj =
408 let module C = Cic in
409 let module E = Eta_fixing in
410 let ids_to_terms = Hashtbl.create 503 in
411 let ids_to_father_ids = Hashtbl.create 503 in
412 let ids_to_inner_sorts = Hashtbl.create 503 in
413 let ids_to_inner_types = Hashtbl.create 503 in
414 let ids_to_conjectures = Hashtbl.create 11 in
415 let ids_to_hypotheses = Hashtbl.create 127 in
416 let hypotheses_seed = ref 0 in
417 let conjectures_seed = ref 0 in
419 let acic_term_of_cic_term_context' =
420 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
421 ids_to_inner_types in
422 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
423 let aconjecture_of_conjecture' = aconjecture_of_conjecture seed
424 ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
425 ids_to_hypotheses hypotheses_seed in
426 let eta_fix metasenv context t =
427 if eta_fix then E.eta_fix metasenv context t else t
431 C.Constant (id,Some bo,ty,params) ->
432 let bo' = eta_fix [] [] bo in
433 let ty' = eta_fix [] [] ty in
434 let abo = acic_term_of_cic_term' bo' (Some ty') in
435 let aty = acic_term_of_cic_term' ty' None in
437 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
438 | C.Constant (id,None,ty,params) ->
439 let ty' = eta_fix [] [] ty in
440 let aty = acic_term_of_cic_term' ty' None in
442 ("mettereaposto",None,id,None,aty,params)
443 | C.Variable (id,bo,ty,params) ->
444 let ty' = eta_fix [] [] ty in
449 let bo' = eta_fix [] [] bo in
450 Some (acic_term_of_cic_term' bo' (Some ty'))
452 let aty = acic_term_of_cic_term' ty' None in
454 ("mettereaposto",id,abo,aty, params)
455 | C.CurrentProof (id,conjectures,bo,ty,params) ->
458 (function (i,canonical_context,term) ->
459 let canonical_context' =
461 (fun d canonical_context' ->
465 | Some (n, C.Decl t)->
466 Some (n, C.Decl (eta_fix conjectures canonical_context' t))
467 | Some (n, C.Def (t,None)) ->
469 C.Def ((eta_fix conjectures canonical_context' t),None))
470 | Some (_,C.Def (_,Some _)) -> assert false
472 d::canonical_context'
473 ) [] canonical_context
475 let term' = eta_fix conjectures canonical_context' term in
476 (i,canonical_context',term')
481 (function (i,canonical_context,term) as conjecture ->
482 let cid = "c" ^ string_of_int !conjectures_seed in
483 xxx_add ids_to_conjectures cid conjecture ;
484 incr conjectures_seed ;
485 let (i,acanonical_context,aterm)
486 = aconjecture_of_conjecture' conjectures conjecture in
487 (cid,i,acanonical_context,aterm))
489 (* let idrefs',revacanonical_context =
490 let rec aux context idrefs =
494 let hid = "h" ^ string_of_int !hypotheses_seed in
495 let new_idrefs = hid::idrefs in
496 xxx_add ids_to_hypotheses hid hyp ;
497 incr hypotheses_seed ;
499 (Some (n,C.Decl t)) ->
500 let final_idrefs,atl =
501 aux (hyp::context) new_idrefs tl in
503 acic_term_of_cic_term_context'
504 conjectures context idrefs t None
506 final_idrefs,(hid,Some (n,C.ADecl at))::atl
507 | (Some (n,C.Def (t,_))) ->
508 let final_idrefs,atl =
509 aux (hyp::context) new_idrefs tl in
511 acic_term_of_cic_term_context'
512 conjectures context idrefs t None
514 final_idrefs,(hid,Some (n,C.ADef at))::atl
516 let final_idrefs,atl =
517 aux (hyp::context) new_idrefs tl
519 final_idrefs,(hid,None)::atl
521 aux [] [] (List.rev canonical_context)
524 acic_term_of_cic_term_context' conjectures
525 canonical_context idrefs' term None
527 (cid,i,(List.rev revacanonical_context),aterm)
529 (* let time1 = Sys.time () in *)
530 let bo' = eta_fix conjectures' [] bo in
531 let ty' = eta_fix conjectures' [] ty in
533 let time2 = Sys.time () in
535 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
536 hashtbl_add_time := 0.0 ;
537 type_of_aux'_add_time := 0.0 ;
538 DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
541 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
542 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
544 let time3 = Sys.time () in
546 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
548 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
550 ("++++++++++++ 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) ;
552 ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
554 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
556 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
559 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
560 | C.InductiveDefinition (tys,params,paramsno) ->
563 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
564 let idrefs = List.map (function _ -> gen_id seed) tys in
567 (fun id (name,inductive,ty,cons) ->
570 (function (name,ty) ->
572 acic_term_of_cic_term_context' [] context idrefs ty None)
575 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
576 ) (List.rev idrefs) tys
578 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
580 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
581 ids_to_conjectures,ids_to_hypotheses