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