use of catdata.fql.decl.MapExp.Prod 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);
}
use of catdata.fql.decl.MapExp.Prod 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));
}
Aggregations