Search in sources :

Example 36 with Node

use of suite.node.Node in project suite by stupidsing.

the class Fractional method format.

public Node format(Fract<I> fract) {
    OpGroup add = ex.add;
    OpGroup mul = ex.mul;
    Fun2<I, I, Node> f = (n, d) -> {
        Node i0 = format_.apply(n);
        Node i1 = format_.apply(d);
        return mul.apply(i0, mul.inverse(i1));
    };
    I n_ = fract.t0;
    I d_ = fract.t1;
    I nn = neg_.apply(n_);
    if (0 <= sgn_.apply(n_))
        return f.apply(n_, d_);
    else
        return add.inverse(f.apply(nn, d_));
}
Also used : Ring(suite.math.sym.Sym.Ring) Pattern(suite.BindArrayUtil.Pattern) OpGroup(suite.math.sym.Express.OpGroup) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) Opt(suite.adt.Opt) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) Fun2(suite.util.FunUtil2.Fun2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) Field(suite.math.sym.Sym.Field) SwitchNode(suite.node.io.SwitchNode) Int(suite.node.Int) OpGroup(suite.math.sym.Express.OpGroup) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode)

Example 37 with Node

use of suite.node.Node in project suite by stupidsing.

the class Formatter method format.

/**
 * Converts a node to its string representation.
 *
 * @param node
 *            Node to be converted.
 * @param parentPrec
 *            Minimum operator precedence without adding parentheses.
 */
private void format(Node node0, int parentPrec) {
    Node node = node0.finalNode();
    Integer objectId = System.identityHashCode(node);
    // avoids infinite recursion if object is recursive
    if (set.add(objectId)) {
        format_(node, parentPrec);
        set.remove(objectId);
    } else
        sb.append("<<recurse>>");
}
Also used : Node(suite.node.Node)

Example 38 with Node

use of suite.node.Node in project suite by stupidsing.

the class Formatter method format_.

private void format_(Node node, int parentPrec) {
    new // 
    SwitchNode<Node>(// 
    node).doIf(Atom.class, n -> {
        sb.append(quoteAtomIfRequired(n.name));
    }).doIf(Data.class, n -> {
        Object data = n.data;
        if (data instanceof Chars)
            sb.append("Chars<" + quoteStringIfRequired(data.toString()) + ">");
        else if (data instanceof Node)
            sb.append("Data<" + data.toString() + ">");
        else
            sb.append("Data<" + data.getClass().getSimpleName() + ">");
    }).doIf(Dict.class, n -> {
        sb.append("dict<");
        for (Entry<Node, Reference> e : n.map.entrySet()) {
            format(e.getKey(), TermOp.getLeftPrec(TermOp.AND___));
            sb.append(":");
            format(e.getValue(), TermOp.getLeftPrec(TermOp.AND___));
            sb.append(",");
        }
        sb.append(">");
    }).doIf(Int.class, n -> {
        sb.append(n.number);
    }).doIf(Reference.class, n -> {
        sb.append(n.name());
    }).doIf(Str.class, n -> {
        sb.append(quoteStringIfRequired(n.value));
    }).doIf(Tree.class, n -> {
        formatTree(parentPrec, n);
    }).doIf(Tuple.class, n -> {
        sb.append("tuple<");
        for (Node n_ : n.nodes) {
            format(n_, TermOp.getLeftPrec(TermOp.AND___));
            sb.append(", ");
        }
        sb.append(">");
    }).doIf(Node.class, n -> {
        sb.append(n.getClass().getSimpleName() + '@' + Integer.toHexString(System.identityHashCode(n)));
    }).nonNullResult();
}
Also used : Reference(suite.node.Reference) Set(java.util.Set) Chars(suite.primitive.Chars) Tree(suite.node.Tree) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) Data(suite.node.Data) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Str(suite.node.Str) Entry(java.util.Map.Entry) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Chars(suite.primitive.Chars) Atom(suite.node.Atom)

Example 39 with Node

use of suite.node.Node in project suite by stupidsing.

the class Grapher method graph_.

private int graph_(Map<IdentityKey<Node>, Integer> ids, Node node) {
    IdentityKey<Node> key = IdentityKey.of(node);
    Integer id = ids.get(key);
    if (id == null) {
        ids.put(key, id = gns.size());
        gns.add(null);
        NodeRead nr = NodeRead.of(node);
        List<IntIntPair> children = // 
        Read.from(// 
        nr.children).map(// 
        p -> IntIntPair.of(graph_(ids, p.t0), graph_(ids, p.t1))).toList();
        gns.set(id, new GN(nr.type, nr.terminal, nr.op, children));
    }
    return id;
}
Also used : DataInputStream(java.io.DataInputStream) Read(suite.streamlet.Read) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) DataOutputStream(java.io.DataOutputStream) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Reference(suite.node.Reference) Trail(suite.lp.Trail) IntIntPair(suite.primitive.adt.pair.IntIntPair) Set(java.util.Set) IOException(java.io.IOException) NodeRead(suite.node.io.Rewrite_.NodeRead) NodeHead(suite.node.io.Rewrite_.NodeHead) IntObjMap(suite.primitive.adt.map.IntObjMap) Tree(suite.node.Tree) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Entry(java.util.Map.Entry) As(suite.streamlet.As) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) NodeRead(suite.node.io.Rewrite_.NodeRead) Node(suite.node.Node) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 40 with Node

use of suite.node.Node in project suite by stupidsing.

the class Lister method leaves.

private Streamlet<IList<Node>> leaves(Node node, IList<Node> prefix) {
    NodeRead nr = NodeRead.of(node);
    Streamlet<IList<Node>> st;
    if (nr.type == ReadType.TUPLE)
        st = // 
        Read.from(// 
        nr.children).index().map(// 
        (i, p) -> leaves(p.t1, IList.cons(Int.of(i), prefix))).collect(As::concat);
    else if (nr.type != ReadType.TERM)
        st = Read.from(nr.children).concatMap(p -> leaves(p.t1, IList.cons(p.t0, prefix)));
    else
        st = Read.from(List.of(IList.cons(nr.terminal, prefix)));
    if (nr.op != null)
        st = st.cons(IList.cons(Atom.of(nr.op.toString()), prefix));
    return st;
}
Also used : List(java.util.List) Read(suite.streamlet.Read) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) As(suite.streamlet.As) IList(suite.immutable.IList) NodeRead(suite.node.io.Rewrite_.NodeRead) Int(suite.node.Int) Node(suite.node.Node) ReadType(suite.node.io.Rewrite_.ReadType) NodeRead(suite.node.io.Rewrite_.NodeRead) As(suite.streamlet.As) IList(suite.immutable.IList)

Aggregations

Node (suite.node.Node)139 Tree (suite.node.Tree)50 Reference (suite.node.Reference)41 Atom (suite.node.Atom)37 Int (suite.node.Int)33 ArrayList (java.util.ArrayList)32 Pair (suite.adt.pair.Pair)25 List (java.util.List)24 TermOp (suite.node.io.TermOp)21 Read (suite.streamlet.Read)21 Test (org.junit.Test)20 Map (java.util.Map)19 Suite (suite.Suite)18 Generalizer (suite.lp.doer.Generalizer)18 Fail (suite.util.Fail)18 Fun (suite.util.FunUtil.Fun)18 Str (suite.node.Str)17 Trail (suite.lp.Trail)16 RuleSet (suite.lp.kb.RuleSet)16 Tuple (suite.node.Tuple)16