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
81 | A.Uri (s, None) -> current_size + (String.length s)
85 (fun c (v,t) -> countterm (c + (String.length v)) t)
86 (current_size + (String.length s)) l
87 | A.Implicit -> current_size + 1 (* underscore *)
92 None -> c + 1 (* underscore *)
93 | Some t -> countterm c t)
94 (current_size + 1) l (* num *)
95 | A.Num (s, _) -> current_size + String.length s
96 | A.Sort _ -> current_size + 4 (* sort name *)
97 | A.Symbol (s,_) -> current_size + String.length s
99 | A.UserInput -> current_size
103 ((countterm 0 t) > maxsize)
106 let rec make_attributes l1 =
109 | None::tl -> make_attributes (List.tl l1) tl
111 let p,n = List.hd l1 in
112 (p,n,s)::(make_attributes (List.tl l1) tl)
115 let map_tex unicode texcmd =
116 let default_map_tex = Printf.sprintf "\\%s " in
119 Utf8Macro.expand texcmd
120 with Utf8Macro.Macro_not_found _ -> default_map_tex texcmd
122 default_map_tex texcmd
124 let resolve_binder unicode = function
125 | `Lambda -> map_tex unicode "lambda"
126 | `Pi -> map_tex unicode "Pi"
127 | `Forall -> map_tex unicode "forall"
128 | `Exists -> map_tex unicode "exists"
131 let rec make_args f ~tail = function
133 | [arg] -> [f ~tail arg]
134 | arg :: tl -> (f ~tail:[] arg) :: (make_args f ~tail tl)
136 let append_tail ~tail box =
139 | _ -> Box.H ([], box :: (List.map (fun t -> Box.Text ([], t)) tail))
141 let rec find_symbol idref = function
142 | A.AttributedTerm (`Loc _, t) -> find_symbol None t
143 | A.AttributedTerm (`IdRef id, t) -> find_symbol (Some id) t
144 | A.Symbol (name, _) -> `Symbol (name, idref)
147 let ast2astBox ?(unicode = true) ?(priority = 0) ?(assoc = false) ?(tail = [])
150 let lookup_uri = function
154 Some (Hashtbl.find ids_to_uris id)
155 with Not_found -> None)
157 let make_std_attributes xref =
158 let uri = lookup_uri xref in
159 make_attributes [Some "helm","xref"; Some "xlink","href"] [xref;uri]
161 let rec ast2box ?(priority = 0) ?(assoc = false) ?xref ~tail t =
163 bigast2box ~priority ~assoc ?xref ~tail t
165 let attrs = make_std_attributes xref in
166 append_tail ~tail (Box.Object (attrs, t))
168 bigast2box ?(priority = 0) ?(assoc = false) ?xref ~tail t =
170 | A.AttributedTerm (`Loc _, t) -> bigast2box ?xref ~tail t
171 | A.AttributedTerm (`IdRef xref, t) -> bigast2box ~xref ~tail t
172 | A.Appl [] -> assert false
173 | A.Appl ((hd::tl) as l) ->
174 let aattr = make_attributes [Some "helm","xref"] [xref] in
175 (match find_symbol None hd with
176 | `Symbol (name, sxref) ->
177 let sattr = make_std_attributes sxref in
179 (let f = Hashtbl.find symbol_table_charcount name in
180 f tl ~unicode ~priority ~assoc ~tail ~ids_to_uris aattr sattr)
183 (make_attributes [Some "helm","xref"] [xref],
186 make_args (fun ~tail t -> ast2box ~tail t)
187 ~tail:(")" :: tail) l);
191 (make_attributes [Some "helm","xref"] [xref],
195 (fun ~tail t -> ast2box ~tail t)
196 ~tail:(")" :: tail) l)]
198 | A.Binder (`Forall, (Cic.Anonymous, typ), body)
199 | A.Binder (`Pi, (Cic.Anonymous, typ), body) ->
200 Box.V(make_attributes [Some "helm","xref"] [xref],
202 | None -> Box.Text([], "?")
203 | Some typ -> ast2box ~tail:[] typ);
206 Box.Text([], map_tex unicode "to");
207 ast2box ~tail body])])
208 | A.Binder(kind, var, body) ->
209 Box.V(make_attributes [Some "helm","xref"] [xref],
211 [Box.Text ([None,"color","blue"], resolve_binder unicode kind);
212 make_var ~tail:("." :: tail) var]);
213 Box.indent (ast2box ~tail body)])
214 | A.Case(arg, _, ty, pl) ->
215 let make_rule ~tail sep ((constr,vars),rhs) =
217 Box.V([],[Box.H([],[Box.Text([],sep);
219 make_pattern constr vars;
221 Box.Text([],map_tex unicode "Rightarrow")]);
222 Box.indent (bigast2box ~tail rhs)])
224 Box.H([],[Box.Text([],sep);
226 make_pattern constr vars;
228 Box.Text([],map_tex unicode "Rightarrow");
230 append_tail ~tail (Box.Object([],rhs))]) in
233 [] -> append_tail ~tail (Box.Text([],"[]"))
236 (make_rule ~tail:[] "[" r) ::
238 (fun ~tail pat -> make_rule ~tail "|" pat)
245 [ Box.H([],[Box.Text([],"[");
251 Box.V(make_attributes [Some "helm","xref"] [xref],
253 [Box.Text([],"match");
254 Box.indent (Box.H([],[Box.skip; bigast2box ~tail:[] arg]));
258 Box.V(make_attributes [Some "helm","xref"] [xref],
260 [Box.H(make_attributes [Some "helm","xref"] [xref],
261 [Box.Text([],"match");
263 ast2box ~tail:[] arg;
265 Box.Text([],"with")]);
267 | A.LetIn (var, s, t) ->
268 Box.V(make_attributes [Some "helm","xref"] [xref],
269 (make_def "let" var s "in")@[ast2box ~tail t])
270 | A.LetRec (_, vars, body) ->
272 let rec make_defs let_txt = function
274 | [(var,s,_)] -> make_def let_txt var s "in"
276 (make_def let_txt var s "")@(make_defs "and" tl) in
277 make_defs "let rec" vars in
278 Box.V(make_attributes [Some "helm","xref"] [xref],
279 definitions@[ast2box ~tail body])
281 | A.Uri (s, subst) ->
283 let rec make_substs start_txt =
287 make_subst start_txt s t ""
289 (make_subst start_txt s t ";")@(make_substs "" tl)
294 [Box.Text(make_std_attributes xref,s);
296 [Box.Text([],"[...]");
297 Box.H([], [Box.Text([], map_tex unicode "subst" ^ "[");
298 Box.V([], make_substs "" subst);
299 Box.Text([], "]")])])])
300 | None -> Box.Text(make_std_attributes xref, s)
304 Box.H([], (* attr here or on Vbox? *)
305 [Box.Text(make_std_attributes xref,s);
306 Box.Action([], [Box.Text([], "[_]"); Box.V([],subst)])])
307 (* Box.indent(Box.V([],subst))]) *)
310 Box.Text([],"_") (* big? *)
315 Box.H([],[aux_option ~tail t; Box.Text([],";")]))
317 Box.V(make_attributes [Some "helm","xref"] [xref],
318 [Box.Text([],"?"^(string_of_int n));
319 Box.H([],[Box.Text([],"[");
320 Box.V([],local_context);
326 `Prop -> Box.Text([],"Prop")
327 | `Set -> Box.Text([],"Set")
328 | `Type -> Box.Text([],"Type")
329 | `CProp -> Box.Text([],"CProp"))
333 | A.UserInput -> Box.Text([],"")
335 and aux_option ~tail = function
336 None -> Box.Text([],"_")
337 | Some ast -> ast2box ~tail ast
339 and make_var ~tail = function
340 (Cic.Anonymous, None) -> Box.Text([],"_:_")
341 | (Cic.Anonymous, Some t) ->
342 Box.H([],[Box.Text([],"_:");ast2box ~tail t])
343 | (Cic.Name s, None) -> Box.Text([],s)
344 | (Cic.Name s, Some t) ->
346 Box.V([],[Box.Text([],s^":");
347 Box.indent(bigast2box ~tail t)])
349 Box.H([],[Box.Text([],s^":");Box.Object([],t)])
351 and make_pattern constr =
353 [] -> Box.Text([],constr)
360 (* ignoring the type *)
361 (Cic.Anonymous, _) -> Box.Text([],"_")
362 | (Cic.Name s, _) -> Box.Text([],s) in
363 Box.Text([]," ")::bv::acc) vars [Box.Text([],")")] in
364 Box.H([],Box.Text([],"(")::Box.Text([],constr)::bvars)
367 and make_def let_txt var def end_txt =
370 (* ignoring the type *)
371 (Cic.Anonymous, _) -> Box.Text([],"_")
372 | (Cic.Name s, _) -> Box.Text([],s)) in
373 pretty_append ~tail:[end_txt]
374 [Box.Text([],let_txt);
382 and make_subst start_txt varname body end_txt =
383 pretty_append ~tail:[end_txt]
384 ((if start_txt <> "" then [Box.Text([],start_txt)] else [])@
385 [Box.Text([],varname);
387 Box.Text([],map_tex unicode "Assign")
391 and pretty_append ~tail l ast =
394 Box.H([],[Box.skip; bigast2box ~tail ast])]
396 [Box.H([],l@[Box.smallskip; (ast2box ~tail ast)])]
399 ast2box ~priority ~assoc ~tail t
402 let m_row_with_fences = P.row_with_brackets
403 let m_row = P.row_without_brackets
404 let m_open_fence = P.Mo([None, "stretchy", "false"], "(")
405 let m_close_fence = P.Mo([None, "stretchy", "false"], ")")
407 let ast2mpres ?(priority = 0) ?(assoc = false) (ast, ids_to_uris) =
408 let lookup_uri = function
412 Some (Hashtbl.find ids_to_uris id)
413 with Not_found -> None)
415 let make_std_attributes xref =
416 let uri = lookup_uri xref in
417 make_attributes [Some "helm","xref"; Some "xlink","href"] [xref;uri]
421 | A.AttributedTerm (`Loc _, ast) -> aux ?xref ast
422 | A.AttributedTerm (`IdRef xref, ast) -> aux ~xref ast
423 | A.Symbol (name,_) ->
424 let attr = make_std_attributes xref in
426 | A.Ident (name,subst)
427 | A.Uri (name,subst) ->
428 let attr = make_std_attributes xref in
442 | Some subst -> make_subst subst
445 | [] -> P.Mi (attr, name)
451 [(P.Mtext([],"]"))]))
453 let attr = make_attributes [Some "helm","xref"] [xref] in
461 P.Mrow ([],P.Mi (attr,"?" ^ string_of_int no) :: P.Mo ([],"[") ::
462 l' @ [P.Mo ([],"]")])
463 | A.Num (value, _) ->
464 let attr = make_attributes [Some "helm","xref"] [xref] in
466 | A.Sort `Prop -> P.Mtext ([], "Prop")
467 | A.Sort `Set -> P.Mtext ([], "Set")
468 | A.Sort `Type -> P.Mtext ([], "Type")
469 | A.Sort `CProp -> P.Mtext ([], "CProp")
470 | A.Implicit -> P.Mtext([], "?")
471 | A.UserInput -> P.Mtext([], "")
472 | A.Appl [] -> assert false
473 | A.Appl ((hd::tl) as l) ->
474 let aattr = make_attributes [Some "helm","xref"] [xref] in
475 (match find_symbol None hd with
476 | `Symbol (name, sxref) ->
477 let sattr = make_std_attributes sxref in
479 (let f = Hashtbl.find symbol_table name in
480 f tl ~priority ~assoc ~ids_to_uris aattr sattr)
483 m_open_fence :: P.Mi(sattr,name)::(make_args tl) @
486 P.Mrow(aattr, m_open_fence :: (aux hd) :: (make_args tl) @
488 | A.Binder (`Pi, (Cic.Anonymous, Some ty), body)
489 | A.Binder (`Forall, (Cic.Anonymous, Some ty), body) ->
490 let attr = make_attributes [Some "helm","xref"] [xref] in
493 P.Mtext ([], map_tex true "to");
495 | A.Binder (kind,var,body) ->
496 let attr = make_attributes [Some "helm","xref"] [xref] in
497 let binder = resolve_binder true kind in
498 let var = make_capture_variable var in
500 P.Mtext([None,"mathcolor","blue"],binder) :: var @
501 [P.Mo([],"."); aux body])
502 | A.LetIn (var,s,body) ->
503 let attr = make_attributes [Some "helm","xref"] [xref] in
505 P.Mtext([],("let ")) ::
506 (make_capture_variable var @
511 | A.LetRec (_, defs, body) ->
512 let attr = make_attributes [Some "helm","xref"] [xref] in
517 make_capture_variable var @ [P.Mtext([],"=");(aux body)]
518 | (var,body,_)::tl ->
519 make_capture_variable var @
521 (aux body)::P.Mtext([]," and")::(make_defs tl))) in
523 P.Mtext([],("let rec "))::
525 (P.Mtext([]," in ")::
527 | A.Case (arg,_,outtype,patterns) ->
528 (* TODO print outtype *)
529 let attr = make_attributes [Some "helm","xref"] [xref] in
530 let rec make_patterns =
533 | [(constr, vars),rhs] -> make_pattern constr vars rhs
534 | ((constr,vars), rhs)::tl ->
535 (make_pattern constr vars rhs)@(P.smallskip::
536 P.Mtext([],"|")::P.smallskip::(make_patterns tl)))
537 and make_pattern constr vars rhs =
539 P.Mtext([], constr) ::
542 (fun var -> P.smallskip :: make_capture_variable var)
546 [P.smallskip; P.Mtext([],map_tex true "to"); P.smallskip; aux rhs]
549 P.Mtext([],"match")::P.smallskip::
550 (aux arg)::P.smallskip::
551 P.Mtext([],"with")::P.smallskip::
552 P.Mtext([],"[")::P.smallskip::
553 (make_patterns patterns)@(P.smallskip::[P.Mtext([],("]"))]))
555 and make_capture_variable (name, ty) =
558 | Cic.Anonymous -> [P.Mtext([], "_")]
559 | Cic.Name s -> [P.Mtext([], s)]
564 | Some ty -> [P.Mtext([],":"); aux ty]
568 and make_args ?(priority = 0) ?(assoc = false) =
571 | a::tl -> P.smallskip::(aux a)::(make_args tl)
577 let box_prefix = "b";;
579 let add_xml_declaration stream =
581 Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
583 Xml.xml_nempty ~prefix:box_prefix "box"
584 [ Some "xmlns","m","http://www.w3.org/1998/Math/MathML" ;
585 Some "xmlns","b","http://helm.cs.unibo.it/2003/BoxML" ;
586 Some "xmlns","helm","http://www.cs.unibo.it/helm" ;
587 Some "xmlns","xlink","http://www.w3.org/1999/xlink"
591 let ast2mpresXml ((ast, ids_to_uris) as arg) =
592 let astBox = ast2astBox arg in
593 let smallAst2mpresXml ast =
594 P.print_mpres (ast2mpres (ast, ids_to_uris))
596 (Box.box2xml ~obj2xml:smallAst2mpresXml astBox)
598 let b_open_fence = Box.b_text [] "("
599 let b_close_fence = Box.b_text [] ")"
600 let b_v_with_fences attrs a b op =
609 let b_v_without_fences attrs a b op =
615 let _ = (** fill symbol_table *)
616 let binary f = function [a;b] -> f a b | _ -> assert false in
617 let unary f = function [a] -> f a | _ -> assert false in
618 let add_binary_op name threshold ?(assoc=`Left) symbol =
619 let assoc = match assoc with `Left -> true | `Right -> false in
620 Hashtbl.add symbol_table name (binary
621 (fun a b ~priority ~assoc ~ids_to_uris aattr sattr ->
624 | `Symbol name -> map_tex true name
627 if (priority > threshold) || (priority = threshold && assoc) then
628 m_row_with_fences aattr
629 (ast2mpres ~priority:threshold ~assoc (a, ids_to_uris))
630 (ast2mpres ~priority:threshold ~assoc:(not assoc) (b, ids_to_uris))
634 (ast2mpres ~priority:threshold ~assoc (a, ids_to_uris))
635 (ast2mpres ~priority:threshold ~assoc:(not assoc) (b, ids_to_uris))
636 (P.Mo(sattr,symbol))));
637 Hashtbl.add symbol_table_charcount name (binary
638 (fun a b ~unicode ~priority ~assoc ~tail ~ids_to_uris aattr sattr ->
641 | `Symbol name -> map_tex unicode name
645 if (priority > threshold) || (priority = threshold && assoc) then
646 b_v_with_fences aattr
647 (ast2astBox ~priority:threshold ~assoc ~tail:[] (a, ids_to_uris))
648 (ast2astBox ~priority:threshold ~assoc:(not assoc) ~tail
650 (Box.Text(sattr,symbol))
652 b_v_without_fences aattr
653 (ast2astBox ~priority:threshold ~assoc ~tail:[] (a, ids_to_uris))
654 (ast2astBox ~priority:threshold ~assoc:(not assoc) ~tail
656 (Box.Text(sattr,symbol))))
658 Hashtbl.add symbol_table "not" (unary
659 (fun a ~priority ~assoc ~ids_to_uris attr sattr ->
661 m_open_fence; P.Mo([],map_tex true "lnot");
662 ast2mpres (a, ids_to_uris); m_close_fence]))));
663 Hashtbl.add symbol_table "inv" (unary
664 (fun a ~priority ~assoc ~ids_to_uris attr sattr ->
666 P.Mrow([],[ m_open_fence; ast2mpres (a, ids_to_uris); m_close_fence]),
667 P.Mrow([],[ P.Mo([],"-"); P.Mn([],"1")]))));
668 Hashtbl.add symbol_table "opp" (unary
669 (fun a ~priority ~assoc ~ids_to_uris attr sattr ->
673 ast2mpres (a, ids_to_uris);
675 add_binary_op "arrow" 5 ~assoc:`Right (`Symbol "to");
676 add_binary_op "eq" 40 (`Ascii "=");
677 add_binary_op "and" 20 (`Symbol "land");
678 add_binary_op "or" 10 (`Symbol "lor");
679 add_binary_op "iff" 5 (`Symbol "iff");
680 add_binary_op "leq" 40 (`Symbol "leq");
681 add_binary_op "lt" 40 (`Symbol "lt");
682 add_binary_op "geq" 40 (`Symbol "geq");
683 add_binary_op "gt" 40 (`Symbol "gt");
684 add_binary_op "plus" 60 (`Ascii "+");
685 add_binary_op "times" 70 (`Ascii "*");
686 add_binary_op "minus" 60 (`Ascii "-");
687 add_binary_op "div" 60 (`Ascii "/");
688 add_binary_op "cast" 80 (`Ascii ":");