clmisc.red 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. % ----------------------------------------------------------------------
  2. % $Id: clmisc.red,v 1.15 1999/04/13 13:10:57 sturm Exp $
  3. % ----------------------------------------------------------------------
  4. % Copyright (c) 1995-1999 Andreas Dolzmann and Thomas Sturm
  5. % ----------------------------------------------------------------------
  6. % $Log: clmisc.red,v $
  7. % Revision 1.15 1999/04/13 13:10:57 sturm
  8. % Updated comments for exported procedures.
  9. %
  10. % Revision 1.14 1999/03/22 17:08:06 dolzmann
  11. % Changed copyright information.
  12. %
  13. % Revision 1.13 1999/03/21 13:34:24 dolzmann
  14. % Removed unused procedure cl_varmem.
  15. % Corrected comments.
  16. %
  17. % Revision 1.12 1999/01/17 16:02:30 dolzmann
  18. % Added and corrected comments.
  19. %
  20. % Revision 1.11 1997/10/01 11:11:42 dolzmann
  21. % Added procedure cl_splt.
  22. %
  23. % Revision 1.10 1997/08/24 16:18:48 sturm
  24. % Added service rl_surep with black box rl_multsurep.
  25. % Added service rl_siaddatl.
  26. %
  27. % Revision 1.9 1997/04/08 14:33:17 sturm
  28. % Fixed a bug in cl_subfof: sub(y=z,ex(x,x+y=0)) did unnecessarily rename x.
  29. %
  30. % Revision 1.8 1996/10/23 14:28:55 sturm
  31. % sub(a=0,ex(x,true)) crashed due to the truth value within the
  32. % quantifier scope.
  33. %
  34. % Revision 1.7 1996/10/17 13:54:07 sturm
  35. % Renamed cl_varl to cl_varl1.
  36. % Rename cl_fvarl to cl_subfvarl and removed redundant parameter there.
  37. % Same for cl_fvarl1.
  38. %
  39. % Revision 1.6 1996/10/07 11:45:50 sturm
  40. % Added fluids for CVS and copyright information.
  41. %
  42. % Revision 1.5 1996/09/05 11:11:51 dolzmann
  43. % Added procedure cl_f2ml. Procedure cl_atml1 now uses cl_f2ml.
  44. % New procedures for determing multiplicity lists and sets of terms or
  45. % irreducible factors.
  46. % Added procedures cl_struct and cl_ifstruct and related code.
  47. %
  48. % Revision 1.4 1996/07/07 14:35:16 sturm
  49. % Introduced new black box rl_eqnrhskernels.
  50. %
  51. % Revision 1.3 1996/06/05 15:08:42 sturm
  52. % Second argument of cl_subfof is a formula now.
  53. % Added renaming to cl_subfof.
  54. %
  55. % Revision 1.2 1996/05/21 17:13:19 sturm
  56. % Added service implementation cl_subfof.
  57. %
  58. % Revision 1.1 1996/03/22 10:31:28 sturm
  59. % Moved and split.
  60. %
  61. % ----------------------------------------------------------------------
  62. lisp <<
  63. fluid '(cl_misc_rcsid!* cl_misc_copyright!*);
  64. cl_misc_rcsid!* := "$Id: clmisc.red,v 1.15 1999/04/13 13:10:57 sturm Exp $";
  65. cl_misc_copyright!* := "(c) 1995-1999 by A. Dolzmann and T. Sturm"
  66. >>;
  67. module clmisc;
  68. % Common logic miscellaneous algorithms. Submodule of [cl].
  69. procedure cl_apply2ats(f,client);
  70. % Common logic apply to atomic formulas. [f] is formula; [client]
  71. % is a function that maps atomic formulas to formulas. Returns a
  72. % formula derived from [f] by replacing each atomic formula
  73. % $\alpha$ by $[client](\alpha)$.
  74. cl_apply2ats1(f,client,nil);
  75. procedure cl_apply2ats1(f,client,clpl);
  76. % Common logic apply to atomic formulas variant. [f] is formula;
  77. % [client] is a function $[client](a,p_1,...,p_n)$; [clpl] is a
  78. % list $(s_1,...,s_n)$; the first argument $a$ of [clpl] is an
  79. % atomic formula, the specifications of the remaining arguments
  80. % $p_1,...,p_n$ are met by the entries $s_1,...,s_n$ of [clpl]; the
  81. % return value of [client] is an atomic formula. Returns a formula
  82. % derived from [f] by replacing each atomic formula $\alpha$ by
  83. % $[client](\alpha,s_1,...,s_n)$.
  84. begin scalar op;
  85. op := rl_op f;
  86. if rl_tvalp op then return f;
  87. if rl_quap op then
  88. return rl_mkq(op,rl_var f,cl_apply2ats1(
  89. rl_mat f,client,clpl));
  90. if rl_boolp op then
  91. return rl_mkn(op,for each subf in rl_argn f collect
  92. cl_apply2ats1(subf,client,clpl));
  93. % [f] is an atomic formula.
  94. return apply(client,f . clpl)
  95. end;
  96. procedure cl_apply2ats2(f,client,clpl,sop);
  97. % Common logic apply to atomic formulas variant. [f] is formula;
  98. % [client] is a function $[client](a,r,p_1,...,p_n)$; [clpl] is a
  99. % list $(s_1,...,s_n)$; the first argument $a$ of [clpl] is an
  100. % atomic formula, the second argument $r$ is a non-atomic formula
  101. % operator, the specifications of the remaining arguments
  102. % $p_1,...,p_n$ are met by the entries $s_1,...,s_n$ of [clpl]; the
  103. % return value of [client] is an atomic formula; [sop] is an
  104. % operator. Returns a formula derived from [f] by replacing each
  105. % atomic formula $\alpha$ by $[client](\alpha,r_0,s_1,...,s_n)$,
  106. % where $r_0$ is the operator of the complex subformula in which
  107. % $\alpha$ occurs.
  108. begin scalar op;
  109. op := rl_op f;
  110. if rl_tvalp op then return f;
  111. if rl_quap op then
  112. return rl_mkq(op,rl_var f,cl_apply2ats2(
  113. rl_mat f,client,clpl,op));
  114. if rl_boolp op then
  115. return rl_mkn(op,for each subf in rl_argn f collect
  116. cl_apply2ats2(subf,client,clpl,op));
  117. % [f] is an atomic formula.
  118. return apply(client,f . sop . clpl)
  119. end;
  120. procedure cl_atnum(f);
  121. % Common logic atomic formula nummber. [f] is a formula. Returns
  122. % the number of atomic formulas in [f] counting multiplicities.
  123. begin scalar op;
  124. op := rl_op f;
  125. if rl_boolp op then
  126. return for each subf in rl_argn f sum
  127. cl_atnum(subf);
  128. if rl_quap op then
  129. return cl_atnum(rl_mat f);
  130. if rl_tvalp op then return 0;
  131. % [f] is an atomic formula.
  132. return 1
  133. end;
  134. %DS
  135. % <MULTIPLICITY LIST> ::= (..., (<S-EXPRESSION> . <OCCURRENCES>), ...)
  136. % <OCCURRENCES> ::= <INTEGER>
  137. procedure cl_f2ml(f,client);
  138. % Common logic formula to multiplicity list. [f] is a formula;
  139. % [client] is a procedure that maps an atomic formula to a
  140. % MULTIPLICITY LIST. Retuns a MULTIPLICITY LIST. [client] is
  141. % applied to all atomic formulas in [f], and the resulting
  142. % MULTIPLICITY LIST are merged.
  143. begin scalar op;
  144. op := rl_op f;
  145. if rl_tvalp f then
  146. return nil;
  147. if rl_boolp op then
  148. return lto_almerge(
  149. for each subf in rl_argn f collect cl_f2ml(subf,client),'plus2);
  150. if rl_quap op then
  151. return cl_f2ml(rl_mat f,client);
  152. % [f] is an atomic formula.
  153. return apply(client,{f})
  154. end;
  155. procedure cl_atml(f);
  156. % Common logic atomic formula multiplicity list. [f] is a formula.
  157. % Returns a MULTYPLICITY LIST of the atomic formulas occurring in
  158. % [f]. The result is sorted wrt. [rl_ordatp].
  159. sort(cl_atml1 f,function(lambda(x,y); rl_ordatp(car x,car y)));
  160. procedure cl_atml1(f);
  161. % Common logic atomic formula multiplicity list subroutine. [f] is
  162. % a formula. Returns a MULTYPLICITY LIST of the atomic formulas
  163. % occurring in [f].
  164. cl_f2ml(f,'cl_atmlc);
  165. procedure cl_atmlc(atf);
  166. {atf . 1};
  167. procedure cl_atl(f);
  168. % Common logic atomic formula list. Returns the set of atomic
  169. % formulas contained in [f] as a list. The result is sorted wrt.
  170. % [rl_ordatp].
  171. sort(cl_atl1 f,'rl_ordatp);
  172. procedure cl_atl1(f);
  173. % Common logic atomic formula list. Returns the set of atomic
  174. % formulas contained in [f] as a list.
  175. for each x in cl_atml1 f collect car x;
  176. procedure cl_identifyonoff(b);
  177. % Common logic identify on off. [b] is bool. This is the [simpfg]
  178. % of the switch [rlidentify]. Clears fluid [cl_identify!-atl!*].
  179. cl_identify!-atl!* := nil;
  180. procedure cl_ifacml(f);
  181. % Common logic irreducible factors multiplicity list. [f] is a
  182. % formula. Returns the MULTIPLICITY LIST of all irreducible
  183. % non-unit factors of the terms occurring in [f]. The result is
  184. % sorted wrt. [rl_tordp].
  185. sort(cl_ifacml1 f,function(lambda(x,y); rl_tordp(car x,car y)));
  186. procedure cl_ifacml1(f);
  187. % Common logic irreducible factors multiplicity list subroutine.
  188. % [f] is a formula. Returns the MULTIPLICITY LIST of all
  189. % irreducible non-unit factors of the terms occurring in [f].
  190. cl_f2ml(f,'rl_fctrat);
  191. procedure cl_ifacl(f);
  192. % Common logic irreducible factors list. [f] is a formula. Returns
  193. % the set of all irreducible non-unit factors of the terms
  194. % occurring in [f] as a list. The result is sorted wrt. [rl_tordp].
  195. sort(cl_ifacl1 f,'rl_tordp);
  196. procedure cl_ifacl1(f);
  197. % Common logic irreducible factors list subroutine. [f] is a
  198. % formula. Returns the set of all irreducible non-unit factors of
  199. % the terms occurring in [f] as a list.
  200. for each x in cl_ifacml1 f collect car x;
  201. procedure cl_matrix(f);
  202. % Common logic formula matrix. [f] is a formula. Returns a formula.
  203. % Remove all leading quantifiers from [f].
  204. if rl_quap rl_op f then cl_matrix rl_mat f else f;
  205. procedure cl_closure(q,f,nl);
  206. % Common logig closure. [q] is one of the quantifers [ex], [all];
  207. % [f] is a formula; [nl] is a list of variables. Returns the
  208. % formula $[q] v_1 ... [q] v_n ([f])$ where the $v_i$ are all free
  209. % variables of [f] that are not in [nl].
  210. begin scalar freevarl,result;
  211. % Create a list of all free variables.
  212. freevarl := car cl_varl1 f;
  213. % Remove the variables of the negative list.
  214. for each v in nl do
  215. freevarl := delqip(v,freevarl);
  216. % [q]-quantify with the remaining variables.
  217. result := f;
  218. for each x in freevarl do
  219. result := rl_mkq(q,x,result);
  220. return result
  221. end;
  222. procedure cl_all(f,nl);
  223. % Common logic [all]-quantify; universal closure. [f] is a formula;
  224. % [nl] is a list of variables. Returns the formula $\forall v_1 ...
  225. % \forall v_n ([f])$, where the $v_i$ are all free variables of [f]
  226. % that are not in [nl].
  227. cl_closure('all,f,nl);
  228. procedure cl_ex(f,nl);
  229. % Common logic [ex]-quantify; existential closure. [f] is a
  230. % formula; [nl] is a list of variables. Returns the formula
  231. % $\exists v_1 ... \exists v_n ([f])$ where the $v_i$ are all free
  232. % variables of [f] that are not in [nl].
  233. cl_closure('ex,f,nl);
  234. procedure cl_flip(op);
  235. % Common logic flip. [op] is one of the operators [and], [or],
  236. % [all], [ex], [true], [false]. Returns an operator. Maps [and] to
  237. % [or], [all] to [ex], [true] to [false], and vice versa.
  238. if op eq 'and then 'or
  239. else if op eq 'or then 'and
  240. else if op eq 'all then 'ex
  241. else if op eq 'ex then 'all
  242. else if op eq 'true then 'false
  243. else if op eq 'false then 'true
  244. else rederr {"cl_flip(): don't know",op};
  245. procedure cl_cflip(op,flag);
  246. % Common logic conditionally flip. [op] is one of the operators
  247. % [and], [or], [all], [ex], [true], [false]; [flag] is bool.
  248. % Returns an operator. Returns [op] if [flag] is [true], and
  249. % [cl_flip op] else.
  250. if flag then op else cl_flip op;
  251. procedure cl_subfof(al,f);
  252. % Common logic substitute into first-order formula. [al] is an
  253. % ALIST $(..., (v_i . p_i), ...)$, where $v_i$ are variables and
  254. % $p_i$ are Lisp prefix forms; [f] is a formula. Returns a formula.
  255. % In the retuned formula, all occurrences of the $v_i$ are replaced
  256. % by the $p_i$. If the $p_i$ contain parametric denominators then
  257. % an error is raised.
  258. begin scalar asgal,w,allvl;
  259. rl_subalchk al;
  260. for each x in al do <<
  261. w := rl_eqnrhskernels(x);
  262. asgal := lto_alunion {{car x . w},asgal};
  263. allvl := car x . append(w,allvl)
  264. >>;
  265. w := cl_varl1 f;
  266. allvl := lto_nconcn {allvl,car w,cdr w};
  267. return cl_subfof1(al,f,asgal,allvl)
  268. end;
  269. procedure cl_subfof1(al,f,asgal,allvl);
  270. % Common logic substitute first-order formula. [al] is an ALIST;
  271. % [f] is a formula; [asgal] is an ALIST; [allvl] is a list of
  272. % variables. Returns a formula. [al] is of the form $(...,(v_i .
  273. % p_i),...)$, where $v_i$ are variables and $p_i$ are Lisp-prefix
  274. % forms. In the retuned formula all occurences of the $v_i$ are
  275. % replaced by the $p_i$. If the $p_i$ contain parametric
  276. % denominators an error is raised.
  277. begin scalar op,v,newv,m;
  278. op := rl_op f;
  279. if rl_tvalp op then
  280. return f;
  281. if rl_quap op then <<
  282. v := rl_var f;
  283. m := rl_mat f;
  284. al := for each x in al join if not eqcar(x,v) then {x};
  285. asgal := for each x in asgal join if not eqcar(x,v) then {x};
  286. newv := cl_newv(v,m,asgal,allvl);
  287. if newv neq v then <<
  288. allvl := newv . allvl;
  289. m := cl_subvarsubstat(newv,v,m)
  290. >>;
  291. return rl_mkq(op,newv,cl_subfof1(al,m,asgal,allvl))
  292. >>;
  293. if rl_boolp op then
  294. return rl_mkn(op,for each x in rl_argn f collect
  295. cl_subfof1(al,x,asgal,allvl));
  296. % [f] is atomic.
  297. return rl_subat(al,f)
  298. end;
  299. procedure cl_newv(v,m,asgal,allvl);
  300. % Common logic new v. Returns a variable. Rename [v] if necessary.
  301. begin scalar a,fvl,w,newv; integer n;
  302. newv := v;
  303. fvl := cl_subfvarl m;
  304. while fvl do <<
  305. a := car fvl;
  306. fvl := cdr fvl;
  307. if (w := atsoc(a,asgal)) and v memq w then <<
  308. % There is a substitution of [v] for a free variable.
  309. repeat <<
  310. newv := mkid(v,n);
  311. n := n + 1
  312. >> until not (newv memq allvl or get(v,'avalue));
  313. fvl := nil
  314. >>
  315. >>;
  316. return newv
  317. end;
  318. procedure cl_subvarsubstat(newv,oldv,f);
  319. begin scalar op;
  320. op := rl_op f;
  321. if rl_quap op then
  322. if rl_var f eq oldv then
  323. return rl_mkq(op,newv,cl_subvarsubstat(newv,oldv,rl_mat f))
  324. else
  325. return rl_mkq(op,rl_var f,cl_subvarsubstat(newv,oldv,rl_mat f));
  326. if rl_boolp op then
  327. return rl_mkn(op,for each x in rl_argn f collect
  328. cl_subvarsubstat(newv,oldv,x));
  329. return rl_varsubstat(f,newv,oldv)
  330. end;
  331. procedure cl_subfvarl(m);
  332. cl_subfvarl1(m,nil);
  333. procedure cl_subfvarl1(f,cbvl);
  334. begin scalar op;
  335. op := rl_op f;
  336. if rl_quap op then
  337. return cl_subfvarl1(rl_mat f,rl_var f . cbvl);
  338. if rl_boolp op then
  339. return for each x in rl_argn f join cl_subfvarl1(x,cbvl);
  340. if rl_tvalp op then
  341. return nil;
  342. return for each x in rl_varlat f join if not (x memq cbvl) then {x}
  343. end;
  344. procedure cl_termml(f);
  345. % Common logic term multiplicity list. [f] is a formula. Returns
  346. % the MULTIPLICITY LIST of all non-zero terms occurring in
  347. % [f]. The result is sorted wrt. [rl_tordp].
  348. sort(cl_termml1 f,function(lambda(x,y); rl_tordp(car x,car y)));
  349. procedure cl_termml1(f);
  350. % Common logic term multiplicity list subroutine. [f] is a formula.
  351. % Returns the MULTIPLICITY LIST of all non-zero terms occurring in
  352. % [f].
  353. cl_f2ml(f,'rl_termmlat);
  354. procedure cl_terml(f);
  355. % Common logic term list. [f] is a formula. Returns the set of all
  356. % non-zero terms occurring in [f] as a list. The result is sorted wrt.
  357. % [rl_tordp].
  358. sort(cl_terml1 f,'rl_tordp);
  359. procedure cl_terml1(f);
  360. % Common logic term list subroutine. [f] is a formula. Returns the
  361. % set of all non-zero terms occurring in [f] as a list.
  362. for each x in cl_termml1 f collect car x;
  363. procedure cl_struct(f,v);
  364. % Common logic structure of a formula. [f] is a formula; [v] is a
  365. % kernel. Returns a pair $(\phi . (..., (v_i . t_i), ...))$. The
  366. % $v_i$ are the kernels $[v] \circ i$ with $i = 1, 2, ...$; the
  367. % $t_i$ are the terms occurring in [f]. $\phi$ is a formula
  368. % obtained from [f] by replacing each term $t_i$ with $v_i$.
  369. begin scalar w; integer j;
  370. w := cl_terml(f);
  371. w := for each s in w collect
  372. (s . mkid(v,j := j+1));
  373. return cl_struct1(f,w) . w;
  374. end;
  375. procedure cl_struct1(f,al);
  376. cl_apply2ats1(f,'rl_structat,{al});
  377. procedure cl_ifstruct(f,v);
  378. % Common logic irreducible factor structure of a formula. [f] is a
  379. % formula; [v] is a kernel. Returns a pair $(\phi . (..., (v_i .
  380. % s_i), ...))$. The $v_i$ are the kernels $[v] \circ i$ with $i =
  381. % 1, 2, ...$; the $s_i$ are the irreducible non-unit factors of the
  382. % terms occurring in [f]. $\phi$ is a formula obtained from [f] by
  383. % replacing within the terms each factor $s_i$ with $v_i$. That is,
  384. % the terms of $\phi$ are products of kernels $[v] \circ i$, $i =
  385. % 1, 2, ...$.
  386. begin scalar w; integer j;
  387. w := cl_ifacl(f);
  388. w := for each s in w collect
  389. (s . mkid(v,j := j+1));
  390. return cl_ifstruct1(f,w) . w;
  391. end;
  392. procedure cl_ifstruct1(f,al);
  393. cl_apply2ats1(f,'rl_ifstructat,{al});
  394. procedure cl_surep(at,atl);
  395. % Common logic sure predicate. [at] is an atomic formula; [atl] is
  396. % a THEORY. Returns bool. Heurictically check whether [at] follows
  397. % from [atl].
  398. if !*rlspgs then
  399. rl_gsd(at,atl) eq 'true or rl_multsurep(at,atl)
  400. else
  401. rl_simpl(at,atl,-1) eq 'true or rl_multsurep(at,atl);
  402. %DS
  403. % <QBLK> ::= (<QUANTIFIER> . <VARLIST>)
  404. % <QUANTIFIER> ::= ['ex] | ['all]
  405. % <VARLIST> ::= (...,<VARIABLE>,...)
  406. procedure cl_splt(f);
  407. % Common logic split. [f] is a formula in prenex normal form.
  408. % Returns a list $(\Gamma,\phi,\Lambda)$, where $\Gamma$ is a list
  409. % of QBLK's, $\phi$ is the quantifier-free matrix formula of [f],
  410. % and $\Lambda$ is the list of all bound variables. $\Gamma$
  411. % contains the quantifier blocks of [f] in reverse order.
  412. begin scalar w,q,vl,qblkl,bvl,v;
  413. q := rl_op f;
  414. if not(q memq '(ex all)) then
  415. return {nil,f,nil};
  416. while (w := rl_op f) memq '(ex all) do <<
  417. v := rl_var f;
  418. bvl := v . bvl;
  419. if w eq q then
  420. vl := v . vl
  421. else <<
  422. qblkl := (q . vl) . qblkl;
  423. q := w;
  424. vl := {v}
  425. >>;
  426. f := rl_mat f
  427. >>;
  428. qblkl := (q . vl) . qblkl;
  429. return {qblkl,f,bvl}
  430. end;
  431. endmodule; % [clmisc]
  432. end; % of file