Search in sources :

Example 11 with Quad

use of catdata.Quad in project fql by CategoricalData.

the class InstOps method visit.

@Override
public Pair<List<PSM>, Object> visit(String dst, Times e) {
    try {
        SigExp k = e.type(prog);
        Signature s = k.toSig(prog);
        List<PSM> ret = new LinkedList<>();
        ret.addAll(PSMGen.makeTables(dst + "_fst", s, false));
        ret.addAll(PSMGen.makeTables(dst + "_snd", s, false));
        Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> l_obs = Relationalizer.observations(s, dst + "_l_obs", e.a, false);
        Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> r_obs = Relationalizer.observations(s, dst + "_r_obs", e.b, false);
        if (!(l_obs.first.equals(r_obs.first))) {
            throw new RuntimeException("Internal error, please report.");
        }
        ret.addAll(PSMGen.makeTables(dst + "_l_obs", s, false));
        ret.addAll(l_obs.second);
        ret.addAll(PSMGen.dropTables(dst + "_l_obs", s));
        ret.addAll(PSMGen.makeTables(dst + "_r_obs", s, false));
        ret.addAll(r_obs.second);
        ret.addAll(PSMGen.dropTables(dst + "_r_obs", s));
        for (Node n : s.nodes) {
            List<Pair<Path, Attribute<Node>>> lats = l_obs.first.get(n);
            // List<Pair<Path, Attribute<Node>>> rats = r_obs.first.get(n);
            LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
            Map<String, String> from = new HashMap<>();
            List<String> attrs = new LinkedList<>();
            Map<String, String> attrsM = new HashMap<>();
            List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
            from.put("lft", dst + "_l_obs_" + n.string + "_observables");
            from.put("rght", dst + "_r_obs_" + n.string + "_observables");
            attrs.add("lft");
            attrs.add("rght");
            attrsM.put("lft", PSM.VARCHAR());
            attrsM.put("rght", PSM.VARCHAR());
            select.put("lft", new Pair<>("lft", "id"));
            select.put("rght", new Pair<>("rght", "id"));
            int idx = 0;
            for (Pair<Path, Attribute<Node>> aa : lats) {
                Attribute<Node> a = aa.second;
                where.add(new Pair<>(new Pair<>("lft", "c" + idx), new Pair<>("rght", "c" + idx)));
                select.put("c" + idx, new Pair<>("lft", "c" + idx));
                attrs.add("c" + idx);
                attrsM.put("c" + idx, a.target.psm());
                idx++;
            }
            Flower f = new Flower(select, from, where);
            ret.add(new CreateTable(dst + "_prod_temp_" + n.string, attrsM, false));
            ret.add(new InsertSQL2(dst + "_prod_temp_" + n.string, f, attrs));
            Map<String, String> attrsM0 = new HashMap<>(attrsM);
            attrsM0.put("gguid", PSM.VARCHAR());
            ret.add(new CreateTable(dst + "_prod_guid_" + n.string, attrsM0, false));
            ret.add(new InsertKeygen(dst + "_prod_guid_" + n.string, "gguid", dst + "_prod_temp_" + n.string, attrs));
            List<Pair<Pair<String, String>, Pair<String, String>>> where0 = new LinkedList<>();
            from = new HashMap<>();
            from.put("t", dst + "_prod_guid_" + n.string);
            select = new LinkedHashMap<>();
            select.put("c0", new Pair<>("t", "gguid"));
            select.put("c1", new Pair<>("t", "lft"));
            f = new Flower(select, from, where0);
            ret.add(new InsertSQL(dst + "_fst_" + n, f, "c0", "c1"));
            from = new HashMap<>();
            from.put("t", dst + "_prod_guid_" + n.string);
            select = new LinkedHashMap<>();
            select.put("c0", new Pair<>("t", "gguid"));
            select.put("c1", new Pair<>("t", "rght"));
            f = new Flower(select, from, where0);
            ret.add(new InsertSQL(dst + "_snd_" + n, f, "c0", "c1"));
            LinkedHashMap<String, Pair<String, String>> select0 = new LinkedHashMap<>();
            select0.put("c0", new Pair<>("t", "gguid"));
            select0.put("c1", new Pair<>("t", "gguid"));
            Map<String, String> from0 = new HashMap<>();
            from0.put("t", dst + "_prod_guid_" + n.string);
            Flower sql = new Flower(select0, from0, where0);
            ret.add(new InsertSQL(dst + "_" + n.string, sql, "c0", "c1"));
            for (Attribute<Node> a : s.attrsFor(n)) {
                select0 = new LinkedHashMap<>();
                select0.put("c0", new Pair<>("t", "gguid"));
                Arr<Node, Path> ppp = s.toCategory2().first.id(n);
                int ppp0 = lats.indexOf(new Pair<>(ppp.arr, a));
                select0.put("c1", new Pair<>("t", "c" + ppp0));
                from0 = new HashMap<>();
                from0.put("t", dst + "_prod_guid_" + n.string);
                sql = new Flower(select0, from0, where0);
                ret.add(new InsertSQL(dst + "_" + a.name, sql, "c0", "c1"));
            }
        // ret.add(new DropTable(dst + "_prod_temp_" + n));
        }
        for (Edge edge : s.edges) {
            Map<String, String> from = new HashMap<>();
            from.put("leftEdge", e.a + "_" + edge.name);
            from.put("rightEdge", e.b + "_" + edge.name);
            from.put("srcGuid", dst + "_prod_guid_" + edge.source);
            from.put("dstGuid", dst + "_prod_guid_" + edge.target);
            List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
            where.add(new Pair<>(new Pair<>("leftEdge", "c0"), new Pair<>("srcGuid", "lft")));
            where.add(new Pair<>(new Pair<>("rightEdge", "c0"), new Pair<>("srcGuid", "rght")));
            where.add(new Pair<>(new Pair<>("leftEdge", "c1"), new Pair<>("dstGuid", "lft")));
            where.add(new Pair<>(new Pair<>("rightEdge", "c1"), new Pair<>("dstGuid", "rght")));
            LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
            select.put("c0", new Pair<>("srcGuid", "gguid"));
            select.put("c1", new Pair<>("dstGuid", "gguid"));
            Flower f = new Flower(select, from, where);
            ret.add(new InsertSQL(dst + "_" + edge.name, f, "c0", "c1"));
        }
        Fn<Quad<String, String, String, String>, List<PSM>> fn = x -> {
            // x.third -> e.a
            String f = x.first;
            // x.third -> e.b
            String g = x.second;
            // String C = x.third;
            String dst0 = x.fourth;
            // must be a map x.third -> dst
            List<PSM> ret1 = new LinkedList<>();
            for (Node n : s.nodes) {
                List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
                Map<String, String> from = new HashMap<>();
                from.put("f", f + "_" + n.string);
                from.put("g", g + "_" + n.string);
                from.put("lim", dst + "_prod_guid_" + n.string);
                where.add(new Pair<>(new Pair<>("f", "c0"), new Pair<>("g", "c0")));
                where.add(new Pair<>(new Pair<>("lim", "lft"), new Pair<>("f", "c1")));
                where.add(new Pair<>(new Pair<>("lim", "rght"), new Pair<>("g", "c1")));
                LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
                select.put("c0", new Pair<>("f", "c0"));
                select.put("c1", new Pair<>("lim", "gguid"));
                Flower flower = new Flower(select, from, where);
                ret1.add(new InsertSQL(dst0 + "_" + n.string, flower, "c0", "c1"));
            }
            return ret1;
        };
        return new Pair<>(ret, fn);
    } catch (FQLException fe) {
        throw new RuntimeException(fe.getLocalizedMessage());
    }
}
Also used : Inr(catdata.fql.decl.TransExp.Inr) Case(catdata.fql.decl.TransExp.Case) TransEval(catdata.fql.decl.TransExp.TransEval) Snd(catdata.fql.decl.TransExp.Snd) TransIso(catdata.fql.decl.TransExp.TransIso) Kernel(catdata.fql.decl.InstExp.Kernel) TransCurry(catdata.fql.decl.TransExp.TransCurry) Inl(catdata.fql.decl.TransExp.Inl) Coreturn(catdata.fql.decl.TransExp.Coreturn) PSMStep(catdata.fql.sql.PSMStep) CopyFlower(catdata.fql.sql.CopyFlower) Map(java.util.Map) Or(catdata.fql.decl.TransExp.Or) Squash(catdata.fql.decl.TransExp.Squash) TT(catdata.fql.decl.TransExp.TT) And(catdata.fql.decl.TransExp.And) Exp(catdata.fql.decl.InstExp.Exp) Fn(catdata.fql.Fn) Pair(catdata.Pair) Not(catdata.fql.decl.TransExp.Not) FullSigma(catdata.fql.decl.InstExp.FullSigma) PSMAnd(catdata.fql.sql.PSMAnd) Plus(catdata.fql.decl.InstExp.Plus) Quad(catdata.Quad) FullSigmaCounit(catdata.fql.sql.FullSigmaCounit) PropPSM(catdata.fql.sql.PropPSM) FullEval(catdata.fql.decl.InstExp.FullEval) Set(java.util.Set) PSMGen(catdata.fql.sql.PSMGen) FullQueryVisitor(catdata.fql.decl.FullQuery.FullQueryVisitor) Eval(catdata.fql.decl.InstExp.Eval) InsertSQL2(catdata.fql.sql.InsertSQL2) PSMUnChi(catdata.fql.sql.PSMUnChi) Times(catdata.fql.decl.InstExp.Times) Fst(catdata.fql.decl.TransExp.Fst) PSMNot(catdata.fql.sql.PSMNot) List(java.util.List) Delta(catdata.fql.decl.InstExp.Delta) ExpPSM(catdata.fql.sql.ExpPSM) TransExpVisitor(catdata.fql.decl.TransExp.TransExpVisitor) PSMBool(catdata.fql.sql.PSMBool) Triple(catdata.Triple) Flower(catdata.fql.sql.Flower) Implies(catdata.fql.decl.TransExp.Implies) Id(catdata.fql.decl.TransExp.Id) External(catdata.fql.decl.InstExp.External) InsertKeygen(catdata.fql.sql.InsertKeygen) Chi(catdata.fql.decl.TransExp.Chi) FullSigmaTrans(catdata.fql.sql.FullSigmaTrans) HashMap(java.util.HashMap) Var(catdata.fql.decl.TransExp.Var) SQL(catdata.fql.sql.SQL) FQLException(catdata.fql.FQLException) Two(catdata.fql.decl.InstExp.Two) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) PSM(catdata.fql.sql.PSM) FF(catdata.fql.decl.TransExp.FF) LinkedList(java.util.LinkedList) UnChi(catdata.fql.decl.TransExp.UnChi) SimpleCreateTable(catdata.fql.sql.SimpleCreateTable) Comp(catdata.fql.decl.TransExp.Comp) PSMIso(catdata.fql.sql.PSMIso) CreateTable(catdata.fql.sql.CreateTable) InsertValues(catdata.fql.sql.InsertValues) Prod(catdata.fql.decl.TransExp.Prod) Const(catdata.fql.decl.InstExp.Const) Relationalize(catdata.fql.decl.InstExp.Relationalize) PSMCurry(catdata.fql.sql.PSMCurry) One(catdata.fql.decl.InstExp.One) Zero(catdata.fql.decl.InstExp.Zero) Pi(catdata.fql.decl.InstExp.Pi) DropTable(catdata.fql.sql.DropTable) InstExpVisitor(catdata.fql.decl.InstExp.InstExpVisitor) Step(catdata.fql.decl.InstExp.Step) Arr(catdata.fql.cat.Arr) Return(catdata.fql.decl.TransExp.Return) InsertSQL(catdata.fql.sql.InsertSQL) Union(catdata.fql.sql.Union) Sigma(catdata.fql.decl.InstExp.Sigma) Bool(catdata.fql.decl.TransExp.Bool) PSMEval(catdata.fql.sql.PSMEval) Relationalizer(catdata.fql.sql.Relationalizer) PSMChi(catdata.fql.sql.PSMChi) Quad(catdata.Quad) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleCreateTable(catdata.fql.sql.SimpleCreateTable) CreateTable(catdata.fql.sql.CreateTable) InsertKeygen(catdata.fql.sql.InsertKeygen) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) CopyFlower(catdata.fql.sql.CopyFlower) Flower(catdata.fql.sql.Flower) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) PSM(catdata.fql.sql.PSM) LinkedList(java.util.LinkedList) InsertSQL(catdata.fql.sql.InsertSQL) InsertSQL2(catdata.fql.sql.InsertSQL2) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with Quad

use of catdata.Quad in project fql by CategoricalData.

the class FinCat method toSig.

/**
 * Converts a category to a signature.
 *
 * @return a signature and isomorphism
 * @throws FQLException
 */
public Quad<Signature, Pair<Map<Obj, String>, Map<String, Obj>>, Pair<Map<Arr<Obj, Arrow>, String>, Map<String, Arr<Obj, Arrow>>>, Pair<Map<Attribute<Obj>, String>, Map<String, Attribute<Obj>>>> toSig(Map<String, Type> types) throws FQLException {
    Map<Attribute<Obj>, String> attM = new HashMap<>();
    Map<String, Attribute<Obj>> attM2 = new HashMap<>();
    int ax = 0;
    List<String> objs = new LinkedList<>();
    List<Triple<String, String, String>> attrs0 = new LinkedList<>();
    int i = 0;
    Map<String, Obj> objM = new HashMap<>();
    Map<Obj, String> objM2 = new HashMap<>();
    for (Obj o : objects) {
        objM2.put(o, "obj" + i);
        objM.put("obj" + i, o);
        objs.add("obj" + i);
        i++;
    }
    if (attrs != null) {
        for (Attribute<Obj> att : attrs) {
            attM.put(att, "attrib" + ax);
            attM2.put("attrib" + ax, att);
            attrs0.add(new Triple<>("attrib" + ax++, objM2.get(att.source), att.target.toString()));
        }
    }
    List<Triple<String, String, String>> arrs = new LinkedList<>();
    int j = 0;
    Map<String, Arr<Obj, Arrow>> arrM = new HashMap<>();
    Map<Arr<Obj, Arrow>, String> arrM2 = new HashMap<>();
    for (Arr<Obj, Arrow> a : arrows) {
        if (isId(a)) {
            continue;
        }
        arrM.put("arrow" + j, a);
        arrM2.put(a, "arrow" + j);
        arrs.add(new Triple<>(arrM2.get(a), objM2.get(a.src), objM2.get(a.dst)));
        j++;
    }
    LinkedList<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    for (Pair<Arr<Obj, Arrow>, Arr<Obj, Arrow>> k : composition.keySet()) {
        Arr<Obj, Arrow> v = composition.get(k);
        String s = arrM2.get(k.first);
        String t = arrM2.get(k.second);
        String u = arrM2.get(v);
        String ob = objM2.get(v.src);
        List<String> lhs = new LinkedList<>();
        List<String> rhs = new LinkedList<>();
        lhs.add(ob);
        rhs.add(ob);
        if (s != null) {
            lhs.add(s);
        }
        if (t != null) {
            lhs.add(t);
        }
        if (u != null) {
            rhs.add(u);
        }
        if (!lhs.equals(rhs)) {
            eqs.add(new Pair<>(lhs, rhs));
        }
    }
    Signature ret2 = new Signature(types, objs, attrs0, arrs, eqs);
    Quad<Signature, Pair<Map<Obj, String>, Map<String, Obj>>, Pair<Map<Arr<Obj, Arrow>, String>, Map<String, Arr<Obj, Arrow>>>, Pair<Map<Attribute<Obj>, String>, Map<String, Attribute<Obj>>>> retret = new Quad<>(ret2, new Pair<>(objM2, objM), new Pair<>(arrM2, arrM), new Pair<>(attM, attM2));
    return retret;
}
Also used : Quad(catdata.Quad) Attribute(catdata.fql.decl.Attribute) HashMap(java.util.HashMap) Pair(catdata.Pair) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Signature(catdata.fql.decl.Signature)

Example 13 with Quad

use of catdata.Quad in project fql by CategoricalData.

the class SigOps method plus.

private static Quad<SigExp.Const, Const, Const, Fn<Triple<SigExp.Const, Const, Const>, Const>> plus(SigExp.Const a, SigExp.Const b) {
    int node_count = 0;
    Map<String, String> node_map_1 = new LinkedHashMap<>();
    Map<String, String> node_map_2 = new LinkedHashMap<>();
    List<Pair<String, String>> a_objs = new LinkedList<>();
    List<Pair<String, String>> b_objs = new LinkedList<>();
    for (String n : a.nodes) {
        node_map_1.put(n, "node" + node_count);
        a_objs.add(new Pair<>(n, "node" + node_count));
        node_count++;
    }
    for (String n : b.nodes) {
        node_map_2.put(n, "node" + node_count);
        b_objs.add(new Pair<>(n, "node" + node_count));
        node_count++;
    }
    List<String> nodes = new LinkedList<>();
    nodes.addAll(node_map_1.values());
    nodes.addAll(node_map_2.values());
    int attr_count = 0;
    Map<String, Triple<String, String, String>> attr_map_1 = new LinkedHashMap<>();
    Map<String, Triple<String, String, String>> attr_map_2 = new LinkedHashMap<>();
    List<Pair<String, String>> a_attrs = new LinkedList<>();
    List<Pair<String, String>> b_attrs = new LinkedList<>();
    for (Triple<String, String, String> n : a.attrs) {
        attr_map_1.put(n.first, new Triple<>("attr" + attr_count, node_map_1.get(n.second), n.third));
        a_attrs.add(new Pair<>(n.first, "attr" + attr_count));
        attr_count++;
    }
    for (Triple<String, String, String> n : b.attrs) {
        attr_map_2.put(n.first, new Triple<>("attr" + attr_count, node_map_2.get(n.second), n.third));
        b_attrs.add(new Pair<>(n.first, "attr" + attr_count));
        attr_count++;
    }
    List<Triple<String, String, String>> attrs = new LinkedList<>();
    attrs.addAll(attr_map_1.values());
    attrs.addAll(attr_map_2.values());
    int edge_count = 0;
    Map<String, Triple<String, String, String>> edge_map_1 = new LinkedHashMap<>();
    Map<String, Triple<String, String, String>> edge_map_2 = new LinkedHashMap<>();
    List<Pair<String, List<String>>> a_arrows = new LinkedList<>();
    List<Pair<String, List<String>>> b_arrows = new LinkedList<>();
    for (Triple<String, String, String> n : a.arrows) {
        edge_map_1.put(n.first, new Triple<>("edge" + edge_count, node_map_1.get(n.second), node_map_1.get(n.third)));
        List<String> x = new LinkedList<>();
        x.add(node_map_1.get(n.second));
        x.add("edge" + edge_count);
        a_arrows.add(new Pair<>(n.first, x));
        edge_count++;
    }
    for (Triple<String, String, String> n : b.arrows) {
        edge_map_2.put(n.first, new Triple<>("edge" + edge_count, node_map_2.get(n.second), node_map_2.get(n.third)));
        List<String> x = new LinkedList<>();
        x.add(node_map_2.get(n.second));
        x.add("edge" + edge_count);
        b_arrows.add(new Pair<>(n.first, x));
        edge_count++;
    }
    List<Triple<String, String, String>> arrows = new LinkedList<>();
    arrows.addAll(edge_map_1.values());
    arrows.addAll(edge_map_2.values());
    List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    for (Pair<List<String>, List<String>> eq : a.eqs) {
        List<String> lhs = new LinkedList<>();
        lhs.add(node_map_1.get(eq.first.get(0)));
        for (int i = 1; i < eq.first.size(); i++) {
            lhs.add(edge_map_1.get(eq.first.get(i)).first);
        }
        List<String> rhs = new LinkedList<>();
        rhs.add(node_map_1.get(eq.second.get(0)));
        for (int i = 1; i < eq.second.size(); i++) {
            rhs.add(edge_map_1.get(eq.second.get(i)).first);
        }
        eqs.add(new Pair<>(lhs, rhs));
    }
    for (Pair<List<String>, List<String>> eq : b.eqs) {
        List<String> lhs = new LinkedList<>();
        lhs.add(node_map_2.get(eq.first.get(0)));
        for (int i = 1; i < eq.first.size(); i++) {
            lhs.add(edge_map_2.get(eq.first.get(i)).first);
        }
        List<String> rhs = new LinkedList<>();
        rhs.add(node_map_2.get(eq.second.get(0)));
        for (int i = 1; i < eq.second.size(); i++) {
            rhs.add(edge_map_2.get(eq.second.get(i)).first);
        }
        eqs.add(new Pair<>(lhs, rhs));
    }
    SigExp.Const sig = new SigExp.Const(nodes, attrs, arrows, eqs);
    Const inj1 = new Const(a_objs, a_attrs, a_arrows, a, sig);
    Const inj2 = new Const(b_objs, b_attrs, b_arrows, b, sig);
    Fn<Triple<SigExp.Const, Const, Const>, Const> match = x -> {
        SigExp.Const c = x.first;
        Const f = x.second;
        Const g = x.third;
        if (!f.dst.equals(g.dst)) {
            throw new RuntimeException("Targets don't agree: " + f.dst + " and " + g.dst);
        }
        if (!f.src.equals(a)) {
            throw new RuntimeException("Source of " + f + " is not " + a);
        }
        if (!g.src.equals(b)) {
            throw new RuntimeException("Source of " + g + "is not " + b);
        }
        List<Pair<String, String>> objs = new LinkedList<>();
        for (String obj_a : a.nodes) {
            objs.add(new Pair<>(node_map_1.get(obj_a), lookup(obj_a, f.objs)));
        }
        for (String obj_b : b.nodes) {
            objs.add(new Pair<>(node_map_2.get(obj_b), lookup(obj_b, g.objs)));
        }
        List<Pair<String, String>> attrs1 = new LinkedList<>();
        for (Triple<String, String, String> attr_a : a.attrs) {
            attrs1.add(new Pair<>(attr_map_1.get(attr_a.first).first, lookup(attr_a.first, f.attrs)));
        }
        for (Triple<String, String, String> attr_b : b.attrs) {
            attrs1.add(new Pair<>(attr_map_2.get(attr_b.first).first, lookup(attr_b.first, g.attrs)));
        }
        List<Pair<String, List<String>>> arrows1 = new LinkedList<>();
        for (Triple<String, String, String> edge_a : a.arrows) {
            arrows1.add(new Pair<>(edge_map_1.get(edge_a.first).first, lookup(edge_a.first, f.arrows)));
        }
        for (Triple<String, String, String> edge_b : b.arrows) {
            arrows1.add(new Pair<>(edge_map_2.get(edge_b.first).first, lookup(edge_b.first, g.arrows)));
        }
        return new Const(objs, attrs1, arrows1, sig, c);
    };
    return new Quad<>(sig, inj1, inj2, match);
}
Also used : Case(catdata.fql.decl.MapExp.Case) Unknown(catdata.fql.decl.SigExp.Unknown) Inr(catdata.fql.decl.MapExp.Inr) Const(catdata.fql.decl.MapExp.Const) FF(catdata.fql.decl.MapExp.FF) Snd(catdata.fql.decl.MapExp.Snd) TT(catdata.fql.decl.MapExp.TT) HashMap(java.util.HashMap) FQLException(catdata.fql.FQLException) Inl(catdata.fql.decl.MapExp.Inl) LinkedHashMap(java.util.LinkedHashMap) Var(catdata.fql.decl.SigExp.Var) Sub(catdata.fql.decl.MapExp.Sub) Times(catdata.fql.decl.SigExp.Times) One(catdata.fql.decl.SigExp.One) FinCat(catdata.fql.cat.FinCat) Apply(catdata.fql.decl.MapExp.Apply) Map(java.util.Map) Curry(catdata.fql.decl.MapExp.Curry) Exp(catdata.fql.decl.SigExp.Exp) SigExpVisitor(catdata.fql.decl.SigExp.SigExpVisitor) LinkedList(java.util.LinkedList) Fn(catdata.fql.Fn) Pair(catdata.Pair) Zero(catdata.fql.decl.SigExp.Zero) Prod(catdata.fql.decl.MapExp.Prod) Quad(catdata.Quad) Dist1(catdata.fql.decl.MapExp.Dist1) Union(catdata.fql.decl.SigExp.Union) Comp(catdata.fql.decl.MapExp.Comp) Opposite(catdata.fql.decl.SigExp.Opposite) Set(java.util.Set) Inst(catdata.fql.cat.Inst) MapExpVisitor(catdata.fql.decl.MapExp.MapExpVisitor) Plus(catdata.fql.decl.SigExp.Plus) Dist2(catdata.fql.decl.MapExp.Dist2) Iso(catdata.fql.decl.MapExp.Iso) Fst(catdata.fql.decl.MapExp.Fst) List(java.util.List) Id(catdata.fql.decl.MapExp.Id) Arr(catdata.fql.cat.Arr) Triple(catdata.Triple) Collections(java.util.Collections) Quad(catdata.Quad) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) Const(catdata.fql.decl.MapExp.Const) LinkedList(java.util.LinkedList) Triple(catdata.Triple)

Example 14 with Quad

use of catdata.Quad in project fql by CategoricalData.

the class SigOps method visit.

@Override
public Const visit(FQLProgram env, Curry e) {
    try {
        Const F = e.f.accept(env, this);
        Pair<SigExp, SigExp> type = e.f.type(env);
        SigExp.Const C = type.second.toConst(env);
        Signature Csig = C.toSig(env);
        if (!(type.first instanceof Times)) {
            throw new RuntimeException();
        }
        Times src = (Times) type.first;
        SigExp.Const A = src.a.toConst(env);
        SigExp.Const B = src.b.toConst(env);
        // Signature Asig = A.toSig(env);
        Signature Bsig = B.toSig(env);
        if (!A.attrs.isEmpty()) {
            throw new RuntimeException("Cannot curry when context has attributes.");
        }
        Pair<Quad<SigExp.Const, Const, Const, Fn<Triple<SigExp.Const, Const, Const>, Const>>, Quad<Map<Pair<String, String>, String>, Map<Pair<String, String>, String>, Map<Pair<String, String>, String>, Map<Pair<String, String>, String>>> AB_stuff = prod(A, B);
        // SigExp.Const AB = AB_stuff.first.first;
        Quad<Map<Pair<String, String>, String>, Map<Pair<String, String>, String>, Map<Pair<String, String>, String>, Map<Pair<String, String>, String>> maps = AB_stuff.second;
        FinCat<Mapping, Map<Node, Path>> cat = exp(env, C.toSig(env), B.toSig(env));
        Quad<Signature, Pair<Map<Mapping, String>, Map<String, Mapping>>, Pair<Map<Arr<Mapping, Map<Node, Path>>, String>, Map<String, Arr<Mapping, Map<Node, Path>>>>, Pair<Map<Attribute<Mapping>, String>, Map<String, Attribute<Mapping>>>> CB_stuff = cat.toSig(env.enums);
        Signature CB = CB_stuff.first;
        List<Pair<String, String>> nmret = new LinkedList<>();
        for (String a : A.nodes) {
            Mapping m = curry_helper(F, Csig, B, Bsig, maps, a);
            String target = CB_stuff.second.first.get(m);
            nmret.add(new Pair<>(a, target));
        }
        List<Pair<String, List<String>>> amret = new LinkedList<>();
        for (Triple<String, String, String> a : A.arrows) {
            Mapping s = curry_helper(F, Csig, B, Bsig, maps, a.second);
            Mapping t = curry_helper(F, Csig, B, Bsig, maps, a.third);
            Map<Node, Path> nt = new HashMap<>();
            for (String b : B.nodes) {
                // edge A*B
                String p = maps.third.get(new Pair<>(a.first, b));
                // path C
                List<String> p0 = lookup(p, F.arrows);
                Path p1 = new Path(Csig, p0);
                nt.put(new Node(b), p1);
            }
            List<String> l = new LinkedList<>();
            l.add(CB_stuff.second.first.get(s));
            Arr<Mapping, Map<Node, Path>> arr = new Arr<>(nt, s, t);
            if (null != CB_stuff.third.first.get(arr)) {
                l.add(CB_stuff.third.first.get(arr));
            }
            amret.add(new Pair<>(a.first, l));
        }
        List<Pair<String, String>> attrs = new LinkedList<>();
        // A*B -> C
        // A -> C^B
        Const ret = new Const(nmret, attrs, amret, A, CB.toConst());
        ret.toMap(env);
        return ret;
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getMessage());
    }
}
Also used : Arr(catdata.fql.cat.Arr) Quad(catdata.Quad) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) Pair(catdata.Pair) Const(catdata.fql.decl.MapExp.Const) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Times(catdata.fql.decl.SigExp.Times) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Quad (catdata.Quad)14 Pair (catdata.Pair)13 HashMap (java.util.HashMap)11 LinkedList (java.util.LinkedList)10 Map (java.util.Map)10 Triple (catdata.Triple)9 List (java.util.List)8 Set (java.util.Set)7 Arr (catdata.fql.cat.Arr)6 FQLException (catdata.fql.FQLException)5 Fn (catdata.fql.Fn)4 LinkedHashMap (java.util.LinkedHashMap)4 Util (catdata.Util)3 Const (catdata.fql.decl.MapExp.Const)3 Times (catdata.fql.decl.SigExp.Times)3 Paint (java.awt.Paint)3 FullQueryVisitor (catdata.fql.decl.FullQuery.FullQueryVisitor)2 Const (catdata.fql.decl.InstExp.Const)2 Delta (catdata.fql.decl.InstExp.Delta)2 Eval (catdata.fql.decl.InstExp.Eval)2