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