ir2om.red 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. % Description: This module defines all functions necessary to pass from the
  2. % intermediate representation to OpenMath. They print out the
  3. % OpenMath expression on the screen.
  4. %
  5. % Date: 2 May 2000
  6. %
  7. % Author: Luis Alvarez Sobreviela
  8. %
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  11. % The following tables are used by the functions in this file %
  12. % in order to map properly intermediate representation tokens %
  13. % to OpenMath elements and symbols. %
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15. mmltypes!*:=
  16. '((complex_cartesian . (complex_cartesian_type))
  17. (complex_polar . (complex_polar_type))
  18. (constant . (constant_type))
  19. (integer . (integer_type))
  20. (list . (list_type))
  21. (matrix . (matrix_type))
  22. (rational . (rational_type))
  23. (real . (real_type))
  24. (set . (set_type)));
  25. % Maps MathML <interval> attribute values
  26. % to OpenMath symbols
  27. interval!*:=
  28. '((open . (interval_oo))
  29. (closed . (interval_cc))
  30. (open!-closed . (interval_oc))
  31. (closed!-open . (interval_co)));
  32. % Maps MathML constants to OpenMath constant symbols
  33. % and their CDs.
  34. constantsOM!*:=
  35. '((!&ImaginaryI!; . (nums1 i))
  36. (!&ExponentialE!; . (nums1 e))
  37. (!&pi!; . (nums1 pi))
  38. (!&NotANumber!; . (nums1 nan))
  39. (!&gamma!; . (nums1 gamma))
  40. (!&infin!; . (nums1 infinity))
  41. (!&false!; . (logic1 false))
  42. (!&true!; . (logic1 true)));
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. % The function ir2om starts the process of translating intermediate %
  45. % representation into OpenMath IR->OpenMath %
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47. symbolic procedure ir2om( elem );
  48. begin;
  49. ind:=2;
  50. indent:=0;
  51. printout("<OMOBJ>");
  52. indent!* t;
  53. objectOM( elem );
  54. indent!* nil;
  55. printout("</OMOBJ>");
  56. end;
  57. symbolic procedure objectOM(elem);
  58. begin scalar aa;;
  59. if PAIRP elem then <<
  60. if (aa:=assoc(car elem, ir2mml!*)) then <<
  61. apply(cadddr aa, list elem)
  62. >>
  63. else fnOM(elem);
  64. >>
  65. else basicOM(elem);
  66. end;
  67. symbolic procedure strOM(elem);
  68. begin;
  69. printout "<OMSTR> ";princ cadr elem; princ " </OMSTR>";
  70. end;
  71. % Recieves an element which is not a list
  72. % and prints out OpenMath accordingly.
  73. symbolic procedure basicOM(elem);
  74. begin;
  75. if NUMBERP elem then <<
  76. if FIXP elem then integerOM(elem);
  77. if FLOATP elem then floatOM(elem)
  78. >>
  79. else
  80. if IDP elem then variableOM(elem);
  81. end;
  82. % Prints out integers
  83. symbolic procedure integerOM(elem);
  84. begin;
  85. printout("<OMI> ");
  86. princ elem;
  87. princ " </OMI>"
  88. end;
  89. % Prints out decimal floats
  90. symbolic procedure floatOM(elem);
  91. begin;
  92. printout("<OMF ");
  93. princ "dec="""; princ elem; princ """/>";
  94. end;
  95. % Prints out OpenMath variables
  96. symbolic procedure variableOM(elem);
  97. begin scalar aa;
  98. aa:=assoc(intern elem, constantsOM!*);
  99. if aa neq nil then <<
  100. printout("<OMS ");
  101. princ "cd=""";
  102. princ cadr aa;
  103. princ """ ";
  104. princ "name=""";
  105. princ caddr aa;
  106. princ """/>";
  107. >>
  108. else <<
  109. if elem neq nil then <<
  110. printout("<OMV ");
  111. princ "name="""; princ elem; princ """/>";
  112. >>
  113. >>;
  114. end;
  115. % Prints out all OpenMath symbols of 1, 2, or more arguments
  116. % constructed by application.
  117. symbolic procedure naryOM(elem);
  118. begin scalar cd, name;
  119. name:=car elem;
  120. if name='var then name:='variance;
  121. cd := assoc(name, valid_om!*);
  122. if cd neq nil then cd:=cadr cd;
  123. if cadr elem neq nil then <<
  124. if cadr elem = 'multiset then cd:=cadr elem;
  125. >>;
  126. printout "<OMA>";
  127. indent:=indent+2;
  128. printout "<OMS cd="""; princ cd; princ """ name="""; princ name; princ """>";
  129. multiOM(cddr elem);
  130. indent:=indent-2;
  131. printout "</OMA>";
  132. end;
  133. symbolic procedure multiOM(elem);
  134. begin;
  135. if ((length elem)=1) then objectOM( car elem )
  136. else <<objectOM car elem ; multiOM( cdr elem );>>
  137. end;
  138. % Prints out the OpenMath matrix_selector or
  139. % vector_selector symbols
  140. symbolic procedure selectOM(elem);
  141. begin scalar name;
  142. if caaddr elem ='matrix then name:='matrix_selector
  143. else name:='vector_selector;
  144. printout "<OMA>";
  145. indent:=indent+2;
  146. printout "<OMS cd=""linalg3"" name="""; princ name;
  147. princ """/>";
  148. multiOM(cdddr elem);
  149. objectOM caddr elem;
  150. indent:=indent-2;
  151. printout "</OMA>";
  152. end;
  153. % Prints out elements which are
  154. % containers in MathML.
  155. symbolic procedure containerOM(elem);
  156. begin scalar cd, att, name;
  157. att:=cadr elem;
  158. name:=car elem;
  159. printout "<OMA>";
  160. indent!* t;
  161. if name = 'vectorml then name:= 'vector;
  162. cd := cadr assoc(name, valid_om!*);
  163. if car elem = 'set and PAIRP att then <<
  164. if intern cadr car att='multiset then cd:='multiset1;
  165. >>;
  166. if car elem = 'vectorml then name:= "vector";
  167. if car elem = 'vectorml then elem:= 'vector . cdr elem;
  168. printout "<OMS cd="""; princ cd; princ """ name="""; princ name; princ """/>";
  169. multiOM(cddr elem);
  170. indent!* nil;
  171. printout "</OMA>";
  172. end;
  173. % Prints out OpenMath intervals
  174. symbolic procedure intervalOM(elem);
  175. begin scalar aa, att, name, cd;
  176. att:=cadr elem;
  177. name:=car elem;
  178. if name = 'lowupperlimit then <<name:='integer_interval; att:=nil; elem:=car elem . nil . cdr elem>>;
  179. cd := cadr assoc(name, valid_om!*);
  180. if att neq nil then <<
  181. aa:=assoc(intern cadr car att, interval!*);
  182. name:=cadr aa;
  183. >>;
  184. printout "<OMA>";
  185. indent!* t;
  186. printout "<OMS cd="""; princ cd; princ """ name="""; princ name; princ """/>";
  187. multiOM(cddr elem);
  188. indent!* nil;
  189. printout "</OMA>";
  190. end;
  191. % Prints matrices according to the definition
  192. % in CD linalg1
  193. symbolic procedure matrixOM(elem);
  194. begin;
  195. printout "<OMA>";
  196. indent!* t;
  197. printout "<OMS cd=""linalg1"" name=""matrix""/>";
  198. matrixrowOM(cadddr elem);
  199. indent!* nil;
  200. printout "</OMA>";
  201. end;
  202. symbolic procedure matrixrowOM(elem);
  203. begin;
  204. if elem neq nil then <<
  205. printout "<OMA>";
  206. indent!* t;
  207. printout "<OMS cd=""linalg1"" name=""matrixrow""/>";
  208. multiOM(car elem);
  209. indent!* nil;
  210. printout "</OMA>";
  211. matrixrowOM cdr elem;
  212. >>;
  213. end;
  214. % Prints out variables which posses
  215. % an attribute
  216. symbolic procedure ciOM(elem);
  217. begin;
  218. printout "<OMATTR>";
  219. indent!* t;
  220. printout "<OMATP>";
  221. indent!* t;
  222. printout "<OMS cd=""typmml"" name=""type"">";
  223. printout "<OMS cd=""typmml"" name=""";
  224. princ assoc(intern cadr car cadr elem, mmltypes!*);
  225. princ cadr assoc(intern cadr car cadr elem, mmltypes!*);
  226. princ """>";
  227. indent!* nil;
  228. printout "</OMATP>";
  229. objectOM(caddr elem);
  230. indent!* nil;
  231. printout "</OMATTR>";
  232. end;
  233. % Prints out constants such as pi, gamma etc...
  234. symbolic procedure numOM(elem);
  235. begin;
  236. printout "<OMA>";
  237. indent!* t;
  238. printout "<OMS cd=""nums1"" name="""; princ car elem; princ """/>";
  239. objectOM cadr elem;
  240. if car elem='based_integer then strOM cadr caddr elem
  241. else objectOM caddr elem;
  242. indent!* nil;
  243. printout "</OMA>";
  244. end;
  245. symbolic procedure fnOM(elem);
  246. begin;
  247. printout "<OMA>";
  248. indent!* t;
  249. printout "<OMATTR>";
  250. indent!* t;
  251. printout "<OMATP>";
  252. indent!* t;
  253. printout "<OMS cd=""typmml"" name=""type""/>";
  254. printout "<OMS cd=""typmml"" name="""; princ "fn_type"; princ """/>";
  255. indent!* nil;
  256. printout "</OMATP>";
  257. objectOM car elem;
  258. indent!* nil;
  259. printout "</OMATTR>";
  260. multiOM(cddr elem);
  261. indent!* nil;
  262. printout "</OMA>";
  263. end;
  264. % Prints out partial differentiation expressions
  265. symbolic procedure partialdiffOM(elem);
  266. begin scalar cd, var, fun, name;
  267. cd := assoc(car elem, valid_om!*);
  268. if cd neq nil then cd:=cadr cd;
  269. name:=car elem;
  270. var:=cdr reverse cddr elem;
  271. fun:=car reverse elem;
  272. if length var = 1 then symbolsOM('diff . cdr elem);
  273. end;
  274. % Prints out elements such as sum, prod, diff and int.
  275. symbolic procedure symbolsOM(elem);
  276. begin scalar cd, var, fun, int, name;
  277. cd := assoc(car elem, valid_om!*);
  278. if cd neq nil then cd:=cadr cd;
  279. name:=car elem;
  280. var:=caddr elem;
  281. fun:=car reverse elem;
  282. if name neq 'diff then int:=cadddr elem;
  283. % This error states that a <sum> will not be translated to MathML
  284. if int neq nil then if car int = 'condition then errorML("<condition> tag not supported in MathML", 1);
  285. printout "<OMA>";
  286. indent!* t;
  287. if int neq nil AND name='int then name:='defint;
  288. printout "<OMS cd="""; princ cd; princ """ name="""; princ name; princ """/>";
  289. if int neq nil then objectOM int;
  290. lambdaOM ('lambda . nil . var . list fun);
  291. indent!* nil;
  292. printout "</OMA>";
  293. end;
  294. % Prints out lambda expressions
  295. symbolic procedure lambdaOM(elem);
  296. begin scalar var, fun;
  297. var:= cadr caddr elem;
  298. fun:=car reverse elem;
  299. printout "<OMBIND>";
  300. indent!* t;
  301. printout "<OMS cd=""fns1"" name=""lambda""/>";
  302. printout "<OMBVAR>";
  303. indent!* t;
  304. objectOM var;
  305. indent!* nil;
  306. printout "</OMBVAR>";
  307. objectOM fun;
  308. indent!* nil;
  309. printout "</OMBIND>";
  310. end;
  311. % Does not work...
  312. symbolic procedure semanticOM(elem);
  313. begin scalar sem;
  314. printout "<OMA>";
  315. indent!* t;
  316. sem:=cadr cadr elem;
  317. list2string sem;
  318. multiOM cddr elem;
  319. indent!* nil;
  320. printout "</OMA>";
  321. end;
  322. % Prints out limit expressions
  323. symbolic procedure limitOM(elem);
  324. begin scalar limit, fun, var, tendsto;
  325. var:=caddr elem;
  326. limit:=cadddr elem;
  327. fun:=car reverse elem;
  328. printout "<OMA>";
  329. indent!* t;
  330. printout "<OMS cd=""limit1"" name=""limit""/>";
  331. if car limit = 'lowlimit then <<
  332. objectOM cadr limit;
  333. printout "<OMS cd=""limit1"" name=""null""/>"
  334. >>;
  335. if car limit = 'condition then <<
  336. objectOM car reverse cadr limit;
  337. tendsto:= cadr car cadr cadr limit;
  338. printout "<OMS cd=""limit1"" name="""; princ tendsto; princ """/>"
  339. >>;
  340. lambdaOM ('limit . nil . var . list fun);
  341. indent!* nil;
  342. printout "</OMA>";
  343. end;
  344. % Prints out OpenMath quantifiers
  345. symbolic procedure quantOM(elem);
  346. begin;
  347. if cadr reverse elem neq nil then errorML("condition tag not supported in MathML ", 2);
  348. printout "<OMBIND>";
  349. indent!* t;
  350. printout "<OMS cd=""quant1"" name="""; princ car elem; princ """/>";
  351. printout "<OMBVAR>";
  352. indent!* t;
  353. bvarOM cddr elem;
  354. indent!* nil;
  355. printout "</OMBVAR>";
  356. objectOM car reverse elem;
  357. indent!* nil;
  358. printout "</OMBIND>";
  359. end;
  360. symbolic procedure bvarOM(elem);
  361. begin;
  362. if PAIRP car elem then
  363. if car car elem = 'bvar then <<objectOM cadr car elem; bvarOM cdr elem>>;
  364. end;
  365. symbolic procedure printout( str );
  366. begin;
  367. terpri!* t;
  368. for i := 1:indent do << princ " " >>;
  369. princ str;
  370. end;
  371. % This is the function the user types to
  372. % translate MathML to OpenMath
  373. symbolic procedure mml2om();
  374. begin scalar a;;
  375. a:=mml2ir();
  376. terpri!* t;
  377. princ "Intermediate representation: "; terpri!* t; print a;
  378. ir2om a;
  379. end;
  380. lisp operator mml2om;
  381. end;