]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_omdoc/cic2acic.ml
sort CProp added
[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         | C.Sort C.CProp -> "CProp"
107         | _              -> assert false
108       in
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 *)
116 (*XXXXXXX *)
117         try
118 (* *)
119         let {D.synthesized = synthesized; D.expected = expected} =
120          if computeinnertypes then
121           D.CicHash.find terms_to_types tt
122          else
123           (* We are already in an inner-type and Coscoy's double *)
124           (* type inference algorithm has not been applied.      *)
125           {D.synthesized =
126 (***CSC: patch per provare i tempi
127             CicReduction.whd context (xxx_type_of_aux' metasenv context tt) ; *)
128 Cic.Sort Cic.Type ;
129            D.expected = None}
130         in
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 =
136                match expected with
137                   None -> None,false
138                 | Some expectedty' ->
139                    Some
140                     (aux false (Some fresh_id'') context idrefs expectedty'),
141                     true
142             in
143              Some
144               {annsynthesized =
145                 aux false (Some fresh_id'') context idrefs synthesized ;
146                annexpected = annexpected
147               }, expected_available
148            else
149             None,false
150           in
151            ainnertypes,synthesized, string_of_sort innersort, expected_available
152 (*XXXXXXXX *)
153         with
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
157 (* *)
158        in
159         let add_inner_type id =
160          match ainnertypes with
161             None -> ()
162           | Some ainnertypes -> xxx_add ids_to_inner_types id ainnertypes
163         in
164          match tt with
165             C.Rel n ->
166              let id =
167               match get_nth context n with
168                  (Some (C.Name s,_)) -> s
169                | _ -> raise NameExpected
170              in
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' =
180               List.map
181                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
182              in
183               C.AVar (fresh_id'', uri,exp_named_subst')
184           | C.Meta (n,l) ->
185              let (_,canonical_context,_) =
186               List.find (function (m,_,_) -> n = m) metasenv
187              in
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,
192               (List.map2
193                 (fun ct t ->
194                   match (ct, t) with
195                   | None, _ -> None
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'')
201           | C.Cast (v,t) ->
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)
206           | C.Prod (n,s,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) ;
212               let n' =
213                match n with
214                   C.Anonymous -> n
215                 | C.Name n' ->
216                    if DoubleTypeInference.does_not_occur 1 t then
217                     C.Anonymous
218                    else
219                     C.Name n'
220               in
221                C.AProd
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
230                begin
231                 let father_is_lambda =
232                  match father with
233                     None -> false
234                   | Some father' ->
235                      match Hashtbl.find ids_to_terms father' with
236                         C.Lambda _ -> true
237                       | _ -> false
238                 in
239                  if (not father_is_lambda) || expected_available then
240                   add_inner_type fresh_id''
241                end ;
242               C.ALambda
243                (fresh_id'',n, aux' context idrefs s,
244                 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
245           | C.LetIn (n,s,t) ->
246              xxx_add ids_to_inner_sorts fresh_id'' innersort ;
247              if innersort = "Prop" then
248               add_inner_type fresh_id'' ;
249              C.ALetIn
250               (fresh_id'', n, aux' context idrefs s,
251                aux' ((Some (n, C.Def(s,None)))::context) (fresh_id''::idrefs) t)
252           | C.Appl l ->
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' =
262               List.map
263                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
264              in
265               C.AConst (fresh_id'', uri, exp_named_subst')
266           | C.MutInd (uri,tyno,exp_named_subst) ->
267              let exp_named_subst' =
268               List.map
269                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
270              in
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' =
277               List.map
278                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
279              in
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) ->
288              let fresh_idrefs =
289               List.map (function _ -> gen_id seed) funs in
290              let new_idrefs = List.rev fresh_idrefs @ idrefs in
291              let tys =
292               List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
293              in
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,
298                List.map2
299                 (fun id (name, indidx, ty, bo) ->
300                   (id, name, indidx, aux' context idrefs ty,
301                     aux' (tys@context) new_idrefs bo)
302                 ) fresh_idrefs funs
303              )
304           | C.CoFix (funno, funs) ->
305              let fresh_idrefs =
306               List.map (function _ -> gen_id seed) funs in
307              let new_idrefs = List.rev fresh_idrefs @ idrefs in
308              let tys =
309               List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
310              in
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,
315                List.map2
316                 (fun id (name, ty, bo) ->
317                   (id, name, aux' context idrefs ty,
318                     aux' (tys@context) new_idrefs bo)
319                 ) fresh_idrefs funs
320               )
321         in
322          let timea = Sys.time () in
323          let res = aux true None context idrefs t in
324          let timeb = Sys.time () in
325           prerr_endline
326            ("+++++++++++++ Tempi della aux dentro alla acic_of_cic: "^ string_of_float (timeb -. timea)) ;
327           res
328 ;;
329
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
335  let seed = ref 0 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
339 ;;
340
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
352   let 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
357    let aobj =
358     match obj with
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
364         C.AConstant
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
369         C.AConstant
370          ("mettereaposto",None,id,None,aty,params)
371     | C.Variable (id,bo,ty,params) ->
372        let ty' = E.eta_fix [] ty in
373        let abo =
374         match bo with
375            None -> None
376          | Some bo ->
377             let bo' = E.eta_fix [] bo in
378              Some (acic_term_of_cic_term' bo' (Some ty'))
379        in
380        let aty = acic_term_of_cic_term' ty' None in
381         C.AVariable
382          ("mettereaposto",id,abo,aty, params)
383     | C.CurrentProof (id,conjectures,bo,ty,params) ->
384        let conjectures' =
385         List.map
386          (function (i,canonical_context,term) ->
387            let canonical_context' =
388             List.map
389              (function
390                  None -> None
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
395              ) canonical_context
396            in
397            let term' = E.eta_fix conjectures term in
398             (i,canonical_context',term')
399          ) conjectures
400        in
401        let aconjectures =
402         List.map
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 =
409               function
410                  [] -> idrefs,[]
411                | hyp::tl ->
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 ;
416                    match hyp with
417                       (Some (n,C.Decl t)) ->
418                         let final_idrefs,atl =
419                          aux (hyp::context) new_idrefs tl in
420                         let at =
421                          acic_term_of_cic_term_context'
422                           conjectures context idrefs t None
423                         in
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
428                         let at =
429                          acic_term_of_cic_term_context'
430                           conjectures context idrefs t None
431                         in
432                          final_idrefs,(hid,Some (n,C.ADef at))::atl
433                     | None ->
434                        let final_idrefs,atl =
435                         aux (hyp::context) new_idrefs tl
436                        in
437                         final_idrefs,(hid,None)::atl
438              in
439               aux [] [] (List.rev canonical_context)
440             in
441              let aterm =
442               acic_term_of_cic_term_context' conjectures
443                canonical_context idrefs' term None
444              in
445               (cid,i,(List.rev revacanonical_context),aterm)
446          ) conjectures' in
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
451        prerr_endline
452         ("++++++++++ Tempi della eta_fix: "^ string_of_float (time2 -. time1)) ;
453        hashtbl_add_time := 0.0 ;
454        type_of_aux'_add_time := 0.0 ;
455        let abo =
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
459        prerr_endline
460         ("++++++++++++ Tempi della hashtbl_add_time: " ^ string_of_float !hashtbl_add_time) ;
461        prerr_endline
462         ("++++++++++++ Tempi della type_of_aux'_add_time(" ^ string_of_int !number_new_type_of_aux' ^ "): " ^ string_of_float !type_of_aux'_add_time) ;
463        prerr_endline
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) ;
465        prerr_endline
466         ("++++++++++ Tempi della acic_of_cic: " ^ string_of_float (time3 -. time2)) ;
467        prerr_endline
468         ("++++++++++ Numero di iterazioni della acic_of_cic: " ^ string_of_int !seed) ;
469         C.ACurrentProof
470          ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
471     | C.InductiveDefinition (tys,params,paramsno) ->
472        let context =
473         List.map
474          (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
475        let idrefs = List.map (function _ -> gen_id seed) tys in
476        let atys =
477         List.map2
478          (fun id (name,inductive,ty,cons) ->
479            let acons =
480             List.map
481              (function (name,ty) ->
482                (name,
483                  acic_term_of_cic_term_context' [] context idrefs ty None)
484              ) cons
485            in
486             (id,name,inductive,acic_term_of_cic_term' ty None,acons)
487          ) (List.rev idrefs) tys
488        in
489         C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
490    in
491     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
492      ids_to_conjectures,ids_to_hypotheses
493 ;;