Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BABY STOOPS TO THE CLINTONS
- //Yieldprolog tutorial 1 in c++14
- #include <iostream>
- #include <string>
- #include <functional>
- using namespace std;
- typedef string& var;
- typedef function<bool (var, var)> comp;
- //Coroutine generator for a fact
- comp fact(string s, string o) {
- int entry=0;
- return [s,o, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: Ds=s; Do=o; entry=1; return true;
- default: return false;
- }
- };
- }
- //Or optimized specializations for 2,3,N facts...
- comp fact2(string s, string o, string s2, string o2) {
- int entry=0;
- return [s,o, s2, o2, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: Ds=s; Do=o; entry=1; return true;
- case 1: Ds=s2; Do=o2; entry=2; return true;
- default: return false;
- }
- };
- }
- comp fact3(string s, string o, string s2, string o2, string s3, string o3) {
- int entry=0;
- return [s,o, s2, o2, s3, o3, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: Ds=s; Do=o; entry=1; return true;
- case 1: Ds=s2; Do=o2; entry=2; return true;
- case 2: Ds=s3; Do=o3; entry=3; return true;
- default: return false;
- }
- };
- }
- comp fact4(string s, string o, string s2, string o2, string s3, string o3, string s4, string o4) {
- int entry=0;
- return [s,o, s2, o2, s3, o3, s4, o4, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: Ds=s; Do=o; entry=1; return true;
- case 1: Ds=s2; Do=o2; entry=2; return true;
- case 2: Ds=s3; Do=o3; entry=3; return true;
- case 3: Ds=s4; Do=o4; entry=4; return true;
- default: return false;
- }
- };
- }
- comp fact5(string s, string o, string s2, string o2, string s3, string o3, string s4, string o4, string s5, string o5) {
- int entry=0;
- return [s,o, s2, o2, s3, o3, s4, o4, s5, o5, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: Ds=s; Do=o; entry=1; return true;
- case 1: Ds=s2; Do=o2; entry=2; return true;
- case 2: Ds=s3; Do=o3; entry=3; return true;
- case 3: Ds=s4; Do=o4; entry=4; return true;
- case 4: Ds=s5; Do=o5; entry=5; return true;
- default: return false;
- }
- };
- }
- 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) {
- int entry=0;
- return [s,o, s2, o2, s3, o3, s4, o4, s5, o5, s6, o6, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: Ds=s; Do=o; entry=1; return true;
- case 1: Ds=s2; Do=o2; entry=2; return true;
- case 2: Ds=s3; Do=o3; entry=3; return true;
- case 3: Ds=s4; Do=o4; entry=4; return true;
- case 4: Ds=s5; Do=o5; entry=5; return true;
- case 5: Ds=s6; Do=o6; entry=6; return true;
- default: return false;
- }
- };
- }
- //Sequencing combinator
- comp seq(comp a, comp b) {
- int entry=0;
- return [a,b,entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0:
- while(a(Ds, Do)) {
- entry=1; return true;
- case 1: ;
- }
- while(b(Ds, Do)) {
- entry=2; return true;
- case 2: ;
- }
- entry=3;
- default: return false;
- }
- };
- }
- //join combinators
- comp joinavvb(comp a, comp b, var v) {
- int entry=0;
- return [a, b, v, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: while(a(Ds, v)) {
- while(b(v, Do)) {
- entry=1; return true;
- case 1: ;
- }
- entry=2;
- default:
- return false;
- }
- }
- };
- }
- comp joinavvb(comp a, comp b) {
- string v;
- return joinavvb(a, b, v);
- }
- comp joinvabv(comp a, comp b, var v) {
- int entry=0;
- return [a, b, v, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: while(a(v, Do)) {
- while(b(Ds, v)) {
- entry=1; return true;
- case 1: ;
- }
- entry=2;
- default:
- return false;
- }
- }
- };
- }
- comp joinvabv(comp a, comp b) {
- string v;
- return joinvabv(a, b, v);
- }
- //TODO: Others...
- comp joinvavb(comp a, comp b, var v){
- int entry=0;
- return [a, b, v, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: while(a(v, Do)) {
- while(b(v, Ds)) {
- entry=1; return true;
- case 1: ;
- }
- entry=2;
- default:
- return false;
- }
- }
- comp joinvavb(comp a, comp b){
- string v;
- return joinvavb(a,b,v);
- }
- comp joinavbv(comp a, comp b, var v){
- int entry=0;
- return [a, b, v, entry](var Ds, var Do) mutable {
- switch(entry) {
- case 0: while(a(Do, v)) {
- while(b(Ds,v)) {
- entry=1; return true;
- case 1: ;
- }
- entry=2;
- default:
- return false;
- }
- }
- comp joinavbv(comp a, comp b){
- string v;
- return joinavbv(a,b,v);
- }
- //an example clintons
- int main() {
- string _Person = "Person";
- string _Bill = "Bill";
- string _Hillary = "Hillary";
- string _Tony = "Tony";
- string _Chelsea = "Chelsea";
- string _Roger = "Roger";
- string _Hugh = "Hugh";
- comp a = fact6(
- _Bill, _Person, _Hillary, _Person,
- _Tony, _Person, _Chelsea, _Person,
- _Roger, _Person, _Hugh, _Person);
- comp bro = fact3(_Hillary, _Tony, _Hillary, _Hugh, _Bill, _Roger); //or a seq of fact2 and fact1 or something
- comp parent = fact2(_Chelsea, _Hillary, _Chelsea, _Bill);
- comp uncle = joinavvb(parent, bro);
- comp unclesubjppl = joinvabv(a, joinavvb(parent, bro));
- string l, r;
- while(uncle(l,r)) {
- cout << l << " uncle " << r << endl;
- }
- cout << "=======" << endl;
- while(unclesubjppl(l,r)) {
- cout << l << " unclesubjppl " << r << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement