order.red 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. module order; % Functions for internal ordering of expressions.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1999 Anthony C. Hearn. All rights reserved.
  4. fluid '(kord!*);
  5. % symbolic procedure ordad(a,u);
  6. % if null u then list a
  7. % else if ordp(a,car u) then a . u
  8. % else car u . ordad(a,cdr u);
  9. % This definition, due to A.C. Norman, avoids recursion.
  10. symbolic procedure ordad(a,u);
  11. begin scalar r;
  12. while u and not ordp(a,car u) do <<r := car u . r; u := cdr u>>;
  13. u := a . u;
  14. while r do <<a := cdr r; rplacd(r,u); u := r; r := a>>;
  15. return u
  16. end;
  17. symbolic procedure ordn u;
  18. if null u then nil
  19. else if null cdr u then u
  20. else if null cddr u then ord2(car u,cadr u)
  21. else ordad(car u,ordn cdr u);
  22. symbolic procedure ord2(u,v);
  23. if ordp(u,v) then list(u,v) else list(v,u);
  24. symbolic procedure ordp(u,v);
  25. % Returns TRUE if U ordered ahead or equal to V, NIL otherwise.
  26. % An expression with more structure at a given level is ordered
  27. % ahead of one with less.
  28. if null u then null v
  29. else if null v then t
  30. else if vectorp u then if vectorp v then ordpv(u,v) else atom v
  31. else if atom u
  32. then if atom v
  33. then if numberp u then numberp v and not(u<v)
  34. else if idp v then orderp(u,v)
  35. else numberp v
  36. % else flagp(car v,'noncom)
  37. else nil
  38. % else if atom v then not flagp(car u,'noncom)
  39. else if atom v then t
  40. % I used to think the additional noncom check was needed here, but
  41. % it can lead to confusing results.
  42. % else if car u=car v then ordp(cdr u,cdr v)
  43. % else if car u=car v then flagp(car u,'noncom) or ordpl(cdr u,cdr v)
  44. else if car u=car v then ordpl(cdr u,cdr v)
  45. else if flagp(car u,'noncom)
  46. then if flagp(car v,'noncom) then ordp(car u, car v) else t
  47. else if flagp(car v,'noncom) then nil
  48. else ordp(car u,car v);
  49. symbolic procedure ordpl(u,v);
  50. % Returns TRUE if list U ordered ahead or equal to V, NIL otherwise.
  51. % We also allow for a dotted pair.
  52. if atom u then ordp(u,v)
  53. else if atom v then t
  54. else if car u=car v then ordpl(cdr u,cdr v)
  55. else ordp(car u,car v);
  56. symbolic procedure ordpv(u,v);
  57. % U and v are vectors. Set up comparison loop.
  58. ordpv1(u,v,-1,upbv u,upbv v);
  59. symbolic procedure ordpv1(u,v,i,lu,lv);
  60. if (i:=i#+1)>lu then i>lv
  61. else (if x=y then ordpv1(u,v,i,lu,lv) else ordp(x,y))
  62. where x=getv(u,i),y=getv(v,i);
  63. symbolic procedure ordop(u,v);
  64. begin scalar x;
  65. x := kord!*;
  66. a: if null x then return ordp(u,v)
  67. else if u eq car x then return t
  68. else if v eq car x then return;
  69. x := cdr x;
  70. go to a
  71. end;
  72. symbolic procedure ordpp(u,v);
  73. % This version is used for addition, where NONCOM properties aren't
  74. % relevant.
  75. begin scalar x;
  76. if car u eq car v then return cdr u>cdr v;
  77. x := kord!*;
  78. u := car u;
  79. v := car v;
  80. a: if null x then return ordpa(u,v)
  81. else if u eq car x then return t
  82. else if v eq car x then return nil;
  83. x := cdr x;
  84. go to a
  85. end;
  86. symbolic procedure ordpa(u,v);
  87. % Returns TRUE if U ordered ahead or equal to V, NIL otherwise.
  88. % An expression with more structure at a given level is ordered
  89. % ahead of one with less.
  90. if null u then null v
  91. else if null v then t
  92. else if vectorp u then if vectorp v then ordpv(u,v) else atom v
  93. else if atom u
  94. then if atom v
  95. then if numberp u then numberp v and not(u<v)
  96. else if idp v then orderp(u,v)
  97. else numberp v
  98. else nil
  99. else if atom v then t
  100. else if car u=car v then ordpa(cdr u,cdr v)
  101. else ordpa(car u,car v);
  102. endmodule;
  103. end;