예제 #1
0
def test_times_zero_l():
    dataset = get_dataset(chunk=60)
    # Case where times is not root
    # mul
    #   0
    #   mul  < ---- this is where the cursor will be
    #     suc	0
    #     0
    l = dataset[49][0]
    l = label_parents(l)
    cursor = l.children[1]
    op, l = times_zero_l(cursor, l)

    assert repr(l) == "Tree(mul, [Token(NUMBER, '0'), Token(NUMBER, '0')])"
    # Result:
    # mul
    #   0
    #   0

    # Case where times is root
    # mul
    #   0
    #   0
    l = dataset[4][0]
    l = label_parents(l)

    op, l = times_zero_l(l, l)

    assert repr(l) == "Token(NUMBER, '0')"
예제 #2
0
def test_plus_zero_l():
    dataset = get_dataset(chunk=60)

    # Case of add operation being not root
    # mul
    #   0
    #   add
    #     0
    #     0
    l = dataset[51][0]
    l = label_parents(l)
    add_op = l.children[1]  # move to add op
    add_op, l = plus_zero_l(add_op, l)

    assert repr(
        add_op) == "Tree(mul, [Token(NUMBER, '0'), Token(NUMBER, '0')])"

    # Case of add operation being root
    # add
    #   0
    #   0

    l = dataset[8][0]
    l = label_parents(l)
    add_op = l
    add_op, l = plus_zero_l(add_op, l)

    assert repr(l) == "Token(NUMBER, '0')"
예제 #3
0
def test_recursive_plus_l():
    dataset = get_dataset(chunk=60)

    # Case where add is not root
    # suc
    #   add
    #     suc	0
    #     suc	0
    l = dataset[20][0]
    l = label_parents(l)
    tree = l.children[0]
    add_op, l = recursive_plus_l(tree, l)

    assert (
        repr(l) ==
        "Tree(suc, [Tree(suc, [Tree(add, [Tree(suc, [Token(NUMBER, '0')]), Token(NUMBER, '0')])])])"
    )

    # Case where add is root
    # add
    #   0
    #   suc	0
    l = dataset[9][0]
    l = label_parents(l)
    add_op, l = recursive_plus_l(l, l)
    assert repr(
        l
    ) == "Tree(suc, [Tree(add, [Token(NUMBER, '0'), Token(NUMBER, '0')])])"
예제 #4
0
def test_recursive_power_l():
    # x ^ s(y) = x ^ y * x
    example = Tree("suc", [
        Tree("pow", [Token("WORD", "x"),
                     Tree("suc", [Token("NUMBER", "0")])])
    ])

    example = label_parents(example)

    pow_op = example.children[0]

    cursor, entire = recursive_power_l(pow_op, example)

    # Case of not root:
    # suc
    #   pow <----
    #     x
    #     suc	0

    # Expected result:
    # suc
    #   mul
    #       pow
    #           x
    #           0
    #   x

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(mul, [Tree(pow, [Token(WORD, 'x'), Token(NUMBER, '0')]), Token(WORD, 'x')])])"
    )

    example = Tree("pow",
                   [Token("WORD", "x"),
                    Tree("suc", [Token("NUMBER", "0")])])

    example = label_parents(example)

    pow_op = example

    cursor, entire = recursive_power_l(pow_op, example)

    # Case of root:
    #   pow <----
    #     x
    #     suc	0

    # Expected result:
    # mul
    #   pow
    #       x
    #       0
    #   x

    assert (
        repr(entire) ==
        "Tree(mul, [Tree(pow, [Token(WORD, 'x'), Token(NUMBER, '0')]), Token(WORD, 'x')])"
    )
예제 #5
0
def test_first_power_of_x_l():

    # x ^ 1 = x

    example = Tree("suc", [
        Tree("pow", [Token("WORD", "x"),
                     Tree("suc", [Token("NUMBER", "0")])])
    ])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = first_power_of_x_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   pow
    #     x
    #     suc	0

    # Expected results
    # suc	x

    assert repr(entire) == "Tree(suc, [Token(WORD, 'x')])"

    example = Tree("pow",
                   [Token("WORD", "x"),
                    Tree("suc", [Token("NUMBER", "0")])])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = first_power_of_x_l(example, example)

    print("b")
    print(entire)
    print(repr(entire))

    # Case of root
    # pow
    #   x
    #   suc	0

    # Expected result
    # x

    assert repr(entire) == "Token(WORD, 'x')"
예제 #6
0
def test_power_of_one_l():

    # 1 ^ x = 1

    example = Tree("suc", [
        Tree("pow", [Tree("suc", [Token("NUMBER", "0")]),
                     Token("WORD", "x")])
    ])
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = power_of_one_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   pow
    #     suc	0
    #     x

    # Expected result
    # suc
    #   suc	0

    assert repr(entire) == "Tree(suc, [Tree(suc, [Token(NUMBER, '0')])])"

    example = Tree("pow",
                   [Tree("suc", [Token("NUMBER", "0")]),
                    Token("WORD", "x")])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = power_of_one_l(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root
    # pow
    #   suc	0
    #   x

    # Expected result
    # suc	0

    assert repr(entire) == "Tree(suc, [Token(NUMBER, '0')])"
예제 #7
0
def test_first_power_of_x_r():

    # x = x ^ 1

    example = Tree("suc", [Token("WORD", "x")])
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = first_power_of_x_r(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc	x

    # Expected result
    # suc
    #   pow
    #     x
    #     suc	0

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(pow, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])])"
    )

    example = Token("WORD", "x")
    example = label_parents(example)

    print("b")
    print(example)

    cursor, entire = first_power_of_x_r(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root:
    # x

    # Expected result
    # pow
    #   x
    #   suc	0

    assert (repr(entire) ==
            "Tree(pow, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])")
예제 #8
0
def test_times_identity_r():

    # x = x * 1

    example = Tree("suc", [Token("WORD", "x")])
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = times_identity_r(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc	x

    # Expected result
    # suc
    #   mul
    #     x
    #     suc	0

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(mul, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])])"
    )

    example = Token("WORD", "x")
    example = label_parents(example)

    print("b")
    print(example)

    cursor, entire = times_identity_r(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root
    # x

    # Expected result
    # mul
    #   x
    #   suc	0

    assert (repr(entire) ==
            "Tree(mul, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])")
예제 #9
0
def test_times_identity_l():

    # x * 1 = x
    example = Tree("suc", [
        Tree("mul", [Token("WORD", "x"),
                     Tree("suc", [Token("NUMBER", "0")])])
    ])
    example = label_parents(example)
    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = times_identity_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   mul <----
    #     x
    #     suc	0

    # Expected result
    # suc	x

    assert repr(entire) == "Tree(suc, [Token(WORD, 'x')])"

    example = Tree("mul",
                   [Token("WORD", "x"),
                    Tree("suc", [Token("NUMBER", "0")])])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = times_identity_l(example, example)

    print("b")
    print(entire)
    print(repr(entire))

    # case of root
    # mul
    #   x
    #   suc	0

    # Expected result
    # x

    assert repr(entire) == "Token(WORD, 'x')"
예제 #10
0
def test_recursive_times_r():
    dataset = get_dataset(chunk=190)

    # Case of not root
    # suc
    #   add
    #     mul
    #       suc	0
    #       suc	0
    #     suc	0

    l = dataset[180][0]
    l = label_parents(l)
    root = l.children[0]
    add_op, l = recursive_times_r(root, l)

    # expected result
    # suc
    #   mul
    #     suc	0
    #     suc
    #       suc	0

    assert (
        repr(l) ==
        "Tree(suc, [Tree(mul, [Tree(suc, [Token(NUMBER, '0')]), Tree(suc, [Tree(suc, [Token(NUMBER, '0')])])])])"
    )

    # Case of root
    # add
    #   mul
    #     suc	0
    #     0
    #   suc	0

    l = dataset[109][0]
    l = label_parents(l)
    add_op, l = recursive_times_r(l, l)
    # Expected result
    # mul
    #   suc	0
    #   suc	0

    assert (
        repr(l) ==
        "Tree(mul, [Tree(suc, [Token(NUMBER, '0')]), Tree(suc, [Token(NUMBER, '0')])])"
    )
예제 #11
0
def test_calculate_value():

    dataset = get_dataset(chunk=10)
    l = dataset[2][0]
    l = label_parents(l)
    winning = winning_state(l)

    assert calculate_value(l) == 2
예제 #12
0
def test_recursive_times_l():
    dataset = get_dataset(chunk=60)
    # Case where mul is not root
    # suc
    #   suc
    #     mul <--- Cursor will be here
    #       0
    #       suc	0
    l = dataset[31][0]
    l = label_parents(l)

    cursor = l.children[0].children[0]
    op, l = recursive_times_l(cursor, l)

    # Expected result
    # suc
    #   suc
    #     add
    #       mul
    #         0
    #         0
    #       0

    assert (
        repr(l) ==
        "Tree(suc, [Tree(suc, [Tree(add, [Tree(mul, [Token(NUMBER, '0'), Token(NUMBER, '0')]), Token(NUMBER, '0')])])])"
    )
    # Case where mul is root
    # mul
    #   0
    #   suc	0
    l = dataset[5][0]
    l = label_parents(l)
    op, l = recursive_times_l(l, l)

    # Expected result
    # add
    #   mul
    #     0
    #     0
    #   0
    assert (
        repr(l) ==
        "Tree(add, [Tree(mul, [Token(NUMBER, '0'), Token(NUMBER, '0')]), Token(NUMBER, '0')])"
    )
예제 #13
0
def test_winning_state():

    # Should be true
    dataset = get_dataset(chunk=130)
    l = dataset[120][0]
    l = label_parents(l)

    winning = winning_state(l)

    assert winning

    # Should be true
    dataset = get_dataset(chunk=130)
    l = dataset[121][0]
    l = label_parents(l)

    winning = winning_state(l)

    assert not winning
예제 #14
0
def test_check_tree():

    # All beginning states should be well formed
    dataset = get_dataset(chunk=130)
    for i, formula in enumerate(dataset):

        formula = label_parents(formula)
        wellformed = check_tree(formula)

        assert wellformed
예제 #15
0
def test_recursive_plus_r():
    # Case of not root
    # suc
    #   suc <-- cursor will be here
    #     add
    #       0
    #       suc	0
    dataset = get_dataset(chunk=60)

    l = dataset[35][0]
    l = label_parents(l)
    root = l.children[0]

    add_op, l = recursive_plus_r(root, l)
    # suc
    #   add
    #     0
    #     suc
    #       suc	0

    assert (
        repr(l) ==
        "Tree(suc, [Tree(add, [Token(NUMBER, '0'), Tree(suc, [Tree(suc, [Token(NUMBER, '0')])])])])"
    )
    # Case of root
    # suc
    #   add
    #     0
    #     0
    dataset = get_dataset(chunk=60)
    l = dataset[17][0]
    l = label_parents(l)
    add_op, l = recursive_plus_r(l, l)

    # Expected result
    # add
    #   0
    #   suc	0
    assert repr(
        l
    ) == "Tree(add, [Token(NUMBER, '0'), Tree(suc, [Token(NUMBER, '0')])])"
예제 #16
0
def test_plus_zero_r():

    dataset = get_dataset(chunk=60)
    # Case of not root
    # suc
    #   mul <--- cursor here
    #     0
    #     0
    l = dataset[13][0]
    l = label_parents(l)
    root = l.children[0]
    add_op, l = plus_zero_r(root, l)

    # Expected result
    # suc
    #   add
    #     mul
    #       0
    #       0
    #     0

    assert (
        repr(l) ==
        "Tree(suc, [Tree(add, [Tree(mul, [Token(NUMBER, '0'), Token(NUMBER, '0')]), Token(NUMBER, '0')])])"
    )
    # Case of root
    # suc	0
    l = dataset[1][0]
    l = label_parents(l)

    add_op, l = plus_zero_r(l, l)

    # Expected output
    # add
    #   suc	0
    #   0

    assert repr(
        l
    ) == "Tree(add, [Tree(suc, [Token(NUMBER, '0')]), Token(NUMBER, '0')])"
예제 #17
0
    def get_deepcopy_state_variables(self):
        """"
        Return a real copy of the state and the cursor (with interconnected reference)
        """
        path = self.cursor_location_from_root()
        new_state = copy.deepcopy(self.state)
        new_state = label_parents(new_state)
        new_cursor = new_state
        # print(path)
        for step in path:
            new_cursor = new_cursor.children[step]

        return new_cursor, new_state
예제 #18
0
def test_power_zero_l():

    example = Tree(
        "suc",
        [Tree("pow",
              [Token("WORD", "x"), Token("NUMBER", "0")])])
    example = label_parents(example)

    pow_op = example.children[0]

    cursor, entire = power_zero_l(pow_op, example)

    # Case of operation being not root
    # suc
    #   pow <-----
    #     x
    #     0

    # Expected result:
    # suc
    #   suc	0

    assert repr(entire) == "Tree(suc, [Tree(suc, [Token(NUMBER, '0')])])"

    example = Tree("pow", [Token("WORD", "x"), Token("NUMBER", "0")])
    example = label_parents(example)

    cursor, entire = power_zero_l(example, example)

    # # Case of operation being root
    # pow
    #   x
    #   0

    # Expected result:
    # suc	0

    assert repr(entire) == "Tree(suc, [Token(NUMBER, '0')])"
예제 #19
0
    def load_problem_tree(self, tree, goal):

        """"
        Load problem by state
        """
        self.stepcounter = 0

        self.state = copy.deepcopy(tree)
        self.goal = goal
        # TODO Write a new check_tree method
        # assert check_tree(self.state)
        if self.state == self.goal:
            raise ValueError("Starting state of problem is already winning state")

        self.state = label_parents(self.state)
        self.cursor = self.state
예제 #20
0
    def load_problem_tree(self, tree, val=-1):
        """"
        Load problem by state
        """
        self.stepcounter = 0
        self.value = val
        self.state = copy.deepcopy(tree)
        if not check_tree(self.state):
            raise AssertionError(
                "Tree is not correct (check_tree method fails)")

        # assert check_tree(self.state)
        if not self.generation:
            if winning_state(self.state):
                raise ValueError(
                    "Starting state of problem is already winning state")

        self.state = label_parents(self.state)
        self.cursor = self.state
예제 #21
0
def test_distributivity_power_plus_r():
    # x ^ y * x ^ z = x ^ (y + z)

    example = Tree(
        "suc",
        [
            Tree(
                "mul",
                [
                    Tree("pow", [Token("WORD", "x"),
                                 Token("WORD", "y")]),
                    Tree("pow", [Token("WORD", "x"),
                                 Token("WORD", "z")]),
                ],
            )
        ],
    )
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = distributivity_power_plus_r(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   mul <-----
    #     pow
    #       x
    #       y
    #     pow
    #       x
    #       z

    # Expected result
    # suc
    #   pow
    #     x
    #     add
    #       y
    #       z

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(pow, [Token(WORD, 'x'), Tree(add, [Token(WORD, 'y'), Token(WORD, 'z')])])])"
    )

    example = Tree(
        "mul",
        [
            Tree("pow",
                 [Token("WORD", "x"), Token("WORD", "y")]),
            Tree("pow",
                 [Token("WORD", "x"), Token("WORD", "z")]),
        ],
    )

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = distributivity_power_plus_r(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # case of root
    # mul
    #   pow
    #     x
    #     y
    #   pow
    #     x
    #     z

    # Expected result
    # pow
    #   x
    #   add
    #     y
    #     z

    assert (
        repr(entire) ==
        "Tree(pow, [Token(WORD, 'x'), Tree(add, [Token(WORD, 'y'), Token(WORD, 'z')])])"
    )
예제 #22
0
def test_recursive_power_r():
    # x ^ y * x -> x ^ s(y)

    example = Tree(
        "suc",
        [
            Tree(
                "mul",
                [
                    Tree("pow", [Token("WORD", "x"),
                                 Token("NUMBER", "0")]),
                    Token("WORD", "x"),
                ],
            )
        ],
    )
    example = label_parents(example)

    pow_op = example.children[0]

    cursor, entire = recursive_power_r(pow_op, example)

    # Case of not root
    # suc
    #   mul <----
    #     pow
    #       x
    #       0
    #     x

    # Expected result
    # suc
    #   pow
    #       x
    #       suc
    #          0

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(pow, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])])"
    )

    example = Tree(
        "mul",
        [
            Tree("pow",
                 [Token("WORD", "x"), Token("NUMBER", "0")]),
            Token("WORD", "x")
        ],
    )

    example = label_parents(example)

    # Case of root
    #   mul <----
    #     pow
    #       x
    #       0
    #     x

    # Expected result
    # pow
    #   x
    #   suc
    #       0

    cursor, entire = recursive_power_r(example, example)

    assert (repr(entire) ==
            "Tree(pow, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])")
예제 #23
0
def test_associativity_r():

    #  x + (y + z)  =  (x + y) + z

    example = Tree(
        "suc",
        [
            Tree(
                "add",
                [
                    Token("WORD", "x"),
                    Tree("add", [Token("WORD", "y"),
                                 Token("WORD", "z")]),
                ],
            )
        ],
    )
    example = label_parents(example)

    op = example.children[0]

    cursor, entire = associativity_r(op, example)

    # Case of not root
    # suc
    #   add <-------
    #     x
    #     add
    #       y
    #       z

    # Expected result
    # suc
    #   add
    #     add
    #       x
    #       y
    #     z

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(add, [Tree(add, [Token(WORD, 'x'), Token(WORD, 'y')]), Token(WORD, 'z')])])"
    )

    example = Tree(
        "add",
        [
            Token("WORD", "x"),
            Tree("add",
                 [Token("WORD", "y"), Token("WORD", "z")]),
        ],
    )

    example = label_parents(example)

    cursor, entire = associativity_r(example, example)

    # Case of root
    # add
    #   x
    #   add
    #     y
    #     z

    # Expected result
    # add
    #   add
    #     x
    #     y
    #   z

    assert (
        repr(entire) ==
        "Tree(add, [Tree(add, [Token(WORD, 'x'), Token(WORD, 'y')]), Token(WORD, 'z')])"
    )
예제 #24
0
def test_distributivity_times_r():
    #   x * y + x * z  = x * (y + z)

    example = Tree(
        "suc",
        [
            Tree(
                "add",
                [
                    Tree("mul", [Token("WORD", "x"),
                                 Token("WORD", "y")]),
                    Tree("mul", [Token("WORD", "x"),
                                 Token("WORD", "z")]),
                ],
            )
        ],
    )
    example = label_parents(example)

    op = example.children[0]

    cursor, entire = distributivity__times_r(op, example)

    # Case of not root
    # suc
    #   add
    #     mul
    #       x
    #       y
    #     mul
    #       x
    #       z

    # Expected result:
    # suc
    #   mul
    #     x
    #     add
    #       y
    #       z

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(mul, [Token(WORD, 'x'), Tree(add, [Token(WORD, 'y'), Token(WORD, 'z')])])])"
    )

    example = Tree(
        "add",
        [
            Tree("mul",
                 [Token("WORD", "x"), Token("WORD", "y")]),
            Tree("mul",
                 [Token("WORD", "x"), Token("WORD", "z")]),
        ],
    )

    example = label_parents(example)
    cursor, entire = distributivity__times_r(example, example)

    # Case of root
    # add
    #   mul
    #     x
    #     y
    #   mul
    #     x
    #     z

    # Expected result
    # mul
    #   x
    #   add
    #     y
    #     z

    assert (
        repr(entire) ==
        "Tree(mul, [Token(WORD, 'x'), Tree(add, [Token(WORD, 'y'), Token(WORD, 'z')])])"
    )
예제 #25
0
    def step(self, action, return_env=False):

        current_legal = legal_actions(self.cursor)

        if not current_legal[action] == 1:
            raise AssertionError("Action is not legal")

        if action == 0:

            _, self.state = plus_zero_l(self.cursor, self.state)
            # reset cursor to root

            self.cursor = self.state

        elif action == 1:
            _, self.state = plus_zero_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 2:
            _, self.state = recursive_plus_l(self.cursor, self.state)

            self.cursor = self.state
        elif action == 3:
            _, self.state = recursive_plus_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 4:
            _, self.state = times_zero_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 5:
            _, self.state = recursive_times_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 6:
            _, self.state = recursive_times_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 7:

            self.cursor = self.cursor.children[0]

        elif action == 8:

            self.cursor = self.cursor.children[1]

        elif action == 9:
            self.cursor.children[1], self.cursor.children[0] = (
                self.cursor.children[0],
                self.cursor.children[1],
            )

        if not check_tree(self.state):
            raise AssertionError("Check_tree failed")

        # Check if the tree still has the same value!
        # If not -- something major is wrong!

        assert calculate_value_full(self.state) == self.value
        # print(self.state)
        # print(calculate_value_full(self.state))
        # print(self.value)

        self.stepcounter += 1
        winning = winning_state(self.state)

        self.state = label_parents(self.state)
        self.state.parent = None

        if winning:
            if not self.value == -1:
                assert calculate_value(self.state) == self.value

        return self.cursor, self.state, winning
예제 #26
0
def test_distributivity_power_times_l():

    # (x * y) ^ z = x ^ z * y ^ z

    example = Tree(
        "suc",
        [
            Tree(
                "pow",
                [
                    Tree("mul", [Token("WORD", "x"),
                                 Token("WORD", "y")]),
                    Token("WORD", "z"),
                ],
            )
        ],
    )
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = distributivity_power_times_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of  not root
    # suc
    #   pow <----
    #     mul
    #       x
    #       y
    #     z

    # Expected result
    # suc
    #   mul
    #     pow
    #       x
    #       z
    #     pow
    #       y
    #       z

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(mul, [Tree(pow, [Token(WORD, 'x'), Token(WORD, 'z')]), Tree(pow, [Token(WORD, 'y'), Token(WORD, 'z')])])])"
    )

    example = Tree(
        "pow",
        [
            Tree("mul",
                 [Token("WORD", "x"), Token("WORD", "y")]),
            Token("WORD", "z"),
        ],
    )

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = distributivity_power_times_l(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root
    # pow
    #   mul
    #     x
    #     y
    #   z

    # Expected result
    # mul
    #   pow
    #     x
    #     z
    #   pow
    #     y
    #     z

    assert (
        repr(entire) ==
        "Tree(mul, [Tree(pow, [Token(WORD, 'x'), Token(WORD, 'z')]), Tree(pow, [Token(WORD, 'y'), Token(WORD, 'z')])])"
    )
예제 #27
0
    def step(self, action):

        current_legal = self.legal_indices()

        if not action in current_legal:

            raise AssertionError("Action is not legal")

        if action == 0:
            _, self.state = plus_zero_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 1:
            _, self.state = plus_zero_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 2:
            _, self.state = recursive_plus_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 3:
            _, self.state = recursive_plus_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 4:
            _, self.state = times_zero_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 5:
            _, self.state = recursive_times_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 6:
            _, self.state = recursive_times_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 7:

            self.cursor = self.cursor.children[0]

        elif action == 8:

            self.cursor = self.cursor.children[1]

        elif action == 9:
            self.cursor.children[1], self.cursor.children[0] = (
                self.cursor.children[0],
                self.cursor.children[1],
            )
            # TODO
            # Think about if the cursor needs to go to root after switching the subtrees
            self.cursor = self.state

        elif action == 10:
            _, self.state = power_zero_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 11:
            _, self.state = recursive_power_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 12:
            _, self.state = recursive_power_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 13:
            _, self.state = associativity_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 14:

            _, self.state = associativity_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 15:
            _, self.state = distributivity__times_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 16:
            _, self.state = distributivity__times_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 17:
            _, self.state = times_identity_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 18:
            _, self.state = times_identity_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 19:
            _, self.state = power_of_one_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 20:
            _, self.state = first_power_of_x_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 21:
            _, self.state = first_power_of_x_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 22:
            _, self.state = distributivity_power_plus_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 23:
            _, self.state = distributivity_power_plus_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 24:
            _, self.state = distributivity_power_times_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 25:
            _, self.state = distributivity_power_times_r(self.cursor, self.state)

            self.cursor = self.state

        elif action == 26:
            _, self.state = distributivity_power_power_l(self.cursor, self.state)

            self.cursor = self.state

        elif action == 27:
            _, self.state = distributivity_power_power_r(self.cursor, self.state)

            self.cursor = self.state

        self.state = label_parents(self.state)
        self.state.parent = None

        self.stepcounter += 1
        if self.state == self.goal:
            winning = True
        else:
            winning = False

        return self.cursor, self.state, winning