calc-rewr.el 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. ;;; calc-rewr.el --- rewriting functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. ;; This file is autoloaded from calc-ext.el.
  18. (require 'calc-ext)
  19. (require 'calc-macs)
  20. (defvar math-rewrite-default-iters 100)
  21. ;; The variable calc-rewr-sel is local to calc-rewrite-selection and
  22. ;; calc-rewrite, but is used by calc-locate-selection-marker.
  23. (defvar calc-rewr-sel)
  24. (defun calc-rewrite-selection (rules-str &optional many prefix)
  25. (interactive "sRewrite rule(s): \np")
  26. (calc-slow-wrapper
  27. (calc-preserve-point)
  28. (let* ((num (max 1 (calc-locate-cursor-element (point))))
  29. (reselect t)
  30. (pop-rules nil)
  31. rules
  32. (entry (calc-top num 'entry))
  33. (expr (car entry))
  34. (calc-rewr-sel (calc-auto-selection entry))
  35. (math-rewrite-selections t)
  36. (math-rewrite-default-iters 1))
  37. (if (or (null rules-str) (equal rules-str "") (equal rules-str "$"))
  38. (if (= num 1)
  39. (error "Can't use same stack entry for formula and rules")
  40. (setq rules (calc-top-n 1 t)
  41. pop-rules t))
  42. (setq rules (if (stringp rules-str)
  43. (math-read-exprs rules-str) rules-str))
  44. (if (eq (car-safe rules) 'error)
  45. (error "Bad format in expression: %s" (nth 1 rules)))
  46. (if (= (length rules) 1)
  47. (setq rules (car rules))
  48. (setq rules (cons 'vec rules)))
  49. (or (memq (car-safe rules) '(vec var calcFunc-assign
  50. calcFunc-condition))
  51. (let ((rhs (math-read-expr
  52. (read-string (concat "Rewrite from: " rules-str
  53. " to: ")))))
  54. (if (eq (car-safe rhs) 'error)
  55. (error "Bad format in expression: %s" (nth 1 rhs)))
  56. (setq rules (list 'calcFunc-assign rules rhs))))
  57. (or (eq (car-safe rules) 'var)
  58. (calc-record rules "rule")))
  59. (if (eq many 0)
  60. (setq many '(var inf var-inf))
  61. (if many (setq many (prefix-numeric-value many))))
  62. (if calc-rewr-sel
  63. (setq expr (calc-replace-sub-formula (car entry)
  64. calc-rewr-sel
  65. (list 'calcFunc-select calc-rewr-sel)))
  66. (setq expr (car entry)
  67. reselect nil
  68. math-rewrite-selections nil))
  69. (setq expr (calc-encase-atoms
  70. (calc-normalize
  71. (math-rewrite
  72. (calc-normalize expr)
  73. rules many)))
  74. calc-rewr-sel nil
  75. expr (calc-locate-select-marker expr))
  76. (or (consp calc-rewr-sel) (setq calc-rewr-sel nil))
  77. (if pop-rules (calc-pop-stack 1))
  78. (calc-pop-push-record-list 1 (or prefix "rwrt") (list expr)
  79. (- num (if pop-rules 1 0))
  80. (list (and reselect calc-rewr-sel))))
  81. (calc-handle-whys)))
  82. (defun calc-locate-select-marker (expr)
  83. (if (Math-primp expr)
  84. expr
  85. (if (and (eq (car expr) 'calcFunc-select)
  86. (= (length expr) 2))
  87. (progn
  88. (setq calc-rewr-sel (if calc-rewr-sel t (nth 1 expr)))
  89. (nth 1 expr))
  90. (cons (car expr)
  91. (mapcar 'calc-locate-select-marker (cdr expr))))))
  92. (defun calc-rewrite (rules-str many)
  93. (interactive "sRewrite rule(s): \nP")
  94. (calc-slow-wrapper
  95. (let (n rules expr)
  96. (if (or (null rules-str) (equal rules-str "") (equal rules-str "$"))
  97. (setq expr (calc-top-n 2)
  98. rules (calc-top-n 1 t)
  99. n 2)
  100. (setq rules (if (stringp rules-str)
  101. (math-read-exprs rules-str) rules-str))
  102. (if (eq (car-safe rules) 'error)
  103. (error "Bad format in expression: %s" (nth 1 rules)))
  104. (if (= (length rules) 1)
  105. (setq rules (car rules))
  106. (setq rules (cons 'vec rules)))
  107. (or (memq (car-safe rules) '(vec var calcFunc-assign
  108. calcFunc-condition))
  109. (let ((rhs (math-read-expr
  110. (read-string (concat "Rewrite from: " rules-str
  111. " to: ")))))
  112. (if (eq (car-safe rhs) 'error)
  113. (error "Bad format in expression: %s" (nth 1 rhs)))
  114. (setq rules (list 'calcFunc-assign rules rhs))))
  115. (or (eq (car-safe rules) 'var)
  116. (calc-record rules "rule"))
  117. (setq expr (calc-top-n 1)
  118. n 1))
  119. (if (eq many 0)
  120. (setq many '(var inf var-inf))
  121. (if many (setq many (prefix-numeric-value many))))
  122. (setq expr (calc-normalize (math-rewrite expr rules many)))
  123. (let (calc-rewr-sel)
  124. (setq expr (calc-locate-select-marker expr)))
  125. (calc-pop-push-record-list n "rwrt" (list expr)))
  126. (calc-handle-whys)))
  127. (defun calc-match (pat &optional interactive)
  128. (interactive "sPattern: \np")
  129. (calc-slow-wrapper
  130. (let (n expr)
  131. (if (or (null pat) (equal pat "") (equal pat "$"))
  132. (setq expr (calc-top-n 2)
  133. pat (calc-top-n 1)
  134. n 2)
  135. (setq pat (if (stringp pat) (math-read-expr pat) pat))
  136. (if (eq (car-safe pat) 'error)
  137. (error "Bad format in expression: %s" (nth 1 pat)))
  138. (if (not (eq (car-safe pat) 'var))
  139. (calc-record pat "pat"))
  140. (setq expr (calc-top-n 1)
  141. n 1))
  142. (or (math-vectorp expr) (error "Argument must be a vector"))
  143. (if (calc-is-inverse)
  144. (calc-enter-result n "mtcn" (math-match-patterns pat expr t))
  145. (calc-enter-result n "mtch" (math-match-patterns pat expr nil))))))
  146. (defvar math-mt-many)
  147. ;; The variable math-rewrite-whole-expr is local to math-rewrite,
  148. ;; but is used by math-rewrite-phase
  149. (defvar math-rewrite-whole-expr)
  150. (defun math-rewrite (math-rewrite-whole-expr rules &optional math-mt-many)
  151. (let* ((crules (math-compile-rewrites rules))
  152. (heads (math-rewrite-heads math-rewrite-whole-expr))
  153. (trace-buffer (get-buffer "*Trace*"))
  154. (calc-display-just 'center)
  155. (calc-display-origin 39)
  156. (calc-line-breaking 78)
  157. (calc-line-numbering nil)
  158. (calc-show-selections t)
  159. (calc-why nil)
  160. (math-mt-func (function
  161. (lambda (x)
  162. (let ((result (math-apply-rewrites x (cdr crules)
  163. heads crules)))
  164. (if result
  165. (progn
  166. (if trace-buffer
  167. (let ((fmt (math-format-stack-value
  168. (list result nil nil))))
  169. (with-current-buffer trace-buffer
  170. (insert "\nrewrite to\n" fmt "\n"))))
  171. (setq heads (math-rewrite-heads result heads t))))
  172. result)))))
  173. (if trace-buffer
  174. (let ((fmt (math-format-stack-value (list math-rewrite-whole-expr nil nil))))
  175. (with-current-buffer trace-buffer
  176. (setq truncate-lines t)
  177. (goto-char (point-max))
  178. (insert "\n\nBegin rewriting\n" fmt "\n"))))
  179. (or math-mt-many (setq math-mt-many (or (nth 1 (car crules))
  180. math-rewrite-default-iters)))
  181. (if (equal math-mt-many '(var inf var-inf)) (setq math-mt-many 1000000))
  182. (if (equal math-mt-many '(neg (var inf var-inf))) (setq math-mt-many -1000000))
  183. (math-rewrite-phase (nth 3 (car crules)))
  184. (if trace-buffer
  185. (let ((fmt (math-format-stack-value (list math-rewrite-whole-expr nil nil))))
  186. (with-current-buffer trace-buffer
  187. (insert "\nDone rewriting"
  188. (if (= math-mt-many 0) " (reached iteration limit)" "")
  189. ":\n" fmt "\n"))))
  190. math-rewrite-whole-expr))
  191. (defun math-rewrite-phase (sched)
  192. (while (and sched (/= math-mt-many 0))
  193. (if (listp (car sched))
  194. (while (let ((save-expr math-rewrite-whole-expr))
  195. (math-rewrite-phase (car sched))
  196. (not (equal math-rewrite-whole-expr save-expr))))
  197. (if (symbolp (car sched))
  198. (progn
  199. (setq math-rewrite-whole-expr
  200. (math-normalize (list (car sched) math-rewrite-whole-expr)))
  201. (if trace-buffer
  202. (let ((fmt (math-format-stack-value
  203. (list math-rewrite-whole-expr nil nil))))
  204. (with-current-buffer trace-buffer
  205. (insert "\ncall "
  206. (substring (symbol-name (car sched)) 9)
  207. ":\n" fmt "\n")))))
  208. (let ((math-rewrite-phase (car sched)))
  209. (if trace-buffer
  210. (with-current-buffer trace-buffer
  211. (insert (format "\n(Phase %d)\n" math-rewrite-phase))))
  212. (while (let ((save-expr math-rewrite-whole-expr))
  213. (setq math-rewrite-whole-expr (math-normalize
  214. (math-map-tree-rec math-rewrite-whole-expr)))
  215. (not (equal math-rewrite-whole-expr save-expr)))))))
  216. (setq sched (cdr sched))))
  217. (defun calcFunc-rewrite (expr rules &optional many)
  218. (or (null many) (integerp many)
  219. (equal many '(var inf var-inf)) (equal many '(neg (var inf var-inf)))
  220. (math-reject-arg many 'fixnump))
  221. (condition-case err
  222. (math-rewrite expr rules (or many 1))
  223. (error (math-reject-arg rules (nth 1 err)))))
  224. (defun calcFunc-match (pat vec)
  225. (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  226. (condition-case err
  227. (math-match-patterns pat vec nil)
  228. (error (math-reject-arg pat (nth 1 err)))))
  229. (defun calcFunc-matchnot (pat vec)
  230. (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  231. (condition-case err
  232. (math-match-patterns pat vec t)
  233. (error (math-reject-arg pat (nth 1 err)))))
  234. (defun math-match-patterns (pat vec &optional not-flag)
  235. (let ((newvec nil)
  236. (crules (math-compile-patterns pat)))
  237. (while (setq vec (cdr vec))
  238. (if (eq (not (math-apply-rewrites (car vec) crules))
  239. not-flag)
  240. (setq newvec (cons (car vec) newvec))))
  241. (cons 'vec (nreverse newvec))))
  242. (defun calcFunc-matches (expr pat)
  243. (condition-case err
  244. (if (math-apply-rewrites expr (math-compile-patterns pat))
  245. 1
  246. 0)
  247. (error (math-reject-arg pat (nth 1 err)))))
  248. (defun calcFunc-vmatches (expr pat)
  249. (condition-case err
  250. (or (math-apply-rewrites expr (math-compile-patterns pat))
  251. 0)
  252. (error (math-reject-arg pat (nth 1 err)))))
  253. ;; A compiled rule set is an a-list of entries whose cars are functors,
  254. ;; and whose cdrs are lists of rules. If there are rules with no
  255. ;; well-defined head functor, they are included on all lists and also
  256. ;; on an extra list whose car is nil.
  257. ;;
  258. ;; The first entry in the a-list is of the form (schedule A B C ...).
  259. ;;
  260. ;; Rule list entries take the form (regs prog head phases), where:
  261. ;;
  262. ;; regs is a vector of match registers.
  263. ;;
  264. ;; prog is a match program (see below).
  265. ;;
  266. ;; head is a rare function name appearing in the rule body (but not the
  267. ;; head of the whole rule), or nil if none.
  268. ;;
  269. ;; phases is a list of phase numbers for which the rule is enabled.
  270. ;;
  271. ;; A match program is a list of match instructions.
  272. ;;
  273. ;; In the following, "part" is a register number that contains the
  274. ;; subexpression to be operated on.
  275. ;;
  276. ;; Register 0 is the whole expression being matched. The others are
  277. ;; meta-variables in the pattern, temporaries used for matching and
  278. ;; backtracking, and constant expressions.
  279. ;;
  280. ;; (same part reg)
  281. ;; The selected part must be math-equal to the contents of "reg".
  282. ;;
  283. ;; (same-neg part reg)
  284. ;; The selected part must be math-equal to the negative of "reg".
  285. ;;
  286. ;; (copy part reg)
  287. ;; The selected part is copied into "reg". (Rarely used.)
  288. ;;
  289. ;; (copy-neg part reg)
  290. ;; The negative of the selected part is copied into "reg".
  291. ;;
  292. ;; (integer part)
  293. ;; The selected part must be an integer.
  294. ;;
  295. ;; (real part)
  296. ;; The selected part must be a real.
  297. ;;
  298. ;; (constant part)
  299. ;; The selected part must be a constant.
  300. ;;
  301. ;; (negative part)
  302. ;; The selected part must "look" negative.
  303. ;;
  304. ;; (rel part op reg)
  305. ;; The selected part must satisfy "part op reg", where "op"
  306. ;; is one of the 6 relational ops, and "reg" is a register.
  307. ;;
  308. ;; (mod part modulo value)
  309. ;; The selected part must satisfy "part % modulo = value", where
  310. ;; "modulo" and "value" are constants.
  311. ;;
  312. ;; (func part head reg1 reg2 ... regn)
  313. ;; The selected part must be an n-ary call to function "head".
  314. ;; The arguments are stored in "reg1" through "regn".
  315. ;;
  316. ;; (func-def part head defs reg1 reg2 ... regn)
  317. ;; The selected part must be an n-ary call to function "head".
  318. ;; "Defs" is a list of value/register number pairs for default args.
  319. ;; If a match, assign default values to registers and then skip
  320. ;; immediately over any following "func-def" instructions and
  321. ;; the following "func" instruction. If wrong number of arguments,
  322. ;; proceed to the following "func-def" or "func" instruction.
  323. ;;
  324. ;; (func-opt part head defs reg1)
  325. ;; Like func-def with "n=1", except that if the selected part is
  326. ;; not a call to "head", then the part itself successfully matches
  327. ;; "reg1" (and the defaults are assigned).
  328. ;;
  329. ;; (try part heads mark reg1 [def])
  330. ;; The selected part must be a function of the correct type which is
  331. ;; associative and/or commutative. "Heads" is a list of acceptable
  332. ;; types. An initial assignment of arguments to "reg1" is tried.
  333. ;; If the program later fails, it backtracks to this instruction
  334. ;; and tries other assignments of arguments to "reg1".
  335. ;; If "def" exists and normal matching fails, backtrack and assign
  336. ;; "part" to "reg1", and "def" to "reg2" in the following "try2".
  337. ;; The "mark" is a vector of size 5; only "mark[3-4]" are initialized.
  338. ;; "mark[0]" points to the argument list; "mark[1]" points to the
  339. ;; current argument; "mark[2]" is 0 if there are two arguments,
  340. ;; 1 if reg1 is matching single arguments, 2 if reg2 is matching
  341. ;; single arguments (a+b+c+d is never split as (a+b)+(c+d)), or
  342. ;; 3 if reg2 is matching "def"; "mark[3]" is 0 if the function must
  343. ;; have two arguments, 1 if phase-2 can be skipped, 2 if full
  344. ;; backtracking is necessary; "mark[4]" is t if the arguments have
  345. ;; been switched from the order given in the original pattern.
  346. ;;
  347. ;; (try2 try reg2)
  348. ;; Every "try" will be followed by a "try2" whose "try" field is
  349. ;; a pointer to the corresponding "try". The arguments which were
  350. ;; not stored in "reg1" by that "try" are now stored in "reg2".
  351. ;;
  352. ;; (alt instr nil mark)
  353. ;; Basic backtracking. Execute the instruction sequence "instr".
  354. ;; If this fails, back up and execute following the "alt" instruction.
  355. ;; The "mark" must be the vector "[nil nil 4]". The "instr" sequence
  356. ;; should execute "end-alt" at the end.
  357. ;;
  358. ;; (end-alt ptr)
  359. ;; Register success of the first alternative of a previous "alt".
  360. ;; "Ptr" is a pointer to the next instruction following that "alt".
  361. ;;
  362. ;; (apply part reg1 reg2)
  363. ;; The selected part must be a function call. The functor
  364. ;; (as a variable name) is stored in "reg1"; the arguments
  365. ;; (as a vector) are stored in "reg2".
  366. ;;
  367. ;; (cons part reg1 reg2)
  368. ;; The selected part must be a nonempty vector. The first element
  369. ;; of the vector is stored in "reg1"; the rest of the vector
  370. ;; (as another vector) is stored in "reg2".
  371. ;;
  372. ;; (rcons part reg1 reg2)
  373. ;; The selected part must be a nonempty vector. The last element
  374. ;; of the vector is stored in "reg2"; the rest of the vector
  375. ;; (as another vector) is stored in "reg1".
  376. ;;
  377. ;; (select part reg)
  378. ;; If the selected part is a unary call to function "select", its
  379. ;; argument is stored in "reg"; otherwise (provided this is an `a r'
  380. ;; and not a `g r' command) the selected part is stored in "reg".
  381. ;;
  382. ;; (cond expr)
  383. ;; The "expr", with registers substituted, must simplify to
  384. ;; a non-zero value.
  385. ;;
  386. ;; (let reg expr)
  387. ;; Evaluate "expr" and store the result in "reg". Always succeeds.
  388. ;;
  389. ;; (done rhs remember)
  390. ;; Rewrite the expression to "rhs", with register substituted.
  391. ;; Normalize; if the result is different from the original
  392. ;; expression, the match has succeeded. This is the last
  393. ;; instruction of every program. If "remember" is non-nil,
  394. ;; record the result of the match as a new literal rule.
  395. ;; Pseudo-functions related to rewrites:
  396. ;;
  397. ;; In patterns: quote, plain, condition, opt, apply, cons, select
  398. ;;
  399. ;; In righthand sides: quote, plain, eval, evalsimp, evalextsimp,
  400. ;; apply, cons, select
  401. ;;
  402. ;; In conditions: let + same as for righthand sides
  403. ;; Some optimizations that would be nice to have:
  404. ;;
  405. ;; * Merge registers with disjoint lifetimes.
  406. ;; * Merge constant registers with equivalent values.
  407. ;;
  408. ;; * If an argument of a commutative op math-depends neither on the
  409. ;; rest of the pattern nor on any of the conditions, then no backtracking
  410. ;; should be done for that argument. (This won't apply to very many
  411. ;; cases.)
  412. ;;
  413. ;; * If top functor is "select", and its argument is a unique function,
  414. ;; add the rule to the lists for both "select" and that function.
  415. ;; (Currently rules like this go on the "nil" list.)
  416. ;; Same for "func-opt" functions. (Though not urgent for these.)
  417. ;;
  418. ;; * Shouldn't evaluate a "let" condition until the end, or until it
  419. ;; would enable another condition to be evaluated.
  420. ;;
  421. ;; Some additional features to add / things to think about:
  422. ;;;
  423. ;;; * Figure out what happens to "a +/- b" and "a +/- opt(b)".
  424. ;;;
  425. ;;; * Same for interval forms.
  426. ;;;
  427. ;;; * Have a name(v,pat) pattern which matches pat, and gives the
  428. ;;; whole match the name v. Beware of circular structures!
  429. ;;;
  430. (defun math-compile-patterns (pats)
  431. (if (and (eq (car-safe pats) 'var)
  432. (calc-var-value (nth 2 pats)))
  433. (let ((prop (get (nth 2 pats) 'math-pattern-cache)))
  434. (or prop
  435. (put (nth 2 pats) 'math-pattern-cache (setq prop (list nil))))
  436. (or (eq (car prop) (symbol-value (nth 2 pats)))
  437. (progn
  438. (setcdr prop (math-compile-patterns
  439. (symbol-value (nth 2 pats))))
  440. (setcar prop (symbol-value (nth 2 pats)))))
  441. (cdr prop))
  442. (let ((math-rewrite-whole t))
  443. (cdr (math-compile-rewrites (cons
  444. 'vec
  445. (mapcar (function (lambda (x)
  446. (list 'vec x t)))
  447. (if (eq (car-safe pats) 'vec)
  448. (cdr pats)
  449. (list pats)))))))))
  450. (defvar math-rewrite-whole nil)
  451. (defvar math-make-import-list nil)
  452. ;; The variable math-import-list is local to part of math-compile-rewrites,
  453. ;; but is also used in a different part, and so the local version could
  454. ;; be affected by the non-local version when math-compile-rewrites calls itself.
  455. (defvar math-import-list nil)
  456. ;; The variables math-regs, math-num-regs, math-prog-last, math-bound-vars,
  457. ;; math-conds, math-copy-neg, math-rhs, math-pattern, math-remembering and
  458. ;; math-aliased-vars are local to math-compile-rewrites,
  459. ;; but are used by many functions math-rwcomp-*, which are called by
  460. ;; math-compile-rewrites.
  461. (defvar math-regs)
  462. (defvar math-num-regs)
  463. (defvar math-prog-last)
  464. (defvar math-bound-vars)
  465. (defvar math-conds)
  466. (defvar math-copy-neg)
  467. (defvar math-rhs)
  468. (defvar math-pattern)
  469. (defvar math-remembering)
  470. (defvar math-aliased-vars)
  471. (defun math-compile-rewrites (rules &optional name)
  472. (if (eq (car-safe rules) 'var)
  473. (let ((prop (get (nth 2 rules) 'math-rewrite-cache))
  474. (math-import-list nil)
  475. (math-make-import-list t)
  476. p)
  477. (or (calc-var-value (nth 2 rules))
  478. (error "Rules variable %s has no stored value" (nth 1 rules)))
  479. (or prop
  480. (put (nth 2 rules) 'math-rewrite-cache
  481. (setq prop (list (list (cons (nth 2 rules) nil))))))
  482. (setq p (car prop))
  483. (while (and p (eq (symbol-value (car (car p))) (cdr (car p))))
  484. (setq p (cdr p)))
  485. (or (null p)
  486. (progn
  487. (message "Compiling rule set %s..." (nth 1 rules))
  488. (setcdr prop (math-compile-rewrites
  489. (symbol-value (nth 2 rules))
  490. (nth 2 rules)))
  491. (message "Compiling rule set %s...done" (nth 1 rules))
  492. (setcar prop (cons (cons (nth 2 rules)
  493. (symbol-value (nth 2 rules)))
  494. math-import-list))))
  495. (cdr prop))
  496. (if (or (not (eq (car-safe rules) 'vec))
  497. (and (memq (length rules) '(3 4))
  498. (let ((p rules))
  499. (while (and (setq p (cdr p))
  500. (memq (car-safe (car p))
  501. '(vec
  502. calcFunc-assign
  503. calcFunc-condition
  504. calcFunc-import
  505. calcFunc-phase
  506. calcFunc-schedule
  507. calcFunc-iterations))))
  508. p)))
  509. (setq rules (list rules))
  510. (setq rules (cdr rules)))
  511. (if (assq 'calcFunc-import rules)
  512. (let ((pp (setq rules (copy-sequence rules)))
  513. p part)
  514. (while (setq p (car (cdr pp)))
  515. (if (eq (car-safe p) 'calcFunc-import)
  516. (progn
  517. (setcdr pp (cdr (cdr pp)))
  518. (or (and (eq (car-safe (nth 1 p)) 'var)
  519. (setq part (calc-var-value (nth 2 (nth 1 p))))
  520. (memq (car-safe part) '(vec
  521. calcFunc-assign
  522. calcFunc-condition)))
  523. (error "Argument of import() must be a rules variable"))
  524. (if math-make-import-list
  525. (setq math-import-list
  526. (cons (cons (nth 2 (nth 1 p))
  527. (symbol-value (nth 2 (nth 1 p))))
  528. math-import-list)))
  529. (while (setq p (cdr (cdr p)))
  530. (or (cdr p)
  531. (error "import() must have odd number of arguments"))
  532. (setq part (math-rwcomp-substitute part
  533. (car p) (nth 1 p))))
  534. (if (eq (car-safe part) 'vec)
  535. (setq part (cdr part))
  536. (setq part (list part)))
  537. (setcdr pp (append part (cdr pp))))
  538. (setq pp (cdr pp))))))
  539. (let ((rule-set nil)
  540. (all-heads nil)
  541. (nil-rules nil)
  542. (rule-count 0)
  543. (math-schedule nil)
  544. (math-iterations nil)
  545. (math-phases nil)
  546. (math-all-phases nil)
  547. (math-remembering nil)
  548. math-pattern math-rhs math-conds)
  549. (while rules
  550. (cond
  551. ((and (eq (car-safe (car rules)) 'calcFunc-iterations)
  552. (= (length (car rules)) 2))
  553. (or (integerp (nth 1 (car rules)))
  554. (equal (nth 1 (car rules)) '(var inf var-inf))
  555. (equal (nth 1 (car rules)) '(neg (var inf var-inf)))
  556. (error "Invalid argument for iterations(n)"))
  557. (or math-iterations
  558. (setq math-iterations (nth 1 (car rules)))))
  559. ((eq (car-safe (car rules)) 'calcFunc-schedule)
  560. (or math-schedule
  561. (setq math-schedule (math-parse-schedule (cdr (car rules))))))
  562. ((eq (car-safe (car rules)) 'calcFunc-phase)
  563. (setq math-phases (cdr (car rules)))
  564. (if (equal math-phases '((var all var-all)))
  565. (setq math-phases nil))
  566. (let ((p math-phases))
  567. (while p
  568. (or (integerp (car p))
  569. (error "Phase numbers must be small integers"))
  570. (or (memq (car p) math-all-phases)
  571. (setq math-all-phases (cons (car p) math-all-phases)))
  572. (setq p (cdr p)))))
  573. ((or (and (eq (car-safe (car rules)) 'vec)
  574. (cdr (cdr (car rules)))
  575. (not (nthcdr 4 (car rules)))
  576. (setq math-conds (nth 3 (car rules))
  577. math-rhs (nth 2 (car rules))
  578. math-pattern (nth 1 (car rules))))
  579. (progn
  580. (setq math-conds nil
  581. math-pattern (car rules))
  582. (while (and (eq (car-safe math-pattern) 'calcFunc-condition)
  583. (= (length math-pattern) 3))
  584. (let ((cond (nth 2 math-pattern)))
  585. (setq math-conds (if math-conds
  586. (list 'calcFunc-land math-conds cond)
  587. cond)
  588. math-pattern (nth 1 math-pattern))))
  589. (and (eq (car-safe math-pattern) 'calcFunc-assign)
  590. (= (length math-pattern) 3)
  591. (setq math-rhs (nth 2 math-pattern)
  592. math-pattern (nth 1 math-pattern)))))
  593. (let* ((math-prog (list nil))
  594. (math-prog-last math-prog)
  595. (math-num-regs 1)
  596. (math-regs (list (list nil 0 nil nil)))
  597. (math-bound-vars nil)
  598. (math-aliased-vars nil)
  599. (math-copy-neg nil))
  600. (setq math-conds (and math-conds (math-flatten-lands math-conds)))
  601. (math-rwcomp-pattern math-pattern 0)
  602. (while math-conds
  603. (let ((expr (car math-conds)))
  604. (setq math-conds (cdr math-conds))
  605. (math-rwcomp-cond-instr expr)))
  606. (math-rwcomp-instr 'done
  607. (if (eq math-rhs t)
  608. (cons 'vec
  609. (delq
  610. nil
  611. (nreverse
  612. (mapcar
  613. (function
  614. (lambda (v)
  615. (and (car v)
  616. (list
  617. 'calcFunc-assign
  618. (math-build-var-name
  619. (car v))
  620. (math-rwcomp-register-expr
  621. (nth 1 v))))))
  622. math-regs))))
  623. (math-rwcomp-match-vars math-rhs))
  624. math-remembering)
  625. (setq math-prog (cdr math-prog))
  626. (let* ((heads (math-rewrite-heads math-pattern))
  627. (rule (list (vconcat
  628. (nreverse
  629. (mapcar (function (lambda (x) (nth 3 x)))
  630. math-regs)))
  631. math-prog
  632. heads
  633. math-phases))
  634. (head (and (not (Math-primp math-pattern))
  635. (not (and (eq (car (car math-prog)) 'try)
  636. (nth 5 (car math-prog))))
  637. (not (memq (car (car math-prog)) '(func-opt
  638. apply
  639. select
  640. alt)))
  641. (if (memq (car (car math-prog)) '(func
  642. func-def))
  643. (nth 2 (car math-prog))
  644. (if (eq (car math-pattern) 'calcFunc-quote)
  645. (car-safe (nth 1 math-pattern))
  646. (car math-pattern))))))
  647. (let (found)
  648. (while heads
  649. (if (setq found (assq (car heads) all-heads))
  650. (setcdr found (1+ (cdr found)))
  651. (setq all-heads (cons (cons (car heads) 1) all-heads)))
  652. (setq heads (cdr heads))))
  653. (if (eq head '-) (setq head '+))
  654. (if (memq head '(calcFunc-cons calcFunc-rcons)) (setq head 'vec))
  655. (if head
  656. (progn
  657. (nconc (or (assq head rule-set)
  658. (car (setq rule-set (cons (cons head
  659. (copy-sequence
  660. nil-rules))
  661. rule-set))))
  662. (list rule))
  663. (if (eq head '*)
  664. (nconc (or (assq '/ rule-set)
  665. (car (setq rule-set (cons (cons
  666. '/
  667. (copy-sequence
  668. nil-rules))
  669. rule-set))))
  670. (list rule))))
  671. (setq nil-rules (nconc nil-rules (list rule)))
  672. (let ((ptr rule-set))
  673. (while ptr
  674. (nconc (car ptr) (list rule))
  675. (setq ptr (cdr ptr))))))))
  676. (t
  677. (error "Rewrite rule set must be a vector of A := B rules")))
  678. (setq rules (cdr rules)))
  679. (if nil-rules
  680. (setq rule-set (cons (cons nil nil-rules) rule-set)))
  681. (setq all-heads (mapcar 'car
  682. (sort all-heads (function
  683. (lambda (x y)
  684. (< (cdr x) (cdr y)))))))
  685. (let ((set rule-set)
  686. rule heads ptr)
  687. (while set
  688. (setq rule (cdr (car set)))
  689. (while rule
  690. (if (consp (setq heads (nth 2 (car rule))))
  691. (progn
  692. (setq heads (delq (car (car set)) heads)
  693. ptr all-heads)
  694. (while (and ptr (not (memq (car ptr) heads)))
  695. (setq ptr (cdr ptr)))
  696. (setcar (nthcdr 2 (car rule)) (car ptr))))
  697. (setq rule (cdr rule)))
  698. (setq set (cdr set))))
  699. (let ((plus (assq '+ rule-set)))
  700. (if plus
  701. (setq rule-set (cons (cons '- (cdr plus)) rule-set))))
  702. (cons (list 'schedule math-iterations name
  703. (or math-schedule
  704. (sort math-all-phases '<)
  705. (list 1)))
  706. rule-set))))
  707. (defun math-flatten-lands (expr)
  708. (if (eq (car-safe expr) 'calcFunc-land)
  709. (append (math-flatten-lands (nth 1 expr))
  710. (math-flatten-lands (nth 2 expr)))
  711. (list expr)))
  712. ;; The variables math-rewrite-heads-heads (i.e.; heads for math-rewrite-heads)
  713. ;; math-rewrite-heads-blanks and math-rewrite-heads-skips are local to
  714. ;; math-rewrite-heads, but used by math-rewrite-heads-rec, which is called by
  715. ;; math-rewrite-heads.
  716. (defvar math-rewrite-heads-heads)
  717. (defvar math-rewrite-heads-skips)
  718. (defvar math-rewrite-heads-blanks)
  719. (defun math-rewrite-heads (expr &optional more all)
  720. (let ((math-rewrite-heads-heads more)
  721. (math-rewrite-heads-skips (and (not all)
  722. '(calcFunc-apply calcFunc-condition calcFunc-opt
  723. calcFunc-por calcFunc-pnot)))
  724. (math-rewrite-heads-blanks (and (not all)
  725. '(calcFunc-quote calcFunc-plain calcFunc-select
  726. calcFunc-cons calcFunc-rcons
  727. calcFunc-pand))))
  728. (or (Math-primp expr)
  729. (math-rewrite-heads-rec expr))
  730. math-rewrite-heads-heads))
  731. (defun math-rewrite-heads-rec (expr)
  732. (or (memq (car expr) math-rewrite-heads-skips)
  733. (progn
  734. (or (memq (car expr) math-rewrite-heads-heads)
  735. (memq (car expr) math-rewrite-heads-blanks)
  736. (memq 'algebraic (get (car expr) 'math-rewrite-props))
  737. (setq math-rewrite-heads-heads (cons (car expr) math-rewrite-heads-heads)))
  738. (while (setq expr (cdr expr))
  739. (or (Math-primp (car expr))
  740. (math-rewrite-heads-rec (car expr)))))))
  741. (defun math-parse-schedule (sched)
  742. (mapcar (function
  743. (lambda (s)
  744. (if (integerp s)
  745. s
  746. (if (math-vectorp s)
  747. (math-parse-schedule (cdr s))
  748. (if (eq (car-safe s) 'var)
  749. (math-var-to-calcFunc s)
  750. (error "Improper component in rewrite schedule"))))))
  751. sched))
  752. (defun math-rwcomp-match-vars (expr)
  753. (if (Math-primp expr)
  754. (if (eq (car-safe expr) 'var)
  755. (let ((entry (assq (nth 2 expr) math-regs)))
  756. (if entry
  757. (math-rwcomp-register-expr (nth 1 entry))
  758. expr))
  759. expr)
  760. (if (and (eq (car expr) 'calcFunc-quote)
  761. (= (length expr) 2))
  762. (math-rwcomp-match-vars (nth 1 expr))
  763. (if (and (eq (car expr) 'calcFunc-plain)
  764. (= (length expr) 2)
  765. (not (Math-primp (nth 1 expr))))
  766. (list (car expr)
  767. (cons (car (nth 1 expr))
  768. (mapcar 'math-rwcomp-match-vars (cdr (nth 1 expr)))))
  769. (cons (car expr)
  770. (mapcar 'math-rwcomp-match-vars (cdr expr)))))))
  771. (defun math-rwcomp-register-expr (num)
  772. (let ((entry (nth (1- (- math-num-regs num)) math-regs)))
  773. (if (nth 2 entry)
  774. (list 'neg (list 'calcFunc-register (nth 1 entry)))
  775. (list 'calcFunc-register (nth 1 entry)))))
  776. ;; The variables math-rwcomp-subst-old, math-rwcomp-subst-new,
  777. ;; math-rwcomp-subst-old-func and math-rwcomp-subst-new-func
  778. ;; are local to math-rwcomp-substitute, but are used by
  779. ;; math-rwcomp-subst-rec, which is called by math-rwcomp-substitute.
  780. (defvar math-rwcomp-subst-new)
  781. (defvar math-rwcomp-subst-old)
  782. (defvar math-rwcomp-subst-new-func)
  783. (defvar math-rwcomp-subst-old-func)
  784. (defun math-rwcomp-substitute (expr math-rwcomp-subst-old math-rwcomp-subst-new)
  785. (if (and (eq (car-safe math-rwcomp-subst-old) 'var)
  786. (memq (car-safe math-rwcomp-subst-new) '(var calcFunc-lambda)))
  787. (let ((math-rwcomp-subst-old-func (math-var-to-calcFunc math-rwcomp-subst-old))
  788. (math-rwcomp-subst-new-func (math-var-to-calcFunc math-rwcomp-subst-new)))
  789. (math-rwcomp-subst-rec expr))
  790. (let ((math-rwcomp-subst-old-func nil))
  791. (math-rwcomp-subst-rec expr))))
  792. (defun math-rwcomp-subst-rec (expr)
  793. (cond ((equal expr math-rwcomp-subst-old) math-rwcomp-subst-new)
  794. ((Math-primp expr) expr)
  795. (t (if (eq (car expr) math-rwcomp-subst-old-func)
  796. (math-build-call math-rwcomp-subst-new-func
  797. (mapcar 'math-rwcomp-subst-rec
  798. (cdr expr)))
  799. (cons (car expr)
  800. (mapcar 'math-rwcomp-subst-rec (cdr expr)))))))
  801. (defvar math-rwcomp-tracing nil)
  802. (defun math-rwcomp-trace (instr)
  803. (when math-rwcomp-tracing
  804. (terpri) (princ instr))
  805. instr)
  806. (defun math-rwcomp-instr (&rest instr)
  807. (setcdr math-prog-last
  808. (setq math-prog-last (list (math-rwcomp-trace instr)))))
  809. (defun math-rwcomp-multi-instr (tail &rest instr)
  810. (setcdr math-prog-last
  811. (setq math-prog-last (list (math-rwcomp-trace (append instr tail))))))
  812. (defun math-rwcomp-bind-var (reg var)
  813. (setcar (math-rwcomp-reg-entry reg) (nth 2 var))
  814. (setq math-bound-vars (cons (nth 2 var) math-bound-vars))
  815. (math-rwcomp-do-conditions))
  816. (defun math-rwcomp-unbind-vars (mark)
  817. (while (not (eq math-bound-vars mark))
  818. (setcar (assq (car math-bound-vars) math-regs) nil)
  819. (setq math-bound-vars (cdr math-bound-vars))))
  820. (defun math-rwcomp-do-conditions ()
  821. (let ((cond math-conds))
  822. (while cond
  823. (if (math-rwcomp-all-regs-done (car cond))
  824. (let ((expr (car cond)))
  825. (setq math-conds (delq (car cond) math-conds))
  826. (setcar cond 1)
  827. (math-rwcomp-cond-instr expr)))
  828. (setq cond (cdr cond)))))
  829. (defun math-rwcomp-cond-instr (expr)
  830. (let (op arg)
  831. (cond ((and (eq (car-safe expr) 'calcFunc-matches)
  832. (= (length expr) 3)
  833. (eq (car-safe (setq arg (math-rwcomp-match-vars (nth 1 expr))))
  834. 'calcFunc-register))
  835. (math-rwcomp-pattern (nth 2 expr) (nth 1 arg)))
  836. ((math-numberp (setq expr (math-rwcomp-match-vars expr)))
  837. (if (Math-zerop expr)
  838. (math-rwcomp-instr 'backtrack)))
  839. ((and (eq (car expr) 'calcFunc-let)
  840. (= (length expr) 3))
  841. (let ((reg (math-rwcomp-reg)))
  842. (math-rwcomp-instr 'let reg (nth 2 expr))
  843. (math-rwcomp-pattern (nth 1 expr) reg)))
  844. ((and (eq (car expr) 'calcFunc-let)
  845. (= (length expr) 2)
  846. (eq (car-safe (nth 1 expr)) 'calcFunc-assign)
  847. (= (length (nth 1 expr)) 3))
  848. (let ((reg (math-rwcomp-reg)))
  849. (math-rwcomp-instr 'let reg (nth 2 (nth 1 expr)))
  850. (math-rwcomp-pattern (nth 1 (nth 1 expr)) reg)))
  851. ((and (setq op (cdr (assq (car-safe expr)
  852. '( (calcFunc-integer . integer)
  853. (calcFunc-real . real)
  854. (calcFunc-constant . constant)
  855. (calcFunc-negative . negative) ))))
  856. (= (length expr) 2)
  857. (or (and (eq (car-safe (nth 1 expr)) 'neg)
  858. (memq op '(integer real constant))
  859. (setq arg (nth 1 (nth 1 expr))))
  860. (setq arg (nth 1 expr)))
  861. (eq (car-safe (setq arg (nth 1 expr))) 'calcFunc-register))
  862. (math-rwcomp-instr op (nth 1 arg)))
  863. ((and (assq (car-safe expr) calc-tweak-eqn-table)
  864. (= (length expr) 3)
  865. (eq (car-safe (nth 1 expr)) 'calcFunc-register))
  866. (if (math-constp (nth 2 expr))
  867. (let ((reg (math-rwcomp-reg)))
  868. (setcar (nthcdr 3 (car math-regs)) (nth 2 expr))
  869. (math-rwcomp-instr 'rel (nth 1 (nth 1 expr))
  870. (car expr) reg))
  871. (if (eq (car (nth 2 expr)) 'calcFunc-register)
  872. (math-rwcomp-instr 'rel (nth 1 (nth 1 expr))
  873. (car expr) (nth 1 (nth 2 expr)))
  874. (math-rwcomp-instr 'cond expr))))
  875. ((and (eq (car-safe expr) 'calcFunc-eq)
  876. (= (length expr) 3)
  877. (eq (car-safe (nth 1 expr)) '%)
  878. (eq (car-safe (nth 1 (nth 1 expr))) 'calcFunc-register)
  879. (math-constp (nth 2 (nth 1 expr)))
  880. (math-constp (nth 2 expr)))
  881. (math-rwcomp-instr 'mod (nth 1 (nth 1 (nth 1 expr)))
  882. (nth 2 (nth 1 expr)) (nth 2 expr)))
  883. ((equal expr '(var remember var-remember))
  884. (setq math-remembering 1))
  885. ((and (eq (car-safe expr) 'calcFunc-remember)
  886. (= (length expr) 2))
  887. (setq math-remembering (if math-remembering
  888. (list 'calcFunc-lor
  889. math-remembering (nth 1 expr))
  890. (nth 1 expr))))
  891. (t (math-rwcomp-instr 'cond expr)))))
  892. (defun math-rwcomp-same-instr (reg1 reg2 neg)
  893. (math-rwcomp-instr (if (eq (eq (nth 2 (math-rwcomp-reg-entry reg1))
  894. (nth 2 (math-rwcomp-reg-entry reg2)))
  895. neg)
  896. 'same-neg
  897. 'same)
  898. reg1 reg2))
  899. (defun math-rwcomp-copy-instr (reg1 reg2 neg)
  900. (if (eq (eq (nth 2 (math-rwcomp-reg-entry reg1))
  901. (nth 2 (math-rwcomp-reg-entry reg2)))
  902. neg)
  903. (math-rwcomp-instr 'copy-neg reg1 reg2)
  904. (or (eq reg1 reg2)
  905. (math-rwcomp-instr 'copy reg1 reg2))))
  906. (defun math-rwcomp-reg ()
  907. (prog1
  908. math-num-regs
  909. (setq math-regs (cons (list nil math-num-regs nil 0) math-regs)
  910. math-num-regs (1+ math-num-regs))))
  911. (defun math-rwcomp-reg-entry (num)
  912. (nth (1- (- math-num-regs num)) math-regs))
  913. (defun math-rwcomp-pattern (expr part &optional not-direct)
  914. (cond ((or (math-rwcomp-no-vars expr)
  915. (and (eq (car expr) 'calcFunc-quote)
  916. (= (length expr) 2)
  917. (setq expr (nth 1 expr))))
  918. (if (eq (car-safe expr) 'calcFunc-register)
  919. (math-rwcomp-same-instr part (nth 1 expr) nil)
  920. (let ((reg (math-rwcomp-reg)))
  921. (setcar (nthcdr 3 (car math-regs)) expr)
  922. (math-rwcomp-same-instr part reg nil))))
  923. ((eq (car expr) 'var)
  924. (let ((entry (assq (nth 2 expr) math-regs)))
  925. (if entry
  926. (math-rwcomp-same-instr part (nth 1 entry) nil)
  927. (if not-direct
  928. (let ((reg (math-rwcomp-reg)))
  929. (math-rwcomp-pattern expr reg)
  930. (math-rwcomp-copy-instr part reg nil))
  931. (if (setq entry (assq (nth 2 expr) math-aliased-vars))
  932. (progn
  933. (setcar (math-rwcomp-reg-entry (nth 1 entry))
  934. (nth 2 expr))
  935. (setcar entry nil)
  936. (math-rwcomp-copy-instr part (nth 1 entry) nil))
  937. (math-rwcomp-bind-var part expr))))))
  938. ((and (eq (car expr) 'calcFunc-select)
  939. (= (length expr) 2))
  940. (let ((reg (math-rwcomp-reg)))
  941. (math-rwcomp-instr 'select part reg)
  942. (math-rwcomp-pattern (nth 1 expr) reg)))
  943. ((and (eq (car expr) 'calcFunc-opt)
  944. (memq (length expr) '(2 3)))
  945. (error "opt( ) occurs in context where it is not allowed"))
  946. ((eq (car expr) 'neg)
  947. (if (eq (car (nth 1 expr)) 'var)
  948. (let ((entry (assq (nth 2 (nth 1 expr)) math-regs)))
  949. (if entry
  950. (math-rwcomp-same-instr part (nth 1 entry) t)
  951. (if math-copy-neg
  952. (let ((reg (math-rwcomp-best-reg (nth 1 expr))))
  953. (math-rwcomp-copy-instr part reg t)
  954. (math-rwcomp-pattern (nth 1 expr) reg))
  955. (setcar (cdr (cdr (math-rwcomp-reg-entry part))) t)
  956. (math-rwcomp-pattern (nth 1 expr) part))))
  957. (if (math-rwcomp-is-algebraic (nth 1 expr))
  958. (math-rwcomp-cond-instr (list 'calcFunc-eq
  959. (math-rwcomp-register-expr part)
  960. expr))
  961. (let ((reg (math-rwcomp-reg)))
  962. (math-rwcomp-instr 'func part 'neg reg)
  963. (math-rwcomp-pattern (nth 1 expr) reg)))))
  964. ((and (eq (car expr) 'calcFunc-apply)
  965. (= (length expr) 3))
  966. (let ((reg1 (math-rwcomp-reg))
  967. (reg2 (math-rwcomp-reg)))
  968. (math-rwcomp-instr 'apply part reg1 reg2)
  969. (math-rwcomp-pattern (nth 1 expr) reg1)
  970. (math-rwcomp-pattern (nth 2 expr) reg2)))
  971. ((and (eq (car expr) 'calcFunc-cons)
  972. (= (length expr) 3))
  973. (let ((reg1 (math-rwcomp-reg))
  974. (reg2 (math-rwcomp-reg)))
  975. (math-rwcomp-instr 'cons part reg1 reg2)
  976. (math-rwcomp-pattern (nth 1 expr) reg1)
  977. (math-rwcomp-pattern (nth 2 expr) reg2)))
  978. ((and (eq (car expr) 'calcFunc-rcons)
  979. (= (length expr) 3))
  980. (let ((reg1 (math-rwcomp-reg))
  981. (reg2 (math-rwcomp-reg)))
  982. (math-rwcomp-instr 'rcons part reg1 reg2)
  983. (math-rwcomp-pattern (nth 1 expr) reg1)
  984. (math-rwcomp-pattern (nth 2 expr) reg2)))
  985. ((and (eq (car expr) 'calcFunc-condition)
  986. (>= (length expr) 3))
  987. (math-rwcomp-pattern (nth 1 expr) part)
  988. (setq expr (cdr expr))
  989. (while (setq expr (cdr expr))
  990. (let ((cond (math-flatten-lands (car expr))))
  991. (while cond
  992. (if (math-rwcomp-all-regs-done (car cond))
  993. (math-rwcomp-cond-instr (car cond))
  994. (setq math-conds (cons (car cond) math-conds)))
  995. (setq cond (cdr cond))))))
  996. ((and (eq (car expr) 'calcFunc-pand)
  997. (= (length expr) 3))
  998. (math-rwcomp-pattern (nth 1 expr) part)
  999. (math-rwcomp-pattern (nth 2 expr) part))
  1000. ((and (eq (car expr) 'calcFunc-por)
  1001. (= (length expr) 3))
  1002. (math-rwcomp-instr 'alt nil nil [nil nil 4])
  1003. (let ((math-conds nil)
  1004. (head math-prog-last)
  1005. (mark math-bound-vars)
  1006. (math-copy-neg t))
  1007. (math-rwcomp-pattern (nth 1 expr) part t)
  1008. (let ((amark math-aliased-vars)
  1009. (math-aliased-vars math-aliased-vars)
  1010. (tail math-prog-last)
  1011. (p math-bound-vars)
  1012. entry)
  1013. (while (not (eq p mark))
  1014. (setq entry (assq (car p) math-regs)
  1015. math-aliased-vars (cons (list (car p) (nth 1 entry) nil)
  1016. math-aliased-vars)
  1017. p (cdr p))
  1018. (setcar (math-rwcomp-reg-entry (nth 1 entry)) nil))
  1019. (setcar (cdr (car head)) (cdr head))
  1020. (setcdr head nil)
  1021. (setq math-prog-last head)
  1022. (math-rwcomp-pattern (nth 2 expr) part)
  1023. (math-rwcomp-instr 'same 0 0)
  1024. (setcdr tail math-prog-last)
  1025. (setq p math-aliased-vars)
  1026. (while (not (eq p amark))
  1027. (if (car (car p))
  1028. (setcar (math-rwcomp-reg-entry (nth 1 (car p)))
  1029. (car (car p))))
  1030. (setq p (cdr p)))))
  1031. (math-rwcomp-do-conditions))
  1032. ((and (eq (car expr) 'calcFunc-pnot)
  1033. (= (length expr) 2))
  1034. (math-rwcomp-instr 'alt nil nil [nil nil 4])
  1035. (let ((head math-prog-last)
  1036. (mark math-bound-vars))
  1037. (math-rwcomp-pattern (nth 1 expr) part)
  1038. (math-rwcomp-unbind-vars mark)
  1039. (math-rwcomp-instr 'end-alt head)
  1040. (math-rwcomp-instr 'backtrack)
  1041. (setcar (cdr (car head)) (cdr head))
  1042. (setcdr head nil)
  1043. (setq math-prog-last head)))
  1044. (t (let ((props (get (car expr) 'math-rewrite-props)))
  1045. (if (and (eq (car expr) 'calcFunc-plain)
  1046. (= (length expr) 2)
  1047. (not (math-primp (nth 1 expr))))
  1048. (setq expr (nth 1 expr))) ; but "props" is still nil
  1049. (if (and (memq 'algebraic props)
  1050. (math-rwcomp-is-algebraic expr))
  1051. (math-rwcomp-cond-instr (list 'calcFunc-eq
  1052. (math-rwcomp-register-expr part)
  1053. expr))
  1054. (if (and (memq 'commut props)
  1055. (= (length expr) 3))
  1056. (let ((arg1 (nth 1 expr))
  1057. (arg2 (nth 2 expr))
  1058. try1 def code head (flip nil))
  1059. (if (eq (car expr) '-)
  1060. (setq arg2 (math-rwcomp-neg arg2)))
  1061. (setq arg1 (cons arg1 (math-rwcomp-best-reg arg1))
  1062. arg2 (cons arg2 (math-rwcomp-best-reg arg2)))
  1063. (or (math-rwcomp-order arg1 arg2)
  1064. (setq def arg1 arg1 arg2 arg2 def flip t))
  1065. (if (math-rwcomp-optional-arg (car expr) arg1)
  1066. (error "Too many opt( ) arguments in this context"))
  1067. (setq def (math-rwcomp-optional-arg (car expr) arg2)
  1068. head (if (memq (car expr) '(+ -))
  1069. '(+ -)
  1070. (if (eq (car expr) '*)
  1071. '(* /)
  1072. (list (car expr))))
  1073. code (if (math-rwcomp-is-constrained
  1074. (car arg1) head)
  1075. (if (math-rwcomp-is-constrained
  1076. (car arg2) head)
  1077. 0 1)
  1078. 2))
  1079. (math-rwcomp-multi-instr (and def (list def))
  1080. 'try part head
  1081. (vector nil nil nil code flip)
  1082. (cdr arg1))
  1083. (setq try1 (car math-prog-last))
  1084. (math-rwcomp-pattern (car arg1) (cdr arg1))
  1085. (math-rwcomp-instr 'try2 try1 (cdr arg2))
  1086. (if (and (= part 0) (not def) (not math-rewrite-whole)
  1087. (not (eq math-rhs t))
  1088. (setq def (get (car expr)
  1089. 'math-rewrite-default)))
  1090. (let ((reg1 (math-rwcomp-reg))
  1091. (reg2 (math-rwcomp-reg)))
  1092. (if (= (aref (nth 3 try1) 3) 0)
  1093. (aset (nth 3 try1) 3 1))
  1094. (math-rwcomp-instr 'try (cdr arg2)
  1095. (if (equal head '(* /))
  1096. '(*) head)
  1097. (vector nil nil nil
  1098. (if (= code 0)
  1099. 1 2)
  1100. nil)
  1101. reg1 def)
  1102. (setq try1 (car math-prog-last))
  1103. (math-rwcomp-pattern (car arg2) reg1)
  1104. (math-rwcomp-instr 'try2 try1 reg2)
  1105. (setq math-rhs (list (if (eq (car expr) '-)
  1106. '+ (car expr))
  1107. math-rhs
  1108. (list 'calcFunc-register
  1109. reg2))))
  1110. (math-rwcomp-pattern (car arg2) (cdr arg2))))
  1111. (let* ((args (mapcar (function
  1112. (lambda (x)
  1113. (cons x (math-rwcomp-best-reg x))))
  1114. (cdr expr)))
  1115. (args2 (copy-sequence args))
  1116. (argp (reverse args2))
  1117. (defs nil)
  1118. (num 1))
  1119. (while argp
  1120. (let ((def (math-rwcomp-optional-arg (car expr)
  1121. (car argp))))
  1122. (if def
  1123. (progn
  1124. (setq args2 (delq (car argp) args2)
  1125. defs (cons (cons def (cdr (car argp)))
  1126. defs))
  1127. (math-rwcomp-multi-instr
  1128. (mapcar 'cdr args2)
  1129. (if (or (and (memq 'unary1 props)
  1130. (= (length args2) 1)
  1131. (eq (car args2) (car args)))
  1132. (and (memq 'unary2 props)
  1133. (= (length args) 2)
  1134. (eq (car args2) (nth 1 args))))
  1135. 'func-opt
  1136. 'func-def)
  1137. part (car expr)
  1138. defs))))
  1139. (setq argp (cdr argp)))
  1140. (math-rwcomp-multi-instr (mapcar 'cdr args)
  1141. 'func part (car expr))
  1142. (setq args (sort args 'math-rwcomp-order))
  1143. (while args
  1144. (math-rwcomp-pattern (car (car args)) (cdr (car args)))
  1145. (setq num (1+ num)
  1146. args (cdr args))))))))))
  1147. (defun math-rwcomp-best-reg (x)
  1148. (or (and (eq (car-safe x) 'var)
  1149. (let ((entry (assq (nth 2 x) math-aliased-vars)))
  1150. (and entry
  1151. (not (nth 2 entry))
  1152. (not (nth 2 (math-rwcomp-reg-entry (nth 1 entry))))
  1153. (progn
  1154. (setcar (cdr (cdr entry)) t)
  1155. (nth 1 entry)))))
  1156. (math-rwcomp-reg)))
  1157. (defun math-rwcomp-all-regs-done (expr)
  1158. (if (Math-primp expr)
  1159. (or (not (eq (car-safe expr) 'var))
  1160. (assq (nth 2 expr) math-regs)
  1161. (eq (nth 2 expr) 'var-remember)
  1162. (math-const-var expr))
  1163. (if (and (eq (car expr) 'calcFunc-let)
  1164. (= (length expr) 3))
  1165. (math-rwcomp-all-regs-done (nth 2 expr))
  1166. (if (and (eq (car expr) 'calcFunc-let)
  1167. (= (length expr) 2)
  1168. (eq (car-safe (nth 1 expr)) 'calcFunc-assign)
  1169. (= (length (nth 1 expr)) 3))
  1170. (math-rwcomp-all-regs-done (nth 2 (nth 1 expr)))
  1171. (while (and (setq expr (cdr expr))
  1172. (math-rwcomp-all-regs-done (car expr))))
  1173. (null expr)))))
  1174. (defun math-rwcomp-no-vars (expr)
  1175. (if (Math-primp expr)
  1176. (or (not (eq (car-safe expr) 'var))
  1177. (math-const-var expr))
  1178. (and (not (memq (car expr) '(calcFunc-condition
  1179. calcFunc-select calcFunc-quote
  1180. calcFunc-plain calcFunc-opt
  1181. calcFunc-por calcFunc-pand
  1182. calcFunc-pnot calcFunc-apply
  1183. calcFunc-cons calcFunc-rcons)))
  1184. (progn
  1185. (while (and (setq expr (cdr expr))
  1186. (math-rwcomp-no-vars (car expr))))
  1187. (null expr)))))
  1188. (defun math-rwcomp-is-algebraic (expr)
  1189. (if (Math-primp expr)
  1190. (or (not (eq (car-safe expr) 'var))
  1191. (math-const-var expr)
  1192. (assq (nth 2 expr) math-regs))
  1193. (and (memq 'algebraic (get (car expr) 'math-rewrite-props))
  1194. (progn
  1195. (while (and (setq expr (cdr expr))
  1196. (math-rwcomp-is-algebraic (car expr))))
  1197. (null expr)))))
  1198. (defun math-rwcomp-is-constrained (expr not-these)
  1199. (if (Math-primp expr)
  1200. (not (eq (car-safe expr) 'var))
  1201. (if (eq (car expr) 'calcFunc-plain)
  1202. (math-rwcomp-is-constrained (nth 1 expr) not-these)
  1203. (not (or (memq (car expr) '(neg calcFunc-select))
  1204. (memq (car expr) not-these)
  1205. (and (memq 'commut (get (car expr) 'math-rewrite-props))
  1206. (or (eq (car-safe (nth 1 expr)) 'calcFunc-opt)
  1207. (eq (car-safe (nth 2 expr)) 'calcFunc-opt))))))))
  1208. (defun math-rwcomp-optional-arg (head argp)
  1209. (let ((arg (car argp)))
  1210. (if (eq (car-safe arg) 'calcFunc-opt)
  1211. (and (memq (length arg) '(2 3))
  1212. (progn
  1213. (or (eq (car-safe (nth 1 arg)) 'var)
  1214. (error "First argument of opt( ) must be a variable"))
  1215. (setcar argp (nth 1 arg))
  1216. (if (= (length arg) 2)
  1217. (or (get head 'math-rewrite-default)
  1218. (error "opt( ) must include a default in this context"))
  1219. (nth 2 arg))))
  1220. (and (eq (car-safe arg) 'neg)
  1221. (let* ((part (list (nth 1 arg)))
  1222. (partp (math-rwcomp-optional-arg head part)))
  1223. (and partp
  1224. (setcar argp (math-rwcomp-neg (car part)))
  1225. (math-neg partp)))))))
  1226. (defun math-rwcomp-neg (expr)
  1227. (if (memq (car-safe expr) '(* /))
  1228. (if (eq (car-safe (nth 1 expr)) 'var)
  1229. (list (car expr) (list 'neg (nth 1 expr)) (nth 2 expr))
  1230. (if (eq (car-safe (nth 2 expr)) 'var)
  1231. (list (car expr) (nth 1 expr) (list 'neg (nth 2 expr)))
  1232. (math-neg expr)))
  1233. (math-neg expr)))
  1234. (defun math-rwcomp-assoc-args (expr)
  1235. (if (and (eq (car-safe (nth 1 expr)) (car expr))
  1236. (= (length (nth 1 expr)) 3))
  1237. (math-rwcomp-assoc-args (nth 1 expr)))
  1238. (if (and (eq (car-safe (nth 2 expr)) (car expr))
  1239. (= (length (nth 2 expr)) 3))
  1240. (math-rwcomp-assoc-args (nth 2 expr))))
  1241. (defun math-rwcomp-addsub-args (expr)
  1242. (if (memq (car-safe (nth 1 expr)) '(+ -))
  1243. (math-rwcomp-addsub-args (nth 1 expr)))
  1244. (if (eq (car expr) '-)
  1245. ()
  1246. (if (eq (car-safe (nth 2 expr)) '+)
  1247. (math-rwcomp-addsub-args (nth 2 expr)))))
  1248. (defun math-rwcomp-order (a b)
  1249. (< (math-rwcomp-priority (car a))
  1250. (math-rwcomp-priority (car b))))
  1251. ;; Order of priority: 0 Constants and other exact matches (first)
  1252. ;; 10 Functions (except below)
  1253. ;; 20 Meta-variables which occur more than once
  1254. ;; 30 Algebraic functions
  1255. ;; 40 Commutative/associative functions
  1256. ;; 50 Meta-variables which occur only once
  1257. ;; +100 for every "!!!" (pnot) in the pattern
  1258. ;; 10000 Optional arguments (last)
  1259. (defun math-rwcomp-priority (expr)
  1260. (+ (math-rwcomp-count-pnots expr)
  1261. (cond ((eq (car-safe expr) 'calcFunc-opt)
  1262. 10000)
  1263. ((math-rwcomp-no-vars expr)
  1264. 0)
  1265. ((eq (car expr) 'calcFunc-quote)
  1266. 0)
  1267. ((eq (car expr) 'var)
  1268. (if (assq (nth 2 expr) math-regs)
  1269. 0
  1270. (if (= (math-rwcomp-count-refs expr) 1)
  1271. 50
  1272. 20)))
  1273. (t (let ((props (get (car expr) 'math-rewrite-props)))
  1274. (if (or (memq 'commut props)
  1275. (memq 'assoc props))
  1276. 40
  1277. (if (memq 'algebraic props)
  1278. 30
  1279. 10)))))))
  1280. (defun math-rwcomp-count-refs (var)
  1281. (let ((count (or (math-expr-contains-count math-pattern var) 0))
  1282. (p math-conds))
  1283. (while p
  1284. (if (eq (car-safe (car p)) 'calcFunc-let)
  1285. (if (= (length (car p)) 3)
  1286. (setq count (+ count
  1287. (or (math-expr-contains-count (nth 2 (car p)) var)
  1288. 0)))
  1289. (if (and (= (length (car p)) 2)
  1290. (eq (car-safe (nth 1 (car p))) 'calcFunc-assign)
  1291. (= (length (nth 1 (car p))) 3))
  1292. (setq count (+ count
  1293. (or (math-expr-contains-count
  1294. (nth 2 (nth 1 (car p))) var) 0))))))
  1295. (setq p (cdr p)))
  1296. count))
  1297. (defun math-rwcomp-count-pnots (expr)
  1298. (if (Math-primp expr)
  1299. 0
  1300. (if (eq (car expr) 'calcFunc-pnot)
  1301. 100
  1302. (let ((count 0))
  1303. (while (setq expr (cdr expr))
  1304. (setq count (+ count (math-rwcomp-count-pnots (car expr)))))
  1305. count))))
  1306. ;; In the current implementation, all associative functions must
  1307. ;; also be commutative.
  1308. (put '+ 'math-rewrite-props '(algebraic assoc commut))
  1309. (put '- 'math-rewrite-props '(algebraic assoc commut)) ; see below
  1310. (put '* 'math-rewrite-props '(algebraic assoc commut)) ; see below
  1311. (put '/ 'math-rewrite-props '(algebraic unary1))
  1312. (put '^ 'math-rewrite-props '(algebraic unary1))
  1313. (put '% 'math-rewrite-props '(algebraic))
  1314. (put 'neg 'math-rewrite-props '(algebraic))
  1315. (put 'calcFunc-idiv 'math-rewrite-props '(algebraic))
  1316. (put 'calcFunc-abs 'math-rewrite-props '(algebraic))
  1317. (put 'calcFunc-sign 'math-rewrite-props '(algebraic))
  1318. (put 'calcFunc-round 'math-rewrite-props '(algebraic))
  1319. (put 'calcFunc-rounde 'math-rewrite-props '(algebraic))
  1320. (put 'calcFunc-roundu 'math-rewrite-props '(algebraic))
  1321. (put 'calcFunc-trunc 'math-rewrite-props '(algebraic))
  1322. (put 'calcFunc-floor 'math-rewrite-props '(algebraic))
  1323. (put 'calcFunc-ceil 'math-rewrite-props '(algebraic))
  1324. (put 'calcFunc-re 'math-rewrite-props '(algebraic))
  1325. (put 'calcFunc-im 'math-rewrite-props '(algebraic))
  1326. (put 'calcFunc-conj 'math-rewrite-props '(algebraic))
  1327. (put 'calcFunc-arg 'math-rewrite-props '(algebraic))
  1328. (put 'calcFunc-and 'math-rewrite-props '(assoc commut))
  1329. (put 'calcFunc-or 'math-rewrite-props '(assoc commut))
  1330. (put 'calcFunc-xor 'math-rewrite-props '(assoc commut))
  1331. (put 'calcFunc-eq 'math-rewrite-props '(commut))
  1332. (put 'calcFunc-neq 'math-rewrite-props '(commut))
  1333. (put 'calcFunc-land 'math-rewrite-props '(assoc commut))
  1334. (put 'calcFunc-lor 'math-rewrite-props '(assoc commut))
  1335. (put 'calcFunc-beta 'math-rewrite-props '(commut))
  1336. (put 'calcFunc-gcd 'math-rewrite-props '(assoc commut))
  1337. (put 'calcFunc-lcm 'math-rewrite-props '(assoc commut))
  1338. (put 'calcFunc-max 'math-rewrite-props '(algebraic assoc commut))
  1339. (put 'calcFunc-min 'math-rewrite-props '(algebraic assoc commut))
  1340. (put 'calcFunc-vunion 'math-rewrite-props '(assoc commut))
  1341. (put 'calcFunc-vint 'math-rewrite-props '(assoc commut))
  1342. (put 'calcFunc-vxor 'math-rewrite-props '(assoc commut))
  1343. ;; Note: "*" is not commutative for matrix args, but we pretend it is.
  1344. ;; Also, "-" is not commutative but the code tweaks things so that it is.
  1345. (put '+ 'math-rewrite-default 0)
  1346. (put '- 'math-rewrite-default 0)
  1347. (put '* 'math-rewrite-default 1)
  1348. (put '/ 'math-rewrite-default 1)
  1349. (put '^ 'math-rewrite-default 1)
  1350. (put 'calcFunc-land 'math-rewrite-default 1)
  1351. (put 'calcFunc-lor 'math-rewrite-default 0)
  1352. (put 'calcFunc-vunion 'math-rewrite-default '(vec))
  1353. (put 'calcFunc-vint 'math-rewrite-default '(vec))
  1354. (put 'calcFunc-vdiff 'math-rewrite-default '(vec))
  1355. (put 'calcFunc-vxor 'math-rewrite-default '(vec))
  1356. (defmacro math-rwfail (&optional back)
  1357. `(setq pc (and ,(if back
  1358. '(setq btrack (cdr btrack))
  1359. 'btrack)
  1360. '((backtrack)))))
  1361. ;; This monstrosity is necessary because the use of static vectors of
  1362. ;; registers makes rewrite rules non-reentrant. Yucko!
  1363. (defmacro math-rweval (form)
  1364. `(let ((orig (car rules)))
  1365. (setcar rules '(nil nil nil no-phase))
  1366. (unwind-protect
  1367. ,form
  1368. (setcar rules orig))))
  1369. (defvar math-rewrite-phase 1)
  1370. ;; The variable math-apply-rw-regs is local to math-apply-rewrites,
  1371. ;; but is used by math-rwapply-replace-regs and math-rwapply-reg-looks-negp
  1372. ;; which are called by math-apply-rewrites.
  1373. (defvar math-apply-rw-regs)
  1374. ;; The variable math-apply-rw-ruleset is local to math-apply-rewrites,
  1375. ;; but is used by math-rwapply-remember.
  1376. (defvar math-apply-rw-ruleset)
  1377. (defun math-apply-rewrites (expr rules &optional heads math-apply-rw-ruleset)
  1378. (and
  1379. (setq rules (cdr (or (assq (car-safe expr) rules)
  1380. (assq nil rules))))
  1381. (let ((result nil)
  1382. op math-apply-rw-regs inst part pc mark btrack
  1383. (tracing math-rwcomp-tracing)
  1384. (phase math-rewrite-phase))
  1385. (while rules
  1386. (or
  1387. (and (setq part (nth 2 (car rules)))
  1388. heads
  1389. (not (memq part heads)))
  1390. (and (setq part (nth 3 (car rules)))
  1391. (not (memq phase part)))
  1392. (progn
  1393. (setq math-apply-rw-regs (car (car rules))
  1394. pc (nth 1 (car rules))
  1395. btrack nil)
  1396. (aset math-apply-rw-regs 0 expr)
  1397. (while pc
  1398. (and tracing
  1399. (progn (terpri) (princ (car pc))
  1400. (if (and (natnump (nth 1 (car pc)))
  1401. (< (nth 1 (car pc)) (length math-apply-rw-regs)))
  1402. (princ
  1403. (format "\n part = %s"
  1404. (aref math-apply-rw-regs (nth 1 (car pc))))))))
  1405. (cond ((eq (setq op (car (setq inst (car pc)))) 'func)
  1406. (if (and (consp
  1407. (setq part (aref math-apply-rw-regs (car (cdr inst)))))
  1408. (eq (car part)
  1409. (car (setq inst (cdr (cdr inst)))))
  1410. (progn
  1411. (while (and (setq inst (cdr inst)
  1412. part (cdr part))
  1413. inst)
  1414. (aset math-apply-rw-regs (car inst) (car part)))
  1415. (not (or inst part))))
  1416. (setq pc (cdr pc))
  1417. (math-rwfail)))
  1418. ((eq op 'same)
  1419. (if (or (equal (setq part (aref math-apply-rw-regs (nth 1 inst)))
  1420. (setq mark (aref math-apply-rw-regs (nth 2 inst))))
  1421. (Math-equal part mark))
  1422. (setq pc (cdr pc))
  1423. (math-rwfail)))
  1424. ((and (eq op 'try)
  1425. calc-matrix-mode
  1426. (not (eq calc-matrix-mode 'scalar))
  1427. (eq (car (nth 2 inst)) '*)
  1428. (consp (setq part (aref math-apply-rw-regs (car (cdr inst)))))
  1429. (eq (car part) '*)
  1430. (not (math-known-scalarp part)))
  1431. (setq mark (nth 3 inst)
  1432. pc (cdr pc))
  1433. (if (aref mark 4)
  1434. (progn
  1435. (aset math-apply-rw-regs (nth 4 inst) (nth 2 part))
  1436. (aset mark 1 (cdr (cdr part))))
  1437. (aset math-apply-rw-regs (nth 4 inst) (nth 1 part))
  1438. (aset mark 1 (cdr part)))
  1439. (aset mark 0 (cdr part))
  1440. (aset mark 2 0))
  1441. ((eq op 'try)
  1442. (if (and (consp (setq part
  1443. (aref math-apply-rw-regs (car (cdr inst)))))
  1444. (memq (car part) (nth 2 inst))
  1445. (= (length part) 3)
  1446. (or (not (eq (car part) '/))
  1447. (Math-objectp (nth 2 part))))
  1448. (progn
  1449. (setq op nil
  1450. mark (car (cdr (setq inst (cdr (cdr inst))))))
  1451. (and
  1452. (memq 'assoc (get (car part) 'math-rewrite-props))
  1453. (not (= (aref mark 3) 0))
  1454. (while (if (and (consp (nth 1 part))
  1455. (memq (car (nth 1 part)) (car inst)))
  1456. (setq op (cons (if (eq (car part) '-)
  1457. (math-rwapply-neg
  1458. (nth 2 part))
  1459. (nth 2 part))
  1460. op)
  1461. part (nth 1 part))
  1462. (if (and (consp (nth 2 part))
  1463. (memq (car (nth 2 part))
  1464. (car inst))
  1465. (not (eq (car (nth 2 part)) '-)))
  1466. (setq op (cons (nth 1 part) op)
  1467. part (nth 2 part))))))
  1468. (setq op (cons (nth 1 part)
  1469. (cons (if (eq (car part) '-)
  1470. (math-rwapply-neg
  1471. (nth 2 part))
  1472. (if (eq (car part) '/)
  1473. (math-rwapply-inv
  1474. (nth 2 part))
  1475. (nth 2 part)))
  1476. op))
  1477. btrack (cons pc btrack)
  1478. pc (cdr pc))
  1479. (aset math-apply-rw-regs (nth 2 inst) (car op))
  1480. (aset mark 0 op)
  1481. (aset mark 1 op)
  1482. (aset mark 2 (if (cdr (cdr op)) 1 0)))
  1483. (if (nth 5 inst)
  1484. (if (and (consp part)
  1485. (eq (car part) 'neg)
  1486. (eq (car (nth 2 inst)) '*)
  1487. (eq (nth 5 inst) 1))
  1488. (progn
  1489. (setq mark (nth 3 inst)
  1490. pc (cdr pc))
  1491. (aset math-apply-rw-regs (nth 4 inst) (nth 1 part))
  1492. (aset mark 1 -1)
  1493. (aset mark 2 4))
  1494. (setq mark (nth 3 inst)
  1495. pc (cdr pc))
  1496. (aset math-apply-rw-regs (nth 4 inst) part)
  1497. (aset mark 2 3))
  1498. (math-rwfail))))
  1499. ((eq op 'try2)
  1500. (setq part (nth 1 inst) ; try instr
  1501. mark (nth 3 part)
  1502. op (aref mark 2)
  1503. pc (cdr pc))
  1504. (aset math-apply-rw-regs (nth 2 inst)
  1505. (cond
  1506. ((eq op 0)
  1507. (if (eq (aref mark 0) (aref mark 1))
  1508. (nth 1 (aref mark 0))
  1509. (car (aref mark 0))))
  1510. ((eq op 1)
  1511. (setq mark (delq (car (aref mark 1))
  1512. (copy-sequence (aref mark 0)))
  1513. op (car (nth 2 part)))
  1514. (if (eq op '*)
  1515. (progn
  1516. (setq mark (nreverse mark)
  1517. part (list '* (nth 1 mark) (car mark))
  1518. mark (cdr mark))
  1519. (while (setq mark (cdr mark))
  1520. (setq part (list '* (car mark) part))))
  1521. (setq part (car mark)
  1522. mark (cdr mark)
  1523. part (if (and (eq op '+)
  1524. (consp (car mark))
  1525. (eq (car (car mark)) 'neg))
  1526. (list '- part
  1527. (nth 1 (car mark)))
  1528. (list op part (car mark))))
  1529. (while (setq mark (cdr mark))
  1530. (setq part (if (and (eq op '+)
  1531. (consp (car mark))
  1532. (eq (car (car mark)) 'neg))
  1533. (list '- part
  1534. (nth 1 (car mark)))
  1535. (list op part (car mark))))))
  1536. part)
  1537. ((eq op 2)
  1538. (car (aref mark 1)))
  1539. ((eq op 3) (nth 5 part))
  1540. (t (aref mark 1)))))
  1541. ((eq op 'select)
  1542. (setq pc (cdr pc))
  1543. (if (and (consp (setq part (aref math-apply-rw-regs (nth 1 inst))))
  1544. (eq (car part) 'calcFunc-select))
  1545. (aset math-apply-rw-regs (nth 2 inst) (nth 1 part))
  1546. (if math-rewrite-selections
  1547. (math-rwfail)
  1548. (aset math-apply-rw-regs (nth 2 inst) part))))
  1549. ((eq op 'same-neg)
  1550. (if (or (equal (setq part (aref math-apply-rw-regs (nth 1 inst)))
  1551. (setq mark (math-neg
  1552. (aref math-apply-rw-regs (nth 2 inst)))))
  1553. (Math-equal part mark))
  1554. (setq pc (cdr pc))
  1555. (math-rwfail)))
  1556. ((eq op 'backtrack)
  1557. (setq inst (car (car btrack)) ; "try" or "alt" instr
  1558. pc (cdr (car btrack))
  1559. mark (or (nth 3 inst) [nil nil 4])
  1560. op (aref mark 2))
  1561. (cond ((eq op 0)
  1562. (if (setq op (cdr (aref mark 1)))
  1563. (aset math-apply-rw-regs (nth 4 inst)
  1564. (car (aset mark 1 op)))
  1565. (if (nth 5 inst)
  1566. (progn
  1567. (aset mark 2 3)
  1568. (aset math-apply-rw-regs (nth 4 inst)
  1569. (aref math-apply-rw-regs (nth 1 inst))))
  1570. (math-rwfail t))))
  1571. ((eq op 1)
  1572. (if (setq op (cdr (aref mark 1)))
  1573. (aset math-apply-rw-regs (nth 4 inst)
  1574. (car (aset mark 1 op)))
  1575. (if (= (aref mark 3) 1)
  1576. (if (nth 5 inst)
  1577. (progn
  1578. (aset mark 2 3)
  1579. (aset math-apply-rw-regs (nth 4 inst)
  1580. (aref math-apply-rw-regs (nth 1 inst))))
  1581. (math-rwfail t))
  1582. (aset mark 2 2)
  1583. (aset mark 1 (cons nil (aref mark 0)))
  1584. (math-rwfail))))
  1585. ((eq op 2)
  1586. (if (setq op (cdr (aref mark 1)))
  1587. (progn
  1588. (setq mark (delq (car (aset mark 1 op))
  1589. (copy-sequence
  1590. (aref mark 0)))
  1591. op (car (nth 2 inst)))
  1592. (if (eq op '*)
  1593. (progn
  1594. (setq mark (nreverse mark)
  1595. part (list '* (nth 1 mark)
  1596. (car mark))
  1597. mark (cdr mark))
  1598. (while (setq mark (cdr mark))
  1599. (setq part (list '* (car mark)
  1600. part))))
  1601. (setq part (car mark)
  1602. mark (cdr mark)
  1603. part (if (and (eq op '+)
  1604. (consp (car mark))
  1605. (eq (car (car mark))
  1606. 'neg))
  1607. (list '- part
  1608. (nth 1 (car mark)))
  1609. (list op part (car mark))))
  1610. (while (setq mark (cdr mark))
  1611. (setq part (if (and (eq op '+)
  1612. (consp (car mark))
  1613. (eq (car (car mark))
  1614. 'neg))
  1615. (list '- part
  1616. (nth 1 (car mark)))
  1617. (list op part (car mark))))))
  1618. (aset math-apply-rw-regs (nth 4 inst) part))
  1619. (if (nth 5 inst)
  1620. (progn
  1621. (aset mark 2 3)
  1622. (aset math-apply-rw-regs (nth 4 inst)
  1623. (aref math-apply-rw-regs (nth 1 inst))))
  1624. (math-rwfail t))))
  1625. ((eq op 4)
  1626. (setq btrack (cdr btrack)))
  1627. (t (math-rwfail t))))
  1628. ((eq op 'integer)
  1629. (if (Math-integerp (setq part
  1630. (aref math-apply-rw-regs (nth 1 inst))))
  1631. (setq pc (cdr pc))
  1632. (if (Math-primp part)
  1633. (math-rwfail)
  1634. (setq part (math-rweval (math-simplify part)))
  1635. (if (Math-integerp part)
  1636. (setq pc (cdr pc))
  1637. (math-rwfail)))))
  1638. ((eq op 'real)
  1639. (if (Math-realp (setq part (aref math-apply-rw-regs (nth 1 inst))))
  1640. (setq pc (cdr pc))
  1641. (if (Math-primp part)
  1642. (math-rwfail)
  1643. (setq part (math-rweval (math-simplify part)))
  1644. (if (Math-realp part)
  1645. (setq pc (cdr pc))
  1646. (math-rwfail)))))
  1647. ((eq op 'constant)
  1648. (if (math-constp (setq part (aref math-apply-rw-regs (nth 1 inst))))
  1649. (setq pc (cdr pc))
  1650. (if (Math-primp part)
  1651. (math-rwfail)
  1652. (setq part (math-rweval (math-simplify part)))
  1653. (if (math-constp part)
  1654. (setq pc (cdr pc))
  1655. (math-rwfail)))))
  1656. ((eq op 'negative)
  1657. (if (math-looks-negp (setq part
  1658. (aref math-apply-rw-regs (nth 1 inst))))
  1659. (setq pc (cdr pc))
  1660. (if (Math-primp part)
  1661. (math-rwfail)
  1662. (setq part (math-rweval (math-simplify part)))
  1663. (if (math-looks-negp part)
  1664. (setq pc (cdr pc))
  1665. (math-rwfail)))))
  1666. ((eq op 'rel)
  1667. (setq part (math-compare (aref math-apply-rw-regs (nth 1 inst))
  1668. (aref math-apply-rw-regs (nth 3 inst)))
  1669. op (nth 2 inst))
  1670. (if (= part 2)
  1671. (setq part (math-rweval
  1672. (math-simplify
  1673. (calcFunc-sign
  1674. (math-sub
  1675. (aref math-apply-rw-regs (nth 1 inst))
  1676. (aref math-apply-rw-regs (nth 3 inst))))))))
  1677. (if (cond ((eq op 'calcFunc-eq)
  1678. (eq part 0))
  1679. ((eq op 'calcFunc-neq)
  1680. (memq part '(-1 1)))
  1681. ((eq op 'calcFunc-lt)
  1682. (eq part -1))
  1683. ((eq op 'calcFunc-leq)
  1684. (memq part '(-1 0)))
  1685. ((eq op 'calcFunc-gt)
  1686. (eq part 1))
  1687. ((eq op 'calcFunc-geq)
  1688. (memq part '(0 1))))
  1689. (setq pc (cdr pc))
  1690. (math-rwfail)))
  1691. ((eq op 'func-def)
  1692. (if (and
  1693. (consp (setq part (aref math-apply-rw-regs (car (cdr inst)))))
  1694. (eq (car part)
  1695. (car (setq inst (cdr (cdr inst))))))
  1696. (progn
  1697. (setq inst (cdr inst)
  1698. mark (car inst))
  1699. (while (and (setq inst (cdr inst)
  1700. part (cdr part))
  1701. inst)
  1702. (aset math-apply-rw-regs (car inst) (car part)))
  1703. (if (or inst part)
  1704. (setq pc (cdr pc))
  1705. (while (eq (car (car (setq pc (cdr pc))))
  1706. 'func-def))
  1707. (setq pc (cdr pc)) ; skip over "func"
  1708. (while mark
  1709. (aset math-apply-rw-regs (cdr (car mark)) (car (car mark)))
  1710. (setq mark (cdr mark)))))
  1711. (math-rwfail)))
  1712. ((eq op 'func-opt)
  1713. (if (or (not
  1714. (and
  1715. (consp
  1716. (setq part (aref math-apply-rw-regs (car (cdr inst)))))
  1717. (eq (car part) (nth 2 inst))))
  1718. (and (= (length part) 2)
  1719. (setq part (nth 1 part))))
  1720. (progn
  1721. (setq mark (nth 3 inst))
  1722. (aset math-apply-rw-regs (nth 4 inst) part)
  1723. (while (eq (car (car (setq pc (cdr pc)))) 'func-def))
  1724. (setq pc (cdr pc)) ; skip over "func"
  1725. (while mark
  1726. (aset math-apply-rw-regs (cdr (car mark)) (car (car mark)))
  1727. (setq mark (cdr mark))))
  1728. (setq pc (cdr pc))))
  1729. ((eq op 'mod)
  1730. (if (if (Math-zerop
  1731. (setq part (aref math-apply-rw-regs (nth 1 inst))))
  1732. (Math-zerop (nth 3 inst))
  1733. (and (not (Math-zerop (nth 2 inst)))
  1734. (progn
  1735. (setq part (math-mod part (nth 2 inst)))
  1736. (or (Math-numberp part)
  1737. (setq part (math-rweval
  1738. (math-simplify part))))
  1739. (Math-equal part (nth 3 inst)))))
  1740. (setq pc (cdr pc))
  1741. (math-rwfail)))
  1742. ((eq op 'apply)
  1743. (if (and (consp
  1744. (setq part (aref math-apply-rw-regs (car (cdr inst)))))
  1745. (not (Math-objvecp part))
  1746. (not (eq (car part) 'var)))
  1747. (progn
  1748. (aset math-apply-rw-regs (nth 2 inst)
  1749. (math-calcFunc-to-var (car part)))
  1750. (aset math-apply-rw-regs (nth 3 inst)
  1751. (cons 'vec (cdr part)))
  1752. (setq pc (cdr pc)))
  1753. (math-rwfail)))
  1754. ((eq op 'cons)
  1755. (if (and (consp
  1756. (setq part (aref math-apply-rw-regs (car (cdr inst)))))
  1757. (eq (car part) 'vec)
  1758. (cdr part))
  1759. (progn
  1760. (aset math-apply-rw-regs (nth 2 inst) (nth 1 part))
  1761. (aset math-apply-rw-regs (nth 3 inst)
  1762. (cons 'vec (cdr (cdr part))))
  1763. (setq pc (cdr pc)))
  1764. (math-rwfail)))
  1765. ((eq op 'rcons)
  1766. (if (and (consp
  1767. (setq part (aref math-apply-rw-regs (car (cdr inst)))))
  1768. (eq (car part) 'vec)
  1769. (cdr part))
  1770. (progn
  1771. (aset math-apply-rw-regs (nth 2 inst) (calcFunc-rhead part))
  1772. (aset math-apply-rw-regs (nth 3 inst) (calcFunc-rtail part))
  1773. (setq pc (cdr pc)))
  1774. (math-rwfail)))
  1775. ((eq op 'cond)
  1776. (if (math-is-true
  1777. (math-rweval
  1778. (math-simplify
  1779. (math-rwapply-replace-regs (nth 1 inst)))))
  1780. (setq pc (cdr pc))
  1781. (math-rwfail)))
  1782. ((eq op 'let)
  1783. (aset math-apply-rw-regs (nth 1 inst)
  1784. (math-rweval
  1785. (math-normalize
  1786. (math-rwapply-replace-regs (nth 2 inst)))))
  1787. (setq pc (cdr pc)))
  1788. ((eq op 'copy)
  1789. (aset math-apply-rw-regs (nth 2 inst)
  1790. (aref math-apply-rw-regs (nth 1 inst)))
  1791. (setq pc (cdr pc)))
  1792. ((eq op 'copy-neg)
  1793. (aset math-apply-rw-regs (nth 2 inst)
  1794. (math-rwapply-neg (aref math-apply-rw-regs (nth 1 inst))))
  1795. (setq pc (cdr pc)))
  1796. ((eq op 'alt)
  1797. (setq btrack (cons pc btrack)
  1798. pc (nth 1 inst)))
  1799. ((eq op 'end-alt)
  1800. (while (and btrack (not (eq (car btrack) (nth 1 inst))))
  1801. (setq btrack (cdr btrack)))
  1802. (setq btrack (cdr btrack)
  1803. pc (cdr pc)))
  1804. ((eq op 'done)
  1805. (setq result (math-rwapply-replace-regs (nth 1 inst)))
  1806. (if (or (and (eq (car-safe result) '+)
  1807. (eq (nth 2 result) 0))
  1808. (and (eq (car-safe result) '*)
  1809. (eq (nth 2 result) 1)))
  1810. (setq result (nth 1 result)))
  1811. (setq part (and (nth 2 inst)
  1812. (math-is-true
  1813. (math-rweval
  1814. (math-simplify
  1815. (math-rwapply-replace-regs
  1816. (nth 2 inst)))))))
  1817. (if (or (equal result expr)
  1818. (equal (setq result (math-normalize result)) expr))
  1819. (setq result nil)
  1820. (if part (math-rwapply-remember expr result))
  1821. (setq rules nil))
  1822. (setq pc nil))
  1823. (t (error "%s is not a valid rewrite opcode" op))))))
  1824. (setq rules (cdr rules)))
  1825. result)))
  1826. (defun math-rwapply-neg (expr)
  1827. (if (and (consp expr)
  1828. (memq (car expr) '(* /)))
  1829. (if (Math-objectp (nth 2 expr))
  1830. (list (car expr) (nth 1 expr) (math-neg (nth 2 expr)))
  1831. (list (car expr)
  1832. (if (Math-objectp (nth 1 expr))
  1833. (math-neg (nth 1 expr))
  1834. (list '* -1 (nth 1 expr)))
  1835. (nth 2 expr)))
  1836. (math-neg expr)))
  1837. (defun math-rwapply-inv (expr)
  1838. (if (and (Math-integerp expr)
  1839. calc-prefer-frac)
  1840. (math-make-frac 1 expr)
  1841. (list '/ 1 expr)))
  1842. (defun math-rwapply-replace-regs (expr)
  1843. (cond ((Math-primp expr)
  1844. expr)
  1845. ((eq (car expr) 'calcFunc-register)
  1846. (setq expr (aref math-apply-rw-regs (nth 1 expr)))
  1847. (if (eq (car-safe expr) '*)
  1848. (if (eq (nth 1 expr) -1)
  1849. (math-neg (nth 2 expr))
  1850. (if (eq (nth 1 expr) 1)
  1851. (nth 2 expr)
  1852. expr))
  1853. expr))
  1854. ((and (eq (car expr) 'calcFunc-eval)
  1855. (= (length expr) 2))
  1856. (calc-with-default-simplification
  1857. (math-normalize (math-rwapply-replace-regs (nth 1 expr)))))
  1858. ((and (eq (car expr) 'calcFunc-evalsimp)
  1859. (= (length expr) 2))
  1860. (math-simplify (math-rwapply-replace-regs (nth 1 expr))))
  1861. ((and (eq (car expr) 'calcFunc-evalextsimp)
  1862. (= (length expr) 2))
  1863. (math-simplify-extended (math-rwapply-replace-regs (nth 1 expr))))
  1864. ((and (eq (car expr) 'calcFunc-apply)
  1865. (= (length expr) 3))
  1866. (let ((func (math-rwapply-replace-regs (nth 1 expr)))
  1867. (args (math-rwapply-replace-regs (nth 2 expr)))
  1868. call)
  1869. (if (and (math-vectorp args)
  1870. (not (eq (car-safe (setq call (math-build-call
  1871. (math-var-to-calcFunc func)
  1872. (cdr args))))
  1873. 'calcFunc-call)))
  1874. call
  1875. (list 'calcFunc-apply func args))))
  1876. ((and (eq (car expr) 'calcFunc-cons)
  1877. (= (length expr) 3))
  1878. (let ((head (math-rwapply-replace-regs (nth 1 expr)))
  1879. (tail (math-rwapply-replace-regs (nth 2 expr))))
  1880. (if (math-vectorp tail)
  1881. (cons 'vec (cons head (cdr tail)))
  1882. (list 'calcFunc-cons head tail))))
  1883. ((and (eq (car expr) 'calcFunc-rcons)
  1884. (= (length expr) 3))
  1885. (let ((head (math-rwapply-replace-regs (nth 1 expr)))
  1886. (tail (math-rwapply-replace-regs (nth 2 expr))))
  1887. (if (math-vectorp head)
  1888. (append head (list tail))
  1889. (list 'calcFunc-rcons head tail))))
  1890. ((and (eq (car expr) 'neg)
  1891. (math-rwapply-reg-looks-negp (nth 1 expr)))
  1892. (math-rwapply-reg-neg (nth 1 expr)))
  1893. ((and (eq (car expr) 'neg)
  1894. (eq (car-safe (nth 1 expr)) 'calcFunc-register)
  1895. (math-scalarp (aref math-apply-rw-regs (nth 1 (nth 1 expr)))))
  1896. (math-neg (math-rwapply-replace-regs (nth 1 expr))))
  1897. ((and (eq (car expr) '+)
  1898. (math-rwapply-reg-looks-negp (nth 1 expr)))
  1899. (list '- (math-rwapply-replace-regs (nth 2 expr))
  1900. (math-rwapply-reg-neg (nth 1 expr))))
  1901. ((and (eq (car expr) '+)
  1902. (math-rwapply-reg-looks-negp (nth 2 expr)))
  1903. (list '- (math-rwapply-replace-regs (nth 1 expr))
  1904. (math-rwapply-reg-neg (nth 2 expr))))
  1905. ((and (eq (car expr) '-)
  1906. (math-rwapply-reg-looks-negp (nth 2 expr)))
  1907. (list '+ (math-rwapply-replace-regs (nth 1 expr))
  1908. (math-rwapply-reg-neg (nth 2 expr))))
  1909. ((eq (car expr) '*)
  1910. (cond ((eq (nth 1 expr) -1)
  1911. (if (math-rwapply-reg-looks-negp (nth 2 expr))
  1912. (math-rwapply-reg-neg (nth 2 expr))
  1913. (math-neg (math-rwapply-replace-regs (nth 2 expr)))))
  1914. ((eq (nth 1 expr) 1)
  1915. (math-rwapply-replace-regs (nth 2 expr)))
  1916. ((eq (nth 2 expr) -1)
  1917. (if (math-rwapply-reg-looks-negp (nth 1 expr))
  1918. (math-rwapply-reg-neg (nth 1 expr))
  1919. (math-neg (math-rwapply-replace-regs (nth 1 expr)))))
  1920. ((eq (nth 2 expr) 1)
  1921. (math-rwapply-replace-regs (nth 1 expr)))
  1922. (t
  1923. (let ((arg1 (math-rwapply-replace-regs (nth 1 expr)))
  1924. (arg2 (math-rwapply-replace-regs (nth 2 expr))))
  1925. (cond ((and (eq (car-safe arg1) '/)
  1926. (eq (nth 1 arg1) 1))
  1927. (list '/ arg2 (nth 2 arg1)))
  1928. ((and (eq (car-safe arg2) '/)
  1929. (eq (nth 1 arg2) 1))
  1930. (list '/ arg1 (nth 2 arg2)))
  1931. (t (list '* arg1 arg2)))))))
  1932. ((eq (car expr) '/)
  1933. (let ((arg1 (math-rwapply-replace-regs (nth 1 expr)))
  1934. (arg2 (math-rwapply-replace-regs (nth 2 expr))))
  1935. (if (eq (car-safe arg2) '/)
  1936. (list '/ (list '* arg1 (nth 2 arg2)) (nth 1 arg2))
  1937. (list '/ arg1 arg2))))
  1938. ((and (eq (car expr) 'calcFunc-plain)
  1939. (= (length expr) 2))
  1940. (if (Math-primp (nth 1 expr))
  1941. (nth 1 expr)
  1942. (if (eq (car (nth 1 expr)) 'calcFunc-register)
  1943. (aref math-apply-rw-regs (nth 1 (nth 1 expr)))
  1944. (cons (car (nth 1 expr)) (mapcar 'math-rwapply-replace-regs
  1945. (cdr (nth 1 expr)))))))
  1946. (t (cons (car expr) (mapcar 'math-rwapply-replace-regs (cdr expr))))))
  1947. (defun math-rwapply-reg-looks-negp (expr)
  1948. (if (eq (car-safe expr) 'calcFunc-register)
  1949. (math-looks-negp (aref math-apply-rw-regs (nth 1 expr)))
  1950. (if (memq (car-safe expr) '(* /))
  1951. (or (math-rwapply-reg-looks-negp (nth 1 expr))
  1952. (math-rwapply-reg-looks-negp (nth 2 expr))))))
  1953. (defun math-rwapply-reg-neg (expr) ; expr must satisfy rwapply-reg-looks-negp
  1954. (if (eq (car expr) 'calcFunc-register)
  1955. (math-neg (math-rwapply-replace-regs expr))
  1956. (if (math-rwapply-reg-looks-negp (nth 1 expr))
  1957. (math-rwapply-replace-regs (list (car expr)
  1958. (math-rwapply-reg-neg (nth 1 expr))
  1959. (nth 2 expr)))
  1960. (math-rwapply-replace-regs (list (car expr)
  1961. (nth 1 expr)
  1962. (math-rwapply-reg-neg (nth 2 expr)))))))
  1963. (defun math-rwapply-remember (old new)
  1964. (let ((varval (symbol-value (nth 2 (car math-apply-rw-ruleset))))
  1965. (rules (assq (car-safe old) math-apply-rw-ruleset)))
  1966. (if (and (eq (car-safe varval) 'vec)
  1967. (not (memq (car-safe old) '(nil schedule + -)))
  1968. rules)
  1969. (progn
  1970. (setcdr varval (cons (list 'calcFunc-assign
  1971. (if (math-rwcomp-no-vars old)
  1972. old
  1973. (list 'calcFunc-quote old))
  1974. new)
  1975. (cdr varval)))
  1976. (setcdr rules (cons (list (vector nil old)
  1977. (list (list 'same 0 1)
  1978. (list 'done new nil))
  1979. nil nil)
  1980. (cdr rules)))))))
  1981. (provide 'calc-rewr)
  1982. ;;; calc-rewr.el ends here