]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/acic2Ast.ml
version 0.7.1
[helm.git] / helm / ocaml / cic_transformations / acic2Ast.ml
1 (* Copyright (C) 2004, 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://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 module Ast = CicAst
29
30 let symbol_table = Hashtbl.create 1024
31
32 let get_types uri =
33   let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
34     match o with
35       | Cic.InductiveDefinition (l,_,_,_) -> l 
36       | _ -> assert false
37
38 let name_of_inductive_type uri i = 
39   let types = get_types uri in
40   let (name, _, _, _) = try List.nth types i with Not_found -> assert false in
41   name
42
43   (* returns <name, type> pairs *)
44 let constructors_of_inductive_type uri i =
45   let types = get_types uri in
46   let (_, _, _, constructors) = 
47     try List.nth types i with Not_found -> assert false
48   in
49   constructors
50
51   (* returns name only *)
52 let constructor_of_inductive_type uri i j =
53   (try
54     fst (List.nth (constructors_of_inductive_type uri i) (j-1))
55   with Not_found -> assert false)
56
57 let ast_of_acic ids_to_inner_sorts acic =
58   let ids_to_uris = Hashtbl.create 503 in
59   let register_uri id uri = Hashtbl.add ids_to_uris id uri in
60   let sort_of_id id =
61     try
62       Hashtbl.find ids_to_inner_sorts id
63     with Not_found -> assert false
64   in
65   let idref id t = Ast.AttributedTerm (`IdRef id, t) in
66   let rec aux =
67     function
68     | Cic.ARel (id,_,_,b) -> idref id (Ast.Ident (b, None))
69     | Cic.AVar (id,uri,subst) ->
70         register_uri id (UriManager.string_of_uri uri);
71         idref id
72           (Ast.Ident (UriManager.name_of_uri uri, astsubst_of_cicsubst subst))
73     | Cic.AMeta (id,n,l) -> idref id (Ast.Meta (n, astcontext_of_ciccontext l))
74     | Cic.ASort (id,Cic.Prop) -> idref id (Ast.Sort `Prop)
75     | Cic.ASort (id,Cic.Set) -> idref id (Ast.Sort `Set)
76     | Cic.ASort (id,Cic.Type _) -> idref id (Ast.Sort `Type) (* TASSI *)
77     | Cic.ASort (id,Cic.CProp) -> idref id (Ast.Sort `CProp)
78     | Cic.AImplicit _ -> assert false
79     | Cic.AProd (id,n,s,t) ->
80         let binder_kind =
81           match sort_of_id id with
82           | `Set | `Type | `Meta -> `Pi
83           | `Prop | `CProp -> `Forall
84         in
85         idref id (Ast.Binder (binder_kind, (n, Some (aux s)), aux t))
86     | Cic.ACast (id,v,t) ->
87         idref id (Ast.Appl [idref id (Ast.Symbol ("cast", 0)); aux v; aux t])
88     | Cic.ALambda (id,n,s,t) ->
89         idref id (Ast.Binder (`Lambda, (n, Some (aux s)), aux t))
90     | Cic.ALetIn (id,n,s,t) -> idref id (Ast.LetIn ((n, None), aux s, aux t))
91     | Cic.AAppl (aid,Cic.AConst (sid,uri,subst)::tl) ->
92         let uri_str = UriManager.string_of_uri uri in
93         register_uri sid uri_str;
94         (try 
95           let f = Hashtbl.find symbol_table uri_str in
96           f aid sid tl aux
97         with Not_found ->
98           idref aid
99             (Ast.Appl (idref sid
100               (Ast.Ident (UriManager.name_of_uri uri,
101                 astsubst_of_cicsubst subst)) :: (List.map aux tl))))
102     | Cic.AAppl (aid,Cic.AMutInd (sid,uri,i,subst)::tl) ->
103         let name = name_of_inductive_type uri i in
104         let uri_str = UriManager.string_of_uri uri in
105         let puri_str =
106          uri_str ^ "#xpointer(1/" ^ (string_of_int (i + 1)) ^ ")" in
107         register_uri sid puri_str;
108         (try 
109           (let f = Hashtbl.find symbol_table puri_str in
110            f aid sid tl aux)
111          with Not_found ->
112            idref aid
113             (Ast.Appl (idref sid
114               (Ast.Ident (name,
115                 astsubst_of_cicsubst subst)) :: (List.map aux tl))))
116     | Cic.AAppl (id,li) -> idref id (Ast.Appl (List.map aux li))
117     | Cic.AConst (id,uri,subst) ->
118         let uri_str = UriManager.string_of_uri uri in
119         register_uri id uri_str;
120         (try
121           let f = Hashtbl.find symbol_table uri_str in
122           f "dummy" id [] aux
123         with Not_found ->
124           idref id
125             (Ast.Ident
126               (UriManager.name_of_uri uri, astsubst_of_cicsubst subst)))
127     | Cic.AMutInd (id,uri,i,subst) ->
128         let name = name_of_inductive_type uri i in
129         let uri_str = UriManager.string_of_uri uri in
130         let puri_str =
131          uri_str ^ "#xpointer(1/" ^ (string_of_int (i + 1)) ^ ")" in
132         register_uri id puri_str;
133         (try
134           let f = Hashtbl.find symbol_table puri_str in
135           f "dummy" id [] aux
136         with Not_found ->
137           idref id (Ast.Ident (name, astsubst_of_cicsubst subst)))
138     | Cic.AMutConstruct (id,uri,i,j,subst) ->
139         let name = constructor_of_inductive_type uri i j in
140         let uri_str = UriManager.string_of_uri uri in
141         let puri_str = sprintf "%s#xpointer(1/%d/%d)" uri_str (i + 1) j in
142         register_uri id puri_str;
143         (try
144           let f = Hashtbl.find symbol_table puri_str in
145           f "dummy" id [] aux
146         with Not_found ->
147           idref id (Ast.Ident (name, astsubst_of_cicsubst subst)))
148     | Cic.AMutCase (id,uri,typeno,ty,te,patterns) ->
149         let name = name_of_inductive_type uri typeno in
150         let constructors = constructors_of_inductive_type uri typeno in
151         let rec eat_branch ty pat =
152           match (ty, pat) with
153           | Cic.Prod (_, _, t), Cic.ALambda (_, name, s, t') ->
154               let (cv, rhs) = eat_branch t t' in
155               (name, Some (aux s)) :: cv, rhs
156           | _, _ -> [], aux pat
157         in
158         let patterns =
159           List.map2
160             (fun (name, ty) pat ->
161               let (capture_variables, rhs) = eat_branch ty pat in
162               ((name, capture_variables), rhs))
163             constructors patterns
164         in
165         idref id (Ast.Case (aux te, Some name, Some (aux ty), patterns))
166     | Cic.AFix (id, no, funs) -> 
167         let defs = 
168           List.map
169             (fun (_, n, decr_idx, ty, bo) ->
170               ((Cic.Name n, Some (aux ty)), aux bo, decr_idx))
171             funs
172         in
173         let name =
174           try
175             (match List.nth defs no with
176             | (Cic.Name n, _), _, _ -> n
177             | _ -> assert false)
178           with Not_found -> assert false
179         in
180         idref id (Ast.LetRec (`Inductive, defs, Ast.Ident (name, None)))
181     | Cic.ACoFix (id, no, funs) -> 
182         let defs = 
183           List.map
184             (fun (_, n, ty, bo) -> ((Cic.Name n, Some (aux ty)), aux bo, 0))
185             funs
186         in
187         let name =
188           try
189             (match List.nth defs no with
190             | (Cic.Name n, _), _, _ -> n
191             | _ -> assert false)
192           with Not_found -> assert false
193         in
194         idref id (Ast.LetRec (`CoInductive, defs, Ast.Ident (name, None)))
195
196   and astsubst_of_cicsubst subst =
197     Some
198       (List.map (fun (uri, annterm) ->
199         (UriManager.name_of_uri uri, aux annterm))
200         subst)
201
202   and astcontext_of_ciccontext context =
203     List.map
204       (function
205         | None -> None
206         | Some annterm -> Some (aux annterm))
207       context
208
209   in
210   aux acic, ids_to_uris
211
212 let _ = (** fill symbol_table *)
213   let add_symbol name uri =
214     Hashtbl.add symbol_table uri
215       (fun aid sid args acic2ast ->
216         Ast.AttributedTerm (`IdRef aid,
217           Ast.Appl (Ast.AttributedTerm (`IdRef sid, Ast.Symbol (name, 0)) ::
218             List.map acic2ast args)))
219   in
220     (* eq *)
221   Hashtbl.add symbol_table HelmLibraryObjects.Logic.eq_XURI
222     (fun aid sid args acic2ast ->
223       Ast.AttributedTerm (`IdRef aid,
224         Ast.Appl (
225           Ast.AttributedTerm (`IdRef sid, Ast.Symbol ("eq", 0)) ::
226           List.map acic2ast (List.tl args))));
227     (* exists *)
228   Hashtbl.add symbol_table HelmLibraryObjects.Logic.ex_XURI 
229     (fun aid sid args acic2ast ->
230      match (List.tl args) with
231        [Cic.ALambda (_,Cic.Name n,s,t)] ->
232          Ast.AttributedTerm (`IdRef aid,
233           Ast.Binder (`Exists, (Cic.Name n, Some (acic2ast s)), acic2ast t))
234     | _ -> raise Not_found);
235   add_symbol "and" HelmLibraryObjects.Logic.and_XURI;
236   add_symbol "or" HelmLibraryObjects.Logic.or_XURI;
237   add_symbol "iff" HelmLibraryObjects.Logic.iff_SURI;
238   add_symbol "not" HelmLibraryObjects.Logic.not_SURI;
239   add_symbol "inv" HelmLibraryObjects.Reals.rinv_SURI;
240   add_symbol "opp" HelmLibraryObjects.Reals.ropp_SURI;
241   add_symbol "leq" HelmLibraryObjects.Peano.le_XURI;
242   add_symbol "leq" HelmLibraryObjects.Reals.rle_SURI;
243   add_symbol "lt" HelmLibraryObjects.Peano.lt_SURI;
244   add_symbol "lt" HelmLibraryObjects.Reals.rlt_SURI;
245   add_symbol "geq" HelmLibraryObjects.Peano.ge_SURI;
246   add_symbol "geq" HelmLibraryObjects.Reals.rge_SURI;
247   add_symbol "gt" HelmLibraryObjects.Peano.gt_SURI;
248   add_symbol "gt" HelmLibraryObjects.Reals.rgt_SURI;
249   add_symbol "plus" HelmLibraryObjects.Peano.plus_SURI;
250   add_symbol "plus" HelmLibraryObjects.BinInt.zplus_SURI;
251   add_symbol "times" HelmLibraryObjects.Peano.mult_SURI;
252   add_symbol "times" HelmLibraryObjects.Reals.rmult_SURI;
253   add_symbol "minus" HelmLibraryObjects.Peano.minus_SURI;
254   add_symbol "minus" HelmLibraryObjects.Reals.rminus_SURI;
255   add_symbol "div" HelmLibraryObjects.Reals.rdiv_SURI;
256   Hashtbl.add symbol_table HelmLibraryObjects.Reals.r0_SURI
257   (fun aid sid args acic2ast ->
258     Ast.AttributedTerm (`IdRef sid, Ast.Num ("0", 0)));
259   Hashtbl.add symbol_table HelmLibraryObjects.Reals.r1_SURI
260   (fun aid sid args acic2ast ->
261     Ast.AttributedTerm (`IdRef sid, Ast.Num ("1", 0)));
262     (* plus *)
263   Hashtbl.add symbol_table HelmLibraryObjects.Reals.rplus_SURI
264     (fun aid sid args acic2ast ->
265      let appl () =
266        Ast.AttributedTerm (`IdRef aid,
267         Ast.Appl (
268           Ast.AttributedTerm (`IdRef sid, Ast.Symbol ("plus", 0)) ::
269           List.map acic2ast args))
270      in
271       let rec aux acc = function
272         | [ Cic.AConst (nid, uri, []); n] when
273             UriManager.eq uri HelmLibraryObjects.Reals.r1_URI ->
274               (match n with
275               | Cic.AConst (_, uri, []) when
276                  UriManager.eq uri HelmLibraryObjects.Reals.r1_URI ->
277                    Ast.AttributedTerm (`IdRef aid,
278                     Ast.Num (string_of_int (acc + 2), 0))
279               | Cic.AAppl (_, Cic.AConst (_, uri, []) :: args) when
280                   UriManager.eq uri HelmLibraryObjects.Reals.rplus_URI ->
281                     aux (acc + 1) args
282               | _ -> appl ())
283         | _ -> appl ()
284       in
285       aux 0 args)
286