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