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