use of catdata.fqlpp.TransExp.ToSet 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.fqlpp.TransExp.ToSet in project fql by CategoricalData.
the class PPParser method toSet.
private static SetExp toSet(Object o) {
try {
Tuple5 t = (Tuple5) o;
String f = t.b.toString();
SetExp e = toSet(t.e);
return new SetExp.Apply(f, e);
} catch (Exception e) {
}
try {
int i = Integer.parseInt(o.toString());
return new Numeral(i);
} catch (Exception e) {
}
try {
Tuple3<?, ?, ?> t = (Tuple3<?, ?, ?>) o;
String y = t.b.toString();
if (y.equals("+")) {
return new SetExp.Plus(toSet(t.a), toSet(t.c));
} else if (y.equals("*")) {
return new SetExp.Times(toSet(t.a), toSet(t.c));
} else if (y.equals("^")) {
return new SetExp.Exp(toSet(t.a), toSet(t.c));
} else if (y.equals("union")) {
return new SetExp.Union(toSet(t.a), toSet(t.c));
} else if (y.equals("intersect")) {
return new Intersect(toSet(t.a), toSet(t.c));
} else if (t.a.toString().equals("{")) {
List tb = (List) t.b;
Set x = new HashSet();
for (Object uu : tb) {
x.add(toValue(uu));
}
return new SetExp.Const(x);
}
} catch (RuntimeException cce) {
}
try {
org.jparsec.functors.Pair<?, ?> p = (org.jparsec.functors.Pair<?, ?>) o;
if (p.a.toString().equals("dom")) {
return new SetExp.Dom(toFn(p.b));
} else if (p.a.toString().equals("cod")) {
return new SetExp.Cod(toFn(p.b));
} else if (p.a.toString().equals("range")) {
return new Range(toFn(p.b));
}
} catch (RuntimeException cce) {
}
try {
if (o.toString().equals("void")) {
return new SetExp.Zero();
} else if (o.toString().equals("unit")) {
return new SetExp.One();
} else if (o.toString().equals("prop")) {
return new SetExp.Prop();
}
throw new RuntimeException();
} catch (RuntimeException cce) {
}
return new SetExp.Var(o.toString());
}
use of catdata.fqlpp.TransExp.ToSet in project fql by CategoricalData.
the class PPParser method toTrans.
// : ** notation
private static TransExp toTrans(Object o) {
try {
Tuple5 t = (Tuple5) o;
if (t.a.toString().equals("apply")) {
FunctorExp f = toFtr(t.b);
if (t.d.toString().equals("arrow")) {
TransExp e = toTrans(t.e);
return new TransExp.Apply(f, e);
} else {
Tuple3 t3 = (Tuple3) t.e;
List<String> l = (List<String>) t3.a;
return new ApplyPath(f, l.remove(0), l, toCat(t3.c));
}
}
Tuple5 a = (Tuple5) t.c;
Tuple5 b = (Tuple5) t.e;
if (a.c.toString().equals("Set") && a.e.toString().equals("Set") && b.c.toString().equals("Set") && b.e.toString().equals("Set")) {
Tuple3 z = (Tuple3) ((org.jparsec.functors.Pair) t.a).b;
FunctorExp src = toFtr(a.a);
FunctorExp dst = toFtr(b.a);
String ob = z.a.toString();
FnExp fun = toFn(z.c);
return new SetSet(ob, fun, src, dst);
} else if (a.e.toString().equals("Set") && b.e.toString().equals("Set")) {
FunctorExp src = toFtr(a.a);
FunctorExp dst = toFtr(b.a);
List<Object> ob = (List<Object>) ((org.jparsec.functors.Pair) t.a).b;
Map<String, Chc<FnExp, SetExp>> fun = new HashMap<>();
for (Object ttt : ob) {
if (fun.containsKey(o)) {
throw new RuntimeException("Duplicate arrow: " + ttt + " in " + o);
}
Tuple3 u = (Tuple3) ttt;
String n = (String) u.a;
try {
fun.put(n, Chc.inLeft(toFn(u.c)));
} catch (Exception yyy) {
fun.put(n, Chc.inRight(toSet(u.c)));
}
}
return new ToSet(fun, src, dst);
} else if (a.e.toString().equals("Cat") && b.e.toString().equals("Cat")) {
FunctorExp src = toFtr(a.a);
FunctorExp dst = toFtr(b.a);
List<Object> ob = (List<Object>) ((org.jparsec.functors.Pair) t.a).b;
Map<String, FunctorExp> fun = new HashMap<>();
for (Object ttt : ob) {
if (fun.containsKey(o)) {
throw new RuntimeException("Duplicate arrow: " + ttt + " in " + o);
}
Tuple3 u = (Tuple3) ttt;
String n = (String) u.a;
fun.put(n, toFtr(u.c));
}
return new ToCat(fun, src, dst);
} else {
try {
FunctorExp src = toFtr(a.a);
FunctorExp dst = toFtr(b.a);
List<Object> ob = (List<Object>) ((org.jparsec.functors.Pair) t.a).b;
Map<String, TransExp> fun = new HashMap<>();
for (Object ttt : ob) {
if (fun.containsKey(o)) {
throw new RuntimeException("Duplicate arrow: " + ttt + " in " + o);
}
Tuple3 u = (Tuple3) ttt;
String n = (String) u.a;
fun.put(n, toTrans(u.c));
}
return new ToInst(fun, src, dst);
} catch (Exception ex) {
}
FunctorExp src = toFtr(a.a);
FunctorExp dst = toFtr(b.a);
List<Object> ob = (List<Object>) ((org.jparsec.functors.Pair) t.a).b;
Map<String, Pair<String, List<String>>> fun = new HashMap<>();
for (Object ttt : ob) {
if (fun.containsKey(o)) {
throw new RuntimeException("Duplicate arrow: " + ttt + " in " + o);
}
Tuple3 u = (Tuple3) ttt;
String n = (String) u.a;
List<String> l = (List<String>) u.c;
String ll = l.remove(0);
fun.put(n, new Pair<>(ll, l));
}
return new ToMap(fun, src, dst, toCat(a.c), toCat(a.e));
}
} catch (Exception re) {
}
try {
Tuple4 t = (Tuple4) o;
if (t.a.toString().equals("return") || t.a.toString().equals("coreturn")) {
return new Adj(t.a.toString(), t.b.toString(), t.c.toString(), toFtr(t.d));
} else if (t.a.toString().equals("left")) {
return new Whisker(true, toFtr(t.c), toTrans(t.d));
} else if (t.a.toString().equals("right")) {
return new Whisker(false, toFtr(t.c), toTrans(t.d));
} else if (t.a.toString().equals("APPLY")) {
return new PeterApply(t.d.toString(), toTrans(t.b));
} else {
return new ApplyTrans(toTrans(t.b), toFtr(t.d));
}
} catch (Exception re) {
}
try {
Tuple3 p = (Tuple3) o;
Object p2 = p.b;
Object p3 = p.c;
Object o1 = p.a;
if (p2.toString().equals(";")) {
return new TransExp.Comp(toTrans(o1), toTrans(p3));
} else if (o1.toString().equals("fst")) {
return new Proj(toFtr(p2), toFtr(p3), true);
} else if (o1.toString().equals("snd")) {
return new Proj(toFtr(p2), toFtr(p3), false);
} else if (o1.toString().equals("inl")) {
return new Inj(toFtr(p2), toFtr(p3), true);
} else if (o1.toString().equals("inr")) {
return new Inj(toFtr(p2), toFtr(p3), false);
} else if (o1.toString().equals("eval")) {
return new TransExp.Eval(toFtr(p2), toFtr(p3));
} else if (p2.toString().equals("*")) {
return new TransExp.Prod(toTrans(o1), toTrans(p3));
} else if (p2.toString().equals("+")) {
return new CoProd(toTrans(o1), toTrans(p3));
} else if (o1.toString().equals("iso1")) {
return new TransExp.Iso(true, toFtr(p2), toFtr(p3));
} else if (o1.toString().equals("iso2")) {
return new TransExp.Iso(false, toFtr(p2), toFtr(p3));
}
} catch (RuntimeException re) {
}
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 TransExp.Id(toFtr(p2));
case "tt":
return new TransExp.One(toFtr(p2));
case "ff":
return new TransExp.Zero(toFtr(p2));
case "curry":
return new TransExp.Curry(toTrans(p2), true);
case "CURRY":
return new TransExp.Curry(toTrans(p2), false);
case "true":
return new Bool(true, toCat(p2));
case "false":
return new Bool(false, toCat(p2));
case "char":
return new Chr(toTrans(p2));
case "kernel":
return new Ker(toTrans(p2));
case "not":
case "and":
case "implies":
case "or":
return new AndOrNotImplies(p1, toCat(p2));
default:
break;
}
} catch (RuntimeException re) {
}
if (o instanceof String) {
return new TransExp.Var(o.toString());
}
throw new RuntimeException("Could not create transform from " + o);
}
Aggregations