1 (* Copyright (C) 2005, 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/
26 (* $Id: termAcicContent.ml 9304 2008-12-05 23:12:39Z sacerdot $ *)
30 module Ast = CicNotationPt
33 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
38 type interpretation_id = int
41 { sort: (Cic.id, Ast.sort_kind) Hashtbl.t;
42 uri: (Cic.id, UriManager.uri) Hashtbl.t;
46 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
48 | Cic.InductiveDefinition (l,_,leftno,_) -> l, leftno
52 let freshid register_ref register_father_id =
54 fun ?reference father_id ->
56 let id = "i" ^ string_of_int !id in
57 (match reference with None -> () | Some r -> register_ref id r);
58 register_father_id id father_id;
59 Some id, fun t -> Ast.AttributedTerm (`IdRef id, t)
62 (* CODICE c&p da NCicPp *)
65 ?reference:NReference.reference -> id option ->
66 id option * (CicNotationPt.term -> CicNotationPt.term))
67 ~output_type ~subst k ~context father_id =
70 let id, idref = freshid father_id in
72 let name,_ = List.nth context (n-1) in
73 let name = if name = "_" then "__"^string_of_int n else name in
74 idref (Ast.Ident (name,None))
75 with Failure "nth" | Invalid_argument "List.nth" ->
76 idref (Ast.Ident ("-"^ string_of_int (n - List.length context),None)))
78 let id, idref = freshid ~reference:r father_id in
79 idref (Ast.Ident (NCicPp.r2s true r, None))
80 | NCic.Meta (n,lc) when List.mem_assoc n subst ->
81 let _,_,t,_ = List.assoc n subst in
82 k ~context father_id (NCicSubstitution.subst_meta lc t)
83 | NCic.Meta (n,(s,l)) ->
84 let id, idref = freshid father_id in
85 (* CSC: qua non dovremmo espandere *)
86 let l = NCicUtils.expand_local_context l in
89 List.map (fun x->Some(k ~context id (NCicSubstitution.lift s x))) l))
90 | NCic.Sort NCic.Prop ->
91 let id, idref = freshid father_id in
92 idref (Ast.Sort `Prop)
93 | NCic.Sort NCic.Type _ ->
94 let id, idref = freshid father_id in
96 (* CSC: | C.Sort (C.Type []) -> F.fprintf f "Type0"
97 | C.Sort (C.Type [false, u]) -> F.fprintf f "%s" (NUri.name_of_uri u)
98 | C.Sort (C.Type [true, u]) -> F.fprintf f "S(%s)" (NUri.name_of_uri u)
99 | C.Sort (C.Type l) ->
101 aux ctx (C.Sort (C.Type [List.hd l]));
102 List.iter (fun x -> F.fprintf f ",";aux ctx (C.Sort (C.Type [x])))
105 (* CSC: qua siamo grezzi *)
106 | NCic.Implicit `Hole ->
107 let id, idref = freshid father_id in
108 idref (Ast.UserInput)
110 let id, idref = freshid father_id in
112 | NCic.Prod (n,s,t) ->
113 let id, idref = freshid father_id in
114 let n = if n.[0] = '_' then "_" else n in
115 let binder_kind = `Forall in
117 (Ast.Binder (binder_kind, (Ast.Ident (n,None), Some (k ~context id s)),
118 k ~context:((n,NCic.Decl s)::context) id t))
119 | NCic.Lambda (n,s,t) ->
120 let id, idref = freshid father_id in
121 idref (Ast.Binder (`Lambda,(Ast.Ident (n,None), Some (k ~context id s)),
122 k ~context:((n,NCic.Decl s)::context) id t))
123 | NCic.LetIn (n,s,ty,t) ->
124 let id, idref = freshid father_id in
125 idref (Ast.LetIn ((Ast.Ident (n,None), Some (k ~context id ty)),
126 k ~context id s, k ~context:((n,NCic.Decl s)::context) id t))
127 | NCic.Appl (NCic.Meta (n,lc) :: args) when List.mem_assoc n subst ->
128 let _,_,t,_ = List.assoc n subst in
129 let hd = NCicSubstitution.subst_meta lc t in
131 (NCicReduction.head_beta_reduce ~upto:(List.length args)
133 | NCic.Appl l -> NCic.Appl (l@args)
134 | _ -> NCic.Appl (hd :: args)))
136 let id, idref = freshid father_id in
137 idref (Ast.Appl (List.map (k ~context id) args))
138 | NCic.Match (NReference.Ref (uri,_) as r,outty,te,patterns) ->
139 let id, idref = freshid father_id in
140 let name = NUri.name_of_uri uri in
142 let uri_str = UriManager.string_of_uri uri in
143 let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (typeno+1) in
145 UriManager.uri_of_string
146 (sprintf "%s#xpointer(1/%d/%d)" uri_str (typeno+1) j)
150 name, None(*CSC Some (UriManager.uri_of_string puri_str)*) in
151 let constructors, leftno =
152 let _,leftno,tys,_,n = NCicEnvironment.get_checked_indtys r in
153 let _,_,_,cl = List.nth tys n in
156 let rec eat_branch n ctx ty pat =
158 | NCic.Prod (name, s, t), _ when n > 0 ->
159 eat_branch (pred n) ((name,NCic.Decl s)::ctx) t pat
160 | NCic.Prod (_, _, t), NCic.Lambda (name, s, t') ->
161 let cv, rhs = eat_branch 0 ((name,NCic.Decl s)::ctx) t t' in
162 (Ast.Ident (name,None), Some (k ~context id s)) :: cv, rhs
163 | _, _ -> [], k ~context id pat
169 (fun (_, name, ty) pat ->
171 let name,(capture_variables,rhs) =
172 match output_type with
173 `Term -> name, eat_branch leftno context ty pat
174 | `Pattern -> "_", ([], k ~context id pat)
177 (name, None(*CSC Some (ctor_puri !j)*), capture_variables),rhs
178 ) constructors patterns
179 with Invalid_argument _ -> assert false
182 match output_type with
184 | `Term -> Some case_indty
188 (k ~context id te, indty, Some (k ~context id outty), patterns))
191 (* persistent state *)
194 let initial_level2_patterns32 () = Hashtbl.create 211
195 let initial_interpretations () = Hashtbl.create 211
197 let level2_patterns32 = ref (initial_level2_patterns32 ())
198 (* symb -> id list ref *)
199 let interpretations = ref (initial_interpretations ())
201 let compiled32 = ref None
203 let pattern32_matrix = ref []
204 let counter = ref ~-1
209 stack := (!counter,!level2_patterns32,!interpretations,!compiled32,!pattern32_matrix)::!stack;
211 level2_patterns32 := initial_level2_patterns32 ();
212 interpretations := initial_interpretations ();
214 pattern32_matrix := []
220 | (ocounter,olevel2_patterns32,ointerpretations,ocompiled32,opattern32_matrix)::old ->
223 level2_patterns32 := olevel2_patterns32;
224 interpretations := ointerpretations;
225 compiled32 := ocompiled32;
226 pattern32_matrix := opattern32_matrix
230 let get_compiled32 () =
231 match !compiled32 with
232 | None -> assert false
233 | Some f -> Lazy.force f
235 let set_compiled32 f = compiled32 := Some f
238 List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
240 let instantiate32 idrefs env symbol args =
241 let rec instantiate_arg = function
242 | Ast.IdentArg (n, name) ->
244 try List.assoc name env
245 with Not_found -> prerr_endline ("name not found in env: "^name);
248 let rec count_lambda = function
249 | Ast.AttributedTerm (_, t) -> count_lambda t
250 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
253 let rec add_lambda t n =
255 let name = CicNotationUtil.fresh_name () in
256 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
257 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
261 add_lambda t (n - count_lambda t)
264 let symbol = Ast.Symbol (symbol, 0) in
265 add_idrefs idrefs symbol
267 if args = [] then head
268 else Ast.Appl (head :: List.map instantiate_arg args)
270 let rec nast_of_cic1 ~freshid ~output_type ~subst ~context father_id term =
271 match (get_compiled32 ()) term with
273 nast_of_cic0 ~freshid ~output_type ~subst
274 (nast_of_cic1 ~freshid ~output_type ~subst) ~context father_id term
275 | Some (env, ctors, pid) ->
276 let id, idref = freshid father_id in
283 (match term with NCic.Const nref -> nref | _ -> assert false)
286 match id with Some id -> id | None -> assert false
291 name,nast_of_cic1 ~freshid ~output_type ~subst ~context id term
293 let _, symbol, args, _ =
295 TermAcicContent.find_level2_patterns32 pid
296 with Not_found -> assert false in
297 let ast = instantiate32 idrefs env symbol args in
298 idref ast (*Ast.AttributedTerm (`IdRef (idref term), ast)*)
301 let load_patterns32 t =
303 HExtlib.filter_map (function (true, ap, id) -> Some (ap, id) | _ -> None) t
305 set_compiled32 (lazy (Ncic2astMatcher.Matcher32.compiler t))
307 TermAcicContent.add_load_patterns32 load_patterns32;
308 TermAcicContent.init ()
312 let ast_of_acic ~output_type id_to_sort annterm =
313 debug_print (lazy ("ast_of_acic <- "
314 ^ CicPp.ppterm (Deannotate.deannotate_term annterm)));
315 let term_info = { sort = id_to_sort; uri = Hashtbl.create 211 } in
316 let ast = ast_of_acic1 ~output_type term_info annterm in
317 debug_print (lazy ("ast_of_acic -> " ^ CicNotationPp.pp_term ast));
325 let add_interpretation dsc (symbol, args) appl_pattern =
326 let id = fresh_id () in
327 Hashtbl.add !level2_patterns32 id (dsc, symbol, args, appl_pattern);
328 pattern32_matrix := (true, appl_pattern, id) :: !pattern32_matrix;
329 load_patterns32 !pattern32_matrix;
331 let ids = Hashtbl.find !interpretations symbol in
333 with Not_found -> Hashtbl.add !interpretations symbol (ref [id]));
336 let get_all_interpretations () =
338 (function (_, _, id) ->
341 Hashtbl.find !level2_patterns32 id
342 with Not_found -> assert false
347 let get_active_interpretations () =
348 HExtlib.filter_map (function (true, _, id) -> Some id | _ -> None)
351 let set_active_interpretations ids =
352 let pattern32_matrix' =
355 | (_, ap, id) when List.mem id ids -> (true, ap, id)
356 | (_, ap, id) -> (false, ap, id))
359 pattern32_matrix := pattern32_matrix';
360 load_patterns32 !pattern32_matrix
362 exception Interpretation_not_found
364 let lookup_interpretations symbol =
367 (List.sort Pervasives.compare
370 let (dsc, _, args, appl_pattern) =
372 Hashtbl.find !level2_patterns32 id
373 with Not_found -> assert false
375 dsc, args, appl_pattern)
376 !(Hashtbl.find !interpretations symbol)))
377 with Not_found -> raise Interpretation_not_found
379 let remove_interpretation id =
381 let dsc, symbol, _, _ = Hashtbl.find !level2_patterns32 id in
382 let ids = Hashtbl.find !interpretations symbol in
383 ids := List.filter ((<>) id) !ids;
384 Hashtbl.remove !level2_patterns32 id;
385 with Not_found -> raise Interpretation_not_found);
387 List.filter (fun (_, _, id') -> id <> id') !pattern32_matrix;
388 load_patterns32 !pattern32_matrix
390 let _ = load_patterns32 []
392 let instantiate_appl_pattern
393 ~mk_appl ~mk_implicit ~term_of_uri env appl_pattern
396 try List.assoc name env
398 prerr_endline (sprintf "Name %s not found" name);
401 let rec aux = function
402 | Ast.UriPattern uri -> term_of_uri uri
403 | Ast.ImplicitPattern -> mk_implicit false
404 | Ast.VarPattern name -> lookup name
405 | Ast.ApplPattern terms -> mk_appl (List.map aux terms)
410 let nmap_sequent0 ~freshid ~subst (i,(n,context,ty):int * NCic.conjecture) =
411 let module K = Content in
412 let nast_of_cic ~context =
413 nast_of_cic1 ~freshid ~output_type:`Term ~subst ~context None in
416 (fun item (res,context) ->
418 | name,NCic.Decl t ->
420 (* We should call build_decl_item, but we have not computed *)
421 (* the inner-types ==> we always produce a declaration *)
423 { K.dec_name = (Some name);
425 K.dec_inductive = false;
427 K.dec_type = nast_of_cic ~context t
428 })::res,item::context
429 | name,NCic.Def (t,ty) ->
431 (* We should call build_def_item, but we have not computed *)
432 (* the inner-types ==> we always produce a declaration *)
434 { K.def_name = (Some name);
437 K.def_term = nast_of_cic ~context t;
438 K.def_type = nast_of_cic ~context ty
439 })::res,item::context
442 ("-1",i,context',nast_of_cic ~context ty)
445 let nmap_sequent ~subst metasenv =
446 let module K = Content in
447 let ids_to_refs = Hashtbl.create 503 in
448 let ids_to_father_ids = Hashtbl.create 503 in
449 let register_ref = Hashtbl.add ids_to_refs in
450 let register_father_id = Hashtbl.add ids_to_father_ids in
451 nmap_sequent0 ~freshid:(freshid register_ref register_father_id) ~subst
452 metasenv,ids_to_refs,ids_to_father_ids
455 let object_prefix = "obj:";;
456 let declaration_prefix = "decl:";;
457 let definition_prefix = "def:";;
461 Ast.AttributedTerm (`IdRef id, _) -> id
465 let gen_id prefix seed =
466 let res = prefix ^ string_of_int !seed in
471 let build_def_item seed context metasenv id n t ty =
472 let module K = Content in
475 let sort = Hashtbl.find ids_to_inner_sorts id in
478 (acic2content seed context metasenv ?name:(name_of n) ~ids_to_inner_sorts ~ids_to_inner_types t)
484 { K.def_name = Some n;
485 K.def_id = gen_id definition_prefix seed;
492 Not_found -> assert false
495 let build_decl_item seed id n s =
496 let module K = Content in
500 Some (Hashtbl.find ids_to_inner_sorts (Cic2acic.source_id_of_id id))
501 with Not_found -> None
506 { K.dec_name = name_of n;
507 K.dec_id = gen_id declaration_prefix seed;
508 K.dec_inductive = false;
515 { K.dec_name = Some n;
516 K.dec_id = gen_id declaration_prefix seed;
517 K.dec_inductive = false;
523 let nmap_obj (uri,_,metasenv,subst,kind) =
524 let module K = Content in
525 let ids_to_refs = Hashtbl.create 503 in
526 let register_ref = Hashtbl.add ids_to_refs in
527 let ids_to_father_ids = Hashtbl.create 503 in
528 let register_father_id = Hashtbl.add ids_to_father_ids in
529 let freshid = freshid register_ref register_father_id in
530 let nast_of_cic ~context =
531 nast_of_cic1 ~freshid ~output_type:`Term ~subst ~context None in
536 | _ -> (*Some (List.map (map_conjectures seed) metasenv)*)
537 (*CSC: used to be the previous line, that uses seed *)
538 Some (List.map (nmap_sequent0 ~freshid ~subst) metasenv)
542 NCic.Constant (_,_,Some bo,ty,_) ->
543 let ty = nast_of_cic ~context:[] ty in
544 let bo = nast_of_cic ~context:[] bo in
545 (gen_id object_prefix seed, [], conjectures,
547 build_def_item seed [] [] (get_id bo) (NUri.name_of_uri uri) bo ty))
548 | NCic.Constant (_,_,None,ty,_) ->
549 let ty = nast_of_cic ~context:[] ty in
550 (gen_id object_prefix seed, [], conjectures,
552 (*CSC: ??? get_id ty here used to be the id of the axiom! *)
553 build_decl_item seed (get_id ty) (NUri.name_of_uri uri) ty))
555 | C.AInductiveDefinition (id,l,params,nparams,_) ->
556 (gen_id object_prefix seed, params, conjectures,
558 { K.joint_id = gen_id joint_prefix seed;
559 K.joint_kind = `Inductive nparams;
560 K.joint_defs = List.map (build_inductive seed) l
564 build_inductive seed =
565 let module K = Content in
568 { K.inductive_id = gen_id inductive_prefix seed;
569 K.inductive_name = n;
570 K.inductive_kind = b;
571 K.inductive_type = ty;
572 K.inductive_constructors = build_constructors seed l
576 build_constructors seed l =
577 let module K = Content in
580 { K.dec_name = Some n;
581 K.dec_id = gen_id declaration_prefix seed;
582 K.dec_inductive = false;
588 res,ids_to_refs,ids_to_father_ids