mathml.tex 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. \documentclass{article}
  2. \title{REDUCE-MathML Interface}
  3. \author{Luis Alvarez-Sobreviela \\ alvarez@zib.de \\ \\
  4. Konrad--Zuse--Zentrum f\"ur Informationstechnik Berlin \\
  5. Takustra\ss e 7 \\ D-14195 Berlin-Dahlem}
  6. \date{August 9, 1999}
  7. \begin{document}
  8. \maketitle
  9. \section{Introduction}
  10. MathML is intended to facilitate the use and re-use of mathematical and
  11. scientific content on the Web, and for other applications such as computer
  12. algebra systems. For this reason we believe MathML is an important step
  13. for the scientific community considering the widespread use of the
  14. Internet. We found necessary to include REDUCE in this trend, and
  15. developed the MathML-REDUCE interface.
  16. The MathML interface for REDUCE provides an easy to use series of
  17. commands, allowing it to evaluate and output MathML. This manual is
  18. therefore intended to give the user a good guideline on how to make of it a
  19. proper use.
  20. \\
  21. The principal features of this package can be resumed as:
  22. \begin{itemize}
  23. \item Evaluation of MathML code. Allows REDUCE to parse MathML expressions
  24. and evaluate them.
  25. \item Generation of MathML compliant code. Provides the printing of REDUCE
  26. expressions in MathML source code, to be used directly in web page
  27. production.
  28. \item Generation of MathML code surrounded by HTML $<$embed$>$$<$/embed$>$
  29. tags. This is useful for directly including MathML into a webpage. It can
  30. then easily be rendered on any browser with the IBM Tech Explorer plug-in
  31. \footnote{Currently available at
  32. http://www.software.ibm.com/network/techexplorer/}.
  33. \end{itemize}
  34. We assume that the reader is familiar with MathML 1.0. If not, the
  35. specification. is available at: \\
  36. {\tt http://www.w3.org/Math/ }
  37. \section{Getting Started}
  38. \subsection{Loading}
  39. The MathML-REDUCE interface package is under the name {\tt mathml}, and so is
  40. loaded by supplying {\tt load mathml;}.
  41. \subsection{Switches}
  42. There are three switches which can be used alternatively and
  43. incrementally. These are {\bf mathml}, {\bf web} and {\bf both}. There
  44. use can be described as follows:
  45. \begin{description}
  46. \item[{\tt mathml}:] All output will be printed in MathML.
  47. \item[{\tt both}:] All output will be printed in both MathML and normal
  48. REDUCE.
  49. \item[{\tt web}:] All output will be printed within an HTML $<embed>$ tag.
  50. This is for direct use in an HTML web page when using IBM Tech Explorer.
  51. \end{description}
  52. MathML has often been said to be too verbose. If {\bf both} is on, an easy
  53. interpretation of the results is possible, improving MathML readability.
  54. \subsection{Entering MathML}
  55. The MathML-REDUCE interface gives the user various ways of providing
  56. input. This can be done via a file containing MathML or by writing MathML
  57. directly in the prompt. \\
  58. \subsubsection{Reading MathML from a File: {\tt MML}}
  59. When reading from a file the command {\bf mml} is used. {\bf mml} takes as
  60. argument the name of the file containing the MathML. \\
  61. {\tt mml}(FILE:{\it string}):{\it expression}
  62. \paragraph{Example:} As long as the file given contains valid MathML, no
  63. errors should be produced.
  64. {\tt 1: mml "ex.mml";}
  65. \subsubsection{Reading MathML from Prompt: {\tt PARSEML}}
  66. By using the function {\bf parseml} it is possible to introduce a series of
  67. valid mathml tokens to the prompt. {\bf parseml} takes no arguments,
  68. although once it is called it will prompt you to enter mathml tags starting
  69. with {\tt $<$mathml$>$} and ending with {\tt $<$/mathml$>$}. It then returns
  70. an expression resulting from evaluating the input. \\
  71. \paragraph{Example:} Here is an extract of a REDUCE session where {\tt
  72. parseml()} is used:\\
  73. {\tt 2: parseml();
  74. 2: <math>
  75. 2: <apply><plus/>
  76. 2: <cn>3</cn>
  77. 2: <cn>5</cn>
  78. 2: </apply>
  79. 2: </math>
  80. \\
  81. \hspace*{1cm} 8 \\
  82. 3: } \\
  83. If for some reason you do not want to continue typing in at the prompt, and
  84. cancel what has already been typed, it is possible to exit by calling
  85. control {\tt C} or control {\tt D}.
  86. Although it is simpler to edit a file and then use {\tt mml}, we still
  87. added {\bf parseml} for completeness.
  88. \section{The Evaluation of MathML}
  89. MathML is evaluated by the interface always before outputting any results.
  90. Just in the same way as REDUCE normally does. The program works in the same
  91. way as the {\tt algebraic} mode. Undefined variables remain undefined,
  92. and it is possible to use all normal REDUCE switches and packages.
  93. \paragraph{Example:}
  94. \noindent The following MathML:\\
  95. \\
  96. {\tt
  97. \hspace*{0mm}<math>
  98. \hspace*{1mm}<reln><gt/>
  99. \hspace*{6mm}<ci>x</ci>
  100. \hspace*{6mm}<ci>y</ci>
  101. \hspace*{1mm}</reln>
  102. \\
  103. \hspace*{0mm}</math>}
  104. \\
  105. \\
  106. will evaluate to {\tt gt(x,y)}. This only states that x is greater than
  107. y.
  108. \\
  109. The interesting characteristic, is that we can set the undefined values of
  110. x and y.
  111. \\
  112. Suppose we enter the following:
  113. \\
  114. {\tt 5: x:=3; y:=2;
  115. \\
  116. x := 3
  117. \\
  118. y := 2}
  119. \\
  120. \\
  121. If we once again enter and evaluate the above piece of MathML we will have as
  122. result:
  123. \\
  124. {\tt t}
  125. \\
  126. \\
  127. because it is true that 3 is greater than 2. It is important to note that
  128. it is also possible to set only one of the two variables x or y above, say
  129. {\tt y:=4}. The expression will then not evaluate completely, and we will
  130. have: \\
  131. {\tt gt(x,4)}
  132. \\
  133. \\
  134. When one of the switches is on, the MathML output will be:
  135. \\
  136. \\
  137. {\tt
  138. \hspace*{1mm}<math>\\
  139. \hspace*{5mm} <reln><gt/>\\
  140. \hspace*{9mm} <ci>x</ci>\\
  141. \hspace*{9mm} <cn type="integer">4</cn>\\
  142. \hspace*{5mm} </reln>\\
  143. \hspace*{1mm}</math>\\}
  144. Hence, it is possible when dealing with a MathML formula representation in
  145. which there are a set of undefined variables to set each variable to the
  146. desired value and evaluate it. Let us consider a second example to make sure
  147. everything is clear.
  148. \paragraph{Example:}
  149. Let the file {\tt ex.mml} contain the following mathml:
  150. \\
  151. \\
  152. {\tt <math>\\
  153. \hspace*{2mm} <apply><int/>\\
  154. \hspace*{5mm} <bvar>\\
  155. \hspace*{9mm} <ci>x</ci>\\
  156. \hspace*{5mm} </bvar>\\
  157. \hspace*{5mm} <apply><fn><ci>F</ci><fn>\\
  158. \hspace*{9mm} <ci>x</ci>\\
  159. \hspace*{5mm} </apply>\\
  160. \hspace*{2mm} </apply>\\
  161. </math>} \\
  162. \\
  163. If we do the following:\\
  164. {\tt 1: mml "ex.mml";}\\
  165. \\
  166. This is what we get:
  167. \\
  168. \\
  169. {\tt int(f(x),x);}\\
  170. \\
  171. It is clear that this has remained unevaluated. We can now set the function
  172. {\tt f(x)} as follows:\\
  173. {\tt for all x let f(x)=2*x**2;}\\
  174. \\
  175. If we then enter '{\tt mml "ex.mml"}' once again, we will have the
  176. following result:\\
  177. {\Large \( \frac {2*x^{3}}{3}\)}
  178. \\
  179. \\
  180. Hence the MathML-REDUCE interface allows the user to set a value to a
  181. variable in order to manipulate the evaluation of the MathML, without needing
  182. to edit the MathML itself.
  183. \subsection{Using Boolean Values}
  184. Boolean values are not defined in MathML, despite their importance in REDUCE.
  185. To get around this problem, we can set a variable's value to a boolean value,
  186. and when evaluating, the MathML-REDUCE interface will use the boolean value
  187. of the variable.
  188. \\
  189. Suppose we want to evaluate the following expression:
  190. \\
  191. \\
  192. {\tt and(t,nil);}
  193. \\
  194. \\
  195. Then all we do, is we create a file with the following MathML:
  196. \\
  197. \\
  198. {\tt $<$mathml$>$\\
  199. \hspace*{2mm} $<$apply$>$$<$and/$>$\\
  200. \hspace*{5mm} $<$ci$>$ a $<$/ci$>$\\
  201. \hspace*{5mm} $<$ci$>$ b $<$/ci$>$\\
  202. \hspace*{2mm} $<$/apply$>$\\
  203. $<$/mathml$>$\\}
  204. \\
  205. And before evaluating it, we set {\tt a} to {\tt true} and {\tt b} to {\tt
  206. nil}. When evaluating the MathML, it will produce the same result as the
  207. equivalent REDUCE expression.
  208. \section{Interpretation of Error Messages}
  209. The MathML-REDUCE interface has a set of error messages which aim to help the
  210. user understand and correct any invalid MathML. Because there can exist many
  211. different causes of errors, such error messages should be considered merely
  212. as advice. Here we shall consider the most important error messages.
  213. \paragraph{Missing tag:}
  214. Many MathML tags go by pairs, such as {\tt $<$apply$>$$<$/apply$>$,
  215. $<$reln$>$$<$/reln$>$}, {\tt $<$ci$>$$<$/ci$>$}, etc\ldots. In the
  216. case
  217. where the ending tag is missed out, or misspelled, it is very probable that an
  218. error of this type will be thrown.
  219. \paragraph{Ambiguous or Erroneous Use of {\tt $<$apply$>$}}
  220. This error message defines an undefined error. When this error message
  221. appears, it is not very clear to the interface where exactly lies the error.
  222. Probable causes are a misuse of the {\tt $<$apply$>$$<$/apply$>$} tags, or a
  223. mispelling of the tag preceding the {\tt $<$apply$>$}. However, other types
  224. of errors may cause this error message to appear.
  225. Tags following an {\tt $<$apply$>$} tag may be misspelled without causing an
  226. error message, but they will be considered as operators by REDUCE and
  227. therefore evaluate to some unexpected expression.
  228. \paragraph{Syntax Errors}
  229. It is possible that the input MathML is not syntactically correct. In such
  230. a situation, the error will be spotted, and in some cases a solution might be
  231. presented. There are a variety of syntax errors messages, but relying on
  232. their advice might not always be helpful.
  233. \\
  234. Despite the verbose nature of the error messages and their recommended
  235. solutions, we do recommend that in most situations reference to the MathML
  236. specification is made.
  237. \section{Limitations of the Interface}
  238. Not all aspects of MathML have been perfectly fitted into the interface.
  239. There are still some problems unsolved in the present version of the
  240. interface:
  241. \begin{itemize}
  242. \item MathML Presentation Markup is not supported. The
  243. interface will treat every presentation tag as an unknown tag, or a REDUCE
  244. operator. We found presentation markup not prioritary when dealing with
  245. computer algebra systems, although in the future, parsing of presentation
  246. markup within content markup shall be supported.
  247. \item Certain MathML tags
  248. do not play an important role in the REDUCE environment. Such tags will not
  249. evaluate or affect in anyway the interface's behaviour. They will be parsed
  250. correctly, although their action will be ignored. These tags are:
  251. \begin{enumerate}
  252. {\tt
  253. \item $<$interval$><$/interval$>$
  254. \item $<$inverse$><$/inverse$>$
  255. \item $<$condition$><$/condition$>$
  256. \item $<$compose$><$/compose$>$
  257. \item $<$ident$><$/ident$>$
  258. \item $<$forall/$>$
  259. \item $<$exists/$>$}
  260. \end{enumerate}
  261. Although {\tt $<$condition$><$/condition$>$} and {\tt
  262. $<$interval$><$/interval$>$} tags are supported when used within the
  263. following tags:
  264. \begin{enumerate}
  265. {\tt
  266. \item $<$int/$>$
  267. \item $<$limit/$>$
  268. \item $<$sum/$>$
  269. \item $<$product/$>$}
  270. \end{enumerate}
  271. \item The {\tt $<$declare$>$} construct takes one or two arguments. It sets
  272. the first argument to the value of the second. In the case where the second
  273. argument is a vector or a matrix, an obscure error message is produced. It is
  274. clearly something which must be fixed in the future.\\ \item The program
  275. throws an error when it encounters {\tt nil} between {\tt $<$ci$><$/ci$>$}
  276. tags. It is not possible to use boolean values directly. Please refer to the
  277. above subsection treating this matter.
  278. \end{itemize}
  279. \section{Including MathML into a Webpage}
  280. We shall not get into the details of including MathML in the design of a
  281. web page, but it is important for completeness, to give a quick overview.
  282. One can include MathML in two ways:
  283. \begin{itemize}
  284. \item By introducing MathML inside $<$math$>$$<$/math$>$ tags in any HTML
  285. page.
  286. \item By having MathML being embeded inside HTML $<$embed$>$$<$/embed$>$
  287. tags.
  288. \end{itemize}
  289. When one has the {\bf mathml} switch on, MathML is generated inside
  290. $<$math$>$$<$/math$>$ tags. Browsers such as WebEQ \footnote{Available at
  291. http://www.webeq.com}can read this type of encoding.
  292. \paragraph{Example:}
  293. \begin{verbatim}
  294. <html>
  295. <body>
  296. <title>MathML example</title>
  297. <h1>Example of MathML</h1>
  298. <br>
  299. The following is MathML directly encoded within this homepage:
  300. <br><br>
  301. <math>
  302. <apply><int>
  303. <bvar>
  304. <ci>x</ci>
  305. </bvar>
  306. <apply><power/>
  307. <ci>x</ci>
  308. <apply><power/>
  309. <ci>x</ci>
  310. <cn type="integer">2</cn>
  311. </apply>
  312. </apply>
  313. </apply>
  314. </math>
  315. <br><br>
  316. On some browsers such as WebEQ, the MathML is rendered correctly.
  317. </html>
  318. \end{verbatim}
  319. But most people on the web use either Netscape or Microsoft Internet
  320. Explorer, which do not at the moment support complete MathML rendering.
  321. What must be done in cases where Netscape or Microsoft Internet Explorer
  322. are used, is to make available the IBM Tech Explorer Plug-in, and include
  323. the MathML inside the web page surrounded by $<$embed$>$$<$/embed$>$ tags.
  324. This is easily done with the REDUCE-MathML interface by having the {\bf
  325. web} switch on. Here is an example of how to do this:
  326. \paragraph{Example}
  327. \begin{verbatim}
  328. <html>
  329. <body>
  330. <title>MathML example</title>
  331. <h1>Example of MathML</h1>
  332. <br>
  333. The following is MathML directly encoded within this homepage:
  334. <br><br>
  335. <EMBED TYPE="text/mathml" MMLDATA="<math><apply><int><bvar><ci>x</ci></bvar>
  336. <apply><power/><ci>x</ci><apply><power/><ci>x</ci><cn type=&quot;integer&quot;>
  337. 2 </cn></apply></apply></apply></math>"
  338. HEIGHT=300 WIDTH=200>
  339. <br><br>
  340. On all browsers with the Tech Explorer Plug-in, this is rendered
  341. correctly.
  342. </html>
  343. \end{verbatim}
  344. Another way to embed MathML in a web page document is by having a
  345. separate file contain MathML (usually a file with {\bf .mml} extension)
  346. and to embed it inside a page with the following code:
  347. \begin{verbatim}
  348. <EMBED TYPE="text/mathml" src="mathml.mml" HEIGHT=300 WIDTH=200>
  349. \end{verbatim}
  350. \section{Examples}
  351. We would like to present a series of examples which will illustrate the
  352. possibilities of the interface.
  353. \paragraph{Example 1} Type in the following and observe the resulting
  354. expression:
  355. \begin{verbatim}
  356. 23: on mathml;
  357. 24: solve({z=x*a+1},{z,x});
  358. \end{verbatim}
  359. \paragraph{Example 2}Have a file {\tt ex2.mml} containing the following
  360. MathML source code:\\
  361. \\
  362. {\tt $<$mathml$>$\\
  363. \hspace*{1mm} $<$apply$>$$<$sum/$>$\\
  364. \hspace*{5mm} $<$bvar$>$\\
  365. \hspace*{9mm} $<$ci$>$x$<$/ci$>$\\
  366. \hspace*{5mm} $<$/bvar$>$\\
  367. \hspace*{5mm} $<$apply$>$$<$fn$>$$<$ci$>$F$<$/ci$>$$<$fn$>$\\
  368. \hspace*{9mm} $<$ci$>$x$<$/ci$>$\\
  369. \hspace*{5mm} $<$/apply$>$\\
  370. \hspace*{1mm} $<$/apply$>$\\
  371. $<$/mathml$>$\\}
  372. \\
  373. and type:\\
  374. \\
  375. {\tt mml "ex2.mml"}
  376. \paragraph{Example 3} This example illustrates how practical the switch
  377. {\bf both} can be for interpreting verbose MathML.
  378. Introduce the following MathML source into a file, say {\tt ex3.mml}\\
  379. \\
  380. {\tt $<$mathml$>$\\
  381. \hspace*{1mm} $<$apply$>$$<$int/$>$\\
  382. \hspace*{5mm} $<$bvar$>$\\
  383. \hspace*{9mm} $<$ci$>$x$<$/ci$>$\\
  384. \hspace*{5mm} $<$/bvar$>$\\
  385. \hspace*{5mm} $<$apply$>$$<$sin/$>$\\
  386. \hspace*{9mm} $<$apply$>$$<$log/$>$\\
  387. \hspace*{13mm} $<$ci$>$x$<$/ci$>$\\
  388. \hspace*{9mm} $<$/apply$>$\\
  389. \hspace*{5mm} $<$/apply$>$\\
  390. \hspace*{1mm} $<$/apply$>$\\
  391. $<$/mathml$>$ \\}
  392. \\
  393. then do the following:
  394. \begin{verbatim}
  395. 2: on both;
  396. 3: mml "ml";
  397. \end{verbatim}
  398. \section{An overview of how the Interface Works}
  399. The interface is primarily built in two parts. A first one which parses
  400. and evaluates MathML, and a second one which parses REDUCE's algebraic
  401. expressions and prints them out in MathML format. Both parts work by
  402. recursive parsing, using Top-Down Recursive Descent parsing with one token
  403. look ahead.
  404. The BNF description of the MathML grammar is to be defined informally in
  405. APPENDIX E of the current MathML specification. It is with this document that
  406. we have developed the MathML parser. The MathML parser evaluates all
  407. that is possible and returns a valid REDUCE algebraic expression. When {\bf
  408. mathml} or {\bf both} are on, this algebraic expression is fed into the
  409. second part of the program which parses these expressions and transforms them
  410. back into MathML.
  411. The MathML generator parses through the algebraic expression produced by
  412. either REDUCE itself or the MathML parser. It works in a very
  413. similar way as the MathML parser. It is simpler, since no evaluation is
  414. involved. All the generated code is MathML compliant. It is important to note
  415. that the MathML code generator sometimes introduces Presentation Markup tags,
  416. and other tags which are not understood by the MathML parser of the
  417. interface\footnote{The set of tags not understood by the MathML parser are
  418. detailed in section {\bf Limitations}.}.
  419. \end{document}