Search in sources :

Example 1 with Tuple5

use of org.jparsec.functors.Tuple5 in project fql by CategoricalData.

the class PPParser method toValue.

private static Object toValue(Object o) {
    if (o.toString().equals("true")) {
        return true;
    }
    if (o.toString().equals("false")) {
        return false;
    }
    if (o instanceof Tuple5) {
        Tuple5 t = (Tuple5) o;
        if (t.a.toString().equals("(")) {
            return new Pair(toValue(t.b), toValue(t.d));
        }
        List l = (List) t.b;
        Map s = new HashMap();
        for (Object y : l) {
            Pair yy = (Pair) toValue(y);
            if (s.containsKey(yy.first)) {
                throw new RuntimeException("Duplicate domain entry in " + o);
            }
            s.put(yy.first, yy.second);
        }
        Tuple3 tt = (Tuple3) t.e;
        Set ui = (Set) toValue(tt.a);
        Set uj = (Set) toValue(tt.c);
        return new Fn(ui, uj, s::get);
    }
    if (o instanceof Tuple3) {
        Tuple3 p = (Tuple3) o;
        List l = (List) p.b;
        Set s = new HashSet();
        for (Object y : l) {
            s.add(toValue(y));
        }
        return s;
    }
    if (o instanceof org.jparsec.functors.Pair) {
        org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) o;
        if (p.a.toString().equals("inl")) {
            return Chc.inLeft(toValue(p.b));
        } else if (p.a.toString().equals("inr")) {
            return Chc.inRight(toValue(p.b));
        } else {
            return new Unit();
        }
    }
    return o.toString();
}
Also used : Set(java.util.Set) ToSet(catdata.fqlpp.TransExp.ToSet) HashSet(java.util.HashSet) SetSet(catdata.fqlpp.TransExp.SetSet) HashMap(java.util.HashMap) Fn(catdata.fqlpp.cat.FinSet.Fn) Unit(catdata.Unit) Tuple5(org.jparsec.functors.Tuple5) Scanners(org.jparsec.Scanners) Plus(catdata.fqlpp.CatExp.Plus) ApplyTrans(catdata.fqlpp.TransExp.ApplyTrans) Parsers(org.jparsec.Parsers) Times(catdata.fqlpp.CatExp.Times) Terminals(org.jparsec.Terminals) AndOrNotImplies(catdata.fqlpp.TransExp.AndOrNotImplies) Tuple3(org.jparsec.functors.Tuple3) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) ToMap(catdata.fqlpp.TransExp.ToMap) HashMap(java.util.HashMap) Pair(catdata.Pair) HashSet(java.util.HashSet)

Example 2 with Tuple5

use of org.jparsec.functors.Tuple5 in project fql by CategoricalData.

the class PPParser method toFn.

private static FnExp toFn(Object o) {
    try {
        Tuple5 t5 = (Tuple5) o;
        if (t5.a.toString().equals("apply")) {
            Tuple5 t = (Tuple5) o;
            String f = t.b.toString();
            FnExp e = toFn(t.e);
            return new FnExp.Apply(f, e);
        }
        Tuple3 t3 = (Tuple3) t5.e;
        Map s = new HashMap<>();
        List l = (List) t5.b;
        for (Object y : l) {
            Pair yy = (Pair) toValue(y);
            if (s.containsKey(yy.first)) {
                throw new RuntimeException("Duplicate domain entry in " + o);
            }
            s.put(yy.first, yy.second);
        }
        SetExp t3a = toSet(t3.a);
        SetExp t3c = toSet(t3.c);
        return new Const(s::get, t3a, t3c);
    } catch (Exception e) {
    }
    try {
        Tuple3 p = (Tuple3) o;
        Object p2 = p.b;
        Object p3 = p.c;
        Object o1 = p.a;
        String p1 = p.a.toString();
        if (p1.equals("fst")) {
            return new FnExp.Fst(toSet(p2), toSet(p3));
        } else if (p1.equals("snd")) {
            return new FnExp.Snd(toSet(p2), toSet(p3));
        } else if (p1.equals("inl")) {
            return new FnExp.Inl(toSet(p2), toSet(p3));
        } else if (p1.equals("inr")) {
            return new FnExp.Inr(toSet(p2), toSet(p3));
        } else if (p1.equals("iso1")) {
            return new FnExp.Iso(true, toSet(p2), toSet(p3));
        } else if (p1.equals("iso2")) {
            return new FnExp.Iso(false, toSet(p2), toSet(p3));
        } else if (p1.equals("eval")) {
            return new FnExp.Eval(toSet(p2), toSet(p3));
        } else if (p2.toString().equals(";")) {
            return new FnExp.Comp(toFn(o1), toFn(p3));
        } else if (p2.toString().equals("*")) {
            return new FnExp.Prod(toFn(o1), toFn(p3));
        } else if (p2.toString().equals("+")) {
            return new FnExp.Case(toFn(o1), toFn(p3));
        }
        if (p1.equals("apply")) {
            return new FnExp.ApplyTrans(p2.toString(), toSet(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 FnExp.Id(toSet(p2));
            case "curry":
                return new FnExp.Curry(toFn(p2));
            case "ff":
                return new FnExp.FF(toSet(p2));
            case "tt":
                return new FnExp.TT(toSet(p2));
            case "char":
                return new FnExp.Chr(toFn(p2));
            case "kernel":
                return new Krnl(toFn(p2));
            default:
                break;
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof String) {
        return new FnExp.Var(o.toString());
    }
    if (o.toString().equals("true")) {
        return new Tru("true");
    } else if (o.toString().equals("false")) {
        return new Tru("false");
    } else if (o.toString().equals("and")) {
        return new Tru("and");
    } else if (o.toString().equals("or")) {
        return new Tru("or");
    } else if (o.toString().equals("not")) {
        return new Tru("not");
    } else if (o.toString().equals("implies")) {
        return new Tru("implies");
    }
    throw new RuntimeException();
}
Also used : Curry(catdata.fqlpp.FunctorExp.Curry) FF(catdata.fqlpp.FunctorExp.FF) TT(catdata.fqlpp.FunctorExp.TT) HashMap(java.util.HashMap) PeterApply(catdata.fqlpp.TransExp.PeterApply) Apply(catdata.fqlpp.FunctorExp.Apply) Inl(catdata.fqlpp.FunctorExp.Inl) Var(catdata.fqlpp.FunctorExp.Var) Chr(catdata.fqlpp.TransExp.Chr) Prod(catdata.fqlpp.FunctorExp.Prod) CoProd(catdata.fqlpp.TransExp.CoProd) Tru(catdata.fqlpp.FnExp.Tru) List(java.util.List) LinkedList(java.util.LinkedList) Eval(catdata.fqlpp.FunctorExp.Eval) Pair(catdata.Pair) Iso(catdata.fqlpp.FunctorExp.Iso) Fst(catdata.fqlpp.FunctorExp.Fst) Krnl(catdata.fqlpp.FnExp.Krnl) Const(catdata.fqlpp.FnExp.Const) CatConst(catdata.fqlpp.FunctorExp.CatConst) SetSetConst(catdata.fqlpp.FunctorExp.SetSetConst) InstConst(catdata.fqlpp.FunctorExp.InstConst) MapConst(catdata.fqlpp.FunctorExp.MapConst) FinalConst(catdata.fqlpp.FunctorExp.FinalConst) ApplyTrans(catdata.fqlpp.TransExp.ApplyTrans) Tuple5(org.jparsec.functors.Tuple5) Scanners(org.jparsec.Scanners) Plus(catdata.fqlpp.CatExp.Plus) ApplyTrans(catdata.fqlpp.TransExp.ApplyTrans) Parsers(org.jparsec.Parsers) Times(catdata.fqlpp.CatExp.Times) Terminals(org.jparsec.Terminals) AndOrNotImplies(catdata.fqlpp.TransExp.AndOrNotImplies) Tuple3(org.jparsec.functors.Tuple3) Id(catdata.fqlpp.FunctorExp.Id) Map(java.util.Map) ToMap(catdata.fqlpp.TransExp.ToMap) HashMap(java.util.HashMap)

Example 3 with Tuple5

use of org.jparsec.functors.Tuple5 in project fql by CategoricalData.

the class PPParser method toSet.

private static SetExp toSet(Object o) {
    try {
        Tuple5 t = (Tuple5) o;
        String f = t.b.toString();
        SetExp e = toSet(t.e);
        return new SetExp.Apply(f, e);
    } catch (Exception e) {
    }
    try {
        int i = Integer.parseInt(o.toString());
        return new Numeral(i);
    } catch (Exception e) {
    }
    try {
        Tuple3<?, ?, ?> t = (Tuple3<?, ?, ?>) o;
        String y = t.b.toString();
        if (y.equals("+")) {
            return new SetExp.Plus(toSet(t.a), toSet(t.c));
        } else if (y.equals("*")) {
            return new SetExp.Times(toSet(t.a), toSet(t.c));
        } else if (y.equals("^")) {
            return new SetExp.Exp(toSet(t.a), toSet(t.c));
        } else if (y.equals("union")) {
            return new SetExp.Union(toSet(t.a), toSet(t.c));
        } else if (y.equals("intersect")) {
            return new Intersect(toSet(t.a), toSet(t.c));
        } else if (t.a.toString().equals("{")) {
            List tb = (List) t.b;
            Set x = new HashSet();
            for (Object uu : tb) {
                x.add(toValue(uu));
            }
            return new SetExp.Const(x);
        }
    } catch (RuntimeException cce) {
    }
    try {
        org.jparsec.functors.Pair<?, ?> p = (org.jparsec.functors.Pair<?, ?>) o;
        if (p.a.toString().equals("dom")) {
            return new SetExp.Dom(toFn(p.b));
        } else if (p.a.toString().equals("cod")) {
            return new SetExp.Cod(toFn(p.b));
        } else if (p.a.toString().equals("range")) {
            return new Range(toFn(p.b));
        }
    } catch (RuntimeException cce) {
    }
    try {
        if (o.toString().equals("void")) {
            return new SetExp.Zero();
        } else if (o.toString().equals("unit")) {
            return new SetExp.One();
        } else if (o.toString().equals("prop")) {
            return new SetExp.Prop();
        }
        throw new RuntimeException();
    } catch (RuntimeException cce) {
    }
    return new SetExp.Var(o.toString());
}
Also used : Numeral(catdata.fqlpp.SetExp.Numeral) Set(java.util.Set) ToSet(catdata.fqlpp.TransExp.ToSet) HashSet(java.util.HashSet) SetSet(catdata.fqlpp.TransExp.SetSet) PeterApply(catdata.fqlpp.TransExp.PeterApply) Apply(catdata.fqlpp.FunctorExp.Apply) Var(catdata.fqlpp.FunctorExp.Var) Intersect(catdata.fqlpp.SetExp.Intersect) List(java.util.List) LinkedList(java.util.LinkedList) Plus(catdata.fqlpp.CatExp.Plus) HashSet(java.util.HashSet) Pair(catdata.Pair) Zero(catdata.fqlpp.FunctorExp.Zero) Dom(catdata.fqlpp.FunctorExp.Dom) Prop(catdata.fqlpp.FunctorExp.Prop) Range(catdata.fqlpp.SetExp.Range) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) Exp(catdata.fqlpp.FunctorExp.Exp)

Example 4 with Tuple5

use of org.jparsec.functors.Tuple5 in project fql by CategoricalData.

the class PPParser method toFinalConst.

private static FunctorExp toFinalConst(Object decl) {
    Tuple5 y = (Tuple5) decl;
    org.jparsec.functors.Pair x = (org.jparsec.functors.Pair) y.a;
    Tuple3 nodes = (Tuple3) x.a;
    Tuple3 arrows = (Tuple3) x.b;
    List nodes0 = (List) nodes.b;
    List arrows0 = (List) arrows.b;
    Map<String, FunctorExp> nodesX = new HashMap<>();
    for (Object o : nodes0) {
        if (nodesX.containsKey(o)) {
            throw new RuntimeException("Duplicate object: " + o + " in " + decl);
        }
        Tuple3 u = (Tuple3) o;
        String n = (String) u.a;
        FunctorExp l = toFtr(u.c);
        nodesX.put(n, l);
    }
    Map<String, TransExp> arrowsX = new HashMap<>();
    for (Object o : arrows0) {
        if (arrowsX.containsKey(o)) {
            throw new RuntimeException("Duplicate arrow: " + o + " in " + decl);
        }
        Tuple3 u = (Tuple3) o;
        String n = (String) u.a;
        TransExp l = toTrans(u.c);
        arrowsX.put(n, l);
    }
    FinalConst ret = new FinalConst(toCat(y.c), toCat(y.e), nodesX, arrowsX);
    return ret;
}
Also used : HashMap(java.util.HashMap) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) List(java.util.List) LinkedList(java.util.LinkedList) FinalConst(catdata.fqlpp.FunctorExp.FinalConst) Pair(catdata.Pair)

Example 5 with Tuple5

use of org.jparsec.functors.Tuple5 in project fql by CategoricalData.

the class OplParser method toTheory.

private static OplExp toTheory(Object o) {
    Tuple5 t = (Tuple5) o;
    List<String> imports = t.a == null ? new LinkedList<>() : (List<String>) ((org.jparsec.functors.Pair) t.a).b;
    Tuple3 a = (Tuple3) t.b;
    Tuple3 b = (Tuple3) t.c;
    Tuple3 c = (Tuple3) t.d;
    Tuple3 d = (Tuple3) t.e;
    Set<String> sorts = a == null ? new HashSet<>() : new HashSet<>((Collection<String>) a.b);
    List<Tuple3> symbols0 = b == null ? new LinkedList<>() : (List<Tuple3>) b.b;
    List<org.jparsec.functors.Pair> equations0 = c == null ? new LinkedList<>() : (List<org.jparsec.functors.Pair>) c.b;
    List<org.jparsec.functors.Pair> implications0 = new LinkedList<>();
    if (d != null) {
        implications0 = (List<org.jparsec.functors.Pair>) d.b;
    }
    Map<String, Pair<List<String>, String>> symbols = new HashMap<>();
    Map<String, Integer> prec = new HashMap<>();
    for (Tuple3 x : symbols0) {
        String dom;
        List<String> args;
        if (x.c instanceof Tuple3) {
            Tuple3 zzz = (Tuple3) x.c;
            args = (List<String>) zzz.a;
            dom = (String) zzz.c;
        } else {
            dom = (String) x.c;
            args = new LinkedList<>();
        }
        List<org.jparsec.functors.Pair> name0s = (List<org.jparsec.functors.Pair>) x.a;
        for (org.jparsec.functors.Pair name0 : name0s) {
            // org.jparsec.functors.Pair name0 =
            // (org.jparsec.functors.Pair) x.a;
            String name = (String) name0.a;
            if (name0.b != null) {
                org.jparsec.functors.Pair zzz = (org.jparsec.functors.Pair) name0.b;
                Integer i = (Integer) zzz.b;
                prec.put(name, i);
            }
            if (symbols.containsKey(name)) {
                throw new DoNotIgnore("Duplicate symbol " + name);
            }
            symbols.put(name, new Pair<>(args, dom));
        }
    }
    if (sorts.contains("Nat") && symbols.keySet().contains("zero") && symbols.keySet().contains("succ") && DefunctGlobalOptions.debug.opl.opl_desugar_nat) {
        sugarForNat = true;
    }
    List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> equations = new LinkedList<>();
    for (org.jparsec.functors.Pair<Tuple3, Tuple3> x : equations0) {
        List<Tuple3> fa = x.a == null ? new LinkedList<>() : (List<Tuple3>) x.a.b;
        OplCtx<String, String> ctx = toCtx(fa);
        Tuple3 eq = x.b;
        OplTerm lhs = toTerm(ctx.names(), consts(symbols), eq.a, false);
        OplTerm rhs = toTerm(ctx.names(), consts(symbols), eq.c, false);
        equations.add(new Triple<>(ctx, lhs, rhs));
    }
    List<Triple<OplCtx<String, String>, List<Pair<OplTerm<String, String>, OplTerm<String, String>>>, List<Pair<OplTerm<String, String>, OplTerm<String, String>>>>> implications = new LinkedList<>();
    for (org.jparsec.functors.Pair<Tuple3, Tuple3> x : implications0) {
        List<Tuple3> fa = x.a == null ? new LinkedList<>() : (List<Tuple3>) x.a.b;
        OplCtx<String, String> ctx = toCtx(fa);
        Tuple3 eq = x.b;
        List<Tuple3> lhs0 = (List<Tuple3>) eq.a;
        List<Tuple3> rhs0 = (List<Tuple3>) eq.c;
        List<Pair<OplTerm<String, String>, OplTerm<String, String>>> lhs = new LinkedList<>();
        List<Pair<OplTerm<String, String>, OplTerm<String, String>>> rhs = new LinkedList<>();
        for (Tuple3 obj : lhs0) {
            OplTerm lhsX = toTerm(ctx.names(), consts(symbols), obj.a, false);
            OplTerm rhsX = toTerm(ctx.names(), consts(symbols), obj.c, false);
            lhs.add(new Pair<>(lhsX, rhsX));
        }
        for (Tuple3 obj : rhs0) {
            OplTerm lhsX = toTerm(ctx.names(), consts(symbols), obj.a, false);
            OplTerm rhsX = toTerm(ctx.names(), consts(symbols), obj.c, false);
            rhs.add(new Pair<>(lhsX, rhsX));
        }
        implications.add(new Triple<>(ctx, lhs, rhs));
    }
    OplSig ret = new OplSig<>(new VIt(), prec, sorts, symbols, equations, implications);
    ret.imports = new HashSet<>(imports);
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) OplSig(catdata.opl.OplExp.OplSig) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) Collection(java.util.Collection)

Aggregations

Tuple5 (org.jparsec.functors.Tuple5)47 Tuple3 (org.jparsec.functors.Tuple3)41 Pair (catdata.Pair)36 LinkedList (java.util.LinkedList)36 List (java.util.List)36 HashMap (java.util.HashMap)21 Tuple4 (org.jparsec.functors.Tuple4)20 Triple (catdata.Triple)10 HashSet (java.util.HashSet)10 LinkedHashMap (java.util.LinkedHashMap)10 XPair (catdata.fpql.XExp.XPair)9 Map (java.util.Map)8 Set (java.util.Set)5 Apply (catdata.fqlpp.FunctorExp.Apply)4 Var (catdata.fqlpp.FunctorExp.Var)4 PeterApply (catdata.fqlpp.TransExp.PeterApply)4 Pair (org.jparsec.functors.Pair)4 XSchema (catdata.fpql.XExp.XSchema)3 Plus (catdata.fqlpp.CatExp.Plus)3 Program (catdata.Program)2