Esempio n. 1
0
def boot_all_functions():
    from mu.peano import (startup_peano,
                          _succ, _pred, _plus, _mult, _plus_acc, _mult_acc)
    from mu.lists import (startup_list,
                          _append, _reverse, _map)
    from mu.gcbench import (startup_gc_bench,
                            _gc_bench)
    startup_peano()
    startup_list()
    startup_gc_bench()

    # Peano arithmetics
    all_functions["succ"] = Function(_succ(), "p",
                         "Successor of a peano number")
    all_functions["pred"] = Function(_pred(), "p",
                         "Predecessor of a peano number")
    all_functions["plus"] = Function(_plus(), "pp",
                         "Add two peano numbers")
    all_functions["mult"] = Function(_mult(), "pp",
                         "Multiply two peano numbers")
    all_functions["plusa"] = Function(_plus_acc(), "pp",
                          "Add two peano nums, using accumulator")
    all_functions["multa"] = Function(_mult_acc(), "pp",
                          "Multiply two peano nums, using accumulator")
        # List processing
    all_functions["append"] = Function(_append(), "ll",
                           "Append a list to another")
    all_functions["reverse"] = Function(_reverse(), "l",
                            "Reverse a list")
    all_functions["map"] = Function(_map(), "fl",
                        "Apply a function to all elements of a list")

    all_functions["gc_bench"] = Function(_gc_bench(), "",
                        "Run parts of Boehm's GCBench")
Esempio n. 2
0
    def test_mult(self):
        from mu.peano import startup_peano, _mult
        startup_peano()

        a_w = peano_num(2)
        b_w = peano_num(3)

        res = interpret_expression(mu(_mult(), [a_w, b_w]))
        assert python_num(res) == 6
Esempio n. 3
0
    def test_plus_acc(self):
        from mu.peano import startup_peano, _plus_acc
        startup_peano()

        a_w = peano_num(4)
        b_w = peano_num(5)

        res = interpret_expression(mu(_plus_acc(), [a_w, b_w]))
        assert python_num(res) == 9
Esempio n. 4
0
    def test_plus(self):
        from mu.peano import peano_num, python_num, startup_peano, _plus
        startup_peano()

        n = 10
        # n = 100
        arg1 = peano_num(n)
        assert python_num(arg1) == n

        arg2 = peano_num(n)
        assert python_num(arg2) == n

        exp = mu(_plus(), [arg1, arg2])
        assert python_num(arg2) == n
        assert python_num(arg1) == n

        res = interpret_expression(exp)
        assert python_num(arg2) == n
        assert python_num(arg1) == n
        assert python_num(res) == n + n
Esempio n. 5
0
    def test_mult(self):
        from mu.peano import peano_num, python_num, startup_peano, _mult
        startup_peano()

        n = 4
        # n = 100
        arg1 = peano_num(n)
        assert python_num(arg1) == n

        arg2 = peano_num(n)
        assert python_num(arg2) == n

        exp = mu(_mult(), [arg1, arg2])
        assert python_num(arg2) == n
        assert python_num(arg1) == n

        print "\n" * 10

        res = interpret_expression(exp)
        assert python_num(arg2) == n
        assert python_num(arg1) == n
        assert python_num(res) == n * n
Esempio n. 6
0
    def test_map(self):
        """
        in scheme
         (define (map proc lis)
           (cond ((null? lis)
                  '())
                 ((pair? lis)
                  (cons (proc (car lis))
                        (map proc (cdr lis))))))

        nil ≔ (nil)
        map ≔ λ:
            F, (cons X, Y) ↦ (cons μ(F, X), μ(map, F, Y))
            _, nil         ↦ nil
        """
        from mu.peano import startup_peano, _succ
        startup_peano()

        f = Variable("F")
        x = Variable("X")
        y = Variable("Y")
        _ = Variable("_")
        _2 = Variable("_")

        m = lamb()
        m._name = "map"
        m._rules = ziprules(
            ([f, cons("cons", x, y)], cons("cons", mu(f, [x]), mu(m, [f, y]))),
            ([_, nil()], nil()))

        x1 = Variable("x")

        list_w = [peano_num(1), peano_num(2), peano_num(3)]
        #list_w = [peano_num(1)]

        res = interpret_expression(mu(m, [_succ(), conslist(list_w)]))
        assert plist(res) == [peano_num(2), peano_num(3), peano_num(4)]