use of catdata.opl.OplExp.OplSig in project fql by CategoricalData.
the class OplParser method toTheory.
private static OplExp toTheory(Object o) {
Tuple5 t = (Tuple5) o;
List<String> imports = t.a == null ? new LinkedList<>() : (List<String>) ((org.jparsec.functors.Pair) t.a).b;
Tuple3 a = (Tuple3) t.b;
Tuple3 b = (Tuple3) t.c;
Tuple3 c = (Tuple3) t.d;
Tuple3 d = (Tuple3) t.e;
Set<String> sorts = a == null ? new HashSet<>() : new HashSet<>((Collection<String>) a.b);
List<Tuple3> symbols0 = b == null ? new LinkedList<>() : (List<Tuple3>) b.b;
List<org.jparsec.functors.Pair> equations0 = c == null ? new LinkedList<>() : (List<org.jparsec.functors.Pair>) c.b;
List<org.jparsec.functors.Pair> implications0 = new LinkedList<>();
if (d != null) {
implications0 = (List<org.jparsec.functors.Pair>) d.b;
}
Map<String, Pair<List<String>, String>> symbols = new HashMap<>();
Map<String, Integer> prec = new HashMap<>();
for (Tuple3 x : symbols0) {
String dom;
List<String> args;
if (x.c instanceof Tuple3) {
Tuple3 zzz = (Tuple3) x.c;
args = (List<String>) zzz.a;
dom = (String) zzz.c;
} else {
dom = (String) x.c;
args = new LinkedList<>();
}
List<org.jparsec.functors.Pair> name0s = (List<org.jparsec.functors.Pair>) x.a;
for (org.jparsec.functors.Pair name0 : name0s) {
// org.jparsec.functors.Pair name0 =
// (org.jparsec.functors.Pair) x.a;
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, new Pair<>(args, dom));
}
}
if (sorts.contains("Nat") && symbols.keySet().contains("zero") && symbols.keySet().contains("succ") && DefunctGlobalOptions.debug.opl.opl_desugar_nat) {
sugarForNat = true;
}
List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> equations = new LinkedList<>();
for (org.jparsec.functors.Pair<Tuple3, Tuple3> x : equations0) {
List<Tuple3> fa = x.a == null ? new LinkedList<>() : (List<Tuple3>) x.a.b;
OplCtx<String, String> ctx = toCtx(fa);
Tuple3 eq = x.b;
OplTerm lhs = toTerm(ctx.names(), consts(symbols), eq.a, false);
OplTerm rhs = toTerm(ctx.names(), consts(symbols), eq.c, false);
equations.add(new Triple<>(ctx, lhs, rhs));
}
List<Triple<OplCtx<String, String>, List<Pair<OplTerm<String, String>, OplTerm<String, String>>>, List<Pair<OplTerm<String, String>, OplTerm<String, String>>>>> implications = new LinkedList<>();
for (org.jparsec.functors.Pair<Tuple3, Tuple3> x : implications0) {
List<Tuple3> fa = x.a == null ? new LinkedList<>() : (List<Tuple3>) x.a.b;
OplCtx<String, String> ctx = toCtx(fa);
Tuple3 eq = x.b;
List<Tuple3> lhs0 = (List<Tuple3>) eq.a;
List<Tuple3> rhs0 = (List<Tuple3>) eq.c;
List<Pair<OplTerm<String, String>, OplTerm<String, String>>> lhs = new LinkedList<>();
List<Pair<OplTerm<String, String>, OplTerm<String, String>>> rhs = new LinkedList<>();
for (Tuple3 obj : lhs0) {
OplTerm lhsX = toTerm(ctx.names(), consts(symbols), obj.a, false);
OplTerm rhsX = toTerm(ctx.names(), consts(symbols), obj.c, false);
lhs.add(new Pair<>(lhsX, rhsX));
}
for (Tuple3 obj : rhs0) {
OplTerm lhsX = toTerm(ctx.names(), consts(symbols), obj.a, false);
OplTerm rhsX = toTerm(ctx.names(), consts(symbols), obj.c, false);
rhs.add(new Pair<>(lhsX, rhsX));
}
implications.add(new Triple<>(ctx, lhs, rhs));
}
OplSig ret = new OplSig<>(new VIt(), prec, sorts, symbols, equations, implications);
ret.imports = new HashSet<>(imports);
return ret;
}
use of catdata.opl.OplExp.OplSig in project fql by CategoricalData.
the class OplWarehouse method addBlanks.
// TODO aql alphabetize tabs
// TODO aql heuristic: if something already there, leave it. otherwise, create new
private void addBlanks() {
switch(state) {
case INITIAL:
if (!bindings.containsKey(THEORY)) {
bindings.put(THEORY, new OplSig<>(new VIt(), new HashMap<>(), new HashSet<>(), new HashMap<>(), new LinkedList<>()));
}
break;
case THEORY:
if (!bindings.containsKey(SHAPE)) {
bindings.put(SHAPE, new OplGraph<String, String>(new HashSet<>(), new HashMap<>()));
}
break;
case SHAPE:
{
OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
for (String en : shape.nodes) {
if (bindings.containsKey(SCHEMA + "_" + en)) {
// TODO: aql false
continue;
}
bindings.put(SCHEMA + "_" + en, new OplSCHEMA0<String, String, String>(new HashMap<>(), new HashSet<>(), new HashMap<>(), new HashMap<>(), new LinkedList<>(), new LinkedList<>(), THEORY));
}
break;
}
case SCHEMA:
{
OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
for (String ed : shape.edges.keySet()) {
if (bindings.containsKey(MAPPING + "_" + ed)) {
continue;
}
String src = SCHEMA + "_" + shape.edges.get(ed).first;
// String dst = SCHEMA + "_" + shape.edges.get(ed).second;
OplSCHEMA0<String, String, String> src0 = (OplSCHEMA0<String, String, String>) bindings.get(src);
Map<String, String> sorts = new HashMap<>();
for (String x : src0.entities) {
sorts.put(x, "?");
}
Map<String, Pair<OplCtx<String, String>, OplTerm<String, String>>> symbols = new HashMap<>();
for (String g : src0.attrs.keySet()) {
OplCtx<String, String> ctx = new OplCtx<>(Util.singList(new Pair<>("x", "?")));
OplTerm<String, String> term = new OplTerm<>("?");
symbols.put(g, new Pair<>(ctx, term));
}
for (String g : src0.edges.keySet()) {
OplCtx<String, String> ctx = new OplCtx<>(Util.singList(new Pair<>("t", "?")));
OplTerm<String, String> term = new OplTerm<>("?");
symbols.put(g, new Pair<>(ctx, term));
}
bindings.put(MAPPING + "_" + ed, new OplMapping<>(sorts, symbols, src, SCHEMA + "_" + shape.edges.get(ed).second));
}
break;
}
case MAPPING:
{
OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
Set<String> en2 = shape.nodes.stream().map(x -> SCHEMA + "_" + x).collect(Collectors.toSet());
Map<String, Pair<String, String>> ed2 = new HashMap<>();
for (String e : shape.edges.keySet()) {
Pair<String, String> x = shape.edges.get(e);
String l = SCHEMA + "_" + x.first;
ed2.put(MAPPING + "_" + e, new Pair<>(l, SCHEMA + "_" + x.second));
}
OplGraph<String, String> shape2 = new OplGraph<>(en2, ed2);
// TODO: aql needs to compile to here
computeColimit(bindings, shape2, THEORY);
for (String en : shape.nodes) {
if (bindings.containsKey(INSTANCE + "_" + en)) {
// TODO aql false
continue;
}
bindings.put(INSTANCE + "_" + en, new OplInst0<>(new OplPres<>(new HashMap<>(), SCHEMA + "_" + en, null, new HashMap<>(), new LinkedList<>())));
}
break;
}
case INSTANCE:
{
OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
for (String ed : shape.edges.keySet()) {
String en = shape.edges.get(ed).first;
String en2 = shape.edges.get(ed).second;
String src = INSTANCE + "_" + ed + "_forward";
bindings.put(src, new OplSigma(MAPPING + "_" + ed, INSTANCE + "_" + en));
if (bindings.containsKey(TRANSFORM + "_" + ed + "_forward")) {
continue;
}
OplMapping<String, String, String, String, String> mmm = (OplMapping<String, String, String, String, String>) bindings.get(MAPPING + "_" + ed);
// TODO: aql factor getting into separate method, or use compiler
OplInst0<String, String, String, String> src1 = (OplInst0<String, String, String, String>) bindings.get(INSTANCE + "_" + en);
OplInst0<String, String, String, String> src0 = (OplInst0<String, String, String, String>) bindings.get(INSTANCE + "_" + en2);
OplSCHEMA0<String, String, String> src0_sch = (OplSCHEMA0<String, String, String>) bindings.get(src0.P.S);
OplSCHEMA0<String, String, String> src1_sch = (OplSCHEMA0<String, String, String>) bindings.get(src1.P.S);
Map<String, Map<String, OplTerm<Object, String>>> sorts = new HashMap<>();
for (String x : src0_sch.entities) {
sorts.put(x, new HashMap<>());
}
for (String g : src1.P.gens.keySet()) {
String gty = src1.P.gens.get(g);
String s2 = mmm.sorts.get(gty);
if (s2 == null) {
continue;
}
Map<String, OplTerm<Object, String>> symbols = sorts.get(s2);
if (symbols != null) {
symbols.put(g, new OplTerm<>("?"));
}
}
bindings.put(TRANSFORM + "_" + ed + "_forward", new OplPresTrans<String, String, String, String, String>(sorts, src, INSTANCE + "_" + shape.edges.get(ed).second));
}
break;
}
case TRANSFORM:
OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
for (String en : shape.nodes) {
bindings.put(INSTANCE + "_" + en + "_colimit", new OplSigma(MAPPING + "_" + SCHEMA + "_" + en + "_colimit", INSTANCE + "_" + en));
}
Map<String, Pair<String, String>> ed2 = new HashMap<>();
for (String e : shape.edges.keySet()) {
Pair<String, String> x = shape.edges.get(e);
String l = INSTANCE + "_" + x.first + "_colimit";
// String en = x.first.get(0);
String en = x.second;
bindings.put(TRANSFORM + "_" + e + "_colimit", new OplSigma(MAPPING + "_" + SCHEMA + "_" + en + "_colimit", TRANSFORM + "_" + e + "_forward"));
ed2.put(TRANSFORM + "_" + e + "_colimit", new Pair<>(l, INSTANCE + "_" + x.second + "_colimit"));
}
Set<String> en2 = shape.nodes.stream().map(x -> INSTANCE + "_" + x + "_colimit").collect(Collectors.toSet());
OplGraph<String, String> shape2 = new OplGraph<>(en2, ed2);
bindings.put(SCHEMA + "_for_result", shape2);
bindings.put(FINISHED, new OplExp.OplString("colimit " + SCHEMA + "_Colimit" + " " + SCHEMA + "_for_result"));
// bindings.put(FINISHED + " all", new OplExp.OplString(complete().toString()));
break;
default:
break;
}
}
use of catdata.opl.OplExp.OplSig in project fql by CategoricalData.
the class OplDriver method inferPrec.
private static void inferPrec(Program<OplExp> init) {
int curPrec = 10000;
// constants < generators < attributes < foreign keys
for (String k : init.order) {
OplExp v = init.exps.get(k);
if (v instanceof OplSig) {
OplSig sig = (OplSig) v;
sig.prec = new HashMap<>();
for (Object c : sig.symbols.keySet()) {
sig.prec.put(c, curPrec++);
}
}
}
for (String k : init.order) {
OplExp v = init.exps.get(k);
if (v instanceof OplInst0) {
OplInst0 sig = (OplInst0) v;
sig.P.prec = new HashMap<>();
for (Object c : sig.P.gens.keySet()) {
sig.P.prec.put(c, curPrec++);
}
}
}
for (String k : init.order) {
OplExp v = init.exps.get(k);
if (v instanceof OplSCHEMA0) {
OplSCHEMA0 sig = (OplSCHEMA0) v;
sig.prec = new HashMap<>();
for (Object c : sig.attrs.keySet()) {
sig.prec.put(c, curPrec++);
}
}
}
for (String k : init.order) {
OplExp v = init.exps.get(k);
if (v instanceof OplSCHEMA0) {
OplSCHEMA0 sig = (OplSCHEMA0) v;
for (Object c : sig.edges.keySet()) {
sig.prec.put(c, curPrec++);
}
}
}
}
use of catdata.opl.OplExp.OplSig in project fql by CategoricalData.
the class CfgToOpl method toCfg.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static OplExp toCfg(Object o) {
Map<String, List<List<String>>> ret = new HashMap<>();
List<Tuple3> l = (List<Tuple3>) o;
for (Tuple3 p : l) {
String x = (String) p.a;
if (ret.containsKey(x)) {
throw new RuntimeException("Duplicate production name: " + x);
}
ret.put(x, (List<List<String>>) p.c);
}
Map<String, Pair<List<String>, String>> symbols = new HashMap<>();
int i = 0;
for (String k : ret.keySet()) {
List<List<String>> v = ret.get(k);
for (List<String> u : v) {
List<String> pre = new LinkedList<>();
List<String> tys = new LinkedList<>();
for (String z : u) {
if (ret.keySet().contains(z)) {
tys.add(z);
} else {
pre.add(z);
}
}
String name0 = Util.sep(pre, "_");
String xxx = symbols.keySet().contains(name0) ? "_" + (i++) : "";
String name = "\"" + name0 + xxx + "\"";
symbols.put(name, new Pair<>(tys, k));
i++;
}
}
return new OplSig(null, new HashMap<>(), ret.keySet(), symbols, new LinkedList<>());
}
use of catdata.opl.OplExp.OplSig in project fql by CategoricalData.
the class OplToKB method convert.
@SuppressWarnings({ "unchecked", "rawtypes" })
private OplKB<C, V> convert(OplSig<S, C, V> s) {
if (s.prec.keySet().size() != new HashSet<>(s.prec.values()).size()) {
throw new RuntimeException("Cannot duplicate precedence: " + s.prec);
}
// if (!Collections.disjoint(Arrays.asList(KBHorn.reserved), s.symbols.keySet())) {
// throw new RuntimeException("Theory contains reserved symbol, one of " + Arrays.toString(KBHorn.reserved));
// }
Map<C, Pair<List<S>, S>> symbols = new HashMap<>(s.symbols);
List one = new LinkedList();
one.add(new Unit());
List two = new LinkedList();
two.add(new Unit());
two.add(new Unit());
symbols.put((C) KBHorn._eq, new Pair(two, new Unit()));
symbols.put((C) KBHorn._or, new Pair(two, new Unit()));
symbols.put((C) KBHorn._not, new Pair(one, new Unit()));
symbols.put((C) KBHorn._true, new Pair(new LinkedList(), new Unit()));
symbols.put((C) KBHorn._false, new Pair(new LinkedList(), new Unit()));
Function<Pair<C, C>, Boolean> gt = x -> {
Integer l = s.prec.get(x.first);
Integer r = s.prec.get(x.second);
if (l != null && r != null) {
return l > r;
}
if (l == null && r != null) {
return false;
}
if (l != null) {
return true;
}
String lx = x.first.toString();
String rx = x.second.toString();
if (!symbols.containsKey(x.first)) {
throw new RuntimeException("Missing: " + x.first);
}
int la = symbols.get(x.first).first.size();
int ra = symbols.get(x.second).first.size();
if (la == ra) {
if (lx.length() == rx.length()) {
return lx.compareTo(rx) < 0;
}
return lx.length() < rx.length();
}
if (la >= 3 && ra >= 3) {
return la > ra;
}
if (la == 0 && ra > 0) {
return false;
}
if (la == 1 && (ra == 0 || ra == 2)) {
return true;
}
if (la == 1 && ra > 2) {
return false;
}
if (la == 2 && ra == 0) {
return true;
}
if (la == 2 && (ra == 1 || ra > 2)) {
return false;
}
if (la >= 3 || ra >= 3) {
// added Aug 3 16
return la > ra;
}
throw new RuntimeException("Bug in precedence, report to Ryan: la=" + la + ", ra=" + ra + ", l=null r=null");
// function symbols: arity-0 < arity-2 < arity-1 < arity-3 < arity-4
};
Set<Pair<KBExp<C, V>, KBExp<C, V>>> eqs = new HashSet<>();
for (Triple<?, OplTerm<C, V>, OplTerm<C, V>> eq : s.equations) {
eqs.add(new Pair<>(convert(eq.second), convert(eq.third)));
}
Set<Pair<KBExp<C, V>, KBExp<C, V>>> rs = new HashSet<>();
for (Triple<?, List<Pair<OplTerm<C, V>, OplTerm<C, V>>>, List<Pair<OplTerm<C, V>, OplTerm<C, V>>>> impl : s.implications) {
rs.addAll(convert(impl.second, impl.third));
}
KBOptions options = new KBOptions(DefunctGlobalOptions.debug.opl.opl_prover_unfailing, DefunctGlobalOptions.debug.opl.opl_prover_sort, DefunctGlobalOptions.debug.opl.opl_allow_horn && !s.implications.isEmpty(), DefunctGlobalOptions.debug.opl.opl_prover_ac, DefunctGlobalOptions.debug.opl.opl_prover_timeout, DefunctGlobalOptions.debug.opl.opl_prover_reduction_limit, DefunctGlobalOptions.debug.opl.opl_prover_filter_subsumed, /* NEWDEBUG.debug.opl.simplify, */
DefunctGlobalOptions.debug.opl.opl_prover_compose, false);
return new OplKB(eqs, KBOrders.lpogt(DefunctGlobalOptions.debug.opl.opl_allow_horn && !s.implications.isEmpty(), gt), fr, rs, options);
}
Aggregations