Search in sources :

Example 26 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class ColimitSchema method quotient.

private static Schema<Ty, En, Sym, Fk, Att> quotient(Schema<Ty, En, Sym, Fk, Att> sch, Set<Quad<String, String, RawTerm, RawTerm>> eqTerms, AqlOptions options) {
    Collage<Ty, En, Sym, Fk, Att, Void, Void> col = new Collage<>(sch.collage());
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs0 = new HashSet<>(sch.eqs);
    for (Quad<String, String, RawTerm, RawTerm> eq : eqTerms) {
        Map<String, Chc<Ty, En>> ctx = Util.singMap(eq.first, eq.second == null ? null : Chc.inRight(new En(eq.second)));
        Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = RawTerm.infer1x(ctx, eq.third, eq.fourth, null, col.convert(), "", sch.typeSide.js).first3();
        Chc<Ty, En> v = eq0.first.get(new Var(eq.first));
        if (v.left) {
            throw new RuntimeException("In " + eq.third + " = " + eq.fourth + ", variable " + eq.first + " has type " + v.l + " which is not an entity");
        }
        eqs0.add(new Triple<>(new Pair<>(new Var(eq.first), v.r), eq0.second.convert(), eq0.third.convert()));
        col.eqs.add(new Eq<>(new Ctx<>(new Var(eq.first), v), eq0.second.convert(), eq0.third.convert()));
    }
    boolean b = !(Boolean) options.getOrDefault(AqlOption.allow_java_eqs_unsafe);
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = AqlProver.create(options, col, sch.typeSide.js);
    Schema<Ty, En, Sym, Fk, Att> ret = new Schema<>(sch.typeSide, col.ens, col.atts.map, col.fks.map, eqs0, dp, b);
    return ret;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) HashSet(java.util.HashSet) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Triple(catdata.Triple) Chc(catdata.Chc)

Example 27 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class ColimitSchema method removeFk.

public ColimitSchema<N> removeFk(Fk src, List<Fk> l, boolean checkJava) {
    Var v = new Var("v");
    Term<Ty, En, Sym, Fk, Att, Void, Void> t = Term.Fks(l, Term.Var(v));
    if (!schemaStr.fks.containsKey(src)) {
        throw new RuntimeException(src + " is not a foreign_key in \n" + schemaStr);
    }
    if (l.contains(src)) {
        throw new RuntimeException("Cannot replace " + src + " with " + Util.sep(l, ".") + " because that path contains " + src);
    }
    En en1 = schemaStr.fks.get(src).first;
    En en2 = schemaStr.fks.get(src).second;
    if (!schemaStr.type(new Pair<>(v, en1), t).equals(Chc.inRight(en2))) {
        throw new RuntimeException("The term " + t + " has type " + schemaStr.type(new Pair<>(v, en1), t).toStringMash() + " and not " + en2 + " as expected.");
    }
    if (!schemaStr.dp.eq(new Ctx<>(v, Chc.inRight(en1)), t, Term.Fk(src, Term.Var(v)))) {
        throw new RuntimeException("The term " + t + " is not provably equal to " + Term.Fk(src, Term.Var(v)));
    }
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoToUser = Mapping.id(schemaStr);
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoFromUser = Mapping.id(schemaStr);
    Map<Fk, Pair<En, En>> fks = new HashMap<>(schemaStr.fks.map);
    fks.remove(src);
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
    for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : schemaStr.eqs) {
        Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> tr = new Triple<>(eq.first, eq.second.replaceHead(Head.Fk(src), Util.singList(v), t), eq.third.replaceHead(Head.Fk(src), Util.singList(v), t));
        if (!tr.second.equals(tr.third) && !eqs.contains(tr)) {
            eqs.add(tr);
        }
    }
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = new DP<Ty, En, Sym, Fk, Att, Void, Void>() {

        @Override
        public String toStringProver() {
            return "remove foreign key of " + schemaStr.dp.toStringProver();
        }

        @Override
        public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Void, Void> lhs, Term<Ty, En, Sym, Fk, Att, Void, Void> rhs) {
            return schemaStr.dp.eq(ctx, lhs, rhs);
        }
    };
    Schema<Ty, En, Sym, Fk, Att> schemaStr2 = new Schema<>(ty, schemaStr.ens, schemaStr.atts.map, fks, eqs, dp, checkJava);
    Map<Fk, Pair<En, List<Fk>>> fksM = new HashMap<>(isoToUser.fks.map);
    fksM.put(src, new Pair<>(en1, l));
    isoToUser = new Mapping<>(isoToUser.ens.map, isoToUser.atts.map, fksM, schemaStr, schemaStr2, checkJava);
    Map<Fk, Pair<En, List<Fk>>> fksM2 = new HashMap<>(isoFromUser.fks.map);
    fksM2.remove(src);
    isoFromUser = new Mapping<>(isoFromUser.ens.map, isoFromUser.atts.map, fksM2, schemaStr2, schemaStr, checkJava);
    return wrap(isoToUser, isoFromUser);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) Pair(catdata.Pair) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Triple(catdata.Triple)

Example 28 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XMapping method delta.

@SuppressWarnings({ "rawtypes", "unchecked" })
public XCtx<Pair<Triple<D, D, List<D>>, C>> delta(XCtx<D> I) {
    Set<Pair<Triple<D, D, List<D>>, C>> ids = new HashSet<>();
    Map<Pair<Triple<D, D, List<D>>, C>, Pair<Pair<Triple<D, D, List<D>>, C>, Pair<Triple<D, D, List<D>>, C>>> types = new HashMap<>();
    Set<Pair<List<Pair<Triple<D, D, List<D>>, C>>, List<Pair<Triple<D, D, List<D>>, C>>>> eqs = new HashSet<>();
    for (C c : src.allIds()) {
        for (Triple<D, D, List<D>> arr : I.cat().hom((D) "_1", em.get(c).get(0))) {
            if (I.global.cat().objects().contains(c)) {
                if (I.global.cat().hom((D) "_1", (D) c).contains(arr)) {
                    continue;
                }
            }
            Pair tr = new Pair(arr, c);
            types.put(tr, new Pair("_1", c));
        }
    }
    for (Pair<Triple<D, D, List<D>>, C> t1 : types.keySet()) {
        for (C c : src.allTerms()) {
            // f
            if (src.ids.contains(c)) {
                continue;
            }
            Pair<C, C> t = src.type(c);
            D dsrc = t1.first.first;
            D ddst = em.get(t.second).get(0);
            if (t1.second.equals(t.first)) {
                List lhs = new LinkedList<>();
                lhs.add(t1);
                lhs.add(c);
                List j = new LinkedList<>(t1.first.third);
                j.addAll(em.get(c));
                Triple rhs = new Triple(dsrc, ddst, j);
                // Util.sep(I.cat().hom(dsrc, ddst), "\n\n"));
                Triple rhsX = I.find_fast(rhs);
                List g = new LinkedList<>();
                g.add(new Pair<>(rhsX, t.second));
                if (I.global.allIds().contains(t.second)) {
                    // a' in G
                    Triple ooo = I.global.find_fast(rhsX);
                    if (ooo != null) {
                        List lll = new LinkedList();
                        if (((Collection) ooo.third).isEmpty()) {
                            lll.add(ooo.first);
                        }
                        lll.addAll((Collection) ooo.third);
                        eqs.add(new Pair(lhs, lll));
                    } else {
                        eqs.add(new Pair<>(lhs, g));
                    }
                } else {
                    eqs.add(new Pair<>(lhs, g));
                }
            }
        }
    }
    XCtx ret = new XCtx<>(ids, types, eqs, (XCtx<Pair<Triple<D, D, List<D>>, C>>) src.global, (XCtx<Pair<Triple<D, D, List<D>>, C>>) src, "instance");
    // I.saturated;
    ret.saturated = true;
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Collection(java.util.Collection) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) HashSet(java.util.HashSet)

Example 29 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XMapping method makeThetas2.

// /////////////////////////////////////
@SuppressWarnings({ "rawtypes", "unchecked" })
private Pair<Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>>, Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>>> makeThetas2(XCtx<C> I) {
    Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>> ret = new HashMap<>();
    Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>> ret2 = new HashMap<>();
    for (D d : dst.allIds()) {
        // was allIds
        List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>> bad_thetas = new LinkedList<>();
        if (dst.global.allIds().contains(d)) {
            for (Triple<D, D, List<D>> constant : dst.global.cat().hom((D) "_1", d)) {
                Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>> bad_theta = new LinkedHashMap<>();
                for (C c : src.allIds()) {
                    for (Triple<D, D, List<D>> f : dst.cat().hom(d, em.get(c).get(0))) {
                        Triple composed = dst.global.cat().compose(constant, f);
                        bad_theta.put(new Pair<>(c, f), composed);
                    }
                }
                bad_thetas.add(bad_theta);
            }
        }
        ret2.put(d, bad_thetas);
        List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>> thetas = new LinkedList<>();
        Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>> theta = new LinkedHashMap<>();
        Map<Pair<C, Triple<D, D, List<D>>>, Pair<C, Triple<D, D, List<D>>>> theta2 = new LinkedHashMap<>();
        for (C c : src.allIds()) {
            for (Triple<D, D, List<D>> f : dst.cat().hom(d, em.get(c).get(0))) {
                theta.put(new Pair<>(c, f), null);
                theta2.put(new Pair<>(c, f), null);
            }
        }
        if (theta.keySet().isEmpty()) {
            thetas.add(new HashMap<>());
        } else {
            for (Pair<C, Triple<D, D, List<D>>> cf : theta.keySet()) {
                for (Triple<C, C, List<C>> x : I.cat().hom((C) "_1", cf.first)) {
                    try_branch(I, thetas, theta, theta2, cf, x, bad_thetas);
                }
                // this is fine - just do first key
                break;
            }
        }
        ret.put(d, thetas);
    }
    return new Pair<>(ret, ret2);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) Triple(catdata.Triple) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(catdata.Pair)

Example 30 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XMapping method pi_unit.

@SuppressWarnings({ "rawtypes", "unchecked" })
public XMapping pi_unit(XCtx<D> J) {
    XCtx<Pair<Triple<D, D, List<D>>, C>> deltaI = delta(J);
    XCtx pideltaI = pi((XCtx) deltaI);
    Map m = new HashMap();
    for (D x : J.terms()) {
        D d = J.type(x).second;
        if (!J.type(x).first.equals("_1")) {
            throw new RuntimeException();
        }
        Map theta = new HashMap();
        for (C c : src.allIds()) {
            for (Triple<D, D, List<D>> f : dst.cat().hom(d, em.get(c).get(0))) {
                List<D> tofind = new LinkedList<>();
                tofind.add(x);
                tofind.addAll(f.third);
                Triple<D, D, List<D>> found = J.find_fast(new Triple<>((D) "_1", f.second, tofind));
                if (found == null) {
                    throw new RuntimeException();
                }
                List<Pair<Triple<D, D, List<D>>, C>> g = new LinkedList<>();
                Pair<Triple<D, D, List<D>>, C> pr = new Pair<>(found, c);
                g.add(pr);
                if (src.global.allIds().contains(pr.second) && src.global.cat().arrows().contains(pr.first)) {
                    theta.put(new Pair<>(c, f), pr.first);
                } else {
                    Triple tr = new Triple<>("_1", c, g);
                    Object xxx = deltaI.find_fast(tr);
                    if (xxx == null) {
                        throw new RuntimeException("cannot find " + tr);
                    }
                    theta.put(new Pair<>(c, f), xxx);
                }
            }
        }
        List l = new LinkedList();
        l.add(theta);
        m.put(x, l);
    }
    for (Object o : dst.allTerms()) {
        if (m.containsKey(o)) {
            continue;
        }
        List l = new LinkedList();
        l.add(o);
        m.put(o, l);
    }
    return new XMapping(J, pideltaI, m, "homomorphism");
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Triple(catdata.Triple) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(catdata.Pair)

Aggregations

Triple (catdata.Triple)116 Pair (catdata.Pair)93 LinkedList (java.util.LinkedList)84 List (java.util.List)75 HashMap (java.util.HashMap)65 Map (java.util.Map)49 HashSet (java.util.HashSet)47 LinkedHashMap (java.util.LinkedHashMap)36 Set (java.util.Set)28 Chc (catdata.Chc)22 Util (catdata.Util)18 En (catdata.aql.exp.SchExpRaw.En)18 Ty (catdata.aql.exp.TyExpRaw.Ty)18 Ctx (catdata.Ctx)17 Sym (catdata.aql.exp.TyExpRaw.Sym)17 Collectors (java.util.stream.Collectors)17 Att (catdata.aql.exp.SchExpRaw.Att)16 Fk (catdata.aql.exp.SchExpRaw.Fk)16 Tuple3 (org.jparsec.functors.Tuple3)16 Quad (catdata.Quad)13