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" (* TASSI OK*)
112 | C.Sort C.CProp -> "CProp"
114 prerr_endline "Cic2acic: string_of_sort applied to a meta" ;
118 let ainnertypes,innertype,innersort,expected_available =
119 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
120 (*CSC: (expected type + inferred type). Just for now we use the usual *)
121 (*CSC: type-inference, but the result is very poor. As a very weak *)
122 (*CSC: patch, I apply whd to the computed type. Full beta *)
123 (*CSC: reduction would be a much better option. *)
124 (*CSC: solo per testare i tempi *)
128 let {D.synthesized = synthesized; D.expected = expected} =
129 if computeinnertypes then
130 D.CicHash.find terms_to_types tt
132 (* We are already in an inner-type and Coscoy's double *)
133 (* type inference algorithm has not been applied. *)
135 (***CSC: patch per provare i tempi
136 CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
137 Cic.Sort (Cic.Type (CicUniv.fresh())); (* TASSI: non dovrebbe fare danni *)
140 incr number_new_type_of_aux' ;
141 let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
142 let ainnertypes,expected_available =
143 if computeinnertypes then
144 let annexpected,expected_available =
147 | Some expectedty' ->
149 (aux false (Some fresh_id'') context idrefs expectedty'),
154 aux false (Some fresh_id'') context idrefs synthesized ;
155 annexpected = annexpected
156 }, expected_available
160 ainnertypes,synthesized, string_of_sort innersort, expected_available
163 Not_found -> (* l'inner-type non e' nella tabella ==> sort <> Prop *)
164 (* CSC: Type or Set? I can not tell *)
165 None,Cic.Sort (Cic.Type (CicUniv.fresh())),"Type",false
166 (* TASSI non dovrebbe fare danni *)
169 let add_inner_type id =
170 match ainnertypes with
172 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
177 match get_nth context n with
178 (Some (C.Name s,_)) -> s
179 | _ -> "__" ^ string_of_int n
181 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
182 if innersort = "Prop" && expected_available then
183 add_inner_type fresh_id'' ;
184 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
185 | C.Var (uri,exp_named_subst) ->
186 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
187 if innersort = "Prop" && expected_available then
188 add_inner_type fresh_id'' ;
189 let exp_named_subst' =
191 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
193 C.AVar (fresh_id'', uri,exp_named_subst')
195 let (_,canonical_context,_) =
196 List.find (function (m,_,_) -> n = m) metasenv
198 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
199 if innersort = "Prop" && expected_available then
200 add_inner_type fresh_id'' ;
201 C.AMeta (fresh_id'', n,
206 | _, Some t -> Some (aux' context idrefs t)
207 | Some _, None -> assert false (* due to typing rules *))
208 canonical_context l))
209 | C.Sort s -> C.ASort (fresh_id'', s)
210 | C.Implicit annotation -> C.AImplicit (fresh_id'', annotation)
212 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
213 if innersort = "Prop" then
214 add_inner_type fresh_id'' ;
215 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
217 xxx_add ids_to_inner_sorts fresh_id''
218 (string_of_sort innertype) ;
219 let sourcetype = xxx_type_of_aux' metasenv context s in
220 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
221 (string_of_sort sourcetype) ;
226 if DoubleTypeInference.does_not_occur 1 t then
232 (fresh_id'', n', aux' context idrefs s,
233 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
234 | C.Lambda (n,s,t) ->
235 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
236 let sourcetype = xxx_type_of_aux' metasenv context s in
237 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
238 (string_of_sort sourcetype) ;
239 if innersort = "Prop" then
241 let father_is_lambda =
245 match Hashtbl.find ids_to_terms father' with
249 if (not father_is_lambda) || expected_available then
250 add_inner_type fresh_id''
253 (fresh_id'',n, aux' context idrefs s,
254 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
256 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
257 if innersort = "Prop" then
258 add_inner_type fresh_id'' ;
260 (fresh_id'', n, aux' context idrefs s,
261 aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
263 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
264 if innersort = "Prop" then
265 add_inner_type fresh_id'' ;
266 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
267 | C.Const (uri,exp_named_subst) ->
268 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
269 if innersort = "Prop" && expected_available then
270 add_inner_type fresh_id'' ;
271 let exp_named_subst' =
273 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
275 C.AConst (fresh_id'', uri, exp_named_subst')
276 | C.MutInd (uri,tyno,exp_named_subst) ->
277 let exp_named_subst' =
279 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
281 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
282 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
283 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
284 if innersort = "Prop" && expected_available then
285 add_inner_type fresh_id'' ;
286 let exp_named_subst' =
288 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
290 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
291 | C.MutCase (uri, tyno, outty, term, patterns) ->
292 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
293 if innersort = "Prop" then
294 add_inner_type fresh_id'' ;
295 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
296 aux' context idrefs term, List.map (aux' context idrefs) patterns)
297 | C.Fix (funno, funs) ->
299 List.map (function _ -> gen_id seed) funs in
300 let new_idrefs = List.rev fresh_idrefs @ idrefs in
302 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
304 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
305 if innersort = "Prop" then
306 add_inner_type fresh_id'' ;
307 C.AFix (fresh_id'', funno,
309 (fun id (name, indidx, ty, bo) ->
310 (id, name, indidx, aux' context idrefs ty,
311 aux' (tys@context) new_idrefs bo)
314 | C.CoFix (funno, funs) ->
316 List.map (function _ -> gen_id seed) funs in
317 let new_idrefs = List.rev fresh_idrefs @ idrefs in
319 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
321 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
322 if innersort = "Prop" then
323 add_inner_type fresh_id'' ;
324 C.ACoFix (fresh_id'', funno,
326 (fun id (name, ty, bo) ->
327 (id, name, aux' context idrefs ty,
328 aux' (tys@context) new_idrefs bo)
332 let timea = Sys.time () in
333 let res = aux true None context idrefs t in
334 let timeb = Sys.time () in
336 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
340 let acic_of_cic_context metasenv context idrefs t =
341 let ids_to_terms = Hashtbl.create 503 in
342 let ids_to_father_ids = Hashtbl.create 503 in
343 let ids_to_inner_sorts = Hashtbl.create 503 in
344 let ids_to_inner_types = Hashtbl.create 503 in
346 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
347 ids_to_inner_types metasenv context idrefs t,
348 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
351 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
352 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
353 metasenv (metano,context,goal) =
354 let acic_of_cic_context =
355 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
356 ids_to_inner_types metasenv in
357 let _, acontext,final_idrefs =
359 (fun binding (context, acontext,idrefs) ->
360 let hid = "h" ^ string_of_int !hypotheses_seed in
361 Hashtbl.add ids_to_hypotheses hid binding ;
362 incr hypotheses_seed ;
364 Some (n,Cic.Def (t,None)) ->
365 let acic = acic_of_cic_context context idrefs t None in
367 ((hid,Some (n,Cic.ADef acic))::acontext),(hid::idrefs)
368 | Some (n,Cic.Decl t) ->
369 let acic = acic_of_cic_context context idrefs t None in
371 ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
373 (* Invariant: "" is never looked up *)
374 (None::context),((hid,None)::acontext),""::idrefs
375 | Some (_,Cic.Def (_,Some _)) -> assert false
379 let agoal = acic_of_cic_context context final_idrefs goal None in
380 (metano,acontext,agoal)
383 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) =
384 let ids_to_terms = Hashtbl.create 503 in
385 let ids_to_father_ids = Hashtbl.create 503 in
386 let ids_to_inner_sorts = Hashtbl.create 503 in
387 let ids_to_inner_types = Hashtbl.create 503 in
388 let ids_to_hypotheses = Hashtbl.create 23 in
389 let hypotheses_seed = ref 0 in
390 let seed = ref 1 in (* 'i0' is used for the whole sequent *)
391 let (metano,acontext,agoal) =
392 aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
393 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
395 ("i0",metano,acontext,agoal),
396 ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses
399 let acic_object_of_cic_object ?(eta_fix=true) obj =
400 let module C = Cic in
401 let module E = Eta_fixing in
402 let ids_to_terms = Hashtbl.create 503 in
403 let ids_to_father_ids = Hashtbl.create 503 in
404 let ids_to_inner_sorts = Hashtbl.create 503 in
405 let ids_to_inner_types = Hashtbl.create 503 in
406 let ids_to_conjectures = Hashtbl.create 11 in
407 let ids_to_hypotheses = Hashtbl.create 127 in
408 let hypotheses_seed = ref 0 in
409 let conjectures_seed = ref 0 in
411 let acic_term_of_cic_term_context' =
412 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
413 ids_to_inner_types in
414 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
415 let aconjecture_of_conjecture' = aconjecture_of_conjecture seed
416 ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
417 ids_to_hypotheses hypotheses_seed in
418 let eta_fix metasenv context t =
419 if eta_fix then E.eta_fix metasenv context t else t
423 C.Constant (id,Some bo,ty,params) ->
424 let bo' = eta_fix [] [] bo in
425 let ty' = eta_fix [] [] ty in
426 let abo = acic_term_of_cic_term' bo' (Some ty') in
427 let aty = acic_term_of_cic_term' ty' None in
429 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
430 | C.Constant (id,None,ty,params) ->
431 let ty' = eta_fix [] [] ty in
432 let aty = acic_term_of_cic_term' ty' None in
434 ("mettereaposto",None,id,None,aty,params)
435 | C.Variable (id,bo,ty,params) ->
436 let ty' = eta_fix [] [] ty in
441 let bo' = eta_fix [] [] bo in
442 Some (acic_term_of_cic_term' bo' (Some ty'))
444 let aty = acic_term_of_cic_term' ty' None in
446 ("mettereaposto",id,abo,aty, params)
447 | C.CurrentProof (id,conjectures,bo,ty,params) ->
450 (function (i,canonical_context,term) ->
451 let canonical_context' =
453 (fun d canonical_context' ->
457 | Some (n, C.Decl t)->
458 Some (n, C.Decl (eta_fix conjectures canonical_context' t))
459 | Some (n, C.Def (t,None)) ->
461 C.Def ((eta_fix conjectures canonical_context' t),None))
462 | Some (_,C.Def (_,Some _)) -> assert false
464 d::canonical_context'
465 ) [] canonical_context
467 let term' = eta_fix conjectures canonical_context' term in
468 (i,canonical_context',term')
473 (function (i,canonical_context,term) as conjecture ->
474 let cid = "c" ^ string_of_int !conjectures_seed in
475 xxx_add ids_to_conjectures cid conjecture ;
476 incr conjectures_seed ;
477 let (i,acanonical_context,aterm)
478 = aconjecture_of_conjecture' conjectures conjecture in
479 (cid,i,acanonical_context,aterm))
481 (* let idrefs',revacanonical_context =
482 let rec aux context idrefs =
486 let hid = "h" ^ string_of_int !hypotheses_seed in
487 let new_idrefs = hid::idrefs in
488 xxx_add ids_to_hypotheses hid hyp ;
489 incr hypotheses_seed ;
491 (Some (n,C.Decl t)) ->
492 let final_idrefs,atl =
493 aux (hyp::context) new_idrefs tl in
495 acic_term_of_cic_term_context'
496 conjectures context idrefs t None
498 final_idrefs,(hid,Some (n,C.ADecl at))::atl
499 | (Some (n,C.Def (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.ADef at))::atl
508 let final_idrefs,atl =
509 aux (hyp::context) new_idrefs tl
511 final_idrefs,(hid,None)::atl
513 aux [] [] (List.rev canonical_context)
516 acic_term_of_cic_term_context' conjectures
517 canonical_context idrefs' term None
519 (cid,i,(List.rev revacanonical_context),aterm)
521 let time1 = Sys.time () in
522 let bo' = eta_fix conjectures' [] bo in
523 let ty' = eta_fix conjectures' [] ty in
524 let time2 = Sys.time () in
526 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
527 hashtbl_add_time := 0.0 ;
528 type_of_aux'_add_time := 0.0 ;
529 DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
531 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
532 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
533 let time3 = Sys.time () in
535 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
537 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
539 ("++++++++++++ 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) ;
541 ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
543 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
545 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
547 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
548 | C.InductiveDefinition (tys,params,paramsno) ->
551 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
552 let idrefs = List.map (function _ -> gen_id seed) tys in
555 (fun id (name,inductive,ty,cons) ->
558 (function (name,ty) ->
560 acic_term_of_cic_term_context' [] context idrefs ty None)
563 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
564 ) (List.rev idrefs) tys
566 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
568 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
569 ids_to_conjectures,ids_to_hypotheses