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