use of catdata.Chc in project fql by CategoricalData.
the class CatOps method visit.
@Override
public Transform visit(FQLPPProgram env, ToSet e) {
Functor s = e.src.accept(env, this);
Functor t = e.dst.accept(env, this);
FUNCTION o = x -> {
Node n = (Node) x;
Chc<FnExp, SetExp> chc = e.fun.get(n.name);
if (chc == null) {
throw new RuntimeException("Missing object mapping for: " + n.name);
}
if (chc.left) {
return chc.l.accept(env, new SetOps(ENV));
} else {
Set src = (Set) s.applyO(n);
Set dst = (Set) t.applyO(n);
Set<Pair> p = (Set<Pair>) chc.r.accept(env, new SetOps(ENV));
Map<Object, Object> map = new HashMap<>();
for (Pair h : p) {
if (map.containsKey(h.first)) {
throw new RuntimeException("Duplicate arg: " + e);
}
map.put(h.first, h.second);
}
return new Fn(src, dst, map::get);
}
};
return new Transform(s, t, o);
}
use of catdata.Chc in project fql by CategoricalData.
the class PPParser method toInstConst.
// @SuppressWarnings("rawtypes")
private static FunctorExp toInstConst(Object decl) {
Tuple3 y = (Tuple3) decl;
org.jparsec.functors.Pair x = (org.jparsec.functors.Pair) y.a;
Tuple3 nodes = (Tuple3) x.a;
Tuple3 arrows = (Tuple3) x.b;
List nodes0 = (List) nodes.b;
List arrows0 = (List) arrows.b;
Map<String, SetExp> nodesX = new HashMap<>();
for (Object o : nodes0) {
if (nodesX.containsKey(o)) {
throw new RuntimeException("Duplicate object: " + o + " in " + decl);
}
Tuple3 u = (Tuple3) o;
String n = (String) u.a;
SetExp l = toSet(u.c);
nodesX.put(n, l);
}
Map<String, Chc<FnExp, SetExp>> arrowsX = new HashMap<>();
for (Object o : arrows0) {
if (arrowsX.containsKey(o)) {
throw new RuntimeException("Duplicate arrow: " + o + " in " + decl);
}
Tuple3 u = (Tuple3) o;
String n = (String) u.a;
try {
FnExp l = toFn(u.c);
arrowsX.put(n, Chc.inLeft(l));
} catch (Exception eee) {
SetExp l = toSet(u.c);
arrowsX.put(n, Chc.inRight(l));
}
}
InstConst ret = new InstConst(toCat(y.c), nodesX, arrowsX);
return ret;
}
use of catdata.Chc in project fql by CategoricalData.
the class OplOps method visit.
@Override
public OplObject visit(Program<OplExp> env, OplColim e) {
OplObject o = ENV.get(e.name);
if (!(o instanceof OplGraph)) {
throw new RuntimeException("Not a graph: " + e.name);
}
OplGraph<String, String> shape = (OplGraph<String, String>) o;
OplObject base0 = ENV.get(e.base);
String typeSide;
if (base0 instanceof OplSig) {
typeSide = e.base;
OplUnion u0 = new OplUnion(new LinkedList<>(shape.nodes), typeSide);
OplObject u1 = u0.accept(env, this);
OplSchema<String, String, String> u = (OplSchema<String, String, String>) u1;
Map<String, Set<String>> equivs = new HashMap<>();
Map<String, String> equivs0 = new HashMap<>();
for (String schname : shape.nodes) {
OplSchema<String, String, String> sch = (OplSchema<String, String, String>) ENV.get(schname);
for (String ename : sch.entities) {
Set<String> set = new HashSet<>();
set.add(schname + "_" + ename);
equivs.put(schname + "_" + ename, set);
}
}
// : type check colimit
for (String mname : shape.edges.keySet()) {
Pair<String, String> mt = shape.edges.get(mname);
String s = mt.first;
String t = mt.second;
OplSchema<String, String, String> s0 = (OplSchema<String, String, String>) ENV.get(s);
OplTyMapping<String, String, String, String, String> m0 = (OplTyMapping<String, String, String, String, String>) ENV.get(mname);
if (!m0.src0.equals(s)) {
throw new RuntimeException("Source of " + m0 + " is " + m0.src + " and not " + s + "as expected");
}
if (!m0.dst0.equals(t)) {
throw new RuntimeException("Target of " + m0 + " is " + m0.dst + " and not " + t + "as expected");
}
for (String ob : s0.entities) {
String ob0 = m0.m.sorts.get(ob);
Set<String> set1 = equivs.get(s + "_" + ob);
Set<String> set2 = equivs.get(t + "_" + ob0);
set1.addAll(set2);
equivs.put(s + "_" + ob, set1);
equivs.put(t + "_" + ob0, set1);
}
}
for (String k : equivs.keySet()) {
List<String> v = new LinkedList<>(equivs.get(k));
v.sort(String.CASE_INSENSITIVE_ORDER);
equivs0.put(k, Util.sep(v, "__"));
}
Set<String> entities = new HashSet<>(equivs0.values());
Map<String, Pair<List<String>, String>> edges = new HashMap<>();
Map<String, Pair<List<String>, String>> attrs = new HashMap<>();
List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> pathEqs = new LinkedList<>();
List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> obsEqs = new LinkedList<>();
Function<String, String> fun = x -> {
if (equivs0.containsKey(x)) {
return equivs0.get(x);
}
return x;
};
for (String edge : u.toSchema0().edges.keySet()) {
Pair<List<String>, String> ty = u.toSchema0().edges.get(edge);
edges.put(edge, new Pair<>(ty.first.stream().map(fun).collect(Collectors.toList()), fun.apply(ty.second)));
}
for (String attr : u.toSchema0().attrs.keySet()) {
Pair<List<String>, String> ty = u.toSchema0().attrs.get(attr);
attrs.put(attr, new Pair<>(ty.first.stream().map(fun).collect(Collectors.toList()), fun.apply(ty.second)));
}
for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> eq : u.toSchema0().pathEqs) {
OplCtx<String, String> ctx = new OplCtx<>(eq.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(x.second))).collect(Collectors.toList()));
pathEqs.add(new Triple<>(ctx, fun2(equivs0, eq.second), fun2(equivs0, eq.third)));
}
for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> eq : u.toSchema0().obsEqs) {
OplCtx<String, String> ctx = new OplCtx<>(eq.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(x.second))).collect(Collectors.toList()));
obsEqs.add(new Triple<>(ctx, fun2(equivs0, eq.second), fun2(equivs0, eq.third)));
}
for (String mname : shape.edges.keySet()) {
Pair<String, String> mt = shape.edges.get(mname);
String s = mt.first;
String t = mt.second;
OplSchema<String, String, String> s0 = (OplSchema<String, String, String>) ENV.get(s);
// OplSchema<String, String, String> t0 = (OplSchema<String,
// String, String>) ENV.get(t);
OplTyMapping<String, String, String, String, String> m0 = (OplTyMapping<String, String, String, String, String>) ENV.get(mname);
for (String edge : s0.projE().symbols.keySet()) {
Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.m.symbols.get(edge);
List<OplTerm<String, String>> args = edge2.first.vars0.keySet().stream().map((Function<String, OplTerm<String, String>>) OplTerm::new).collect(Collectors.toList());
OplTerm<String, String> lhs = fun2(equivs0, new OplTerm<>(s + "_" + edge, args));
OplCtx<String, String> ctx = new OplCtx<>(edge2.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(s + "_" + x.second))).collect(Collectors.toList()));
OplTerm<String, String> rhs = fun2(equivs0, prepend(t, edge2.second));
pathEqs.add(new Triple<>(ctx, lhs, rhs));
}
for (String edge : s0.projA().symbols.keySet()) {
Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.m.symbols.get(edge);
List<OplTerm<String, String>> args = edge2.first.vars0.keySet().stream().map((Function<String, OplTerm<String, String>>) OplTerm::new).collect(Collectors.toList());
OplTerm<String, String> lhs = fun2(equivs0, new OplTerm<>(s + "_" + edge, args));
OplCtx<String, String> ctx = new OplCtx<>(edge2.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(s + "_" + x.second))).collect(Collectors.toList()));
OplTerm<String, String> rhs = fun2(equivs0, prepend(t, edge2.second));
obsEqs.add(new Triple<>(ctx, lhs, rhs));
}
}
OplSCHEMA0<String, String, String> ret = new OplSCHEMA0<>(new HashMap<>(), entities, edges, attrs, pathEqs, obsEqs, typeSide);
OplSchema retsch = (OplSchema) ret.accept(env, this);
e.compiled.put("Colimit", ret);
for (String schname : shape.nodes) {
OplSchema<String, String, String> sch = (OplSchema<String, String, String>) ENV.get(schname);
Map<String, String> inj_sorts = new HashMap<>();
Map<String, Pair<OplCtx<String, String>, OplTerm<String, String>>> inj_symbols = new HashMap<>();
for (String ename : sch.entities) {
inj_sorts.put(ename, fun.apply(schname + "_" + ename));
}
for (String c1 : sch.projEA().symbols.keySet()) {
Pair<List<String>, String> t = sch.projEA().symbols.get(c1);
List<Pair<String, String>> l = new LinkedList<>();
List<OplTerm<String, String>> vs = new LinkedList<>();
for (String s1 : t.first) {
String v = (String) retsch.sig.fr.next();
vs.add(new OplTerm<>(v));
l.add(new Pair<>(v, fun.apply(schname + "_" + s1)));
}
OplCtx<String, String> ctx = new OplCtx<>(l);
OplTerm<String, String> value = fun2(equivs0, new OplTerm<>(schname + "_" + c1, vs));
inj_symbols.put(c1, new Pair<>(ctx, value));
}
OplMapping<String, String, String, String, String> mapping = new OplMapping<>(inj_sorts, inj_symbols, schname, "Colimit");
// : name of colimit
OplTyMapping<String, String, String, String, String> tm = new OplTyMapping<>(schname, "Colimit", sch, retsch, mapping);
tm.extend().validate(sch.sig, retsch.sig);
e.compiled.put(schname + "To" + "Colimit", mapping);
}
return e;
} else if (base0 instanceof OplSchema) {
OplSchema sch2 = (OplSchema) base0;
// OplSCHEMA0 sch = sch2.toSchema0();
List<Pair<OplTerm<Chc<String, String>, String>, OplTerm<Chc<String, String>, String>>> equations = new LinkedList<>();
// List<Pair<OplTerm<Object, String>, OplTerm<Object, String>>> equations1 = new LinkedList<>();
Map<String, String> gens = new HashMap<>();
Map<String, Integer> prec = new HashMap<>();
for (String s : shape.nodes) {
OplInst<String, String, String, String> toAdd = (OplInst<String, String, String, String>) ENV.get(s);
for (Object gen : toAdd.P.gens.keySet()) {
Object ty = toAdd.P.gens.get(gen);
gens.put(s + "_" + gen, ty.toString());
}
for (Object gen : toAdd.P.prec.keySet()) {
if (toAdd.P.gens.keySet().contains(gen)) {
if (toAdd.P.prec.containsKey(gen)) {
prec.put(s + "_" + gen, toAdd.P.prec.get(gen));
}
} else {
if (toAdd.P.prec.containsKey(gen)) {
prec.put(toAdd.P.S + "_" + gen, toAdd.P.prec.get(gen));
}
}
}
for (Pair<OplTerm<Chc<String, String>, String>, OplTerm<Chc<String, String>, String>> tr : toAdd.P.equations) {
OplTerm lhs1 = prepend3(s, tr.first);
OplTerm rhs1 = prepend3(s, tr.second);
equations.add(new Pair<>(lhs1, rhs1));
}
}
for (String mname : shape.edges.keySet()) {
Pair<String, String> mt = shape.edges.get(mname);
String s = mt.first;
String t = mt.second;
OplInst<String, String, String, String> s0 = (OplInst<String, String, String, String>) ENV.get(s);
// OplSchema<String, String, String> t0 = (OplSchema<String,
// String, String>) ENV.get(t);
OplPresTrans<String, String, String, String, String> m0 = (OplPresTrans<String, String, String, String, String>) ENV.get(mname);
for (String edge : s0.projE().gens.keySet()) {
Pair<OplCtx<String, String>, OplTerm<Chc<String, String>, String>> edge2 = m0.mapping.symbols.get(Chc.inRight(edge));
OplTerm<Chc<String, String>, String> lhs = new OplTerm<>(Chc.inRight(s + "_" + edge), new LinkedList<>());
equations.add(new Pair<>(lhs, prepend3(t, edge2.second)));
}
}
OplPres<String, String, String, String> pres = new OplPres<>(prec, e.base, sch2.sig, gens, equations);
OplInst<String, String, String, String> colimInst = new OplInst<>(e.base, "?", null);
colimInst.validate(sch2, pres, null);
e.compiled.put("ColimitInstance", colimInst);
for (String s : shape.nodes) {
OplInst<String, String, String, String> toAdd = (OplInst<String, String, String, String>) ENV.get(s);
Map<String, Map<String, OplTerm<Chc<String, String>, String>>> inj_map = new HashMap<>();
for (String entity : toAdd.S.entities) {
inj_map.put(entity, new HashMap<>());
}
for (Object gen : toAdd.P.gens.keySet()) {
Object ty = toAdd.P.gens.get(gen);
gens.put(s + "_" + gen, ty.toString());
Map<String, OplTerm<Chc<String, String>, String>> m = inj_map.get(ty);
OplTerm<Chc<String, String>, String> term = new OplTerm<>(Chc.inRight(s + "_" + gen), new LinkedList<>());
m.put((String) gen, term);
}
// OplPresTrans
OplPresTrans<String, String, String, String, String> inj = new OplPresTrans<>(inj_map, s, "ColimitInstance", toAdd.P, pres);
e.compiled.put(s + "ToColimitInstance", inj);
}
return colimInst;
}
throw new RuntimeException("Report your program to Ryan");
}
use of catdata.Chc in project fql by CategoricalData.
the class OplParser method toPresentation.
private static OplPres toPresentation(Object o) {
Tuple4 t = (Tuple4) o;
Tuple3 e = (Tuple3) t.b;
// List<String> imports = e.a == null ? new LinkedList<>() : (List<String>) ((Tuple3)e.a).b;
String yyy = (String) t.d;
org.jparsec.functors.Pair b = (org.jparsec.functors.Pair) e.b;
org.jparsec.functors.Pair c = (org.jparsec.functors.Pair) e.c;
List<Tuple3> symbols0 = (List<Tuple3>) b.b;
List<Tuple4> equations0 = (List<Tuple4>) c.b;
Map<String, String> symbols = new HashMap<>();
Map<String, Integer> prec = new HashMap<>();
for (Tuple3 x : symbols0) {
String dom = (String) x.c;
List<org.jparsec.functors.Pair> name0s = (List<org.jparsec.functors.Pair>) x.a;
for (org.jparsec.functors.Pair name0 : name0s) {
String name = (String) name0.a;
if (name0.b != null) {
org.jparsec.functors.Pair zzz = (org.jparsec.functors.Pair) name0.b;
Integer i = (Integer) zzz.b;
prec.put(name, i);
}
if (symbols.containsKey(name)) {
throw new DoNotIgnore("Duplicate symbol " + name);
}
symbols.put(name, dom);
}
}
List<Pair<OplTerm<Chc<String, String>, String>, OplTerm<Chc<String, String>, String>>> equations = new LinkedList<>();
for (org.jparsec.functors.Pair<Tuple3, Tuple3> x : equations0) {
if (x.a != null) {
throw new DoNotIgnore("Cannot have universally quantified equations in presentations");
}
List<Tuple3> fa = new LinkedList<>();
OplCtx<String, String> ctx = toCtx(fa);
Tuple3 eq = x.b;
OplTerm lhs = toTerm(ctx.names(), symbols.keySet(), eq.a, true);
OplTerm rhs = toTerm(ctx.names(), symbols.keySet(), eq.c, true);
equations.add(new Pair<>(lhs, rhs));
}
OplPres ret = new OplPres<>(prec, yyy, null, symbols, equations);
return ret;
}
use of catdata.Chc in project fql by CategoricalData.
the class OplParser method fromBlock.
private static Block<String, String, String, String, String, String> fromBlock(Object o) {
Tuple4<List, List, List, List> t = (Tuple4<List, List, List, List>) o;
LinkedHashMap<String, String> from = new LinkedHashMap<>();
Set<Pair<OplTerm<String, String>, OplTerm<String, String>>> where = new HashSet<>();
Map<String, Chc<Agg<String, String, String, String, String, String>, OplTerm<String, String>>> attrs = new HashMap<>();
Map<String, Pair<Object, Map<String, OplTerm<String, String>>>> edges = new HashMap<>();
for (Object x : t.a) {
Tuple3 l = (Tuple3) x;
if (from.containsKey(l.a.toString())) {
throw new RuntimeException("Duplicate for: " + l.a);
}
from.put(l.a.toString(), l.c.toString());
}
for (Object x : t.b) {
Tuple3 l = (Tuple3) x;
where.add(new Pair(toTerm(from.keySet(), null, l.a, true), toTerm(from.keySet(), null, l.c, true)));
}
for (Object x : t.c) {
Tuple3 l = (Tuple3) x;
if (attrs.containsKey(l.a.toString())) {
throw new RuntimeException("Duplicate for: " + l.a);
}
if (l.c.toString().contains("agg") && l.c.toString().contains("{") && l.c.toString().contains("return") && l.c.toString().contains("}")) {
attrs.put(l.a.toString(), Chc.inLeft(fromAgg(from.keySet(), null, l.c, true)));
} else {
attrs.put(l.a.toString(), Chc.inRight(toTerm(from.keySet(), null, l.c, true)));
}
}
for (Object x : t.d) {
Tuple5 l = (Tuple5) x;
if (from.containsKey(l.a.toString())) {
throw new RuntimeException("Duplicate for: " + l.a);
}
edges.put(l.a.toString(), new Pair(l.e.toString(), fromBlockHelper(from.keySet(), l.c)));
}
Block bl = new Block<>(from, where, attrs, edges);
return bl;
}
Aggregations