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