Search in sources :

Example 1 with Fst

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

the class FQLParser method toMapping.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static MapExp toMapping(Object o) {
    try {
        Tuple3 p = (Tuple3) o;
        Object p2 = p.b;
        Object p3 = p.c;
        Object o1 = p.a;
        String p1 = p.a.toString();
        if (p1.equals("fst")) {
            return new Fst(toSchema(p2), toSchema(p3));
        } else if (p1.equals("snd")) {
            return new Snd(toSchema(p2), toSchema(p3));
        } else if (p1.equals("unit")) {
            return new TT(toSchema(p3), new HashSet<>((Collection<String>) p2));
        } else if (p1.equals("subschema")) {
            return new Sub(toSchema(p2), toSchema(p3));
        } else if (p1.equals("inl")) {
            return new Inl(toSchema(p2), toSchema(p3));
        } else if (p1.equals("inr")) {
            return new Inr(toSchema(p2), toSchema(p3));
        } else if (p1.equals("iso1")) {
            return new Iso(true, toSchema(p2), toSchema(p3));
        } else if (p1.equals("iso2")) {
            return new Iso(false, toSchema(p2), toSchema(p3));
        } else if (p1.equals("eval")) {
            return new Apply(toSchema(p2), toSchema(p3));
        } else if (p2.toString().equals("then")) {
            return new MapExp.Comp(toMapping(o1), toMapping(p3));
        } else if (p2.toString().equals("*")) {
            return new Prod(toMapping(o1), toMapping(p3));
        } else if (p2.toString().equals("+")) {
            return new Case(toMapping(o1), toMapping(p3));
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof Tuple5) {
        Tuple5 p = (Tuple5) o;
        Object p2 = p.c;
        Object p3 = p.e;
        Object o1 = p.a;
        return toMapConst(o1, toSchema(p2), toSchema(p3));
    }
    try {
        org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) o;
        String p1 = p.a.toString();
        Object p2 = p.b;
        switch(p1) {
            case "id":
                return new Id(toSchema(p2));
            case "curry":
                return new Curry(toMapping(p2));
            case "void":
                return new FF(toSchema(p2));
            case "opposite":
                return new Opposite(toMapping(p2));
            default:
                break;
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof String) {
        return new MapExp.Var(o.toString());
    }
    throw new RuntimeException("Cannot parse " + o);
}
Also used : TT(catdata.fql.decl.MapExp.TT) TransCurry(catdata.fql.decl.TransExp.TransCurry) Curry(catdata.fql.decl.MapExp.Curry) FF(catdata.fql.decl.MapExp.FF) Inl(catdata.fql.decl.MapExp.Inl) Apply(catdata.fql.decl.MapExp.Apply) Var(catdata.fql.decl.FullQueryExp.Var) Snd(catdata.fql.decl.MapExp.Snd) Inr(catdata.fql.decl.MapExp.Inr) Case(catdata.fql.decl.MapExp.Case) Prod(catdata.fql.decl.MapExp.Prod) Pair(catdata.Pair) Opposite(catdata.fql.decl.MapExp.Opposite) Sub(catdata.fql.decl.MapExp.Sub) TransIso(catdata.fql.decl.TransExp.TransIso) Iso(catdata.fql.decl.MapExp.Iso) Fst(catdata.fql.decl.MapExp.Fst) MapExp(catdata.fql.decl.MapExp) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) Id(catdata.fql.decl.MapExp.Id)

Example 2 with Fst

use of catdata.fql.decl.MapExp.Fst 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 3 with Fst

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

the class FQLParser method toTrans.

@SuppressWarnings("rawtypes")
private static TransExp toTrans(Object o) {
    try {
        Tuple4 p = (Tuple4) o;
        String src = p.b.toString();
        String dst = p.c.toString();
        TransExp h = toTrans(p.d);
        String kind = p.a.toString();
        switch(kind) {
            case "delta":
                return new TransExp.Delta(h, src, dst);
            case "pi":
                return new TransExp.Pi(h, src, dst);
            case "sigma":
                return new TransExp.Sigma(h, src, dst);
            case "relationalize":
                return new TransExp.Relationalize(h, src, dst);
            default:
                throw new RuntimeException(o.toString());
        }
    } catch (RuntimeException ex) {
    }
    try {
        Tuple4 p = (Tuple4) o;
        String src = p.b.toString();
        String dst = p.c.toString();
        String name = p.d.toString();
        String kind = p.a.toString();
        switch(kind) {
            case "external":
                return new TransExp.External(src, dst, name);
            case "SIGMA":
                return new TransExp.FullSigma(name, src, dst);
            default:
                throw new RuntimeException(o.toString());
        }
    } catch (RuntimeException ex) {
    }
    try {
        Tuple4 p = (Tuple4) o;
        String obj = p.a.toString();
        String dst = p.d.toString();
        if (p.c.toString().equals("void")) {
            return new TransExp.FF(obj, dst);
        } else if (p.c.toString().equals("unit")) {
            return new TransExp.TT(obj, dst);
        } else if (p.c.toString().equals("curry")) {
            return new TransCurry(obj, dst);
        } else if (p.c.toString().equals("true")) {
            return new Bool(true, dst, obj);
        } else if (p.c.toString().equals("false")) {
            return new Bool(false, dst, obj);
        } else if (p.c.toString().equals("char")) {
            return new Chi(obj, dst);
        }
    } catch (RuntimeException re) {
    }
    try {
        Tuple3 p = (Tuple3) o;
        Object p2 = p.b;
        Object p3 = p.c;
        Object o1 = p.a;
        String p1 = p.a.toString();
        if (p1.equals("iso1")) {
            return new TransIso(true, p2.toString(), p3.toString());
        } else if (p1.equals("iso2")) {
            return new TransIso(false, p2.toString(), p3.toString());
        } else if (p3.toString().equals("fst")) {
            return new TransExp.Fst(p1);
        } else if (p3.toString().equals("not")) {
            return new Not(p1);
        } else if (p3.toString().equals("and")) {
            return new And(p1);
        } else if (p3.toString().equals("or")) {
            return new Or(p1);
        } else if (p3.toString().equals("implies")) {
            return new Implies(p1);
        } else if (p3.toString().equals("eval")) {
            return new TransEval(p1);
        } else if (p3.toString().equals("relationalize")) {
            return new Squash(p1);
        } else if (p3.toString().equals("snd")) {
            return new TransExp.Snd(p1);
        } else if (p3.toString().equals("return")) {
            return new Return(p1);
        } else if (p3.toString().equals("coreturn")) {
            return new Coreturn(p1);
        } else if (p3.toString().equals("inl")) {
            return new TransExp.Inl(p1);
        } else if (p3.toString().equals("kernel")) {
            return new UnChi(p1);
        } else if (p3.toString().equals("inr")) {
            return new TransExp.Inr(p1);
        } else if (p2.toString().equals("then")) {
            return new TransExp.Comp(toTrans(o1), toTrans(p3));
        } else if (p3 instanceof Tuple3) {
            Tuple3 y = (Tuple3) p3;
            String x = y.b.toString();
            switch(x) {
                case "+":
                    return new TransExp.Case(p1, toTrans(y.a), toTrans(y.c));
                case "*":
                    return new TransExp.Prod(p1, toTrans(y.a), toTrans(y.c));
                // return new TransExp.(p1, toTrans(y.a), toTrans(y.c));
                default:
                    throw new RuntimeException("foo");
            }
        }
    } catch (RuntimeException re) {
    }
    try {
        Tuple5 p = (Tuple5) o;
        Object p2 = p.c;
        Object p3 = p.e;
        Object o1 = p.a;
        return toTransConst(o1, p2.toString(), p3.toString());
    } catch (RuntimeException re) {
    }
    try {
        org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) o;
        String p1 = p.a.toString();
        Object p2 = p.b;
        if (p1.equals("id")) {
            return new TransExp.Id(p2.toString());
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof String) {
        return new TransExp.Var(o.toString());
    }
    throw new RuntimeException();
}
Also used : FF(catdata.fql.decl.MapExp.FF) Or(catdata.fql.decl.TransExp.Or) Chi(catdata.fql.decl.TransExp.Chi) UnChi(catdata.fql.decl.TransExp.UnChi) Inl(catdata.fql.decl.MapExp.Inl) Var(catdata.fql.decl.FullQueryExp.Var) TransExp(catdata.fql.decl.TransExp) Sigma(catdata.fql.decl.FullQueryExp.Sigma) FullSigma(catdata.fql.decl.InstExp.FullSigma) UnChi(catdata.fql.decl.TransExp.UnChi) Inr(catdata.fql.decl.MapExp.Inr) Squash(catdata.fql.decl.TransExp.Squash) Case(catdata.fql.decl.MapExp.Case) Prod(catdata.fql.decl.MapExp.Prod) Implies(catdata.fql.decl.TransExp.Implies) Bool(catdata.fql.decl.TransExp.Bool) Pi(catdata.fql.decl.FullQueryExp.Pi) Pair(catdata.Pair) Relationalize(catdata.fql.decl.InstExp.Relationalize) Return(catdata.fql.decl.TransExp.Return) TransEval(catdata.fql.decl.TransExp.TransEval) Fst(catdata.fql.decl.MapExp.Fst) TransCurry(catdata.fql.decl.TransExp.TransCurry) Coreturn(catdata.fql.decl.TransExp.Coreturn) TransIso(catdata.fql.decl.TransExp.TransIso) Tuple4(org.jparsec.functors.Tuple4) Tuple5(org.jparsec.functors.Tuple5) Not(catdata.fql.decl.TransExp.Not) Delta(catdata.fql.decl.FullQueryExp.Delta) And(catdata.fql.decl.TransExp.And) Tuple3(org.jparsec.functors.Tuple3) External(catdata.fql.decl.InstExp.External) FullSigma(catdata.fql.decl.InstExp.FullSigma) Id(catdata.fql.decl.MapExp.Id)

Aggregations

Pair (catdata.Pair)3 Case (catdata.fql.decl.MapExp.Case)3 FF (catdata.fql.decl.MapExp.FF)3 Fst (catdata.fql.decl.MapExp.Fst)3 Id (catdata.fql.decl.MapExp.Id)3 Inl (catdata.fql.decl.MapExp.Inl)3 Inr (catdata.fql.decl.MapExp.Inr)3 Prod (catdata.fql.decl.MapExp.Prod)3 Var (catdata.fql.decl.FullQueryExp.Var)2 Apply (catdata.fql.decl.MapExp.Apply)2 Curry (catdata.fql.decl.MapExp.Curry)2 Iso (catdata.fql.decl.MapExp.Iso)2 Snd (catdata.fql.decl.MapExp.Snd)2 Sub (catdata.fql.decl.MapExp.Sub)2 TT (catdata.fql.decl.MapExp.TT)2 TransCurry (catdata.fql.decl.TransExp.TransCurry)2 Quad (catdata.Quad)1 Triple (catdata.Triple)1 FQLException (catdata.fql.FQLException)1 Fn (catdata.fql.Fn)1