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 = NotationPt
33 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
35 let load_patterns32 = ref (fun _ -> assert false);;
36 let set_load_patterns32 f = load_patterns32 := f;;
38 type interpretation_id = int
40 let idref id t = Ast.AttributedTerm (`IdRef id, t)
45 { sort: (cic_id, Ast.sort_kind) Hashtbl.t;
46 uri: (cic_id, NReference.reference) Hashtbl.t;
49 module IntMap = Map.Make(struct type t = int let compare = compare end);;
50 module StringMap = Map.Make(String);;
54 pattern32_matrix: (bool * NotationPt.cic_appl_pattern * interpretation_id) list;
56 (string * string * NotationPt.argument_pattern list *
57 NotationPt.cic_appl_pattern) IntMap.t;
58 interpretations: interpretation_id list StringMap.t; (* symb -> id list *)
63 pattern32_matrix = [];
64 level2_patterns32 = IntMap.empty;
65 interpretations = StringMap.empty
75 val interp_db = initial_db
76 method interp_db = interp_db
77 method set_interp_db v = {< interp_db = v >}
78 method set_interp_status
79 : 'status. #g_status as 'status -> 'self
80 = fun o -> {< interp_db = o#interp_db >}
83 let find_level2_patterns32 status pid =
84 IntMap.find pid status#interp_db.level2_patterns32
87 List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
89 let instantiate32 term_info idrefs env symbol args =
90 let rec instantiate_arg = function
91 | Ast.IdentArg (n, name) ->
93 try List.assoc name env
94 with Not_found -> prerr_endline ("name not found in env: "^name);
97 let rec count_lambda = function
98 | Ast.AttributedTerm (_, t) -> count_lambda t
99 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
102 let rec add_lambda t n =
104 let name = NotationUtil.fresh_name () in
105 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
106 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
110 add_lambda t (n - count_lambda t)
113 let symbol = Ast.Symbol (symbol, 0) in
114 add_idrefs idrefs symbol
116 if args = [] then head
117 else Ast.Appl (head :: List.map instantiate_arg args)
119 let fresh_id status =
120 let counter = status#interp_db.counter+1 in
121 status#set_interp_db ({ status#interp_db with counter = counter }), counter
123 let add_interpretation (status : #status) dsc (symbol, args) appl_pattern =
124 let status,id = fresh_id status in
127 StringMap.find symbol status#interp_db.interpretations
128 with Not_found -> [id] in
130 status#set_interp_db { status#interp_db with
132 IntMap.add id (dsc, symbol, args, appl_pattern)
133 status#interp_db.level2_patterns32;
134 pattern32_matrix = (true,appl_pattern,id)::status#interp_db.pattern32_matrix;
135 interpretations = StringMap.add symbol ids status#interp_db.interpretations
138 !load_patterns32 status#interp_db.pattern32_matrix;
141 let toggle_active_interpretations status b =
142 status#set_interp_db { status#interp_db with
144 List.map (fun (_,ap,id) -> b,ap,id) status#interp_db.pattern32_matrix }
146 exception Interpretation_not_found
148 let lookup_interpretations status ?(sorted=true) symbol =
153 let (dsc, _, args, appl_pattern) =
154 try IntMap.find id status#interp_db.level2_patterns32
155 with Not_found -> assert false
157 dsc, args, appl_pattern
158 ) (StringMap.find symbol status#interp_db.interpretations)
160 if sorted then HExtlib.list_uniq (List.sort Pervasives.compare raw)
162 with Not_found -> raise Interpretation_not_found
164 let instantiate_appl_pattern
165 ~mk_appl ~mk_implicit ~term_of_nref env appl_pattern
168 try List.assoc name env
170 prerr_endline (sprintf "Name %s not found" name);
173 let rec aux = function
174 | Ast.NRefPattern nref -> term_of_nref nref
175 | Ast.ImplicitPattern -> mk_implicit false
176 | Ast.VarPattern name -> lookup name
177 | Ast.ApplPattern terms -> mk_appl (List.map aux terms)