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