Advertisement
Yevano

Untitled

Mar 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. static AST id = L("x", "x");
  2.  
  3. static AST y = L("f", A(L("x", A("f", A("x", "x"))), L("x", A("f", A("x", "x")))));
  4.  
  5. static AST tru = L("x", "y", "x");
  6. static AST fal = L("x", "y", "y");
  7.  
  8. static AST zero = fal;
  9. static AST iszero = L("n", A("n", L("x", fal), tru));
  10. static AST succ = L("n", "f", "x", A("f", A("n", "f", "x")));
  11. static AST pred = L("n", "f", "x", A("n", L("g", "h", A("h", A("g", "f"))), L("u", "x"), id));
  12. static AST add = L("m", "n", A("m", succ, "n"));
  13. static AST sub = L("m", "n", A("n", pred, "m"));
  14. static AST mul = L("m", "n", A("m", A(add, "n"), zero));
  15.  
  16. static AST not = L("b", A("b", fal, tru));
  17. static AST or = L("a", "b", A("a", "b", fal));
  18. static AST and = L("a", "b", A("a", tru, "b"));
  19.  
  20. static AST leq = L("m", "n", A(iszero, A(sub, "m", "n")));
  21. static AST lt = L("m", "n", A(not, A(leq, "n", "m")));
  22. static AST eq = L("m", "n", A(A(leq, "m", "n"), A(leq, "n", "m"), fal));
  23.  
  24. static AST pair = L("x", "y", "z", A("z", "x", "y"));
  25. static AST fst = L("p", A("p", tru));
  26. static AST snd = L("p", A("p", fal));
  27. static AST nil = L("x", tru);
  28. static AST isnil = L("p", A("p", L("x", "y", fal)));
  29.  
  30. static AST div = A(y, L("div", "q", "m", "n",
  31.     A(A(lt, "m", "n"),
  32.         A(pair, "q", "m"),
  33.         A("div", A(succ, "q"), A(sub, "m", "n"), "n"))),
  34.     0);
  35.  
  36. static AST idiv = L("m", "n", A(fst, A(div, "m", "n")));
  37. static AST mod = L("m", "n", A(snd, A(div, "m", "n")));
  38.  
  39. static AST ilog = A(y, L("ilog", "b", "n",
  40.     A(A(lt, "n", "b"),
  41.         0,
  42.         A(succ, A("ilog", "b", A(idiv, "n", "b"))))));
  43.  
  44. static AST fact = A(y, L("fact", "n",
  45.     A(A(eq, "n", 1),
  46.         1,
  47.         A(mul, "n", A("fact", A(pred, "n"))))));
  48.  
  49. static AST len = A(y, L("len", "l",
  50.     A(A(isnil, "l"),
  51.         zero,
  52.         A(succ, A("len", A(snd, "l"))))));
  53.  
  54. static AST nth = A(y, L("nth", "l", "i",
  55.     A(A(iszero, "i"),
  56.         A(fst, "l"),
  57.         A("nth", A(snd, "l"), A(pred, "i")))));
  58.  
  59. static AST tail = A(y, L("tail", "l",
  60.     A(A(isnil, A(snd, "l")),
  61.         A(fst, "l"),
  62.         A("tail", A(snd, "l")))));
  63.  
  64. static AST append = A(y, L("append", "l", "e",
  65.     A(A(isnil, "l"),
  66.         A(pair, "e", nil),
  67.         A(pair, A(fst, "l"), A("append", A(snd, "l"), "e")))));
  68.  
  69. static AST concat = A(y, L("concat", "l", "m",
  70.     A(A(isnil, "l"),
  71.         "m",
  72.         A(pair, A(fst, "l"), A("concat", A(snd, "l"), "m")))));
  73.  
  74. static AST reverse = A(y, L("reverse", "l",
  75.     A(A(isnil, "l"),
  76.         "l",
  77.         A(append, A("reverse", A(snd, "l")), A(fst, "l")))));
  78.  
  79. static AST map = A(y, L("map", "l", "f",
  80.     A(A(isnil, "l"),
  81.         nil,
  82.         A(pair,
  83.             A("f", A(fst, "l")),
  84.             A("map", A(snd, "l"), "f")))));
  85.  
  86. static AST yp = L("l", A(y, L("self", A(map, "self", "l"))));
  87.  
  88. static AST stoi = L("l",
  89.     A(y,
  90.         L("stoi", "l",
  91.             A(A(isnil, A(snd, "l")),
  92.                 A(sub, A(fst, "l"), '0'),
  93.                 A(add,
  94.                     A(sub,
  95.                         A(fst, "l"),
  96.                         '0'),
  97.                     A(mul,
  98.                         A("stoi", A(snd, "l")),
  99.                         10)))),
  100.         A(reverse, "l")));
  101.  
  102. static AST itos = L("n",
  103.     A(reverse, A(y, L("itos", "n", A(A(iszero, A(idiv, "n", 10)),
  104.         A(pair, A(add, "n", '0'), nil),
  105.         A(pair, A(add, A(mod, "n", 10), '0'), A("itos", A(idiv, "n", 10))))), "n")));
  106.  
  107. static AST cat = A(y, L("m", "i", "o",
  108.     A("i", L("j", "c",
  109.         A(A(eq, "c", ast_church_natural('\n')),
  110.             "o",
  111.             A("m", "j", A("o", "c")))))));
  112.  
  113. static AST read = L("i", "n", "f",
  114.     A(L("p", A("f", A(fst, "p"), A(snd, "p"))), A(y, L("read_1", "i", "n",
  115.         A(A(iszero, "n"),
  116.             A(pair, "i", nil),
  117.             A("i", L("j", "c",
  118.                 A(L("res", A(pair,
  119.                     A(fst, "res"),
  120.                     A(pair,
  121.                         "c",
  122.                         A(snd, "res")))), A("read_1", "j", A(pred, "n"))))))),
  123.         "i", "n")));
  124.  
  125. static AST read_line = L("i", "f",
  126.     let("p",
  127.         A(y, L("read_line", "i",
  128.             A("i", L("j", "c",
  129.                 A(A(eq, "c", '\n'),
  130.                     A(pair, "j", nil),
  131.                     let("res",
  132.                         A("read_line", "j"),
  133.                         A(pair,
  134.                             A(fst, "res"),
  135.                             A(pair,
  136.                                 "c",
  137.                                 A(snd, "res")))))))),
  138.             "i"),
  139.         A("f", A(fst, "p"), A(snd, "p"))));
  140.  
  141. static AST write = A(y, L("write", "o", "l",
  142.     A(A(isnil, "l"),
  143.         "o",
  144.         A("write", A("o", A(fst, "l")), A(snd, "l")))));
  145.  
  146. var term = L("i", "o",
  147.     A(read_line, "i", L("j", "s",
  148.         A(read_line, "j", L("k", "t",
  149.             A(write, "o",
  150.                 A(itos, A(add,
  151.                     A(stoi, "s"),
  152.                     A(stoi, "t")))))))));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement