Search in sources :

Example 36 with FQLException

use of catdata.fql.FQLException 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 37 with FQLException

use of catdata.fql.FQLException in project fql by CategoricalData.

the class LeftKanCat method helper.

private static Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> helper(LeftKan lk, Signature B) throws FQLException {
    List<Node> objects = B.nodes;
    Set<Arr<Node, Path>> arrows = new HashSet<>();
    Map<Node, Arr<Node, Path>> identities = new HashMap<>();
    Fn<Path, Integer> fn = makeFn(lk);
    List<Path> paths = new LinkedList<>();
    Map<Integer, Path> fn2 = new HashMap<>();
    int numarrs = numarrs(lk);
    for (Node n : B.nodes) {
        paths.add(new Path(B, n));
    }
    outer: for (int iter = 0; iter < DefunctGlobalOptions.debug.fql.MAX_PATH_LENGTH; iter++) {
        for (Path p : paths) {
            Integer i = fn.of(p);
            fn2.putIfAbsent(i, p);
            if (fn2.size() == numarrs) {
                break outer;
            }
        }
        List<Path> paths0 = new LinkedList<>();
        for (Path p : paths) {
            for (Edge e : B.outEdges(p.target)) {
                paths0.add(Path.append(B, p, new Path(B, e)));
            }
        }
        paths = paths0;
    }
    if (fn2.size() < numarrs) {
        String old_str = "Basis path lengths exceed allowed limit (" + DefunctGlobalOptions.debug.fql.MAX_PATH_LENGTH + ").  Only have " + fn2.size() + " basis paths out of required " + numarrs + "." + "  Probable cause: using parallel or hybrid left-kan algorithm (see options).";
        throw new FQLException(old_str);
    }
    for (Integer i : fn2.keySet()) {
        Path p = fn2.get(i);
        arrows.add(new Arr<>(p, p.source, p.target));
    }
    for (Node n : objects) {
        Arr<Node, Path> a = new Arr<>(fn2.get(getOne(lk.ua.get(n)).second), n, n);
        identities.put(n, a);
        for (Pair<Integer, Integer> i : lk.Pb.get(n)) {
            Path p = fn2.get(i.first);
            arrows.add(new Arr<>(p, p.source, p.target));
        }
    }
    Fn<Path, Arr<Node, Path>> r2 = (Path x) -> {
        if (fn2.get(fn.of(x)) == null) {
            throw new RuntimeException("Given path " + x + ", transforms to " + fn.of(x) + ", which is not in " + fn2);
        }
        return new Arr<>(fn2.get(fn.of(x)), x.source, x.target);
    };
    Map<Pair<Arr<Node, Path>, Arr<Node, Path>>, Arr<Node, Path>> composition = new HashMap<>();
    for (Arr<Node, Path> x : arrows) {
        for (Arr<Node, Path> y : arrows) {
            if (x.dst.equals(y.src)) {
                composition.put(new Pair<>(x, y), r2.of(Path.append(B, x.arr, y.arr)));
            }
        }
    }
    FinCat<Node, Path> r1 = new FinCat<>(objects, new LinkedList<>(arrows), composition, identities);
    return new Pair<>(r1, r2);
}
Also used : HashMap(java.util.HashMap) Node(catdata.fql.decl.Node) FQLException(catdata.fql.FQLException) List(java.util.List) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Pair(catdata.Pair) Path(catdata.fql.decl.Path) LinkedList(java.util.LinkedList) Edge(catdata.fql.decl.Edge)

Aggregations

FQLException (catdata.fql.FQLException)37 Pair (catdata.Pair)28 LinkedList (java.util.LinkedList)23 HashMap (java.util.HashMap)21 Node (catdata.fql.decl.Node)20 LinkedHashMap (java.util.LinkedHashMap)20 Arr (catdata.fql.cat.Arr)15 Map (java.util.Map)15 Instance (catdata.fql.decl.Instance)14 Path (catdata.fql.decl.Path)14 Transform (catdata.fql.decl.Transform)12 Attribute (catdata.fql.decl.Attribute)11 Edge (catdata.fql.decl.Edge)11 Signature (catdata.fql.decl.Signature)11 List (java.util.List)11 Triple (catdata.Triple)9 HashSet (java.util.HashSet)6 Fn (catdata.fql.Fn)5 CopyFlower (catdata.fql.sql.CopyFlower)5 ExpPSM (catdata.fql.sql.ExpPSM)5