]> matita.cs.unibo.it Git - helm.git/blob - matita/components/content/interpretations.ml
urimanager removed
[helm.git] / matita / components / content / interpretations.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 module Ast = NotationPt
31
32 let debug = false
33 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
34
35 type interpretation_id = int
36
37 let idref id t = Ast.AttributedTerm (`IdRef id, t)
38
39 type cic_id = string
40
41 type term_info =
42   { sort: (cic_id, Ast.sort_kind) Hashtbl.t;
43     uri: (cic_id, NReference.reference) Hashtbl.t;
44   }
45
46   (* persistent state *)
47
48 let initial_level2_patterns32 () = Hashtbl.create 211
49 let initial_interpretations () = Hashtbl.create 211
50
51 let level2_patterns32 = ref (initial_level2_patterns32 ())
52 (* symb -> id list ref *)
53 let interpretations = ref (initial_interpretations ())
54 let pattern32_matrix = ref []
55 let counter = ref ~-1 
56 let find_level2_patterns32 pid = Hashtbl.find !level2_patterns32 pid;;
57
58 let stack = ref []
59
60 let push () =
61  stack := (!counter,!level2_patterns32,!interpretations,!pattern32_matrix)::!stack;
62  counter := ~-1;
63  level2_patterns32 := initial_level2_patterns32 ();
64  interpretations := initial_interpretations ();
65  pattern32_matrix := []
66 ;;
67
68 let pop () =
69  match !stack with
70     [] -> assert false
71   | (ocounter,olevel2_patterns32,ointerpretations,opattern32_matrix)::old ->
72    stack := old;
73    counter := ocounter;
74    level2_patterns32 := olevel2_patterns32;
75    interpretations := ointerpretations;
76    pattern32_matrix := opattern32_matrix
77 ;;
78
79 let add_idrefs =
80   List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
81
82 let instantiate32 term_info idrefs env symbol args =
83   let rec instantiate_arg = function
84     | Ast.IdentArg (n, name) ->
85         let t = 
86           try List.assoc name env 
87           with Not_found -> prerr_endline ("name not found in env: "^name);
88                             assert false
89         in
90         let rec count_lambda = function
91           | Ast.AttributedTerm (_, t) -> count_lambda t
92           | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
93           | _ -> 0
94         in
95         let rec add_lambda t n =
96           if n > 0 then
97             let name = NotationUtil.fresh_name () in
98             Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
99               Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
100           else
101             t
102         in
103         add_lambda t (n - count_lambda t)
104   in
105   let head =
106     let symbol = Ast.Symbol (symbol, 0) in
107     add_idrefs idrefs symbol
108   in
109   if args = [] then head
110   else Ast.Appl (head :: List.map instantiate_arg args)
111
112 let load_patterns32s = ref [];;
113
114 let add_load_patterns32 f = load_patterns32s := f :: !load_patterns32s;;
115 let fresh_id =
116   fun () ->
117     incr counter;
118     !counter
119
120 let add_interpretation dsc (symbol, args) appl_pattern =
121   let id = fresh_id () in
122   Hashtbl.add !level2_patterns32 id (dsc, symbol, args, appl_pattern);
123   pattern32_matrix := (true, appl_pattern, id) :: !pattern32_matrix;
124   List.iter (fun f -> f !pattern32_matrix) !load_patterns32s;
125   (try
126     let ids = Hashtbl.find !interpretations symbol in
127     ids := id :: !ids
128   with Not_found -> Hashtbl.add !interpretations symbol (ref [id]));
129   id
130
131 let get_all_interpretations () =
132   List.map
133     (function (_, _, id) ->
134       let (dsc, _, _, _) =
135         try
136           Hashtbl.find !level2_patterns32 id
137         with Not_found -> assert false
138       in
139       (id, dsc))
140     !pattern32_matrix
141
142 let get_active_interpretations () =
143   HExtlib.filter_map (function (true, _, id) -> Some id | _ -> None)
144     !pattern32_matrix
145
146 let set_active_interpretations ids =
147   let pattern32_matrix' =
148     List.map
149       (function 
150         | (_, ap, id) when List.mem id ids -> (true, ap, id)
151         | (_, ap, id) -> (false, ap, id))
152       !pattern32_matrix
153   in
154   pattern32_matrix := pattern32_matrix';
155   List.iter (fun f -> f !pattern32_matrix) !load_patterns32s
156
157 exception Interpretation_not_found
158
159 let lookup_interpretations ?(sorted=true) symbol =
160   try
161     let raw = 
162       List.map (
163         fun id ->
164           let (dsc, _, args, appl_pattern) =
165             try
166               Hashtbl.find !level2_patterns32 id
167             with Not_found -> assert false 
168           in
169           dsc, args, appl_pattern
170       )
171       !(Hashtbl.find !interpretations symbol)
172     in
173     if sorted then HExtlib.list_uniq (List.sort Pervasives.compare raw)
174               else raw
175   with Not_found -> raise Interpretation_not_found
176
177 let remove_interpretation id =
178   (try
179     let dsc, symbol, _, _ = Hashtbl.find !level2_patterns32 id in
180     let ids = Hashtbl.find !interpretations symbol in
181     ids := List.filter ((<>) id) !ids;
182     Hashtbl.remove !level2_patterns32 id;
183   with Not_found -> raise Interpretation_not_found);
184   pattern32_matrix :=
185     List.filter (fun (_, _, id') -> id <> id') !pattern32_matrix;
186   List.iter (fun f -> f !pattern32_matrix) !load_patterns32s
187
188 let init () = List.iter (fun f -> f []) !load_patterns32s
189
190 let instantiate_appl_pattern 
191   ~mk_appl ~mk_implicit ~term_of_nref env appl_pattern 
192 =
193   let lookup name =
194     try List.assoc name env
195     with Not_found ->
196       prerr_endline (sprintf "Name %s not found" name);
197       assert false
198   in
199   let rec aux = function
200     | Ast.NRefPattern nref -> term_of_nref nref
201     | Ast.ImplicitPattern -> mk_implicit false
202     | Ast.VarPattern name -> lookup name
203     | Ast.ApplPattern terms -> mk_appl (List.map aux terms)
204   in
205   aux appl_pattern
206