]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/acic_content/termAcicContent.ml
Reshaped structure of ocaml/ libraries.
[helm.git] / helm / ocaml / acic_content / termAcicContent.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 open Printf
27
28 module Ast = CicNotationPt
29
30 let debug = false
31 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
32
33 type interpretation_id = int
34
35 let idref id t = Ast.AttributedTerm (`IdRef id, t)
36
37 type term_info =
38   { sort: (Cic.id, Ast.sort_kind) Hashtbl.t;
39     uri: (Cic.id, UriManager.uri) Hashtbl.t;
40   }
41
42 let get_types uri =
43   let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
44     match o with
45       | Cic.InductiveDefinition (l,_,_,_) -> l 
46       | _ -> assert false
47
48 let name_of_inductive_type uri i = 
49   let types = get_types uri in
50   let (name, _, _, _) = try List.nth types i with Not_found -> assert false in
51   name
52
53   (* returns <name, type> pairs *)
54 let constructors_of_inductive_type uri i =
55   let types = get_types uri in
56   let (_, _, _, constructors) = 
57     try List.nth types i with Not_found -> assert false
58   in
59   constructors
60
61   (* returns name only *)
62 let constructor_of_inductive_type uri i j =
63   (try
64     fst (List.nth (constructors_of_inductive_type uri i) (j-1))
65   with Not_found -> assert false)
66
67 let ast_of_acic0 term_info acic k =
68   let k = k term_info in
69   let id_to_uris = term_info.uri in
70   let register_uri id uri = Hashtbl.add id_to_uris id uri in
71   let sort_of_id id =
72     try
73       Hashtbl.find term_info.sort id
74     with Not_found ->
75       prerr_endline (sprintf "warning: sort of id %s not found, using Type" id);
76       `Type (CicUniv.fresh ())
77   in
78   let aux_substs substs =
79     Some
80       (List.map
81         (fun (uri, annterm) -> (UriManager.name_of_uri uri, k annterm))
82         substs)
83   in
84   let aux_context context =
85     List.map
86       (function
87         | None -> None
88         | Some annterm -> Some (k annterm))
89       context
90   in
91   let aux = function
92     | Cic.ARel (id,_,_,b) -> idref id (Ast.Ident (b, None))
93     | Cic.AVar (id,uri,substs) ->
94         register_uri id uri;
95         idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
96     | Cic.AMeta (id,n,l) -> idref id (Ast.Meta (n, aux_context l))
97     | Cic.ASort (id,Cic.Prop) -> idref id (Ast.Sort `Prop)
98     | Cic.ASort (id,Cic.Set) -> idref id (Ast.Sort `Set)
99     | Cic.ASort (id,Cic.Type u) -> idref id (Ast.Sort (`Type u))
100     | Cic.ASort (id,Cic.CProp) -> idref id (Ast.Sort `CProp)
101     | Cic.AImplicit (id, Some `Hole) -> idref id Ast.UserInput
102     | Cic.AImplicit (id, _) -> idref id Ast.Implicit
103     | Cic.AProd (id,n,s,t) ->
104         let binder_kind =
105           match sort_of_id id with
106           | `Set | `Type _ -> `Pi
107           | `Prop | `CProp -> `Forall
108         in
109         idref id (Ast.Binder (binder_kind,
110           (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
111     | Cic.ACast (id,v,t) -> idref id (Ast.Cast (k v, k t))
112     | Cic.ALambda (id,n,s,t) ->
113         idref id (Ast.Binder (`Lambda,
114           (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
115     | Cic.ALetIn (id,n,s,t) ->
116         idref id (Ast.LetIn ((CicNotationUtil.name_of_cic_name n, None),
117           k s, k t))
118     | Cic.AAppl (aid,args) -> idref aid (Ast.Appl (List.map k args))
119     | Cic.AConst (id,uri,substs) ->
120         register_uri id uri;
121         idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
122     | Cic.AMutInd (id,uri,i,substs) as t ->
123         let name = name_of_inductive_type uri i in
124         let uri_str = UriManager.string_of_uri uri in
125         let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (i+1) in
126         register_uri id (UriManager.uri_of_string puri_str);
127         idref id (Ast.Ident (name, aux_substs substs))
128     | Cic.AMutConstruct (id,uri,i,j,substs) ->
129         let name = constructor_of_inductive_type uri i j in
130         let uri_str = UriManager.string_of_uri uri in
131         let puri_str = sprintf "%s#xpointer(1/%d/%d)" uri_str (i + 1) j in
132         register_uri id (UriManager.uri_of_string puri_str);
133         idref id (Ast.Ident (name, aux_substs substs))
134     | Cic.AMutCase (id,uri,typeno,ty,te,patterns) ->
135         let name = name_of_inductive_type uri typeno in
136         let uri_str = UriManager.string_of_uri uri in
137         let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (typeno+1) in
138         let ctor_puri j =
139           UriManager.uri_of_string
140             (sprintf "%s#xpointer(1/%d/%d)" uri_str (typeno+1) j)
141         in
142         let case_indty = name, Some (UriManager.uri_of_string puri_str) in
143         let constructors = constructors_of_inductive_type uri typeno in
144         let rec eat_branch ty pat =
145           match (ty, pat) with
146           | Cic.Prod (_, _, t), Cic.ALambda (_, name, s, t') ->
147               let (cv, rhs) = eat_branch t t' in
148               (CicNotationUtil.name_of_cic_name name, Some (k s)) :: cv, rhs
149           | _, _ -> [], k pat
150         in
151         let j = ref 0 in
152         let patterns =
153           try
154             List.map2
155               (fun (name, ty) pat ->
156                 incr j;
157                 let (capture_variables, rhs) = eat_branch ty pat in
158                 ((name, Some (ctor_puri !j), capture_variables), rhs))
159               constructors patterns
160           with Invalid_argument _ -> assert false
161         in
162         idref id (Ast.Case (k te, Some case_indty, Some (k ty), patterns))
163     | Cic.AFix (id, no, funs) -> 
164         let defs = 
165           List.map
166             (fun (_, n, decr_idx, ty, bo) ->
167               ((Ast.Ident (n, None), Some (k ty)), k bo, decr_idx))
168             funs
169         in
170         let name =
171           try
172             (match List.nth defs no with
173             | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
174             | _ -> assert false)
175           with Not_found -> assert false
176         in
177         idref id (Ast.LetRec (`Inductive, defs, Ast.Ident (name, None)))
178     | Cic.ACoFix (id, no, funs) -> 
179         let defs = 
180           List.map
181             (fun (_, n, ty, bo) ->
182               ((Ast.Ident (n, None), Some (k ty)), k bo, 0))
183             funs
184         in
185         let name =
186           try
187             (match List.nth defs no with
188             | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
189             | _ -> assert false)
190           with Not_found -> assert false
191         in
192         idref id (Ast.LetRec (`CoInductive, defs, Ast.Ident (name, None)))
193   in
194   aux acic
195
196   (* persistent state *)
197
198 let level2_patterns32 = Hashtbl.create 211
199 let interpretations = Hashtbl.create 211  (* symb -> id list ref *)
200
201 let compiled32 = ref None
202 let pattern32_matrix = ref []
203
204 let get_compiled32 () =
205   match !compiled32 with
206   | None -> assert false
207   | Some f -> Lazy.force f
208
209 let set_compiled32 f = compiled32 := Some f
210
211 let add_idrefs =
212   List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
213
214 let instantiate32 term_info idrefs env symbol args =
215   let rec instantiate_arg = function
216     | Ast.IdentArg (n, name) ->
217         let t = (try List.assoc name env with Not_found -> assert false) in
218         let rec count_lambda = function
219           | Ast.AttributedTerm (_, t) -> count_lambda t
220           | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
221           | _ -> 0
222         in
223         let rec add_lambda t n =
224           if n > 0 then
225             let name = CicNotationUtil.fresh_name () in
226             Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
227               Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
228           else
229             t
230         in
231         add_lambda t (n - count_lambda t)
232   in
233   let head =
234     let symbol = Ast.Symbol (symbol, 0) in
235     add_idrefs idrefs symbol
236   in
237   if args = [] then head
238   else Ast.Appl (head :: List.map instantiate_arg args)
239
240 let rec ast_of_acic1 term_info annterm = 
241   let id_to_uris = term_info.uri in
242   let register_uri id uri = Hashtbl.add id_to_uris id uri in
243   match (get_compiled32 ()) annterm with
244   | None -> ast_of_acic0 term_info annterm ast_of_acic1
245   | Some (env, ctors, pid) -> 
246       let idrefs =
247         List.map
248           (fun annterm ->
249             let idref = CicUtil.id_of_annterm annterm in
250             (try
251               register_uri idref
252                 (CicUtil.uri_of_term (Deannotate.deannotate_term annterm))
253             with Invalid_argument _ -> ());
254             idref)
255           ctors
256       in
257       let env' =
258         List.map (fun (name, term) -> (name, ast_of_acic1 term_info term)) env
259       in
260       let _, symbol, args, _ =
261         try
262           Hashtbl.find level2_patterns32 pid
263         with Not_found -> assert false
264       in
265       let ast = instantiate32 term_info idrefs env' symbol args in
266       Ast.AttributedTerm (`IdRef (CicUtil.id_of_annterm annterm), ast)
267
268 let load_patterns32 t =
269   let t =
270     HExtlib.filter_map (function (true, ap, id) -> Some (ap, id) | _ -> None) t
271   in
272   set_compiled32 (lazy (Acic2astMatcher.Matcher32.compiler t))
273
274 let ast_of_acic id_to_sort annterm =
275   debug_print (lazy ("ast_of_acic <- "
276     ^ CicPp.ppterm (Deannotate.deannotate_term annterm)));
277   let term_info = { sort = id_to_sort; uri = Hashtbl.create 211 } in
278   let ast = ast_of_acic1 term_info annterm in
279   debug_print (lazy ("ast_of_acic -> " ^ CicNotationPp.pp_term ast));
280   ast, term_info.uri
281
282 let fresh_id =
283   let counter = ref ~-1 in
284   fun () ->
285     incr counter;
286     !counter
287
288 let add_interpretation dsc (symbol, args) appl_pattern =
289   let id = fresh_id () in
290   Hashtbl.add level2_patterns32 id (dsc, symbol, args, appl_pattern);
291   pattern32_matrix := (true, appl_pattern, id) :: !pattern32_matrix;
292   load_patterns32 !pattern32_matrix;
293   (try
294     let ids = Hashtbl.find interpretations symbol in
295     ids := id :: !ids
296   with Not_found -> Hashtbl.add interpretations symbol (ref [id]));
297   id
298
299 let get_all_interpretations () =
300   List.map
301     (function (_, _, id) ->
302       let (dsc, _, _, _) =
303         try
304           Hashtbl.find level2_patterns32 id
305         with Not_found -> assert false
306       in
307       (id, dsc))
308     !pattern32_matrix
309
310 let get_active_interpretations () =
311   HExtlib.filter_map (function (true, _, id) -> Some id | _ -> None)
312     !pattern32_matrix
313
314 let set_active_interpretations ids =
315   let pattern32_matrix' =
316     List.map
317       (function 
318         | (_, ap, id) when List.mem id ids -> (true, ap, id)
319         | (_, ap, id) -> (false, ap, id))
320       !pattern32_matrix
321   in
322   pattern32_matrix := pattern32_matrix';
323   load_patterns32 !pattern32_matrix
324
325 exception Interpretation_not_found
326
327 let lookup_interpretations symbol =
328   try
329    HExtlib.list_uniq
330     (List.sort Pervasives.compare
331      (List.map
332       (fun id ->
333         let (dsc, _, args, appl_pattern) =
334           try
335             Hashtbl.find level2_patterns32 id
336           with Not_found -> assert false 
337         in
338         dsc, args, appl_pattern)
339       !(Hashtbl.find interpretations symbol)))
340   with Not_found -> raise Interpretation_not_found
341
342 let remove_interpretation id =
343   (try
344     let _, symbol, _, _ = Hashtbl.find level2_patterns32 id in
345     let ids = Hashtbl.find interpretations symbol in
346     ids := List.filter ((<>) id) !ids;
347     Hashtbl.remove level2_patterns32 id;
348   with Not_found -> raise Interpretation_not_found);
349   pattern32_matrix :=
350     List.filter (fun (_, _, id') -> id <> id') !pattern32_matrix;
351   load_patterns32 !pattern32_matrix
352
353 let _ = load_patterns32 []
354
355 let instantiate_appl_pattern env appl_pattern =
356   let lookup name =
357     try List.assoc name env
358     with Not_found ->
359       prerr_endline (sprintf "Name %s not found" name);
360       assert false
361   in
362   let rec aux = function
363     | Ast.UriPattern uri -> CicUtil.term_of_uri uri
364     | Ast.ImplicitPattern -> Cic.Implicit None
365     | Ast.VarPattern name -> lookup name
366     | Ast.ApplPattern terms -> Cic.Appl (List.map aux terms)
367   in
368   aux appl_pattern
369