use of suite.lp.doer.Generalizer in project suite by stupidsing.
the class FactorizeResult method rewrite.
public static FactorizeResult rewrite(FactorizeResult frfrom, FactorizeResult frto, FactorizeResult fr0) {
Generalizer generalizer = new Generalizer();
Iterate<Node> rewrite = n0 -> {
Node[] m = Suite.pattern(FTerminal.class.getName() + ":.0").match(n0);
Node n1 = m != null ? m[0] : null;
Node n2 = n1 instanceof Dict ? ((Dict) n1).map.get(Atom.of("chars")) : null;
Node n3 = n2 != null ? n2.finalNode() : null;
String s = n3 instanceof Str ? ((Str) n3).value : null;
boolean b = s != null && s.startsWith(ProverConstant.variablePrefix) && s.substring(1).matches("[0-9]*");
return b ? generalizer.generalize(Atom.of(s)) : n0;
};
Fun<FactorizeResult, Node> parse = fr -> rw.rewrite(rewrite, nodify.nodify(FNode.class, fr.node));
Node nodeFrom = parse.apply(frfrom);
Node nodeTo = parse.apply(frto);
FNode fn0 = fr0.node;
Node node0 = nodify.nodify(FNode.class, fn0);
Node nodex = rw.rewrite(nodeFrom, nodeTo, node0);
FNode fnx = nodify.unnodify(FNode.class, nodex);
return new FactorizeResult(fr0.pre, fnx, fr0.post);
}
use of suite.lp.doer.Generalizer in project suite by stupidsing.
the class P0Parse method expandMacros.
private Node expandMacros(Node node0) {
class Expand {
private IMap<Prototype, Node[]> macros;
private Expand(IMap<Prototype, Node[]> macros) {
this.macros = macros;
}
private Node expand(Node node) {
Tree tree;
Node[] m;
Node[] ht;
if ((m = Suite.pattern("expand .0 := .1 >> .2").match(node)) != null) {
Node head = m[0];
return new Expand(macros.put(Prototype.of(head), new Node[] { head, m[1] })).expand(m[2]);
} else if ((ht = macros.get(Prototype.of(node))) != null) {
Generalizer g = new Generalizer();
Node t0_ = g.generalize(ht[0]);
Node t1_ = g.generalize(ht[1]);
if (Binder.bind(node, t0_, new Trail()))
return expand(t1_);
}
if ((tree = Tree.decompose(node)) != null)
return Tree.of(tree.getOperator(), expand(tree.getLeft()), expand(tree.getRight()));
else
return node;
}
}
return new Expand(IMap.empty()).expand(node0);
}
use of suite.lp.doer.Generalizer in project suite by stupidsing.
the class Constants method bindSecrets.
public static Fixie<String, String, String, String, String, String, String, String, String, String> bindSecrets(String pattern) {
Generalizer generalizer = new Generalizer();
String[] m;
if (secrets().prove(generalizer.generalize(Suite.parse(pattern)))) {
List<String> list = new ArrayList<>();
int i = 0;
Node n;
while (!((n = generalizer.getVariable(Atom.of("." + i++))).finalNode() instanceof Reference)) list.add(Formatter.display(n));
m = list.toArray(new String[0]);
} else
m = null;
if (m != null) {
int length = m.length;
return //
Fixie.of(//
0 < length ? m[0] : null, //
1 < length ? m[1] : null, //
2 < length ? m[2] : null, //
3 < length ? m[3] : null, //
4 < length ? m[4] : null, //
5 < length ? m[5] : null, //
6 < length ? m[6] : null, //
7 < length ? m[7] : null, //
8 < length ? m[8] : null, 9 < length ? m[9] : null);
} else
return Fail.t("Cannot match " + pattern);
}
use of suite.lp.doer.Generalizer in project suite by stupidsing.
the class Constants method secrets.
public static String[] secrets(String pattern) {
Generalizer generalizer = new Generalizer();
if (secrets().prove(generalizer.generalize(Suite.parse(pattern)))) {
List<String> list = new ArrayList<>();
int i = 0;
Node n;
while (!((n = generalizer.getVariable(Atom.of("." + i++))).finalNode() instanceof Reference)) list.add(Formatter.display(n));
return list.toArray(new String[0]);
} else
return null;
}
Aggregations