Search in sources :

Example 1 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class InterpretFunLazy method lazy.

public Thunk_ lazy(Node node) {
    Node parsed = parse(node);
    Map<String, Thunk_> df = new HashMap<>();
    df.put(TermOp.AND___.name, binary((a, b) -> new Pair_(a, b)));
    df.put("fst", () -> new Fun_(in -> ((Pair_) in.get()).first_));
    df.put("if", () -> new Fun_(a -> () -> new Fun_(b -> () -> new Fun_(c -> a.get() == Atom.TRUE ? b : c))));
    df.put("snd", () -> new Fun_(in -> ((Pair_) in.get()).second));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        df.put(e.getKey().getName(), binary((a, b) -> b(fun.apply(compare(a.get(), b.get()), 0))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        df.put(e.getKey().getName(), binary((a, b) -> Int.of(fun.apply(i(a), i(b)))));
    }
    List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
    Lazy_ lazy0 = new Lazy_(0, IMap.empty());
    Frame frame = new Frame(null);
    for (String key : keys) {
        lazy0 = lazy0.put(Atom.of(key));
        frame.add(df.get(key));
    }
    return lazy0.lazy_(parsed).apply(frame);
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) APPLY(suite.fp.match.Matchers.APPLY) Prover(suite.lp.doer.Prover) DECONS(suite.fp.match.Matchers.DECONS) Mutable(suite.adt.Mutable) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) BOOLEAN(suite.fp.match.Matchers.BOOLEAN) VAR(suite.fp.match.Matchers.VAR) Map(java.util.Map) NUMBER(suite.fp.match.Matchers.NUMBER) ERROR(suite.fp.match.Matchers.ERROR) TREE(suite.fp.match.Matchers.TREE) IMap(suite.immutable.IMap) FUN(suite.fp.match.Matchers.FUN) To(suite.util.To) Collectors(java.util.stream.Collectors) Matcher(suite.fp.match.Matcher) PRAGMA(suite.fp.match.Matchers.PRAGMA) Tree(suite.node.Tree) UNWRAP(suite.fp.match.Matchers.UNWRAP) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) List(java.util.List) Entry(java.util.Map.Entry) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) IF(suite.fp.match.Matchers.IF) Comparer(suite.node.util.Comparer) HashMap(java.util.HashMap) TCO(suite.fp.match.Matchers.TCO) ArrayList(java.util.ArrayList) CONS(suite.fp.match.Matchers.CONS) Data(suite.node.Data) Formatter(suite.node.io.Formatter) DEFVARS(suite.fp.match.Matchers.DEFVARS) Reference(suite.node.Reference) Suite(suite.Suite) Pattern(suite.BindArrayUtil.Pattern) TermOp(suite.node.io.TermOp) ATOM(suite.fp.match.Matchers.ATOM) WRAP(suite.fp.match.Matchers.WRAP) Streamlet(suite.streamlet.Streamlet) CHARS(suite.fp.match.Matchers.CHARS) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Fail(suite.util.Fail) Str(suite.node.Str) BiFun(suite.util.FunUtil2.BiFun) Operator(suite.node.io.Operator) HashMap(java.util.HashMap) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 2 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class InterpretFunLazy0 method lazy.

public Thunk_ lazy(Node node) {
    Thunk_ error = () -> Fail.t("error termination");
    IMap<String, Thunk_> env = IMap.empty();
    env = env.put(Atom.TRUE.name, () -> Atom.TRUE);
    env = env.put(Atom.FALSE.name, () -> Atom.FALSE);
    env = env.put(TermOp.AND___.name, () -> new Fun_(a -> () -> new Fun_(b -> () -> new Pair_(a, b))));
    env = env.put(ERROR.name, error);
    env = env.put(FST__.name, () -> new Fun_(in -> ((Pair_) in.get()).first));
    env = env.put(SND__.name, () -> new Fun_(in -> ((Pair_) in.get()).second));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> b(fun.apply(i(a), i(b))))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> Int.of(fun.apply(i(a), i(b))))));
    }
    return lazy_(node).apply(env);
}
Also used : Suite(suite.Suite) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IMap(suite.immutable.IMap) Mutable(suite.adt.Mutable) TermOp(suite.node.io.TermOp) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Fail(suite.util.Fail) Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 3 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class SystemPredicates method call.

public Boolean call(Node query) {
    BuiltinPredicate predicate;
    Tree tree;
    Operator op;
    Node left;
    String name = null;
    Node pass = null;
    if (query instanceof Atom) {
        name = ((Atom) query).name;
        pass = Atom.NIL;
    } else if ((tree = Tree.decompose(query)) != null)
        if ((op = tree.getOperator()) != TermOp.TUPLE_) {
            name = op.getName();
            pass = query;
        } else if ((left = tree.getLeft()) instanceof Atom) {
            name = ((Atom) left).name;
            pass = tree.getRight();
        }
    predicate = name != null ? get(name) : null;
    return predicate != null ? predicate.prove(prover, pass) : null;
}
Also used : BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) Operator(suite.node.io.Operator) Node(suite.node.Node) Tree(suite.node.Tree) Atom(suite.node.Atom)

Example 4 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class SewingExpressionImpl method evaluator.

public Evaluate_ evaluator(Node node) {
    Tree tree = Tree.decompose(node);
    if (tree != null) {
        Operator op = tree.getOperator();
        Evaluate_ lhs, rhs;
        IntInt_Int fun;
        if (op == TermOp.TUPLE_) {
            Tree rightTree = Tree.decompose(tree.getRight());
            lhs = evaluator(tree.getLeft());
            rhs = evaluator(rightTree.getRight());
            fun = TreeUtil.evaluateOp(rightTree.getLeft());
        } else {
            lhs = evaluator(tree.getLeft());
            rhs = evaluator(tree.getRight());
            fun = TreeUtil.evaluateOp(op);
        }
        return env -> fun.apply(lhs.evaluate(env), rhs.evaluate(env));
    } else if (node instanceof Int) {
        int i = ((Int) node).number;
        return env -> i;
    } else {
        Clone_ f = clonerFactory.cloner(node);
        return env -> TreeUtil.evaluate(f.apply(env));
    }
}
Also used : Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) Clone_(suite.lp.doer.ClonerFactory.Clone_) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) TermOp(suite.node.io.TermOp) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Tree(suite.node.Tree) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Int(suite.primitive.IntInt_Int) Int(suite.node.Int)

Example 5 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class SewingGeneralizerImpl method generalizer.

@Override
public Generalize_ generalizer(Node node) {
    List<Generalize_> funs = new ArrayList<>();
    Generalize_ fun;
    while (true) {
        Node node0 = node;
        Tree tree;
        if (node0 instanceof Atom) {
            Atom atom = (Atom) node0;
            String name = atom.name;
            if (ProverConstant.isCut(node0) || ProverConstant.isVariable(name)) {
                int index = vm.computeIndex(atom);
                fun = env -> env.get(index);
            } else if (ProverConstant.isWildcard(name))
                fun = env -> new Reference();
            else
                fun = env -> node0;
        } else if (node0 instanceof Dict) {
            Generalize_[][] array = // 
            Read.from2(// 
            ((Dict) node0).map).map(// 
            (key, value) -> new Generalize_[] { generalizer(key), generalizer(value) }).toArray(Generalize_[].class);
            int length = array.length;
            fun = env -> {
                @SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
                for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
                return Dict.of(pairs);
            };
        } else if ((tree = Tree.decompose(node0)) != null) {
            Operator operator = tree.getOperator();
            if (operator != TermOp.OR____) {
                Generalize_ f = generalizer(tree.getLeft());
                funs.add(env -> Tree.of(operator, f.apply(env), null));
                node = tree.getRight();
                continue;
            } else {
                // delay generalizing for performance
                Generalize_ lf = generalizer(tree.getLeft());
                Generalize_ rf = generalizer(tree.getRight());
                fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
            }
        } else if (node0 instanceof Tuple) {
            Generalize_[] fs = Read.from(((Tuple) node0).nodes).map(this::generalizer).toArray(Generalize_.class);
            int length = fs.length;
            fun = env -> {
                Node[] array = new Node[length];
                for (int i = 0; i < length; i++) array[i] = fs[i].apply(env);
                return Tuple.of(array);
            };
        } else
            fun = env -> node0;
        funs.add(fun);
        break;
    }
    if (1 < funs.size())
        return env -> {
            Tree t = Tree.of(null, null, null);
            Node node_ = t;
            for (Generalize_ fun_ : funs) {
                Tree t_ = Tree.decompose(node_);
                Tree.forceSetRight(t_, fun_.apply(env));
                node_ = t_.getRight();
            }
            return t.getRight();
        };
    else
        return funs.get(0);
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) GeneralizerFactory(suite.lp.doer.GeneralizerFactory) TermOp(suite.node.io.TermOp) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Atom(suite.node.Atom) Suspend(suite.node.Suspend) Tuple(suite.node.Tuple) Operator(suite.node.io.Operator) Dict(suite.node.Dict) Operator(suite.node.io.Operator) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Suspend(suite.node.Suspend) Atom(suite.node.Atom) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Aggregations

Operator (suite.node.io.Operator)15 Tree (suite.node.Tree)11 Node (suite.node.Node)9 TermOp (suite.node.io.TermOp)8 ArrayList (java.util.ArrayList)6 Atom (suite.node.Atom)6 Int (suite.node.Int)6 TreeUtil (suite.node.util.TreeUtil)6 IntInt_Int (suite.primitive.IntInt_Int)6 List (java.util.List)5 Entry (java.util.Map.Entry)4 Suite (suite.Suite)4 Pair (suite.adt.pair.Pair)4 IMap (suite.immutable.IMap)4 IntInt_Bool (suite.node.util.TreeUtil.IntInt_Bool)4 Fail (suite.util.Fail)4 Fun (suite.util.FunUtil.Fun)4 Iterate (suite.util.FunUtil.Iterate)4 Mutable (suite.adt.Mutable)3 HashMap (java.util.HashMap)2