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 D.double_type_of metasenv context t expectedty
89 let time2 = Sys.time () in
91 ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
92 let rec aux computeinnertypes father context idrefs tt =
93 let fresh_id'' = fresh_id' father tt in
94 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
95 let aux' = aux computeinnertypes (Some fresh_id'') in
96 (* First of all we compute the inner type and the inner sort *)
97 (* of the term. They may be useful in what follows. *)
98 (*CSC: This is a very inefficient way of computing inner types *)
99 (*CSC: and inner sorts: very deep terms have their types/sorts *)
100 (*CSC: computed again and again. *)
101 let string_of_sort t =
102 match CicReduction.whd context t with
103 C.Sort C.Prop -> "Prop"
104 | C.Sort C.Set -> "Set"
105 | C.Sort C.Type -> "Type"
106 | C.Sort C.CProp -> "CProp"
109 let ainnertypes,innertype,innersort,expected_available =
110 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
111 (*CSC: (expected type + inferred type). Just for now we use the usual *)
112 (*CSC: type-inference, but the result is very poor. As a very weak *)
113 (*CSC: patch, I apply whd to the computed type. Full beta *)
114 (*CSC: reduction would be a much better option. *)
115 (*CSC: solo per testare i tempi *)
119 let {D.synthesized = synthesized; D.expected = expected} =
120 if computeinnertypes then
121 D.CicHash.find terms_to_types tt
123 (* We are already in an inner-type and Coscoy's double *)
124 (* type inference algorithm has not been applied. *)
126 (***CSC: patch per provare i tempi
127 CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
131 incr number_new_type_of_aux' ;
132 let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
133 let ainnertypes,expected_available =
134 if computeinnertypes then
135 let annexpected,expected_available =
138 | Some expectedty' ->
140 (aux false (Some fresh_id'') context idrefs expectedty'),
145 aux false (Some fresh_id'') context idrefs synthesized ;
146 annexpected = annexpected
147 }, expected_available
151 ainnertypes,synthesized, string_of_sort innersort, expected_available
154 Not_found -> (* l'inner-type non e' nella tabella ==> sort <> Prop *)
155 (* CSC: Type or Set? I can not tell *)
156 None,Cic.Sort Cic.Type,"Type",false
159 let add_inner_type id =
160 match ainnertypes with
162 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
167 match get_nth context n with
168 (Some (C.Name s,_)) -> s
169 | _ -> raise NameExpected
171 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
172 if innersort = "Prop" && expected_available then
173 add_inner_type fresh_id'' ;
174 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
175 | C.Var (uri,exp_named_subst) ->
176 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
177 if innersort = "Prop" && expected_available then
178 add_inner_type fresh_id'' ;
179 let exp_named_subst' =
181 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
183 C.AVar (fresh_id'', uri,exp_named_subst')
185 let (_,canonical_context,_) =
186 List.find (function (m,_,_) -> n = m) metasenv
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.AMeta (fresh_id'', n,
196 | _, Some t -> Some (aux' context idrefs t)
197 | Some _, None -> assert false (* due to typing rules *))
198 canonical_context l))
199 | C.Sort s -> C.ASort (fresh_id'', s)
200 | C.Implicit -> C.AImplicit (fresh_id'')
202 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
203 if innersort = "Prop" then
204 add_inner_type fresh_id'' ;
205 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
207 xxx_add ids_to_inner_sorts fresh_id''
208 (string_of_sort innertype) ;
209 let sourcetype = xxx_type_of_aux' metasenv context s in
210 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
211 (string_of_sort sourcetype) ;
216 if DoubleTypeInference.does_not_occur 1 t then
222 (fresh_id'', n', aux' context idrefs s,
223 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
224 | C.Lambda (n,s,t) ->
225 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
226 let sourcetype = xxx_type_of_aux' metasenv context s in
227 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
228 (string_of_sort sourcetype) ;
229 if innersort = "Prop" then
231 let father_is_lambda =
235 match Hashtbl.find ids_to_terms father' with
239 if (not father_is_lambda) || expected_available then
240 add_inner_type fresh_id''
243 (fresh_id'',n, aux' context idrefs s,
244 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
246 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
247 if innersort = "Prop" then
248 add_inner_type fresh_id'' ;
250 (fresh_id'', n, aux' context idrefs s,
251 aux' ((Some (n, C.Def(s,None)))::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'' ;
256 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
257 | C.Const (uri,exp_named_subst) ->
258 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
259 if innersort = "Prop" && expected_available then
260 add_inner_type fresh_id'' ;
261 let exp_named_subst' =
263 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
265 C.AConst (fresh_id'', uri, exp_named_subst')
266 | C.MutInd (uri,tyno,exp_named_subst) ->
267 let exp_named_subst' =
269 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
271 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
272 | C.MutConstruct (uri,tyno,consno,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.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
281 | C.MutCase (uri, tyno, outty, term, patterns) ->
282 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
283 if innersort = "Prop" then
284 add_inner_type fresh_id'' ;
285 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
286 aux' context idrefs term, List.map (aux' context idrefs) patterns)
287 | C.Fix (funno, funs) ->
289 List.map (function _ -> gen_id seed) funs in
290 let new_idrefs = List.rev fresh_idrefs @ idrefs in
292 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
294 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
295 if innersort = "Prop" then
296 add_inner_type fresh_id'' ;
297 C.AFix (fresh_id'', funno,
299 (fun id (name, indidx, ty, bo) ->
300 (id, name, indidx, aux' context idrefs ty,
301 aux' (tys@context) new_idrefs bo)
304 | C.CoFix (funno, funs) ->
306 List.map (function _ -> gen_id seed) funs in
307 let new_idrefs = List.rev fresh_idrefs @ idrefs in
309 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
311 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
312 if innersort = "Prop" then
313 add_inner_type fresh_id'' ;
314 C.ACoFix (fresh_id'', funno,
316 (fun id (name, ty, bo) ->
317 (id, name, aux' context idrefs ty,
318 aux' (tys@context) new_idrefs bo)
322 let timea = Sys.time () in
323 let res = aux true None context idrefs t in
324 let timeb = Sys.time () in
326 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
330 let acic_of_cic_context metasenv context idrefs t =
331 let ids_to_terms = Hashtbl.create 503 in
332 let ids_to_father_ids = Hashtbl.create 503 in
333 let ids_to_inner_sorts = Hashtbl.create 503 in
334 let ids_to_inner_types = Hashtbl.create 503 in
336 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
337 ids_to_inner_types metasenv context idrefs t,
338 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
341 let acic_object_of_cic_object obj =
342 let module C = Cic in
343 let module E = Eta_fixing in
344 let ids_to_terms = Hashtbl.create 503 in
345 let ids_to_father_ids = Hashtbl.create 503 in
346 let ids_to_inner_sorts = Hashtbl.create 503 in
347 let ids_to_inner_types = Hashtbl.create 503 in
348 let ids_to_conjectures = Hashtbl.create 11 in
349 let ids_to_hypotheses = Hashtbl.create 127 in
350 let hypotheses_seed = ref 0 in
351 let conjectures_seed = ref 0 in
353 let acic_term_of_cic_term_context' =
354 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
355 ids_to_inner_types in
356 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
359 C.Constant (id,Some bo,ty,params) ->
360 let bo' = E.eta_fix [] bo in
361 let ty' = E.eta_fix [] ty in
362 let abo = acic_term_of_cic_term' bo' (Some ty') in
363 let aty = acic_term_of_cic_term' ty' None in
365 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
366 | C.Constant (id,None,ty,params) ->
367 let ty' = E.eta_fix [] ty in
368 let aty = acic_term_of_cic_term' ty' None in
370 ("mettereaposto",None,id,None,aty,params)
371 | C.Variable (id,bo,ty,params) ->
372 let ty' = E.eta_fix [] ty in
377 let bo' = E.eta_fix [] bo in
378 Some (acic_term_of_cic_term' bo' (Some ty'))
380 let aty = acic_term_of_cic_term' ty' None in
382 ("mettereaposto",id,abo,aty, params)
383 | C.CurrentProof (id,conjectures,bo,ty,params) ->
386 (function (i,canonical_context,term) ->
387 let canonical_context' =
391 | Some (n, C.Decl t)-> Some (n, C.Decl (E.eta_fix conjectures t))
392 | Some (n, C.Def (t,None)) ->
393 Some (n, C.Def ((E.eta_fix conjectures t),None))
394 | Some (_,C.Def (_,Some _)) -> assert false
397 let term' = E.eta_fix conjectures term in
398 (i,canonical_context',term')
403 (function (i,canonical_context,term) as conjecture ->
404 let cid = "c" ^ string_of_int !conjectures_seed in
405 xxx_add ids_to_conjectures cid conjecture ;
406 incr conjectures_seed ;
407 let idrefs',revacanonical_context =
408 let rec aux context idrefs =
412 let hid = "h" ^ string_of_int !hypotheses_seed in
413 let new_idrefs = hid::idrefs in
414 xxx_add ids_to_hypotheses hid hyp ;
415 incr hypotheses_seed ;
417 (Some (n,C.Decl t)) ->
418 let final_idrefs,atl =
419 aux (hyp::context) new_idrefs tl in
421 acic_term_of_cic_term_context'
422 conjectures context idrefs t None
424 final_idrefs,(hid,Some (n,C.ADecl at))::atl
425 | (Some (n,C.Def (t,_))) ->
426 let final_idrefs,atl =
427 aux (hyp::context) new_idrefs tl in
429 acic_term_of_cic_term_context'
430 conjectures context idrefs t None
432 final_idrefs,(hid,Some (n,C.ADef at))::atl
434 let final_idrefs,atl =
435 aux (hyp::context) new_idrefs tl
437 final_idrefs,(hid,None)::atl
439 aux [] [] (List.rev canonical_context)
442 acic_term_of_cic_term_context' conjectures
443 canonical_context idrefs' term None
445 (cid,i,(List.rev revacanonical_context),aterm)
447 let time1 = Sys.time () in
448 let bo' = E.eta_fix conjectures' bo in
449 let ty' = E.eta_fix conjectures' ty in
450 let time2 = Sys.time () in
452 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
453 hashtbl_add_time := 0.0 ;
454 type_of_aux'_add_time := 0.0 ;
456 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
457 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
458 let time3 = Sys.time () in
460 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
462 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
464 ("++++++++++++ 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) ;
466 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
468 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
470 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
471 | C.InductiveDefinition (tys,params,paramsno) ->
474 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
475 let idrefs = List.map (function _ -> gen_id seed) tys in
478 (fun id (name,inductive,ty,cons) ->
481 (function (name,ty) ->
483 acic_term_of_cic_term_context' [] context idrefs ty None)
486 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
487 ) (List.rev idrefs) tys
489 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
491 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
492 ids_to_conjectures,ids_to_hypotheses