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 type sort_kind = [ `Prop | `Set | `Type | `CProp ]
28 let sort_of_string = function
35 let string_of_sort = function
41 let sort_of_sort = function
47 (* let hashtbl_add_time = ref 0.0;; *)
50 (* let t1 = Sys.time () in *)
52 (* let t2 = Sys.time () in
53 hashtbl_add_time := !hashtbl_add_time +. t2 -. t1 *)
56 (* let number_new_type_of_aux' = ref 0;;
57 let type_of_aux'_add_time = ref 0.0;; *)
59 let xxx_type_of_aux' m c t =
60 (* let t1 = Sys.time () in *)
61 let res,_ = CicTypeChecker.type_of_aux' m c t CicUniv.empty_ugraph in
62 (* let t2 = Sys.time () in
63 type_of_aux'_add_time := !type_of_aux'_add_time +. t2 -. t1 ; *)
68 {annsynthesized : Cic.annterm ; annexpected : Cic.annterm option}
72 let res = "i" ^ string_of_int !seed in
77 let fresh_id seed ids_to_terms ids_to_father_ids =
79 let res = gen_id seed in
80 xxx_add ids_to_father_ids res father ;
81 xxx_add ids_to_terms res t ;
85 let source_id_of_id id = "#source#" ^ id;;
87 exception NotEnoughElements;;
89 (*CSC: cut&paste da cicPp.ml *)
90 (* get_nth l n returns the nth element of the list l if it exists or *)
91 (* raises NotEnoughElements if l has less than n elements *)
95 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
96 | (_,_) -> raise NotEnoughElements
99 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
100 ids_to_inner_types metasenv context idrefs t expectedty
102 let module D = DoubleTypeInference in
103 let module C = Cic in
104 let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
105 (* let time1 = Sys.time () in *)
108 let time0 = Sys.time () in
109 let prova = CicTypeChecker.type_of_aux' metasenv context t in
110 let time1 = Sys.time () in
111 prerr_endline ("*** Fine type_inference:" ^ (string_of_float (time1 -. time0)));
112 let res = D.double_type_of metasenv context t expectedty in
113 let time2 = Sys.time () in
114 prerr_endline ("*** Fine double_type_inference:" ^ (string_of_float (time2 -. time1)));
117 D.double_type_of metasenv context t expectedty
120 let time2 = Sys.time () in
122 ("++++++++++++ Tempi della double_type_of: "^ string_of_float (time2 -. time1)) ;
124 let rec aux computeinnertypes father context idrefs tt =
125 let fresh_id'' = fresh_id' father tt in
126 (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
127 let aux' = aux computeinnertypes (Some fresh_id'') in
128 (* First of all we compute the inner type and the inner sort *)
129 (* of the term. They may be useful in what follows. *)
130 (*CSC: This is a very inefficient way of computing inner types *)
131 (*CSC: and inner sorts: very deep terms have their types/sorts *)
132 (*CSC: computed again and again. *)
134 match CicReduction.whd context t with
135 C.Sort C.Prop -> `Prop
136 | C.Sort C.Set -> `Set
139 | C.Sort C.CProp -> `CProp
141 prerr_endline ("Cic2acic.sort_of applied to: " ^ CicPp.ppterm t) ;
144 let ainnertypes,innertype,innersort,expected_available =
145 (*CSC: Here we need the algorithm for Coscoy's double type-inference *)
146 (*CSC: (expected type + inferred type). Just for now we use the usual *)
147 (*CSC: type-inference, but the result is very poor. As a very weak *)
148 (*CSC: patch, I apply whd to the computed type. Full beta *)
149 (*CSC: reduction would be a much better option. *)
150 (*CSC: solo per testare i tempi *)
154 let {D.synthesized = synthesized; D.expected = expected} =
155 if computeinnertypes then
156 D.CicHash.find terms_to_types tt
158 (* We are already in an inner-type and Coscoy's double *)
159 (* type inference algorithm has not been applied. *)
161 (***CSC: patch per provare i tempi
162 CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
163 Cic.Sort (Cic.Type (CicUniv.fresh())); (* TASSI: non dovrebbe fare danni *)
166 (* incr number_new_type_of_aux' ; *)
167 let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
168 let ainnertypes,expected_available =
169 if computeinnertypes then
170 let annexpected,expected_available =
173 | Some expectedty' ->
175 (aux false (Some fresh_id'') context idrefs expectedty'),
180 aux false (Some fresh_id'') context idrefs synthesized ;
181 annexpected = annexpected
182 }, expected_available
186 ainnertypes,synthesized, sort_of innersort, expected_available
189 Not_found -> (* l'inner-type non e' nella tabella ==> sort <> Prop *)
190 (* CSC: Type or Set? I can not tell *)
191 None,Cic.Sort (Cic.Type (CicUniv.fresh())),`Type,false
192 (* TASSI non dovrebbe fare danni *)
195 let add_inner_type id =
196 match ainnertypes with
198 | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
203 match get_nth context n with
204 (Some (C.Name s,_)) -> s
205 | _ -> "__" ^ string_of_int n
207 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
208 if innersort = `Prop && expected_available then
209 add_inner_type fresh_id'' ;
210 C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
211 | C.Var (uri,exp_named_subst) ->
212 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
213 if innersort = `Prop && expected_available then
214 add_inner_type fresh_id'' ;
215 let exp_named_subst' =
217 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
219 C.AVar (fresh_id'', uri,exp_named_subst')
221 let (_,canonical_context,_) = CicUtil.lookup_meta n metasenv in
222 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
223 if innersort = `Prop && expected_available then
224 add_inner_type fresh_id'' ;
225 C.AMeta (fresh_id'', n,
230 | _, Some t -> Some (aux' context idrefs t)
231 | Some _, None -> assert false (* due to typing rules *))
232 canonical_context l))
233 | C.Sort s -> C.ASort (fresh_id'', s)
234 | C.Implicit annotation -> C.AImplicit (fresh_id'', annotation)
236 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
237 if innersort = `Prop then
238 add_inner_type fresh_id'' ;
239 C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
241 xxx_add ids_to_inner_sorts fresh_id''
242 (sort_of innertype) ;
243 let sourcetype = xxx_type_of_aux' metasenv context s in
244 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
245 (sort_of sourcetype) ;
250 if DoubleTypeInference.does_not_occur 1 t then
256 (fresh_id'', n', aux' context idrefs s,
257 aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
258 | C.Lambda (n,s,t) ->
259 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
260 let sourcetype = xxx_type_of_aux' metasenv context s in
261 xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
262 (sort_of sourcetype) ;
263 if innersort = `Prop then
265 let father_is_lambda =
269 match Hashtbl.find ids_to_terms father' with
273 if (not father_is_lambda) || expected_available then
274 add_inner_type fresh_id''
277 (fresh_id'',n, aux' context idrefs s,
278 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
280 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
281 if innersort = `Prop then
282 add_inner_type fresh_id'' ;
284 (fresh_id'', n, aux' context idrefs s,
285 aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
287 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
288 if innersort = `Prop then
289 add_inner_type fresh_id'' ;
290 C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
291 | C.Const (uri,exp_named_subst) ->
292 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
293 if innersort = `Prop && expected_available then
294 add_inner_type fresh_id'' ;
295 let exp_named_subst' =
297 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
299 C.AConst (fresh_id'', uri, exp_named_subst')
300 | C.MutInd (uri,tyno,exp_named_subst) ->
301 let exp_named_subst' =
303 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
305 C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
306 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
307 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
308 if innersort = `Prop && expected_available then
309 add_inner_type fresh_id'' ;
310 let exp_named_subst' =
312 (function i,t -> i, (aux' context idrefs t)) exp_named_subst
314 C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
315 | C.MutCase (uri, tyno, outty, term, patterns) ->
316 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
317 if innersort = `Prop then
318 add_inner_type fresh_id'' ;
319 C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
320 aux' context idrefs term, List.map (aux' context idrefs) patterns)
321 | C.Fix (funno, funs) ->
323 List.map (function _ -> gen_id seed) funs in
324 let new_idrefs = List.rev fresh_idrefs @ idrefs in
326 List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
328 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
329 if innersort = `Prop then
330 add_inner_type fresh_id'' ;
331 C.AFix (fresh_id'', funno,
333 (fun id (name, indidx, ty, bo) ->
334 (id, name, indidx, aux' context idrefs ty,
335 aux' (tys@context) new_idrefs bo)
338 | C.CoFix (funno, funs) ->
340 List.map (function _ -> gen_id seed) funs in
341 let new_idrefs = List.rev fresh_idrefs @ idrefs in
343 List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
345 xxx_add ids_to_inner_sorts fresh_id'' innersort ;
346 if innersort = `Prop then
347 add_inner_type fresh_id'' ;
348 C.ACoFix (fresh_id'', funno,
350 (fun id (name, ty, bo) ->
351 (id, name, aux' context idrefs ty,
352 aux' (tys@context) new_idrefs bo)
357 let timea = Sys.time () in
358 let res = aux true None context idrefs t in
359 let timeb = Sys.time () in
361 ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
364 aux true None context idrefs t
367 let acic_of_cic_context metasenv context idrefs t =
368 let ids_to_terms = Hashtbl.create 503 in
369 let ids_to_father_ids = Hashtbl.create 503 in
370 let ids_to_inner_sorts = Hashtbl.create 503 in
371 let ids_to_inner_types = Hashtbl.create 503 in
373 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
374 ids_to_inner_types metasenv context idrefs t,
375 ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
378 let aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
379 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
380 metasenv (metano,context,goal) =
381 let acic_of_cic_context =
382 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
383 ids_to_inner_types metasenv in
384 let _, acontext,final_idrefs =
386 (fun binding (context, acontext,idrefs) ->
387 let hid = "h" ^ string_of_int !hypotheses_seed in
388 Hashtbl.add ids_to_hypotheses hid binding ;
389 incr hypotheses_seed ;
391 Some (n,Cic.Def (t,None)) ->
392 let acic = acic_of_cic_context context idrefs t None in
394 ((hid,Some (n,Cic.ADef acic))::acontext),(hid::idrefs)
395 | Some (n,Cic.Decl t) ->
396 let acic = acic_of_cic_context context idrefs t None in
398 ((hid,Some (n,Cic.ADecl acic))::acontext),(hid::idrefs)
400 (* Invariant: "" is never looked up *)
401 (None::context),((hid,None)::acontext),""::idrefs
402 | Some (_,Cic.Def (_,Some _)) -> assert false
406 let agoal = acic_of_cic_context context final_idrefs goal None in
407 (metano,acontext,agoal)
410 let asequent_of_sequent (metasenv:Cic.metasenv) (sequent:Cic.conjecture) =
411 let ids_to_terms = Hashtbl.create 503 in
412 let ids_to_father_ids = Hashtbl.create 503 in
413 let ids_to_inner_sorts = Hashtbl.create 503 in
414 let ids_to_inner_types = Hashtbl.create 503 in
415 let ids_to_hypotheses = Hashtbl.create 23 in
416 let hypotheses_seed = ref 0 in
417 let seed = ref 1 in (* 'i0' is used for the whole sequent *)
418 let (metano,acontext,agoal) =
419 aconjecture_of_conjecture seed ids_to_terms ids_to_father_ids
420 ids_to_inner_sorts ids_to_inner_types ids_to_hypotheses hypotheses_seed
422 ("i0",metano,acontext,agoal),
423 ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses
426 let acic_object_of_cic_object ?(eta_fix=true) obj =
427 let module C = Cic in
428 let module E = Eta_fixing in
429 let ids_to_terms = Hashtbl.create 503 in
430 let ids_to_father_ids = Hashtbl.create 503 in
431 let ids_to_inner_sorts = Hashtbl.create 503 in
432 let ids_to_inner_types = Hashtbl.create 503 in
433 let ids_to_conjectures = Hashtbl.create 11 in
434 let ids_to_hypotheses = Hashtbl.create 127 in
435 let hypotheses_seed = ref 0 in
436 let conjectures_seed = ref 0 in
438 let acic_term_of_cic_term_context' =
439 acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
440 ids_to_inner_types in
441 let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
442 let aconjecture_of_conjecture' = aconjecture_of_conjecture seed
443 ids_to_terms ids_to_father_ids ids_to_inner_sorts ids_to_inner_types
444 ids_to_hypotheses hypotheses_seed in
445 let eta_fix metasenv context t =
446 if eta_fix then E.eta_fix metasenv context t else t
450 C.Constant (id,Some bo,ty,params,attrs) ->
451 let bo' = eta_fix [] [] bo in
452 let ty' = eta_fix [] [] ty in
453 let abo = acic_term_of_cic_term' bo' (Some ty') in
454 let aty = acic_term_of_cic_term' ty' None in
456 ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params,attrs)
457 | C.Constant (id,None,ty,params,attrs) ->
458 let ty' = eta_fix [] [] ty in
459 let aty = acic_term_of_cic_term' ty' None in
461 ("mettereaposto",None,id,None,aty,params,attrs)
462 | C.Variable (id,bo,ty,params,attrs) ->
463 let ty' = eta_fix [] [] ty in
468 let bo' = eta_fix [] [] bo in
469 Some (acic_term_of_cic_term' bo' (Some ty'))
471 let aty = acic_term_of_cic_term' ty' None in
473 ("mettereaposto",id,abo,aty,params,attrs)
474 | C.CurrentProof (id,conjectures,bo,ty,params,attrs) ->
477 (function (i,canonical_context,term) ->
478 let canonical_context' =
480 (fun d canonical_context' ->
484 | Some (n, C.Decl t)->
485 Some (n, C.Decl (eta_fix conjectures canonical_context' t))
486 | Some (n, C.Def (t,None)) ->
488 C.Def ((eta_fix conjectures canonical_context' t),None))
489 | Some (_,C.Def (_,Some _)) -> assert false
491 d::canonical_context'
492 ) [] canonical_context
494 let term' = eta_fix conjectures canonical_context' term in
495 (i,canonical_context',term')
500 (function (i,canonical_context,term) as conjecture ->
501 let cid = "c" ^ string_of_int !conjectures_seed in
502 xxx_add ids_to_conjectures cid conjecture ;
503 incr conjectures_seed ;
504 let (i,acanonical_context,aterm)
505 = aconjecture_of_conjecture' conjectures conjecture in
506 (cid,i,acanonical_context,aterm))
508 (* let idrefs',revacanonical_context =
509 let rec aux context idrefs =
513 let hid = "h" ^ string_of_int !hypotheses_seed in
514 let new_idrefs = hid::idrefs in
515 xxx_add ids_to_hypotheses hid hyp ;
516 incr hypotheses_seed ;
518 (Some (n,C.Decl t)) ->
519 let final_idrefs,atl =
520 aux (hyp::context) new_idrefs tl in
522 acic_term_of_cic_term_context'
523 conjectures context idrefs t None
525 final_idrefs,(hid,Some (n,C.ADecl at))::atl
526 | (Some (n,C.Def (t,_))) ->
527 let final_idrefs,atl =
528 aux (hyp::context) new_idrefs tl in
530 acic_term_of_cic_term_context'
531 conjectures context idrefs t None
533 final_idrefs,(hid,Some (n,C.ADef at))::atl
535 let final_idrefs,atl =
536 aux (hyp::context) new_idrefs tl
538 final_idrefs,(hid,None)::atl
540 aux [] [] (List.rev canonical_context)
543 acic_term_of_cic_term_context' conjectures
544 canonical_context idrefs' term None
546 (cid,i,(List.rev revacanonical_context),aterm)
548 (* let time1 = Sys.time () in *)
549 let bo' = eta_fix conjectures' [] bo in
550 let ty' = eta_fix conjectures' [] ty in
552 let time2 = Sys.time () in
554 ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
555 hashtbl_add_time := 0.0 ;
556 type_of_aux'_add_time := 0.0 ;
557 DoubleTypeInference.syntactic_equality_add_time := 0.0 ;
560 acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
561 let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
563 let time3 = Sys.time () in
565 ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
567 ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
569 ("++++++++++++ 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) ;
571 ("++++++++++++ Tempi della syntactic_equality_add_time: " ^ string_of_float !DoubleTypeInference.syntactic_equality_add_time) ;
573 ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
575 ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
578 ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params,attrs)
579 | C.InductiveDefinition (tys,params,paramsno,attrs) ->
582 (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
583 let idrefs = List.map (function _ -> gen_id seed) tys in
586 (fun id (name,inductive,ty,cons) ->
589 (function (name,ty) ->
591 acic_term_of_cic_term_context' [] context idrefs ty None)
594 (id,name,inductive,acic_term_of_cic_term' ty None,acons)
595 ) (List.rev idrefs) tys
597 C.AInductiveDefinition ("mettereaposto",atys,params,paramsno,attrs)
599 aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
600 ids_to_conjectures,ids_to_hypotheses