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