Exemplo n.º 1
0
 def wrapped(w_arguments):
     assert len(w_arguments) == len_unwrap_spec
     args = ()
     for i, spec in unrolling_unwrap_spec:
         w_arg = w_arguments[i]
         if False: pass
         elif spec is generic:
             assert isinstance(w_arg, model.W_Object)
             args += (w_arg, )
         elif spec is int:
             assert isinstance(w_arg, model.W_Integer)
             args += (w_arg.value(), )
         elif spec is float:
             assert isinstance(w_arg, model.W_Float)
             args += (w_arg.value(), )
         elif spec is long:
             assert isinstance(w_arg, model.W_Bignumber)
             args += (w_arg.value(), )
         elif spec is str:
             assert isinstance(w_arg, model.W_String)
             args += (w_arg.value(), )
         elif spec is list:
             assert isinstance(w_arg, model.W_Constructor)
             t = w_arg.get_tag()
             assert t.arity() == 2
             args += (plist(w_arg), )
         else:
             raise NotImplementedError("unknown unwrap_spec %s" %
                                       (spec, ))
     w_result = func(*args)
     return w_result
Exemplo n.º 2
0
    def test_simple_append_processing(self):
        fun = all_functions["append"]
        args = ["1;i:1", "1;i:1"]
        ops = [fun.parse_arg(i, a) for i, a in enumerate(args)]
        assert ops == [conslist([integer(1)]), conslist([integer(1)])]

        ret = run(fun.lamb, ops)
        assert plist(ret) == [integer(1), integer(1)]
        assert fun.format_ret(ret) == "(1,1)"
Exemplo n.º 3
0
    def test_mapping(self):
        fun = all_functions["map"]
        args = ["succ", "5;p:1,f:succ"]
        l = 5
        ops = [fun.parse_arg(i, a) for i, a in enumerate(args)]
        assert ops[0] == all_functions["succ"].lamb
        assert ops[1] == conslist([peano_num(i + 1) for i in range(l)])

        ret = run(fun.lamb, ops)
        assert plist(ret) == [peano_num(i + 2) for i in range(l)]
Exemplo n.º 4
0
    def test_mapping_cliissue(self):
        from theseus.shape import CompoundShape
        from theseus.tests.test_shape import SConf
        with SConf(substitution_threshold=1):
            fun = all_functions["map"]
            args = ["succ", "10;p:1,f:succ"]
            l = 10
            ops = [fun.parse_arg(i, a) for i, a in enumerate(args)]
            assert ops[0] == all_functions["succ"].lamb
            assert ops[1] == conslist([peano_num(i + 1) for i in range(l)])

            ret = run(fun.lamb, ops)
            assert plist(ret) == [peano_num(i + 2) for i in range(l)]
Exemplo n.º 5
0
    def test_append(self):

        x1 = Variable("x")
        x2 = Variable("x")
        h = Variable("head")
        t = Variable("tail")

        l = lamb()
        l._rules = ziprules(
            ([nil(), x1], x1),
            ([cons("cons", h, t), x2], cons("cons", h, mu(l, [t, x2]))))

        list1_w = [integer(1), integer(2), integer(3)]
        list2_w = [integer(4), integer(5), integer(6)]
        assert plist(l.call([conslist(list1_w),
                             conslist(list2_w)])) == list1_w + list2_w
Exemplo n.º 6
0
    def test_append(self):

        x1 = Variable("x")
        x2 = Variable("x")
        h = Variable("head")
        t = Variable("tail")

        l = lamb()
        l._name = "append"
        l._rules = ziprules(
            ([nil(), x1], x1),
            ([cons("cons", h, t), x2], cons("cons", h, mu(l, [t, x2]))))

        list1_w = [integer(1), integer(2), integer(3)]
        list2_w = [integer(4), integer(5), integer(6)]

        res = interpret_expression(
            mu(l, [conslist(list1_w), conslist(list2_w)]))
        assert plist(res) == list1_w + list2_w
Exemplo n.º 7
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)]
Exemplo n.º 8
0
    def test_reverse(self):

        debug = False

        if debug:
            print ""

        a1 = Variable("accumulator")
        a2 = Variable("accumulator")
        h = Variable("head")
        t = Variable("tail")

        w_nil_shape = nil().shape()

        c = clean_tag("cons", 2)

        def _cons(*children):
            ch = list(children)
            constr = W_Constructor.construct(c.default_shape, ch)
            return constr

        def _conslist(p_list):
            result = nil()
            for element in reversed(p_list):
                result = _cons(element, result)
            return result

        cons_shape = c.default_shape
        with SConf(substitution_threshold=sys.maxint):
            cons_1_shape = CompoundShape(c, [in_storage_shape, cons_shape])
            cons_2_shape = CompoundShape(c, [in_storage_shape, cons_1_shape])
            cons_3_shape = CompoundShape(c, [in_storage_shape, cons_2_shape])
            cons_4_shape = CompoundShape(c, [in_storage_shape, cons_3_shape])
            cons_5_shape = CompoundShape(c, [in_storage_shape, cons_4_shape])
            cons_1_shape._config.substitution_threshold = sys.maxint
            cons_2_shape._config.substitution_threshold = sys.maxint
            cons_3_shape._config.substitution_threshold = sys.maxint
            cons_4_shape._config.substitution_threshold = sys.maxint
            cons_5_shape._config.substitution_threshold = sys.maxint
            cons_shape.transformation_rules[(1, cons_shape)] = cons_1_shape
            cons_shape.transformation_rules[(1, cons_1_shape)] = cons_2_shape
            cons_shape.transformation_rules[(1, cons_2_shape)] = cons_3_shape
            # cons_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape
            # cons_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape

            cons_1_shape.transformation_rules[(1, cons_1_shape)] = cons_2_shape
            cons_1_shape.transformation_rules[(1, cons_2_shape)] = cons_3_shape
            # cons_1_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape
            # cons_1_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape

            cons_2_shape.transformation_rules[(1, cons_2_shape)] = cons_3_shape
            # cons_2_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape
            # cons_2_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape

            # cons_3_shape.transformation_rules[(1, cons_3_shape)] = cons_4_shape
            # cons_3_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape

            # cons_4_shape.transformation_rules[(1, cons_4_shape)] = cons_5_shape

            reverse_acc = lamb()
            reverse_acc._name = "reverse_acc"
            reverse_acc._rules = ziprules(
                ([nil(), a1], a1),
                ([_cons(h, t), a2], mu(reverse_acc, [t, _cons(h, a2)])),
            )

            l = Variable("l")
            reverse = lamb(([l], mu(reverse_acc, [l, nil()])))
            reverse._name = "reverse"

            nums = 50
            list1_w = [integer(x) for x in range(nums)]
            clist1_w = _conslist(list1_w)
            assert clist1_w.get_tag() is c

            res = interpret_expression(mu(reverse, [clist1_w]))
            list1_w.reverse()
            assert plist(res) == list1_w