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/
30 module Ast = CicNotationPt
31 module Obj = LibraryObjects
34 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;
45 let destroy_nat annterm =
46 let is_zero = function
47 | Cic.AMutConstruct (_, uri, 0, 1, _) when Obj.is_nat_URI uri -> true
50 let is_succ = function
51 | Cic.AMutConstruct (_, uri, 0, 2, _) when Obj.is_nat_URI uri -> true
54 let rec aux acc = function
55 | Cic.AAppl (_, [he ; tl]) when is_succ he -> aux (acc + 1) tl
56 | t when is_zero t -> Some acc
60 (* persistent state *)
62 let initial_level2_patterns32 () = Hashtbl.create 211
63 let initial_interpretations () = Hashtbl.create 211
65 let level2_patterns32 = ref (initial_level2_patterns32 ())
66 (* symb -> id list ref *)
67 let interpretations = ref (initial_interpretations ())
68 let pattern32_matrix = ref []
70 let find_level2_patterns32 pid = Hashtbl.find !level2_patterns32 pid;;
75 stack := (!counter,!level2_patterns32,!interpretations,!pattern32_matrix)::!stack;
77 level2_patterns32 := initial_level2_patterns32 ();
78 interpretations := initial_interpretations ();
79 pattern32_matrix := []
85 | (ocounter,olevel2_patterns32,ointerpretations,opattern32_matrix)::old ->
88 level2_patterns32 := olevel2_patterns32;
89 interpretations := ointerpretations;
90 pattern32_matrix := opattern32_matrix
94 List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
96 let instantiate32 term_info idrefs env symbol args =
97 let rec instantiate_arg = function
98 | Ast.IdentArg (n, name) ->
100 try List.assoc name env
101 with Not_found -> prerr_endline ("name not found in env: "^name);
104 let rec count_lambda = function
105 | Ast.AttributedTerm (_, t) -> count_lambda t
106 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
109 let rec add_lambda t n =
111 let name = CicNotationUtil.fresh_name () in
112 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
113 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
117 add_lambda t (n - count_lambda t)
120 let symbol = Ast.Symbol (symbol, 0) in
121 add_idrefs idrefs symbol
123 if args = [] then head
124 else Ast.Appl (head :: List.map instantiate_arg args)
126 let load_patterns32s = ref [];;
128 let add_load_patterns32 f = load_patterns32s := f :: !load_patterns32s;;
134 let add_interpretation dsc (symbol, args) appl_pattern =
135 let id = fresh_id () in
136 Hashtbl.add !level2_patterns32 id (dsc, symbol, args, appl_pattern);
137 pattern32_matrix := (true, appl_pattern, id) :: !pattern32_matrix;
138 List.iter (fun f -> f !pattern32_matrix) !load_patterns32s;
140 let ids = Hashtbl.find !interpretations symbol in
142 with Not_found -> Hashtbl.add !interpretations symbol (ref [id]));
145 let get_all_interpretations () =
147 (function (_, _, id) ->
150 Hashtbl.find !level2_patterns32 id
151 with Not_found -> assert false
156 let get_active_interpretations () =
157 HExtlib.filter_map (function (true, _, id) -> Some id | _ -> None)
160 let set_active_interpretations ids =
161 let pattern32_matrix' =
164 | (_, ap, id) when List.mem id ids -> (true, ap, id)
165 | (_, ap, id) -> (false, ap, id))
168 pattern32_matrix := pattern32_matrix';
169 List.iter (fun f -> f !pattern32_matrix) !load_patterns32s
171 exception Interpretation_not_found
173 let lookup_interpretations ?(sorted=true) symbol =
178 let (dsc, _, args, appl_pattern) =
180 Hashtbl.find !level2_patterns32 id
181 with Not_found -> assert false
183 dsc, args, appl_pattern
185 !(Hashtbl.find !interpretations symbol)
187 if sorted then HExtlib.list_uniq (List.sort Pervasives.compare raw)
189 with Not_found -> raise Interpretation_not_found
191 let remove_interpretation id =
193 let dsc, symbol, _, _ = Hashtbl.find !level2_patterns32 id in
194 let ids = Hashtbl.find !interpretations symbol in
195 ids := List.filter ((<>) id) !ids;
196 Hashtbl.remove !level2_patterns32 id;
197 with Not_found -> raise Interpretation_not_found);
199 List.filter (fun (_, _, id') -> id <> id') !pattern32_matrix;
200 List.iter (fun f -> f !pattern32_matrix) !load_patterns32s
202 let init () = List.iter (fun f -> f []) !load_patterns32s
204 let instantiate_appl_pattern
205 ~mk_appl ~mk_implicit ~term_of_uri ~term_of_nref env appl_pattern
208 try List.assoc name env
210 prerr_endline (sprintf "Name %s not found" name);
213 let rec aux = function
214 | Ast.UriPattern uri -> term_of_uri uri
215 | Ast.NRefPattern nref -> term_of_nref nref
216 | Ast.ImplicitPattern -> mk_implicit false
217 | Ast.VarPattern name -> lookup name
218 | Ast.ApplPattern terms -> mk_appl (List.map aux terms)