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