rsupport.red 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. module rsupport; % Basic functions needed to support RLISP and REDUCE.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  4. fluid '(!*backtrace);
  5. global '(!*comp);
  6. symbolic procedure aconc(u,v);
  7. % Adds element v to the tail of u. u is destroyed in process.
  8. nconc(u,list v);
  9. symbolic procedure arrayp u; get(u,'rtype) eq 'array;
  10. symbolic procedure atsoc(u,v);
  11. % This definition allows for a search of a general list.
  12. if null v then nil
  13. else if eqcar(car v,u) then car v
  14. else atsoc(u,cdr v);
  15. symbolic procedure copyd(new,old);
  16. % Copy the function definition from old id to new.
  17. begin scalar x;
  18. x := getd old;
  19. if null x
  20. then rerror('rlisp,1,list(old,"has no definition in copyd"));
  21. putd(new,car x,cdr x);
  22. return new
  23. end;
  24. symbolic procedure eqcar(u,v); null atom u and car u eq v;
  25. symbolic procedure errorset!*(u,v); errorset(u,v,!*backtrace);
  26. symbolic procedure errorset2 u;
  27. begin scalar !*protfg;
  28. !*protfg := t;
  29. return errorset(u,nil,nil)
  30. end;
  31. symbolic procedure flagpcar(u,v);
  32. null atom u and idp car u and flagp(car u,v);
  33. symbolic procedure idlistp u;
  34. % True if u is a list of id's.
  35. null u or null atom u and idp car u and idlistp cdr u;
  36. symbolic procedure listp u;
  37. % Returns T if U is a top level list.
  38. null u or null atom u and listp cdr u;
  39. symbolic procedure mkprog(u,v); 'prog . (u . v);
  40. symbolic procedure mkquote u; list('quote,u);
  41. symbolic procedure mksetq(u,v);
  42. if atom u then list('setq,u,v)
  43. else begin scalar x;
  44. if (x := get(car u,'setfn)) then return apply2(x,u,v)
  45. else typerr(u,"assignment argument")
  46. end;
  47. symbolic procedure pairvars(u,vars,mode);
  48. % Sets up pairings of parameters and modes.
  49. begin scalar x;
  50. a: if null u then return append(reversip!* x,vars)
  51. else if null idp car u or get(car u,'infix) or get(car u,'stat)
  52. then symerr(list("Invalid parameter:",car u),nil);
  53. x := (car u . mode) . x;
  54. u := cdr u;
  55. go to a
  56. end;
  57. symbolic procedure prin2t u; progn(prin2 u, terpri(), u);
  58. % The following is included for compatibility with some old code.
  59. % Its use is discouraged.
  60. symbolic procedure princ u; prin2 u;
  61. symbolic procedure putc(name,type,body);
  62. % Defines a non-standard function, such as an smacro. Returns NAME.
  63. begin
  64. if !*comp and flagp(type,'compile) then compd(name,type,body)
  65. else put(name,type,body);
  66. return name
  67. end;
  68. % flag('(putc),'eval);
  69. symbolic procedure reversip u;
  70. begin scalar x,y;
  71. a: if null u then return y;
  72. x := cdr u; y := rplacd(u,y); u := x;
  73. go to a
  74. end;
  75. symbolic procedure smemq(u,v);
  76. % True if id U is a member of V at any level (excluding quoted
  77. % expressions).
  78. if atom v then u eq v
  79. else if car v eq 'quote then nil
  80. else smemq(u,car v) or smemq(u,cdr v);
  81. symbolic procedure subsetp(u,v);
  82. % True if u is a subset of v.
  83. null u or car u member v and subsetp(cdr u,v);
  84. symbolic procedure union(x,y);
  85. if null x then y
  86. else union(cdr x,if car x member y then y else car x . y);
  87. symbolic procedure intersection(u,v);
  88. % This definition is consistent with PSL.
  89. if null u then nil
  90. else if car u member v
  91. then car u . intersection(cdr u,delete(car u,v))
  92. else intersection(cdr u,v);
  93. symbolic procedure u>=v; null(u<v);
  94. symbolic procedure u<=v; null(u>v);
  95. symbolic procedure u neq v; null(u=v);
  96. symbolic procedure setdiff(u,v);
  97. if null v then u
  98. else if null u then nil
  99. else setdiff(delete(car v,u),cdr v);
  100. % symbolic smacro procedure u>=v; null(u<v);
  101. % symbolic smacro procedure u<=v; null(u>v);
  102. % symbolic smacro procedure u neq v; null(u=v);
  103. % List changing alternates (may also be defined as copying functions).
  104. symbolic procedure aconc!*(u,v); nconc(u,list v); % append(u,list v);
  105. symbolic procedure nconc!*(u,v); nconc(u,v); % append(u,v);
  106. symbolic procedure reversip!* u; reversip u; % reverse u;
  107. symbolic procedure rplaca!*(u,v); rplaca(u,v); % v . cdr u;
  108. symbolic procedure rplacd!*(u,v); rplacd(u,v); % car u . v;
  109. % The following functions should be provided in the compiler for
  110. % efficient coding.
  111. symbolic procedure lispapply(u,v);
  112. % I'd like to use idp in the following test, but the TPS package
  113. % stores code pointers on property lists which then get used here.
  114. if null atom u
  115. then rerror('rlisp,2,list("Apply called with non-id arg",u))
  116. else apply(u,v);
  117. symbolic procedure lispeval u; eval u;
  118. symbolic procedure apply1(u,v); apply(u,list v);
  119. symbolic procedure apply2(u,v,w); apply(u,list(v,w));
  120. symbolic procedure apply3(u,v,w,x); apply(u,list(v,w,x));
  121. % The following function is needed by several modules. It is more
  122. % REDUCE-specific than other functions in this module, but since it
  123. % needs to be defined early on, it might as well go here.
  124. symbolic procedure gettype u;
  125. % Returns a REDUCE-related type for the expression U.
  126. % It needs to be more table driven than the current definition.
  127. if numberp u then 'number
  128. else if null atom u or null u or null idp u then 'form
  129. else if get(u,'simpfn) then 'operator
  130. else if get(u,'avalue) then car get(u,'avalue)
  131. else if getd u then 'procedure
  132. else if globalp u then 'global
  133. else if fluidp u then 'fluid
  134. else if flagp(u,'parm) then 'parameter
  135. else get(u,'rtype);
  136. endmodule;
  137. end;
  138.