Search in sources :

Example 11 with RawTerm

use of catdata.aql.RawTerm in project fql by CategoricalData.

the class AqlViewer method viewMorphism.

private static <Ty, En1, Sym, Fk1, Att1, Gen1, Sk1, En2, Fk2, Att2, Gen2, Sk2> JComponent viewMorphism(Morphism m, AqlJs js) {
    CodeTextPanel input = new CodeTextPanel("Input term-in-ctx", "");
    CodeTextPanel output = new CodeTextPanel("Output term-in-ctx", "");
    JButton nf = new JButton("Translate");
    JPanel buttonPanel = new JPanel(new GridLayout(1, 1));
    buttonPanel.add(nf);
    // TODO: aql does not position correctly
    Split split = new Split(.5, JSplitPane.VERTICAL_SPLIT);
    split.add(input);
    split.add(output);
    JPanel main = new JPanel(new BorderLayout());
    main.add(split, BorderLayout.CENTER);
    main.add(buttonPanel, BorderLayout.NORTH);
    nf.addActionListener(x -> {
        try {
            Pair<List<Pair<String, String>>, RawTerm> y = AqlParser.getParser().parseTermInCtx(input.getText());
            Triple<Ctx<Var, Chc<Ty, En1>>, Term<Ty, En1, Sym, Fk1, Att1, Gen1, Sk1>, Term<Ty, En1, Sym, Fk1, Att1, Gen1, Sk1>> z = RawTerm.infer2(y.first, y.second, y.second, m.src(), js);
            Pair<Ctx<Var, Chc<Ty, En2>>, Term<Ty, En2, Sym, Fk2, Att2, Gen2, Sk2>> a = m.translate(z.first, z.second);
            output.setText(a.first.toString() + a.second);
        } catch (Exception ex) {
            ex.printStackTrace();
            output.setText(ex.getMessage());
        }
    });
    return main;
}
Also used : JPanel(javax.swing.JPanel) Ty(catdata.aql.exp.TyExpRaw.Ty) Ctx(catdata.Ctx) JButton(javax.swing.JButton) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) RawTerm(catdata.aql.RawTerm) GridLayout(java.awt.GridLayout) BorderLayout(java.awt.BorderLayout) CodeTextPanel(catdata.ide.CodeTextPanel) List(java.util.List) LinkedList(java.util.LinkedList) Split(catdata.ide.Split)

Example 12 with RawTerm

use of catdata.aql.RawTerm in project fql by CategoricalData.

the class InstExpQuotient method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>();
    for (Pair<RawTerm, RawTerm> eq : eqs) {
        try {
            Map<String, Chc<Ty, En>> ctx = Collections.emptyMap();
            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.first, eq.second, null, col, "", J.schema().typeSide.js).first3();
            if (J.type(eq0.second).left) {
                throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.second + " at type " + J.type(eq0.second).l);
            }
            if (J.type(eq0.third).left) {
                throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.third + " at type " + J.type(eq0.third).l);
            }
            eqs0.add(new Pair<>(eq0.second, eq0.third));
            col.eqs.add(new Eq<>(new Ctx<>(), eq0.second, eq0.third));
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            throw new LocException(find("equations", eq), "In equation " + eq.first + " = " + eq.second + ", " + ex.getMessage());
        }
    }
    AqlOptions strat = new AqlOptions(options, col, env.defaults);
    InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
    return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) ID(catdata.aql.It.ID) 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) RawTerm(catdata.aql.RawTerm) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) Chc(catdata.Chc)

Example 13 with RawTerm

use of catdata.aql.RawTerm in project fql by CategoricalData.

the class InstExpQuotient method toString.

@Override
public synchronized String toString() {
    if (toString != null) {
        return toString;
    }
    toString = "";
    List<String> temp = new LinkedList<>();
    if (!eqs.isEmpty()) {
        toString += "\tequations";
        temp = new LinkedList<>();
        for (Pair<RawTerm, RawTerm> sym : Util.alphabetical(eqs)) {
            temp.add(sym.first + " = " + sym.second);
        }
        if (eqs.size() < 9) {
            toString += "\n\t\t" + Util.sep(temp, "\n\t\t") + "\n";
        } else {
            int step = 3;
            int longest = 32;
            for (String s : temp) {
                if (s.length() > longest) {
                    longest = s.length() + 4;
                }
            }
            for (int i = 0; i < temp.size(); i += step) {
                StringBuilder sb = new StringBuilder();
                Formatter formatter = new Formatter(sb, Locale.US);
                List<String> args = new LinkedList<>();
                List<String> format = new LinkedList<>();
                for (int j = i; j < Integer.min(temp.size(), i + step); j++) {
                    args.add(temp.get(j));
                    format.add("%-" + longest + "s");
                }
                String x = formatter.format(Util.sep(format, ""), args.toArray(new String[0])).toString();
                formatter.close();
                toString += "\n\t\t" + x;
            }
            toString += "\n";
        }
    }
    if (!options.isEmpty()) {
        toString += "\toptions";
        temp = new LinkedList<>();
        for (Entry<String, String> sym : options.entrySet()) {
            temp.add(sym.getKey() + " = " + sym.getValue());
        }
        toString += "\n\t\t" + Util.sep(temp, "\n\t\t") + "\n";
    }
    return "quotient " + I + "{\n" + toString + "}";
}
Also used : Formatter(java.util.Formatter) RawTerm(catdata.aql.RawTerm) LinkedList(java.util.LinkedList)

Example 14 with RawTerm

use of catdata.aql.RawTerm in project fql by CategoricalData.

the class InstExpRaw method eval.

@Override
public synchronized Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(sch.collage());
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>();
    for (String k : imports) {
        @SuppressWarnings("unchecked") Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> v = env.defs.insts.get(k);
        col.addAll(v.collage());
        eqs0.addAll(v.eqs());
    }
    for (Pair<String, String> p : gens) {
        String gen = p.first;
        String ty = p.second;
        if (col.ens.contains(new En(ty))) {
            col.gens.put(new Gen(gen), new En(ty));
        } else if (col.tys.contains(new Ty(ty))) {
            col.sks.put(new Sk(gen), new Ty(ty));
        } else {
            throw new LocException(find("generators", p), "The sort for " + gen + ", namely " + ty + ", is not declared as a type or entity");
        }
    }
    for (Pair<RawTerm, RawTerm> eq : eqs) {
        try {
            Map<String, Chc<Ty, En>> ctx = Collections.emptyMap();
            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.first, eq.second, null, col, "", sch.typeSide.js).first3();
            eqs0.add(new Pair<>(eq0.second, eq0.third));
            col.eqs.add(new Eq<>(new Ctx<>(), eq0.second, eq0.third));
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            throw new LocException(find("equations", eq), "In equation " + eq.first + " = " + eq.second + ", " + ex.getMessage());
        }
    }
    AqlOptions strat = new AqlOptions(options, col, env.defaults);
    boolean interpret_as_algebra = (boolean) strat.getOrDefault(AqlOption.interpret_as_algebra);
    boolean dont_check_closure = (boolean) strat.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
    if (interpret_as_algebra) {
        Ctx<En, Set<Gen>> ens0x = new Ctx<>(Util.revS(col.gens.map));
        Ctx<En, Collection<Gen>> ens0 = ens0x.map(x -> (Collection<Gen>) x);
        if (!col.sks.isEmpty()) {
            throw new RuntimeException("Cannot have generating labelled nulls with import_as_theory");
        }
        Ctx<Ty, Collection<Null<?>>> tys0 = new Ctx<>();
        for (Ty ty : sch.typeSide.tys) {
            tys0.put(ty, new HashSet<>());
        }
        Ctx<Gen, Ctx<Fk, Gen>> fks0 = new Ctx<>();
        Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0 = new Ctx<>();
        for (Gen gen : col.gens.keySet()) {
            fks0.put(gen, new Ctx<>());
            atts0.put(gen, new Ctx<>());
        }
        for (Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> e : eqs0) {
            Term<Ty, En, Sym, Fk, Att, Gen, Sk> lhs = e.first;
            Term<Ty, En, Sym, Fk, Att, Gen, Sk> rhs = e.second;
            if (rhs.gen != null && lhs.fk != null && lhs.arg.gen != null) {
                fks0.get(lhs.arg.gen).put(lhs.fk, rhs.gen);
            } else if (lhs.gen != null && rhs.fk != null && rhs.arg.gen != null) {
                fks0.get(rhs.arg.gen).put(rhs.fk, lhs.gen);
            } else if (rhs.obj != null && lhs.att != null && lhs.arg.gen != null) {
                atts0.get(lhs.arg.gen).put(lhs.att, Term.Obj(rhs.obj, rhs.ty));
            } else if (lhs.obj != null && rhs.att != null && rhs.arg.gen != null) {
                atts0.get(rhs.arg.gen).put(rhs.att, Term.Obj(lhs.obj, lhs.ty));
            } else {
                throw new RuntimeException("import_as_theory not compatible with equation " + lhs + " = " + rhs + "; each equation must be of the form gen.fk=gen or gen.att=javaobject");
            }
        }
        Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr = new Ctx<>();
        for (Gen gen : col.gens.keySet()) {
            for (Att att : sch.attsFrom(col.gens.get(gen))) {
                if (!atts0.get(gen).containsKey(att)) {
                    atts0.get(gen).put(att, InstExpImport.objectToSk(sch, null, gen, att, tys0, extraRepr, false, false));
                }
            }
        }
        ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dont_check_closure);
        return new SaturatedInstance(alg, alg, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
    }
    InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial = new InitialAlgebra<>(strat, sch, col, new It(), Object::toString, Object::toString);
    return new LiteralInstance<>(sch, col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Null(catdata.Null) Ctx(catdata.Ctx) ID(catdata.aql.It.ID) HashSet(java.util.HashSet) ImportAlgebra(catdata.aql.ImportAlgebra) Ty(catdata.aql.exp.TyExpRaw.Ty) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) Term(catdata.aql.Term) Gen(catdata.aql.exp.InstExpRaw.Gen) Collection(java.util.Collection) SaturatedInstance(catdata.aql.fdm.SaturatedInstance) Collage(catdata.aql.Collage) HashSet(java.util.HashSet) Set(java.util.Set) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) Pair(catdata.Pair) Fk(catdata.aql.exp.SchExpRaw.Fk) RawTerm(catdata.aql.RawTerm) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Chc(catdata.Chc)

Example 15 with RawTerm

use of catdata.aql.RawTerm in project fql by CategoricalData.

the class CombinatorParser method trans.

private static Parser<Trans> trans() {
    Parser<List<catdata.Pair<LocStr, RawTerm>>> gens = env(term(), "->");
    // Parser<List<catdata.Pair<LocStr, RawTerm>>> gens2 =
    // env_backwards(term(), "<-");
    // Parser<List<catdata.Pair<LocStr, RawTerm>>> gens = Parsers.or(gens2,
    // gens1);
    Parser<Pair<List<catdata.Pair<LocStr, RawTerm>>, List<catdata.Pair<String, String>>>> pa = Parsers.tuple(gens.optional(), options);
    Parser<Trans> ret = Parsers.tuple(token("{"), pa, token("}")).map(x -> new Trans(Util.newIfNull(x.b.a), x.b.b));
    return ret;
}
Also used : List(java.util.List) LinkedList(java.util.LinkedList) RawTerm(catdata.aql.RawTerm) PragmaExpToJdbcTrans(catdata.aql.exp.PragmaExp.PragmaExpToJdbcTrans) Trans(catdata.aql.exp.QueryExpRaw.Trans) PragmaExpToCsvTrans(catdata.aql.exp.PragmaExp.PragmaExpToCsvTrans) Pair(org.jparsec.functors.Pair)

Aggregations

RawTerm (catdata.aql.RawTerm)23 LinkedList (java.util.LinkedList)18 Ty (catdata.aql.exp.TyExpRaw.Ty)15 List (java.util.List)15 En (catdata.aql.exp.SchExpRaw.En)13 Sym (catdata.aql.exp.TyExpRaw.Sym)13 Att (catdata.aql.exp.SchExpRaw.Att)12 Fk (catdata.aql.exp.SchExpRaw.Fk)12 Ctx (catdata.Ctx)11 AqlOptions (catdata.aql.AqlOptions)11 Term (catdata.aql.Term)11 Chc (catdata.Chc)10 Pair (catdata.Pair)10 Triple (catdata.Triple)9 Collage (catdata.aql.Collage)9 Gen (catdata.aql.exp.InstExpRaw.Gen)8 Sk (catdata.aql.exp.InstExpRaw.Sk)8 HashSet (java.util.HashSet)8 Var (catdata.aql.Var)7 HashMap (java.util.HashMap)7