1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
26 (**************************************************************************)
30 (* Andrea Asperti <asperti@cs.unibo.it> *)
33 (**************************************************************************)
36 (* the type cexpr is inspired by OpenMath. A few primitive constructors
37 have been added, in order to take into account some special features
38 of functional expressions. Most notably: case, let in, let rec, and
39 explicit substitutions *)
42 Symbol of string option * string * subst option * string option
43 (* h:xref, name, subst, definitionURL *)
44 | LocalVar of (string option) * string (* h:xref, name *)
45 | Meta of string option * string * meta_subst (* h:xref, name, meta_subst *)
46 | Num of string option * string (* h:xref, value *)
47 | Appl of string option * cexpr list (* h:xref, args *)
48 | Binder of string option * string * decl * cexpr
49 (* h:xref, name, decl, body *)
50 | Letin of string option * def * cexpr (* h:xref, def, body *)
51 | Letrec of string option * def list * cexpr (* h:xref, def list, body *)
52 | Case of string option * cexpr * ((string * cexpr) list)
53 (* h:xref, case_expr, named-pattern list *)
56 decl = string * cexpr (* name, type *)
58 def = string * cexpr (* name, body *)
60 subst = (UriManager.uri * cexpr) list
62 meta_subst = cexpr option list
67 let symbol_table = Hashtbl.create 503;;
70 Hashtbl.add symbol_table HelmLibraryObjects.Logic.eq_XURI
71 (fun aid sid args acic2cexpr ->
73 (Some aid, (Symbol (Some sid, "eq",
74 None, Some HelmLibraryObjects.Logic.eq_SURI))
75 :: List.map acic2cexpr (List.tl args)));;
78 Hashtbl.add symbol_table HelmLibraryObjects.Logic.and_XURI
79 (fun aid sid args acic2cexpr ->
81 (Some aid, (Symbol (Some sid, "and",
82 None, Some HelmLibraryObjects.Logic.and_SURI))
83 :: List.map acic2cexpr args));;
86 Hashtbl.add symbol_table HelmLibraryObjects.Logic.or_XURI
87 (fun aid sid args acic2cexpr ->
89 (Some aid, (Symbol (Some sid, "or",
90 None, Some HelmLibraryObjects.Logic.or_SURI))
91 :: List.map acic2cexpr args));;
94 Hashtbl.add symbol_table HelmLibraryObjects.Logic.iff_SURI
95 (fun aid sid args acic2cexpr ->
97 (Some aid, (Symbol (Some sid, "iff",
98 None, Some HelmLibraryObjects.Logic.iff_SURI))
99 :: List.map acic2cexpr args));;
102 Hashtbl.add symbol_table HelmLibraryObjects.Logic.not_SURI
103 (fun aid sid args acic2cexpr ->
105 (Some aid, (Symbol (Some sid, "not",
106 None, Some HelmLibraryObjects.Logic.not_SURI))
107 :: List.map acic2cexpr args));;
110 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rinv_SURI
111 (fun aid sid args acic2cexpr ->
113 (Some aid, (Symbol (Some sid, "inv",
114 None, Some HelmLibraryObjects.Reals.rinv_SURI))
115 :: List.map acic2cexpr args));;
118 Hashtbl.add symbol_table HelmLibraryObjects.Reals.ropp_SURI
119 (fun aid sid args acic2cexpr ->
121 (Some aid, (Symbol (Some sid, "opp",
122 None, Some HelmLibraryObjects.Reals.ropp_SURI))
123 :: List.map acic2cexpr args));;
126 Hashtbl.add symbol_table HelmLibraryObjects.Logic.ex_XURI
127 (fun aid sid args acic2cexpr ->
128 match (List.tl args) with
129 [Cic.ALambda (_,Cic.Name n,s,t)] ->
131 (Some aid, "Exists", (n,acic2cexpr s),acic2cexpr t)
132 | _ -> raise Not_found);;
135 Hashtbl.add symbol_table HelmLibraryObjects.Peano.le_XURI
136 (fun aid sid args acic2cexpr ->
138 (Some aid, (Symbol (Some sid, "leq",
139 None, Some HelmLibraryObjects.Peano.le_SURI))
140 :: List.map acic2cexpr args));;
142 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rle_SURI
143 (fun aid sid args acic2cexpr ->
145 (Some aid, (Symbol (Some sid, "leq",
146 None, Some HelmLibraryObjects.Reals.rle_SURI))
147 :: List.map acic2cexpr args));;
150 Hashtbl.add symbol_table HelmLibraryObjects.Peano.lt_SURI
151 (fun aid sid args acic2cexpr ->
153 (Some aid, (Symbol (Some sid, "lt",
154 None, Some HelmLibraryObjects.Peano.lt_SURI))
155 :: List.map acic2cexpr args));;
157 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rlt_SURI
158 (fun aid sid args acic2cexpr ->
160 (Some aid, (Symbol (Some sid, "lt",
161 None, Some HelmLibraryObjects.Reals.rlt_SURI))
162 :: List.map acic2cexpr args));;
165 Hashtbl.add symbol_table HelmLibraryObjects.Peano.ge_SURI
166 (fun aid sid args acic2cexpr ->
168 (Some aid, (Symbol (Some sid, "geq",
169 None, Some HelmLibraryObjects.Peano.ge_SURI))
170 :: List.map acic2cexpr args));;
172 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rge_SURI
173 (fun aid sid args acic2cexpr ->
175 (Some aid, (Symbol (Some sid, "geq",
176 None, Some HelmLibraryObjects.Reals.rge_SURI))
177 :: List.map acic2cexpr args));;
180 Hashtbl.add symbol_table HelmLibraryObjects.Peano.gt_SURI
181 (fun aid sid args acic2cexpr ->
183 (Some aid, (Symbol (Some sid, "gt",
184 None, Some HelmLibraryObjects.Peano.gt_SURI))
185 :: List.map acic2cexpr args));;
187 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rgt_SURI
188 (fun aid sid args acic2cexpr ->
190 (Some aid, (Symbol (Some sid, "gt",
191 None, Some HelmLibraryObjects.Reals.rgt_SURI))
192 :: List.map acic2cexpr args));;
195 Hashtbl.add symbol_table HelmLibraryObjects.Peano.plus_SURI
196 (fun aid sid args acic2cexpr ->
198 (Some aid, (Symbol (Some sid, "plus",
199 None, Some HelmLibraryObjects.Peano.plus_SURI))
200 :: List.map acic2cexpr args));;
202 Hashtbl.add symbol_table HelmLibraryObjects.BinInt.zplus_SURI
203 (fun aid sid args acic2cexpr ->
205 (Some aid, (Symbol (Some sid, "plus",
206 None, Some HelmLibraryObjects.BinInt.zplus_SURI))
207 :: List.map acic2cexpr args));;
209 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rplus_SURI
210 (fun aid sid args acic2cexpr ->
213 (Some aid, (Symbol (Some sid, "plus",
214 None, Some HelmLibraryObjects.Reals.rplus_SURI))
215 :: List.map acic2cexpr args)
217 let rec aux acc = function
218 | [ Cic.AConst (nid, uri, []); n] when
219 UriManager.eq uri HelmLibraryObjects.Reals.r1_URI ->
221 | Cic.AConst (_, uri, []) when
222 UriManager.eq uri HelmLibraryObjects.Reals.r1_URI ->
223 Num (Some aid, string_of_int (acc + 2))
224 | Cic.AAppl (_, Cic.AConst (_, uri, []) :: args) when
225 UriManager.eq uri HelmLibraryObjects.Reals.rplus_URI ->
235 Hashtbl.add symbol_table HelmLibraryObjects.Reals.r0_SURI
236 (fun aid sid args acic2cexpr -> Num (Some sid, "0")) ;;
238 Hashtbl.add symbol_table HelmLibraryObjects.Reals.r1_SURI
239 (fun aid sid args acic2cexpr -> Num (Some sid, "1")) ;;
242 Hashtbl.add symbol_table HelmLibraryObjects.Peano.mult_SURI
243 (fun aid sid args acic2cexpr ->
245 (Some aid, (Symbol (Some sid, "times",
246 None, Some HelmLibraryObjects.Peano.mult_SURI))
247 :: List.map acic2cexpr args));;
250 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rmult_SURI
251 (fun aid sid args acic2cexpr ->
253 (Some aid, (Symbol (Some sid, "times",
254 None, Some HelmLibraryObjects.Reals.rmult_SURI))
255 :: List.map acic2cexpr args));;
257 Hashtbl.add symbol_table HelmLibraryObjects.Peano.minus_SURI
258 (fun aid sid args acic2cexpr ->
260 (Some aid, (Symbol (Some sid, "minus",
261 None, Some HelmLibraryObjects.Peano.minus_SURI))
262 :: List.map acic2cexpr args));;
264 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rminus_SURI
265 (fun aid sid args acic2cexpr ->
267 (Some aid, (Symbol (Some sid, "minus",
268 None, Some HelmLibraryObjects.Reals.rminus_SURI))
269 :: List.map acic2cexpr args));;
272 Hashtbl.add symbol_table HelmLibraryObjects.Reals.rdiv_SURI
273 (fun aid sid args acic2cexpr ->
275 (Some aid, (Symbol (Some sid, "div",
276 None, Some HelmLibraryObjects.Reals.rdiv_SURI))
277 :: List.map acic2cexpr args));;
289 | Cic.Type _ -> "Type" (* TASSI *)
290 | Cic.CProp -> "Type"
293 let get_constructors uri i =
294 let inductive_types =
295 (let o,_ = CicEnvironment.get_obj uri CicUniv.empty_ugraph in
297 Cic.Constant _ -> assert false
298 | Cic.Variable _ -> assert false
299 | Cic.CurrentProof _ -> assert false
300 | Cic.InductiveDefinition (l,_,_) -> l
302 let (_,_,_,constructors) = List.nth inductive_types i in
306 exception NotImplemented;;
308 let acic2cexpr ids_to_inner_sorts t =
309 let rec acic2cexpr t =
310 let module C = Cic in
311 let module X = Xml in
312 let module U = UriManager in
313 let module C2A = Cic2acic in
317 | l -> Some (List.map (function (uri,t) -> (uri, acic2cexpr t)) l) in
319 C.ARel (id,idref,n,b) -> LocalVar (Some id,b)
320 | C.AVar (id,uri,subst) ->
321 Symbol (Some id, UriManager.name_of_uri uri,
322 make_subst subst, Some (UriManager.string_of_uri uri))
323 | C.AMeta (id,n,l) ->
328 | Some t -> Some (acic2cexpr t)
331 Meta (Some id,("?" ^ (string_of_int n)),l')
332 | C.ASort (id,s) -> Symbol (Some id,string_of_sort s,None,None)
333 | C.AImplicit _ -> raise NotImplemented
334 | C.AProd (id,n,s,t) ->
337 Appl (Some id, [Symbol (None, "arrow",None,None);
338 acic2cexpr s; acic2cexpr t])
341 (try Hashtbl.find ids_to_inner_sorts id
343 (* if the Prod does not have the sort, it means
344 that it has been generated by cic2content, and
345 thus is a statement *)
347 let binder = if sort = "Prop" then "Forall" else "Prod" in
348 let decl = (name, acic2cexpr s) in
349 Binder (Some id,binder,decl,acic2cexpr t))
350 | C.ACast (id,v,t) -> acic2cexpr v
351 | C.ALambda (id,n,s,t) ->
355 | Cic.Name name -> name) in
356 let decl = (name, acic2cexpr s) in
357 Binder (Some id,"Lambda",decl,acic2cexpr t)
358 | C.ALetIn (id,n,s,t) ->
360 Cic.Anonymous -> assert false
362 let def = (name, acic2cexpr s) in
363 Letin (Some id,def,acic2cexpr t))
364 | C.AAppl (aid,C.AConst (sid,uri,subst)::tl) ->
365 let uri_str = UriManager.string_of_uri uri in
367 (let f = Hashtbl.find symbol_table uri_str in
368 f aid sid tl acic2cexpr)
370 Appl (Some aid, Symbol (Some sid,UriManager.name_of_uri uri,
371 make_subst subst, Some uri_str)::List.map acic2cexpr tl))
372 | C.AAppl (aid,C.AMutInd (sid,uri,i,subst)::tl) ->
373 let inductive_types =
374 (let o,_ = CicEnvironment.get_obj uri CicUniv.empty_ugraph in
376 Cic.Constant _ -> assert false
377 | Cic.Variable _ -> assert false
378 | Cic.CurrentProof _ -> assert false
379 | Cic.InductiveDefinition (l,_,_) -> l
381 let (name,_,_,_) = List.nth inductive_types i in
382 let uri_str = UriManager.string_of_uri uri in
384 uri_str ^ "#xpointer(1/" ^ (string_of_int (i + 1)) ^ ")" in
386 (let f = Hashtbl.find symbol_table puri_str in
387 f aid sid tl acic2cexpr)
389 Appl (Some aid, Symbol (Some sid, name,
390 make_subst subst, Some uri_str)::List.map acic2cexpr tl))
392 Appl (Some id, List.map acic2cexpr li)
393 | C.AConst (id,uri,subst) ->
394 let uri_str = UriManager.string_of_uri uri in
396 let f = Hashtbl.find symbol_table uri_str in
397 f "dummy" id [] acic2cexpr
399 Symbol (Some id, UriManager.name_of_uri uri,
400 make_subst subst, Some (UriManager.string_of_uri uri)))
401 | C.AMutInd (id,uri,i,subst) ->
402 let inductive_types =
403 (let o,_ = CicEnvironment.get_obj uri CicUniv.empty_ugraph in
405 Cic.Constant _ -> assert false
406 | Cic.Variable _ -> assert false
407 | Cic.CurrentProof _ -> assert false
408 | Cic.InductiveDefinition (l,_,_) -> l
410 let (name,_,_,_) = List.nth inductive_types i in
411 let uri_str = UriManager.string_of_uri uri in
412 Symbol (Some id, name, make_subst subst, Some uri_str)
413 | C.AMutConstruct (id,uri,i,j,subst) ->
414 let constructors = get_constructors uri i in
415 let (name,_) = List.nth constructors (j-1) in
416 let uri_str = UriManager.string_of_uri uri in
417 Symbol (Some id, name, make_subst subst, Some uri_str)
418 | C.AMutCase (id,uri,typeno,ty,te,patterns) ->
419 let constructors = get_constructors uri typeno in
421 List.map2 (fun c p -> (fst c, acic2cexpr p))
422 constructors patterns in
423 Case (Some id, acic2cexpr te, named_patterns)
424 | C.AFix (id, no, funs) ->
426 List.map (function (id1,n,_,_,bo) -> (n, acic2cexpr bo)) funs in
427 let (name,_) = List.nth defs no in
428 let body = LocalVar (None, name) in
429 Letrec (Some id, defs, body)
430 | C.ACoFix (id,no,funs) ->
432 List.map (function (id1,n,_,bo) -> (n, acic2cexpr bo)) funs in
433 let (name,_) = List.nth defs no in
434 let body = LocalVar (None, name) in
435 Letrec (Some id, defs, body) in