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 (**************************************************************************)
35 module P = Mpresentation;;
37 let symbol_table = Hashtbl.create 503;;
38 let symbol_table_charcount = Hashtbl.create 503;;
42 let countterm current_size t =
43 let module CE = Content_expressions in
44 let rec aux current_size t =
45 if current_size > maxsize then current_size
47 CE.Symbol (_,name,None,_) -> current_size + (String.length name)
48 | CE.Symbol (_,name,Some subst,_) ->
49 let c1 = current_size + (String.length name) in
51 | CE.LocalVar (_,name) -> current_size + (String.length name)
52 | CE.Meta (_,name) -> current_size + (String.length name)
53 | CE.Num (_,value) -> current_size + (String.length value)
55 List.fold_left aux current_size l
56 | CE.Binder (_, _,(n,s),body) ->
57 let cs = aux (current_size + 2 + (String.length n)) s in
59 | CE.Letin (_,(n,s),body) ->
60 let cs = aux (current_size + 3 + (String.length n)) s in
62 | CE.Letrec (_,defs,body) ->
65 (fun c (n,bo) -> aux (c+(String.length n)) bo) current_size defs in
68 let cs = aux (current_size + 4) a in
70 (fun c (n,bo) -> aux (c+(String.length n)) bo) current_size np
72 countsubst subst current_size =
74 (fun current_size (uri,expr) ->
75 if (current_size > maxsize) then current_size
78 (current_size + (String.length (UriManager.name_of_uri uri))) in
79 (aux c1 expr)) current_size subst
85 ((countterm 0 t) > maxsize)
88 let rec make_attributes l1 =
91 | None::tl -> make_attributes (List.tl l1) tl
93 let p,n = List.hd l1 in
94 (p,n,s)::(make_attributes (List.tl l1) tl)
97 let rec cexpr2pres ?(priority = 0) ?(assoc = false) ?(tail = []) t =
98 let module CE = Content_expressions in
99 let module P = Mpresentation in
102 CE.Symbol (xref,name,None,uri) ->
105 [Some "helm","xref";Some "xlink","href"] [xref;uri] in
108 else P.Mrow([],P.Mi (attr,name)::tail)
109 | CE.Symbol (xref,name,Some subst,uri) ->
112 [Some "helm","xref";Some "xlink","href"] [xref;uri] in
119 P.Mi([],UriManager.name_of_uri uri)]
123 P.Mi([],UriManager.name_of_uri uri)::
131 (P.Mtext([],"]")::tail))
132 | CE.LocalVar (xref,name) ->
133 let attr = make_attributes [Some "helm","xref"] [xref] in
136 else P.Mrow([],P.Mi (attr,name)::tail)
137 | CE.Meta (xref,name) ->
138 let attr = make_attributes [Some "helm","xref"] [xref] in
141 else P.Mrow([],P.Mi (attr,name)::tail)
142 | CE.Num (xref,value) ->
143 let attr = make_attributes [Some "helm","xref"] [xref] in
146 else P.Mrow([],P.Mn (attr,value)::tail)
147 | CE.Appl (axref,CE.Symbol(sxref,n,subst,uri)::tl) ->
148 let aattr = make_attributes [Some "helm","xref"] [axref] in
149 let sattr = make_attributes [Some "helm","xref";Some "xlink","href"] [sxref;uri] in
151 (let f = Hashtbl.find symbol_table n in
152 f tl ~priority ~assoc ~tail aattr sattr)
155 P.Mo([],"(")::P.Mi(sattr,n)::(make_args tl)@(P.Mo([],")")::tail)))
156 | CE.Appl (xref,l) as t ->
157 let attr = make_attributes [Some"helm","xref"] [xref] in
159 P.Mo([],"(")::(make_args l)@(P.Mo([],")")::tail))
160 | CE.Binder (xref, kind,(n,s),body) ->
161 let attr = make_attributes [Some "helm","xref"] [xref] in
163 if kind = "Lambda" then
164 Netconversion.ustring_of_uchar `Enc_utf8 0x03bb
165 else if kind = "Prod" then
166 Netconversion.ustring_of_uchar `Enc_utf8 0x03a0
167 else if kind = "Forall" then
168 Netconversion.ustring_of_uchar `Enc_utf8 0x2200
169 else if kind = "Exists" then
170 Netconversion.ustring_of_uchar `Enc_utf8 0x2203
173 P.Mtext([None,"mathcolor","Blue"],binder)::
174 P.Mtext([],n ^ ":")::
178 | CE.Letin (xref,(n,s),body) ->
179 let attr = make_attributes [Some "helm","xref"] [xref] in
181 P.Mtext([],("let "))::
182 P.Mtext([],(n ^ "="))::
186 | CE.Letrec (xref,defs,body) ->
187 let attr = make_attributes [Some "helm","xref"] [xref] in
192 [P.Mtext([],(n ^ "="));(aux body)]
194 P.Mtext([],(n ^ "="))::
195 (aux body)::P.Mtext([]," and")::(make_defs tl)) in
197 P.Mtext([],("let rec "))::
199 (P.Mtext([]," in ")::
201 | CE.Case (xref,a,np) ->
202 let attr = make_attributes [Some "helm","xref"] [xref] in
203 let rec make_patterns =
207 [P.Mtext([],(n ^ " -> "));(aux p)]
209 P.Mtext([],(n ^ " -> "))::
210 (aux p)::P.Mtext([]," | ")::(make_patterns tl)) in
212 P.Mtext([],("case "))::
214 P.Mtext([],(" of "))::
215 (make_patterns np)@tail) in
220 make_args ?(priority = 0) ?(assoc = false) ?(tail = []) =
221 let module P = Mpresentation in
224 | a::tl -> P.smallskip::(cexpr2pres a)::(make_args ~tail:tail tl)
227 let rec make_args_charcount ?(priority = 0) ?(assoc = false) ?(tail = []) =
228 let module P = Mpresentation in
232 [P.Mtr([],[P.Mtd([],P.indented (cexpr2pres_charcount ~tail:tail a))])]
234 let c = List.fold_left countterm 0 l in
236 P.Mtr([],[P.Mtd([],P.indented (cexpr2pres_charcount a))])::
237 (make_args_charcount ~tail:tail tl)
238 else [P.Mtr([],[P.Mtd([],P.Mrow([],(P.Mspace([None,"width","0.2cm"]))::(make_args ~tail:tail l)))])]
245 let c = List.fold_left countterm 0 tl in
247 P.Mtable ([("align","baseline 1");("equalrows","false");
248 ("columnalign","left")],
249 (make_args_charcount tl))
251 P.Mrow([], make_args tl) in
252 [P.Mtr([],[P.Mtd([],(cexpr2pres_charcount a))]);
253 P.Mtr([],[P.Mtd([],P.indented tlpres)])] *)
256 cexpr2pres_charcount ?(priority = 0) ?(assoc = false) ?(tail = []) t =
257 let module CE = Content_expressions in
258 let module P = Mpresentation in
261 CE.Symbol (xref,name,None,uri) ->
264 [Some "helm","xref";Some "xlink","href"] [xref;uri] in
267 else P.Mrow ([],P.Mi (attr,name)::tail)
268 | CE.Symbol (xref,name,Some subst,uri) ->
271 [Some "helm","xref";Some "xlink","href"] [xref;uri] in
278 P.Mi([],UriManager.name_of_uri uri)]
282 P.Mi([],UriManager.name_of_uri uri)::
290 (P.Mtext([],"]")::tail))
291 | CE.LocalVar (xref,name) ->
292 let attr = make_attributes [Some "helm","xref"] [xref] in
295 else P.Mrow ([],P.Mi (attr,name)::tail)
296 | CE.Meta (xref,name) ->
297 let attr = make_attributes [Some "helm","xref"] [xref] in
300 else P.Mrow ([],P.Mi (attr,name)::tail)
301 | CE.Num (xref,value) ->
302 let attr = make_attributes [Some "helm","xref"] [xref] in
305 else P.Mrow ([],P.Mn (attr,value)::tail)
306 | CE.Appl (axref,CE.Symbol(sxref,n,subst,uri)::tl) ->
307 let aattr = make_attributes [Some "helm","xref"] [axref] in
308 let sattr = make_attributes [Some "helm","xref";Some "xlink","href"] [sxref;uri] in
311 (let f = Hashtbl.find symbol_table_charcount n in
312 f tl ~priority ~assoc ~tail aattr sattr)
314 P.Mtable (aattr@P.standard_tbl_attr,
315 P.Mtr([],[P.Mtd([],P.Mrow([],
317 cexpr2pres (CE.Symbol(sxref,n,subst,uri))]))])::
318 make_args_charcount ~tail:(P.Mtext([],")")::tail) tl))
320 | CE.Appl (xref,l) as t ->
321 let attr = make_attributes [Some "helm","xref"] [xref] in
323 P.Mtable (attr@P.standard_tbl_attr,
324 P.Mtr([],[P.Mtd([],P.Mrow([],
326 cexpr2pres_charcount (List.hd l)]))])::
327 make_args_charcount ~tail:(P.Mtext([],")")::tail) (List.tl l))
329 | CE.Binder (xref, kind,(n,s),body) as t ->
331 let attr = make_attributes [Some "helm","xref"] [xref] in
333 if kind = "Lambda" then
334 Netconversion.ustring_of_uchar `Enc_utf8 0x03bb
335 else if kind = "Prod" then
336 Netconversion.ustring_of_uchar `Enc_utf8 0x03a0
337 else if kind = "Forall" then
338 Netconversion.ustring_of_uchar `Enc_utf8 0x2200
339 else if kind = "Exists" then
340 Netconversion.ustring_of_uchar `Enc_utf8 0x2203
342 P.Mtable (attr@P.standard_tbl_attr,
343 [P.Mtr ([],[P.Mtd ([],
345 [P.Mtext([None,"mathcolor","Blue"],binder);
347 cexpr2pres_charcount s ~tail:[P.Mtext([],".")]]))]);
348 P.Mtr ([],[P.Mtd ([],
349 P.indented (cexpr2pres_charcount body ~tail:tail))])])
350 else (cexpr2pres t ~tail:tail)
351 | CE.Letin (xref,(n,s),body) as t ->
353 let attr = make_attributes [Some "helm","xref"] [xref] in
354 P.Mtable (attr@P.standard_tbl_attr,
355 [P.Mtr ([],[P.Mtd ([],
357 [P.Mtext([None,"mathcolor","Blue"],"let");
360 cexpr2pres_charcount s;
364 P.Mtr ([],[P.Mtd ([],
365 P.indented (cexpr2pres_charcount body))])])
367 | CE.Letrec (xref,defs,body) ->
368 let attr = make_attributes [Some "helm","xref"] [xref] in
373 [P.Mtext([],(n ^ "="));(aux body)]
375 P.Mtext([],(n ^ "="))::
376 (aux body)::P.Mtext([]," and")::(make_defs tl)) in
378 P.Mtext([],("let rec "))::
382 | CE.Case (xref,a,np) ->
383 let attr = make_attributes [Some "helm","xref"] [xref] in
384 let rec make_patterns =
388 [P.Mtext([],(n ^ " -> "));(aux p)]
390 P.Mtext([],(n ^ " -> "))::
391 (aux p)::P.Mtext([]," | ")::(make_patterns tl)) in
393 P.Mtext([],("case "))::
395 P.Mtext([],(" of "))::
396 (make_patterns np)) in