Search in sources :

Example 21 with Unit

use of catdata.Unit in project fql by CategoricalData.

the class OplToKB method convert.

@SuppressWarnings({ "unchecked", "rawtypes" })
private OplKB<C, V> convert(OplSig<S, C, V> s) {
    if (s.prec.keySet().size() != new HashSet<>(s.prec.values()).size()) {
        throw new RuntimeException("Cannot duplicate precedence: " + s.prec);
    }
    // if (!Collections.disjoint(Arrays.asList(KBHorn.reserved), s.symbols.keySet())) {
    // throw new RuntimeException("Theory contains reserved symbol, one of " + Arrays.toString(KBHorn.reserved));
    // }
    Map<C, Pair<List<S>, S>> symbols = new HashMap<>(s.symbols);
    List one = new LinkedList();
    one.add(new Unit());
    List two = new LinkedList();
    two.add(new Unit());
    two.add(new Unit());
    symbols.put((C) KBHorn._eq, new Pair(two, new Unit()));
    symbols.put((C) KBHorn._or, new Pair(two, new Unit()));
    symbols.put((C) KBHorn._not, new Pair(one, new Unit()));
    symbols.put((C) KBHorn._true, new Pair(new LinkedList(), new Unit()));
    symbols.put((C) KBHorn._false, new Pair(new LinkedList(), new Unit()));
    Function<Pair<C, C>, Boolean> gt = x -> {
        Integer l = s.prec.get(x.first);
        Integer r = s.prec.get(x.second);
        if (l != null && r != null) {
            return l > r;
        }
        if (l == null && r != null) {
            return false;
        }
        if (l != null) {
            return true;
        }
        String lx = x.first.toString();
        String rx = x.second.toString();
        if (!symbols.containsKey(x.first)) {
            throw new RuntimeException("Missing: " + x.first);
        }
        int la = symbols.get(x.first).first.size();
        int ra = symbols.get(x.second).first.size();
        if (la == ra) {
            if (lx.length() == rx.length()) {
                return lx.compareTo(rx) < 0;
            }
            return lx.length() < rx.length();
        }
        if (la >= 3 && ra >= 3) {
            return la > ra;
        }
        if (la == 0 && ra > 0) {
            return false;
        }
        if (la == 1 && (ra == 0 || ra == 2)) {
            return true;
        }
        if (la == 1 && ra > 2) {
            return false;
        }
        if (la == 2 && ra == 0) {
            return true;
        }
        if (la == 2 && (ra == 1 || ra > 2)) {
            return false;
        }
        if (la >= 3 || ra >= 3) {
            // added Aug 3 16
            return la > ra;
        }
        throw new RuntimeException("Bug in precedence, report to Ryan: la=" + la + ", ra=" + ra + ", l=null r=null");
    // function symbols: arity-0 < arity-2 < arity-1 < arity-3 < arity-4
    };
    Set<Pair<KBExp<C, V>, KBExp<C, V>>> eqs = new HashSet<>();
    for (Triple<?, OplTerm<C, V>, OplTerm<C, V>> eq : s.equations) {
        eqs.add(new Pair<>(convert(eq.second), convert(eq.third)));
    }
    Set<Pair<KBExp<C, V>, KBExp<C, V>>> rs = new HashSet<>();
    for (Triple<?, List<Pair<OplTerm<C, V>, OplTerm<C, V>>>, List<Pair<OplTerm<C, V>, OplTerm<C, V>>>> impl : s.implications) {
        rs.addAll(convert(impl.second, impl.third));
    }
    KBOptions options = new KBOptions(DefunctGlobalOptions.debug.opl.opl_prover_unfailing, DefunctGlobalOptions.debug.opl.opl_prover_sort, DefunctGlobalOptions.debug.opl.opl_allow_horn && !s.implications.isEmpty(), DefunctGlobalOptions.debug.opl.opl_prover_ac, DefunctGlobalOptions.debug.opl.opl_prover_timeout, DefunctGlobalOptions.debug.opl.opl_prover_reduction_limit, DefunctGlobalOptions.debug.opl.opl_prover_filter_subsumed, /* NEWDEBUG.debug.opl.simplify, */
    DefunctGlobalOptions.debug.opl.opl_prover_compose, false);
    return new OplKB(eqs, KBOrders.lpogt(DefunctGlobalOptions.debug.opl.opl_allow_horn && !s.implications.isEmpty(), gt), fr, rs, options);
}
Also used : OplJavaInst(catdata.opl.OplExp.OplJavaInst) KBExp(catdata.provers.KBExp) Chc(catdata.Chc) HashMap(java.util.HashMap) KBOptions(catdata.provers.KBOptions) Function(java.util.function.Function) KBOrders(catdata.provers.KBOrders) OplSig(catdata.opl.OplExp.OplSig) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) KBHorn(catdata.provers.KBHorn) Map(java.util.Map) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Iterator(java.util.Iterator) KBApp(catdata.provers.KBExp.KBApp) Collection(java.util.Collection) Util(catdata.Util) Set(java.util.Set) Unit(catdata.Unit) Collectors(java.util.stream.Collectors) FinSet(catdata.fqlpp.cat.FinSet) KBVar(catdata.provers.KBExp.KBVar) List(java.util.List) Invocable(javax.script.Invocable) DoNotIgnore(catdata.opl.OplParser.DoNotIgnore) Triple(catdata.Triple) Collections(java.util.Collections) DefunctGlobalOptions(catdata.ide.DefunctGlobalOptions) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Unit(catdata.Unit) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) List(java.util.List) KBOptions(catdata.provers.KBOptions) HashSet(java.util.HashSet) Pair(catdata.Pair)

Example 22 with Unit

use of catdata.Unit in project fql by CategoricalData.

the class Pi method lim2.

private static <OA, AA, OB, AB, AC> Set<Map> lim2(Functor<Triple<OA, OB, AC>, Quad<AA, AB, AC, AC>, Set, Fn> I) {
    Category<Triple<OA, OB, AC>, Quad<AA, AB, AC, AC>> B = I.source;
    if (B.objects().isEmpty()) {
        Set<Map> ret = new HashSet<>();
        Map m = new HashMap();
        m.put(0, new Unit());
        ret.add(m);
        return ret;
    }
    Triple<OA, OB, AC>[] cnames = cnames(I.source);
    int temp = 0;
    List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
    // store objects of B directly
    Map<String, Object> from = new HashMap<>();
    LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
    Map<Object, Set<Map<Object, Object>>> state = new HashMap<>();
    for (Triple<OA, OB, AC> n : B.objects()) {
        from.put("t" + temp, n);
        select.put("c" + temp, new Pair<>("t" + temp, "c0"));
        temp++;
        state.put(n, shred(I.applyO(n)));
    }
    for (Quad<AA, AB, AC, AC> e : B.arrows()) {
        if (B.isId(e)) {
            continue;
        }
        from.put("t" + temp, e);
        where.add(new Pair<>(new Pair<>("t" + temp, "c0"), new Pair<>("t" + cnamelkp(cnames, B.source(e)), "c0")));
        where.add(new Pair<>(new Pair<>("t" + temp, "c1"), new Pair<>("t" + cnamelkp(cnames, B.target(e)), "c0")));
        temp++;
        state.put(e, shred(I.applyA(e)));
    }
    Flower f = new Flower(select, from, where);
    Set<Map<Object, Object>> uuu = f.eval(state);
    Set<Map> x0 = unshred(B.objects().size(), uuu);
    x0 = keygen(x0);
    return x0;
}
Also used : Quad(catdata.Quad) Unit(catdata.Unit) Pair(catdata.Pair) Triple(catdata.Triple)

Example 23 with Unit

use of catdata.Unit in project fql by CategoricalData.

the class Signature method onlyObjects.

private Pair<Signature<O, A>, Mapping<O, A, O, A>> onlyObjects() {
    Signature<O, A> x = new Signature<>(nodes, new HashSet<Signature<O, A>.Edge>(), new HashSet<Signature<O, A>.Eq>(), new Unit());
    Map<Signature<O, A>.Node, Signature<O, A>.Node> nm = new HashMap<>();
    for (Signature<O, A>.Node k : nodes) {
        nm.put(k, k);
    }
    Map<Signature<O, A>.Edge, Signature<O, A>.Path> em = new HashMap<>();
    Mapping<O, A, O, A> m = new Mapping<>(nm, em, x, this);
    return new Pair<>(x, m);
}
Also used : HashMap(java.util.HashMap) Unit(catdata.Unit) Pair(catdata.Pair)

Example 24 with Unit

use of catdata.Unit in project fql by CategoricalData.

the class XProd method subst_new.

// going to need bigger schema here, for typing
/*	public static <C> Triple<C, C, List<C>> eval(List<String> eq, Map<String, Triple<C, C, List<C>>> tuple, XCtx S, XCtx<C> I) {
		Triple<C, C, List<C>> sofar = tuple.get(eq.get(0));
		if (sofar == null) {
			sofar = I.type(c)
		}

		///if (sofar == null) {
		//	return null;
		//}

		
	//	String s = eq.get(0);
		
		for (String edge : eq.subList(1, eq.size())) {
			Triple<C, C, List<C>> t = tuple.get(edge);
			if (t != null) {
				if (sofar == null) {
					sofar = t;
					continue;
				}
				sofar = I.cat().compose(sofar, t);
				continue;
			} 
			
			
		}
		
		return sofar;
	}
	*/
private static <C, D, E> List subst_new(List<D> eq, Map<E, Triple<C, C, List<C>>> tuple, Set<E> keys, Set xxx) {
    List ret = (List) eq.stream().flatMap(x -> {
        List l = new LinkedList<>();
        if (tuple.containsKey(x)) {
            l.add(tuple.get(x).first);
            l.addAll(tuple.get(x).third);
            return l.stream();
        } else if (keys.contains(x)) {
            l.add(x);
            xxx.add(new Unit());
            return l.stream();
        } else {
            l.add(x);
            return l.stream();
        }
    }).collect(Collectors.toList());
    return ret;
}
Also used : List(java.util.List) LinkedList(java.util.LinkedList) Unit(catdata.Unit) LinkedList(java.util.LinkedList)

Example 25 with Unit

use of catdata.Unit in project fql by CategoricalData.

the class FQLProgram method build.

private Graph<String, Object> build() {
    // Graph<V, E> where V is the type of the vertices
    Graph<String, Object> g2 = new DirectedSparseMultigraph<>();
    Ref<Integer> guid = new Ref<>(0);
    for (String k : insts.keySet()) {
        InstExp i = insts.get(k);
        i.type(this).toConst(this);
        g2.addVertex(k);
        i.accept(new Unit(), new InstExpVisitor<Unit, Unit>() {

            @Override
            public Unit visit(Unit env, Zero e) {
                return null;
            }

            @Override
            public Unit visit(Unit env, One e) {
                return null;
            }

            @Override
            public Unit visit(Unit env, Two e) {
                return null;
            }

            @Override
            public Unit visit(Unit env, Plus e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.a, k);
                g2.addEdge(new Pair<>(pp(guid), e), e.b, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, Times e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.a, k);
                g2.addEdge(new Pair<>(pp(guid), e), e.b, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, Exp e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.a, k);
                g2.addEdge(new Pair<>(pp(guid), e), e.b, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, Const e) {
                return null;
            }

            @Override
            public Unit visit(Unit env, Delta e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, Sigma e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, Pi e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, FullSigma e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, Relationalize e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, External e) {
                return null;
            }

            @Override
            public Unit visit(Unit env, Eval e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.e, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, FullEval e) {
                g2.addEdge(new Pair<>(pp(guid), e), e.e, k);
                return null;
            }

            @Override
            public Unit visit(Unit env, Kernel e) {
                TransExp t = transforms.get(e.trans);
                Pair<String, String> p = t.type(FQLProgram.this);
                g2.addEdge(new Pair<>(pp(guid), e), p.first, k);
                g2.addEdge(new Pair<>(pp(guid), e), p.second, k);
                return null;
            }

            private Integer pp(Ref<Integer> ref) {
                int ret = ref.x;
                ref.x = ret + 1;
                return ref.x;
            }

            @Override
            public Unit visit(Unit env, Step e) {
                // (Step) this should add an edge
                return null;
            }
        });
    }
    return g2;
}
Also used : FullEval(catdata.fql.decl.InstExp.FullEval) One(catdata.fql.decl.InstExp.One) FullSigma(catdata.fql.decl.InstExp.FullSigma) Sigma(catdata.fql.decl.InstExp.Sigma) Step(catdata.fql.decl.InstExp.Step) Unit(catdata.Unit) Pi(catdata.fql.decl.InstExp.Pi) Plus(catdata.fql.decl.InstExp.Plus) FullEval(catdata.fql.decl.InstExp.FullEval) Eval(catdata.fql.decl.InstExp.Eval) Kernel(catdata.fql.decl.InstExp.Kernel) Pair(catdata.Pair) Relationalize(catdata.fql.decl.InstExp.Relationalize) Zero(catdata.fql.decl.InstExp.Zero) Const(catdata.fql.decl.InstExp.Const) Two(catdata.fql.decl.InstExp.Two) Ref(catdata.Ref) Delta(catdata.fql.decl.InstExp.Delta) DirectedSparseMultigraph(edu.uci.ics.jung.graph.DirectedSparseMultigraph) Times(catdata.fql.decl.InstExp.Times) External(catdata.fql.decl.InstExp.External) FullSigma(catdata.fql.decl.InstExp.FullSigma) Exp(catdata.fql.decl.InstExp.Exp)

Aggregations

Unit (catdata.Unit)29 Pair (catdata.Pair)16 LinkedList (java.util.LinkedList)11 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10 List (java.util.List)9 Map (java.util.Map)8 Set (java.util.Set)8 Chc (catdata.Chc)6 Util (catdata.Util)6 Fn (catdata.fqlpp.cat.FinSet.Fn)6 JPanel (javax.swing.JPanel)6 GridLayout (java.awt.GridLayout)5 LinkedHashMap (java.util.LinkedHashMap)5 Collectors (java.util.stream.Collectors)5 FUNCTION (catdata.fqlpp.FUNCTION)4 CodeTextPanel (catdata.ide.CodeTextPanel)4 Entry (java.util.Map.Entry)4 Triple (catdata.Triple)3 Optional (java.util.Optional)3