def main(t1: Place) -> Place:
    IOExists1(Place)(lambda t2: (
        Requires(
            token(t1, 2) and matching_brackets(t1, t2) and MustTerminate(2)),
        Ensures(token(t2)),
    ))
    Open(matching_brackets(t1))
    Open(matching_brackets_helper(t1))
    success, t2 = putchar('(', t1)
    Open(matching_brackets(t2))
    t3 = NoOp(t2)
    success, t4 = putchar(')', t3)
    Open(matching_brackets(t4))
    t5 = NoOp(t4)
    return t5
Example #2
0
def run(t1: Place) -> None:
    Requires(token(t1, 2) and listener_io(t1))
    Ensures(False)

    Open(listener_io(t1))

    server_socket, t_loop = create_server_socket(t1)

    while True:
        Invariant(
            token(t_loop, 1) and
            listener_loop_io(t_loop, server_socket) and
            server_socket != None
        )

        Open(listener_loop_io(t_loop, server_socket))

        client_socket, t3 = accept(t_loop, server_socket)

        data, t4 = read_all(t3, client_socket, timeout=1)

        Open(output_io(t4, client_socket, data))

        if data is not None:
            t5, t6 = Split(t4)

            t7 = print_int(t6, get_address(client_socket))

            t8 = send(t5, client_socket, data)

            t9 = Join(t8, t7)
        else:
            t9 = NoOp(t4)

        t_loop = close(t9, client_socket)
Example #3
0
def ensure_dir_exists(t1: Place, path: str) -> Tuple[bool, Place]:
    IOExists2(Place, bool)(lambda t2, success: (
        Requires(path is not None and token(t1, 2) and ensure_dir_exists_io(
            t1, path, success, t2) and MustTerminate(2)),
        Ensures(token(t2) and t2 == Result()[1] and Result()[0] == success),
    ))

    Open(ensure_dir_exists_io(t1, path))

    try:

        t3 = mkdir(t1, path)

        t2 = NoOp(t3)

        return True, t2

    except OSErrorWrapper as ex1:

        try:

            res, t2 = is_dir(ex1.place, path)

            return res, t2

        except OSErrorWrapper as ex2:

            return False, ex2.place
    def brackets(self, t_read1: Place) -> Tuple[Place, bool]:
        IOExists3(str, bool, Place)(lambda read5, valid, t_read5: (
            Requires(
                Acc(self.c) and brackets_io(t_read1, self.c, read5, valid,
                                            t_read5) and token(t_read1, 3)),
            Ensures(
                Acc(self.c) and self.c is read5 and token(t_read5) and t_read5
                == Result()[0] and valid == Result()[1]),
        ))

        Open(brackets_io(t_read1, self.c))
        if self.peek_read_ahead() is '(':
            t_read2, _ = self.pop_read_ahead(t_read1)
            t_read3, brackets1 = self.brackets(t_read2)
            t_read4, c = self.pop_read_ahead(t_read3)
            should_be_close = (c is ')')
            t_read5, brackets2 = self.brackets(t_read4)
            return t_read5, (brackets1 and should_be_close and brackets2)
        else:
            i = self.peek_read_ahead()
            t_read2 = NoOp(t_read1)
            if i is None:
                return t_read2, True  # Empty string because of read EOF.
            elif self.peek_read_ahead() is ')':
                return t_read2, True  # Match empty string because read ')'.
            else:
                return t_read2, False  # No match because read invalid
Example #5
0
def handle_client(client_socket: Socket, t1: Place) -> None:
    Requires(client_socket is not None)
    Requires(token(t1, 2) and handle_client_io(t1, client_socket))
    Requires(MustTerminate(2))

    Open(handle_client_io(t1, client_socket))

    data, t4 = read_all(t1, client_socket, timeout=1)

    Open(output_io(t4, client_socket, data))

    if data is not None:
        t5, t6 = Split(t4)

        t7 = print_int(t6, get_address(client_socket))

        t8 = send(t5, client_socket, data)

        t9 = Join(t8, t7)
    else:
        t9 = NoOp(t4)

    t10 = close(t9, client_socket)

    End(t10)
Example #6
0
def test(t1: Place) -> Place:
    IOExists1(Place)(lambda t_end: (
        Requires(token(t1, 2) and test_io(t1, t_end)),
        Ensures(token(t_end) and t_end == Result()),
    ))

    Open(test_io(t1))

    t2 = NoOp(t1)
    t3, t4 = Split(t2)
    res, t5 = SetVar(t4, 1)
    t6 = Join(t3, t5)
    t_end = NoOp(t6)

    Assert(res == 1)

    return t_end
Example #7
0
def ensure_dir_exists2(t1: Place, path: str) -> Place:
    IOExists2(Place, OSErrorWrapper)(
        lambda t2, ex: (
            Requires(
                path is not None and
                token(t1, 2) and
                ensure_dir_exists_io2(t1, path, ex, t2) and
                MustTerminate(2)
            ),
            Ensures(
                token(t2) and t2 == Result() and ex is None
            ),
            Exsures(OSErrorWrapper,
                #:: UnexpectedOutput(postcondition.violated:assertion.false,55) | UnexpectedOutput(carbon)(postcondition.violated:assertion.false,168)
                ex is RaisedException() and
                Acc(ex.place) and ex.place == t2 and token(t2) and
                Acc(ex.exception) and isinstance(ex.exception, Exception)
            ),
        )
    )

    Open(ensure_dir_exists_io2(t1, path))

    res = True

    try:

        t3 = mkdir(t1, path)

        t2 = NoOp(t3)

        return t2

    except OSErrorWrapper as ex1:

        try:

            res, t2 = is_dir(ex1.place, path)

        except OSErrorWrapper as ex2:

            raise ex1

        else:

            if not res:

                raise ex1

            else:

                return t2
Example #8
0
def main(t1: Place) -> Place:
    IOExists1(Place)(lambda t2: (
        Requires(token(t1) and main_io(t1, t2)),
        Ensures(token(t2) and t2 == Result()),
    ))
    t2 = GetGhostOutput(main_io(t1), 't_post')  # type: Place
    Open(main_io(t1))
    success = True
    t_loop = t1
    while success:
        Invariant(
            token(t_loop, 1) and tee_io(t_loop, t2)
            if success else token(t_loop) and no_op_io(t_loop, t2))
        Open(tee_io(t_loop))
        c, success, t_loop = getchar(t_loop)
        if success:
            t_loop = tee_out(t_loop, c)
    return NoOp(t_loop)
Example #9
0
def main(t1: Place) -> None:
    Requires(token(t1, 2) and infinite_counter_io(t1, 0))
    Ensures(False)

    count = 0
    t_cur = t1

    while True:
        Invariant(
            token(t_cur, 1) and infinite_counter_io(t_cur, count)
            and count >= 0)

        unary_count = 0

        Open(infinite_counter_io(t_cur, count))

        t1_unary = t_cur
        t_unary_end = GetGhostOutput(print_unary_io(t1_unary, count),
                                     't_end')  # type: Place

        while (unary_count != count):
            Invariant(
                token(t1_unary, 1)
                and print_unary_io(t1_unary, count - unary_count, t_unary_end)
                and unary_count <= count)

            Open(print_unary_io(t1_unary, count - unary_count))

            success, t1_unary = putchar('1', t1_unary)

            unary_count += 1

        Open(print_unary_io(t1_unary, 0))

        t2 = NoOp(t1_unary)

        success, t_cur = putchar('\n', t2)

        count = count + 1