]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationRew.ml
changed type of ids_to_uris table to (Cic.id, UriManager.uri) Hashtbl.t
[helm.git] / helm / ocaml / cic_notation / cicNotationRew.ml
1 (* Copyright (C) 2004-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 pattern_id = int
34 type interpretation_id = pattern_id
35 type pretty_printer_id = pattern_id
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 idref id t = Ast.AttributedTerm (`IdRef id, t)
68
69 let resolve_binder = function
70   | `Lambda -> "\\lambda"
71   | `Pi -> "\\Pi"
72   | `Forall -> "\\forall"
73   | `Exists -> "\\exists"
74
75 let add_level_info prec assoc t = Ast.AttributedTerm (`Level (prec, assoc), t)
76
77 let rec remove_level_info =
78   function
79   | Ast.AttributedTerm (`Level _, t) -> remove_level_info t
80   | Ast.AttributedTerm (a, t) -> Ast.AttributedTerm (a, remove_level_info t)
81   | t -> t
82
83 let add_xml_attrs attrs t =
84   if attrs = [] then t else Ast.AttributedTerm (`XmlAttrs attrs, t)
85
86 let add_keyword_attrs =
87   add_xml_attrs (RenderingAttrs.keyword_attributes `MathML)
88
89 let box kind spacing indent content =
90   Ast.Layout (Ast.Box ((kind, spacing, indent), content))
91
92 let hbox = box Ast.H
93 let vbox = box Ast.V
94 let hvbox = box Ast.HV
95 let hovbox = box Ast.HOV
96 let break = Ast.Layout Ast.Break
97 let builtin_symbol s = Ast.Literal (`Symbol s)
98 let keyword k = add_keyword_attrs (Ast.Literal (`Keyword k))
99
100 let number s =
101   add_xml_attrs (RenderingAttrs.number_attributes `MathML)
102     (Ast.Literal (`Number s))
103
104 let ident i =
105   add_xml_attrs (RenderingAttrs.ident_attributes `MathML) (Ast.Ident (i, None))
106
107 let ident_w_href href i =
108   match href with
109   | None -> ident i
110   | Some href ->
111       let href = UriManager.string_of_uri href in
112       add_xml_attrs [Some "xlink", "href", href] (ident i)
113
114 let binder_symbol s =
115   add_xml_attrs (RenderingAttrs.builtin_symbol_attributes `MathML)
116     (builtin_symbol s)
117
118 let string_of_sort_kind = function
119   | `Prop -> "Prop"
120   | `Set -> "Set"
121   | `CProp -> "CProp"
122   | `Type _ -> "Type"
123
124 let pp_ast0 t k =
125   let rec aux =
126     function
127     | Ast.Appl ts ->
128         add_level_info Ast.apply_prec Ast.apply_assoc
129           (hovbox true true (CicNotationUtil.dress break (List.map k ts)))
130     | Ast.Binder (binder_kind, (id, ty), body) ->
131         add_level_info Ast.binder_prec Ast.binder_assoc
132           (hvbox false true
133             [ binder_symbol (resolve_binder binder_kind);
134               k id; builtin_symbol ":"; aux_ty ty; break;
135               builtin_symbol "."; k body ])
136     | Ast.Case (what, indty_opt, outty_opt, patterns) ->
137         let outty_box =
138           match outty_opt with
139           | None -> []
140           | Some outty ->
141               [ builtin_symbol "["; remove_level_info (k outty);
142                 builtin_symbol "]"; break ]
143         in
144         let indty_box =
145           match indty_opt with
146           | None -> []
147           | Some (indty, href) -> [ keyword "in"; ident_w_href href indty ]
148         in
149         let match_box =
150           hvbox false true [
151             keyword "match"; break;
152             hvbox false false ([ k what ] @ indty_box); break;
153             keyword "with" ]
154         in
155         let mk_case_pattern (head, href, vars) =
156           hbox true false (ident_w_href href head :: List.map aux_var vars)
157         in
158         let patterns' =
159           List.map
160             (fun (lhs, rhs) ->
161               remove_level_info
162                 (hvbox false true [
163                   hbox false true [
164                     mk_case_pattern lhs; builtin_symbol "\\Rightarrow" ];
165                   break; k rhs ]))
166             patterns
167         in
168         let patterns'' =
169           let rec aux_patterns = function
170             | [] -> assert false
171             | [ last ] ->
172                 [ break; 
173                   hbox false false [
174                     builtin_symbol "|";
175                     last; builtin_symbol "]" ] ]
176             | hd :: tl ->
177                 [ break; hbox false false [ builtin_symbol "|"; hd ] ]
178                 @ aux_patterns tl
179           in
180           match patterns' with
181           | [] ->
182               [ hbox false false [ builtin_symbol "["; builtin_symbol "]" ] ]
183           | [ one ] ->
184               [ hbox false false [
185                 builtin_symbol "["; one; builtin_symbol "]" ] ]
186           | hd :: tl ->
187               hbox false false [ builtin_symbol "["; hd ]
188               :: aux_patterns tl
189         in
190         add_level_info Ast.simple_prec Ast.simple_assoc
191           (hvbox false false [
192             hvbox false false (outty_box @ [ match_box ]); break;
193             hbox false false [ hvbox false false patterns'' ] ])
194     | Ast.Cast (bo, ty) ->
195         add_level_info Ast.simple_prec Ast.simple_assoc
196           (hvbox false true [
197             builtin_symbol "("; k bo; break; builtin_symbol ":"; k ty;
198             builtin_symbol ")"])
199     | Ast.LetIn (var, s, t) ->
200         add_level_info Ast.let_in_prec Ast.let_in_assoc
201           (hvbox false true [
202             hvbox false true [
203               keyword "let";
204               hvbox false true [
205                 aux_var var; builtin_symbol "\\def"; break; k s ];
206               break; keyword "in" ];
207             break;
208             k t ])
209     | Ast.LetRec (rec_kind, funs, where) ->
210         let rec_op =
211           match rec_kind with `Inductive -> "rec" | `CoInductive -> "corec"
212         in
213         let mk_fun (var, body, _) = aux_var var, k body in
214         let mk_funs = List.map mk_fun in
215         let fst_fun, tl_funs =
216           match mk_funs funs with hd :: tl -> hd, tl | [] -> assert false
217         in
218         let fst_row =
219           let (name, body) = fst_fun in
220           hvbox false true [
221             keyword "let"; keyword rec_op; name; builtin_symbol "\\def"; break;
222             body ]
223         in
224         let tl_rows =
225           List.map
226             (fun (name, body) ->
227               [ break;
228                 hvbox false true [
229                   keyword "and"; name; builtin_symbol "\\def"; break; body ] ])
230             tl_funs
231         in
232         add_level_info Ast.let_in_prec Ast.let_in_assoc
233           ((hvbox false false
234             (fst_row :: List.flatten tl_rows
235              @ [ break; keyword "in"; break; k where ])))
236     | Ast.Implicit -> builtin_symbol "?"
237     | Ast.Meta (n, l) ->
238         let local_context l =
239           CicNotationUtil.dress (builtin_symbol ";")
240             (List.map (function None -> builtin_symbol "_" | Some t -> k t) l)
241         in
242         hbox false false
243           ([ builtin_symbol "?"; number (string_of_int n) ]
244             @ (if l <> [] then local_context l else []))
245     | Ast.Sort sort -> aux_sort sort
246     | Ast.Num _
247     | Ast.Symbol _
248     | Ast.Ident (_, None) | Ast.Ident (_, Some [])
249     | Ast.Uri (_, None) | Ast.Uri (_, Some [])
250     | Ast.Literal _
251     | Ast.UserInput as leaf -> leaf
252     | t -> CicNotationUtil.visit_ast ~special_k k t
253   and aux_sort sort_kind =
254     add_xml_attrs (RenderingAttrs.keyword_attributes `MathML)
255       (Ast.Ident (string_of_sort_kind sort_kind, None))
256   and aux_ty = function
257     | None -> builtin_symbol "?"
258     | Some ty -> k ty
259   and aux_var = function
260     | name, Some ty ->
261         hvbox false true [
262           builtin_symbol "("; name; builtin_symbol ":"; break; k ty;
263           builtin_symbol ")" ]
264     | name, None -> name
265   and special_k = function
266     | Ast.AttributedTerm (attrs, t) -> Ast.AttributedTerm (attrs, k t)
267     | t ->
268         prerr_endline ("unexpected special: " ^ CicNotationPp.pp_term t);
269         assert false
270   in
271   aux t
272
273 let ast_of_acic0 term_info acic k =
274   let k = k term_info in
275   let id_to_uris = term_info.uri in
276   let register_uri id uri = Hashtbl.add id_to_uris id uri in
277   let sort_of_id id =
278     try
279       Hashtbl.find term_info.sort id
280     with Not_found ->
281       prerr_endline (sprintf "warning: sort of id %s not found, using Type" id);
282       `Type (CicUniv.fresh ())
283   in
284   let aux_substs substs =
285     Some
286       (List.map
287         (fun (uri, annterm) -> (UriManager.name_of_uri uri, k annterm))
288         substs)
289   in
290   let aux_context context =
291     List.map
292       (function
293         | None -> None
294         | Some annterm -> Some (k annterm))
295       context
296   in
297   let aux = function
298     | Cic.ARel (id,_,_,b) -> idref id (Ast.Ident (b, None))
299     | Cic.AVar (id,uri,substs) ->
300         register_uri id uri;
301         idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
302     | Cic.AMeta (id,n,l) -> idref id (Ast.Meta (n, aux_context l))
303     | Cic.ASort (id,Cic.Prop) -> idref id (Ast.Sort `Prop)
304     | Cic.ASort (id,Cic.Set) -> idref id (Ast.Sort `Set)
305     | Cic.ASort (id,Cic.Type u) -> idref id (Ast.Sort (`Type u))
306     | Cic.ASort (id,Cic.CProp) -> idref id (Ast.Sort `CProp)
307     | Cic.AImplicit _ -> assert false
308     | Cic.AProd (id,n,s,t) ->
309         let binder_kind =
310           match sort_of_id id with
311           | `Set | `Type _ -> `Pi
312           | `Prop | `CProp -> `Forall
313         in
314         idref id (Ast.Binder (binder_kind,
315           (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
316     | Cic.ACast (id,v,t) -> idref id (Ast.Cast (k v, k t))
317     | Cic.ALambda (id,n,s,t) ->
318         idref id (Ast.Binder (`Lambda,
319           (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
320     | Cic.ALetIn (id,n,s,t) ->
321         idref id (Ast.LetIn ((CicNotationUtil.name_of_cic_name n, None),
322           k s, k t))
323     | Cic.AAppl (aid,args) -> idref aid (Ast.Appl (List.map k args))
324     | Cic.AConst (id,uri,substs) ->
325         register_uri id uri;
326         idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
327     | Cic.AMutInd (id,uri,i,substs) as t ->
328         let name = name_of_inductive_type uri i in
329         let uri_str = UriManager.string_of_uri uri in
330         let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (i+1) in
331         register_uri id (UriManager.uri_of_string puri_str);
332         idref id (Ast.Ident (name, aux_substs substs))
333     | Cic.AMutConstruct (id,uri,i,j,substs) ->
334         let name = constructor_of_inductive_type uri i j in
335         let uri_str = UriManager.string_of_uri uri in
336         let puri_str = sprintf "%s#xpointer(1/%d/%d)" uri_str (i + 1) j in
337         register_uri id (UriManager.uri_of_string puri_str);
338         idref id (Ast.Ident (name, aux_substs substs))
339     | Cic.AMutCase (id,uri,typeno,ty,te,patterns) ->
340         let name = name_of_inductive_type uri typeno in
341         let uri_str = UriManager.string_of_uri uri in
342         let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (typeno+1) in
343         let ctor_puri j =
344           UriManager.uri_of_string
345             (sprintf "%s#xpointer(1/%d/%d)" uri_str (typeno+1) j)
346         in
347         let case_indty = name, Some (UriManager.uri_of_string puri_str) in
348         let constructors = constructors_of_inductive_type uri typeno in
349         let rec eat_branch ty pat =
350           match (ty, pat) with
351           | Cic.Prod (_, _, t), Cic.ALambda (_, name, s, t') ->
352               let (cv, rhs) = eat_branch t t' in
353               (CicNotationUtil.name_of_cic_name name, Some (k s)) :: cv, rhs
354           | _, _ -> [], k pat
355         in
356         let j = ref 0 in
357         let patterns =
358           try
359             List.map2
360               (fun (name, ty) pat ->
361                 incr j;
362                 let (capture_variables, rhs) = eat_branch ty pat in
363                 ((name, Some (ctor_puri !j), capture_variables), rhs))
364               constructors patterns
365           with Invalid_argument _ -> assert false
366         in
367         idref id (Ast.Case (k te, Some case_indty, Some (k ty), patterns))
368     | Cic.AFix (id, no, funs) -> 
369         let defs = 
370           List.map
371             (fun (_, n, decr_idx, ty, bo) ->
372               ((Ast.Ident (n, None), Some (k ty)), k bo, decr_idx))
373             funs
374         in
375         let name =
376           try
377             (match List.nth defs no with
378             | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
379             | _ -> assert false)
380           with Not_found -> assert false
381         in
382         idref id (Ast.LetRec (`Inductive, defs, Ast.Ident (name, None)))
383     | Cic.ACoFix (id, no, funs) -> 
384         let defs = 
385           List.map
386             (fun (_, n, ty, bo) ->
387               ((Ast.Ident (n, None), Some (k ty)), k bo, 0))
388             funs
389         in
390         let name =
391           try
392             (match List.nth defs no with
393             | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
394             | _ -> assert false)
395           with Not_found -> assert false
396         in
397         idref id (Ast.LetRec (`CoInductive, defs, Ast.Ident (name, None)))
398   in
399   aux acic
400
401   (* persistent state *)
402
403 let level1_patterns21 = Hashtbl.create 211
404 let level2_patterns32 = Hashtbl.create 211
405 let interpretations = Hashtbl.create 211  (* symb -> id list ref *)
406
407 let compiled21 = ref None
408 let compiled32 = ref None
409
410 let pattern21_matrix = ref []
411 let pattern32_matrix = ref []
412
413 let get_compiled21 () =
414   match !compiled21 with
415   | None -> assert false
416   | Some f -> Lazy.force f
417 let get_compiled32 () =
418   match !compiled32 with
419   | None -> assert false
420   | Some f -> Lazy.force f
421
422 let set_compiled21 f = compiled21 := Some f
423 let set_compiled32 f = compiled32 := Some f
424
425 let add_idrefs =
426   List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
427
428 let instantiate21 idrefs env l1 =
429   let rec subst_singleton env =
430     function
431       Ast.AttributedTerm (attr, t) ->
432         Ast.AttributedTerm (attr, subst_singleton env t)
433     | t -> CicNotationUtil.group (subst env t)
434   and subst env = function
435     | Ast.AttributedTerm (attr, t) as term ->
436         subst env t
437     | Ast.Variable var ->
438         let name, expected_ty = CicNotationEnv.declaration_of_var var in
439         let ty, value =
440           try
441             List.assoc name env
442           with Not_found ->
443             prerr_endline ("name " ^ name ^ " not found in environment");
444             assert false
445         in
446         assert (CicNotationEnv.well_typed ty value); (* INVARIANT *)
447         (* following assertion should be a conditional that makes this
448          * instantiation fail *)
449         assert (CicNotationEnv.well_typed expected_ty value);
450         [ CicNotationEnv.term_of_value value ]
451     | Ast.Magic m -> subst_magic env m
452     | Ast.Literal l as t ->
453         let t = add_idrefs idrefs t in
454         (match l with
455         | `Keyword k -> [ add_keyword_attrs t ]
456         | _ -> [ t ])
457     | Ast.Layout l -> [ Ast.Layout (subst_layout env l) ]
458     | t -> [ CicNotationUtil.visit_ast (subst_singleton env) t ]
459   and subst_magic env = function
460     | Ast.List0 (p, sep_opt)
461     | Ast.List1 (p, sep_opt) ->
462         let rec_decls = CicNotationEnv.declarations_of_term p in
463         let rec_values =
464           List.map (fun (n, _) -> CicNotationEnv.lookup_list env n) rec_decls
465         in
466         let values = CicNotationUtil.ncombine rec_values in
467         let sep =
468           match sep_opt with
469             | None -> []
470             | Some l -> [ Ast.Literal l ]
471         in
472         let rec instantiate_list acc = function
473           | [] -> List.rev acc
474           | value_set :: [] ->
475               let env = CicNotationEnv.combine rec_decls value_set in
476               instantiate_list (CicNotationUtil.group (subst env p) :: acc) []
477           | value_set :: tl ->
478               let env = CicNotationEnv.combine rec_decls value_set in
479               let terms = subst env p in
480               instantiate_list (CicNotationUtil.group (terms @ sep) :: acc) tl
481         in
482         instantiate_list [] values
483     | Ast.Opt p ->
484         let opt_decls = CicNotationEnv.declarations_of_term p in
485         let env =
486           let rec build_env = function
487             | [] -> []
488             | (name, ty) :: tl ->
489                   (* assumption: if one of the value is None then all are *)
490                 (match CicNotationEnv.lookup_opt env name with
491                 | None -> raise Exit
492                 | Some v -> (name, (ty, v)) :: build_env tl)
493           in
494           try build_env opt_decls with Exit -> []
495         in
496           begin
497             match env with
498               | [] -> []
499               | _ -> subst env p
500           end
501     | _ -> assert false (* impossible *)
502   and subst_layout env = function
503     | Ast.Box (kind, tl) ->
504         Ast.Box (kind, List.concat (List.map (subst env) tl))
505     | l -> CicNotationUtil.visit_layout (subst_singleton env) l
506   in
507     subst_singleton env l1
508
509 let rec pp_ast1 term = 
510   let rec pp_value = function
511     | CicNotationEnv.NumValue _ as v -> v
512     | CicNotationEnv.StringValue _ as v -> v
513 (*     | CicNotationEnv.TermValue t when t == term -> CicNotationEnv.TermValue (pp_ast0 t pp_ast1) *)
514     | CicNotationEnv.TermValue t -> CicNotationEnv.TermValue (pp_ast1 t)
515     | CicNotationEnv.OptValue None as v -> v
516     | CicNotationEnv.OptValue (Some v) -> 
517         CicNotationEnv.OptValue (Some (pp_value v))
518     | CicNotationEnv.ListValue vl ->
519         CicNotationEnv.ListValue (List.map pp_value vl)
520   in
521   let ast_env_of_env env =
522     List.map (fun (var, (ty, value)) -> (var, (ty, pp_value value))) env
523   in
524 (* prerr_endline ("pattern matching from 2 to 1 on term " ^ CicNotationPp.pp_term term); *)
525   match term with
526   | Ast.AttributedTerm (attrs, term') ->
527       Ast.AttributedTerm (attrs, pp_ast1 term')
528   | _ ->
529       (match (get_compiled21 ()) term with
530       | None -> pp_ast0 term pp_ast1
531       | Some (env, ctors, pid) ->
532           let idrefs =
533             List.flatten (List.map CicNotationUtil.get_idrefs ctors)
534           in
535           let prec, assoc, l1 =
536             try
537               Hashtbl.find level1_patterns21 pid
538             with Not_found -> assert false
539           in
540           add_level_info prec assoc
541             (instantiate21 idrefs (ast_env_of_env env) l1))
542
543 let instantiate32 term_info idrefs env symbol args =
544   let rec instantiate_arg = function
545     | Ast.IdentArg (n, name) ->
546         let t = (try List.assoc name env with Not_found -> assert false) in
547         let rec count_lambda = function
548           | Ast.AttributedTerm (_, t) -> count_lambda t
549           | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
550           | _ -> 0
551         in
552         let rec add_lambda t n =
553           if n > 0 then
554             let name = CicNotationUtil.fresh_name () in
555             Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
556               Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
557           else
558             t
559         in
560         add_lambda t (n - count_lambda t)
561   in
562   let head =
563     let symbol = Ast.Symbol (symbol, 0) in
564     add_idrefs idrefs symbol
565   in
566   if args = [] then head
567   else Ast.Appl (head :: List.map instantiate_arg args)
568
569 let rec ast_of_acic1 term_info annterm = 
570   let id_to_uris = term_info.uri in
571   let register_uri id uri = Hashtbl.add id_to_uris id uri in
572   match (get_compiled32 ()) annterm with
573   | None -> ast_of_acic0 term_info annterm ast_of_acic1
574   | Some (env, ctors, pid) -> 
575       let idrefs =
576         List.map
577           (fun annterm ->
578             let idref = CicUtil.id_of_annterm annterm in
579             (try
580               register_uri idref
581                 (CicUtil.uri_of_term (Deannotate.deannotate_term annterm))
582             with Invalid_argument _ -> ());
583             idref)
584           ctors
585       in
586       let env' =
587         List.map (fun (name, term) -> (name, ast_of_acic1 term_info term)) env
588       in
589       let _, symbol, args, _ =
590         try
591           Hashtbl.find level2_patterns32 pid
592         with Not_found -> assert false
593       in
594       let ast = instantiate32 term_info idrefs env' symbol args in
595       Ast.AttributedTerm (`IdRef (CicUtil.id_of_annterm annterm), ast)
596
597 let load_patterns32 t =
598   set_compiled32 (lazy (CicNotationMatcher.Matcher32.compiler t))
599
600 let load_patterns21 t =
601   set_compiled21 (lazy (CicNotationMatcher.Matcher21.compiler t))
602
603 let ast_of_acic id_to_sort annterm =
604   debug_print (lazy ("ast_of_acic <- "
605     ^ CicPp.ppterm (Deannotate.deannotate_term annterm)));
606   let term_info = { sort = id_to_sort; uri = Hashtbl.create 211 } in
607   let ast = ast_of_acic1 term_info annterm in
608   debug_print (lazy ("ast_of_acic -> " ^ CicNotationPp.pp_term ast));
609   ast, term_info.uri
610
611 let pp_ast ast =
612   debug_print (lazy "pp_ast <-");
613   let ast' = pp_ast1 ast in
614   debug_print (lazy ("pp_ast -> " ^ CicNotationPp.pp_term ast'));
615   ast'
616
617 let fresh_id =
618   let counter = ref ~-1 in
619   fun () ->
620     incr counter;
621     !counter
622
623 let add_interpretation dsc (symbol, args) appl_pattern =
624   let id = fresh_id () in
625   Hashtbl.add level2_patterns32 id (dsc, symbol, args, appl_pattern);
626   pattern32_matrix := (appl_pattern, id) :: !pattern32_matrix;
627   load_patterns32 !pattern32_matrix;
628   (try
629     let ids = Hashtbl.find interpretations symbol in
630     ids := id :: !ids
631   with Not_found -> Hashtbl.add interpretations symbol (ref [id]));
632   id
633
634 exception Interpretation_not_found
635 exception Pretty_printer_not_found
636
637 let rec list_uniq = function 
638   | [] -> []
639   | h::[] -> [h]
640   | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl) 
641   | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
642
643 let lookup_interpretations symbol =
644   try
645    list_uniq
646     (List.sort Pervasives.compare
647      (List.map
648       (fun id ->
649         let (dsc, _, args, appl_pattern) =
650           try
651             Hashtbl.find level2_patterns32 id
652           with Not_found -> assert false 
653         in
654         dsc, args, appl_pattern)
655       !(Hashtbl.find interpretations symbol)))
656   with Not_found -> raise Interpretation_not_found
657
658 let add_pretty_printer ~precedence ~associativity l2 l1 =
659   let id = fresh_id () in
660   let l2' = CicNotationUtil.strip_attributes l2 in
661   Hashtbl.add level1_patterns21 id (precedence, associativity, l1);
662   pattern21_matrix := (l2', id) :: !pattern21_matrix;
663   load_patterns21 !pattern21_matrix;
664   id
665
666 let remove_interpretation id =
667   (try
668     let _, symbol, _, _ = Hashtbl.find level2_patterns32 id in
669     let ids = Hashtbl.find interpretations symbol in
670     ids := List.filter ((<>) id) !ids;
671     Hashtbl.remove level2_patterns32 id;
672   with Not_found -> raise Interpretation_not_found);
673   pattern32_matrix := List.filter (fun (_, id') -> id <> id') !pattern32_matrix;
674   load_patterns32 !pattern32_matrix
675
676 let remove_pretty_printer id =
677   (try
678     Hashtbl.remove level1_patterns21 id;
679   with Not_found -> raise Pretty_printer_not_found);
680   pattern21_matrix := List.filter (fun (_, id') -> id <> id') !pattern21_matrix;
681   load_patterns21 !pattern21_matrix
682
683 let _ =
684   load_patterns21 [];
685   load_patterns32 []
686