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