Search in sources :

Example 1 with KBOptions

use of catdata.provers.KBOptions 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)

Aggregations

Chc (catdata.Chc)1 Pair (catdata.Pair)1 Triple (catdata.Triple)1 Unit (catdata.Unit)1 Util (catdata.Util)1 FinSet (catdata.fqlpp.cat.FinSet)1 DefunctGlobalOptions (catdata.ide.DefunctGlobalOptions)1 OplJavaInst (catdata.opl.OplExp.OplJavaInst)1 OplSig (catdata.opl.OplExp.OplSig)1 DoNotIgnore (catdata.opl.OplParser.DoNotIgnore)1 KBExp (catdata.provers.KBExp)1 KBApp (catdata.provers.KBExp.KBApp)1 KBVar (catdata.provers.KBExp.KBVar)1 KBHorn (catdata.provers.KBHorn)1 KBOptions (catdata.provers.KBOptions)1 KBOrders (catdata.provers.KBOrders)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1