use of catdata.aql.exp.SchExpRaw.Fk in project fql by CategoricalData.
the class ColimitSchema method isoOneWay.
private void isoOneWay(Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> F, Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> G, String str) {
if (!F.dst.equals(G.src)) {
throw new RuntimeException("Target of " + F + " \n, namely " + F.dst + "\ndoes not match source of " + G + ", namely " + F.src + "\n" + str);
}
Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> f = Mapping.compose(F, G);
for (En en : f.src.ens) {
En en2 = f.ens.get(en);
if (!en.equals(en2)) {
throw new RuntimeException(en + " taken to " + en2 + ", rather than itself, " + str);
}
}
for (Fk fk : f.src.fks.keySet()) {
Pair<En, List<Fk>> fk2 = f.fks.get(fk);
Var v = new Var("v");
Term<Ty, En, Sym, Fk, Att, Void, Void> t = Term.Fks(fk2.second, Term.Var(v));
Term<Ty, En, Sym, Fk, Att, Void, Void> s = Term.Fk(fk, Term.Var(v));
boolean eq = F.src.dp.eq(new Ctx<>(new Pair<>(v, Chc.inRight(fk2.first))), s, t);
if (!eq) {
throw new RuntimeException(fk + " taken to " + t + ", which is not provably equal to itself, " + str);
}
}
for (Att att : f.src.atts.keySet()) {
Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>> att2 = f.atts.get(att);
Var v = att2.first;
// Term.Fks(att2.second, Term.Var(v));
Term<Ty, En, Sym, Fk, Att, Void, Void> t = att2.third;
Term<Ty, En, Sym, Fk, Att, Void, Void> s = Term.Att(att, Term.Var(v));
boolean eq = F.src.dp.eq(new Ctx<>(new Pair<>(v, Chc.inRight(att2.second))), s, t);
if (!eq) {
throw new RuntimeException(att + " taken to " + t + ", which is not provably equal to itself, " + str);
}
}
}
use of catdata.aql.exp.SchExpRaw.Fk in project fql by CategoricalData.
the class ColimitSchema method removeAtt.
public ColimitSchema<N> removeAtt(Att src, Var v, Term<Ty, En, Sym, Fk, Att, Void, Void> t, boolean checkJava) {
if (!schemaStr.atts.containsKey(src)) {
throw new RuntimeException(src + " is not an attribute in \n" + schemaStr);
}
En en1 = schemaStr.atts.get(src).first;
Ty ty0 = schemaStr.atts.get(src).second;
if (!schemaStr.type(new Pair<>(v, en1), t).equals(Chc.inLeft(ty0))) {
throw new RuntimeException("The term " + t + " has type " + schemaStr.type(new Pair<>(v, en1), t).toStringMash() + " and not " + ty0 + " as expected.");
}
if (!schemaStr.dp.eq(new Ctx<>(v, Chc.inRight(en1)), t, Term.Att(src, Term.Var(v)))) {
throw new RuntimeException("The term " + t + " is not provably equal to " + Term.Att(src, Term.Var(v)));
}
if (t.contains(Head.Att(src))) {
throw new RuntimeException("Cannot replace " + src + " with " + t + " because that term contains " + src);
}
Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoToUser = Mapping.id(schemaStr);
Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoFromUser = Mapping.id(schemaStr);
Map<Att, Pair<En, Ty>> atts = new HashMap<>(schemaStr.atts.map);
atts.remove(src);
Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : schemaStr.eqs) {
Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> tr = new Triple<>(eq.first, eq.second.replaceHead(Head.Att(src), Util.singList(v), t), eq.third.replaceHead(Head.Att(src), Util.singList(v), t));
if (!tr.second.equals(tr.third) && !eqs.contains(tr)) {
eqs.add(tr);
}
}
DP<Ty, En, Sym, Fk, Att, Void, Void> dp = new DP<Ty, En, Sym, Fk, Att, Void, Void>() {
@Override
public String toStringProver() {
return "remove attribute of " + schemaStr.dp.toStringProver();
}
@Override
public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Void, Void> lhs, Term<Ty, En, Sym, Fk, Att, Void, Void> rhs) {
return schemaStr.dp.eq(ctx, lhs, rhs);
}
};
Schema<Ty, En, Sym, Fk, Att> schemaStr2 = new Schema<>(ty, schemaStr.ens, atts, schemaStr.fks.map, eqs, dp, checkJava);
Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> attsM = new HashMap<>(isoToUser.atts.map);
attsM.put(src, new Triple<>(v, en1, t));
isoToUser = new Mapping<>(isoToUser.ens.map, attsM, isoToUser.fks.map, schemaStr, schemaStr2, checkJava);
Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> attsM2 = new HashMap<>(isoFromUser.atts.map);
attsM2.remove(src);
isoFromUser = new Mapping<>(isoFromUser.ens.map, attsM2, isoFromUser.fks.map, schemaStr2, schemaStr, checkJava);
return wrap(isoToUser, isoFromUser);
}
use of catdata.aql.exp.SchExpRaw.Fk in project fql by CategoricalData.
the class RawTerm method infer_good.
private static Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> infer_good(RawTerm e, Chc<Ty, En> expected, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col, String pre, AqlJs<Ty, Sym> js, Map<Var, Chc<Ty, En>> vars) {
if (e.annotation != null && !col.tys.contains(new Ty(e.annotation))) {
throw new RuntimeException(pre + "Annotation " + e.annotation + " is not a type (" + col.tys + ").");
}
Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> ret = new HashSet<>();
if (vars.keySet().contains(new Var((String) e.head)) && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Var(new Var((String) e.head));
if (expected != null) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
ret2.put(new Var((String) e.head), expected);
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, expected));
}
} else {
for (En en : col.ens) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
ret2.put(new Var((String) e.head), Chc.inRight(en));
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, Chc.inRight(en)));
}
}
for (Ty ty : col.tys) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret2.put(new Var((String) e.head), Chc.inLeft(ty));
}
ret.add(new Triple<>(ret1, ret2, Chc.inLeft(ty)));
}
}
}
if (col.syms.containsKey(new Sym(e.head)) && e.annotation == null) {
// //System.out.println("a " + e);
List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l = new LinkedList<>();
l.add(new LinkedList<>());
for (int i = 0; i < e.args.size(); i++) {
RawTerm arg = e.args.get(i);
// //System.out.println("arg " + arg);
Ty ty = col.syms.get(new Sym(e.head)).first.get(i);
Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> z = infer_good(arg, Chc.inLeft(ty), col, pre, js, vars);
List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l2 = new LinkedList<>();
for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> old : l) {
// //System.out.println("old " + old);
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> y : z) {
if (y.third.equals(Chc.inLeft(ty))) {
// //System.out.println("z z");
l2.add(Util.append(old, Util.singList(y)));
}
}
}
l = l2;
}
outer: for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> outcome : l) {
// //System.out.println("outcome " + outcome);
List<Term<Ty, En, Sym, Fk, Att, Gen, Sk>> w = outcome.stream().map(x -> x.first).collect(Collectors.toList());
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sym(new Sym(e.head), w);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> ctx0 : outcome) {
if (!ctx0.second.agreeOnOverlap(ret2) || !ctx0.second.agreeOnOverlap(Ctx.fromNullable(vars))) {
// //System.out.println("xxx ");
continue outer;
}
// //System.out.println("yyy");
ret2.map.putAll(ctx0.second.map);
}
for (int i = 0; i < e.args.size(); i++) {
RawTerm arg = e.args.get(i);
// //System.out.println("2arx " + arg);
Chc<Ty, En> ty = Chc.inLeft(col.syms.get(new Sym(e.head)).first.get(i));
Var v = new Var((String) arg.head);
if (vars.keySet().contains(v)) {
// //System.out.println("a " + v);
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
// //System.out.println("b " + v);
continue;
} else if (!ret2.containsKey(v)) {
// //System.out.println("c " + v);
ret2.put(new Var(e.args.get(i).head), ty);
}
}
}
Chc<Ty, En> ret3 = Chc.inLeft(col.syms.get(new Sym(e.head)).second);
if (expected != null && !expected.equals(ret3)) {
// //System.out.println("d " );
} else {
// //System.out.println("e " );
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
}
}
}
}
for (En en : col.ens) {
if (col.fks.containsKey(new Fk(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.fks.get(new Fk(en, e.head)).first), col, pre, js, vars)) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Fk(new Fk(en, e.head), outcome.first);
// System.out.println("trying " + en + " and " + e.head);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
Var v = new Var(e.args.get(0).head);
Chc<Ty, En> ty = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
if (vars.keySet().contains(v)) {
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
// System.out.println("no1");
continue;
} else if (!ret2.containsKey(v)) {
ret2.put(new Var(e.args.get(0).head), ty);
}
}
Chc<Ty, En> ret3 = Chc.inRight(col.fks.get(new Fk(en, e.head)).second);
Chc<Ty, En> argt = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
if (expected != null && !expected.equals(ret3)) {
} else {
if (argt.equals(outcome.third)) {
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
} else {
// System.out.println("b3");
}
} else {
// System.out.println("c3");
}
}
}
}
if (col.atts.containsKey(new Att(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
// System.out.println("x " + e);
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.atts.get(new Att(en, e.head)).first), col, pre, js, vars)) {
// System.out.println("y " + outcome);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Att(new Att(en, e.head), outcome.first);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
Var v = new Var(e.args.get(0).head);
Chc<Ty, En> ty = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
if (vars.keySet().contains(v)) {
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
continue;
} else if (!ret2.containsKey(v)) {
// System.out.println("b " + v);
ret2.put(v, ty);
}
}
Chc<Ty, En> ret3 = Chc.inLeft(col.atts.get(new Att(en, e.head)).second);
Chc<Ty, En> argt = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
if (expected != null && !expected.equals(ret3)) {
// System.out.println("d " + v);
} else {
// System.out.println("e " + v);
if (argt.equals(outcome.third)) {
// System.out.println("f " + v);
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
}
}
}
}
}
}
if (col.gens.containsKey(new Gen(e.head)) && e.args.isEmpty() && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Gen(new Gen(e.head));
Chc<Ty, En> ret3 = Chc.inRight(col.gens.get(new Gen(e.head)));
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
if (col.sks.containsKey(new Sk(e.head)) && e.args.isEmpty() && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sk(new Sk(e.head));
Chc<Ty, En> ret3 = Chc.inLeft(col.sks.get(new Sk(e.head)));
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
if (e.args.isEmpty() && e.annotation != null) {
Ty ty = new Ty(e.annotation);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
Chc<Ty, En> ret3 = Chc.inLeft(ty);
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
// as primitive - only if not a variable/generator/etc in scope i.e. none above fired
if (e.args.isEmpty() && e.annotation == null && ret.isEmpty()) {
for (Ty ty : col.tys) {
if (expected != null && !expected.equals(Chc.inLeft(ty))) {
continue;
}
try {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
Chc<Ty, En> ret3 = Chc.inLeft(ty);
if (expected != null && !expected.equals(ret3)) {
// System.out.println("zzz");
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
// System.out.println("added " + ret + " and " + ret3);
}
} catch (Exception ex) {
if (expected != null) {
ex.printStackTrace();
// throw ex;
}
// //ex.printStackTrace();
}
}
}
return ret;
}
use of catdata.aql.exp.SchExpRaw.Fk in project fql by CategoricalData.
the class AqlViewer method viewDP.
/*public static <Ty, En, Sym, Fk, Att> JComponent viewSchema2(Schema<Ty, En, Sym, Fk, Att> schema) {
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
Ctx<Chc<En,Ty>, Object> nodes = new Ctx<>();
for (En en : schema.ens) {
Object v1 = graph.insertVertex(parent, null, en.toString(), 20, 20, 80, 30);
nodes.put(Chc.inLeft(en), v1);
}
for (Ty ty : schema.typeSide.tys) {
Object v1 = graph.insertVertex(parent, null, ty.toString(), 20, 20, 80, 30);
nodes.put(Chc.inRight(ty), v1);
}
for (Att att : schema.atts.keySet()) {
graph.insertEdge(parent, null, att.toString(), nodes.get(Chc.inLeft(schema.atts.get(att).first)), nodes.get(Chc.inRight(schema.atts.get(att).second)));
}
for (Fk fk : schema.fks.keySet()) {
graph.insertEdge(parent, null, fk.toString(), nodes.get(Chc.inLeft(schema.fks.get(fk).first)), nodes.get(Chc.inLeft(schema.fks.get(fk).second)));
}
mxGraphLayout layout = new mxOrganicLayout(graph); // or whatever layouting algorithm
layout.execute(parent);
mxGraphComponent graphComponent = new mxGraphComponent(graph);
// Map<String, Object> style = graph.getStylesheet().getDefaultEdgeStyle();
// style.put(mxConstants.STYLE_ROUNDED, true);
// style.put(mxConstants.STYLE_EDGE, mxConstants.EDGESTYLE_ENTITY_RELATION);
return graphComponent;
} */
private static <Ty, En, Sym, Fk, Att, Gen, Sk> JComponent viewDP(DP<Ty, En, Sym, Fk, Att, Gen, Sk> dp, Collage col, AqlJs js) {
CodeTextPanel input = new CodeTextPanel("Input (either equation-in-ctx or term-in-ctx)", "");
CodeTextPanel output = new CodeTextPanel("Output", "");
JButton eq = new JButton("Decide Equation-in-ctx");
JButton nf = new JButton("Normalize Term-in-ctx");
/*
* if (!dp.hasNFs()) { nf.setEnabled(false); }
*/
JButton print = new JButton("Show Info");
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
buttonPanel.add(eq);
buttonPanel.add(nf);
buttonPanel.add(print);
// TODO: aql does not position correctly
Split split = new Split(.5, JSplitPane.VERTICAL_SPLIT);
split.add(input);
split.add(output);
JPanel main = new JPanel(new BorderLayout());
main.add(split, BorderLayout.CENTER);
main.add(buttonPanel, BorderLayout.NORTH);
print.addActionListener(x -> output.setText(dp.toStringProver()));
eq.addActionListener(x -> {
try {
Triple<List<Pair<String, String>>, RawTerm, RawTerm> y = AqlParser.getParser().parseEq(input.getText());
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.third, col, js);
boolean isEq = dp.eq(z.first, z.second, z.third);
output.setText(Boolean.toString(isEq));
} catch (Exception ex) {
ex.printStackTrace();
output.setText(ex.getMessage());
}
});
nf.addActionListener(x -> {
try {
Pair<List<Pair<String, String>>, RawTerm> y = AqlParser.getParser().parseTermInCtx(input.getText());
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.second, col, js);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> w = dp.nf(z.first, z.second);
output.setText(w.toString());
} catch (Exception ex) {
ex.printStackTrace();
output.setText(ex.getMessage());
}
});
return main;
}
use of catdata.aql.exp.SchExpRaw.Fk in project fql by CategoricalData.
the class AqlViewer method viewTransform.
private static <Ty, En, Sym, Fk, Att, Gen1, Sk1, Gen2, Sk2, X1, Y1, X2, Y2> JComponent viewTransform(Transform<Ty, En, Sym, Fk, Att, Gen1, Sk1, Gen2, Sk2, X1, Y1, X2, Y2> t) {
List<JComponent> list = new LinkedList<>();
List<En> ens = Util.alphabetical(t.src().schema().ens);
List<Ty> tys = Util.alphabetical(t.src().schema().typeSide.tys);
for (En en : ens) {
List<String> header = new LinkedList<>();
header.add("Input");
header.add("Output");
int n = t.src().algebra().en(en).size();
Object[][] data = new Object[n][2];
int i = 0;
for (X1 x1 : Util.alphabetical(t.src().algebra().en(en))) {
Object[] row = new Object[2];
row[0] = t.src().algebra().printX(x1);
X2 x2 = t.repr(x1);
row[1] = t.dst().algebra().printX(x2);
data[i] = row;
i++;
}
// TODO: aql boldify attributes
list.add(GuiUtil.makeTable(BorderFactory.createEmptyBorder(), en + " (" + n + ")", data, header.toArray()));
}
Map<Ty, Set<Y1>> z = Util.revS(t.src().algebra().talg().sks.map);
for (Ty ty : tys) {
List<String> header = new LinkedList<>();
header.add("Input");
header.add("Output");
if (!z.containsKey(ty)) {
continue;
}
int n = z.get(ty).size();
Object[][] data = new Object[n][2];
int i = 0;
for (Y1 y1 : z.get(ty)) {
Object[] row = new Object[2];
Term<Ty, En, Sym, Fk, Att, Gen1, Sk1> a = t.src().algebra().reprT_protected(Term.Sk(y1));
row[0] = t.src().algebra().printY(y1);
// t.dst().algebra().intoY(t.reprT(y1));
Term<Ty, En, Sym, Fk, Att, Gen2, Sk2> y0 = t.trans(a);
// t.dst().algebra().pr, Util.voidFn()); //TODO aql viewer printing revisit
row[1] = y0.toString();
data[i] = row;
i++;
}
// TODO: aql boldify attributes
list.add(GuiUtil.makeTable(BorderFactory.createEmptyBorder(), ty + " (" + n + ")", data, header.toArray()));
}
return GuiUtil.makeGrid(list);
}
Aggregations