Search in sources :

Example 6 with Const

use of catdata.fql.decl.MapExp.Const 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 7 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, 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)

Example 8 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, MapExp.Opposite e) {
    Const k = e.e.accept(env, this);
    Pair<SigExp, SigExp> p = k.type(env);
    List<Pair<String, List<String>>> arrows = new LinkedList<>();
    SigExp.Const yyy = p.second.toConst(env);
    for (Pair<String, List<String>> arrow : k.arrows) {
        List<String> xxx = new LinkedList<>(arrow.second);
        xxx.remove(0);
        Collections.reverse(xxx);
        if (xxx.isEmpty()) {
            xxx.add(arrow.second.get(0));
        } else {
            String v = lookup(yyy.arrows, xxx.get(0));
            xxx.add(0, v);
        }
        arrows.add(new Pair<>(arrow.first, xxx));
    }
    return new Const(new LinkedList<>(k.objs), new LinkedList<>(k.attrs), arrows, p.first.toConst(env), yyy);
}
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