Search in sources :

Example 1 with ISet

use of suite.immutable.ISet in project suite by stupidsing.

the class P2InferType method captureLambdas.

private Funp captureLambdas(Funp node0) {
    class Capture {

        private Fun<String, Funp> accesses;

        private ISet<String> locals;

        private ISet<String> globals;

        private Capture(Fun<String, Funp> accesses, ISet<String> locals, ISet<String> globals) {
            this.accesses = accesses;
            this.locals = locals;
            this.globals = globals;
        }

        private Funp capture(Funp n) {
            return inspect.rewrite(Funp.class, this::capture_, n);
        }

        private Funp capture_(Funp n) {
            return // 
            n.<// 
            Funp>switch_().applyIf(FunpDefine.class, f -> f.apply((isPolyType, var, value, expr) -> {
                Capture c1 = new Capture(accesses, locals.add(var), globals);
                return FunpDefine.of(isPolyType, var, capture(value), c1.capture(expr));
            })).applyIf(FunpDefineRec.class, f -> f.apply((vars, expr) -> {
                List<Pair<String, Funp>> vars1 = new ArrayList<>();
                ISet<String> locals1 = locals;
                for (Pair<String, Funp> pair : vars) {
                    locals1 = locals1.add(pair.t0);
                    vars1.add(Pair.of(pair.t0, capture(pair.t1)));
                }
                Capture c1 = new Capture(accesses, locals1, globals);
                return FunpDefineRec.of(vars1, c1.capture(expr));
            })).applyIf(FunpGlobal.class, f -> f.apply((var, value, expr) -> {
                Capture c1 = new Capture(accesses, locals, globals.add(var));
                return FunpGlobal.of(var, capture(value), c1.capture(expr));
            })).applyIf(FunpIterate.class, f -> f.apply((var, init, cond, iterate) -> {
                Capture c1 = new Capture(accesses, locals.add(var), globals);
                return FunpIterate.of(var, capture(init), c1.capture(cond), c1.capture(iterate));
            })).applyIf(FunpLambda.class, f -> f.apply((var, expr) -> {
                ISet<String> locals1 = ISet.empty();
                String capn = "cap" + Util.temp();
                FunpVariable cap = FunpVariable.of(capn);
                FunpReference ref = FunpReference.of(cap);
                Set<String> set = new HashSet<>();
                List<Pair<String, Funp>> list = new ArrayList<>();
                FunpStruct struct = FunpStruct.of(list);
                Capture c1 = new Capture(v -> {
                    if (set.add(v))
                        list.add(Pair.of(v, FunpVariable.of(v)));
                    return FunpField.of(ref, v);
                }, locals1.add(capn).add(var), globals);
                return FunpGlobal.of(capn, struct, FunpLambdaCapture.of(var, capn, cap, c1.capture(expr)));
            // TODO allocate cap on heap
            // TODO free cap after use
            })).applyIf(FunpVariable.class, f -> f.apply(var -> {
                return locals.contains(var) || globals.contains(var) ? f : accesses.apply(var);
            })).result();
        }
    }
    return new Capture(v -> Fail.t(), ISet.empty(), ISet.empty()).capture(node0);
}
Also used : FunpAllocGlobal(suite.funp.P2.FunpAllocGlobal) FunpInvokeIo(suite.funp.P2.FunpInvokeIo) Mutable(suite.adt.Mutable) FunpOperand(suite.funp.P2.FunpOperand) FunpDefine(suite.funp.P0.FunpDefine) Fun(suite.util.FunUtil.Fun) FunpLambda(suite.funp.P0.FunpLambda) FunpFramePointer(suite.funp.P2.FunpFramePointer) FunpPredefine(suite.funp.P0.FunpPredefine) Operand(suite.assembler.Amd64.Operand) Map(java.util.Map) FunpWhile(suite.funp.P2.FunpWhile) FunpData(suite.funp.P2.FunpData) FunpInvoke(suite.funp.P2.FunpInvoke) UnNode(suite.fp.Unify.UnNode) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpDefineRec(suite.funp.P0.FunpDefineRec) IMap(suite.immutable.IMap) Set(java.util.Set) ISet(suite.immutable.ISet) Unify(suite.fp.Unify) FunpRoutineIo(suite.funp.P2.FunpRoutineIo) Pair(suite.adt.pair.Pair) List(java.util.List) FunpInvoke2(suite.funp.P2.FunpInvoke2) FunpRepeat(suite.funp.P0.FunpRepeat) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpSaveRegisters(suite.funp.P2.FunpSaveRegisters) FunpVariable(suite.funp.P0.FunpVariable) FunpIterate(suite.funp.P0.FunpIterate) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) AutoObject(suite.util.AutoObject) FunpBoolean(suite.funp.P0.FunpBoolean) Util(suite.util.Util) FunpArray(suite.funp.P0.FunpArray) HashMap(java.util.HashMap) OpReg(suite.assembler.Amd64.OpReg) FunpIndex(suite.funp.P0.FunpIndex) FunpIoCat(suite.funp.P0.FunpIoCat) FunpReference(suite.funp.P0.FunpReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FunpApply(suite.funp.P0.FunpApply) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) String_(suite.util.String_) FunpIf(suite.funp.P0.FunpIf) Rethrow(suite.util.Rethrow) FunpGlobal(suite.funp.P0.FunpGlobal) FunpIo(suite.funp.P0.FunpIo) FunpAsm(suite.funp.P0.FunpAsm) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) FunpField(suite.funp.P0.FunpField) TermOp(suite.node.io.TermOp) FunpAssign(suite.funp.P2.FunpAssign) FunpAssignReference(suite.funp.P0.FunpAssignReference) FunpCheckType(suite.funp.P0.FunpCheckType) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) FunpError(suite.funp.P0.FunpError) FunpRoutine2(suite.funp.P2.FunpRoutine2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) FunpNumber(suite.funp.P0.FunpNumber) FunpRoutine(suite.funp.P2.FunpRoutine) FixieFun1(suite.adt.pair.Fixie_.FixieFun1) FixieFun2(suite.adt.pair.Fixie_.FixieFun2) Switch(suite.util.Switch) FunpStruct(suite.funp.P0.FunpStruct) FixieFun0(suite.adt.pair.Fixie_.FixieFun0) FunpAllocStack(suite.funp.P2.FunpAllocStack) Fail(suite.util.Fail) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) Set(java.util.Set) ISet(suite.immutable.ISet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) FunpIterate(suite.funp.P0.FunpIterate) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) Funp(suite.funp.Funp_.Funp) FunpDefineRec(suite.funp.P0.FunpDefineRec) FunpStruct(suite.funp.P0.FunpStruct) FunpVariable(suite.funp.P0.FunpVariable) List(java.util.List) ArrayList(java.util.ArrayList) ISet(suite.immutable.ISet) Fun(suite.util.FunUtil.Fun) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 2 with ISet

use of suite.immutable.ISet in project suite by stupidsing.

the class Chr method chrIf.

private Streamlet<State> chrIf(Streamlet<State> states, Trail trail, Node if_) {
    Prototype prototype = Prototype.of(if_);
    Fun<State, Streamlet<State>> fun = state -> {
        ISet<Node> facts = getFacts(state, prototype);
        Predicate<Node> bindFun = bindFun(trail, if_);
        return facts.streamlet().filter(bindFun).map(node -> setFacts(state, prototype, facts.remove(node)));
    };
    return states.concatMap(fun);
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Prover(suite.lp.doer.Prover) Read(suite.streamlet.Read) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IMap(suite.immutable.IMap) ISet(suite.immutable.ISet) TermOp(suite.node.io.TermOp) To(suite.util.To) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Prototype(suite.lp.kb.Prototype) Binder(suite.lp.doer.Binder) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Prototype(suite.lp.kb.Prototype) Streamlet(suite.streamlet.Streamlet) ISet(suite.immutable.ISet) Predicate(java.util.function.Predicate)

Example 3 with ISet

use of suite.immutable.ISet in project suite by stupidsing.

the class Chr method chr.

public Collection<Node> chr(Collection<Node> facts) {
    State state = new State(IMap.empty());
    for (Node fact : facts) {
        Prototype prototype = Prototype.of(fact);
        state = setFacts(state, prototype, getFacts(state, prototype).replace(fact));
    }
    State state1;
    while ((state1 = chr(state)) != null) state = state1;
    List<Node> nodes1 = new ArrayList<>();
    for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) nodes1.addAll(To.list(e.t1));
    return nodes1;
}
Also used : Prototype(suite.lp.kb.Prototype) Node(suite.node.Node) ArrayList(java.util.ArrayList) ISet(suite.immutable.ISet)

Example 4 with ISet

use of suite.immutable.ISet in project suite by stupidsing.

the class Chr method chrThen.

private Streamlet<State> chrThen(Streamlet<State> states, Node then) {
    Generalizer generalizer = new Generalizer();
    Atom a = atom(".a"), b = atom(".b");
    if (Binder.bind(then, generalizer.generalize(Suite.substitute(".0 = .1", a, b)), new Trail())) {
        // built-in syntactic equality
        Reference from = generalizer.getVariable(a);
        Reference to = generalizer.getVariable(b);
        states = states.map(new Fun<>() {

            public State apply(State state) {
                IMap<Prototype, ISet<Node>> factsByPrototype1 = IMap.empty();
                for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) factsByPrototype1 = factsByPrototype1.put(e.t0, replace(e.t1));
                return new State(factsByPrototype1);
            }

            private ISet<Node> replace(ISet<Node> facts) {
                ISet<Node> facts1 = ISet.empty();
                for (Node node : facts) facts1 = facts1.replace(rw.replace(from, to, node));
                return facts1;
            }
        });
    }
    return states.map(state -> {
        Prototype prototype = Prototype.of(then);
        ISet<Node> facts = getFacts(state, prototype);
        return setFacts(state, prototype, facts.replace(then));
    });
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Prototype(suite.lp.kb.Prototype) Reference(suite.node.Reference) Node(suite.node.Node) ISet(suite.immutable.ISet) Atom(suite.node.Atom) Fun(suite.util.FunUtil.Fun)

Aggregations

ISet (suite.immutable.ISet)4 ArrayList (java.util.ArrayList)3 Prototype (suite.lp.kb.Prototype)3 Node (suite.node.Node)3 Fun (suite.util.FunUtil.Fun)3 List (java.util.List)2 Pair (suite.adt.pair.Pair)2 IMap (suite.immutable.IMap)2 TermOp (suite.node.io.TermOp)2 Read (suite.streamlet.Read)2 Fail (suite.util.Fail)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Suite (suite.Suite)1 Mutable (suite.adt.Mutable)1 FixieFun0 (suite.adt.pair.Fixie_.FixieFun0)1