]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/cic2acic.ml
Computed inner-types are now also put in whd normal form.
[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_env' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
51      ids_to_inner_types metasenv env 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 rec aux computeinnertypes father bs tt =
57     let fresh_id'' = fresh_id' father tt in
58     let aux' = aux true (Some fresh_id'') in
59      (* First of all we compute the inner type and the inner sort *)
60      (* of the term. They may be useful in what follows.          *)
61      (*CSC: This is a very inefficient way of computing inner types *)
62      (*CSC: and inner sorts: very deep terms have their types/sorts *)
63      (*CSC: computed again and again.                               *)
64      let string_of_sort =
65       function 
66          C.Sort C.Prop -> "Prop"
67        | C.Sort C.Set  -> "Set"
68        | C.Sort C.Type -> "Type"
69        | _ -> assert false
70      in
71       let ainnertype,innertype,innersort =
72        let cicenv = List.map (function (_,ty) -> ty) bs in
73 (*CSC: Here we need the algorithm for Coscoy's double type-inference  *)
74 (*CSC: (expected type + inferred type). Just for now we use the usual *)
75 (*CSC: type-inference, but the result is very poort. As a very weak   *)
76 (*CSC: patch, I apply whd to the computed type. Full beta             *)
77 (*CSC: reduction would be a much better option.                       *)
78         let innertype = CicReduction.whd (T.type_of_aux' metasenv cicenv tt) in
79          let innersort = T.type_of_aux' metasenv cicenv innertype in
80           let ainnertype =
81            if computeinnertypes then
82             Some (aux false (Some fresh_id'') bs innertype)
83            else
84             None
85           in
86            ainnertype, innertype, string_of_sort innersort
87       in
88       let add_inner_type id =
89        match ainnertype with
90           None -> ()
91         | Some ainnertype -> Hashtbl.add ids_to_inner_types id ainnertype
92       in
93        match tt with
94           C.Rel n ->
95            let id =
96             match get_nth bs n with
97                (C.Name s,_) -> s
98              | _ -> raise NameExpected
99            in
100             Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
101             C.ARel (fresh_id'', n, id)
102         | C.Var uri ->
103            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
104            C.AVar (fresh_id'', uri)
105         | C.Meta n ->
106            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
107            C.AMeta (fresh_id'', n)
108         | C.Sort s -> C.ASort (fresh_id'', s)
109         | C.Implicit -> C.AImplicit (fresh_id'')
110         | C.Cast (v,t) ->
111            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
112            if innersort = "Prop" then
113             add_inner_type fresh_id'' ;
114            C.ACast (fresh_id'', aux' bs v, aux' bs t)
115         | C.Prod (n,s,t) ->
116             Hashtbl.add ids_to_inner_sorts fresh_id''
117              (string_of_sort innertype) ;
118             C.AProd (fresh_id'', n, aux' bs s, aux' ((n, C.Decl s)::bs) t)
119         | C.Lambda (n,s,t) ->
120            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
121            if innersort = "Prop" then
122             begin
123              let father_is_lambda =
124               match father with
125                  None -> false
126                | Some father' ->
127                   match Hashtbl.find ids_to_terms father' with
128                      C.Lambda _ -> true
129                    | _ -> false
130              in
131               if not father_is_lambda then
132                add_inner_type fresh_id''
133             end ;
134            C.ALambda (fresh_id'',n, aux' bs s, aux' ((n, C.Decl s)::bs) t)
135         | C.LetIn (n,s,t) ->
136           Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
137           C.ALetIn (fresh_id'', n, aux' bs s, aux' ((n, C.Def s)::bs) t)
138         | C.Appl l ->
139            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
140            if innersort = "Prop" then
141             add_inner_type fresh_id'' ;
142            C.AAppl (fresh_id'', List.map (aux' bs) l)
143         | C.Const (uri,cn) ->
144            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
145            C.AConst (fresh_id'', uri, cn)
146         | C.Abst _ -> raise NotImplemented
147         | C.MutInd (uri,cn,tyno) -> C.AMutInd (fresh_id'', uri, cn, tyno)
148         | C.MutConstruct (uri,cn,tyno,consno) ->
149            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
150            C.AMutConstruct (fresh_id'', uri, cn, tyno, consno)
151         | C.MutCase (uri, cn, tyno, outty, term, patterns) ->
152            Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
153            if innersort = "Prop" then
154             add_inner_type fresh_id'' ;
155            C.AMutCase (fresh_id'', uri, cn, tyno, aux' bs outty,
156             aux' bs term, List.map (aux' bs) patterns)
157         | C.Fix (funno, funs) ->
158            let names =
159             List.map (fun (name,_,ty,_) -> C.Name name, C.Decl ty) funs
160            in
161             Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
162             if innersort = "Prop" then
163              add_inner_type fresh_id'' ;
164             C.AFix (fresh_id'', funno,
165              List.map
166               (fun (name, indidx, ty, bo) ->
167                 (name, indidx, aux' bs ty, aux' (names@bs) bo)
168               ) funs
169            )
170         | C.CoFix (funno, funs) ->
171            let names =
172             List.map (fun (name,ty,_) -> C.Name name, C.Decl ty) funs in
173             Hashtbl.add ids_to_inner_sorts fresh_id'' innersort ;
174             if innersort = "Prop" then
175              add_inner_type fresh_id'' ;
176             C.ACoFix (fresh_id'', funno,
177              List.map
178               (fun (name, ty, bo) ->
179                 (name, aux' bs ty, aux' (names@bs) bo)
180               ) funs
181             )
182       in
183        aux true None env t
184 ;;
185
186 let acic_of_cic_env metasenv env t =
187  let ids_to_terms = Hashtbl.create 503 in
188  let ids_to_father_ids = Hashtbl.create 503 in
189  let ids_to_inner_sorts = Hashtbl.create 503 in
190  let ids_to_inner_types = Hashtbl.create 503 in
191  let seed = ref 0 in
192    acic_of_cic_env' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
193     ids_to_inner_types metasenv env t,
194    ids_to_terms, ids_to_father_ids, ids_to_inner_sorts, ids_to_inner_types
195 ;;
196
197 exception Found of (Cic.name * Cic.context_entry) list;;
198
199 (* get_context_of_meta meta term                                 *)
200 (* returns the context of the occurrence of [meta] in [term].    *)
201 (* Warning: if [meta] occurs not linearly in [term], the context *)
202 (* of one "random" occurrence is returned.                       *)
203 let get_context_of_meta meta term =
204  let module C = Cic in
205   let rec aux ctx =
206    function
207       C.Rel _
208     | C.Var _ -> ()
209     | C.Meta i when meta = i -> raise (Found ctx)
210     | C.Meta _
211     | C.Sort _
212     | C.Implicit -> ()
213     | C.Cast (te,ty) -> aux ctx te ; aux ctx ty
214     | C.Prod (n,s,t) -> aux ctx s ; aux ((n, C.Decl s)::ctx) t
215     | C.Lambda (n,s,t) -> aux ctx s ; aux ((n, C.Decl s)::ctx) t
216     | C.LetIn (n,s,t) -> aux ctx s ; aux ((n, C.Def s)::ctx) t
217     | C.Appl l -> List.iter (aux ctx) l
218     | C.Const _ -> ()
219     | C.Abst _ -> assert false
220     | C.MutInd _
221     | C.MutConstruct _ -> ()
222     | C.MutCase (_,_,_,outt,t,pl) ->
223        aux ctx outt ; aux ctx t; List.iter (aux ctx) pl
224     | C.Fix (_,ifl) ->
225        let counter = ref 0 in
226         let ctx' =
227          List.rev_map
228           (function (name,_,ty,bo) ->
229             let res = (C.Name name, C.Def (C.Fix (!counter,ifl))) in
230              incr counter ;
231              res
232           ) ifl
233          @ ctx
234         in
235          List.iter (function (_,_,ty,bo) -> aux ctx ty ; aux ctx' bo) ifl
236     | C.CoFix (_,ifl) ->
237        let counter = ref 0 in
238         let ctx' =
239          List.rev_map
240           (function (name,ty,bo) ->
241             let res = (C.Name name, C.Def (C.CoFix (!counter,ifl))) in
242              incr counter ;
243              res
244           ) ifl
245          @ ctx
246         in
247          List.iter (function (_,ty,bo) -> aux ctx ty ; aux ctx' bo) ifl
248   in
249    try
250     aux [] term ;
251     assert false (* No occurrences found. *)
252    with
253     Found context -> context
254 ;;
255
256 exception NotImplemented;;
257
258 let acic_object_of_cic_object obj =
259  let module C = Cic in
260   let ids_to_terms = Hashtbl.create 503 in
261   let ids_to_father_ids = Hashtbl.create 503 in
262   let ids_to_inner_sorts = Hashtbl.create 503 in
263   let ids_to_inner_types = Hashtbl.create 503 in
264   let seed = ref 0 in
265   let acic_term_of_cic_term_env' =
266    acic_of_cic_env' seed ids_to_terms ids_to_father_ids ids_to_inner_sorts
267     ids_to_inner_types in
268   let acic_term_of_cic_term' = acic_term_of_cic_term_env' [] [] in
269    let aobj =
270     match obj with
271       C.Definition (id,bo,ty,params) ->
272        let abo = acic_term_of_cic_term' bo in
273        let aty = acic_term_of_cic_term' ty
274        in
275         C.ADefinition ("mettereaposto",id,abo,aty,(Cic.Actual params))
276     | C.Axiom (id,ty,params) -> raise NotImplemented
277     | C.Variable (id,bo,ty) -> raise NotImplemented
278     | C.CurrentProof (id,conjectures,bo,ty) ->
279        let aconjectures =
280         List.map
281          (function (i,term) ->
282            let context = get_context_of_meta i bo in
283             let aterm = acic_term_of_cic_term_env' conjectures context term in
284              (i, aterm))
285          conjectures in
286        let abo = acic_term_of_cic_term_env' conjectures [] bo in
287        let aty = acic_term_of_cic_term_env' conjectures [] ty in
288         C.ACurrentProof ("mettereaposto",id,aconjectures,abo,aty)
289     | C.InductiveDefinition (tys,params,paramsno) -> raise NotImplemented
290    in
291     aobj,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,ids_to_inner_types
292 ;;