コード例 #1
0
def make_map():
    """
    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
    """

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

    m = lamb()
    m._name = "map"
    m._rules = rules([([p(f), p(cons("cons", x, y))],
                       e(cons("cons", mu(f, [x]), mu(m, [f, y])))),
                      ([p(_), p(nil())], e(nil()))])
    return m
コード例 #2
0
    def test_fail_lambda(self):
        w_int1 = integer(1)
        w_int2 = integer(2)
        l = lamb(([w_int1], w_int2))

        with py.test.raises(NoMatch) as e:
            l.call([w_int2])
コード例 #3
0
    def test_fail_lambda(self):
        w_int1 = integer(1)
        w_int2 = integer(2)
        l = lamb(([w_int1], w_int2))

        with py.test.raises(NoMatch) as e:
            res = interpret_expression(mu(l, [w_int2]))
コード例 #4
0
    def test_lambda_not(self):

        w_true = cons("true")
        w_false = cons("false")

        l = lamb(([w_true], w_false), ([w_false], w_true))
        assert l.call([w_true]) == w_false
        assert l.call([w_false]) == w_true
コード例 #5
0
ファイル: peano.py プロジェクト: shiplift/theseus
def make_pred():
    x = Variable("x")
    l = lamb()
    l._rules = rules([
        ([_p(_zero())], e(_zero())),
        ([pp(x)      ], e(x))])
    l._name = "pred"
    return l
コード例 #6
0
    def test_muffle(self):

        for i in range(20):
            vars = [Variable("x%s" % n) for n in range(i)]

            vars2 = [Variable("x%s" % n) for n in range(i - 1)]

            m = lamb((vars2, cons("cons", *vars2)))
            m._name = "construct"

            l = lamb()
            l._name = "muffle%s" % i
            l._rules = ziprules(([cons("cons", *vars)], mu(m, vars[1:])))

            list1 = [integer(n) for n in range(i)]
            w_cons1 = cons("cons", *list1)
            res = l.call([w_cons1])
            assert res == cons("cons", *(list1[1:]))
コード例 #7
0
def make_reverse():
    a1 = Variable("acc")
    a2 = Variable("acc")
    h = Variable("head")
    t = Variable("tail")
    reverse_acc = lamb()
    reverse_acc._name = "r_acc"

    reverse_acc._rules = rules([([p(nil()), p(a1)], e(a1)),
                                ([p(cons("cons", h, t)),
                                  p(a2)],
                                 e(mu(reverse_acc,
                                      [t, cons("cons", h, a2)])))])

    l = Variable("list")
    reverse = lamb()
    reverse._rules = rules([([p(l)], mu(reverse_acc, [l, nil()]))])
    reverse._name = "reverse"
    return reverse
コード例 #8
0
def make_append():
    x1 = Variable("x")
    x2 = Variable("x")
    h = Variable("head")
    t = Variable("tail")

    l = lamb()
    l._name = "append"
    l._rules = rules([([p(nil()), p(x1)], e(x1)),
                      ([p(cons("cons", h, t)),
                        p(x2)], e(cons("cons", h, mu(l, [t, x2]))))])
    return l
コード例 #9
0
    def test_lambda_not(self):

        w_true = cons("true")
        w_false = cons("false")

        l = lamb(([w_true], w_false), ([w_false], w_true))

        res = interpret_expression(mu(l, [w_true]))
        assert res == w_false

        res = interpret_expression(mu(l, [w_false]))
        assert res == w_true
コード例 #10
0
    def test_shuffle(self):

        for i in range(20):
            vars = [Variable("x%s" % n) for n in range(i)]

            l = lamb(([cons("cons",
                            *vars)], cons("cons", *(vars[1:] + vars[:1]))))
            l._name = "shuffle%s" % i

            list1 = [integer(n) for n in range(i)]
            w_cons1 = cons("cons", *list1)
            res = l.call([w_cons1])
            assert res == cons("cons", *(list1[1:] + list1[:1]))
コード例 #11
0
ファイル: peano.py プロジェクト: shiplift/theseus
def make_plus_acc():
    x1 = Variable("x")
    x2 = Variable("x")
    x3 = Variable("x")
    y = Variable("y")

    a1 = Variable("accumulator")
    a2 = Variable("accumulator")
    o1 = Variable("op")
    l_acc = lamb()
    l_acc._rules = rules([
        ([pp(a1), pp(_zero())], e(a1)),
        ([pp(a2), _p(o1)     ], e(mu(l_acc, [p_(a2), o1])))])
    l_acc._name = "plus/a"

    l = lamb()
    l._rules = rules([
        ([pp(_zero()), pp(_zero()) ], e(_zero())),
        ([pp(x1)     , pp(_zero()) ], e(x1)),
        ([pp(_zero()), pp(x2)      ], e(x2)),
        ([pp(x3)     , pp(y)       ], e(mu(l_acc, [x3, y])))])
    l._name = "plus"
    return l
コード例 #12
0
ファイル: peano.py プロジェクト: shiplift/theseus
def make_plus():
    x1 = Variable("x")
    x2 = Variable("x")
    x3 = Variable("x")
    y = Variable("y")

    l = lamb()
    l._rules = rules([
        ([pp(_zero()), pp(_zero())], e(_zero())),
        ([pp(x1)     , pp(_zero())], e(x1)),
        ([pp(_zero()), pp(x2)     ], e(x2)),
        ([pp(x3)     , _p(y)      ], e(p_(mu(l, [x3, y]))))])
    l._name = "plus"
    return l
コード例 #13
0
ファイル: peano.py プロジェクト: shiplift/theseus
def make_mult():
    _1 = Variable("_")
    _2 = Variable("_")
    x = Variable("x")
    y = Variable("y")

    l = lamb()
    l._rules = rules([
        ([pp(_1)     , pp(_zero()) ], e(_zero())),
        ([pp(_zero()), pp(_2)      ], e(_zero())),
        # ([pp(x)      , _p(y)       ], e(mu(_plus(),[]), mu(l, [x, y])), x)))
        ([pp(x)      , _p(y)       ], e(mu(_plus(),[x, mu(l, [x, y])])))
    ])
    l._name = "mult"
    return l
コード例 #14
0
ファイル: peano.py プロジェクト: shiplift/theseus
def make_mult_acc():

    _f1 = Variable("_")
    _f2 = Variable("_")
    a1 = Variable("accumulator")
    a2 = Variable("accumulator")
    a3 = Variable("accumulator")
    a4 = Variable("accumulator")

    f1 = Variable("factor1")
    f2 = Variable("factor2")
    l_acc = lamb()
    l_acc._rules = rules([
        ([pp(_zero()), pp(_zero()), pp(a1)], e(a1)),
        ([pp(_f1)    , pp(_zero()), pp(a2)], e(a2)),
        ([pp(_zero()), pp(_f2)    , pp(a3)], e(a3)),
        ([pp(f1)     , _p(f2)     , pp(a4)],
         e(mu(l_acc, [f1, f2, mu(_plus_acc(), [a4, f1])])))
    ])
    l_acc._name = "mult/a"

    _1 = Variable("_")
    _2 = Variable("_")
    x = Variable("x")
    y = Variable("y")

    l = lamb()
    l._rules = rules([
        ([pp(_1)     , pp(_zero())], e(_zero())),
        ([pp(_zero()), pp(_2)     ], e(_zero())),
        ([pp(x)      , pp(y)      ], e(mu(l_acc, [x, y, _zero()]))),
        # ([pp(x)      , _p(y)      ], e(mu(_plus(),[]), mu(l, [x, y])), x)))
        # ([pp(x)      , _p(y)      ], e(mu(_plus(),[]), x, mu(l, [x, y])))))
    ])
    l._name = "mult"
    return l
コード例 #15
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
コード例 #16
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
コード例 #17
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)]
コード例 #18
0
 def test_lambda_id(self):
     x = Variable("x")
     l = lamb(([x], x))
     w_int = integer(1)
     res = interpret_expression(mu(l, [w_int]))
     assert res is w_int
コード例 #19
0
ファイル: peano.py プロジェクト: shiplift/theseus
def make_succ():
    x = Variable("x")
    l = lamb()
    l._rules= rules( [([pp(x)], e(p_(x)) )] )
    l._name = "succ"
    return l
コード例 #20
0
 def test_simple_lambda(self):
     w_int = integer(1)
     l = lamb(([], w_int))
     assert l.call([]) is w_int
コード例 #21
0
 def test_lambda_id(self):
     x = Variable("x")
     l = lamb(([x], x))
     w_int = integer(1)
     assert l.call([w_int]) is w_int
コード例 #22
0
 def test_simple_lambda(self):
     w_int = integer(1)
     l = lamb(([], w_int))
     res = interpret_expression(mu(l, []))
     assert res is w_int
コード例 #23
0
ファイル: test_shape.py プロジェクト: shiplift/theseus
    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
コード例 #24
0
def make_gc_bench():
    w_tStart_v0 = Variable("tStart")
    w_tStart_v1 = Variable("tStart")
    w_tStart_v2 = Variable("tStart")
    w_tFinish_v0 = Variable("tFinish")
    w_root_v0 = Variable("root")
    w_root_v1 = Variable("root")
    w_kStretchTreeDepth_v0 = Variable("kStretchTreeDepth")
    w_iDepth_v0 = Variable("iDepth")

    w_Make_Tree = lamb()
    w_Make_Tree._name = "MakeTree"
    w_Make_Tree._rules = rules([
        ([p(integer(0))], e(cons("Node", nil(), nil()))),
        ([p(w_iDepth_v0)],
         e(
             cons("Node",
                  mu(w_Make_Tree,
                     [mu(_minus(), [w_iDepth_v0, integer(1)])]),
                  mu(w_Make_Tree,
                     [mu(_minus(), [w_iDepth_v0, integer(1)])]))))
    ])

    w_gc_bench_cont1 = lamb()
    w_gc_bench_cont1._name = "gc_bench$cont0"
    w_gc_bench_cont1._rules = rules([(
        [p(w_tStart_v2), p(w_tFinish_v0),
         p(w_root_v1)],  # we 'un-reference'
        e(mu(_print_int(), [mu(_minus(), [w_tFinish_v0, w_tStart_v2])])))])

    w_gc_bench_cont0 = lamb()
    w_gc_bench_cont0._name = "gc_bench$cont0"
    w_gc_bench_cont0._rules = rules([([p(w_tStart_v1),
                                       p(w_root_v0)],
                                      e(
                                          mu(w_gc_bench_cont1, [
                                              w_tStart_v1,
                                              mu(_currentmilliseconds(), []),
                                              w_root_v0,
                                          ])))])

    w_gc_bench_let0 = lamb()
    w_gc_bench_let0._name = "gc_bench$let0"
    w_gc_bench_let0._rules = rules([
        ([p(w_kStretchTreeDepth_v0), p(w_tStart_v0)],
         e(
             mu(w_gc_bench_cont0,
                [w_tStart_v0,
                 mu(w_Make_Tree, [w_kStretchTreeDepth_v0])])))
    ])

    w_gc_bench = lamb()
    w_gc_bench._name = "gc_bench"
    w_gc_bench._rules = rules([(
        [],
        e(
            mu(
                w_gc_bench_let0,
                [
                    integer(18),  # kStretchTreeDepth
                    mu(_currentmilliseconds(), []),  # tStart
                ])))])
    return w_gc_bench