1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 let symbol_table = Hashtbl.create 1024
33 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
35 | Cic.InductiveDefinition (l,_,_,_) -> l
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
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
51 (* returns name only *)
52 let constructor_of_inductive_type uri i j =
54 fst (List.nth (constructors_of_inductive_type uri i) (j-1))
55 with Not_found -> assert false)
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
62 Hashtbl.find ids_to_inner_sorts id
63 with Not_found -> assert false
65 let idref id t = Ast.AttributedTerm (`IdRef id, t) in
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);
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) ->
81 match sort_of_id id with
82 | `Set | `Type | `Meta -> `Pi
83 | `Prop | `CProp -> `Forall
85 idref id (Ast.Binder (binder_kind, (n, Some (aux s)), aux t))
86 | Cic.ACast (id,v,t) -> idref id (Ast.Cast (aux v, aux t))
87 | Cic.ALambda (id,n,s,t) ->
88 idref id (Ast.Binder (`Lambda, (n, Some (aux s)), aux t))
89 | Cic.ALetIn (id,n,s,t) -> idref id (Ast.LetIn ((n, None), aux s, aux t))
90 | Cic.AAppl (aid,Cic.AConst (sid,uri,subst)::tl) ->
91 let uri_str = UriManager.string_of_uri uri in
92 register_uri sid uri_str;
94 let f = Hashtbl.find symbol_table uri_str in
99 (Ast.Ident (UriManager.name_of_uri uri,
100 astsubst_of_cicsubst subst)) :: (List.map aux tl))))
101 | Cic.AAppl (aid,Cic.AMutInd (sid,uri,i,subst)::tl) ->
102 let name = name_of_inductive_type uri i in
103 let uri_str = UriManager.string_of_uri uri in
105 uri_str ^ "#xpointer(1/" ^ (string_of_int (i + 1)) ^ ")" in
106 register_uri sid puri_str;
108 (let f = Hashtbl.find symbol_table puri_str in
114 astsubst_of_cicsubst subst)) :: (List.map aux tl))))
115 | Cic.AAppl (id,li) -> idref id (Ast.Appl (List.map aux li))
116 | Cic.AConst (id,uri,subst) ->
117 let uri_str = UriManager.string_of_uri uri in
118 register_uri id uri_str;
120 let f = Hashtbl.find symbol_table uri_str in
125 (UriManager.name_of_uri uri, astsubst_of_cicsubst subst)))
126 | Cic.AMutInd (id,uri,i,subst) ->
127 let name = name_of_inductive_type uri i in
128 let uri_str = UriManager.string_of_uri uri in
130 uri_str ^ "#xpointer(1/" ^ (string_of_int (i + 1)) ^ ")" in
131 register_uri id puri_str;
133 let f = Hashtbl.find symbol_table puri_str in
136 idref id (Ast.Ident (name, astsubst_of_cicsubst subst)))
137 | Cic.AMutConstruct (id,uri,i,j,subst) ->
138 let name = constructor_of_inductive_type uri i j in
139 let uri_str = UriManager.string_of_uri uri in
140 let puri_str = sprintf "%s#xpointer(1/%d/%d)" uri_str (i + 1) j in
141 register_uri id puri_str;
143 let f = Hashtbl.find symbol_table puri_str in
146 idref id (Ast.Ident (name, astsubst_of_cicsubst subst)))
147 | Cic.AMutCase (id,uri,typeno,ty,te,patterns) ->
148 let name = name_of_inductive_type uri typeno in
149 let constructors = constructors_of_inductive_type uri typeno in
150 let rec eat_branch ty pat =
152 | Cic.Prod (_, _, t), Cic.ALambda (_, name, s, t') ->
153 let (cv, rhs) = eat_branch t t' in
154 (name, Some (aux s)) :: cv, rhs
155 | _, _ -> [], aux pat
159 (fun (name, ty) pat ->
160 let (capture_variables, rhs) = eat_branch ty pat in
161 ((name, capture_variables), rhs))
162 constructors patterns
164 idref id (Ast.Case (aux te, Some name, Some (aux ty), patterns))
165 | Cic.AFix (id, no, funs) ->
168 (fun (_, n, decr_idx, ty, bo) ->
169 ((Cic.Name n, Some (aux ty)), aux bo, decr_idx))
174 (match List.nth defs no with
175 | (Cic.Name n, _), _, _ -> n
177 with Not_found -> assert false
179 idref id (Ast.LetRec (`Inductive, defs, Ast.Ident (name, None)))
180 | Cic.ACoFix (id, no, funs) ->
183 (fun (_, n, ty, bo) -> ((Cic.Name n, Some (aux ty)), aux bo, 0))
188 (match List.nth defs no with
189 | (Cic.Name n, _), _, _ -> n
191 with Not_found -> assert false
193 idref id (Ast.LetRec (`CoInductive, defs, Ast.Ident (name, None)))
195 and astsubst_of_cicsubst subst =
197 (List.map (fun (uri, annterm) ->
198 (UriManager.name_of_uri uri, aux annterm))
201 and astcontext_of_ciccontext context =
205 | Some annterm -> Some (aux annterm))
209 aux acic, ids_to_uris
211 let _ = (** fill symbol_table *)
212 let add_symbol name uri =
213 Hashtbl.add symbol_table uri
214 (fun aid sid args acic2ast ->
215 Ast.AttributedTerm (`IdRef aid,
216 Ast.Appl (Ast.AttributedTerm (`IdRef sid, Ast.Symbol (name, 0)) ::
217 List.map acic2ast args)))
220 Hashtbl.add symbol_table HelmLibraryObjects.Logic.eq_XURI
221 (fun aid sid args acic2ast ->
222 Ast.AttributedTerm (`IdRef aid,
224 Ast.AttributedTerm (`IdRef sid, Ast.Symbol ("eq", 0)) ::
225 List.map acic2ast (List.tl args))));
227 Hashtbl.add symbol_table HelmLibraryObjects.Logic.ex_XURI
228 (fun aid sid args acic2ast ->
229 match (List.tl args) with
230 [Cic.ALambda (_,Cic.Name n,s,t)] ->
231 Ast.AttributedTerm (`IdRef aid,
232 Ast.Binder (`Exists, (Cic.Name n, Some (acic2ast s)), acic2ast t))
233 | _ -> raise Not_found);
234 add_symbol "and" HelmLibraryObjects.Logic.and_XURI;
235 add_symbol "or" HelmLibraryObjects.Logic.or_XURI;
236 add_symbol "iff" HelmLibraryObjects.Logic.iff_SURI;
237 add_symbol "not" HelmLibraryObjects.Logic.not_SURI;
238 add_symbol "inv" HelmLibraryObjects.Reals.rinv_SURI;
239 add_symbol "opp" HelmLibraryObjects.Reals.ropp_SURI;
240 add_symbol "leq" HelmLibraryObjects.Peano.le_XURI;
241 add_symbol "leq" HelmLibraryObjects.Reals.rle_SURI;
242 add_symbol "lt" HelmLibraryObjects.Peano.lt_SURI;
243 add_symbol "lt" HelmLibraryObjects.Reals.rlt_SURI;
244 add_symbol "geq" HelmLibraryObjects.Peano.ge_SURI;
245 add_symbol "geq" HelmLibraryObjects.Reals.rge_SURI;
246 add_symbol "gt" HelmLibraryObjects.Peano.gt_SURI;
247 add_symbol "gt" HelmLibraryObjects.Reals.rgt_SURI;
248 add_symbol "plus" HelmLibraryObjects.Peano.plus_SURI;
249 add_symbol "plus" HelmLibraryObjects.BinInt.zplus_SURI;
250 add_symbol "times" HelmLibraryObjects.Peano.mult_SURI;
251 add_symbol "times" HelmLibraryObjects.Reals.rmult_SURI;
252 add_symbol "minus" HelmLibraryObjects.Peano.minus_SURI;
253 add_symbol "minus" HelmLibraryObjects.Reals.rminus_SURI;
254 add_symbol "div" HelmLibraryObjects.Reals.rdiv_SURI;
255 Hashtbl.add symbol_table HelmLibraryObjects.Reals.r0_SURI
256 (fun aid sid args acic2ast ->
257 Ast.AttributedTerm (`IdRef sid, Ast.Num ("0", 0)));
258 Hashtbl.add symbol_table HelmLibraryObjects.Reals.r1_SURI
259 (fun aid sid args acic2ast ->
260 Ast.AttributedTerm (`IdRef sid, Ast.Num ("1", 0)));
262 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rplus_SURI
263 (fun aid sid args acic2ast ->
265 Ast.AttributedTerm (`IdRef aid,
267 Ast.AttributedTerm (`IdRef sid, Ast.Symbol ("plus", 0)) ::
268 List.map acic2ast args))
270 let rec aux acc = function
271 | [ Cic.AConst (nid, uri, []); n] when
272 UriManager.eq uri HelmLibraryObjects.Reals.r1_URI ->
274 | Cic.AConst (_, uri, []) when
275 UriManager.eq uri HelmLibraryObjects.Reals.r1_URI ->
276 Ast.AttributedTerm (`IdRef aid,
277 Ast.Num (string_of_int (acc + 2), 0))
278 | Cic.AAppl (_, Cic.AConst (_, uri, []) :: args) when
279 UriManager.eq uri HelmLibraryObjects.Reals.rplus_URI ->