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