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