chriswarbo-net: 2d205a4611aa4a22d96c6b7cd3b29038c4251bdf
1: // Combinatory logic with the S and K combinators
2: // Combinators are either 'S', 'K' or an array of combinators
3:
4: window.step_combinator = function(comb, allowed) {
5: // "allowed" is a nullary function which returns a boolean, and
6: // is called before each rewrite step. If it returns false, the
7: // rewrite is aborted at its current state and this is returned.
8: if (typeof allowed != typeof function(a){return a;}) {
9: // If we've been given no condition, allow all rewrites
10: allowed = function() { return true; };
11: }
12: // These are the base cases of our recursion
13: if (comb === 'S') { return comb; }
14: if (comb === 'K') { return comb; }
15:
16: // These are our rewrite rules
17: // K x y -> x
18: var pos = jQuery.inArray('K', comb);
19: var args;
20: if (pos >= 0 && pos < comb.length - 3 && allowed()) {
21: args = comb.splice(pos, 3); // Pop off the elements
22: comb.splice(pos, 0, args[1]); // Put the first argument to K back
23: }
24: // S x y z -> (x z) (y z)
25: pos = jQuery.inArray('S', comb);
26: if (pos >= 0 && pos < comb.length - 4 && allowed()) {
27: args = comb.splice(pos, 4); // Pop off the elements
28: comb.splice(pos, 0, [args[1], args[3]], [args[2], args[3]]); // Put the modified tree back
29: }
30: return _.map(comb, function(c) {return window.step_combinator(c, allowed); });
31: };
Generated by git2html.