]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/mpresentation.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / ocaml / cic_transformations / mpresentation.ml
1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
24  *)
25
26 (**************************************************************************)
27 (*                                                                        *)
28 (*                           PROJECT HELM                                 *)
29 (*                                                                        *)
30 (*                Andrea Asperti <asperti@cs.unibo.it>                    *)
31 (*                             16/62003                                   *)
32 (*                                                                        *)
33 (**************************************************************************)
34
35 type 
36   mpres = 
37   (* token elements *)
38     Mi of attr * string
39   | Mn of attr * string
40   | Mo of attr * string
41   | Mtext of attr * string
42   | Mspace of attr
43   | Ms of attr * string
44   | Mgliph of attr * string
45   (* General Layout Schemata *)
46   | Mrow of attr * mpres list
47   | Mfrac of attr * mpres * mpres
48   | Msqrt of attr * mpres
49   | Mroot of attr * mpres * mpres
50   | Mstyle of attr * mpres
51   | Merror of attr * mpres
52   | Mpadded of attr * mpres
53   | Mphantom of attr * mpres
54   | Mfenced of attr * mpres list
55   | Menclose of attr * mpres
56   (* Script and Limit Schemata *)
57   | Msub of attr * mpres * mpres
58   | Msup of attr * mpres * mpres
59   | Msubsup of attr * mpres * mpres *mpres 
60   | Munder of attr * mpres * mpres
61   | Mover of attr * mpres * mpres
62   | Munderover of attr * mpres * mpres *mpres 
63 (* | Multiscripts of ???  NOT IMPLEMEMENTED *)
64   (* Tables and Matrices *)
65   | Mtable of attr * row list
66   (* Enlivening Expressions *)
67   | Maction of attr * mpres list
68
69 and row = Mtr of attr * mtd list
70
71 and mtd = Mtd of attr * mpres
72
73 and attr = (string option * string * string) list
74 ;;
75
76 let smallskip = Mspace([None,"width","0.1cm"]);;
77 let indentation = Mspace([None,"width","0.3cm"]);;
78
79 let indented elem =
80   Mrow([],[indentation;elem]);;
81
82 let standard_tbl_attr = 
83   [None,"align","baseline 1";None,"equalrows","false";None,"columnalign","left"]
84 ;;
85
86 let two_rows_table attr a b =
87   Mtable(attr@standard_tbl_attr,
88     [Mtr([],[Mtd([],a)]);
89      Mtr([],[Mtd([],b)])]);;
90
91 let two_rows_table_with_brackets attr a b op =
92   (* only the open bracket is added; the closed bracket must be in b *)
93   Mtable(attr@standard_tbl_attr,
94     [Mtr([],[Mtd([],Mrow([],[Mtext([],"(");a]))]);
95      Mtr([],[Mtd([],Mrow([],[indentation;op;b]))])]);;
96
97 let two_rows_table_without_brackets attr a b op =
98   Mtable(attr@standard_tbl_attr,
99     [Mtr([],[Mtd([],a)]);
100      Mtr([],[Mtd([],Mrow([],[indentation;op;b]))])]);;
101
102 let row_with_brackets attr a b op =
103   (* by analogy with two_rows_table_with_brackets we only add the
104      open brackets *)
105   Mrow(attr,[Mtext([],"(");a;op;b])
106
107 let row_without_brackets attr a b op =
108   Mrow(attr,[a;op;b])
109
110 (* MathML prefix *)
111 let prefix = "m";;
112  
113 let rec print_mpres =
114  let module X = Xml in
115   function
116     Mi (attr,s) -> X.xml_nempty ~prefix "mi" attr (X.xml_cdata s)
117   | Mn (attr,s) -> X.xml_nempty ~prefix "mn" attr (X.xml_cdata s)
118   | Mo (attr,s) -> X.xml_nempty ~prefix "mo" attr (X.xml_cdata s)
119   | Mtext (attr,s) -> X.xml_nempty ~prefix "mtext" attr (X.xml_cdata s)
120   | Mspace attr -> X.xml_empty ~prefix "mspace" attr
121   | Ms (attr,s) -> X.xml_nempty ~prefix "ms" attr (X.xml_cdata s)
122   | Mgliph (attr,s) -> X.xml_nempty ~prefix "mgliph" attr (X.xml_cdata s)
123   (* General Layout Schemata *)
124   | Mrow (attr,l) ->
125       X.xml_nempty ~prefix "mrow" attr 
126          [< (List.fold_right (fun x i -> [< (print_mpres x) ; i >]) l [<>])
127           >]
128   | Mfrac (attr,m1,m2) ->
129        X.xml_nempty ~prefix "mfrac" attr 
130          [< print_mpres m1;
131             print_mpres m2
132          >]
133   | Msqrt (attr,m) ->
134        X.xml_nempty ~prefix "msqrt" attr [< print_mpres m >]
135   | Mroot  (attr,m1,m2) ->
136        X.xml_nempty ~prefix "mroot" attr 
137          [< print_mpres m1;
138             print_mpres m2
139          >]
140   | Mstyle (attr,m) ->
141        X.xml_nempty ~prefix "mstyle" attr [< print_mpres m >]
142   | Merror (attr,m) ->
143        X.xml_nempty ~prefix "merror" attr [< print_mpres m >]
144   | Mpadded (attr,m) ->
145        X.xml_nempty ~prefix "mpadded" attr [< print_mpres m >]
146   | Mphantom (attr,m) ->
147        X.xml_nempty ~prefix "mphantom" attr [< print_mpres m >]
148   | Mfenced (attr,l) ->
149       X.xml_nempty ~prefix "mfenced" attr 
150          [< (List.fold_right (fun x i -> [< (print_mpres x) ; i >]) l [<>])
151           >]
152   | Menclose (attr,m) ->
153       X.xml_nempty ~prefix "menclose" attr [< print_mpres m >]
154   (* Script and Limit Schemata *)
155   | Msub (attr,m1,m2) ->
156       X.xml_nempty ~prefix "msub" attr 
157          [< print_mpres m1;
158             print_mpres m2
159          >]
160   | Msup (attr,m1,m2) ->
161       X.xml_nempty ~prefix "msup" attr 
162          [< print_mpres m1;
163             print_mpres m2
164          >]
165   | Msubsup (attr,m1,m2,m3) ->
166       X.xml_nempty ~prefix "msubsup" attr 
167          [< print_mpres m1;
168             print_mpres m2;
169             print_mpres m3
170          >]
171   | Munder (attr,m1,m2) ->
172       X.xml_nempty ~prefix "munder" attr 
173          [< print_mpres m1;
174             print_mpres m2
175          >]
176   | Mover (attr,m1,m2) ->
177       X.xml_nempty ~prefix "mover" attr 
178          [< print_mpres m1;
179             print_mpres m2
180          >]
181   | Munderover (attr,m1,m2,m3) ->
182       X.xml_nempty ~prefix "munderover" attr 
183          [< print_mpres m1;
184             print_mpres m2;
185             print_mpres m3
186          >]
187 (* | Multiscripts of ???  NOT IMPLEMEMENTED *)
188   (* Tables and Matrices *)
189   | Mtable (attr, rl) ->
190       X.xml_nempty ~prefix "mtable" attr 
191          [< (List.fold_right (fun x i -> [< (print_mrow x) ; i >]) rl [<>])
192           >]
193   (* Enlivening Expressions *)
194   | Maction (attr, l) ->
195       X.xml_nempty ~prefix "maction" attr 
196          [< (List.fold_right (fun x i -> [< (print_mpres x) ; i >]) l [<>])
197           >]
198
199 and print_mrow =
200  let module X = Xml in
201  function 
202     Mtr (attr, l) -> 
203       X.xml_nempty ~prefix "mtr" attr 
204          [< (List.fold_right (fun x i -> [< (print_mtd x) ; i >]) l [<>])
205           >]
206
207 and print_mtd =
208   let module X = Xml in
209   function 
210      Mtd (attr,m) -> X.xml_nempty ~prefix "mtd" attr [< (print_mpres m) ; X.xml_nempty ~prefix "mphantom" [] (X.xml_nempty ~prefix "mtext" [] (X.xml_cdata "(")) >]
211 ;;
212
213 let print_mpres pres =
214  [< Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
215     Xml.xml_cdata "\n";
216     Xml.xml_nempty ~prefix "math"
217      [Some "xmlns","m","http://www.w3.org/1998/Math/MathML" ;
218       Some "xmlns","helm","http://www.cs.unibo.it/helm" ;
219       Some "xmlns","xlink","http://www.w3.org/1999/xlink"
220      ] (Xml.xml_nempty ~prefix "mstyle" [None, "mathvariant", "normal"; None, "rowspacing", "0.6ex"] (print_mpres pres))
221  >]
222
223