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