Search in sources :

Example 11 with Generalizer

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);
}
Also used : Suite(suite.Suite) Singleton(suite.node.util.Singleton) Inspect(suite.inspect.Inspect) List_(suite.util.List_) To(suite.util.To) Chars(suite.primitive.Chars) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CharsBuilder(suite.primitive.Chars.CharsBuilder) Iterate(suite.util.FunUtil.Iterate) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) Rewrite(suite.node.util.Rewrite) Atom(suite.node.Atom) Nodify(suite.util.Nodify) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Str(suite.node.Str) Str(suite.node.Str) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Node(suite.node.Node)

Example 12 with Generalizer

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);
}
Also used : IMap(suite.immutable.IMap) Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Tree(suite.node.Tree) FunpTree(suite.funp.P0.FunpTree)

Example 13 with Generalizer

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);
}
Also used : Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList)

Example 14 with Generalizer

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;
}
Also used : Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList)

Aggregations

Generalizer (suite.lp.doer.Generalizer)14 Node (suite.node.Node)11 Reference (suite.node.Reference)7 Trail (suite.lp.Trail)6 ArrayList (java.util.ArrayList)5 Atom (suite.node.Atom)5 Tree (suite.node.Tree)4 List (java.util.List)3 Suite (suite.Suite)3 Pair (suite.adt.pair.Pair)3 RuleSet (suite.lp.kb.RuleSet)3 Fun (suite.util.FunUtil.Fun)3 Test (org.junit.Test)2 ProverConfig (suite.lp.Configuration.ProverConfig)2 Binder (suite.lp.doer.Binder)2 ProverFactory (suite.lp.doer.ProverFactory)2 Prototype (suite.lp.kb.Prototype)2 SewingProverImpl (suite.lp.sewing.impl.SewingProverImpl)2 Dict (suite.node.Dict)2 TermOp (suite.node.io.TermOp)2