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"
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) ; *)
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,"Type",false
168 let add_inner_type id =
169 match ainnertypes with
171 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
176 match get_nth context n with
177 (Some (C.Name s,_)) -> s
178 | _ -> "__" ^ string_of_int n
180 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
181 if innersort = "Prop" && expected_available then
182 add_inner_type fresh_id'' ;
183 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
184 | C.Var (uri,exp_named_subst) ->
185 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
186 if innersort = "Prop" && expected_available then
187 add_inner_type fresh_id'' ;
188 let exp_named_subst' =
190 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
192 C.AVar (fresh_id'', uri,exp_named_subst')
194 let (_,canonical_context,_) =
195 List.find (function (m,_,_) -> n = m) metasenv
197 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
198 if innersort = "Prop" && expected_available then
199 add_inner_type fresh_id'' ;
200 C.AMeta (fresh_id'', n,
205 | _, Some t -> Some (aux' context idrefs t)
206 | Some _, None -> assert false (* due to typing rules *))
207 canonical_context l))
208 | C.Sort s -> C.ASort (fresh_id'', s)
209 | C.Implicit annotation -> C.AImplicit (fresh_id'', annotation)
211 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
212 if innersort = "Prop" then
213 add_inner_type fresh_id'' ;
214 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
216 xxx_add ids_to_inner_sorts fresh_id''
217 (string_of_sort innertype) ;
218 let sourcetype = xxx_type_of_aux' metasenv context s in
219 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
220 (string_of_sort sourcetype) ;
225 if DoubleTypeInference.does_not_occur 1 t then
231 (fresh_id'', n', aux' context idrefs s,
232 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
233 | C.Lambda (n,s,t) ->
234 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
235 let sourcetype = xxx_type_of_aux' metasenv context s in
236 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
237 (string_of_sort sourcetype) ;
238 if innersort = "Prop" then
240 let father_is_lambda =
244 match Hashtbl.find ids_to_terms father' with
248 if (not father_is_lambda) || expected_available then
249 add_inner_type fresh_id''
252 (fresh_id'',n, aux' context idrefs s,
253 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
255 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
256 if innersort = "Prop" then
257 add_inner_type fresh_id'' ;
259 (fresh_id'', n, aux' context idrefs s,
260 aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
262 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
263 if innersort = "Prop" then
264 add_inner_type fresh_id'' ;
265 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
266 | C.Const (uri,exp_named_subst) ->
267 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
268 if innersort = "Prop" && expected_available then
269 add_inner_type fresh_id'' ;
270 let exp_named_subst' =
272 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
274 C.AConst (fresh_id'', uri, exp_named_subst')
275 | C.MutInd (uri,tyno,exp_named_subst) ->
276 let exp_named_subst' =
278 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
280 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
281 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
282 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
283 if innersort = "Prop" && expected_available then
284 add_inner_type fresh_id'' ;
285 let exp_named_subst' =
287 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
289 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
290 | C.MutCase (uri, tyno, outty, term, patterns) ->
291 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
292 if innersort = "Prop" then
293 add_inner_type fresh_id'' ;
294 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
295 aux' context idrefs term, List.map (aux' context idrefs) patterns)
296 | C.Fix (funno, funs) ->
298 List.map (function _ -> gen_id seed) funs in
299 let new_idrefs = List.rev fresh_idrefs @ idrefs in
301 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
303 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
304 if innersort = "Prop" then
305 add_inner_type fresh_id'' ;
306 C.AFix (fresh_id'', funno,
308 (fun id (name, indidx, ty, bo) ->
309 (id, name, indidx, aux' context idrefs ty,
310 aux' (tys@context) new_idrefs bo)
313 | C.CoFix (funno, funs) ->
315 List.map (function _ -> gen_id seed) funs in
316 let new_idrefs = List.rev fresh_idrefs @ idrefs in
318 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
320 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
321 if innersort = "Prop" then
322 add_inner_type fresh_id'' ;
323 C.ACoFix (fresh_id'', funno,
325 (fun id (name, ty, bo) ->
326 (id, name, aux' context idrefs ty,
327 aux' (tys@context) new_idrefs bo)
331 let timea = Sys.time () in
332 let res = aux true None context idrefs t in
333 let timeb = Sys.time () in
335 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
339 let acic_of_cic_context metasenv context idrefs t =
340 let ids_to_terms = Hashtbl.create 503 in
341 let ids_to_father_ids = Hashtbl.create 503 in
342 let ids_to_inner_sorts = Hashtbl.create 503 in
343 let ids_to_inner_types = Hashtbl.create 503 in
345 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
346 ids_to_inner_types metasenv context idrefs t,
347 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
350 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
351 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
352 metasenv (metano,context,goal) =
353 let acic_of_cic_context =
354 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
355 ids_to_inner_types metasenv in
356 let _, acontext,final_idrefs =
358 (fun binding (context, acontext,idrefs) ->
359 let hid = "h" ^ string_of_int !hypotheses_seed in
360 Hashtbl.add ids_to_hypotheses hid binding ;
361 incr hypotheses_seed ;
363 Some (n,Cic.Def (t,None)) ->
364 let acic = acic_of_cic_context context idrefs t None in
366 ((hid,Some (n,Cic.ADef acic))::acontext),(hid::idrefs)
367 | Some (n,Cic.Decl t) ->
368 let acic = acic_of_cic_context context idrefs t None in
370 ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
372 (* Invariant: "" is never looked up *)
373 (None::context),((hid,None)::acontext),""::idrefs
374 | Some (_,Cic.Def (_,Some _)) -> assert false
378 let agoal = acic_of_cic_context context final_idrefs goal None in
379 (metano,acontext,agoal)
382 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) =
383 let ids_to_terms = Hashtbl.create 503 in
384 let ids_to_father_ids = Hashtbl.create 503 in
385 let ids_to_inner_sorts = Hashtbl.create 503 in
386 let ids_to_inner_types = Hashtbl.create 503 in
387 let ids_to_hypotheses = Hashtbl.create 23 in
388 let hypotheses_seed = ref 0 in
389 let seed = ref 1 in (* 'i0' is used for the whole sequent *)
390 let (metano,acontext,agoal) =
391 aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
392 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
394 ("i0",metano,acontext,agoal),
395 ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses
398 let acic_object_of_cic_object ?(eta_fix=true) obj =
399 let module C = Cic in
400 let module E = Eta_fixing in
401 let ids_to_terms = Hashtbl.create 503 in
402 let ids_to_father_ids = Hashtbl.create 503 in
403 let ids_to_inner_sorts = Hashtbl.create 503 in
404 let ids_to_inner_types = Hashtbl.create 503 in
405 let ids_to_conjectures = Hashtbl.create 11 in
406 let ids_to_hypotheses = Hashtbl.create 127 in
407 let hypotheses_seed = ref 0 in
408 let conjectures_seed = ref 0 in
410 let acic_term_of_cic_term_context' =
411 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
412 ids_to_inner_types in
413 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
414 let aconjecture_of_conjecture' = aconjecture_of_conjecture seed
415 ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
416 ids_to_hypotheses hypotheses_seed in
417 let eta_fix context t =
418 if eta_fix then E.eta_fix context t else t
422 C.Constant (id,Some bo,ty,params) ->
423 let bo' = eta_fix [] bo in
424 let ty' = eta_fix [] ty in
425 let abo = acic_term_of_cic_term' bo' (Some ty') in
426 let aty = acic_term_of_cic_term' ty' None in
428 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
429 | C.Constant (id,None,ty,params) ->
430 let ty' = eta_fix [] ty in
431 let aty = acic_term_of_cic_term' ty' None in
433 ("mettereaposto",None,id,None,aty,params)
434 | C.Variable (id,bo,ty,params) ->
435 let ty' = eta_fix [] ty in
440 let bo' = eta_fix [] bo in
441 Some (acic_term_of_cic_term' bo' (Some ty'))
443 let aty = acic_term_of_cic_term' ty' None in
445 ("mettereaposto",id,abo,aty, params)
446 | C.CurrentProof (id,conjectures,bo,ty,params) ->
449 (function (i,canonical_context,term) ->
450 let canonical_context' =
454 | Some (n, C.Decl t)-> Some (n, C.Decl (eta_fix conjectures t))
455 | Some (n, C.Def (t,None)) ->
456 Some (n, C.Def ((eta_fix conjectures t),None))
457 | Some (_,C.Def (_,Some _)) -> assert false
460 let term' = eta_fix conjectures term in
461 (i,canonical_context',term')
466 (function (i,canonical_context,term) as conjecture ->
467 let cid = "c" ^ string_of_int !conjectures_seed in
468 xxx_add ids_to_conjectures cid conjecture ;
469 incr conjectures_seed ;
470 let (i,acanonical_context,aterm)
471 = aconjecture_of_conjecture' conjectures conjecture in
472 (cid,i,acanonical_context,aterm))
474 (* let idrefs',revacanonical_context =
475 let rec aux context idrefs =
479 let hid = "h" ^ string_of_int !hypotheses_seed in
480 let new_idrefs = hid::idrefs in
481 xxx_add ids_to_hypotheses hid hyp ;
482 incr hypotheses_seed ;
484 (Some (n,C.Decl t)) ->
485 let final_idrefs,atl =
486 aux (hyp::context) new_idrefs tl in
488 acic_term_of_cic_term_context'
489 conjectures context idrefs t None
491 final_idrefs,(hid,Some (n,C.ADecl at))::atl
492 | (Some (n,C.Def (t,_))) ->
493 let final_idrefs,atl =
494 aux (hyp::context) new_idrefs tl in
496 acic_term_of_cic_term_context'
497 conjectures context idrefs t None
499 final_idrefs,(hid,Some (n,C.ADef at))::atl
501 let final_idrefs,atl =
502 aux (hyp::context) new_idrefs tl
504 final_idrefs,(hid,None)::atl
506 aux [] [] (List.rev canonical_context)
509 acic_term_of_cic_term_context' conjectures
510 canonical_context idrefs' term None
512 (cid,i,(List.rev revacanonical_context),aterm)
514 let time1 = Sys.time () in
515 let bo' = eta_fix conjectures' bo in
516 let ty' = eta_fix conjectures' ty in
517 let time2 = Sys.time () in
519 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
520 hashtbl_add_time := 0.0 ;
521 type_of_aux'_add_time := 0.0 ;
522 DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
524 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
525 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
526 let time3 = Sys.time () in
528 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
530 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
532 ("++++++++++++ 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) ;
534 ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
536 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
538 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
540 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
541 | C.InductiveDefinition (tys,params,paramsno) ->
544 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
545 let idrefs = List.map (function _ -> gen_id seed) tys in
548 (fun id (name,inductive,ty,cons) ->
551 (function (name,ty) ->
553 acic_term_of_cic_term_context' [] context idrefs ty None)
556 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
557 ) (List.rev idrefs) tys
559 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
561 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
562 ids_to_conjectures,ids_to_hypotheses