use of org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class PPParser method toValue.
private static Object toValue(Object o) {
if (o.toString().equals("true")) {
return true;
}
if (o.toString().equals("false")) {
return false;
}
if (o instanceof Tuple5) {
Tuple5 t = (Tuple5) o;
if (t.a.toString().equals("(")) {
return new Pair(toValue(t.b), toValue(t.d));
}
List l = (List) t.b;
Map s = new HashMap();
for (Object y : l) {
Pair yy = (Pair) toValue(y);
if (s.containsKey(yy.first)) {
throw new RuntimeException("Duplicate domain entry in " + o);
}
s.put(yy.first, yy.second);
}
Tuple3 tt = (Tuple3) t.e;
Set ui = (Set) toValue(tt.a);
Set uj = (Set) toValue(tt.c);
return new Fn(ui, uj, s::get);
}
if (o instanceof Tuple3) {
Tuple3 p = (Tuple3) o;
List l = (List) p.b;
Set s = new HashSet();
for (Object y : l) {
s.add(toValue(y));
}
return s;
}
if (o instanceof org.jparsec.functors.Pair) {
org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) o;
if (p.a.toString().equals("inl")) {
return Chc.inLeft(toValue(p.b));
} else if (p.a.toString().equals("inr")) {
return Chc.inRight(toValue(p.b));
} else {
return new Unit();
}
}
return o.toString();
}
use of org.jparsec.functors.Tuple3 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 org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class PPParser method toFn.
private static FnExp toFn(Object o) {
try {
Tuple5 t5 = (Tuple5) o;
if (t5.a.toString().equals("apply")) {
Tuple5 t = (Tuple5) o;
String f = t.b.toString();
FnExp e = toFn(t.e);
return new FnExp.Apply(f, e);
}
Tuple3 t3 = (Tuple3) t5.e;
Map s = new HashMap<>();
List l = (List) t5.b;
for (Object y : l) {
Pair yy = (Pair) toValue(y);
if (s.containsKey(yy.first)) {
throw new RuntimeException("Duplicate domain entry in " + o);
}
s.put(yy.first, yy.second);
}
SetExp t3a = toSet(t3.a);
SetExp t3c = toSet(t3.c);
return new Const(s::get, t3a, t3c);
} catch (Exception e) {
}
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 FnExp.Fst(toSet(p2), toSet(p3));
} else if (p1.equals("snd")) {
return new FnExp.Snd(toSet(p2), toSet(p3));
} else if (p1.equals("inl")) {
return new FnExp.Inl(toSet(p2), toSet(p3));
} else if (p1.equals("inr")) {
return new FnExp.Inr(toSet(p2), toSet(p3));
} else if (p1.equals("iso1")) {
return new FnExp.Iso(true, toSet(p2), toSet(p3));
} else if (p1.equals("iso2")) {
return new FnExp.Iso(false, toSet(p2), toSet(p3));
} else if (p1.equals("eval")) {
return new FnExp.Eval(toSet(p2), toSet(p3));
} else if (p2.toString().equals(";")) {
return new FnExp.Comp(toFn(o1), toFn(p3));
} else if (p2.toString().equals("*")) {
return new FnExp.Prod(toFn(o1), toFn(p3));
} else if (p2.toString().equals("+")) {
return new FnExp.Case(toFn(o1), toFn(p3));
}
if (p1.equals("apply")) {
return new FnExp.ApplyTrans(p2.toString(), toSet(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 FnExp.Id(toSet(p2));
case "curry":
return new FnExp.Curry(toFn(p2));
case "ff":
return new FnExp.FF(toSet(p2));
case "tt":
return new FnExp.TT(toSet(p2));
case "char":
return new FnExp.Chr(toFn(p2));
case "kernel":
return new Krnl(toFn(p2));
default:
break;
}
} catch (RuntimeException re) {
}
if (o instanceof String) {
return new FnExp.Var(o.toString());
}
if (o.toString().equals("true")) {
return new Tru("true");
} else if (o.toString().equals("false")) {
return new Tru("false");
} else if (o.toString().equals("and")) {
return new Tru("and");
} else if (o.toString().equals("or")) {
return new Tru("or");
} else if (o.toString().equals("not")) {
return new Tru("not");
} else if (o.toString().equals("implies")) {
return new Tru("implies");
}
throw new RuntimeException();
}
use of org.jparsec.functors.Tuple3 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 org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class PPParser method toCat.
private static CatExp toCat(Object o) {
if (o.toString().equals("Set")) {
return new Named("Set");
}
if (o.toString().equals("Cat")) {
return new Named("Cat");
}
if (o instanceof Tuple4) {
Tuple4 t = (Tuple4) o;
return new Kleisli(t.b.toString(), t.c.toString(), t.d.toString(), t.a.toString().equals("cokleisli"));
}
try {
Tuple3<?, ?, ?> t = (Tuple3<?, ?, ?>) o;
String y = t.b.toString();
if (y.equals("+")) {
return new Plus(toCat(t.a), toCat(t.c));
} else if (y.equals("*")) {
return new Times(toCat(t.a), toCat(t.c));
} else if (y.equals("^")) {
return new CatExp.Exp(toCat(t.a), toCat(t.c));
} else if (t.a.toString().equals("union")) {
return new Union(toCat(t.b), toCat(t.c));
} else {
return toCatConst(o);
}
} catch (RuntimeException cce) {
}
try {
org.jparsec.functors.Pair<?, ?> p = (org.jparsec.functors.Pair<?, ?>) o;
if (p.a.toString().equals("dom")) {
return new CatExp.Dom(toFtr(p.b));
} else if (p.a.toString().equals("cod")) {
return new Cod(toFtr(p.b));
} else if (p.a.toString().equals("colim")) {
return new Colim((String) p.b);
}
} catch (RuntimeException cce) {
}
try {
if (o.toString().equals("void")) {
return new CatExp.Zero();
} else if (o.toString().equals("unit")) {
return new CatExp.One();
}
} catch (RuntimeException cce) {
}
return new CatExp.Var(o.toString());
}
Aggregations