Example #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
Example #2
0
def make_pred():
    x = Variable("x")
    l = lamb()
    l._rules = rules([
        ([_p(_zero())], e(_zero())),
        ([pp(x)      ], e(x))])
    l._name = "pred"
    return l
Example #3
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
Example #4
0
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
Example #5
0
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
Example #6
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
Example #7
0
    def test_lambda_solo(self, parser):
        t = parser.parse("""λ. ↦ 1""")
        # lambdas cannot compare equal meaningfully
        assert t[0]._rules == [
            expression.Rule(patterns=[], expression=model.w_integer(1))
        ]

        t = parser.parse("""λ. x ↦ x""")
        # lambdas cannot compare equal meaningfully
        x = Variable("x")
        assert t[0]._rules == [
            expression.Rule(patterns=[p(x)], expression=e(x))
        ]
        assert t[0]._rules[0]._patterns[0].variable is \
               t[0]._rules[0]._expression.variable
Example #8
0
    def test_lambda_simple_pattern(self, parser):
        t = parser.parse("""λ. 1. 1 ↦ 5""")
        # lambdas cannot compare equal meaningfully
        assert t[0]._rules == [
            expression.Rule(patterns=[p(model.w_integer(1))],
                            expression=e(model.w_integer(5)))
        ]

        # should work, but meaningless
        t = parser.parse("""λ.
        1. 1 ↦ 4
        2. 2 ↦ 12345
        """)
        assert t[0]._rules == [
            expression.Rule(patterns=[p(model.w_integer(1))],
                            expression=e(model.w_integer(4))),
            expression.Rule(patterns=[p(model.w_integer(2))],
                            expression=e(model.w_integer(12345)))
        ]

        t = parser.parse("""λ. 1. x ↦ x""")
        # lambdas cannot compare equal meaningfully
        x = Variable("x")
        assert t[0]._rules == [
            expression.Rule(patterns=[p(x)], expression=e(x))
        ]
        assert t[0]._rules[0]._patterns[0].variable is \
               t[0]._rules[0]._expression.variable

        # should work, but meaningless
        t = parser.parse("""λ.
        1. x ↦ x
        2. x ↦ 4
        """)
        x1 = Variable("x")
        x2 = Variable("x")
        assert t[0]._rules == [
            expression.Rule(patterns=[p(x1)], expression=e(x1)),
            expression.Rule(patterns=[p(x2)], expression=e(model.w_integer(4)))
        ]
        assert t[0]._rules[0]._patterns[0].variable.name == \
               t[0]._rules[1]._patterns[0].variable.name
        assert t[0]._rules[0]._patterns[0].variable is not \
               t[0]._rules[1]._patterns[0].variable
Example #9
0
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
Example #10
0
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
Example #11
0
def make_succ():
    x = Variable("x")
    l = lamb()
    l._rules= rules( [([pp(x)], e(p_(x)) )] )
    l._name = "succ"
    return l
Example #12
0
def p_(x):
    from theseus.model import w_constructor
    return w_constructor(tag("p", 1), [e(x)])
Example #13
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