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 ()
36 type interpretation_id = int
38 let idref id t = Ast.AttributedTerm (`IdRef id, t)
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,_,lpsno,_) -> l, lpsno
51 let name_of_inductive_type uri i =
52 let types, _ = get_types uri in
53 let (name, _, _, _) = try List.nth types i with Not_found -> assert false in
56 (* returns <name, type> pairs *)
57 let constructors_of_inductive_type uri i =
58 let types, _ = get_types uri in
59 let (_, _, _, constructors) =
60 try List.nth types i with Not_found -> assert false
64 (* returns name only *)
65 let constructor_of_inductive_type uri i j =
67 fst (List.nth (constructors_of_inductive_type uri i) (j-1))
68 with Not_found -> assert false)
70 (* returns the number of left parameters *)
71 let left_params_no_of_inductive_type uri =
75 (* CODICE c&p da NCicPp *)
76 let r2s pp_fix_name r =
79 | NReference.Ref (u,NReference.Ind (_,i,_)) ->
80 (match NCicLibrary.get_obj u with
81 | _,_,_,_, NCic.Inductive (_,_,itl,_) ->
82 let _,name,_,_ = List.nth itl i in name
84 | NReference.Ref (u,NReference.Con (i,j,_)) ->
85 (match NCicLibrary.get_obj u with
86 | _,_,_,_, NCic.Inductive (_,_,itl,_) ->
87 let _,_,_,cl = List.nth itl i in
88 let _,name,_ = List.nth cl (j-1) in name
90 | NReference.Ref (u,(NReference.Decl | NReference.Def _)) ->
91 (match NCicLibrary.get_obj u with
92 | _,_,_,_, NCic.Constant (_,name,_,_,_) -> name
94 | NReference.Ref (u,(NReference.Fix (i,_,_)|NReference.CoFix i)) ->
95 (match NCicLibrary.get_obj u with
96 | _,_,_,_, NCic.Fixpoint (_,fl,_) ->
98 let _,name,_,_,_ = List.nth fl i in name
100 NUri.name_of_uri u ^"("^ string_of_int i ^ ")"
102 with NCicLibrary.ObjectNotFound _ -> NReference.string_of_reference r
107 | NCic.Rel n -> Ast.Ident (string_of_int n, None)
108 | NCic.Const r -> Ast.Ident (r2s true r, None)
109 | NCic.Meta (n,l) -> Ast.Meta (n, [](*aux_context l*))
110 | NCic.Sort NCic.Prop -> Ast.Sort `Prop
111 | NCic.Sort NCic.Type _ -> Ast.Sort `Set
112 | NCic.Implicit `Hole -> Ast.UserInput
113 | NCic.Implicit _ -> Ast.Implicit
114 | NCic.Prod (n,s,t) ->
115 let binder_kind = `Forall in
116 Ast.Binder (binder_kind, (Ast.Ident (n,None), Some (k s)), k t)
117 | NCic.Lambda (n,s,t) ->
118 Ast.Binder (`Lambda,(Ast.Ident (n,None), Some (k s)), k t)
119 | NCic.LetIn (n,s,ty,t) ->
120 Ast.LetIn ((Ast.Ident (n,None), Some (k ty)), k s, k t)
121 | NCic.Appl args -> Ast.Appl (List.map k args)
122 (*| NCic.AConst (id,uri,substs) ->
124 idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))*)
125 | NCic.Match (uri,ty,te,patterns) ->
127 let name = NReference.name_of_reference uri in
128 let uri_str = UriManager.string_of_uri uri in
129 let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (typeno+1) in
131 UriManager.uri_of_string
132 (sprintf "%s#xpointer(1/%d/%d)" uri_str (typeno+1) j)
134 let case_indty = name, Some (UriManager.uri_of_string puri_str) in
135 let constructors = constructors_of_inductive_type uri typeno in
136 let lpsno = left_params_no_of_inductive_type uri in
137 let rec eat_branch n ty pat =
139 | NCic.Prod (_, _, t), _ when n > 0 -> eat_branch (pred n) t pat
140 | NCic.Prod (_, _, t), NCic.ALambda (_, name, s, t') ->
141 let (cv, rhs) = eat_branch 0 t t' in
142 (CicNotationUtil.name_of_cic_name name, Some (k s)) :: cv, rhs
149 (fun (name, ty) pat ->
151 let name,(capture_variables,rhs) =
152 match output_type with
153 `Term -> name, eat_branch lpsno ty pat
154 | `Pattern -> "_", ([], k pat)
156 Ast.Pattern (name, Some (ctor_puri !j), capture_variables), rhs
157 ) constructors patterns
158 with Invalid_argument _ -> assert false
161 match output_type with
163 | `Term -> Some case_indty
165 idref id (Ast.Case (k te, indty, Some (k ty), patterns))
166 *) Ast.Ident ("Un case",None)
171 let map_sequent (i,(n,context,ty):int * NCic.conjecture) =
172 let module K = Content in
176 | name,NCic.Decl t ->
178 (* We should call build_decl_item, but we have not computed *)
179 (* the inner-types ==> we always produce a declaration *)
181 { K.dec_name = (Some name);
183 K.dec_inductive = false;
187 | name,NCic.Def (t,ty) ->
189 (* We should call build_def_item, but we have not computed *)
190 (* the inner-types ==> we always produce a declaration *)
192 { K.def_name = (Some name);
204 (* persistent state *)
206 let initial_level2_patterns32 () = Hashtbl.create 211
207 let initial_interpretations () = Hashtbl.create 211
209 let level2_patterns32 = ref (initial_level2_patterns32 ())
210 (* symb -> id list ref *)
211 let interpretations = ref (initial_interpretations ())
212 let compiled32 = ref None
213 let pattern32_matrix = ref []
214 let counter = ref ~-1
219 stack := (!counter,!level2_patterns32,!interpretations,!compiled32,!pattern32_matrix)::!stack;
221 level2_patterns32 := initial_level2_patterns32 ();
222 interpretations := initial_interpretations ();
224 pattern32_matrix := []
230 | (ocounter,olevel2_patterns32,ointerpretations,ocompiled32,opattern32_matrix)::old ->
233 level2_patterns32 := olevel2_patterns32;
234 interpretations := ointerpretations;
235 compiled32 := ocompiled32;
236 pattern32_matrix := opattern32_matrix
239 let get_compiled32 () =
240 match !compiled32 with
241 | None -> assert false
242 | Some f -> Lazy.force f
244 let set_compiled32 f = compiled32 := Some f
247 List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
249 let instantiate32 term_info idrefs env symbol args =
250 let rec instantiate_arg = function
251 | Ast.IdentArg (n, name) ->
253 try List.assoc name env
254 with Not_found -> prerr_endline ("name not found in env: "^name);
257 let rec count_lambda = function
258 | Ast.AttributedTerm (_, t) -> count_lambda t
259 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
262 let rec add_lambda t n =
264 let name = CicNotationUtil.fresh_name () in
265 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
266 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
270 add_lambda t (n - count_lambda t)
273 let symbol = Ast.Symbol (symbol, 0) in
274 add_idrefs idrefs symbol
276 if args = [] then head
277 else Ast.Appl (head :: List.map instantiate_arg args)
279 let rec ast_of_acic1 ~output_type term_info annterm =
280 let id_to_uris = term_info.uri in
281 let register_uri id uri = Hashtbl.add id_to_uris id uri in
282 match (get_compiled32 ()) annterm with
284 ast_of_acic0 ~output_type term_info annterm (ast_of_acic1 ~output_type)
285 | Some (env, ctors, pid) ->
289 let idref = CicUtil.id_of_annterm annterm in
292 (CicUtil.uri_of_term (Deannotate.deannotate_term annterm))
293 with Invalid_argument _ -> ());
299 (fun (name, term) -> name, ast_of_acic1 ~output_type term_info term) env
301 let _, symbol, args, _ =
303 Hashtbl.find !level2_patterns32 pid
304 with Not_found -> assert false
306 let ast = instantiate32 term_info idrefs env' symbol args in
307 Ast.AttributedTerm (`IdRef (CicUtil.id_of_annterm annterm), ast)
309 let load_patterns32 t =
311 HExtlib.filter_map (function (true, ap, id) -> Some (ap, id) | _ -> None) t
313 set_compiled32 (lazy (Acic2astMatcher.Matcher32.compiler t))
315 let ast_of_acic ~output_type id_to_sort annterm =
316 debug_print (lazy ("ast_of_acic <- "
317 ^ CicPp.ppterm (Deannotate.deannotate_term annterm)));
318 let term_info = { sort = id_to_sort; uri = Hashtbl.create 211 } in
319 let ast = ast_of_acic1 ~output_type term_info annterm in
320 debug_print (lazy ("ast_of_acic -> " ^ CicNotationPp.pp_term ast));
328 let add_interpretation dsc (symbol, args) appl_pattern =
329 let id = fresh_id () in
330 Hashtbl.add !level2_patterns32 id (dsc, symbol, args, appl_pattern);
331 pattern32_matrix := (true, appl_pattern, id) :: !pattern32_matrix;
332 load_patterns32 !pattern32_matrix;
334 let ids = Hashtbl.find !interpretations symbol in
336 with Not_found -> Hashtbl.add !interpretations symbol (ref [id]));
339 let get_all_interpretations () =
341 (function (_, _, id) ->
344 Hashtbl.find !level2_patterns32 id
345 with Not_found -> assert false
350 let get_active_interpretations () =
351 HExtlib.filter_map (function (true, _, id) -> Some id | _ -> None)
354 let set_active_interpretations ids =
355 let pattern32_matrix' =
358 | (_, ap, id) when List.mem id ids -> (true, ap, id)
359 | (_, ap, id) -> (false, ap, id))
362 pattern32_matrix := pattern32_matrix';
363 load_patterns32 !pattern32_matrix
365 exception Interpretation_not_found
367 let lookup_interpretations symbol =
370 (List.sort Pervasives.compare
373 let (dsc, _, args, appl_pattern) =
375 Hashtbl.find !level2_patterns32 id
376 with Not_found -> assert false
378 dsc, args, appl_pattern)
379 !(Hashtbl.find !interpretations symbol)))
380 with Not_found -> raise Interpretation_not_found
382 let remove_interpretation id =
384 let dsc, symbol, _, _ = Hashtbl.find !level2_patterns32 id in
385 let ids = Hashtbl.find !interpretations symbol in
386 ids := List.filter ((<>) id) !ids;
387 Hashtbl.remove !level2_patterns32 id;
388 with Not_found -> raise Interpretation_not_found);
390 List.filter (fun (_, _, id') -> id <> id') !pattern32_matrix;
391 load_patterns32 !pattern32_matrix
393 let _ = load_patterns32 []
395 let instantiate_appl_pattern
396 ~mk_appl ~mk_implicit ~term_of_uri env appl_pattern
399 try List.assoc name env
401 prerr_endline (sprintf "Name %s not found" name);
404 let rec aux = function
405 | Ast.UriPattern uri -> term_of_uri uri
406 | Ast.ImplicitPattern -> mk_implicit false
407 | Ast.VarPattern name -> lookup name
408 | Ast.ApplPattern terms -> mk_appl (List.map aux terms)