1 (* Copyright (C) 2004-2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 type interpretation_id = pattern_id
30 type pretty_printer_id = pattern_id
33 let default_assoc = Gramext.NonA
36 { sort: (Cic.id, CicNotationPt.sort_kind) Hashtbl.t;
37 uri: (Cic.id, string) Hashtbl.t;
41 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
43 | Cic.InductiveDefinition (l,_,_,_) -> l
46 let name_of_inductive_type uri i =
47 let types = get_types uri in
48 let (name, _, _, _) = try List.nth types i with Not_found -> assert false in
51 (* returns <name, type> pairs *)
52 let constructors_of_inductive_type uri i =
53 let types = get_types uri in
54 let (_, _, _, constructors) =
55 try List.nth types i with Not_found -> assert false
59 (* returns name only *)
60 let constructor_of_inductive_type uri i j =
62 fst (List.nth (constructors_of_inductive_type uri i) (j-1))
63 with Not_found -> assert false)
65 module Ast = CicNotationPt
66 module Parser = CicNotationParser
68 let string_of_name = function
70 | Cic.Anonymous -> "_"
72 let ident_of_name n = Ast.Ident (string_of_name n, None)
74 let idref id t = Ast.AttributedTerm (`IdRef id, t)
76 let resolve_binder = function
77 | `Lambda -> "\\lambda"
79 | `Forall -> "\\forall"
80 | `Exists -> "\\exists"
82 let binder_attributes = [None, "mathcolor", "blue"]
83 let atop_attributes = [None, "linethickness", "0pt"]
84 let indent_attributes = [None, "indent", "1em"]
85 let keyword_attributes = [None, "mathcolor", "blue"]
88 let reset_href t = Ast.AttributedTerm (`Href [], t) in
89 let builtin_symbol s = reset_href (Ast.Literal (`Symbol s)) in
91 Ast.AttributedTerm (`XmlAttrs binder_attributes, builtin_symbol s)
93 let rec aux = function
95 Ast.AttributedTerm (`Level (Parser.apply_prec, Parser.apply_assoc),
97 (Ast.Box ((Ast.HOV, true, true),
98 (CicNotationUtil.dress
99 (Ast.Layout Ast.Break)
101 | Ast.Binder (`Forall, (Ast.Ident ("_", _), ty), body)
102 | Ast.Binder (`Pi, (Ast.Ident ("_", _), ty), body) ->
103 Ast.AttributedTerm (`Level (Parser.binder_prec, Parser.binder_assoc),
104 Ast.Layout (Ast.Box ((Ast.HV, false, true), [
106 Ast.Layout Ast.Break;
107 binder_symbol "\\to";
109 | Ast.Binder (binder_kind, (id, ty), body) ->
110 Ast.AttributedTerm (`Level (Parser.binder_prec, Parser.binder_assoc),
111 Ast.Layout (Ast.Box ((Ast.HV, false, true), [
112 binder_symbol (resolve_binder binder_kind);
116 Ast.Layout Ast.Break;
119 | t -> CicNotationUtil.visit_ast ~special_k k t
120 and aux_ty = function
121 | None -> builtin_symbol "?"
123 and special_k = function
124 | Ast.AttributedTerm (attrs, t) -> Ast.AttributedTerm (attrs, k t)
129 let ast_of_acic0 term_info acic k =
130 let k = k term_info in
131 let register_uri id uri = Hashtbl.add term_info.uri id uri in
134 Hashtbl.find term_info.sort id
135 with Not_found -> assert false
137 let aux_substs substs =
140 (fun (uri, annterm) -> (UriManager.name_of_uri uri, k annterm))
143 let aux_context context =
147 | Some annterm -> Some (k annterm))
151 | Cic.ARel (id,_,_,b) -> idref id (Ast.Ident (b, None))
152 | Cic.AVar (id,uri,substs) ->
153 register_uri id (UriManager.string_of_uri uri);
154 idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
155 | Cic.AMeta (id,n,l) -> idref id (Ast.Meta (n, aux_context l))
156 | Cic.ASort (id,Cic.Prop) -> idref id (Ast.Sort `Prop)
157 | Cic.ASort (id,Cic.Set) -> idref id (Ast.Sort `Set)
158 | Cic.ASort (id,Cic.Type _) -> idref id (Ast.Sort `Type)
159 | Cic.ASort (id,Cic.CProp) -> idref id (Ast.Sort `CProp)
160 | Cic.AImplicit _ -> assert false
161 | Cic.AProd (id,n,s,t) ->
163 match sort_of_id id with
164 | `Set | `Type -> `Pi
165 | `Prop | `CProp -> `Forall
167 idref id (Ast.Binder (binder_kind, (ident_of_name n, Some (k s)), k t))
168 | Cic.ACast (id,v,t) ->
169 idref id (Ast.Appl [idref id (Ast.Symbol ("cast", 0)); k v; k t])
170 | Cic.ALambda (id,n,s,t) ->
171 idref id (Ast.Binder (`Lambda, (ident_of_name n, Some (k s)), k t))
172 | Cic.ALetIn (id,n,s,t) ->
173 idref id (Ast.LetIn ((ident_of_name n, None), k s, k t))
174 | Cic.AAppl (aid,args) -> idref aid (Ast.Appl (List.map k args))
175 | Cic.AConst (id,uri,substs) ->
176 register_uri id (UriManager.string_of_uri uri);
177 idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
178 | Cic.AMutInd (id,uri,i,substs) as t ->
179 let name = name_of_inductive_type uri i in
180 let uri_str = UriManager.string_of_uri uri in
182 uri_str ^ "#xpointer(1/" ^ (string_of_int (i + 1)) ^ ")"
184 register_uri id puri_str;
185 idref id (Ast.Ident (name, aux_substs substs))
186 | Cic.AMutConstruct (id,uri,i,j,substs) ->
187 let name = constructor_of_inductive_type uri i j in
188 let uri_str = UriManager.string_of_uri uri in
189 let puri_str = sprintf "%s#xpointer(1/%d/%d)" uri_str (i + 1) j in
190 register_uri id puri_str;
191 idref id (Ast.Ident (name, aux_substs substs))
192 | Cic.AMutCase (id,uri,typeno,ty,te,patterns) ->
193 let name = name_of_inductive_type uri typeno in
194 let constructors = constructors_of_inductive_type uri typeno in
195 let rec eat_branch ty pat =
197 | Cic.Prod (_, _, t), Cic.ALambda (_, name, s, t') ->
198 let (cv, rhs) = eat_branch t t' in
199 (ident_of_name name, Some (k s)) :: cv, rhs
204 (fun (name, ty) pat ->
205 let (capture_variables, rhs) = eat_branch ty pat in
206 ((name, capture_variables), rhs))
207 constructors patterns
209 idref id (Ast.Case (k te, Some name, Some (k ty), patterns))
210 | Cic.AFix (id, no, funs) ->
213 (fun (_, n, decr_idx, ty, bo) ->
214 ((Ast.Ident (n, None), Some (k ty)), k bo, decr_idx))
219 (match List.nth defs no with
220 | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
222 with Not_found -> assert false
224 idref id (Ast.LetRec (`Inductive, defs, Ast.Ident (name, None)))
225 | Cic.ACoFix (id, no, funs) ->
228 (fun (_, n, ty, bo) -> ((Ast.Ident (n, None), Some (k ty)), k bo, 0))
233 (match List.nth defs no with
234 | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
236 with Not_found -> assert false
238 idref id (Ast.LetRec (`CoInductive, defs, Ast.Ident (name, None)))
242 (* persistent state *)
244 let level1_patterns21 = Hashtbl.create 211
245 let level2_patterns32 = Hashtbl.create 211
247 let (compiled21: (CicNotationPt.term -> (CicNotationEnv.t * int) option)
250 let (compiled32: (Cic.annterm -> ((string * Cic.annterm) list * int) option)
254 let pattern21_matrix = ref []
255 let pattern32_matrix = ref []
257 let get_compiled21 () =
258 match !compiled21 with
259 | None -> assert false
261 let get_compiled32 () =
262 match !compiled32 with
263 | None -> assert false
266 let set_compiled21 f = compiled21 := Some f
267 let set_compiled32 f = compiled32 := Some f
269 let instantiate21 env (* precedence associativity *) l1 =
270 let rec subst_singleton env t =
271 CicNotationUtil.group (subst env t)
272 and subst env = function
273 | Ast.AttributedTerm (_, t) -> subst env t
274 | Ast.Variable var ->
275 let name, expected_ty = CicNotationEnv.declaration_of_var var in
279 with Not_found -> assert false
281 assert (CicNotationEnv.well_typed ty value); (* INVARIANT *)
282 (* following assertion should be a conditional that makes this
283 * instantiation fail *)
284 assert (CicNotationEnv.well_typed expected_ty value);
285 [ CicNotationEnv.term_of_value value ]
286 | Ast.Magic m -> subst_magic env m
287 | Ast.Literal (`Keyword k) as t ->
288 [ Ast.AttributedTerm (`XmlAttrs keyword_attributes, t) ]
289 | Ast.Literal _ as t -> [ t ]
290 | Ast.Layout l -> [ Ast.Layout (subst_layout env l) ]
291 | t -> [ CicNotationUtil.visit_ast (subst_singleton env) t ]
292 and subst_magic env = function
293 | Ast.List0 (p, sep_opt)
294 | Ast.List1 (p, sep_opt) ->
295 let rec_decls = CicNotationEnv.declarations_of_term p in
297 List.map (fun (n, _) -> CicNotationEnv.lookup_list env n) rec_decls
299 let values = CicNotationUtil.ncombine rec_values in
303 | Some l -> [ CicNotationPt.Literal l ]
305 let rec instantiate_list acc = function
308 let env = CicNotationEnv.combine rec_decls value_set in
309 instantiate_list (CicNotationUtil.group (subst env p) :: acc) []
311 let env = CicNotationEnv.combine rec_decls value_set in
312 instantiate_list (CicNotationUtil.group ((subst env p) @ sep) :: acc) tl
314 instantiate_list [] values
316 let opt_decls = CicNotationEnv.declarations_of_term p in
318 let rec build_env = function
320 | (name, ty) :: tl ->
321 (* assumption: if one of the value is None then all are *)
322 (match CicNotationEnv.lookup_opt env name with
324 | Some v -> (name, (ty, v)) :: build_env tl)
326 try build_env opt_decls with Exit -> []
333 | _ -> assert false (* impossible *)
334 and subst_layout env = function
335 | Ast.Box (kind, tl) ->
336 Ast.Box (kind, List.concat (List.map (subst env) tl))
337 | l -> CicNotationUtil.visit_layout (subst_singleton env) l
339 subst_singleton env l1
341 let rec pp_ast1 term =
342 let rec pp_value = function
343 | CicNotationEnv.NumValue _ as v -> v
344 | CicNotationEnv.StringValue _ as v -> v
345 (* | CicNotationEnv.TermValue t when t == term -> CicNotationEnv.TermValue (pp_ast0 t pp_ast1) *)
346 | CicNotationEnv.TermValue t -> CicNotationEnv.TermValue (pp_ast1 t)
347 | CicNotationEnv.OptValue None as v -> v
348 | CicNotationEnv.OptValue (Some v) ->
349 CicNotationEnv.OptValue (Some (pp_value v))
350 | CicNotationEnv.ListValue vl ->
351 CicNotationEnv.ListValue (List.map pp_value vl)
353 let ast_env_of_env env =
354 List.map (fun (var, (ty, value)) -> (var, (ty, pp_value value))) env
357 | Ast.AttributedTerm (attrs, t) -> Ast.AttributedTerm (attrs, pp_ast1 t)
360 match (get_compiled21 ()) term with
361 | None -> pp_ast0 term pp_ast1
363 let precedence, associativity, l1 =
365 Hashtbl.find level1_patterns21 pid
366 with Not_found -> assert false
368 Ast.AttributedTerm (`Level (precedence, associativity),
369 (instantiate21 (ast_env_of_env env) l1))
372 let instantiate32 term_info env symbol args =
373 let rec instantiate_arg = function
374 | Ast.IdentArg (n, name) ->
375 let t = (try List.assoc name env with Not_found -> assert false) in
376 let rec count_lambda = function
377 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
380 let rec add_lambda t n =
382 let name = CicNotationUtil.fresh_name () in
383 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
384 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
388 add_lambda t (n - count_lambda t)
390 let args' = List.map instantiate_arg args in
391 Ast.Appl (Ast.Symbol (symbol, 0) :: args')
393 let rec ast_of_acic1 term_info annterm =
394 match (get_compiled32 ()) annterm with
395 | None -> ast_of_acic0 term_info annterm ast_of_acic1
398 List.map (fun (name, term) -> (name, ast_of_acic1 term_info term)) env
400 let symbol, args, uris =
402 Hashtbl.find level2_patterns32 pid
403 with Not_found -> assert false
405 let ast = instantiate32 term_info env' symbol args in
408 | _ -> Ast.AttributedTerm (`Href uris, ast)
410 let load_patterns32 t =
411 set_compiled32 (CicNotationMatcher.Matcher32.compiler t)
413 let load_patterns21 t =
414 set_compiled21 (CicNotationMatcher.Matcher21.compiler t)
416 let ast_of_acic id_to_sort annterm =
417 let term_info = { sort = id_to_sort; uri = Hashtbl.create 211 } in
418 let ast = ast_of_acic1 term_info annterm in
421 let pp_ast term = pp_ast1 term
424 let counter = ref ~-1 in
429 let add_interpretation (symbol, args) appl_pattern =
430 let id = fresh_id () in
431 let uris = CicNotationUtil.find_appl_pattern_uris appl_pattern in
432 Hashtbl.add level2_patterns32 id (symbol, args, uris);
433 pattern32_matrix := (appl_pattern, id) :: !pattern32_matrix;
434 load_patterns32 !pattern32_matrix;
437 let add_pretty_printer
438 ?(precedence = default_prec) ?(associativity = default_assoc) l2 l1
440 let id = fresh_id () in
441 let l2' = CicNotationUtil.strip_attributes l2 in
442 Hashtbl.add level1_patterns21 id (precedence, associativity, l1);
443 pattern21_matrix := (l2', id) :: !pattern21_matrix;
444 load_patterns21 !pattern21_matrix;
447 exception Interpretation_not_found
448 exception Pretty_printer_not_found
450 let remove_interpretation id =
452 Hashtbl.remove level2_patterns32 id;
453 with Not_found -> raise Interpretation_not_found);
454 pattern32_matrix := List.filter (fun (_, id') -> id <> id') !pattern32_matrix;
455 load_patterns32 !pattern32_matrix
457 let remove_pretty_printer id =
459 Hashtbl.remove level1_patterns21 id;
460 with Not_found -> raise Pretty_printer_not_found);
461 pattern21_matrix := List.filter (fun (_, id') -> id <> id') !pattern21_matrix;
462 load_patterns21 !pattern21_matrix