rlami.red 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. % ----------------------------------------------------------------------
  2. % $Id: rlami.red,v 1.18 1999/03/23 09:23:55 dolzmann Exp $
  3. % ----------------------------------------------------------------------
  4. % Copyright (c) 1995-1999 Andreas Dolzmann and Thomas Sturm
  5. % ----------------------------------------------------------------------
  6. % $Log: rlami.red,v $
  7. % Revision 1.18 1999/03/23 09:23:55 dolzmann
  8. % Changed copyright information.
  9. %
  10. % Revision 1.17 1999/03/22 08:07:56 sturm
  11. % Turned error message "select a language" in into "select a context".
  12. %
  13. % Revision 1.16 1999/03/21 13:39:44 dolzmann
  14. % Modified procedure rl_qvarchk: Reserved identifiers are not allowed
  15. % as quantified variables.
  16. %
  17. % Revision 1.15 1997/08/14 10:10:46 sturm
  18. % Renamed rldecdeg to rldecdeg1.
  19. % Added service rldecdeg.
  20. %
  21. % Revision 1.14 1997/08/13 12:45:46 dolzmann
  22. % Added procedure rl_s2a!-decdeg.
  23. %
  24. % Revision 1.13 1997/08/12 17:03:54 sturm
  25. % Fixed fancy printing for Xr and PC versions.
  26. %
  27. % Revision 1.12 1996/10/17 13:52:23 sturm
  28. % Introduced services rlvarl, rlfvarl, and rlbvarl. Renamed cl_varl to
  29. % cl_varl1 for this.
  30. %
  31. % Revision 1.11 1996/10/07 12:03:54 sturm
  32. % Added fluids for CVS and copyright information.
  33. %
  34. % Revision 1.10 1996/10/03 16:07:04 sturm
  35. % Modified error message in rl_s2a!-struct.
  36. %
  37. % Revision 1.9 1996/10/01 10:25:18 reiske
  38. % Introduced new service rltnf and related code.
  39. %
  40. % Revision 1.8 1996/09/29 14:21:28 sturm
  41. % Removed switch rlqeans. Introduced service rlqea instead.
  42. % Also introduced corresponding service rlgqea.
  43. %
  44. % Revision 1.7 1996/09/26 11:51:09 dolzmann
  45. % Do not use T as formal parameter.
  46. %
  47. % Revision 1.6 1996/09/05 11:16:48 dolzmann
  48. % Added procedures rl_cleanup, rl_a2s!-id, rl_s2a!-term, and rl_s2a!-struct.
  49. % Renamed procedure rl_a2s!-terml to rl_s2a!-struct.
  50. %
  51. % Revision 1.5 1996/07/02 15:12:28 sturm
  52. % Fixed a bug in length computation.
  53. %
  54. % Revision 1.4 1996/06/05 15:11:25 sturm
  55. % Added procedure rl_sub!*fof.
  56. %
  57. % Revision 1.3 1996/05/21 17:12:34 sturm
  58. % Removed rl_subfof. Substitution code has moved to cl.
  59. %
  60. % Revision 1.2 1996/05/12 08:28:23 sturm
  61. % Added procedures rl_s2a!-gqe and rl_s2a!-atl.
  62. %
  63. % Revision 1.1 1996/03/22 12:18:27 sturm
  64. % Moved and split.
  65. %
  66. % ----------------------------------------------------------------------
  67. lisp <<
  68. fluid '(rl_ami_rcsid!* rl_ami_copyright!*);
  69. rl_ami_rcsid!* :=
  70. "$Id: rlami.red,v 1.18 1999/03/23 09:23:55 dolzmann Exp $";
  71. rl_ami_copyright!* := "Copyright (c) 1995-1999 by A. Dolzmann and T. Sturm"
  72. >>;
  73. module rlami;
  74. % Reduce logic component algebraic mode interface. Submodule of [redlog].
  75. procedure rl_mk!*fof(u);
  76. % Reduce logic make tagged form of first-order formula. [u] is a
  77. % first-order formula. Returns pseudo Lisp prefix of [u]. This is
  78. % analogous to [mk!*sq] in [alg.red].
  79. rl_mk!*fof1 rl_csimpl u;
  80. procedure rl_mk!*fof1(u);
  81. % Reduce logic make tagged form of first-order formula subroutine.
  82. % [u] is a first-order formula. Returns pseudo Lisp prefix of [u].
  83. % This is analogous to [mk!*sq] in [alg.red].
  84. if u eq 'true or u eq 'false then
  85. mk!*sq simp u
  86. else if eqcar(u,'equal) then
  87. rl_prepfof1 u
  88. else
  89. '!*fof . rl_cid!* . u . if !*resubs then !*sqvar!* else {nil};
  90. procedure rl_reval(u,v);
  91. % Reduce logic [reval]. [u] is a formula in some mixed pseudo Lisp
  92. % prefix form where [car u] is either ['!*fof] or a first-order
  93. % operator; [v] is bool. Returns Lisp prefix of [u] in case [v] is
  94. % non-[nil], and pseudo Lisp prefix of [u] else.
  95. if v then rl_prepfof rl_simp1 u else rl_mk!*fof rl_simp1 u;
  96. procedure rl_csimpl(u);
  97. % Conditional simplify.
  98. if !*rlsimpl and getd 'rl_simpl then %???
  99. rl_simpl(u,{},-1)
  100. else
  101. u;
  102. procedure rl_prepfof(f);
  103. % [prep] first-order formula.
  104. rl_prepfof1 rl_csimpl f;
  105. procedure rl_prepfof1(f);
  106. % [prep] first-order formula subroutine.
  107. begin scalar op;
  108. op := rl_op f;
  109. if rl_tvalp op then
  110. return op;
  111. if rl_quap op then
  112. return {op,rl_var f,rl_prepfof1 rl_mat f};
  113. if rl_cxp op then
  114. return op . for each x in rl_argn f collect rl_prepfof1 x;
  115. % [f] is atomic.
  116. return apply(get(rl_cid!*,'rl_prepat),{f})
  117. end;
  118. procedure rl_cleanup(u,v);
  119. reval1(u,v);
  120. procedure rl_simp(u);
  121. % [simp] first-order formula.
  122. rl_csimpl rl_simp1 u;
  123. procedure rl_simp1(u);
  124. % Reduce logic [simp]. [u] is (pseudo) Lisp prefix of a formula.
  125. % Returns the formula encoded by [u].
  126. begin scalar w;
  127. if null rl_cid!* then rederr {"select a context"};
  128. if atom u then
  129. return rl_simpatom u;
  130. argnochk u;
  131. if (w := get(car u,'rl_simpfn)) then
  132. return if flagp(w,'full) then apply(w,{u}) else apply(w,{cdr u});
  133. if (w := get(car u,get(rl_cid!*,'simpfnname))) then
  134. return if flagp(w,'full) then apply(w,{u}) else apply(w,{cdr u});
  135. if (w := get(car u,'psopfn)) then
  136. return rl_simp1 apply1(w,cdr u);
  137. if flagp(car u,'opfn) then
  138. return rl_simp1 apply(car u,for each x in cdr u collect reval x);
  139. if (w := get(car u,'prepfn2)) then
  140. return rl_simp1 apply(w,{u});
  141. rl_redmsg(car u,"predicate");
  142. put(car u,get(rl_cid!*,'simpfnname),get(rl_cid!*,'simpdefault));
  143. return rl_simp1(u)
  144. end;
  145. procedure rl_simpatom(u);
  146. % Reduce logic simp atom. [u] is an atom.
  147. begin scalar w;
  148. if null u then typerr("nil","logical");
  149. if numberp u then typerr({"number",u},"logical");
  150. if stringp u then typerr({"string",u},"logical");
  151. if (w := rl_gettype(u)) then <<
  152. if w eq 'logical or w eq 'equation or w eq 'scalar then
  153. return rl_simp1 cadr get(u,'avalue);
  154. typerr({w,u},"logical")
  155. >>;
  156. % [u] algebraically unbound.
  157. if rl_tvalp u then return u;
  158. if boundp u then return rl_simp1 eval u;
  159. typerr({"unbound id",u},"logical")
  160. end;
  161. procedure rl_simpbop(f);
  162. % Reduce logic simp boolean operator.
  163. rl_mkn(car f,for each x in cdr f collect rl_simp1 x);
  164. procedure rl_simpq(f);
  165. % Reduce logic simp quantifier.
  166. begin scalar vl,w;
  167. vl := reval cadr f;
  168. if eqcar(vl,'list) then
  169. vl := cdr vl
  170. else
  171. vl := {vl};
  172. w := rl_simp1 caddr f;
  173. for each x in reverse vl do <<
  174. rl_qvarchk x;
  175. w := rl_mkq(car f,x,w)
  176. >>;
  177. flag(vl,'used!*);
  178. return w
  179. end;
  180. procedure rl_qvarchk(v);
  181. % Syntax-check quantified variable.
  182. if null v then
  183. typerr("nil","quantified variable")
  184. else if numberp v then
  185. typerr({"number",v},"quantified variable")
  186. else if stringp v then
  187. typerr({"string",v},"quantified variable")
  188. else if pairp v then
  189. typerr({"complex expression",v},"quantified variable")
  190. else if idp v and flagp(v,'reserved) then
  191. typerr({"reserved identifier",v},"quantified variable");
  192. procedure rl_simp!*fof(u);
  193. % Reduce logic simp [!*fof] operator. [u] is of the form
  194. % $([tag],f,[!*sqvar!*])$ where [tag] is a context tag and $f$ is a
  195. % formula.
  196. begin scalar tag,f,w;
  197. if caddr u then return cadr u; % [!*sqvar!*=T]
  198. tag := car u;
  199. f := cadr u;
  200. if tag neq rl_cid!* then <<
  201. w := rl_set {tag} where !*msg=nil;
  202. f := rl_prepfof f;
  203. rl_set w where !*msg=nil;
  204. return rl_simp f
  205. >>;
  206. return rl_resimp f
  207. end;
  208. procedure rl_resimp(u);
  209. % Reduce logic resimp. [u] is a formula.
  210. begin scalar op,w;
  211. op := rl_op u;
  212. if rl_tvalp op then
  213. return u;
  214. if rl_quap op then <<
  215. if (w := rl_gettype(rl_var u)) then
  216. typerr({w,rl_var u},"quantified variable");
  217. return rl_mkq(op,rl_var u,rl_resimp rl_mat u)
  218. >>;
  219. if rl_cxp op then
  220. return rl_mkn(op,for each x in rl_argn u collect rl_resimp x);
  221. return apply(get(rl_cid!*,'rl_resimpat),{u})
  222. end;
  223. procedure rl_gettype(v);
  224. % Get type. Return type information if present. Handle scalars
  225. % properly.
  226. (if w then car w else get(v,'rtype)) where w = get(v,'avalue);
  227. procedure rl_redmsg(u,v);
  228. % Reduce msg. [u] is an identifier, [v] is a category which must be
  229. % "predicate". Ask for declaring [u] predicate.
  230. if null !*msg or v neq "predicate" then
  231. nil % :-)
  232. else if terminalp() then
  233. yesp list("Declare",u,v,"?") or error1()
  234. else
  235. lprim list(u,"declared",v);
  236. procedure rl_lengthlogical(u);
  237. rl_lengthfof rl_simp u;
  238. procedure rl_lengthfof(f);
  239. % First order formula length. [u] is a formula. Returns the number
  240. % of top-level constituents of [u].
  241. begin scalar op;
  242. op := rl_op f;
  243. if rl_tvalp op then
  244. return 1;
  245. if rl_quap op then
  246. return 2;
  247. if rl_cxp op then
  248. return length rl_argn f;
  249. % [f] is atomic.
  250. return apply(get(rl_cid!*,'rl_lengthat),{f})
  251. end;
  252. procedure rl_sub!*fof(al,f);
  253. rl_mk!*fof rl_subfof(al,rl_simp f);
  254. procedure rl_print!*fof(u);
  255. maprin reval u;
  256. procedure rl_priq(qf);
  257. begin scalar m;
  258. if null !*nat then return 'failed;
  259. maprin car qf;
  260. prin2!* " ";
  261. maprin cadr qf;
  262. prin2!* " ";
  263. if pairp(m := caddr qf) and car m memq '(ex all) then
  264. maprin m
  265. else <<
  266. prin2!* "(";
  267. maprin m;
  268. prin2!* ")"
  269. >>
  270. end;
  271. procedure rl_ppriop(f,n);
  272. if null !*nat or null !*rlbrop or eqn(n,0) then
  273. 'failed
  274. else <<
  275. prin2!* "(";
  276. inprint(car f,get(car f,'infix),cdr f);
  277. prin2!* ")"
  278. >>;
  279. procedure rl_fancy!-ppriop(f,n);
  280. if null !*nat or null !*rlbrop or eqn(n,0) then
  281. 'failed
  282. else <<
  283. fancy!-prin2 "(";
  284. fancy!-inprint(car f,get(car f,'infix),cdr f);
  285. fancy!-prin2 ")"
  286. >>;
  287. procedure rl_fancy!-priq(qf);
  288. begin scalar m;
  289. if null !*nat then return 'failed;
  290. fancy!-prefix!-operator car qf;
  291. fancy!-prin2 " ";
  292. maprin cadr qf;
  293. fancy!-prin2 " ";
  294. if pairp(m := caddr qf) and car m memq '(ex all) then
  295. maprin m
  296. else <<
  297. fancy!-prin2 "(";
  298. maprin m;
  299. fancy!-prin2 ")"
  300. >>
  301. end;
  302. procedure rl_interf1(fname,evalfnl,oevalfnl,odefl,resconv,argl);
  303. begin integer l1,l2,l3; scalar w;
  304. if null eval intern compress nconc(explode fname,'(!! !*)) then
  305. rederr {"service",fname,"not implemented in context",rl_cid!*};
  306. l1 := length argl;
  307. l2 := length evalfnl;
  308. l3 := length oevalfnl;
  309. if l1 < l2 or l1 > l2 + l3 then
  310. rederr {fname,"called with",l1,"arguments instead of",l2,"-",l2+l3};
  311. argl := for each x in append(evalfnl,oevalfnl) collect <<
  312. if argl then <<
  313. w := car argl;
  314. argl := cdr argl
  315. >> else
  316. w := car odefl;
  317. if l2 = 0 then % evaluation of optional parameters
  318. odefl := cdr odefl
  319. else
  320. l2 := l2 - 1;
  321. apply(x,{w})
  322. >>;
  323. if !*rlrealtime then ioto_realtime();
  324. w := apply(resconv,{apply(fname,argl)});
  325. if !*rlrealtime then ioto_tprin2t {"Realtime: ",ioto_realtime()," s"};
  326. return w
  327. end;
  328. procedure rl_a2s!-decdeg1(u);
  329. if u eq 'fvarl then 'fvarl else rl_a2s!-varl u;
  330. procedure rl_a2s!-varl(l);
  331. begin scalar w;
  332. w := reval l;
  333. if not eqcar(w,'list) then typerr(w,"list");
  334. w := cdr w;
  335. for each x in w do
  336. if not idp x then typerr(x,"variable");
  337. return w
  338. end;
  339. procedure rl_a2s!-number(n);
  340. % Algebraic to symbolic number.
  341. begin
  342. n := reval n;
  343. if not numberp n then typerr(n,"number");
  344. return n
  345. end;
  346. procedure rl_a2s!-id(k);
  347. % Algebraic to symbolic identifier
  348. begin
  349. k := reval k;
  350. if not idp k then typerr(k,"identifier");
  351. return k
  352. end;
  353. procedure rl_a2s!-atl(l);
  354. % Algebraic to symbolic atomic formula list.
  355. begin scalar w,!*rlsimpl;
  356. l := reval l;
  357. if not eqcar(l,'list) then
  358. typerr(l,"list");
  359. return for each x in cdr l collect <<
  360. if rl_cxp rl_op (w := rl_simp x) then
  361. typerr(x,"atomic formula");
  362. w
  363. >>
  364. end;
  365. procedure rl_a2s!-posf(f);
  366. % Algebraic to symbolic positive formula.
  367. rl_nnf rl_simp f;
  368. procedure rl_s2a!-simpl(f);
  369. if f eq 'inctheo then rederr "inconsistent theory" else rl_mk!*fof f;
  370. procedure rl_s2a!-gqe(res);
  371. if res eq 'inctheo then
  372. rederr "inconsistent theory"
  373. else
  374. {'list,rl_s2a!-atl car res,rl_mk!*fof cdr res};
  375. procedure rl_s2a!-gqea(res);
  376. if res eq 'inctheo then
  377. rederr "inconsistent theory"
  378. else
  379. {'list,rl_s2a!-atl car res,rl_s2a!-qea cdr res};
  380. procedure rl_s2a!-qea(res);
  381. if res eq 'inctheo then
  382. rederr "inconsistent theory"
  383. else
  384. 'list . for each x in res collect
  385. {'list,rl_mk!*fof car x,'list . cadr x};
  386. procedure rl_s2a!-opt(res);
  387. if res eq 'infeasible then
  388. 'infeasible
  389. else
  390. {'list,mk!*sq car res,'list . for each x in cadr res collect 'list . x};
  391. procedure rl_s2a!-atl(l);
  392. 'list . for each x in l collect rl_mk!*fof x;
  393. procedure rl_s2a!-ml(ml,s2acar);
  394. 'list . for each p in ml collect {'list,apply(s2acar,{car p}),cdr p};
  395. procedure rl_s2a!-term(u);
  396. apply(get(rl_cid!*,'rl_prepterm),{u});
  397. procedure rl_s2a!-decdeg1(p);
  398. begin scalar w;
  399. w := if cdr p then
  400. for each x in cdr p collect {'list,car x,cdr x}
  401. else
  402. nil;
  403. return {'list,rl_mk!*fof car p,'list . w}
  404. end;
  405. procedure rl_a2s!-targfn(x);
  406. begin scalar w;
  407. w := simp x;
  408. if not domainp denr w then
  409. rederr {"variable in target function denominator"};
  410. return w
  411. end;
  412. procedure rl_a2s!-terml(l);
  413. begin scalar w;
  414. w := reval l;
  415. if not eqcar(w,'list) then
  416. typerr(l,"list");
  417. return for each x in cdr w collect
  418. apply(get(rl_cid!*,'rl_simpterm),{x})
  419. end;
  420. procedure rl_s2a!-terml(l);
  421. 'list . for each u in l collect apply(get(rl_cid!*,'rl_prepterm),{u});
  422. procedure rl_a2s!-term(l);
  423. apply(get(rl_cid!*,'rl_simpterm),{l});
  424. procedure rl_s2a!-varl(pr);
  425. {'list,'list . car pr,'list . cdr pr};
  426. procedure rl_s2a!-fbvarl(l);
  427. 'list . l;
  428. procedure rl_s2a!-struct(l);
  429. <<
  430. for each x in cdr l do
  431. if get(cdr x,'avalue) then
  432. rederr {"identifier",cdr x,"has an avalue"};
  433. {'list, rl_mk!*fof car l,
  434. 'list . for each x in cdr l collect
  435. {'equal,cdr x,prepf car x}}
  436. >>;
  437. foractions!* := 'mkand . 'mkor . foractions!*;
  438. deflist('((mkand rlmkand) (mkor rlmkor)),'bin);
  439. deflist('((mkand (quote true)) (mkor (quote false))),'initval);
  440. symbolic operator rlmkor,rlmkand;
  441. procedure rlmkor(a,b);
  442. if !*mode eq 'symbolic then
  443. rederr "`mkor' invalid in symbolic mode"
  444. else <<
  445. if null a then a := 'false;
  446. if null b then b := 'false;
  447. a := if eqcar(a,'or) then cdr a else {a};
  448. b := if eqcar(b,'or) then cdr b else {b};
  449. 'or . nconc(b,a)
  450. >>;
  451. procedure rlmkand(a,b);
  452. if !*mode eq 'symbolic then
  453. rederr "`mkand' invalid in symbolic mode"
  454. else <<
  455. if null a then a := 'true;
  456. if null b then b := 'true;
  457. a := if eqcar(a,'and) then cdr a else {a};
  458. b := if eqcar(b,'and) then cdr b else {b};
  459. 'and . nconc(b,a)
  460. >>;
  461. endmodule; % [rlami]
  462. end; % of file