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