]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_omdoc/cic2acic.ml
Defs in context may now have an optional type (when unknown).
[helm.git] / helm / ocaml / cic_omdoc / cic2acic.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 let hashtbl_add_time = ref 0.0;;
27
28 let xxx_add h k v =
29  let t1 = Sys.time () in
30   Hashtbl.add h k v ;
31   let t2 = Sys.time () in
32    hashtbl_add_time := !hashtbl_add_time +. t2 -. t1
33 ;;
34
35 let number_new_type_of_aux' = ref 0;;
36 let type_of_aux'_add_time = ref 0.0;;
37
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 ;
43  res
44 ;;
45
46 type anntypes =
47  {annsynthesized : Cic.annterm ; annexpected : Cic.annterm option}
48 ;;
49
50 let gen_id seed =
51  let res = "i" ^ string_of_int !seed in
52   incr seed ;
53   res
54 ;;
55
56 let fresh_id seed ids_to_terms ids_to_father_ids =
57  fun father t ->
58   let res = gen_id seed in
59    xxx_add ids_to_father_ids res father ;
60    xxx_add ids_to_terms res t ;
61    res
62 ;;
63
64 let source_id_of_id id = "#source#" ^ id;;
65
66 exception NotEnoughElements;;
67 exception NameExpected;;
68
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             *)
72 let rec get_nth l n =
73  match (n,l) with
74     (1, he::_) -> he
75   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
76   | (_,_) -> raise NotEnoughElements
77 ;;
78
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
81 =
82  let module D = DoubleTypeInference in
83  let module C = Cic in
84   let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
85    let time1 = Sys.time () in
86    let terms_to_types =
87     D.double_type_of metasenv context t expectedty
88    in
89    let time2 = Sys.time () in
90    prerr_endline
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         | _ -> assert false
107       in
108        let ainnertypes,innertype,innersort,expected_available =
109 (*CSC: Here we need the algorithm for Coscoy's double type-inference  *)
110 (*CSC: (expected type + inferred type). Just for now we use the usual *)
111 (*CSC: type-inference, but the result is very poor. As a very weak    *)
112 (*CSC: patch, I apply whd to the computed type. Full beta             *)
113 (*CSC: reduction would be a much better option.                       *)
114 (*CSC: solo per testare i tempi *)
115 (*XXXXXXX *)
116         try
117 (* *)
118         let {D.synthesized = synthesized; D.expected = expected} =
119          if computeinnertypes then
120           D.CicHash.find terms_to_types tt
121          else
122           (* We are already in an inner-type and Coscoy's double *)
123           (* type inference algorithm has not been applied.      *)
124           {D.synthesized =
125 (***CSC: patch per provare i tempi
126             CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
127 Cic.Sort Cic.Type ;
128            D.expected = None}
129         in
130          incr number_new_type_of_aux' ;
131          let innersort = (*XXXXX *) xxx_type_of_aux' metasenv context synthesized (* Cic.Sort Cic.Prop *) in
132           let ainnertypes,expected_available =
133            if computeinnertypes then
134             let annexpected,expected_available =
135                match expected with
136                   None -> None,false
137                 | Some expectedty' ->
138                    Some
139                     (aux false (Some fresh_id'') context idrefs expectedty'),
140                     true
141             in
142              Some
143               {annsynthesized =
144                 aux false (Some fresh_id'') context idrefs synthesized ;
145                annexpected = annexpected
146               }, expected_available
147            else
148             None,false
149           in
150            ainnertypes,synthesized, string_of_sort innersort, expected_available
151 (*XXXXXXXX *)
152         with
153          Not_found ->  (* l'inner-type non e' nella tabella ==> sort <> Prop *)
154           (* CSC: Type or Set? I can not tell *)
155           None,Cic.Sort Cic.Type,"Type",false
156 (* *)
157        in
158         let add_inner_type id =
159          match ainnertypes with
160             None -> ()
161           | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
162         in
163          match tt with
164             C.Rel n ->
165              let id =
166               match get_nth context n with
167                  (Some (C.Name s,_)) -> s
168                | _ -> raise NameExpected
169              in
170               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
171               if innersort = "Prop"  && expected_available then
172                add_inner_type fresh_id'' ;
173               C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
174           | C.Var (uri,exp_named_subst) ->
175              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
176              if innersort = "Prop"  && expected_available then
177               add_inner_type fresh_id'' ;
178              let exp_named_subst' =
179               List.map
180                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
181              in
182               C.AVar (fresh_id'', uri,exp_named_subst')
183           | C.Meta (n,l) ->
184              let (_,canonical_context,_) =
185               List.find (function (m,_,_) -> n = m) metasenv
186              in
187              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
188              if innersort = "Prop"  && expected_available then
189               add_inner_type fresh_id'' ;
190              C.AMeta (fresh_id'', n,
191               (List.map2
192                 (fun ct t ->
193                   match (ct, t) with
194                   | None, _ -> None
195                   | _, Some t -> Some (aux' context idrefs t)
196                   | Some _, None -> assert false (* due to typing rules *))
197                 canonical_context l))
198           | C.Sort s -> C.ASort (fresh_id'', s)
199           | C.Implicit -> C.AImplicit (fresh_id'')
200           | C.Cast (v,t) ->
201              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
202              if innersort = "Prop" then
203               add_inner_type fresh_id'' ;
204              C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
205           | C.Prod (n,s,t) ->
206               xxx_add ids_to_inner_sorts fresh_id''
207                (string_of_sort innertype) ;
208                     let sourcetype = xxx_type_of_aux' metasenv context s in
209                      xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
210                       (string_of_sort sourcetype) ;
211               let n' =
212                match n with
213                   C.Anonymous -> n
214                 | C.Name n' ->
215                    if DoubleTypeInference.does_not_occur 1 t then
216                     C.Anonymous
217                    else
218                     C.Name n'
219               in
220                C.AProd
221                 (fresh_id'', n', aux' context idrefs s,
222                  aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
223           | C.Lambda (n,s,t) ->
224              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
225                    let sourcetype = xxx_type_of_aux' metasenv context s in
226                     xxx_add ids_to_inner_sorts (source_id_of_id fresh_id'')
227                      (string_of_sort sourcetype) ;
228               if innersort = "Prop" then
229                begin
230                 let father_is_lambda =
231                  match father with
232                     None -> false
233                   | Some father' ->
234                      match Hashtbl.find ids_to_terms father' with
235                         C.Lambda _ -> true
236                       | _ -> false
237                 in
238                  if (not father_is_lambda) || expected_available then
239                   add_inner_type fresh_id''
240                end ;
241               C.ALambda
242                (fresh_id'',n, aux' context idrefs s,
243                 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
244           | C.LetIn (n,s,t) ->
245              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
246              if innersort = "Prop" then
247               add_inner_type fresh_id'' ;
248              C.ALetIn
249               (fresh_id'', n, aux' context idrefs s,
250                aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
251           | C.Appl l ->
252              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
253              if innersort = "Prop" then
254               add_inner_type fresh_id'' ;
255              C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
256           | C.Const (uri,exp_named_subst) ->
257              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
258              if innersort = "Prop"  && expected_available then
259               add_inner_type fresh_id'' ;
260              let exp_named_subst' =
261               List.map
262                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
263              in
264               C.AConst (fresh_id'', uri, exp_named_subst')
265           | C.MutInd (uri,tyno,exp_named_subst) ->
266              let exp_named_subst' =
267               List.map
268                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
269              in
270               C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
271           | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
272              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
273              if innersort = "Prop"  && expected_available then
274               add_inner_type fresh_id'' ;
275              let exp_named_subst' =
276               List.map
277                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
278              in
279               C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
280           | C.MutCase (uri, tyno, outty, term, patterns) ->
281              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
282              if innersort = "Prop" then
283               add_inner_type fresh_id'' ;
284              C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
285               aux' context idrefs term, List.map (aux' context idrefs) patterns)
286           | C.Fix (funno, funs) ->
287              let fresh_idrefs =
288               List.map (function _ -> gen_id seed) funs in
289              let new_idrefs = List.rev fresh_idrefs @ idrefs in
290              let tys =
291               List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
292              in
293               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
294               if innersort = "Prop" then
295                add_inner_type fresh_id'' ;
296               C.AFix (fresh_id'', funno,
297                List.map2
298                 (fun id (name, indidx, ty, bo) ->
299                   (id, name, indidx, aux' context idrefs ty,
300                     aux' (tys@context) new_idrefs bo)
301                 ) fresh_idrefs funs
302              )
303           | C.CoFix (funno, funs) ->
304              let fresh_idrefs =
305               List.map (function _ -> gen_id seed) funs in
306              let new_idrefs = List.rev fresh_idrefs @ idrefs in
307              let tys =
308               List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
309              in
310               xxx_add ids_to_inner_sorts fresh_id'' innersort ;
311               if innersort = "Prop" then
312                add_inner_type fresh_id'' ;
313               C.ACoFix (fresh_id'', funno,
314                List.map2
315                 (fun id (name, ty, bo) ->
316                   (id, name, aux' context idrefs ty,
317                     aux' (tys@context) new_idrefs bo)
318                 ) fresh_idrefs funs
319               )
320         in
321          let timea = Sys.time () in
322          let res = aux true None context idrefs t in
323          let timeb = Sys.time () in
324           prerr_endline
325            ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
326           res
327 ;;
328
329 let acic_of_cic_context metasenv context idrefs t =
330  let ids_to_terms = Hashtbl.create 503 in
331  let ids_to_father_ids = Hashtbl.create 503 in
332  let ids_to_inner_sorts = Hashtbl.create 503 in
333  let ids_to_inner_types = Hashtbl.create 503 in
334  let seed = ref 0 in
335    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
336     ids_to_inner_types metasenv context idrefs t,
337    ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
338 ;;
339
340 let acic_object_of_cic_object obj =
341  let module C = Cic in
342  let module E = Eta_fixing in
343   let ids_to_terms = Hashtbl.create 503 in
344   let ids_to_father_ids = Hashtbl.create 503 in
345   let ids_to_inner_sorts = Hashtbl.create 503 in
346   let ids_to_inner_types = Hashtbl.create 503 in
347   let ids_to_conjectures = Hashtbl.create 11 in
348   let ids_to_hypotheses = Hashtbl.create 127 in
349   let hypotheses_seed = ref 0 in
350   let conjectures_seed = ref 0 in
351   let seed = ref 0 in
352   let acic_term_of_cic_term_context' =
353    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
354     ids_to_inner_types in
355   let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
356    let aobj =
357     match obj with
358       C.Constant (id,Some bo,ty,params) ->
359        let bo' = E.eta_fix [] bo in
360        let ty' = E.eta_fix [] ty in
361        let abo = acic_term_of_cic_term' bo' (Some ty') in
362        let aty = acic_term_of_cic_term' ty' None in
363         C.AConstant
364          ("mettereaposto",Some "mettereaposto2",id,Some abo,aty,params)
365     | C.Constant (id,None,ty,params) ->
366        let ty' = E.eta_fix [] ty in
367        let aty = acic_term_of_cic_term' ty' None in
368         C.AConstant
369          ("mettereaposto",None,id,None,aty,params)
370     | C.Variable (id,bo,ty,params) ->
371        let ty' = E.eta_fix [] ty in
372        let abo =
373         match bo with
374            None -> None
375          | Some bo ->
376             let bo' = E.eta_fix [] bo in
377              Some (acic_term_of_cic_term' bo' (Some ty'))
378        in
379        let aty = acic_term_of_cic_term' ty' None in
380         C.AVariable
381          ("mettereaposto",id,abo,aty, params)
382     | C.CurrentProof (id,conjectures,bo,ty,params) ->
383        let conjectures' =
384         List.map
385          (function (i,canonical_context,term) ->
386            let canonical_context' =
387             List.map
388              (function
389                  None -> None
390                | Some (n, C.Decl t)-> Some (n, C.Decl (E.eta_fix conjectures t))
391                | Some (n, C.Def (t,None)) ->
392                   Some (n, C.Def ((E.eta_fix conjectures t),None))
393                | Some (_,C.Def (_,Some _)) -> assert false
394              ) canonical_context
395            in
396            let term' = E.eta_fix conjectures term in
397             (i,canonical_context',term')
398          ) conjectures
399        in
400        let aconjectures =
401         List.map
402          (function (i,canonical_context,term) as conjecture ->
403            let cid = "c" ^ string_of_int !conjectures_seed in
404             xxx_add ids_to_conjectures cid conjecture ;
405             incr conjectures_seed ;
406             let idrefs',revacanonical_context =
407              let rec aux context idrefs =
408               function
409                  [] -> idrefs,[]
410                | hyp::tl ->
411                   let hid = "h" ^ string_of_int !hypotheses_seed in
412                   let new_idrefs = hid::idrefs in
413                    xxx_add ids_to_hypotheses hid hyp ;
414                    incr hypotheses_seed ;
415                    match hyp with
416                       (Some (n,C.Decl t)) ->
417                         let final_idrefs,atl =
418                          aux (hyp::context) new_idrefs tl in
419                         let at =
420                          acic_term_of_cic_term_context'
421                           conjectures context idrefs t None
422                         in
423                          final_idrefs,(hid,Some (n,C.ADecl at))::atl
424                     | (Some (n,C.Def (t,_))) ->
425                         let final_idrefs,atl =
426                          aux (hyp::context) new_idrefs tl in
427                         let at =
428                          acic_term_of_cic_term_context'
429                           conjectures context idrefs t None
430                         in
431                          final_idrefs,(hid,Some (n,C.ADef at))::atl
432                     | None ->
433                        let final_idrefs,atl =
434                         aux (hyp::context) new_idrefs tl
435                        in
436                         final_idrefs,(hid,None)::atl
437              in
438               aux [] [] (List.rev canonical_context)
439             in
440              let aterm =
441               acic_term_of_cic_term_context' conjectures
442                canonical_context idrefs' term None
443              in
444               (cid,i,(List.rev revacanonical_context),aterm)
445          ) conjectures' in
446        let time1 = Sys.time () in
447        let bo' = E.eta_fix conjectures' bo in
448        let ty' = E.eta_fix conjectures' ty in
449        let time2 = Sys.time () in
450        prerr_endline
451         ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
452        hashtbl_add_time := 0.0 ;
453        type_of_aux'_add_time := 0.0 ;
454        let abo =
455         acic_term_of_cic_term_context' conjectures' [] [] bo' (Some ty') in
456        let aty = acic_term_of_cic_term_context' conjectures' [] [] ty' None in
457        let time3 = Sys.time () in
458        prerr_endline
459         ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
460        prerr_endline
461         ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
462        prerr_endline
463         ("++++++++++++ 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) ;
464        prerr_endline
465         ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
466        prerr_endline
467         ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
468         C.ACurrentProof
469          ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
470     | C.InductiveDefinition (tys,params,paramsno) ->
471        let context =
472         List.map
473          (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
474        let idrefs = List.map (function _ -> gen_id seed) tys in
475        let atys =
476         List.map2
477          (fun id (name,inductive,ty,cons) ->
478            let acons =
479             List.map
480              (function (name,ty) ->
481                (name,
482                  acic_term_of_cic_term_context' [] context idrefs ty None)
483              ) cons
484            in
485             (id,name,inductive,acic_term_of_cic_term' ty None,acons)
486          ) (List.rev idrefs) tys
487        in
488         C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
489    in
490     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
491      ids_to_conjectures,ids_to_hypotheses
492 ;;