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