Search in sources :

Example 1 with Const

use of catdata.fql.decl.MapExp.Const in project fql by CategoricalData.

the class SigOps method visit.

@Override
public Const visit(FQLProgram env, Case e) {
    Const lx = e.l.accept(env, this);
    Const rx = e.r.accept(env, this);
    SigExp.Const cx = lx.dst.accept(env, this);
    return plus(lx.src.accept(env, this), rx.src.accept(env, this)).fourth.of(new Triple<>(cx, lx, rx));
}
Also used : Const(catdata.fql.decl.MapExp.Const)

Example 2 with Const

use of catdata.fql.decl.MapExp.Const in project fql by CategoricalData.

the class SigOps method visit.

@Override
public Const visit(FQLProgram env, Prod e) {
    Const lx = e.l.accept(env, this);
    Const rx = e.r.accept(env, this);
    SigExp.Const cx = lx.src.accept(env, this);
    SigExp.Const dx = rx.src.accept(env, this);
    if (!cx.equals(dx)) {
        throw new RuntimeException(cx + " and " + dx + " and " + lx + " and " + rx);
    }
    return prod(lx.dst.accept(env, this), rx.dst.accept(env, this)).first.fourth.of(new Triple<>(cx, lx, rx));
}
Also used : Const(catdata.fql.decl.MapExp.Const)

Example 3 with Const

use of catdata.fql.decl.MapExp.Const in project fql by CategoricalData.

the class SigOps method one.

private static Pair<SigExp.Const, Fn<SigExp.Const, Const>> one(Set<String> at) {
    List<String> nodes = new LinkedList<>();
    nodes.add("node0");
    List<Triple<String, String, String>> attrs = new LinkedList<>();
    for (String k : at) {
        attrs.add(new Triple<>(k + "_attr", "node0", k));
    }
    List<Triple<String, String, String>> arrows = new LinkedList<>();
    SigExp.Const sig = new SigExp.Const(nodes, attrs, arrows, new LinkedList<>());
    Fn<SigExp.Const, Const> fn = (SigExp.Const src) -> {
        List<Pair<String, String>> nm = new LinkedList<>();
        for (String k : src.nodes) {
            nm.add(new Pair<>(k, "node0"));
        }
        List<Pair<String, String>> am = new LinkedList<>();
        for (Triple<String, String, String> k : src.attrs) {
            if (!at.contains(k.third)) {
                throw new RuntimeException("Enum/type not found: " + k.third);
            }
            am.add(new Pair<>(k.first, k.third + "_attr"));
        }
        List<Pair<String, List<String>>> em = new LinkedList<>();
        for (Triple<String, String, String> k : src.arrows) {
            List<String> l = new LinkedList<>();
            l.add("node0");
            em.add(new Pair<>(k.first, l));
        }
        return new Const(nm, am, em, src, sig);
    };
    return new Pair<>(sig, fn);
}
Also used : Const(catdata.fql.decl.MapExp.Const) LinkedList(java.util.LinkedList) Triple(catdata.Triple) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair)

Example 4 with Const

use of catdata.fql.decl.MapExp.Const in project fql by CategoricalData.

the class SigOps method prod.

private static 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>>> prod(SigExp.Const a, SigExp.Const b) {
    int node_count = 0;
    Map<Pair<String, String>, String> node_map = new LinkedHashMap<>();
    // fst
    List<Pair<String, String>> a_objs = new LinkedList<>();
    // snd
    List<Pair<String, String>> b_objs = new LinkedList<>();
    for (String n : a.nodes) {
        for (String m : b.nodes) {
            node_map.put(new Pair<>(n, m), "node" + node_count);
            a_objs.add(new Pair<>("node" + node_count, n));
            b_objs.add(new Pair<>("node" + node_count, m));
            node_count++;
        }
    }
    List<String> nodes = new LinkedList<>();
    nodes.addAll(node_map.values());
    int attr_count = 0;
    Map<Pair<String, String>, String> attr_map = new LinkedHashMap<>();
    // fst
    List<Pair<String, String>> a_attrs = new LinkedList<>();
    // snd
    List<Pair<String, String>> b_attrs = new LinkedList<>();
    List<Triple<String, String, String>> attrs = new LinkedList<>();
    for (Triple<String, String, String> n : a.attrs) {
        for (Triple<String, String, String> m : b.attrs) {
            if (!n.third.equals(m.third)) {
                continue;
            }
            String k = node_map.get(new Pair<>(n.second, m.second));
            attrs.add(new Triple<>("attr" + attr_count, k, n.third));
            attr_map.put(new Pair<>(n.first, m.first), "attr" + attr_count);
            a_attrs.add(new Pair<>("attr" + attr_count, n.first));
            b_attrs.add(new Pair<>("attr" + attr_count, m.first));
            attr_count++;
        }
    }
    int edge_count = 0;
    Map<Pair<String, String>, String> edge_map_1 = new LinkedHashMap<>();
    Map<Pair<String, String>, String> edge_map_2 = new LinkedHashMap<>();
    // fst
    List<Pair<String, List<String>>> a_edges = new LinkedList<>();
    // snd
    List<Pair<String, List<String>>> b_edges = new LinkedList<>();
    List<Triple<String, String, String>> edges = new LinkedList<>();
    for (Triple<String, String, String> n : a.arrows) {
        for (String m : b.nodes) {
            String k1 = node_map.get(new Pair<>(n.second, m));
            String k2 = node_map.get(new Pair<>(n.third, m));
            edges.add(new Triple<>("edge" + edge_count, k1, k2));
            edge_map_1.put(new Pair<>(n.first, m), "edge" + edge_count);
            List<String> al = new LinkedList<>();
            al.add(n.second);
            al.add(n.first);
            a_edges.add(new Pair<>("edge" + edge_count, al));
            List<String> bl = new LinkedList<>();
            bl.add(m);
            b_edges.add(new Pair<>("edge" + edge_count, bl));
            edge_count++;
        }
    }
    for (Triple<String, String, String> n : b.arrows) {
        for (String m : a.nodes) {
            String k1 = node_map.get(new Pair<>(m, n.second));
            String k2 = node_map.get(new Pair<>(m, n.third));
            edges.add(new Triple<>("edge" + edge_count, k1, k2));
            edge_map_2.put(new Pair<>(n.first, m), "edge" + edge_count);
            List<String> al = new LinkedList<>();
            al.add(n.second);
            al.add(n.first);
            b_edges.add(new Pair<>("edge" + edge_count, al));
            List<String> bl = new LinkedList<>();
            bl.add(m);
            a_edges.add(new Pair<>("edge" + edge_count, bl));
            edge_count++;
        }
    }
    List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    for (Triple<String, String, String> n : a.arrows) {
        for (Triple<String, String, String> m : b.arrows) {
            List<String> lhs = new LinkedList<>();
            List<String> rhs = new LinkedList<>();
            String src = node_map.get(new Pair<>(n.second, m.second));
            String lhs1 = edge_map_1.get(new Pair<>(n.first, m.second));
            String lhs2 = edge_map_2.get(new Pair<>(m.first, n.third));
            lhs.add(src);
            lhs.add(lhs1);
            lhs.add(lhs2);
            String rhs1 = edge_map_2.get(new Pair<>(m.first, n.second));
            String rhs2 = edge_map_1.get(new Pair<>(n.first, m.third));
            rhs.add(src);
            rhs.add(rhs1);
            rhs.add(rhs2);
            eqs.add(new Pair<>(lhs, rhs));
        }
    }
    for (Pair<List<String>, List<String>> eqA : a.eqs) {
        for (String srcB : b.nodes) {
            List<String> lhsA = new LinkedList<>(eqA.first);
            List<String> rhsA = new LinkedList<>(eqA.second);
            String srcA = lhsA.remove(0);
            rhsA.remove(0);
            String src = node_map.get(new Pair<>(srcA, srcB));
            List<String> lhs = new LinkedList<>();
            List<String> rhs = new LinkedList<>();
            lhs.add(src);
            rhs.add(src);
            for (String k : lhsA) {
                lhs.add(edge_map_1.get(new Pair<>(k, srcB)));
            }
            for (String k : rhsA) {
                rhs.add(edge_map_1.get(new Pair<>(k, srcB)));
            }
            eqs.add(new Pair<>(lhs, rhs));
        }
    }
    for (Pair<List<String>, List<String>> eqA : b.eqs) {
        for (String srcB : a.nodes) {
            List<String> lhsA = new LinkedList<>(eqA.first);
            List<String> rhsA = new LinkedList<>(eqA.second);
            String srcA = lhsA.remove(0);
            rhsA.remove(0);
            String src = node_map.get(new Pair<>(srcB, srcA));
            List<String> lhs = new LinkedList<>();
            List<String> rhs = new LinkedList<>();
            lhs.add(src);
            rhs.add(src);
            for (String k : lhsA) {
                lhs.add(edge_map_2.get(new Pair<>(k, srcB)));
            }
            for (String k : rhsA) {
                rhs.add(edge_map_2.get(new Pair<>(k, srcB)));
            }
            eqs.add(new Pair<>(lhs, rhs));
        }
    }
    SigExp.Const sig = new SigExp.Const(nodes, attrs, edges, eqs);
    Const fst = new Const(a_objs, a_attrs, a_edges, sig, a);
    Const snd = new Const(b_objs, b_attrs, b_edges, sig, b);
    Fn<Triple<SigExp.Const, Const, Const>, Const> pair = x -> {
        SigExp.Const c = x.first;
        Const f = x.second;
        Const g = x.third;
        if (!f.src.equals(g.src)) {
            throw new RuntimeException("Sources don't agree: " + f.src + " and " + g.src);
        }
        if (!f.dst.equals(a)) {
            throw new RuntimeException("Target of " + f + " is not " + a);
        }
        if (!g.dst.equals(b)) {
            throw new RuntimeException("Target of " + g + "is not " + b);
        }
        List<Pair<String, String>> objs = new LinkedList<>();
        for (String obj_c : c.nodes) {
            objs.add(new Pair<>(obj_c, node_map.get(new Pair<>(lookup(obj_c, f.objs), lookup(obj_c, g.objs)))));
        }
        List<Pair<String, String>> attrs1 = new LinkedList<>();
        for (Triple<String, String, String> attr_c : c.attrs) {
            attrs1.add(new Pair<>(attr_c.first, attr_map.get(new Pair<>(lookup(attr_c.first, f.attrs), lookup(attr_c.first, g.attrs)))));
        }
        List<Pair<String, List<String>>> arrows = new LinkedList<>();
        for (Triple<String, String, String> edge_c : c.arrows) {
            List<String> fc = lookup(edge_c.first, f.arrows);
            List<String> gc = lookup(edge_c.first, g.arrows);
            List<String> ret = new LinkedList<>();
            String fcN = fc.get(0);
            String gcN = gc.get(0);
            String node_start = node_map.get(new Pair<>(fcN, gcN));
            ret.add(node_start);
            for (int i = 1; i < fc.size(); i++) {
                String fcE = fc.get(i);
                Pair<String, String> p = new Pair<>(fcE, gcN);
                String v = edge_map_1.get(p);
                ret.add(v);
            }
            node_start = lookup(edge_c.third, f.objs);
            for (int i = 1; i < gc.size(); i++) {
                String gcE = gc.get(i);
                Pair<String, String> p = new Pair<>(gcE, node_start);
                String v = edge_map_2.get(p);
                ret.add(v);
            }
            arrows.add(new Pair<>(edge_c.first, ret));
        }
        Const ret = new Const(objs, attrs1, arrows, c, sig);
        return ret;
    };
    return new Pair<>(new Quad<>(sig, fst, snd, pair), new Quad<>(node_map, attr_map, edge_map_1, edge_map_2));
}
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) 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 5 with Const

use of catdata.fql.decl.MapExp.Const in project fql by CategoricalData.

the class SigOps method visit.

@Override
public Const visit(FQLProgram env, Comp e) {
    Const a = e.l.toConst(env);
    Const b = e.r.toConst(env);
    if (!a.dst.equals(b.src)) {
        throw new RuntimeException();
    }
    List<Pair<String, String>> objs = new LinkedList<>();
    for (Pair<String, String> x : a.objs) {
        objs.add(new Pair<>(x.first, lookup(x.second, b.objs)));
    }
    List<Pair<String, String>> attrs = new LinkedList<>();
    for (Pair<String, String> x : a.attrs) {
        attrs.add(new Pair<>(x.first, lookup(x.second, b.attrs)));
    }
    List<Pair<String, List<String>>> arrows = new LinkedList<>();
    for (Pair<String, List<String>> x : a.arrows) {
        String n = lookup(x.second.get(0), b.objs);
        List<String> l = new LinkedList<>();
        l.add(n);
        for (int i = 1; i < x.second.size(); i++) {
            List<String> p = lookup(x.second.get(i), b.arrows);
            l.addAll(p.subList(1, p.size()));
        }
        arrows.add(new Pair<>(x.first, l));
    }
    return new Const(objs, attrs, arrows, a.src, b.dst);
}
Also used : Const(catdata.fql.decl.MapExp.Const) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair)

Aggregations

Const (catdata.fql.decl.MapExp.Const)8 Pair (catdata.Pair)6 LinkedList (java.util.LinkedList)6 List (java.util.List)5 Triple (catdata.Triple)4 Quad (catdata.Quad)3 FQLException (catdata.fql.FQLException)3 Arr (catdata.fql.cat.Arr)3 Times (catdata.fql.decl.SigExp.Times)3 HashMap (java.util.HashMap)3 Fn (catdata.fql.Fn)2 FinCat (catdata.fql.cat.FinCat)2 Inst (catdata.fql.cat.Inst)2 Apply (catdata.fql.decl.MapExp.Apply)2 Case (catdata.fql.decl.MapExp.Case)2 Comp (catdata.fql.decl.MapExp.Comp)2 Curry (catdata.fql.decl.MapExp.Curry)2 Dist1 (catdata.fql.decl.MapExp.Dist1)2 Dist2 (catdata.fql.decl.MapExp.Dist2)2 FF (catdata.fql.decl.MapExp.FF)2