]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/cic2acic.ml
c18e7d6a684f7ba143a08cbebe12cd67fc59edbb
[helm.git] / helm / gTopLevel / 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 type anntypes =
27  {annsynthesized : Cic.annterm ; annexpected : Cic.annterm option}
28 ;;
29
30 let gen_id seed =
31  let res = "i" ^ string_of_int !seed in
32   incr seed ;
33   res
34 ;;
35
36 let fresh_id seed ids_to_terms ids_to_father_ids =
37  fun father t ->
38   let res = gen_id seed in
39    Hashtbl.add ids_to_father_ids res father ;
40    Hashtbl.add ids_to_terms res t ;
41    res
42 ;;
43
44 let source_id_of_id id = "#source#" ^ id;;
45
46 exception NotEnoughElements;;
47 exception NameExpected;;
48
49 (*CSC: cut&paste da cicPp.ml *)
50 (* get_nth l n   returns the nth element of the list l if it exists or *)
51 (* raises NotEnoughElements if l has less than n elements             *)
52 let rec get_nth l n =
53  match (n,l) with
54     (1, he::_) -> he
55   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
56   | (_,_) -> raise NotEnoughElements
57 ;;
58
59 let acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
60      ids_to_inner_types metasenv context idrefs t expectedty
61 =
62  let module D = DoubleTypeInference in
63  let module T = CicTypeChecker in
64  let module C = Cic in
65   let fresh_id' = fresh_id seed ids_to_terms ids_to_father_ids in
66    let terms_to_types =
67     D.double_type_of metasenv context t expectedty
68    in
69     let rec aux computeinnertypes father context idrefs tt =
70      let fresh_id'' = fresh_id' father tt in
71      (*CSC: computeinnertypes era true, il che e' proprio sbagliato, no? *)
72      let aux' = aux computeinnertypes (Some fresh_id'') in
73       (* First of all we compute the inner type and the inner sort *)
74       (* of the term. They may be useful in what follows.          *)
75       (*CSC: This is a very inefficient way of computing inner types *)
76       (*CSC: and inner sorts: very deep terms have their types/sorts *)
77       (*CSC: computed again and again.                               *)
78       let string_of_sort t =
79        match CicReduction.whd context t with 
80           C.Sort C.Prop -> "Prop"
81         | C.Sort C.Set  -> "Set"
82         | C.Sort C.Type -> "Type"
83         | _ -> assert false
84       in
85        let ainnertypes,innertype,innersort,expected_available =
86 (*CSC: Here we need the algorithm for Coscoy's double type-inference  *)
87 (*CSC: (expected type + inferred type). Just for now we use the usual *)
88 (*CSC: type-inference, but the result is very poor. As a very weak    *)
89 (*CSC: patch, I apply whd to the computed type. Full beta             *)
90 (*CSC: reduction would be a much better option.                       *)
91         let {D.synthesized = synthesized; D.expected = expected} =
92          if computeinnertypes then
93           D.CicHash.find terms_to_types tt
94          else
95           (* We are already in an inner-type and Coscoy's double *)
96           (* type inference algorithm has not been applied.      *)
97           {D.synthesized =
98             CicReduction.whd context (T.type_of_aux' metasenv context tt) ;
99            D.expected = None}
100         in
101          let innersort = T.type_of_aux' metasenv context synthesized in
102           let ainnertypes,expected_available =
103            if computeinnertypes then
104             let annexpected,expected_available =
105                match expected with
106                   None -> None,false
107                 | Some expectedty' ->
108                    Some
109                     (aux false (Some fresh_id'') context idrefs expectedty'),
110                     true
111             in
112              Some
113               {annsynthesized =
114                 aux false (Some fresh_id'') context idrefs synthesized ;
115                annexpected = annexpected
116               }, expected_available
117            else
118             None,false
119           in
120            ainnertypes,synthesized, string_of_sort innersort, expected_available
121        in
122         let add_inner_type id =
123          match ainnertypes with
124             None -> ()
125           | Some ainnertypes -> Hashtbl.add ids_to_inner_types id ainnertypes
126         in
127          match tt with
128             C.Rel n ->
129              let id =
130               match get_nth context n with
131                  (Some (C.Name s,_)) -> s
132                | _ -> raise NameExpected
133              in
134               Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
135               if innersort = "Prop"  && expected_available then
136                add_inner_type fresh_id'' ;
137               C.ARel (fresh_id'', List.nth idrefs (n-1), n, id)
138           | C.Var (uri,exp_named_subst) ->
139              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
140              if innersort = "Prop"  && expected_available then
141               add_inner_type fresh_id'' ;
142              let exp_named_subst' =
143               List.map
144                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
145              in
146               C.AVar (fresh_id'', uri,exp_named_subst')
147           | C.Meta (n,l) ->
148              let (_,canonical_context,_) =
149               List.find (function (m,_,_) -> n = m) metasenv
150              in
151              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
152              if innersort = "Prop"  && expected_available then
153               add_inner_type fresh_id'' ;
154              C.AMeta (fresh_id'', n,
155               (List.map2
156                 (fun ct t ->
157                   match (ct, t) with
158                   | None, _ -> None
159                   | _, Some t -> Some (aux' context idrefs t)
160                   | Some _, None -> assert false (* due to typing rules *))
161                 canonical_context l))
162           | C.Sort s -> C.ASort (fresh_id'', s)
163           | C.Implicit -> C.AImplicit (fresh_id'')
164           | C.Cast (v,t) ->
165              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
166              if innersort = "Prop" then
167               add_inner_type fresh_id'' ;
168              C.ACast (fresh_id'', aux' context idrefs v, aux' context idrefs t)
169           | C.Prod (n,s,t) ->
170               Hashtbl.add ids_to_inner_sorts fresh_id''
171                (string_of_sort innertype) ;
172                     let sourcetype = T.type_of_aux' metasenv context s in
173                      Hashtbl.add ids_to_inner_sorts (source_id_of_id fresh_id'')
174                       (string_of_sort sourcetype) ;
175                C.AProd
176                 (fresh_id'', n, aux' context idrefs s,
177                  aux' ((Some (n, C.Decl s))::context) (fresh_id''::idrefs) t)
178           | C.Lambda (n,s,t) ->
179              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
180                    let sourcetype = T.type_of_aux' metasenv context s in
181                     Hashtbl.add ids_to_inner_sorts (source_id_of_id fresh_id'')
182                      (string_of_sort sourcetype) ;
183               if innersort = "Prop" then
184                begin
185                 let father_is_lambda =
186                  match father with
187                     None -> false
188                   | Some father' ->
189                      match Hashtbl.find ids_to_terms father' with
190                         C.Lambda _ -> true
191                       | _ -> false
192                 in
193                  if (not father_is_lambda) || expected_available then
194                   add_inner_type fresh_id''
195                end ;
196               C.ALambda
197                (fresh_id'',n, aux' context idrefs s,
198                 aux' ((Some (n, C.Decl s)::context)) (fresh_id''::idrefs) t)
199           | C.LetIn (n,s,t) ->
200              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
201              if innersort = "Prop" then
202               add_inner_type fresh_id'' ;
203              C.ALetIn
204               (fresh_id'', n, aux' context idrefs s,
205                aux' ((Some (n, C.Def s))::context) (fresh_id''::idrefs) t)
206           | C.Appl l ->
207              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
208              if innersort = "Prop" then
209               add_inner_type fresh_id'' ;
210              C.AAppl (fresh_id'', List.map (aux' context idrefs) l)
211           | C.Const (uri,exp_named_subst) ->
212              Hashtbl.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' =
216               List.map
217                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
218              in
219               C.AConst (fresh_id'', uri, exp_named_subst')
220           | C.MutInd (uri,tyno,exp_named_subst) ->
221              let exp_named_subst' =
222               List.map
223                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
224              in
225               C.AMutInd (fresh_id'', uri, tyno, exp_named_subst')
226           | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
227              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
228              if innersort = "Prop"  && expected_available then
229               add_inner_type fresh_id'' ;
230              let exp_named_subst' =
231               List.map
232                (function i,t -> i, (aux' context idrefs t)) exp_named_subst
233              in
234               C.AMutConstruct (fresh_id'', uri, tyno, consno, exp_named_subst')
235           | C.MutCase (uri, tyno, outty, term, patterns) ->
236              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
237              if innersort = "Prop" then
238               add_inner_type fresh_id'' ;
239              C.AMutCase (fresh_id'', uri, tyno, aux' context idrefs outty,
240               aux' context idrefs term, List.map (aux' context idrefs) patterns)
241           | C.Fix (funno, funs) ->
242              let fresh_idrefs =
243               List.map (function _ -> gen_id seed) funs in
244              let new_idrefs = List.rev fresh_idrefs @ idrefs in
245              let tys =
246               List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
247              in
248               Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
249               if innersort = "Prop" then
250                add_inner_type fresh_id'' ;
251               C.AFix (fresh_id'', funno,
252                List.map2
253                 (fun id (name, indidx, ty, bo) ->
254                   (id, name, indidx, aux' context idrefs ty,
255                     aux' (tys@context) new_idrefs bo)
256                 ) fresh_idrefs funs
257              )
258           | C.CoFix (funno, funs) ->
259              let fresh_idrefs =
260               List.map (function _ -> gen_id seed) funs in
261              let new_idrefs = List.rev fresh_idrefs @ idrefs in
262              let tys =
263               List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
264              in
265               Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
266               if innersort = "Prop" then
267                add_inner_type fresh_id'' ;
268               C.ACoFix (fresh_id'', funno,
269                List.map2
270                 (fun id (name, ty, bo) ->
271                   (id, name, aux' context idrefs ty,
272                     aux' (tys@context) new_idrefs bo)
273                 ) fresh_idrefs funs
274               )
275         in
276          aux true None context idrefs t
277 ;;
278
279 let acic_of_cic_context metasenv context idrefs t =
280  let ids_to_terms = Hashtbl.create 503 in
281  let ids_to_father_ids = Hashtbl.create 503 in
282  let ids_to_inner_sorts = Hashtbl.create 503 in
283  let ids_to_inner_types = Hashtbl.create 503 in
284  let seed = ref 0 in
285    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
286     ids_to_inner_types metasenv context idrefs t,
287    ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
288 ;;
289
290 let acic_object_of_cic_object obj =
291  let module C = Cic in
292   let ids_to_terms = Hashtbl.create 503 in
293   let ids_to_father_ids = Hashtbl.create 503 in
294   let ids_to_inner_sorts = Hashtbl.create 503 in
295   let ids_to_inner_types = Hashtbl.create 503 in
296   let ids_to_conjectures = Hashtbl.create 11 in
297   let ids_to_hypotheses = Hashtbl.create 127 in
298   let hypotheses_seed = ref 0 in
299   let conjectures_seed = ref 0 in
300   let seed = ref 0 in
301   let acic_term_of_cic_term_context' =
302    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
303     ids_to_inner_types in
304   let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] [] in
305    let aobj =
306     match obj with
307       C.Constant (id,Some bo,ty,params) ->
308        let abo = acic_term_of_cic_term' bo (Some ty) in
309        let aty = acic_term_of_cic_term' ty None in
310         C.AConstant
311          ("mettereaposto",Some "mettereaposto2",id,Some abo,aty, params)
312     | C.Constant (id,None,ty,params) ->
313        let aty = acic_term_of_cic_term' ty None in
314         C.AConstant
315          ("mettereaposto",None,id,None,aty, params)
316     | C.Variable (id,bo,ty,params) ->
317        let abo =
318         match bo with
319            None -> None
320          | Some bo -> Some (acic_term_of_cic_term' bo (Some ty))
321        in
322        let aty = acic_term_of_cic_term' ty None in
323         C.AVariable
324          ("mettereaposto",id,abo,aty, params)
325     | C.CurrentProof (id,conjectures,bo,ty,params) ->
326        let aconjectures =
327         List.map
328          (function (i,canonical_context,term) as conjecture ->
329            let cid = "c" ^ string_of_int !conjectures_seed in
330             Hashtbl.add ids_to_conjectures cid conjecture ;
331             incr conjectures_seed ;
332             let idrefs',revacanonical_context =
333              let rec aux context idrefs =
334               function
335                  [] -> idrefs,[]
336                | hyp::tl ->
337                   let hid = "h" ^ string_of_int !hypotheses_seed in
338                   let new_idrefs = hid::idrefs in
339                    Hashtbl.add ids_to_hypotheses hid hyp ;
340                    incr hypotheses_seed ;
341                    match hyp with
342                       (Some (n,C.Decl t)) ->
343                         let final_idrefs,atl =
344                          aux (hyp::context) new_idrefs tl in
345                         let at =
346                          acic_term_of_cic_term_context'
347                           conjectures context idrefs t None
348                         in
349                          final_idrefs,(hid,Some (n,C.ADecl at))::atl
350                     | (Some (n,C.Def t)) ->
351                         let final_idrefs,atl =
352                          aux (hyp::context) new_idrefs tl in
353                         let at =
354                          acic_term_of_cic_term_context'
355                           conjectures context idrefs t None
356                         in
357                          final_idrefs,(hid,Some (n,C.ADef at))::atl
358                     | None ->
359                        let final_idrefs,atl =
360                         aux (hyp::context) new_idrefs tl
361                        in
362                         final_idrefs,(hid,None)::atl
363              in
364               aux [] [] (List.rev canonical_context)
365             in
366              let aterm =
367               acic_term_of_cic_term_context' conjectures
368                canonical_context idrefs' term None
369              in
370               (cid,i,(List.rev revacanonical_context),aterm)
371          ) conjectures in
372        let abo =
373         acic_term_of_cic_term_context' conjectures [] [] bo (Some ty) in
374        let aty = acic_term_of_cic_term_context' conjectures [] [] ty None in
375         C.ACurrentProof
376          ("mettereaposto","mettereaposto2",id,aconjectures,abo,aty,params)
377     | C.InductiveDefinition (tys,params,paramsno) ->
378        let context =
379         List.map
380          (fun (name,_,arity,_) -> Some (C.Name name, C.Decl arity)) tys in
381        let idrefs = List.map (function _ -> gen_id seed) tys in
382        let atys =
383         List.map2
384          (fun id (name,inductive,ty,cons) ->
385            let acons =
386             List.map
387              (function (name,ty) ->
388                (name,
389                  acic_term_of_cic_term_context' [] context idrefs ty None)
390              ) cons
391            in
392             (id,name,inductive,acic_term_of_cic_term' ty None,acons)
393          ) (List.rev idrefs) tys
394        in
395         C.AInductiveDefinition ("mettereaposto",atys,params,paramsno)
396    in
397     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
398      ids_to_conjectures,ids_to_hypotheses
399 ;;