]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/content_pres/cicNotationPres.ml
test branch
[helm.git] / helm / ocaml / content_pres / cicNotationPres.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 (* $Id$ *)
27
28 module Ast = CicNotationPt
29 module Mpres = Mpresentation
30
31 type mathml_markup = boxml_markup Mpres.mpres
32 and boxml_markup = mathml_markup Box.box
33
34 type markup = mathml_markup
35
36 let atop_attributes = [None, "linethickness", "0pt"]
37
38 let to_unicode = Utf8Macro.unicode_of_tex
39
40 let rec make_attributes l1 = function
41   | [] -> []
42   | hd :: tl ->
43       (match hd with
44       | None -> make_attributes (List.tl l1) tl
45       | Some s ->
46           let p,n = List.hd l1 in
47           (p,n,s) :: make_attributes (List.tl l1) tl)
48
49 let box_of_mpres =
50   function
51   | Mpresentation.Mobject (attrs, box) ->
52       assert (attrs = []);
53       box
54   | mpres -> Box.Object ([], mpres)
55
56 let mpres_of_box =
57   function
58   | Box.Object (attrs, mpres) ->
59       assert (attrs = []);
60       mpres
61   | box -> Mpresentation.Mobject ([], box)
62
63 let rec genuine_math =
64   function
65   | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
66   | _ -> true
67 and genuine_box =
68   function
69   | Box.Object ([], mpres) -> not (genuine_math mpres)
70   | _ -> true
71
72 let rec eligible_math =
73   function
74   | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
75   | Mpresentation.Mobject ([], _) -> false
76   | _ -> true
77
78 let rec promote_to_math =
79   function
80   | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
81   | math -> math
82
83 let small_skip =
84   Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
85
86 let rec add_mpres_attributes new_attr = function
87   | Mpresentation.Mobject (attr, box) ->
88       Mpresentation.Mobject (attr, add_box_attributes new_attr box)
89   | mpres ->
90       Mpresentation.set_attr (new_attr @ Mpresentation.get_attr mpres) mpres
91 and add_box_attributes new_attr = function
92   | Box.Object (attr, mpres) ->
93       Box.Object (attr, add_mpres_attributes new_attr mpres)
94   | box -> Box.set_attr (new_attr @ Box.get_attr box) box
95
96 let box_of mathonly spec attrs children =
97   match children with
98     | [t] -> add_mpres_attributes attrs t
99     | _ ->
100         let kind, spacing, indent = spec in
101         let dress children =
102           if spacing then
103             CicNotationUtil.dress small_skip children
104           else
105             children
106         in
107           if mathonly then Mpresentation.Mrow (attrs, dress children)
108           else
109             let attrs' =
110               (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
111               @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
112               @ attrs
113             in
114               match kind with
115                 | Ast.H ->
116                     if List.for_all eligible_math children then
117                       Mpresentation.Mrow (attrs',
118                         dress (List.map promote_to_math children))
119                     else
120                       mpres_of_box (Box.H (attrs',
121                         List.map box_of_mpres children))
122 (*                 | Ast.H when List.for_all genuine_math children ->
123                     Mpresentation.Mrow (attrs', dress children) *)
124                 | Ast.V ->
125                     mpres_of_box (Box.V (attrs',
126                       List.map box_of_mpres children))
127                 | Ast.HV ->
128                     mpres_of_box (Box.HV (attrs',
129                       List.map box_of_mpres children))
130                 | Ast.HOV ->
131                     mpres_of_box (Box.HOV (attrs',
132                       List.map box_of_mpres children))
133
134 let open_paren        = Mpresentation.Mo ([], "(")
135 let closed_paren      = Mpresentation.Mo ([], ")")
136 let open_brace        = Mpresentation.Mo ([], "{")
137 let closed_brace      = Mpresentation.Mo ([], "}")
138 let hidden_substs     = Mpresentation.Mtext ([], "{...}")
139 let open_box_paren    = Box.Text ([], "(")
140 let closed_box_paren  = Box.Text ([], ")")
141 let semicolon         = Mpresentation.Mo ([], ";")
142 let toggle_action children =
143   Mpresentation.Maction ([None, "actiontype", "toggle"], children)
144
145 type child_pos = [ `Left | `Right | `Inner ]
146
147 let pp_assoc =
148   function
149   | Gramext.LeftA -> "LeftA"
150   | Gramext.RightA -> "RightA"
151   | Gramext.NonA -> "NonA"
152
153 let is_atomic t =
154   let rec aux_mpres = function
155     | Mpres.Mi _
156     | Mpres.Mo _
157     | Mpres.Mn _
158     | Mpres.Ms _
159     | Mpres.Mtext _
160     | Mpres.Mspace _ -> true
161     | Mpres.Mobject (_, box) -> aux_box box
162     | Mpres.Maction (_, [mpres])
163     | Mpres.Mrow (_, [mpres]) -> aux_mpres mpres
164     | _ -> false
165   and aux_box = function
166     | Box.Space _
167     | Box.Ink _
168     | Box.Text _ -> true
169     | Box.Object (_, mpres) -> aux_mpres mpres
170     | Box.H (_, [box])
171     | Box.V (_, [box])
172     | Box.HV (_, [box])
173     | Box.HOV (_, [box])
174     | Box.Action (_, [box]) -> aux_box box
175     | _ -> false
176   in
177   aux_mpres t
178
179 let add_parens child_prec child_assoc child_pos curr_prec t =
180   if is_atomic t then t
181   else if child_prec >= 0
182     && (child_prec < curr_prec
183       || (child_prec = curr_prec &&
184           child_assoc = Gramext.LeftA &&
185           child_pos = `Right)
186       || (child_prec = curr_prec &&
187           child_assoc = Gramext.RightA &&
188           child_pos = `Left))
189   then  (* parens should be added *)
190 (*     (prerr_endline "adding parens";
191     prerr_endline (Printf.sprintf "child_prec = %d\nchild_assoc = %s\nchild_pos = %s\ncurr_prec= %d"
192       child_prec (pp_assoc child_assoc) (CicNotationPp.pp_pos
193       child_pos) curr_prec); *)
194     match t with
195     | Mpresentation.Mobject (_, box) ->
196         mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
197     | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
198   else
199     t
200
201 let render ids_to_uris =
202   let module A = Ast in
203   let module P = Mpresentation in
204 (*   let use_unicode = true in *)
205   let lookup_uri id =
206     (try
207       let uri = Hashtbl.find ids_to_uris id in
208       Some (UriManager.string_of_uri uri)
209     with Not_found -> None)
210   in
211   let make_href xmlattrs xref =
212     let xref_uris =
213       List.fold_right
214         (fun xref uris ->
215           match lookup_uri xref with
216           | None -> uris
217           | Some uri -> uri :: uris)
218         !xref []
219     in
220     let xmlattrs_uris, xmlattrs =
221       let xref_attrs, other_attrs =
222         List.partition
223           (function Some "xlink", "href", _ -> true | _ -> false)
224           xmlattrs
225       in
226       List.map (fun (_, _, uri) -> uri) xref_attrs,
227       other_attrs
228     in
229     let uris =
230       match xmlattrs_uris @ xref_uris with
231       | [] -> None
232       | uris ->
233           Some (String.concat " "
234             (HExtlib.list_uniq (List.sort String.compare uris)))
235     in
236     let xrefs =
237       match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
238     in
239     xref := [];
240     xmlattrs
241     @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
242         [xrefs; uris]
243   in
244   let make_xref xref =
245     let xrefs =
246       match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
247     in
248     xref := [];
249     make_attributes [Some "helm","xref"] [xrefs]
250   in
251   (* when mathonly is true no boxes should be generated, only mrows *)
252   (* "xref" is  *)
253   let rec aux xmlattrs mathonly xref pos prec t =
254     match t with
255     | A.AttributedTerm _ ->
256         aux_attributes xmlattrs mathonly xref pos prec t
257     | A.Num (literal, _) ->
258         let attrs =
259           (RenderingAttrs.number_attributes `MathML)
260           @ make_href xmlattrs xref
261         in
262         Mpres.Mn (attrs, literal)
263     | A.Symbol (literal, _) ->
264         let attrs =
265           (RenderingAttrs.symbol_attributes `MathML)
266           @ make_href xmlattrs xref
267         in
268         Mpres.Mo (attrs, to_unicode literal)
269     | A.Ident (literal, subst)
270     | A.Uri (literal, subst) ->
271         let attrs =
272           (RenderingAttrs.ident_attributes `MathML)
273           @ make_href xmlattrs xref
274         in
275         let name = Mpres.Mi (attrs, to_unicode literal) in
276         (match subst with
277         | Some []
278         | None -> name
279         | Some substs ->
280             let substs' =
281               box_of mathonly (A.H, false, false) []
282                 (open_brace
283                 :: (CicNotationUtil.dress semicolon
284                     (List.map
285                       (fun (name, t) ->
286                         box_of mathonly (A.H, false, false) [] [
287                           Mpres.Mi ([], name);
288                           Mpres.Mo ([], to_unicode "\\def");
289                           aux [] mathonly xref pos prec t ])
290                       substs))
291                 @ [ closed_brace ])
292             in
293             let substs_maction = toggle_action [ hidden_substs; substs' ] in
294             box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
295     | A.Literal l -> aux_literal xmlattrs xref prec l
296     | A.UserInput -> Mpres.Mtext ([], "%")
297     | A.Layout l -> aux_layout mathonly xref pos prec l
298     | A.Magic _
299     | A.Variable _ -> assert false  (* should have been instantiated *)
300     | t ->
301         prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
302         assert false
303   and aux_attributes xmlattrs mathonly xref pos prec t =
304     let reset = ref false in
305     let new_level = ref None in
306     let new_xref = ref [] in
307     let new_xmlattrs = ref [] in
308     let new_pos = ref pos in
309 (*     let reinit = ref false in *)
310     let rec aux_attribute =
311       function
312       | A.AttributedTerm (attr, t) ->
313           (match attr with
314           | `Loc _
315           | `Raw _ -> ()
316           | `Level (-1, _) -> reset := true
317           | `Level (child_prec, child_assoc) ->
318               new_level := Some (child_prec, child_assoc)
319           | `IdRef xref -> new_xref := xref :: !new_xref
320           | `ChildPos pos -> new_pos := pos
321           | `XmlAttrs attrs -> new_xmlattrs := attrs @ !new_xmlattrs);
322           aux_attribute t
323       | t ->
324           (match !new_level with
325           | None -> aux !new_xmlattrs mathonly new_xref !new_pos prec t
326           | Some (child_prec, child_assoc) ->
327               let t' = 
328                 aux !new_xmlattrs mathonly new_xref !new_pos child_prec t
329               in
330               if !reset then t'
331               else add_parens child_prec child_assoc !new_pos prec t')
332     in
333     aux_attribute t
334   and aux_literal xmlattrs xref prec l =
335     let attrs = make_href xmlattrs xref in
336     (match l with
337     | `Symbol s -> Mpres.Mo (attrs, to_unicode s)
338     | `Keyword s -> Mpres.Mo (attrs, to_unicode s)
339     | `Number s  -> Mpres.Mn (attrs, to_unicode s))
340   and aux_layout mathonly xref pos prec l =
341     let attrs = make_xref xref in
342     let invoke' t = aux [] true (ref []) pos prec t in
343       (* use the one below to reset precedence and associativity *)
344     let invoke_reinit t = aux [] mathonly xref `Inner ~-1 t in
345     match l with
346     | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke_reinit t2)
347     | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke_reinit t2)
348     | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke_reinit t2)
349     | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke_reinit t2)
350     | A.Frac (t1, t2)
351     | A.Over (t1, t2) ->
352         Mpres.Mfrac (attrs, invoke_reinit t1, invoke_reinit t2)
353     | A.Atop (t1, t2) ->
354         Mpres.Mfrac (atop_attributes @ attrs, invoke_reinit t1,
355           invoke_reinit t2)
356     | A.Sqrt t -> Mpres.Msqrt (attrs, invoke_reinit t)
357     | A.Root (t1, t2) ->
358         Mpres.Mroot (attrs, invoke_reinit t1, invoke_reinit t2)
359     | A.Box ((_, spacing, _) as kind, terms) ->
360         let children =
361           aux_children mathonly spacing xref pos prec
362             (CicNotationUtil.ungroup terms)
363         in
364         box_of mathonly kind attrs children
365     | A.Group terms ->
366         let children =
367           aux_children mathonly false xref pos prec
368             (CicNotationUtil.ungroup terms)
369         in
370         box_of mathonly (A.H, false, false) attrs children
371     | A.Break -> assert false (* TODO? *)
372   and aux_children mathonly spacing xref pos prec terms =
373     let find_clusters =
374       let rec aux_list first clusters acc =
375         function
376             [] when acc = [] -> List.rev clusters
377           | [] -> aux_list first (List.rev acc :: clusters) [] []
378           | (A.Layout A.Break) :: tl when acc = [] ->
379               aux_list first clusters [] tl
380           | (A.Layout A.Break) :: tl ->
381               aux_list first (List.rev acc :: clusters) [] tl
382           | [hd] ->
383 (*               let pos' = 
384                 if first then
385                   pos
386                 else
387                   match pos with
388                       `None -> `Right
389                     | `Inner -> `Inner
390                     | `Right -> `Right
391                     | `Left -> `Inner
392               in *)
393                 aux_list false clusters
394                   (aux [] mathonly xref pos prec hd :: acc) []
395           | hd :: tl ->
396 (*               let pos' =
397                 match pos, first with
398                     `None, true -> `Left
399                   | `None, false -> `Inner
400                   | `Left, true -> `Left
401                   | `Left, false -> `Inner
402                   | `Right, _ -> `Inner
403                   | `Inner, _ -> `Inner
404               in *)
405                 aux_list false clusters
406                   (aux [] mathonly xref pos prec hd :: acc) tl
407       in
408         aux_list true [] []
409     in
410     let boxify_pres =
411       function
412           [t] -> t
413         | tl -> box_of mathonly (A.H, spacing, false) [] tl
414     in
415       List.map boxify_pres (find_clusters terms)
416   in
417   aux [] false (ref []) `Inner ~-1
418
419 let rec print_box (t: boxml_markup) =
420   Box.box2xml print_mpres t
421 and print_mpres (t: mathml_markup) =
422   Mpresentation.print_mpres print_box t
423
424 let print_xml = print_mpres
425
426 (* let render_to_boxml id_to_uri t =
427   let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
428   Xml.add_xml_declaration xml_stream *)
429