noncom2.red 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. module noncom2;
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % N O N C O M 2 %
  4. % %
  5. % A Package to redefine %
  6. % noncommutativity in REDUCE %
  7. % %
  8. % Author: Mathias Warns %
  9. % Physics Institute %
  10. % University of Bonn %
  11. % Nussallee 12 %
  12. % D-5300 BONN 1 (F.R.G.) %
  13. % <UNP008@DBNRHRZ1.bitnet> %
  14. % %
  15. % Version: 2.5 06 Jan 92 %
  16. % %
  17. % %
  18. % Designed for: REDUCE version 3.3 / 3.4 %
  19. % Tested on : - IBM 3081/3084/9000-620 VM/CMS MVS/XA %
  20. % SLISP implementation of REDUCE %
  21. % PSL/370 implementation of REDUCE %
  22. % - Intel 386/486 AT compatible %
  23. % PSL implementation of REDUCE %
  24. % %
  25. % Copyright (c) Mathias Warns 1990 - 1992 %
  26. % %
  27. % %
  28. % Permission is granted to any individual or institution to %
  29. % use, copy or re-distribute this software as long as it is %
  30. % not sold for profit, provided that this copyright notice %
  31. % is retained and the file is not altered. %
  32. % %
  33. % **** Summary of changes since version 1.0 **** %
  34. % %
  35. % - Various small bugs have been corrected in the utility %
  36. % functions %
  37. % - The sloppy use of CAR on atoms allowed in SLISP systems has %
  38. % been removed %
  39. % - The pattern matching routine SUBS3TNC has been entirely %
  40. % recoded for greater efficiency and is now used for ALL %
  41. % terms (not only for the noncommuting cases) %
  42. % Procedures SUBLIST, LOCATE!_N and MTCHP1!* added %
  43. % - Enhanced tracing utilities added %
  44. % - NONCOMP has been changed to NONCOMP!* since the former %
  45. % cannot be redefined on some systems %
  46. % 2.0 100691 mw : %
  47. % - deleting functions recoded %
  48. % - append replaced by nconc everywhere %
  49. % - switch MYMATCH added to choose between pattern matchers %
  50. % - procedures NONCOM and NONCOMMUTING modified %
  51. % 2.5 210891 mw %
  52. % - Bug in SUBSTNC corrected and enhanced %
  53. % - procedure ZERLEG added for much faster handling of s. t. %
  54. % - procedure !*SUBS3TNC modfified accordingly %
  55. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  56. create!-package('(noncom2),'(contrib physics));
  57. %------------------------------------------------------------------
  58. % this package relies on modified standard REDUCE routines %
  59. % and is therefore version dependent %
  60. %-------------------------------------------------------------------
  61. fluid '(!*nosq !*mymatch frlis!* ncmp!* subfg!* wtl!*);
  62. !*nosq := t;
  63. switch mymatch;
  64. !*mymatch := t; %this is the default
  65. %------------------------------%
  66. % general utility functions %
  67. %------------------------------%
  68. symbolic procedure trwrite u;
  69. begin scalar x;
  70. if not flagp(car u,'tracing) then return nil;
  71. write "**in procedure: ", car u; terpri();
  72. for each x in cdr u do write x;
  73. terpri();
  74. end;
  75. symbolic procedure funtrace u;
  76. for each x in u do flag(list(x),'tracing);
  77. deflist('((trwrite rlis) (funtrace rlis)),'stat);
  78. symbolic procedure pnth!*(u,n); % slightly modified from pnth
  79. if null u then nil
  80. else if n=1 then u
  81. else pnth!*(cdr u,n-1);
  82. symbolic procedure nth!*(u,n);
  83. if length(u) < n then nil
  84. else car pnth!*(u,n);
  85. symbolic procedure revassoc(u,v); % added 140791 mw
  86. % revesre of assoc
  87. % checks the a-list v for a pair whose CDR is u
  88. begin scalar x;
  89. if not listp v then rederr "invalid argument to revassoc";
  90. a: if null v then return;
  91. x := car v;
  92. v := cdr v;
  93. if (pairp x) and (cdr x = u) then return car x;
  94. go to a;
  95. end;
  96. symbolic procedure kernelp u; %new
  97. % checks if an algebraic expression is a kernel
  98. if null u or domain!*p u then nil
  99. else if idp u then t
  100. else if listp u and idp car u and not (car u memq
  101. '(!*sq set setq plus minus difference times quotient))
  102. then t
  103. else nil;
  104. symbolic procedure spp u; %new
  105. % checks if u is a standard power
  106. pairp u and kernelp car u;
  107. symbolic procedure stp u; %new
  108. % checks if u is a s.t.
  109. pairp u and spp car u;
  110. symbolic procedure sfp2 u; %new
  111. % checks if u if a s.f.
  112. % sfp seems to be ill defined
  113. pairp u and stp car u;
  114. symbolic procedure tstp u; %new
  115. % checks if u is a "true" standard term, i.e. a product term
  116. stp u and (car !*f2a !*t2f u neq 'plus);
  117. symbolic procedure !*!*a2f u; %new
  118. %converts u without call of subs2
  119. begin scalar flg,res;
  120. flg := subfg!*; subfg!* := nil;
  121. res := !*a2f u;
  122. subfg!* := flg;
  123. return res
  124. end;
  125. symbolic procedure !*!*a2q u; %new
  126. %converts an algebraic expression into a s.q. using !*!*a2f
  127. if car u eq 'quotient then !*!*a2f cadr u . !*!*a2f caddr u
  128. else !*f2q !*!*a2f u;
  129. symbolic procedure !*a2q u; %new
  130. %converts an algebraic expression into a s.q. using !*a2f
  131. if (not atom u and car u eq 'quotient) then
  132. !*a2f cadr u . !*a2f caddr u
  133. else !*f2q !*a2f u;
  134. symbolic procedure atsoc2(u,v);
  135. % same as atsoc but looks for the caar part
  136. begin scalar res;
  137. for each x in v do
  138. if (not atom car x and caar x eq u) then res:= x;
  139. return res
  140. end;
  141. symbolic procedure sublist(u,v);
  142. % u and v are lists of sp
  143. % checks if all elements of u are included in v in the right order
  144. % return a sublist of containing the elements of u + the rest of v
  145. begin scalar x,z,y,w,reslist,n,u1;
  146. if not (listp u and listp v) then
  147. rederr " invalid arguments to sublist";
  148. %initialization
  149. if null u or null v or not (V:= member(car u,v)) then return;
  150. a : if null u then return nconc(reslist,append(u1,v));
  151. z:= v;
  152. x := car u;
  153. u := cdr u;
  154. if not (v:= member(x,z)) then return;
  155. v := cdr v;
  156. n:= length(z) - length(v) - 1;
  157. z := for k:= 1 : n collect nth(z,k);
  158. trwrite(sublist,"z= ",z," v= ",v," x= ",x);
  159. a0: if null z then
  160. <<
  161. u1 := nconc(u1,list(x));
  162. go to a;
  163. >>;
  164. w := car z;
  165. z := cdr z;
  166. if noncommuting!_splist(w,u1) then go to a1
  167. else reslist := nconc(reslist,list(w));
  168. go to a0;
  169. a1:
  170. z := reverse (w . z);
  171. if noncommutingsp(car z,x) then return;
  172. v := (car z) . v;
  173. z := reverse cdr z;
  174. go to a0;
  175. end;
  176. symbolic procedure deleteall(x,u); %2.1
  177. % deletes all occurrences of x in u
  178. begin scalar y,res;
  179. a: if null u then return res;
  180. y:= car u;
  181. u := cdr u;
  182. if not (y = x) then res:= nconc(res,list(y));
  183. go to a
  184. end;
  185. symbolic procedure deletemult(x,u); %2.1
  186. % deletes multiples occurences of x in u
  187. % keeping only one left
  188. begin scalar y,n;
  189. if null (y:= cdr member(x,u)) then return u;
  190. n:=length(u)-length(y);
  191. return nconc(for k:=1 :n collect nth(u,k),deleteall(x,y));
  192. end;
  193. symbolic procedure deletemult!* u;
  194. % deletes all multiple occurences of elements in u
  195. begin scalar x;
  196. if null u then return u;
  197. x:=list(car u);
  198. u := cdr u;
  199. for each y in u do
  200. if not member(y,x) then nconc(x,list(y));
  201. return x
  202. end;
  203. symbolic procedure listofvarnames u; %new
  204. % u is a list of s.p.
  205. % returns list of vars in u
  206. % we keep nil as placeholder for numbers in u
  207. if not listp u then rederr "invalid argument to listofvarnames"
  208. else for each x in u collect if domain!*p x then (nil . 'free)
  209. else if atom x then (nil . 'free)
  210. else if idp car x then ((car x) . 'free)
  211. else if idp caar x then ((caar x) . 'free);
  212. symbolic procedure replsublist(u,v,w); %new
  213. % v and w are p-lists
  214. % u is anything
  215. % replaces the sublist v in w by u
  216. begin scalar n,x,res;
  217. if not (x:= sublist(v,w)) then return w;
  218. n:= length(w)-length(x);
  219. % trwrite "n= ",n," x= ",x;
  220. % u := if listp u then u else list(u);
  221. % trwrite "u= ",u,listp u;
  222. res := if zerop n then nil
  223. else for k:= 1 :n collect nth(w,k);
  224. res := if null res then u else nconc(res,u);
  225. % trwrite "res= ",res;
  226. return if (length(v) = length(x)) then res
  227. else nconc(res,pnth(x,length(v)+1))
  228. end;
  229. symbolic procedure locate!_n(x,lst,n);
  230. % returns the position of the n-th occurence of x in lst
  231. % nil if not succesful
  232. begin scalar n2,lst2,ntot;
  233. if null lst then return nil;
  234. lst2 := lst;
  235. ntot:= 0;
  236. a: if n = 0 then return ntot;
  237. n2:= locate_member(x,lst2);
  238. % trwrite "n2=",n2," lst2= ",lst2;
  239. if null n2 then return nil;
  240. lst2 := cdr pnth(lst2,n2);
  241. ntot := ntot+n2;
  242. n:= n-1;
  243. go to a;
  244. end;
  245. symbolic procedure term2listpows u; %new
  246. % u is a s.t. containing only products
  247. % return a list of the s.p. of u
  248. begin
  249. trwrite(term2listpows,"u= ",u);
  250. return
  251. if null u then u
  252. else if atom u then list u
  253. else if domain!*p cdr u then car u . list cdr u
  254. else car u . term2listpows cadr u;
  255. end;
  256. symbolic procedure listprod2term u; %new
  257. % u is a list of product terms (numbers,s.p.,s.t.,s.f.)
  258. % value is the s.q. obtained by multiplying all the terms together
  259. begin scalar x,res;
  260. if not listp u then rederr "invalid argument to listprod2term";
  261. if null u then return u;
  262. res:= car u;
  263. res := if domain!*p res then !*d2q res
  264. else if spp res then !*p2q res else if stp res then !*t2q res
  265. else if sfp2 res then res . 1 else res;
  266. % trwrite "res= ",res;
  267. u :=cdr u;
  268. a: if null u then return res;
  269. x := car u;
  270. x := if domain!*p x then !*d2q x
  271. else if spp x then !*p2q x else if stp x then !*t2q x
  272. else if sfp2 x then x . 1 else x;
  273. u := cdr u;
  274. res := multsq(res,x);
  275. go to a;
  276. end;
  277. % this routine gives the position of an object in a list. the first
  278. % object is numbered 1. returns nil if the object can't be found.
  279. symbolic procedure locate_member(u,v);
  280. if not member(u,v) then nil
  281. else if u=car v then 1
  282. else 1+locate_member(u,cdr v);
  283. global '(domainlist!*);
  284. symbolic procedure domain!*p u;
  285. % this is a much more precise domain checker than domainp
  286. null u or numberp u or (not atom u and memq(car u,domainlist!*));
  287. %------------------------------------------------%
  288. % new defintions of noncom and testing functions %
  289. %------------------------------------------------%
  290. % clear previous definitions of noncom
  291. remflag('(noncom),'flagop);
  292. remprop('noncom,'stat);
  293. symbolic procedure noncomp2 u; % changed
  294. % u is a kernel checks for noncom flag
  295. if atom u then flagp(u,'noncom)
  296. else flagpcar(u,'noncom);
  297. symbolic procedure noncom u; %new
  298. begin scalar y,liste;
  299. if not listp u then rederr(u, "invalid argument to noncom");
  300. for each x in u do <<
  301. if not idp x then rederr(x, "invalid argument to noncom");
  302. noncom1 x;
  303. liste:=get(x,'noncommutes);
  304. y := delete(x,u);
  305. put(x,'noncommutes,deletemult!* nconc(liste,y));
  306. % the following is needed for the physop package added 2.1 140891 mw
  307. if (get(x,'rtype) = 'physop) then
  308. <<
  309. noncom1 adjp x;
  310. liste:=get(adjp x,'noncommutes);
  311. y := delete(adjp x,for each j in u collect adjp j);
  312. put(adjp x,'noncommutes,deletemult!* nconc(liste,y));
  313. noncom1 invp x;
  314. liste:=get(invp x,'noncommutes);
  315. y := delete(invp x,for each j in u collect invp j);
  316. put(invp x,'noncommutes,deletemult!* nconc(liste,y));
  317. >>;
  318. >>;
  319. return nil
  320. end;
  321. deflist('((noncom rlis)),'stat);
  322. symbolic procedure noncommuting(u,v); % modifed 2.1 140891 mw
  323. % u and v are two kernels
  324. % checks for noncommuting
  325. begin scalar list,res;
  326. u := if atom u then u else car u;
  327. v := if atom v then v else car v;
  328. if not (noncomp2 u and noncomp2 v) then nil
  329. else <<
  330. list :=get(u,'noncommutes);
  331. res:=member(v,list);
  332. >>;
  333. return res
  334. end;
  335. symbolic procedure noncommutingterm u; %new
  336. % u is a standard term
  337. % checks if there are some noncommuting products in u
  338. begin scalar x,y;
  339. if null u or domain!*p u or spp u then return nil;
  340. x := tvar u; % <-- term variable
  341. u := cdr u; % <-- tc (s.f.)
  342. a: if null u or domain!*p u then return nil;
  343. y := car u; % <-- lt
  344. if noncommutingf(x,list(y)) or noncommutingterm y then return t;
  345. u := cdr u;
  346. go to a
  347. end;
  348. symbolic procedure noncommutingf(x,u); % new
  349. % x is a kernel, u is a standard form
  350. % checks for noncommuting
  351. if domain!*p u then nil
  352. else noncommuting(x, mvar u) or noncommutingf(x, lc u)
  353. or noncommutingf(x, red u);
  354. symbolic procedure noncommutingsp(u,v);
  355. % u and v are sp or numbers
  356. if null u or null v or numberp u or numberp v then nil
  357. else noncommuting(car u,car v);
  358. symbolic procedure noncommuting!_splist(u,v);
  359. % u is a sp, v is a list of sp
  360. % checks if u commutes with all elements of v
  361. if null v or null u then nil
  362. else noncommutingsp(u,car v) or noncommuting!_splist(u,cdr v);
  363. %----------------------------------%
  364. % modified multiplication routine %
  365. %----------------------------------%
  366. symbolic procedure multf(u,v); % changed
  367. %u and v are standard forms.
  368. %value is standard form for u*v;
  369. begin scalar ncmp,x,y;
  370. a: if null u or null v then return nil
  371. else if u=1 then return v % onep
  372. else if v=1 then return u % onep
  373. else if domainp u then return multd(u,v)
  374. else if domainp v then return multd(v,u)
  375. else if not(!*exp or ncmp!* or wtl!* or x)
  376. then <<u := mkprod u; v := mkprod v; x := t; go to a>>;
  377. x := mvar u;
  378. y := mvar v;
  379. % the following line has been replaced
  380. % if (ncmp:= noncomp2 y) and noncomp2 x then return multfnc(u,v)
  381. if noncommuting(x,y) then return multfnc(u,v)
  382. % we have to put this clause here to prevent evaluation in case
  383. % of equal main vars
  384. else if noncommutingf(y, lc u) or (ordop(x,y) and (x neq y))
  385. then << x := multf(lc u,v);
  386. y := multf(red u,v);
  387. return if null x then y else lpow u .* x .+ y>>
  388. else if x eq y
  389. % two forms have the same mvars
  390. then << x := mkspm(x,ldeg u+ldeg v);
  391. y := addf(multf(red u,v),multf(!*t2f lt u,red v));
  392. return if null x or null(u := multf(lc u,lc v))
  393. then <<!*asymp!* := t; y>>
  394. else if x=1 then addf(u,y)
  395. else if null !*mcd then addf(!*t2f(x .* u),y)
  396. else x .* u .+ y>>;
  397. x := multf(u,lc v);
  398. y := multf(u,red v);
  399. return if null x then y else lpow v .* x .+ y
  400. end;
  401. %--------------------------------------------%
  402. % procedures for ordering of expressions %
  403. %--------------------------------------------%
  404. symbolic procedure ordp(u,v); % modified
  405. %returns true if u ordered ahead or equal to v, nil otherwise.
  406. %an expression with more structure at a given level is ordered
  407. % behind (and not ahead) of one with less;
  408. % ordering of numbers is left as default
  409. if null u then t
  410. else if null v then nil
  411. else if atom u then
  412. if atom v then
  413. if numberp u then
  414. if numberp v then not(u < v)
  415. else t
  416. else if numberp v then nil
  417. else orderp(u,v)
  418. else t
  419. else if atom v then nil
  420. else if car u=car v then ordp(cdr u,cdr v)
  421. else ordp(car u,car v);
  422. symbolic procedure reordop(u,v); %changed
  423. % modilfied so that every commuting op is ordered ahead
  424. % of every noncommuting op
  425. if noncommuting(u,v) then t
  426. else if noncomp2 u and not noncomp2 v then nil
  427. else if noncomp2 v and not noncomp2 u then t
  428. else ordop(u,v);
  429. %--------------------------------------------------%
  430. % procedures for handling noncommutative %
  431. % terms in pattern matching %
  432. %--------------------------------------------------%
  433. % we have to modify subs3f1 since the handling of noncom mvars
  434. % in subs3t is not correct so we must prevent the system from
  435. % calling this procedure
  436. symbolic procedure subs3f1(u,l,bool); %modified
  437. %u is a standard form.
  438. %l is a list of possible matches.
  439. %bool is a boolean variable which is true if we are at top level.
  440. %value is a standard quotient with all product substitutions made;
  441. begin scalar x,z;
  442. z := nil ./ 1;
  443. a: if null u then return z
  444. else if domainp u then return addsq(z,u ./ 1)
  445. else if bool and domainp lc u then go to c;
  446. % the following line has been changed 2.1
  447. x := if !*mymatch then !*subs3tnc(lt u,l)
  448. else subs3t(lt u,l);
  449. % x := if noncommutingterm lt u then !*subs3tnc(lt u,l)
  450. % else subs3t(lt u,l);
  451. if not bool %not top level;
  452. or not mchfg!* then go to b; %no replacement made;
  453. mchfg!* := nil;
  454. if numr x = u and denr x = 1 then <<x := u ./ 1; go to b>>
  455. % also shows no replacement made (sometimes true with non
  456. % commuting expressions)
  457. else if null !*resubs then go to b
  458. else if !*sub2 or powlis1!* then x := subs2q x;
  459. %make another pass;
  460. x := subs3q x;
  461. b: z := addsq(z,x);
  462. u := cdr u;
  463. go to a;
  464. c: x := list lt u ./ 1;
  465. go to b
  466. end;
  467. symbolic procedure !*subs3tnc(u,v); %new 2.2
  468. % header procedure for subs3tnc
  469. % u is a standard term, v a list of matching templates
  470. % call subs3tnc on every product term of u and return a s.q.
  471. % if u not standard term
  472. begin scalar x,y,res,mchfg;
  473. trwrite('subs3tnc,"before mchfg!*= ",mchfg!*);
  474. if domain!*p u then return !*d2q u;
  475. if kernelp u then return !*k2q u;
  476. if spp u then return !*p2q u;
  477. y := zerleg u; % transform u in a list of true s.t.
  478. trwrite('!*subs3tnc," y= ",y);
  479. res := (nil . 1);
  480. a: if null y then << mchfg!* := mchfg; return res >>;
  481. x := car y;
  482. y := cdr y;
  483. res := addsq(res,subs3tnc(x,v));
  484. if mchfg!* then <<mchfg := mchfg!*; mchfg!* := nil >>;
  485. trwrite('!*subs3tnc,"res= ",res);
  486. go to a
  487. end;
  488. symbolic procedure zerleg u; % new 2.2
  489. % u is a s. t.
  490. % value is a list of termlists
  491. % each termlist contains the s. p. of a true s. t. of u
  492. begin scalar x,res;
  493. if null u then return u;
  494. if domain!*p u then return list u;
  495. x:= car u; % <-- lpow
  496. u := cdr u; % <-- lc
  497. if null u then return list(list(x));
  498. if domain!*p u then return list(list(x,u));
  499. res := zerleg(car u);
  500. res := for each j in res collect (x . j);
  501. if null cdr u then return res
  502. else return append(res,zerleg(x . cdr u));
  503. end;
  504. symbolic procedure subs3tnc(termlist,v); %new
  505. % new version including more general templates
  506. % u is a list of s.p. from a product term in s. t. form (2.2),
  507. % v a list of matching templates.
  508. % value is the s.t. modified by relevant substitutions
  509. % (eg a s.q. in general case)
  510. begin scalar termlist2,templ,temp,tempsp,tempvar,freetemp,rhs,
  511. lhs,bool,boolp,matchinglist,x,y,z,z1,w,w1,termlist3,na,ka,n,k,
  512. prevterml2,nabs; % added 2.2
  513. % return trivial cases removed 2.2
  514. % if domain!*p u then return !*d2q u;
  515. % build a list of s.p. in u
  516. % termlist := term2listpows u; %this line replaced by argument 2.2
  517. % trwrite(subs3tnc, "termlist= ",termlist);
  518. mchfg!* := nil;
  519. % this is the main loop scanning each template
  520. % terminating if no match found
  521. a: if null v then return listprod2term termlist; %changed 2.2
  522. % these are the variable names in termlist moved here 2.2
  523. termlist2:= listofvarnames termlist;
  524. % select a template
  525. templ := car v;
  526. v := cdr v;
  527. % trwrite(subs3tnc," templ= ",templ," v= ",v);
  528. % rhs is an algebraic expression
  529. rhs := nth(templ,3);
  530. % boolean expression to be satisfied by the matching args
  531. bool := cdadr templ;
  532. % flag to indicate if exact power matching required
  533. boolp := caadr templ;
  534. trwrite(subs3tnc, "bool= ",bool," boolp= ",boolp);
  535. % lhs of templ is already a list of s.p.
  536. lhs := car templ;
  537. temp := nil; freetemp := nil; % initialization
  538. % first we separate the lhs in a list of free and of nonfree
  539. % variables
  540. for each x in reverse lhs do
  541. if memq(car x,frlis!*) then freetemp := x . freetemp
  542. else temp := x . temp;
  543. lhs := nil; % will be rebuilt later on
  544. trwrite(subs3tnc, "temp= ",temp,"freetemp= ",freetemp);
  545. if null temp then go to b;
  546. % we allow nonexact power matching only in the case of 2 sp in lhs
  547. boolp := if length(temp) = 2 then boolp
  548. else t;
  549. k := 1; % counter for number of terms in lhs
  550. na:= 1;
  551. nabs := 0; % added 2.2
  552. z1 := nil;
  553. matchinglist := nil;
  554. a1: if (k > length(temp)) then go to b;
  555. aa: if (k < na) then go to a;
  556. tempsp := nth(temp,k);
  557. tempvar := if idp car tempsp then car tempsp
  558. else caar tempsp;
  559. a2: n:= locate_member((tempvar . 'free),termlist2);
  560. if numberp n then go to ab;
  561. k := k-1;
  562. z1 := nil;
  563. lhs := if null lhs then lhs
  564. else cdr lhs;
  565. % 2.2 two lines added 210891 mw
  566. termlist2 := prevterml2;
  567. nabs := length(termlist) - length(termlist2); % update nabs
  568. go to aa;
  569. ab: % mark tempvar as being used in the pattern matching process
  570. termlist2 :=nconc(for k:=1 :(n-1) collect nth(termlist2,k),
  571. ((tempvar . 'used) . pnth(termlist2,n+1)));
  572. % trwrite(subs3tnc, "termlist2= ",termlist2);
  573. x:= nth(termlist,n+nabs); %2.2 modified to get the absolute position
  574. z:= mtchp1!*(x,tempsp,boolp,bool,z1);
  575. if null cdr z then go to a2;
  576. if car z then
  577. <<
  578. if not sublist(car z ,matchinglist) then
  579. matchinglist:= nconc(matchinglist,car z);
  580. trwrite(subs3tnc, "matchinglist= ",matchinglist);
  581. % do the substitutions of car z in temp and bool
  582. for each y in car z do
  583. <<
  584. bool := subst(cdr y,car y,bool);
  585. temp := subst(cdr y,car y,temp)
  586. >>;
  587. >>;
  588. lhs := x . lhs;
  589. trwrite(subs3tnc, "lhs= ",lhs);
  590. z1:= cdr z;
  591. na:= k;
  592. k:= k + 1;
  593. % 2.2: 3 lines added 210891 mw
  594. prevterml2 := termlist2;
  595. termlist2 := pnth!*(termlist2,n+1);
  596. nabs := nabs + n; %update the absolute position counter
  597. go to a1;
  598. b: if not sublist(car z1,matchinglist) then
  599. matchinglist:= nconc(matchinglist,car z1);
  600. % special hack for nonexact power matching
  601. if (length(lhs) = 2) then
  602. <<
  603. x := cadr lhs; % this is the first term !
  604. y := nth(temp,1);
  605. if ((na:= cdr y) neq (ka := cdr x)) then
  606. <<
  607. termlist := replsublist(list(car x .** (ka - na),
  608. car x .** na),
  609. list(car x .** ka),termlist);
  610. w := list(car x . na);
  611. >>
  612. else w:= list(x);
  613. x:= car lhs; % this is the second term
  614. y := nth(temp,2);
  615. if (na:= cdr y) neq (ka := cdr x) then
  616. <<
  617. termlist := replsublist(list(car x .** na,
  618. car x .** (ka - na)),
  619. list(car x .** ka),termlist);
  620. lhs := (car x . na) . w;
  621. >>
  622. else lhs := x . w;
  623. >>;
  624. % from here on in principle all the terms in lhs are matched
  625. lhs := reverse lhs;
  626. % cross check
  627. if null (termlist3 := sublist(lhs,termlist)) then go to a;
  628. n := length(termlist)-length(termlist3);
  629. % trwrite(subs3tnc, "n= ",n);
  630. % rebuild the termlist after rearrangement
  631. termlist := nconc(for k := 1 : n collect nth(termlist,k),
  632. termlist3);
  633. na := length(freetemp);
  634. if (na = 0) then go to d;
  635. freetemp := reverse freetemp;
  636. % recalculation of n is necessary because lhs do not sit
  637. % in front of termlist3
  638. n:= length(termlist) - length(member(car lhs,termlist));
  639. % match the free variable(s) to be placed in front
  640. if (n < na) then go to a;
  641. % take all the terms in front in this case
  642. if (na = 1) and (cdar freetemp = 1) then
  643. <<
  644. lhs := termlist;
  645. matchinglist:= nconc(matchinglist,list(caar freetemp .
  646. !*q2a listprod2term nconc(
  647. for k:=1 :n collect nth(termlist,k),
  648. for k:= (length(lhs)+1) : length(termlist3)
  649. collect nth(termlist3,k))));
  650. >>
  651. else for k:=1 :na do
  652. <<
  653. x := nth(termlist,n-k+1);
  654. y := nth(freetemp,k);
  655. z:= mtchp1(x,y,boolp,bool);
  656. if not sublist(car z ,matchinglist) then
  657. matchinglist:= nconc(matchinglist,car z);
  658. for each w in car z do
  659. y:= subst(cdr w,car w,y);
  660. lhs := y . lhs;
  661. if (na:= cdr y) neq (ka := cdr x) then
  662. <<
  663. termlist := replsublist(list(car x .** (ka - na),
  664. car x .** na),list(car x .** ka),termlist);
  665. n:= n+1;
  666. >>
  667. >>;
  668. d:
  669. trwrite(subs3tnc,"lhs= ",lhs);
  670. trwrite(susb3tnc," termlist= ",termlist);
  671. % trwrite(subs3tnc,"matchinglist= ",matchinglist);
  672. % replace the free variables in the rhs
  673. for each x in matchinglist do
  674. rhs:= subst(cdr x, car x,rhs);
  675. % trwrite(subs3tnc," rhs= ",rhs);
  676. % and finally we replace the lhs in u by the rhs
  677. % for this we have to replace in the termlist the s.p. of lhs by
  678. % the rhs converted to a standard quotient
  679. rhs := list(simp rhs);
  680. trwrite(subs3tnc," rhs= ",rhs);
  681. termlist:= replsublist(rhs,lhs,termlist);
  682. trwrite(subs3tnc, "resulting termlist = ",termlist);
  683. mchfg!* := t;
  684. return listprod2term termlist
  685. end;
  686. symbolic procedure mtchp1!*(u,v,boolp,bool,z);
  687. % u is a sp, v is a sp to be matched against x
  688. % boolp is a flg (t if exact power matching required)
  689. % bool is a boolean expr to be satisfied during matching
  690. % z is a list of possible matchings for the free variables in y
  691. % returns a list of matching pair lists first is that element of z
  692. % which leads to a succesful matching or nil
  693. begin scalar temp1,bool1,x,z1;
  694. if null z then return nconc(list(nil),mtchp2(u,v,boolp,bool));
  695. a: if null z then return list(nil);
  696. x:= car z;
  697. z:= cdr z;
  698. % trwrite "x= ",x," z= ",z;
  699. temp1:= v;
  700. bool1 := bool;
  701. for each w in x do
  702. <<
  703. temp1:= subst(cdr w,car w, temp1);
  704. bool1 := subst(cdr w,car w,bool1);
  705. >>;
  706. if (z1:=mtchp2(u,temp1,boolp,bool1)) then return x . z1;
  707. go to a;
  708. end;
  709. symbolic procedure mtchp2(u,v,boolp,bool);
  710. % does the same job as mtchp1 but more accurately
  711. % since mtchp1 does not check bool at all
  712. begin scalar z,x,reslist,bool1,bool2;
  713. z := reverse mtchp1(u,v,boolp,bool);
  714. if (bool = t) then return z;
  715. a: if null z then return reslist;
  716. x := car z;
  717. z := cdr z;
  718. bool1 := bool;
  719. for each w in x do bool1 := subst(cdr w,car w,bool1);
  720. bool2:= bool1;
  721. % trick used here to check for remaining free variables in bool
  722. for each w in frlis!* do bool2:=subst(nil,w, bool2);
  723. trwrite(mtchp2, "bool1= ",bool1," bool2= ",bool2);
  724. if ((bool2 = bool1) and null eval bool1) then return nil
  725. else reslist := x . reslist;
  726. go to a
  727. end;
  728. endmodule;
  729. end;