Search in sources :

Example 1 with Id

use of catdata.fqlpp.FunctorExp.Id 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();
}
Also used : Curry(catdata.fqlpp.FunctorExp.Curry) FF(catdata.fqlpp.FunctorExp.FF) TT(catdata.fqlpp.FunctorExp.TT) HashMap(java.util.HashMap) PeterApply(catdata.fqlpp.TransExp.PeterApply) Apply(catdata.fqlpp.FunctorExp.Apply) Inl(catdata.fqlpp.FunctorExp.Inl) Var(catdata.fqlpp.FunctorExp.Var) Chr(catdata.fqlpp.TransExp.Chr) Prod(catdata.fqlpp.FunctorExp.Prod) CoProd(catdata.fqlpp.TransExp.CoProd) Tru(catdata.fqlpp.FnExp.Tru) List(java.util.List) LinkedList(java.util.LinkedList) Eval(catdata.fqlpp.FunctorExp.Eval) Pair(catdata.Pair) Iso(catdata.fqlpp.FunctorExp.Iso) Fst(catdata.fqlpp.FunctorExp.Fst) Krnl(catdata.fqlpp.FnExp.Krnl) Const(catdata.fqlpp.FnExp.Const) CatConst(catdata.fqlpp.FunctorExp.CatConst) SetSetConst(catdata.fqlpp.FunctorExp.SetSetConst) InstConst(catdata.fqlpp.FunctorExp.InstConst) MapConst(catdata.fqlpp.FunctorExp.MapConst) FinalConst(catdata.fqlpp.FunctorExp.FinalConst) ApplyTrans(catdata.fqlpp.TransExp.ApplyTrans) Tuple5(org.jparsec.functors.Tuple5) Scanners(org.jparsec.Scanners) Plus(catdata.fqlpp.CatExp.Plus) ApplyTrans(catdata.fqlpp.TransExp.ApplyTrans) Parsers(org.jparsec.Parsers) Times(catdata.fqlpp.CatExp.Times) Terminals(org.jparsec.Terminals) AndOrNotImplies(catdata.fqlpp.TransExp.AndOrNotImplies) Tuple3(org.jparsec.functors.Tuple3) Id(catdata.fqlpp.FunctorExp.Id) Map(java.util.Map) ToMap(catdata.fqlpp.TransExp.ToMap) HashMap(java.util.HashMap)

Example 2 with Id

use of catdata.fqlpp.FunctorExp.Id 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);
}
Also used : Curry(catdata.fqlpp.FunctorExp.Curry) HashMap(java.util.HashMap) PeterApply(catdata.fqlpp.TransExp.PeterApply) Apply(catdata.fqlpp.FunctorExp.Apply) Adj(catdata.fqlpp.TransExp.Adj) Var(catdata.fqlpp.FunctorExp.Var) Chr(catdata.fqlpp.TransExp.Chr) AndOrNotImplies(catdata.fqlpp.TransExp.AndOrNotImplies) Comp(catdata.fqlpp.FunctorExp.Comp) ToMap(catdata.fqlpp.TransExp.ToMap) ToSet(catdata.fqlpp.TransExp.ToSet) CoProd(catdata.fqlpp.TransExp.CoProd) Bool(catdata.fqlpp.TransExp.Bool) ToInst(catdata.fqlpp.TransExp.ToInst) List(java.util.List) LinkedList(java.util.LinkedList) SetSet(catdata.fqlpp.TransExp.SetSet) ApplyPath(catdata.fqlpp.TransExp.ApplyPath) Whisker(catdata.fqlpp.TransExp.Whisker) Ker(catdata.fqlpp.TransExp.Ker) Id(catdata.fqlpp.FunctorExp.Id) Map(java.util.Map) ToMap(catdata.fqlpp.TransExp.ToMap) HashMap(java.util.HashMap) PeterApply(catdata.fqlpp.TransExp.PeterApply) Inj(catdata.fqlpp.TransExp.Inj) One(catdata.fqlpp.FunctorExp.One) Prod(catdata.fqlpp.FunctorExp.Prod) CoProd(catdata.fqlpp.TransExp.CoProd) Proj(catdata.fqlpp.TransExp.Proj) Pair(catdata.Pair) Zero(catdata.fqlpp.FunctorExp.Zero) Iso(catdata.fqlpp.FunctorExp.Iso) ToCat(catdata.fqlpp.TransExp.ToCat) Tuple4(org.jparsec.functors.Tuple4) ApplyTrans(catdata.fqlpp.TransExp.ApplyTrans) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3)

Example 3 with Id

use of catdata.fqlpp.FunctorExp.Id in project fql by CategoricalData.

the class PPParser method toFtr.

private static FunctorExp toFtr(Object o) {
    try {
        Tuple5 p = (Tuple5) o;
        if (p.a.toString().equals("apply")) {
            FunctorExp f = toFtr(p.b);
            FunctorExp e = toFtr(p.e);
            return new Apply(f, e);
        }
        if (p.e.toString().equals("Set")) {
            return p.c.toString().equals("Set") ? toSetSet(o) : toInstConst(o);
        } else if (p.e.toString().equals("Cat")) {
            return toCatFtrConst(o);
        } else {
            if (p.e instanceof Tuple3) {
                Tuple3 t = (Tuple3) p.e;
                if ((t.a.toString().equals("Cat") || t.a.toString().equals("Set")) && t.b.toString().equals("^")) {
                    return toFinalConst(o);
                } else {
                    throw new RuntimeException();
                }
            }
            return toMapConst(o);
        }
    } 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 Fst(toCat(p2), toCat(p3));
        } else if (p1.equals("snd")) {
            return new Snd(toCat(p2), toCat(p3));
        } else if (p1.equals("inl")) {
            return new Inl(toCat(p2), toCat(p3));
        } else if (p1.equals("inr")) {
            return new Inr(toCat(p2), toCat(p3));
        } else if (p1.equals("iso1")) {
            return new Iso(true, toCat(p2), toCat(p3));
        } else if (p1.equals("iso2")) {
            return new Iso(false, toCat(p2), toCat(p3));
        } else if (p1.equals("eval")) {
            return new Eval(toCat(p2), toCat(p3));
        } else if (p2.toString().equals(";")) {
            return new Comp(toFtr(o1), toFtr(p3));
        } else if (p2.toString().equals("*")) {
            return new Prod(toFtr(o1), toFtr(p3));
        } else if (p2.toString().equals("+")) {
            return new Case(toFtr(o1), toFtr(p3));
        } else if (p2.toString().equals("^")) {
            return new Exp(toFtr(o1), toFtr(p3));
        } else if (p1.equals("unit")) {
            return new One(toCat(p.b), toCat(p.c));
        } else if (p1.equals("void")) {
            return new Zero(toCat(p.b), toCat(p.c));
        } else if (p1.equals("pushout")) {
            return new Pushout(p.b.toString(), p.c.toString());
        }
    } 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 Id(toCat(p2));
            case "prop":
                return new Prop(toCat(p2));
            case "curry":
                return new Curry(toFtr(p2));
            case "uncurry":
                return new Uncurry(toFtr(p2));
            case "delta":
            case "sigma":
            case "pi":
                return new Migrate(toFtr(p2), p1);
            case "ff":
                return new FF(toCat(p2));
            case "tt":
                return new TT(toCat(p2));
            case "dom":
                return new Dom(p2.toString(), true);
            case "cod":
                return new Dom(p2.toString(), false);
            case "pivot":
                return new Pivot(toFtr(p2), true);
            case "unpivot":
                return new Pivot(toFtr(p2), false);
            default:
                break;
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof String) {
        return new Var(o.toString());
    }
    throw new RuntimeException("Bad: " + o);
}
Also used : Curry(catdata.fqlpp.FunctorExp.Curry) FF(catdata.fqlpp.FunctorExp.FF) TT(catdata.fqlpp.FunctorExp.TT) PeterApply(catdata.fqlpp.TransExp.PeterApply) Apply(catdata.fqlpp.FunctorExp.Apply) Inl(catdata.fqlpp.FunctorExp.Inl) Migrate(catdata.fqlpp.FunctorExp.Migrate) Var(catdata.fqlpp.FunctorExp.Var) One(catdata.fqlpp.FunctorExp.One) Snd(catdata.fqlpp.FunctorExp.Snd) Pushout(catdata.fqlpp.FunctorExp.Pushout) Inr(catdata.fqlpp.FunctorExp.Inr) Comp(catdata.fqlpp.FunctorExp.Comp) Case(catdata.fqlpp.FunctorExp.Case) Prod(catdata.fqlpp.FunctorExp.Prod) CoProd(catdata.fqlpp.TransExp.CoProd) Eval(catdata.fqlpp.FunctorExp.Eval) Pair(catdata.Pair) Zero(catdata.fqlpp.FunctorExp.Zero) Dom(catdata.fqlpp.FunctorExp.Dom) Iso(catdata.fqlpp.FunctorExp.Iso) Prop(catdata.fqlpp.FunctorExp.Prop) Fst(catdata.fqlpp.FunctorExp.Fst) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) Pivot(catdata.fqlpp.FunctorExp.Pivot) Id(catdata.fqlpp.FunctorExp.Id) Exp(catdata.fqlpp.FunctorExp.Exp) Uncurry(catdata.fqlpp.FunctorExp.Uncurry)

Aggregations

Pair (catdata.Pair)3 Apply (catdata.fqlpp.FunctorExp.Apply)3 Curry (catdata.fqlpp.FunctorExp.Curry)3 Id (catdata.fqlpp.FunctorExp.Id)3 Iso (catdata.fqlpp.FunctorExp.Iso)3 Prod (catdata.fqlpp.FunctorExp.Prod)3 Var (catdata.fqlpp.FunctorExp.Var)3 CoProd (catdata.fqlpp.TransExp.CoProd)3 PeterApply (catdata.fqlpp.TransExp.PeterApply)3 Tuple3 (org.jparsec.functors.Tuple3)3 Tuple5 (org.jparsec.functors.Tuple5)3 Comp (catdata.fqlpp.FunctorExp.Comp)2 Eval (catdata.fqlpp.FunctorExp.Eval)2 FF (catdata.fqlpp.FunctorExp.FF)2 Fst (catdata.fqlpp.FunctorExp.Fst)2 Inl (catdata.fqlpp.FunctorExp.Inl)2 One (catdata.fqlpp.FunctorExp.One)2 TT (catdata.fqlpp.FunctorExp.TT)2 Zero (catdata.fqlpp.FunctorExp.Zero)2 AndOrNotImplies (catdata.fqlpp.TransExp.AndOrNotImplies)2