Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. //BABY STOOPS TO THE CLINTONS
  2. //Yieldprolog tutorial 1 in c++14
  3.  
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <functional>
  8. using namespace std;
  9.  
  10. typedef string& var;
  11.  
  12. typedef function<bool (var, var)> comp;
  13.  
  14. //Coroutine generator for a fact
  15. comp fact(string s, string o) {
  16. int entry=0;
  17. return [s,o, entry](var Ds, var Do) mutable {
  18. switch(entry) {
  19. case 0: Ds=s; Do=o; entry=1; return true;
  20. default: return false;
  21. }
  22. };
  23. }
  24.  
  25. //Or optimized specializations for 2,3,N facts...
  26.  
  27. comp fact2(string s, string o, string s2, string o2) {
  28. int entry=0;
  29. return [s,o, s2, o2, entry](var Ds, var Do) mutable {
  30. switch(entry) {
  31. case 0: Ds=s; Do=o; entry=1; return true;
  32. case 1: Ds=s2; Do=o2; entry=2; return true;
  33. default: return false;
  34. }
  35. };
  36. }
  37.  
  38. comp fact3(string s, string o, string s2, string o2, string s3, string o3) {
  39. int entry=0;
  40. return [s,o, s2, o2, s3, o3, entry](var Ds, var Do) mutable {
  41. switch(entry) {
  42. case 0: Ds=s; Do=o; entry=1; return true;
  43. case 1: Ds=s2; Do=o2; entry=2; return true;
  44. case 2: Ds=s3; Do=o3; entry=3; return true;
  45. default: return false;
  46. }
  47. };
  48. }
  49.  
  50. comp fact4(string s, string o, string s2, string o2, string s3, string o3, string s4, string o4) {
  51. int entry=0;
  52. return [s,o, s2, o2, s3, o3, s4, o4, entry](var Ds, var Do) mutable {
  53. switch(entry) {
  54. case 0: Ds=s; Do=o; entry=1; return true;
  55. case 1: Ds=s2; Do=o2; entry=2; return true;
  56. case 2: Ds=s3; Do=o3; entry=3; return true;
  57. case 3: Ds=s4; Do=o4; entry=4; return true;
  58. default: return false;
  59. }
  60. };
  61. }
  62.  
  63. comp fact5(string s, string o, string s2, string o2, string s3, string o3, string s4, string o4, string s5, string o5) {
  64. int entry=0;
  65. return [s,o, s2, o2, s3, o3, s4, o4, s5, o5, entry](var Ds, var Do) mutable {
  66. switch(entry) {
  67. case 0: Ds=s; Do=o; entry=1; return true;
  68. case 1: Ds=s2; Do=o2; entry=2; return true;
  69. case 2: Ds=s3; Do=o3; entry=3; return true;
  70. case 3: Ds=s4; Do=o4; entry=4; return true;
  71. case 4: Ds=s5; Do=o5; entry=5; return true;
  72. default: return false;
  73. }
  74. };
  75. }
  76.  
  77. comp fact6(string s, string o, string s2, string o2, string s3, string o3, string s4, string o4, string s5, string o5, string s6, string o6) {
  78. int entry=0;
  79. return [s,o, s2, o2, s3, o3, s4, o4, s5, o5, s6, o6, entry](var Ds, var Do) mutable {
  80. switch(entry) {
  81. case 0: Ds=s; Do=o; entry=1; return true;
  82. case 1: Ds=s2; Do=o2; entry=2; return true;
  83. case 2: Ds=s3; Do=o3; entry=3; return true;
  84. case 3: Ds=s4; Do=o4; entry=4; return true;
  85. case 4: Ds=s5; Do=o5; entry=5; return true;
  86. case 5: Ds=s6; Do=o6; entry=6; return true;
  87. default: return false;
  88. }
  89. };
  90. }
  91.  
  92. //Sequencing combinator
  93.  
  94. comp seq(comp a, comp b) {
  95. int entry=0;
  96. return [a,b,entry](var Ds, var Do) mutable {
  97. switch(entry) {
  98. case 0:
  99. while(a(Ds, Do)) {
  100. entry=1; return true;
  101. case 1: ;
  102. }
  103. while(b(Ds, Do)) {
  104. entry=2; return true;
  105. case 2: ;
  106. }
  107. entry=3;
  108. default: return false;
  109. }
  110. };
  111. }
  112.  
  113. //join combinators
  114.  
  115. comp joinavvb(comp a, comp b, var v) {
  116. int entry=0;
  117. return [a, b, v, entry](var Ds, var Do) mutable {
  118. switch(entry) {
  119. case 0: while(a(Ds, v)) {
  120. while(b(v, Do)) {
  121. entry=1; return true;
  122. case 1: ;
  123. }
  124. entry=2;
  125. default:
  126. return false;
  127. }
  128. }
  129. };
  130. }
  131.  
  132.  
  133. comp joinavvb(comp a, comp b) {
  134. string v;
  135. return joinavvb(a, b, v);
  136. }
  137.  
  138. comp joinvabv(comp a, comp b, var v) {
  139. int entry=0;
  140. return [a, b, v, entry](var Ds, var Do) mutable {
  141. switch(entry) {
  142. case 0: while(a(v, Do)) {
  143. while(b(Ds, v)) {
  144. entry=1; return true;
  145. case 1: ;
  146. }
  147. entry=2;
  148. default:
  149. return false;
  150. }
  151. }
  152. };
  153. }
  154.  
  155.  
  156. comp joinvabv(comp a, comp b) {
  157. string v;
  158. return joinvabv(a, b, v);
  159. }
  160.  
  161. //TODO: Others...
  162.  
  163. comp joinvavb(comp a, comp b, var v){
  164. int entry=0;
  165. return [a, b, v, entry](var Ds, var Do) mutable {
  166. switch(entry) {
  167. case 0: while(a(v, Do)) {
  168. while(b(v, Ds)) {
  169. entry=1; return true;
  170. case 1: ;
  171. }
  172. entry=2;
  173. default:
  174. return false;
  175. }
  176. }
  177.  
  178. comp joinvavb(comp a, comp b){
  179. string v;
  180. return joinvavb(a,b,v);
  181. }
  182.  
  183. comp joinavbv(comp a, comp b, var v){
  184. int entry=0;
  185. return [a, b, v, entry](var Ds, var Do) mutable {
  186. switch(entry) {
  187. case 0: while(a(Do, v)) {
  188. while(b(Ds,v)) {
  189. entry=1; return true;
  190. case 1: ;
  191. }
  192. entry=2;
  193. default:
  194. return false;
  195. }
  196. }
  197.  
  198. comp joinavbv(comp a, comp b){
  199. string v;
  200. return joinavbv(a,b,v);
  201. }
  202.  
  203. //an example clintons
  204. int main() {
  205. string _Person = "Person";
  206. string _Bill = "Bill";
  207. string _Hillary = "Hillary";
  208. string _Tony = "Tony";
  209. string _Chelsea = "Chelsea";
  210. string _Roger = "Roger";
  211. string _Hugh = "Hugh";
  212.  
  213. comp a = fact6(
  214. _Bill, _Person, _Hillary, _Person,
  215. _Tony, _Person, _Chelsea, _Person,
  216. _Roger, _Person, _Hugh, _Person);
  217. comp bro = fact3(_Hillary, _Tony, _Hillary, _Hugh, _Bill, _Roger); //or a seq of fact2 and fact1 or something
  218. comp parent = fact2(_Chelsea, _Hillary, _Chelsea, _Bill);
  219. comp uncle = joinavvb(parent, bro);
  220.  
  221. comp unclesubjppl = joinvabv(a, joinavvb(parent, bro));
  222.  
  223. string l, r;
  224. while(uncle(l,r)) {
  225. cout << l << " uncle " << r << endl;
  226. }
  227. cout << "=======" << endl;
  228. while(unclesubjppl(l,r)) {
  229. cout << l << " unclesubjppl " << r << endl;
  230. }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement