]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/cic2acic.ml
* Abst removed from the DTD
[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 =
74        function 
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 =
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 =
98            if computeinnertypes then
99             Some
100              {annsynthesized =
101                aux false (Some fresh_id'') context synthesized ;
102               annexpected =
103                match expected with
104                   None -> None
105                 | Some expectedty' ->
106                    Some (aux false (Some fresh_id'') context expectedty')
107              }
108            else
109             None
110           in
111            ainnertypes, synthesized, string_of_sort innersort
112        in
113         let add_inner_type id =
114          match ainnertypes with
115             None -> ()
116           | Some ainnertypes -> Hashtbl.add ids_to_inner_types id ainnertypes
117         in
118          match tt with
119             C.Rel n ->
120              let id =
121               match get_nth context n with
122                  (Some (C.Name s,_)) -> s
123                | _ -> raise NameExpected
124              in
125               Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
126               C.ARel (fresh_id'', n, id)
127           | C.Var uri ->
128              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
129              C.AVar (fresh_id'', uri)
130           | C.Meta (n,l) ->
131              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
132              C.AMeta (fresh_id'', n,
133               (List.map
134                 (function None -> None | Some t -> Some (aux' context t)) l))
135           | C.Sort s -> C.ASort (fresh_id'', s)
136           | C.Implicit -> C.AImplicit (fresh_id'')
137           | C.Cast (v,t) ->
138              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
139              if innersort = "Prop" then
140               add_inner_type fresh_id'' ;
141              C.ACast (fresh_id'', aux' context v, aux' context t)
142           | C.Prod (n,s,t) ->
143               Hashtbl.add ids_to_inner_sorts fresh_id''
144                (string_of_sort innertype) ;
145               C.AProd
146                (fresh_id'', n, aux' context s,
147                 aux' ((Some (n, C.Decl s))::context) t)
148           | C.Lambda (n,s,t) ->
149              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
150              if innersort = "Prop" then
151               begin
152                let father_is_lambda =
153                 match father with
154                    None -> false
155                  | Some father' ->
156                     match Hashtbl.find ids_to_terms father' with
157                        C.Lambda _ -> true
158                      | _ -> false
159                in
160                 if not father_is_lambda then
161                  add_inner_type fresh_id''
162               end ;
163              C.ALambda
164               (fresh_id'',n, aux' context s,
165                aux' ((Some (n, C.Decl s)::context)) t)
166           | C.LetIn (n,s,t) ->
167             Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
168             C.ALetIn
169              (fresh_id'', n, aux' context s,
170               aux' ((Some (n, C.Def s))::context) t)
171           | C.Appl l ->
172              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
173              if innersort = "Prop" then
174               add_inner_type fresh_id'' ;
175              C.AAppl (fresh_id'', List.map (aux' context) l)
176           | C.Const (uri,cn) ->
177              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
178              C.AConst (fresh_id'', uri, cn)
179           | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
180           | C.MutConstruct (uri,cn,tyno,consno) ->
181              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
182              C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
183           | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
184              Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
185              if innersort = "Prop" then
186               add_inner_type fresh_id'' ;
187              C.AMutCase (fresh_id'', uri, cn, tyno, aux' context outty,
188               aux' context term, List.map (aux' context) patterns)
189           | C.Fix (funno, funs) ->
190              let tys =
191               List.map (fun (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) funs
192              in
193               Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
194               if innersort = "Prop" then
195                add_inner_type fresh_id'' ;
196               C.AFix (fresh_id'', funno,
197                List.map
198                 (fun (name, indidx, ty, bo) ->
199                   (name, indidx, aux' context ty, aux' (tys@context) bo)
200                 ) funs
201              )
202           | C.CoFix (funno, funs) ->
203              let tys =
204               List.map (fun (name,ty,_) -> Some (C.Name name, C.Decl ty)) funs
205              in
206               Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
207               if innersort = "Prop" then
208                add_inner_type fresh_id'' ;
209               C.ACoFix (fresh_id'', funno,
210                List.map
211                 (fun (name, ty, bo) ->
212                   (name, aux' context ty, aux' (tys@context) bo)
213                 ) funs
214               )
215         in
216          aux true None context t
217 ;;
218
219 let acic_of_cic_context metasenv context t =
220  let ids_to_terms = Hashtbl.create 503 in
221  let ids_to_father_ids = Hashtbl.create 503 in
222  let ids_to_inner_sorts = Hashtbl.create 503 in
223  let ids_to_inner_types = Hashtbl.create 503 in
224  let seed = ref 0 in
225    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
226     ids_to_inner_types metasenv context t,
227    ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
228 ;;
229
230 let acic_object_of_cic_object obj =
231  let module C = Cic in
232   let ids_to_terms = Hashtbl.create 503 in
233   let ids_to_father_ids = Hashtbl.create 503 in
234   let ids_to_inner_sorts = Hashtbl.create 503 in
235   let ids_to_inner_types = Hashtbl.create 503 in
236   let ids_to_conjectures = Hashtbl.create 11 in
237   let ids_to_hypotheses = Hashtbl.create 127 in
238   let hypotheses_seed = ref 0 in
239   let conjectures_seed = ref 0 in
240   let seed = ref 0 in
241   let acic_term_of_cic_term_context' =
242    acic_of_cic_context' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
243     ids_to_inner_types in
244   let acic_term_of_cic_term' = acic_term_of_cic_term_context' [] [] in
245    let aobj =
246     match obj with
247       C.Definition (id,bo,ty,params) ->
248        let abo = acic_term_of_cic_term' bo (Some ty) in
249        let aty = acic_term_of_cic_term' ty None in
250         C.ADefinition ("mettereaposto",id,abo,aty,(Cic.Actual params))
251     | C.Axiom (id,ty,params) -> raise NotImplemented
252     | C.Variable (id,bo,ty) -> raise NotImplemented
253     | C.CurrentProof (id,conjectures,bo,ty) ->
254        let aconjectures =
255         List.map
256          (function (i,canonical_context,term) as conjecture ->
257            let cid = "c" ^ string_of_int !conjectures_seed in
258             Hashtbl.add ids_to_conjectures cid conjecture ;
259             incr conjectures_seed ;
260             let acanonical_context =
261              let rec aux =
262               function
263                  [] -> []
264                | hyp::tl ->
265                   let hid = "h" ^ string_of_int !hypotheses_seed in
266                    Hashtbl.add ids_to_hypotheses hid hyp ;
267                    incr hypotheses_seed ;
268                    match hyp with
269                       (Some (n,C.Decl t)) ->
270                         let at =
271                          acic_term_of_cic_term_context' conjectures tl t None
272                         in
273                          (hid,Some (n,C.ADecl at))::(aux tl)
274                     | (Some (n,C.Def t)) ->
275                         let at =
276                          acic_term_of_cic_term_context' conjectures tl t None
277                         in
278                          (hid,Some (n,C.ADef at))::(aux tl)
279                     | None -> (hid,None)::(aux tl)
280              in
281               aux canonical_context
282             in
283              let aterm =
284               acic_term_of_cic_term_context' conjectures canonical_context
285                term None
286              in
287               (cid,i,acanonical_context,aterm)
288          ) conjectures in
289        let abo = acic_term_of_cic_term_context' conjectures [] bo (Some ty) in
290        let aty = acic_term_of_cic_term_context' conjectures [] ty None in
291         C.ACurrentProof ("mettereaposto",id,aconjectures,abo,aty)
292     | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
293    in
294     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types,
295      ids_to_conjectures,ids_to_hypotheses
296 ;;