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 let idref id t = Ast.AttributedTerm (`IdRef id, t)
43 { sort: (cic_id, Ast.sort_kind) Hashtbl.t;
44 uri: (cic_id, NReference.reference) Hashtbl.t;
47 module IntMap = Map.Make(struct type t = int let compare = compare end);;
48 module StringMap = Map.Make(String);;
52 pattern32_matrix: (bool * NotationPt.cic_appl_pattern * int) list;
54 (string * string * NotationPt.argument_pattern list *
55 NotationPt.cic_appl_pattern) IntMap.t;
56 interpretations: int list StringMap.t; (* symb -> id list *)
61 pattern32_matrix = [];
62 level2_patterns32 = IntMap.empty;
63 interpretations = StringMap.empty
73 val interp_db = initial_db
74 method interp_db = interp_db
75 method set_interp_db v = {< interp_db = v >}
76 method set_interp_status
77 : 'status. #g_status as 'status -> 'self
78 = fun o -> {< interp_db = o#interp_db >}
81 let find_level2_patterns32 status pid =
82 IntMap.find pid status#interp_db.level2_patterns32
85 List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
87 let instantiate32 term_info idrefs env symbol args =
88 let rec instantiate_arg = function
89 | Ast.IdentArg (n, name) ->
91 try List.assoc name env
92 with Not_found -> prerr_endline ("name not found in env: "^name);
95 let rec count_lambda = function
96 | Ast.AttributedTerm (_, t) -> count_lambda t
97 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
100 let rec add_lambda t n =
102 let name = NotationUtil.fresh_name () in
103 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
104 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
108 add_lambda t (n - count_lambda t)
111 let symbol = Ast.Symbol (symbol, 0) in
112 add_idrefs idrefs symbol
114 if args = [] then head
115 else Ast.Appl (head :: List.map instantiate_arg args)
117 let fresh_id status =
118 let counter = status#interp_db.counter+1 in
119 status#set_interp_db ({ status#interp_db with counter = counter }), counter
121 let add_interpretation (status : #status) dsc (symbol, args) appl_pattern =
122 let status,id = fresh_id status in
125 StringMap.find symbol status#interp_db.interpretations
126 with Not_found -> [id] in
128 status#set_interp_db { status#interp_db with
130 IntMap.add id (dsc, symbol, args, appl_pattern)
131 status#interp_db.level2_patterns32;
132 pattern32_matrix = (true,appl_pattern,id)::status#interp_db.pattern32_matrix;
133 interpretations = StringMap.add symbol ids status#interp_db.interpretations
136 !load_patterns32 status#interp_db.pattern32_matrix;
139 let toggle_active_interpretations status b =
140 status#set_interp_db { status#interp_db with
142 List.map (fun (_,ap,id) -> b,ap,id) status#interp_db.pattern32_matrix }
144 exception Interpretation_not_found
146 let lookup_interpretations status ?(sorted=true) symbol =
151 let (dsc, _, args, appl_pattern) =
152 try IntMap.find id status#interp_db.level2_patterns32
153 with Not_found -> assert false
155 dsc, args, appl_pattern
156 ) (StringMap.find symbol status#interp_db.interpretations)
158 if sorted then HExtlib.list_uniq (List.sort Pervasives.compare raw)
160 with Not_found -> raise Interpretation_not_found
162 let instantiate_appl_pattern
163 ~mk_appl ~mk_implicit ~term_of_nref env appl_pattern
166 try List.assoc name env
168 prerr_endline (sprintf "Name %s not found" name);
171 let rec aux = function
172 | Ast.NRefPattern nref -> term_of_nref nref
173 | Ast.ImplicitPattern -> mk_implicit false
174 | Ast.VarPattern name -> lookup name
175 | Ast.ApplPattern terms -> mk_appl (List.map aux terms)