1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
26 (**************************************************************************)
30 (* Andrea Asperti <asperti@cs.unibo.it> *)
33 (**************************************************************************)
36 module P = Mpresentation;;
38 let symbol_table = Hashtbl.create 503;;
39 let symbol_table_charcount = Hashtbl.create 503;;
43 let rec countvar current_size = function
44 (Cic.Anonymous, None) -> current_size + 1 (* underscore *)
45 | (Cic.Anonymous, Some ty) -> countterm current_size ty
46 | (Cic.Name s, None) -> current_size + String.length s
47 | (Cic.Name s, Some ty) -> current_size + countterm current_size ty
49 and countterm current_size t =
50 if current_size > maxsize then current_size
52 A.AttributedTerm (_,t) -> countterm current_size t
54 List.fold_left countterm current_size l
55 | A.Binder (_,var,body) ->
56 let varsize = countvar current_size var in
57 countterm (varsize + 1) body (* binder *)
58 | A.Case (arg, _, ty, pl) ->
59 let size1 = countterm (current_size+10) arg in (* match with *)
63 | Some ty -> countterm size1 ty in
65 (fun c ((constr,vars),action) ->
67 List.fold_left countvar (c+String.length constr) vars in
68 countterm size3 action) size2 pl
69 | A.LetIn (var,s,t) ->
70 let size1 = countvar current_size var in
71 let size2 = countterm size1 s in
77 let size1 = countvar c var in
78 countterm size1 s) current_size l in
80 | A.Ident(s,None) -> current_size + (String.length s)
81 | A.Ident(s,Some l) ->
83 (fun c (v,t) -> countterm (c + (String.length v)) t)
84 (current_size + (String.length s)) l
85 | A.Implicit -> current_size + 1 (* underscore *)
90 None -> c + 1 (* underscore *)
91 | Some t -> countterm c t)
92 (current_size + 1) l (* num *)
93 | A.Num (s, _) -> current_size + String.length s
94 | A.Sort _ -> current_size + 4 (* sort name *)
95 | A.Symbol (s,_) -> current_size + String.length s
99 ((countterm 0 t) > maxsize)
102 let rec make_attributes l1 =
105 | None::tl -> make_attributes (List.tl l1) tl
107 let p,n = List.hd l1 in
108 (p,n,s)::(make_attributes (List.tl l1) tl)
111 let map_tex unicode texcmd =
112 let default_map_tex = Printf.sprintf "\\%s " in
115 Utf8Macro.expand texcmd
116 with Utf8Macro.Macro_not_found _ -> default_map_tex texcmd
118 default_map_tex texcmd
120 let resolve_binder unicode = function
121 | `Lambda -> map_tex unicode "lambda"
122 | `Pi -> map_tex unicode "Pi"
123 | `Forall -> map_tex unicode "forall"
124 | `Exists -> map_tex unicode "exists"
127 let rec make_args f ~tail = function
129 | [arg] -> [f ~tail arg]
130 | arg :: tl -> (f ~tail:[] arg) :: (make_args f ~tail tl)
132 let append_tail ~tail box =
135 | _ -> Box.H ([], box :: (List.map (fun t -> Box.Text ([], t)) tail))
137 let ast2astBox ?(unicode = true) ?(priority = 0) ?(assoc = false) ?(tail = [])
140 let lookup_uri = function
144 Some (Hashtbl.find ids_to_uris id)
145 with Not_found -> None)
147 let make_std_attributes xref =
148 let uri = lookup_uri xref in
149 make_attributes [Some "helm","xref"; Some "xlink","href"] [xref;uri]
151 let rec ast2box ?(priority = 0) ?(assoc = false) ?xref ~tail t =
153 bigast2box ~priority ~assoc ?xref ~tail t
155 let attrs = make_std_attributes xref in
156 append_tail ~tail (Box.Object (attrs, t))
158 bigast2box ?(priority = 0) ?(assoc = false) ?xref ~tail t =
160 | A.AttributedTerm (`Loc _, t) -> bigast2box ?xref ~tail t
161 | A.AttributedTerm (`IdRef xref, t) -> bigast2box ~xref ~tail t
162 | A.Appl [] -> assert false
163 | A.Appl ((hd::tl) as l) ->
164 let rec find_symbol idref = function
165 | A.AttributedTerm (`Loc _, t) -> find_symbol None t
166 | A.AttributedTerm (`IdRef id, t) -> find_symbol (Some id) t
167 | A.Symbol (name, _) ->
169 | None -> assert false
170 | Some idref -> `Symbol (name, idref))
173 let aattr = make_attributes [Some "helm","xref"] [xref] in
174 (match find_symbol None hd with
175 | `Symbol (name, sxref) ->
176 let sattr = make_std_attributes (Some sxref) in
178 (let f = Hashtbl.find symbol_table_charcount name in
179 f tl ~unicode ~priority ~assoc ~tail ~ids_to_uris aattr sattr)
182 (make_attributes [Some "helm","xref"] [xref],
185 make_args (fun ~tail t -> ast2box ~tail t)
186 ~tail:(")" :: tail) l);
190 (make_attributes [Some "helm","xref"] [xref],
194 (fun ~tail t -> ast2box ~tail t)
195 ~tail:(")" :: tail) l)]
197 | A.Binder (`Forall, (Cic.Anonymous, typ), body)
198 | A.Binder (`Pi, (Cic.Anonymous, typ), body) ->
199 Box.V(make_attributes [Some "helm","xref"] [xref],
201 | None -> Box.Text([], "?")
202 | Some typ -> ast2box ~tail:[] typ);
205 Box.Text([], map_tex unicode "to");
206 ast2box ~tail body])])
207 | A.Binder(kind, var, body) ->
208 Box.V(make_attributes [Some "helm","xref"] [xref],
210 [Box.Text ([None,"color","blue"], resolve_binder unicode kind);
211 make_var ~tail:("." :: tail) var]);
212 Box.indent (ast2box ~tail body)])
213 | A.Case(arg, _, ty, pl) ->
214 let make_rule ~tail sep ((constr,vars),rhs) =
216 Box.V([],[Box.H([],[Box.Text([],sep);
218 make_pattern constr vars;
220 Box.Text([],map_tex unicode "Rightarrow")]);
221 Box.indent (bigast2box ~tail rhs)])
223 Box.H([],[Box.Text([],sep);
225 make_pattern constr vars;
227 Box.Text([],map_tex unicode "Rightarrow");
229 Box.Object([],rhs)]) in
230 let plbox = match pl with
231 [] -> Box.Text([],"[]")
234 (make_rule ~tail:[] "[" r) ::
236 (fun ~tail pat -> make_rule ~tail:[] "|" pat)
243 [ Box.H([],[Box.Text([],"[");
249 Box.V(make_attributes [Some "helm","xref"] [xref],
251 [Box.Text([],"match");
252 Box.indent (Box.H([],[Box.skip; bigast2box ~tail arg]));
256 Box.V(make_attributes [Some "helm","xref"] [xref],
258 [Box.H(make_attributes [Some "helm","xref"] [xref],
259 [Box.Text([],"match");
263 Box.Text([],"with")]);
265 | A.LetIn (var, s, t) ->
266 Box.V(make_attributes [Some "helm","xref"] [xref],
267 (make_def "let" var s "in")@[ast2box ~tail t])
268 | A.LetRec (_, vars, body) ->
270 let rec make_defs let_txt = function
273 make_def let_txt var s "in"
275 (make_def let_txt var s "")@(make_defs "and" tl) in
276 make_defs "let rec" vars in
277 Box.V(make_attributes [Some "helm","xref"] [xref],
278 definitions@[ast2box ~tail body])
279 | A.Ident (s, subst) ->
281 let rec make_substs start_txt =
285 make_subst start_txt s t "]"
287 (make_subst start_txt s t ";")@(make_substs " " tl)
290 | Some subst -> make_substs ((map_tex unicode "subst") ^ "[") subst
293 Box.V([], (* attr here or on Vbox? *)
294 [Box.Text(make_std_attributes xref,s);
295 Box.indent(Box.V([],subst))])
297 Box.Text([],"_") (* big? *)
302 Box.H([],[aux_option ~tail t; Box.Text([],";")]))
304 Box.V(make_attributes [Some "helm","xref"] [xref],
305 [Box.Text([],"?"^(string_of_int n));
306 Box.H([],[Box.Text([],"[");
307 Box.V([],local_context);
313 `Prop -> Box.Text([],"Prop")
314 | `Set -> Box.Text([],"Set")
315 | `Type -> Box.Text([],"Type")
316 | `CProp -> Box.Text([],"CProp"))
320 and aux_option ~tail = function
321 None -> Box.Text([],"_")
322 | Some ast -> ast2box ~tail ast
324 and make_var ~tail = function
325 (Cic.Anonymous, None) -> Box.Text([],"_:_")
326 | (Cic.Anonymous, Some t) ->
327 Box.H([],[Box.Text([],"_:");ast2box ~tail t])
328 | (Cic.Name s, None) -> Box.Text([],s)
329 | (Cic.Name s, Some t) ->
331 Box.V([],[Box.Text([],s^":");
332 Box.indent(bigast2box ~tail t)])
334 Box.H([],[Box.Text([],s^":");Box.Object([],t)])
336 and make_pattern constr =
338 [] -> Box.Text([],constr)
345 (* ignoring the type *)
346 (Cic.Anonymous, _) -> Box.Text([],"_")
347 | (Cic.Name s, _) -> Box.Text([],s) in
348 Box.Text([]," ")::bv::acc) vars [Box.Text([],")")] in
349 Box.H([],Box.Text([],"(")::Box.Text([],constr)::bvars)
352 and make_def let_txt var def end_txt =
355 (* ignoring the type *)
356 (Cic.Anonymous, _) -> Box.Text([],"_")
357 | (Cic.Name s, _) -> Box.Text([],s)) in
358 pretty_append ~tail:[end_txt]
359 [Box.Text([],let_txt);
367 and make_subst start_txt varname body end_txt =
368 pretty_append ~tail:[end_txt]
369 [Box.Text([],start_txt);
370 Box.Text([],varname);
372 Box.Text([],map_tex unicode "Assign")
376 and pretty_append ~tail l ast =
379 Box.H([],[Box.skip; bigast2box ~tail ast])]
381 [Box.H([],l@[Box.smallskip; (ast2box ~tail ast)])]
384 ast2box ~priority ~assoc ~tail t
387 let m_row_with_fences = P.row_with_brackets
388 let m_row = P.row_without_brackets
389 let m_open_fence = P.Mo([None, "stretchy", "false"], "(")
390 let m_close_fence = P.Mo([None, "stretchy", "false"], ")")
392 let ast2mpres ?(priority = 0) ?(assoc = false) (ast, ids_to_uris) =
393 let lookup_uri = function
397 Some (Hashtbl.find ids_to_uris id)
398 with Not_found -> None)
400 let make_std_attributes xref =
401 let uri = lookup_uri xref in
402 make_attributes [Some "helm","xref"; Some "xlink","href"] [xref;uri]
406 | A.AttributedTerm (`Loc _, ast) -> aux ?xref ast
407 | A.AttributedTerm (`IdRef xref, ast) -> aux ~xref ast
408 | A.Symbol (name,_) ->
409 let attr = make_std_attributes xref in
411 | A.Ident (name,subst) ->
412 let attr = make_std_attributes xref in
426 | Some subst -> make_subst subst
429 | [] -> P.Mi (attr, name)
435 [(P.Mtext([],"]"))]))
437 let attr = make_attributes [Some "helm","xref"] [xref] in
445 P.Mrow ([],P.Mi (attr,"?" ^ string_of_int no) :: P.Mo ([],"[") ::
446 l' @ [P.Mo ([],"]")])
447 | A.Num (value, _) ->
448 let attr = make_attributes [Some "helm","xref"] [xref] in
450 | A.Sort `Prop -> P.Mtext ([], "Prop")
451 | A.Sort `Set -> P.Mtext ([], "Set")
452 | A.Sort `Type -> P.Mtext ([], "Type")
453 | A.Sort `CProp -> P.Mtext ([], "CProp")
454 | A.Implicit -> P.Mtext([], "?")
455 | A.Appl [] -> assert false
456 | A.Appl ((hd::tl) as l) ->
457 let rec find_symbol idref = function
458 | A.AttributedTerm (`Loc _, t) -> find_symbol None t
459 | A.AttributedTerm (`IdRef id, t) -> find_symbol (Some id) t
460 | A.Symbol (name, _) ->
462 | None -> assert false
463 | Some idref -> `Symbol (name, idref))
467 let aattr = make_attributes [Some "helm","xref"] [xref] in
468 (match find_symbol None hd with
469 | `Symbol (name, sxref) ->
470 let sattr = make_std_attributes (Some sxref) in
472 (let f = Hashtbl.find symbol_table name in
473 f tl ~priority ~assoc ~ids_to_uris aattr sattr)
476 m_open_fence :: P.Mi(sattr,name)::(make_args tl) @
479 P.Mrow(aattr, m_open_fence :: (aux hd) :: (make_args tl) @
481 | A.Binder (`Pi, (Cic.Anonymous, Some ty), body)
482 | A.Binder (`Forall, (Cic.Anonymous, Some ty), body) ->
483 let attr = make_attributes [Some "helm","xref"] [xref] in
486 P.Mtext ([], map_tex true "to");
488 | A.Binder (kind,var,body) ->
489 let attr = make_attributes [Some "helm","xref"] [xref] in
490 let binder = resolve_binder true kind in
491 let var = make_capture_variable var in
493 P.Mtext([None,"mathcolor","blue"],binder) :: var @
494 [P.Mo([],"."); aux body])
495 | A.LetIn (var,s,body) ->
496 let attr = make_attributes [Some "helm","xref"] [xref] in
498 P.Mtext([],("let ")) ::
499 (make_capture_variable var @
504 | A.LetRec (_, defs, body) ->
505 let attr = make_attributes [Some "helm","xref"] [xref] in
510 make_capture_variable var @ [P.Mtext([],"=");(aux body)]
511 | (var,body,_)::tl ->
512 make_capture_variable var @
514 (aux body)::P.Mtext([]," and")::(make_defs tl))) in
516 P.Mtext([],("let rec "))::
518 (P.Mtext([]," in ")::
520 | A.Case (arg,_,outtype,patterns) ->
521 (* TODO print outtype *)
522 let attr = make_attributes [Some "helm","xref"] [xref] in
523 let rec make_patterns =
526 | [(constr, vars),rhs] -> make_pattern constr vars rhs
527 | ((constr,vars), rhs)::tl ->
528 (make_pattern constr vars rhs)@(P.smallskip::
529 P.Mtext([],"|")::P.smallskip::(make_patterns tl)))
530 and make_pattern constr vars rhs =
532 P.Mtext([], constr) ::
534 (List.map (fun var -> P.smallskip :: make_capture_variable var)
538 [P.smallskip; P.Mtext([],map_tex true "to"); P.smallskip; aux rhs]
541 P.Mtext([],"match")::P.smallskip::
542 (aux arg)::P.smallskip::
543 P.Mtext([],"with")::P.smallskip::
544 P.Mtext([],"[")::P.smallskip::
545 (make_patterns patterns)@(P.smallskip::[P.Mtext([],("]"))]))
547 and make_capture_variable (name, ty) =
550 | Cic.Anonymous -> [P.Mtext([], "_")]
551 | Cic.Name s -> [P.Mtext([], s)]
556 | Some ty -> [P.Mtext([],":"); aux ty]
560 and make_args ?(priority = 0) ?(assoc = false) =
563 | a::tl -> P.smallskip::(aux a)::(make_args tl)
569 let box_prefix = "b";;
571 let add_xml_declaration stream =
573 Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
575 Xml.xml_nempty ~prefix:box_prefix "box"
576 [ Some "xmlns","m","http://www.w3.org/1998/Math/MathML" ;
577 Some "xmlns","b","http://helm.cs.unibo.it/2003/BoxML" ;
578 Some "xmlns","helm","http://www.cs.unibo.it/helm" ;
579 Some "xmlns","xlink","http://www.w3.org/1999/xlink"
583 let ast2mpresXml ((ast, ids_to_uris) as arg) =
584 let astBox = ast2astBox arg in
585 let smallAst2mpresXml ast =
586 P.print_mpres (ast2mpres (ast, ids_to_uris))
588 (Box.box2xml ~obj2xml:smallAst2mpresXml astBox)
590 let b_open_fence = Box.b_text [] "("
591 let b_close_fence = Box.b_text [] ")"
592 let b_v_with_fences attrs a b op =
601 let b_v_without_fences attrs a b op =
607 let _ = (** fill symbol_table *)
608 let binary f = function [a;b] -> f a b | _ -> assert false in
609 let unary f = function [a] -> f a | _ -> assert false in
610 let add_binary_op name threshold ?(assoc=`Left) symbol =
611 let assoc = match assoc with `Left -> true | `Right -> false in
612 Hashtbl.add symbol_table name (binary
613 (fun a b ~priority ~assoc ~ids_to_uris aattr sattr ->
616 | `Symbol name -> map_tex true name
619 if (priority > threshold) || (priority = threshold && assoc) then
620 m_row_with_fences aattr
621 (ast2mpres ~priority:threshold ~assoc (a, ids_to_uris))
622 (ast2mpres ~priority:threshold ~assoc:(not assoc) (b, ids_to_uris))
626 (ast2mpres ~priority:threshold ~assoc (a, ids_to_uris))
627 (ast2mpres ~priority:threshold ~assoc:(not assoc) (b, ids_to_uris))
628 (P.Mo(sattr,symbol))));
629 Hashtbl.add symbol_table_charcount name (binary
630 (fun a b ~unicode ~priority ~assoc ~tail ~ids_to_uris aattr sattr ->
633 | `Symbol name -> map_tex unicode name
637 if (priority > threshold) || (priority = threshold && assoc) then
638 b_v_with_fences aattr
639 (ast2astBox ~priority:threshold ~assoc ~tail:[] (a, ids_to_uris))
640 (ast2astBox ~priority:threshold ~assoc:(not assoc) ~tail
642 (Box.Text(sattr,symbol))
644 b_v_without_fences aattr
645 (ast2astBox ~priority:threshold ~assoc ~tail:[] (a, ids_to_uris))
646 (ast2astBox ~priority:threshold ~assoc:(not assoc) ~tail
648 (Box.Text(sattr,symbol))))
650 Hashtbl.add symbol_table "not" (unary
651 (fun a ~priority ~assoc ~ids_to_uris attr sattr ->
653 m_open_fence; P.Mo([],map_tex true "lnot");
654 ast2mpres (a, ids_to_uris); m_close_fence]))));
655 Hashtbl.add symbol_table "inv" (unary
656 (fun a ~priority ~assoc ~ids_to_uris attr sattr ->
658 P.Mrow([],[ m_open_fence; ast2mpres (a, ids_to_uris); m_close_fence]),
659 P.Mrow([],[ P.Mo([],"-"); P.Mn([],"1")]))));
660 Hashtbl.add symbol_table "opp" (unary
661 (fun a ~priority ~assoc ~ids_to_uris attr sattr ->
665 ast2mpres (a, ids_to_uris);
667 add_binary_op "arrow" 5 ~assoc:`Right (`Symbol "to");
668 add_binary_op "eq" 40 (`Ascii "=");
669 add_binary_op "and" 20 (`Symbol "land");
670 add_binary_op "or" 10 (`Symbol "lor");
671 add_binary_op "iff" 5 (`Symbol "iff");
672 add_binary_op "leq" 40 (`Symbol "leq");
673 add_binary_op "lt" 40 (`Symbol "lt");
674 add_binary_op "geq" 40 (`Symbol "geq");
675 add_binary_op "gt" 40 (`Symbol "gt");
676 add_binary_op "plus" 60 (`Ascii "+");
677 add_binary_op "times" 70 (`Ascii "*");
678 add_binary_op "minus" 60 (`Ascii "-");
679 add_binary_op "div" 60 (`Ascii "/");