hall.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #include "assert.h"
  6. #include "param.h"
  7. #include "set.h"
  8. #include "hall.h"
  9. #include "error.h"
  10. #include <stdio.h>
  11. /*
  12. * This file implements the marriage thesis from Hall.
  13. * The thesis says that given a number, say N, of subsets from
  14. * a finite set, it is possible to create a set with cardinality N,
  15. * that contains one member for each of the subsets,
  16. * iff for each number, say M, of subsets from 2 to N the union of
  17. * each M-tuple sets has cardinality >= M.
  18. *
  19. * So what, you might say. As indeed I did.
  20. * But this is actually used here to check the possibility of each
  21. * code rule. If a code rule has a number of token_sets in the with
  22. * clause and a number of properties in the uses rule it must be
  23. * possible to do this from an empty fakestack. Hall helps.
  24. */
  25. #define MAXHALL (TOKPATMAX+MAXALLREG)
  26. short hallsets[MAXHALL][SETSIZE];
  27. int nhallsets= -1;
  28. int hallfreq[MAXHALL][2];
  29. void hallverbose()
  30. {
  31. int i;
  32. int max;
  33. fprintf(stderr,"Table of hall frequencies\n # pre post\n");
  34. for (max=MAXHALL-1;hallfreq[max][0]==0 && hallfreq[max][1]==0;max--)
  35. ;
  36. for (i=0;i<=max;i++)
  37. fprintf(stderr,"%3d%6d%6d\n",i,hallfreq[i][0],hallfreq[i][1]);
  38. }
  39. void inithall()
  40. {
  41. assert(nhallsets == -1);
  42. nhallsets=0;
  43. }
  44. void nexthall(short *sp)
  45. {
  46. int i;
  47. assert(nhallsets>=0);
  48. for(i=0;i<SETSIZE;i++)
  49. hallsets[nhallsets][i] = sp[i];
  50. nhallsets++;
  51. }
  52. int card(short *sp)
  53. {
  54. int sum,i;
  55. sum=0;
  56. for(i=0;i<8*sizeof(short)*SETSIZE;i++)
  57. if (BIT(sp,i))
  58. sum++;
  59. return(sum);
  60. }
  61. void checkhall()
  62. {
  63. assert(nhallsets>=0);
  64. if (!hall())
  65. error("Hall says: \"You can't have those registers\"");
  66. }
  67. int hall()
  68. {
  69. int i,j,k;
  70. int ok;
  71. hallfreq[nhallsets][0]++;
  72. /*
  73. * If a set has cardinality >= nhallsets it can never be the cause
  74. * of the hall algorithm failing. So it can be thrown away.
  75. * But then nhallsets is less, so this step can be re-applied.
  76. */
  77. do {
  78. ok = 0;
  79. for(i=0;i<nhallsets;i++)
  80. if (card(hallsets[i])>=nhallsets) {
  81. for (j=i+1;j<nhallsets;j++)
  82. for(k=0;k<SETSIZE;k++)
  83. hallsets[j-1][k] =
  84. hallsets[j][k];
  85. nhallsets--;
  86. ok = 1;
  87. break;
  88. }
  89. } while (ok);
  90. /*
  91. * Now all sets have cardinality < nhallsets
  92. */
  93. hallfreq[nhallsets][1]++;
  94. ok=recurhall(nhallsets,hallsets);
  95. nhallsets = -1;
  96. return(ok);
  97. }
  98. int recurhall(int nhallsets, short hallsets[][SETSIZE])
  99. {
  100. short copysets[MAXHALL][SETSIZE];
  101. short setsum[SETSIZE];
  102. int i,j,k,ncopys;
  103. /*
  104. * First check cardinality of union of all
  105. */
  106. for(k=0;k<SETSIZE;k++)
  107. setsum[k]=0;
  108. for(i=0;i<nhallsets;i++)
  109. unite(hallsets[i],setsum);
  110. if (card(setsum)<nhallsets)
  111. return(0);
  112. /*
  113. * Now check the hall property of everything but one set,
  114. * for all sets
  115. */
  116. for(i=0;i<nhallsets;i++) {
  117. ncopys=0;
  118. for(j=0;j<nhallsets;j++) if (j!=i) {
  119. for(k=0;k<SETSIZE;k++)
  120. copysets[ncopys][k] = hallsets[j][k];
  121. ncopys++;
  122. }
  123. assert(ncopys == nhallsets-1);
  124. if (!recurhall(ncopys,copysets))
  125. return(0);
  126. }
  127. return(1);
  128. }
  129. void unite(short *sp, short *into)
  130. {
  131. int i;
  132. for(i=0;i<SETSIZE;i++)
  133. into[i] |= sp[i];
  134. }
  135. /*
  136. * Limerick (rot13)
  137. *
  138. * N zngurzngvpvna anzrq Unyy
  139. * Unf n urknurqebavpny onyy,
  140. * Naq gur phor bs vgf jrvtug
  141. * Gvzrf uvf crpxre'f, cyhf rvtug
  142. * Vf uvf cubar ahzore -- tvir uvz n pnyy..
  143. */