mml_ir.red 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. % Description: This module contains both functions for passing MathML to the Intermediate
  2. % representation and from the intermediate representation to MathML.
  3. % Both main functions are: mml2ir() and ir2mml().
  4. %
  5. % Date: 2 May 2000
  6. %
  7. % Author: Luis Alvarez Sobreviela
  8. %
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  11. % Here start the functions in charge of parsing MathML and printing %
  12. % it out in REDUCE intermediate representation. MathML->REDUCE IR %
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. % WN global '(f);
  15. fluid '(constructors!* !*f!*);
  16. % This is the function for reading from a file. It is given the name of a file which contains
  17. % the mathml input. It launches the program by calling mml2ir().
  18. symbolic procedure mml(f);
  19. begin;
  20. FILE!*:=t;
  21. !*f!*:= open(f, 'input);
  22. !*f!*:= rds(!*f!*);
  23. mml2ir();
  24. close rds !*f!*;
  25. FILE!*:=nil;
  26. end;
  27. % This function starts the parsing mechanism, which is a recursive descent
  28. % parsing. Begins at the <math> token.
  29. symbolic procedure mml2ir();
  30. begin scalar res;
  31. res:=nil;
  32. mmlatts:=nil;
  33. space:=int2id(32);
  34. count:=0;
  35. ch:=readch();
  36. temp2:=nil;
  37. lex();
  38. if char='(m a t h) then
  39. res:=mathML()
  40. else errorML("<math>",2);
  41. lex();
  42. if char='(!/ m a t h) then
  43. terpri()
  44. else errorML("</math>",19);
  45. return res;
  46. end;
  47. % The two next functions differ in that one of them parses from the next
  48. % token onwards, and the other one from the actual token onwards.
  49. % It is necessary to have both since some functions end their task one
  50. % token ahead (eg getargs()).
  51. symbolic procedure mathML();
  52. begin scalar a;
  53. a:=nil;
  54. lex();
  55. return sub_math();
  56. end;
  57. symbolic procedure mathML2();
  58. begin scalar a;
  59. a:=nil;
  60. return sub_math();
  61. end;
  62. % Parses all tokens which legally follow a <math> token.
  63. % These tokens have to be constructors.
  64. symbolic procedure sub_math();
  65. begin scalar a, aa;
  66. a:=nil;
  67. if char='(i d e n t !/) then return list 'ident;
  68. % The reason why we perform an individual test to see if we are dealing with a vector tag
  69. % is because REDUCE changes vector in the list to ~vector when compressing (v e c t o r)
  70. % and then it doesnt work anymore...
  71. if char='(v e c t o r) then
  72. <<a:=vectorRD();
  73. if char neq '(!/ v e c t o r) then
  74. errorML("</vector>",2);
  75. return a>>;
  76. if (aa:=assoc(compress!* char, constructors!*)) then <<
  77. a:=apply(cadr aa, nil );
  78. if PAIRP a then if car a = 'csymbol then a:=cddr a;
  79. if PAIRP a then if car a = 'fn then a:=cddr a;
  80. if compress!* char neq third aa then
  81. errorML(third cdr aa, 2);
  82. return a>>;
  83. return nil;
  84. end;
  85. % The next two functions parse the <cn> and <ci> tokens and extract its
  86. % content to be used by the function calling it. It will have different
  87. % behaviours according to the attributes contained.
  88. symbolic procedure cnRD();
  89. begin scalar type, sep, tt, base;
  90. % Must check that what is being returned is an int.
  91. type:=nil;
  92. sep:=nil;
  93. type:=intern find(atts, 'type);
  94. base:=find(atts, 'base);
  95. lex();
  96. tt := char;
  97. lex();
  98. if type='constant then return compress!* tt;
  99. if type=nil then return compress!* tt;
  100. if member(type, '(real integer)) neq nil then <<
  101. if base eq nil then return compress!* tt
  102. else return 'based_integer . nil . base . list ('string . list compress!* tt)
  103. >>;
  104. if member(intern type, '(rational complex!-cartesian complex!-polar)) neq nil then
  105. << sep:=sepRD();
  106. if type='rational then <<
  107. lex();
  108. return rational(compress!* tt, sep)
  109. >>
  110. else
  111. if type='complex!-cartesian then
  112. << lex();return 'complex_cartesian . nil . compress!* tt . list sep >>
  113. else
  114. if type='complex!-polar then
  115. << lex();return 'complex_polar . nil . compress!* tt . list sep >>
  116. >>;
  117. end;
  118. symbolic procedure ciRD();
  119. begin scalar test, type,aa, tt, ats;
  120. aa:=nil; type:=nil; test:=nil;
  121. ats:=retattributes(atts, '(type));
  122. lex();
  123. tt := char;
  124. lex();
  125. << test:=compress tt;
  126. if NUMBERP test then errorML(test, 4);
  127. test := compress!* tt;
  128. if ats = nil then return test;
  129. return list('ci, ats, test)>>
  130. end;
  131. % returns the value of the constant values.
  132. % !!!!!!!!!! USELESS
  133. %symbolic procedure consts(c);
  134. %begin;
  135. % if c='(quote i) then return 'i;
  136. % if c='(quote d) then return 'd;
  137. % if c='(quote e) then return 'e;
  138. % if c='(quote p) then return 'pi;
  139. % if c='(quote infinity) then return 'infinity;
  140. % if c='(quote gamma) then return 'gamma;
  141. %end;
  142. % Constructs a rational number in intermediate representation
  143. symbolic procedure rational(a,b);
  144. begin;
  145. return 'rational . nil . a . list b;
  146. end;
  147. % Reads through values seperated by <sep/> tags and
  148. % returns them in a list
  149. symbolic procedure sepRD();
  150. begin scalar p1, p2;
  151. p1:=nil; p2:=nil;
  152. if char neq '(s e p !/) then errorML("<sep/>",2);
  153. lex();
  154. p2:=compress!* char;
  155. return p2;
  156. end;
  157. % Creates a vector by using function matrix_row.
  158. symbolic procedure vectorRD();
  159. begin scalar a, ats;
  160. ats:=retattributes(atts, '(type other));
  161. a:=nil;
  162. a:=matrixrowRD();
  163. a:=cons('vectorml,cons(ats, a));
  164. return a;
  165. end;
  166. % The following functions constructs the matrix from the mathml information.
  167. symbolic procedure matrixRD();
  168. begin scalar b1, stop, ats, b2;
  169. ats:=retattributes(atts, '(type));
  170. stop:=0;
  171. b1:='();
  172. b2:=nil;
  173. while stop=0 do
  174. <<
  175. lex();
  176. if char='(m a t r i x r o w) then
  177. <<b2:=matrixrowRD();
  178. if b1 neq nil then b1:=append(b1, list b2)
  179. else b1:=list b2;
  180. if char neq '(!/ m a t r i x r o w) then
  181. errorML("</matrixrow>",2)>>
  182. else stop:=1
  183. >>;
  184. return cons('matrix, cons(ats,cons('matrixrow, list b1)));
  185. end;
  186. symbolic procedure matrixrowRD();
  187. begin scalar a;
  188. a:=nil;
  189. a:=mathML();
  190. return if a=nil then nil
  191. else cons(a, matrixrowRD());
  192. end;
  193. % returns a lambda function constructed from the information supplied.
  194. symbolic procedure lambdaRD();
  195. begin scalar b1, b2, ats;
  196. ats:=retattributes(atts, '(type definitionURL encoding));
  197. lex();
  198. b1:=getargsRD();
  199. b2:=mathML2();
  200. lex();
  201. return cons('lambda, cons(ats, append (b1, list b2)));
  202. end;
  203. % returns a set constructed from the information supplied.
  204. symbolic procedure setRD();
  205. begin scalar setvars, ats;
  206. ats:=retattributes(atts, '(type));
  207. setvars:= cons('set, cons(ats, stats_getargs()));
  208. return setvars;
  209. end;
  210. % returns a list constructed from the information supplied.
  211. symbolic procedure listRD();
  212. begin scalar ats;
  213. ats:=retattributes(atts, '(order));
  214. return cons('list, cons(ats , stats_getargs()));
  215. end;
  216. symbolic procedure fnRD();
  217. begin scalar b1;
  218. lex();
  219. if char neq '(c i) then errorML(compress char,20)
  220. else b1:= mathML2();
  221. lex();
  222. return b1;
  223. end;
  224. % Reads the declare construct and sets the value of the given variable to
  225. % the given value.
  226. symbolic procedure declareRD();
  227. begin scalar b1, b2, ats;
  228. ats:=retattributes(atts, '(type nargs occurence scope definitionURL));
  229. lex();
  230. if char='(c i) then
  231. << b1:=ciRD()>>
  232. else errorML("<ci>", 8);
  233. lex();
  234. if char neq '(!/ d e c l a r e) then <<b2 :=mathML2(); lex()>>;
  235. return cons('declare, list(ats, b1, b2));
  236. end;
  237. % This function will determine if the next token is a valid token following
  238. % an apply token. It then calls the appropriate function if succesful.
  239. symbolic procedure applyRD();
  240. begin scalar aa, fun;
  241. lex();
  242. % This following _if_ statement relates the mathml tag to its entry in functions!*
  243. % It then returns a list starting with the name of the function followed by its
  244. % arguments: eg: (plus 1 2 3).
  245. % It uses the table in functions!* to find the function name (the third entry) and
  246. % the arguments to send the RD function.
  247. mmlatts:=retattributes(atts, '(type definitionURL encoding));
  248. if (aa:=assoc(compress!* char, functions!*)) then <<
  249. fun:=apply(cadr aa, nil);
  250. fun:=mmlatts . fun;
  251. mmlatts:=nil;
  252. return cons(cadr rest aa, fun);
  253. >>;
  254. errorML(compress char, 17);
  255. end;
  256. % Reads through a select construct and acts accordingly.
  257. symbolic procedure selectRD();
  258. begin scalar a1, b2, b3;
  259. a1:=mathml();
  260. if car a1 = 'matrix then <<
  261. b2:=mathml();
  262. lex();
  263. if char neq '(!/ a p p l y) then <<b3:=MathML2(); lex()>>;
  264. return cons(a1, list(b2, b3))
  265. >>;
  266. if car a1 = 'list OR car a1 = 'vectorml then <<
  267. b2:=mathml();
  268. lex();
  269. return cons(a1, list b2)
  270. >>;
  271. end;
  272. % Returns the transpose of the element contained in the transpose tags.
  273. symbolic procedure transposeRD();
  274. begin scalar a;
  275. a:=mathML();
  276. lex();
  277. return list a;
  278. end;
  279. % Returns the determinant of the given element.
  280. symbolic procedure determinantRD();
  281. begin scalar a;
  282. a:=mathML();
  283. lex();
  284. return list a;
  285. end;
  286. % Takes the given function name, makes it an operator, and then
  287. % applies it to the arguments specified in the mathml input.
  288. symbolic procedure applyfnRD();
  289. begin scalar b1, b2, c1;
  290. b1:=nil; b2:=nil; c1:=nil;
  291. b1:=fnRD();
  292. b2:=stats_getargs();
  293. return b1 . nil . b2;
  294. end;
  295. % Introduces the new csymbol element of MathML 2.0
  296. symbolic procedure csymbolRD();
  297. begin scalar b1, b2, c1;
  298. b1:=nil; b2:=nil; c1:=nil;
  299. b1:=fnRD();
  300. b2:=stats_getargs();
  301. return b1 . nil . b2;
  302. end;
  303. % Reads the condition tag.
  304. symbolic procedure conditionRD();
  305. begin scalar a;
  306. a:=mathml();
  307. lex();
  308. if char neq '(!/ c o n d i t i o n) then errorML("</condition>", 2);
  309. return cons('condition, list a);
  310. end;
  311. % This function will read all legal tags following the <reln> tag.
  312. fluid '(relations!*);
  313. symbolic procedure relnRD();
  314. begin scalar aa, ats;
  315. lex();
  316. ats:=retattributes(atts, '(type definitionURL));
  317. if (aa:=assoc(compress!* char, relations!*)) then return cons(cadr rest aa, cons(ats, apply(cadr aa, nil)));
  318. end;
  319. symbolic procedure relationRD( type );
  320. begin scalar args;
  321. args:=stats_getargs();
  322. return cons(cadr type, args);
  323. end;
  324. %!!!!!!!! PROBABLY USELESS FUNCTION!!!!!
  325. symbolic procedure binaryrelationRD( type );
  326. begin scalar arg1, arg2;
  327. arg1 := MathML();
  328. arg2 := MathML();
  329. lex();
  330. return cons(type, list (arg1, arg2));
  331. end;
  332. % The following functions do all the necessay actions in order to evaluate
  333. % what should be by the tags.
  334. symbolic procedure subsetRD();
  335. begin scalar abc1;
  336. abc1:=nil;
  337. abc1:=mathML();
  338. return if abc1 = nil then '()
  339. else cons(abc1, subsetRD());
  340. end;
  341. symbolic procedure prsubsetRD();
  342. begin scalar abc1;
  343. abc1:=nil;
  344. abc1:=mathML();
  345. return if abc1 = nil then '()
  346. else cons(abc1, prsubsetRD());
  347. end;
  348. % These functions parse through most MathML elements,
  349. % since many fall in the unary, binary and nary categories.
  350. symbolic procedure unaryRD();
  351. begin scalar a;
  352. a:= mathML();
  353. lex();
  354. return list a;
  355. end;
  356. symbolic procedure binaryRD();
  357. begin scalar a1, a2;
  358. a1:=mathML();
  359. a2:=mathML();
  360. lex();
  361. return cons(a1, list a2);
  362. end;
  363. symbolic procedure naryRD();
  364. begin scalar a;
  365. a:=mathML();
  366. return if a = nil then '()
  367. else cons(a, naryRD());
  368. end;
  369. symbolic procedure setFuncsNaryRD();
  370. begin scalar a;
  371. a:=mathML();
  372. if PAIRP a then <<if cadr a neq nil then if intern cadr car cadr a = 'multiset then mmlatts:='multiset;>>;
  373. return if a = nil then '()
  374. else cons(a, setFuncsnaryRD());
  375. end;
  376. symbolic procedure setFuncsBinRD();
  377. begin scalar flag,a1,a2;
  378. flag:=nil;
  379. a1:=mathML();
  380. if PAIRP a1 then <<if cadr a1 neq nil then if intern cadr car cadr a1 = 'multiset then flag:=t;>>;
  381. a2:=mathML();
  382. if PAIRP a2 then <<if cadr a2 neq nil then if intern cadr car cadr a2 = 'multiset then flag:=t else flag:=nil;>>;
  383. lex();
  384. if flag=t then mmlatts:='multiset;
  385. return cons(a1, list a2);
  386. end;
  387. % Encodes information given in a <limit/> tag.
  388. symbolic procedure limitRD();
  389. begin scalar var, condi, low, exp, ats;
  390. ats:=retattributes(atts, '(definitionurl));
  391. low:=nil;
  392. lex();
  393. if char='(b v a r) then
  394. << var:=bvarRD();
  395. if (caddr var neq 1) then errorML("<degree>",8);
  396. lex()>>
  397. else var:=nil;
  398. if char='(l o w l i m i t) then
  399. << low:=lowlimitRD();
  400. >>
  401. else if char='(c o n d i t i o n) then
  402. << condi:=conditionRD()
  403. >>
  404. else condi:=nil;
  405. exp:=mathML();
  406. lex();
  407. if condi=nil then
  408. return list(var, low, exp);
  409. if low=nil then
  410. return list(var, condi, exp);
  411. end;
  412. % Returns the partial derivative.
  413. symbolic procedure partialdiffRD();
  414. begin scalar res, bvar, express;
  415. lex();
  416. bvar:=getargsRD();
  417. express:=mathML2();
  418. lex();
  419. % res:=cons(express, bvar);
  420. res:=append(bvar, list express);
  421. return res;
  422. end;
  423. % Returns the derivative.
  424. symbolic procedure diffRD();
  425. begin scalar bvar, express;
  426. lex();
  427. if char='(b v a r) then
  428. <<bvar:=bvarRD();
  429. lex()>>
  430. else bvar:=nil;
  431. express:=mathML2();
  432. lex();
  433. return diff2 list(bvar, express);
  434. end;
  435. % This function restructures the IR when we are differentiating
  436. % more than degree 1 so the translation is possible to OM
  437. symbolic procedure diff2(elem);
  438. begin scalar fun, res, deg, var;
  439. deg:=caddr car elem;
  440. var:=cadr car elem;
  441. if deg=1 then return elem;
  442. fun:=car reverse elem;
  443. res:='diff . nil . ('bvar . var .list 1) . list fun;
  444. deg:=deg-1;
  445. while deg > 0 do <<
  446. res:='diff . nil . ('bvar . var .list 1) . list res;
  447. deg:=deg-1;
  448. >>;
  449. return cddr res;
  450. end;
  451. % This function reads through the a series of <bvar> tags and extracts the
  452. % variables.
  453. symbolic procedure getargsRD();
  454. begin scalar a;
  455. % Dont forget. This function leaves the file pointer on
  456. % the next token after the last bvar. So you need to use mathML2 after.
  457. if char='(b v a r) then
  458. <<a:=bvarRD();
  459. lex();
  460. return cons (a,getargsRD())>>;
  461. end;
  462. % Parses through MathML quantifiers
  463. symbolic procedure quantifierRD();
  464. begin scalar bvars, condi, exp;
  465. lex();
  466. bvars:=getargsRD();
  467. if char='(c o n d i t i o n) then
  468. condi:=conditionRD()
  469. else condi:=nil;
  470. if condi neq nil then exp:=MathML()
  471. else exp:=MathML2();
  472. lex();
  473. return append(bvars, list(condi, exp));
  474. end;
  475. % This function will parse through the sum, product and int tags. Takes in the expression, then
  476. % the bound variable, and finally the limits, conditions or intervals if they exist.
  477. symbolic procedure symbolsRD();
  478. begin scalar bvar, low, upper, int, exp, result, cond;
  479. low:=nil;
  480. upper:=nil;
  481. int:=nil;
  482. exp:=nil;
  483. result:=nil;
  484. cond:=nil;
  485. lex();
  486. if char='(b v a r) then
  487. <<bvar:=bvarRD();
  488. if (caddr bvar eq 1) then bvar:=bvar
  489. else
  490. errorML("",13);
  491. lex()>>
  492. else errorML("<bvar>",14);
  493. if char='(l o w l i m i t) then <<low:=lowupperlimitRD(); lex()>>
  494. else low:=nil;
  495. if char='(i n t e r v a l) then
  496. <<int:=intervalRD();
  497. lex()>>
  498. else int:=nil;
  499. if char='(c o n d i t i o n) then
  500. <<cond:=conditionRD();
  501. lex()>>
  502. else cond:=nil;
  503. exp:=mathML2();
  504. lex();
  505. if (low neq nil) then return list(bvar, low, exp);
  506. if (int neq nil) then return list(bvar, int, exp);
  507. if (cond neq nil) then return list(bvar, cond, exp);
  508. return list(bvar, nil, exp);
  509. end;
  510. % Here we parse bound variables. The function reads the variable as well as
  511. % the degree if there is one.
  512. symbolic procedure bvarRD();
  513. begin scalar var, deg;
  514. lex();
  515. if char='(d e g r e e) then
  516. errorML("<bvar>",15);
  517. var:=mathML2();
  518. lex();
  519. if char='(d e g r e e) then
  520. << deg:=mathML();
  521. lex();
  522. if char neq '(!/ d e g r e e) then
  523. error("</degree>",2);
  524. lex()>>
  525. else deg:=1;
  526. if char='(!/ b v a r) then return cons('bvar , list(var, deg))
  527. else errorML("</bvar>", 2);
  528. end;
  529. % Functions used to parse the limits of an integral, sum, or product.
  530. symbolic procedure lowupperlimitRD();
  531. begin scalar lowlimit, upperlimit;
  532. lowlimit:=mathML();
  533. lex();
  534. if char='(!/ l o w l i m i t) then upperlimit:=upperlimitRD()
  535. else errorML("</lowlimit>", 2);
  536. return cons('lowupperlimit, list (lowlimit, upperlimit))
  537. end;
  538. symbolic procedure lowlimitRD();
  539. begin scalar lowlimit;
  540. lowlimit:=mathML();
  541. lex();
  542. if char neq '(!/ l o w l i m i t) then errorML("</lowlimit>", 2);
  543. return cons('lowlimit, list lowlimit);
  544. end;
  545. symbolic procedure upperlimitRD();
  546. begin scalar upperlimit;
  547. lex();
  548. if char neq '(u p l i m i t) then errorML("<uplimit>", 10);
  549. upperlimit:=mathML();
  550. lex();
  551. if char='(!/ u p l i m i t) then return upperlimit
  552. else errorML("</uplimit>", 2);
  553. end;
  554. symbolic procedure intervalRD();
  555. begin scalar l,u, ats;
  556. ats:=retattributes(atts, '(closure));
  557. l:=mathML();
  558. u:=mathML();
  559. lex();
  560. if char='(!/ i n t e r v a l) then return cons('interval, list(ats, l,u))
  561. else errorML("</interval>", 2);
  562. end;
  563. % Following functions just evaluate calculus functions.
  564. symbolic procedure logRD();
  565. begin scalar a1, base;
  566. base:=nil;
  567. lex();
  568. if char='(l o g b a s e) then
  569. <<base:=logbaseRD();
  570. lex()>>;
  571. a1:=mathML2();
  572. lex();
  573. return cons(base, list a1);
  574. end;
  575. symbolic procedure logbaseRD();
  576. begin scalar a;
  577. a:=mathML();
  578. lex();
  579. if char='(!/ l o g b a s e) then return a
  580. else errorML("</logbase>",2);
  581. end;
  582. % % Work on here. Make sure you can have either one or two arguments...
  583. symbolic procedure minusRD();
  584. begin scalar c,b;
  585. c:=mathML();
  586. b:=mathML();
  587. if b=nil then c:= cons(c,'())
  588. else <<
  589. c:=cons(c, cons(b, '()));
  590. lex()>>;
  591. return c;
  592. end;
  593. symbolic procedure rootRD();
  594. begin scalar b,deg;
  595. lex();
  596. if char='(d e g r e e) then
  597. << deg:=mathML();
  598. lex();
  599. if char neq '(!/ d e g r e e) then
  600. error("</degree>","Syntax ERROR: Missing end tag");
  601. lex()>>
  602. else deg:=2;
  603. b:=mathML2();
  604. lex();
  605. return list(cons('degree, list deg), b);
  606. end;
  607. symbolic procedure minmaxRD();
  608. begin scalar a, bvar, cond, flag;
  609. lex();
  610. flag:=0;
  611. if char = '(b v a r) then <<bvar:=bvarRD(); flag:=1; lex();>> else bvar:=nil;
  612. if char = '(c o n d i t i o n) then <<cond:=conditionRD()>>
  613. else <<
  614. a:=mathml2();
  615. a:=cons(a, stats_getargs());
  616. cond:=nil
  617. >>;
  618. if flag=1 then << a:=MathML2(); lex()>>;
  619. if bvar neq nil then return cons(bvar, append(list cond, list a));
  620. if cond neq nil then return list(cond);
  621. return a;
  622. end;
  623. % Following function are in charge of parsing statistics related mathml.
  624. symbolic procedure momentRD( );
  625. begin scalar deg, child;
  626. lex();
  627. if char='(d e g r e e) then
  628. << deg:=mathML();
  629. lex();
  630. if char neq '(!/ d e g r e e) then
  631. error("</degree>",2);
  632. lex()>>
  633. else deg:=nil;
  634. child:=mathml2();
  635. lex();
  636. return list(cons('degree, list deg), child);
  637. end;
  638. % The following function gets all arguments from the mathml input.
  639. symbolic procedure stats_getargs();
  640. begin scalar ww;
  641. ww:=nil;
  642. ww:=mathML();
  643. if ww neq nil then <<
  644. return cons (ww,stats_getargs())>>;
  645. end;
  646. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  647. % Here start the functions in charge of parsing reduce's output and printing %
  648. % it out in MathML. REDUCE->MathML %
  649. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  650. % The following function takes an IR expression and produces a MathML equivalent
  651. symbolic procedure ir2mml( u );
  652. begin;
  653. FLUID '(indent);
  654. ind:=3;
  655. indent:=0$
  656. printout("<math>");
  657. indent!* t;
  658. expression u;
  659. indent!* nil;
  660. printout( "</math>" );
  661. end;
  662. % Prints out vectors.
  663. symbolic procedure vectorML( elem );
  664. begin;
  665. printout("<vector");
  666. attributesML(car elem, "");
  667. indent!* t;
  668. multi_elem(cdr elem);
  669. indent!* nil;
  670. printout("</vector>");
  671. end;
  672. % Following functions print out matrices.
  673. symbolic procedure matrixML( elem );
  674. begin;
  675. printout("<matrix");
  676. attributesML(car elem, "");
  677. indent!* t;
  678. if cadr elem = 'matrixrow then matrix_rows(caddr elem)
  679. else matrix_rows(cols2rows caddr elem);
  680. indent!* nil;
  681. printout("</matrix>")
  682. end;
  683. symbolic procedure matrix_rows( elem );
  684. begin;
  685. if (elem neq()) then
  686. << printout("<matrixrow>");
  687. indent!* t;
  688. row(car elem);
  689. indent!* nil;
  690. printout("</matrixrow>");
  691. matrix_rows( cdr elem ); >>
  692. end;
  693. symbolic procedure row( elem );
  694. begin;
  695. if (elem neq()) then
  696. << expression(car elem); row(cdr elem);>>
  697. end;
  698. symbolic procedure identML(elem);
  699. begin;
  700. printout( "<ident/>" );
  701. end;
  702. % This function takes in an intermediate representation expression, parses it and prints it
  703. % in MathML.
  704. fluid '(ir2mml!*);
  705. symbolic procedure expression( elem );
  706. begin scalar aa;;
  707. if elem neq nil then
  708. if (ATOM elem) then constsML( elem ) else
  709. <<
  710. if (aa:=assoc(car elem, ir2mml!*)) then <<
  711. if caddr aa = nil then
  712. apply(cadr aa, list cdr elem)
  713. else
  714. apply(cadr aa, list(cdr elem, car elem))
  715. >>
  716. else
  717. if ((car elem)= '!*sq) then expression (PREPSQ (cadr elem))
  718. else operator_fn(elem);>>;
  719. end;
  720. symbolic procedure tendstoML( elem );
  721. begin;
  722. printout("<apply><tendsto");
  723. attributesML(car elem, "/");
  724. indent!* t;
  725. expression(cadr elem);
  726. expression(caddr elem);
  727. indent!* nil;
  728. printout("</apply>");
  729. end;
  730. % Prints out derivatives.
  731. symbolic procedure dfml( elem );
  732. begin scalar test;
  733. test:=cdr elem;
  734. if length test=1 OR (length test=2 AND NUMBERP
  735. cadr test) then
  736. printout("<apply><diff/>")
  737. else
  738. printout("<apply><partialdiff/>");
  739. indent!* t;
  740. dfargs(cdr elem);
  741. expression(car elem);
  742. indent!* nil;
  743. printout("</apply>");
  744. end;
  745. symbolic procedure dfargs( elem );
  746. begin;
  747. if elem neq nil then
  748. << if length elem>1 then
  749. << if NUMBERP cadr elem then
  750. <<printout("<bvar>");
  751. indent!* t;
  752. expression car elem;
  753. degreeML(cadr elem);
  754. indent!* nil;
  755. printout("</bvar>");
  756. dfargs(cddr elem)>>
  757. else
  758. <<printout("<bvar>");
  759. indent!* t;
  760. expression car elem;
  761. indent!* nil;
  762. printout("</bvar>");
  763. dfargs(cdr elem)>>; >>
  764. else
  765. << printout("<bvar>");
  766. indent!* t;
  767. expression car elem;
  768. indent!* nil;
  769. printout("</bvar>");
  770. dfargs(cdr elem)>> >>;
  771. end;
  772. % Prints out degree statements.
  773. symbolic procedure degreeML( elem );
  774. begin;
  775. if car elem neq nil then <<
  776. printout("<degree>");
  777. indent!* t;
  778. expression( car elem );
  779. indent!* nil;
  780. printout("</degree>") >>;
  781. end;
  782. symbolic procedure rationalML(elem);
  783. begin scalar a, b;
  784. a:=cadr elem;
  785. b:=caddr elem;
  786. printout("<cn type=""rational"">");
  787. princ a; princ "<sep/>"; princ b; princ "</cn>";
  788. end;
  789. % Prints out relns.
  790. symbolic procedure reln(elem, tty);
  791. begin;
  792. printout("<apply>");
  793. princ "<"; princ tty;
  794. attributesML(car elem, "/");
  795. indent!* t;
  796. multi_elem( cdr elem );
  797. indent!* nil;
  798. printout("</apply>");
  799. end;
  800. % Prints out a set.
  801. symbolic procedure containerML( elem, tty );
  802. begin;
  803. if tty = 'integer_interval then tty:='interval;
  804. printout("<"); princ tty;
  805. attributesML(car elem, "");
  806. indent!* t;
  807. multi_elem( cdr elem );
  808. indent!* nil;
  809. printout("</"); princ tty; princ ">";
  810. end;
  811. % Prints out set theory related functions.
  812. symbolic procedure sets(elem, tty);
  813. begin;
  814. printout("<apply>");
  815. princ "<"; princ tty;
  816. attributesML(car elem, "/");
  817. indent!* t;
  818. multi_elem( cdr elem );
  819. indent!* nil;
  820. printout("</apply>");
  821. end;
  822. symbolic procedure listML( elem );
  823. begin;
  824. printout( "<list" );
  825. attributesML(car elem,"");
  826. indent!* t;
  827. multilists( cdr elem );
  828. indent!* nil;
  829. printout( "</list>" );
  830. end;
  831. symbolic procedure multilists( elem );
  832. begin;
  833. if elem neq nil then
  834. if ((LENGTH elem)=1) then expression (car elem)
  835. else <<expression(car elem); multilists(cdr elem);>>
  836. end;
  837. % Prints out unknown functions as a function. It prints out all variables
  838. % declared as operators.
  839. symbolic procedure csymbol_fn( elem );
  840. begin;
  841. printout("<apply>");
  842. indent!* t;
  843. printout("<csymbol");
  844. if car elem neq nil then attributesML(car elem, "")
  845. else princ ">";
  846. indent!* t;
  847. printout("<ci>");
  848. princ cadr elem;
  849. princ "</ci>";
  850. indent!* nil;
  851. printout("</csymbol>");
  852. multi_args(cddr elem);
  853. indent!* nil;
  854. printout("</apply>");
  855. end;
  856. symbolic procedure operator_fn( elem );
  857. begin;
  858. printout("<apply>");
  859. indent!* t;
  860. printout("<csymbol>");
  861. indent!* t;
  862. printout("<ci>");
  863. princ car elem;
  864. princ "</ci>";
  865. indent!* nil;
  866. printout("</csymbol>");
  867. multi_args(cdr elem);
  868. indent!* nil;
  869. printout("</apply>");
  870. end;
  871. % Reads through a list and prints out each component.
  872. symbolic procedure multi_args( elem );
  873. begin;
  874. if (elem neq ()) then <<expression(car elem); multi_args( cdr elem );>>
  875. end;
  876. % Prints out logs with a base.
  877. symbolic procedure log_baseML(elem, type);
  878. begin;
  879. printout("<apply><log");
  880. attributesML(car elem, "/");
  881. indent!* t;
  882. if car reverse elem neq nil then <<
  883. printout("<logbase>");
  884. indent!* t;
  885. expression(cadr elem);
  886. indent!* nil;
  887. printout("</logbase>")>>;
  888. expression(caddr elem);
  889. indent!* nil;
  890. printout("<apply>");
  891. end;
  892. % Prints out equal relns.
  893. symbolic procedure equalML( elem );
  894. begin;
  895. printout( "<reln><eq/>" );
  896. indent!* t;
  897. expression(car elem);
  898. expression(cadr elem);
  899. indent!* nil;
  900. printout( "</reln>" );
  901. end;
  902. % Prints out square roots and moments.
  903. symbolic procedure degreetoksML( elem, tty );
  904. begin;
  905. printout( "<apply><" ); princ tty;
  906. attributesML(car elem, "/");
  907. indent!* t;
  908. degreeML(cdadr elem);
  909. expression( caddr elem );
  910. indent!* nil;
  911. printout( "</apply>" );
  912. end;
  913. symbolic procedure bvarML(elem);
  914. begin;
  915. printout("<bvar>");
  916. indent!* t;
  917. expression(car elem);
  918. if cadr elem neq 1 then <<
  919. degreeML(list cadr elem);
  920. >>;
  921. indent!* nil;
  922. printout("</bvar>")
  923. end;
  924. % This function prints a series of bvar statements
  925. symbolic procedure xbvarML(elem);
  926. begin;
  927. if elem neq nil then <<bvarML cdar elem; xbvarML cdr elem>>;
  928. end;
  929. symbolic procedure conditionML( elem );
  930. begin;
  931. printout("<condition>");
  932. indent!* t;
  933. expression(car elem);
  934. indent!* nil;
  935. printout("</condition>")
  936. end;
  937. symbolic procedure lambdaML( elem );
  938. begin;
  939. printout("<lambda");
  940. attributesml(car elem, "");
  941. indent!* t;
  942. multi_elem(cdr elem);
  943. indent!* nil;
  944. printout("</lambda>")
  945. end;
  946. symbolic procedure attributesML( a, s );
  947. begin;
  948. if a eq nil then <<princ s; princ ">">>
  949. else <<
  950. princ " ";
  951. princ caar a;
  952. princ "=""";
  953. if caar a neq 'definitionurl then <<
  954. if cadar a = 'vectorml then princ "vector"
  955. else princ cadar a;
  956. >>
  957. else list2string(cadar a);
  958. princ"""";
  959. attributesML(cdr a, s);
  960. >>;
  961. end;
  962. symbolic procedure list2string(a);
  963. begin;
  964. if a neq nil then <<princ car a; list2string(cdr a)>>;
  965. end;
  966. symbolic procedure declareML( elem );
  967. begin;
  968. printout("<declare");
  969. attributesML(car elem, "");
  970. indent!* t;
  971. multi_elem(cdr elem);
  972. indent!* nil;
  973. printout("</declare>")
  974. end;
  975. symbolic procedure lowupperlimitML( elem );
  976. begin;
  977. printout("<lowlimit>");
  978. indent!* t;
  979. expression(cadr elem);
  980. indent!* nil;
  981. printout("</lowlimit>");
  982. printout("<uplimit>");
  983. indent!* t;
  984. expression(caddr elem);
  985. indent!* nil;
  986. printout("</uplimit>");
  987. end;
  988. symbolic procedure lowlimitML( elem );
  989. begin;
  990. printout("<lowlimit>");
  991. indent!* t;
  992. expression(car elem);
  993. indent!* nil;
  994. printout("</lowlimit>");
  995. end;
  996. % Prints out quotients.
  997. symbolic procedure quotientML( elem , tty);
  998. begin;
  999. if (NUMBERP car elem) AND (NUMBERP cadr elem) then <<
  1000. if !*web=nil then printout("<cn type=""rational""> ")
  1001. else printout("<cn type=&quot;rational&quot;> ");
  1002. princ car elem;
  1003. princ " <sep/> ";
  1004. princ cadr elem;
  1005. princ " </cn>">>
  1006. else <<
  1007. printout( "<apply>" );
  1008. princ "<"; princ tty; princ "/>";
  1009. indent!* t;
  1010. expression( cadr elem );
  1011. expression( caddr elem );
  1012. indent!* nil;
  1013. printout( "</apply>" )>>;
  1014. end;
  1015. % Prints out all nary functions.
  1016. symbolic procedure nary( elem, type );
  1017. begin;
  1018. if car elem = 'e AND type = 'power then unary(cdr elem, 'exp)
  1019. else <<
  1020. printout( "<apply>" );
  1021. princ "<";
  1022. princ type;
  1023. attributesml(car elem, "/");
  1024. indent!* t;
  1025. multi_elem( cdr elem );
  1026. indent!* nil;
  1027. printout( "</apply>" )>>
  1028. end;
  1029. symbolic procedure multi_elem( elem );
  1030. begin;
  1031. if ((length elem)=1) then expression( car elem )
  1032. else <<expression car elem ; multi_elem( cdr elem );>>
  1033. end;
  1034. symbolic procedure minusML( elem );
  1035. begin;
  1036. printout( "<apply><minus/>" );
  1037. indent!* t;
  1038. multiminus( cdr elem );
  1039. indent!* nil;
  1040. printout( "</apply>" );
  1041. end;
  1042. symbolic procedure multiminus( elem );
  1043. begin;
  1044. expression(car elem);
  1045. if ((length elem)=2) then expression cadr elem;
  1046. end;
  1047. symbolic procedure ciML(elem);
  1048. begin;
  1049. printout("<ci");
  1050. attributesML(car elem, "");
  1051. princ(cadr elem);
  1052. princ("</ci>");
  1053. end;
  1054. symbolic procedure cnML(elem);
  1055. begin;
  1056. printout("<cn");
  1057. attributesML(car elem, "");
  1058. princ(cadr elem);
  1059. princ("</cn>");
  1060. end;
  1061. symbolic procedure semanticML(elem);
  1062. begin;
  1063. if length elem > 1 then <<
  1064. printout("<apply>");
  1065. indent!* t;
  1066. printout("<fn>");
  1067. indent!* t;
  1068. >>;
  1069. printout("<semantic>");
  1070. indent!* t;
  1071. printout("<ci><mo>"); princ caar elem; princ "</mo></ci>";
  1072. printout("<annotation-xml encoding=""OpenMath"">");
  1073. indent!* t;
  1074. printout"<"; list2string cadar elem; princ ">";
  1075. indent!* nil;
  1076. printout("</annotation-xml>");
  1077. indent!* nil;
  1078. printout("</semantic>");
  1079. if length elem > 1 then <<
  1080. indent!* nil;
  1081. printout("</fn>");
  1082. multi_elem(cdr elem);
  1083. indent!* nil;
  1084. printout("</apply>");
  1085. >>;
  1086. end;
  1087. symbolic procedure numML(elem, type);
  1088. begin;
  1089. if type='based_integer then <<
  1090. printout "<cn type=""integer"" base="""; princ cadr elem; princ """> "; princ cadr caddr elem; princ " </cn>";
  1091. >>;
  1092. if type='complex_cartesian then <<
  1093. printout "<cn type=""complex-cartesian""> "; princ cadr elem; princ " <sep/> "; princ caddr elem; princ " </cn>";
  1094. >>;
  1095. if type='complex_polar then <<
  1096. printout "<cn type=""complex-polar""> "; princ cadr elem; princ " <sep/> "; princ caddr elem; princ " </cn>";
  1097. >>;
  1098. end;
  1099. % Prints out all pieces of data: i.e terminal symbols.
  1100. % They can be numbers, identifiers, or constants.
  1101. symbolic procedure constsML(exp);
  1102. begin;
  1103. if (NUMBERP exp) then
  1104. << printout "<cn";
  1105. if (FLOATP exp) then princ " type=""real""> "
  1106. else
  1107. if (FIXP exp) then princ " type=""integer""> "
  1108. else princ "> ";
  1109. princ exp;
  1110. princ " </cn>">>;
  1111. if (IDP exp) then
  1112. <<
  1113. if member(intern exp, constants!*) neq nil then
  1114. % <<printout "<cn type=""constant""> "; princ exp; princ " </cn>"; return nil>>
  1115. <<printout "<cn type=""constant""> "; princ exp; princ " </cn>">>
  1116. else << printout "<ci";
  1117. if (listp exp) then princ " type=""list""> "
  1118. else
  1119. if (vectorp exp) then princ " type=""vector""> "
  1120. else princ "> ";
  1121. princ exp;
  1122. princ " </ci>">>; >>;
  1123. end;
  1124. % Functions used to print out variables with a subscript.
  1125. % Prints out expressions in math form. Plagiarised from reduce code of
  1126. % mathprint
  1127. symbolic procedure ma_print l;
  1128. begin scalar temp;
  1129. temp:=outputhandler!*;
  1130. outputhandler!*:=nil;
  1131. terpri!* nil;
  1132. if !*web=nil then maprin "<cn type=""real"">"
  1133. else maprin "<cn type=&quot;real&quot;>";
  1134. maprin l;
  1135. maprin "</cn>";
  1136. terpri!* nil;
  1137. outputhandler!*:=temp;
  1138. end;
  1139. % Function in charge of doing all printing in order to make sure the
  1140. % indentation is always correct.
  1141. symbolic procedure printout( str );
  1142. begin;
  1143. if !*web = nil then terpri();
  1144. if !*web = nil then for i := 1:indent
  1145. do << princ " " >>;
  1146. if PAIRP str then
  1147. <<if car str='!:rd!: OR car str='!:rn!: then ma_print str
  1148. else princ str>>
  1149. else princ str;
  1150. end;
  1151. lisp operator mml;
  1152. lisp operator mml2ir;
  1153. algebraic operator g_eq;
  1154. algebraic operator l_eq;
  1155. algebraic operator gt;
  1156. algebraic operator lt;
  1157. lisp operator plusRD;
  1158. symbolic procedure test();
  1159. begin scalar a;
  1160. a:=mml2ir();
  1161. terpri!* t;
  1162. princ "Intermediate representation: ";
  1163. terpri!* t;
  1164. print a;
  1165. ir2mml a;
  1166. end;
  1167. end;