Search in sources :

Example 1 with ToMap

use of catdata.fqlpp.TransExp.ToMap 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 2 with ToMap

use of catdata.fqlpp.TransExp.ToMap in project fql by CategoricalData.

the class CatOps method visit.

@Override
public Transform visit(FQLPPProgram env, ToMap e) {
    Functor s = e.src.accept(env, this);
    Functor t = e.dst.accept(env, this);
    CatExp scat = resolve(env, e.s);
    if (!(scat instanceof Const)) {
        throw new RuntimeException("Source category of " + e + " is not a constant.");
    }
    // CatExp.Const scon = (CatExp.Const) scat;
    CatExp tcat = resolve(env, e.t);
    if (!(tcat instanceof Const)) {
        throw new RuntimeException("Target category of " + e + " is not a constant.");
    }
    Const tcon = (Const) tcat;
    // Signature ssig = new Signature(scon.nodes, scon.arrows, scon.eqs);
    Signature<String, String> tsig = new Signature<>(tcon.nodes, tcon.arrows, tcon.eqs);
    FUNCTION o = x -> {
        Node n = (Node) x;
        // Set src = (Set) s.applyO(n);
        // Set dst = (Set) t.applyO(n);
        Pair<String, List<String>> k = e.fun.get(n.name);
        Signature<String, String>.Path fun = tsig.path(k.first, k.second);
        // new Fn(src, dst, fun);
        return fun;
    };
    return new Transform(s, t, o);
}
Also used : PeterApply(catdata.fqlpp.TransExp.PeterApply) FiniteCategory(catdata.fqlpp.cat.FiniteCategory) Named(catdata.fqlpp.CatExp.Named) Edge(catdata.fqlpp.cat.Signature.Edge) Snd(catdata.fqlpp.FunctorExp.Snd) Comp(catdata.fqlpp.FunctorExp.Comp) ToInst(catdata.fqlpp.TransExp.ToInst) Id(catdata.fqlpp.FunctorExp.Id) Inr(catdata.fqlpp.FunctorExp.Inr) FF(catdata.fqlpp.FunctorExp.FF) Migrate(catdata.fqlpp.FunctorExp.Migrate) Colim(catdata.fqlpp.CatExp.Colim) Exp(catdata.fqlpp.CatExp.Exp) Pushout(catdata.fqlpp.FunctorExp.Pushout) FinCat(catdata.fqlpp.cat.FinCat) FunCat(catdata.fqlpp.cat.FunCat) Pair(catdata.Pair) CoMonad(catdata.fqlpp.cat.CoMonad) Iso(catdata.fqlpp.FunctorExp.Iso) ApplyPath(catdata.fqlpp.TransExp.ApplyPath) Inj(catdata.fqlpp.TransExp.Inj) Fn(catdata.fqlpp.cat.FinSet.Fn) Bool(catdata.fqlpp.TransExp.Bool) Case(catdata.fqlpp.FunctorExp.Case) ToMap(catdata.fqlpp.TransExp.ToMap) Zero(catdata.fqlpp.CatExp.Zero) Monad(catdata.fqlpp.cat.Monad) Plus(catdata.fqlpp.CatExp.Plus) Category(catdata.fqlpp.cat.Category) CatConst(catdata.fqlpp.FunctorExp.CatConst) Inst(catdata.fqlpp.cat.Inst) ToSet(catdata.fqlpp.TransExp.ToSet) Groth(catdata.fqlpp.cat.Groth) Mapping(catdata.fqlpp.cat.Mapping) Serializable(java.io.Serializable) Kleisli(catdata.fqlpp.CatExp.Kleisli) ApplyTrans(catdata.fqlpp.TransExp.ApplyTrans) SetSetConst(catdata.fqlpp.FunctorExp.SetSetConst) Dom(catdata.fqlpp.CatExp.Dom) Triple(catdata.Triple) Eval(catdata.fqlpp.FunctorExp.Eval) Var(catdata.fqlpp.FunctorExp.Var) Times(catdata.fqlpp.CatExp.Times) Const(catdata.fqlpp.CatExp.Const) java.util(java.util) Prod(catdata.fqlpp.FunctorExp.Prod) InstConst(catdata.fqlpp.FunctorExp.InstConst) Node(catdata.fqlpp.cat.Signature.Node) Chc(catdata.Chc) TT(catdata.fqlpp.FunctorExp.TT) Functor(catdata.fqlpp.cat.Functor) Proj(catdata.fqlpp.TransExp.Proj) Prop(catdata.fqlpp.FunctorExp.Prop) CatExpVisitor(catdata.fqlpp.CatExp.CatExpVisitor) Signature(catdata.fqlpp.cat.Signature) TransExpVisitor(catdata.fqlpp.TransExp.TransExpVisitor) Union(catdata.fqlpp.CatExp.Union) ToCat(catdata.fqlpp.TransExp.ToCat) One(catdata.fqlpp.CatExp.One) FDM(catdata.fqlpp.cat.FDM) Ker(catdata.fqlpp.TransExp.Ker) Fst(catdata.fqlpp.FunctorExp.Fst) Whisker(catdata.fqlpp.TransExp.Whisker) Curry(catdata.fqlpp.FunctorExp.Curry) Transform(catdata.fqlpp.cat.Transform) Apply(catdata.fqlpp.FunctorExp.Apply) Instance(catdata.fqlpp.cat.Instance) MapConst(catdata.fqlpp.FunctorExp.MapConst) Path(catdata.fqlpp.cat.Signature.Path) Pivot(catdata.fqlpp.FunctorExp.Pivot) Chr(catdata.fqlpp.TransExp.Chr) Cod(catdata.fqlpp.CatExp.Cod) FinSet(catdata.fqlpp.cat.FinSet) Uncurry(catdata.fqlpp.FunctorExp.Uncurry) FunctorExpVisitor(catdata.fqlpp.FunctorExp.FunctorExpVisitor) Adj(catdata.fqlpp.TransExp.Adj) SetSet(catdata.fqlpp.TransExp.SetSet) AndOrNotImplies(catdata.fqlpp.TransExp.AndOrNotImplies) FinalConst(catdata.fqlpp.FunctorExp.FinalConst) Inl(catdata.fqlpp.FunctorExp.Inl) CoProd(catdata.fqlpp.TransExp.CoProd) ApplyPath(catdata.fqlpp.TransExp.ApplyPath) Path(catdata.fqlpp.cat.Signature.Path) CatConst(catdata.fqlpp.FunctorExp.CatConst) SetSetConst(catdata.fqlpp.FunctorExp.SetSetConst) Const(catdata.fqlpp.CatExp.Const) InstConst(catdata.fqlpp.FunctorExp.InstConst) MapConst(catdata.fqlpp.FunctorExp.MapConst) FinalConst(catdata.fqlpp.FunctorExp.FinalConst) Node(catdata.fqlpp.cat.Signature.Node) Functor(catdata.fqlpp.cat.Functor) Signature(catdata.fqlpp.cat.Signature) Transform(catdata.fqlpp.cat.Transform) Pair(catdata.Pair)

Example 3 with ToMap

use of catdata.fqlpp.TransExp.ToMap in project fql by CategoricalData.

the class CatOps method toInstance.

private Pair<Category, Instance<String, String>> toInstance(FQLPPProgram env, InstConst ic) {
    CatExp e = resolve(env, ic.sig);
    if (!(e instanceof Const)) {
        throw new RuntimeException("Can only create instances for finitely-presented categories.");
    }
    Const c = (Const) e;
    Category src = c.accept(env, this);
    Signature<String, String> sig = new Signature<>(c.nodes, c.arrows, c.eqs);
    Map<Node, Set> nm = new HashMap<>();
    for (String n0 : ic.nm.keySet()) {
        Node n = sig.getNode(n0);
        SetExp kkk = ic.nm.get(n.name);
        if (kkk == null) {
            throw new RuntimeException("Missing node mapping from " + n);
        }
        nm.put(n, kkk.accept(env, new SetOps(ENV)));
    }
    Map<Edge, Map> em = new HashMap<>();
    for (String n0 : ic.em.keySet()) {
        Edge n = sig.getEdge(n0);
        Chc<FnExp, SetExp> chc = ic.em.get(n.name);
        if (chc == null) {
            throw new RuntimeException("Missing edge mapping from " + n);
        }
        if (chc.left) {
            FnExp kkk = chc.l;
            em.put(n, kkk.accept(env, new SetOps(ENV)).toMap());
        } else {
            SetExp sss = chc.r;
            Set vvv = sss.accept(env, new SetOps(ENV));
            Map<Object, Object> uuu = new HashMap<>();
            for (Object o : vvv) {
                if (!(o instanceof Pair)) {
                    throw new RuntimeException("Not a pair: " + o);
                }
                Pair oo = (Pair) o;
                if (uuu.containsKey(oo.first)) {
                    throw new RuntimeException("Duplicate domain entry: " + o + " in " + ic);
                }
                uuu.put(oo.first, oo.second);
            }
            FnExp kkk = new FnExp.Const(uuu::get, ic.nm.get(n.source.name), ic.nm.get(n.target.name));
            em.put(n, kkk.accept(env, new SetOps(ENV)).toMap());
        }
    }
    return new Pair<>(src, new Instance(nm, em, sig));
}
Also used : FiniteCategory(catdata.fqlpp.cat.FiniteCategory) Category(catdata.fqlpp.cat.Category) ToSet(catdata.fqlpp.TransExp.ToSet) FinSet(catdata.fqlpp.cat.FinSet) SetSet(catdata.fqlpp.TransExp.SetSet) Instance(catdata.fqlpp.cat.Instance) CatConst(catdata.fqlpp.FunctorExp.CatConst) SetSetConst(catdata.fqlpp.FunctorExp.SetSetConst) Const(catdata.fqlpp.CatExp.Const) InstConst(catdata.fqlpp.FunctorExp.InstConst) MapConst(catdata.fqlpp.FunctorExp.MapConst) FinalConst(catdata.fqlpp.FunctorExp.FinalConst) Node(catdata.fqlpp.cat.Signature.Node) Signature(catdata.fqlpp.cat.Signature) Edge(catdata.fqlpp.cat.Signature.Edge) ToMap(catdata.fqlpp.TransExp.ToMap) Pair(catdata.Pair)

Aggregations

Pair (catdata.Pair)3 SetSet (catdata.fqlpp.TransExp.SetSet)3 ToMap (catdata.fqlpp.TransExp.ToMap)3 ToSet (catdata.fqlpp.TransExp.ToSet)3 Const (catdata.fqlpp.CatExp.Const)2 Apply (catdata.fqlpp.FunctorExp.Apply)2 CatConst (catdata.fqlpp.FunctorExp.CatConst)2 Comp (catdata.fqlpp.FunctorExp.Comp)2 Curry (catdata.fqlpp.FunctorExp.Curry)2 FinalConst (catdata.fqlpp.FunctorExp.FinalConst)2 Id (catdata.fqlpp.FunctorExp.Id)2 InstConst (catdata.fqlpp.FunctorExp.InstConst)2 Iso (catdata.fqlpp.FunctorExp.Iso)2 MapConst (catdata.fqlpp.FunctorExp.MapConst)2 Prod (catdata.fqlpp.FunctorExp.Prod)2 SetSetConst (catdata.fqlpp.FunctorExp.SetSetConst)2 Var (catdata.fqlpp.FunctorExp.Var)2 Adj (catdata.fqlpp.TransExp.Adj)2 AndOrNotImplies (catdata.fqlpp.TransExp.AndOrNotImplies)2 ApplyPath (catdata.fqlpp.TransExp.ApplyPath)2